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 
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 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
48 
49 #if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || defined (doxygen)
50 
53 #define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
54 
56 inline long ArchSign(long val) {
57  return (val > 0) - (val < 0);
58 }
59 
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 
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 
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 
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 
111 inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
112 
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 
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 
156 
157 PXR_NAMESPACE_CLOSE_SCOPE
158 
159 #endif // PXR_BASE_ARCH_MATH_H
double ArchBitPatternToDouble(uint64_t v)
Returns The double precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition: math.h:95
int ArchCountTrailingZeros(uint64_t x)
Return the number of consecutive 0-bits in x starting from the least significant bit position.
Definition: math.h:135
float ArchBitPatternToFloat(uint32_t v)
Returns The single precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition: math.h:73
uint64_t ArchDoubleToBitPattern(double v)
Returns The IEEE-754 bit pattern of the specified double precision value as a 64-bit unsigned integer...
Definition: math.h:84
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition: math.h:111
long ArchSign(long val)
Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
Definition: math.h:56
Define integral types.
uint32_t ArchFloatToBitPattern(float v)
Returns The IEEE-754 bit pattern of the specified single precision value as a 32-bit unsigned integer...
Definition: math.h:62
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition: math.h:114