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 /* IEEE functions
13ef94de73SDavid Schultz * nextafter(x,y)
14ef94de73SDavid Schultz * return the next machine floating-point number of x in the
15ef94de73SDavid Schultz * direction toward y.
16ef94de73SDavid Schultz * Special cases:
17ef94de73SDavid Schultz */
18ef94de73SDavid Schultz
19ef94de73SDavid Schultz #include <float.h>
20ef94de73SDavid Schultz
21ef94de73SDavid Schultz #include "fpmath.h"
22ef94de73SDavid Schultz #include "math.h"
23ef94de73SDavid Schultz #include "math_private.h"
24ef94de73SDavid Schultz
25ef94de73SDavid Schultz #if LDBL_MAX_EXP != 0x4000
26ef94de73SDavid Schultz #error "Unsupported long double format"
27ef94de73SDavid Schultz #endif
28ef94de73SDavid Schultz
29ef94de73SDavid Schultz long double
nextafterl(long double x,long double y)30ef94de73SDavid Schultz nextafterl(long double x, long double y)
31ef94de73SDavid Schultz {
32ef94de73SDavid Schultz volatile long double t;
33ef94de73SDavid Schultz union IEEEl2bits ux, uy;
34ef94de73SDavid Schultz
35ef94de73SDavid Schultz ux.e = x;
36ef94de73SDavid Schultz uy.e = y;
37ef94de73SDavid Schultz
38ef94de73SDavid Schultz if ((ux.bits.exp == 0x7fff &&
39ef94de73SDavid Schultz ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl) != 0) ||
40ef94de73SDavid Schultz (uy.bits.exp == 0x7fff &&
41ef94de73SDavid Schultz ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
42ef94de73SDavid Schultz return x+y; /* x or y is nan */
43ef94de73SDavid Schultz if(x==y) return y; /* x=y, return y */
44ef94de73SDavid Schultz if(x==0.0) {
45ef94de73SDavid Schultz ux.bits.manh = 0; /* return +-minsubnormal */
46ef94de73SDavid Schultz ux.bits.manl = 1;
47ef94de73SDavid Schultz ux.bits.sign = uy.bits.sign;
48ef94de73SDavid Schultz t = ux.e*ux.e;
49ef94de73SDavid Schultz if(t==ux.e) return t; else return ux.e; /* raise underflow flag */
50ef94de73SDavid Schultz }
51ef94de73SDavid Schultz if(x>0.0 ^ x<y) { /* x -= ulp */
52ef94de73SDavid Schultz if(ux.bits.manl==0) {
53ef94de73SDavid Schultz if ((ux.bits.manh&~LDBL_NBIT)==0)
54ef94de73SDavid Schultz ux.bits.exp -= 1;
55ef94de73SDavid Schultz ux.bits.manh = (ux.bits.manh - 1) | (ux.bits.manh & LDBL_NBIT);
56ef94de73SDavid Schultz }
57ef94de73SDavid Schultz ux.bits.manl -= 1;
58ef94de73SDavid Schultz } else { /* x += ulp */
59ef94de73SDavid Schultz ux.bits.manl += 1;
60ef94de73SDavid Schultz if(ux.bits.manl==0) {
61ef94de73SDavid Schultz ux.bits.manh = (ux.bits.manh + 1) | (ux.bits.manh & LDBL_NBIT);
62ef94de73SDavid Schultz if ((ux.bits.manh&~LDBL_NBIT)==0)
63ef94de73SDavid Schultz ux.bits.exp += 1;
64ef94de73SDavid Schultz }
65ef94de73SDavid Schultz }
66ef94de73SDavid Schultz if(ux.bits.exp==0x7fff) return x+x; /* overflow */
67ef94de73SDavid Schultz if(ux.bits.exp==0) { /* underflow */
68ef94de73SDavid Schultz mask_nbit_l(ux);
69ef94de73SDavid Schultz t = ux.e * ux.e;
70ef94de73SDavid Schultz if(t!=ux.e) /* raise underflow flag */
71ef94de73SDavid Schultz return ux.e;
72ef94de73SDavid Schultz }
73ef94de73SDavid Schultz return ux.e;
74ef94de73SDavid Schultz }
75ef94de73SDavid Schultz
76ef94de73SDavid Schultz __strong_reference(nextafterl, nexttowardl);
77