Bug 24140 – Int128.opBinary [+-*/%&|^] with negative long arguments gives wrong answers

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-09-11T21:06:09Z
Last change time
2023-09-16T10:18:42Z
Keywords
pull
Assigned to
No Owner
Creator
Nathan S.

Comments

Comment #0 by n8sh.secondary — 2023-09-11T21:06:09Z
The longs are converted to Int128 without sign extension. This is the same thing that was going on in https://issues.dlang.org/show_bug.cgi?id=24083. --- void main() { import std.int128 : Int128; import std.stdio : writeln; const zero = Int128(0L); const neg_one = Int128(-1L); const neg_two = Int128(-2L); // Correct result with unsigned arguments: assert(zero + ulong.max == ulong.max); assert(neg_two * 3UL == -6L); assert(neg_two / 2UL == -1L); assert(neg_two % 2UL == 0L); assert((neg_one & ulong.max) == ulong.max); assert((zero | ulong.max) == ulong.max); assert((zero ^ ulong.max) == ulong.max); // Incorrect result with unsigned arguments: writeln(zero + -1L == -1L); // "false" writeln(neg_two * -3L == 6L); // "false" writeln(neg_two / -2L == 1L); // "false" writeln(neg_two % -2L == 0L); // "false" writeln((neg_one & -1L) == -1L); // "false" writeln((zero | -1L) == -1L); // "false" writeln((zero ^ -1L) == -1L); // "false" } ---
Comment #1 by dlang-bot — 2023-09-11T21:40:36Z
@n8sh created dlang/phobos pull request #8809 "Fix Issue 24140 - Int128.opBinary [+-*/%&|^] with negative long arguments gives wrong answers" fixing this issue: - Fix Issue 24140 - Int128.opBinary [+-*/%&|^] with negative long arguments gives wrong answers https://github.com/dlang/phobos/pull/8809
Comment #2 by dlang-bot — 2023-09-12T00:03:16Z
dlang/phobos pull request #8809 "Fix Issue 24140 - Int128.opBinary [+-*/%&|^] with negative long arguments gives wrong answers" was merged into stable: - 4f0b8bfca9a32ef1ccd9ec434d1c4aeab5f7318e by Nathan Sashihara: Fix Issue 24140 - Int128.opBinary [+-*/%&|^] with negative long arguments gives wrong answers https://github.com/dlang/phobos/pull/8809
Comment #3 by dlang-bot — 2023-09-16T10:18:42Z
dlang/phobos pull request #8816 "merge stable" was merged into master: - 932ce6d17c7686b0c9b94e60b5eea082ba84dedc by Nathan Sashihara: Fix Issue 24140 - Int128.opBinary [+-*/%&|^] with negative long arguments gives wrong answers https://github.com/dlang/phobos/pull/8816