source: trunk/src/real.cpp @ 969

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

core: switch real mantissa to uint16_t instead of uint32_t to ease the
multiplication.

File size: 2.5 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    int e = ((u.x >> 23) & 0xff) + (1 << 30) - (1 << 7);
31
32    m_signexp = sign | e;
33    m_mantissa[0] = u.x >> 7;
34    m_mantissa[1] = u.x << 9;
35    memset(m_mantissa + 2, 0, sizeof(m_mantissa) - sizeof(m_mantissa[0]));
36}
37
38real::operator float() const
39{
40    union { float f; uint32_t x; } u;
41
42    u.x = m_mantissa[0] << 7;
43    u.x |= m_mantissa[1] >> 9;
44    u.x |= ((m_signexp & 0x7fffffffu) - (1 << 30) + (1 << 7)) << 23;
45    u.x |= m_signexp & 0x80000000u;
46
47    return u.f;
48}
49
50real real::operator *(real const &x) const
51{
52    real ret;
53
54    ret.m_signexp = (m_signexp ^ x.m_signexp) & 0x80000000u;
55    int e = (m_signexp & 0x7fffffffu) - (1 << 30) + 1
56          + (x.m_signexp & 0x7fffffffu) - (1 << 30) + 1;
57
58    /* Accumulate low order product; no need to store it, we just
59     * want the carry value */
60    uint32_t carry = 0;
61    for (int i = 0; i < BIGITS; i++)
62    {
63        for (int j = 0; j < i + 1; j++)
64            carry += m_mantissa[BIGITS - 1 - j]
65                   * x.m_mantissa[BIGITS - 1 + j - i];
66        carry >>= 16;
67    }
68
69    for (int i = 0; i < BIGITS; i++)
70    {
71        for (int j = i + 1; j < BIGITS; j++)
72            carry += m_mantissa[BIGITS - 1 - j]
73                   * x.m_mantissa[j - 1 - i];
74
75        carry += m_mantissa[BIGITS - 1 - i];
76        carry += x.m_mantissa[BIGITS - 1 - i];
77        ret.m_mantissa[BIGITS - 1 - i] = carry & 0xffffu;
78        carry >>= 16;
79    }
80
81    /* Renormalise in case we overflowed the mantissa */
82    if (carry)
83    {
84        carry--;
85        for (int i = 0; i < BIGITS; i++)
86        {
87            uint16_t tmp = ret.m_mantissa[i];
88            ret.m_mantissa[i] = (carry << 15) | (tmp >> 1);
89            carry = tmp & 0x0001u;
90        }
91        e++;
92    }
93
94    ret.m_signexp |= e + (1 << 30) - 1;
95
96    return ret;
97}
98
99void real::print() const
100{
101    printf("%x  %08x  ", m_signexp >> 31, (m_signexp << 1) >> 1);
102    for (int i = 0; i < BIGITS; i++)
103        printf("%04x ", m_mantissa[i]);
104    printf("\n");
105}
106
107} /* namespace lol */
108
Note: See TracBrowser for help on using the repository browser.