https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88542

            Bug ID: 88542
           Summary: Optimize symmetric range check
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzi...@poradnik-webmastera.com
  Target Milestone: ---

[code]
#include <math.h>

bool test1(double d, double max)
{
    return (d < max) && (d > -max);
}

bool test2(double d, double max)
{
    return fabs(d) < max;
}
[/code]

When code checks if some number d is in (or outside of) symmetric range like
(-max, max), code from test1() can be replaced with one from test2(). This of
course assumes that expression does not produce any side effects. This can be
done nicely for floating point numbers stored in IEEE format, what leads to
faster code:

[asm]
test1(double, double):
        vcomisd xmm1, xmm0
        jbe     .L6
        vxorpd  xmm1, xmm1, XMMWORD PTR .LC0[rip]
        vcomisd xmm0, xmm1
        seta    al
        ret
.L6:
        xor     eax, eax
        ret
test2(double, double):
        vandpd  xmm0, xmm0, XMMWORD PTR .LC1[rip]
        vcomisd xmm1, xmm0
        seta    al
        ret
[/asm]

For integer types stored in two's complement format similar change gives slower
code. However on platforms which uses different integer format with dedicated
sign bit this optimizations may be beneficial.

Reply via email to