pstore2
round2.hpp
Go to the documentation of this file.
1 //===- include/pstore/support/round2.hpp ------------------*- mode: C++ -*-===//
2 //* _ ____ *
3 //* _ __ ___ _ _ _ __ __| |___ \ *
4 //* | '__/ _ \| | | | '_ \ / _` | __) | *
5 //* | | | (_) | |_| | | | | (_| |/ __/ *
6 //* |_| \___/ \__,_|_| |_|\__,_|_____| *
7 //* *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
23 
24 #ifndef PSTORE_SUPPORT_ROUND2_HPP
25 #define PSTORE_SUPPORT_ROUND2_HPP
26 
27 #include <cstdint>
28 
29 namespace pstore {
30 
31  inline std::uint8_t round_to_power_of_2 (std::uint8_t v) noexcept {
32  --v;
33  v |= v >> 1;
34  v |= v >> 2;
35  v |= v >> 4;
36  ++v;
37  return v;
38  }
39 
40  inline std::uint16_t round_to_power_of_2 (std::uint16_t v) noexcept {
41  --v;
42  v |= v >> 1;
43  v |= v >> 2;
44  v |= v >> 4;
45  v |= v >> 8;
46  ++v;
47  return v;
48  }
49 
50  inline std::uint32_t round_to_power_of_2 (std::uint32_t v) noexcept {
51  --v;
52  v |= v >> 1;
53  v |= v >> 2;
54  v |= v >> 4;
55  v |= v >> 8;
56  v |= v >> 16;
57  ++v;
58  return v;
59  }
60 
61  inline std::uint64_t round_to_power_of_2 (std::uint64_t v) noexcept {
62  --v;
63  v |= v >> 1;
64  v |= v >> 2;
65  v |= v >> 4;
66  v |= v >> 8;
67  v |= v >> 16;
68  v |= v >> 32;
69  ++v;
70  return v;
71  }
72 
73 } // end namespace pstore
74 
75 #endif // PSTORE_SUPPORT_ROUND2_HPP
Definition: nonpod2.cpp:40