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) || defined (doxygen)
33 
34 /// This is the smallest value e such that 1+e^2 == 1, using floats.
35 /// True for all IEEE754 chipsets.
36 #define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
37 
38 /// Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
39 inline long ArchSign(long val) {
40  return (val > 0) - (val < 0);
41 }
42 
43 /// Returns The IEEE-754 bit pattern of the specified single precision value
44 /// as a 32-bit unsigned integer.
45 inline uint32_t ArchFloatToBitPattern(float v) {
46  union {
47  float _float;
48  uint32_t _uint;
49  } value;
50  value._float = v;
51  return value._uint;
52 }
53 
54 /// Returns The single precision floating point value corresponding to the
55 /// given IEEE-754 bit pattern.
56 inline float ArchBitPatternToFloat(uint32_t v) {
57  union {
58  uint32_t _uint;
59  float _float;
60  } value;
61  value._uint = v;
62  return value._float;
63 }
64 
65 /// Returns The IEEE-754 bit pattern of the specified double precision value
66 /// as a 64-bit unsigned integer.
67 inline uint64_t ArchDoubleToBitPattern(double v) {
68  union {
69  double _double;
70  uint64_t _uint;
71  } value;
72  value._double = v;
73  return value._uint;
74 }
75 
76 /// Returns The double precision floating point value corresponding to the
77 /// given IEEE-754 bit pattern.
78 inline double ArchBitPatternToDouble(uint64_t v) {
79  union {
80  uint64_t _uint;
81  double _double;
82  } value;
83  value._uint = v;
84  return value._double;
85 }
86 
87 #else
88 #error Unknown system architecture.
89 #endif
90 
91 #if defined(ARCH_OS_LINUX) || defined(doxygen)
92 
93 /// Computes the sine and cosine of the specified value as a float.
94 inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
95 
96 /// Computes the sine and cosine of the specified value as a double.
97 inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
98 
99 #elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS)
100 
101 inline void ArchSinCosf(float v, float *s, float *c) {
102  *s = std::sin(v);
103  *c = std::cos(v);
104 }
105 inline void ArchSinCos(double v, double *s, double *c) {
106  *s = std::sin(v);
107  *c = std::cos(v);
108 }
109 
110 #else
111 #error Unknown architecture.
112 #endif
113 
114 
115 /// Return the number of consecutive 0-bits in \p x starting from the least
116 /// significant bit position. If \p x is 0, the result is undefined.
117 inline int
119 {
120 #if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
121  return __builtin_ctzl(x);
122 #elif defined(ARCH_COMPILER_MSVC)
123  unsigned long index;
124  _BitScanForward64(&index, x);
125  return index;
126 #else
127  // Flip trailing zeros to 1s, and clear all other bits, then count.
128  x = (x ^ (x - 1)) >> 1;
129  int c = 0;
130  for (; x; ++c) {
131  x >>= 1;
132  }
133  return c;
134 #endif
135 }
136 
137 
138 ///@}
139 
141 
142 #endif // PXR_BASE_ARCH_MATH_H
SYS_API double cos(double x)
int ArchCountTrailingZeros(uint64_t x)
Definition: math.h:118
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:711
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:1425
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SYS_API double sin(double x)