?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #ifndef included_f32tof16
  2. #define included_f32tof16
  3.  
  4. f32tof16:
  5. ;convert an IEEE-754 binary32 to an IEEE-754 binary16 format.
  6. ;Input: HL points to the input float
  7. ;Output: HL is the f16 float
  8. ;Destroys: AF, BC, DE
  9.  
  10.   ld c,(hl)
  11.   inc hl
  12.   ld e,(hl)
  13.   inc hl
  14.   ld a,(hl)
  15.   ld d,a
  16.   add a,a
  17.   inc hl
  18.   ld a,(hl)
  19.   adc a,a
  20.   jr z,f32tof16_return_0_noA
  21.   inc a
  22.   jr z,f32tof16_return_infnan
  23.   rr c    ; save the sign, we no longer need C
  24.  
  25.   sub 113
  26.   jr c,f32tof16_return_0
  27.   cp 31
  28.   jr nc,f32tof16_return_inf
  29.   ;A is the exponent
  30.   ;(DE>>5)&%0000001111111111 encodes the significand
  31.   ex de,hl
  32.   add hl,hl
  33.   ; rla   ; we want to omit the top bit of the significand
  34.   add hl,hl
  35.   rla
  36.   add hl,hl
  37.   rla
  38.   ; now get the sign
  39.   add a,a
  40.   rl c
  41.   rra
  42.   ; we'll round
  43.   sla l
  44.   ld l,h
  45.   ld h,a
  46.   ret nc
  47.   inc hl
  48.   ret
  49.  
  50. f32tof16_return_infnan:
  51.   ld a,%11111000
  52.   rra
  53.   ld h,a  ; sign and exponent set
  54.   ld a,d
  55.   add a,a
  56.   or e
  57.   or c
  58.   ld l,a  ; if the input was NaN, this will be non-zero, else zero
  59.   ret
  60.  
  61. f32tof16_return_inf:
  62.   ld a,c
  63.   or %01111100
  64.   ld l,0
  65.   ld h,a
  66.   ret
  67.  
  68. f32tof16_return_0:
  69.   xor a
  70.   rl c
  71. f32tof16_return_0_noA:
  72.   ; ld l,a  ; Not necessary, just need the exponent = 0
  73.   rra     ; shift the sign back in
  74.   ld h,a
  75.   ret
  76. #endif
  77.