
DBLWORD f_to_i64(f,sgn)
FLOAT f;
int sgn;
{
 int rb;
 int ov;
 SPLITFLOAT sf;
 DBLWORD rv;

 sf = split(f);
 if (sf.exp == 0)
  { rv.h = 0;
    rv.l = 0;
    return(rv);
  }
 sf.exp -= 0x80;
 rb = 0;
 ov = 0;
 if (sf.exp < -56)
  { rv.h = 0;
    rv.l = 0;
  }
 else if (sf.exp == -56)
  { rv.h = 0;
    rv.l = 0;
    rb = 1;
  }
 else if (sf.exp < -32)
  { rv.h = 0;
    rv.l = sf.mant.h >> (-sf.exp-32);
    rb = (sf.mant.h >> (-sf.exp-31)) & 1;
  }
 else if (sf.exp == -32)
  { rv.h = 0;
    rv.l = sf.mant.h;
    rb = sf.mant.l >> 31;
  }
 else if (sf.exp <= -24)
  { rv.h = 0;
    rv.l = ((sf.mant.h << (sf.exp+32)) | (sf.mant.l >> -sf.exp)) & 0xffffffff;
    rb = (sf.mant.l >> (-sf.exp-1)) & 1;
  }
 else if (sf.exp < -1)
  { rv.h = sf.mant.h >> -sf.exp;
    rv.l = ((sf.mant.h << (sf.exp+32)) | (sf.mant.l >> -sf.exp)) & 0xffffffff;
    rb = (sf.mant.l >> (-sf.exp-1)) & 1;
  }
 else if (sf.exp == -1)
  { rv.h = sf.mant.h >> 1;
    rv.l = ((sf.mant.h << 31) | (sf.mant.l >> 1)) & 0xffffffff;
    rb = sf.mant.l & 1;
  }
 else if (sf.exp == 0)
  { rv.h = sf.mant.h;
    rv.l = sf.mant.l;
  }
 else if (sf.exp < 8)
  { rv.h = ((sf.mant.h << sf.exp) | (sf.mant.l >> (32-sf.exp))) & 0xffffffff;
    rv.l = (sf.mant.l << sf.exp) & 0xffffffff;
    if (sf.mant.h >> (32-sgn-sf.exp)) ov = 1;
  }
 else if (sf.exp == 8)
  { rv.h = ((sf.mant.h << 8) | (sf.mant.l >> 24)) & 0xffffffff;
    rv.l = (sf.mant.l << 8) & 0xffffffff;
    if (sgn) ov = 1;
  }
 else if (sf.exp < 32)
  { rv.h = ((sf.mant.h << sf.exp) | (sf.mant.l >> (32-sf.exp))) & 0xffffffff;
    rv.l = (sf.mant.l << sf.exp) & 0xffffffff;
    ov = 1;
  }
 else if (sf.exp == 32)
  { rv.h = sf.mant.l;
    rv.l = 0;
    ov = 1;
  }
 else if (sf.exp < 64)
  { rv.h = (sf.mant.l << (sf.exp-32)) & 0xffffffff;
    rv.l = 0;
    ov = 1;
  }
 else
  { rv.h = 0;
    rv.l = 0;
    ov = 1;
  }
 if (rb)
  { rv.l = (rv.l + 1) & 0xffffffff;
    if (rv.l == 0)
     { rv.h = (rv.h + 1) & 0xffffffff;
       if ((rv.h == 0) || (sgn && (rv.h & 0x80000000))) ov = 1;
     }
  }
 if (ov) f_to_i_ovf();
 return(rv);
}

puts:
	mov	r1,-(sp)
	mov	r0,r1
putsloop:
	movb	(r1)+,r0
	beq	putsdone
	jsr	pc,putc
	br	putsloop
putsdone:
	mov	(sp)+,r1
	rts	pc

print_8:
	; on entry: TOS is return pc, TOS+2 min # digits, TOS+4 number low, TOS+6 number high
	cmp	2(sp),#1
	bgt	p8_hard
	tst	6(sp)
	bne	p8_hard
	mov	4(sp),r0
	cmp	r0,#8.
	bhis	p8_hard
	bne	p8_put
	tst	2(sp)
	bgt	p8_put
	rts	pc
p8_put:
	add	#'0,r0
	jsr	pc,putc
	rts	pc
p8_hard:
	sub	#10,sp
	mov	16(sp),4(sp)
	mov	14(sp),2(sp)
	mov	#8.,(sp)
	jsr	pc,div_32_by_8
	mov	(sp),6(sp)
	mov	12(sp),r0
	dec	r0
	mov	r0,(sp)
	jsr	pc,print_8
	add	#6,sp
	mov	(sp)+,r0
	br	p8_put
                                                                                                                                                                                                                                                                                                                                                                                                                              