?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #ifndef included_x_to_int16
  2. #define included_x_to_int16
  3.  
  4. x_to_int16:
  5. ;Input: HL points to an extended-precision float
  6. ;Output: HL is a 16-bit signed integer representation of the input float.
  7.  
  8.   push ix
  9.   push de
  10.   push bc
  11.   push af
  12.   call +_
  13.   pop af
  14.   pop bc
  15.   pop de
  16.   pop ix
  17.   ret
  18. _:
  19.   push hl
  20.   pop ix
  21.   ld e,(ix+6)
  22.   ld d,(ix+7)
  23.   ld a,(ix+9)
  24.   ld hl,0
  25.   add a,a
  26.   jp p,x_to_int16_zero
  27.   add a,a
  28.   jr nz,x_to_int16_inf
  29.   ld a,(ix+8)
  30.   cp 15       ;at most 15 bits since signed
  31.   jr nc,x_to_int16_inf
  32.   inc a
  33.   cp 8
  34.   jr c,+_
  35.   sub 8
  36.   ld l,d
  37.   ld d,e
  38.   jr z,x_to_int16_end
  39. _:
  40.   ld b,a
  41.   ld a,d
  42. _:
  43.   rla
  44.   adc hl,hl
  45.   djnz -_
  46. x_to_int16_end:
  47.   ld a,(ix+9)
  48.   add a,a
  49.   ret nc
  50.   xor a
  51.   sub l
  52.   ld l,a
  53.   sbc a,a
  54.   sub h
  55.   ld h,a
  56.   ret
  57.  
  58.  
  59. x_to_int16_zero:
  60.   ret nz
  61. ;maybe it is a special number, though
  62.   ld a,(ix+8)
  63.   or a
  64.   ret nz
  65. ;it is special, so now we check for special values
  66. ; 0 --> 0
  67. ; NAN --> 65535
  68. ; -INF --> 65535
  69. ;INF --> 32767
  70.   ld a,d
  71.   add a,a
  72.   jr c,x_to_int16_inf
  73.   ret z
  74. x_to_int16_inf:
  75.   dec hl
  76.   ld a,(ix+9)
  77.   add a,a
  78.   ret c
  79.   res 7,h
  80.   ret
  81.  
  82. #endif
  83.