POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/nextafter.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* @(#)s_nextafter.c 5.1 93/09/24 */
00018 /*
00019  * ====================================================
00020  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00021  *
00022  * Developed at SunPro, a Sun Microsystems, Inc. business.
00023  * Permission to use, copy, modify, and distribute this
00024  * software is freely granted, provided that this notice
00025  * is preserved.
00026  * ====================================================
00027  */
00028 
00029 #ifdef POK_NEEDS_LIBMATH
00030 
00031 /* IEEE functions
00032  *      nextafter(x,y)
00033  *      return the next machine floating-point number of x in the
00034  *      direction toward y.
00035  *   Special cases:
00036  */
00037 
00038 #include <libm.h>
00039 #include "math_private.h"
00040 
00041 double
00042 nextafter(double x, double y)
00043 {
00044         int32_t hx,hy,ix,iy;
00045         uint32_t lx,ly;
00046 
00047         EXTRACT_WORDS(hx,lx,x);
00048         EXTRACT_WORDS(hy,ly,y);
00049         ix = hx&0x7fffffff;             /* |x| */
00050         iy = hy&0x7fffffff;             /* |y| */
00051 
00052         if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
00053            ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */
00054            return x+y;
00055         if(x==y) return x;              /* x=y, return x */
00056         if((ix|lx)==0) {                        /* x == 0 */
00057             INSERT_WORDS(x,hy&0x80000000,1);    /* return +-minsubnormal */
00058             y = x*x;
00059             if(y==x) return y; else return x;   /* raise underflow flag */
00060         }
00061         if(hx>=0) {                             /* x > 0 */
00062             if(hx>hy||((hx==hy)&&(lx>ly))) {    /* x > y, x -= ulp */
00063                 if(lx==0) hx -= 1;
00064                 lx -= 1;
00065             } else {                            /* x < y, x += ulp */
00066                 lx += 1;
00067                 if(lx==0) hx += 1;
00068             }
00069         } else {                                /* x < 0 */
00070             if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
00071                 if(lx==0) hx -= 1;
00072                 lx -= 1;
00073             } else {                            /* x > y, x += ulp */
00074                 lx += 1;
00075                 if(lx==0) hx += 1;
00076             }
00077         }
00078         hy = hx&0x7ff00000;
00079         if(hy>=0x7ff00000) return x+x;  /* overflow  */
00080         if(hy<0x00100000) {             /* underflow */
00081             y = x*x;
00082             if(y!=x) {          /* raise underflow flag */
00083                 INSERT_WORDS(y,hx,lx);
00084                 return y;
00085             }
00086         }
00087         INSERT_WORDS(x,hx,lx);
00088         return x;
00089 }
00090 
00091 #endif