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

Left shift limits #731

Open
lwli11 opened this issue Dec 2, 2024 · 6 comments
Open

Left shift limits #731

lwli11 opened this issue Dec 2, 2024 · 6 comments

Comments

@lwli11
Copy link

lwli11 commented Dec 2, 2024

Is there a max value left shift works on? If I use the code below, it verifies, but if I comment out the line and check if val is <INT_MAX instead, I get an error:

left_shift.c:5:9: error: Undefined behaviour
        return val<<6;
               ~~~^~~
an exceptional condition occurs during the evaluation of an expression (§6.5#5)
#include <limits.h>
int test(int val){
        if (val>=0 && val<9999999){
//      if (val>=0 && val<INT_MAX){
        return val<<6;
        }
        return 0;
}
int main(){
        test(0);
        }

Also, the code in the trace file does not fully appear if I use 'INT_MAX' but does if I use 2147483647.

@PeterSewell
Copy link
Contributor

PeterSewell commented Dec 2, 2024 via email

@lwli11
Copy link
Author

lwli11 commented Dec 2, 2024

Thanks, but I'm not sure I understand the Core syntax in the paper. What's the max value v can be? (It wasn't 2^15 either as I guessed).

@PeterSewell
Copy link
Contributor

PeterSewell commented Dec 2, 2024 via email

@kmemarian
Copy link
Collaborator

The max value of E1 for which the shift is defined depends on E2 and the promoted type of E1. In your example here, the promoted type is signed int and E2 = 6, so the max allowed value for E1 = 0x1FFFFFF (max value such that E1 * 2 ^E2 is representable in signed int).

@kmemarian
Copy link
Collaborator

if the promoted type of E1 was unsigned, any value of E1 would give a defined shift.

@lwli11
Copy link
Author

lwli11 commented Dec 2, 2024

Ok, that makes sense. Thanks!

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