• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Double-precision vector log10(x) 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 "v_math.h"
9 #include "include/mathlib.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #if V_SUPPORTED
14 
15 #define A(i) v_f64 (__v_log10_data.poly[i])
16 #define T(s, i) __v_log10_data.tab[i].s
17 #define Ln2 v_f64 (0x1.62e42fefa39efp-1)
18 #define N (1 << V_LOG10_TABLE_BITS)
19 #define OFF v_u64 (0x3fe6900900000000)
20 
21 struct entry
22 {
23   v_f64_t invc;
24   v_f64_t log10c;
25 };
26 
27 static inline struct entry
lookup(v_u64_t i)28 lookup (v_u64_t i)
29 {
30   struct entry e;
31 #ifdef SCALAR
32   e.invc = T (invc, i);
33   e.log10c = T (log10c, i);
34 #else
35   e.invc[0] = T (invc, i[0]);
36   e.log10c[0] = T (log10c, i[0]);
37   e.invc[1] = T (invc, i[1]);
38   e.log10c[1] = T (log10c, i[1]);
39 #endif
40   return e;
41 }
42 
43 VPCS_ATTR
44 inline static v_f64_t
specialcase(v_f64_t x,v_f64_t y,v_u64_t cmp)45 specialcase (v_f64_t x, v_f64_t y, v_u64_t cmp)
46 {
47   return v_call_f64 (log10, x, y, cmp);
48 }
49 
50 /* Our implementation of v_log10 is a slight modification of v_log (1.660ulps).
51    Max ULP error: < 2.5 ulp (nearest rounding.)
52    Maximum measured at 2.46 ulp for x in [0.96, 0.97]
53      __v_log10(0x1.13192407fcb46p+0) got 0x1.fff6be3cae4bbp-6
54 				    want 0x1.fff6be3cae4b9p-6
55      -0.459999 ulp err 1.96.  */
56 VPCS_ATTR
V_NAME(log10)57 v_f64_t V_NAME (log10) (v_f64_t x)
58 {
59   v_f64_t z, r, r2, p, y, kd, hi;
60   v_u64_t ix, iz, tmp, top, i, cmp;
61   v_s64_t k;
62   struct entry e;
63 
64   ix = v_as_u64_f64 (x);
65   top = ix >> 48;
66   cmp = v_cond_u64 (top - v_u64 (0x0010) >= v_u64 (0x7ff0 - 0x0010));
67 
68   /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
69      The range is split into N subintervals.
70      The ith subinterval contains z and c is near its center.  */
71   tmp = ix - OFF;
72   i = (tmp >> (52 - V_LOG10_TABLE_BITS)) % N;
73   k = v_as_s64_u64 (tmp) >> 52; /* arithmetic shift.  */
74   iz = ix - (tmp & v_u64 (0xfffULL << 52));
75   z = v_as_f64_u64 (iz);
76   e = lookup (i);
77 
78   /* log10(x) = log1p(z/c-1)/log(10) + log10(c) + k*log10(2).  */
79   r = v_fma_f64 (z, e.invc, v_f64 (-1.0));
80   kd = v_to_f64_s64 (k);
81 
82   /* hi = r / log(10) + log10(c) + k*log10(2).
83      Constants in `v_log10_data.c` are computed (in extended precision) as
84      e.log10c := e.logc * ivln10.  */
85   v_f64_t w = v_fma_f64 (r, v_f64 (__v_log10_data.invln10), e.log10c);
86 
87   /* y = log10(1+r) + n * log10(2).  */
88   hi = v_fma_f64 (kd, v_f64 (__v_log10_data.log10_2), w);
89 
90   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
91   r2 = r * r;
92   y = v_fma_f64 (A (3), r, A (2));
93   p = v_fma_f64 (A (1), r, A (0));
94   y = v_fma_f64 (A (4), r2, y);
95   y = v_fma_f64 (y, r2, p);
96   y = v_fma_f64 (y, r2, hi);
97 
98   if (unlikely (v_any_u64 (cmp)))
99     return specialcase (x, y, cmp);
100   return y;
101 }
102 VPCS_ALIAS
103 
104 PL_SIG (V, D, 1, log10, 0.01, 11.1)
105 PL_TEST_ULP (V_NAME (log10), 1.97)
106 PL_TEST_EXPECT_FENV_ALWAYS (V_NAME (log10))
107 PL_TEST_INTERVAL (V_NAME (log10), 0, 0xffff000000000000, 10000)
108 PL_TEST_INTERVAL (V_NAME (log10), 0x1p-4, 0x1p4, 400000)
109 PL_TEST_INTERVAL (V_NAME (log10), 0, inf, 400000)
110 #endif
111