public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/63644] New: Kahan Summation with fast-math, pattern not always recognized
@ 2014-10-25 20:14 vincenzo.innocente at cern dot ch
  0 siblings, 0 replies; only message in thread
From: vincenzo.innocente at cern dot ch @ 2014-10-25 20:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 63644
           Summary: Kahan Summation with fast-math, pattern not always
                    recognized
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincenzo.innocente at cern dot ch

in the following example (compiled with -Ofast -std=c++11) the kahan summation
pattern is recognized in "sum", not in "counter"
see 
http://goo.gl/aJn61B


#include<cstdio>

template<typename T>
struct KahanSum {
  KahanSum(T is=0) : sum(is){}
  KahanSum<T> operator+=(T a) { add(a); return *this;}
  void add(T a) {
    float x = a - eps;
    float s = sum + x;
    eps = (s-sum) - x;
    sum = s;

  }
  T result() const { return sum;}
  T sum;
  T eps=0;

};

float a[1204];
float sum() {
  KahanSum<float> res;
  for (int i=0; i<1024; ++i) res+= a[i];
  return res.result();
}


float counter(int maxl) {
   float tenth=0.1f;
   KahanSum<float> sum = tenth; 
   int n=0;
   while(n<maxl) {
     sum += tenth;
     ++n;
     // if (n<21 || n%36000==0) printf("%d %f
%a\n",n,sum.result(),sum.result());
   }
   // use eps to avoid optimization out

   float count = float(60*60*100*10);
   printf("\n\n%f %f %a\n\n",count,float(count*tenth),float(count*tenth));

   return sum.result();
}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-10-25 12:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-25 20:14 [Bug tree-optimization/63644] New: Kahan Summation with fast-math, pattern not always recognized vincenzo.innocente at cern dot ch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).