More accurate and somewhat faster implementation of atanf()

A recent Q&A on Stackoverflow on fast implementations of atanf() prompted me to take a look at the implementation provided by CUDA 11. Without any fancy algorithms, simply by focusing on the use of MUFU.RCP and tuning the coefficients of the core approximation accordingly, I was able to reduce maximum error from 2.05199 ulps in CUDA 11 to 1.32278 ulps in my own implementation, which means that my_atanf() never differs by more than 1 ulp from a correctly-rounded single-precision result. As for performance, I observe a speedup between 6% and 13% on a Turing-class GPU (Quadro RTX 4000) depending on argument magnitude.

[Code below updated 1/15/2023, 2/19/2023]

/*
  Copyright (c) 2022-2023, Norbert Juffa
  All rights reserved.

  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.
*/

/* Core approximation for arctangent. Approximate atan(a) on [-1,1] */
__forceinline__ __device__ float atanf_poly (float a)
{
    float r, s, t;

    s = a * a;
    r =              2.74944305e-3f;  //  0x1.686000p-9
    r = fmaf (r, s, -1.57383122e-2f); // -0x1.01db44p-6
    r = fmaf (r, s,  4.23044525e-2f); //  0x1.5a8edep-5
    r = fmaf (r, s, -7.48807490e-2f); // -0x1.32b628p-4
    r = fmaf (r, s,  1.06435075e-1f); //  0x1.b3f544p-4
    r = fmaf (r, s, -1.42076507e-1f); // -0x1.22f902p-3
    r = fmaf (r, s,  1.99936226e-1f); //  0x1.99782ap-3
    r = fmaf (r, s, -3.33331466e-1f); // -0x1.5554d8p-2
    t = s * a;
    r = fmaf (r, t, a);
    return r;
}

/* Use MUFU.RCP directly */
__forceinline__ __device__ float rcp_approx_gpu (float divisor)
{
    float r;
    asm ("rcp.approx.ftz.f32 %0,%1;\n\t" : "=f"(r) : "f"(divisor));
    return r;
}

/* Transfer sign of second argument to (positive!) first argument */
__forceinline__ __device__ float copysignf_pos (float a, float b)
{
    return __int_as_float((__float_as_int(a) | (__float_as_int(b) & 0x80000000)));
}

/* Compute arctangent with a maximum error of 1.33068 ulps */
__device__ float my_atanf (float a)
{
    float r, t;

    t = fabsf (a);
    r = t;
    if (t > 1.0f) {
        r = rcp_approx_gpu (r);
    }
    r = atanf_poly (r);
    if (t > 1.0f) {
        r = fmaf (0x1.ddcb02p-1f, 0x1.aee9d6p+0f, -r); // pi/2 - r
    }
    if (t <= INFINITY) {
        r = copysignf_pos (r, a);
    }
    return r;
}
4 Likes