New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_hash / rte_crc_arm64.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Cavium, Inc. 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 Cavium, Inc 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_CRC_ARM64_H_
35 #define _RTE_CRC_ARM64_H_
36
37 /**
38  * @file
39  *
40  * RTE CRC arm64 Hash
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <stdint.h>
48 #include <rte_cpuflags.h>
49 #include <rte_branch_prediction.h>
50 #include <rte_common.h>
51
52 static inline uint32_t
53 crc32c_arm64_u8(uint8_t data, uint32_t init_val)
54 {
55         __asm__ volatile(
56                         "crc32cb %w[crc], %w[crc], %w[value]"
57                         : [crc] "+r" (init_val)
58                         : [value] "r" (data));
59         return init_val;
60 }
61
62 static inline uint32_t
63 crc32c_arm64_u16(uint16_t data, uint32_t init_val)
64 {
65         __asm__ volatile(
66                         "crc32ch %w[crc], %w[crc], %w[value]"
67                         : [crc] "+r" (init_val)
68                         : [value] "r" (data));
69         return init_val;
70 }
71
72 static inline uint32_t
73 crc32c_arm64_u32(uint32_t data, uint32_t init_val)
74 {
75         __asm__ volatile(
76                         "crc32cw %w[crc], %w[crc], %w[value]"
77                         : [crc] "+r" (init_val)
78                         : [value] "r" (data));
79         return init_val;
80 }
81
82 static inline uint32_t
83 crc32c_arm64_u64(uint64_t data, uint32_t init_val)
84 {
85         __asm__ volatile(
86                         "crc32cx %w[crc], %w[crc], %x[value]"
87                         : [crc] "+r" (init_val)
88                         : [value] "r" (data));
89         return init_val;
90 }
91
92 /**
93  * Allow or disallow use of arm64 SIMD instrinsics for CRC32 hash
94  * calculation.
95  *
96  * @param alg
97  *   An OR of following flags:
98  *   - (CRC32_SW) Don't use arm64 crc intrinsics
99  *   - (CRC32_ARM64) Use ARMv8 CRC intrinsic if available
100  *
101  */
102 static inline void
103 rte_hash_crc_set_alg(uint8_t alg)
104 {
105         switch (alg) {
106         case CRC32_ARM64:
107                 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
108                         alg = CRC32_SW;
109                 /* fall-through */
110         case CRC32_SW:
111                 crc32_alg = alg;
112                 /* fall-through */
113         default:
114                 break;
115         }
116 }
117
118 /* Setting the best available algorithm */
119 RTE_INIT(rte_hash_crc_init_alg)
120 {
121         rte_hash_crc_set_alg(CRC32_ARM64);
122 }
123
124 /**
125  * Use single crc32 instruction to perform a hash on a 1 byte value.
126  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
127  * not supported
128  *
129  * @param data
130  *   Data to perform hash on.
131  * @param init_val
132  *   Value to initialise hash generator.
133  * @return
134  *   32bit calculated hash value.
135  */
136 static inline uint32_t
137 rte_hash_crc_1byte(uint8_t data, uint32_t init_val)
138 {
139         if (likely(crc32_alg & CRC32_ARM64))
140                 return crc32c_arm64_u8(data, init_val);
141
142         return crc32c_1byte(data, init_val);
143 }
144
145 /**
146  * Use single crc32 instruction to perform a hash on a 2 bytes value.
147  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
148  * not supported
149  *
150  * @param data
151  *   Data to perform hash on.
152  * @param init_val
153  *   Value to initialise hash generator.
154  * @return
155  *   32bit calculated hash value.
156  */
157 static inline uint32_t
158 rte_hash_crc_2byte(uint16_t data, uint32_t init_val)
159 {
160         if (likely(crc32_alg & CRC32_ARM64))
161                 return crc32c_arm64_u16(data, init_val);
162
163         return crc32c_2bytes(data, init_val);
164 }
165
166 /**
167  * Use single crc32 instruction to perform a hash on a 4 byte value.
168  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
169  * not supported
170  *
171  * @param data
172  *   Data to perform hash on.
173  * @param init_val
174  *   Value to initialise hash generator.
175  * @return
176  *   32bit calculated hash value.
177  */
178 static inline uint32_t
179 rte_hash_crc_4byte(uint32_t data, uint32_t init_val)
180 {
181         if (likely(crc32_alg & CRC32_ARM64))
182                 return crc32c_arm64_u32(data, init_val);
183
184         return crc32c_1word(data, init_val);
185 }
186
187 /**
188  * Use single crc32 instruction to perform a hash on a 8 byte value.
189  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
190  * not supported
191  *
192  * @param data
193  *   Data to perform hash on.
194  * @param init_val
195  *   Value to initialise hash generator.
196  * @return
197  *   32bit calculated hash value.
198  */
199 static inline uint32_t
200 rte_hash_crc_8byte(uint64_t data, uint32_t init_val)
201 {
202         if (likely(crc32_alg == CRC32_ARM64))
203                 return crc32c_arm64_u64(data, init_val);
204
205         return crc32c_2words(data, init_val);
206 }
207
208 #ifdef __cplusplus
209 }
210 #endif
211
212 #endif /* _RTE_CRC_ARM64_H_ */