A faster double-precision exp()

One thing I learned over the years is that as there are enough scientific and financial applications that make heavy use of exp(), that this function can never be fast enough.

I recently investigated some aspects of math functions across multiple platforms, and thought I would share my CUDA version of double-precision exp(). On my lowly Quadro 2000 (sm_21) this has about 11% higher throughput than the CUDA 6.5 implementation. Performance gains should be similar on other “DP lite” platforms, as the speedup is from minimizing the use of double-precision operations. For architectures with fast double precision, the performance gain is probably a few percent only. The accuracy is equivalent to the CUDA 6.5 implementation and can probably be improved a little bit more, but finding more accurate core approximations is hard.

I am placing this code under a 2-clause BSD license which is OSI-approved.

Code below updated 2/11/2017, 9/4/2017

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

/* Compute exponential function. maximum ulp error observed = 0.89028 */
__device__ __forceinline__ double my_exp (double a)
{
    const double ln2_hi = 6.9314718055829871e-01;
    const double ln2_lo = 1.6465949582897082e-12;
    const double l2e = 1.4426950408889634; // log2(e)
    const double cvt = 6755399441055744.0; // 3 * 2**51
    double f, j, p, r;
    int i;

    // exp(a) = 2**i * exp(f); i = rint (a / log(2))
    j = fma (l2e, a, cvt);
    i = __double2loint (j);
    j = j - cvt;
    f = fma (j, -ln2_hi, a);
    f = fma (j, -ln2_lo, f);

    // approximate p = exp(f) on interval [-log(2)/2, +log(2)/2]
    p =            2.5022018235176802e-8;
    p = fma (p, f, 2.7630903481118922e-7);
    p = fma (p, f, 2.7557514543922205e-6);
    p = fma (p, f, 2.4801491039429033e-5);
    p = fma (p, f, 1.9841269589083001e-4);
    p = fma (p, f, 1.3888888945916664e-3);
    p = fma (p, f, 8.3333333334557492e-3);
    p = fma (p, f, 4.1666666666519782e-2);
    p = fma (p, f, 1.6666666666666477e-1);
    p = fma (p, f, 5.0000000000000122e-1);
    p = fma (p, f, 1.0000000000000000e+0);
    p = fma (p, f, 1.0000000000000000e+0);

    // exp(a) = 2**i * exp(f);
    int rlo = __double2loint (p);
    int rhi = (i << 20) + __double2hiint (p);
    r = __hiloint2double (rhi, rlo);

    // handle special cases
    int ia = __double2hiint (a);
    int ib = __double2hiint (708.0); // |a| >= 708 requires double scaling
    int ic = __double2hiint (746.0); // |a| >= 746 severe overflow / underflow
    float fa = __int_as_float (ia);
    float fb = __int_as_float (ib);
    float fc = __int_as_float (ic);

    if (! (fabsf (fa) < fb)) { // !(|a| < 708)
        i = (i > 0) ?  0 : 0x80300000;
        r = __hiloint2double (0x7fe00000 + i, 0);
        r = r * __hiloint2double (rhi - i - 0x3ff00000, rlo);
        if (! (fabsf (fa) < fc)) { // !(|a| < 746)
            r = __hiloint2double ((ia > 0) ? 0x7ff00000 : 0, 0); // +INF, +0
            if (isnan (a)) r = a + a;
        }
    }
    return r;
}

You should really provide a Bitcoin or paypal donation address when posting such gems.

Thanks for posting!

Have a use for this in for some of my Monte Carlo simulation code.

CUDA 7.5 double-precision exp should be faster for architectures with fast double precision.
Release Candidate is now available.
[url]https://developer.nvidia.com/cuda-toolkit[/url]

I have reworked the range check slightly. The correct cut-off is 746, since exp(-746.0) == 0. I have also added an alternative range check using tricky comparison via ‘float’ as that provides the absolute value for free as part of the FSET instruction, and thus saves one instruction. This may provide a performance benefit on some architectures. Based on my measurements not on sm_21, tough.

I have adjusted the coefficients of the core approximation to reduce the maximum error a bit more. Across 4 billion test vectors, the maximum error observed is now less than 0.894 ulps. Based on the equivalent single-precision code, it is probably possible to find a set of coefficients that pushes the maximum error below 0.89 ulps, but I know of no good heuristic that would achieve that in any reasonable time frame. For the record, the two test vectors with the largest ulp error found were:

ulp err = 0.89126 @ -3.8222901322785963e+0
ulp err = 0.89390 @ +1.2130297309009944e+1

As skierat pointed out, in CUDA 7.5 the performance of double-precision exp() was improved. The code I posted above now is only 6.5% faster than CUDA’s built-in exp(), as measured on a Quadro K2200 (sm_50).

Based on my recent simplifications to my_expf() [[url]https://devtalk.nvidia.com/default/topic/990115/cuda-programming-and-performance/a-more-accurate-performance-competitive-implementation-of-expf-/[/url]], I have streamlined the exponent scaling and special case handling for my_exp() above, reducing code size and tightening the register footprint (although the latter is a bit at the mercy of the PTXAS register allocator).

At least on my Quadro K2200 (sm_50), where DP operations limit the code, the performance of this code is identical to the built-in exp() of CUDA 8.0, pointing to performance improvements between CUDA 7.5 and CUDA 8.0. Whether my_exp() could be a tad faster than CUDA’s built-in function on GPUs with full double-precision support (e.g. sm35) I cannot say; however there seems to be little reason at this point to use a custom implementation over that from CUDA 8.

Improved core approximation, reducing maximum observed error to 0.89028 ulp (observed across 4 billion trials). Maximum error occurs for an input of 50.261874612897010.