774428be0a5e58a5dffd2cb9d394e0b39311e54f
[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 static inline void __attribute__((constructor))
120 rte_hash_crc_init_alg(void)
121 {
122         rte_hash_crc_set_alg(CRC32_ARM64);
123 }
124
125 /**
126  * Use single crc32 instruction to perform a hash on a 1 byte value.
127  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
128  * not supported
129  *
130  * @param data
131  *   Data to perform hash on.
132  * @param init_val
133  *   Value to initialise hash generator.
134  * @return
135  *   32bit calculated hash value.
136  */
137 static inline uint32_t
138 rte_hash_crc_1byte(uint8_t data, uint32_t init_val)
139 {
140         if (likely(crc32_alg & CRC32_ARM64))
141                 return crc32c_arm64_u8(data, init_val);
142
143         return crc32c_1byte(data, init_val);
144 }
145
146 /**
147  * Use single crc32 instruction to perform a hash on a 2 bytes value.
148  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
149  * not supported
150  *
151  * @param data
152  *   Data to perform hash on.
153  * @param init_val
154  *   Value to initialise hash generator.
155  * @return
156  *   32bit calculated hash value.
157  */
158 static inline uint32_t
159 rte_hash_crc_2byte(uint16_t data, uint32_t init_val)
160 {
161         if (likely(crc32_alg & CRC32_ARM64))
162                 return crc32c_arm64_u16(data, init_val);
163
164         return crc32c_2bytes(data, init_val);
165 }
166
167 /**
168  * Use single crc32 instruction to perform a hash on a 4 byte value.
169  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
170  * not supported
171  *
172  * @param data
173  *   Data to perform hash on.
174  * @param init_val
175  *   Value to initialise hash generator.
176  * @return
177  *   32bit calculated hash value.
178  */
179 static inline uint32_t
180 rte_hash_crc_4byte(uint32_t data, uint32_t init_val)
181 {
182         if (likely(crc32_alg & CRC32_ARM64))
183                 return crc32c_arm64_u32(data, init_val);
184
185         return crc32c_1word(data, init_val);
186 }
187
188 /**
189  * Use single crc32 instruction to perform a hash on a 8 byte value.
190  * Fall back to software crc32 implementation in case arm64 crc intrinsics is
191  * not supported
192  *
193  * @param data
194  *   Data to perform hash on.
195  * @param init_val
196  *   Value to initialise hash generator.
197  * @return
198  *   32bit calculated hash value.
199  */
200 static inline uint32_t
201 rte_hash_crc_8byte(uint64_t data, uint32_t init_val)
202 {
203         if (likely(crc32_alg == CRC32_ARM64))
204                 return crc32c_arm64_u64(data, init_val);
205
206         return crc32c_2words(data, init_val);
207 }
208
209 #ifdef __cplusplus
210 }
211 #endif
212
213 #endif /* _RTE_CRC_ARM64_H_ */