ReactOS 0.4.17-dev-1321-g5c122a9
exp.c File Reference
#include <math.h>
#include <stdint.h>
#include "libm.h"
#include "exp_data.h"
Include dependency graph for exp.c:

Go to the source code of this file.

Macros

#define N   (1 << EXP_TABLE_BITS)
 
#define InvLn2N   __exp_data.invln2N
 
#define NegLn2hiN   __exp_data.negln2hiN
 
#define NegLn2loN   __exp_data.negln2loN
 
#define Shift   __exp_data.shift
 
#define T   __exp_data.tab
 
#define C2   __exp_data.poly[5 - EXP_POLY_ORDER]
 
#define C3   __exp_data.poly[6 - EXP_POLY_ORDER]
 
#define C4   __exp_data.poly[7 - EXP_POLY_ORDER]
 
#define C5   __exp_data.poly[8 - EXP_POLY_ORDER]
 

Functions

static double specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
 
static uint32_t top12 (double x)
 
double exp (double x)
 

Macro Definition Documentation

◆ C2

#define C2   __exp_data.poly[5 - EXP_POLY_ORDER]

Definition at line 33 of file exp.c.

◆ C3

#define C3   __exp_data.poly[6 - EXP_POLY_ORDER]

Definition at line 34 of file exp.c.

◆ C4

#define C4   __exp_data.poly[7 - EXP_POLY_ORDER]

Definition at line 35 of file exp.c.

◆ C5

#define C5   __exp_data.poly[8 - EXP_POLY_ORDER]

Definition at line 36 of file exp.c.

◆ InvLn2N

#define InvLn2N   __exp_data.invln2N

Definition at line 28 of file exp.c.

◆ N

#define N   (1 << EXP_TABLE_BITS)

Definition at line 27 of file exp.c.

◆ NegLn2hiN

#define NegLn2hiN   __exp_data.negln2hiN

Definition at line 29 of file exp.c.

◆ NegLn2loN

#define NegLn2loN   __exp_data.negln2loN

Definition at line 30 of file exp.c.

◆ Shift

#define Shift   __exp_data.shift

Definition at line 31 of file exp.c.

◆ T

Definition at line 32 of file exp.c.

Function Documentation

◆ exp()

double exp ( double  x)

Definition at line 86 of file exp.c.

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
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
#define predict_false(x)
Definition: libm.h:93
#define WANT_ROUNDING
Definition: libm.h:76
static DNS_RECORDW r2
Definition: record.c:38
#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

◆ specialcase()

static double specialcase ( double_t  tmp,
uint64_t  sbits,
uint64_t  ki 
)
inlinestatic

Definition at line 45 of file exp.c.

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}
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
static void fp_force_eval(double x)
Definition: libm.h:57
static double fp_barrier(double x)
Definition: libm.h:114

Referenced by exp().

◆ top12()

static uint32_t top12 ( double  x)
inlinestatic

Definition at line 81 of file exp.c.

82{
83 return asuint64(x) >> 52;
84}

Referenced by exp().