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,2023 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){
51  union { FXfloat f; FXuint u; } r={f};
52  v=fhb[(r.u>>23)&0x1ff]+((r.u&0x007fffff)>>fhs[(r.u>>23)&0x1ff]);
53  }
54 
55  // Convert half to float
56  operator FXfloat() const {
57  union { FXuint u; FXfloat f; } r={hfm[hfw[v>>10]+(v&0x3ff)]+hfe[v>>10]};
58  return r.f;
59  }
60 
61  // Test for zero
62  FXbool operator!() const { return v==0; }
63 
64  // Unary
65  FXhalf operator+() const { return *this; }
66  FXhalf operator-() const { FXhalf h; h.v=v^0x8000; return h; }
67 
68  // Equality
69  FXbool operator==(FXhalf h) const { return v==h.v; }
70  FXbool operator!=(FXhalf h) const { return v!=h.v; }
71 
72  // Assignment
73  FXhalf& operator=(FXhalf h){ v=h.v; return *this; }
74  FXhalf& operator=(FXfloat f){ *this=FXhalf(f); return *this; }
75 
76  // Assignment operators
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  FXhalf& operator*=(FXhalf h){ *this=FXhalf(FXfloat(*this)*FXfloat(h)); return *this; }
84  FXhalf& operator*=(FXfloat f){ *this=FXhalf(FXfloat(*this)*f); return *this; }
85 
86  FXhalf& operator/=(FXhalf h){ *this=FXhalf(FXfloat(*this)/FXfloat(h)); return *this; }
87  FXhalf& operator/=(FXfloat f){ *this=FXhalf(FXfloat(*this)/f); return *this; }
88  };
89 
90 
91 }
92 
93 #endif
Definition: FX4Splitter.h:28
Half float (16-bit float)
Definition: FXhalf.h:34

Copyright © 1997-2022 Jeroen van der Zijp