/proc/self/cwd/internal/align.h
Line | Count | Source |
1 | | // Copyright 2024 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_INTERNAL_ALIGN_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_INTERNAL_ALIGN_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <type_traits> |
21 | | |
22 | | #include "absl/base/casts.h" |
23 | | #include "absl/base/config.h" |
24 | | #include "absl/base/macros.h" |
25 | | #include "absl/numeric/bits.h" |
26 | | |
27 | | namespace cel::internal { |
28 | | |
29 | | template <typename T> |
30 | | constexpr std::enable_if_t< |
31 | | std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>>, T> |
32 | | AlignmentMask(T alignment) { |
33 | | ABSL_ASSERT(absl::has_single_bit(alignment)); |
34 | | return alignment - T{1}; |
35 | | } |
36 | | |
37 | | template <typename T> |
38 | | std::enable_if_t<std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>>, |
39 | | T> |
40 | | AlignDown(T x, size_t alignment) { |
41 | | ABSL_ASSERT(absl::has_single_bit(alignment)); |
42 | | #if ABSL_HAVE_BUILTIN(__builtin_align_up) |
43 | | return __builtin_align_down(x, alignment); |
44 | | #else |
45 | | using C = std::common_type_t<T, size_t>; |
46 | | return static_cast<T>(static_cast<C>(x) & |
47 | | ~AlignmentMask(static_cast<C>(alignment))); |
48 | | #endif |
49 | | } |
50 | | |
51 | | template <typename T> |
52 | | std::enable_if_t<std::is_pointer_v<T>, T> AlignDown(T x, size_t alignment) { |
53 | | return absl::bit_cast<T>(AlignDown(absl::bit_cast<uintptr_t>(x), alignment)); |
54 | | } |
55 | | |
56 | | template <typename T> |
57 | | std::enable_if_t<std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>>, |
58 | | T> |
59 | 29.7k | AlignUp(T x, size_t alignment) { |
60 | 29.7k | ABSL_ASSERT(absl::has_single_bit(alignment)); |
61 | 29.7k | #if ABSL_HAVE_BUILTIN(__builtin_align_up) |
62 | 29.7k | return __builtin_align_up(x, alignment); |
63 | | #else |
64 | | using C = std::common_type_t<T, size_t>; |
65 | | return static_cast<T>(AlignDown( |
66 | | static_cast<C>(x) + AlignmentMask(static_cast<C>(alignment)), alignment)); |
67 | | #endif |
68 | 29.7k | } |
69 | | |
70 | | template <typename T> |
71 | | std::enable_if_t<std::is_pointer_v<T>, T> AlignUp(T x, size_t alignment) { |
72 | | return absl::bit_cast<T>(AlignUp(absl::bit_cast<uintptr_t>(x), alignment)); |
73 | | } |
74 | | |
75 | | template <typename T> |
76 | | constexpr std::enable_if_t< |
77 | | std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>>, bool> |
78 | | IsAligned(T x, size_t alignment) { |
79 | | ABSL_ASSERT(absl::has_single_bit(alignment)); |
80 | | #if ABSL_HAVE_BUILTIN(__builtin_is_aligned) |
81 | | return __builtin_is_aligned(x, alignment); |
82 | | #else |
83 | | using C = std::common_type_t<T, size_t>; |
84 | | return (static_cast<C>(x) & AlignmentMask(static_cast<C>(alignment))) == C{0}; |
85 | | #endif |
86 | | } |
87 | | |
88 | | template <typename T> |
89 | | std::enable_if_t<std::is_pointer_v<T>, bool> IsAligned(T x, size_t alignment) { |
90 | | return IsAligned(absl::bit_cast<uintptr_t>(x), alignment); |
91 | | } |
92 | | |
93 | | } // namespace cel::internal |
94 | | |
95 | | #endif // THIRD_PARTY_CEL_CPP_INTERNAL_ALIGN_H_ |