A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 126 lines 4.1 kB view raw
1/* Bitwise operations library */ 2/* (c) Reuben Thomas 2000-2008 */ 3/* bitlib is copyright Reuben Thomas 2000-2008, and is released under the MIT 4 license, like Lua (see http://www.lua.org/copyright.html; it's 5 basically the same as the BSD license). There is no warranty. */ 6 7#include "config.h" 8 9#include "lua.h" 10#include "lauxlib.h" 11#include "lualib.h" 12#include <limits.h> 13 14 15/* FIXME: Assume lua_Integer is ptrdiff_t */ 16#define LUA_INTEGER_MAX INTPTR_MAX 17#define LUA_INTEGER_MIN INTPTR_MIN 18 19/* FIXME: Assume size_t is an unsigned lua_Integer */ 20typedef size_t lua_UInteger; 21#define LUA_UINTEGER_MAX UINT_MAX 22 23 24/* Bit type size and limits */ 25 26#define BIT_BITS (CHAR_BIT * sizeof(lua_Integer)) 27 28/* This code may give warnings if BITLIB_FLOAT_* are too big to fit in 29 long, but that doesn't matter since in that case they won't be 30 used. */ 31#define BIT_MAX (LUA_INTEGER_MAX) 32 33#define BIT_MIN (LUA_INTEGER_MIN) 34 35#define BIT_UMAX (LUA_UINTEGER_MAX) 36 37 38/* Define TOBIT to get a bit value */ 39#ifdef BUILTIN_CAST 40#define 41#define TOBIT(L, n, res) \ 42 ((void)(res), luaL_checkinteger((L), (n))) 43#else 44 45#define TOBIT(L, n, res) \ 46 ((lua_Integer)(((res) = luaL_checknumber(L, (n)) % BIT_UMAX), \ 47 (res) > BIT_MAX ? ((res) -= BIT_UMAX, (res) -= 1) : \ 48 ((res) < BIT_MIN ? ((res) += BIT_UMAX, (res) += 1) : (res)))) 49#endif 50 51 52#define BIT_TRUNCATE(i) \ 53 ((i) & BIT_UMAX) 54 55 56/* Operations 57 58 The macros MONADIC and VARIADIC only deal with bitwise operations. 59 60 LOGICAL_SHIFT truncates its left-hand operand before shifting so 61 that any extra bits at the most-significant end are not shifted 62 into the result. 63 64 ARITHMETIC_SHIFT does not truncate its left-hand operand, so that 65 the sign bits are not removed and right shift work properly. 66 */ 67 68#define MONADIC(name, op) \ 69 static int bit_ ## name(lua_State *L) { \ 70 lua_Number f; \ 71 lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \ 72 return 1; \ 73 } 74 75#define VARIADIC(name, op) \ 76 static int bit_ ## name(lua_State *L) { \ 77 lua_Number f; \ 78 int n = lua_gettop(L), i; \ 79 lua_Integer w = TOBIT(L, 1, f); \ 80 for (i = 2; i <= n; i++) \ 81 w op TOBIT(L, i, f); \ 82 lua_pushinteger(L, BIT_TRUNCATE(w)); \ 83 return 1; \ 84 } 85 86#define LOGICAL_SHIFT(name, op) \ 87 static int bit_ ## name(lua_State *L) { \ 88 lua_Number f; \ 89 lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \ 90 (unsigned)luaL_checknumber(L, 2))); \ 91 return 1; \ 92 } 93 94#define ARITHMETIC_SHIFT(name, op) \ 95 static int bit_ ## name(lua_State *L) { \ 96 lua_Number f; \ 97 lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \ 98 (unsigned)luaL_checknumber(L, 2))); \ 99 return 1; \ 100 } 101 102MONADIC(bnot, ~) 103VARIADIC(band, &=) 104VARIADIC(bor, |=) 105VARIADIC(bxor, ^=) 106ARITHMETIC_SHIFT(lshift, <<) 107LOGICAL_SHIFT(rshift, >>) 108ARITHMETIC_SHIFT(arshift, >>) 109 110static const struct luaL_reg bitlib[] = { 111 {"bnot", bit_bnot}, 112 {"band", bit_band}, 113 {"bor", bit_bor}, 114 {"bxor", bit_bxor}, 115 {"lshift", bit_lshift}, 116 {"rshift", bit_rshift}, 117 {"arshift", bit_arshift}, 118 {NULL, NULL} 119}; 120 121LUALIB_API int luaopen_bit (lua_State *L) { 122 luaL_register(L, "bit", bitlib); 123 lua_pushnumber(L, BIT_BITS); 124 lua_setfield(L, -2, "bits"); 125 return 1; 126}