use core::marker::PhantomData;
use crate::{
fbig::FBig,
repr::{Context, Repr, Word},
round::{mode, Round},
};
use dashu_base::EstimatedLog2;
use dashu_int::{
rand::{BitRng, UniformBelow, UniformBits},
DoubleWord, UBig,
};
pub struct UniformFBig<R: Round, const B: Word> {
pub(crate) sampler: Uniform01<B>,
pub(crate) scale: Repr<B>,
pub(crate) offset: Repr<B>,
pub(crate) _marker: PhantomData<R>,
}
impl<R: Round, const B: Word> UniformFBig<R, B> {
#[inline]
pub fn new(low: &FBig<R, B>, high: &FBig<R, B>, precision: usize) -> Self {
assert!(low <= high);
Self {
sampler: Uniform01::new(precision),
scale: (high - low).into_repr(),
offset: low.repr().clone(),
_marker: PhantomData,
}
}
#[inline]
pub fn new_inclusive(low: &FBig<R, B>, high: &FBig<R, B>, precision: usize) -> Self {
assert!(low <= high);
Self {
sampler: Uniform01::new_closed(precision),
scale: (high - low).into_repr(),
offset: low.repr().clone(),
_marker: PhantomData,
}
}
pub fn sample_fbig<BR: BitRng + ?Sized>(&self, rng: &mut BR) -> FBig<R, B> {
let unit: FBig<mode::Down, B> = self.sampler.sample01::<mode::Down, _>(rng);
let context = unit.context();
let scaled = context.mul(unit.repr(), &self.scale).value();
context
.add(scaled.repr(), &self.offset)
.value()
.with_rounding()
}
}
pub struct Uniform01<const BASE: Word> {
pub(crate) precision: usize,
pub(crate) range: Option<UBig>, pub(crate) include_zero: bool, pub(crate) include_one: bool, }
impl<const B: Word> Uniform01<B> {
#[inline]
pub fn new(precision: usize) -> Self {
let range = match B {
2 => None,
_ => Some(UBig::from_word(B).pow(precision)),
};
Self {
precision,
range,
include_zero: true,
include_one: false,
}
}
#[inline]
pub fn new_closed(precision: usize) -> Self {
let range = Some(UBig::from_word(B).pow(precision) + UBig::ONE);
Self {
precision,
range,
include_zero: true,
include_one: true,
}
}
#[inline]
pub fn new_open(precision: usize) -> Self {
let range = match B {
2 => None,
_ => Some(UBig::from_word(B).pow(precision) - UBig::ONE),
};
Self {
precision,
range,
include_zero: false,
include_one: false,
}
}
#[inline]
pub fn new_open_closed(precision: usize) -> Self {
let range = match B {
2 => None,
_ => Some(UBig::from_word(B).pow(precision)),
};
Self {
precision,
range,
include_zero: false,
include_one: true,
}
}
pub fn sample01<R: Round, BR: BitRng + ?Sized>(&self, rng: &mut BR) -> FBig<R, B> {
let repr = match (self.include_zero, self.include_one) {
(true, false) => {
let signif: UBig = if B == 2 {
UniformBits::new(self.precision).sample_ubig(rng)
} else {
UniformBelow::new(self.range.as_ref().unwrap()).sample_ubig(rng)
};
Repr::<B>::new(signif.into(), -(self.precision as isize))
}
(true, true) => {
let signif: UBig = UniformBelow::new(self.range.as_ref().unwrap()).sample_ubig(rng);
Repr::new(signif.into(), -(self.precision as isize))
}
(false, false) => {
let signif = if B == 2 {
loop {
let n: UBig = UniformBits::new(self.precision).sample_ubig(rng);
if !n.is_zero() {
break n;
}
}
} else {
let n: UBig = UniformBelow::new(self.range.as_ref().unwrap()).sample_ubig(rng);
n + UBig::ONE
};
Repr::<B>::new(signif.into(), -(self.precision as isize))
}
(false, true) => {
let signif: UBig = if B == 2 {
UniformBits::new(self.precision).sample_ubig(rng)
} else {
UniformBelow::new(self.range.as_ref().unwrap()).sample_ubig(rng)
};
Repr::<B>::new((signif + UBig::ONE).into(), -(self.precision as isize))
}
};
let context = Context::<mode::Down>::new(self.precision);
FBig::new(repr, context).with_rounding()
}
}
#[inline]
pub(crate) fn get_inline_precision<const B: Word>() -> usize {
(DoubleWord::BITS as f32 / B.log2_bounds().1) as _
}