diff options
| author | Mike Pall <mike> | 2013-05-12 23:14:12 +0200 |
|---|---|---|
| committer | Mike Pall <mike> | 2013-05-12 23:14:12 +0200 |
| commit | 7d5acc29181a5194b05fd4650b9e28e4c4978ad1 (patch) | |
| tree | ec34b54585b0b07827b81c8a8670f8bb0b7d595e /src | |
| parent | 5bb1f0edac809302b299e189fb3c4006e0bc939a (diff) | |
| download | luajit-7d5acc29181a5194b05fd4650b9e28e4c4978ad1.tar.gz luajit-7d5acc29181a5194b05fd4650b9e28e4c4978ad1.tar.bz2 luajit-7d5acc29181a5194b05fd4650b9e28e4c4978ad1.zip | |
Refactor bit.tohex().
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.dep | 5 | ||||
| -rw-r--r-- | src/lib_bit.c | 41 | ||||
| -rw-r--r-- | src/lj_lib.c | 14 | ||||
| -rw-r--r-- | src/lj_lib.h | 1 |
4 files changed, 31 insertions, 30 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep index 8e01865c..b2ced9c9 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep | |||
| @@ -7,8 +7,9 @@ lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ | |||
| 7 | lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \ | 7 | lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \ |
| 8 | lj_lib.h lj_libdef.h | 8 | lj_lib.h lj_libdef.h |
| 9 | lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ | 9 | lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ |
| 10 | lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cdata.h \ | 10 | lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_strfmt.h \ |
| 11 | lj_cconv.h lj_carith.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h | 11 | lj_ctype.h lj_cdata.h lj_cconv.h lj_carith.h lj_ff.h lj_ffdef.h lj_lib.h \ |
| 12 | lj_libdef.h | ||
| 12 | lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ | 13 | lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ |
| 13 | lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \ | 14 | lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \ |
| 14 | lj_libdef.h | 15 | lj_libdef.h |
diff --git a/src/lib_bit.c b/src/lib_bit.c index 85821b81..b1f0beb2 100644 --- a/src/lib_bit.c +++ b/src/lib_bit.c | |||
| @@ -12,7 +12,9 @@ | |||
| 12 | 12 | ||
| 13 | #include "lj_obj.h" | 13 | #include "lj_obj.h" |
| 14 | #include "lj_err.h" | 14 | #include "lj_err.h" |
| 15 | #include "lj_buf.h" | ||
| 15 | #include "lj_str.h" | 16 | #include "lj_str.h" |
| 17 | #include "lj_strfmt.h" | ||
| 16 | #if LJ_HASFFI | 18 | #if LJ_HASFFI |
| 17 | #include "lj_ctype.h" | 19 | #include "lj_ctype.h" |
| 18 | #include "lj_cdata.h" | 20 | #include "lj_cdata.h" |
| @@ -34,6 +36,20 @@ static int bit_result64(lua_State *L, CTypeID id, uint64_t x) | |||
| 34 | setcdataV(L, L->base-1, cd); | 36 | setcdataV(L, L->base-1, cd); |
| 35 | return FFH_RES(1); | 37 | return FFH_RES(1); |
| 36 | } | 38 | } |
| 39 | #else | ||
| 40 | static int32_t bit_checkbit(lua_State *L, int narg) | ||
| 41 | { | ||
| 42 | TValue *o = L->base + narg-1; | ||
| 43 | if (!(o < L->top && lj_strscan_numberobj(o))) | ||
| 44 | lj_err_argt(L, narg, LUA_TNUMBER); | ||
| 45 | if (LJ_LIKELY(tvisint(o))) { | ||
| 46 | return intV(o); | ||
| 47 | } else { | ||
| 48 | int32_t i = lj_num2bit(numV(o)); | ||
| 49 | if (LJ_DUALNUM) setintV(o, i); | ||
| 50 | return i; | ||
| 51 | } | ||
| 52 | } | ||
| 37 | #endif | 53 | #endif |
| 38 | 54 | ||
| 39 | LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit) | 55 | LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit) |
| @@ -86,7 +102,7 @@ LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) | |||
| 86 | return FFH_RETRY; | 102 | return FFH_RETRY; |
| 87 | #else | 103 | #else |
| 88 | lj_lib_checknumber(L, 1); | 104 | lj_lib_checknumber(L, 1); |
| 89 | lj_lib_checkbit(L, 2); | 105 | bit_checkbit(L, 2); |
| 90 | return FFH_RETRY; | 106 | return FFH_RETRY; |
| 91 | #endif | 107 | #endif |
| 92 | } | 108 | } |
| @@ -131,20 +147,19 @@ LJLIB_CF(bit_tohex) | |||
| 131 | #if LJ_HASFFI | 147 | #if LJ_HASFFI |
| 132 | CTypeID id = 0, id2 = 0; | 148 | CTypeID id = 0, id2 = 0; |
| 133 | uint64_t b = lj_carith_check64(L, 1, &id); | 149 | uint64_t b = lj_carith_check64(L, 1, &id); |
| 134 | int32_t i, dig = id ? 16 : 8; | 150 | int32_t n = L->base+1>=L->top ? (id ? 16 : 8) : |
| 135 | int32_t n = L->base+1>=L->top ? dig : (int32_t)lj_carith_check64(L, 2, &id2); | 151 | (int32_t)lj_carith_check64(L, 2, &id2); |
| 136 | char buf[16]; | ||
| 137 | #else | 152 | #else |
| 138 | uint32_t b = (uint32_t)lj_lib_checkbit(L, 1); | 153 | uint32_t b = (uint32_t)bit_checkbit(L, 1); |
| 139 | int32_t i, dig = 8; | 154 | int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2); |
| 140 | int32_t n = L->base+1>=L->top ? dig : lj_lib_checkbit(L, 2); | ||
| 141 | char buf[8]; | ||
| 142 | #endif | 155 | #endif |
| 143 | const char *hexdigits = "0123456789abcdef"; | 156 | SBuf *sb = lj_buf_tmp_(L); |
| 144 | if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } | 157 | SFormat sf = (STRFMT_UINT|STRFMT_T_HEX); |
| 145 | if (n > dig) n = dig; | 158 | if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; } |
| 146 | for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } | 159 | sf |= ((SFormat)(n+1) << STRFMT_SH_PREC); |
| 147 | lua_pushlstring(L, buf, (size_t)n); | 160 | sb = lj_strfmt_putxint(sb, sf, b); |
| 161 | setstrV(L, L->top-1, lj_buf_str(L, sb)); | ||
| 162 | lj_gc_check(L); | ||
| 148 | return 1; | 163 | return 1; |
| 149 | } | 164 | } |
| 150 | 165 | ||
diff --git a/src/lj_lib.c b/src/lj_lib.c index b6aa97a0..a4bde57a 100644 --- a/src/lj_lib.c +++ b/src/lj_lib.c | |||
| @@ -223,20 +223,6 @@ int32_t lj_lib_optint(lua_State *L, int narg, int32_t def) | |||
| 223 | return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def; | 223 | return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def; |
| 224 | } | 224 | } |
| 225 | 225 | ||
| 226 | int32_t lj_lib_checkbit(lua_State *L, int narg) | ||
| 227 | { | ||
| 228 | TValue *o = L->base + narg-1; | ||
| 229 | if (!(o < L->top && lj_strscan_numberobj(o))) | ||
| 230 | lj_err_argt(L, narg, LUA_TNUMBER); | ||
| 231 | if (LJ_LIKELY(tvisint(o))) { | ||
| 232 | return intV(o); | ||
| 233 | } else { | ||
| 234 | int32_t i = lj_num2bit(numV(o)); | ||
| 235 | if (LJ_DUALNUM) setintV(o, i); | ||
| 236 | return i; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | GCfunc *lj_lib_checkfunc(lua_State *L, int narg) | 226 | GCfunc *lj_lib_checkfunc(lua_State *L, int narg) |
| 241 | { | 227 | { |
| 242 | TValue *o = L->base + narg-1; | 228 | TValue *o = L->base + narg-1; |
diff --git a/src/lj_lib.h b/src/lj_lib.h index 05f90de5..2dd45adb 100644 --- a/src/lj_lib.h +++ b/src/lj_lib.h | |||
| @@ -41,7 +41,6 @@ LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg); | |||
| 41 | LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg); | 41 | LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg); |
| 42 | LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg); | 42 | LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg); |
| 43 | LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def); | 43 | LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def); |
| 44 | LJ_FUNC int32_t lj_lib_checkbit(lua_State *L, int narg); | ||
| 45 | LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg); | 44 | LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg); |
| 46 | LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg); | 45 | LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg); |
| 47 | LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg); | 46 | LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg); |
