[PATCH, i386]: Expand round(a) as sgn(a) * floor(fabs(a) + 0.5) for x87 math

Richard Guenther rguenther@suse.de
Tue Aug 9 09:36:00 GMT 2011


On Mon, 8 Aug 2011, Uros Bizjak wrote:

> Hello!
> 
> One notable omission from x87 rounding sequences is plain round(),
> since x87 does not support round-to-nearest, away-from-zero directly.
> However, this function can be synthesized as round(a) = sgn(a) *
> floor(fabs(a) + 0.5). Fortunately, we have all building blocks ready
> in i386.md, we should just wire them correctly.
> 
> The x87 expansion is protected with flag_unsafe_math_optimizations,
> since the input range is reduced (true?) due to addition - OTOH, insn
> that calculates floor is protected by this flag anyway.
> 
> 2011-08-08  Uros Bizjak  <ubizjak@gmail.com>
> 
> 	* config/i386/i386.c (ix86_emit_i387_round): New function.
> 	* config/i386/i386-protos.h (ix86_emit_i387_round): Declare.
> 	* config/i386/i386.md (round<mode>2): Use X87MODEF mode iterator.
> 	Use ix86_emit_i387_round to expand round function for i387 math.
> 
> Patch was bootstrapped and regression tested on x86_64-pc-linux-gnu.
> It also computes correctly +-NaN, +-Inf and all inputs I have thrown
> in.
> 
> Any comments?

Can't you do sth similar to ix86_expand_round instead,
using trunc (fabs (a) + nextafter (0.5, 0.0)) * sgn(a)
(not sure with the sign multiply, copysign would be better)?

The addition doesn't really reduce the range if you guard the
round by a proper conditional like ix86_expand_round does.

Richard.



More information about the Gcc-patches mailing list