Imported Upstream version 16.11
[deb_dpdk.git] / lib / librte_lpm / rte_lpm_altivec.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) IBM Corporation 2016.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of IBM Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifndef _RTE_LPM_ALTIVEC_H_
34 #define _RTE_LPM_ALTIVEC_H_
35
36 #include <rte_branch_prediction.h>
37 #include <rte_byteorder.h>
38 #include <rte_common.h>
39 #include <rte_vect.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 static inline void
46 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
47         uint32_t defv)
48 {
49         vector signed int i24;
50         rte_xmm_t i8;
51         uint32_t tbl[4];
52         uint64_t idx, pt, pt2;
53         const uint32_t *ptbl;
54
55         const uint32_t mask = UINT8_MAX;
56         const vector signed int mask8 = (xmm_t){mask, mask, mask, mask};
57
58         /*
59          * RTE_LPM_VALID_EXT_ENTRY_BITMASK for 2 LPM entries
60          * as one 64-bit value (0x0300000003000000).
61          */
62         const uint64_t mask_xv =
63                 ((uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK |
64                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 32);
65
66         /*
67          * RTE_LPM_LOOKUP_SUCCESS for 2 LPM entries
68          * as one 64-bit value (0x0100000001000000).
69          */
70         const uint64_t mask_v =
71                 ((uint64_t)RTE_LPM_LOOKUP_SUCCESS |
72                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 32);
73
74         /* get 4 indexes for tbl24[]. */
75         i24 = vec_sr((xmm_t) ip,
76                 (vector unsigned int){CHAR_BIT, CHAR_BIT, CHAR_BIT, CHAR_BIT});
77
78         /* extract values from tbl24[] */
79         idx = (uint32_t)i24[0];
80         idx = idx < (1<<24) ? idx : (1<<24)-1;
81         ptbl = (const uint32_t *)&lpm->tbl24[idx];
82         tbl[0] = *ptbl;
83
84         idx = (uint32_t) i24[1];
85         idx = idx < (1<<24) ? idx : (1<<24)-1;
86         ptbl = (const uint32_t *)&lpm->tbl24[idx];
87         tbl[1] = *ptbl;
88
89         idx = (uint32_t) i24[2];
90         idx = idx < (1<<24) ? idx : (1<<24)-1;
91         ptbl = (const uint32_t *)&lpm->tbl24[idx];
92         tbl[2] = *ptbl;
93
94         idx = (uint32_t) i24[3];
95         idx = idx < (1<<24) ? idx : (1<<24)-1;
96         ptbl = (const uint32_t *)&lpm->tbl24[idx];
97         tbl[3] = *ptbl;
98
99         /* get 4 indexes for tbl8[]. */
100         i8.x = vec_and(ip, mask8);
101
102         pt = (uint64_t)tbl[0] |
103                 (uint64_t)tbl[1] << 32;
104         pt2 = (uint64_t)tbl[2] |
105                 (uint64_t)tbl[3] << 32;
106
107         /* search successfully finished for all 4 IP addresses. */
108         if (likely((pt & mask_xv) == mask_v) &&
109                         likely((pt2 & mask_xv) == mask_v)) {
110                 *(uint64_t *)hop = pt & RTE_LPM_MASKX4_RES;
111                 *(uint64_t *)(hop + 2) = pt2 & RTE_LPM_MASKX4_RES;
112                 return;
113         }
114
115         if (unlikely((pt & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
116                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
117                 i8.u32[0] = i8.u32[0] +
118                         (uint8_t)tbl[0] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
119                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[0]];
120                 tbl[0] = *ptbl;
121         }
122         if (unlikely((pt >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
123                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
124                 i8.u32[1] = i8.u32[1] +
125                         (uint8_t)tbl[1] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
126                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[1]];
127                 tbl[1] = *ptbl;
128         }
129         if (unlikely((pt2 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
130                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
131                 i8.u32[2] = i8.u32[2] +
132                         (uint8_t)tbl[2] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
133                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[2]];
134                 tbl[2] = *ptbl;
135         }
136         if (unlikely((pt2 >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
137                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
138                 i8.u32[3] = i8.u32[3] +
139                         (uint8_t)tbl[3] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
140                 ptbl = (const uint32_t *)&lpm->tbl8[i8.u32[3]];
141                 tbl[3] = *ptbl;
142         }
143
144         hop[0] = (tbl[0] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[0] & 0x00FFFFFF : defv;
145         hop[1] = (tbl[1] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[1] & 0x00FFFFFF : defv;
146         hop[2] = (tbl[2] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[2] & 0x00FFFFFF : defv;
147         hop[3] = (tbl[3] & RTE_LPM_LOOKUP_SUCCESS) ? tbl[3] & 0x00FFFFFF : defv;
148 }
149
150 #ifdef __cplusplus
151 }
152 #endif
153
154 #endif /* _RTE_LPM_ALTIVEC_H_ */