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 | static real asinacos(real const &x, bool is_asin, bool is_negative) |
---|
750 | { |
---|
751 | /* Strategy for asin(): in [-0.5..0.5], use a Taylor series around |
---|
752 | * zero. In [0.5..1], use asin(x) = π/2 - 2*asin(sqrt((1-x)/2)), and |
---|
753 | * in [-1..-0.5] just revert the sign. |
---|
754 | * Strategy for acos(): use acos(x) = π/2 - asin(x) and try not to |
---|
755 | * lose the precision around x=1. */ |
---|
756 | real absx = fabs(x); |
---|
757 | bool around_zero = (absx < (real::R_1 >> 1)); |
---|
758 | |
---|
759 | if (!around_zero) |
---|
760 | absx = sqrt((real::R_1 - absx) >> 1); |
---|
761 | |
---|
762 | real ret = absx, xn = absx, x2 = absx * absx, fact1 = 2, fact2 = 1; |
---|
763 | for (int i = 1; ; i++) |
---|
764 | { |
---|
765 | xn *= x2; |
---|
766 | real mul = (real)(2 * i + 1); |
---|
767 | real newret = ret + ((fact1 * xn / (mul * fact2)) >> (i * 2)); |
---|
768 | if (newret == ret) |
---|
769 | break; |
---|
770 | ret = newret; |
---|
771 | fact1 *= (real)((2 * i + 1) * (2 * i + 2)); |
---|
772 | fact2 *= (real)((i + 1) * (i + 1)); |
---|
773 | } |
---|
774 | |
---|
775 | if (is_negative) |
---|
776 | ret = -ret; |
---|
777 | |
---|
778 | if (around_zero) |
---|
779 | ret = is_asin ? ret : real::R_PI_2 - ret; |
---|
780 | else |
---|
781 | { |
---|
782 | real adjust = is_negative ? real::R_PI : real::R_0; |
---|
783 | if (is_asin) |
---|
784 | ret = real::R_PI_2 - adjust - (ret << 1); |
---|
785 | else |
---|
786 | ret = adjust + (ret << 1); |
---|
787 | } |
---|
788 | |
---|
789 | return ret; |
---|
790 | } |
---|
791 | |
---|
792 | real asin(real const &x) |
---|
793 | { |
---|
794 | return asinacos(x, true, x.m_signexp >> 31); |
---|
795 | } |
---|
796 | |
---|
797 | real acos(real const &x) |
---|
798 | { |
---|
799 | return asinacos(x, false, x.m_signexp >> 31); |
---|
800 | } |
---|
801 | |
---|
802 | real atan(real const &x) |
---|
803 | { |
---|
804 | /* Computing atan(x): we choose a different Taylor series depending on |
---|
805 | * the value of x to help with convergence. |
---|
806 | * |
---|
807 | * If |x| < 0.5 we evaluate atan(y) near 0: |
---|
808 | * atan(y) = y - y^3/3 + y^5/5 - y^7/7 + y^9/9 ... |
---|
809 | * |
---|
810 | * If 0.5 <= |x| < 1.5 we evaluate atan(1+y) near 0: |
---|
811 | * atan(1+y) = π/4 + y/(1*2^1) - y^2/(2*2^1) + y^3/(3*2^2) |
---|
812 | * - y^5/(5*2^3) + y^6/(6*2^3) - y^7/(7*2^4) |
---|
813 | * + y^9/(9*2^5) - y^10/(10*2^5) + y^11/(11*2^6) ... |
---|
814 | * |
---|
815 | * If 1.5 <= |x| < 2 we evaluate atan(sqrt(3)+2y) near 0: |
---|
816 | * atan(sqrt(3)+2y) = π/3 + 1/2 y - sqrt(3)/2 y^2/2 |
---|
817 | * + y^3/3 - sqrt(3)/2 y^4/4 + 1/2 y^5/5 |
---|
818 | * - 1/2 y^7/7 + sqrt(3)/2 y^8/8 |
---|
819 | * - y^9/9 + sqrt(3)/2 y^10/10 - 1/2 y^11/11 |
---|
820 | * + 1/2 y^13/13 - sqrt(3)/2 y^14/14 |
---|
821 | * + y^15/15 - sqrt(3)/2 y^16/16 + 1/2 y^17/17 ... |
---|
822 | * |
---|
823 | * If |x| >= 2 we evaluate atan(y) near +∞: |
---|
824 | * atan(y) = π/2 - y^-1 + y^-3/3 - y^-5/5 + y^-7/7 - y^-9/9 ... |
---|
825 | */ |
---|
826 | real absx = fabs(x); |
---|
827 | |
---|
828 | if (absx < (real::R_1 >> 1)) |
---|
829 | { |
---|
830 | real ret = x, xn = x, mx2 = -x * x; |
---|
831 | for (int i = 3; ; i += 2) |
---|
832 | { |
---|
833 | xn *= mx2; |
---|
834 | real newret = ret + xn / (real)i; |
---|
835 | if (newret == ret) |
---|
836 | break; |
---|
837 | ret = newret; |
---|
838 | } |
---|
839 | return ret; |
---|
840 | } |
---|
841 | |
---|
842 | real ret = 0; |
---|
843 | |
---|
844 | if (absx < (real::R_3 >> 1)) |
---|
845 | { |
---|
846 | real y = real::R_1 - absx; |
---|
847 | real yn = y, my2 = -y * y; |
---|
848 | for (int i = 0; ; i += 2) |
---|
849 | { |
---|
850 | real newret = ret + ((yn / (real)(2 * i + 1)) >> (i + 1)); |
---|
851 | yn *= y; |
---|
852 | newret += (yn / (real)(2 * i + 2)) >> (i + 1); |
---|
853 | yn *= y; |
---|
854 | newret += (yn / (real)(2 * i + 3)) >> (i + 2); |
---|
855 | if (newret == ret) |
---|
856 | break; |
---|
857 | ret = newret; |
---|
858 | yn *= my2; |
---|
859 | } |
---|
860 | ret = real::R_PI_4 - ret; |
---|
861 | } |
---|
862 | else if (absx < real::R_2) |
---|
863 | { |
---|
864 | real y = (absx - real::R_SQRT3) >> 1; |
---|
865 | real yn = y, my2 = -y * y; |
---|
866 | for (int i = 1; ; i += 6) |
---|
867 | { |
---|
868 | real newret = ret + ((yn / (real)i) >> 1); |
---|
869 | yn *= y; |
---|
870 | newret -= (real::R_SQRT3 >> 1) * yn / (real)(i + 1); |
---|
871 | yn *= y; |
---|
872 | newret += yn / (real)(i + 2); |
---|
873 | yn *= y; |
---|
874 | newret -= (real::R_SQRT3 >> 1) * yn / (real)(i + 3); |
---|
875 | yn *= y; |
---|
876 | newret += (yn / (real)(i + 4)) >> 1; |
---|
877 | if (newret == ret) |
---|
878 | break; |
---|
879 | ret = newret; |
---|
880 | yn *= my2; |
---|
881 | } |
---|
882 | ret = real::R_PI_3 + ret; |
---|
883 | } |
---|
884 | else |
---|
885 | { |
---|
886 | real y = re(absx); |
---|
887 | real yn = y, my2 = -y * y; |
---|
888 | ret = y; |
---|
889 | for (int i = 3; ; i += 2) |
---|
890 | { |
---|
891 | yn *= my2; |
---|
892 | real newret = ret + yn / (real)i; |
---|
893 | if (newret == ret) |
---|
894 | break; |
---|
895 | ret = newret; |
---|
896 | } |
---|
897 | ret = real::R_PI_2 - ret; |
---|
898 | } |
---|
899 | |
---|
900 | /* Propagate sign */ |
---|
901 | ret.m_signexp |= (x.m_signexp & 0x80000000u); |
---|
902 | return ret; |
---|
903 | } |
---|
904 | |
---|
905 | void real::print(int ndigits) const |
---|
906 | { |
---|
907 | real const r1 = 1, r10 = 10; |
---|
908 | real x = *this; |
---|
909 | |
---|
910 | if (x.m_signexp >> 31) |
---|
911 | { |
---|
912 | printf("-"); |
---|
913 | x = -x; |
---|
914 | } |
---|
915 | |
---|
916 | /* Normalise x so that mantissa is in [1..9.999] */ |
---|
917 | int exponent = 0; |
---|
918 | if (x.m_signexp) |
---|
919 | { |
---|
920 | for (real div = r1, newdiv; true; div = newdiv) |
---|
921 | { |
---|
922 | newdiv = div * r10; |
---|
923 | if (x < newdiv) |
---|
924 | { |
---|
925 | x /= div; |
---|
926 | break; |
---|
927 | } |
---|
928 | exponent++; |
---|
929 | } |
---|
930 | for (real mul = 1, newx; true; mul *= r10) |
---|
931 | { |
---|
932 | newx = x * mul; |
---|
933 | if (newx >= r1) |
---|
934 | { |
---|
935 | x = newx; |
---|
936 | break; |
---|
937 | } |
---|
938 | exponent--; |
---|
939 | } |
---|
940 | } |
---|
941 | |
---|
942 | /* Print digits */ |
---|
943 | for (int i = 0; i < ndigits; i++) |
---|
944 | { |
---|
945 | int digit = (int)x; |
---|
946 | printf("%i", digit); |
---|
947 | if (i == 0) |
---|
948 | printf("."); |
---|
949 | x -= real(digit); |
---|
950 | x *= r10; |
---|
951 | } |
---|
952 | |
---|
953 | /* Print exponent information */ |
---|
954 | if (exponent < 0) |
---|
955 | printf("e-%i", -exponent); |
---|
956 | else if (exponent > 0) |
---|
957 | printf("e+%i", exponent); |
---|
958 | |
---|
959 | printf("\n"); |
---|
960 | } |
---|
961 | |
---|
962 | static real fast_pi() |
---|
963 | { |
---|
964 | /* Approximate Pi using Machin's formula: 16*atan(1/5)-4*atan(1/239) */ |
---|
965 | real ret = 0.0, x0 = 5.0, x1 = 239.0; |
---|
966 | real const m0 = -x0 * x0, m1 = -x1 * x1, r16 = 16.0, r4 = 4.0; |
---|
967 | |
---|
968 | for (int i = 1; ; i += 2) |
---|
969 | { |
---|
970 | real newret = ret + r16 / (x0 * (real)i) - r4 / (x1 * (real)i); |
---|
971 | if (newret == ret) |
---|
972 | break; |
---|
973 | ret = newret; |
---|
974 | x0 *= m0; |
---|
975 | x1 *= m1; |
---|
976 | } |
---|
977 | |
---|
978 | return ret; |
---|
979 | } |
---|
980 | |
---|
981 | real const real::R_0 = (real)0.0; |
---|
982 | real const real::R_1 = (real)1.0; |
---|
983 | real const real::R_2 = (real)2.0; |
---|
984 | real const real::R_3 = (real)3.0; |
---|
985 | real const real::R_10 = (real)10.0; |
---|
986 | |
---|
987 | real const real::R_LN2 = fast_log(R_2); |
---|
988 | real const real::R_LN10 = log(R_10); |
---|
989 | real const real::R_LOG2E = re(R_LN2); |
---|
990 | real const real::R_LOG10E = re(R_LN10); |
---|
991 | real const real::R_E = exp(R_1); |
---|
992 | real const real::R_PI = fast_pi(); |
---|
993 | real const real::R_PI_2 = R_PI >> 1; |
---|
994 | real const real::R_PI_3 = R_PI / R_3; |
---|
995 | real const real::R_PI_4 = R_PI >> 2; |
---|
996 | real const real::R_1_PI = re(R_PI); |
---|
997 | real const real::R_2_PI = R_1_PI << 1; |
---|
998 | real const real::R_2_SQRTPI = re(sqrt(R_PI)) << 1; |
---|
999 | real const real::R_SQRT2 = sqrt(R_2); |
---|
1000 | real const real::R_SQRT3 = sqrt(R_3); |
---|
1001 | real const real::R_SQRT1_2 = R_SQRT2 >> 1; |
---|
1002 | |
---|
1003 | } /* namespace lol */ |
---|
1004 | |
---|