Add type conversion functions (#839)
Some checks failed
Build coop / build-linux (push) Has been cancelled
Build coop / build-steamos (push) Has been cancelled
Build coop / build-windows-opengl (push) Has been cancelled
Build coop / build-windows-directx (push) Has been cancelled
Build coop / build-macos-arm (push) Has been cancelled
Build coop / build-macos-intel (push) Has been cancelled

This commit is contained in:
Sunk 2025-06-09 12:35:57 -04:00 committed by GitHub
parent 4d942eadad
commit 0ecaaf767a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 153 additions and 0 deletions

View file

@ -249,4 +249,56 @@ end
--- Rounds `x` to the nearest integer value
function math.round(x)
return x > 0 and __math_floor(x + 0.5) or __math_ceil(x - 0.5)
end
local __common_signed_conversion = function (x, size)
x = __math_floor(x) & (1 << size) - 1
return x - ((x & (1 << (size - 1))) << 1)
end
local __common_unsigned_conversion = function (x, size)
return __math_floor(x) & (1 << size) - 1
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `s8` range
--- - `[-128, 127]`
function math.s8(x)
return __common_signed_conversion(x, 8)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `s16` range
--- - `[-32768, 32767]`
function math.s16(x)
return __common_signed_conversion(x, 16)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `s32` range
--- - `[-2147483648, 2147483647]`
function math.s32(x)
return __common_signed_conversion(x, 32)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `u8` range
--- - `[0, 255]`
function math.u8(x)
return __common_unsigned_conversion(x, 8)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `u16` range
--- - `[0, 65535]`
function math.u16(x)
return __common_unsigned_conversion(x, 16)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `u32` range
--- - `[0, 4294967295]`
function math.u32(x)
return __common_unsigned_conversion(x, 32)
end

View file

@ -253,6 +253,58 @@ function math.round(x)
return x > 0 and __math_floor(x + 0.5) or __math_ceil(x - 0.5)
end
local __common_signed_conversion = function (x, size)
x = __math_floor(x) & (1 << size) - 1
return x - ((x & (1 << (size - 1))) << 1)
end
local __common_unsigned_conversion = function (x, size)
return __math_floor(x) & (1 << size) - 1
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `s8` range
--- - `[-128, 127]`
function math.s8(x)
return __common_signed_conversion(x, 8)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `s16` range
--- - `[-32768, 32767]`
function math.s16(x)
return __common_signed_conversion(x, 16)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `s32` range
--- - `[-2147483648, 2147483647]`
function math.s32(x)
return __common_signed_conversion(x, 32)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `u8` range
--- - `[0, 255]`
function math.u8(x)
return __common_unsigned_conversion(x, 8)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `u16` range
--- - `[0, 65535]`
function math.u16(x)
return __common_unsigned_conversion(x, 16)
end
--- @param x number
--- @return integer
--- Converts `x` into a valid `u32` range
--- - `[0, 4294967295]`
function math.u32(x)
return __common_unsigned_conversion(x, 32)
end
-------------------------
-- vec types constants --

View file

@ -224,6 +224,55 @@ char gSmluaConstants[] = ""
"function math.round(x)\n"
"return x > 0 and __math_floor(x + 0.5) or __math_ceil(x - 0.5)\n"
"end\n"
"local __common_signed_conversion = function (x, size)\n"
"x = __math_floor(x) & (1 << size) - 1\n"
"return x - ((x & (1 << (size - 1))) << 1)\n"
"end\n"
"local __common_unsigned_conversion = function (x, size)\n"
"return __math_floor(x) & (1 << size) - 1\n"
"end\n"
"--- @param x number\n"
"--- @return integer\n"
"--- Converts `x` into a valid `s8` range\n"
"--- - `[-128, 127]`\n"
"function math.s8(x)\n"
"return __common_signed_conversion(x, 8)\n"
"end\n"
"--- @param x number\n"
"--- @return integer\n"
"--- Converts `x` into a valid `s16` range\n"
"--- - `[-32768, 32767]`\n"
"function math.s16(x)\n"
"return __common_signed_conversion(x, 16)\n"
"end\n"
"--- @param x number\n"
"--- @return integer\n"
"--- Converts `x` into a valid `s32` range\n"
"--- - `[-2147483648, 2147483647]`\n"
"function math.s32(x)\n"
"return __common_signed_conversion(x, 32)\n"
"end\n"
"--- @param x number\n"
"--- @return integer\n"
"--- Converts `x` into a valid `u8` range\n"
"--- - `[0, 255]`\n"
"function math.u8(x)\n"
"return __common_unsigned_conversion(x, 8)\n"
"end\n"
"--- @param x number\n"
"--- @return integer\n"
"--- Converts `x` into a valid `u16` range\n"
"--- - `[0, 65535]`\n"
"function math.u16(x)\n"
"return __common_unsigned_conversion(x, 16)\n"
"end\n"
"--- @param x number\n"
"--- @return integer\n"
"--- Converts `x` into a valid `u32` range\n"
"--- - `[0, 4294967295]`\n"
"function math.u32(x)\n"
"return __common_unsigned_conversion(x, 32)\n"
"end\n"
"--- @type integer\n"
"FONT_TINY = -1\n"
"--- @type integer\n"