mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Add normal implementation of bit32.byteswap
This commit is contained in:
parent
61afc5e0dd
commit
e34b0160f1
3 changed files with 20 additions and 0 deletions
|
@ -21,6 +21,7 @@ declare bit32: {
|
|||
replace: (n: number, v: number, field: number, width: number?) -> number,
|
||||
countlz: (n: number) -> number,
|
||||
countrz: (n: number) -> number,
|
||||
byteswap: (n: number) -> number,
|
||||
}
|
||||
|
||||
declare math: {
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include "lcommon.h"
|
||||
#include "lnumutils.h"
|
||||
|
||||
LUAU_FASTFLAG(LuauBit32Byteswap)
|
||||
|
||||
#define ALLONES ~0u
|
||||
#define NBITS int(8 * sizeof(unsigned))
|
||||
|
||||
|
@ -210,6 +212,16 @@ static int b_countrz(lua_State* L)
|
|||
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[] = {
|
||||
{"arshift", b_arshift},
|
||||
{"band", b_and},
|
||||
|
@ -225,6 +237,7 @@ static const luaL_Reg bitlib[] = {
|
|||
{"rshift", b_rshift},
|
||||
{"countlz", b_countlz},
|
||||
{"countrz", b_countrz},
|
||||
{"byteswap", b_swap},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
||||
```
|
||||
function bit32.byteswap(n: number): number
|
||||
```
|
||||
|
||||
Returns `n` with the order of the bytes swappped.
|
||||
|
||||
## 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.
|
||||
|
|
Loading…
Add table
Reference in a new issue