]>
Commit | Line | Data |
---|---|---|
f7eac6eb RM |
1 | /* @(#)s_nextafter.c 5.1 93/09/24 */ |
2 | /* | |
3 | * ==================================================== | |
4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | |
5 | * | |
6 | * Developed at SunPro, a Sun Microsystems, Inc. business. | |
7 | * Permission to use, copy, modify, and distribute this | |
cccda09f | 8 | * software is freely granted, provided that this notice |
f7eac6eb RM |
9 | * is preserved. |
10 | * ==================================================== | |
11 | */ | |
12 | ||
13 | #if defined(LIBM_SCCS) && !defined(lint) | |
14 | static char rcsid[] = "$NetBSD: s_nextafter.c,v 1.8 1995/05/10 20:47:58 jtc Exp $"; | |
15 | #endif | |
16 | ||
17 | /* IEEE functions | |
18 | * nextafter(x,y) | |
19 | * return the next machine floating-point number of x in the | |
20 | * direction toward y. | |
21 | * Special cases: | |
22 | */ | |
23 | ||
249fd241 UD |
24 | /* Ugly hack so that the aliasing works. */ |
25 | #define __nexttoward __internal___nexttoward | |
26 | #define nexttoward __internal_nexttoward | |
27 | ||
f7eac6eb RM |
28 | #include "math.h" |
29 | #include "math_private.h" | |
30 | ||
31 | #ifdef __STDC__ | |
32 | double __nextafter(double x, double y) | |
33 | #else | |
34 | double __nextafter(x,y) | |
35 | double x,y; | |
36 | #endif | |
37 | { | |
38 | int32_t hx,hy,ix,iy; | |
39 | u_int32_t lx,ly; | |
40 | ||
41 | EXTRACT_WORDS(hx,lx,x); | |
42 | EXTRACT_WORDS(hy,ly,y); | |
43 | ix = hx&0x7fffffff; /* |x| */ | |
44 | iy = hy&0x7fffffff; /* |y| */ | |
45 | ||
cccda09f UD |
46 | if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */ |
47 | ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ | |
48 | return x+y; | |
bc9f6000 | 49 | if(x==y) return y; /* x=y, return y */ |
f7eac6eb RM |
50 | if((ix|lx)==0) { /* x == 0 */ |
51 | INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */ | |
52 | y = x*x; | |
53 | if(y==x) return y; else return x; /* raise underflow flag */ | |
cccda09f | 54 | } |
f7eac6eb RM |
55 | if(hx>=0) { /* x > 0 */ |
56 | if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */ | |
57 | if(lx==0) hx -= 1; | |
58 | lx -= 1; | |
59 | } else { /* x < y, x += ulp */ | |
60 | lx += 1; | |
61 | if(lx==0) hx += 1; | |
62 | } | |
63 | } else { /* x < 0 */ | |
64 | if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */ | |
65 | if(lx==0) hx -= 1; | |
66 | lx -= 1; | |
67 | } else { /* x > y, x += ulp */ | |
68 | lx += 1; | |
69 | if(lx==0) hx += 1; | |
70 | } | |
71 | } | |
72 | hy = hx&0x7ff00000; | |
73 | if(hy>=0x7ff00000) return x+x; /* overflow */ | |
74 | if(hy<0x00100000) { /* underflow */ | |
75 | y = x*x; | |
76 | if(y!=x) { /* raise underflow flag */ | |
77 | INSERT_WORDS(y,hx,lx); | |
78 | return y; | |
79 | } | |
80 | } | |
81 | INSERT_WORDS(x,hx,lx); | |
82 | return x; | |
83 | } | |
84 | weak_alias (__nextafter, nextafter) | |
cccda09f UD |
85 | #ifdef NO_LONG_DOUBLE |
86 | strong_alias (__nextafter, __nextafterl) | |
87 | weak_alias (__nextafter, nextafterl) | |
90dd37a6 AJ |
88 | strong_alias (__nextafter, __nexttowardl) |
89 | weak_alias (__nexttowardl, nexttowardl) | |
d03db7f0 | 90 | #undef __nexttoward |
abfbdde1 | 91 | strong_alias (__nextafter, __nexttoward) |
d03db7f0 | 92 | #undef nexttoward |
abfbdde1 | 93 | weak_alias (__nextafter, nexttoward) |
cccda09f | 94 | #endif |