source: trunk/src/real.cpp @ 986

Last change on this file since 986 was 986, checked in by sam, 11 years ago

core: add sqrt() for real numbers.

File size: 13.0 KB
Line 
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
20using namespace std;
21
22namespace lol
23{
24
25real::real(float f) { *this = (double)f; }
26real::real(int i) { *this = (double)i; }
27real::real(unsigned int i) { *this = (double)i; }
28
29real::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 >> 36;
50    m_mantissa[1] = u.x >> 20;
51    m_mantissa[2] = u.x >> 4;
52    m_mantissa[3] = u.x << 12;
53    memset(m_mantissa + 4, 0, sizeof(m_mantissa) - 4 * sizeof(m_mantissa[0]));
54}
55
56real::operator float() const { return (float)(double)(*this); }
57real::operator int() const { return (int)(double)(*this); }
58real::operator unsigned int() const { return (unsigned int)(double)(*this); }
59
60real::operator double() const
61{
62    union { double d; uint64_t x; } u;
63
64    /* Get sign */
65    u.x = m_signexp >> 31;
66    u.x <<= 11;
67
68    /* Compute new exponent */
69    uint32_t exponent = (m_signexp << 1) >> 1;
70    int e = (int)exponent - (1 << 30) + (1 << 10);
71
72    if (e < 0)
73        u.x <<= 52;
74    else if (e >= 0x7ff)
75    {
76        u.x |= 0x7ff;
77        u.x <<= 52;
78    }
79    else
80    {
81        u.x |= e;
82
83        /* Store mantissa if necessary */
84        u.x <<= 16;
85        u.x |= m_mantissa[0];
86        u.x <<= 16;
87        u.x |= m_mantissa[1];
88        u.x <<= 16;
89        u.x |= m_mantissa[2];
90        u.x <<= 4;
91        u.x |= m_mantissa[3] >> 12;
92        /* Rounding */
93        u.x += (m_mantissa[3] >> 11) & 1;
94    }
95
96    return u.d;
97}
98
99real real::operator -() const
100{
101    real ret = *this;
102    ret.m_signexp ^= 0x80000000u;
103    return ret;
104}
105
106real real::operator +(real const &x) const
107{
108    if (x.m_signexp << 1 == 0)
109        return *this;
110
111    /* Ensure both arguments are positive. Otherwise, switch signs,
112     * or replace + with -. */
113    if (m_signexp >> 31)
114        return -(-*this + -x);
115
116    if (x.m_signexp >> 31)
117        return *this - (-x);
118
119    /* Ensure *this is the larger exponent (no need to be strictly larger,
120     * as in subtraction). Otherwise, switch. */
121    if ((m_signexp << 1) < (x.m_signexp << 1))
122        return x + *this;
123
124    real ret;
125
126    int e1 = m_signexp - (1 << 30) + 1;
127    int e2 = x.m_signexp - (1 << 30) + 1;
128
129    int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
130    int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);
131
132    ret.m_signexp = m_signexp;
133
134    uint32_t carry = 0;
135    for (int i = BIGITS; i--; )
136    {
137        carry += m_mantissa[i];
138        if (i - bigoff >= 0)
139            carry += x.m_mantissa[i - bigoff] >> off;
140
141        if (i - bigoff > 0)
142            carry += (x.m_mantissa[i - bigoff - 1] << (16 - off)) & 0xffffu;
143        else if (i - bigoff == 0)
144            carry += 0x0001u << (16 - off);
145
146        ret.m_mantissa[i] = carry;
147        carry >>= 16;
148    }
149
150    /* Renormalise in case we overflowed the mantissa */
151    if (carry)
152    {
153        carry--;
154        for (int i = 0; i < BIGITS; i++)
155        {
156            uint16_t tmp = ret.m_mantissa[i];
157            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
158            carry = tmp & 0x0001u;
159        }
160        ret.m_signexp++;
161    }
162
163    return ret;
164}
165
166real real::operator -(real const &x) const
167{
168    if (x.m_signexp << 1 == 0)
169        return *this;
170
171    /* Ensure both arguments are positive. Otherwise, switch signs,
172     * or replace - with +. */
173    if (m_signexp >> 31)
174        return -(-*this + x);
175
176    if (x.m_signexp >> 31)
177        return (*this) + (-x);
178
179    /* Ensure *this is larger than x */
180    if (*this < x)
181        return -(x - *this);
182
183    real ret;
184
185    int e1 = m_signexp - (1 << 30) + 1;
186    int e2 = x.m_signexp - (1 << 30) + 1;
187
188    int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
189    int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);
190
191    ret.m_signexp = m_signexp;
192
193    int32_t carry = 0;
194    for (int i = 0; i < bigoff; i++)
195    {
196        carry -= x.m_mantissa[BIGITS - i];
197        carry = (carry & 0xffff0000u) | (carry >> 16);
198    }
199    carry -= x.m_mantissa[BIGITS - 1 - bigoff] & ((1 << off) - 1);
200    carry /= (1 << off);
201
202    for (int i = BIGITS; i--; )
203    {
204        carry += m_mantissa[i];
205        if (i - bigoff >= 0)
206            carry -= x.m_mantissa[i - bigoff] >> off;
207
208        if (i - bigoff > 0)
209            carry -= (x.m_mantissa[i - bigoff - 1] << (16 - off)) & 0xffffu;
210        else if (i - bigoff == 0)
211            carry -= 0x0001u << (16 - off);
212
213        ret.m_mantissa[i] = carry;
214        carry = (carry & 0xffff0000u) | (carry >> 16);
215    }
216
217    carry += 1;
218
219    /* Renormalise if we underflowed the mantissa */
220    if (carry == 0)
221    {
222        /* How much do we need to shift the mantissa? FIXME: this could
223         * be computed above */
224        off = 0;
225        for (int i = 0; i < BIGITS; i++)
226        {
227            if (!ret.m_mantissa[i])
228            {
229                off += sizeof(uint16_t) * 8;
230                continue;
231            }
232
233            for (uint16_t tmp = ret.m_mantissa[i]; tmp < 0x8000u; tmp <<= 1)
234                off++;
235            break;
236        }
237        if (off == BIGITS * sizeof(uint16_t) * 8)
238            ret.m_signexp &= 0x80000000u;
239        else
240        {
241            off++; /* Shift one more to get rid of the leading one */
242            ret.m_signexp -= off;
243
244            bigoff = off / (sizeof(uint16_t) * 8);
245            off -= bigoff * sizeof(uint16_t) * 8;
246
247            for (int i = 0; i < BIGITS; i++)
248            {
249                uint16_t tmp = 0;
250                if (i + bigoff < BIGITS)
251                    tmp |= ret.m_mantissa[i + bigoff] << off;
252                if (i + bigoff + 1 < BIGITS)
253                    tmp |= ret.m_mantissa[i + bigoff + 1] >> (16 - off);
254                ret.m_mantissa[i] = tmp;
255            }
256        }
257    }
258
259    return ret;
260}
261
262real real::operator *(real const &x) const
263{
264    real ret;
265
266    ret.m_signexp = (m_signexp ^ x.m_signexp) & 0x80000000u;
267    int e = (m_signexp & 0x7fffffffu) - (1 << 30) + 1
268          + (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1;
269
270    /* Accumulate low order product; no need to store it, we just
271     * want the carry value */
272    uint64_t carry = 0;
273    for (int i = 0; i < BIGITS; i++)
274    {
275        for (int j = 0; j < i + 1; j++)
276            carry += (uint32_t)m_mantissa[BIGITS - 1 - j]
277                   * (uint32_t)x.m_mantissa[BIGITS - 1 + j - i];
278        carry >>= 16;
279    }
280
281    for (int i = 0; i < BIGITS; i++)
282    {
283        for (int j = i + 1; j < BIGITS; j++)
284            carry += (uint32_t)m_mantissa[BIGITS - 1 - j]
285                   * (uint32_t)x.m_mantissa[j - 1 - i];
286
287        carry += m_mantissa[BIGITS - 1 - i];
288        carry += x.m_mantissa[BIGITS - 1 - i];
289        ret.m_mantissa[BIGITS - 1 - i] = carry & 0xffffu;
290        carry >>= 16;
291    }
292
293    /* Renormalise in case we overflowed the mantissa */
294    if (carry)
295    {
296        carry--;
297        for (int i = 0; i < BIGITS; i++)
298        {
299            uint16_t tmp = ret.m_mantissa[i];
300            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
301            carry = tmp & 0x0001u;
302        }
303        e++;
304    }
305
306    ret.m_signexp |= e + (1 << 30) - 1;
307
308    return ret;
309}
310
311real real::operator /(real const &x) const
312{
313    return *this * fres(x);
314}
315
316real &real::operator +=(real const &x)
317{
318    real tmp = *this;
319    return *this = tmp + x;
320}
321
322real &real::operator -=(real const &x)
323{
324    real tmp = *this;
325    return *this = tmp - x;
326}
327
328real &real::operator *=(real const &x)
329{
330    real tmp = *this;
331    return *this = tmp * x;
332}
333
334real &real::operator /=(real const &x)
335{
336    real tmp = *this;
337    return *this = tmp / x;
338}
339
340bool real::operator ==(real const &x) const
341{
342    if (m_signexp != x.m_signexp)
343        return false;
344
345    return memcmp(m_mantissa, x.m_mantissa, sizeof(m_mantissa)) == 0;
346}
347
348bool real::operator !=(real const &x) const
349{
350    return !(*this == x);
351}
352
353bool real::operator <(real const &x) const
354{
355    /* Ensure both numbers are positive */
356    if (m_signexp >> 31)
357        return (x.m_signexp >> 31) ? -*this > -x : true;
358
359    if (x.m_signexp >> 31)
360        return false;
361
362    /* Compare all relevant bits */
363    if (m_signexp != x.m_signexp)
364        return m_signexp < x.m_signexp;
365
366    for (int i = 0; i < BIGITS; i++)
367        if (m_mantissa[i] != x.m_mantissa[i])
368            return m_mantissa[i] < x.m_mantissa[i];
369
370    return false;
371}
372
373bool real::operator <=(real const &x) const
374{
375    return !(*this > x);
376}
377
378bool real::operator >(real const &x) const
379{
380    /* Ensure both numbers are positive */
381    if (m_signexp >> 31)
382        return (x.m_signexp >> 31) ? -*this < -x : false;
383
384    if (x.m_signexp >> 31)
385        return true;
386
387    /* Compare all relevant bits */
388    if (m_signexp != x.m_signexp)
389        return m_signexp > x.m_signexp;
390
391    for (int i = 0; i < BIGITS; i++)
392        if (m_mantissa[i] != x.m_mantissa[i])
393            return m_mantissa[i] > x.m_mantissa[i];
394
395    return false;
396}
397
398bool real::operator >=(real const &x) const
399{
400    return !(*this < x);
401}
402
403real fres(real const &x)
404{
405    if (!(x.m_signexp << 1))
406    {
407        real ret = x;
408        ret.m_signexp = x.m_signexp | 0x7fffffffu;
409        ret.m_mantissa[0] = 0;
410        return ret;
411    }
412
413    /* Use the system's float inversion to approximate 1/x */
414    union { float f; uint32_t x; } u = { 1.0f }, v = { 1.0f };
415    v.x |= (uint32_t)x.m_mantissa[0] << 7;
416    v.x |= (uint32_t)x.m_mantissa[1] >> 9;
417    v.f = 1.0 / v.f;
418
419    real ret;
420    ret.m_mantissa[0] = (v.x >> 7) & 0xffffu;
421    ret.m_mantissa[1] = (v.x << 9) & 0xffffu;
422
423    uint32_t sign = x.m_signexp & 0x80000000u;
424    ret.m_signexp = sign;
425
426    int exponent = (x.m_signexp & 0x7fffffffu) + 1;
427    exponent = -exponent + (v.x >> 23) - (u.x >> 23);
428    ret.m_signexp |= (exponent - 1) & 0x7fffffffu;
429
430    /* Five steps of Newton-Raphson seems enough for 32-bigit reals. */
431    real two = 2;
432    ret = ret * (two - ret * x);
433    ret = ret * (two - ret * x);
434    ret = ret * (two - ret * x);
435    ret = ret * (two - ret * x);
436    ret = ret * (two - ret * x);
437
438    return ret;
439}
440
441real sqrt(real const &x)
442{
443    /* if zero, return x */
444    if (!(x.m_signexp << 1))
445        return x;
446
447    /* if negative, return NaN */
448    if (x.m_signexp >> 31)
449    {
450        real ret;
451        ret.m_signexp = 0x7fffffffu;
452        ret.m_mantissa[0] = 0xffffu;
453        return ret;
454    }
455
456    /* Use the system's float inversion to approximate 1/sqrt(x). First
457     * we construct a float in the [1..4[ range that has roughly the same
458     * mantissa as our real. Its exponent is 0 or 1, depending on the
459     * partity of x. The final exponent is 0, -1 or -2. We use the final
460     * exponent and final mantissa to pre-fill the result. */
461    union { float f; uint32_t x; } u = { 1.0f }, v = { 2.0f };
462    v.x -= ((x.m_signexp & 1) << 23);
463    v.x |= (uint32_t)x.m_mantissa[0] << 7;
464    v.x |= (uint32_t)x.m_mantissa[1] >> 9;
465    v.f = 1.0 / sqrtf(v.f);
466
467    real ret;
468    ret.m_mantissa[0] = (v.x >> 7) & 0xffffu;
469    ret.m_mantissa[1] = (v.x << 9) & 0xffffu;
470
471    uint32_t sign = x.m_signexp & 0x80000000u;
472    ret.m_signexp = sign;
473
474    int exponent = (x.m_signexp & 0x7fffffffu) - ((1 << 30) - 1);
475    exponent = - (exponent / 2) + (v.x >> 23) - (u.x >> 23);
476    ret.m_signexp |= (exponent + ((1 << 30) - 1)) & 0x7fffffffu;
477
478    /* Five steps of Newton-Raphson seems enough for 32-bigit reals. */
479    real three = 3;
480    ret = ret * (three - ret * ret * x);
481    ret.m_signexp--;
482    ret = ret * (three - ret * ret * x);
483    ret.m_signexp--;
484    ret = ret * (three - ret * ret * x);
485    ret.m_signexp--;
486    ret = ret * (three - ret * ret * x);
487    ret.m_signexp--;
488    ret = ret * (three - ret * ret * x);
489    ret.m_signexp--;
490
491    return ret * x;
492}
493
494void real::print(int ndigits) const
495{
496    real const r1 = 1, r10 = 10;
497    real x = *this;
498
499    if (x.m_signexp >> 31)
500    {
501        printf("-");
502        x = -x;
503    }
504
505    /* Normalise x so that mantissa is in [1..9.999] */
506    int exponent = 0;
507    for (real div = r1, newdiv; true; div = newdiv)
508    {
509        newdiv = div * r10;
510        if (x < newdiv)
511        {
512            x /= div;
513            break;
514        }
515        exponent++;
516    }
517    for (real mul = 1, newx; true; mul *= r10)
518    {
519        newx = x * mul;
520        if (newx >= r1)
521        {
522            x = newx;
523            break;
524        }
525        exponent--;
526    }
527
528    /* Print digits */
529    for (int i = 0; i < ndigits; i++)
530    {
531        int digit = (int)x;
532        printf("%i", digit);
533        if (i == 0)
534            printf(".");
535        x -= real(digit);
536        x *= r10;
537    }
538
539    /* Print exponent information */
540    if (exponent < 0)
541        printf("e-%i", -exponent);
542    else if (exponent > 0)
543        printf("e+%i", exponent);
544
545    printf("\n");
546}
547
548} /* namespace lol */
549
Note: See TracBrowser for help on using the repository browser.