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 | |
---|
21 | namespace lol |
---|
22 | { |
---|
23 | |
---|
24 | class half |
---|
25 | { |
---|
26 | private: |
---|
27 | uint16_t m_bits; |
---|
28 | |
---|
29 | public: |
---|
30 | inline half() { } |
---|
31 | |
---|
32 | inline half(float f) |
---|
33 | { |
---|
34 | *this = makefast(f); |
---|
35 | } |
---|
36 | |
---|
37 | inline int isnan() const |
---|
38 | { |
---|
39 | return ((m_bits & 0x7c00u) == 0x7c00u) && (m_bits & 0x03ffu); |
---|
40 | } |
---|
41 | |
---|
42 | inline int isfinite() const |
---|
43 | { |
---|
44 | return (m_bits & 0x7c00u) != 0x7c00u; |
---|
45 | } |
---|
46 | |
---|
47 | inline int isinf() const |
---|
48 | { |
---|
49 | return (uint16_t)(m_bits << 1) == (0x7c00u << 1); |
---|
50 | } |
---|
51 | |
---|
52 | inline int isnormal() const |
---|
53 | { |
---|
54 | return (isfinite() && (m_bits & 0x7c00u)) || ((m_bits & 0x7fffu) == 0); |
---|
55 | } |
---|
56 | |
---|
57 | inline uint16_t bits() |
---|
58 | { |
---|
59 | return m_bits; |
---|
60 | } |
---|
61 | |
---|
62 | /* Cast to other types */ |
---|
63 | operator float() const; |
---|
64 | inline operator int() const { return (int)(float)*this; } |
---|
65 | |
---|
66 | /* Operations */ |
---|
67 | inline half operator -() { return makebits(m_bits ^ 0x8000u); } |
---|
68 | inline half &operator +=(float f) { return (*this = (half)(*this + f)); } |
---|
69 | inline half &operator -=(float f) { return (*this = (half)(*this - f)); } |
---|
70 | inline half &operator *=(float f) { return (*this = (half)(*this * f)); } |
---|
71 | inline half &operator /=(float f) { return (*this = (half)(*this / f)); } |
---|
72 | inline half &operator +=(half h) { return (*this = (half)(*this + h)); } |
---|
73 | inline half &operator -=(half h) { return (*this = (half)(*this - h)); } |
---|
74 | inline half &operator *=(half h) { return (*this = (half)(*this * h)); } |
---|
75 | inline half &operator /=(half h) { return (*this = (half)(*this / h)); } |
---|
76 | |
---|
77 | inline float operator +(float f) const { return (float)*this + f; } |
---|
78 | inline float operator -(float f) const { return (float)*this - f; } |
---|
79 | inline float operator *(float f) const { return (float)*this * f; } |
---|
80 | inline float operator /(float f) const { return (float)*this / f; } |
---|
81 | inline float operator +(half h) const { return (float)*this + (float)h; } |
---|
82 | inline float operator -(half h) const { return (float)*this - (float)h; } |
---|
83 | inline float operator *(half h) const { return (float)*this * (float)h; } |
---|
84 | inline float operator /(half h) const { return (float)*this / (float)h; } |
---|
85 | |
---|
86 | /* Factories */ |
---|
87 | static half makeslow(float f); |
---|
88 | static half makefast(float f); |
---|
89 | static inline half makebits(uint16_t x) |
---|
90 | { |
---|
91 | half ret; |
---|
92 | ret.m_bits = x; |
---|
93 | return ret; |
---|
94 | } |
---|
95 | }; |
---|
96 | |
---|
97 | inline float &operator +=(float &f, half h) { return f += (float)h; } |
---|
98 | inline float &operator -=(float &f, half h) { return f -= (float)h; } |
---|
99 | inline float &operator *=(float &f, half h) { return f *= (float)h; } |
---|
100 | inline float &operator /=(float &f, half h) { return f /= (float)h; } |
---|
101 | |
---|
102 | inline float operator +(float f, half h) { return f + (float)h; } |
---|
103 | inline float operator -(float f, half h) { return f - (float)h; } |
---|
104 | inline float operator *(float f, half h) { return f * (float)h; } |
---|
105 | inline float operator /(float f, half h) { return f / (float)h; } |
---|
106 | |
---|
107 | } /* namespace lol */ |
---|
108 | |
---|
109 | #endif // __LOL_HALF_H__ |
---|
110 | |
---|