1bbbf1280Sopenharmony_ci/*
2bbbf1280Sopenharmony_ci * Single-precision vector powf function.
3bbbf1280Sopenharmony_ci *
4bbbf1280Sopenharmony_ci * Copyright (c) 2019, Arm Limited.
5bbbf1280Sopenharmony_ci * SPDX-License-Identifier: MIT
6bbbf1280Sopenharmony_ci */
7bbbf1280Sopenharmony_ci
8bbbf1280Sopenharmony_ci#include "mathlib.h"
9bbbf1280Sopenharmony_ci#include "v_math.h"
10bbbf1280Sopenharmony_ci#if V_SUPPORTED
11bbbf1280Sopenharmony_ci
12bbbf1280Sopenharmony_ci#define Min v_u32 (0x00800000)
13bbbf1280Sopenharmony_ci#define Max v_u32 (0x7f800000)
14bbbf1280Sopenharmony_ci#define SBITS 5
15bbbf1280Sopenharmony_ci#define Tlog v__powf_log2_data.tab
16bbbf1280Sopenharmony_ci#define Texp v__exp2f_data.tab
17bbbf1280Sopenharmony_ci#define A v__powf_log2_data.poly
18bbbf1280Sopenharmony_ci#define C v__exp2f_data.poly
19bbbf1280Sopenharmony_ci#define LOGDEG 4
20bbbf1280Sopenharmony_ci
21bbbf1280Sopenharmony_ci#if LOGDEG == 5
22bbbf1280Sopenharmony_ci/* 1.01 ulp */
23bbbf1280Sopenharmony_ci#define OFF v_u32 (0x3f330000)
24bbbf1280Sopenharmony_ci#define TBITS 4
25bbbf1280Sopenharmony_ci#elif LOGDEG == 4
26bbbf1280Sopenharmony_ci/* 2.6 ulp ~ 0.5 + 2^24 (128*Ln2*relerr_log2 + relerr_exp2) */
27bbbf1280Sopenharmony_ci#define OFF v_u32 (0x3f35d000)
28bbbf1280Sopenharmony_ci#define TBITS 5
29bbbf1280Sopenharmony_ci#endif
30bbbf1280Sopenharmony_ci
31bbbf1280Sopenharmony_ci#define V_EXP2F_TABLE_BITS SBITS
32bbbf1280Sopenharmony_ci#define V_EXP2F_POLY_ORDER 3
33bbbf1280Sopenharmony_cistruct v_exp2f_data
34bbbf1280Sopenharmony_ci{
35bbbf1280Sopenharmony_ci  uint64_t tab[1 << V_EXP2F_TABLE_BITS];
36bbbf1280Sopenharmony_ci  double poly[V_EXP2F_POLY_ORDER];
37bbbf1280Sopenharmony_ci};
38bbbf1280Sopenharmony_ci
39bbbf1280Sopenharmony_ci#define V_POWF_LOG2_TABLE_BITS TBITS
40bbbf1280Sopenharmony_ci#define V_POWF_LOG2_POLY_ORDER LOGDEG
41bbbf1280Sopenharmony_ci#define SCALE ((double) (1 << SBITS))
42bbbf1280Sopenharmony_cistruct v_powf_log2_data
43bbbf1280Sopenharmony_ci{
44bbbf1280Sopenharmony_ci  struct
45bbbf1280Sopenharmony_ci  {
46bbbf1280Sopenharmony_ci    double invc, logc;
47bbbf1280Sopenharmony_ci  } tab[1 << V_POWF_LOG2_TABLE_BITS];
48bbbf1280Sopenharmony_ci  double poly[V_POWF_LOG2_POLY_ORDER];
49bbbf1280Sopenharmony_ci};
50bbbf1280Sopenharmony_ci
51bbbf1280Sopenharmony_cistatic const struct v_powf_log2_data v__powf_log2_data = {
52bbbf1280Sopenharmony_ci#if LOGDEG == 5
53bbbf1280Sopenharmony_ci  .tab = {
54bbbf1280Sopenharmony_ci{ 0x1.661ec79f8f3bep+0, -0x1.efec65b963019p-2 * SCALE },
55bbbf1280Sopenharmony_ci{ 0x1.571ed4aaf883dp+0, -0x1.b0b6832d4fca4p-2 * SCALE },
56bbbf1280Sopenharmony_ci{ 0x1.49539f0f010bp+0, -0x1.7418b0a1fb77bp-2 * SCALE },
57bbbf1280Sopenharmony_ci{ 0x1.3c995b0b80385p+0, -0x1.39de91a6dcf7bp-2 * SCALE },
58bbbf1280Sopenharmony_ci{ 0x1.30d190c8864a5p+0, -0x1.01d9bf3f2b631p-2 * SCALE },
59bbbf1280Sopenharmony_ci{ 0x1.25e227b0b8eap+0, -0x1.97c1d1b3b7afp-3 * SCALE },
60bbbf1280Sopenharmony_ci{ 0x1.1bb4a4a1a343fp+0, -0x1.2f9e393af3c9fp-3 * SCALE },
61bbbf1280Sopenharmony_ci{ 0x1.12358f08ae5bap+0, -0x1.960cbbf788d5cp-4 * SCALE },
62bbbf1280Sopenharmony_ci{ 0x1.0953f419900a7p+0, -0x1.a6f9db6475fcep-5 * SCALE },
63bbbf1280Sopenharmony_ci{ 0x1p+0, 0x0p+0 * SCALE },
64bbbf1280Sopenharmony_ci{ 0x1.e608cfd9a47acp-1, 0x1.338ca9f24f53dp-4 * SCALE },
65bbbf1280Sopenharmony_ci{ 0x1.ca4b31f026aap-1, 0x1.476a9543891bap-3 * SCALE },
66bbbf1280Sopenharmony_ci{ 0x1.b2036576afce6p-1, 0x1.e840b4ac4e4d2p-3 * SCALE },
67bbbf1280Sopenharmony_ci{ 0x1.9c2d163a1aa2dp-1, 0x1.40645f0c6651cp-2 * SCALE },
68bbbf1280Sopenharmony_ci{ 0x1.886e6037841edp-1, 0x1.88e9c2c1b9ff8p-2 * SCALE },
69bbbf1280Sopenharmony_ci{ 0x1.767dcf5534862p-1, 0x1.ce0a44eb17bccp-2 * SCALE },
70bbbf1280Sopenharmony_ci  },
71bbbf1280Sopenharmony_ci/* rel err: 1.46 * 2^-32 */
72bbbf1280Sopenharmony_ci  .poly = {
73bbbf1280Sopenharmony_ci0x1.27616c9496e0bp-2 * SCALE, -0x1.71969a075c67ap-2 * SCALE,
74bbbf1280Sopenharmony_ci0x1.ec70a6ca7baddp-2 * SCALE, -0x1.7154748bef6c8p-1 * SCALE,
75bbbf1280Sopenharmony_ci0x1.71547652ab82bp0 * SCALE,
76bbbf1280Sopenharmony_ci  }
77bbbf1280Sopenharmony_ci#elif LOGDEG == 4
78bbbf1280Sopenharmony_ci  .tab = {
79bbbf1280Sopenharmony_ci{0x1.6489890582816p+0, -0x1.e960f97b22702p-2 * SCALE},
80bbbf1280Sopenharmony_ci{0x1.5cf19b35e3472p+0, -0x1.c993406cd4db6p-2 * SCALE},
81bbbf1280Sopenharmony_ci{0x1.55aac0e956d65p+0, -0x1.aa711d9a7d0f3p-2 * SCALE},
82bbbf1280Sopenharmony_ci{0x1.4eb0022977e01p+0, -0x1.8bf37bacdce9bp-2 * SCALE},
83bbbf1280Sopenharmony_ci{0x1.47fcccda1dd1fp+0, -0x1.6e13b3519946ep-2 * SCALE},
84bbbf1280Sopenharmony_ci{0x1.418ceabab68c1p+0, -0x1.50cb8281e4089p-2 * SCALE},
85bbbf1280Sopenharmony_ci{0x1.3b5c788f1edb3p+0, -0x1.341504a237e2bp-2 * SCALE},
86bbbf1280Sopenharmony_ci{0x1.3567de48e9c9ap+0, -0x1.17eaab624ffbbp-2 * SCALE},
87bbbf1280Sopenharmony_ci{0x1.2fabc80fd19bap+0, -0x1.f88e708f8c853p-3 * SCALE},
88bbbf1280Sopenharmony_ci{0x1.2a25200ce536bp+0, -0x1.c24b6da113914p-3 * SCALE},
89bbbf1280Sopenharmony_ci{0x1.24d108e0152e3p+0, -0x1.8d02ee397cb1dp-3 * SCALE},
90bbbf1280Sopenharmony_ci{0x1.1facd8ab2fbe1p+0, -0x1.58ac1223408b3p-3 * SCALE},
91bbbf1280Sopenharmony_ci{0x1.1ab614a03efdfp+0, -0x1.253e6fd190e89p-3 * SCALE},
92bbbf1280Sopenharmony_ci{0x1.15ea6d03af9ffp+0, -0x1.e5641882c12ffp-4 * SCALE},
93bbbf1280Sopenharmony_ci{0x1.1147b994bb776p+0, -0x1.81fea712926f7p-4 * SCALE},
94bbbf1280Sopenharmony_ci{0x1.0ccbf650593aap+0, -0x1.203e240de64a3p-4 * SCALE},
95bbbf1280Sopenharmony_ci{0x1.0875408477302p+0, -0x1.8029b86a78281p-5 * SCALE},
96bbbf1280Sopenharmony_ci{0x1.0441d42a93328p+0, -0x1.85d713190fb9p-6 * SCALE},
97bbbf1280Sopenharmony_ci{0x1p+0, 0x0p+0 * SCALE},
98bbbf1280Sopenharmony_ci{0x1.f1d006c855e86p-1, 0x1.4c1cc07312997p-5 * SCALE},
99bbbf1280Sopenharmony_ci{0x1.e28c3341aa301p-1, 0x1.5e1848ccec948p-4 * SCALE},
100bbbf1280Sopenharmony_ci{0x1.d4bdf9aa64747p-1, 0x1.04cfcb7f1196fp-3 * SCALE},
101bbbf1280Sopenharmony_ci{0x1.c7b45a24e5803p-1, 0x1.582813d463c21p-3 * SCALE},
102bbbf1280Sopenharmony_ci{0x1.bb5f5eb2ed60ap-1, 0x1.a936fa68760ccp-3 * SCALE},
103bbbf1280Sopenharmony_ci{0x1.afb0bff8fe6b4p-1, 0x1.f81bc31d6cc4ep-3 * SCALE},
104bbbf1280Sopenharmony_ci{0x1.a49badf7ab1f5p-1, 0x1.2279a09fae6b1p-2 * SCALE},
105bbbf1280Sopenharmony_ci{0x1.9a14a111fc4c9p-1, 0x1.47ec0b6df5526p-2 * SCALE},
106bbbf1280Sopenharmony_ci{0x1.901131f5b2fdcp-1, 0x1.6c71762280f1p-2 * SCALE},
107bbbf1280Sopenharmony_ci{0x1.8687f73f6d865p-1, 0x1.90155070798dap-2 * SCALE},
108bbbf1280Sopenharmony_ci{0x1.7d7067eb77986p-1, 0x1.b2e23b1d3068cp-2 * SCALE},
109bbbf1280Sopenharmony_ci{0x1.74c2c1cf97b65p-1, 0x1.d4e21b0daa86ap-2 * SCALE},
110bbbf1280Sopenharmony_ci{0x1.6c77f37cff2a1p-1, 0x1.f61e2a2f67f3fp-2 * SCALE},
111bbbf1280Sopenharmony_ci  },
112bbbf1280Sopenharmony_ci/* rel err: 1.5 * 2^-30 */
113bbbf1280Sopenharmony_ci  .poly = {
114bbbf1280Sopenharmony_ci -0x1.6ff5daa3b3d7cp-2 * SCALE,
115bbbf1280Sopenharmony_ci 0x1.ec81d03c01aebp-2 * SCALE,
116bbbf1280Sopenharmony_ci -0x1.71547bb43f101p-1 * SCALE,
117bbbf1280Sopenharmony_ci 0x1.7154764a815cbp0 * SCALE,
118bbbf1280Sopenharmony_ci  }
119bbbf1280Sopenharmony_ci#endif
120bbbf1280Sopenharmony_ci};
121bbbf1280Sopenharmony_ci
122bbbf1280Sopenharmony_cistatic const struct v_exp2f_data v__exp2f_data = {
123bbbf1280Sopenharmony_ci  .tab = {
124bbbf1280Sopenharmony_ci0x3ff0000000000000, 0x3fefd9b0d3158574, 0x3fefb5586cf9890f, 0x3fef9301d0125b51,
125bbbf1280Sopenharmony_ci0x3fef72b83c7d517b, 0x3fef54873168b9aa, 0x3fef387a6e756238, 0x3fef1e9df51fdee1,
126bbbf1280Sopenharmony_ci0x3fef06fe0a31b715, 0x3feef1a7373aa9cb, 0x3feedea64c123422, 0x3feece086061892d,
127bbbf1280Sopenharmony_ci0x3feebfdad5362a27, 0x3feeb42b569d4f82, 0x3feeab07dd485429, 0x3feea47eb03a5585,
128bbbf1280Sopenharmony_ci0x3feea09e667f3bcd, 0x3fee9f75e8ec5f74, 0x3feea11473eb0187, 0x3feea589994cce13,
129bbbf1280Sopenharmony_ci0x3feeace5422aa0db, 0x3feeb737b0cdc5e5, 0x3feec49182a3f090, 0x3feed503b23e255d,
130bbbf1280Sopenharmony_ci0x3feee89f995ad3ad, 0x3feeff76f2fb5e47, 0x3fef199bdd85529c, 0x3fef3720dcef9069,
131bbbf1280Sopenharmony_ci0x3fef5818dcfba487, 0x3fef7c97337b9b5f, 0x3fefa4afa2a490da, 0x3fefd0765b6e4540,
132bbbf1280Sopenharmony_ci  },
133bbbf1280Sopenharmony_ci/* rel err: 1.69 * 2^-34 */
134bbbf1280Sopenharmony_ci  .poly = {
135bbbf1280Sopenharmony_ci0x1.c6af84b912394p-5/SCALE/SCALE/SCALE, 0x1.ebfce50fac4f3p-3/SCALE/SCALE, 0x1.62e42ff0c52d6p-1/SCALE
136bbbf1280Sopenharmony_ci  },
137bbbf1280Sopenharmony_ci};
138bbbf1280Sopenharmony_ci
139bbbf1280Sopenharmony_ciVPCS_ATTR
140bbbf1280Sopenharmony_ci__attribute__ ((noinline)) static v_f32_t
141bbbf1280Sopenharmony_cispecialcase (v_f32_t x, v_f32_t y, v_f32_t ret, v_u32_t cmp)
142bbbf1280Sopenharmony_ci{
143bbbf1280Sopenharmony_ci  return v_call2_f32 (powf, x, y, ret, cmp);
144bbbf1280Sopenharmony_ci}
145bbbf1280Sopenharmony_ci
146bbbf1280Sopenharmony_ciVPCS_ATTR
147bbbf1280Sopenharmony_civ_f32_t
148bbbf1280Sopenharmony_ciV_NAME(powf) (v_f32_t x, v_f32_t y)
149bbbf1280Sopenharmony_ci{
150bbbf1280Sopenharmony_ci  v_u32_t u, tmp, cmp, i, top, iz;
151bbbf1280Sopenharmony_ci  v_s32_t k;
152bbbf1280Sopenharmony_ci  v_f32_t ret;
153bbbf1280Sopenharmony_ci
154bbbf1280Sopenharmony_ci  u = v_as_u32_f32 (x);
155bbbf1280Sopenharmony_ci  cmp = v_cond_u32 (u - Min >= Max - Min);
156bbbf1280Sopenharmony_ci  tmp = u - OFF;
157bbbf1280Sopenharmony_ci  i = (tmp >> (23 - TBITS)) % (1 << TBITS);
158bbbf1280Sopenharmony_ci  top = tmp & 0xff800000;
159bbbf1280Sopenharmony_ci  iz = u - top;
160bbbf1280Sopenharmony_ci  k = v_as_s32_u32 (top) >> (23 - SBITS); /* arithmetic shift */
161bbbf1280Sopenharmony_ci
162bbbf1280Sopenharmony_ci  for (int lane = 0; lane < v_lanes32 (); lane++)
163bbbf1280Sopenharmony_ci    {
164bbbf1280Sopenharmony_ci      uint32_t si, siz;
165bbbf1280Sopenharmony_ci      int32_t sk;
166bbbf1280Sopenharmony_ci      float sy;
167bbbf1280Sopenharmony_ci
168bbbf1280Sopenharmony_ci      /* Use double precision for each lane.  */
169bbbf1280Sopenharmony_ci      double invc, logc, z, r, p, y0, logx, ylogx, kd, s;
170bbbf1280Sopenharmony_ci      uint64_t ki, t;
171bbbf1280Sopenharmony_ci
172bbbf1280Sopenharmony_ci      si = v_get_u32 (i, lane);
173bbbf1280Sopenharmony_ci      siz = v_get_u32 (iz, lane);
174bbbf1280Sopenharmony_ci      sk = v_get_s32 (k, lane);
175bbbf1280Sopenharmony_ci      sy = v_get_f32 (y, lane);
176bbbf1280Sopenharmony_ci
177bbbf1280Sopenharmony_ci      invc = Tlog[si].invc;
178bbbf1280Sopenharmony_ci      logc = Tlog[si].logc;
179bbbf1280Sopenharmony_ci      z = (double) as_f32_u32 (siz);
180bbbf1280Sopenharmony_ci
181bbbf1280Sopenharmony_ci      /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
182bbbf1280Sopenharmony_ci      r = __builtin_fma (z, invc, -1.0);
183bbbf1280Sopenharmony_ci      y0 = logc + (double) sk;
184bbbf1280Sopenharmony_ci
185bbbf1280Sopenharmony_ci      /* Polynomial to approximate log1p(r)/ln2.  */
186bbbf1280Sopenharmony_ci#if LOGDEG == 5
187bbbf1280Sopenharmony_ci      logx = A[0];
188bbbf1280Sopenharmony_ci      logx = r * logx + A[1];
189bbbf1280Sopenharmony_ci      logx = r * logx + A[2];
190bbbf1280Sopenharmony_ci      logx = r * logx + A[3];
191bbbf1280Sopenharmony_ci      logx = r * logx + A[4];
192bbbf1280Sopenharmony_ci      logx = r * logx + y0;
193bbbf1280Sopenharmony_ci#elif LOGDEG == 4
194bbbf1280Sopenharmony_ci      logx = A[0];
195bbbf1280Sopenharmony_ci      logx = r * logx + A[1];
196bbbf1280Sopenharmony_ci      logx = r * logx + A[2];
197bbbf1280Sopenharmony_ci      logx = r * logx + A[3];
198bbbf1280Sopenharmony_ci      logx = r * logx + y0;
199bbbf1280Sopenharmony_ci#endif
200bbbf1280Sopenharmony_ci      ylogx = sy * logx;
201bbbf1280Sopenharmony_ci      v_set_u32 (&cmp, lane,
202bbbf1280Sopenharmony_ci		 (as_u64_f64 (ylogx) >> 47 & 0xffff)
203bbbf1280Sopenharmony_ci		     >= as_u64_f64 (126.0 * (1 << SBITS)) >> 47
204bbbf1280Sopenharmony_ci		   ? 1
205bbbf1280Sopenharmony_ci		   : v_get_u32 (cmp, lane));
206bbbf1280Sopenharmony_ci
207bbbf1280Sopenharmony_ci      /* N*x = k + r with r in [-1/2, 1/2] */
208bbbf1280Sopenharmony_ci#if TOINT_INTRINSICS
209bbbf1280Sopenharmony_ci      kd = roundtoint (ylogx); /* k */
210bbbf1280Sopenharmony_ci      ki = converttoint (ylogx);
211bbbf1280Sopenharmony_ci#else
212bbbf1280Sopenharmony_ci# define SHIFT 0x1.8p52
213bbbf1280Sopenharmony_ci      kd = eval_as_double (ylogx + SHIFT);
214bbbf1280Sopenharmony_ci      ki = asuint64 (kd);
215bbbf1280Sopenharmony_ci      kd -= SHIFT;
216bbbf1280Sopenharmony_ci#endif
217bbbf1280Sopenharmony_ci      r = ylogx - kd;
218bbbf1280Sopenharmony_ci
219bbbf1280Sopenharmony_ci      /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
220bbbf1280Sopenharmony_ci      t = Texp[ki % (1 << SBITS)];
221bbbf1280Sopenharmony_ci      t += ki << (52 - SBITS);
222bbbf1280Sopenharmony_ci      s = as_f64_u64 (t);
223bbbf1280Sopenharmony_ci      p = C[0];
224bbbf1280Sopenharmony_ci      p = __builtin_fma (p, r, C[1]);
225bbbf1280Sopenharmony_ci      p = __builtin_fma (p, r, C[2]);
226bbbf1280Sopenharmony_ci      p = __builtin_fma (p, s * r, s);
227bbbf1280Sopenharmony_ci
228bbbf1280Sopenharmony_ci      v_set_f32 (&ret, lane, p);
229bbbf1280Sopenharmony_ci    }
230bbbf1280Sopenharmony_ci  if (unlikely (v_any_u32 (cmp)))
231bbbf1280Sopenharmony_ci    return specialcase (x, y, ret, cmp);
232bbbf1280Sopenharmony_ci  return ret;
233bbbf1280Sopenharmony_ci}
234bbbf1280Sopenharmony_ciVPCS_ALIAS
235bbbf1280Sopenharmony_ci#endif
236