?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #ifndef included_f64tof32
  2. #define included_f64tof32
  3.  
  4. #include "routines/rl64.z80"
  5. #include "mov.z80"
  6. #include "pushpop.z80"
  7.  
  8. f64tof32:
  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. f64tof32_nopush:
  18. ; we don't need the bottom 3 bytes except if we need to distinguish between inf
  19. ; and NaN.
  20.   push bc     ;save the pointer to the output
  21.   ld a,(hl)
  22.   inc hl
  23.   or (hl)
  24.   inc hl
  25.   or (hl)
  26.   inc hl
  27.   ld e,(hl)
  28.   inc hl
  29.   ld d,(hl)
  30.   inc hl
  31.   ld c,(hl)
  32.   inc hl
  33.   ld b,(hl)
  34.   inc hl
  35.   ld h,(hl)
  36.   ld l,a    ;save the OR of the bottom 3 bytes
  37. ;HBCDE is the top 5 bytes of the f64 float
  38.   ld a,h
  39.   add a,a
  40.   jr z,f64tof32_check_0
  41.   add a,2
  42.   jr nz,f64tof32_continue
  43. f64tof32_check_infnan:
  44.   ld a,b
  45.   add a,16
  46.   jr nc,f64tof32_continue
  47. f64tof32_infnan:
  48.   ld a,b
  49.   and %00001111
  50.   or c
  51.   or d
  52.   or e
  53.   or l
  54.   ld d,h
  55.   pop hl
  56.   ld (hl),a
  57.   inc hl
  58.   ld (hl),a
  59.   inc hl
  60.   or %10000000
  61.   ld (hl),a
  62.   inc hl
  63.   ld a,d
  64.   or %01111111
  65.   ld (hl),a
  66.   ret
  67. f64tof32_check_0:
  68.   ld a,b
  69.   and %11110000
  70.   jr z,f64tof32_zero
  71. f64tof32_continue:
  72. ; We need to scale down the exponent, subtract 1023 and add 127, net -896
  73.   ld a,h
  74.   add a,a
  75.   rrca
  76.   sub 56
  77.   jr c,f64tof32_zero_setA
  78.   cp 16
  79.   jr nc,f64tof32_inf1
  80.   ex de,hl
  81. ;DBCHL
  82.   add hl,hl
  83.   rl c
  84.   rl b
  85.   rla
  86.  
  87.   add hl,hl
  88.   rl c
  89.   rl b
  90.   rla
  91.  
  92.   add hl,hl
  93.   rl c
  94.   rl b
  95.   rla
  96.  
  97.   xor d
  98.   and %01111111
  99.   xor d
  100.   ;ABCHL
  101.   ; round befoe writing out
  102.   sla l
  103.   jr nc,f64tof32_rounded
  104.   inc h
  105.   jr nz,f64tof32_rounded
  106.   inc c
  107.   jr nz,f64tof32_rounded
  108.   inc b
  109.   jr nz,f64tof32_rounded
  110.   inc a
  111.   jr z,f64tof32_inf
  112. f64tof32_rounded:
  113.   ld d,h
  114.   pop hl
  115.   ld (hl),d
  116.   inc hl
  117.   ld (hl),c
  118.   inc hl
  119.   ld (hl),b
  120.   inc hl
  121.   ld (hl),a
  122.   ret
  123.  
  124. f64tof32_zero_setA:
  125.   xor a
  126. f64tof32_zero:
  127.   ld d,h
  128.   pop hl
  129.   ld (hl),a
  130.   inc hl
  131.   ld (hl),a
  132.   inc hl
  133.   ld (hl),a
  134.   inc hl
  135.   ld a,d
  136.   and %10000000
  137.   ld (hl),a
  138.   ret
  139. f64tof32_inf1:
  140.   ld a,h
  141. f64tof32_inf:
  142.   pop hl
  143.   ld (hl),0
  144.   inc hl
  145.   ld (hl),0
  146.   inc hl
  147.   ld (hl),80h
  148.   inc hl
  149.   or %01111111
  150.   ld (hl),a
  151. #endif
  152.