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(); }