| 1 | // Derived from code in LLVM, which is: |
| 2 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | // See https://llvm.org/LICENSE.txt for license information. |
| 4 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | |
| 6 | pub(crate) fn align_to_power_of2(value: u64, align: u64) -> u64 { |
| 7 | assert!( |
| 8 | align != 0 && (align & (align - 1)) == 0, |
| 9 | "Align must be a power of 2" |
| 10 | ); |
| 11 | // Replace unary minus to avoid compilation error on Windows: |
| 12 | // "unary minus operator applied to unsigned type, result still unsigned" |
| 13 | let neg_align: u64 = !(align) + 1; |
| 14 | (value + align - 1) & neg_align |
| 15 | } |
| 16 | |