| 1 | /* s_nextafterf.c -- float version of s_nextafter.c. |
| 2 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * ==================================================== |
| 7 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 8 | * |
| 9 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 10 | * Permission to use, copy, modify, and distribute this |
| 11 | * software is freely granted, provided that this notice |
| 12 | * is preserved. |
| 13 | * ==================================================== |
| 14 | */ |
| 15 | |
| 16 | #if defined(LIBM_SCCS) && !defined(lint) |
| 17 | static char rcsid[] = "$NetBSD: s_nextafterf.c,v 1.4 1995/05/10 20:48:01 jtc Exp $" ; |
| 18 | #endif |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <math.h> |
| 22 | #include <math-barriers.h> |
| 23 | #include <math_private.h> |
| 24 | #include <libm-alias-float.h> |
| 25 | #include <float.h> |
| 26 | |
| 27 | float __nextafterf(float x, float y) |
| 28 | { |
| 29 | int32_t hx,hy,ix,iy; |
| 30 | |
| 31 | GET_FLOAT_WORD(hx,x); |
| 32 | GET_FLOAT_WORD(hy,y); |
| 33 | ix = hx&0x7fffffff; /* |x| */ |
| 34 | iy = hy&0x7fffffff; /* |y| */ |
| 35 | |
| 36 | if((ix>0x7f800000) || /* x is nan */ |
| 37 | (iy>0x7f800000)) /* y is nan */ |
| 38 | return x+y; |
| 39 | if(x==y) return y; /* x=y, return y */ |
| 40 | if(ix==0) { /* x == 0 */ |
| 41 | float u; |
| 42 | SET_FLOAT_WORD(x,(hy&0x80000000)|1);/* return +-minsubnormal */ |
| 43 | u = math_opt_barrier (x); |
| 44 | u = u*u; |
| 45 | math_force_eval (u); /* raise underflow flag */ |
| 46 | return x; |
| 47 | } |
| 48 | if(hx>=0) { /* x > 0 */ |
| 49 | if(hx>hy) { /* x > y, x -= ulp */ |
| 50 | hx -= 1; |
| 51 | } else { /* x < y, x += ulp */ |
| 52 | hx += 1; |
| 53 | } |
| 54 | } else { /* x < 0 */ |
| 55 | if(hy>=0||hx>hy){ /* x < y, x -= ulp */ |
| 56 | hx -= 1; |
| 57 | } else { /* x > y, x += ulp */ |
| 58 | hx += 1; |
| 59 | } |
| 60 | } |
| 61 | hy = hx&0x7f800000; |
| 62 | if(hy>=0x7f800000) { |
| 63 | float u = x+x; /* overflow */ |
| 64 | math_force_eval (u); |
| 65 | __set_errno (ERANGE); |
| 66 | } |
| 67 | if(hy<0x00800000) { |
| 68 | float u = x*x; /* underflow */ |
| 69 | math_force_eval (u); /* raise underflow flag */ |
| 70 | __set_errno (ERANGE); |
| 71 | } |
| 72 | SET_FLOAT_WORD(x,hx); |
| 73 | return x; |
| 74 | } |
| 75 | libm_alias_float (__nextafter, nextafter) |
| 76 | |