| 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 PSFOUNDATION_PSUTILITIES_H |
| 31 | #define PSFOUNDATION_PSUTILITIES_H |
| 32 | |
| 33 | #include "foundation/PxVec3.h" |
| 34 | #include "foundation/PxAssert.h" |
| 35 | #include "Ps.h" |
| 36 | #include "PsIntrinsics.h" |
| 37 | #include "PsBasicTemplates.h" |
| 38 | |
| 39 | namespace physx |
| 40 | { |
| 41 | namespace shdfnd |
| 42 | { |
| 43 | PX_INLINE char littleEndian() |
| 44 | { |
| 45 | int i = 1; |
| 46 | return *(reinterpret_cast<char*>(&i)); |
| 47 | } |
| 48 | |
| 49 | // PT: checked casts |
| 50 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 to32(PxU64 value) |
| 51 | { |
| 52 | PX_ASSERT(value <= 0xffffffff); |
| 53 | return PxU32(value); |
| 54 | } |
| 55 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxU16 to16(PxU32 value) |
| 56 | { |
| 57 | PX_ASSERT(value <= 0xffff); |
| 58 | return PxU16(value); |
| 59 | } |
| 60 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxU8 to8(PxU16 value) |
| 61 | { |
| 62 | PX_ASSERT(value <= 0xff); |
| 63 | return PxU8(value); |
| 64 | } |
| 65 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxU8 to8(PxU32 value) |
| 66 | { |
| 67 | PX_ASSERT(value <= 0xff); |
| 68 | return PxU8(value); |
| 69 | } |
| 70 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxU8 to8(PxI32 value) |
| 71 | { |
| 72 | PX_ASSERT(value <= 0xff); |
| 73 | PX_ASSERT(value >= 0); |
| 74 | return PxU8(value); |
| 75 | } |
| 76 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxI8 toI8(PxU32 value) |
| 77 | { |
| 78 | PX_ASSERT(value <= 0x7f); |
| 79 | return PxI8(value); |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | Get number of elements in array |
| 84 | */ |
| 85 | template <typename T, size_t N> |
| 86 | char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 87 | #define PX_ARRAY_SIZE(_array) (sizeof(physx::shdfnd::ArraySizeHelper(_array))) |
| 88 | |
| 89 | /*! |
| 90 | Sort two elements using operator< |
| 91 | |
| 92 | On return x will be the smaller of the two |
| 93 | */ |
| 94 | template <class T> |
| 95 | PX_CUDA_CALLABLE PX_FORCE_INLINE void order(T& x, T& y) |
| 96 | { |
| 97 | if(y < x) |
| 98 | swap(x, y); |
| 99 | } |
| 100 | |
| 101 | // most architectures can do predication on real comparisons, and on VMX, it matters |
| 102 | |
| 103 | PX_CUDA_CALLABLE PX_FORCE_INLINE void order(PxReal& x, PxReal& y) |
| 104 | { |
| 105 | PxReal newX = PxMin(a: x, b: y); |
| 106 | PxReal newY = PxMax(a: x, b: y); |
| 107 | x = newX; |
| 108 | y = newY; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | Sort two elements using operator< and also keep order |
| 113 | of any extra data |
| 114 | */ |
| 115 | template <class T, class E1> |
| 116 | PX_CUDA_CALLABLE PX_FORCE_INLINE void order(T& x, T& y, E1& xe1, E1& ye1) |
| 117 | { |
| 118 | if(y < x) |
| 119 | { |
| 120 | swap(x, y); |
| 121 | swap(xe1, ye1); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #if PX_GCC_FAMILY && !PX_EMSCRIPTEN && !PX_LINUX |
| 126 | __attribute__((noreturn)) |
| 127 | #endif |
| 128 | PX_INLINE void debugBreak() |
| 129 | { |
| 130 | #if PX_WINDOWS || PX_XBOXONE || PX_XBOX_SERIES_X |
| 131 | __debugbreak(); |
| 132 | #elif PX_ANDROID |
| 133 | raise(SIGTRAP); // works better than __builtin_trap. Proper call stack and can be continued. |
| 134 | #elif PX_LINUX |
| 135 | asm("int $3" ); |
| 136 | #elif PX_GCC_FAMILY |
| 137 | __builtin_trap(); |
| 138 | #else |
| 139 | PX_ASSERT(false); |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | bool checkValid(const float&); |
| 144 | bool checkValid(const PxVec3&); |
| 145 | bool checkValid(const PxQuat&); |
| 146 | bool checkValid(const PxMat33&); |
| 147 | bool checkValid(const PxTransform&); |
| 148 | bool checkValid(const char*); |
| 149 | |
| 150 | // equivalent to std::max_element |
| 151 | template <typename T> |
| 152 | inline const T* maxElement(const T* first, const T* last) |
| 153 | { |
| 154 | const T* m = first; |
| 155 | for(const T* it = first + 1; it < last; ++it) |
| 156 | if(*m < *it) |
| 157 | m = it; |
| 158 | |
| 159 | return m; |
| 160 | } |
| 161 | |
| 162 | } // namespace shdfnd |
| 163 | } // namespace physx |
| 164 | |
| 165 | #endif |
| 166 | |