| 1 | /* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh |
| 2 | * Jagannathan, and Stephen Weeks. |
| 3 | * Copyright (C) 1997-2000 NEC Research Institute. |
| 4 | * |
| 5 | * MLton is released under a BSD-style license. |
| 6 | * See the file MLton-LICENSE for details. |
| 7 | */ |
| 8 | |
| 9 | static inline bool isAligned (size_t a, size_t b) { |
| 10 | return 0 == a % b; |
| 11 | } |
| 12 | |
| 13 | static inline bool isAlignedMax (uintmax_t a, uintmax_t b) { |
| 14 | return 0 == a % b; |
| 15 | } |
| 16 | |
| 17 | static inline size_t alignDown (size_t a, size_t b) { |
| 18 | assert (b >= 1 && b == (b & -b)); |
| 19 | a &= -b; |
| 20 | assert (isAligned (a, b)); |
| 21 | return a; |
| 22 | } |
| 23 | |
| 24 | static inline uintmax_t alignMaxDown (uintmax_t a, uintmax_t b) { |
| 25 | assert (b >= 1 && b == (b & -b)); |
| 26 | a &= -b; |
| 27 | assert (isAlignedMax (a, b)); |
| 28 | return a; |
| 29 | } |
| 30 | |
| 31 | static inline size_t align (size_t a, size_t b) { |
| 32 | assert (b >= 1 && b == (b & -b)); |
| 33 | a += b - 1; |
| 34 | a &= -b; |
| 35 | assert (isAligned (a, b)); |
| 36 | return a; |
| 37 | } |
| 38 | |
| 39 | static inline uintmax_t alignMax (uintmax_t a, uintmax_t b) { |
| 40 | assert (b >= 1 && b == (b & -b)); |
| 41 | a += b - 1; |
| 42 | a &= -b; |
| 43 | assert (isAlignedMax (a, b)); |
| 44 | return a; |
| 45 | } |