1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstring> |
---|
16 | #include <cstdio> |
---|
17 | |
---|
18 | #include "core.h" |
---|
19 | |
---|
20 | using namespace std; |
---|
21 | |
---|
22 | namespace lol |
---|
23 | { |
---|
24 | |
---|
25 | real::real(float f) { *this = (double)f; } |
---|
26 | real::real(int i) { *this = (double)i; } |
---|
27 | real::real(unsigned int i) { *this = (double)i; } |
---|
28 | |
---|
29 | real::real(double d) |
---|
30 | { |
---|
31 | union { double d; uint64_t x; } u = { d }; |
---|
32 | |
---|
33 | uint32_t sign = (u.x >> 63) << 31; |
---|
34 | uint32_t exponent = (u.x << 1) >> 53; |
---|
35 | |
---|
36 | switch (exponent) |
---|
37 | { |
---|
38 | case 0x00: |
---|
39 | m_signexp = sign; |
---|
40 | break; |
---|
41 | case 0x7ff: |
---|
42 | m_signexp = sign | 0x7fffffffu; |
---|
43 | break; |
---|
44 | default: |
---|
45 | m_signexp = sign | (exponent + (1 << 30) - (1 << 10)); |
---|
46 | break; |
---|
47 | } |
---|
48 | |
---|
49 | m_mantissa[0] = u.x >> 20; |
---|
50 | m_mantissa[1] = u.x << 12; |
---|
51 | memset(m_mantissa + 2, 0, (BIGITS - 2) * sizeof(m_mantissa[0])); |
---|
52 | } |
---|
53 | |
---|
54 | real::operator float() const { return (float)(double)(*this); } |
---|
55 | real::operator int() const { return (int)(double)(*this); } |
---|
56 | real::operator unsigned int() const { return (unsigned int)(double)(*this); } |
---|
57 | |
---|
58 | real::operator double() const |
---|
59 | { |
---|
60 | union { double d; uint64_t x; } u; |
---|
61 | |
---|
62 | /* Get sign */ |
---|
63 | u.x = m_signexp >> 31; |
---|
64 | u.x <<= 11; |
---|
65 | |
---|
66 | /* Compute new exponent */ |
---|
67 | uint32_t exponent = (m_signexp << 1) >> 1; |
---|
68 | int e = (int)exponent - (1 << 30) + (1 << 10); |
---|
69 | |
---|
70 | if (e < 0) |
---|
71 | u.x <<= 52; |
---|
72 | else if (e >= 0x7ff) |
---|
73 | { |
---|
74 | u.x |= 0x7ff; |
---|
75 | u.x <<= 52; |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | u.x |= e; |
---|
80 | |
---|
81 | /* Store mantissa if necessary */ |
---|
82 | u.x <<= 32; |
---|
83 | u.x |= m_mantissa[0]; |
---|
84 | u.x <<= 20; |
---|
85 | u.x |= m_mantissa[1] >> 12; |
---|
86 | /* Rounding */ |
---|
87 | u.x += (m_mantissa[1] >> 11) & 1; |
---|
88 | } |
---|
89 | |
---|
90 | return u.d; |
---|
91 | } |
---|
92 | |
---|
93 | real real::operator +() const |
---|
94 | { |
---|
95 | return *this; |
---|
96 | } |
---|
97 | |
---|
98 | real real::operator -() const |
---|
99 | { |
---|
100 | real ret = *this; |
---|
101 | ret.m_signexp ^= 0x80000000u; |
---|
102 | return ret; |
---|
103 | } |
---|
104 | |
---|
105 | real real::operator +(real const &x) const |
---|
106 | { |
---|
107 | if (x.m_signexp << 1 == 0) |
---|
108 | return *this; |
---|
109 | |
---|
110 | /* Ensure both arguments are positive. Otherwise, switch signs, |
---|
111 | * or replace + with -. */ |
---|
112 | if (m_signexp >> 31) |
---|
113 | return -(-*this + -x); |
---|
114 | |
---|
115 | if (x.m_signexp >> 31) |
---|
116 | return *this - (-x); |
---|
117 | |
---|
118 | /* Ensure *this has the larger exponent (no need for the mantissa to |
---|
119 | * be larger, as in subtraction). Otherwise, switch. */ |
---|
120 | if ((m_signexp << 1) < (x.m_signexp << 1)) |
---|
121 | return x + *this; |
---|
122 | |
---|
123 | real ret; |
---|
124 | |
---|
125 | int e1 = m_signexp - (1 << 30) + 1; |
---|
126 | int e2 = x.m_signexp - (1 << 30) + 1; |
---|
127 | |
---|
128 | int bigoff = (e1 - e2) / BIGIT_BITS; |
---|
129 | int off = e1 - e2 - bigoff * BIGIT_BITS; |
---|
130 | |
---|
131 | if (bigoff > BIGITS) |
---|
132 | return *this; |
---|
133 | |
---|
134 | ret.m_signexp = m_signexp; |
---|
135 | |
---|
136 | uint64_t carry = 0; |
---|
137 | for (int i = BIGITS; i--; ) |
---|
138 | { |
---|
139 | carry += m_mantissa[i]; |
---|
140 | if (i - bigoff >= 0) |
---|
141 | carry += x.m_mantissa[i - bigoff] >> off; |
---|
142 | |
---|
143 | if (off && i - bigoff > 0) |
---|
144 | carry += (x.m_mantissa[i - bigoff - 1] << (BIGIT_BITS - off)) & 0xffffffffu; |
---|
145 | else if (i - bigoff == 0) |
---|
146 | carry += (uint64_t)1 << (BIGIT_BITS - off); |
---|
147 | |
---|
148 | ret.m_mantissa[i] = carry; |
---|
149 | carry >>= BIGIT_BITS; |
---|
150 | } |
---|
151 | |
---|
152 | /* Renormalise in case we overflowed the mantissa */ |
---|
153 | if (carry) |
---|
154 | { |
---|
155 | carry--; |
---|
156 | for (int i = 0; i < BIGITS; i++) |
---|
157 | { |
---|
158 | uint32_t tmp = ret.m_mantissa[i]; |
---|
159 | ret.m_mantissa[i] = (carry << (BIGIT_BITS - 1)) | (tmp >> 1); |
---|
160 | carry = tmp & 1u; |
---|
161 | } |
---|
162 | ret.m_signexp++; |
---|
163 | } |
---|
164 | |
---|
165 | return ret; |
---|
166 | } |
---|
167 | |
---|
168 | real real::operator -(real const &x) const |
---|
169 | { |
---|
170 | if (x.m_signexp << 1 == 0) |
---|
171 | return *this; |
---|
172 | |
---|
173 | /* Ensure both arguments are positive. Otherwise, switch signs, |
---|
174 | * or replace - with +. */ |
---|
175 | if (m_signexp >> 31) |
---|
176 | return -(-*this + x); |
---|
177 | |
---|
178 | if (x.m_signexp >> 31) |
---|
179 | return (*this) + (-x); |
---|
180 | |
---|
181 | /* Ensure *this is larger than x */ |
---|
182 | if (*this < x) |
---|
183 | return -(x - *this); |
---|
184 | |
---|
185 | real ret; |
---|
186 | |
---|
187 | int e1 = m_signexp - (1 << 30) + 1; |
---|
188 | int e2 = x.m_signexp - (1 << 30) + 1; |
---|
189 | |
---|
190 | int bigoff = (e1 - e2) / BIGIT_BITS; |
---|
191 | int off = e1 - e2 - bigoff * BIGIT_BITS; |
---|
192 | |
---|
193 | if (bigoff > BIGITS) |
---|
194 | return *this; |
---|
195 | |
---|
196 | ret.m_signexp = m_signexp; |
---|
197 | |
---|
198 | int64_t carry = 0; |
---|
199 | for (int i = 0; i < bigoff; i++) |
---|
200 | { |
---|
201 | carry -= x.m_mantissa[BIGITS - i]; |
---|
202 | /* Emulates a signed shift */ |
---|
203 | carry >>= BIGIT_BITS; |
---|
204 | carry |= carry << BIGIT_BITS; |
---|
205 | } |
---|
206 | carry -= x.m_mantissa[BIGITS - 1 - bigoff] & (((int64_t)1 << off) - 1); |
---|
207 | carry /= (int64_t)1 << off; |
---|
208 | |
---|
209 | for (int i = BIGITS; i--; ) |
---|
210 | { |
---|
211 | carry += m_mantissa[i]; |
---|
212 | if (i - bigoff >= 0) |
---|
213 | carry -= x.m_mantissa[i - bigoff] >> off; |
---|
214 | |
---|
215 | if (off && i - bigoff > 0) |
---|
216 | carry -= (x.m_mantissa[i - bigoff - 1] << (BIGIT_BITS - off)) & 0xffffffffu; |
---|
217 | else if (i - bigoff == 0) |
---|
218 | carry -= (uint64_t)1 << (BIGIT_BITS - off); |
---|
219 | |
---|
220 | ret.m_mantissa[i] = carry; |
---|
221 | carry >>= BIGIT_BITS; |
---|
222 | carry |= carry << BIGIT_BITS; |
---|
223 | } |
---|
224 | |
---|
225 | carry += 1; |
---|
226 | |
---|
227 | /* Renormalise if we underflowed the mantissa */ |
---|
228 | if (carry == 0) |
---|
229 | { |
---|
230 | /* How much do we need to shift the mantissa? FIXME: this could |
---|
231 | * be computed above */ |
---|
232 | off = 0; |
---|
233 | for (int i = 0; i < BIGITS; i++) |
---|
234 | { |
---|
235 | if (!ret.m_mantissa[i]) |
---|
236 | { |
---|
237 | off += BIGIT_BITS; |
---|
238 | continue; |
---|
239 | } |
---|
240 | |
---|
241 | for (uint32_t tmp = ret.m_mantissa[i]; tmp < 0x80000000u; tmp <<= 1) |
---|
242 | off++; |
---|
243 | break; |
---|
244 | } |
---|
245 | if (off == BIGITS * BIGIT_BITS) |
---|
246 | ret.m_signexp &= 0x80000000u; |
---|
247 | else |
---|
248 | { |
---|
249 | off++; /* Shift one more to get rid of the leading one */ |
---|
250 | ret.m_signexp -= off; |
---|
251 | |
---|
252 | bigoff = off / BIGIT_BITS; |
---|
253 | off -= bigoff * BIGIT_BITS; |
---|
254 | |
---|
255 | for (int i = 0; i < BIGITS; i++) |
---|
256 | { |
---|
257 | uint32_t tmp = 0; |
---|
258 | if (i + bigoff < BIGITS) |
---|
259 | tmp |= ret.m_mantissa[i + bigoff] << off; |
---|
260 | if (off && i + bigoff + 1 < BIGITS) |
---|
261 | tmp |= ret.m_mantissa[i + bigoff + 1] >> (BIGIT_BITS - off); |
---|
262 | ret.m_mantissa[i] = tmp; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | return ret; |
---|
268 | } |
---|
269 | |
---|
270 | real real::operator *(real const &x) const |
---|
271 | { |
---|
272 | real ret; |
---|
273 | |
---|
274 | if (m_signexp << 1 == 0 || x.m_signexp << 1 == 0) |
---|
275 | { |
---|
276 | ret = (m_signexp << 1 == 0) ? *this : x; |
---|
277 | ret.m_signexp ^= x.m_signexp & 0x80000000u; |
---|
278 | return ret; |
---|
279 | } |
---|
280 | |
---|
281 | ret.m_signexp = (m_signexp ^ x.m_signexp) & 0x80000000u; |
---|
282 | int e = (m_signexp & 0x7fffffffu) - (1 << 30) + 1 |
---|
283 | + (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1; |
---|
284 | |
---|
285 | /* Accumulate low order product; no need to store it, we just |
---|
286 | * want the carry value */ |
---|
287 | uint64_t carry = 0, hicarry = 0, prev; |
---|
288 | for (int i = 0; i < BIGITS; i++) |
---|
289 | { |
---|
290 | for (int j = 0; j < i + 1; j++) |
---|
291 | { |
---|
292 | prev = carry; |
---|
293 | carry += (uint64_t)m_mantissa[BIGITS - 1 - j] |
---|
294 | * (uint64_t)x.m_mantissa[BIGITS - 1 + j - i]; |
---|
295 | if (carry < prev) |
---|
296 | hicarry++; |
---|
297 | } |
---|
298 | carry >>= BIGIT_BITS; |
---|
299 | carry |= hicarry << BIGIT_BITS; |
---|
300 | hicarry >>= BIGIT_BITS; |
---|
301 | } |
---|
302 | |
---|
303 | for (int i = 0; i < BIGITS; i++) |
---|
304 | { |
---|
305 | for (int j = i + 1; j < BIGITS; j++) |
---|
306 | { |
---|
307 | prev = carry; |
---|
308 | carry += (uint64_t)m_mantissa[BIGITS - 1 - j] |
---|
309 | * (uint64_t)x.m_mantissa[j - 1 - i]; |
---|
310 | if (carry < prev) |
---|
311 | hicarry++; |
---|
312 | } |
---|
313 | prev = carry; |
---|
314 | carry += m_mantissa[BIGITS - 1 - i]; |
---|
315 | carry += x.m_mantissa[BIGITS - 1 - i]; |
---|
316 | if (carry < prev) |
---|
317 | hicarry++; |
---|
318 | ret.m_mantissa[BIGITS - 1 - i] = carry & 0xffffffffu; |
---|
319 | carry >>= BIGIT_BITS; |
---|
320 | carry |= hicarry << BIGIT_BITS; |
---|
321 | hicarry >>= BIGIT_BITS; |
---|
322 | } |
---|
323 | |
---|
324 | /* Renormalise in case we overflowed the mantissa */ |
---|
325 | if (carry) |
---|
326 | { |
---|
327 | carry--; |
---|
328 | for (int i = 0; i < BIGITS; i++) |
---|
329 | { |
---|
330 | uint32_t tmp = ret.m_mantissa[i]; |
---|
331 | ret.m_mantissa[i] = (carry << (BIGIT_BITS - 1)) | (tmp >> 1); |
---|
332 | carry = tmp & 1u; |
---|
333 | } |
---|
334 | e++; |
---|
335 | } |
---|
336 | |
---|
337 | ret.m_signexp |= e + (1 << 30) - 1; |
---|
338 | |
---|
339 | return ret; |
---|
340 | } |
---|
341 | |
---|
342 | real real::operator /(real const &x) const |
---|
343 | { |
---|
344 | return *this * re(x); |
---|
345 | } |
---|
346 | |
---|
347 | real &real::operator +=(real const &x) |
---|
348 | { |
---|
349 | real tmp = *this; |
---|
350 | return *this = tmp + x; |
---|
351 | } |
---|
352 | |
---|
353 | real &real::operator -=(real const &x) |
---|
354 | { |
---|
355 | real tmp = *this; |
---|
356 | return *this = tmp - x; |
---|
357 | } |
---|
358 | |
---|
359 | real &real::operator *=(real const &x) |
---|
360 | { |
---|
361 | real tmp = *this; |
---|
362 | return *this = tmp * x; |
---|
363 | } |
---|
364 | |
---|
365 | real &real::operator /=(real const &x) |
---|
366 | { |
---|
367 | real tmp = *this; |
---|
368 | return *this = tmp / x; |
---|
369 | } |
---|
370 | |
---|
371 | real real::operator <<(int x) const |
---|
372 | { |
---|
373 | real tmp = *this; |
---|
374 | return tmp <<= x; |
---|
375 | } |
---|
376 | |
---|
377 | real real::operator >>(int x) const |
---|
378 | { |
---|
379 | real tmp = *this; |
---|
380 | return tmp >>= x; |
---|
381 | } |
---|
382 | |
---|
383 | real &real::operator <<=(int x) |
---|
384 | { |
---|
385 | if (m_signexp << 1) |
---|
386 | m_signexp += x; |
---|
387 | return *this; |
---|
388 | } |
---|
389 | |
---|
390 | real &real::operator >>=(int x) |
---|
391 | { |
---|
392 | if (m_signexp << 1) |
---|
393 | m_signexp -= x; |
---|
394 | return *this; |
---|
395 | } |
---|
396 | |
---|
397 | bool real::operator ==(real const &x) const |
---|
398 | { |
---|
399 | if ((m_signexp << 1) == 0 && (x.m_signexp << 1) == 0) |
---|
400 | return true; |
---|
401 | |
---|
402 | if (m_signexp != x.m_signexp) |
---|
403 | return false; |
---|
404 | |
---|
405 | return memcmp(m_mantissa, x.m_mantissa, sizeof(m_mantissa)) == 0; |
---|
406 | } |
---|
407 | |
---|
408 | bool real::operator !=(real const &x) const |
---|
409 | { |
---|
410 | return !(*this == x); |
---|
411 | } |
---|
412 | |
---|
413 | bool real::operator <(real const &x) const |
---|
414 | { |
---|
415 | /* Ensure both numbers are positive */ |
---|
416 | if (m_signexp >> 31) |
---|
417 | return (x.m_signexp >> 31) ? -*this > -x : true; |
---|
418 | |
---|
419 | if (x.m_signexp >> 31) |
---|
420 | return false; |
---|
421 | |
---|
422 | /* Compare all relevant bits */ |
---|
423 | if (m_signexp != x.m_signexp) |
---|
424 | return m_signexp < x.m_signexp; |
---|
425 | |
---|
426 | for (int i = 0; i < BIGITS; i++) |
---|
427 | if (m_mantissa[i] != x.m_mantissa[i]) |
---|
428 | return m_mantissa[i] < x.m_mantissa[i]; |
---|
429 | |
---|
430 | return false; |
---|
431 | } |
---|
432 | |
---|
433 | bool real::operator <=(real const &x) const |
---|
434 | { |
---|
435 | return !(*this > x); |
---|
436 | } |
---|
437 | |
---|
438 | bool real::operator >(real const &x) const |
---|
439 | { |
---|
440 | /* Ensure both numbers are positive */ |
---|
441 | if (m_signexp >> 31) |
---|
442 | return (x.m_signexp >> 31) ? -*this < -x : false; |
---|
443 | |
---|
444 | if (x.m_signexp >> 31) |
---|
445 | return true; |
---|
446 | |
---|
447 | /* Compare all relevant bits */ |
---|
448 | if (m_signexp != x.m_signexp) |
---|
449 | return m_signexp > x.m_signexp; |
---|
450 | |
---|
451 | for (int i = 0; i < BIGITS; i++) |
---|
452 | if (m_mantissa[i] != x.m_mantissa[i]) |
---|
453 | return m_mantissa[i] > x.m_mantissa[i]; |
---|
454 | |
---|
455 | return false; |
---|
456 | } |
---|
457 | |
---|
458 | bool real::operator >=(real const &x) const |
---|
459 | { |
---|
460 | return !(*this < x); |
---|
461 | } |
---|
462 | |
---|
463 | bool real::operator !() const |
---|
464 | { |
---|
465 | return !(bool)*this; |
---|
466 | } |
---|
467 | |
---|
468 | real::operator bool() const |
---|
469 | { |
---|
470 | /* A real is "true" if it is non-zero (exponent is non-zero) AND |
---|
471 | * not NaN (exponent is not full bits OR higher order mantissa is zero) */ |
---|
472 | uint32_t exponent = m_signexp << 1; |
---|
473 | return exponent && (~exponent || m_mantissa[0] == 0); |
---|
474 | } |
---|
475 | |
---|
476 | real re(real const &x) |
---|
477 | { |
---|
478 | if (!(x.m_signexp << 1)) |
---|
479 | { |
---|
480 | real ret = x; |
---|
481 | ret.m_signexp = x.m_signexp | 0x7fffffffu; |
---|
482 | ret.m_mantissa[0] = 0; |
---|
483 | return ret; |
---|
484 | } |
---|
485 | |
---|
486 | /* Use the system's float inversion to approximate 1/x */ |
---|
487 | union { float f; uint32_t x; } u = { 1.0f }, v = { 1.0f }; |
---|
488 | v.x |= x.m_mantissa[0] >> 9; |
---|
489 | v.f = 1.0 / v.f; |
---|
490 | |
---|
491 | real ret; |
---|
492 | ret.m_mantissa[0] = v.x << 9; |
---|
493 | |
---|
494 | uint32_t sign = x.m_signexp & 0x80000000u; |
---|
495 | ret.m_signexp = sign; |
---|
496 | |
---|
497 | int exponent = (x.m_signexp & 0x7fffffffu) + 1; |
---|
498 | exponent = -exponent + (v.x >> 23) - (u.x >> 23); |
---|
499 | ret.m_signexp |= (exponent - 1) & 0x7fffffffu; |
---|
500 | |
---|
501 | /* FIXME: 1+log2(BIGITS) steps of Newton-Raphson seems to be enough for |
---|
502 | * convergence, but this hasn't been checked seriously. */ |
---|
503 | for (int i = 1; i <= real::BIGITS; i *= 2) |
---|
504 | ret = ret * (real::R_2 - ret * x); |
---|
505 | |
---|
506 | return ret; |
---|
507 | } |
---|
508 | |
---|
509 | real sqrt(real const &x) |
---|
510 | { |
---|
511 | /* if zero, return x */ |
---|
512 | if (!(x.m_signexp << 1)) |
---|
513 | return x; |
---|
514 | |
---|
515 | /* if negative, return NaN */ |
---|
516 | if (x.m_signexp >> 31) |
---|
517 | { |
---|
518 | real ret; |
---|
519 | ret.m_signexp = 0x7fffffffu; |
---|
520 | ret.m_mantissa[0] = 0xffffu; |
---|
521 | return ret; |
---|
522 | } |
---|
523 | |
---|
524 | /* Use the system's float inversion to approximate 1/sqrt(x). First |
---|
525 | * we construct a float in the [1..4[ range that has roughly the same |
---|
526 | * mantissa as our real. Its exponent is 0 or 1, depending on the |
---|
527 | * partity of x. The final exponent is 0, -1 or -2. We use the final |
---|
528 | * exponent and final mantissa to pre-fill the result. */ |
---|
529 | union { float f; uint32_t x; } u = { 1.0f }, v = { 2.0f }; |
---|
530 | v.x -= ((x.m_signexp & 1) << 23); |
---|
531 | v.x |= x.m_mantissa[0] >> 9; |
---|
532 | v.f = 1.0 / sqrtf(v.f); |
---|
533 | |
---|
534 | real ret; |
---|
535 | ret.m_mantissa[0] = v.x << 9; |
---|
536 | |
---|
537 | uint32_t sign = x.m_signexp & 0x80000000u; |
---|
538 | ret.m_signexp = sign; |
---|
539 | |
---|
540 | uint32_t exponent = (x.m_signexp & 0x7fffffffu); |
---|
541 | exponent = ((1 << 30) + (1 << 29) - 1) - (exponent + 1) / 2; |
---|
542 | exponent = exponent + (v.x >> 23) - (u.x >> 23); |
---|
543 | ret.m_signexp |= exponent & 0x7fffffffu; |
---|
544 | |
---|
545 | /* FIXME: 1+log2(BIGITS) steps of Newton-Raphson seems to be enough for |
---|
546 | * convergence, but this hasn't been checked seriously. */ |
---|
547 | for (int i = 1; i <= real::BIGITS; i *= 2) |
---|
548 | { |
---|
549 | ret = ret * (real::R_3 - ret * ret * x); |
---|
550 | ret.m_signexp--; |
---|
551 | } |
---|
552 | |
---|
553 | return ret * x; |
---|
554 | } |
---|
555 | |
---|
556 | real fabs(real const &x) |
---|
557 | { |
---|
558 | real ret = x; |
---|
559 | ret.m_signexp &= 0x7fffffffu; |
---|
560 | return ret; |
---|
561 | } |
---|
562 | |
---|
563 | static real fast_log(real const &x) |
---|
564 | { |
---|
565 | /* This fast log method is tuned to work on the [1..2] range and |
---|
566 | * no effort whatsoever was made to improve convergence outside this |
---|
567 | * domain of validity. It can converge pretty fast, provided we use |
---|
568 | * the following variable substitutions: |
---|
569 | * y = sqrt(x) |
---|
570 | * z = (y - 1) / (y + 1) |
---|
571 | * |
---|
572 | * And the following identities: |
---|
573 | * ln(x) = 2 ln(y) |
---|
574 | * = 2 ln((1 + z) / (1 - z)) |
---|
575 | * = 4 z (1 + z^2 / 3 + z^4 / 5 + z^6 / 7...) |
---|
576 | * |
---|
577 | * Any additional sqrt() call would halve the convergence time, but |
---|
578 | * would also impact the final precision. For now we stick with one |
---|
579 | * sqrt() call. */ |
---|
580 | real y = sqrt(x); |
---|
581 | real z = (y - real::R_1) / (y + real::R_1), z2 = z * z, zn = z2; |
---|
582 | real sum = real::R_1; |
---|
583 | |
---|
584 | for (int i = 3; ; i += 2) |
---|
585 | { |
---|
586 | real newsum = sum + zn / (real)i; |
---|
587 | if (newsum == sum) |
---|
588 | break; |
---|
589 | sum = newsum; |
---|
590 | zn *= z2; |
---|
591 | } |
---|
592 | |
---|
593 | return z * (sum << 2); |
---|
594 | } |
---|
595 | |
---|
596 | real log(real const &x) |
---|
597 | { |
---|
598 | /* Strategy for log(x): if x = 2^E*M then log(x) = E log(2) + log(M), |
---|
599 | * with the property that M is in [1..2[, so fast_log() applies here. */ |
---|
600 | real tmp = x; |
---|
601 | if (x.m_signexp >> 31 || x.m_signexp == 0) |
---|
602 | { |
---|
603 | tmp.m_signexp = 0xffffffffu; |
---|
604 | tmp.m_mantissa[0] = 0xffffffffu; |
---|
605 | return tmp; |
---|
606 | } |
---|
607 | tmp.m_signexp = (1 << 30) - 1; |
---|
608 | return (real)(x.m_signexp - (1 << 30) + 1) * real::R_LN2 + fast_log(tmp); |
---|
609 | } |
---|
610 | |
---|
611 | real exp(real const &x) |
---|
612 | { |
---|
613 | /* Strategy for exp(x): the Taylor series does not converge very fast |
---|
614 | * with large positive or negative values. |
---|
615 | * |
---|
616 | * However, we know that the result is going to be in the form M*2^E, |
---|
617 | * where M is the mantissa and E the exponent. We first try to predict |
---|
618 | * a value for E, which is approximately log2(exp(x)) = x / log(2). |
---|
619 | * |
---|
620 | * Let E0 be an integer close to x / log(2). We need to find a value x0 |
---|
621 | * such that exp(x) = 2^E0 * exp(x0). We get x0 = x - E0 log(2). |
---|
622 | * |
---|
623 | * Thus the final algorithm: |
---|
624 | * int E0 = x / log(2) |
---|
625 | * real x0 = x - E0 log(2) |
---|
626 | * real x1 = exp(x0) |
---|
627 | * return x1 * 2^E0 |
---|
628 | */ |
---|
629 | int e0 = x / real::R_LN2; |
---|
630 | real x0 = x - (real)e0 * real::R_LN2; |
---|
631 | real x1 = real::R_1, fact = real::R_1, xn = x0; |
---|
632 | |
---|
633 | for (int i = 1; ; i++) |
---|
634 | { |
---|
635 | fact *= (real)i; |
---|
636 | real newx1 = x1 + xn / fact; |
---|
637 | if (newx1 == x1) |
---|
638 | break; |
---|
639 | x1 = newx1; |
---|
640 | xn *= x0; |
---|
641 | } |
---|
642 | |
---|
643 | x1.m_signexp += e0; |
---|
644 | return x1; |
---|
645 | } |
---|
646 | |
---|
647 | real floor(real const &x) |
---|
648 | { |
---|
649 | /* Strategy for floor(x): |
---|
650 | * - if negative, return -ceil(-x) |
---|
651 | * - if zero or negative zero, return x |
---|
652 | * - if less than one, return zero |
---|
653 | * - otherwise, if e is the exponent, clear all bits except the |
---|
654 | * first e. */ |
---|
655 | if (x < -real::R_0) |
---|
656 | return -ceil(-x); |
---|
657 | if (!x) |
---|
658 | return x; |
---|
659 | if (x < real::R_1) |
---|
660 | return real::R_0; |
---|
661 | |
---|
662 | real ret = x; |
---|
663 | int exponent = x.m_signexp - (1 << 30) + 1; |
---|
664 | |
---|
665 | for (int i = 0; i < real::BIGITS; i++) |
---|
666 | { |
---|
667 | if (exponent <= 0) |
---|
668 | ret.m_mantissa[i] = 0; |
---|
669 | else if (exponent < real::BIGIT_BITS) |
---|
670 | ret.m_mantissa[i] &= ~((1 << (real::BIGIT_BITS - exponent)) - 1); |
---|
671 | |
---|
672 | exponent -= real::BIGIT_BITS; |
---|
673 | } |
---|
674 | |
---|
675 | return ret; |
---|
676 | } |
---|
677 | |
---|
678 | real ceil(real const &x) |
---|
679 | { |
---|
680 | /* Strategy for ceil(x): |
---|
681 | * - if negative, return -floor(-x) |
---|
682 | * - if x == floor(x), return x |
---|
683 | * - otherwise, return floor(x) + 1 */ |
---|
684 | if (x < -real::R_0) |
---|
685 | return -floor(-x); |
---|
686 | real ret = floor(x); |
---|
687 | if (x == ret) |
---|
688 | return ret; |
---|
689 | else |
---|
690 | return ret + real::R_1; |
---|
691 | } |
---|
692 | |
---|
693 | real round(real const &x) |
---|
694 | { |
---|
695 | if (x < real::R_0) |
---|
696 | return -round(-x); |
---|
697 | |
---|
698 | return floor(x + (real::R_1 >> 1)); |
---|
699 | } |
---|
700 | |
---|
701 | real fmod(real const &x, real const &y) |
---|
702 | { |
---|
703 | if (!y) |
---|
704 | return real::R_0; /* FIXME: return NaN */ |
---|
705 | |
---|
706 | if (!x) |
---|
707 | return x; |
---|
708 | |
---|
709 | real tmp = round(x / y); |
---|
710 | return x - tmp * y; |
---|
711 | } |
---|
712 | |
---|
713 | real sin(real const &x) |
---|
714 | { |
---|
715 | bool switch_sign = x.m_signexp & 0x80000000u; |
---|
716 | |
---|
717 | real absx = fmod(fabs(x), real::R_PI << 1); |
---|
718 | if (absx > real::R_PI) |
---|
719 | { |
---|
720 | absx -= real::R_PI; |
---|
721 | switch_sign = !switch_sign; |
---|
722 | } |
---|
723 | |
---|
724 | if (absx > real::R_PI_2) |
---|
725 | absx = real::R_PI - absx; |
---|
726 | |
---|
727 | real ret = real::R_0, fact = real::R_1, xn = absx, x2 = absx * absx; |
---|
728 | for (int i = 1; ; i += 2) |
---|
729 | { |
---|
730 | real newret = ret + xn / fact; |
---|
731 | if (newret == ret) |
---|
732 | break; |
---|
733 | ret = newret; |
---|
734 | xn *= x2; |
---|
735 | fact *= (real)(-(i + 1) * (i + 2)); |
---|
736 | } |
---|
737 | |
---|
738 | /* Propagate sign */ |
---|
739 | if (switch_sign) |
---|
740 | ret.m_signexp ^= 0x80000000u; |
---|
741 | return ret; |
---|
742 | } |
---|
743 | |
---|
744 | real cos(real const &x) |
---|
745 | { |
---|
746 | return sin(real::R_PI_2 - x); |
---|
747 | } |
---|
748 | |
---|
749 | real tan(real const &x) |
---|
750 | { |
---|
751 | /* Constrain input to [-π,π] */ |
---|
752 | real y = fmod(x, real::R_PI); |
---|
753 | |
---|
754 | /* Constrain input to [-π/2,π/2] */ |
---|
755 | if (y < -real::R_PI_2) |
---|
756 | y += real::R_PI; |
---|
757 | else if (y > real::R_PI_2) |
---|
758 | y -= real::R_PI; |
---|
759 | |
---|
760 | /* In [-π/4,π/4] return sin/cos */ |
---|
761 | if (fabs(y) <= real::R_PI_4) |
---|
762 | return sin(y) / cos(y); |
---|
763 | |
---|
764 | /* Otherwise, return cos/sin */ |
---|
765 | if (y > real::R_0) |
---|
766 | y = real::R_PI_2 - y; |
---|
767 | else |
---|
768 | y = -real::R_PI_2 - y; |
---|
769 | |
---|
770 | return cos(y) / sin(y); |
---|
771 | } |
---|
772 | |
---|
773 | static real asinacos(real const &x, bool is_asin, bool is_negative) |
---|
774 | { |
---|
775 | /* Strategy for asin(): in [-0.5..0.5], use a Taylor series around |
---|
776 | * zero. In [0.5..1], use asin(x) = π/2 - 2*asin(sqrt((1-x)/2)), and |
---|
777 | * in [-1..-0.5] just revert the sign. |
---|
778 | * Strategy for acos(): use acos(x) = π/2 - asin(x) and try not to |
---|
779 | * lose the precision around x=1. */ |
---|
780 | real absx = fabs(x); |
---|
781 | bool around_zero = (absx < (real::R_1 >> 1)); |
---|
782 | |
---|
783 | if (!around_zero) |
---|
784 | absx = sqrt((real::R_1 - absx) >> 1); |
---|
785 | |
---|
786 | real ret = absx, xn = absx, x2 = absx * absx, fact1 = 2, fact2 = 1; |
---|
787 | for (int i = 1; ; i++) |
---|
788 | { |
---|
789 | xn *= x2; |
---|
790 | real mul = (real)(2 * i + 1); |
---|
791 | real newret = ret + ((fact1 * xn / (mul * fact2)) >> (i * 2)); |
---|
792 | if (newret == ret) |
---|
793 | break; |
---|
794 | ret = newret; |
---|
795 | fact1 *= (real)((2 * i + 1) * (2 * i + 2)); |
---|
796 | fact2 *= (real)((i + 1) * (i + 1)); |
---|
797 | } |
---|
798 | |
---|
799 | if (is_negative) |
---|
800 | ret = -ret; |
---|
801 | |
---|
802 | if (around_zero) |
---|
803 | ret = is_asin ? ret : real::R_PI_2 - ret; |
---|
804 | else |
---|
805 | { |
---|
806 | real adjust = is_negative ? real::R_PI : real::R_0; |
---|
807 | if (is_asin) |
---|
808 | ret = real::R_PI_2 - adjust - (ret << 1); |
---|
809 | else |
---|
810 | ret = adjust + (ret << 1); |
---|
811 | } |
---|
812 | |
---|
813 | return ret; |
---|
814 | } |
---|
815 | |
---|
816 | real asin(real const &x) |
---|
817 | { |
---|
818 | return asinacos(x, true, x.m_signexp >> 31); |
---|
819 | } |
---|
820 | |
---|
821 | real acos(real const &x) |
---|
822 | { |
---|
823 | return asinacos(x, false, x.m_signexp >> 31); |
---|
824 | } |
---|
825 | |
---|
826 | real atan(real const &x) |
---|
827 | { |
---|
828 | /* Computing atan(x): we choose a different Taylor series depending on |
---|
829 | * the value of x to help with convergence. |
---|
830 | * |
---|
831 | * If |x| < 0.5 we evaluate atan(y) near 0: |
---|
832 | * atan(y) = y - y^3/3 + y^5/5 - y^7/7 + y^9/9 ... |
---|
833 | * |
---|
834 | * If 0.5 <= |x| < 1.5 we evaluate atan(1+y) near 0: |
---|
835 | * atan(1+y) = π/4 + y/(1*2^1) - y^2/(2*2^1) + y^3/(3*2^2) |
---|
836 | * - y^5/(5*2^3) + y^6/(6*2^3) - y^7/(7*2^4) |
---|
837 | * + y^9/(9*2^5) - y^10/(10*2^5) + y^11/(11*2^6) ... |
---|
838 | * |
---|
839 | * If 1.5 <= |x| < 2 we evaluate atan(sqrt(3)+2y) near 0: |
---|
840 | * atan(sqrt(3)+2y) = π/3 + 1/2 y - sqrt(3)/2 y^2/2 |
---|
841 | * + y^3/3 - sqrt(3)/2 y^4/4 + 1/2 y^5/5 |
---|
842 | * - 1/2 y^7/7 + sqrt(3)/2 y^8/8 |
---|
843 | * - y^9/9 + sqrt(3)/2 y^10/10 - 1/2 y^11/11 |
---|
844 | * + 1/2 y^13/13 - sqrt(3)/2 y^14/14 |
---|
845 | * + y^15/15 - sqrt(3)/2 y^16/16 + 1/2 y^17/17 ... |
---|
846 | * |
---|
847 | * If |x| >= 2 we evaluate atan(y) near +∞: |
---|
848 | * atan(y) = π/2 - y^-1 + y^-3/3 - y^-5/5 + y^-7/7 - y^-9/9 ... |
---|
849 | */ |
---|
850 | real absx = fabs(x); |
---|
851 | |
---|
852 | if (absx < (real::R_1 >> 1)) |
---|
853 | { |
---|
854 | real ret = x, xn = x, mx2 = -x * x; |
---|
855 | for (int i = 3; ; i += 2) |
---|
856 | { |
---|
857 | xn *= mx2; |
---|
858 | real newret = ret + xn / (real)i; |
---|
859 | if (newret == ret) |
---|
860 | break; |
---|
861 | ret = newret; |
---|
862 | } |
---|
863 | return ret; |
---|
864 | } |
---|
865 | |
---|
866 | real ret = 0; |
---|
867 | |
---|
868 | if (absx < (real::R_3 >> 1)) |
---|
869 | { |
---|
870 | real y = real::R_1 - absx; |
---|
871 | real yn = y, my2 = -y * y; |
---|
872 | for (int i = 0; ; i += 2) |
---|
873 | { |
---|
874 | real newret = ret + ((yn / (real)(2 * i + 1)) >> (i + 1)); |
---|
875 | yn *= y; |
---|
876 | newret += (yn / (real)(2 * i + 2)) >> (i + 1); |
---|
877 | yn *= y; |
---|
878 | newret += (yn / (real)(2 * i + 3)) >> (i + 2); |
---|
879 | if (newret == ret) |
---|
880 | break; |
---|
881 | ret = newret; |
---|
882 | yn *= my2; |
---|
883 | } |
---|
884 | ret = real::R_PI_4 - ret; |
---|
885 | } |
---|
886 | else if (absx < real::R_2) |
---|
887 | { |
---|
888 | real y = (absx - real::R_SQRT3) >> 1; |
---|
889 | real yn = y, my2 = -y * y; |
---|
890 | for (int i = 1; ; i += 6) |
---|
891 | { |
---|
892 | real newret = ret + ((yn / (real)i) >> 1); |
---|
893 | yn *= y; |
---|
894 | newret -= (real::R_SQRT3 >> 1) * yn / (real)(i + 1); |
---|
895 | yn *= y; |
---|
896 | newret += yn / (real)(i + 2); |
---|
897 | yn *= y; |
---|
898 | newret -= (real::R_SQRT3 >> 1) * yn / (real)(i + 3); |
---|
899 | yn *= y; |
---|
900 | newret += (yn / (real)(i + 4)) >> 1; |
---|
901 | if (newret == ret) |
---|
902 | break; |
---|
903 | ret = newret; |
---|
904 | yn *= my2; |
---|
905 | } |
---|
906 | ret = real::R_PI_3 + ret; |
---|
907 | } |
---|
908 | else |
---|
909 | { |
---|
910 | real y = re(absx); |
---|
911 | real yn = y, my2 = -y * y; |
---|
912 | ret = y; |
---|
913 | for (int i = 3; ; i += 2) |
---|
914 | { |
---|
915 | yn *= my2; |
---|
916 | real newret = ret + yn / (real)i; |
---|
917 | if (newret == ret) |
---|
918 | break; |
---|
919 | ret = newret; |
---|
920 | } |
---|
921 | ret = real::R_PI_2 - ret; |
---|
922 | } |
---|
923 | |
---|
924 | /* Propagate sign */ |
---|
925 | ret.m_signexp |= (x.m_signexp & 0x80000000u); |
---|
926 | return ret; |
---|
927 | } |
---|
928 | |
---|
929 | void real::print(int ndigits) const |
---|
930 | { |
---|
931 | real const r1 = 1, r10 = 10; |
---|
932 | real x = *this; |
---|
933 | |
---|
934 | if (x.m_signexp >> 31) |
---|
935 | { |
---|
936 | printf("-"); |
---|
937 | x = -x; |
---|
938 | } |
---|
939 | |
---|
940 | /* Normalise x so that mantissa is in [1..9.999] */ |
---|
941 | int exponent = 0; |
---|
942 | if (x.m_signexp) |
---|
943 | { |
---|
944 | for (real div = r1, newdiv; true; div = newdiv) |
---|
945 | { |
---|
946 | newdiv = div * r10; |
---|
947 | if (x < newdiv) |
---|
948 | { |
---|
949 | x /= div; |
---|
950 | break; |
---|
951 | } |
---|
952 | exponent++; |
---|
953 | } |
---|
954 | for (real mul = 1, newx; true; mul *= r10) |
---|
955 | { |
---|
956 | newx = x * mul; |
---|
957 | if (newx >= r1) |
---|
958 | { |
---|
959 | x = newx; |
---|
960 | break; |
---|
961 | } |
---|
962 | exponent--; |
---|
963 | } |
---|
964 | } |
---|
965 | |
---|
966 | /* Print digits */ |
---|
967 | for (int i = 0; i < ndigits; i++) |
---|
968 | { |
---|
969 | int digit = (int)x; |
---|
970 | printf("%i", digit); |
---|
971 | if (i == 0) |
---|
972 | printf("."); |
---|
973 | x -= real(digit); |
---|
974 | x *= r10; |
---|
975 | } |
---|
976 | |
---|
977 | /* Print exponent information */ |
---|
978 | if (exponent < 0) |
---|
979 | printf("e-%i", -exponent); |
---|
980 | else if (exponent > 0) |
---|
981 | printf("e+%i", exponent); |
---|
982 | |
---|
983 | printf("\n"); |
---|
984 | } |
---|
985 | |
---|
986 | static real fast_pi() |
---|
987 | { |
---|
988 | /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */ |
---|
989 | real ret = 0.0, x0 = 5.0, x1 = 239.0; |
---|
990 | real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0; |
---|
991 | |
---|
992 | for (int i = 1; ; i += 2) |
---|
993 | { |
---|
994 | real newret = ret + r16 / (x0 * (real)i) - r4 / (x1 * (real)i); |
---|
995 | if (newret == ret) |
---|
996 | break; |
---|
997 | ret = newret; |
---|
998 | x0 *= m0; |
---|
999 | x1 *= m1; |
---|
1000 | } |
---|
1001 | |
---|
1002 | return ret; |
---|
1003 | } |
---|
1004 | |
---|
1005 | real const real::R_0 = (real)0.0; |
---|
1006 | real const real::R_1 = (real)1.0; |
---|
1007 | real const real::R_2 = (real)2.0; |
---|
1008 | real const real::R_3 = (real)3.0; |
---|
1009 | real const real::R_10 = (real)10.0; |
---|
1010 | |
---|
1011 | real const real::R_LN2 = fast_log(R_2); |
---|
1012 | real const real::R_LN10 = log(R_10); |
---|
1013 | real const real::R_LOG2E = re(R_LN2); |
---|
1014 | real const real::R_LOG10E = re(R_LN10); |
---|
1015 | real const real::R_E = exp(R_1); |
---|
1016 | real const real::R_PI = fast_pi(); |
---|
1017 | real const real::R_PI_2 = R_PI >> 1; |
---|
1018 | real const real::R_PI_3 = R_PI / R_3; |
---|
1019 | real const real::R_PI_4 = R_PI >> 2; |
---|
1020 | real const real::R_1_PI = re(R_PI); |
---|
1021 | real const real::R_2_PI = R_1_PI << 1; |
---|
1022 | real const real::R_2_SQRTPI = re(sqrt(R_PI)) << 1; |
---|
1023 | real const real::R_SQRT2 = sqrt(R_2); |
---|
1024 | real const real::R_SQRT3 = sqrt(R_3); |
---|
1025 | real const real::R_SQRT1_2 = R_SQRT2 >> 1; |
---|
1026 | |
---|
1027 | } /* namespace lol */ |
---|
1028 | |
---|