• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Single-precision vector log function.
3  *
4  * Copyright (c) 2019-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11 
12 #if SV_SUPPORTED
13 
14 #define P(i) __sv_logf_poly[i]
15 
16 #define Ln2 (0x1.62e43p-1f) /* 0x3f317218 */
17 #define Min (0x00800000)
18 #define Max (0x7f800000)
19 #define Mask (0x007fffff)
20 #define Off (0x3f2aaaab) /* 0.666667 */
21 
22 float
23 optr_aor_log_f32 (float);
24 
25 static NOINLINE sv_f32_t
__sv_logf_specialcase(sv_f32_t x,sv_f32_t y,svbool_t cmp)26 __sv_logf_specialcase (sv_f32_t x, sv_f32_t y, svbool_t cmp)
27 {
28   return sv_call_f32 (optr_aor_log_f32, x, y, cmp);
29 }
30 
31 /* Optimised implementation of SVE logf, using the same algorithm and polynomial
32    as the Neon routine in math/. Maximum error is 3.34 ULPs:
33    __sv_logf(0x1.557298p+0) got 0x1.26edecp-2
34 			   want 0x1.26ede6p-2.  */
35 sv_f32_t
__sv_logf_x(sv_f32_t x,const svbool_t pg)36 __sv_logf_x (sv_f32_t x, const svbool_t pg)
37 {
38   sv_u32_t u = sv_as_u32_f32 (x);
39   svbool_t cmp
40     = svcmpge_u32 (pg, svsub_n_u32_x (pg, u, Min), sv_u32 (Max - Min));
41 
42   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
43   u = svsub_n_u32_x (pg, u, Off);
44   sv_f32_t n = sv_to_f32_s32_x (pg, svasr_n_s32_x (pg, sv_as_s32_u32 (u),
45 						   23)); /* Sign-extend.  */
46   u = svand_n_u32_x (pg, u, Mask);
47   u = svadd_n_u32_x (pg, u, Off);
48   sv_f32_t r = svsub_n_f32_x (pg, sv_as_f32_u32 (u), 1.0f);
49 
50   /* y = log(1+r) + n*ln2.  */
51   sv_f32_t r2 = svmul_f32_x (pg, r, r);
52   /* n*ln2 + r + r2*(P6 + r*P5 + r2*(P4 + r*P3 + r2*(P2 + r*P1 + r2*P0))).  */
53   sv_f32_t p = sv_fma_n_f32_x (pg, P (1), r, sv_f32 (P (2)));
54   sv_f32_t q = sv_fma_n_f32_x (pg, P (3), r, sv_f32 (P (4)));
55   sv_f32_t y = sv_fma_n_f32_x (pg, P (5), r, sv_f32 (P (6)));
56   p = sv_fma_n_f32_x (pg, P (0), r2, p);
57   q = sv_fma_f32_x (pg, p, r2, q);
58   y = sv_fma_f32_x (pg, q, r2, y);
59   p = sv_fma_n_f32_x (pg, Ln2, n, r);
60   y = sv_fma_f32_x (pg, y, r2, p);
61 
62   if (unlikely (svptest_any (pg, cmp)))
63     return __sv_logf_specialcase (x, y, cmp);
64   return y;
65 }
66 
67 PL_ALIAS (__sv_logf_x, _ZGVsMxv_logf)
68 
69 PL_SIG (SV, F, 1, log, 0.01, 11.1)
70 PL_TEST_ULP (__sv_logf, 2.85)
71 PL_TEST_INTERVAL (__sv_logf, -0.0, -0x1p126, 100)
72 PL_TEST_INTERVAL (__sv_logf, 0x1p-149, 0x1p-126, 4000)
73 PL_TEST_INTERVAL (__sv_logf, 0x1p-126, 0x1p-23, 50000)
74 PL_TEST_INTERVAL (__sv_logf, 0x1p-23, 1.0, 50000)
75 PL_TEST_INTERVAL (__sv_logf, 1.0, 100, 50000)
76 PL_TEST_INTERVAL (__sv_logf, 100, inf, 50000)
77 #endif // SV_SUPPORTED
78