| 1 | /* s_nexttowardf.c -- float version of s_nextafter.c. |
| 2 | * Special i387 version. |
| 3 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
| 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 | #include <errno.h> |
| 22 | #include <math.h> |
| 23 | #include <math-barriers.h> |
| 24 | #include <math_private.h> |
| 25 | #include <float.h> |
| 26 | |
| 27 | float __nexttowardf(float x, long double y) |
| 28 | { |
| 29 | int32_t hx,ix,iy; |
| 30 | uint32_t hy,ly,esy; |
| 31 | |
| 32 | GET_FLOAT_WORD(hx,x); |
| 33 | GET_LDOUBLE_WORDS(esy,hy,ly,y); |
| 34 | ix = hx&0x7fffffff; /* |x| */ |
| 35 | iy = esy&0x7fff; /* |y| */ |
| 36 | |
| 37 | /* Intel's extended format has the normally implicit 1 explicit |
| 38 | present. Sigh! */ |
| 39 | if((ix>0x7f800000) || /* x is nan */ |
| 40 | (iy>=0x7fff&&(((hy&0x7fffffff)|ly)!=0))) /* y is nan */ |
| 41 | return x+y; |
| 42 | if((long double) x==y) return y; /* x=y, return y */ |
| 43 | if(ix==0) { /* x == 0 */ |
| 44 | float u; |
| 45 | SET_FLOAT_WORD(x,((esy&0x8000)<<16)|1);/* return +-minsub*/ |
| 46 | u = math_opt_barrier (x); |
| 47 | u = u * u; |
| 48 | math_force_eval (u); /* raise underflow flag */ |
| 49 | return x; |
| 50 | } |
| 51 | if(hx>=0) { /* x > 0 */ |
| 52 | if(x > y) { /* x -= ulp */ |
| 53 | hx -= 1; |
| 54 | } else { /* x < y, x += ulp */ |
| 55 | hx += 1; |
| 56 | } |
| 57 | } else { /* x < 0 */ |
| 58 | if(x < y) { /* x -= ulp */ |
| 59 | hx -= 1; |
| 60 | } else { /* x > y, x += ulp */ |
| 61 | hx += 1; |
| 62 | } |
| 63 | } |
| 64 | hy = hx&0x7f800000; |
| 65 | if(hy>=0x7f800000) { |
| 66 | float u = x+x; /* overflow */ |
| 67 | math_force_eval (u); |
| 68 | __set_errno (ERANGE); |
| 69 | } |
| 70 | if(hy<0x00800000) { |
| 71 | float u = x*x; /* underflow */ |
| 72 | math_force_eval (u); /* raise underflow flag */ |
| 73 | __set_errno (ERANGE); |
| 74 | } |
| 75 | SET_FLOAT_WORD(x,hx); |
| 76 | return x; |
| 77 | } |
| 78 | weak_alias (__nexttowardf, nexttowardf) |
| 79 | |