Skip to content

Commit 85ffbbf

Browse files
Mike PallBuristan
authored andcommitted
Fix bit op coercion in DUALNUM builds.
Thanks to Sergey Kaplun. (cherry picked from commit f5fd222) The `lj_carith_check64()` function coerces the given number value to the 32-bit wide value. In this case, the 64-bit-wide operands will lose upper bits. This patch removes the excess coercion for the DUALNUM mode. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#10199 Reviewed-by: Sergey Bronnikov <[email protected]> Reviewed-by: Maxim Kokryashkin <[email protected]> Signed-off-by: Sergey Kaplun <[email protected]> (cherry picked from commit 8cd79d1)
1 parent fe23eb2 commit 85ffbbf

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/lj_carith.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id)
349349
if (LJ_LIKELY(tvisint(o))) {
350350
return (uint32_t)intV(o);
351351
} else {
352-
int32_t i = lj_num2bit(numV(o));
353-
if (LJ_DUALNUM) setintV(o, i);
354-
return (uint32_t)i;
352+
return (uint32_t)lj_num2bit(numV(o));
355353
}
356354
}
357355

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local tap = require('tap')
2+
3+
-- Test file to demonstrate LuaJIT misbehaviour on operating
4+
-- for 64-bit operands in built-in `bit` library in DUALNUM mode.
5+
-- See also, https://github.com/LuaJIT/LuaJIT/issues/1273.
6+
7+
local test = tap.test('lj-1273-dualnum-bit-coercion')
8+
test:plan(1)
9+
10+
local bit = require('bit')
11+
12+
-- The cdata value for 2 ^ 33.
13+
local EXPECTED = 2 ^ 33 + 0LL
14+
-- Same value is used as mask for `bit.band()`.
15+
local MASK = EXPECTED
16+
17+
test:is(bit.band(2 ^ 33, MASK), EXPECTED, 'correct bit.band result')
18+
19+
test:done(true)

0 commit comments

Comments
 (0)