oarust 0.20.1

GPU-first semantic computing runtime written in Rust
Documentation
struct SplitMix64 {
	state: u64,
}

impl SplitMix64 {
	const fn new(seed: u64) -> Self {
		Self { state: seed }
	}

	fn next_f32(&mut self) -> f32 {
		self.state = self.state.wrapping_add(0x9e37_79b9_7f4a_7c15);
		let mut value = self.state;
		value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
		value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
		value ^= value >> 31;
		let mantissa = (value >> 40) as u32;
		mantissa as f32 / 16_777_216.0
	}
}

pub(super) fn symmetric_uniform(length: usize, limit: f32, seed: u64) -> Vec<f32> {
	let mut random = SplitMix64::new(seed);
	(0..length)
		.map(|_| (random.next_f32() * 2.0 - 1.0) * limit)
		.collect()
}

pub(super) fn unit_uniform(length: usize, seed: u64) -> Vec<f32> {
	let mut random = SplitMix64::new(seed);
	(0..length).map(|_| random.next_f32()).collect()
}

/// Box-Muller standard-normal stream scaled by `stddev`, sharing the
/// SplitMix64 seed contract. Pairwise draws keep prefixes stable across
/// call lengths so constructor seeds stay comparable.
pub(super) fn normal(length: usize, stddev: f32, seed: u64) -> Vec<f32> {
	let mut random = SplitMix64::new(seed);
	(0..length.div_ceil(2))
		.flat_map(|_| {
			let u1 = random.next_f32().max(f32::MIN_POSITIVE);
			let u2 = random.next_f32();
			let radius = (-2.0 * u1.ln()).sqrt() * stddev;
			let angle = std::f32::consts::TAU * u2;
			[radius * angle.cos(), radius * angle.sin()]
		})
		.take(length)
		.collect()
}