r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
Blog post Unsigned Sizes: A Five Year Mistake
https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91
Upvotes
r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
View all comments
Show parent comment
1
u/EggplantExtra4946 May 09 '26 edited May 09 '26
Not quite. This optimization is only valid if we know that MAX <= INTMAX_MAX. The compiler can know that if MAX is a constant but then this optimization would only apply to fixed-size buffer with a capacity known at compile time. The compiler can not know that in general without interval analysis and possibly more if MAX is variable or expression reading from the heap. A simple optimizer doing only straightfoward dataflow optimizations cannot do this optimization.
If MAX > INTMAX_MAX and INTMAX_MAX < x <= MAX, then (unsigned)x <= MAX is true but we also have (signed)x < 0.