An implementation of single-precision tanpi() for CUDA

When extending the math library for a compute platform beyond relevant standards, it is usually a good idea to base this on practical usage patterns, rather than on the wholesale adoption of entire groups of new functions, some of which may go unused.

This is how the CUDA standard math library wound up with functions sinpi(), cospi(), and sincospi(), where the “pi” suffix of the name indicates that the trig function argument is internally multiplied by π. For example, these functions allow the computation of the sine and cosine of degree-based angles with high accuracy.

I had never come across an idiomatic usage pattern suggesting the need for a tanpi() function, until a week ago I found myself looking at the Cauchy distribution (see, for example https://en.wikipedia.org/wiki/Cauchy_distribution), and noticed that tan (π*x) occurs in the inverse of its CDF (cumulative distribution function). So I set out to implement this, in single precision only for now. Below is what I believe is a credible initial effort, tested exhaustive with CUDA 8.0 in default compilation mode. Maybe some other CUDA user finds this useful. It should be noted that tanpi() has been part of the OpenCL standard math library ever since that came into existence.

[code below updated 7/26/2017]

/*
  Copyright (c) 2017, 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.
*/

/* Argument reduction for sinpi, cospi, tanpi. Reduces to [-0.25, +0.25] */
__device__ __forceinline__ float trig_red_pi_f (float a, int *i)
{
    float r;

    r = rintf (a + a);
    *i = (int)r;
    r = fmaf (-0.5f, r, a);
    return r;
}

/* Mathematically, tanpi(x) = tan(π*x). Multiplication with π is implicit,
   resulting in higher performance and accuracy compared to the discrete 
   computation.

   Special case handling is based on tanpi(x) = sinpi(x) / cospi(x), where the
   special case behavior of these two functions is defined by IEEE 754 (2008). 
   For x >= 0, tanpi(n) is -0 if n odd, +0 otherwise; tanpi (n+0.5) is -INF if
   n odd, +INF otherwise. As tangent is an odd function, tanpi(-x) = -tanpi(x)

   maximum error 1.60536 ulp
*/
__device__ __forceinline__ float my_tanpif (float a)
{
    float e, p, r, s, t;
    int i;

    r = trig_red_pi_f (a, &i);
    e = (((i + 1) & 2) ? (-0.0f) : 0.0f); // needed for exceptional cases

    s = r * r;
    p =             1.25408000e+5f;  // 0x1.e9e000p+16
    p = fmaf (p, s, 2.59904968e+2f); // 0x1.03e7acp+8
    p = fmaf (p, s, 3.18810547e+3f); // 0x1.8e8360p+11
    p = fmaf (p, s, 6.34397766e+2f); // 0x1.3d32eap+9
    p = fmaf (p, s, 1.63275925e+2f); // 0x1.468d46p+7
    p = fmaf (p, s, 4.08006248e+1f); // 0x1.4667aep+5
    p = fmaf (p, s, 1.03354301e+1f); // 0x1.4abbd8p+3
    t = s * r;
    t = fmaf (p, t, -8.74227766e-8f * r); // PI_lo // -0x1.777a5cp-24
    r = fmaf (r, 3.14159274f, t);         // PI_hi //  0x1.921fb6p+1

    /* handle half-integer arguments */
    if (r == 0.0f) r = e;

    if (i & 1) r = -r;  // prevent contraction into division, since slower
    if (i & 1) r = 1.0f / r;

    /* handle integer arguments */
    if (a == floorf (a)) r = a * e;

    return r;
}

I have updated the code in #1 with a new core approximation to improve the accuracy of the implementation.