Created
January 29, 2019 15:16
-
-
Save spacejam/35bfd0279f58f58959f29453e397b490 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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