1ef94de73SDavid Schultz /*
2ef94de73SDavid Schultz * ====================================================
3ef94de73SDavid Schultz * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4ef94de73SDavid Schultz *
5ef94de73SDavid Schultz * Developed at SunPro, a Sun Microsystems, Inc. business.
6ef94de73SDavid Schultz * Permission to use, copy, modify, and distribute this
7ef94de73SDavid Schultz * software is freely granted, provided that this notice
8ef94de73SDavid Schultz * is preserved.
9ef94de73SDavid Schultz * ====================================================
10ef94de73SDavid Schultz */
11ef94de73SDavid Schultz
12ef94de73SDavid Schultz /*
13ef94de73SDavid Schultz * We assume that a long double has a 15-bit exponent. On systems
14ef94de73SDavid Schultz * where long double is the same as double, nexttoward() is an alias
15ef94de73SDavid Schultz * for nextafter(), so we don't use this routine.
16ef94de73SDavid Schultz */
17ef94de73SDavid Schultz
18ef94de73SDavid Schultz #include <float.h>
19ef94de73SDavid Schultz
20ef94de73SDavid Schultz #include "fpmath.h"
21ef94de73SDavid Schultz #include "math.h"
22ef94de73SDavid Schultz #include "math_private.h"
23ef94de73SDavid Schultz
24ef94de73SDavid Schultz #if LDBL_MAX_EXP != 0x4000
25ef94de73SDavid Schultz #error "Unsupported long double format"
26ef94de73SDavid Schultz #endif
27ef94de73SDavid Schultz
28ef94de73SDavid Schultz double
nexttoward(double x,long double y)29ef94de73SDavid Schultz nexttoward(double x, long double y)
30ef94de73SDavid Schultz {
31ef94de73SDavid Schultz union IEEEl2bits uy;
32ef94de73SDavid Schultz volatile double t;
33ef94de73SDavid Schultz int32_t hx,ix;
34ef94de73SDavid Schultz u_int32_t lx;
35ef94de73SDavid Schultz
36ef94de73SDavid Schultz EXTRACT_WORDS(hx,lx,x);
37ef94de73SDavid Schultz ix = hx&0x7fffffff; /* |x| */
38ef94de73SDavid Schultz uy.e = y;
39ef94de73SDavid Schultz
40ef94de73SDavid Schultz if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||
41ef94de73SDavid Schultz (uy.bits.exp == 0x7fff &&
42ef94de73SDavid Schultz ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
43ef94de73SDavid Schultz return x+y; /* x or y is nan */
44ef94de73SDavid Schultz if(x==y) return (double)y; /* x=y, return y */
45ef94de73SDavid Schultz if(x==0.0) {
46ef94de73SDavid Schultz INSERT_WORDS(x,uy.bits.sign<<31,1); /* return +-minsubnormal */
47ef94de73SDavid Schultz t = x*x;
48ef94de73SDavid Schultz if(t==x) return t; else return x; /* raise underflow flag */
49ef94de73SDavid Schultz }
50ef94de73SDavid Schultz if(hx>0.0 ^ x < y) { /* x -= ulp */
51ef94de73SDavid Schultz if(lx==0) hx -= 1;
52ef94de73SDavid Schultz lx -= 1;
53ef94de73SDavid Schultz } else { /* x += ulp */
54ef94de73SDavid Schultz lx += 1;
55ef94de73SDavid Schultz if(lx==0) hx += 1;
56ef94de73SDavid Schultz }
57ef94de73SDavid Schultz ix = hx&0x7ff00000;
58ef94de73SDavid Schultz if(ix>=0x7ff00000) return x+x; /* overflow */
59ef94de73SDavid Schultz if(ix<0x00100000) { /* underflow */
60ef94de73SDavid Schultz t = x*x;
61ef94de73SDavid Schultz if(t!=x) { /* raise underflow flag */
62*5ffd745eSDavid Schultz INSERT_WORDS(x,hx,lx);
63*5ffd745eSDavid Schultz return x;
64ef94de73SDavid Schultz }
65ef94de73SDavid Schultz }
66ef94de73SDavid Schultz INSERT_WORDS(x,hx,lx);
67ef94de73SDavid Schultz return x;
68ef94de73SDavid Schultz }
69