1ef94de73SDavid Schultz /* @(#)s_nextafter.c 5.1 93/09/24 */ 2ef94de73SDavid Schultz /* 3ef94de73SDavid Schultz * ==================================================== 4ef94de73SDavid Schultz * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5ef94de73SDavid Schultz * 6ef94de73SDavid Schultz * Developed at SunPro, a Sun Microsystems, Inc. business. 7ef94de73SDavid Schultz * Permission to use, copy, modify, and distribute this 8ef94de73SDavid Schultz * software is freely granted, provided that this notice 9ef94de73SDavid Schultz * is preserved. 10ef94de73SDavid Schultz * ==================================================== 11ef94de73SDavid Schultz */ 12ef94de73SDavid Schultz 13ef94de73SDavid Schultz #ifndef lint 14ef94de73SDavid Schultz static char rcsid[] = "$FreeBSD$"; 15ef94de73SDavid Schultz #endif 16ef94de73SDavid Schultz 17ef94de73SDavid Schultz /* 18ef94de73SDavid Schultz * We assume that a long double has a 15-bit exponent. On systems 19ef94de73SDavid Schultz * where long double is the same as double, nexttoward() is an alias 20ef94de73SDavid Schultz * for nextafter(), so we don't use this routine. 21ef94de73SDavid Schultz */ 22ef94de73SDavid Schultz 23ef94de73SDavid Schultz #include <float.h> 24ef94de73SDavid Schultz 25ef94de73SDavid Schultz #include "fpmath.h" 26ef94de73SDavid Schultz #include "math.h" 27ef94de73SDavid Schultz #include "math_private.h" 28ef94de73SDavid Schultz 29ef94de73SDavid Schultz #if LDBL_MAX_EXP != 0x4000 30ef94de73SDavid Schultz #error "Unsupported long double format" 31ef94de73SDavid Schultz #endif 32ef94de73SDavid Schultz 33ef94de73SDavid Schultz double 34ef94de73SDavid Schultz nexttoward(double x, long double y) 35ef94de73SDavid Schultz { 36ef94de73SDavid Schultz union IEEEl2bits uy; 37ef94de73SDavid Schultz volatile double t; 38ef94de73SDavid Schultz int32_t hx,ix; 39ef94de73SDavid Schultz u_int32_t lx; 40ef94de73SDavid Schultz 41ef94de73SDavid Schultz EXTRACT_WORDS(hx,lx,x); 42ef94de73SDavid Schultz ix = hx&0x7fffffff; /* |x| */ 43ef94de73SDavid Schultz uy.e = y; 44ef94de73SDavid Schultz 45ef94de73SDavid Schultz if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || 46ef94de73SDavid Schultz (uy.bits.exp == 0x7fff && 47ef94de73SDavid Schultz ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0)) 48ef94de73SDavid Schultz return x+y; /* x or y is nan */ 49ef94de73SDavid Schultz if(x==y) return (double)y; /* x=y, return y */ 50ef94de73SDavid Schultz if(x==0.0) { 51ef94de73SDavid Schultz INSERT_WORDS(x,uy.bits.sign<<31,1); /* return +-minsubnormal */ 52ef94de73SDavid Schultz t = x*x; 53ef94de73SDavid Schultz if(t==x) return t; else return x; /* raise underflow flag */ 54ef94de73SDavid Schultz } 55ef94de73SDavid Schultz if(hx>0.0 ^ x < y) { /* x -= ulp */ 56ef94de73SDavid Schultz if(lx==0) hx -= 1; 57ef94de73SDavid Schultz lx -= 1; 58ef94de73SDavid Schultz } else { /* x += ulp */ 59ef94de73SDavid Schultz lx += 1; 60ef94de73SDavid Schultz if(lx==0) hx += 1; 61ef94de73SDavid Schultz } 62ef94de73SDavid Schultz ix = hx&0x7ff00000; 63ef94de73SDavid Schultz if(ix>=0x7ff00000) return x+x; /* overflow */ 64ef94de73SDavid Schultz if(ix<0x00100000) { /* underflow */ 65ef94de73SDavid Schultz t = x*x; 66ef94de73SDavid Schultz if(t!=x) { /* raise underflow flag */ 67ef94de73SDavid Schultz INSERT_WORDS(y,hx,lx); 68ef94de73SDavid Schultz return y; 69ef94de73SDavid Schultz } 70ef94de73SDavid Schultz } 71ef94de73SDavid Schultz INSERT_WORDS(x,hx,lx); 72ef94de73SDavid Schultz return x; 73ef94de73SDavid Schultz } 74