RavEngine
Loading...
Searching...
No Matches
PxWindowsIntrinsics.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_WINDOWS_INTRINSICS_H
30#define PX_WINDOWS_INTRINSICS_H
31
32#include "foundation/PxAssert.h"
33
34// this file is for internal intrinsics - that is, intrinsics that are used in
35// cross platform code but do not appear in the API
36
37#if !PX_WINDOWS_FAMILY
38#error "This file should only be included by Windows builds!!"
39#endif
40
41#pragma warning(push)
42//'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives'
43#pragma warning(disable : 4668)
44#if PX_VC == 10
45#pragma warning(disable : 4987) // nonstandard extension used: 'throw (...)'
46#endif
47#include <intrin.h>
48#pragma warning(pop)
49
50#pragma warning(push)
51#pragma warning(disable : 4985) // 'symbol name': attributes not present on previous declaration
52#include <math.h>
53#pragma warning(pop)
54
55#include <float.h>
56// do not include for ARM target
57#if !PX_ARM && !PX_A64
58#include <mmintrin.h>
59#endif
60
61#pragma intrinsic(_BitScanForward)
62#pragma intrinsic(_BitScanReverse)
63
64#if !PX_DOXYGEN
65namespace physx
66{
67#endif
68
69/*
70* Implements a memory barrier
71*/
72PX_FORCE_INLINE void PxMemoryBarrier()
73{
74 _ReadWriteBarrier();
75 /* long Barrier;
76 __asm {
77 xchg Barrier, eax
78 }*/
79}
80
85{
86 unsigned long retval;
87 _BitScanReverse(&retval, v);
88 return retval;
89}
90
95{
96 unsigned long retval;
97 _BitScanForward(&retval, v);
98 return retval;
99}
100
105{
106 if(v)
107 {
108 unsigned long bsr = (unsigned long)-1;
109 _BitScanReverse(&bsr, v);
110 return 31 - bsr;
111 }
112 else
113 return 32;
114}
115
119#if !PX_ARM && !PX_A64
120PX_FORCE_INLINE void PxPrefetchLine(const void* ptr, uint32_t offset = 0)
121{
122 // cache line on X86/X64 is 64-bytes so a 128-byte prefetch would require 2 prefetches.
123 // However, we can only dispatch a limited number of prefetch instructions so we opt to prefetch just 1 cache line
124 /*_mm_prefetch(((const char*)ptr + offset), _MM_HINT_T0);*/
125 // We get slightly better performance prefetching to non-temporal addresses instead of all cache levels
126 _mm_prefetch(((const char*)ptr + offset), _MM_HINT_NTA);
127}
128#else
129PX_FORCE_INLINE void PxPrefetchLine(const void* ptr, uint32_t offset = 0)
130{
131 // arm does have 32b cache line size
132 __prefetch(((const char*)ptr + offset));
133}
134#endif
135
139#if !PX_ARM
140PX_FORCE_INLINE void PxPrefetch(const void* ptr, uint32_t count = 1)
141{
142 const char* cp = (char*)ptr;
143 uint64_t p = size_t(ptr);
144 uint64_t startLine = p >> 6, endLine = (p + count - 1) >> 6;
145 uint64_t lines = endLine - startLine + 1;
146 do
147 {
148 PxPrefetchLine(cp);
149 cp += 64;
150 } while(--lines);
151}
152#else
153PX_FORCE_INLINE void PxPrefetch(const void* ptr, uint32_t count = 1)
154{
155 const char* cp = (char*)ptr;
156 uint32_t p = size_t(ptr);
157 uint32_t startLine = p >> 5, endLine = (p + count - 1) >> 5;
158 uint32_t lines = endLine - startLine + 1;
159 do
160 {
161 PxPrefetchLine(cp);
162 cp += 32;
163 } while(--lines);
164}
165#endif
166
167#if !PX_DOXYGEN
168} // namespace physx
169#endif
170
171#endif
172
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
uint64 uint64_t
Definition fwd.hpp:145
uint32 uint32_t
Definition fwd.hpp:131
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_INLINE uint32_t PxLowestSetBitUnsafe(uint32_t v)
Definition PxUnixIntrinsics.h:64
PX_FORCE_INLINE void PxPrefetch(const void *ptr, uint32_t count=1)
Definition PxUnixIntrinsics.h:91
PX_INLINE uint32_t PxCountLeadingZeros(uint32_t v)
Definition PxUnixIntrinsics.h:72
PX_INLINE uint32_t PxHighestSetBitUnsafe(uint32_t v)
Definition PxUnixIntrinsics.h:55
PX_FORCE_INLINE void PxPrefetchLine(const void *ptr, uint32_t offset=0)
Definition PxUnixIntrinsics.h:83