use crate::_auxlib as aux;
#[allow(non_snake_case, clippy::just_underscores_and_digits)]
pub fn has_ingest_capacity<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> bool {
let _0: Uint = Uint::zero();
let _1: Uint = Uint::one();
let surface_size_ok: bool = S > _1 && (S.count_ones() == 1);
let overflow_epsilon: Uint = aux::from_bool::<Uint>(
T.wrapping_add(&_1) < T, );
surface_size_ok
&& (_0
== aux::overflow_shr::<Uint>(
(T - overflow_epsilon) + _1, S - overflow_epsilon, ))
}
#[allow(non_snake_case, clippy::just_underscores_and_digits)]
pub fn _assign_storage_site<Uint: aux::UnsignedTrait>(
S: Uint, T: Uint, ) -> Uint {
let _0: Uint = Uint::zero();
let _1: Uint = Uint::one();
debug_assert!(aux::clz(S) >= _1); debug_assert!(has_ingest_capacity(S, T));
let s: Uint = aux::bit_length::<Uint>(S) - _1;
let t: Uint = aux::floor_subtract::<Uint>(
aux::bit_length::<Uint>(T), s, );
let h: Uint = aux::ctz::<Uint>(T.wrapping_add(&_1)); let i: Uint = aux::overflow_shr::<Uint>(T, h + _1);
let blt: Uint = aux::bit_length::<Uint>(t); let epsilon_tau: Uint = aux::from_bool::<Uint>(
(aux::bit_floor::<Uint>(t) << 1) > t + blt, );
let tau: Uint = blt - epsilon_tau;
let b_l: Uint = i;
let v: Uint = aux::bit_length::<Uint>(b_l);
let w: Uint = aux::shr::<Uint>(S, v) * aux::from_bool::<Uint>(v > _0);
let o: Uint = w >> 1; let p: Uint = b_l - aux::bit_floor::<Uint>(b_l);
let b_p: Uint = o + w * p;
let epsilon_k_b: Uint = aux::from_bool::<Uint>(b_l > _0);
let k_b: Uint = (b_p << 1)
+ aux::popcount::<Uint>((S << 1) - b_p).wrapping_sub(
&(_1 + epsilon_k_b), );
let b: Uint = std::cmp::max(aux::shr::<Uint>(S, tau + _1), _1);
if i >= b {
S } else {
k_b + h }
}
#[allow(non_snake_case)]
pub fn assign_storage_site<Uint: aux::UnsignedTrait>(
S: Uint, T: Uint, ) -> Option<Uint> {
let k = _assign_storage_site(S, T);
if k == S {
None
} else {
Some(k)
}
}
pub struct Algo;
#[allow(non_snake_case)]
impl crate::dstream::HasIngestCapacityTrait for Algo {
fn has_ingest_capacity<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> bool {
has_ingest_capacity::<Uint>(S, T)
}
}
#[allow(non_snake_case)]
impl crate::dstream::AssignStorageSiteTrait for Algo {
fn _assign_storage_site<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> Uint {
_assign_storage_site::<Uint>(S, T)
}
fn assign_storage_site<Uint: aux::UnsignedTrait>(S: Uint, T: Uint) -> Option<Uint> {
assign_storage_site::<Uint>(S, T)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_smoke_has_ingest_capacity() {
has_ingest_capacity::<u32>(16, 101);
}
#[test]
fn test_smoke_impl_assign_storage_site() {
_assign_storage_site::<u32>(16, 101);
}
#[test]
fn test_smoke_assign_storage_site() {
assign_storage_site::<u32>(16, 101);
}
}