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

shl causes undefined behaviour #19983

Open
arnetheduck opened this issue Jul 7, 2022 · 3 comments
Open

shl causes undefined behaviour #19983

arnetheduck opened this issue Jul 7, 2022 · 3 comments

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Jul 7, 2022

var x = 10000
echo x shl 1000
       T1_[0] = dollar___systemZdollars_3((NI)((NU64)(x__testit_1) << (NU64)(((NI) 1000))));

Shifting by more than sizeof(x) * 8 is undefined behaviour in C - moreover, the conversions between int and uint here are quite wonky.
Nim can either flag the behaviour or do min(sizeof(x) * 8, shift) which is probably what is intended for large shifts

nim 1.6.6

@arnetheduck
Copy link
Contributor Author

ditto shifting by negative numbers

@RSDuck
Copy link
Contributor

RSDuck commented Jul 7, 2022

Nim can either flag the behaviour or do min(sizeof(x) * 8, shift) which is probably what is intended for large shifts

it probably makes more sense to mask shifts (shift and (sizeof(x)*8-1)) as pretty much all processor architectures do this (except for 32-bit ARM) and C compilers will just optimise the and away.

@metagn
Copy link
Collaborator

metagn commented Jul 10, 2022

See also #11555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants