44935a69f458a82d72d1f70921ee5d59c706f1a6
[deb_dpdk.git] / lib / librte_distributor / rte_distributor_match_sse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
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 Intel 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 #include <rte_mbuf.h>
34 #include "rte_distributor_private.h"
35 #include "rte_distributor.h"
36 #include "smmintrin.h"
37 #include "nmmintrin.h"
38
39
40 void
41 find_match_vec(struct rte_distributor *d,
42                         uint16_t *data_ptr,
43                         uint16_t *output_ptr)
44 {
45         /* Setup */
46         __m128i incoming_fids;
47         __m128i inflight_fids;
48         __m128i preflight_fids;
49         __m128i wkr;
50         __m128i mask1;
51         __m128i mask2;
52         __m128i output;
53         struct rte_distributor_backlog *bl;
54         uint16_t i;
55
56         /*
57          * Function overview:
58          * 2. Loop through all worker ID's
59          *  2a. Load the current inflights for that worker into an xmm reg
60          *  2b. Load the current backlog for that worker into an xmm reg
61          *  2c. use cmpestrm to intersect flow_ids with backlog and inflights
62          *  2d. Add any matches to the output
63          * 3. Write the output xmm (matching worker ids).
64          */
65
66
67         output = _mm_set1_epi16(0);
68         incoming_fids = _mm_load_si128((__m128i *)data_ptr);
69
70         for (i = 0; i < d->num_workers; i++) {
71                 bl = &d->backlog[i];
72
73                 inflight_fids =
74                         _mm_load_si128((__m128i *)&(d->in_flight_tags[i]));
75                 preflight_fids =
76                         _mm_load_si128((__m128i *)(bl->tags));
77
78                 /*
79                  * Any incoming_fid that exists anywhere in inflight_fids will
80                  * have 0xffff in same position of the mask as the incoming fid
81                  * Example (shortened to bytes for brevity):
82                  * incoming_fids   0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08
83                  * inflight_fids   0x03 0x05 0x07 0x00 0x00 0x00 0x00 0x00
84                  * mask            0x00 0x00 0xff 0x00 0xff 0x00 0xff 0x00
85                  */
86
87                 mask1 = _mm_cmpestrm(inflight_fids, 8, incoming_fids, 8,
88                         _SIDD_UWORD_OPS |
89                         _SIDD_CMP_EQUAL_ANY |
90                         _SIDD_UNIT_MASK);
91                 mask2 = _mm_cmpestrm(preflight_fids, 8, incoming_fids, 8,
92                         _SIDD_UWORD_OPS |
93                         _SIDD_CMP_EQUAL_ANY |
94                         _SIDD_UNIT_MASK);
95
96                 mask1 = _mm_or_si128(mask1, mask2);
97                 /*
98                  * Now mask contains 0xffff where there's a match.
99                  * Next we need to store the worker_id in the relevant position
100                  * in the output.
101                  */
102
103                 wkr = _mm_set1_epi16(i+1);
104                 mask1 = _mm_and_si128(mask1, wkr);
105                 output = _mm_or_si128(mask1, output);
106         }
107
108         /*
109          * At this stage, the output 128-bit contains 8 16-bit values, with
110          * each non-zero value containing the worker ID on which the
111          * corresponding flow is pinned to.
112          */
113         _mm_store_si128((__m128i *)output_ptr, output);
114 }