?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1.  ifndef included_sqrtHLIX
  2.  define included_sqrtHLIX
  3.  include "sqrt16.asm"
  4.  
  5. sqrtHLIX:
  6. ;Input: HLIX
  7. ;Output: DE is the sqrt, AHL is the remainder
  8. ;speed: 751+6{0,6}+{0,3+{0,18}}+{0,38}+sqrtHL
  9. ;min: 1103
  10. ;max: 1237
  11. ;avg: 1165.5
  12. ;166 bytes
  13.  
  14.   call sqrtHL   ;expects returns A as sqrt, HL as remainder, D = 0
  15.   add a,a
  16.   ld e,a
  17.   rl d
  18.  
  19.   ld a,ixh
  20.   sla e:inc e;sll e
  21.   rl d
  22.   add a,a : adc hl,hl
  23.   add a,a : adc hl,hl
  24.   sbc hl,de
  25.   jr nc,$+2+1+1+1;+_
  26.   add hl,de
  27.   dec e
  28.   db $FE     ;start of `cp *`
  29. ;_:
  30.   inc e
  31.  
  32.   sla e:inc e;sll e
  33.   rl d
  34.   add a,a : adc hl,hl
  35.   add a,a : adc hl,hl
  36.   sbc hl,de
  37.   jr nc,$+2+1+1+1;+_
  38.   add hl,de
  39.   dec e
  40.   db $FE     ;start of `cp *`
  41. ;_:
  42.   inc e
  43.  
  44.   sla e:inc e;sll e
  45.   rl d
  46.   add a,a : adc hl,hl
  47.   add a,a : adc hl,hl
  48.   sbc hl,de
  49.   jr nc,$+2+1+1+1;+_
  50.   add hl,de
  51.   dec e
  52.   db $FE     ;start of `cp *`
  53. ;_:
  54.   inc e
  55.  
  56.   sla e:inc e;sll e
  57.   rl d
  58.   add a,a : adc hl,hl
  59.   add a,a : adc hl,hl
  60.   sbc hl,de
  61.   jr nc,$+2+1+1+1;+_
  62.   add hl,de
  63.   dec e
  64.   db $FE     ;start of `cp *`
  65. ;_:
  66.   inc e
  67.  
  68. ;Now we have four more iterations
  69. ;The first two are no problem
  70.   ld a,ixl
  71.   sla e:inc e;sll e
  72.   rl d
  73.   add a,a : adc hl,hl
  74.   add a,a : adc hl,hl
  75.   sbc hl,de
  76.   jr nc,$+2+1+1+1;+_
  77.   add hl,de
  78.   dec e
  79.   db $FE     ;start of `cp *`
  80. ;_:
  81.   inc e
  82.  
  83.   sla e:inc e;sll e
  84.   rl d
  85.   add a,a : adc hl,hl
  86.   add a,a : adc hl,hl
  87.   sbc hl,de
  88.   jr nc,$+2+1+1+1;+_
  89.   add hl,de
  90.   dec e
  91.   db $FE     ;start of `cp *`
  92. ;_:
  93.   inc e
  94.  
  95. sqrt32_iter15:
  96. ;On the next iteration, HL might temporarily overflow by 1 bit
  97.   sla e:inc e;sll e
  98.   rl d
  99.   add a,a
  100.   adc hl,hl
  101.   add a,a
  102.   adc hl,hl       ;This might overflow!
  103.   jr c,sqrt32_iter15_br0
  104. ;
  105.   sbc hl,de
  106.   jr nc,sqrt32_iter15q;+_
  107.   add hl,de
  108.   dec e
  109.   jr sqrt32_iter16
  110. sqrt32_iter15_br0:
  111.   or a
  112.   sbc hl,de
  113. sqrt32_iter15q;_:
  114.   inc e
  115.  
  116. ;On the next iteration, HL is allowed to overflow, DE could overflow with our current routine, but it needs to be shifted right at the end, anyways
  117. sqrt32_iter16:
  118.   add a,a
  119.   ld b,a        ;either 0x00 or 0x80
  120.   adc hl,hl
  121.   rla
  122.   adc hl,hl
  123.   rla
  124. ;AHL - (DE+DE+1)
  125.   sbc hl,de : sbc a,b
  126.   inc e
  127.   or a
  128.   sbc hl,de : sbc a,b
  129.   ret p
  130.   add hl,de
  131.   adc a,b
  132.   dec e
  133.   add hl,de
  134.   adc a,b
  135.   ret
  136.  endif
  137.