HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
math.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_ARCH_MATH_H
25 #define PXR_BASE_ARCH_MATH_H
26 
27 /// \file arch/math.h
28 /// \ingroup group_arch_Math
29 /// Architecture-specific math function calls.
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/defines.h"
33 #include "pxr/base/arch/inttypes.h"
34 
35 #if defined(ARCH_COMPILER_MSVC)
36 #include <intrin.h>
37 #endif
38 
39 #include <cmath>
40 #if !defined(M_PI)
41 #define M_PI 3.14159265358979323846
42 #endif
43 
45 
46 /// \addtogroup group_arch_Math
47 ///@{
48 
49 #if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || defined (doxygen)
50 
51 /// This is the smallest value e such that 1+e^2 == 1, using floats.
52 /// True for all IEEE754 chipsets.
53 #define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
54 
55 /// Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
56 inline long ArchSign(long val) {
57  return (val > 0) - (val < 0);
58 }
59 
60 /// Returns The IEEE-754 bit pattern of the specified single precision value
61 /// as a 32-bit unsigned integer.
62 inline uint32_t ArchFloatToBitPattern(float v) {
63  union {
64  float _float;
65  uint32_t _uint;
66  } value;
67  value._float = v;
68  return value._uint;
69 }
70 
71 /// Returns The single precision floating point value corresponding to the
72 /// given IEEE-754 bit pattern.
73 inline float ArchBitPatternToFloat(uint32_t v) {
74  union {
75  uint32_t _uint;
76  float _float;
77  } value;
78  value._uint = v;
79  return value._float;
80 }
81 
82 /// Returns The IEEE-754 bit pattern of the specified double precision value
83 /// as a 64-bit unsigned integer.
84 inline uint64_t ArchDoubleToBitPattern(double v) {
85  union {
86  double _double;
87  uint64_t _uint;
88  } value;
89  value._double = v;
90  return value._uint;
91 }
92 
93 /// Returns The double precision floating point value corresponding to the
94 /// given IEEE-754 bit pattern.
95 inline double ArchBitPatternToDouble(uint64_t v) {
96  union {
97  uint64_t _uint;
98  double _double;
99  } value;
100  value._uint = v;
101  return value._double;
102 }
103 
104 #else
105 #error Unknown system architecture.
106 #endif
107 
108 #if defined(ARCH_OS_LINUX) || defined(doxygen)
109 
110 /// Computes the sine and cosine of the specified value as a float.
111 inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
112 
113 /// Computes the sine and cosine of the specified value as a double.
114 inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
115 
116 #elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS)
117 
118 inline void ArchSinCosf(float v, float *s, float *c) {
119  *s = std::sin(v);
120  *c = std::cos(v);
121 }
122 inline void ArchSinCos(double v, double *s, double *c) {
123  *s = std::sin(v);
124  *c = std::cos(v);
125 }
126 
127 #else
128 #error Unknown architecture.
129 #endif
130 
131 
132 /// Return the number of consecutive 0-bits in \p x starting from the least
133 /// significant bit position. If \p x is 0, the result is undefined.
134 inline int
136 {
137 #if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
138  return __builtin_ctzl(x);
139 #elif defined(ARCH_COMPILER_MSVC)
140  unsigned long index;
141  _BitScanForward64(&index, x);
142  return index;
143 #else
144  // Flip trailing zeros to 1s, and clear all other bits, then count.
145  x = (x ^ (x - 1)) >> 1;
146  int c = 0;
147  for (; x; ++c) {
148  x >>= 1;
149  }
150  return c;
151 #endif
152 }
153 
154 
155 ///@}
156 
158 
159 #endif // PXR_BASE_ARCH_MATH_H
SYS_API double cos(double x)
Definition: SYS_FPUMath.h:69
int ArchCountTrailingZeros(uint64_t x)
Definition: math.h:135
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
OIIO_HOSTDEVICE void sincos(float x, float *sine, float *cosine)
Definition: fmath.h:703
GLdouble s
Definition: glad.h:3009
SYS_API void sincosf(float x, float *s, float *c)
GLint GLenum GLint x
Definition: glcorearb.h:409
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:1131
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71