use crate::utils::bit_reader_utils::{
BITS, VP8_LOG2_RANGE, VP8_NEW_RANGE, VP8BitReader, bit_t, range_t, vp8_load_final_bytes,
};
use crate::utils::utils::bits_log2_floor;
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32",
target_arch = "wasm64"
))]
#[expect(non_camel_case_types, reason = "C type name")]
pub type lbit_t = u64;
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "wasm32",
target_arch = "wasm64"
)))]
#[expect(non_camel_case_types, reason = "C type name")]
pub type lbit_t = u32;
pub const LBIT_BYTES: usize = size_of::<lbit_t>();
const LOAD_BYTES: usize = (BITS >> 3) as usize;
#[inline]
fn low_range(value: bit_t) -> range_t {
let bytes = value.to_le_bytes();
range_t::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "sizeof(lbit_t) is at most 8, which fits in an int"
)]
const LOAD_SHIFT: i32 = 8 * LBIT_BYTES as i32 - BITS;
#[inline]
pub fn vp8_load_new_bytes(br: &mut VP8BitReader, data: &[u8]) {
debug_assert!(br.has_buf); if br.buf < br.buf_max {
let mut in_bits: lbit_t = 0;
for &byte in &data[br.buf..br.buf + LBIT_BYTES] {
in_bits = (in_bits << 8) | lbit_t::from(byte);
}
br.buf += LOAD_BYTES;
let bits: bit_t = in_bits >> LOAD_SHIFT;
br.value = bits | (br.value << BITS);
br.bits += BITS;
} else {
vp8_load_final_bytes(br, data); }
}
#[inline]
#[expect(
clippy::cast_sign_loss,
reason = "C converts the int probability to uint32_t"
)]
pub fn vp8_get_bit(br: &mut VP8BitReader, data: &[u8], prob: i32) -> i32 {
let mut range = br.range;
if br.bits < 0 {
vp8_load_new_bytes(br, data);
}
let pos = br.bits;
let split = range.wrapping_mul(prob as range_t) >> 8;
let value = low_range(br.value >> pos);
let bit = value > split;
if bit {
range -= split;
br.value -= bit_t::from(split + 1) << pos;
} else {
range = split + 1;
}
let shift = 7 ^ bits_log2_floor(range);
range <<= shift;
br.bits -= shift;
br.range = range - 1;
i32::from(bit)
}
#[inline]
#[expect(
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
reason = "C reinterprets the unsigned difference as int32_t and back"
)]
pub fn vp8_get_signed(br: &mut VP8BitReader, data: &[u8], v: i32) -> i32 {
if br.bits < 0 {
vp8_load_new_bytes(br, data);
}
let pos = br.bits;
let split: range_t = br.range >> 1;
let value = low_range(br.value >> pos);
let mask = (split.wrapping_sub(value) as i32) >> 31;
br.bits -= 1;
br.range = br.range.wrapping_add(mask as range_t);
br.range |= 1;
br.value -= bit_t::from((split + 1) & (mask as range_t)) << pos;
(v ^ mask) - mask
}
#[inline]
#[expect(
clippy::cast_sign_loss,
reason = "C converts the int probability to uint32_t"
)]
pub fn vp8_get_bit_alt(br: &mut VP8BitReader, data: &[u8], prob: i32) -> i32 {
let mut range = br.range;
if br.bits < 0 {
vp8_load_new_bytes(br, data);
}
let pos = br.bits;
let split = range.wrapping_mul(prob as range_t) >> 8;
let value = low_range(br.value >> pos);
let bit;
if value > split {
range -= split + 1;
br.value -= bit_t::from(split + 1) << pos;
bit = 1;
} else {
range = split;
bit = 0;
}
if range <= 0x7e {
let shift = i32::from(VP8_LOG2_RANGE[range as usize]);
range = u32::from(VP8_NEW_RANGE[range as usize]);
br.bits -= shift;
}
br.range = range;
bit
}