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 <cstdio> |
---|
20 | #include <stdint.h> |
---|
21 | |
---|
22 | namespace lol |
---|
23 | { |
---|
24 | |
---|
25 | class half |
---|
26 | { |
---|
27 | public: |
---|
28 | /* Constructors. Always inline so that the code can work in registers |
---|
29 | * instead of calling routines with the hidden "this" parameter. */ |
---|
30 | inline half() { } |
---|
31 | inline half(float f) { *this = makefast(f); } |
---|
32 | |
---|
33 | inline int is_nan() const |
---|
34 | { |
---|
35 | return ((bits & 0x7c00u) == 0x7c00u) && (bits & 0x03ffu); |
---|
36 | } |
---|
37 | |
---|
38 | inline int is_finite() const |
---|
39 | { |
---|
40 | return (bits & 0x7c00u) != 0x7c00u; |
---|
41 | } |
---|
42 | |
---|
43 | inline int is_inf() const |
---|
44 | { |
---|
45 | return (uint16_t)(bits << 1) == (0x7c00u << 1); |
---|
46 | } |
---|
47 | |
---|
48 | inline int is_normal() const |
---|
49 | { |
---|
50 | return (is_finite() && (bits & 0x7c00u)) || ((bits & 0x7fffu) == 0); |
---|
51 | } |
---|
52 | |
---|
53 | /* Cast to other types */ |
---|
54 | operator float() const; |
---|
55 | inline operator int() const { return (int)(float)*this; } |
---|
56 | |
---|
57 | /* Array conversions */ |
---|
58 | static size_t convert(half *dst, float const *src, size_t nelem); |
---|
59 | static size_t convert(float *dst, half const *src, size_t nelem); |
---|
60 | |
---|
61 | /* Operations */ |
---|
62 | inline half operator -() { return makebits(bits ^ 0x8000u); } |
---|
63 | inline half &operator +=(float f) { return (*this = (half)(*this + f)); } |
---|
64 | inline half &operator -=(float f) { return (*this = (half)(*this - f)); } |
---|
65 | inline half &operator *=(float f) { return (*this = (half)(*this * f)); } |
---|
66 | inline half &operator /=(float f) { return (*this = (half)(*this / f)); } |
---|
67 | inline half &operator +=(half h) { return (*this = (half)(*this + h)); } |
---|
68 | inline half &operator -=(half h) { return (*this = (half)(*this - h)); } |
---|
69 | inline half &operator *=(half h) { return (*this = (half)(*this * h)); } |
---|
70 | inline half &operator /=(half h) { return (*this = (half)(*this / h)); } |
---|
71 | |
---|
72 | inline float operator +(float f) const { return (float)*this + f; } |
---|
73 | inline float operator -(float f) const { return (float)*this - f; } |
---|
74 | inline float operator *(float f) const { return (float)*this * f; } |
---|
75 | inline float operator /(float f) const { return (float)*this / f; } |
---|
76 | inline float operator +(half h) const { return (float)*this + (float)h; } |
---|
77 | inline float operator -(half h) const { return (float)*this - (float)h; } |
---|
78 | inline float operator *(half h) const { return (float)*this * (float)h; } |
---|
79 | inline float operator /(half h) const { return (float)*this / (float)h; } |
---|
80 | |
---|
81 | /* Factories */ |
---|
82 | static half makefast(float f); |
---|
83 | static half makeaccurate(float f); |
---|
84 | static inline half makebits(uint16_t x) |
---|
85 | { |
---|
86 | half ret; |
---|
87 | ret.bits = x; |
---|
88 | return ret; |
---|
89 | } |
---|
90 | |
---|
91 | /* Internal representation */ |
---|
92 | uint16_t bits; |
---|
93 | }; |
---|
94 | |
---|
95 | inline float &operator +=(float &f, half h) { return f += (float)h; } |
---|
96 | inline float &operator -=(float &f, half h) { return f -= (float)h; } |
---|
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 | |
---|
100 | inline float operator +(float f, half h) { return f + (float)h; } |
---|
101 | inline float operator -(float f, half h) { return f - (float)h; } |
---|
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 | |
---|
105 | } /* namespace lol */ |
---|
106 | |
---|
107 | #endif // __LOL_HALF_H__ |
---|
108 | |
---|