source: trunk/src/real.cpp @ 972

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

core: fix real unary minus and handle negative numbers in additions.

File size: 4.9 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)
26{
27    union { float f; uint32_t x; } u = { f };
28
29    uint32_t sign = u.x & 0x80000000u;
30    uint32_t exponent = (u.x >> 23) & 0xff;
31
32    switch (exponent)
33    {
34    case 0x00:
35    case 0xff:
36        m_signexp = sign | exponent;
37        break;
38    default:
39        m_signexp = sign | (exponent + (1 << 30) - (1 << 7));
40        break;
41    }
42
43    m_mantissa[0] = u.x >> 7;
44    m_mantissa[1] = u.x << 9;
45    memset(m_mantissa + 2, 0, sizeof(m_mantissa) - sizeof(m_mantissa[0]));
46}
47
48real::operator float() const
49{
50    union { float f; uint32_t x; } u;
51
52    uint32_t sign = m_signexp & 0x80000000u;
53    uint32_t exponent = m_signexp & 0x7fffffffu;
54    uint32_t mantissa = (m_mantissa[0] << 7) | (m_mantissa[1] >> 9);
55
56    int e = (int)exponent - (1 << 30) + (1 << 7);
57
58    if (e < 0)
59        u.x = sign;
60    else if (e >= 0xff)
61        u.x = sign | (0xff << 23);
62    else
63        u.x = sign | (e << 23) | mantissa;
64
65    return u.f;
66}
67
68real real::operator -() const
69{
70    real ret = *this;
71    ret.m_signexp ^= 0x80000000u;
72    return ret;
73}
74
75real real::operator +(real const &x) const
76{
77    if (x.m_signexp << 1 == 0)
78        return *this;
79
80    /* Ensure *this is the larger exponent, and both arguments are
81     * positive (otherwise, switch signs, or replace + with -). */
82    if ((m_signexp << 1) < (x.m_signexp << 1))
83        return x + *this;
84
85    if (m_signexp >> 31)
86        return -(-*this + -x);
87
88    if (x.m_signexp >> 31)
89        return *this - x;
90
91    real ret;
92
93    int e1 = m_signexp - (1 << 30) + 1;
94    int e2 = x.m_signexp - (1 << 30) + 1;
95
96    int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
97    int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);
98
99    ret.m_signexp = m_signexp;
100
101    uint32_t carry = 0;
102    for (int i = BIGITS; i--; )
103    {
104        carry = m_mantissa[i];
105        if (i - bigoff >= 0)
106            carry += x.m_mantissa[i - bigoff] >> off;
107        else if (i - bigoff == -1)
108            carry += 0x0001u >> off;
109
110        if (i - bigoff > 0)
111            carry += (x.m_mantissa[i - bigoff - 1] << (16 - off)) & 0xffffu;
112        else if (i - bigoff == 0)
113            carry += 0x0001u << (16 - off);
114
115        ret.m_mantissa[i] = carry;
116        carry >>= 16;
117    }
118
119    /* Renormalise in case we overflowed the mantissa */
120    if (carry)
121    {
122        carry--;
123        for (int i = 0; i < BIGITS; i++)
124        {
125            uint16_t tmp = ret.m_mantissa[i];
126            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
127            carry = tmp & 0x0001u;
128        }
129        ret.m_signexp++;
130    }
131
132    return ret;
133}
134
135real real::operator -(real const &x) const
136{
137    if (x.m_signexp << 1 == 0)
138        return *this;
139
140    /* Ensure *this is the larger exponent, and both arguments are
141     * positive (otherwise, switch signs, or replace - with +). */
142    if ((m_signexp << 1) < (x.m_signexp << 1))
143        return -(x - *this);
144
145    if (m_signexp >> 31)
146        return -(-*this + x);
147
148    if (x.m_signexp >> 31)
149        return (*this) + (-x);
150
151    real ret;
152
153    return ret;
154}
155
156real real::operator *(real const &x) const
157{
158    real ret;
159
160    ret.m_signexp = (m_signexp ^ x.m_signexp) & 0x80000000u;
161    int e = (m_signexp & 0x7fffffffu) - (1 << 30) + 1
162          + (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1;
163
164    /* Accumulate low order product; no need to store it, we just
165     * want the carry value */
166    uint32_t carry = 0;
167    for (int i = 0; i < BIGITS; i++)
168    {
169        for (int j = 0; j < i + 1; j++)
170            carry += m_mantissa[BIGITS - 1 - j]
171                   * x.m_mantissa[BIGITS - 1 + j - i];
172        carry >>= 16;
173    }
174
175    for (int i = 0; i < BIGITS; i++)
176    {
177        for (int j = i + 1; j < BIGITS; j++)
178            carry += m_mantissa[BIGITS - 1 - j]
179                   * x.m_mantissa[j - 1 - i];
180
181        carry += m_mantissa[BIGITS - 1 - i];
182        carry += x.m_mantissa[BIGITS - 1 - i];
183        ret.m_mantissa[BIGITS - 1 - i] = carry & 0xffffu;
184        carry >>= 16;
185    }
186
187    /* Renormalise in case we overflowed the mantissa */
188    if (carry)
189    {
190        carry--;
191        for (int i = 0; i < BIGITS; i++)
192        {
193            uint16_t tmp = ret.m_mantissa[i];
194            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
195            carry = tmp & 0x0001u;
196        }
197        e++;
198    }
199
200    ret.m_signexp |= e + (1 << 30) - 1;
201
202    return ret;
203}
204
205void real::print() const
206{
207    printf("%x  %08x  ", m_signexp >> 31, (m_signexp << 1) >> 1);
208    for (int i = 0; i < BIGITS; i++)
209        printf("%04x ", m_mantissa[i]);
210    printf("\n");
211}
212
213} /* namespace lol */
214
Note: See TracBrowser for help on using the repository browser.