fixunssfdi.c revision 1.7
11.7Schristos/*	$NetBSD: fixunssfdi.c,v 1.7 2012/03/13 21:13:42 christos 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.6Sagc * 3. Neither the name of the University nor the names of its contributors
201.1Smycroft *    may be used to endorse or promote products derived from this software
211.1Smycroft *    without specific prior written permission.
221.1Smycroft *
231.1Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241.1Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251.1Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261.1Smycroft * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271.1Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281.1Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291.1Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301.1Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311.1Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321.1Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331.1Smycroft * SUCH DAMAGE.
341.1Smycroft */
351.1Smycroft
361.4Schristos#include <sys/cdefs.h>
371.1Smycroft#if defined(LIBC_SCCS) && !defined(lint)
381.3Scgd#if 0
391.3Scgdstatic char sccsid[] = "@(#)fixunssfdi.c	8.1 (Berkeley) 6/4/93";
401.3Scgd#else
411.7Schristos__RCSID("$NetBSD: fixunssfdi.c,v 1.7 2012/03/13 21:13:42 christos Exp $");
421.3Scgd#endif
431.1Smycroft#endif /* LIBC_SCCS and not lint */
441.1Smycroft
451.1Smycroft#include "quad.h"
461.1Smycroft
471.7Schristos#define	ONE_FOURTH	((int)1 << (unsigned int)(INT_BITS - 2))
481.1Smycroft#define	ONE_HALF	(ONE_FOURTH * 2.0)
491.1Smycroft#define	ONE		(ONE_FOURTH * 4.0)
501.1Smycroft
511.1Smycroft/*
521.1Smycroft * Convert float to (unsigned) quad.  We do most of our work in double,
531.1Smycroft * out of sheer paranoia.
541.1Smycroft *
551.1Smycroft * Not sure what to do with negative numbers---for now, anything out
561.1Smycroft * of range becomes UQUAD_MAX.
571.1Smycroft *
581.1Smycroft * N.B.: must use new ANSI syntax (sorry).
591.1Smycroft */
601.1Smycroftu_quad_t
611.1Smycroft__fixunssfdi(float f)
621.1Smycroft{
631.1Smycroft	double x, toppart;
641.1Smycroft	union uu t;
651.1Smycroft
661.1Smycroft	if (f < 0)
671.1Smycroft		return (UQUAD_MAX);	/* ??? should be 0?  ERANGE??? */
681.1Smycroft#ifdef notdef				/* this falls afoul of a GCC bug */
691.1Smycroft	if (f >= UQUAD_MAX)
701.1Smycroft		return (UQUAD_MAX);
711.1Smycroft#else					/* so we wire in 2^64-1 instead */
721.1Smycroft	if (f >= 18446744073709551615.0)	/* XXX */
731.1Smycroft		return (UQUAD_MAX);
741.1Smycroft#endif
751.1Smycroft	x = f;
761.1Smycroft	/*
771.1Smycroft	 * Get the upper part of the result.  Note that the divide
781.1Smycroft	 * may round up; we want to avoid this if possible, so we
791.1Smycroft	 * subtract `1/2' first.
801.1Smycroft	 */
811.1Smycroft	toppart = (x - ONE_HALF) / ONE;
821.1Smycroft	/*
831.1Smycroft	 * Now build a u_quad_t out of the top part.  The difference
841.1Smycroft	 * between x and this is the bottom part (this may introduce
851.1Smycroft	 * a few fuzzy bits, but what the heck).  With any luck this
861.1Smycroft	 * difference will be nonnegative: x should wind up in the
871.5Sscw	 * range [0..UINT_MAX].  For paranoia, we assume [INT_MIN..
881.5Sscw	 * 2*UINT_MAX] instead.
891.1Smycroft	 */
901.5Sscw	t.ul[H] = (unsigned int)toppart;
911.1Smycroft	t.ul[L] = 0;
921.1Smycroft	x -= (double)t.uq;
931.1Smycroft	if (x < 0) {
941.1Smycroft		t.ul[H]--;
951.5Sscw		x += UINT_MAX;
961.1Smycroft	}
971.5Sscw	if (x > UINT_MAX) {
981.1Smycroft		t.ul[H]++;
991.5Sscw		x -= UINT_MAX;
1001.1Smycroft	}
1011.5Sscw	t.ul[L] = (u_int)x;
1021.1Smycroft	return (t.uq);
1031.1Smycroft}
104