1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2019, 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 
44 /**  @file  qnan.h
45  *   @brief Some utilities for our dealings with qnans.
46  *
47  *   @note Some loaders use qnans to mark invalid values tempoarily, also
48  *     Assimp explicitly enforces undefined normals to be set to qnan.
49  *     qnan utilities are available in standard libraries (C99 for example)
50  *     but last time I checked compiler coverage was so bad that I decided
51  *     to reinvent the wheel.
52  */
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 // ---------------------------------------------------------------------------
67 /** Data structure to represent the bit pattern of a 32 Bit
68  *         IEEE 754 floating-point number. */
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 // ---------------------------------------------------------------------------
80 /** Data structure to represent the bit pattern of a 64 Bit
81  *         IEEE 754 floating-point number. */
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 // ---------------------------------------------------------------------------
93 /** Check whether a given float is qNaN.
94  *  @param in Input value */
is_qnan(float in)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 // ---------------------------------------------------------------------------
110 /** Check whether a given double is qNaN.
111  *  @param in Input value */
is_qnan(double in)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 // ---------------------------------------------------------------------------
127 /** @brief check whether a float is either NaN or (+/-) INF.
128  *
129  *  Denorms return false, they're treated like normal values.
130  *  @param in Input value */
is_special_float(float in)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 // ---------------------------------------------------------------------------
138 /** @brief check whether a double is either NaN or (+/-) INF.
139  *
140  *  Denorms return false, they're treated like normal values.
141  *  @param in Input value */
is_special_float(double in)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 // ---------------------------------------------------------------------------
149 /** Check whether a float is NOT qNaN.
150  *  @param in Input value */
151 template<class TReal>
is_not_qnan(TReal in)152 AI_FORCE_INLINE bool is_not_qnan(TReal in) {
153     return !is_qnan(in);
154 }
155 
156 // ---------------------------------------------------------------------------
157 /** @brief Get a fresh qnan.  */
get_qnan()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
163