GteIEEEBinary16.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.1 (2017/01/22)
7 
8 #include <GTEnginePCH.h>
11 
12 namespace gte
13 {
14 
16 {
17 }
18 
20  :
21  IEEEBinary<_Float16, uint16_t, 16, 11>()
22 {
23  // uninitialized
24 }
25 
27  :
28  IEEEBinary<_Float16, uint16_t, 16, 11>(object)
29 {
30 }
31 
33  :
34  IEEEBinary<_Float16, uint16_t, 16, 11>()
35 {
36  union { float n; uint32_t e; } temp = { number };
37  encoding = Convert32To16(temp.e);
38 }
39 
41  :
42  IEEEBinary<_Float16, uint16_t, 16, 11>()
43 {
44  union { float n; uint32_t e; } temp;
45  temp.n = (float)number;
46  encoding = Convert32To16(temp.e);
47 }
48 
50  :
51  IEEEBinary<_Float16, uint16_t, 16, 11>(encoding)
52 {
53 }
54 
55 IEEEBinary16::operator float() const
56 {
57  union { uint32_t e; float n; } temp = { Convert16To32(encoding) };
58  return temp.n;
59 }
60 
61 IEEEBinary16::operator double() const
62 {
63  union { uint32_t e; float n; } temp = { Convert16To32(encoding) };
64  return (double)temp.n;
65 }
66 
68 {
70  return *this;
71 }
72 
73 bool IEEEBinary16::operator==(IEEEBinary16 const& object) const
74 {
75  return (float)*this == (float)object;
76 }
77 
78 bool IEEEBinary16::operator!=(IEEEBinary16 const& object) const
79 {
80  return (float)*this != (float)object;
81 }
82 
83 bool IEEEBinary16::operator< (IEEEBinary16 const& object) const
84 {
85  return (float)*this < (float)object;
86 }
87 
88 bool IEEEBinary16::operator<=(IEEEBinary16 const& object) const
89 {
90  return (float)*this <= (float)object;
91 }
92 
93 bool IEEEBinary16::operator> (IEEEBinary16 const& object) const
94 {
95  return (float)*this > (float)object;
96 }
97 
98 bool IEEEBinary16::operator>=(IEEEBinary16 const& object) const
99 {
100  return (float)*this >= (float)object;
101 }
102 
104 {
105  // Extract the channels for the binary32 number.
106  uint32_t sign32 = (encoding & F32_SIGN_MASK);
107  uint32_t biased32 =
109  uint32_t trailing32 = (encoding & F32_TRAILING_MASK);
110  uint32_t nonneg32 = (encoding & F32_NOT_SIGN_MASK);
111 
112  // Generate the channels for the IEEEBinary16 number.
113  uint16_t sign16 = static_cast<uint16_t>(sign32 >> DIFF_NUM_ENCODING_BITS);
114  uint16_t biased16, trailing16;
115  uint32_t frcpart;
116 
117  if (biased32 == 0)
118  {
119  // nonneg32 is 32-zero or 32-subnormal, nearest is 16-zero.
120  return sign16;
121  }
122 
123  if (biased32 < F32_MAX_BIASED_EXPONENT)
124  {
125  // nonneg32 is 32-normal.
126  if (nonneg32 <= F16_AVR_MIN_SUBNORMAL_ZERO)
127  {
128  // nonneg32 <= 2^{-25}, nearest is 16-zero.
129  return sign16;
130  }
131 
132  if (nonneg32 <= F16_MIN_SUBNORMAL)
133  {
134  // 2^{-25} < nonneg32 <= 2^{-24}, nearest is 16-min-subnormal.
135  return sign16 | IEEEBinary16::MIN_SUBNORMAL;
136  }
137 
138  if (nonneg32 < F16_MIN_NORMAL)
139  {
140  // 2^{-24} < nonneg32 < 2^{-14}, round to nearest
141  // 16-subnormal with ties to even. Note that biased16 is zero.
142  trailing16 = static_cast<uint16_t>(((trailing32 & INT_PART_MASK) >> DIFF_NUM_TRAILING_BITS));
143  frcpart = (trailing32 & FRC_PART_MASK);
144  if (frcpart > FRC_HALF || (frcpart == FRC_HALF && (trailing16 & 1)))
145  {
146  // If there is a carry into the exponent, the nearest is
147  // actually 16-min-normal 1.0*2^{-14}, so the high-order bit
148  // of trailing16 makes biased16 equal to 1 and the result is
149  // correct.
150  ++trailing16;
151  }
152  return sign16 | trailing16;
153  }
154 
155  if (nonneg32 <= F16_MAX_NORMAL)
156  {
157  // 2^{-14} <= nonneg32 <= 1.1111111111*2^{15}, round to nearest
158  // 16-normal with ties to even.
159  biased16 = static_cast<uint16_t>((biased32 - F32_EXPONENT_BIAS +
162  trailing16 = static_cast<uint16_t>(((trailing32 & INT_PART_MASK) >> DIFF_NUM_TRAILING_BITS));
163  frcpart = (trailing32 & FRC_PART_MASK);
164  if (frcpart > FRC_HALF || (frcpart == FRC_HALF && (trailing16 & 1)))
165  {
166  // If there is a carry into the exponent, the addition of
167  // trailing16 to biased16 (rather than or-ing) produces the
168  // correct result.
169  ++trailing16;
170  }
171  return sign16 | (biased16 + trailing16);
172  }
173 
174  if (nonneg32 < F16_AVR_MAX_NORMAL_INFINITY)
175  {
176  // 1.1111111111*2^{15} < nonneg32 < (MAX_NORMAL+INFINITY)/2, so
177  // the number is closest to 16-max-normal.
178  return sign16 | IEEEBinary16::MAX_NORMAL;
179  }
180 
181  // nonneg32 >= (MAX_NORMAL+INFINITY)/2, so convert to 16-infinite.
182  return sign16 | IEEEBinary16::POS_INFINITY;
183  }
184 
185  if (trailing32 == 0)
186  {
187  // The number is 32-infinite. Convert to 16-infinite.
188  return sign16 | IEEEBinary16::POS_INFINITY;
189  }
190 
191  // The number is 32-NaN. Convert to 16-NaN with 16-payload the high-order
192  // 9 bits of the 32-payload. The code also grabs the 32-quietNaN mask
193  // bit.
194  uint16_t maskPayload = static_cast<uint16_t>(
195  (trailing32 & 0x007FE000u) >> 13);
196  return sign16 | IEEEBinary16::EXPONENT_MASK | maskPayload;
197 }
198 
200 {
201  // Extract the channels for the IEEEBinary16 number.
202  uint16_t sign16 = (encoding & IEEEBinary16::SIGN_MASK);
203  uint16_t biased16 = ((encoding & IEEEBinary16::EXPONENT_MASK)
205  uint16_t trailing16 = (encoding & IEEEBinary16::TRAILING_MASK);
206 
207  // Generate the channels for the binary32 number.
208  uint32_t sign32 = static_cast<uint32_t>(sign16 << DIFF_NUM_ENCODING_BITS);
209  uint32_t biased32, trailing32;
210 
211  if (biased16 == 0)
212  {
213  if (trailing16 == 0)
214  {
215  // The number is 16-zero. Convert to 32-zero.
216  return sign32;
217  }
218  else
219  {
220  // The number is 16-subnormal. Convert to 32-normal.
221  trailing32 = static_cast<uint32_t>(trailing16);
222  int32_t leading = GetLeadingBit(trailing32);
223  int32_t shift = 23 - leading;
224  biased32 = static_cast<uint32_t>(F32_EXPONENT_BIAS - 1 - shift);
225  trailing32 = (trailing32 << shift) & F32_TRAILING_MASK;
226  return sign32 | (biased32 << F32_NUM_TRAILING_BITS) | trailing32;
227  }
228  }
229 
230  if (biased16 < IEEEBinary16::MAX_BIASED_EXPONENT)
231  {
232  // The number is 16-normal. Convert to 32-normal.
233  biased32 = static_cast<uint32_t>(biased16 - IEEEBinary16::EXPONENT_BIAS +
235  trailing32 = (static_cast<uint32_t>(
236  trailing16) << DIFF_NUM_TRAILING_BITS);
237  return sign32 | (biased32 << F32_NUM_TRAILING_BITS) | trailing32;
238  }
239 
240  if (trailing16 == 0)
241  {
242  // The number is 16-infinite. Convert to 32-infinite.
243  return sign32 | F32_BIASED_EXPONENT_MASK;
244  }
245 
246  // The number is 16-NaN. Convert to 32-NaN with 16-payload embedded in
247  // the high-order 9 bits of the 32-payload. The code also copies the
248  // 16-quietNaN mask bit.
249  uint32_t maskPayload =
251  return sign32 | F32_BIASED_EXPONENT_MASK | maskPayload;
252 }
253 
255 {
256  uint16_t result = static_cast<uint16_t>(x) ^ IEEEBinary16::SIGN_MASK;
257  return result;
258 }
259 
261 {
262  return static_cast<float>(x)+static_cast<float>(y);
263 }
264 
266 {
267  return static_cast<float>(x)-static_cast<float>(y);
268 }
269 
271 {
272  return static_cast<float>(x)* static_cast<float>(y);
273 }
274 
276 {
277  return static_cast<float>(x) / static_cast<float>(y);
278 }
279 
280 float operator+ (IEEEBinary16 x, float y)
281 {
282  return static_cast<float>(x)+y;
283 }
284 
285 float operator- (IEEEBinary16 x, float y)
286 {
287  return static_cast<float>(x)-y;
288 }
289 
290 float operator* (IEEEBinary16 x, float y)
291 {
292  return static_cast<float>(x)* y;
293 }
294 
295 float operator/ (IEEEBinary16 x, float y)
296 {
297  return static_cast<float>(x) / y;
298 }
299 
300 float operator+ (float x, IEEEBinary16 y)
301 {
302  return x + static_cast<float>(y);
303 }
304 
305 float operator- (float x, IEEEBinary16 y)
306 {
307  return x - static_cast<float>(y);
308 }
309 
310 float operator* (float x, IEEEBinary16 y)
311 {
312  return x * static_cast<float>(y);
313 }
314 
315 float operator/ (float x, IEEEBinary16 y)
316 {
317  return x / static_cast<float>(y);
318 }
319 
321 {
322  x = static_cast<float>(x)+static_cast<float>(y);
323  return x;
324 }
325 
327 {
328  x = static_cast<float>(x)-static_cast<float>(y);
329  return x;
330 }
331 
333 {
334  x = static_cast<float>(x)* static_cast<float>(y);
335  return x;
336 }
337 
339 {
340  x = static_cast<float>(x) / static_cast<float>(y);
341  return x;
342 }
343 
345 {
346  x = static_cast<float>(x)+y;
347  return x;
348 }
349 
351 {
352  x = static_cast<float>(x)-y;
353  return x;
354 }
355 
357 {
358  x = static_cast<float>(x)* y;
359  return x;
360 }
361 
363 {
364  x = static_cast<float>(x) / y;
365  return x;
366 }
367 
368 }
GLdouble n
Definition: glcorearb.h:2003
bool operator>=(IEEEBinary16 const &object) const
DualQuaternion< Real > & operator*=(DualQuaternion< Real > &d, Real scalar)
GTE_IMPEXP int32_t GetLeadingBit(uint32_t value)
Definition: GteBitHacks.cpp:61
bool operator!=(IEEEBinary16 const &object) const
bool operator<(IEEEBinary16 const &object) const
GLint GLenum GLint x
Definition: glcorearb.h:404
IEEEBinary16 & operator=(IEEEBinary16 const &object)
bool operator<=(IEEEBinary16 const &object) const
bool operator==(IEEEBinary16 const &object) const
DualQuaternion< Real > operator+(DualQuaternion< Real > const &d)
static uint32_t Convert16To32(uint16_t encoding)
DualQuaternion< Real > & operator-=(DualQuaternion< Real > &d0, DualQuaternion< Real > const &d1)
DualQuaternion< Real > operator-(DualQuaternion< Real > const &d)
DualQuaternion< Real > & operator+=(DualQuaternion< Real > &d0, DualQuaternion< Real > const &d1)
IEEEBinary & operator=(IEEEBinary const &object)
static uint16_t Convert32To16(uint32_t encoding)
DualQuaternion< Real > & operator/=(DualQuaternion< Real > &d, Real scalar)
Vector4< float > operator*(Transform const &M, Vector4< float > const &V)
GLuint64EXT * result
Definition: glext.h:10003
GLint y
Definition: glcorearb.h:98
DualQuaternion< Real > operator/(DualQuaternion< Real > const &d, Real scalar)
bool operator>(IEEEBinary16 const &object) const


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00