Skip to main content

neura_program/
init.rs

1#[derive(Clone, Copy, PartialEq, Debug)]
2pub enum Init {
3    Zero,
4    Constant(f32),
5    Uniform { low: f32, high: f32 },
6}
7
8impl Init {
9    pub fn samples(self, elements: u32, entropy: &mut u32) -> Vec<f32> {
10        match self {
11            Self::Zero => vec![0.0; elements as usize],
12            Self::Constant(value) => vec![value; elements as usize],
13            Self::Uniform { low, high } => {
14                assert!(
15                    low < high,
16                    "a uniform init spans {low} to {high} without a range",
17                );
18                (0..elements)
19                    .map(|_| low + (high - low) * unit(entropy))
20                    .collect()
21            }
22        }
23    }
24}
25
26pub fn unit(entropy: &mut u32) -> f32 {
27    *entropy ^= *entropy << 13;
28    *entropy ^= *entropy >> 17;
29    *entropy ^= *entropy << 5;
30    (*entropy >> 8) as f32 / (1u32 << 24) as f32
31}