1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use libc::{c_int, c_uint, c_double};

#[repr(C)]
pub struct AVLFG {
	pub state: [c_uint; 64],
	pub index: c_int,
}

#[inline(always)]
pub unsafe fn av_lfg_get(ctx: *mut AVLFG) -> c_uint {
	(*ctx).state[((*ctx).index & 63) as usize] = (*ctx).state[(((*ctx).index - 24) & 63) as usize] + (*ctx).state[(((*ctx).index - 55) & 63) as usize];
	(*ctx).index += 1;

	(*ctx).state[(((*ctx).index - 1) & 63) as usize]
}

#[inline(always)]
pub unsafe fn av_mlfg_get(ctx: *mut AVLFG) -> c_uint {
	let a = (*ctx).state[(((*ctx).index - 55) & 63) as usize];
	let b = (*ctx).state[(((*ctx).index - 24) & 63) as usize];
	let c = 2 * a * b + a + b;

	(*ctx).index += 1;
	(*ctx).state[(((*ctx).index - 1) & 63) as usize] = c;

	c
}

extern {
	pub fn av_lfg_init(c: *mut AVLFG, seed: c_uint);
	pub fn av_bmg_get(lfg: *mut AVLFG, out: *mut c_double);
}