97ad45193feff0c22ebbec9129651f4747458383
[folly.git] / folly / detail / ChecksumDetail.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <immintrin.h>
20 #include <stdint.h>
21 #include <cstddef>
22
23 namespace folly { namespace detail {
24
25 /**
26  * Compute a CRC-32C checksum of a buffer using a hardware-accelerated
27  * implementation.
28  *
29  * @note This function is exposed to support special cases where the
30  *       calling code is absolutely certain it ought to invoke a hardware-
31  *       accelerated CRC-32C implementation - unit tests, for example.  For
32  *       all other scenarios, please call crc32c() and let it pick an
33  *       implementation based on the capabilities of the underlying CPU.
34  */
35 uint32_t crc32c_hw(const uint8_t* data, size_t nbytes,
36     uint32_t startingChecksum = ~0U);
37
38 /**
39  * Check whether a hardware-accelerated CRC-32C implementation is
40  * supported on the current CPU.
41  */
42 bool crc32c_hw_supported();
43
44 /**
45  * Compute a CRC-32C checksum of a buffer using a portable,
46  * software-only implementation.
47  *
48  * @note This function is exposed to support special cases where the
49  *       calling code is absolutely certain it wants to use the software
50  *       implementation instead of the hardware-accelerated code - unit
51  *       tests, for example.  For all other scenarios, please call crc32c()
52  *       and let it pick an implementation based on the capabilities of
53  *       the underlying CPU.
54  */
55 uint32_t crc32c_sw(const uint8_t* data, size_t nbytes,
56     uint32_t startingChecksum = ~0U);
57
58 /**
59  * Compute a CRC-32 checksum of a buffer using a hardware-accelerated
60  * implementation.
61  *
62  * @note This function is exposed to support special cases where the
63  *       calling code is absolutely certain it ought to invoke a hardware-
64  *       accelerated CRC-32 implementation - unit tests, for example.  For
65  *       all other scenarios, please call crc32() and let it pick an
66  *       implementation based on the capabilities of the underlying CPU.
67  */
68 uint32_t
69 crc32_hw(const uint8_t* data, size_t nbytes, uint32_t startingChecksum = ~0U);
70
71 uint32_t
72 crc32_hw_aligned(uint32_t remainder, const __m128i* p, size_t vec_count);
73
74 /**
75  * Check whether a hardware-accelerated CRC-32 implementation is
76  * supported on the current CPU.
77  */
78 bool crc32_hw_supported();
79
80 /**
81  * Compute a CRC-32 checksum of a buffer using a portable,
82  * software-only implementation.
83  *
84  * @note This function is exposed to support special cases where the
85  *       calling code is absolutely certain it wants to use the software
86  *       implementation instead of the hardware-accelerated code - unit
87  *       tests, for example.  For all other scenarios, please call crc32()
88  *       and let it pick an implementation based on the capabilities of
89  *       the underlying CPU.
90  */
91 uint32_t
92 crc32_sw(const uint8_t* data, size_t nbytes, uint32_t startingChecksum = ~0U);
93 }} // folly::detail