ruxla-kernels 0.1.0

Internal crate of ruxla (use the `ruxla` crate): semantics of ruxla's operations, shared by the native backend and the IR interpreter
Documentation
//! The hash of JAX's random numbers.

/// The rotations of the rounds (alternating).
pub const ROTATIONS: [[u32; 4]; 2] = [[13, 15, 26, 6], [17, 29, 16, 24]];

/// The threefry2x32 hash (20 rounds) of the counters `(x1, x2)` with the
/// key `(k1, k2)`, as in `jax.random`.
///
/// # Examples
///
/// ```
/// // A known answer of the Random123 test vectors.
/// let (a, b) = ruxla_kernels::random::threefry2x32(0x13198a2e, 0x03707344, 0x243f6a88, 0x85a308d3);
/// assert_eq!((a, b), (0xc4923a9c, 0x483df7a0));
/// ```
#[inline]
pub fn threefry2x32(k1: u32, k2: u32, x1: u32, x2: u32) -> (u32, u32) {
    let ks = [k1, k2, k1 ^ k2 ^ 0x1BD1_1BDA];
    let mut x = [x1.wrapping_add(ks[0]), x2.wrapping_add(ks[1])];
    for i in 0..5 {
        for &r in &ROTATIONS[i % 2] {
            x[0] = x[0].wrapping_add(x[1]);
            x[1] = x[1].rotate_left(r) ^ x[0];
        }
        x[0] = x[0].wrapping_add(ks[(i + 1) % 3]);
        x[1] = x[1]
            .wrapping_add(ks[(i + 2) % 3])
            .wrapping_add(i as u32 + 1);
    }
    (x[0], x[1])
}