Skip to content

Instantly share code, notes, and snippets.

@spacejam
Created January 29, 2019 15:16
Show Gist options
  • Save spacejam/35bfd0279f58f58959f29453e397b490 to your computer and use it in GitHub Desktop.
Save spacejam/35bfd0279f58f58959f29453e397b490 to your computer and use it in GitHub Desktop.
pub struct Fib16 {
seed: u16,
lfsr: u16,
bit: u16,
}
impl Default for Fib16 {
fn default() -> Fib16 {
Fib16 {
seed: 0xACE1u16,
lfsr: 0xACE1u16,
bit: 0,
}
}
}
impl Fib16 {
fn from_seed(seed: u16) -> Fib16 {
Fib16 {
seed: seed,
lfsr: seed,
bit: 0,
}
}
}
impl Iterator for Fib16 {
type Item = u16;
fn next(&mut self) -> Option<u16> {
self.bit = ((self.lfsr >> 0) ^ (self.lfsr >> 2) ^ (self.lfsr >> 3) ^
(self.lfsr >> 5)) & 1;
self.lfsr = (self.lfsr >> 1) | (self.bit << 15);
if self.lfsr != self.seed {
Some(self.lfsr)
} else {
None
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment