Arithmetic Shifting Considered Harmful (1976) [pdf]
dspace.mit.edu
dspace.mit.edu
$ echo "print(-7 % 3)" | python3
2
$ echo "print(-7 % -3)" | python3
-1
$ echo "System.out.println(-7 % 3)" | jshell -
-1
$ echo "System.out.println(-7 % -3)" | jshell -
-1Really it's a note from a world we've forgotten, where "all" computer operations were unsigned and 2's complement math was a clever trick deployed only occasionally.
I think this is just the MIT AI Lab tilting at windmills. They had their quirks like wanting the world to call pixels pels well after the world had decided on the former.
x / 2 resulting in the off-by-one error mentioned in the paper, if x is LLONG_MIN. LLONG_MIN / 2 = 0 in with their optimization.
return x / 2
baz: # @baz mov rax, rdi shr rax, 63 add rax, rdi sar rax ret
Are you saying clang is leading to an off-by-one error? The code seems to behave correctly to me.
Yes, this is because asr/and rounds toward negative infinity (as it should), while your DIVMOD (also round toward -inf) routine incorrectly[0] implements QUOREM (round toward zero).
0: Well, you could say that it's rather the name that is incorrect, since I gather it's intended to implement quo/rem rather than div/mod.