xref: /aosp_15_r20/external/webrtc/third_party/crc32c/src/src/crc32c_read_le.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 // Copyright 2017 The CRC32C Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 
5 #ifndef CRC32C_CRC32C_READ_LE_H_
6 #define CRC32C_CRC32C_READ_LE_H_
7 
8 #include <cstdint>
9 #include <cstring>
10 
11 #include "crc32c/crc32c_config.h"
12 
13 namespace crc32c {
14 
15 // Reads a little-endian 32-bit integer from a 32-bit-aligned buffer.
ReadUint32LE(const uint8_t * buffer)16 inline uint32_t ReadUint32LE(const uint8_t* buffer) {
17 #if BYTE_ORDER_BIG_ENDIAN
18   return ((static_cast<uint32_t>(static_cast<uint8_t>(buffer[0]))) |
19           (static_cast<uint32_t>(static_cast<uint8_t>(buffer[1])) << 8) |
20           (static_cast<uint32_t>(static_cast<uint8_t>(buffer[2])) << 16) |
21           (static_cast<uint32_t>(static_cast<uint8_t>(buffer[3])) << 24));
22 #else   // !BYTE_ORDER_BIG_ENDIAN
23   uint32_t result;
24   // This should be optimized to a single instruction.
25   std::memcpy(&result, buffer, sizeof(result));
26   return result;
27 #endif  // BYTE_ORDER_BIG_ENDIAN
28 }
29 
30 // Reads a little-endian 64-bit integer from a 64-bit-aligned buffer.
ReadUint64LE(const uint8_t * buffer)31 inline uint64_t ReadUint64LE(const uint8_t* buffer) {
32 #if BYTE_ORDER_BIG_ENDIAN
33   return ((static_cast<uint64_t>(static_cast<uint8_t>(buffer[0]))) |
34           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[1])) << 8) |
35           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[2])) << 16) |
36           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[3])) << 24) |
37           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[4])) << 32) |
38           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[5])) << 40) |
39           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[6])) << 48) |
40           (static_cast<uint64_t>(static_cast<uint8_t>(buffer[7])) << 56));
41 #else   // !BYTE_ORDER_BIG_ENDIAN
42   uint64_t result;
43   // This should be optimized to a single instruction.
44   std::memcpy(&result, buffer, sizeof(result));
45   return result;
46 #endif  // BYTE_ORDER_BIG_ENDIAN
47 }
48 
49 }  // namespace crc32c
50 
51 #endif  // CRC32C_CRC32C_READ_LE_H_
52