Anticipating future standards: Implementation of exp2m1f()

Per WG 14 N2401, the upcoming ISO-C2x standard (probably C23) will add some new standard math functions, which I would expect to percolate through to the ISO-C++ standard in due course. CUDA already offers some of these, e.g. sinpi().

One new function will be exp2m1f() which computes 2x-1 accurately near unity. This is a companion function to the existing expm1f(), but for exponentiation base 2. Below is my initial effort at a high-performance implementation for CUDA which others may find useful. I had initially tried to craft a faithfully-rounded implementation (i.e. one with a maximum error less than 1 ulp), however that turned out to be harder than I had anticipated.

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

/* 
  Compute exponential base 2 minus 1. Maximum ulp error = 1.203555

  i = rint(a, f = a-i. Then exp2m1(a) = 2**i * (exp2m1(f)+1) - 1. Compute 
  r = exp2m1(f). Then exp2m1(a)= 2 * (0.5 * 2**i * r + 0.5 * 2**i - 0.5). With 
  t = 0.5*2**i, exp2m1(a) = 2*(r * t + t - 0.5). However, for best accuracy,
  when i == 1, exp2m1(a)= 2*(r + 0.5), and when i == 0, exp2m1(a) = r.
*/
__device__ float my_exp2m1f (float a)
{
    const float log2_hi =  6.93147182e-1f; //  0x1.62e430p-1
    const float log2_lo = -1.90465421e-9f; // -0x1.05c610p-29
    float f, j, r, s, t, v, x, y;
    int i;

    // exp2(a) = exp2(i + f); i = rint (a)
    j = fmaf (1.0f, a, 12582912.0f); // 0x1.8p23
    i = __float_as_int (j); // trailing bits contain integer
    j = j - 12582912.0f; // 0x1.8p23
    f = a - j;
    
    // approximate r = exp2(f)-1 on interval [-0.5, +0.5]
    s = f * f;
    r =             1.78217888e-5f;  // 0x1.2b0000p-16
    r = fmaf (r, f, 1.53642643e-4f); // 0x1.423644p-13
    r = fmaf (r, f, 1.33236521e-3f); // 0x1.5d4584p-10
    r = fmaf (r, f, 9.61851049e-3f); // 0x1.3b2deap-7
    r = fmaf (r, f, 5.55042028e-2f); // 0x1.c6b0c0p-5
    r = fmaf (r, f, 2.40226462e-1f); // 0x1.ebfbdap-3f

    // if i == 0, exp2m1(a) = r*(f*f)+f
    // if i == 1, exp2m1(a) = 2*(r*(f*f)+f+0.5)
    // if (i < 0) || (i > 1) exp2m1(a) = 2*((r*(f*f)+f)*t-0.5+t)
    v = (j == 1) ? 
        fmaf (r, s, fmaf (f, log2_hi, 0.5f)) :
        fmaf (f, log2_hi, fmaf (r, s, log2_lo * f));
    if (a == 0.0f) v = a; // ensure -0 is passed through

    s = 0.5f;
    t = __int_as_float ((i << 23) + __float_as_int (s));
    y = t - s;
    x = (t - y) - s; // double-float canonicalization of difference
    r = fmaf (v, t, x) + y;
    r = r + r;
    if (j == 0) r = v;
    if (j == 1) r = v + v;

    // handle special cases
    if (fabsf (a - 1.0f) >= 127.0f) {
        asm ("ex2.approx.ftz.f32 %0,%1;" : "=f"(r) : "f"(a));
        r = fmaf (r, r, -1.0f);
    }
    return r;
}
2 Likes

Thanks njuffa! I have filed RFE 3357377 FYI.