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