source: trunk/src/half.h @ 867

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

core: start working on the "half" type.

  • Property svn:keywords set to Id
File size: 1.2 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//
12// The Half class
13// --------------
14//
15
16#if !defined __LOL_HALF_H__
17#define __LOL_HALF_H__
18
19#include <stdint.h>
20
21namespace lol
22{
23
24class half
25{
26private:
27    uint16_t m_bits;
28
29public:
30    inline half() { }
31
32    inline half(float f)
33    {
34        *this = makefast(f);
35    }
36
37    static half makeslow(float f);
38    static half makefast(float f);
39
40    static inline half makebits(uint16_t x)
41    {
42        half ret;
43        ret.m_bits = x;
44        return ret;
45    }
46
47    inline operator float() const
48    {
49        int s = m_bits & 0x8000u;
50        int e = m_bits & 0x7c00u;
51        int m = m_bits & 0x03ffu;
52
53        union { float f; uint32_t x; } u;
54        u.x = 0;
55        u.x |= s << 16;
56        u.x |= (-15 + (e >> 10) + 127) << 23;
57        u.x |= m << 13;
58
59        return u.f;
60    }
61
62    inline uint16_t bits()
63    {
64        return m_bits;
65    }
66};
67
68} /* namespace lol */
69
70#endif // __LOL_HALF_H__
71
Note: See TracBrowser for help on using the repository browser.