Advertisement
Zeda

ConvFloat

Aug 4th, 2015
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ConvOP1:
  2. ;;Output: HL is the 16-bit result.
  3.     ld de,OP1
  4. ConvFloat:
  5. ;;Input: DE points to the float.
  6. ;;Output: HL is the 16-bit result.
  7. ;;Errors: DataType if the float is negative or complex
  8. ;;        Domain if the integer exceeds 16 bits.
  9. ;;Timings:  Assume no errors were called.
  10. ;;  Input is on:
  11. ;;  (0,1)         => 59cc                        Average=59
  12. ;;  0 or [1,10)   => 120cc or 129cc                     =124.5
  13. ;;  [10,100)      => 176cc or 177cc                     =176.5
  14. ;;  [100,1000)    => 309cc, 310cc, 318cc, or 319cc.     =314
  15. ;;  [1000,10000)  => 376cc to 378cc                     =377
  16. ;;  [10000,65536) => 514cc to 516cc, or 523cc to 525cc  =519.5
  17. ;;Average case:  496.577178955078125cc
  18. ;;vs 959.656982421875cc
  19. ;;87 bytes
  20.  
  21.     ld a,(de)
  22.     or a
  23.     jr nz,ErrDataType
  24.     inc de
  25.     ld hl,0
  26.     ld a,(de)
  27.     inc de
  28.     sub 80h
  29.     ret c
  30.     jr z,final
  31.     cp 5
  32.     jp c,enterloop
  33. ErrDomain:
  34. ;Throws a domain error.
  35.     bcall(_ErrDomain)
  36. ErrDataType:
  37. ;Throws a data type error.
  38.     bcall(_ErrDataType)
  39. loop:
  40.     ld a,b
  41.     ld b,h
  42.     ld c,l
  43.     add hl,hl
  44.     add hl,bc
  45.     add hl,hl
  46.     add hl,hl
  47.     add hl,hl
  48.     add hl,bc
  49.     add hl,hl
  50.     add hl,hl
  51. enterloop:
  52.     ld b,a
  53.     ex de,hl
  54.     ld a,(hl) \ and $F0 \ rra \ ld c,a \ rra \ rra \ sub c \ add a,(hl)
  55.     inc hl
  56.     ex de,hl
  57.     add a,l
  58.     ld l,a
  59.     jr nc,$+3
  60.     inc h
  61.     dec b
  62.     ret z
  63.     djnz loop
  64.     ld b,h
  65.     ld c,l
  66.     xor a
  67. ;check overflow in this mul by 10!
  68.     add hl,hl \ adc a,a
  69.     add hl,hl \ adc a,a
  70.     add hl,bc \ adc a,0
  71.     add hl,hl \ adc a,a
  72.     jr nz,ErrDomain
  73. final:
  74.     ld a,(de)
  75.     rrca
  76.     rrca
  77.     rrca
  78.     rrca
  79.     and 15
  80.     add a,l
  81.     ld l,a
  82.     ret nc
  83.     inc h
  84.     ret nz
  85.     jr ErrDomain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement