[PATCH] D90218: Fix PR47973: Addressing integer division edge case with INT_MIN
    Ayshe Kuran via Phabricator via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Tue Nov 10 02:40:33 PST 2020
    
    
  
akuran added inline comments.
================
Comment at: compiler-rt/lib/builtins/int_div_impl.inc:77
+  fixint_t s_b = b >> N;                            // s_b = b < 0 ? -1 : 0
+  fixuint_t a_u = (fixuint_t)(a ^ s_a) + (-s_a);    // negate if s_a == -1
+  fixuint_t b_u = (fixuint_t)(b ^ s_b) + (-s_b);    // negate if s_b == -1
----------------
sepavloff wrote:
> What is the purpose of using addition+negation instead of subtraction?
(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.
Repository:
  rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90218/new/
https://reviews.llvm.org/D90218
    
    
More information about the llvm-commits
mailing list