Fix old bug in div_and_round_double

Eric Botcazou ebotcazou@adacore.com
Tue May 27 20:31:00 GMT 2014


This is an old bug in div_and_round_double for ROUND_DIV_EXPR: when the code 
detects that it needs to adjust the quotient, it needs to decide whether it 
increases or decreases it by 1.  This only depends on the expected sign of the 
quotient, but the test reads:

	    if (*hquo < 0)

So if the quotient is 0, the code will always bump it, thus yielding 1, even 
if the expected quotient is negative.  This causes the attached Ada testcase 
to fail at -O on all active branches... except for mainline, because of the 
correct implementation of the same test in wi::div_round:

	      if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
		return quotient - 1;
	      else
		return quotient + 1;

It turns out that div_and_round_double has a predicate quo_neg that implements 
the same test as the wide-int one and ought to be used here.

Tested on x86_64-suse-linux (of course this probably doesn't mean anything on 
the mainline, but it was also test with 4.7 and 4.9 compilers) and applied on 
the mainline as obvious.


2014-05-27  Eric Botcazou  <ebotcazou@adacore.com>

	* double-int.c (div_and_round_double) <ROUND_DIV_EXPR>: Use the proper
	predicate to detect a negative quotient.


2014-05-27  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/overflow_fixed.adb: New test.


-- 
Eric Botcazou
-------------- next part --------------
A non-text attachment was scrubbed...
Name: p.diff
Type: text/x-patch
Size: 453 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20140527/dfcc546c/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: overflow_fixed.adb
Type: text/x-adasrc
Size: 364 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20140527/dfcc546c/attachment-0001.bin>


More information about the Gcc-patches mailing list