Fast RNG algorithm

PM development, programming, hacking and all that fun stuff.
Post Reply
zoranc
Posts: 298
Joined: August 13th, 2010, 18:13
Contact:

Fast RNG algorithm

Post by zoranc »

Just found a nice pseudo random number generator algorithm suitable for S1C88. It uses only 16 bit numbers and is very fast when implemented in assembly:

Code: Select all

uint16_t xs = 1; // initialised with a non-zero seed
xs ^= xs << 7
xs ^= xs >> 9
xs ^= xs << 8
Here is implementation for several processors including z80 which is very similar to S1C88: https://github.com/impomatic/xorshift798
User avatar
YasaSheep
Posts: 205
Joined: November 20th, 2005, 04:04
Location: Portland (Also the discord)
Contact:

Re: Fast RNG algorithm

Post by YasaSheep »

Nice! Here's a 36 cycle version in Epson syntax, best I could get:

Code: Select all

xorshift:
	LD HL,#XSHFT
	LD BA,[HL]
	RR B
	RR A
	XOR [HL],A
	LD BA,[HL]
	RR B
	XOR A,B
	XOR [HL],A
	INC HL
	LD [HL],A
	RET
xor is fairly limited (the only two register version is a,b) so I switched to xoring in memory where possible.
Post Reply