summaryrefslogtreecommitdiff
path: root/src/lj_vmmath.c
diff options
context:
space:
mode:
authorMike Pall <mike>2022-12-22 00:03:06 +0100
committerMike Pall <mike>2022-12-22 00:03:06 +0100
commit8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b (patch)
treedae089564f58db2963bae8e3530c1faae104cc61 /src/lj_vmmath.c
parentb2791179ef96d652d00d78d2a8780af690537f6a (diff)
downloadluajit2-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.gz
Avoid negation of signed integers in C that may hold INT*_MIN.
Reported by minoki. Recent C compilers 'take advantage' of the undefined behavior. This completely changes the meaning of expressions like (k == -k).
Diffstat (limited to 'src/lj_vmmath.c')
-rw-r--r--src/lj_vmmath.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/lj_vmmath.c b/src/lj_vmmath.c
index ff41ba28..6369bc6b 100644
--- a/src/lj_vmmath.c
+++ b/src/lj_vmmath.c
@@ -66,11 +66,11 @@ int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b)
{
uint32_t y, ua, ub;
lua_assert(b != 0); /* This must be checked before using this function. */
- ua = a < 0 ? (uint32_t)-a : (uint32_t)a;
- ub = b < 0 ? (uint32_t)-b : (uint32_t)b;
+ ua = a < 0 ? ~(uint32_t)a+1u : (uint32_t)a;
+ ub = b < 0 ? ~(uint32_t)b+1u : (uint32_t)b;
y = ua % ub;
if (y != 0 && (a^b) < 0) y = y - ub;
- if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y;
+ if (((int32_t)y^b) < 0) y = ~y+1u;
return (int32_t)y;
}
#endif
@@ -105,7 +105,7 @@ double lj_vm_powi(double x, int32_t k)
else if (k == 0)
return 1.0;
else
- return 1.0 / lj_vm_powui(x, (uint32_t)-k);
+ return 1.0 / lj_vm_powui(x, ~(uint32_t)k+1u);
}
/* Computes fpm(x) for extended math functions. */