410-bitlib_25-embedded.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. Index: lua-5.1.4/src/Makefile
  2. ===================================================================
  3. --- lua-5.1.4.orig/src/Makefile 2008-09-25 13:08:31.000000000 +0200
  4. +++ lua-5.1.4/src/Makefile 2008-09-25 13:08:43.000000000 +0200
  5. @@ -29,7 +29,7 @@
  6. lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
  7. lundump.o lvm.o lzio.o lnum.o
  8. LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
  9. - lstrlib.o loadlib.o linit.o lposix.o
  10. + lstrlib.o loadlib.o linit.o lposix.o lbitlib.o
  11. LUA_T= lua
  12. LUA_O= lua.o
  13. Index: lua-5.1.4/src/bit_limits.h
  14. ===================================================================
  15. --- /dev/null 1970-01-01 00:00:00.000000000 +0000
  16. +++ lua-5.1.4/src/bit_limits.h 2008-09-25 13:09:16.000000000 +0200
  17. @@ -0,0 +1,4 @@
  18. +#define BITLIB_FLOAT_BITS 53
  19. +#define BITLIB_FLOAT_MAX 0xfffffffffffffL
  20. +#define BITLIB_FLOAT_MIN (-0x10000000000000L)
  21. +#define BITLIB_FLOAT_UMAX 0x1fffffffffffffUL
  22. Index: lua-5.1.4/src/lbitlib.c
  23. ===================================================================
  24. --- /dev/null 1970-01-01 00:00:00.000000000 +0000
  25. +++ lua-5.1.4/src/lbitlib.c 2008-09-25 13:05:15.000000000 +0200
  26. @@ -0,0 +1,133 @@
  27. +/* Bitwise operations library */
  28. +/* (c) Reuben Thomas 2000-2008 */
  29. +/* See README for license */
  30. +
  31. +#include "lua.h"
  32. +#include "lauxlib.h"
  33. +#include "limits.h"
  34. +
  35. +#include "bit_limits.h"
  36. +
  37. +
  38. +/* FIXME: Assume lua_Integer is int */
  39. +#define LUA_INTEGER_MAX INT_MAX
  40. +#define LUA_INTEGER_MIN INT_MIN
  41. +
  42. +/* FIXME: Assume uint is an unsigned lua_Integer */
  43. +typedef unsigned int lua_UInteger;
  44. +#define LUA_UINTEGER_MAX UINT_MAX
  45. +
  46. +
  47. +/* Bit type size and limits */
  48. +
  49. +#define BIT_BITS \
  50. + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
  51. + BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
  52. +
  53. +/* This code may give warnings if BITLIB_FLOAT_* are too big to fit in
  54. + long, but that doesn't matter since in that case they won't be
  55. + used. */
  56. +#define BIT_MAX \
  57. + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
  58. +
  59. +#define BIT_MIN \
  60. + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
  61. +
  62. +#define BIT_UMAX \
  63. + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
  64. +
  65. +
  66. +/* Define TOBIT to get a bit value */
  67. +#ifdef BUILTIN_CAST
  68. +#define
  69. +#define TOBIT(L, n, res) \
  70. + ((void)(res), luaL_checkinteger((L), (n)))
  71. +#else
  72. +#include <stdint.h>
  73. +#include <math.h>
  74. +
  75. +/* FIXME: Assume lua_Number fits in a double (use of fmod). */
  76. +#define TOBIT(L, n, res) \
  77. + ((lua_Integer)(((res) = fmod(luaL_checknumber(L, (n)), (double)BIT_UMAX + 1.0)), \
  78. + (res) > BIT_MAX ? ((res) -= (double)BIT_UMAX, (res) -= 1) : \
  79. + ((res) < BIT_MIN ? ((res) += (double)BIT_UMAX, (res) += 1) : (res))))
  80. +#endif
  81. +
  82. +
  83. +#define BIT_TRUNCATE(i) \
  84. + ((i) & BIT_UMAX)
  85. +
  86. +
  87. +/* Operations
  88. +
  89. + The macros MONADIC and VARIADIC only deal with bitwise operations.
  90. +
  91. + LOGICAL_SHIFT truncates its left-hand operand before shifting so
  92. + that any extra bits at the most-significant end are not shifted
  93. + into the result.
  94. +
  95. + ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
  96. + the sign bits are not removed and right shift work properly.
  97. + */
  98. +
  99. +#define MONADIC(name, op) \
  100. + static int bit_ ## name(lua_State *L) { \
  101. + lua_Number f; \
  102. + lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
  103. + return 1; \
  104. + }
  105. +
  106. +#define VARIADIC(name, op) \
  107. + static int bit_ ## name(lua_State *L) { \
  108. + lua_Number f; \
  109. + int n = lua_gettop(L), i; \
  110. + lua_Integer w = TOBIT(L, 1, f); \
  111. + for (i = 2; i <= n; i++) \
  112. + w op TOBIT(L, i, f); \
  113. + lua_pushinteger(L, BIT_TRUNCATE(w)); \
  114. + return 1; \
  115. + }
  116. +
  117. +#define LOGICAL_SHIFT(name, op) \
  118. + static int bit_ ## name(lua_State *L) { \
  119. + lua_Number f; \
  120. + lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
  121. + (unsigned)luaL_checknumber(L, 2))); \
  122. + return 1; \
  123. + }
  124. +
  125. +#define ARITHMETIC_SHIFT(name, op) \
  126. + static int bit_ ## name(lua_State *L) { \
  127. + lua_Number f; \
  128. + lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
  129. + (unsigned)luaL_checknumber(L, 2))); \
  130. + return 1; \
  131. + }
  132. +
  133. +MONADIC(cast, +)
  134. +MONADIC(bnot, ~)
  135. +VARIADIC(band, &=)
  136. +VARIADIC(bor, |=)
  137. +VARIADIC(bxor, ^=)
  138. +ARITHMETIC_SHIFT(lshift, <<)
  139. +LOGICAL_SHIFT(rshift, >>)
  140. +ARITHMETIC_SHIFT(arshift, >>)
  141. +
  142. +static const struct luaL_reg bitlib[] = {
  143. + {"cast", bit_cast},
  144. + {"bnot", bit_bnot},
  145. + {"band", bit_band},
  146. + {"bor", bit_bor},
  147. + {"bxor", bit_bxor},
  148. + {"lshift", bit_lshift},
  149. + {"rshift", bit_rshift},
  150. + {"arshift", bit_arshift},
  151. + {NULL, NULL}
  152. +};
  153. +
  154. +LUALIB_API int luaopen_bit (lua_State *L) {
  155. + luaL_register(L, "bit", bitlib);
  156. + lua_pushnumber(L, BIT_BITS);
  157. + lua_setfield(L, -2, "bits");
  158. + return 1;
  159. +}
  160. Index: lua-5.1.4/src/linit.c
  161. ===================================================================
  162. --- lua-5.1.4.orig/src/linit.c 2008-09-25 13:08:11.000000000 +0200
  163. +++ lua-5.1.4/src/linit.c 2008-09-25 13:08:27.000000000 +0200
  164. @@ -24,6 +24,7 @@
  165. {LUA_MATHLIBNAME, luaopen_math},
  166. {LUA_DBLIBNAME, luaopen_debug},
  167. {LUA_POSIXLIBNAME, luaopen_posix},
  168. + {LUA_BITLIBNAME, luaopen_bit},
  169. {NULL, NULL}
  170. };
  171. Index: lua-5.1.4/src/lualib.h
  172. ===================================================================
  173. --- lua-5.1.4.orig/src/lualib.h 2008-09-25 13:07:29.000000000 +0200
  174. +++ lua-5.1.4/src/lualib.h 2008-09-25 13:08:06.000000000 +0200
  175. @@ -42,6 +42,9 @@
  176. #define LUA_POSIXLIBNAME "posix"
  177. LUALIB_API int (luaopen_posix) (lua_State *L);
  178. +#define LUA_BITLIBNAME "bit"
  179. +LUALIB_API int (luaopen_bit) (lua_State *L);
  180. +
  181. /* open all previous libraries */
  182. LUALIB_API void (luaL_openlibs) (lua_State *L);