ReactOS 0.4.17-dev-1321-g5c122a9
exp.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS CRT
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Implementation of exp.
5 * COPYRIGHT: Imported from musl libc
6 * https://git.musl-libc.org/cgit/musl/tree/src/math/exp.c
7 * blob: b764d73cfe339c22579d28388b387ba22ba7eda0
8 * See https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
9 */
10
11/*
12 * Double-precision e^x function.
13 *
14 * Copyright (c) 2018, Arm Limited.
15 * SPDX-License-Identifier: MIT
16 */
17
18#include <math.h>
19#include <stdint.h>
20#include "libm.h"
21#include "exp_data.h"
22
23#ifdef _MSC_VER
24#pragma function(exp)
25#endif
26
27#define N (1 << EXP_TABLE_BITS)
28#define InvLn2N __exp_data.invln2N
29#define NegLn2hiN __exp_data.negln2hiN
30#define NegLn2loN __exp_data.negln2loN
31#define Shift __exp_data.shift
32#define T __exp_data.tab
33#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
34#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
35#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
36#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
37
38/* Handle cases that may overflow or underflow when computing the result that
39 is scale*(1+TMP) without intermediate rounding. The bit representation of
40 scale is in SBITS, however it has a computed exponent that may have
41 overflown into the sign bit so that needs to be adjusted before using it as
42 a double. (int32_t)KI is the k used in the argument reduction and exponent
43 adjustment of scale, positive k here means the result may overflow and
44 negative k means the result may underflow. */
45static inline double specialcase(double_t tmp, uint64_t sbits, uint64_t ki)
46{
48
49 if ((ki & 0x80000000) == 0) {
50 /* k > 0, the exponent of scale might have overflowed by <= 460. */
51 sbits -= 1009ull << 52;
52 scale = asdouble(sbits);
53 y = 0x1p1009 * (scale + scale * tmp);
54 return eval_as_double(y);
55 }
56 /* k < 0, need special care in the subnormal range. */
57 sbits += 1022ull << 52;
58 scale = asdouble(sbits);
59 y = scale + scale * tmp;
60 if (y < 1.0) {
61 /* Round y to the right precision before scaling it into the subnormal
62 range to avoid double rounding that can cause 0.5+E/2 ulp error where
63 E is the worst-case ulp error outside the subnormal range. So this
64 is only useful if the goal is better than 1 ulp worst-case error. */
65 double_t hi, lo;
66 lo = scale - y + scale * tmp;
67 hi = 1.0 + y;
68 lo = 1.0 - hi + y + lo;
69 y = eval_as_double(hi + lo) - 1.0;
70 /* Avoid -0.0 with downward rounding. */
71 if (WANT_ROUNDING && y == 0.0)
72 y = 0.0;
73 /* The underflow exception needs to be signaled explicitly. */
74 fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022);
75 }
76 y = 0x1p-1022 * y;
77 return eval_as_double(y);
78}
79
80/* Top 12 bits of a double (sign and exponent bits). */
81static inline uint32_t top12(double x)
82{
83 return asuint64(x) >> 52;
84}
85
86double exp(double x)
87{
88 uint32_t abstop;
89 uint64_t ki, idx, top, sbits;
90 double_t kd, z, r, r2, scale, tail, tmp;
91
92 abstop = top12(x) & 0x7ff;
93 if (predict_false(abstop - top12(0x1p-54) >= top12(512.0) - top12(0x1p-54))) {
94 if (abstop - top12(0x1p-54) >= 0x80000000)
95 /* Avoid spurious underflow for tiny x. */
96 /* Note: 0 is common input. */
97 return WANT_ROUNDING ? 1.0 + x : 1.0;
98 if (abstop >= top12(1024.0)) {
99 if (asuint64(x) == asuint64(-INFINITY))
100 return 0.0;
101 if (abstop >= top12(INFINITY))
102 return 1.0 + x;
103 if (asuint64(x) >> 63)
104 return __math_uflow(0);
105 else
106 return __math_oflow(0);
107 }
108 /* Large x is special cased below. */
109 abstop = 0;
110 }
111
112 /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
113 /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
114 z = InvLn2N * x;
115#if TOINT_INTRINSICS
116 kd = roundtoint(z);
117 ki = converttoint(z);
118#elif EXP_USE_TOINT_NARROW
119 /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
120 kd = eval_as_double(z + Shift);
121 ki = asuint64(kd) >> 16;
122 kd = (double_t)(int32_t)ki;
123#else
124 /* z - kd is in [-1, 1] in non-nearest rounding modes. */
125 kd = eval_as_double(z + Shift);
126 ki = asuint64(kd);
127 kd -= Shift;
128#endif
129 r = x + kd * NegLn2hiN + kd * NegLn2loN;
130 /* 2^(k/N) ~= scale * (1 + tail). */
131 idx = 2 * (ki % N);
132 top = ki << (52 - EXP_TABLE_BITS);
133 tail = asdouble(T[idx]);
134 /* This is only a valid scale when -1023*N < k < 1024*N. */
135 sbits = T[idx + 1] + top;
136 /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
137 /* Evaluation is optimized assuming superscalar pipelined execution. */
138 r2 = r * r;
139 /* Without fma the worst case error is 0.25/N ulp larger. */
140 /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
141 tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
142 if (predict_false(abstop == 0))
143 return specialcase(tmp, sbits, ki);
144 scale = asdouble(sbits);
145 /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
146 is no spurious underflow here even without fma. */
147 return eval_as_double(scale + scale * tmp);
148}
double __math_oflow(uint32_t sign)
Definition: __math_oflow.c:13
double __math_uflow(uint32_t sign)
Definition: __math_uflow.c:13
struct outqueuenode * tail
Definition: adnsresfilter.c:66
INT32 int32_t
Definition: types.h:71
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
unsigned int idx
Definition: utils.c:40
#define INFINITY
Definition: math.h:272
#define EXP_TABLE_BITS
Definition: exp_data.h:21
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9032
GLdouble GLdouble GLdouble GLdouble top
Definition: glext.h:10859
GLdouble GLdouble z
Definition: glext.h:5874
static double asdouble(uint64_t i)
Definition: libm.h:142
static uint64_t asuint64(double f)
Definition: libm.h:135
static double eval_as_double(double x)
Definition: libm.h:102
static void fp_force_eval(double x)
Definition: libm.h:57
static double fp_barrier(double x)
Definition: libm.h:114
#define predict_false(x)
Definition: libm.h:93
#define WANT_ROUNDING
Definition: libm.h:76
static DNS_RECORDW r2
Definition: record.c:38
DWORD exp
Definition: msg.c:18625
#define N
Definition: exp.c:27
#define T
Definition: exp.c:32
#define NegLn2loN
Definition: exp.c:30
#define C5
Definition: exp.c:36
#define C3
Definition: exp.c:34
static double specialcase(double_t tmp, uint64_t sbits, uint64_t ki)
Definition: exp.c:45
#define NegLn2hiN
Definition: exp.c:29
#define InvLn2N
Definition: exp.c:28
static uint32_t top12(double x)
Definition: exp.c:81
#define C4
Definition: exp.c:35
#define C2
Definition: exp.c:33
#define Shift
Definition: exp.c:31