[0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Bugs that are actually features.
User avatar
infinitysimplex
Burner Inserter
Burner Inserter
Posts: 14
Joined: Fri Jul 28, 2017 8:26 pm
Contact:

[0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by infinitysimplex »

One of the combinator bugs that have been bugging me ( :mrgreen: :roll: ) for a long time now. Finally having the resolve to open a bug thread. I've been looking up and down the reports, known issues etc., apologies if it's already been reported.
  • Synopsis: Bitwise right-shift operation of arithmetic combinator (">>" operation) behaves like division by 2
  • Factorio Version: 0.17.45 (build 45219, linux64, steam)
  • Example:
    • Screenshot (explanation below):
      file.png
      file.png (917.5 KiB) Viewed 2901 times
      The lights display the signal "X" bitwise. With proper bitshift, the last two rows should be identical.
      The constant combinator on top sets X=1.
      The first row of combinators does X << 31 (left) or nothing (right).
      Second row does X >> 1 (left) and X << 30 (right), giving identical outputs if bitshift were working correctly. The difference in the outputs is clearly visible.
      The last row does X >> 30 (both left and right), giving output X=1 if bitshift were working correctly. Again, the difference is clearly visible, being X=1 and X=-1, in two's-complement-encoding.
    • Blueprint string:

      Code: Select all

      0eNrtnd1uHEeShd+FtysuOiL/BcPAAnu91wssDIOWOWMCEiVQlLGGoQfYt9hn2ydZklKN7e6KznMKY2cGqm9mIMtuSRFK1sevMuL8evXD20+3Hx7u7h+vXv96dffm/f3Hq9f/9evVx7u/39+8ff5nj798uL16fXX3ePvu6tXV/c275x/dPNw9/vTu9vHuzfWb9+9+uLu/eXz/cPX51dXd/Y+3/331Wj5/9+rq9v7x7vHu9ssHvvzgl+/vP7374fbh6V/ofNSrqw/vPz791+/vn38PT594HcKrq1+e/j//a3r6dX68e7h98+Wn46urp9/248P7t9//cPvTzc93T//503/z2+d+//TTP7581sfnn/jb3cPHx+9P/oA/3z08fnr6J//4jX35N67/8/mP9fH2+TOeP+jj481zreRQQolS9elXf//h9uHmy+/l6t/+49+fPuH9p8cPn7hf4/Pnlz/G/Zc/1ctvVJ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WTpdkK6omSZwNfgXsmyoWSZMLJ0u0ehrZLld6+u7h5v3z193g9vP91+eLh7+qVeXf18+/Dx5fdewkFKaEFS+Pz5/wHZIHzS
      
  • Reproducible: Always
  • Suspected cause: ">>" implemented by divison by 2 instead of bitshift. Maybe correspondingly for "<<".
  • Impediment: Low
  • Workaround and required effort: Separate negative and positive signals, handle them differently. Minor effort for typical players, medium for extravagant combinator setups.
  • Fixing effort: Minimal. Implement by bitshift, either using Lua's bitshift operation (I hope Lua has some sort of stdlib...) or bindings to C/C++ bitshift functions. This should also give a major performance boost for these operations since the libraries should transparently hand them to the CPU, computing it in hardware. Same goes for all other bitwise operations.
There were some similar bugs and inconveniences. I'll try to verify them in the current version and post them here or in the suggestions section, respectively, as soon as I can find the time.
Thanks for looking into it! :)
Rseding91
Factorio Staff
Factorio Staff
Posts: 14338
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by Rseding91 »

Doing a quick search yields this: https://stackoverflow.com/questions/185 ... mbers-in-c

And looking at the logic all it's doing is this:
shifts.PNG
shifts.PNG (10.59 KiB) Viewed 2897 times
Which leads me to believe that this is all working "as intended" and isn't likely to be changed. I'll leave that up to Twinsen though.
If you want to get ahold of me I'm almost always on Discord.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3717
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: [0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by DaveMcW »

DaveMcW wrote: Fri Apr 28, 2017 6:01 am I would love to have a >>> operator for logical shift instead of arithmetic shift.

Any bitwise operation that touches the sign bit is very awkward to do without it.
Twinsen
Factorio Staff
Factorio Staff
Posts: 1351
Joined: Tue Sep 23, 2014 7:10 am
Contact:

Re: [0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by Twinsen »

We are going for whatever C++ sets as standard, so it's indeed working as intended.
mrvn
Smart Inserter
Smart Inserter
Posts: 5881
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: [0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by mrvn »

Twinsen wrote: Tue Jun 04, 2019 9:14 am We are going for whatever C++ sets as standard, so it's indeed working as intended.
C++ has both arithmetic (signed) and logical (unsigned) shift depending on the type of variable. But signals seem to be int32_t so arithmetic shift it is.
User avatar
infinitysimplex
Burner Inserter
Burner Inserter
Posts: 14
Joined: Fri Jul 28, 2017 8:26 pm
Contact:

Re: [0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by infinitysimplex »

Ah, I see. So It's indeed what your C/C++ implementation does. Thanks for the quick answers, that was basically all I wondered. :)
Actually, considering it's the arithmetic combinator, not the logical, I could have expected that it does arithmetic shift, not logical. It just never occurred to me until now. My bad. 🤦

Sorry for the disturbance, and thanks again. :)
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3717
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: [0.17.45] Arithmetic Combinator: right-shift (>>) not working correctly on negative numbers

Post by DaveMcW »

Halke1986 discovered a trick to turn a signed right-shift into unsigned right-shift. You flip the sign bit and right-shift, then add two copies of 2^30 right-shifted. This can be done with a single constant combinator.

The complete solution includes an extra combinator to handle the special case of right-shifting by 31.



unsigned_right_shift.jpg
unsigned_right_shift.jpg (87.37 KiB) Viewed 2435 times
Post Reply

Return to “Not a bug”