Back to index

glibc  2.9
s_nexttowardf.c
Go to the documentation of this file.
00001 /* s_nexttowardf.c -- float version of s_nextafter.c.
00002  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com
00003  * and Jakub Jelinek, jj@ultra.linux.cz.
00004  */
00005 
00006 /*
00007  * ====================================================
00008  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00009  *
00010  * Developed at SunPro, a Sun Microsystems, Inc. business.
00011  * Permission to use, copy, modify, and distribute this
00012  * software is freely granted, provided that this notice
00013  * is preserved.
00014  * ====================================================
00015  */
00016 
00017 #if defined(LIBM_SCCS) && !defined(lint)
00018 static char rcsid[] = "$NetBSD: $";
00019 #endif
00020 
00021 #include "math.h"
00022 #include "math_private.h"
00023 
00024 #ifdef __STDC__
00025        float __nexttowardf(float x, long double y)
00026 #else
00027        float __nexttowardf(x,y)
00028        float x;
00029        long double y;
00030 #endif
00031 {
00032        int32_t hx,ix;
00033        int64_t hy,iy;
00034        u_int64_t ly;
00035                                 
00036        GET_FLOAT_WORD(hx,x);
00037        GET_LDOUBLE_WORDS64(hy,ly,y);
00038        ix = hx&0x7fffffff;         /* |x| */
00039        iy = hy&0x7fffffffffffffffLL;      /* |y| */
00040 
00041        if((ix>0x7f800000) ||   /* x is nan */
00042           ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|ly)!=0))
00043                             /* y is nan */
00044           return x+y;
00045        if((long double) x==y) return y;   /* x=y, return y */
00046        if(ix==0) {                        /* x == 0 */
00047            float x2;
00048            SET_FLOAT_WORD(x,(u_int32_t)((hy>>32)&0x80000000)|1);/* return +-minsub*/
00049            x2 = x*x;
00050            if(x2==x) return x2; else return x;   /* raise underflow flag */
00051        }
00052        if(hx>=0) {                        /* x > 0 */
00053            if(hy<0||(ix>>23)>(iy>>48)-0x3f80
00054               || ((ix>>23)==(iy>>48)-0x3f80
00055                  && (ix&0x7fffff)>((hy>>25)&0x7fffff))) {/* x > y, x -= ulp */
00056               hx -= 1;
00057            } else {                       /* x < y, x += ulp */
00058               hx += 1;
00059            }
00060        } else {                           /* x < 0 */
00061            if(hy>=0||(ix>>23)>(iy>>48)-0x3f80
00062               || ((ix>>23)==(iy>>48)-0x3f80
00063                  && (ix&0x7fffff)>((hy>>25)&0x7fffff))) {/* x < y, x -= ulp */
00064               hx -= 1;
00065            } else {                       /* x > y, x += ulp */
00066               hx += 1;
00067            }
00068        }
00069        hy = hx&0x7f800000;
00070        if(hy>=0x7f800000) return x+x;     /* overflow  */
00071        if(hy<0x00800000) {         /* underflow */
00072            float x2 = x*x;
00073            if(x2!=x) {             /* raise underflow flag */
00074                SET_FLOAT_WORD(x2,hx);
00075               return x2;
00076            }
00077        }
00078        SET_FLOAT_WORD(x,hx);
00079        return x;
00080 }
00081 weak_alias (__nexttowardf, nexttowardf)