This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/68105] New: optimizing repeated floating point addition to multiplication


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

            Bug ID: 68105
           Summary: optimizing repeated floating point addition to
                    multiplication
           Product: gcc
           Version: 5.2.1
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zboson at zboson dot net
  Target Milestone: ---

This discussion is based on a question I asked on Stack Overflow
http://stackoverflow.com/questions/33152446/floating-point-multiplication-vs-repeated-addition

Using appropriate compilers options I think GCC should simplify

    //N is a compile time unsigned value
    float sum = 0;
    for(unsigned i=0; i<N; i++) sum += a;  // a is a float

to 

    a*(float)N

For N<6 GCC could do this with e.g. -O3. For N>=6 GCC should do this when
associative math is enabled e.g. with -Ofast

However, GCC only simplifies this in log(N) additions and only for small N. For
example for N = 8 GCC does with -Ofast

    sum = a + a
    sum = sum + sum // (a + a) + (a + a)
    sum = sum + sum // ((a + a) + (a + a)) + ((a + a) + (a + a))

but it could simply do

    8.0f*a;

Not only is this simpler but it's more accurate (which I show in the question
on Stack Overflow)

But even worse is that by N = 32 GCC does every iteration , i.e. N-1 additions.

In addition, the following equations are always true even without associative
math.

    2*a = a + a
    3*a = a + a + a
    4*a = a + a + a + a
    5*a = a + a + a + a + a

It turns out that GCC does simplify a + a + a + a to 4*a but only with
associative math enabled e.g. with -Ofast when it could do it with -O3.

Read the Stack Overflow question for more details.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]