1 /* Single-precision e^x function.
2    Copyright (c) 2017 Arm Ltd.  All rights reserved.
3 
4    SPDX-License-Identifier: BSD-3-Clause
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9    1. Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11    2. Redistributions in binary form must reproduce the above copyright
12       notice, this list of conditions and the following disclaimer in the
13       documentation and/or other materials provided with the distribution.
14    3. The name of the company may not be used to endorse or promote
15       products derived from this software without specific prior written
16       permission.
17 
18    THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
19    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21    IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 
29 #include "fdlibm.h"
30 #if !__OBSOLETE_MATH_FLOAT
31 
32 #include <math.h>
33 #include <stdint.h>
34 #include "math_config.h"
35 
36 /*
37 EXP2F_TABLE_BITS = 5
38 EXP2F_POLY_ORDER = 3
39 
40 ULP error: 0.502 (nearest rounding.)
41 Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.)
42 Wrong count: 170635 (all nearest rounding wrong results with fma.)
43 Non-nearest ULP error: 1 (rounded ULP error)
44 */
45 
46 #define N (1 << EXP2F_TABLE_BITS)
47 #define InvLn2N __exp2f_data.invln2_scaled
48 #define T __exp2f_data.tab
49 #define C __exp2f_data.poly_scaled
50 
51 static inline uint32_t
top12(float x)52 top12 (float x)
53 {
54   return asuint (x) >> 20;
55 }
56 
57 float
expf(float x)58 expf (float x)
59 {
60   uint32_t abstop;
61   uint64_t ki, t;
62   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
63   double_t kd, xd, z, r, r2, y, s;
64 
65   xd = (double_t) x;
66   abstop = top12 (x) & 0x7ff;
67   if (__builtin_expect (abstop >= top12 (88.0f), 0))
68     {
69       /* |x| >= 88 or x is nan.  */
70       if (asuint (x) == asuint (-INFINITY))
71 	return 0.0f;
72       if (abstop >= top12 (INFINITY))
73 	return x + x;
74       if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */
75 	return __math_oflowf (0);
76       if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */
77 	return __math_uflowf (0);
78 #if WANT_ERRNO_UFLOW
79       if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */
80 	return __math_may_uflowf (0);
81 #endif
82     }
83 
84   /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k.  */
85   z = InvLn2N * xd;
86 
87   /* Round and convert z to int, the result is in [-150*N, 128*N] and
88      ideally ties-to-even rule is used, otherwise the magnitude of r
89      can be bigger which gives larger approximation error.  */
90 #if TOINT_INTRINSICS
91   kd = roundtoint (z);
92   ki = converttoint (z);
93 #else
94 # define SHIFT __exp2f_data.shift
95   kd = (double) (z + SHIFT); /* Rounding to double precision is required.  */
96   ki = asuint64 (kd);
97   kd -= SHIFT;
98 #endif
99   r = z - kd;
100 
101   /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
102   t = T[ki % N];
103   t += ki << (52 - EXP2F_TABLE_BITS);
104   s = asfloat64 (t);
105   z = C[0] * r + C[1];
106   r2 = r * r;
107   y = C[2] * r + 1;
108   y = z * r2 + y;
109   y = y * s;
110   return (float) y;
111 }
112 #endif /* !__OBSOLETE_MATH_FLOAT */
113