1 | /* s_nexttoward.c |
2 | * Conversion from s_nextafter.c by Ulrich Drepper, Cygnus Support, |
3 | * drepper@cygnus.com and Jakub Jelinek, jj@ultra.linux.cz. |
4 | */ |
5 | |
6 | /* |
7 | * ==================================================== |
8 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
9 | * |
10 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
11 | * Permission to use, copy, modify, and distribute this |
12 | * software is freely granted, provided that this notice |
13 | * is preserved. |
14 | * ==================================================== |
15 | */ |
16 | |
17 | #if defined(LIBM_SCCS) && !defined(lint) |
18 | static char rcsid[] = "$NetBSD: $" ; |
19 | #endif |
20 | |
21 | /* IEEE functions |
22 | * nexttoward(x,y) |
23 | * return the next machine floating-point number of x in the |
24 | * direction toward y. |
25 | * Special cases: |
26 | */ |
27 | |
28 | #include <errno.h> |
29 | #include <math.h> |
30 | #include <math-barriers.h> |
31 | #include <math_private.h> |
32 | #include <math_ldbl_opt.h> |
33 | #include <float.h> |
34 | |
35 | double __nexttoward(double x, long double y) |
36 | { |
37 | int32_t hx,ix; |
38 | int64_t hy,iy; |
39 | uint32_t lx; |
40 | double yhi; |
41 | |
42 | EXTRACT_WORDS(hx,lx,x); |
43 | yhi = ldbl_high (y); |
44 | EXTRACT_WORDS64(hy,yhi); |
45 | ix = hx&0x7fffffff; /* |x| */ |
46 | iy = hy&0x7fffffffffffffffLL; /* |y| */ |
47 | |
48 | if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */ |
49 | iy>0x7ff0000000000000LL) /* y is nan */ |
50 | return x+y; |
51 | if((long double) x==y) return y; /* x=y, return y */ |
52 | if((ix|lx)==0) { /* x == 0 */ |
53 | double u; |
54 | INSERT_WORDS(x,(uint32_t)((hy>>32)&0x80000000),1);/* return +-minsub */ |
55 | u = math_opt_barrier (x); |
56 | u = u * u; |
57 | math_force_eval (u); /* raise underflow flag */ |
58 | return x; |
59 | } |
60 | if(hx>=0) { /* x > 0 */ |
61 | if (x > y) { /* x > 0 */ |
62 | if(lx==0) hx -= 1; |
63 | lx -= 1; |
64 | } else { /* x < y, x += ulp */ |
65 | lx += 1; |
66 | if(lx==0) hx += 1; |
67 | } |
68 | } else { /* x < 0 */ |
69 | if (x < y) { /* x < 0 */ |
70 | if(lx==0) hx -= 1; |
71 | lx -= 1; |
72 | } else { /* x > y, x += ulp */ |
73 | lx += 1; |
74 | if(lx==0) hx += 1; |
75 | } |
76 | } |
77 | hy = hx&0x7ff00000; |
78 | if(hy>=0x7ff00000) { |
79 | double u = x+x; /* overflow */ |
80 | math_force_eval (u); |
81 | __set_errno (ERANGE); |
82 | } |
83 | if(hy<0x00100000) { |
84 | double u = x*x; /* underflow */ |
85 | math_force_eval (u); /* raise underflow flag */ |
86 | __set_errno (ERANGE); |
87 | } |
88 | INSERT_WORDS(x,hx,lx); |
89 | return x; |
90 | } |
91 | long_double_symbol (libm, __nexttoward, nexttoward); |
92 | |