Path Tracer
qnan.h
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2020, assimp team
7 
8 
9 
10 All rights reserved.
11 
12 Redistribution and use of this software in source and binary forms,
13 with or without modification, are permitted provided that the following
14 conditions are met:
15 
16 * Redistributions of source code must retain the above
17  copyright notice, this list of conditions and the
18  following disclaimer.
19 
20 * Redistributions in binary form must reproduce the above
21  copyright notice, this list of conditions and the
22  following disclaimer in the documentation and/or other
23  materials provided with the distribution.
24 
25 * Neither the name of the assimp team, nor the names of its
26  contributors may be used to endorse or promote products
27  derived from this software without specific prior
28  written permission of the assimp team.
29 
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ---------------------------------------------------------------------------
42 */
43 
53 #pragma once
54 #ifndef AI_QNAN_H_INCLUDED
55 #define AI_QNAN_H_INCLUDED
56 
57 #ifdef __GNUC__
58 # pragma GCC system_header
59 #endif
60 
61 #include <assimp/defs.h>
62 
63 #include <limits>
64 #include <stdint.h>
65 
66 // ---------------------------------------------------------------------------
69 union _IEEESingle {
70  float Float;
71  struct
72  {
73  uint32_t Frac : 23;
74  uint32_t Exp : 8;
75  uint32_t Sign : 1;
76  } IEEE;
77 };
78 
79 // ---------------------------------------------------------------------------
82 union _IEEEDouble {
83  double Double;
84  struct
85  {
86  uint64_t Frac : 52;
87  uint64_t Exp : 11;
88  uint64_t Sign : 1;
89  } IEEE;
90 };
91 
92 // ---------------------------------------------------------------------------
95 AI_FORCE_INLINE bool is_qnan(float in) {
96  // the straightforward solution does not work:
97  // return (in != in);
98  // compiler generates code like this
99  // load <in> to <register-with-different-width>
100  // compare <register-with-different-width> against <in>
101 
102  // FIXME: Use <float> stuff instead? I think fpclassify needs C99
103  _IEEESingle temp;
104  memcpy(&temp, &in, sizeof(float));
105  return (temp.IEEE.Exp == (1u << 8)-1 &&
106  temp.IEEE.Frac);
107 }
108 
109 // ---------------------------------------------------------------------------
112 AI_FORCE_INLINE bool is_qnan(double in) {
113  // the straightforward solution does not work:
114  // return (in != in);
115  // compiler generates code like this
116  // load <in> to <register-with-different-width>
117  // compare <register-with-different-width> against <in>
118 
119  // FIXME: Use <float> stuff instead? I think fpclassify needs C99
120  _IEEEDouble temp;
121  memcpy(&temp, &in, sizeof(in));
122  return (temp.IEEE.Exp == (1u << 11)-1 &&
123  temp.IEEE.Frac);
124 }
125 
126 // ---------------------------------------------------------------------------
131 AI_FORCE_INLINE bool is_special_float(float in) {
132  _IEEESingle temp;
133  memcpy(&temp, &in, sizeof(float));
134  return (temp.IEEE.Exp == (1u << 8)-1);
135 }
136 
137 // ---------------------------------------------------------------------------
142 AI_FORCE_INLINE bool is_special_float(double in) {
143  _IEEESingle temp;
144  memcpy(&temp, &in, sizeof(float));
145  return (temp.IEEE.Exp == (1u << 11)-1);
146 }
147 
148 // ---------------------------------------------------------------------------
151 template<class TReal>
152 AI_FORCE_INLINE bool is_not_qnan(TReal in) {
153  return !is_qnan(in);
154 }
155 
156 // ---------------------------------------------------------------------------
158 AI_FORCE_INLINE ai_real get_qnan() {
159  return std::numeric_limits<ai_real>::quiet_NaN();
160 }
161 
162 #endif // !! AI_QNAN_H_INCLUDED
_IEEESingle
Definition: qnan.h:69
is_not_qnan
AI_FORCE_INLINE bool is_not_qnan(TReal in)
Definition: qnan.h:152
is_qnan
AI_FORCE_INLINE bool is_qnan(float in)
Definition: qnan.h:95
get_qnan
AI_FORCE_INLINE ai_real get_qnan()
Get a fresh qnan.
Definition: qnan.h:158
_IEEEDouble
Definition: qnan.h:82
defs.h
Assimp build configuration setup. See the notes in the comment blocks to find out how to customize yo...
is_special_float
AI_FORCE_INLINE bool is_special_float(float in)
check whether a float is either NaN or (+/-) INF.
Definition: qnan.h:131