?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #ifndef included_f64tox
  2. #define included_f64tox
  3.  
  4. #include "routines/rl64.z80"
  5. #include "mov.z80"
  6. #include "pushpop.z80"
  7.  
  8. f64tox:
  9. ;Inputs:
  10. ;   HL points to the input double-precision float
  11. ;   BC points to where to output the result
  12. ;Outputs:
  13. ;   The double-precision float (binary64) is converted to an extended-precision
  14. ;   float at BC.
  15. ;
  16.   call pushpop
  17. f64tox_nopush:
  18.   ld d,b
  19.   ld e,c
  20.  
  21.  
  22. ;bottom 8 bits are 0
  23.   xor a
  24.   ld (de),a
  25.   inc de
  26.  
  27. ;load the next 7 bytes
  28.   call mov7
  29.   ;load the upper 8 bits of the sign/exponent into c
  30.   ld c,(hl)
  31.  
  32. ;now shift the mantissa up 4 times
  33. ;lower 4 bits of BC contain the mantissa, upper 12 are the exponent and sign
  34.   ld hl,-7
  35.   ex de,hl
  36.   add hl,de
  37.   inc e
  38.   rlc c   ;\
  39.   srl c   ; |
  40.   sbc a,a ; | Get the sign where it needs to be
  41.   and 8   ; | also resets carry, helpfully
  42.   ld b,a  ;/
  43.                      call rl56 \ rl c \ rl b
  44.   add hl,de \ or a \ call rl56 \ rl c \ rl b
  45.   add hl,de \ or a \ call rl56 \ rl c \ rl b
  46.  
  47. ;now the top bit of the mantissa contains the bottom bit of the exponent.
  48. ;we also need to replace that top bit with a 1
  49.   ld a,(hl)
  50.   rlca
  51.   scf
  52.   rra
  53.   ld (hl),a
  54.   rl c
  55.   rl b
  56.  
  57. ; BC is the input exponent, check if it is special
  58.   ld a,b
  59.   add a,a
  60.   or c
  61.   jr z,f64tox_zero
  62.  
  63.   ld a,c
  64.   inc a
  65.   jr nz,+_
  66.   ld a,b
  67.   inc a
  68.   and 7
  69.   jr z,f64tox_infnan
  70.  
  71.   ;now we need to subtract 1023 and add 16384
  72.   ld a,(16384-1023)&255
  73.   add a,c
  74.   inc hl
  75.   ld (hl),a
  76.  
  77.   ld a,(16384-1023)>>8
  78.   adc a,b
  79.   inc hl
  80.   ld (hl),a
  81.   ret
  82.  
  83. f64tox_zero:
  84.   xor a
  85.   ld (hl),a
  86.   inc hl
  87.   ld (hl),a
  88.   inc hl
  89.   ld (hl),b
  90.   ret
  91. f64tox_infnan:
  92.   or (hl)
  93.   ld a,$80
  94.   jr nz,+_
  95.   scf
  96. _:
  97.   rra
  98.   ld (hl),a
  99.   xor a
  100.   inc hl
  101.   ld (hl),a
  102.   inc hl
  103.   rl b
  104.   rra
  105.   ld (hl),a
  106.   ret
  107. #endif
  108.