diff options
author | Mike Pall <mike> | 2022-12-22 00:03:06 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2022-12-22 00:03:06 +0100 |
commit | 8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b (patch) | |
tree | dae089564f58db2963bae8e3530c1faae104cc61 /src/lj_vmmath.c | |
parent | b2791179ef96d652d00d78d2a8780af690537f6a (diff) | |
download | luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.gz luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.tar.bz2 luajit-8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b.zip |
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.c | 8 |
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) | |||
66 | { | 66 | { |
67 | uint32_t y, ua, ub; | 67 | uint32_t y, ua, ub; |
68 | lua_assert(b != 0); /* This must be checked before using this function. */ | 68 | lua_assert(b != 0); /* This must be checked before using this function. */ |
69 | ua = a < 0 ? (uint32_t)-a : (uint32_t)a; | 69 | ua = a < 0 ? ~(uint32_t)a+1u : (uint32_t)a; |
70 | ub = b < 0 ? (uint32_t)-b : (uint32_t)b; | 70 | ub = b < 0 ? ~(uint32_t)b+1u : (uint32_t)b; |
71 | y = ua % ub; | 71 | y = ua % ub; |
72 | if (y != 0 && (a^b) < 0) y = y - ub; | 72 | if (y != 0 && (a^b) < 0) y = y - ub; |
73 | if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y; | 73 | if (((int32_t)y^b) < 0) y = ~y+1u; |
74 | return (int32_t)y; | 74 | return (int32_t)y; |
75 | } | 75 | } |
76 | #endif | 76 | #endif |
@@ -105,7 +105,7 @@ double lj_vm_powi(double x, int32_t k) | |||
105 | else if (k == 0) | 105 | else if (k == 0) |
106 | return 1.0; | 106 | return 1.0; |
107 | else | 107 | else |
108 | return 1.0 / lj_vm_powui(x, (uint32_t)-k); | 108 | return 1.0 / lj_vm_powui(x, ~(uint32_t)k+1u); |
109 | } | 109 | } |
110 | 110 | ||
111 | /* Computes fpm(x) for extended math functions. */ | 111 | /* Computes fpm(x) for extended math functions. */ |