diff options
Diffstat (limited to 'src/lib_bit.c')
| -rw-r--r-- | src/lib_bit.c | 134 |
1 files changed, 120 insertions, 14 deletions
diff --git a/src/lib_bit.c b/src/lib_bit.c index 553beed8..6fb8ad47 100644 --- a/src/lib_bit.c +++ b/src/lib_bit.c | |||
| @@ -12,26 +12,99 @@ | |||
| 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_str.h" | 15 | #include "lj_buf.h" |
| 16 | #include "lj_strscan.h" | ||
| 17 | #include "lj_strfmt.h" | ||
| 18 | #if LJ_HASFFI | ||
| 19 | #include "lj_ctype.h" | ||
| 20 | #include "lj_cdata.h" | ||
| 21 | #include "lj_cconv.h" | ||
| 22 | #include "lj_carith.h" | ||
| 23 | #endif | ||
| 24 | #include "lj_ff.h" | ||
| 16 | #include "lj_lib.h" | 25 | #include "lj_lib.h" |
| 17 | 26 | ||
| 18 | /* ------------------------------------------------------------------------ */ | 27 | /* ------------------------------------------------------------------------ */ |
| 19 | 28 | ||
| 20 | #define LJLIB_MODULE_bit | 29 | #define LJLIB_MODULE_bit |
| 21 | 30 | ||
| 22 | LJLIB_ASM(bit_tobit) LJLIB_REC(bit_unary IR_TOBIT) | 31 | #if LJ_HASFFI |
| 32 | static int bit_result64(lua_State *L, CTypeID id, uint64_t x) | ||
| 23 | { | 33 | { |
| 34 | GCcdata *cd = lj_cdata_new_(L, id, 8); | ||
| 35 | *(uint64_t *)cdataptr(cd) = x; | ||
| 36 | setcdataV(L, L->base-1-LJ_FR2, cd); | ||
| 37 | return FFH_RES(1); | ||
| 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 | } | ||
| 53 | #endif | ||
| 54 | |||
| 55 | LJLIB_ASM(bit_tobit) LJLIB_REC(bit_tobit) | ||
| 56 | { | ||
| 57 | #if LJ_HASFFI | ||
| 58 | CTypeID id = 0; | ||
| 59 | setintV(L->base-1-LJ_FR2, (int32_t)lj_carith_check64(L, 1, &id)); | ||
| 60 | return FFH_RES(1); | ||
| 61 | #else | ||
| 62 | lj_lib_checknumber(L, 1); | ||
| 63 | return FFH_RETRY; | ||
| 64 | #endif | ||
| 65 | } | ||
| 66 | |||
| 67 | LJLIB_ASM(bit_bnot) LJLIB_REC(bit_unary IR_BNOT) | ||
| 68 | { | ||
| 69 | #if LJ_HASFFI | ||
| 70 | CTypeID id = 0; | ||
| 71 | uint64_t x = lj_carith_check64(L, 1, &id); | ||
| 72 | return id ? bit_result64(L, id, ~x) : FFH_RETRY; | ||
| 73 | #else | ||
| 24 | lj_lib_checknumber(L, 1); | 74 | lj_lib_checknumber(L, 1); |
| 25 | return FFH_RETRY; | 75 | return FFH_RETRY; |
| 76 | #endif | ||
| 77 | } | ||
| 78 | |||
| 79 | LJLIB_ASM(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP) | ||
| 80 | { | ||
| 81 | #if LJ_HASFFI | ||
| 82 | CTypeID id = 0; | ||
| 83 | uint64_t x = lj_carith_check64(L, 1, &id); | ||
| 84 | return id ? bit_result64(L, id, lj_bswap64(x)) : FFH_RETRY; | ||
| 85 | #else | ||
| 86 | lj_lib_checknumber(L, 1); | ||
| 87 | return FFH_RETRY; | ||
| 88 | #endif | ||
| 26 | } | 89 | } |
| 27 | LJLIB_ASM_(bit_bnot) LJLIB_REC(bit_unary IR_BNOT) | ||
| 28 | LJLIB_ASM_(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP) | ||
| 29 | 90 | ||
| 30 | LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) | 91 | LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) |
| 31 | { | 92 | { |
| 93 | #if LJ_HASFFI | ||
| 94 | CTypeID id = 0, id2 = 0; | ||
| 95 | uint64_t x = lj_carith_check64(L, 1, &id); | ||
| 96 | int32_t sh = (int32_t)lj_carith_check64(L, 2, &id2); | ||
| 97 | if (id) { | ||
| 98 | x = lj_carith_shift64(x, sh, curr_func(L)->c.ffid - (int)FF_bit_lshift); | ||
| 99 | return bit_result64(L, id, x); | ||
| 100 | } | ||
| 101 | if (id2) setintV(L->base+1, sh); | ||
| 102 | return FFH_RETRY; | ||
| 103 | #else | ||
| 32 | lj_lib_checknumber(L, 1); | 104 | lj_lib_checknumber(L, 1); |
| 33 | lj_lib_checkbit(L, 2); | 105 | bit_checkbit(L, 2); |
| 34 | return FFH_RETRY; | 106 | return FFH_RETRY; |
| 107 | #endif | ||
| 35 | } | 108 | } |
| 36 | LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR) | 109 | LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR) |
| 37 | LJLIB_ASM_(bit_arshift) LJLIB_REC(bit_shift IR_BSAR) | 110 | LJLIB_ASM_(bit_arshift) LJLIB_REC(bit_shift IR_BSAR) |
| @@ -40,25 +113,58 @@ LJLIB_ASM_(bit_ror) LJLIB_REC(bit_shift IR_BROR) | |||
| 40 | 113 | ||
| 41 | LJLIB_ASM(bit_band) LJLIB_REC(bit_nary IR_BAND) | 114 | LJLIB_ASM(bit_band) LJLIB_REC(bit_nary IR_BAND) |
| 42 | { | 115 | { |
| 116 | #if LJ_HASFFI | ||
| 117 | CTypeID id = 0; | ||
| 118 | TValue *o = L->base, *top = L->top; | ||
| 119 | int i = 0; | ||
| 120 | do { lj_carith_check64(L, ++i, &id); } while (++o < top); | ||
| 121 | if (id) { | ||
| 122 | CTState *cts = ctype_cts(L); | ||
| 123 | CType *ct = ctype_get(cts, id); | ||
| 124 | int op = curr_func(L)->c.ffid - (int)FF_bit_bor; | ||
| 125 | uint64_t x, y = op >= 0 ? 0 : ~(uint64_t)0; | ||
| 126 | o = L->base; | ||
| 127 | do { | ||
| 128 | lj_cconv_ct_tv(cts, ct, (uint8_t *)&x, o, 0); | ||
| 129 | if (op < 0) y &= x; else if (op == 0) y |= x; else y ^= x; | ||
| 130 | } while (++o < top); | ||
| 131 | return bit_result64(L, id, y); | ||
| 132 | } | ||
| 133 | return FFH_RETRY; | ||
| 134 | #else | ||
| 43 | int i = 0; | 135 | int i = 0; |
| 44 | do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); | 136 | do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); |
| 45 | return FFH_RETRY; | 137 | return FFH_RETRY; |
| 138 | #endif | ||
| 46 | } | 139 | } |
| 47 | LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR) | 140 | LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR) |
| 48 | LJLIB_ASM_(bit_bxor) LJLIB_REC(bit_nary IR_BXOR) | 141 | LJLIB_ASM_(bit_bxor) LJLIB_REC(bit_nary IR_BXOR) |
| 49 | 142 | ||
| 50 | /* ------------------------------------------------------------------------ */ | 143 | /* ------------------------------------------------------------------------ */ |
| 51 | 144 | ||
| 52 | LJLIB_CF(bit_tohex) | 145 | LJLIB_CF(bit_tohex) LJLIB_REC(.) |
| 53 | { | 146 | { |
| 54 | uint32_t b = (uint32_t)lj_lib_checkbit(L, 1); | 147 | #if LJ_HASFFI |
| 55 | int32_t i, n = L->base+1 >= L->top ? 8 : lj_lib_checkbit(L, 2); | 148 | CTypeID id = 0, id2 = 0; |
| 56 | const char *hexdigits = "0123456789abcdef"; | 149 | uint64_t b = lj_carith_check64(L, 1, &id); |
| 57 | char buf[8]; | 150 | int32_t n = L->base+1>=L->top ? (id ? 16 : 8) : |
| 58 | if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } | 151 | (int32_t)lj_carith_check64(L, 2, &id2); |
| 59 | if (n > 8) n = 8; | 152 | #else |
| 60 | for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } | 153 | uint32_t b = (uint32_t)bit_checkbit(L, 1); |
| 61 | lua_pushlstring(L, buf, (size_t)n); | 154 | int32_t n = L->base+1>=L->top ? 8 : bit_checkbit(L, 2); |
| 155 | #endif | ||
| 156 | SBuf *sb = lj_buf_tmp_(L); | ||
| 157 | SFormat sf = (STRFMT_UINT|STRFMT_T_HEX); | ||
| 158 | if (n < 0) { n = -n; sf |= STRFMT_F_UPPER; } | ||
| 159 | sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC); | ||
| 160 | #if LJ_HASFFI | ||
| 161 | if (n < 16) b &= ((uint64_t)1 << 4*n)-1; | ||
| 162 | #else | ||
| 163 | if (n < 8) b &= (1u << 4*n)-1; | ||
| 164 | #endif | ||
| 165 | sb = lj_strfmt_putfxint(sb, sf, b); | ||
| 166 | setstrV(L, L->top-1, lj_buf_str(L, sb)); | ||
| 167 | lj_gc_check(L); | ||
| 62 | return 1; | 168 | return 1; |
| 63 | } | 169 | } |
| 64 | 170 | ||
