1 /* $NetBSD: s_nexttoward.c,v 1.1 2010/09/15 16:12:05 christos Exp $ */
3 /* @(#)s_nextafter.c 5.1 93/09/24 */
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 * Developed at SunPro, a Sun Microsystems, Inc. business.
9 * Permission to use, copy, modify, and distribute this
10 * software is freely granted, provided that this notice
12 * ====================================================
16 * We assume that a long double has a 15-bit exponent. On systems
17 * where long double is the same as double, nexttoward() is an alias
18 * for nextafter(), so we don't use this routine.
22 #include <machine/ieee.h>
24 #include "math_private.h"
26 #if LDBL_MAX_EXP != 0x4000
27 #error "Unsupported long double format"
31 * The nexttoward() function is equivalent to nextafter() function,
32 * except that the second parameter shall have type long double and
33 * the functions shall return y converted to the type of the function
39 nexttoward(double x, long double y)
46 EXTRACT_WORDS(hx, lx, x);
47 ix = hx & 0x7fffffff; /* |x| */
50 if (((ix >= 0x7ff00000) && ((ix - 0x7ff00000) | lx) != 0) ||
51 (uy.extu_exp == 0x7fff &&
52 ((uy.extu_frach & ~LDBL_NBIT) | uy.extu_fracl) != 0))
53 return x+y; /* x or y is nan */
56 return (double)y; /* x=y, return y */
59 INSERT_WORDS(x, uy.extu_sign<<31, 1); /* return +-minsubnormal */
64 return x; /* raise underflow flag */
67 if ((hx > 0.0) ^ (x < y)) { /* x -= ulp */
70 } else { /* x += ulp */
75 if (ix >= 0x7ff00000) return x+x; /* overflow */
76 if (ix < 0x00100000) { /* underflow */
78 if (t != x) { /* raise underflow flag */
79 INSERT_WORDS(y, hx, lx);
83 INSERT_WORDS(x, hx, lx);