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_PSUNIXINTRINSICS_H |
31 | #define PSFOUNDATION_PSUNIXINTRINSICS_H |
32 | |
33 | #include "Ps.h" |
34 | #include "foundation/PxAssert.h" |
35 | #include <math.h> |
36 | |
37 | #if PX_ANDROID |
38 | #include <signal.h> // for Ns::debugBreak() { raise(SIGTRAP); } |
39 | #endif |
40 | |
41 | #if 0 |
42 | #include <libkern/OSAtomic.h> |
43 | #endif |
44 | |
45 | // this file is for internal intrinsics - that is, intrinsics that are used in |
46 | // cross platform code but do not appear in the API |
47 | |
48 | #if !(PX_LINUX || PX_ANDROID || PX_PS4 || PX_APPLE_FAMILY) |
49 | #error "This file should only be included by unix builds!!" |
50 | #endif |
51 | |
52 | namespace physx |
53 | { |
54 | namespace shdfnd |
55 | { |
56 | |
57 | PX_FORCE_INLINE void memoryBarrier() |
58 | { |
59 | __sync_synchronize(); |
60 | } |
61 | |
62 | /*! |
63 | Return the index of the highest set bit. Undefined for zero arg. |
64 | */ |
65 | PX_INLINE uint32_t highestSetBitUnsafe(uint32_t v) |
66 | { |
67 | |
68 | return 31 - __builtin_clz(v); |
69 | } |
70 | |
71 | /*! |
72 | Return the index of the highest set bit. Undefined for zero arg. |
73 | */ |
74 | PX_INLINE int32_t lowestSetBitUnsafe(uint32_t v) |
75 | { |
76 | return __builtin_ctz(v); |
77 | } |
78 | |
79 | /*! |
80 | Returns the index of the highest set bit. Returns 32 for v=0. |
81 | */ |
82 | PX_INLINE uint32_t countLeadingZeros(uint32_t v) |
83 | { |
84 | if(v) |
85 | return __builtin_clz(v); |
86 | else |
87 | return 32; |
88 | } |
89 | |
90 | /*! |
91 | Prefetch aligned 64B x86, 32b ARM around \c ptr+offset. |
92 | */ |
93 | PX_FORCE_INLINE void prefetchLine(const void* ptr, uint32_t offset = 0) |
94 | { |
95 | __builtin_prefetch(reinterpret_cast<const char* PX_RESTRICT>(ptr) + offset, 0, 3); |
96 | } |
97 | |
98 | /*! |
99 | Prefetch \c count bytes starting at \c ptr. |
100 | */ |
101 | #if PX_ANDROID || PX_IOS |
102 | PX_FORCE_INLINE void prefetch(const void* ptr, uint32_t count = 1) |
103 | { |
104 | const char* cp = static_cast<const char*>(ptr); |
105 | size_t p = reinterpret_cast<size_t>(ptr); |
106 | uint32_t startLine = uint32_t(p >> 5), endLine = uint32_t((p + count - 1) >> 5); |
107 | uint32_t lines = endLine - startLine + 1; |
108 | do |
109 | { |
110 | prefetchLine(cp); |
111 | cp += 32; |
112 | } while(--lines); |
113 | } |
114 | #else |
115 | PX_FORCE_INLINE void prefetch(const void* ptr, uint32_t count = 1) |
116 | { |
117 | const char* cp = reinterpret_cast<const char*>(ptr); |
118 | uint64_t p = size_t(ptr); |
119 | uint64_t startLine = p >> 6, endLine = (p + count - 1) >> 6; |
120 | uint64_t lines = endLine - startLine + 1; |
121 | do |
122 | { |
123 | prefetchLine(ptr: cp); |
124 | cp += 64; |
125 | } while(--lines); |
126 | } |
127 | #endif |
128 | |
129 | //! \brief platform-specific reciprocal |
130 | PX_CUDA_CALLABLE PX_FORCE_INLINE float recipFast(float a) |
131 | { |
132 | return 1.0f / a; |
133 | } |
134 | |
135 | //! \brief platform-specific fast reciprocal square root |
136 | PX_CUDA_CALLABLE PX_FORCE_INLINE float recipSqrtFast(float a) |
137 | { |
138 | return 1.0f / ::sqrtf(x: a); |
139 | } |
140 | |
141 | //! \brief platform-specific floor |
142 | PX_CUDA_CALLABLE PX_FORCE_INLINE float floatFloor(float x) |
143 | { |
144 | return ::floorf(x: x); |
145 | } |
146 | |
147 | #define NS_EXPECT_TRUE(x) x |
148 | #define NS_EXPECT_FALSE(x) x |
149 | |
150 | } // namespace shdfnd |
151 | } // namespace physx |
152 | |
153 | #endif // #ifndef PSFOUNDATION_PSUNIXINTRINSICS_H |
154 | |