1 /*
2 * Double-precision SVE e^(x+tail) function.
3 *
4 * Copyright (c) 2021-2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #ifndef SV_EXP_TAIL_H
9 #define SV_EXP_TAIL_H
10
11 #include "sv_math.h"
12 #if SV_SUPPORTED
13
14 #include "v_exp_tail.h"
15
16 #define C1 sv_f64 (C1_scal)
17 #define C2 sv_f64 (C2_scal)
18 #define C3 sv_f64 (C3_scal)
19 #define MinusLn2hi (-Ln2hi_scal)
20 #define MinusLn2lo (-Ln2lo_scal)
21
22 #define N (1 << V_EXP_TAIL_TABLE_BITS)
23 #define Tab __v_exp_tail_data
24 #define IndexMask (N - 1)
25 #define Shift sv_f64 (0x1.8p+52)
26 #define Thres 704.0
27
28 static inline sv_f64_t
sv_exp_tail_special_case(svbool_t pg,sv_f64_t s,sv_f64_t y,sv_f64_t n)29 sv_exp_tail_special_case (svbool_t pg, sv_f64_t s, sv_f64_t y, sv_f64_t n)
30 {
31 sv_f64_t absn = svabs_f64_x (pg, n);
32
33 /* 2^(n/N) may overflow, break it up into s1*s2. */
34 sv_u64_t b = svsel_u64 (svcmple_n_f64 (pg, n, 0), sv_u64 (0x6000000000000000),
35 sv_u64 (0));
36 sv_f64_t s1 = sv_as_f64_u64 (svsubr_n_u64_x (pg, b, 0x7000000000000000));
37 sv_f64_t s2 = sv_as_f64_u64 (
38 svadd_u64_x (pg, svsub_n_u64_x (pg, sv_as_u64_f64 (s), 0x3010000000000000),
39 b));
40
41 svbool_t cmp = svcmpgt_n_f64 (pg, absn, 1280.0 * N);
42 sv_f64_t r1 = svmul_f64_x (pg, s1, s1);
43 sv_f64_t r0 = svmul_f64_x (pg, sv_fma_f64_x (pg, y, s2, s2), s1);
44 return svsel_f64 (cmp, r1, r0);
45 }
46
47 static inline sv_f64_t
sv_exp_tail(const svbool_t pg,sv_f64_t x,sv_f64_t xtail)48 sv_exp_tail (const svbool_t pg, sv_f64_t x, sv_f64_t xtail)
49 {
50 /* Calculate exp(x + xtail). */
51 sv_f64_t z = sv_fma_n_f64_x (pg, InvLn2_scal, x, Shift);
52 sv_f64_t n = svsub_f64_x (pg, z, Shift);
53
54 sv_f64_t r = sv_fma_n_f64_x (pg, MinusLn2hi, n, x);
55 r = sv_fma_n_f64_x (pg, MinusLn2lo, n, r);
56
57 sv_u64_t u = sv_as_u64_f64 (z);
58 sv_u64_t e = svlsl_n_u64_x (pg, u, 52 - V_EXP_TAIL_TABLE_BITS);
59 sv_u64_t i = svand_n_u64_x (pg, u, IndexMask);
60
61 sv_f64_t y = sv_fma_f64_x (pg, C3, r, C2);
62 y = sv_fma_f64_x (pg, y, r, C1);
63 y = sv_fma_f64_x (pg, y, r, sv_f64 (1.0));
64 y = sv_fma_f64_x (pg, y, r, xtail);
65
66 /* s = 2^(n/N). */
67 u = sv_lookup_u64_x (pg, Tab, i);
68 sv_f64_t s = sv_as_f64_u64 (svadd_u64_x (pg, u, e));
69
70 svbool_t cmp = svcmpgt_n_f64 (pg, svabs_f64_x (pg, x), Thres);
71 if (unlikely (svptest_any (pg, cmp)))
72 {
73 return sv_exp_tail_special_case (pg, s, y, n);
74 }
75 return sv_fma_f64_x (pg, y, s, s);
76 }
77
78 #endif
79 #endif
80