xref: /csrg-svn/lib/libc/quad/fixunssfdi.c (revision 61160)
154746Storek /*-
2*61160Sbostic  * Copyright (c) 1992, 1993
3*61160Sbostic  *	The Regents of the University of California.  All rights reserved.
454746Storek  *
554746Storek  * This software was developed by the Computer Systems Engineering group
654746Storek  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
754746Storek  * contributed to Berkeley.
854746Storek  *
954746Storek  * %sccs.include.redist.c%
1054746Storek  */
1154746Storek 
1254746Storek #if defined(LIBC_SCCS) && !defined(lint)
13*61160Sbostic static char sccsid[] = "@(#)fixunssfdi.c	8.1 (Berkeley) 06/04/93";
1454746Storek #endif /* LIBC_SCCS and not lint */
1554746Storek 
1654746Storek #include "quad.h"
1754746Storek 
1854746Storek #define	ONE_FOURTH	(1 << (LONG_BITS - 2))
1954746Storek #define	ONE_HALF	(ONE_FOURTH * 2.0)
2054746Storek #define	ONE		(ONE_FOURTH * 4.0)
2154746Storek 
2254746Storek /*
2354746Storek  * Convert float to (unsigned) quad.  We do most of our work in double,
2454746Storek  * out of sheer paranoia.
2554746Storek  *
2654746Storek  * Not sure what to do with negative numbers---for now, anything out
2754746Storek  * of range becomes UQUAD_MAX.
2854746Storek  *
2954746Storek  * N.B.: must use new ANSI syntax (sorry).
3054746Storek  */
3154746Storek u_quad_t
__fixunssfdi(float f)3254746Storek __fixunssfdi(float f)
3354746Storek {
3454746Storek 	double x, toppart;
3554746Storek 	union uu t;
3654746Storek 
3754746Storek 	if (f < 0)
3854746Storek 		return (UQUAD_MAX);	/* ??? should be 0?  ERANGE??? */
3955180Storek #ifdef notdef				/* this falls afoul of a GCC bug */
4054746Storek 	if (f >= UQUAD_MAX)
4154746Storek 		return (UQUAD_MAX);
4255180Storek #else					/* so we wire in 2^64-1 instead */
4355180Storek 	if (f >= 18446744073709551615.0)
4455180Storek 		return (UQUAD_MAX);
4555180Storek #endif
4654746Storek 	x = f;
4754746Storek 	/*
4854746Storek 	 * Get the upper part of the result.  Note that the divide
4954746Storek 	 * may round up; we want to avoid this if possible, so we
5054746Storek 	 * subtract `1/2' first.
5154746Storek 	 */
5254746Storek 	toppart = (x - ONE_HALF) / ONE;
5354746Storek 	/*
5454746Storek 	 * Now build a u_quad_t out of the top part.  The difference
5554746Storek 	 * between x and this is the bottom part (this may introduce
5654746Storek 	 * a few fuzzy bits, but what the heck).  With any luck this
5754746Storek 	 * difference will be nonnegative: x should wind up in the
5854746Storek 	 * range [0..ULONG_MAX].  For paranoia, we assume [LONG_MIN..
5954746Storek 	 * 2*ULONG_MAX] instead.
6054746Storek 	 */
6154746Storek 	t.ul[H] = (unsigned long)toppart;
6254746Storek 	t.ul[L] = 0;
6354746Storek 	x -= (double)t.uq;
6454746Storek 	if (x < 0) {
6554746Storek 		t.ul[H]--;
6654746Storek 		x += ULONG_MAX;
6754746Storek 	}
6854746Storek 	if (x > ULONG_MAX) {
6954746Storek 		t.ul[H]++;
7054746Storek 		x -= ULONG_MAX;
7154746Storek 	}
7254746Storek 	t.ul[L] = (u_long)x;
7354746Storek 	return (t.uq);
7454746Storek }
75