?login_element?

Subversion Repositories NedoOS

Rev

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

  1.  ifndef included_sqrtHL
  2.  define included_sqrtHL
  3.  
  4. sqrtHL:
  5. ;returns A as the sqrt, HL as the remainder, D = 0
  6. ;min: 352cc
  7. ;max: 391cc
  8. ;avg: 371.5cc
  9.  
  10.  
  11.   ld de,05040h  ; 10
  12.   ld a,h        ; 4
  13.   sub e         ; 4
  14.   jr nc,sq7     ;\
  15.   add a,e       ; | branch 1: 12cc
  16.   ld d,16       ; | branch 2: 18cc
  17. sq7:            ;/
  18.  
  19. ; ----------
  20.  
  21.   cp d          ; 4
  22.   jr c,sq6      ;\
  23.   sub d         ; | branch 1: 12cc
  24.   set 5,d       ; | branch 2: 19cc
  25. sq6:            ;/
  26.  
  27. ; ----------
  28.   res 4,d       ; 8
  29.   srl d         ; 8
  30.   set 2,d       ; 8
  31.   cp d          ; 4
  32.   jr c,sq5      ;\
  33.   sub d         ; | branch 1: 12cc
  34.   set 3,d       ; | branch 2: 19cc
  35. sq5:            ;/
  36.   srl d         ; 8
  37.  
  38. ; ----------
  39.  
  40.   inc a         ; 4
  41.   sub d         ; 4
  42.   jr nc,sq4     ;\
  43.   dec d         ; | branch 1: 12cc
  44.   add a,d       ; | branch 2: 19cc
  45.   dec d         ; | <-- this resets the low bit of D, so `srl d` resets carry.
  46. sq4:            ;/
  47.   srl d         ; 8
  48.   ld h,a        ; 4
  49.  
  50. ; ----------
  51.  
  52.   ld a,e        ; 4
  53.   sbc hl,de     ; 15
  54.   jr nc,sq3     ;\
  55.   add hl,de     ; | 12cc or 18cc
  56. sq3:            ;/
  57.   ccf           ; 4
  58.   rra           ; 4
  59.   srl d         ; 8
  60.   rra           ; 4
  61.  
  62. ; ----------
  63.  
  64.   ld e,a        ; 4
  65.   sbc hl,de     ; 15
  66.   jr c,sq2      ;\
  67.   or 20h        ; | branch 1: 23cc
  68.   db 254        ; |   <-- start of `cp *` which is 7cc to skip the next byte.
  69. sq2:            ; | branch 2: 21cc
  70.   add hl,de     ;/
  71.  
  72.   xor 18h       ; 7
  73.   srl d         ; 8
  74.   rra           ; 4
  75.  
  76. ; ----------
  77.  
  78.   ld e,a        ; 4
  79.   sbc hl,de     ; 15
  80.   jr c,sq1      ;\
  81.   or 8          ; | branch 1: 23cc
  82.   db 254        ; |   <-- start of `cp *` which is 7cc to skip the next byte.
  83. sq1:            ; | branch 2: 21cc
  84.   add hl,de     ;/
  85.  
  86.   xor 6         ; 7
  87.   srl d         ; 8
  88.   rra           ; 4
  89.  
  90. ; ----------
  91.  
  92.   ld e,a        ; 4
  93.   sbc hl,de     ; 15
  94.   jr nc,sq0;+_      ;    \
  95.   add hl,de     ; 15  |
  96.   srl d         ; 8   |
  97.   rra           ; 4   | branch 1: 38cc
  98.   ret           ; 10  | branch 2: 40cc
  99. sq0;_:              ;     |
  100.   inc a         ; 4   |
  101.   srl d         ; 8   |
  102.   rra           ; 4   |
  103.   ret           ; 10 /
  104.  
  105.  endif
  106.