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