This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
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) || defined (doxygen)
33
36#define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
37
39inline long ArchSign(long val) {
40 return (val > 0) - (val < 0);
41}
42
45inline 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
56inline 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
67inline 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
78inline 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
94inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
95
97inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
98
99#elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS)
100
101inline void ArchSinCosf(float v, float *s, float *c) {
102 *s = std::sin(v);
103 *c = std::cos(v);
104}
105inline 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
117inline 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
139
140PXR_NAMESPACE_CLOSE_SCOPE
141
142#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:97
long ArchSign(long val)
Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
Definition: math.h:39
void ArchSinCosf(float v, float *s, float *c)
Computes the sine and cosine of the specified value as a float.
Definition: math.h:94
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:45
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:67
float ArchBitPatternToFloat(uint32_t v)
Returns The single precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition: math.h:56
int ArchCountTrailingZeros(uint64_t x)
Return the number of consecutive 0-bits in x starting from the least significant bit position.
Definition: math.h:118
double ArchBitPatternToDouble(uint64_t v)
Returns The double precision floating point value corresponding to the given IEEE-754 bit pattern.
Definition: math.h:78
Define integral types.