use std::sync::Arc;
use unityrs_core::limits::{ChargeError, Meter, MeteredVec, heap_cost};
pub(super) fn hash52(mut value: u32) -> u32 {
value ^= value >> 15;
value = value.wrapping_mul(0xeede_0891);
value ^= value >> 5;
value = value.wrapping_add(value << 16);
value ^= value >> 7;
value ^= value >> 3;
value ^= value << 6;
value ^= value >> 17;
value
}
pub(super) fn select(seed: u16, count: u8, x: u8, y: u8, small: bool) -> Option<u8> {
if seed >= 1024 || !(1..=4).contains(&count) || x >= 12 || y >= 12 {
return None;
}
let seed = u32::from(seed) + u32::from(count - 1) * 1024;
let random = hash52(seed);
let sh1 = if seed & 1 != 0 {
if seed & 2 != 0 { 4 } else { 5 }
} else if count == 3 {
6
} else {
5
};
let sh2 = if seed & 1 == 0 {
if seed & 2 != 0 { 4 } else { 5 }
} else if count == 3 {
6
} else {
5
};
let scale = if small { 2 } else { 1 };
let x = u32::from(x) * scale;
let y = u32::from(y) * scale;
let mut best_score = 0;
let mut best_partition = 0;
for partition in 0..count {
let shift = u32::from(partition) * 8;
let sx = (random >> shift) & 15;
let sy = (random >> (shift + 4)) & 15;
let bias_shift = 14 - 4 * u32::from(partition);
let score = (((sx * sx) >> sh1) * x + ((sy * sy) >> sh2) * y + (random >> bias_shift)) & 63;
if score > best_score {
best_score = score;
best_partition = partition;
}
}
Some(best_partition)
}
pub(super) struct PartitionTable {
width: u8,
height: u8,
data: MeteredVec<u8>,
at: [u16; ROWS],
}
const _: () = crate::send_sync::<PartitionTable>();
const ROWS: usize = 3 * 1024;
const UNBUILT: u16 = u16::MAX;
impl PartitionTable {
pub(super) fn charge_bytes(width: u8, height: u8) -> Option<u64> {
if !(4..=12).contains(&width) || !(4..=12).contains(&height) {
return None;
}
Some(heap_cost(3 * 1024 * u64::from(width) * u64::from(height)))
}
#[cfg(test)]
pub(super) fn new(
width: u8,
height: u8,
meter: &Arc<Meter>,
) -> Result<Option<Self>, ChargeError> {
if Self::charge_bytes(width, height).is_none() {
return Ok(None);
}
Self::alloc(width, height, meter).map(Some)
}
pub(super) fn alloc(width: u8, height: u8, meter: &Arc<Meter>) -> Result<Self, ChargeError> {
let len = ROWS * usize::from(width) * usize::from(height);
Ok(Self {
width,
height,
data: MeteredVec::with_capacity(meter, len)?,
at: [UNBUILT; ROWS],
})
}
pub(super) fn row(&mut self, seed: u16, count: u8) -> Option<&[u8]> {
if seed >= 1024 || !(2..=4).contains(&count) {
return None;
}
let texels = usize::from(self.width) * usize::from(self.height);
let at = self
.at
.get_mut(usize::from(count - 2) * 1024 + usize::from(seed))?;
if *at == UNBUILT {
let small = texels < 32;
let width = usize::from(self.width);
let mut row = [0; 144];
for (i, part) in row.iter_mut().take(texels).enumerate() {
let (x, y) = (u8::try_from(i % width).ok()?, u8::try_from(i / width).ok()?);
*part = select(seed, count, x, y, small)?;
}
*at = u16::try_from(self.data.len() / texels).ok()?;
self.data.extend_from_slice(row.get(..texels)?).ok()?;
}
let start = usize::from(*at) * texels;
self.data.as_slice().get(start..start + texels)
}
#[cfg(test)]
pub(super) fn get(&mut self, seed: u16, count: u8, x: u8, y: u8) -> Option<u8> {
if seed >= 1024 || !(1..=4).contains(&count) || x >= self.width || y >= self.height {
return None;
}
if count == 1 {
return Some(0);
}
let at = usize::from(y) * usize::from(self.width) + usize::from(x);
self.row(seed, count)?.get(at).copied()
}
}