source: trunk/src/lol/math/half.h @ 1161

Last change on this file since 1161 was 1161, checked in by gary, 9 years ago

math: fix minor warnings in half.h and vector.h.

  • Property svn:keywords set to Id
File size: 4.5 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 <cstdio>
20#include <stdint.h>
21
22namespace lol
23{
24
25class half
26{
27public:
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(int f) { *this = makefast((float)f); }
32    inline half(float f) { *this = makefast(f); }
33    inline half(double f) { *this = makefast((float)f); }
34
35    inline int is_nan() const
36    {
37        return ((bits & 0x7c00u) == 0x7c00u) && (bits & 0x03ffu);
38    }
39
40    inline int is_finite() const
41    {
42        return (bits & 0x7c00u) != 0x7c00u;
43    }
44
45    inline int is_inf() const
46    {
47        return (uint16_t)(bits << 1) == (0x7c00u << 1);
48    }
49
50    inline int is_normal() const
51    {
52        return (is_finite() && (bits & 0x7c00u)) || ((bits & 0x7fffu) == 0);
53    }
54
55    /* Cast to other types -- always inline, see constructors */
56    inline half &operator =(int f) { return *this = makefast((float)f); }
57    inline half &operator =(float f) { return *this = makefast(f); }
58    inline half &operator =(double f) { return *this = makefast((float)f); }
59    inline operator int() const { return (int)tofloat(*this); }
60    inline operator float() const { return tofloat(*this); }
61
62    static float tofloat(half h);
63
64    /* Array conversions */
65    static size_t convert(half *dst, float const *src, size_t nelem);
66    static size_t convert(float *dst, half const *src, size_t nelem);
67
68    /* Operations */
69    bool operator ==(half x) const { return (float)*this == (float)x; }
70    bool operator !=(half x) const { return (float)*this != (float)x; }
71    bool operator <(half x) const { return (float)*this < (float)x; }
72    bool operator >(half x) const { return (float)*this > (float)x; }
73    bool operator <=(half x) const { return (float)*this <= (float)x; }
74    bool operator >=(half x) const { return (float)*this >= (float)x; }
75
76    bool operator !() const { return !(bits & 0x7fffu); }
77    operator bool() const { return !!*this; }
78
79    inline half operator -() const { return makebits(bits ^ 0x8000u); }
80    inline half operator +() const { return *this; }
81    inline half &operator +=(float f) { return (*this = (half)(*this + f)); }
82    inline half &operator -=(float f) { return (*this = (half)(*this - f)); }
83    inline half &operator *=(float f) { return (*this = (half)(*this * f)); }
84    inline half &operator /=(float f) { return (*this = (half)(*this / f)); }
85    inline half &operator +=(half h) { return (*this = (half)(*this + h)); }
86    inline half &operator -=(half h) { return (*this = (half)(*this - h)); }
87    inline half &operator *=(half h) { return (*this = (half)(*this * h)); }
88    inline half &operator /=(half h) { return (*this = (half)(*this / h)); }
89
90    inline float operator +(float f) const { return (float)*this + f; }
91    inline float operator -(float f) const { return (float)*this - f; }
92    inline float operator *(float f) const { return (float)*this * f; }
93    inline float operator /(float f) const { return (float)*this / f; }
94    inline float operator +(half h) const { return (float)*this + (float)h; }
95    inline float operator -(half h) const { return (float)*this - (float)h; }
96    inline float operator *(half h) const { return (float)*this * (float)h; }
97    inline float operator /(half h) const { return (float)*this / (float)h; }
98
99    /* Factories */
100    static half makefast(float f);
101    static half makeaccurate(float f);
102    static inline half makebits(uint16_t x)
103    {
104        half ret;
105        ret.bits = x;
106        return ret;
107    }
108
109    /* Internal representation */
110    uint16_t bits;
111};
112
113inline float &operator +=(float &f, half h) { return f += (float)h; }
114inline float &operator -=(float &f, half h) { return f -= (float)h; }
115inline float &operator *=(float &f, half h) { return f *= (float)h; }
116inline float &operator /=(float &f, half h) { return f /= (float)h; }
117
118inline float operator +(float f, half h) { return f + (float)h; }
119inline float operator -(float f, half h) { return f - (float)h; }
120inline float operator *(float f, half h) { return f * (float)h; }
121inline float operator /(float f, half h) { return f / (float)h; }
122
123} /* namespace lol */
124
125#endif // __LOL_HALF_H__
126
Note: See TracBrowser for help on using the repository browser.