Accuracy-optimized implementation of erfcinvf(), without performance impact

The function erfcinvf() is primarily used for the computation of quantiles of the standard normal distribution, and thus the building block from normcdfinvf(). It is somewhat difficult to make this accurate when high performance is required.

The maximum error of erfcinvf() in CUDA is currently 3.955166 ulps. Below I show how to reduce this to 3.809635 ulps without performance impact (MORE_ACCURATE = 0), and how to reduce this even further to 3.405854 ulps with one additional instruction (MORE_ACCURATE=1) for a performance impact of about -2% to -3%.

/*
  Copyright 2023, Norbert Juffa

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

__forceinline__ __device__ float raw_rcp (float a)
{
    float r;
    asm ("rcp.approx.ftz.f32 %0,%1;" : "=f"(r) : "f"(a));
    return r;
}

__forceinline__ __device__ float raw_rsqrt (float a)
{
    float r;
    asm ("rsqrt.approx.ftz.f32 %0,%1;" : "=f"(r) : "f"(a));
    return r;
}

__forceinline__ __device__ float raw_lg2 (float a)
{
    float r;
    asm ("lg2.approx.ftz.f32 %0,%1;" : "=f"(r) : "f"(a));
    return r;
}

// MORE_ACCURATE=0: maximum error = 3.809635 ulps
// MORE_ACCURATE=1: maximum error = 3.405854 ulps
__device__  __forceinline__ float my_erfcinvf (float a)
{
    float p, t;
    t = __fsub_rn (2.0f, a);
    if ((a >= 2.1875e-3f) && (a <= 1.998125f)) {
        t = __fmul_rn (t, a);
        t = - raw_lg2 (t);
        p =             -2.00998329e-10f;  // -0x1.ba0000p-33
        p = fmaf (p, t,  7.63788321e-09f); //  0x1.066f88p-27
        p = fmaf (p, t, -9.44120231e-08f); // -0x1.957f1ep-24
        p = fmaf (p, t,  1.27279494e-08f); //  0x1.b5543ap-27
        p = fmaf (p, t,  8.98369126e-06f); //  0x1.2d7152p-17
        p = fmaf (p, t, -3.40910883e-05f); // -0x1.1dfa0ep-15
        p = fmaf (p, t, -7.70850747e-04f); // -0x1.9425d6p-11
        p = fmaf (p, t,  5.54407062e-03f); //  0x1.6b5612p-8
        p = fmaf (p, t,  1.60820886e-01f); //  0x1.495c76p-3
        t = fmaf (p, t,  8.86226892e-01f); //  0x1.c5bf88p-1
        t = fmaf (t, -a, t);
    } else {
        t = (a > 1.0f) ? t : a;
        t = __log2f (t);
        t = raw_rsqrt (-t);
#if MORE_ACCURATE
        p =              1.16093750e+2f;  //  0x1.d06000p+6
        p = fmaf (p, t, -2.26577423e+2f); // -0x1.c527a4p+7
        p = fmaf (p, t,  1.96353668e+2f); //  0x1.88b514p+7
        p = fmaf (p, t, -1.00602257e+2f); // -0x1.9268b6p+6
        p = fmaf (p, t,  3.46447067e+1f); //  0x1.15285cp+5
        p = fmaf (p, t, -9.10134983e+0f); // -0x1.233e42p+3
        p = fmaf (p, t,  2.65257812e+0f); //  0x1.5387aep+1
        p = fmaf (p, t,  4.29913402e-2f); //  0x1.602f60p-5
        p = fmaf (p, t, -4.46814462e-4f); // -0x1.d484d8p-12
        p = fmaf (1.201122409f, t, p * t);//  0x1.337cc2p+0
        if (a > 1.0f) p = -p;
        t = raw_rcp (p);
#else // MORE_ACCURATE
        p =             -6.31093750e+1f;  // -0x1.f8e000p+5
        p = fmaf (p, t,  1.27482025e+2f); //  0x1.fded98p+6
        p = fmaf (p, t, -1.14105965e+2f); // -0x1.c86c82p+6
        p = fmaf (p, t,  6.03263092e+1f); //  0x1.e29c48p+5
        p = fmaf (p, t, -2.17900028e+1f); // -0x1.5ca3dap+4
        p = fmaf (p, t,  6.46741486e+0f); //  0x1.9dea20p+2
        p = fmaf (p, t, -1.83294702e+0f); // -0x1.d53c04p+0
        p = fmaf (p, t, -3.03277727e-2f); // -0x1.f0e3e6p-6
        p = fmaf (p, t,  8.32877457e-1f); //  0x1.aa6eeap-1
        t = raw_rcp (t);
        if (a > 1.0f) t = -t;
        t = p * t;
#endif // MORE_ACCURATE
    }
    return t;
}
1 Like