Ludovic Mermod | be75da8 | 2025-09-17 11:13:28 +0200 | [diff] [blame^] | 1 | // SPDX-FileCopyrightText: Copyright The arm-tzc Contributors. |
| 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
| 4 | #[inline(always)] |
| 5 | /// Extracts from `value` the bits in the range `lsb..=end`. |
| 6 | pub(crate) const fn extract_bits(value: u32, msb: u32, lsb: u32) -> u32 { |
| 7 | (value >> lsb) & ((1 << (msb - lsb + 1)) - 1) |
| 8 | } |
| 9 | |
| 10 | #[inline(always)] |
| 11 | /// Extracts the bit at the given position. Equivalent to `extract_bits(value, position, position)`. |
| 12 | pub(crate) const fn get_bit(value: u32, position: usize) -> bool { |
| 13 | value >> position & 0b1 == 1 |
| 14 | } |
| 15 | |
| 16 | #[inline(always)] |
| 17 | /// Sets the bit at `position` to the given state. |
| 18 | pub(crate) const fn set_bit(value: &mut u32, position: usize, bit: bool) { |
| 19 | if bit { |
| 20 | *value |= 1 << position; |
| 21 | } else { |
| 22 | *value &= !(1 << position); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | #[inline(always)] |
| 27 | /// Performs a bitwise concatenation of two `u32` into a single `u64`. This is the opposite |
| 28 | /// operation of [`split_address`]. |
| 29 | pub(crate) const fn concatenate_address_parts(low: u32, high: u32) -> u64 { |
| 30 | (high as u64) << 32 | low as u64 |
| 31 | } |
| 32 | |
| 33 | #[inline(always)] |
| 34 | /// Perform a bitwise split of a `u64` into two `u32`. This is the opposite operation of |
| 35 | /// [`concatenate_address_parts`]. |
| 36 | pub(crate) fn split_address(address: u64) -> (u32, u32) { |
| 37 | ((address & u32::MAX as u64) as u32, (address >> 32) as u32) |
| 38 | } |