Pixar Arch
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 "./defines.h"
15#include "./inttypes.h"
16
17#if defined(ARCH_COMPILER_MSVC)
18#include <intrin.h>
19#endif
20
21#include <cmath>
22#if !defined(M_PI)
23#define M_PI 3.14159265358979323846
24#endif
25
26namespace pxr {
27
28#if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || defined (doxygen)
29
32#define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
33
35inline long ArchSign(long val) {
36 return (val > 0) - (val < 0);
37}
38
41inline uint32_t ArchFloatToBitPattern(float v) {
42 union {
43 float _float;
44 uint32_t _uint;
45 } value;
46 value._float = v;
47 return value._uint;
48}
49
52inline float ArchBitPatternToFloat(uint32_t v) {
53 union {
54 uint32_t _uint;
55 float _float;
56 } value;
57 value._uint = v;
58 return value._float;
59}
60
63inline uint64_t ArchDoubleToBitPattern(double v) {
64 union {
65 double _double;
66 uint64_t _uint;
67 } value;
68 value._double = v;
69 return value._uint;
70}
71
74inline double ArchBitPatternToDouble(uint64_t v) {
75 union {
76 uint64_t _uint;
77 double _double;
78 } value;
79 value._uint = v;
80 return value._double;
81}
82
83#else
84#error Unknown system architecture.
85#endif
86
87#if defined(ARCH_OS_LINUX) || defined(doxygen)
88
90inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
91
93inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
94
95#elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS)
96
97inline void ArchSinCosf(float v, float *s, float *c) {
98 *s = std::sin(v);
99 *c = std::cos(v);
100}
101inline void ArchSinCos(double v, double *s, double *c) {
102 *s = std::sin(v);
103 *c = std::cos(v);
104}
105
106#else
107#error Unknown architecture.
108#endif
109
110
113inline int
115{
116#if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
117 return __builtin_ctzl(x);
118#elif defined(ARCH_COMPILER_MSVC)
119 unsigned long index;
120 _BitScanForward64(&index, x);
121 return index;
122#else
123 // Flip trailing zeros to 1s, and clear all other bits, then count.
124 x = (x ^ (x - 1)) >> 1;
125 int c = 0;
126 for (; x; ++c) {
127 x >>= 1;
128 }
129 return c;
130#endif
131}
132
133} // namespace pxr
134
135#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:41
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:63
double ArchBitPatternToDouble(uint64_t v)
Returns The double precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition math.h:74
long ArchSign(long val)
Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
Definition math.h:35
void ArchSinCos(double v, double *s, double *c)
Computes the sine and cosine of the specified value as a double.
Definition math.h:93
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition math.h:90
float ArchBitPatternToFloat(uint32_t v)
Returns The single precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition math.h:52
int ArchCountTrailingZeros(uint64_t x)
Return the number of consecutive 0-bits in x starting from the least significant bit position.
Definition math.h:114