Faithfully-rounded implementation of log10f(), without performance penalty

As I continue to work my way through the report on the accuracy of math library functions by Innocente and Zimmermann, log10f seemed an obvious target for improvements. I managed to reduce the maximum error from 2.08518 ulps in CUDA 11 to 0.95920 ulps for my implementation below. On my sm_75 GPU, the performance of my_log10f() equals that of the CUDA 11’s built-in log10f() function.

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

/* logarithm base 10; maxulp=0.95920  ulp1=12844035 */
__inline__ __device__ float my_log10f (float a)
{
    const float TWO_TO_23 = 8388608.0f; // 0x1.0p+23
    const float TWO_TO_M23 = 1.19209290e-7f; // 0x1.0p-23
    const float TWO_TO_M126 = 1.175494351e-38f; // 0x1.0p-126
    const float LOG10_TWO_HI = 3.01025391e-1f; // 0x1.344000p-2
    const float LOG10_TWO_LO = 4.60503907e-6f; // 0x1.3509f8p-18
    const float INF_F = __int_as_float (0x7f800000); // infinity
    float f, m, r, i, t;
    int e;

    i = 0.0f;
    if (a < TWO_TO_M126) {
        a = a * TWO_TO_23;
        i = -23.0f;
    }
    e = (__float_as_int (a) - 0x3f3504f3) & 0xff800000;
    m = __int_as_float (__float_as_int (a) - e);
    i = fmaf ((float)e, TWO_TO_M23, i);
    f = m - 1.0f;

    /* Compute log10_1p(m) for m in [sqrt(0.5)-1, sqrt(2.0)-1] */
    r =             -3.30963135e-2f;  // -0x1.0f2000p-5
    r = fmaf (r, f,  5.60370684e-2f); //  0x1.cb0e40p-5
    r = fmaf (r, f, -5.75154796e-2f); // -0x1.d72ab4p-5
    r = fmaf (r, f,  6.15895353e-2f); //  0x1.f88a9ep-5
    r = fmaf (r, f, -7.21328035e-2f); // -0x1.2774bap-4
    r = fmaf (r, f,  8.68686363e-2f); //  0x1.63d05ep-4
    r = fmaf (r, f, -1.08580485e-1f); // -0x1.bcbee4p-4
    r = fmaf (r, f,  1.44764677e-1f); //  0x1.287a62p-3
    r = fmaf (r, f, -2.17147186e-1f); // -0x1.bcb7aap-3
    r = fmaf (r, f, -6.57055154e-2f); // -0x1.0d213ap-4

    /* log10(a) = i*log10(2) + log10(m) */
    t = fmaf (LOG10_TWO_LO, i, 0.5f * f);
    r = fmaf (r, f, t);
    r = fmaf (LOG10_TWO_HI, i, r);

    /* handle special cases: INFs, NANs, zeros, negative arguments */
    if (!((a > 0.0f) && (a < INF_F))) {
        asm ("lg2.approx.ftz.f32 %0,%1;" : "=f"(r) : "f"(a));
    }
    return r;
}
1 Like