robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3f416ef524
master
1/* 2** Lua BitOp -- a bit operations library for Lua 5.1/5.2. 3** http://bitop.luajit.org/ 4** 5** Copyright (C) 2008-2025 Mike Pall. All rights reserved. 6** 7** Permission is hereby granted, free of charge, to any person obtaining 8** a copy of this software and associated documentation files (the 9** "Software"), to deal in the Software without restriction, including 10** without limitation the rights to use, copy, modify, merge, publish, 11** distribute, sublicense, and/or sell copies of the Software, and to 12** permit persons to whom the Software is furnished to do so, subject to 13** the following conditions: 14** 15** The above copyright notice and this permission notice shall be 16** included in all copies or substantial portions of the Software. 17** 18** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25** 26** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] 27*/ 28 29#define LUA_BITOP_VERSION "1.0.3" 30 31#define LUA_LIB 32#include "lua.h" 33#include "lauxlib.h" 34 35#include <stdint.h> 36 37typedef int32_t SBits ; 38typedef uint32_t UBits ; 39 40typedef union { 41lua_Number n ; 42#ifdef LUA_NUMBER_DOUBLE 43uint64_t b ; 44#else 45UBits b ; 46#endif 47}BitNum ; 48 49/* Convert argument to bit type. */ 50static UBits barg (lua_State * L ,int idx ) 51{ 52BitNum bn ; 53UBits b ; 54#if LUA_VERSION_NUM < 502 55bn .n = lua_tonumber (L ,idx ); 56#else 57bn .n = luaL_checknumber (L ,idx ); 58#endif 59#if defined(LUA_NUMBER_DOUBLE ) 60bn .n += 6755399441055744.0 ;/* 2^52+2^51 */ 61#ifdef SWAPPED_DOUBLE 62b = (UBits )(bn .b >>32 ); 63#else 64b = (UBits )bn .b ; 65#endif 66#elif defined(LUA_NUMBER_INT )|| defined(LUA_NUMBER_LONG )|| \ 67 defined(LUA_NUMBER_LONGLONG )|| defined(LUA_NUMBER_LONG_LONG )|| \ 68 defined(LUA_NUMBER_LLONG ) 69if (sizeof (UBits )== sizeof (lua_Number )) 70b = bn .b ; 71else 72b = (UBits )(SBits )bn .n ; 73#elif defined(LUA_NUMBER_FLOAT ) 74#error "A 'float' lua_Number type is incompatible with this library" 75#else 76#error "Unknown number type, check LUA_NUMBER_* in luaconf.h" 77#endif 78#if LUA_VERSION_NUM < 502 79if (b == 0 && !lua_isnumber (L ,idx )) { 80luaL_typerror (L ,idx ,"number" ); 81 } 82#endif 83return b ; 84} 85 86/* Return bit type. */ 87#define BRET (b ) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; 88 89static int bit_tobit (lua_State * L ) {BRET (barg (L ,1 )) } 90static int bit_bnot (lua_State * L ) {BRET (~barg (L ,1 )) } 91 92#define BIT_OP (func ,opr ) \ 93 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \ 94 for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) } 95BIT_OP (bit_band ,&= ) 96BIT_OP (bit_bor , |=) 97BIT_OP (bit_bxor , ^=) 98 99#define bshl (b ,n ) (b <<n ) 100#define bshr (b ,n ) (b >>n ) 101#define bsar (b ,n ) ((SBits )b >>n ) 102#define brol (b ,n ) ((b <<n ) | (b >> (32 - n ))) 103#define bror (b ,n ) ((b << (32 - n )) | (b >>n )) 104#define BIT_SH (func ,fn ) \ 105static int func (lua_State * L ) { \ 106UBits b = barg (L ,1 );UBits n = barg (L ,2 )& 31 ;BRET (fn (b ,n )) } 107BIT_SH (bit_lshift ,bshl ) 108BIT_SH (bit_rshift ,bshr ) 109BIT_SH (bit_arshift ,bsar ) 110BIT_SH (bit_rol ,brol ) 111BIT_SH (bit_ror ,bror ) 112 113static int bit_bswap (lua_State * L ) 114{ 115UBits b = barg (L ,1 ); 116b = (b >>24 ) | ((b >>8 )& 0xff00 ) | ((b & 0xff00 ) <<8 ) | (b <<24 ); 117BRET (b ) 118} 119 120static int bit_tohex (lua_State * L ) 121{ 122UBits b = barg (L ,1 ); 123UBits n = lua_isnone (L ,2 ) ?8 :barg (L ,2 ); 124const char * hexdigits = "0123456789abcdef" ; 125char buf [8 ]; 126int i ; 127if ((SBits )n < 0 ) {n = ~n + 1 ;hexdigits = "0123456789ABCDEF" ; } 128if (n > 8 )n = 8 ; 129for (i = (int )n ;-- i >=0 ; ) {buf [i ]= hexdigits [b & 15 ];b >>=4 ; } 130lua_pushlstring (L ,buf , (size_t )n ); 131return 1 ; 132} 133 134static const struct luaL_Reg bit_funcs []= { 135 {"tobit" ,bit_tobit }, 136 {"bnot" ,bit_bnot }, 137 {"band" ,bit_band }, 138 {"bor" ,bit_bor }, 139 {"bxor" ,bit_bxor }, 140 {"lshift" ,bit_lshift }, 141 {"rshift" ,bit_rshift }, 142 {"arshift" ,bit_arshift }, 143 {"rol" ,bit_rol }, 144 {"ror" ,bit_ror }, 145 {"bswap" ,bit_bswap }, 146 {"tohex" ,bit_tohex }, 147 {NULL ,NULL } 148}; 149 150/* Signed right-shifts are implementation-defined per C89/C99. 151** But the de facto standard are arithmetic right-shifts on two's 152** complement CPUs. This behaviour is required here, so test for it. 153*/ 154#define BAD_SAR (bsar(-8, 2) != (SBits)-2) 155 156LUALIB_API int luaopen_bit (lua_State * L ) 157{ 158UBits b ; 159lua_pushnumber (L , (lua_Number )1437217655L ); 160b = barg (L ,-1 ); 161if (b != (UBits )1437217655L || BAD_SAR ) {/* Perform a simple self-test. */ 162const char * msg = "compiled with incompatible luaconf.h" ; 163#ifdef LUA_NUMBER_DOUBLE 164#ifdef _WIN32 165if (b == (UBits )1610612736L ) 166msg = "use D3DCREATE_FPU_PRESERVE with DirectX" ; 167#endif 168if (b == (UBits )1127743488L ) 169msg = "not compiled with SWAPPED_DOUBLE" ; 170#endif 171if (BAD_SAR ) 172msg = "arithmetic right-shift broken" ; 173luaL_error (L ,"bit library self-test failed (%s)" ,msg ); 174 } 175#if LUA_VERSION_NUM < 502 176luaL_register (L ,"bit" ,bit_funcs ); 177#else 178luaL_newlib (L ,bit_funcs ); 179#endif 180return 1 ; 181} 182