ReactOS 0.4.15-dev-8100-g1887773
exp2.c
Go to the documentation of this file.
1
2/*******************************************************************************
3MIT License
4-----------
5
6Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this Software and associated documentaon files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in
16all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24THE SOFTWARE.
25*******************************************************************************/
26
27#include "libm.h"
28#include "libm_util.h"
29
30#define USE_SPLITEXP
31#define USE_SCALEDOUBLE_1
32#define USE_SCALEDOUBLE_2
33#define USE_ZERO_WITH_FLAGS
34#define USE_INFINITY_WITH_FLAGS
35#define USE_HANDLE_ERROR
36
37#include "libm_inlines.h"
38#undef USE_ZERO_WITH_FLAGS
39#undef USE_SPLITEXP
40#undef USE_SCALEDOUBLE_1
41#undef USE_SCALEDOUBLE_2
42#undef USE_INFINITY_WITH_FLAGS
43#undef USE_HANDLE_ERROR
44
45#include "libm_errno.h"
46
47/* exp2 is only provided for use by powf under Windows, so give
48 it a leading underscore. */
49double FN_PROTOTYPE(_exp2)(double x)
50{
51 static const double
52 max_exp2_arg = 1024.0, /* 0x4090000000000000 */
53 min_exp2_arg = -1074.0, /* 0xc090c80000000000 */
54 log2 = 6.931471805599453094178e-01, /* 0x3fe62e42fefa39ef */
55 log2_lead = 6.93147167563438415527E-01, /* 0x3fe62e42f8000000 */
56 log2_tail = 1.29965068938898869640E-08, /* 0x3e4be8e7bcd5e4f1 */
57 one_by_32_lead = 0.03125;
58
59 double y, z1, z2, z, hx, tx, y1, y2;
60 int m;
61 unsigned long long ux, ax;
62
63 /*
64 Computation of exp2(x).
65
66 We compute the values m, z1, and z2 such that
67 exp2(x) = 2**m * (z1 + z2), where exp2(x) is 2**x.
68
69 Computations needed in order to obtain m, z1, and z2
70 involve three steps.
71
72 First, we reduce the argument x to the form
73 x = n/32 + remainder,
74 where n has the value of an integer and |remainder| <= 1/64.
75 The value of n = x * 32 rounded to the nearest integer and
76 the remainder = x - n/32.
77
78 Second, we approximate exp2(r1 + r2) - 1 where r1 is the leading
79 part of the remainder and r2 is the trailing part of the remainder.
80
81 Third, we reconstruct exp2(x) so that
82 exp2(x) = 2**m * (z1 + z2).
83 */
84
85
86 GET_BITS_DP64(x, ux);
87 ax = ux & (~SIGNBIT_DP64);
88
89 if (ax >= 0x4090000000000000) /* abs(x) >= 1024.0 */
90 {
91 if(ax >= 0x7ff0000000000000)
92 {
93 /* x is either NaN or infinity */
94 if (ux & MANTBITS_DP64)
95 /* x is NaN */
96 return _handle_error("exp2", OP_EXP, ux|0x0008000000000000, _DOMAIN,
97 0, EDOM, x, 0.0, 1);
98 else if (ux & SIGNBIT_DP64)
99 /* x is negative infinity; return 0.0 with no flags. */
100 return 0.0;
101 else
102 /* x is positive infinity */
103 return x;
104 }
105 if (x > max_exp2_arg)
106 /* Return +infinity with overflow flag */
107 return _handle_error("exp2", OP_EXP, PINFBITPATT_DP64, _OVERFLOW,
109 else if (x < min_exp2_arg)
110 /* x is negative. Return +zero with underflow and inexact flags */
111 return _handle_error("exp2", OP_EXP, 0, _UNDERFLOW,
113 }
114
115
116 /* Handle small arguments separately */
117 if (ax < 0x3fb7154764ee6c2f) /* abs(x) < 1/(16*log2) */
118 {
119 if (ax < 0x3c00000000000000) /* abs(x) < 2^(-63) */
120 return 1.0 + x; /* Raises inexact if x is non-zero */
121 else
122 {
123 /* Split x into hx (head) and tx (tail). */
124 unsigned long long u;
125 hx = x;
126 GET_BITS_DP64(hx, u);
127 u &= 0xfffffffff8000000;
128 PUT_BITS_DP64(u, hx);
129 tx = x - hx;
130 /* Carefully multiply x by log2. y1 is the most significant
131 part of the result, and y2 the least significant part */
132 y1 = x * log2_lead;
133 y2 = (((hx * log2_lead - y1) + hx * log2_tail) +
134 tx * log2_lead) + tx * log2_tail;
135
136 y = y1 + y2;
137 z = (9.99564649780173690e-1 +
138 (1.61251249355268050e-5 +
139 (2.37986978239838493e-2 +
140 2.68724774856111190e-7*y)*y)*y)/
141 (9.99564649780173692e-1 +
142 (-4.99766199765151309e-1 +
143 (1.070876894098586184e-1 +
144 (-1.189773642681502232e-2 +
145 5.9480622371960190616e-4*y)*y)*y)*y);
146 z = ((z * y1) + (z * y2)) + 1.0;
147 }
148 }
149 else
150 {
151 /* Find m, z1 and z2 such that exp2(x) = 2**m * (z1 + z2) */
152
153 splitexp(x, log2, 32.0, one_by_32_lead, 0.0, &m, &z1, &z2);
154
155 /* Scale (z1 + z2) by 2.0**m */
156 if (m > EMIN_DP64 && m < EMAX_DP64)
157 z = scaleDouble_1((z1+z2),m);
158 else
159 z = scaleDouble_2((z1+z2),m);
160 }
161 return z;
162}
double __cdecl _handle_error(char *fname, int opcode, unsigned long long value, int type, int flags, int error, double arg1, double arg2, int nargs)
Handles an error condition.
Definition: _handle_error.c:34
#define ERANGE
Definition: acclib.h:92
#define EDOM
Definition: errno.h:39
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble GLdouble z
Definition: glext.h:5874
const GLfloat * m
Definition: glext.h:10848
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
#define _DOMAIN
Definition: math.h:39
#define _UNDERFLOW
Definition: math.h:42
_Check_return_ __CRT_INLINE double log2(_In_ double x)
Definition: math.h:301
#define _OVERFLOW
Definition: math.h:41
#define FN_PROTOTYPE(fname)
Definition: libm.h:29
#define AMD_F_INEXACT
Definition: libm_new.h:82
#define AMD_F_UNDERFLOW
Definition: libm_new.h:84
#define AMD_F_OVERFLOW
Definition: libm_new.h:83
double FN_PROTOTYPE() _exp2(double x)
Definition: exp2.c:49
#define GET_BITS_DP64(x, ux)
Definition: libm_util.h:118
#define PINFBITPATT_DP64
Definition: libm_util.h:53
#define SIGNBIT_DP64
Definition: libm_util.h:44
#define EMAX_DP64
Definition: libm_util.h:60
#define EMIN_DP64
Definition: libm_util.h:58
#define MANTBITS_DP64
Definition: libm_util.h:46
#define PUT_BITS_DP64(ux, x)
Definition: libm_util.h:124
ecx edi movl ebx edx edi decl ecx esi eax jecxz decl eax andl eax esi movl edx movl TEMP incl eax andl eax ecx incl ebx testl eax jnz xchgl ecx incl TEMP esp ecx subl ebx pushl ecx ecx edx ecx shrl ecx mm0 mm4 mm0 mm4 mm1 mm5 mm1 mm5 mm2 mm6 mm2 mm6 mm3 mm7 mm3 mm7 paddd mm0 paddd mm4 paddd mm0 paddd mm4 paddd mm0 paddd mm4 movq mm1 movq mm5 psrlq mm1 psrlq mm5 paddd mm0 paddd mm4 psrad mm0 psrad mm4 packssdw mm0 packssdw mm4 mm1 punpckldq mm0 pand mm1 pand mm0 por mm1 movq edi esi edx edi decl ecx jnz popl ecx andl ecx jecxz mm0 mm0 mm1 mm1 mm2 mm2 mm3 mm3 paddd mm0 paddd mm0 paddd mm0 movq mm1 psrlq mm1 paddd mm0 psrad mm0 packssdw mm0 movd eax movw ax
Definition: synth_sse3d.h:180
_In_ CLIPOBJ _In_ BRUSHOBJ _In_ LONG _In_ LONG y1
Definition: winddi.h:3709
_In_ CLIPOBJ _In_ BRUSHOBJ _In_ LONG _In_ LONG _In_ LONG _In_ LONG y2
Definition: winddi.h:3711