FFI: Add 64 bit integer arithmetic.

This commit is contained in:
Mike Pall
2010-12-25 22:54:25 +01:00
parent dd65e00831
commit 44935dae0d
6 changed files with 158 additions and 18 deletions

View File

@@ -230,4 +230,44 @@ void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual)
lj_cconv_ct_tv(cts, d, dp, o, 0);
}
/* -- 64 bit integer arithmetic helpers ----------------------------------- */
/* 64 bit integer x^k. */
uint64_t lj_cdata_powi64(uint64_t x, uint64_t k, int isunsigned)
{
uint64_t y = 0;
int sign = 0;
if (k == 0)
return 1;
if (!isunsigned) {
if ((int64_t)k < 0) {
if (x == 0)
return U64x(7fffffff,ffffffff);
else if (x == 1)
return 1;
else if ((int64_t)x == -1)
return (k & 1) ? -1 : 1;
else
return 0;
}
if ((int64_t)x < 0) {
x = -x;
sign = (k & 1);
}
}
for (; (k & 1) == 0; k >>= 1) x *= x;
y = x;
if ((k >>= 1) != 0) {
for (;;) {
x *= x;
if (k == 1) break;
if (k & 1) y *= x;
k >>= 1;
}
y *= x;
}
if (sign) y = (uint64_t)-(int64_t)y;
return y;
}
#endif