Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251}253// Returns the smallest address >= the given address that is aligned to N bytes.254//255// N must be a power of two.256template <int N>257constexpr inline const uint8_t* RoundUp(const uint8_t* pointer) {258return reinterpret_cast<uint8_t*>(259(reinterpret_cast<uintptr_t>(pointer) + (N - 1)) &260~static_cast<uintptr_t>(N - 1));261}263} // namespace265// Determine if the CPU running this program can accelerate the CRC32C266// calculation.267static bool CanAccelerateCRC32C() {268// port::AcceleretedCRC32C returns zero when unable to accelerate.269static const char kTestCRCBuffer[] = "TestCRCBuffer";270static const char kBufSize = sizeof(kTestCRCBuffer) - 1;271static const uint32_t kTestCRCValue = 0xdcbc59fa;273return port::AcceleratedCRC32C(0, kTestCRCBuffer, kBufSize) == kTestCRCValue;274}276uint32_t Extend(uint32_t crc, const char* data, size_t n) {277static bool accelerate = CanAccelerateCRC32C();278if (accelerate) {279return port::AcceleratedCRC32C(crc, data, n);280}282const uint8_t* p = reinterpret_cast<const uint8_t*>(data);283const uint8_t* e = p + n;284uint32_t l = crc ^ kCRC32Xor;286// Process one byte at a time.287#define STEP1 \288do { \289int c = (l & 0xff) ^ *p++; \290l = kByteExtensionTable[c] ^ (l >> 8); \291} while (0)293// Process one of the 4 strides of 4-byte data.294#define STEP4(s) \295do { \296crc##s = ReadUint32LE(p + s * 4) ^ kStrideExtensionTable3[crc##s & 0xff] ^ \297kStrideExtensionTable2[(crc##s >> 8) & 0xff] ^ \298kStrideExtensionTable1[(crc##s >> 16) & 0xff] ^ \299kStrideExtensionTable0[crc##s >> 24]; \300} while (0)302// Process a 16-byte swath of 4 strides, each of which has 4 bytes of data.303#define STEP16 \304do { \305STEP4(0); \306STEP4(1); \307STEP4(2); \308STEP4(3); \309p += 16; \310} while (0)312// Process 4 bytes that were already loaded into a word.313#define STEP4W(w) \314do { \315w ^= l; \316for (size_t i = 0; i < 4; ++i) { \317w = (w >> 8) ^ kByteExtensionTable[w & 0xff]; \318} \319l = w; \320} while (0)322// Point x at first 4-byte aligned byte in the buffer. This might be past the323// end of the buffer.324const uint8_t* x = RoundUp<4>(p);325if (x <= e) {326// Process bytes p is 4-byte aligned.327while (p != x) {328STEP1;329}330}332if ((e - p) >= 16) {333// Load a 16-byte swath into the stride partial results.334uint32_t crc0 = ReadUint32LE(p + 0 * 4) ^ l;335uint32_t crc1 = ReadUint32LE(p + 1 * 4);336uint32_t crc2 = ReadUint32LE(p + 2 * 4);337uint32_t crc3 = ReadUint32LE(p + 3 * 4);338p += 16;340// It is possible to get better speeds (at least on x86) by interleaving341// prefetching 256 bytes ahead with processing 64 bytes at a time. See the342// portable implementation in https://github.com/google/crc32c/.344// Process one 16-byte swath at a time.345while ((e - p) >= 16) {346STEP16;347}349// Advance one word at a time as far as possible.350while ((e - p) >= 4) {351STEP4(0);352uint32_t tmp = crc0;353crc0 = crc1;354crc1 = crc2;355crc2 = crc3;356crc3 = tmp;357p += 4;358}360// Combine the 4 partial stride results.361l = 0;362STEP4W(crc0);363STEP4W(crc1);364STEP4W(crc2);365STEP4W(crc3);366}368// Process the last few bytes.369while (p != e) {370STEP1;371}372#undef STEP4W373#undef STEP16374#undef STEP4375#undef STEP1376return l ^ kCRC32Xor;377}379} // namespace crc32c380} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.