comparison src/video/e_sqrt.h @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
86 86
87 /*#include "math.h"*/ 87 /*#include "math.h"*/
88 #include "math_private.h" 88 #include "math_private.h"
89 89
90 #ifdef __STDC__ 90 #ifdef __STDC__
91 double SDL_NAME (copysign) (double x, double y) 91 double SDL_NAME(copysign) (double x, double y)
92 #else 92 #else
93 double SDL_NAME (copysign) (x, y) 93 double SDL_NAME(copysign) (x, y)
94 double 94 double
95 x, 95 x,
96 y; 96 y;
97 #endif 97 #endif
98 { 98 {
99 u_int32_t hx, hy; 99 u_int32_t hx, hy;
100 GET_HIGH_WORD (hx, x); 100 GET_HIGH_WORD(hx, x);
101 GET_HIGH_WORD (hy, y); 101 GET_HIGH_WORD(hy, y);
102 SET_HIGH_WORD (x, (hx & 0x7fffffff) | (hy & 0x80000000)); 102 SET_HIGH_WORD(x, (hx & 0x7fffffff) | (hy & 0x80000000));
103 return x; 103 return x;
104 } 104 }
105 105
106 #ifdef __STDC__ 106 #ifdef __STDC__
107 double SDL_NAME (scalbn) (double x, int n) 107 double SDL_NAME(scalbn) (double x, int n)
108 #else 108 #else
109 double SDL_NAME (scalbn) (x, n) 109 double SDL_NAME(scalbn) (x, n)
110 double 110 double
111 x; 111 x;
112 int 112 int
113 n; 113 n;
114 #endif 114 #endif
115 { 115 {
116 int32_t k, hx, lx; 116 int32_t k, hx, lx;
117 EXTRACT_WORDS (hx, lx, x); 117 EXTRACT_WORDS(hx, lx, x);
118 k = (hx & 0x7ff00000) >> 20; /* extract exponent */ 118 k = (hx & 0x7ff00000) >> 20; /* extract exponent */
119 if (k == 0) { /* 0 or subnormal x */ 119 if (k == 0) { /* 0 or subnormal x */
120 if ((lx | (hx & 0x7fffffff)) == 0) 120 if ((lx | (hx & 0x7fffffff)) == 0)
121 return x; /* +-0 */ 121 return x; /* +-0 */
122 x *= two54; 122 x *= two54;
123 GET_HIGH_WORD (hx, x); 123 GET_HIGH_WORD(hx, x);
124 k = ((hx & 0x7ff00000) >> 20) - 54; 124 k = ((hx & 0x7ff00000) >> 20) - 54;
125 if (n < -50000) 125 if (n < -50000)
126 return tiny * x; /*underflow */ 126 return tiny * x; /*underflow */
127 } 127 }
128 if (k == 0x7ff) 128 if (k == 0x7ff)
129 return x + x; /* NaN or Inf */ 129 return x + x; /* NaN or Inf */
130 k = k + n; 130 k = k + n;
131 if (k > 0x7fe) 131 if (k > 0x7fe)
132 return huge * SDL_NAME (copysign) (huge, x); /* overflow */ 132 return huge * SDL_NAME(copysign) (huge, x); /* overflow */
133 if (k > 0) { /* normal result */ 133 if (k > 0) { /* normal result */
134 SET_HIGH_WORD (x, (hx & 0x800fffff) | (k << 20)); 134 SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
135 return x; 135 return x;
136 } 136 }
137 if (k <= -54) { 137 if (k <= -54) {
138 if (n > 50000) /* in case integer overflow in n+k */ 138 if (n > 50000) /* in case integer overflow in n+k */
139 return huge * SDL_NAME (copysign) (huge, x); /*overflow */ 139 return huge * SDL_NAME(copysign) (huge, x); /*overflow */
140 else 140 else
141 return tiny * SDL_NAME (copysign) (tiny, x); /*underflow */ 141 return tiny * SDL_NAME(copysign) (tiny, x); /*underflow */
142 } 142 }
143 k += 54; /* subnormal result */ 143 k += 54; /* subnormal result */
144 SET_HIGH_WORD (x, (hx & 0x800fffff) | (k << 20)); 144 SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
145 return x * twom54; 145 return x * twom54;
146 } 146 }
147 147
148 #ifdef __STDC__ 148 #ifdef __STDC__
149 double 149 double
150 __ieee754_sqrt (double x) 150 __ieee754_sqrt(double x)
151 #else 151 #else
152 double 152 double
153 __ieee754_sqrt (x) 153 __ieee754_sqrt(x)
154 double x; 154 double x;
155 #endif 155 #endif
156 { 156 {
157 double z; 157 double z;
158 int32_t sign = (int) 0x80000000; 158 int32_t sign = (int) 0x80000000;
159 int32_t ix0, s0, q, m, t, i; 159 int32_t ix0, s0, q, m, t, i;
160 u_int32_t r, t1, s1, ix1, q1; 160 u_int32_t r, t1, s1, ix1, q1;
161 161
162 EXTRACT_WORDS (ix0, ix1, x); 162 EXTRACT_WORDS(ix0, ix1, x);
163 163
164 /* take care of Inf and NaN */ 164 /* take care of Inf and NaN */
165 if ((ix0 & 0x7ff00000) == 0x7ff00000) { 165 if ((ix0 & 0x7ff00000) == 0x7ff00000) {
166 return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf 166 return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
167 sqrt(-inf)=sNaN */ 167 sqrt(-inf)=sNaN */
251 ix0 = (q >> 1) + 0x3fe00000; 251 ix0 = (q >> 1) + 0x3fe00000;
252 ix1 = q1 >> 1; 252 ix1 = q1 >> 1;
253 if ((q & 1) == 1) 253 if ((q & 1) == 1)
254 ix1 |= sign; 254 ix1 |= sign;
255 ix0 += (m << 20); 255 ix0 += (m << 20);
256 INSERT_WORDS (z, ix0, ix1); 256 INSERT_WORDS(z, ix0, ix1);
257 return z; 257 return z;
258 } 258 }
259 259
260 /* 260 /*
261 Other methods (use floating-point arithmetic) 261 Other methods (use floating-point arithmetic)