Pixar Arch 0.25.8
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1// Copyright 2016 Pixar
2//
3// Licensed under the terms set forth in the LICENSE.txt file available at
4// https://openusd.org/license.
5//
6// Modified by Jeremy Retailleau.
7
8#ifndef PXR_ARCH_MATH_H
9#define PXR_ARCH_MATH_H
10
13
14#include "pxr/arch/pxr.h"
15#include "pxr/arch/defines.h"
16#include "pxr/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
27ARCH_NAMESPACE_OPEN_SCOPE
28
29#if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || \
30 defined(ARCH_OS_WASM_VM) || defined (doxygen)
31
34#define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
35
37inline long ArchSign(long val) {
38 return (val > 0) - (val < 0);
39}
40
43inline uint32_t ArchFloatToBitPattern(float v) {
44 union {
45 float _float;
46 uint32_t _uint;
47 } value;
48 value._float = v;
49 return value._uint;
50}
51
54inline float ArchBitPatternToFloat(uint32_t v) {
55 union {
56 uint32_t _uint;
57 float _float;
58 } value;
59 value._uint = v;
60 return value._float;
61}
62
65inline uint64_t ArchDoubleToBitPattern(double v) {
66 union {
67 double _double;
68 uint64_t _uint;
69 } value;
70 value._double = v;
71 return value._uint;
72}
73
76inline double ArchBitPatternToDouble(uint64_t v) {
77 union {
78 uint64_t _uint;
79 double _double;
80 } value;
81 value._uint = v;
82 return value._double;
83}
84
85#else
86#error Unknown system architecture.
87#endif
88
89#if defined(ARCH_OS_LINUX) || defined(doxygen)
90
92inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
93
95inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
96
97#elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS) || \
98 defined(ARCH_OS_WASM_VM)
99
100inline void ArchSinCosf(float v, float *s, float *c) {
101 *s = std::sin(v);
102 *c = std::cos(v);
103}
104inline void ArchSinCos(double v, double *s, double *c) {
105 *s = std::sin(v);
106 *c = std::cos(v);
107}
108
109#else
110#error Unknown architecture.
111#endif
112
113
116inline int
118{
119#if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG) && \
120 !defined(ARCH_OS_WASM_VM)
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
137ARCH_NAMESPACE_CLOSE_SCOPE
138
139#endif // PXR_ARCH_MATH_H
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:43
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:65
double ArchBitPatternToDouble(uint64_t v)
Returns The double precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition math.h:76
long ArchSign(long val)
Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
Definition math.h:37
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition math.h:95
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition math.h:92
float ArchBitPatternToFloat(uint32_t v)
Returns The single precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition math.h:54
int ArchCountTrailingZeros(uint64_t x)
Return the number of consecutive 0-bits in x starting from the least significant bit position.
Definition math.h:117