mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-02-08 07:34:07 +00:00
Refactor bit.tohex().
This commit is contained in:
parent
5bb1f0edac
commit
7d5acc2918
@ -7,8 +7,9 @@ lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
|
|||||||
lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \
|
lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \
|
||||||
lj_lib.h lj_libdef.h
|
lj_lib.h lj_libdef.h
|
||||||
lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
|
lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
|
||||||
lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cdata.h \
|
lj_arch.h lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_strfmt.h \
|
||||||
lj_cconv.h lj_carith.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
|
lj_ctype.h lj_cdata.h lj_cconv.h lj_carith.h lj_ff.h lj_ffdef.h lj_lib.h \
|
||||||
|
lj_libdef.h
|
||||||
lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
|
lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
|
||||||
lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \
|
lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \
|
||||||
lj_libdef.h
|
lj_libdef.h
|
||||||
|
@ -12,7 +12,9 @@
|
|||||||
|
|
||||||
#include "lj_obj.h"
|
#include "lj_obj.h"
|
||||||
#include "lj_err.h"
|
#include "lj_err.h"
|
||||||
|
#include "lj_buf.h"
|
||||||
#include "lj_str.h"
|
#include "lj_str.h"
|
||||||
|
#include "lj_strfmt.h"
|
||||||
#if LJ_HASFFI
|
#if LJ_HASFFI
|
||||||
#include "lj_ctype.h"
|
#include "lj_ctype.h"
|
||||||
#include "lj_cdata.h"
|
#include "lj_cdata.h"
|
||||||
@ -34,6 +36,20 @@ static int bit_result64(lua_State *L, CTypeID id, uint64_t x)
|
|||||||
setcdataV(L, L->base-1, cd);
|
setcdataV(L, L->base-1, cd);
|
||||||
return FFH_RES(1);
|
return FFH_RES(1);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
static int32_t bit_checkbit(lua_State *L, int narg)
|
||||||
|
{
|
||||||
|
TValue *o = L->base + narg-1;
|
||||||
|
if (!(o < L->top && lj_strscan_numberobj(o)))
|
||||||
|
lj_err_argt(L, narg, LUA_TNUMBER);
|
||||||
|
if (LJ_LIKELY(tvisint(o))) {
|
||||||
|
return intV(o);
|
||||||
|
} else {
|
||||||
|
int32_t i = lj_num2bit(numV(o));
|
||||||
|
if (LJ_DUALNUM) setintV(o, i);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit)
|
LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit)
|
||||||
@ -86,7 +102,7 @@ LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL)
|
|||||||
return FFH_RETRY;
|
return FFH_RETRY;
|
||||||
#else
|
#else
|
||||||
lj_lib_checknumber(L, 1);
|
lj_lib_checknumber(L, 1);
|
||||||
lj_lib_checkbit(L, 2);
|
bit_checkbit(L, 2);
|
||||||
return FFH_RETRY;
|
return FFH_RETRY;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -131,20 +147,19 @@ LJLIB_CF(bit_tohex)
|
|||||||
#if LJ_HASFFI
|
#if LJ_HASFFI
|
||||||
CTypeID id = 0, id2 = 0;
|
CTypeID id = 0, id2 = 0;
|
||||||
uint64_t b = lj_carith_check64(L, 1, &id);
|
uint64_t b = lj_carith_check64(L, 1, &id);
|
||||||
int32_t i, dig = id ? 16 : 8;
|
int32_t n = L->base+1>=L->top ? (id ? 16 : 8) :
|
||||||
int32_t n = L->base+1>=L->top ? dig : (int32_t)lj_carith_check64(L, 2, &id2);
|
(int32_t)lj_carith_check64(L, 2, &id2);
|
||||||
char buf[16];
|
|
||||||
#else
|
#else
|
||||||
uint32_t b = (uint32_t)lj_lib_checkbit(L, 1);
|
uint32_t b = (uint32_t)bit_checkbit(L, 1);
|
||||||
int32_t i, dig = 8;
|
int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2);
|
||||||
int32_t n = L->base+1>=L->top ? dig : lj_lib_checkbit(L, 2);
|
|
||||||
char buf[8];
|
|
||||||
#endif
|
#endif
|
||||||
const char *hexdigits = "0123456789abcdef";
|
SBuf *sb = lj_buf_tmp_(L);
|
||||||
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
|
SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);
|
||||||
if (n > dig) n = dig;
|
if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; }
|
||||||
for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
|
sf |= ((SFormat)(n+1) << STRFMT_SH_PREC);
|
||||||
lua_pushlstring(L, buf, (size_t)n);
|
sb = lj_strfmt_putxint(sb, sf, b);
|
||||||
|
setstrV(L, L->top-1, lj_buf_str(L, sb));
|
||||||
|
lj_gc_check(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
src/lj_lib.c
14
src/lj_lib.c
@ -223,20 +223,6 @@ int32_t lj_lib_optint(lua_State *L, int narg, int32_t def)
|
|||||||
return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;
|
return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t lj_lib_checkbit(lua_State *L, int narg)
|
|
||||||
{
|
|
||||||
TValue *o = L->base + narg-1;
|
|
||||||
if (!(o < L->top && lj_strscan_numberobj(o)))
|
|
||||||
lj_err_argt(L, narg, LUA_TNUMBER);
|
|
||||||
if (LJ_LIKELY(tvisint(o))) {
|
|
||||||
return intV(o);
|
|
||||||
} else {
|
|
||||||
int32_t i = lj_num2bit(numV(o));
|
|
||||||
if (LJ_DUALNUM) setintV(o, i);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GCfunc *lj_lib_checkfunc(lua_State *L, int narg)
|
GCfunc *lj_lib_checkfunc(lua_State *L, int narg)
|
||||||
{
|
{
|
||||||
TValue *o = L->base + narg-1;
|
TValue *o = L->base + narg-1;
|
||||||
|
@ -41,7 +41,6 @@ LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg);
|
|||||||
LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg);
|
LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg);
|
||||||
LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg);
|
LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg);
|
||||||
LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def);
|
LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def);
|
||||||
LJ_FUNC int32_t lj_lib_checkbit(lua_State *L, int narg);
|
|
||||||
LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg);
|
LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg);
|
||||||
LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg);
|
LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg);
|
||||||
LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg);
|
LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg);
|
||||||
|
Loading…
Reference in New Issue
Block a user