DPDK  17.08.2
rte_crc_arm64.h
Go to the documentation of this file.
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 
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 
102 static inline void
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 
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 
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 
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 
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_ */