AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Prefetch.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_PREFETCH_H
18 #define AGX_PREFETCH_H
19 
21 #include <agx/config/AGX_USE_SSE.h>
22 
23 #include <agx/config.h>
24 #include <agx/macros.h>
25 #include <cstddef> // mostly for size_t
26 
27 #if AGX_USE_PREFETCHING() && ( defined(_MSC_VER) || AGX_USE_SSE() )
28 
29 #include <xmmintrin.h>
30 
31 #if defined(__clang__) || defined(__INTEL_COMPILER)
32 // Clang use defines instead of enum
33 #define _mm_hint int
34 #endif
35 
36 namespace agx
37 {
38 
40  {
41  L1 = _MM_HINT_T0,
42  L2 = _MM_HINT_T1,
43  L3 = _MM_HINT_T2,
44  NTA = _MM_HINT_NTA
45  };
46 
47  // _MM_HINT_T0 - All cache levels
48  // _MM_HINT_T1 - All except 0
49  // _MM_HINT_T2 - All except 0&1
50  // _MM_HINT_NTA - Non-temporal data prefetch
51 
52 
53  template< int hint, typename T >
54  AGX_FORCE_INLINE void prefetch( const T* ptr )
55  {
56  #ifdef __GNUC__
57  _mm_prefetch( (const char*) ptr, (_mm_hint) hint );
58  #else
59  _mm_prefetch( (const char*) ptr, hint );
60  #endif
61  }
62 
63 
64  template< int hint, typename T >
65  AGX_FORCE_INLINE void prefetch_64( const T* ptr )
66  {
67  prefetch<hint>( ptr );
68  }
69 
70  template< int hint, typename T >
71  AGX_FORCE_INLINE void prefetch_128( const T* ptr )
72  {
73  prefetch<hint>( ptr );
74  prefetch<hint>( ((const char*)ptr) + 64 );
75  }
76 
77  template< int hint, typename T >
78  AGX_FORCE_INLINE void prefetch_256( const T* ptr )
79  {
80  prefetch<hint>( ptr );
81  prefetch<hint>( ((const char*)ptr) + 64 );
82  prefetch<hint>( ((const char*)ptr) + 128 );
83  prefetch<hint>( ((const char*)ptr) + 192 );
84  }
85 
86 
87 
88 
89  const size_t PREFETCH_SIZE = 64;
90 
91  template <PrefetchType PrefetchT>
92  AGX_FORCE_INLINE void prefetchBytes(const void *buffer, size_t numBytes)
93  {
94  const size_t numBlocks = (numBytes + PREFETCH_SIZE-1) / PREFETCH_SIZE;
95  // agxVerify(agx::isAligned(buffer, PREFETCH_SIZE));
96 
97  for (size_t i = 0; i < numBlocks; ++i)
98  {
99  const void *ptr = (const char *)buffer + i * PREFETCH_SIZE;
100  prefetch<PrefetchT>(ptr);
101  }
102  }
103 
104  template <typename T>
105  AGX_FORCE_INLINE void prefetchNTA(const T *buffer, size_t numElements)
106  {
107  const size_t numBytes = numElements * sizeof(T);
108  prefetchBytes<agx::NTA>(buffer, numBytes);
109  }
110 
111  template <typename T>
112  AGX_FORCE_INLINE void prefetchL3(const T *buffer, size_t numElements)
113  {
114  const size_t numBytes = numElements * sizeof(T);
115  prefetchBytes<agx::L3>(buffer, numBytes);
116  }
117 
118 } // namespace agx
119 
120 #else
121  // prefetching disabled
122 
123 namespace agx
124 {
125 
126  enum PrefetchType
127  {
128  L1 = 3,
129  L2 = 2,
130  L3 = 1,
131  NTA = 0
132  };
133 
134  template< int hint, typename T >
135  AGX_FORCE_INLINE void prefetch( const T* ) {}
136 
137  template< int hint, typename T >
138  AGX_FORCE_INLINE void prefetch_64( const T* ) {}
139 
140  template< int hint, typename T >
141  AGX_FORCE_INLINE void prefetch_128( const T* ) {}
142 
143  template< int hint, typename T >
144  AGX_FORCE_INLINE void prefetch_256( const T* ) {}
145 
146 
147  template <int PrefetchT>
148  AGX_FORCE_INLINE void prefetchBytes(const void *, size_t )
149  {}
150 
151  template <typename T>
152  AGX_FORCE_INLINE void prefetchNTA(const T *, size_t )
153  {}
154 
155  template <typename T>
156  AGX_FORCE_INLINE void prefetchL3(const T *, size_t )
157  {}
158 
159 }
160 #endif // AGX_USE_SSE etc.
161 
162 #endif // Include guard.
163 
void prefetchNTA(const T *buffer, size_t numElements)
Definition: Prefetch.h:105
const size_t PREFETCH_SIZE
Definition: Prefetch.h:89
#define AGX_FORCE_INLINE
Definition: Object.h:62
PrefetchType
Definition: Prefetch.h:39
void prefetch(const T *ptr)
Definition: Prefetch.h:54
void prefetch_128(const T *ptr)
Definition: Prefetch.h:71
void prefetch_256(const T *ptr)
Definition: Prefetch.h:78
void prefetchL3(const T *buffer, size_t numElements)
Definition: Prefetch.h:112
void prefetch_64(const T *ptr)
Definition: Prefetch.h:65
void prefetchBytes(const void *buffer, size_t numBytes)
Definition: Prefetch.h:92