?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #ifndef included_f32toi8
  2. #define included_f32toi8
  3.  
  4. f32toi8:
  5. ;Inputs: HL points to an f32 float
  6. ;Outputs: A is the signed 8-bit integer part of the input (rounded down)
  7. ;Special cases:
  8. ;   NaN              ==> 0
  9. ;   greater than 127 ==> 127
  10. ;   less than -128   ==> -128
  11.  
  12.   push hl
  13.   push de
  14.   push bc
  15.   ld a,(hl)
  16.   inc hl
  17.   or (hl)
  18.   ld d,a    ; save the OR of the bottom two bytes of the significand
  19.   inc hl
  20.   ld a,(hl)
  21.   rlca
  22.   scf
  23.   rra
  24.   ld c,a
  25.   inc hl
  26.   ld a,(hl)
  27.   adc a,a
  28.  
  29.   rr e    ; save the sign
  30. ; E has the sign, C is the portion of the significand that matters,
  31. ; A is the exponent
  32.  
  33.   call f32toi8_get_int
  34.  
  35. f32toi8_return:
  36.   pop bc
  37.   pop de
  38.   pop hl
  39.   ret
  40.  
  41. f32toi8_zero_ish:
  42.   xor a
  43.   rl e
  44.   ret nc
  45.   ld a,c
  46.   add a,a
  47.   or d
  48.   add a,255
  49.   sbc a,a
  50.   ret
  51.  
  52. f32toi8_infnan:
  53.   ; if the significand is non-zero, return NaN
  54.   ld a,c
  55.   add a,a
  56.   or d
  57.   sub 1
  58.   sbc a,a
  59.   ret z
  60. f32toi8_inf:
  61.   ld a,127
  62.   rl e
  63.   adc a,0
  64.   ret
  65.  
  66. f32toi8_get_int:
  67.   inc a
  68.   jr z,f32toi8_infnan
  69.   jp p,f32toi8_zero_ish
  70.   sub 128
  71.   cp 7
  72.   jr nc,f32toi8_inf
  73.  
  74.   ld h,0
  75.   ld l,c    ; upper 8 bits of the significand, H is 0
  76.   ld b,a
  77.   inc b
  78.   add hl,hl
  79.   djnz $-1
  80.   ld a,h
  81.   rl e
  82.   ret nc
  83.   ld a,l
  84.   or d
  85.   add a,255
  86.   sbc a,a
  87.   sub h
  88.   ld h,a
  89.   ret
  90. #endif
  91.