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