Rev 4872 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 1 | |
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 |
||
8 | * software is freely granted, provided that this notice |
||
9 | * is preserved. |
||
10 | * ==================================================== |
||
11 | */ |
||
12 | |||
13 | |||
14 | FUNCTION |
||
15 | < |
||
16 | |||
17 | |||
18 | nextafter |
||
19 | INDEX |
||
20 | nextafterf |
||
21 | |||
22 | |||
23 | #include |
||
24 | double nextafter(double <[val]>, double <[dir]>); |
||
25 | float nextafterf(float <[val]>, float <[dir]>); |
||
26 | |||
27 | |||
28 | #include |
||
29 | |||
30 | |||
31 | double <[val]>; |
||
32 | double <[exp]>; |
||
33 | |||
34 | |||
35 | float <[val]>; |
||
36 | float <[dir]>; |
||
37 | |||
38 | |||
39 | |||
40 | < |
||
41 | closest to <[val]> in the direction toward <[dir]>. < |
||
42 | performs the same operation in single precision. For example, |
||
43 | < |
||
44 | representable in double precision. |
||
45 | |||
46 | |||
47 | Returns the next closest number to <[val]> in the direction toward |
||
48 | <[dir]>. |
||
49 | |||
50 | |||
51 | Neither < |
||
52 | or by the System V Interface Definition (Issue 2). |
||
53 | */ |
||
54 | |||
55 | |||
56 | * nextafter(x,y) |
||
57 | * return the next machine floating-point number of x in the |
||
58 | * direction toward y. |
||
59 | * Special cases: |
||
60 | */ |
||
61 | |||
62 | |||
63 | |||
64 | |||
65 | |||
66 | |||
67 | double nextafter(double x, double y) |
||
68 | #else |
||
69 | double nextafter(x,y) |
||
70 | double x,y; |
||
71 | #endif |
||
72 | { |
||
73 | __int32_t hx,hy,ix,iy; |
||
74 | __uint32_t lx,ly; |
||
75 | |||
76 | |||
77 | EXTRACT_WORDS(hy,ly,y); |
||
78 | ix = hx&0x7fffffff; /* |x| */ |
||
79 | iy = hy&0x7fffffff; /* |y| */ |
||
80 | |||
81 | |||
82 | ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ |
||
83 | return x+y; |
||
84 | if(x==y) return x; /* x=y, return x */ |
||
85 | if((ix|lx)==0) { /* x == 0 */ |
||
86 | INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */ |
||
87 | y = x*x; |
||
88 | if(y==x) return y; else return x; /* raise underflow flag */ |
||
89 | } |
||
90 | if(hx>=0) { /* x > 0 */ |
||
91 | if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */ |
||
92 | if(lx==0) hx -= 1; |
||
93 | lx -= 1; |
||
94 | } else { /* x < y, x += ulp */ |
||
95 | lx += 1; |
||
96 | if(lx==0) hx += 1; |
||
97 | } |
||
98 | } else { /* x < 0 */ |
||
99 | if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */ |
||
100 | if(lx==0) hx -= 1; |
||
101 | lx -= 1; |
||
102 | } else { /* x > y, x += ulp */ |
||
103 | lx += 1; |
||
104 | if(lx==0) hx += 1; |
||
105 | } |
||
106 | } |
||
107 | hy = hx&0x7ff00000; |
||
108 | if(hy>=0x7ff00000) return x+x; /* overflow */ |
||
109 | if(hy<0x00100000) { /* underflow */ |
||
110 | y = x*x; |
||
111 | if(y!=x) { /* raise underflow flag */ |
||
112 | INSERT_WORDS(y,hx,lx); |
||
113 | return y; |
||
114 | } |
||
115 | } |
||
116 | INSERT_WORDS(x,hx,lx); |
||
117 | return x; |
||
118 | } |
||
119 | |||
120 | |||
121 |