source: trunk/src/real.cpp @ 973

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

core: implement comparison operators for reals.

File size: 6.1 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 both arguments are positive. Otherwise, switch signs,
81     * or replace + with -). */
82    if (m_signexp >> 31)
83        return -(-*this + -x);
84
85    if (x.m_signexp >> 31)
86        return *this - x;
87
88    /* Ensure *this is the larger exponent (no need to be strictly larger,
89     * as in subtraction). Otherwise, switch. */
90    if ((m_signexp << 1) < (x.m_signexp << 1))
91        return x + *this;
92
93    real ret;
94
95    int e1 = m_signexp - (1 << 30) + 1;
96    int e2 = x.m_signexp - (1 << 30) + 1;
97
98    int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8);
99    int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8);
100
101    ret.m_signexp = m_signexp;
102
103    uint32_t carry = 0;
104    for (int i = BIGITS; i--; )
105    {
106        carry = m_mantissa[i];
107        if (i - bigoff >= 0)
108            carry += x.m_mantissa[i - bigoff] >> off;
109        else if (i - bigoff == -1)
110            carry += 0x0001u >> off;
111
112        if (i - bigoff > 0)
113            carry += (x.m_mantissa[i - bigoff - 1] << (16 - off)) & 0xffffu;
114        else if (i - bigoff == 0)
115            carry += 0x0001u << (16 - off);
116
117        ret.m_mantissa[i] = carry;
118        carry >>= 16;
119    }
120
121    /* Renormalise in case we overflowed the mantissa */
122    if (carry)
123    {
124        carry--;
125        for (int i = 0; i < BIGITS; i++)
126        {
127            uint16_t tmp = ret.m_mantissa[i];
128            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
129            carry = tmp & 0x0001u;
130        }
131        ret.m_signexp++;
132    }
133
134    return ret;
135}
136
137real real::operator -(real const &x) const
138{
139    if (x.m_signexp << 1 == 0)
140        return *this;
141
142    /* Ensure both arguments are positive. Otherwise, switch signs,
143     * or replace - with +). */
144    if (m_signexp >> 31)
145        return -(-*this + x);
146
147    if (x.m_signexp >> 31)
148        return (*this) + (-x);
149
150    /* Ensure *this is larger than x */
151    if (*this < x)
152        return -(x - *this);
153
154    real ret;
155
156    return ret;
157}
158
159real real::operator *(real const &x) const
160{
161    real ret;
162
163    ret.m_signexp = (m_signexp ^ x.m_signexp) & 0x80000000u;
164    int e = (m_signexp & 0x7fffffffu) - (1 << 30) + 1
165          + (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1;
166
167    /* Accumulate low order product; no need to store it, we just
168     * want the carry value */
169    uint32_t carry = 0;
170    for (int i = 0; i < BIGITS; i++)
171    {
172        for (int j = 0; j < i + 1; j++)
173            carry += m_mantissa[BIGITS - 1 - j]
174                   * x.m_mantissa[BIGITS - 1 + j - i];
175        carry >>= 16;
176    }
177
178    for (int i = 0; i < BIGITS; i++)
179    {
180        for (int j = i + 1; j < BIGITS; j++)
181            carry += m_mantissa[BIGITS - 1 - j]
182                   * x.m_mantissa[j - 1 - i];
183
184        carry += m_mantissa[BIGITS - 1 - i];
185        carry += x.m_mantissa[BIGITS - 1 - i];
186        ret.m_mantissa[BIGITS - 1 - i] = carry & 0xffffu;
187        carry >>= 16;
188    }
189
190    /* Renormalise in case we overflowed the mantissa */
191    if (carry)
192    {
193        carry--;
194        for (int i = 0; i < BIGITS; i++)
195        {
196            uint16_t tmp = ret.m_mantissa[i];
197            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
198            carry = tmp & 0x0001u;
199        }
200        e++;
201    }
202
203    ret.m_signexp |= e + (1 << 30) - 1;
204
205    return ret;
206}
207
208bool real::operator <(real const &x) const
209{
210    /* Ensure both numbers are positive */
211    if (m_signexp >> 31)
212        return (x.m_signexp >> 31) ? -*this > -x : true;
213
214    if (x.m_signexp >> 31)
215        return false;
216
217    /* Compare all relevant bits */
218    if (m_signexp != x.m_signexp)
219        return m_signexp < x.m_signexp;
220
221    for (int i = 0; i < BIGITS; i++)
222        if (m_mantissa[i] != x.m_mantissa[i])
223            return m_mantissa[i] < x.m_mantissa[i];
224
225    return false;
226}
227
228bool real::operator <=(real const &x) const
229{
230    return !(*this > x);
231}
232
233bool real::operator >(real const &x) const
234{
235    /* Ensure both numbers are positive */
236    if (m_signexp >> 31)
237        return (x.m_signexp >> 31) ? -*this < -x : false;
238
239    if (x.m_signexp >> 31)
240        return true;
241
242    /* Compare all relevant bits */
243    if (m_signexp != x.m_signexp)
244        return m_signexp > x.m_signexp;
245
246    for (int i = 0; i < BIGITS; i++)
247        if (m_mantissa[i] != x.m_mantissa[i])
248            return m_mantissa[i] > x.m_mantissa[i];
249
250    return false;
251}
252
253bool real::operator >=(real const &x) const
254{
255    return !(*this < x);
256}
257
258void real::print() const
259{
260    printf("%x  %08x  ", m_signexp >> 31, (m_signexp << 1) >> 1);
261    for (int i = 0; i < BIGITS; i++)
262        printf("%04x ", m_mantissa[i]);
263    printf("\n");
264}
265
266} /* namespace lol */
267
Note: See TracBrowser for help on using the repository browser.