?login_element?

Subversion Repositories NedoOS

Rev

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

  1.  ifndef included_xrand
  2.  define included_xrand
  3.  include "../common/pushpop.asm"
  4.  include "../common/rand.asm"
  5.  include "../common/mov.asm"
  6.  include "constantsx.asm"
  7.  
  8. xrand:
  9. ;Stores a pseudo-random number on [0,1) with uniform distribution.
  10. ;speed: 312+pushpop+m*40+n*(67+rand)+4*rand
  11. ;          n is at least 1
  12. ;          m is the number of random bits that we had to generate
  13. ;          probability that n <= k is 1-65536^-k
  14. ;          probability that n == k is 65535*65536^-k
  15. ;          probability that m == k is 2^-k
  16. ;
  17. ;min: 2160cc
  18. ;max:~1056448cc  (absurdly unlikely to happen before heatdeath of the universe).
  19. ;avg: 2200.00595102cc
  20. ;85 bytes.
  21.  
  22.   call pushpop
  23.   push bc
  24.  
  25. ;The first thing we'll do is generate the exponent.
  26. ;Initialize to 0 (stored as 0x4000) and we'll generate
  27. ;random bits, decrementing the exponent until we get a 1.
  28.  
  29.   ld bc,$4000    ;exponent
  30.   jr xrand_gen_exp
  31. xrand_gen_exp0;_:
  32.   dec bc
  33.   add hl,hl
  34.   jr c,xrand_gen_expq;+_   ;we have completed calculation of the exponent
  35.   dec a
  36.   jr nz,xrand_gen_exp0;-_
  37.  
  38. ;make sure the exponent isn't zero!
  39.   ld a,b
  40.   or c
  41.   jr z,xrand_zero
  42.  
  43. xrand_gen_exp:
  44.   push bc
  45.   call rand
  46.   pop bc
  47.   ld a,16
  48.   jr xrand_gen_exp0;-_
  49. xrand_gen_expq;_:
  50.  
  51. ; Now we have generated the exponent, let's generate the mantissa
  52.   pop ix    ;pointer to the output
  53.   ld (ix+8),c
  54.   ld (ix+9),b
  55.  
  56.  
  57.   call rand
  58.   ld (ix),h
  59.   ld (ix+1),l
  60.  
  61.   call rand
  62.   ld (ix+2),h
  63.   ld (ix+3),l
  64.  
  65.  
  66.   call rand
  67.   ld (ix+4),l   ;it's "random" anyways, just fun to change it up :P
  68.   ld (ix+5),h
  69.  
  70.   call rand
  71.   ld (ix+6),h
  72.   set 7,l
  73.   ld (ix+7),l
  74.  
  75.   ret
  76.  
  77. xrand_zero:
  78.   pop de
  79.   ld hl,xconst_0
  80.   jp mov10
  81.  endif
  82.