Skip to content
Snippets Groups Projects
  • Jakub Jelinek's avatar
    36f4c8a9
    libgcc: Another __divmodbitint4 bug fix [PR114762] · 36f4c8a9
    Jakub Jelinek authored
    The following testcase is miscompiled because the code to decrement
    vn on negative value with all ones in most significant limb (even partial)
    and 0 in most significant bit of the second most significant limb doesn't
    take into account the case where all bits below the most significant limb
    are zero.  This has been a problem both in the version before yesterday's
    commit where it has been done only if un was one shorter than vn before this
    decrement, and is now problem even more often when it is done earlier.
    When we decrement vn in such case and negate it, we end up with all 0s in
    the v2 value, so have both the problems with UB on __builtin_clz* and the
    expectations of the algorithm that the divisor has most significant bit set
    after shifting, plus when the decremented vn is 1 it can SIGFPE on division
    by zero even when it is not division by zero etc.  Other values shouldn't
    get 0 in the new most significant limb after negation, because the
    bitint_reduce_prec canonicalization should reduce prec if the second most
    significant limb is all ones and if that limb is all zeros, if at least
    one limb below it is non-zero, carry in will make it non-zero.
    
    The following patch fixes it by checking if at least one bit below the
    most significant limb is non-zero, in that case it decrements, otherwise
    it will do nothing (but e.g. for the un < vn case that also means the
    divisor is large enough that the result should be q 0 r u).
    
    2024-04-18  Jakub Jelinek  <jakub@redhat.com>
    
    	PR libgcc/114762
    	* libgcc2.c (__divmodbitint4): Perform the decrement on negative
    	v with most significant limb all ones and the second least
    	significant limb with most significant bit clear always, regardless of
    	un < vn.
    
    	* gcc.dg/torture/bitint-70.c: New test.
    36f4c8a9
    History
    libgcc: Another __divmodbitint4 bug fix [PR114762]
    Jakub Jelinek authored
    The following testcase is miscompiled because the code to decrement
    vn on negative value with all ones in most significant limb (even partial)
    and 0 in most significant bit of the second most significant limb doesn't
    take into account the case where all bits below the most significant limb
    are zero.  This has been a problem both in the version before yesterday's
    commit where it has been done only if un was one shorter than vn before this
    decrement, and is now problem even more often when it is done earlier.
    When we decrement vn in such case and negate it, we end up with all 0s in
    the v2 value, so have both the problems with UB on __builtin_clz* and the
    expectations of the algorithm that the divisor has most significant bit set
    after shifting, plus when the decremented vn is 1 it can SIGFPE on division
    by zero even when it is not division by zero etc.  Other values shouldn't
    get 0 in the new most significant limb after negation, because the
    bitint_reduce_prec canonicalization should reduce prec if the second most
    significant limb is all ones and if that limb is all zeros, if at least
    one limb below it is non-zero, carry in will make it non-zero.
    
    The following patch fixes it by checking if at least one bit below the
    most significant limb is non-zero, in that case it decrements, otherwise
    it will do nothing (but e.g. for the un < vn case that also means the
    divisor is large enough that the result should be q 0 r u).
    
    2024-04-18  Jakub Jelinek  <jakub@redhat.com>
    
    	PR libgcc/114762
    	* libgcc2.c (__divmodbitint4): Perform the decrement on negative
    	v with most significant limb all ones and the second least
    	significant limb with most significant bit clear always, regardless of
    	un < vn.
    
    	* gcc.dg/torture/bitint-70.c: New test.