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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_ARCH_MATH_H
8 #define PXR_BASE_ARCH_MATH_H
9 
10 /// \file arch/math.h
11 /// \ingroup group_arch_Math
12 /// Architecture-specific math function calls.
13 
14 #include "pxr/pxr.h"
15 #include "pxr/base/arch/defines.h"
16 #include "pxr/base/arch/inttypes.h"
17 
18 #if defined(ARCH_COMPILER_MSVC)
19 #include <intrin.h>
20 #endif
21 
22 #include <cmath>
23 #if !defined(M_PI)
24 #define M_PI 3.14159265358979323846
25 #endif
26 
28 
29 /// \addtogroup group_arch_Math
30 ///@{
31 
32 #if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || \
33  defined(ARCH_OS_WASM_VM) || defined (doxygen)
34 
35 /// This is the smallest value e such that 1+e^2 == 1, using floats.
36 /// True for all IEEE754 chipsets.
37 #define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
38 
39 /// Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
40 inline long ArchSign(long val) {
41  return (val > 0) - (val < 0);
42 }
43 
44 /// Returns The IEEE-754 bit pattern of the specified single precision value
45 /// as a 32-bit unsigned integer.
46 inline uint32_t ArchFloatToBitPattern(float v) {
47  union {
48  float _float;
49  uint32_t _uint;
50  } value;
51  value._float = v;
52  return value._uint;
53 }
54 
55 /// Returns The single precision floating point value corresponding to the
56 /// given IEEE-754 bit pattern.
57 inline float ArchBitPatternToFloat(uint32_t v) {
58  union {
59  uint32_t _uint;
60  float _float;
61  } value;
62  value._uint = v;
63  return value._float;
64 }
65 
66 /// Returns The IEEE-754 bit pattern of the specified double precision value
67 /// as a 64-bit unsigned integer.
68 inline uint64_t ArchDoubleToBitPattern(double v) {
69  union {
70  double _double;
71  uint64_t _uint;
72  } value;
73  value._double = v;
74  return value._uint;
75 }
76 
77 /// Returns The double precision floating point value corresponding to the
78 /// given IEEE-754 bit pattern.
79 inline double ArchBitPatternToDouble(uint64_t v) {
80  union {
81  uint64_t _uint;
82  double _double;
83  } value;
84  value._uint = v;
85  return value._double;
86 }
87 
88 #else
89 #error Unknown system architecture.
90 #endif
91 
92 #if defined(ARCH_OS_LINUX) || defined(doxygen)
93 
94 /// Computes the sine and cosine of the specified value as a float.
95 inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
96 
97 /// Computes the sine and cosine of the specified value as a double.
98 inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
99 
100 #elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS) || \
101  defined(ARCH_OS_WASM_VM)
102 
103 inline void ArchSinCosf(float v, float *s, float *c) {
104  *s = std::sin(v);
105  *c = std::cos(v);
106 }
107 inline void ArchSinCos(double v, double *s, double *c) {
108  *s = std::sin(v);
109  *c = std::cos(v);
110 }
111 
112 #else
113 #error Unknown architecture.
114 #endif
115 
116 
117 /// Return the number of consecutive 0-bits in \p x starting from the least
118 /// significant bit position. If \p x is 0, the result is undefined.
119 inline int
121 {
122 #if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG) && \
123  !defined(ARCH_OS_WASM_VM)
124  return __builtin_ctzl(x);
125 #elif defined(ARCH_COMPILER_MSVC)
126  unsigned long index;
127  _BitScanForward64(&index, x);
128  return index;
129 #else
130  // Flip trailing zeros to 1s, and clear all other bits, then count.
131  x = (x ^ (x - 1)) >> 1;
132  int c = 0;
133  for (; x; ++c) {
134  x >>= 1;
135  }
136  return c;
137 #endif
138 }
139 
140 
141 ///@}
142 
144 
145 #endif // PXR_BASE_ARCH_MATH_H
int ArchCountTrailingZeros(uint64_t x)
Definition: math.h:120
const GLdouble * v
Definition: glcorearb.h:837
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
OIIO_HOSTDEVICE void sincos(float x, float *sine, float *cosine)
Definition: fmath.h:711
GLdouble s
Definition: glad.h:3009
GLint GLenum GLint x
Definition: glcorearb.h:409
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74