Loading...
Searching...
No Matches
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
13
14#include "pxr/pxr.h"
15#include "pxr/base/arch/defines.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
27PXR_NAMESPACE_OPEN_SCOPE
28
31
32#if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || \
33 defined(ARCH_OS_WASM_VM) || defined (doxygen)
34
37#define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
38
40inline long ArchSign(long val) {
41 return (val > 0) - (val < 0);
42}
43
46inline 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
57inline 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
68inline 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
79inline 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
95inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
96
98inline 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
103inline void ArchSinCosf(float v, float *s, float *c) {
104 *s = std::sin(v);
105 *c = std::cos(v);
106}
107inline 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
119inline 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
142
143PXR_NAMESPACE_CLOSE_SCOPE
144
145#endif // PXR_BASE_ARCH_MATH_H
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition: math.h:98
long ArchSign(long val)
Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
Definition: math.h:40
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition: math.h:95
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:46
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:68
float ArchBitPatternToFloat(uint32_t v)
Returns The single precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition: math.h:57
int ArchCountTrailingZeros(uint64_t x)
Return the number of consecutive 0-bits in x starting from the least significant bit position.
Definition: math.h:120
double ArchBitPatternToDouble(uint64_t v)
Returns The double precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition: math.h:79
Define integral types.