?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  ifndef included_rand
  2.  define included_rand
  3.  
  4. rand:
  5. ; Output is in HL
  6. ; This rand routine combines Patrik Rak's fantastic 32-bit xorshift
  7. ; (https://gist.github.com/raxoft/c074743ea3f926db0037) with a simple lcg for
  8. ; extra smoothing.
  9. ; It has a period of 281,474,976,645,120 (2^48-2^16) and uses 48 bits of state.
  10. ; 42 bytes
  11. ; 210cc
  12.   ld hl,(seed0)
  13.   ld b,h
  14.   ld c,l
  15.   add hl,hl
  16.   add hl,hl
  17.   inc l
  18.   add hl,bc
  19.   ld (seed0),hl
  20.  
  21. ; xorshift
  22.   ld hl,(seed1)     ; yw -> zt
  23.   ld de,(seed1+2)   ; xz -> yw
  24.   ld (seed1+2),hl   ; x = y, z = w
  25.   ld a,l            ; w = w ^ ( w << 3 )
  26.   add a,a
  27.   add a,a
  28.   add a,a
  29.   xor l
  30.   ld l,a
  31.   ld a,d         ; t = x ^ (x << 1)
  32.   add a,a
  33.   xor d
  34.   ld h,a
  35.   rra            ; t = t ^ (t >> 1) ^ w
  36.   xor h
  37.   xor l
  38.   ld h,e         ; y = z
  39.   ld l,a         ; w = t
  40.   ld (seed1),hl
  41.  
  42. ; Mix the xorshift and the lcg
  43.   add hl,bc
  44.   ret
  45.  endif
  46.