34 #ifndef __INCLUDE_RTE_LRU_H__
35 #define __INCLUDE_RTE_LRU_H__
43 #ifdef __INTEL_COMPILER
44 #define GCC_VERSION (0)
46 #define GCC_VERSION (__GNUC__ * 10000+__GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
49 #ifndef RTE_TABLE_HASH_LRU_STRATEGY
51 #define RTE_TABLE_HASH_LRU_STRATEGY 2
53 #define RTE_TABLE_HASH_LRU_STRATEGY 1
57 #ifndef RTE_ARCH_X86_64
58 #undef RTE_TABLE_HASH_LRU_STRATEGY
59 #define RTE_TABLE_HASH_LRU_STRATEGY 1
62 #if (RTE_TABLE_HASH_LRU_STRATEGY < 0) || (RTE_TABLE_HASH_LRU_STRATEGY > 3)
63 #error Invalid value for RTE_TABLE_HASH_LRU_STRATEGY
66 #if RTE_TABLE_HASH_LRU_STRATEGY == 0
68 #define lru_init(bucket) \
73 #define lru_pos(bucket) (bucket->lru_list & 0xFFFFLLU)
75 #define lru_update(bucket, mru_val) \
81 #elif RTE_TABLE_HASH_LRU_STRATEGY == 1
83 #define lru_init(bucket) \
85 bucket->lru_list = 0x0000000100020003LLU; \
88 #define lru_pos(bucket) (bucket->lru_list & 0xFFFFLLU)
90 #define lru_update(bucket, mru_val) \
92 uint64_t x, pos, x0, x1, x2, mask; \
94 x = bucket->lru_list; \
97 if ((x >> 48) == ((uint64_t) mru_val)) \
100 if (((x >> 32) & 0xFFFFLLU) == ((uint64_t) mru_val)) \
103 if (((x >> 16) & 0xFFFFLLU) == ((uint64_t) mru_val)) \
106 if ((x & 0xFFFFLLU) == ((uint64_t) mru_val)) \
111 mask = (~0LLU) << pos; \
113 x1 = (x >> 16) & mask; \
114 x2 = (x << (48 - pos)) & (0xFFFFLLU << 48); \
118 bucket->lru_list = x; \
121 #elif RTE_TABLE_HASH_LRU_STRATEGY == 2
123 #if GCC_VERSION > 40306
124 #include <x86intrin.h>
126 #include <emmintrin.h>
127 #include <smmintrin.h>
128 #include <xmmintrin.h>
131 #define lru_init(bucket) \
133 bucket->lru_list = 0x0000000100020003LLU; \
136 #define lru_pos(bucket) (bucket->lru_list & 0xFFFFLLU)
138 #define lru_update(bucket, mru_val) \
141 static uint64_t masks[10] = { \
143 0x0100070605040302, 0x8080808080808080, \
144 0x0302070605040100, 0x8080808080808080, \
145 0x0504070603020100, 0x8080808080808080, \
146 0x0706050403020100, 0x8080808080808080, \
147 0x0706050403020100, 0x8080808080808080}; \
149 uint64_t mru2 = mru_val; \
150 uint64_t mru3 = mru2 | (mru2 << 16); \
151 uint64_t lru = bucket->lru_list; \
153 uint64_t mru = lru ^ ((mru3 << 32) | mru3); \
154 __m128i c = _mm_cvtsi64_si128(mru); \
155 __m128i b = _mm_cvtsi64_si128(lru); \
157 __m128i d = _mm_minpos_epu16(c); \
159 unsigned pos = _mm_extract_epi16(d, 1); \
161 __m128i k = _mm_shuffle_epi8(b, *((__m128i *) &masks[2 * pos]));\
163 bucket->lru_list = _mm_extract_epi64(k, 0); \
167 #elif RTE_TABLE_HASH_LRU_STRATEGY == 3
169 #if GCC_VERSION > 40306
170 #include <x86intrin.h>
172 #include <emmintrin.h>
173 #include <smmintrin.h>
174 #include <xmmintrin.h>
177 #define lru_init(bucket) \
179 bucket->lru_list = ~0LLU; \
184 f_lru_pos(uint64_t lru_list)
186 __m128i lst = _mm_set_epi64x((uint64_t)-1, lru_list);
187 __m128i min = _mm_minpos_epu16(lst);
188 return _mm_extract_epi16(min, 1);
190 #define lru_pos(bucket) f_lru_pos(bucket->lru_list)
192 #define lru_update(bucket, mru_val) \
194 const uint64_t orvals[] = {0xFFFFLLU, 0xFFFFLLU << 16, \
195 0xFFFFLLU << 32, 0xFFFFLLU << 48, 0LLU}; \
196 const uint64_t decs[] = {0x1000100010001LLU, 0}; \
197 __m128i lru = _mm_cvtsi64_si128(bucket->lru_list); \
198 __m128i vdec = _mm_cvtsi64_si128(decs[mru_val>>2]); \
199 lru = _mm_subs_epu16(lru, vdec); \
200 bucket->lru_list = _mm_extract_epi64(lru, 0) | orvals[mru_val]; \
205 #error "Incorrect value for RTE_TABLE_HASH_LRU_STRATEGY"