?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #ifndef included_xtof64
  2. #define included_xtof64
  3.  
  4. #include "routines/rr64.z80"
  5. #include "mov.z80"
  6. #include "pushpop.z80"
  7.  
  8. xtof64:
  9. ;Inputs:
  10. ;   HL points to the input extended-precision float
  11. ;   BC points to where to output the result
  12. ;Outputs:
  13. ;   The extended-precision float is converted to a double (binary64) at BC.
  14. ;
  15.  
  16. ;bias is 1023, so 0x03FF is exponent of 0
  17. ;sign is 1 bit
  18. ;exponent is 11 bits
  19. ;mantissa is 53 bits (top bit is implicit)
  20. ;
  21.  
  22.   call pushpop
  23. xtof64_nopush:
  24.  
  25.   ; The extended precision float has a 64-bit mantissa, so drop the bottom byte.
  26.   inc hl
  27.  
  28.   ; Move the remaining 7 bytes to our output
  29.   ld d,b
  30.   ld e,c
  31.   call mov6
  32.   ld a,(hl)
  33.   ld (de),a
  34.  
  35.   ; the next two bytes are the exponent and sign
  36.   inc hl
  37.   ld c,(hl)
  38.   inc hl
  39.   ld b,(hl)
  40.  
  41.   ; Check if the input is a special number
  42.   ld a,b
  43.   add a,a
  44.   or c
  45.   jr z,xtof64_special
  46.  
  47.   ;save the sign
  48.   ld a,b
  49.   res 7,b
  50.  
  51.   ; make sure the exponent is not too small
  52.   ld hl,-15362
  53.   add hl,bc
  54.   jr nc,xtof64_zero
  55.  
  56.   ; make sure the exponent is not too big
  57.   ld bc,-2045
  58.   add hl,bc
  59.   jr c,xtof64_inf
  60.  
  61.   ; now we need to adjust the exponent
  62.   ld bc,2046
  63.   add hl,bc
  64.  
  65.   ;now we shift the exponent down into the top bit of the mantissa
  66.   ex de,hl
  67.   rl (hl)
  68.   rr d
  69.   rr e
  70.   rr (hl)
  71.  
  72. ;now shift DE down 3 more bits into the old mantissa
  73. ;final shift, shift the sign into E
  74.   ld bc,6
  75.   rr d \ rr e \ call rr56 \ add hl,bc
  76.   rr d \ rr e \ call rr56 \ add hl,bc
  77.   rla \ rr e \  call rr56
  78.   ;need to potentially round up!
  79.   jr nc,+_
  80.   dec c
  81.   jr z,$+7
  82.   inc (hl)
  83.   inc hl
  84.   jr z,$-5
  85.   .db $FE
  86.   inc e
  87. _:
  88.   inc c \ add hl,bc
  89.  
  90. ;now write the top 7 bits of the exponent and the sign to the output
  91.   ld (hl),e
  92.   ret
  93.  
  94. xtof64_special:
  95.   ld a,(de)
  96.   add a,a
  97.   jr c,xtof64_inf
  98.   jp p,xtof64_zero
  99. xtof64_nan:
  100. ;mantissa needs to be non-zero to encode NaN
  101.   ld a,-1
  102.   ex de,hl
  103.   ld (hl),a
  104.   inc hl
  105.   ld (hl),a
  106.   inc hl
  107. ;  rl b
  108. ;  rra
  109.   ld (hl),a
  110.   ret
  111.  
  112. xtof64_inf:
  113. ;carry is always set here
  114. xtof64_zero:
  115. ;carry is always reset here
  116.   sbc a,a
  117.   ex de,hl
  118.  
  119.   inc hl
  120.   and $F0
  121.   ld (hl),a
  122.   add a,a
  123.   sbc a,a
  124.   inc hl
  125.   rl b
  126.   rra
  127.   ld (hl),a
  128.  
  129. ; need to set mantissa to 0
  130.   dec hl
  131.   ld bc,$0600
  132. _:
  133.   dec hl
  134.   ld (hl),c
  135.   djnz -_
  136.   ret
  137. #endif
  138.