Skip to content

Commit 9456905

Browse files
committed
JumpAhead for LFSR113
1 parent 46a84c4 commit 9456905

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,53 @@ impl RngCore for LFSR113 {
591591
Ok(self.fill_bytes(dest))
592592
}
593593
}
594+
impl RngJumpAhead for LFSR113 {
595+
fn jumpahead<N>(&mut self, n: N)
596+
where N: Unsigned + PrimInt
597+
{
598+
const LFSR113_Z1_MATRIX_ARRAY: [u32; 32] = [
599+
0x00000000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, 0x02000001,
600+
0x04000002, 0x08000004, 0x10000008, 0x20000010, 0x40000020, 0x80000041, 0x00000082, 0x00000104,
601+
0x00000208, 0x00000410, 0x00000820, 0x00001040, 0x00002080, 0x00004100, 0x00008200, 0x00010400,
602+
0x00020800, 0x00041000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000, 0x00040000
603+
];
604+
const LFSR113_Z2_MATRIX_ARRAY: [u32; 32] = [
605+
0x00000000, 0x00000000, 0x00000000, 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200,
606+
0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000,
607+
0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, 0x02000000,
608+
0x04000000, 0x08000001, 0x10000002, 0x20000005, 0x4000000A, 0x80000014, 0x00000008, 0x00000010
609+
];
610+
const LFSR113_Z3_MATRIX_ARRAY: [u32; 32] = [
611+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000800, 0x00001000, 0x00002000, 0x00004000,
612+
0x00008001, 0x00010002, 0x00020004, 0x00040008, 0x00080010, 0x00100020, 0x00200040, 0x00400080,
613+
0x00800100, 0x01000200, 0x02000400, 0x04000000, 0x08000000, 0x10000001, 0x20000002, 0x40000004,
614+
0x80000008, 0x00000010, 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400
615+
];
616+
const LFSR113_Z4_MATRIX_ARRAY: [u32; 32] = [
617+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00100000,
618+
0x00200000, 0x00400001, 0x00800002, 0x01000004, 0x02000009, 0x04000012, 0x08000024, 0x10000048,
619+
0x20000090, 0x40000120, 0x80000240, 0x00000480, 0x00000900, 0x00001200, 0x00002400, 0x00004800,
620+
0x00009000, 0x00012000, 0x00024000, 0x00048000, 0x00090000, 0x00020000, 0x00040000, 0x00080000
621+
];
622+
623+
self.sanitise_z1();
624+
let lfsr113_matrix = bitcolumnmatrix::BitColumnMatrix32::new(&LFSR113_Z1_MATRIX_ARRAY);
625+
let lfsr113_mult = lfsr113_matrix.pow(n);
626+
self.z1 = lfsr113_mult.dot_vec(self.z1);
627+
628+
self.sanitise_z2();
629+
let lfsr113_matrix = bitcolumnmatrix::BitColumnMatrix32::new(&LFSR113_Z2_MATRIX_ARRAY);
630+
let lfsr113_mult = lfsr113_matrix.pow(n);
631+
self.z2 = lfsr113_mult.dot_vec(self.z2);
632+
633+
self.sanitise_z3();
634+
let lfsr113_matrix = bitcolumnmatrix::BitColumnMatrix32::new(&LFSR113_Z3_MATRIX_ARRAY);
635+
let lfsr113_mult = lfsr113_matrix.pow(n);
636+
self.z3 = lfsr113_mult.dot_vec(self.z3);
637+
638+
self.sanitise_z4();
639+
let lfsr113_matrix = bitcolumnmatrix::BitColumnMatrix32::new(&LFSR113_Z4_MATRIX_ARRAY);
640+
let lfsr113_mult = lfsr113_matrix.pow(n);
641+
self.z4 = lfsr113_mult.dot_vec(self.z4);
642+
}
643+
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn test_new_and_next_u32() {
1212
//let mut s = simplerandom::KISS::new(1, 2, 3, 4);
1313
//let mut s = simplerandom::MWC64::new(1, 2);
1414
//let mut s = simplerandom::KISS2::new(1, 2, 3, 4);
15-
let mut s = simplerandom::LFSR88::new(1, 2, 3);
16-
//let mut s = simplerandom::LFSR113::new(1, 2, 3, 4);
15+
//let mut s = simplerandom::LFSR88::new(1, 2, 3);
16+
let mut s = simplerandom::LFSR113::new(1, 2, 3, 4);
1717
for _ in 0..4 {
1818
println!("{}, {:?}", s.next_u32(), s);
1919
//println!("{}, {:?}", s.gen::<u32>(), s);

0 commit comments

Comments
 (0)