vrshoot
view libs/assimp/qnan.h @ 1:e7ca128b8713
looks nice :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 00:35:22 +0200 |
parents | |
children |
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 /** @file qnan.h
43 * @brief Some utilities for our dealings with qnans.
44 *
45 * @note Some loaders use qnans to mark invalid values tempoarily, also
46 * Assimp explicitly enforces undefined normals to be set to qnan.
47 * qnan utilities are available in standard libraries (C99 for example)
48 * but last time I checked compiler coverage was so bad that I decided
49 * to reinvent the wheel.
50 */
52 #ifndef AI_QNAN_H_INCLUDED
53 #define AI_QNAN_H_INCLUDED
55 // ---------------------------------------------------------------------------
56 /** Data structure to represent the bit pattern of a 32 Bit
57 * IEEE 754 floating-point number. */
58 union _IEEESingle
59 {
60 float Float;
61 struct
62 {
63 uint32_t Frac : 23;
64 uint32_t Exp : 8;
65 uint32_t Sign : 1;
66 } IEEE;
67 } ;
69 // ---------------------------------------------------------------------------
70 /** Check whether a given float is qNaN.
71 * @param in Input value */
72 AI_FORCE_INLINE bool is_qnan(float in)
73 {
74 // the straightforward solution does not work:
75 // return (in != in);
76 // compiler generates code like this
77 // load <in> to <register-with-different-width>
78 // compare <register-with-different-width> against <in>
80 // FIXME: Use <float> stuff instead? I think fpclassify needs C99
81 return (reinterpret_cast<_IEEESingle*>(&in)->IEEE.Exp == (1u << 8)-1 &&
82 reinterpret_cast<_IEEESingle*>(&in)->IEEE.Frac);
83 }
85 // ---------------------------------------------------------------------------
86 /** Check whether a float is NOT qNaN.
87 * @param in Input value */
88 AI_FORCE_INLINE bool is_not_qnan(float in)
89 {
90 return !is_qnan(in);
91 }
93 // ---------------------------------------------------------------------------
94 /** @brief check whether a float is either NaN or (+/-) INF.
95 *
96 * Denorms return false, they're treated like normal values.
97 * @param in Input value */
98 AI_FORCE_INLINE bool is_special_float(float in)
99 {
100 return (reinterpret_cast<_IEEESingle*>(&in)->IEEE.Exp == (1u << 8)-1);
101 }
103 // ---------------------------------------------------------------------------
104 /** @brief Get a fresh qnan. */
105 AI_FORCE_INLINE float get_qnan()
106 {
107 return std::numeric_limits<float>::quiet_NaN();
108 }
110 #endif // !! AI_QNAN_H_INCLUDED