Dumping this here in case someone finds it useful. The performance of this code should be roughly identical to CUDA’s built-in version across all GPU architectures currently in use. The difference is that the maximum error is guaranteed (by exhaustive test) to be smaller than 1 ulp, making it faithfully rounded. The maximum error of CUDA’s current implementation is about 2 ulps.
Only tested with the compiler’s default mode (-ftz=false). Adjustments may be in order when operating in FTZ mode, and will be necessary for non-GPU platforms.
/*
Copyright (c) 2019, 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 logarithm base 10. Max. error = 0.95920 ulp */
__device__ float my_log10f (float a)
{
const float LOG10_TWO_HI_F = 3.01025391e-1f; // 0x1.344000p-2
const float LOG10_TWO_LO_F = 4.60503907e-6f; // 0x1.3509f8p-18
float m, r, i;
int e;
i = 0.0f;
if (a < 1.175494351e-38f){ // 0x1.0p-126
a = a * 8388608.0f; // 0x1.0p+23
i = -23.0f;
}
e = (__float_as_int (a) - 0x3f3504f3) & 0xff800000;
m = __int_as_float (__float_as_int (a) - e);
i = fmaf ((float)e, 1.19209290e-7f, i); // 0x1.0p-23
m = m - 1.0f;
/* Compute log10(1+m) for m in [sqrt(0.5)-1, sqrt(2.0)-1] */
r = -3.30963135e-2f; // -0x1.0f2000p-5
r = fmaf (r, m, 5.60370684e-2f); // 0x1.cb0e40p-5
r = fmaf (r, m, -5.75154796e-2f); // -0x1.d72ab4p-5
r = fmaf (r, m, 6.15895353e-2f); // 0x1.f88a9ep-5
r = fmaf (r, m, -7.21328035e-2f); // -0x1.2774bap-4
r = fmaf (r, m, 8.68686363e-2f); // 0x1.63d05ep-4
r = fmaf (r, m, -1.08580485e-1f); // -0x1.bcbee4p-4
r = fmaf (r, m, 1.44764677e-1f); // 0x1.287a62p-3
r = fmaf (r, m, -2.17147186e-1f); // -0x1.bcb7aap-3
r = fmaf (r, m, -6.57055154e-2f); // -0x1.0d213ap-4
/* add in the exponent portion */
r = fmaf (r, m, fmaf (LOG10_TWO_LO_F, i, 0.5f * m));
r = fmaf (LOG10_TWO_HI_F, i, r);
/* handle special cases */
if (!((a > 0.0f) && (a < __int_as_float (0x7f800000)))) { // +INF
asm ("lg2.approx.ftz.f32 %0,%1;" : "=f"(r) : "f"(a));
}
return r;
}