| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | #ifndef PXFOUNDATION_PXUNIXINTRINSICS_H |
| 31 | #define PXFOUNDATION_PXUNIXINTRINSICS_H |
| 32 | |
| 33 | #include "foundation/Px.h" |
| 34 | #include "foundation/PxSharedAssert.h" |
| 35 | |
| 36 | #if !(PX_LINUX || PX_ANDROID || PX_PS4 || PX_APPLE_FAMILY) |
| 37 | #error "This file should only be included by Unix builds!!" |
| 38 | #endif |
| 39 | |
| 40 | #if (PX_LINUX || PX_ANDROID) && !defined(__CUDACC__) && !PX_EMSCRIPTEN |
| 41 | // Linux/android and CUDA compilation does not work with std::isfnite, as it is not marked as CUDA callable |
| 42 | #include <cmath> |
| 43 | #ifndef isfinite |
| 44 | using std::isfinite; |
| 45 | #endif |
| 46 | #endif |
| 47 | |
| 48 | #include <math.h> |
| 49 | #include <float.h> |
| 50 | |
| 51 | namespace physx |
| 52 | { |
| 53 | namespace intrinsics |
| 54 | { |
| 55 | //! \brief platform-specific absolute value |
| 56 | PX_CUDA_CALLABLE PX_FORCE_INLINE float abs(float a) |
| 57 | { |
| 58 | return ::fabsf(x: a); |
| 59 | } |
| 60 | |
| 61 | //! \brief platform-specific select float |
| 62 | PX_CUDA_CALLABLE PX_FORCE_INLINE float fsel(float a, float b, float c) |
| 63 | { |
| 64 | return (a >= 0.0f) ? b : c; |
| 65 | } |
| 66 | |
| 67 | //! \brief platform-specific sign |
| 68 | PX_CUDA_CALLABLE PX_FORCE_INLINE float sign(float a) |
| 69 | { |
| 70 | return (a >= 0.0f) ? 1.0f : -1.0f; |
| 71 | } |
| 72 | |
| 73 | //! \brief platform-specific reciprocal |
| 74 | PX_CUDA_CALLABLE PX_FORCE_INLINE float recip(float a) |
| 75 | { |
| 76 | return 1.0f / a; |
| 77 | } |
| 78 | |
| 79 | //! \brief platform-specific reciprocal estimate |
| 80 | PX_CUDA_CALLABLE PX_FORCE_INLINE float recipFast(float a) |
| 81 | { |
| 82 | return 1.0f / a; |
| 83 | } |
| 84 | |
| 85 | //! \brief platform-specific square root |
| 86 | PX_CUDA_CALLABLE PX_FORCE_INLINE float sqrt(float a) |
| 87 | { |
| 88 | return ::sqrtf(x: a); |
| 89 | } |
| 90 | |
| 91 | //! \brief platform-specific reciprocal square root |
| 92 | PX_CUDA_CALLABLE PX_FORCE_INLINE float recipSqrt(float a) |
| 93 | { |
| 94 | return 1.0f / ::sqrtf(x: a); |
| 95 | } |
| 96 | |
| 97 | PX_CUDA_CALLABLE PX_FORCE_INLINE float recipSqrtFast(float a) |
| 98 | { |
| 99 | return 1.0f / ::sqrtf(x: a); |
| 100 | } |
| 101 | |
| 102 | //! \brief platform-specific sine |
| 103 | PX_CUDA_CALLABLE PX_FORCE_INLINE float sin(float a) |
| 104 | { |
| 105 | return ::sinf(x: a); |
| 106 | } |
| 107 | |
| 108 | //! \brief platform-specific cosine |
| 109 | PX_CUDA_CALLABLE PX_FORCE_INLINE float cos(float a) |
| 110 | { |
| 111 | return ::cosf(x: a); |
| 112 | } |
| 113 | |
| 114 | //! \brief platform-specific minimum |
| 115 | PX_CUDA_CALLABLE PX_FORCE_INLINE float selectMin(float a, float b) |
| 116 | { |
| 117 | return a < b ? a : b; |
| 118 | } |
| 119 | |
| 120 | //! \brief platform-specific maximum |
| 121 | PX_CUDA_CALLABLE PX_FORCE_INLINE float selectMax(float a, float b) |
| 122 | { |
| 123 | return a > b ? a : b; |
| 124 | } |
| 125 | |
| 126 | //! \brief platform-specific finiteness check (not INF or NAN) |
| 127 | PX_CUDA_CALLABLE PX_FORCE_INLINE bool isFinite(float a) |
| 128 | { |
| 129 | //std::isfinite not recommended as of Feb 2017, since it doesn't work with g++/clang's floating point optimization. |
| 130 | union localU { PxU32 i; float f; } floatUnion; |
| 131 | floatUnion.f = a; |
| 132 | return !((floatUnion.i & 0x7fffffff) >= 0x7f800000); |
| 133 | } |
| 134 | |
| 135 | //! \brief platform-specific finiteness check (not INF or NAN) |
| 136 | PX_CUDA_CALLABLE PX_FORCE_INLINE bool isFinite(double a) |
| 137 | { |
| 138 | return !!isfinite(x: a); |
| 139 | } |
| 140 | |
| 141 | /*! |
| 142 | Sets \c count bytes starting at \c dst to zero. |
| 143 | */ |
| 144 | PX_FORCE_INLINE void* memZero(void* dest, uint32_t count) |
| 145 | { |
| 146 | return memset(s: dest, c: 0, n: count); |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | Sets \c count bytes starting at \c dst to \c c. |
| 151 | */ |
| 152 | PX_FORCE_INLINE void* memSet(void* dest, int32_t c, uint32_t count) |
| 153 | { |
| 154 | return memset(s: dest, c: c, n: count); |
| 155 | } |
| 156 | |
| 157 | /*! |
| 158 | Copies \c count bytes from \c src to \c dst. User memMove if regions overlap. |
| 159 | */ |
| 160 | PX_FORCE_INLINE void* memCopy(void* dest, const void* src, uint32_t count) |
| 161 | { |
| 162 | return memcpy(dest: dest, src: src, n: count); |
| 163 | } |
| 164 | |
| 165 | /*! |
| 166 | Copies \c count bytes from \c src to \c dst. Supports overlapping regions. |
| 167 | */ |
| 168 | PX_FORCE_INLINE void* memMove(void* dest, const void* src, uint32_t count) |
| 169 | { |
| 170 | return memmove(dest: dest, src: src, n: count); |
| 171 | } |
| 172 | |
| 173 | /*! |
| 174 | Set 128B to zero starting at \c dst+offset. Must be aligned. |
| 175 | */ |
| 176 | PX_FORCE_INLINE void memZero128(void* dest, uint32_t offset = 0) |
| 177 | { |
| 178 | PX_SHARED_ASSERT(((size_t(dest) + offset) & 0x7f) == 0); |
| 179 | memSet(dest: reinterpret_cast<char*>(dest) + offset, c: 0, count: 128); |
| 180 | } |
| 181 | |
| 182 | } // namespace intrinsics |
| 183 | } // namespace physx |
| 184 | |
| 185 | #endif // #ifndef PXFOUNDATION_PXUNIXINTRINSICS_H |
| 186 | |