Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXhalf.h
1 /********************************************************************************
2 * *
3 * H a l f - F l o a t S u p p o r t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2008,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXHALF_H
22 #define FXHALF_H
23 
24 
25 // Some important numbers
26 #define HALF_MIN 5.9604644775391E-08 // Smallest half number
27 #define HALF_MAX 65504.0 // Largest half number
28 #define HALF_EPSILON 0.00097656 // Smallest number where (1+e) != 1
29 
30 namespace FX {
31 
32 
34 class FXAPI FXhalf {
35 private:
36  FXushort v;
37 private:
38  static const FXushort fhb[512];
39  static const FXuchar fhs[512];
40  static const FXuint hfm[2048];
41  static const FXuint hfe[64];
42  static const FXushort hfw[64];
43 public:
44  FXhalf(){}
45 
46  // Initialize half with half
47  FXhalf(const FXhalf& h):v(h.v){}
48 
49  // Initialize half with float
50  FXhalf(FXfloat f){ union { FXuint u; FXfloat f; } r; r.f=f; v=fhb[(r.u>>23)&0x1ff]+((r.u&0x007fffff)>>fhs[(r.u>>23)&0x1ff]); }
51 
52  // Convert half to float
53  operator FXfloat() const { union { FXuint u; FXfloat f; } r; r.u=hfm[hfw[v>>10]+(v&0x3ff)]+hfe[v>>10]; return r.f; }
54 
55  // Test for zero
56  FXbool operator!() const { return v==0; }
57 
58  // Unary
59  FXhalf operator+() const { return *this; }
60  FXhalf operator-() const { FXhalf h; h.v=v^0x8000; return h; }
61 
62  // Equality
63  FXbool operator==(FXhalf h) const { return v==h.v; }
64  FXbool operator!=(FXhalf h) const { return v!=h.v; }
65 
66  // Assignment
67  FXhalf& operator=(FXhalf h){ v=h.v; return *this; }
68  FXhalf& operator=(FXfloat f){ *this=FXhalf(f); return *this; }
69 
70  // Assignment operators
71  FXhalf& operator+=(FXhalf h){ *this=FXhalf(FXfloat(*this)+FXfloat(h)); return *this; }
72  FXhalf& operator+=(FXfloat f){ *this=FXhalf(FXfloat(*this)+f); return *this; }
73 
74  FXhalf& operator-=(FXhalf h){ *this=FXhalf(FXfloat(*this)-FXfloat(h)); return *this; }
75  FXhalf& operator-=(FXfloat f){ *this=FXhalf(FXfloat(*this)-f); return *this; }
76 
77  FXhalf& operator*=(FXhalf h){ *this=FXhalf(FXfloat(*this)*FXfloat(h)); return *this; }
78  FXhalf& operator*=(FXfloat f){ *this=FXhalf(FXfloat(*this)*f); return *this; }
79 
80  FXhalf& operator/=(FXhalf h){ *this=FXhalf(FXfloat(*this)/FXfloat(h)); return *this; }
81  FXhalf& operator/=(FXfloat f){ *this=FXhalf(FXfloat(*this)/f); return *this; }
82  };
83 
84 
85 }
86 
87 #endif

Copyright © 1997-2016 Jeroen van der Zijp