1 : // Copyright 2011 Google Inc. All Rights Reserved.
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 : // http://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 : // Utility functions to align and test alignment.
16 :
17 : #ifndef SYZYGY_COMMON_ALIGN_H_
18 : #define SYZYGY_COMMON_ALIGN_H_
19 :
20 : #include "base/basictypes.h"
21 :
22 m : namespace common {
23 :
24 : // @tparam T Any type.
25 : // @param value The value to test.
26 : // @param pointer The pointer to test.
27 : // @returns true iff @p value or @p pointer is a (positive) power of two.
28 m : bool IsPowerOfTwo(size_t value);
29 m : template<typename T> bool IsPowerOfTwo(const T* pointer);
30 :
31 : // @tparam T Any type.
32 : // @param value The value to round up.
33 : // @param pointer The pointer to round up.
34 : // @param alignment the alignment boundary to round @p value up to.
35 : // @pre alignment != 0.
36 : // @returns @p value or @p pointer rounded up to the nearest higher multiple of
37 : // @p alignment.
38 m : size_t AlignUp(size_t value, size_t alignment);
39 m : template<typename T> T* AlignUp(T* pointer, size_t alignment);
40 m : template<typename T> const T* AlignUp(const T* pointer, size_t alignment);
41 :
42 : // @tparam T Any type.
43 : // @param value The value to round up.
44 : // @param pointer The pointer to round down.
45 : // @param alignment The alignment boundary to round @p value up to.
46 : // @pre alignment != 0.
47 : // @returns @p value or @p pointer rounded down to the nearest lower multiple of
48 : // @p alignment.
49 m : size_t AlignDown(size_t value, size_t alignment);
50 m : template<typename T> T* AlignDown(T* pointer, size_t alignment);
51 m : template<typename T> const T* AlignDown(const T* pointer, size_t alignment);
52 :
53 : // @tparam T Any type.
54 : // @param value The value to test.
55 : // @param pointer The pointer to test.
56 : // @param alignment The alignment boundary to test.
57 : // @pre alignment != 0.
58 : // @returns true iff value is an even multiple of alignment.
59 m : bool IsAligned(size_t value, size_t alignment);
60 m : template<typename T> bool IsAligned(const T* pointer, size_t alignment);
61 :
62 : // Determines the address alignment. If @p value or @p pointer is 0, the
63 : // maximum alignment for a 32-bit value is returned (0x80000000).
64 : // @tparam T Any type.
65 : // @param value The value for which to get the alignment.
66 : // @param pointer The pointer for which to get the alignment.
67 : // @returns the power of 2 on which @p value or @p pointer is aligned.
68 m : size_t GetAlignment(size_t value);
69 m : template<typename T> size_t GetAlignment(const T* pointer);
70 :
71 : // @tparam T Any type.
72 : // @param value An integer value to test.
73 : // @param pointer The pointer to test.
74 : // @returns true iff @p value or @p value is a power of two.
75 m : bool IsPowerOfTwo64(uint64 value);
76 :
77 : // @param value the value to round up.
78 : // @param alignment the alignment boundary to round @p value up to.
79 : // @pre alignment != 0.
80 : // @returns @p value rounded up to the nearest higher multiple of @p alignment.
81 m : uint64 AlignUp64(uint64 value, uint64 alignment);
82 :
83 : // @param value the value to round up.
84 : // @param alignment the alignment boundary to round @p value up to.
85 : // @pre alignment != 0.
86 : // @returns @p value rounded down to the nearest lower multiple of @p alignment.
87 m : uint64 AlignDown64(uint64 value, uint64 alignment);
88 :
89 : // @param value the value to test.
90 : // @param alignment the alignment boundary to test.
91 : // @pre alignment != 0.
92 : // @returns true iff value is an even multiple of alignment.
93 m : bool IsAligned64(uint64 value, uint64 alignment);
94 :
95 : // Determines the address alignment. If @p value or @p pointer is 0, the
96 : // maximum alignment for a 64-bit value is returned (1 << 63).
97 : // @param value The value for which to get the alignment.
98 : // @returns the power of 2 on which @p value is aligned.
99 m : uint64 GetAlignment64(uint64 value);
100 :
101 m : } // namespace common
102 :
103 : // Brings in the implementations of the templated functions.
104 : #include "syzygy/common/align_impl.h"
105 :
106 : #endif // SYZYGY_COMMON_ALIGN_H_
|