?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #ifndef included_f32tou16
  2. #define included_f32tou16
  3.  
  4. f32tou16:
  5. ;Inputs: HL points to an f32 float
  6. ;Outputs: HL is the unsigned 16-bit integer part of the input (rounded down)
  7. ;Special cases:
  8. ;   NaN                ==> 0
  9. ;   greater than 65535 ==> 65535
  10. ;   less than 0        ==> 0
  11.  
  12.   push de
  13.   push bc
  14.   push af
  15.   ld c,(hl)
  16.   inc hl
  17.   ld e,(hl)
  18.   inc hl
  19.   ld a,(hl)
  20.   rlca
  21.   scf
  22.   rra
  23.   ld d,a
  24.   inc hl
  25.   ld a,(hl)
  26.   adc a,a
  27.   ccf
  28.   jr nc,f32tou16_return_carry
  29.   or a
  30.   jr z,f32tou16_return_carry
  31.   inc a
  32.   jr z,f32tou16_infnan
  33.  
  34.   add a,256-143
  35.   jr c,f32tou16_return_carry
  36.   add a,15
  37.   jr nc,f32tou16_return_carry
  38.  
  39.   ld b,a
  40.   inc b
  41.   ex de,hl
  42.   xor a
  43.   ld e,a
  44.  
  45.   add hl,hl
  46.   rl e
  47.   rla
  48.   djnz $-4
  49.  
  50.   ld l,e
  51.   ld h,a
  52.  
  53.   .db $01 ; start of `ld bc,**` to skip the next two bytes
  54. f32tou16_return_carry:
  55.   sbc hl,hl
  56.  
  57. f32tou16_return:
  58.   pop af
  59.   pop bc
  60.   pop de
  61.   ret
  62. f32tou16_infnan:
  63.   ld a,d
  64.   add a,a
  65.   or e
  66.   or c
  67.   sub 1
  68.   jr f32tou16_return_carry
  69. #endif
  70.