HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
missing_math.h
Go to the documentation of this file.
1 /*
2  Copyright 2008-2014 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 /// \file
33 ///
34 /// This adds definitions we think *should* be in math.h, but which on
35 /// on some platforms appears to be missing in action.
36 ///
37 
38 
39 #pragma once
40 
41 #include <cmath>
42 
44 
45 
46 #ifndef M_PI
47 /// PI
48 # define M_PI 3.14159265358979323846264338327950288
49 #endif
50 
51 #ifndef M_PI_2
52 /// PI / 2
53 # define M_PI_2 1.57079632679489661923132169163975144
54 #endif
55 
56 #ifndef M_PI_4
57 /// PI / 4
58 # define M_PI_4 0.785398163397448309615660845819875721
59 #endif
60 
61 #ifndef M_TWO_PI
62 /// PI * 2
63 # define M_TWO_PI (M_PI * 2.0)
64 #endif
65 
66 #ifndef M_1_PI
67 /// 1/PI
68 # define M_1_PI 0.318309886183790671537767526745028724
69 #endif
70 
71 #ifndef M_2_PI
72 /// 2/PI
73 # define M_2_PI 0.636619772367581343075535053490057448
74 #endif
75 
76 #ifndef M_SQRT2
77 /// sqrt(2)
78 # define M_SQRT2 1.41421356237309504880168872420969808
79 #endif
80 
81 #ifndef M_SQRT1_2
82 /// 1/sqrt(2)
83 # define M_SQRT1_2 0.707106781186547524400844362104849039
84 #endif
85 
86 #ifndef M_LN2
87 /// ln(2)
88 # define M_LN2 0.69314718055994530941723212145817656
89 #endif
90 
91 #ifndef M_LN10
92 /// ln(10)
93 # define M_LN10 2.30258509299404568401799145468436421
94 #endif
95 
96 #ifndef M_E
97 /// e, Euler's number
98 # define M_E 2.71828182845904523536028747135266250
99 #endif
100 
101 #ifndef M_LOG2E
102 /// log2(e)
103 # define M_LOG2E 1.44269504088896340735992468100189214
104 #endif
105 
106 
108 
109 #ifdef _WIN32
110 // Windows doesn't define these functions from math.h
111 # define hypotf _hypotf
112 # define copysign(x, y) _copysign(x, y)
113 # define copysignf(x, y) float(copysign(x, y))
114 
115 
116 inline float
117 truncf(float val)
118 {
119  return (float)(int)val;
120 }
121 
122 
123 inline float
124 exp2f(float val)
125 {
126  // 2^val = e^(val*ln(2))
127  return (float)exp(val * M_LN2);
128 }
129 
130 
131 #endif /* _WIN32 */
132 
133 
134 #if OIIO_MSVS_AT_LEAST_2013 && __cplusplus <= 201103L
135 // Prior to c++11, these were implementation defined, and on msvc, were
136 // not in the std namespace.
137 using ::isfinite;
140 #else
141 using std::isfinite;
142 using std::isinf;
143 using std::isnan;
144 #endif
145 
146 
147 
148 // Functions missing from FreeBSD
149 #if (defined(__FreeBSD__) && (__FreeBSD_version < 803000))
150 
151 inline float
152 log2f(float val)
153 {
154  return logf(val) / static_cast<float>(M_LN2);
155 }
156 
157 #endif
158 
159 
GLuint const GLfloat * val
Definition: glew.h:2794
SYS_API float truncf(float x)
dummy_int isinf(...)
Definition: format.h:276
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
#define M_LN2
ln(2)
Definition: missing_math.h:88
SYS_API float logf(float x)
T exp(const T &v)
Definition: simd.h:7377
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
dummy_int isnan(...)
Definition: format.h:278
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65