Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32 bit signed xor broken on VM #10482

Closed
krux02 opened this issue Jan 28, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@krux02
Copy link
Contributor

commented Jan 28, 2019

the bit xor operator doesn't work properly in the vm on signed integer types.

The cause of the bug is, on the VM negatives numbers are represented in higher precision. So a -1 has 64 bits set to 1, even if it represents a 32 bit -1. In combination with xor and a positive number, these upper bits are set to 0. If only the actual 32 important bits would be taken care of everything would seem fine, but sadly the CPU thinks these are now positive numbers.

Example

import strutils

static:  # only the visible part is printed
  let x = -1'i32
  let y = 1'i32
  echo toHex(x)            #   FFFFFFFF
  echo toHex(y)            #   00000001
  echo toHex(x xor y)      #   FFFFFFFE
  echo toHex(-2'i32)       #   FFFFFFFE  <- seems right doesn't it ?
  echo int64(x xor y)        #   4294967294  <- something is wrong here
  echo toHex(int64(x xor y)) #   00000000FFFFFFFE  <-\
  echo toHex(int64(-2))      #   FFFFFFFFFFFFFFFE  <---- the difference is in the hidden part

@krux02 krux02 self-assigned this Jan 28, 2019

@krux02 krux02 added the VM label Jan 28, 2019

@krux02

This comment has been minimized.

Copy link
Contributor Author

commented Jan 29, 2019

@mratsim This bug causes breaking tests on 32 bit platforms. But the but itself isn't a platform dependent bug. It can be reproduced on all platforms.

cooldome added a commit to cooldome/Nim that referenced this issue Jan 30, 2019

krux02 added a commit that referenced this issue Jan 31, 2019

vm fix for bitwise signed ints (#10507)
* fixes #10482

* add missing file

* bug fix

krux02 added a commit to krux02/Nim that referenced this issue Jan 31, 2019

krux02 added a commit to krux02/Nim that referenced this issue Jan 31, 2019

Araq added a commit that referenced this issue Feb 1, 2019

Fix vm signed xor (#10519)
* fix #10482

* undo changes

* fix for bitwise not

* remove dead opcode

LemonBoy added a commit to LemonBoy/Nim that referenced this issue Feb 1, 2019

narimiran added a commit that referenced this issue Feb 5, 2019

vm fix for bitwise signed ints (#10507)
* fixes #10482

* add missing file

* bug fix

(cherry picked from commit 1d5437e)

narimiran added a commit that referenced this issue Feb 5, 2019

Fix vm signed xor (#10519)
* fix #10482

* undo changes

* fix for bitwise not

* remove dead opcode

(cherry picked from commit b80dbdb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.