Kahan summation algorithm has big computing error when it is compiled by GCC

前端 未结 1 779
攒了一身酷
攒了一身酷 2021-01-14 05:58

I use Kahan summation algorithm:

inline void KahanSum(float value, float & sum, float & correction)
{
    float term = value - correction;
    fl         


        
相关标签:
1条回答
  • 2021-01-14 06:21

    I suppouse, that it's a result of aggresive compiler optimization. So GCC can reduce the expression from:

        float term = value - correction;
        float temp = sum + term;
        correction = (temp - sum) - term;
        sum = temp;
    

    to

        float term = value - correction;
        correction = 0;
        sum += term;
    

    because this transformation is mathematically correct, but this optimization kills Kahan algorithm.

    In order to avoid this problem you can use "-O1" GCC compiler options to compile the code. It will be something like this:

    #if defined(__GNUC__)
    #  pragma GCC push_options
    #  pragma GCC optimize ("O1")
    #endif 
    inline void KahanSum(float value, float & sum, float & correction)
    {
        float term = value - correction;
        float temp = sum + term;
        correction = (temp - sum) - term;
        sum = temp; 
    }
    
    float KahanSum(const float * ptr, size_t size)
    {
        float sum = 0, correction = 0;
        for(size_t i = 0; i < size; ++i)
            KahanSum(ptr[i], sum, correction);
        return sum;
    }
    #if defined(__GNUC__)
    #  pragma GCC pop_options
    #endif 
    
    0 讨论(0)
提交回复
热议问题