[BACK]Return to s_nexttoward.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libm / src

File: [cvs.NetBSD.org] / src / lib / libm / src / s_nexttoward.c (download)

Revision 1.1, Wed Sep 15 16:12:05 2010 UTC (13 years, 6 months ago) by christos
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-mips64-premerge-20101231, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver
Branch point for: yamt-pagecache, tls-maxphys

commit long double SoC code from Stathis Kamperis

/*	$NetBSD: s_nexttoward.c,v 1.1 2010/09/15 16:12:05 christos Exp $	*/

/* @(#)s_nextafter.c 5.1 93/09/24 */
/*
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 * ====================================================
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: s_nexttoward.c,v 1.1 2010/09/15 16:12:05 christos Exp $");

/*
 * We assume that a long double has a 15-bit exponent.  On systems
 * where long double is the same as double, nexttoward() is an alias
 * for nextafter(), so we don't use this routine.
 */
#include <float.h>

#include <machine/ieee.h>
#include "math.h"
#include "math_private.h"

#if LDBL_MAX_EXP != 0x4000
#error "Unsupported long double format"
#endif

/*
 * The nexttoward() function is equivalent to nextafter() function,
 * except that the second parameter shall have type long double and
 * the functions shall return y converted to the type of the function
 * if x equals y.
 *
 * Special cases: XXX
 */
double
nexttoward(double x, long double y)
{
	union ieee_ext_u uy;
	volatile double t;
	int32_t hx, ix;
	uint32_t lx;

	EXTRACT_WORDS(hx, lx, x);
	ix = hx & 0x7fffffff;			/* |x| */
	uy.extu_ld = y;

	if (((ix >= 0x7ff00000) && ((ix - 0x7ff00000) | lx) != 0) ||
	    (uy.extu_exp == 0x7fff &&
		((uy.extu_frach & ~LDBL_NBIT) | uy.extu_fracl) != 0))
		return x+y;			/* x or y is nan */

	if (x == y)
		return (double)y;		/* x=y, return y */

	if (x == 0.0) {
		INSERT_WORDS(x, uy.extu_sign<<31, 1);	/* return +-minsubnormal */
		t = x*x;
		if (t == x)
			return t;
		else
			return x;		/* raise underflow flag */
	}

	if ((hx > 0.0) ^ (x < y)) {		/* x -= ulp */
		if (lx == 0) hx -= 1;
		lx -= 1;
	} else {				/* x += ulp */
		lx += 1;
		if (lx == 0) hx += 1;
	}
	ix = hx & 0x7ff00000;
	if (ix >= 0x7ff00000) return x+x;	/* overflow  */
	if (ix <  0x00100000) {			/* underflow */
		t = x*x;
		if (t != x) {			/* raise underflow flag */
			INSERT_WORDS(y, hx, lx);
			return y;
		}
	}
	INSERT_WORDS(x, hx, lx);

	return x;
}