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/lib_bit.c | |
parent | 5bb1f0edac809302b299e189fb3c4006e0bc939a (diff) | |
download | luajit-7d5acc29181a5194b05fd4650b9e28e4c4978ad1.tar.gz luajit-7d5acc29181a5194b05fd4650b9e28e4c4978ad1.tar.bz2 luajit-7d5acc29181a5194b05fd4650b9e28e4c4978ad1.zip |
Refactor bit.tohex().
Diffstat (limited to 'src/lib_bit.c')
-rw-r--r-- | src/lib_bit.c | 41 |
1 files changed, 28 insertions, 13 deletions
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 | ||