├── LICENSE ├── Makefile ├── README.md ├── int64.c └── test.lua /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2013 codingow.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | install : 2 | gcc -g -Wall -fPIC --shared -o int64.so int64.c -I/usr/local/include 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A int64 library for lua 2 | 3 | In 64 bit architecture , we can use lightuserdata type for 64bit number operation. It's a simple way to support int64 in lua. 4 | 5 | This library only offer one api : create a int64 number , see test.lua . 6 | 7 | It can create a int64 number from lua number or a string (A little-endian 8 bytes string) . you can also push a lightuserdata as a int64 number from C library. 8 | -------------------------------------------------------------------------------- /int64.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static int64_t 9 | _int64(lua_State *L, int index) { 10 | int type = lua_type(L,index); 11 | int64_t n = 0; 12 | switch(type) { 13 | case LUA_TNUMBER: { 14 | lua_Number d = lua_tonumber(L,index); 15 | n = (int64_t)d; 16 | break; 17 | } 18 | case LUA_TSTRING: { 19 | size_t len = 0; 20 | const uint8_t * str = (const uint8_t *)lua_tolstring(L, index, &len); 21 | if (len>8) { 22 | return luaL_error(L, "The string (length = %d) is not an int64 string", len); 23 | } 24 | int i = 0; 25 | uint64_t n64 = 0; 26 | for (i=0;i<(int)len;i++) { 27 | n64 |= (uint64_t)str[i] << (i*8); 28 | } 29 | n = (int64_t)n64; 30 | break; 31 | } 32 | case LUA_TLIGHTUSERDATA: { 33 | void * p = lua_touserdata(L,index); 34 | n = (intptr_t)p; 35 | break; 36 | } 37 | default: 38 | return luaL_error(L, "argument %d error type %s", index, lua_typename(L,type)); 39 | } 40 | return n; 41 | } 42 | 43 | static inline void 44 | _pushint64(lua_State *L, int64_t n) { 45 | void * p = (void *)(intptr_t)n; 46 | lua_pushlightuserdata(L,p); 47 | } 48 | 49 | static int 50 | int64_add(lua_State *L) { 51 | int64_t a = _int64(L,1); 52 | int64_t b = _int64(L,2); 53 | _pushint64(L, a+b); 54 | 55 | return 1; 56 | } 57 | 58 | static int 59 | int64_sub(lua_State *L) { 60 | int64_t a = _int64(L,1); 61 | int64_t b = _int64(L,2); 62 | _pushint64(L, a-b); 63 | 64 | return 1; 65 | } 66 | 67 | static int 68 | int64_mul(lua_State *L) { 69 | int64_t a = _int64(L,1); 70 | int64_t b = _int64(L,2); 71 | _pushint64(L, a * b); 72 | 73 | return 1; 74 | } 75 | 76 | static int 77 | int64_div(lua_State *L) { 78 | int64_t a = _int64(L,1); 79 | int64_t b = _int64(L,2); 80 | if (b == 0) { 81 | return luaL_error(L, "div by zero"); 82 | } 83 | _pushint64(L, a / b); 84 | 85 | return 1; 86 | } 87 | 88 | static int 89 | int64_mod(lua_State *L) { 90 | int64_t a = _int64(L,1); 91 | int64_t b = _int64(L,2); 92 | if (b == 0) { 93 | return luaL_error(L, "mod by zero"); 94 | } 95 | _pushint64(L, a % b); 96 | 97 | return 1; 98 | } 99 | 100 | static int64_t 101 | _pow64(int64_t a, int64_t b) { 102 | if (b == 1) { 103 | return a; 104 | } 105 | int64_t a2 = a * a; 106 | if (b % 2 == 1) { 107 | return _pow64(a2, b/2) * a; 108 | } else { 109 | return _pow64(a2, b/2); 110 | } 111 | } 112 | 113 | static int 114 | int64_pow(lua_State *L) { 115 | int64_t a = _int64(L,1); 116 | int64_t b = _int64(L,2); 117 | int64_t p; 118 | if (b > 0) { 119 | p = _pow64(a,b); 120 | } else if (b == 0) { 121 | p = 1; 122 | } else { 123 | return luaL_error(L, "pow by nagtive number %d",(int)b); 124 | } 125 | _pushint64(L, p); 126 | 127 | return 1; 128 | } 129 | 130 | static int 131 | int64_unm(lua_State *L) { 132 | int64_t a = _int64(L,1); 133 | _pushint64(L, -a); 134 | return 1; 135 | } 136 | 137 | static int 138 | int64_new(lua_State *L) { 139 | int top = lua_gettop(L); 140 | int64_t n; 141 | switch(top) { 142 | case 0 : 143 | lua_pushlightuserdata(L,NULL); 144 | break; 145 | case 1 : 146 | n = _int64(L,1); 147 | _pushint64(L,n); 148 | break; 149 | default: { 150 | int base = luaL_checkinteger(L,2); 151 | if (base < 2) { 152 | luaL_error(L, "base must be >= 2"); 153 | } 154 | const char * str = luaL_checkstring(L, 1); 155 | n = strtoll(str, NULL, base); 156 | _pushint64(L,n); 157 | break; 158 | } 159 | } 160 | return 1; 161 | } 162 | 163 | static int 164 | int64_eq(lua_State *L) { 165 | int64_t a = _int64(L,1); 166 | int64_t b = _int64(L,2); 167 | printf("%s %s\n",lua_typename(L,1),lua_typename(L,2)); 168 | printf("%ld %ld\n",a,b); 169 | lua_pushboolean(L,a == b); 170 | return 1; 171 | } 172 | 173 | static int 174 | int64_lt(lua_State *L) { 175 | int64_t a = _int64(L,1); 176 | int64_t b = _int64(L,2); 177 | lua_pushboolean(L,a < b); 178 | return 1; 179 | } 180 | 181 | static int 182 | int64_le(lua_State *L) { 183 | int64_t a = _int64(L,1); 184 | int64_t b = _int64(L,2); 185 | lua_pushboolean(L,a <= b); 186 | return 1; 187 | } 188 | 189 | static int 190 | int64_len(lua_State *L) { 191 | int64_t a = _int64(L,1); 192 | lua_pushnumber(L,(lua_Number)a); 193 | return 1; 194 | } 195 | 196 | static int 197 | tostring(lua_State *L) { 198 | static char hex[16] = "0123456789ABCDEF"; 199 | uintptr_t n = (uintptr_t)lua_touserdata(L,1); 200 | if (lua_gettop(L) == 1) { 201 | luaL_Buffer b; 202 | luaL_buffinitsize(L , &b , 28); 203 | luaL_addstring(&b, "int64: 0x"); 204 | int i; 205 | bool strip = true; 206 | for (i=15;i>=0;i--) { 207 | int c = (n >> (i*4)) & 0xf; 208 | if (strip && c ==0) { 209 | continue; 210 | } 211 | strip = false; 212 | luaL_addchar(&b, hex[c]); 213 | } 214 | if (strip) { 215 | luaL_addchar(&b , '0'); 216 | } 217 | luaL_pushresult(&b); 218 | } else { 219 | int base = luaL_checkinteger(L,2); 220 | int shift , mask; 221 | switch(base) { 222 | case 0: { 223 | unsigned char buffer[8]; 224 | int i; 225 | for (i=0;i<8;i++) { 226 | buffer[i] = (n >> (i*8)) & 0xff; 227 | } 228 | lua_pushlstring(L,(const char *)buffer, 8); 229 | return 1; 230 | } 231 | case 10: { 232 | int64_t dec = (int64_t)n; 233 | luaL_Buffer b; 234 | luaL_buffinitsize(L , &b , 28); 235 | if (dec<0) { 236 | luaL_addchar(&b, '-'); 237 | dec = -dec; 238 | } 239 | int buffer[32]; 240 | int i; 241 | for (i=0;i<32;i++) { 242 | buffer[i] = dec%10; 243 | dec /= 10; 244 | if (dec == 0) 245 | break; 246 | } 247 | while (i>=0) { 248 | luaL_addchar(&b, hex[buffer[i]]); 249 | --i; 250 | } 251 | luaL_pushresult(&b); 252 | return 1; 253 | } 254 | case 2: 255 | shift = 1; 256 | mask = 1; 257 | break; 258 | case 8: 259 | shift = 3; 260 | mask = 7; 261 | break; 262 | case 16: 263 | shift = 4; 264 | mask = 0xf; 265 | break; 266 | default: 267 | luaL_error(L, "Unsupport base %d",base); 268 | break; 269 | } 270 | int i; 271 | char buffer[64]; 272 | for (i=0;i<64;i+=shift) { 273 | buffer[i/shift] = hex[(n>>(64-shift-i)) & mask]; 274 | } 275 | lua_pushlstring(L, buffer, 64 / shift); 276 | } 277 | return 1; 278 | } 279 | 280 | static void 281 | make_mt(lua_State *L) { 282 | luaL_Reg lib[] = { 283 | { "__add", int64_add }, 284 | { "__sub", int64_sub }, 285 | { "__mul", int64_mul }, 286 | { "__div", int64_div }, 287 | { "__mod", int64_mod }, 288 | { "__unm", int64_unm }, 289 | { "__pow", int64_pow }, 290 | { "__eq", int64_eq }, 291 | { "__lt", int64_lt }, 292 | { "__le", int64_le }, 293 | { "__len", int64_len }, 294 | { "__tostring", tostring }, 295 | { NULL, NULL }, 296 | }; 297 | luaL_newlib(L,lib); 298 | } 299 | 300 | int 301 | luaopen_int64(lua_State *L) { 302 | if (sizeof(intptr_t)!=sizeof(int64_t)) { 303 | return luaL_error(L, "Only support 64bit architecture"); 304 | } 305 | lua_pushlightuserdata(L,NULL); 306 | make_mt(L); 307 | lua_setmetatable(L,-2); 308 | lua_pop(L,1); 309 | 310 | lua_newtable(L); 311 | lua_pushcfunction(L, int64_new); 312 | lua_setfield(L, -2, "new"); 313 | lua_pushcfunction(L, tostring); 314 | lua_setfield(L, -2, "tostring"); 315 | 316 | return 1; 317 | } 318 | 319 | -------------------------------------------------------------------------------- /test.lua: -------------------------------------------------------------------------------- 1 | lib = require "int64" 2 | 3 | local int64 = lib.new 4 | print(lib.tostring(int64 "\1\2\3\4\5\6\7\8")) 5 | 6 | a = 1 + int64(1) 7 | b = int64 "\16" + int64("9",10) 8 | print(lib.tostring(a,10), lib.tostring(b,2)) 9 | print("+", a+b) 10 | print("-", lib.tostring(a-b,10)) 11 | print("*", a*b) 12 | print("/", a/b) 13 | print("%", a%b) 14 | print("^", a^b) 15 | print("==", a == b) 16 | print(">", a > b) 17 | print("#", #a) 18 | --------------------------------------------------------------------------------