VTFLib
A C and C++ API that, with a few simple functions, can open and save .vtf and .vmt files.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Float16.h
1 /*
2  * VTFLib
3  * Copyright (C) 2005-2010 Neil Jedrzejewski & Ryan Gregg
4 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later
9  * version.
10  */
11 
12 // Reference: http://sourceforge.net/mailarchive/forum.php?thread_id=1823849&forum_id=10511
13 // Summary: Uses the first 16 bits of a 32 bit IEEE single to preform native 32 floating point
14 // operations. Last 16 bits of mantissa are cleared after every operation.
15 // Format: 16 Bit: seeeeeeeemmmmmmm
16 // 32 Bit: seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
17 
18 #ifndef VTFLIB_FLOAT16_H
19 #define VTFLIB_FLOAT16_H
20 
21 #define SFLOAT16_MSB 1 // Most Significant Byte (1 for bigendian)
22 #define SFLOAT16_LSB 0 // Least Significant Byte (0 for bigendian)
23 
24 #include "stdafx.h"
25 
26 struct SFloat16
27 {
28 private:
29  union UFloat16
30  {
31  vlUShort usUShort[2];
32  vlSingle sSingle;
33  } Float16;
34 
35 public:
36  inline SFloat16()
37  {
38 
39  }
40 
41  inline SFloat16(vlUShort usFloat)
42  {
43  this->Float16.usUShort[SFLOAT16_MSB] = usFloat;
44  this->Float16.usUShort[SFLOAT16_LSB] = 0;
45  }
46 
47  inline SFloat16(vlSingle sFloat)
48  {
49  this->Float16.sSingle = sFloat;
50  this->Float16.usUShort[SFLOAT16_LSB] = 0;
51  }
52 
53 public:
54  inline vlUShort GetUShort() const
55  {
56  return this->Float16.usUShort[SFLOAT16_MSB];
57  }
58 
59  inline vlSingle GetSingle() const
60  {
61  return this->Float16.sSingle;
62  }
63 
64 public:
65  inline SFloat16 &operator=(vlUShort usFloat)
66  {
67  this->Float16.usUShort[SFLOAT16_MSB] = usFloat;
68  this->Float16.usUShort[SFLOAT16_LSB] = 0;
69 
70  return *this;
71  }
72 
73  inline SFloat16 &operator=(vlSingle sFloat)
74  {
75  this->Float16.sSingle = sFloat;
76  this->Float16.usUShort[SFLOAT16_LSB] = 0;
77 
78  return *this;
79  }
80 
81  inline SFloat16 &operator=(const SFloat16 &Float16)
82  {
83  this->Float16 = Float16.Float16;
84 
85  return *this;
86  }
87 
88  inline vlBool operator==(const SFloat16 &Float16) const
89  {
90  return this->Float16.sSingle == Float16.Float16.sSingle;
91  }
92 
93  inline vlBool operator!=(const SFloat16 &Float16) const
94  {
95  return this->Float16.sSingle != Float16.Float16.sSingle;
96  }
97 
98  inline vlBool operator<(const SFloat16 &Float16) const
99  {
100  return this->Float16.sSingle < Float16.Float16.sSingle;
101  }
102 
103  inline vlBool operator<=(const SFloat16 &Float16) const
104  {
105  return this->Float16.sSingle <= Float16.Float16.sSingle;
106  }
107 
108  inline vlBool operator>(const SFloat16 &Float16) const
109  {
110  return this->Float16.sSingle > Float16.Float16.sSingle;
111  }
112 
113  inline vlBool operator>=(const SFloat16 &Float16) const
114  {
115  return this->Float16.sSingle >= Float16.Float16.sSingle;
116  }
117 
118  inline SFloat16 operator+() const
119  {
120  return SFloat16(+this->Float16.sSingle);
121  }
122 
123  inline SFloat16 operator+(const SFloat16 &Float16) const
124  {
125  return SFloat16(this->Float16.sSingle + Float16.Float16.sSingle);
126  }
127 
128  inline SFloat16 &operator+=(const SFloat16 &Float16)
129  {
130  this->Float16.sSingle += Float16.Float16.sSingle;
131  this->Float16.usUShort[SFLOAT16_LSB] = 0;
132 
133  return *this;
134  }
135 
136  inline SFloat16 operator-() const
137  {
138  return SFloat16(-this->Float16.sSingle);
139  }
140 
141  inline SFloat16 operator-(const SFloat16 &Float16) const
142  {
143  return SFloat16(this->Float16.sSingle - Float16.Float16.sSingle);
144  }
145 
146  inline SFloat16 &operator-=(const SFloat16 &Float16)
147  {
148  this->Float16.sSingle += Float16.Float16.sSingle;
149  this->Float16.usUShort[SFLOAT16_LSB] = 0;
150 
151  return *this;
152  }
153 
154  inline SFloat16 operator*(const SFloat16 &Float16) const
155  {
156  return SFloat16(this->Float16.sSingle - Float16.Float16.sSingle);
157  }
158 
159  inline SFloat16 &operator*=(const SFloat16 &Float16)
160  {
161  this->Float16.sSingle *= Float16.Float16.sSingle;
162  this->Float16.usUShort[SFLOAT16_LSB] = 0;
163 
164  return *this;
165  }
166 
167  inline SFloat16 operator/(const SFloat16 &Float16) const
168  {
169  return SFloat16(this->Float16.sSingle - Float16.Float16.sSingle);
170  }
171 
172  inline SFloat16 &operator/=(const SFloat16 &Float16)
173  {
174  this->Float16.sSingle /= Float16.Float16.sSingle;
175  this->Float16.usUShort[SFLOAT16_LSB] = 0;
176 
177  return *this;
178  }
179 };
180 
181 #endif
Application framework header plus VTFLib custom data types.
unsigned short vlUShort
Unsigned short floating point value.
Definition: stdafx.h:54
unsigned char vlBool
Boolean value 0/1.
Definition: stdafx.h:50
float vlSingle
Floating point number.
Definition: stdafx.h:59