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