4 * Copyright(c) 2015 Cavium, Inc. 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, 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.
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)
56 "crc32cb %w[crc], %w[crc], %w[value]"
57 : [crc] "+r" (init_val)
58 : [value] "r" (data));
62 static inline uint32_t
63 crc32c_arm64_u16(uint16_t data, uint32_t init_val)
66 "crc32ch %w[crc], %w[crc], %w[value]"
67 : [crc] "+r" (init_val)
68 : [value] "r" (data));
72 static inline uint32_t
73 crc32c_arm64_u32(uint32_t data, uint32_t init_val)
76 "crc32cw %w[crc], %w[crc], %w[value]"
77 : [crc] "+r" (init_val)
78 : [value] "r" (data));
82 static inline uint32_t
83 crc32c_arm64_u64(uint64_t data, uint32_t init_val)
86 "crc32cx %w[crc], %w[crc], %x[value]"
87 : [crc] "+r" (init_val)
88 : [value] "r" (data));
93 * Allow or disallow use of arm64 SIMD instrinsics for CRC32 hash
97 * An OR of following flags:
98 * - (CRC32_SW) Don't use arm64 crc intrinsics
99 * - (CRC32_ARM64) Use ARMv8 CRC intrinsic if available
103 rte_hash_crc_set_alg(uint8_t alg)
107 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
118 /* Setting the best available algorithm */
119 static inline void __attribute__((constructor))
120 rte_hash_crc_init_alg(void)
122 rte_hash_crc_set_alg(CRC32_ARM64);
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
131 * Data to perform hash on.
133 * Value to initialise hash generator.
135 * 32bit calculated hash value.
137 static inline uint32_t
138 rte_hash_crc_1byte(uint8_t data, uint32_t init_val)
140 if (likely(crc32_alg & CRC32_ARM64))
141 return crc32c_arm64_u8(data, init_val);
143 return crc32c_1byte(data, init_val);
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
152 * Data to perform hash on.
154 * Value to initialise hash generator.
156 * 32bit calculated hash value.
158 static inline uint32_t
159 rte_hash_crc_2byte(uint16_t data, uint32_t init_val)
161 if (likely(crc32_alg & CRC32_ARM64))
162 return crc32c_arm64_u16(data, init_val);
164 return crc32c_2bytes(data, init_val);
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
173 * Data to perform hash on.
175 * Value to initialise hash generator.
177 * 32bit calculated hash value.
179 static inline uint32_t
180 rte_hash_crc_4byte(uint32_t data, uint32_t init_val)
182 if (likely(crc32_alg & CRC32_ARM64))
183 return crc32c_arm64_u32(data, init_val);
185 return crc32c_1word(data, init_val);
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
194 * Data to perform hash on.
196 * Value to initialise hash generator.
198 * 32bit calculated hash value.
200 static inline uint32_t
201 rte_hash_crc_8byte(uint64_t data, uint32_t init_val)
203 if (likely(crc32_alg == CRC32_ARM64))
204 return crc32c_arm64_u64(data, init_val);
206 return crc32c_2words(data, init_val);
213 #endif /* _RTE_CRC_ARM64_H_ */