Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
What's in an E-Graph? (bernsteinbear.com)
3 points by dpassens on Sept 11, 2024 | hide | past | favorite | 2 comments


> Consider the expression (a * 2) / 2,

For two's complement integers it's equivalent to (a & ((-1) >>> 1)) for unsigned integers, and to (a+a) >> 1 for the signed ones. It is not equivalent to a, and should not be optimized to it (even if you're optimizing C; both GCC and Clang do it correctly by the way).

> recognizing that expressions of the form (a * b) / b are equivalent to a * (b / b) and therefore equivalent to a

None of those three are equivalent if you use two's complement arithmetic and trap division by zero. If you don't trap division by zero, then a * (b / b) is indeed equivalent to a.

Optimizing integer arithmetic correctly is hard :/


I did not specify the integer size. It could be arbitrary precision like Python.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: