Clang Love
[folly.git] / folly / Checksum.cpp
1 /*
2  * Copyright 2014 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 #include <folly/Checksum.h>
18 #include <algorithm>
19 #include <stdexcept>
20 #include <boost/crc.hpp>
21 #include <folly/CpuId.h>
22
23 namespace folly {
24
25 namespace detail {
26
27 #ifndef __has_builtin
28   #define __has_builtin(x) 0
29 #endif
30
31 #if (__has_builtin(__builtin_ia32_crc32qi) && \
32      __has_builtin(__builtin_ia32_crc32di)) || \
33     (FOLLY_X64 && defined(__GNUC__) && defined(__GNUC_MINOR__) && \
34      (((__GNUC__ * 100) + __GNUC_MINOR__) >= 407))
35
36 // Fast SIMD implementation of CRC-32C for x86 with SSE 4.2
37 uint32_t crc32c_hw(const uint8_t *data, size_t nbytes,
38     uint32_t startingChecksum) {
39   uint32_t sum = startingChecksum;
40   size_t offset = 0;
41
42   // Process bytes one at a time until we reach an 8-byte boundary and can
43   // start doing aligned 64-bit reads.
44   static uintptr_t ALIGN_MASK = sizeof(uint64_t) - 1;
45   size_t mask = (size_t)((uintptr_t)data & ALIGN_MASK);
46   if (mask != 0) {
47     size_t limit = std::min(nbytes, sizeof(uint64_t) - mask);
48     while (offset < limit) {
49       sum = (uint32_t)__builtin_ia32_crc32qi(sum, data[offset]);
50       offset++;
51     }
52   }
53
54   // Process 8 bytes at a time until we have fewer than 8 bytes left.
55   while (offset + sizeof(uint64_t) <= nbytes) {
56     const uint64_t* src = (const uint64_t*)(data + offset);
57     sum = __builtin_ia32_crc32di(sum, *src);
58     offset += sizeof(uint64_t);
59   }
60
61   // Process any bytes remaining after the last aligned 8-byte block.
62   while (offset < nbytes) {
63     sum = (uint32_t)__builtin_ia32_crc32qi(sum, data[offset]);
64     offset++;
65   }
66   return sum;
67 }
68
69 bool crc32c_hw_supported() {
70   static folly::CpuId id;
71   return id.sse42();
72 }
73
74 #else
75
76 uint32_t crc32c_hw(const uint8_t *data, size_t nbytes,
77     uint32_t startingChecksum) {
78   throw std::runtime_error("crc32_hw is not implemented on this platform");
79 }
80
81 bool crc32c_hw_supported() {
82   return false;
83 }
84
85 #endif
86
87 uint32_t crc32c_sw(const uint8_t *data, size_t nbytes,
88     uint32_t startingChecksum) {
89
90   // Reverse the bits in the starting checksum so they'll be in the
91   // right internal format for Boost's CRC engine.
92   //     O(1)-time, branchless bit reversal algorithm from
93   //     http://graphics.stanford.edu/~seander/bithacks.html
94   startingChecksum = ((startingChecksum >> 1) & 0x55555555) |
95       ((startingChecksum & 0x55555555) << 1);
96   startingChecksum = ((startingChecksum >> 2) & 0x33333333) |
97       ((startingChecksum & 0x33333333) << 2);
98   startingChecksum = ((startingChecksum >> 4) & 0x0f0f0f0f) |
99       ((startingChecksum & 0x0f0f0f0f) << 4);
100   startingChecksum = ((startingChecksum >> 8) & 0x00ff00ff) |
101       ((startingChecksum & 0x00ff00ff) << 8);
102   startingChecksum = (startingChecksum >> 16) |
103       (startingChecksum << 16);
104
105   static const uint32_t CRC32C_POLYNOMIAL = 0x1EDC6F41;
106   boost::crc_optimal<32, CRC32C_POLYNOMIAL, ~0U, 0, true, true> sum(
107       startingChecksum);
108   sum.process_bytes(data, nbytes);
109   return sum.checksum();
110 }
111
112 } // folly::detail
113
114 uint32_t crc32c(const uint8_t *data, size_t nbytes,
115     uint32_t startingChecksum) {
116   if (detail::crc32c_hw_supported()) {
117     return detail::crc32c_hw(data, nbytes, startingChecksum);
118   } else {
119     return detail::crc32c_sw(data, nbytes, startingChecksum);
120   }
121 }
122
123 } // folly