Add normal implementation of bit32.byteswap

This commit is contained in:
Dekkonot 2023-10-16 17:34:40 -07:00
parent 61afc5e0dd
commit e34b0160f1
No known key found for this signature in database
3 changed files with 20 additions and 0 deletions

View file

@ -21,6 +21,7 @@ declare bit32: {
replace: (n: number, v: number, field: number, width: number?) -> number, replace: (n: number, v: number, field: number, width: number?) -> number,
countlz: (n: number) -> number, countlz: (n: number) -> number,
countrz: (n: number) -> number, countrz: (n: number) -> number,
byteswap: (n: number) -> number,
} }
declare math: { declare math: {

View file

@ -5,6 +5,8 @@
#include "lcommon.h" #include "lcommon.h"
#include "lnumutils.h" #include "lnumutils.h"
LUAU_FASTFLAG(LuauBit32Byteswap)
#define ALLONES ~0u #define ALLONES ~0u
#define NBITS int(8 * sizeof(unsigned)) #define NBITS int(8 * sizeof(unsigned))
@ -210,6 +212,16 @@ static int b_countrz(lua_State* L)
return 1; return 1;
} }
static int b_swap(lua_State* L)
{
LUAU_ASSERT(FFlag::LuauBit32Byteswap);
b_uint n = luaL_checkunsigned(L, 1);
n = ((n >> 24) & 0xff) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | ((n << 24) & 0xff000000);
lua_pushunsigned(L, n);
return 1;
}
static const luaL_Reg bitlib[] = { static const luaL_Reg bitlib[] = {
{"arshift", b_arshift}, {"arshift", b_arshift},
{"band", b_and}, {"band", b_and},
@ -225,6 +237,7 @@ static const luaL_Reg bitlib[] = {
{"rshift", b_rshift}, {"rshift", b_rshift},
{"countlz", b_countlz}, {"countlz", b_countlz},
{"countrz", b_countrz}, {"countrz", b_countrz},
{"byteswap", b_swap},
{NULL, NULL}, {NULL, NULL},
}; };

View file

@ -728,6 +728,12 @@ function bit32.countrz(n: number): number
Returns the number of consecutive zero bits in the 32-bit representation of `n` starting from the right-most (least significant) bit. Returns 32 if `n` is zero. Returns the number of consecutive zero bits in the 32-bit representation of `n` starting from the right-most (least significant) bit. Returns 32 if `n` is zero.
```
function bit32.byteswap(n: number): number
```
Returns `n` with the order of the bytes swappped.
## utf8 library ## utf8 library
Strings in Luau can contain arbitrary bytes; however, in many applications strings representing text contain UTF8 encoded data by convention, that can be inspected and manipulated using `utf8` library. Strings in Luau can contain arbitrary bytes; however, in many applications strings representing text contain UTF8 encoded data by convention, that can be inspected and manipulated using `utf8` library.