518 |
518 |
519 #define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment))) |
519 #define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment))) |
520 |
520 |
521 #define is_aligned_(size, alignment) ((size) == (align_up_(size, alignment))) |
521 #define is_aligned_(size, alignment) ((size) == (align_up_(size, alignment))) |
522 |
522 |
|
523 // Temporary declaration until this file has been restructured. |
|
524 template <typename T> |
|
525 bool is_power_of_2_t(T x) { |
|
526 return (x != T(0)) && ((x & (x - 1)) == T(0)); |
|
527 } |
|
528 |
523 // Helpers to align sizes and check for alignment |
529 // Helpers to align sizes and check for alignment |
524 |
530 |
525 template <typename T, typename A> |
531 template <typename T, typename A> |
526 inline T align_up(T size, A alignment) { |
532 inline T align_up(T size, A alignment) { |
527 return align_up_(size, alignment); |
533 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment); |
|
534 |
|
535 T ret = align_up_(size, alignment); |
|
536 assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret); |
|
537 |
|
538 return ret; |
528 } |
539 } |
529 |
540 |
530 template <typename T, typename A> |
541 template <typename T, typename A> |
531 inline T align_down(T size, A alignment) { |
542 inline T align_down(T size, A alignment) { |
532 return align_down_(size, alignment); |
543 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment); |
|
544 |
|
545 T ret = align_down_(size, alignment); |
|
546 assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret); |
|
547 |
|
548 return ret; |
533 } |
549 } |
534 |
550 |
535 template <typename T, typename A> |
551 template <typename T, typename A> |
536 inline bool is_aligned(T size, A alignment) { |
552 inline bool is_aligned(T size, A alignment) { |
|
553 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment); |
|
554 |
537 return is_aligned_(size, alignment); |
555 return is_aligned_(size, alignment); |
538 } |
556 } |
539 |
557 |
540 // Align down with a lower bound. If the aligning results in 0, return 'alignment'. |
558 // Align down with a lower bound. If the aligning results in 0, return 'alignment'. |
541 template <typename T, typename A> |
559 template <typename T, typename A> |
1203 inline int exact_log2_long(jlong x) { |
1221 inline int exact_log2_long(jlong x) { |
1204 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); |
1222 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); |
1205 return log2_long(x); |
1223 return log2_long(x); |
1206 } |
1224 } |
1207 |
1225 |
1208 |
|
1209 // returns integer round-up to the nearest multiple of s (s must be a power of two) |
|
1210 inline intptr_t round_to(intptr_t x, uintx s) { |
|
1211 assert(is_power_of_2(s), "s must be a power of 2: " UINTX_FORMAT, s); |
|
1212 const uintx m = s - 1; |
|
1213 return mask_bits(x + m, ~m); |
|
1214 } |
|
1215 |
|
1216 // returns integer round-down to the nearest multiple of s (s must be a power of two) |
|
1217 inline intptr_t round_down(intptr_t x, uintx s) { |
|
1218 assert(is_power_of_2(s), "s must be a power of 2: " UINTX_FORMAT, s); |
|
1219 const uintx m = s - 1; |
|
1220 return mask_bits(x, ~m); |
|
1221 } |
|
1222 |
|
1223 |
|
1224 inline bool is_odd (intx x) { return x & 1; } |
1226 inline bool is_odd (intx x) { return x & 1; } |
1225 inline bool is_even(intx x) { return !is_odd(x); } |
1227 inline bool is_even(intx x) { return !is_odd(x); } |
1226 |
1228 |
1227 // "to" should be greater than "from." |
1229 // "to" should be greater than "from." |
1228 inline intx byte_size(void* from, void* to) { |
1230 inline intx byte_size(void* from, void* to) { |