xref: /dpdk/lib/table/rte_lru_x86.h (revision 99a2dd95)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef __INCLUDE_RTE_LRU_X86_H__
6 #define __INCLUDE_RTE_LRU_X86_H__
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include <stdint.h>
13 
14 #include <rte_config.h>
15 #include <rte_common.h>
16 
17 #ifndef RTE_TABLE_HASH_LRU_STRATEGY
18 #define RTE_TABLE_HASH_LRU_STRATEGY                        2
19 #endif
20 
21 #if RTE_TABLE_HASH_LRU_STRATEGY == 2
22 
23 #if RTE_CC_IS_GNU && (GCC_VERSION > 40306)
24 #include <x86intrin.h>
25 #else
26 #include <emmintrin.h>
27 #include <smmintrin.h>
28 #include <xmmintrin.h>
29 #endif
30 
31 #define lru_init(bucket)						\
32 	{ bucket->lru_list = 0x0000000100020003LLU; }
33 
34 #define lru_pos(bucket) (bucket->lru_list & 0xFFFFLLU)
35 
36 #define lru_update(bucket, mru_val)					\
37 do {									\
38 	/* set up the masks for all possible shuffles, depends on pos */\
39 	static uint64_t masks[10] = {					\
40 		/* Shuffle order; Make Zero (see _mm_shuffle_epi8 manual) */\
41 		0x0100070605040302, 0x8080808080808080,			\
42 		0x0302070605040100, 0x8080808080808080,			\
43 		0x0504070603020100, 0x8080808080808080,			\
44 		0x0706050403020100, 0x8080808080808080,			\
45 		0x0706050403020100, 0x8080808080808080};		\
46 	/* load up one register with repeats of mru-val  */		\
47 	uint64_t mru2 = mru_val;					\
48 	uint64_t mru3 = mru2 | (mru2 << 16);				\
49 	uint64_t lru = bucket->lru_list;				\
50 	/* XOR to cause the word we're looking for to go to zero */	\
51 	uint64_t mru = lru ^ ((mru3 << 32) | mru3);			\
52 	__m128i c = _mm_cvtsi64_si128(mru);				\
53 	__m128i b = _mm_cvtsi64_si128(lru);				\
54 	/* Find the minimum value (first zero word, if it's in there) */\
55 	__m128i d = _mm_minpos_epu16(c);				\
56 	/* Second word is the index to found word (first word is the value) */\
57 	unsigned int pos = _mm_extract_epi16(d, 1);			\
58 	/* move the recently used location to top of list */		\
59 	__m128i k = _mm_shuffle_epi8(b, *((__m128i *) &masks[2 * pos]));\
60 	/* Finally, update the original list with the reordered data */	\
61 	bucket->lru_list = _mm_extract_epi64(k, 0);			\
62 	/* Phwew! */							\
63 } while (0)
64 
65 #elif RTE_TABLE_HASH_LRU_STRATEGY == 3
66 
67 #if RTE_CC_IS_GNU && (GCC_VERSION > 40306)
68 #include <x86intrin.h>
69 #else
70 #include <emmintrin.h>
71 #include <smmintrin.h>
72 #include <xmmintrin.h>
73 #endif
74 
75 #define lru_init(bucket)						\
76 	{ bucket->lru_list = ~0LLU; }
77 
78 static inline int
79 f_lru_pos(uint64_t lru_list)
80 {
81 	__m128i lst = _mm_set_epi64x((uint64_t)-1, lru_list);
82 	__m128i min = _mm_minpos_epu16(lst);
83 	return _mm_extract_epi16(min, 1);
84 }
85 #define lru_pos(bucket) f_lru_pos(bucket->lru_list)
86 
87 #define lru_update(bucket, mru_val)					\
88 do {									\
89 	const uint64_t orvals[] = {0xFFFFLLU, 0xFFFFLLU << 16,		\
90 		0xFFFFLLU << 32, 0xFFFFLLU << 48, 0LLU};		\
91 	const uint64_t decs[] = {0x1000100010001LLU, 0};		\
92 	__m128i lru = _mm_cvtsi64_si128(bucket->lru_list);		\
93 	__m128i vdec = _mm_cvtsi64_si128(decs[mru_val>>2]);		\
94 	lru = _mm_subs_epu16(lru, vdec);				\
95 	bucket->lru_list = _mm_extract_epi64(lru, 0) | orvals[mru_val];	\
96 } while (0)
97 
98 #endif
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif
105