Imported Upstream version 16.11
[deb_dpdk.git] / lib / librte_lpm / rte_lpm_sse.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #ifndef _RTE_LPM_SSE_H_
35 #define _RTE_LPM_SSE_H_
36
37 #include <rte_branch_prediction.h>
38 #include <rte_byteorder.h>
39 #include <rte_common.h>
40 #include <rte_vect.h>
41 #include <rte_lpm.h>
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 static inline void
48 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
49         uint32_t defv)
50 {
51         __m128i i24;
52         rte_xmm_t i8;
53         uint32_t tbl[4];
54         uint64_t idx, pt, pt2;
55         const uint32_t *ptbl;
56
57         const __m128i mask8 =
58                 _mm_set_epi32(UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX);
59
60         /*
61          * RTE_LPM_VALID_EXT_ENTRY_BITMASK for 2 LPM entries
62          * as one 64-bit value (0x0300000003000000).
63          */
64         const uint64_t mask_xv =
65                 ((uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK |
66                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 32);
67
68         /*
69          * RTE_LPM_LOOKUP_SUCCESS for 2 LPM entries
70          * as one 64-bit value (0x0100000001000000).
71          */
72         const uint64_t mask_v =
73                 ((uint64_t)RTE_LPM_LOOKUP_SUCCESS |
74                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 32);
75
76         /* get 4 indexes for tbl24[]. */
77         i24 = _mm_srli_epi32(ip, CHAR_BIT);
78
79         /* extract values from tbl24[] */
80         idx = _mm_cvtsi128_si64(i24);
81         i24 = _mm_srli_si128(i24, sizeof(uint64_t));
82
83         ptbl = (const uint32_t *)&lpm->tbl24[(uint32_t)idx];
84         tbl[0] = *ptbl;
85         ptbl = (const uint32_t *)&lpm->tbl24[idx >> 32];
86         tbl[1] = *ptbl;
87
88         idx = _mm_cvtsi128_si64(i24);
89
90         ptbl = (const uint32_t *)&lpm->tbl24[(uint32_t)idx];
91         tbl[2] = *ptbl;
92         ptbl = (const uint32_t *)&lpm->tbl24[idx >> 32];
93         tbl[3] = *ptbl;
94
95         /* get 4 indexes for tbl8[]. */
96         i8.x = _mm_and_si128(ip, mask8);
97
98         pt = (uint64_t)tbl[0] |
99                 (uint64_t)tbl[1] << 32;
100         pt2 = (uint64_t)tbl[2] |
101                 (uint64_t)tbl[3] << 32;
102
103         /* search successfully finished for all 4 IP addresses. */
104         if (likely((pt & mask_xv) == mask_v) &&
105                         likely((pt2 & mask_xv) == mask_v)) {
106                 *(uint64_t *)hop = pt & RTE_LPM_MASKX4_RES;
107                 *(uint64_t *)(hop + 2) = pt2 & RTE_LPM_MASKX4_RES;
108                 return;
109         }
110
111         if (unlikely((pt & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
112                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
113                 i8.u32[0] = i8.u32[0] +
114                         (uint8_t)tbl[0] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
115                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[0]];
116                 tbl[0] = *ptbl;
117         }
118         if (unlikely((pt >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
119                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
120                 i8.u32[1] = i8.u32[1] +
121                         (uint8_t)tbl[1] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
122                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[1]];
123                 tbl[1] = *ptbl;
124         }
125         if (unlikely((pt2 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
126                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
127                 i8.u32[2] = i8.u32[2] +
128                         (uint8_t)tbl[2] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
129                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[2]];
130                 tbl[2] = *ptbl;
131         }
132         if (unlikely((pt2 >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
133                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
134                 i8.u32[3] = i8.u32[3] +
135                         (uint8_t)tbl[3] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
136                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[3]];
137                 tbl[3] = *ptbl;
138         }
139
140         hop[0] = (tbl[0] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[0] & 0x00FFFFFF : defv;
141         hop[1] = (tbl[1] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[1] & 0x00FFFFFF : defv;
142         hop[2] = (tbl[2] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[2] & 0x00FFFFFF : defv;
143         hop[3] = (tbl[3] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[3] & 0x00FFFFFF : defv;
144 }
145
146 #ifdef __cplusplus
147 }
148 #endif
149
150 #endif /* _RTE_LPM_SSE_H_ */