fixunsdfdi.c revision 1.3
11.3Scgd/*	$NetBSD: fixunsdfdi.c,v 1.3 1995/02/27 17:29:58 cgd Exp $	*/
21.3Scgd
31.1Smycroft/*-
41.1Smycroft * Copyright (c) 1992, 1993
51.1Smycroft *	The Regents of the University of California.  All rights reserved.
61.1Smycroft *
71.1Smycroft * This software was developed by the Computer Systems Engineering group
81.1Smycroft * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
91.1Smycroft * contributed to Berkeley.
101.1Smycroft *
111.1Smycroft * Redistribution and use in source and binary forms, with or without
121.1Smycroft * modification, are permitted provided that the following conditions
131.1Smycroft * are met:
141.1Smycroft * 1. Redistributions of source code must retain the above copyright
151.1Smycroft *    notice, this list of conditions and the following disclaimer.
161.1Smycroft * 2. Redistributions in binary form must reproduce the above copyright
171.1Smycroft *    notice, this list of conditions and the following disclaimer in the
181.1Smycroft *    documentation and/or other materials provided with the distribution.
191.1Smycroft * 3. All advertising materials mentioning features or use of this software
201.1Smycroft *    must display the following acknowledgement:
211.1Smycroft *	This product includes software developed by the University of
221.1Smycroft *	California, Berkeley and its contributors.
231.1Smycroft * 4. Neither the name of the University nor the names of its contributors
241.1Smycroft *    may be used to endorse or promote products derived from this software
251.1Smycroft *    without specific prior written permission.
261.1Smycroft *
271.1Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
281.1Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
291.1Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
301.1Smycroft * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
311.1Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
321.1Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
331.1Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
341.1Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
351.1Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
361.1Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
371.1Smycroft * SUCH DAMAGE.
381.1Smycroft */
391.1Smycroft
401.1Smycroft#if defined(LIBC_SCCS) && !defined(lint)
411.3Scgd#if 0
421.3Scgdstatic char sccsid[] = "@(#)fixunsdfdi.c	8.1 (Berkeley) 6/4/93";
431.3Scgd#else
441.3Scgdstatic char rcsid[] = "$NetBSD: fixunsdfdi.c,v 1.3 1995/02/27 17:29:58 cgd Exp $";
451.3Scgd#endif
461.1Smycroft#endif /* LIBC_SCCS and not lint */
471.1Smycroft
481.1Smycroft#include "quad.h"
491.1Smycroft
501.2Scgd#define	ONE_FOURTH	((long)1 << (LONG_BITS - 2))
511.1Smycroft#define	ONE_HALF	(ONE_FOURTH * 2.0)
521.1Smycroft#define	ONE		(ONE_FOURTH * 4.0)
531.1Smycroft
541.1Smycroft/*
551.1Smycroft * Convert double to (unsigned) quad.
561.1Smycroft * Not sure what to do with negative numbers---for now, anything out
571.1Smycroft * of range becomes UQUAD_MAX.
581.1Smycroft */
591.1Smycroftu_quad_t
601.1Smycroft__fixunsdfdi(x)
611.1Smycroft	double x;
621.1Smycroft{
631.1Smycroft	double toppart;
641.1Smycroft	union uu t;
651.1Smycroft
661.1Smycroft	if (x < 0)
671.1Smycroft		return (UQUAD_MAX);	/* ??? should be 0?  ERANGE??? */
681.1Smycroft#ifdef notdef				/* this falls afoul of a GCC bug */
691.1Smycroft	if (x >= UQUAD_MAX)
701.1Smycroft		return (UQUAD_MAX);
711.1Smycroft#else					/* so we wire in 2^64-1 instead */
721.1Smycroft	if (x >= 18446744073709551615.0)	/* XXX */
731.1Smycroft		return (UQUAD_MAX);
741.1Smycroft#endif
751.1Smycroft	/*
761.1Smycroft	 * Get the upper part of the result.  Note that the divide
771.1Smycroft	 * may round up; we want to avoid this if possible, so we
781.1Smycroft	 * subtract `1/2' first.
791.1Smycroft	 */
801.1Smycroft	toppart = (x - ONE_HALF) / ONE;
811.1Smycroft	/*
821.1Smycroft	 * Now build a u_quad_t out of the top part.  The difference
831.1Smycroft	 * between x and this is the bottom part (this may introduce
841.1Smycroft	 * a few fuzzy bits, but what the heck).  With any luck this
851.1Smycroft	 * difference will be nonnegative: x should wind up in the
861.1Smycroft	 * range [0..ULONG_MAX].  For paranoia, we assume [LONG_MIN..
871.1Smycroft	 * 2*ULONG_MAX] instead.
881.1Smycroft	 */
891.1Smycroft	t.ul[H] = (unsigned long)toppart;
901.1Smycroft	t.ul[L] = 0;
911.1Smycroft	x -= (double)t.uq;
921.1Smycroft	if (x < 0) {
931.1Smycroft		t.ul[H]--;
941.1Smycroft		x += ULONG_MAX;
951.1Smycroft	}
961.1Smycroft	if (x > ULONG_MAX) {
971.1Smycroft		t.ul[H]++;
981.1Smycroft		x -= ULONG_MAX;
991.1Smycroft	}
1001.1Smycroft	t.ul[L] = (u_long)x;
1011.1Smycroft	return (t.uq);
1021.1Smycroft}
103