1*84d9c625SLionel Sambuc /* $NetBSD: s_nexttoward.c,v 1.2 2013/08/21 13:03:56 martin Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /* @(#)s_nextafter.c 5.1 93/09/24 */
42fe8fb19SBen Gras /*
52fe8fb19SBen Gras * ====================================================
62fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
72fe8fb19SBen Gras *
82fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
92fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
102fe8fb19SBen Gras * software is freely granted, provided that this notice
112fe8fb19SBen Gras * is preserved.
122fe8fb19SBen Gras * ====================================================
132fe8fb19SBen Gras */
142fe8fb19SBen Gras
152fe8fb19SBen Gras #include <sys/cdefs.h>
16*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_nexttoward.c,v 1.2 2013/08/21 13:03:56 martin Exp $");
172fe8fb19SBen Gras
182fe8fb19SBen Gras /*
192fe8fb19SBen Gras * We assume that a long double has a 15-bit exponent. On systems
202fe8fb19SBen Gras * where long double is the same as double, nexttoward() is an alias
212fe8fb19SBen Gras * for nextafter(), so we don't use this routine.
222fe8fb19SBen Gras */
232fe8fb19SBen Gras #include <float.h>
242fe8fb19SBen Gras
252fe8fb19SBen Gras #include <machine/ieee.h>
262fe8fb19SBen Gras #include "math.h"
272fe8fb19SBen Gras #include "math_private.h"
282fe8fb19SBen Gras
292fe8fb19SBen Gras #if LDBL_MAX_EXP != 0x4000
302fe8fb19SBen Gras #error "Unsupported long double format"
312fe8fb19SBen Gras #endif
322fe8fb19SBen Gras
33*84d9c625SLionel Sambuc #ifdef LDBL_IMPLICIT_NBIT
34*84d9c625SLionel Sambuc #define LDBL_NBIT 0
35*84d9c625SLionel Sambuc #endif
36*84d9c625SLionel Sambuc
372fe8fb19SBen Gras /*
382fe8fb19SBen Gras * The nexttoward() function is equivalent to nextafter() function,
392fe8fb19SBen Gras * except that the second parameter shall have type long double and
402fe8fb19SBen Gras * the functions shall return y converted to the type of the function
412fe8fb19SBen Gras * if x equals y.
422fe8fb19SBen Gras *
432fe8fb19SBen Gras * Special cases: XXX
442fe8fb19SBen Gras */
452fe8fb19SBen Gras double
nexttoward(double x,long double y)462fe8fb19SBen Gras nexttoward(double x, long double y)
472fe8fb19SBen Gras {
482fe8fb19SBen Gras union ieee_ext_u uy;
492fe8fb19SBen Gras volatile double t;
502fe8fb19SBen Gras int32_t hx, ix;
512fe8fb19SBen Gras uint32_t lx;
522fe8fb19SBen Gras
532fe8fb19SBen Gras EXTRACT_WORDS(hx, lx, x);
542fe8fb19SBen Gras ix = hx & 0x7fffffff; /* |x| */
552fe8fb19SBen Gras uy.extu_ld = y;
562fe8fb19SBen Gras
572fe8fb19SBen Gras if (((ix >= 0x7ff00000) && ((ix - 0x7ff00000) | lx) != 0) ||
582fe8fb19SBen Gras (uy.extu_exp == 0x7fff &&
592fe8fb19SBen Gras ((uy.extu_frach & ~LDBL_NBIT) | uy.extu_fracl) != 0))
602fe8fb19SBen Gras return x+y; /* x or y is nan */
612fe8fb19SBen Gras
622fe8fb19SBen Gras if (x == y)
632fe8fb19SBen Gras return (double)y; /* x=y, return y */
642fe8fb19SBen Gras
652fe8fb19SBen Gras if (x == 0.0) {
662fe8fb19SBen Gras INSERT_WORDS(x, uy.extu_sign<<31, 1); /* return +-minsubnormal */
672fe8fb19SBen Gras t = x*x;
682fe8fb19SBen Gras if (t == x)
692fe8fb19SBen Gras return t;
702fe8fb19SBen Gras else
712fe8fb19SBen Gras return x; /* raise underflow flag */
722fe8fb19SBen Gras }
732fe8fb19SBen Gras
742fe8fb19SBen Gras if ((hx > 0.0) ^ (x < y)) { /* x -= ulp */
752fe8fb19SBen Gras if (lx == 0) hx -= 1;
762fe8fb19SBen Gras lx -= 1;
772fe8fb19SBen Gras } else { /* x += ulp */
782fe8fb19SBen Gras lx += 1;
792fe8fb19SBen Gras if (lx == 0) hx += 1;
802fe8fb19SBen Gras }
812fe8fb19SBen Gras ix = hx & 0x7ff00000;
822fe8fb19SBen Gras if (ix >= 0x7ff00000) return x+x; /* overflow */
832fe8fb19SBen Gras if (ix < 0x00100000) { /* underflow */
842fe8fb19SBen Gras t = x*x;
852fe8fb19SBen Gras if (t != x) { /* raise underflow flag */
862fe8fb19SBen Gras INSERT_WORDS(y, hx, lx);
872fe8fb19SBen Gras return y;
882fe8fb19SBen Gras }
892fe8fb19SBen Gras }
902fe8fb19SBen Gras INSERT_WORDS(x, hx, lx);
912fe8fb19SBen Gras
922fe8fb19SBen Gras return x;
932fe8fb19SBen Gras }
94