Back to index

lightning-sunbird  0.9+nobinonly
s_nextafter.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)s_nextafter.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  */
00051 
00052 /* IEEE functions
00053  *     nextafter(x,y)
00054  *     return the next machine floating-point number of x in the
00055  *     direction toward y.
00056  *   Special cases:
00057  */
00058 
00059 #include "fdlibm.h"
00060 
00061 #ifdef __STDC__
00062        double fd_nextafter(double x, double y)
00063 #else
00064        double fd_nextafter(x,y)
00065        double x,y;
00066 #endif
00067 {
00068        int    hx,hy,ix,iy;
00069        unsigned lx,ly;
00070         fd_twoints ux, uy;
00071 
00072         ux.d = x; uy.d = y;
00073        hx = __HI(ux);              /* high word of x */
00074        lx = __LO(ux);              /* low  word of x */
00075        hy = __HI(uy);              /* high word of y */
00076        ly = __LO(uy);              /* low  word of y */
00077        ix = hx&0x7fffffff;         /* |x| */
00078        iy = hy&0x7fffffff;         /* |y| */
00079 
00080        if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */ 
00081           ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */ 
00082           return x+y;                            
00083        if(x==y) return x;          /* x=y, return x */
00084        if((ix|lx)==0) {                   /* x == 0 */
00085             ux.d = x;
00086            __HI(ux) = hy&0x80000000;      /* return +-minsubnormal */
00087            __LO(ux) = 1;
00088             x = ux.d;
00089            y = x*x;
00090            if(y==x) return y; else return x;     /* raise underflow flag */
00091        } 
00092        if(hx>=0) {                        /* x > 0 */
00093            if(hx>hy||((hx==hy)&&(lx>ly))) {      /* x > y, x -= ulp */
00094               if(lx==0) hx -= 1;
00095               lx -= 1;
00096            } else {                       /* x < y, x += ulp */
00097               lx += 1;
00098               if(lx==0) hx += 1;
00099            }
00100        } else {                           /* x < 0 */
00101            if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
00102               if(lx==0) hx -= 1;
00103               lx -= 1;
00104            } else {                       /* x > y, x += ulp */
00105               lx += 1;
00106               if(lx==0) hx += 1;
00107            }
00108        }
00109        hy = hx&0x7ff00000;
00110        if(hy>=0x7ff00000) return x+x;     /* overflow  */
00111        if(hy<0x00100000) {         /* underflow */
00112            y = x*x;
00113            if(y!=x) {              /* raise underflow flag */
00114                 uy.d = y;
00115               __HI(uy) = hx; __LO(uy) = lx;
00116                 y = uy.d;
00117               return y;
00118            }
00119        }
00120         ux.d = x;
00121        __HI(ux) = hx; __LO(ux) = lx;
00122         x = ux.d;
00123        return x;
00124 }