27
28 #include "utilities/globalDefinitions.hpp"
29
30 // Signed variants of alignment helpers. There are two versions of each, a macro
31 // for use in places like enum definitions that require compile-time constant
32 // expressions and a function for all other places so as to get type checking.
33
34 // Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
35 // 'alignment' is an unsigned int and 'what' is a wider type. The & operation
36 // will widen the inverted mask, and not sign extend it, leading to a mask with
37 // zeros in the most significant bits. The use of align_mask_widened() solves
38 // this problem.
39 #define align_mask(alignment) ((alignment) - 1)
40 #define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
41 #define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
42
43 #define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
44
45 #define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))
46
47 #define is_aligned_(size, alignment) ((size) == (align_up_((size), (alignment))))
48
49 // Temporary declaration until this file has been restructured.
50 template <typename T>
51 bool is_power_of_2_t(T x) {
52 return (x != T(0)) && ((x & (x - 1)) == T(0));
53 }
54
55 // Helpers to align sizes and check for alignment
56
57 template <typename T, typename A>
58 inline T align_up(T size, A alignment) {
59 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
60
61 T ret = align_up_(size, alignment);
62 assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
63
64 return ret;
65 }
66
67 template <typename T, typename A>
|
27
28 #include "utilities/globalDefinitions.hpp"
29
30 // Signed variants of alignment helpers. There are two versions of each, a macro
31 // for use in places like enum definitions that require compile-time constant
32 // expressions and a function for all other places so as to get type checking.
33
34 // Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
35 // 'alignment' is an unsigned int and 'what' is a wider type. The & operation
36 // will widen the inverted mask, and not sign extend it, leading to a mask with
37 // zeros in the most significant bits. The use of align_mask_widened() solves
38 // this problem.
39 #define align_mask(alignment) ((alignment) - 1)
40 #define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
41 #define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
42
43 #define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
44
45 #define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))
46
47 #define is_aligned_(size, alignment) (((size) & align_mask(alignment)) == 0)
48
49 // Temporary declaration until this file has been restructured.
50 template <typename T>
51 bool is_power_of_2_t(T x) {
52 return (x != T(0)) && ((x & (x - 1)) == T(0));
53 }
54
55 // Helpers to align sizes and check for alignment
56
57 template <typename T, typename A>
58 inline T align_up(T size, A alignment) {
59 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
60
61 T ret = align_up_(size, alignment);
62 assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
63
64 return ret;
65 }
66
67 template <typename T, typename A>
|