base:16bit_xorshift_random_generator
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
base:16bit_xorshift_random_generator [2019-05-09 23:20] – created vsariola | base:16bit_xorshift_random_generator [2019-08-12 15:41] (current) – Standardized format of the heading ftc | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | === 16-bit " | + | ====== 16-bit " |
* original idea: [[https:// | * original idea: [[https:// | ||
* idea for fast 8-bit implementation: | * idea for fast 8-bit implementation: | ||
* ported by: Veikko Sariola | * ported by: Veikko Sariola | ||
- | Xorshift is a fast pseudorandom generator algorithm originally developed by [[https:// | + | Xorshift is a fast pseudorandom generator algorithm originally developed by [[https:// |
<code c> | <code c> | ||
/* 16-bit xorshift PRNG */ | /* 16-bit xorshift PRNG */ | ||
- | unsigned | + | unsigned |
unsigned xorshift( ) | unsigned xorshift( ) | ||
{ | { | ||
- | | + | |
- | | + | |
- | | + | |
- | return | + | return |
} | } | ||
</ | </ | ||
- | Here is an implementation for the C64. 38 cycles | + | Here is an implementation for the C64. 30 cycles without the RTS. |
<code asm> | <code asm> | ||
Line 31: | Line 32: | ||
STA rng_zp_high | STA rng_zp_high | ||
... | ... | ||
- | ; the RNG, you can get 8-bit random numbers in A, leaves | + | ; the RNG. You can get 8-bit random numbers in A or 16-bit numbers |
- | random | + | ; from the zero page addresses. Leaves |
+ | random | ||
LSR | LSR | ||
+ | LDA rng_zp_low | ||
+ | ROR | ||
EOR rng_zp_high | EOR rng_zp_high | ||
- | STA rng_zp_high | + | STA rng_zp_high ; high part of x ^= x << 7 done |
- | BCC @skip | + | |
- | LDA #%10000000 ; the highest bit of the low byte. | + | |
- | | + | |
- | STA rng_zp_low | + | |
- | @skip LDA rng_zp_high | + | |
- | LSR | + | |
EOR rng_zp_low | EOR rng_zp_low | ||
- | STA rng_zp_low | + | STA rng_zp_low |
- | EOR rng_zp_high ; x ^= x << 8 | + | EOR rng_zp_high |
- | STA rng_zp_high | + | STA rng_zp_high ; x ^= x << 8 done |
RTS | RTS | ||
</ | </ | ||
Line 52: | Line 51: | ||
{{: | {{: | ||
- | |||
base/16bit_xorshift_random_generator.1557436848.txt.gz · Last modified: 2019-05-09 23:20 by vsariola