source: trunk/src/half.h @ 872

Last change on this file since 872 was 872, checked in by sam, 10 years ago

core: minor refactoring in the float / half conversions to accomodate
for future array versions.

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