source: trunk/src/real.cpp @ 965

Last change on this file since 965 was 965, checked in by sam, 9 years ago

core: start working on a "real" class for arbitrarily sized floats.

File size: 1.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
17#include "core.h"
18
19using namespace std;
20
21namespace lol
22{
23
24template<> real4k::Real(float f)
25{
26    union { float f; uint32_t x; } u = { f };
27
28    uint32_t sign = u.x & 0x80000000u;
29    int e = ((u.x >> 23) & 0xff) + (1 << 30) - (1 << 10);
30
31    m_signexp = sign | e;
32    m_mantissa[0] = u.x << 17;
33    memset(m_mantissa + 1, 0, sizeof(m_mantissa) - sizeof(m_mantissa[0]));
34}
35
36template<> real4k::operator float() const
37{
38    union { float f; uint32_t x; } u;
39
40    u.x = m_mantissa[0] >> 17;
41    u.x |= ((m_signexp & 0x7fffffffu) - (1 << 30) + (1 << 10)) << 23;
42    u.x |= m_signexp & 0x80000000u;
43
44    return u.f;
45}
46
47} /* namespace lol */
48
Note: See TracBrowser for help on using the repository browser.