Skip to content

Instantly share code, notes, and snippets.

@jar
Last active March 21, 2017 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jar/21db297b7e0a2cc836b101890bc72318 to your computer and use it in GitHub Desktop.
Save jar/21db297b7e0a2cc836b101890bc72318 to your computer and use it in GitHub Desktop.
A 32-bit floating pont random number generator [0,1) for the Adapteva Epiphany
/*
* A 32-bit floating pont random number generator [0,1) for the Adapteva Epiphany
* Based on http://simul.iro.umontreal.ca/rng/lfsr113.c
* A shortcut was taken for conversion of unsigned int to floating point so that
* half of the results are off by the least significant bit. This probably shouldn't
* be used for Monte Carlo simulation, but should still have a normal distribution
* and the underlying state remains the same.
* C Prototype: float randf(void);
*/
.section .text
.balign 4
.global _randf
_randf:
mov r3, %low(_state)
ldrd r0, [r3, #0]
lsl r2, r0, #6
eor r2, r2, r0
lsr r2, r2, #13
lsr r0, r0, #1
lsl r0, r0, #19
eor r0, r0, r2
lsl r2, r1, #2
eor r2, r2, r1
lsr r2, r2, #27
lsr r1, r1, #3
lsl r1, r1, #5
eor r1, r1, r2
strd r0, [r3, #0]
eor r0, r0, r1
ldr r1, [r3, #2]
lsl r2, r1, #13
eor r2, r2, r1
lsr r2, r2, #21
lsr r1, r1, #4
lsl r1, r1, #11
eor r1, r1, r2
str r1, [r3, #2]
eor r0, r0, r1
ldr r1, [r3, #3]
lsl r2, r1, #3
eor r2, r2, r1
lsr r2, r2, #12
lsr r1, r1, #7
lsl r1, r1, #20
eor r1, r1, r2
eor r0, r0, r1
lsr r0, r0, #1
str r1, [r3, #3]
float r0, r0
mov r1, #3
lsl r1, r1, #28
fmul r0, r0, r1
rts
.size _randf, .-_randf
.section .data
.balign 8
.type _state, @object
.size _state, 16
_state:
.word 987654321
.word 987654321
.word 987654321
.word 987654321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment