https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119543

--- Comment #4 from Kevin Tang <2023152001 at email dot szu.edu.cn> ---
Dear Andrew Pinski,


Thank you very much for your prompt response and especially for providing the
well-defined implementation of the isTmax function.


I appreciate you taking the time not only to explain why my original
implementation was problematic but also to provide a standards-compliant
alternative that preserves the core logic of my function.


The resources you shared about undefined behavior will be very helpful for my
future programming work. This was a valuable lesson in writing robust, portable
C code.


Thank you again for your help!


Best regards,
Kevin Tang

&nbsp;
&nbsp;
------------------&nbsp;Original&nbsp;------------------
From: "pinskia&nbsp;at&nbsp;gcc&nbsp;dot&nbsp;gnu.org"; 
Date: 2025年3月30日(星期天) 下午5:21
To: "唐济宁"<2023152...@email.szu.edu.cn>; 
Subject: [Bug tree-optimization/119543] At -O1 and higher, GCC incorrectly
optimizes isTmax function to always return 0

&nbsp;
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119543

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org&gt; ---
here is an implementation like your original one which is well defined:
```
int isTmax(int x) {
&nbsp; unsigned temp = x;
&nbsp; temp = temp + 1; // do the addition in unsigned rather than signed to
avoid
undefined behavior and get wrapping behavior rather than an overflow
&nbsp; int tmin = 1u << 31; /* 0x80000000 - Minimum two's complement value */
&nbsp; int t = temp ^ tmin;
&nbsp; int y = !t;
&nbsp; return y;
}
```

Reply via email to