xref: /freebsd/contrib/arm-optimized-routines/pl/math/log10f.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*
2  * Single-precision log10 function.
3  *
4  * Copyright (c) 2022-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include <math.h>
9 #include <stdint.h>
10 
11 #include "math_config.h"
12 #include "pl_sig.h"
13 #include "pl_test.h"
14 
15 /* Data associated to logf:
16 
17    LOGF_TABLE_BITS = 4
18    LOGF_POLY_ORDER = 4
19 
20    ULP error: 0.818 (nearest rounding.)
21    Relative error: 1.957 * 2^-26 (before rounding.).  */
22 
23 #define T __logf_data.tab
24 #define A __logf_data.poly
25 #define Ln2 __logf_data.ln2
26 #define InvLn10 __logf_data.invln10
27 #define N (1 << LOGF_TABLE_BITS)
28 #define OFF 0x3f330000
29 
30 /* This naive implementation of log10f mimics that of log
31    then simply scales the result by 1/log(10) to switch from base e to
32    base 10. Hence, most computations are carried out in double precision.
33    Scaling before rounding to single precision is both faster and more accurate.
34 
35    ULP error: 0.797 ulp (nearest rounding.).  */
36 float
37 log10f (float x)
38 {
39   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
40   double_t z, r, r2, y, y0, invc, logc;
41   uint32_t ix, iz, tmp;
42   int k, i;
43 
44   ix = asuint (x);
45 #if WANT_ROUNDING
46   /* Fix sign of zero with downward rounding when x==1.  */
47   if (unlikely (ix == 0x3f800000))
48     return 0;
49 #endif
50   if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000))
51     {
52       /* x < 0x1p-126 or inf or nan.  */
53       if (ix * 2 == 0)
54 	return __math_divzerof (1);
55       if (ix == 0x7f800000) /* log(inf) == inf.  */
56 	return x;
57       if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
58 	return __math_invalidf (x);
59       /* x is subnormal, normalize it.  */
60       ix = asuint (x * 0x1p23f);
61       ix -= 23 << 23;
62     }
63 
64   /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
65      The range is split into N subintervals.
66      The ith subinterval contains z and c is near its center.  */
67   tmp = ix - OFF;
68   i = (tmp >> (23 - LOGF_TABLE_BITS)) % N;
69   k = (int32_t) tmp >> 23; /* arithmetic shift.  */
70   iz = ix - (tmp & 0xff800000);
71   invc = T[i].invc;
72   logc = T[i].logc;
73   z = (double_t) asfloat (iz);
74 
75   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
76   r = z * invc - 1;
77   y0 = logc + (double_t) k * Ln2;
78 
79   /* Pipelined polynomial evaluation to approximate log1p(r).  */
80   r2 = r * r;
81   y = A[1] * r + A[2];
82   y = A[0] * r2 + y;
83   y = y * r2 + (y0 + r);
84 
85   /* Multiply by 1/log(10).  */
86   y = y * InvLn10;
87 
88   return eval_as_float (y);
89 }
90 
91 PL_SIG (S, F, 1, log10, 0.01, 11.1)
92 PL_TEST_ULP (log10f, 0.30)
93 PL_TEST_INTERVAL (log10f, 0, 0xffff0000, 10000)
94 PL_TEST_INTERVAL (log10f, 0x1p-127, 0x1p-26, 50000)
95 PL_TEST_INTERVAL (log10f, 0x1p-26, 0x1p3, 50000)
96 PL_TEST_INTERVAL (log10f, 0x1p-4, 0x1p4, 50000)
97 PL_TEST_INTERVAL (log10f, 0, inf, 50000)
98