xref: /aosp_15_r20/external/webrtc/third_party/crc32c/src/src/crc32c_round_up.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_ROUND_UP_H_
6 #define CRC32C_CRC32C_ROUND_UP_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 namespace crc32c {
12 
13 // Returns the smallest number >= the given number that is evenly divided by N.
14 //
15 // N must be a power of two.
16 template <int N>
RoundUp(uintptr_t pointer)17 constexpr inline uintptr_t RoundUp(uintptr_t pointer) {
18   static_assert((N & (N - 1)) == 0, "N must be a power of two");
19   return (pointer + (N - 1)) & ~(N - 1);
20 }
21 
22 // Returns the smallest address >= the given address that is aligned to N bytes.
23 //
24 // N must be a power of two.
25 template <int N>
RoundUp(const uint8_t * pointer)26 constexpr inline const uint8_t* RoundUp(const uint8_t* pointer) {
27   static_assert((N & (N - 1)) == 0, "N must be a power of two");
28   return reinterpret_cast<uint8_t*>(
29       RoundUp<N>(reinterpret_cast<uintptr_t>(pointer)));
30 }
31 
32 }  // namespace crc32c
33 
34 #endif  // CRC32C_CRC32C_ROUND_UP_H_
35