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

Consider to implement left shift operator by multiplication in C++ #671

Closed
mikir opened this issue Nov 26, 2024 · 2 comments
Closed

Consider to implement left shift operator by multiplication in C++ #671

mikir opened this issue Nov 26, 2024 · 2 comments
Assignees
Labels
c++ C++ language generator enhancement New feature or request
Milestone

Comments

@mikir
Copy link
Contributor

mikir commented Nov 26, 2024

The left shift of a signed integer is officially undefined behavior in C++17 and earlier: https://en.cppreference.com/w/cpp/language/operator_arithmetic#Bitwise_shift_operators

In C++20, signed integers are officially two's-complement, so left-shift becomes well defined: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html.

Consider to fix it by generating the following code instead of a << b:

a * (1 << b);
@mikir mikir added enhancement New feature or request c++ C++ language generator labels Nov 26, 2024
@mikir mikir added this to the 2.16 milestone Nov 26, 2024
@Mi-La Mi-La self-assigned this Dec 2, 2024
@Mi-La
Copy link
Contributor

Mi-La commented Dec 2, 2024

@Mi-La
Copy link
Contributor

Mi-La commented Dec 3, 2024

Note that it seems that modern compilers can optimize multiplication with power of two - see in godbolt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ C++ language generator enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants