ReactOS 0.4.16-dev-1041-g8b6907f
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
47double FN_PROTOTYPE(exp2)(double x)
48{
49 static const double
50 max_exp2_arg = 1024.0, /* 0x4090000000000000 */
51 min_exp2_arg = -1074.0, /* 0xc090c80000000000 */
52 log2 = 6.931471805599453094178e-01, /* 0x3fe62e42fefa39ef */
53 log2_lead = 6.93147167563438415527E-01, /* 0x3fe62e42f8000000 */
54 log2_tail = 1.29965068938898869640E-08, /* 0x3e4be8e7bcd5e4f1 */
55 one_by_32_lead = 0.03125;
56
57 double y, z1, z2, z, hx, tx, y1, y2;
58 int m;
59 unsigned long long ux, ax;
60
61 /*
62 Computation of exp2(x).
63
64 We compute the values m, z1, and z2 such that
65 exp2(x) = 2**m * (z1 + z2), where exp2(x) is 2**x.
66
67 Computations needed in order to obtain m, z1, and z2
68 involve three steps.
69
70 First, we reduce the argument x to the form
71 x = n/32 + remainder,
72 where n has the value of an integer and |remainder| <= 1/64.
73 The value of n = x * 32 rounded to the nearest integer and
74 the remainder = x - n/32.
75
76 Second, we approximate exp2(r1 + r2) - 1 where r1 is the leading
77 part of the remainder and r2 is the trailing part of the remainder.
78
79 Third, we reconstruct exp2(x) so that
80 exp2(x) = 2**m * (z1 + z2).
81 */
82
83
84 GET_BITS_DP64(x, ux);
85 ax = ux & (~SIGNBIT_DP64);
86
87 if (ax >= 0x4090000000000000) /* abs(x) >= 1024.0 */
88 {
89 if(ax >= 0x7ff0000000000000)
90 {
91 /* x is either NaN or infinity */
92 if (ux & MANTBITS_DP64)
93 /* x is NaN */
94 return _handle_error("exp2", OP_EXP, ux|0x0008000000000000, _DOMAIN,
95 0, EDOM, x, 0.0, 1);
96 else if (ux & SIGNBIT_DP64)
97 /* x is negative infinity; return 0.0 with no flags. */
98 return 0.0;
99 else
100 /* x is positive infinity */
101 return x;
102 }
103 if (x > max_exp2_arg)
104 /* Return +infinity with overflow flag */
105 return _handle_error("exp2", OP_EXP, PINFBITPATT_DP64, _OVERFLOW,
107 else if (x < min_exp2_arg)
108 /* x is negative. Return +zero with underflow and inexact flags */
109 return _handle_error("exp2", OP_EXP, 0, _UNDERFLOW,
111 }
112
113
114 /* Handle small arguments separately */
115 if (ax < 0x3fb7154764ee6c2f) /* abs(x) < 1/(16*log2) */
116 {
117 if (ax < 0x3c00000000000000) /* abs(x) < 2^(-63) */
118 return 1.0 + x; /* Raises inexact if x is non-zero */
119 else
120 {
121 /* Split x into hx (head) and tx (tail). */
122 unsigned long long u;
123 hx = x;
124 GET_BITS_DP64(hx, u);
125 u &= 0xfffffffff8000000;
126 PUT_BITS_DP64(u, hx);
127 tx = x - hx;
128 /* Carefully multiply x by log2. y1 is the most significant
129 part of the result, and y2 the least significant part */
130 y1 = x * log2_lead;
131 y2 = (((hx * log2_lead - y1) + hx * log2_tail) +
132 tx * log2_lead) + tx * log2_tail;
133
134 y = y1 + y2;
135 z = (9.99564649780173690e-1 +
136 (1.61251249355268050e-5 +
137 (2.37986978239838493e-2 +
138 2.68724774856111190e-7*y)*y)*y)/
139 (9.99564649780173692e-1 +
140 (-4.99766199765151309e-1 +
141 (1.070876894098586184e-1 +
142 (-1.189773642681502232e-2 +
143 5.9480622371960190616e-4*y)*y)*y)*y);
144 z = ((z * y1) + (z * y2)) + 1.0;
145 }
146 }
147 else
148 {
149 /* Find m, z1 and z2 such that exp2(x) = 2**m * (z1 + z2) */
150
151 splitexp(x, log2, 32.0, one_by_32_lead, 0.0, &m, &z1, &z2);
152
153 /* Scale (z1 + z2) by 2.0**m */
154 if (m > EMIN_DP64 && m < EMAX_DP64)
155 z = scaleDouble_1((z1+z2),m);
156 else
157 z = scaleDouble_2((z1+z2),m);
158 }
159 return z;
160}
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
double log2(double x)
Definition: stubs.c:62
#define EDOM
Definition: errno.h:39
_Check_return_ double __cdecl exp2(_In_ double x)
Definition: exp2.c:7
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
#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
#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