xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/v_expf_1u.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*
2  * Single-precision vector e^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 
11 static const float Poly[] = {
12   /*  maxerr: 0.36565 +0.5 ulp.  */
13   0x1.6a6000p-10f,
14   0x1.12718ep-7f,
15   0x1.555af0p-5f,
16   0x1.555430p-3f,
17   0x1.fffff4p-2f,
18 };
19 #define C0 v_f32 (Poly[0])
20 #define C1 v_f32 (Poly[1])
21 #define C2 v_f32 (Poly[2])
22 #define C3 v_f32 (Poly[3])
23 #define C4 v_f32 (Poly[4])
24 
25 #define Shift v_f32 (0x1.8p23f)
26 #define InvLn2 v_f32 (0x1.715476p+0f)
27 #define Ln2hi v_f32 (0x1.62e4p-1f)
28 #define Ln2lo v_f32 (0x1.7f7d1cp-20f)
29 
30 static float32x4_t VPCS_ATTR NOINLINE
31 specialcase (float32x4_t poly, float32x4_t n, uint32x4_t e, float32x4_t absn)
32 {
33   /* 2^n may overflow, break it up into s1*s2.  */
34   uint32x4_t b = (n <= v_f32 (0.0f)) & v_u32 (0x83000000);
35   float32x4_t s1 = vreinterpretq_f32_u32 (v_u32 (0x7f000000) + b);
36   float32x4_t s2 = vreinterpretq_f32_u32 (e - b);
37   uint32x4_t cmp = absn > v_f32 (192.0f);
38   float32x4_t r1 = s1 * s1;
39   float32x4_t r0 = poly * s1 * s2;
40   return vreinterpretq_f32_u32 ((cmp & vreinterpretq_u32_f32 (r1))
41 				| (~cmp & vreinterpretq_u32_f32 (r0)));
42 }
43 
44 float32x4_t VPCS_ATTR
45 _ZGVnN4v_expf_1u (float32x4_t x)
46 {
47   float32x4_t n, r, scale, poly, absn, z;
48   uint32x4_t cmp, e;
49 
50   /* exp(x) = 2^n * poly(r), with poly(r) in [1/sqrt(2),sqrt(2)]
51      x = ln2*n + r, with r in [-ln2/2, ln2/2].  */
52 #if 1
53   z = vfmaq_f32 (Shift, x, InvLn2);
54   n = z - Shift;
55   r = vfmaq_f32 (x, n, -Ln2hi);
56   r = vfmaq_f32 (r, n, -Ln2lo);
57   e = vreinterpretq_u32_f32 (z) << 23;
58 #else
59   z = x * InvLn2;
60   n = vrndaq_f32 (z);
61   r = vfmaq_f32 (x, n, -Ln2hi);
62   r = vfmaq_f32 (r, n, -Ln2lo);
63   e = vreinterpretq_u32_s32 (vcvtaq_s32_f32 (z)) << 23;
64 #endif
65   scale = vreinterpretq_f32_u32 (e + v_u32 (0x3f800000));
66   absn = vabsq_f32 (n);
67   cmp = absn > v_f32 (126.0f);
68   poly = vfmaq_f32 (C1, C0, r);
69   poly = vfmaq_f32 (C2, poly, r);
70   poly = vfmaq_f32 (C3, poly, r);
71   poly = vfmaq_f32 (C4, poly, r);
72   poly = vfmaq_f32 (v_f32 (1.0f), poly, r);
73   poly = vfmaq_f32 (v_f32 (1.0f), poly, r);
74   if (unlikely (v_any_u32 (cmp)))
75     return specialcase (poly, n, e, absn);
76   return scale * poly;
77 }
78