blob: 408c8eeccfe76d73f50199d7a0162d08bfd82946 [file] [log] [blame]
Ludovic Mermodbe75da82025-09-17 11:13:28 +02001// 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`.
6pub(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)`.
12pub(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.
18pub(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`].
29pub(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`].
36pub(crate) fn split_address(address: u64) -> (u32, u32) {
37 ((address & u32::MAX as u64) as u32, (address >> 32) as u32)
38}