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 13176451Sdas#include <sys/cdefs.h> 14176451Sdas__FBSDID("$FreeBSD: releng/10.3/lib/msun/src/s_nexttoward.c 218510 2011-02-10 07:38:13Z das $"); 15143217Sdas 16143217Sdas/* 17143217Sdas * We assume that a long double has a 15-bit exponent. On systems 18143217Sdas * where long double is the same as double, nexttoward() is an alias 19143217Sdas * for nextafter(), so we don't use this routine. 20143217Sdas */ 21143217Sdas 22143217Sdas#include <float.h> 23143217Sdas 24143217Sdas#include "fpmath.h" 25143217Sdas#include "math.h" 26143217Sdas#include "math_private.h" 27143217Sdas 28143217Sdas#if LDBL_MAX_EXP != 0x4000 29143217Sdas#error "Unsupported long double format" 30143217Sdas#endif 31143217Sdas 32143217Sdasdouble 33143217Sdasnexttoward(double x, long double y) 34143217Sdas{ 35143217Sdas union IEEEl2bits uy; 36143217Sdas volatile double t; 37143217Sdas int32_t hx,ix; 38143217Sdas u_int32_t lx; 39143217Sdas 40143217Sdas EXTRACT_WORDS(hx,lx,x); 41143217Sdas ix = hx&0x7fffffff; /* |x| */ 42143217Sdas uy.e = y; 43143217Sdas 44143217Sdas if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || 45143217Sdas (uy.bits.exp == 0x7fff && 46143217Sdas ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0)) 47143217Sdas return x+y; /* x or y is nan */ 48143217Sdas if(x==y) return (double)y; /* x=y, return y */ 49143217Sdas if(x==0.0) { 50143217Sdas INSERT_WORDS(x,uy.bits.sign<<31,1); /* return +-minsubnormal */ 51143217Sdas t = x*x; 52143217Sdas if(t==x) return t; else return x; /* raise underflow flag */ 53143217Sdas } 54143217Sdas if(hx>0.0 ^ x < y) { /* x -= ulp */ 55143217Sdas if(lx==0) hx -= 1; 56143217Sdas lx -= 1; 57143217Sdas } else { /* x += ulp */ 58143217Sdas lx += 1; 59143217Sdas if(lx==0) hx += 1; 60143217Sdas } 61143217Sdas ix = hx&0x7ff00000; 62143217Sdas if(ix>=0x7ff00000) return x+x; /* overflow */ 63143217Sdas if(ix<0x00100000) { /* underflow */ 64143217Sdas t = x*x; 65143217Sdas if(t!=x) { /* raise underflow flag */ 66218510Sdas INSERT_WORDS(x,hx,lx); 67218510Sdas return x; 68143217Sdas } 69143217Sdas } 70143217Sdas INSERT_WORDS(x,hx,lx); 71143217Sdas return x; 72143217Sdas} 73