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