?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #ifndef included_singletof32
  2. #define included_singletof32
  3. #include "pushpop.z80"
  4.  
  5. singletof32:
  6. ;convert a "single" to an IEEE-754 binary32
  7. ;HL points to the input, BC is where to output
  8.   call pushpop
  9.   ld e,(hl)
  10.   inc hl
  11.   ld d,(hl)
  12.   inc hl
  13.   ld a,(hl)
  14.   inc hl
  15.   ld h,(hl)
  16. ;HADE is the single
  17.  
  18. ;first, check for special values
  19.   inc h
  20.   dec h
  21.   jr z,singletof32_special
  22.  
  23. ;now decrement the exponent
  24. ;note: "single" here allows for an exponent as small as -127, but binary32 only
  25. ;      allows as small as -126. So this could mean an underflow. Lucky for us,
  26. ;      this would set the exponent to logical 0x00, which encodes 0 for binary32
  27. singletof32_return_inf_nan:
  28.   dec h
  29.  
  30. ; Now we need to rotate HA right
  31. ;This puts the sign in the top bit, and moves one bit of the exponent to the top
  32. ;of the significand
  33. singletof32_return:
  34.   add a,a
  35.   rr h
  36.   rra
  37.  
  38. ;now write the float
  39.   ld l,c
  40.   ld c,h
  41.   ld h,b
  42.  
  43. ;CADE holds the result
  44.   ld (hl),e
  45.   inc hl
  46.   ld (hl),d
  47.   inc hl
  48.   ld (hl),a
  49.   inc hl
  50.   ld (hl),c
  51.   ret
  52.  
  53. singletof32_special:
  54. ;H is 0
  55. ;set DE to 0 as well
  56.   ld d,h
  57.   ld e,h
  58.  
  59. ;A encodes whether the value is 0, inf, or NaN. We'll want to return A with the
  60. ;bottom 7 bits as 0
  61.   ld l,a
  62.   and $80
  63.  
  64. ;if bit 6 is 1, then we have inf, so we keep DE==0
  65.   bit 6,l
  66.   jr nz,singletof32_return_inf_nan
  67.  
  68. ;if bit 5 is 0, then we have 0
  69.   bit 5,l
  70.   jr z,singletof32_return
  71.  
  72. ;Otherwise it is NaN, so we need to set DE to be non-zero
  73.   inc e
  74.   jr singletof32_return_inf_nan
  75.  
  76. #endif
  77.