1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_ARCH_MATH_H
25 #define PXR_BASE_ARCH_MATH_H
26 
27 /// \file arch/math.h
28 /// \ingroup group_arch_Math
29 /// Architecture-specific math function calls.
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/defines.h"
33 #include "pxr/base/arch/inttypes.h"
34 
35 #include <cmath>
36 #if !defined(M_PI)
37 #define M_PI 3.14159265358979323846
38 #endif
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 /// \addtogroup group_arch_Math
43 ///@{
44 
45 #if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || defined (doxygen)
46 
47 /// This is the smallest value e such that 1+e^2 == 1, using floats.
48 /// True for all IEEE754 chipsets.
49 #define ARCH_MIN_FLOAT_EPS_SQR      0.000244141F
50 
51 /// Three-valued sign.  Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
ArchSign(long val)52 inline long ArchSign(long val) {
53     return (val > 0) - (val < 0);
54 }
55 
56 /// Returns The IEEE-754 bit pattern of the specified single precision value
57 /// as a 32-bit unsigned integer.
ArchFloatToBitPattern(float v)58 inline uint32_t ArchFloatToBitPattern(float v) {
59     union {
60         float _float;
61         uint32_t _uint;
62     } value;
63     value._float = v;
64     return value._uint;
65 }
66 
67 /// Returns The single precision floating point value corresponding to the
68 /// given IEEE-754 bit pattern.
ArchBitPatternToFloat(uint32_t v)69 inline float ArchBitPatternToFloat(uint32_t v) {
70     union {
71         uint32_t _uint;
72         float _float;
73     } value;
74     value._uint = v;
75     return value._float;
76 }
77 
78 /// Returns The IEEE-754 bit pattern of the specified double precision value
79 /// as a 64-bit unsigned integer.
ArchDoubleToBitPattern(double v)80 inline uint64_t ArchDoubleToBitPattern(double v) {
81     union {
82         double _double;
83         uint64_t _uint;
84     } value;
85     value._double = v;
86     return value._uint;
87 }
88 
89 /// Returns The double precision floating point value corresponding to the
90 /// given IEEE-754 bit pattern.
ArchBitPatternToDouble(uint64_t v)91 inline double ArchBitPatternToDouble(uint64_t v) {
92     union {
93         uint64_t _uint;
94         double _double;
95     } value;
96     value._uint = v;
97     return value._double;
98 }
99 
100 #else
101 #error Unknown system architecture.
102 #endif
103 
104 #if defined(ARCH_OS_LINUX) || defined(ARCH_OS_FREEBSD) || defined(doxygen)
105 
106 /// Computes the sine and cosine of the specified value as a float.
ArchSinCosf(float v,float * s,float * c)107 inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
108 
109 /// Computes the sine and cosine of the specified value as a double.
ArchSinCos(double v,double * s,double * c)110 inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
111 
112 #elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS)
113 
ArchSinCosf(float v,float * s,float * c)114 inline void ArchSinCosf(float v, float *s, float *c) {
115     *s = std::sin(v);
116     *c = std::cos(v);
117 }
ArchSinCos(double v,double * s,double * c)118 inline void ArchSinCos(double v, double *s, double *c) {
119     *s = std::sin(v);
120     *c = std::cos(v);
121 }
122 
123 #else
124 #error Unknown architecture.
125 #endif
126 
127 ///@}
128 
129 PXR_NAMESPACE_CLOSE_SCOPE
130 
131 #endif // PXR_BASE_ARCH_MATH_H
132