4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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
52 #else /* if no SSE, use simple scalar version */
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) \
140 /* set up the masks for all possible shuffles, depends on pos */\
141 static uint64_t masks[10] = { \
142 /* Shuffle order; Make Zero (see _mm_shuffle_epi8 manual) */\
143 0x0100070605040302, 0x8080808080808080, \
144 0x0302070605040100, 0x8080808080808080, \
145 0x0504070603020100, 0x8080808080808080, \
146 0x0706050403020100, 0x8080808080808080, \
147 0x0706050403020100, 0x8080808080808080}; \
148 /* load up one register with repeats of mru-val */ \
149 uint64_t mru2 = mru_val; \
150 uint64_t mru3 = mru2 | (mru2 << 16); \
151 uint64_t lru = bucket->lru_list; \
152 /* XOR to cause the word we're looking for to go to zero */ \
153 uint64_t mru = lru ^ ((mru3 << 32) | mru3); \
154 __m128i c = _mm_cvtsi64_si128(mru); \
155 __m128i b = _mm_cvtsi64_si128(lru); \
156 /* Find the minimum value (first zero word, if it's in there) */\
157 __m128i d = _mm_minpos_epu16(c); \
158 /* Second word is the index to found word (first word is the value) */\
159 unsigned pos = _mm_extract_epi16(d, 1); \
160 /* move the recently used location to top of list */ \
161 __m128i k = _mm_shuffle_epi8(b, *((__m128i *) &masks[2 * pos]));\
162 /* Finally, update the original list with the reordered data */ \
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"