s_nexttoward.c revision 143217
1143217Sdas/* @(#)s_nextafter.c 5.1 93/09/24 */ 2143217Sdas/* 3143217Sdas * ==================================================== 4143217Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5143217Sdas * 6143217Sdas * Developed at SunPro, a Sun Microsystems, Inc. business. 7143217Sdas * Permission to use, copy, modify, and distribute this 8143217Sdas * software is freely granted, provided that this notice 9143217Sdas * is preserved. 10143217Sdas * ==================================================== 11143217Sdas */ 12143217Sdas 13143217Sdas#ifndef lint 14143217Sdasstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_nexttoward.c 143217 2005-03-07 04:56:46Z das $"; 15143217Sdas#endif 16143217Sdas 17143217Sdas/* 18143217Sdas * We assume that a long double has a 15-bit exponent. On systems 19143217Sdas * where long double is the same as double, nexttoward() is an alias 20143217Sdas * for nextafter(), so we don't use this routine. 21143217Sdas */ 22143217Sdas 23143217Sdas#include <float.h> 24143217Sdas 25143217Sdas#include "fpmath.h" 26143217Sdas#include "math.h" 27143217Sdas#include "math_private.h" 28143217Sdas 29143217Sdas#if LDBL_MAX_EXP != 0x4000 30143217Sdas#error "Unsupported long double format" 31143217Sdas#endif 32143217Sdas 33143217Sdasdouble 34143217Sdasnexttoward(double x, long double y) 35143217Sdas{ 36143217Sdas union IEEEl2bits uy; 37143217Sdas volatile double t; 38143217Sdas int32_t hx,ix; 39143217Sdas u_int32_t lx; 40143217Sdas 41143217Sdas EXTRACT_WORDS(hx,lx,x); 42143217Sdas ix = hx&0x7fffffff; /* |x| */ 43143217Sdas uy.e = y; 44143217Sdas 45143217Sdas if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || 46143217Sdas (uy.bits.exp == 0x7fff && 47143217Sdas ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0)) 48143217Sdas return x+y; /* x or y is nan */ 49143217Sdas if(x==y) return (double)y; /* x=y, return y */ 50143217Sdas if(x==0.0) { 51143217Sdas INSERT_WORDS(x,uy.bits.sign<<31,1); /* return +-minsubnormal */ 52143217Sdas t = x*x; 53143217Sdas if(t==x) return t; else return x; /* raise underflow flag */ 54143217Sdas } 55143217Sdas if(hx>0.0 ^ x < y) { /* x -= ulp */ 56143217Sdas if(lx==0) hx -= 1; 57143217Sdas lx -= 1; 58143217Sdas } else { /* x += ulp */ 59143217Sdas lx += 1; 60143217Sdas if(lx==0) hx += 1; 61143217Sdas } 62143217Sdas ix = hx&0x7ff00000; 63143217Sdas if(ix>=0x7ff00000) return x+x; /* overflow */ 64143217Sdas if(ix<0x00100000) { /* underflow */ 65143217Sdas t = x*x; 66143217Sdas if(t!=x) { /* raise underflow flag */ 67143217Sdas INSERT_WORDS(y,hx,lx); 68143217Sdas return y; 69143217Sdas } 70143217Sdas } 71143217Sdas INSERT_WORDS(x,hx,lx); 72143217Sdas return x; 73143217Sdas} 74