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