简体   繁体   English

FLT_EPSILON,用于带有SSE / AVX的第n个根查找器

[英]FLT_EPSILON for a nth root finder with SSE/AVX

I'm trying to convert a function that finds the nth root in C for a double value from the following link http://rosettacode.org/wiki/Nth_root#C to find the nth root for 8 floats at once using AVX. 我正在尝试转换一个函数,该函数可从以下链接http://rosettacode.org/wiki/Nth_root#C中找到C中的第n个根作为双精度值,以使用AVX一次找到8个浮点数的第n个根。

Part of that code uses DBL_EPSILON * 10. However, when I convert this to use float with AVX I have to use FLT_EPSILON*1000 or the code hangs and does not converge. 该代码的一部分使用DBL_EPSILON *10。但是,当我将其转换为在AVX中使用float时,我必须使用FLT_EPSILON * 1000或代码挂起并且无法收敛。 When I print out FLT_EPSILON I see it is order 1E-7. 当我打印出FLT_EPSILON时,看到的是订单1E-7。 But this link, http://www.cplusplus.com/reference/cfloat/ , says it should be 1E-5. 但是此链接http://www.cplusplus.com/reference/cfloat/表示应该为1E-5。 When I print out DBL_EPSILON it's 1E-16 but the link says it should only be 1E-9. 当我打印出DBL_EPSILON时,它是1E-16,但是链接说它应该只是1E-9。 What's going on? 这是怎么回事?

Here is the code so far (not fully optimized). 这是到目前为止的代码(尚未完全优化)。

#include <stdio.h>
#include <float.h>
#include <immintrin.h>                 // AVX

inline double abs_(double x) { 
    return x >= 0 ? x : -x; 
}

double pow_(double x, int e)
{
    double ret = 1;
    for (ret = 1; e; x *= x, e >>= 1) {
        if ((e & 1)) ret *= x;
    }
    return ret;
}

double root(double a, int n)
{
    double d, x = 1;
    x = a/n;
    if (!a) return 0;
    //if (n < 1 || (a < 0 && !(n&1))) return 0./0.; /* NaN */

    int cnt = 0;
    do {
        cnt++;  
        d = (a / pow_(x, n - 1) - x) / n;
        x+= d;
    } while (abs_(d) >= abs_(x) * (DBL_EPSILON * 10));
    printf("%d\n", cnt);    

    return x;
}


__m256 pow_avx(__m256 x, int e) {
    __m256 ret = _mm256_set1_ps(1.0f);
    for (; e; x = _mm256_mul_ps(x,x), e >>= 1) {
        if ((e & 1)) ret = _mm256_mul_ps(x,ret);
    }
    return ret;
}

inline __m256 abs_avx (__m256 x) { 
    return _mm256_max_ps(_mm256_sub_ps(_mm256_setzero_ps(), x), x);
    //return x >= 0 ? x : -x; 
}

int get_mask(const __m256 d, const __m256 x) {
    __m256 ad = abs_avx(d);
    __m256 ax = abs_avx(x);
    __m256i mask = _mm256_castps_si256(_mm256_cmp_ps(ad, ax, _CMP_GT_OQ));
    return _mm_movemask_epi8(_mm256_castsi256_si128(mask)) + _mm_movemask_epi8(_mm256_extractf128_si256(mask,1));
}

__m256 root_avx(__m256 a, int n) {
    printf("%e\n", FLT_EPSILON);
    printf("%e\n", DBL_EPSILON);
    printf("%e\n", FLT_EPSILON*1000.0f);
    __m256 d;
    __m256 x = _mm256_set1_ps(1.0f);
    //if (!a) return 0;
    //if (n < 1 || (a < 0 && !(n&1))) return 0./0.; /* NaN */
    __m256 in = _mm256_set1_ps(1.0f/n);
    __m256 xtmp;
    do {
        d = _mm256_rcp_ps(pow_avx(x, n - 1));
        d = _mm256_sub_ps(_mm256_mul_ps(a,d),x);
        d = _mm256_mul_ps(d,in);
        //d = (a / pow_avx(x, n - 1) - x) / n;
        x = _mm256_add_ps(x, d); //x+= d;
        xtmp =_mm256_mul_ps(x, _mm256_set1_ps(FLT_EPSILON*100.0f));
    //} while (abs_(d) >= abs_(x) * (DBL_EPSILON * 10));
    } while (get_mask(d, xtmp)); 
    return x;
}

int main()
{
    __m256 d = _mm256_set1_ps(16.0f);
    __m256 out = root_avx(d, 4);
    float result[8];
    int i;
    _mm256_storeu_ps(result, out);

    for(i=0; i<8; i++) {
        printf("%f\n", result[i]);
    } printf("\n");

    //double x = 16;
    //printf("root(%g, 15) = %g\n", x, root(x, 4));

    //double x = pow_(-3.14159, 15);
    //printf("root(%g, 15) = %g\n", x, root(x, 15));
    return 0;
}

_mm256_rcp_ps , which maps to the rcpps instruction, performs only an approximate reciprocal. _mm256_rcp_ps映射到rcpps指令,仅执行近似倒数。 The Intel 64 and IA-32 Architectures Software Developer's Manual says its relative error may be up to 1.5•2 -12 . 英特尔64和IA-32体系结构软件开发人员手册说,其相对误差可能高达1.5•2 -12 This is insufficient to cause the root finder to converge with accuracy 100*FLT_EPSILON . 这不足以导致根查找器以100*FLT_EPSILON精度收敛。

You could use an exact division, such as: 您可以使用精确的除法,例如:

d = pow_avx(x, n-1);
d = _mm256_sub_ps(_mm256_div_ps(a, d), x);

or add some refinement steps for the reciprocal estimate. 或为倒数估算添加一些优化步骤。

Incidentally, if your compiler supports using regular C operators with SIMD objects, consider using the regular C operators instead: 顺便说一句,如果您的编译器支持对SIMD对象使用常规C运算符,请考虑改用常规C运算符:

d = pow_avx(x, n-1);
d = a/d - x;

1e-5 is simply the maximum value the C standard allows an implementation to use for FLT_EPSILON . 1e-5只是C标准允许实现用于FLT_EPSILON In practice, you'll be using IEEE-754 single-precision, which has an epsilon of 2 -23 , which is approximately 1e-7 . 实际上,您将使用IEEE-754单精度,其epsilon为2 -23 ,大约为1e-7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM