1/*
2 * Single-precision vector 2^x 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 "mathlib.h"
9#include "v_math.h"
10
11static const float Poly[] = {
12  /*  maxerr: 0.878 ulp.  */
13  0x1.416b5ep-13f, 0x1.5f082ep-10f, 0x1.3b2dep-7f, 0x1.c6af7cp-5f, 0x1.ebfbdcp-3f, 0x1.62e43p-1f
14};
15#define C0 v_f32 (Poly[0])
16#define C1 v_f32 (Poly[1])
17#define C2 v_f32 (Poly[2])
18#define C3 v_f32 (Poly[3])
19#define C4 v_f32 (Poly[4])
20#define C5 v_f32 (Poly[5])
21
22#define Shift v_f32 (0x1.8p23f)
23#define InvLn2 v_f32 (0x1.715476p+0f)
24#define Ln2hi v_f32 (0x1.62e4p-1f)
25#define Ln2lo v_f32 (0x1.7f7d1cp-20f)
26
27static float32x4_t VPCS_ATTR NOINLINE
28specialcase (float32x4_t poly, float32x4_t n, uint32x4_t e, float32x4_t absn)
29{
30  /* 2^n may overflow, break it up into s1*s2.  */
31  uint32x4_t b = (n <= v_f32 (0.0f)) & v_u32 (0x83000000);
32  float32x4_t s1 = vreinterpretq_f32_u32 (v_u32 (0x7f000000) + b);
33  float32x4_t s2 = vreinterpretq_f32_u32 (e - b);
34  uint32x4_t cmp = absn > v_f32 (192.0f);
35  float32x4_t r1 = s1 * s1;
36  float32x4_t r0 = poly * s1 * s2;
37  return vreinterpretq_f32_u32 ((cmp & vreinterpretq_u32_f32 (r1))
38				| (~cmp & vreinterpretq_u32_f32 (r0)));
39}
40
41float32x4_t VPCS_ATTR
42_ZGVnN4v_exp2f_1u (float32x4_t x)
43{
44  float32x4_t n, r, scale, poly, absn;
45  uint32x4_t cmp, e;
46
47  /* exp2(x) = 2^n * poly(r), with poly(r) in [1/sqrt(2),sqrt(2)]
48     x = n + r, with r in [-1/2, 1/2].  */
49#if 0
50  float32x4_t z;
51  z = x + Shift;
52  n = z - Shift;
53  r = x - n;
54  e = vreinterpretq_u32_f32 (z) << 23;
55#else
56  n = vrndaq_f32 (x);
57  r = x - n;
58  e = vreinterpretq_u32_s32 (vcvtaq_s32_f32 (x)) << 23;
59#endif
60  scale = vreinterpretq_f32_u32 (e + v_u32 (0x3f800000));
61  absn = vabsq_f32 (n);
62  cmp = absn > v_f32 (126.0f);
63  poly = vfmaq_f32 (C1, C0, r);
64  poly = vfmaq_f32 (C2, poly, r);
65  poly = vfmaq_f32 (C3, poly, r);
66  poly = vfmaq_f32 (C4, poly, r);
67  poly = vfmaq_f32 (C5, poly, r);
68  poly = vfmaq_f32 (v_f32 (1.0f), poly, r);
69  if (unlikely (v_any_u32 (cmp)))
70    return specialcase (poly, n, e, absn);
71  return scale * poly;
72}
73