4 * Copyright(c) 2015 Cavium networks. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _RTE_CRC_ARM64_H_
35 #define _RTE_CRC_ARM64_H_
48 #include <rte_cpuflags.h>
49 #include <rte_branch_prediction.h>
50 #include <rte_common.h>
52 static inline uint32_t
53 crc32c_arm64_u8(uint8_t data, uint32_t init_val)
55 asm(".arch armv8-a+crc");
57 "crc32cb %w[crc], %w[crc], %w[value]"
58 : [crc] "+r" (init_val)
59 : [value] "r" (data));
63 static inline uint32_t
64 crc32c_arm64_u16(uint16_t data, uint32_t init_val)
66 asm(".arch armv8-a+crc");
68 "crc32ch %w[crc], %w[crc], %w[value]"
69 : [crc] "+r" (init_val)
70 : [value] "r" (data));
74 static inline uint32_t
75 crc32c_arm64_u32(uint32_t data, uint32_t init_val)
77 asm(".arch armv8-a+crc");
79 "crc32cw %w[crc], %w[crc], %w[value]"
80 : [crc] "+r" (init_val)
81 : [value] "r" (data));
85 static inline uint32_t
86 crc32c_arm64_u64(uint64_t data, uint32_t init_val)
88 asm(".arch armv8-a+crc");
90 "crc32cx %w[crc], %w[crc], %x[value]"
91 : [crc] "+r" (init_val)
92 : [value] "r" (data));
97 * Allow or disallow use of arm64 SIMD instrinsics for CRC32 hash
101 * An OR of following flags:
102 * - (CRC32_SW) Don't use arm64 crc intrinsics
103 * - (CRC32_ARM64) Use ARMv8 CRC intrinsic if available
107 rte_hash_crc_set_alg(uint8_t alg)
111 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
120 /* Setting the best available algorithm */
121 static inline void __attribute__((constructor))
122 rte_hash_crc_init_alg(void)
124 rte_hash_crc_set_alg(CRC32_ARM64);
128 * Use single crc32 instruction to perform a hash on a 1 byte value.
129 * Fall back to software crc32 implementation in case arm64 crc intrinsics is
133 * Data to perform hash on.
135 * Value to initialise hash generator.
137 * 32bit calculated hash value.
139 static inline uint32_t
140 rte_hash_crc_1byte(uint8_t data, uint32_t init_val)
142 if (likely(crc32_alg & CRC32_ARM64))
143 return crc32c_arm64_u8(data, init_val);
145 return crc32c_1byte(data, init_val);
149 * Use single crc32 instruction to perform a hash on a 2 bytes value.
150 * Fall back to software crc32 implementation in case arm64 crc intrinsics is
154 * Data to perform hash on.
156 * Value to initialise hash generator.
158 * 32bit calculated hash value.
160 static inline uint32_t
161 rte_hash_crc_2byte(uint16_t data, uint32_t init_val)
163 if (likely(crc32_alg & CRC32_ARM64))
164 return crc32c_arm64_u16(data, init_val);
166 return crc32c_2bytes(data, init_val);
170 * Use single crc32 instruction to perform a hash on a 4 byte value.
171 * Fall back to software crc32 implementation in case arm64 crc intrinsics is
175 * Data to perform hash on.
177 * Value to initialise hash generator.
179 * 32bit calculated hash value.
181 static inline uint32_t
182 rte_hash_crc_4byte(uint32_t data, uint32_t init_val)
184 if (likely(crc32_alg & CRC32_ARM64))
185 return crc32c_arm64_u32(data, init_val);
187 return crc32c_1word(data, init_val);
191 * Use single crc32 instruction to perform a hash on a 8 byte value.
192 * Fall back to software crc32 implementation in case arm64 crc intrinsics is
196 * Data to perform hash on.
198 * Value to initialise hash generator.
200 * 32bit calculated hash value.
202 static inline uint32_t
203 rte_hash_crc_8byte(uint64_t data, uint32_t init_val)
205 if (likely(crc32_alg == CRC32_ARM64))
206 return crc32c_arm64_u64(data, init_val);
208 return crc32c_2words(data, init_val);
215 #endif /* _RTE_CRC_ARM64_H_ */