Adjustment to integer division in int_div_impl.inc to avoid undefined behaviour that can occur as a result of having INT_MIN as one of the parameters.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
compiler-rt/lib/builtins/int_div_impl.inc | ||
---|---|---|
77 | What is the purpose of using addition+negation instead of subtraction? |
compiler-rt/lib/builtins/int_div_impl.inc | ||
---|---|---|
77 | (fixuint_t)a^s_a will always be some unsigned value, and with just subtraction we have <unsigned> - s_a. And when s_a = -1, s_a will get promoted to an unsigned value UINT_MAX, resulting in <unsigned> - UINT_MAX which can result in undefined behaviour. By negating s_a first we'd ensure that the operation there is always just either +0 or +1. |
What is the purpose of using addition+negation instead of subtraction?