1143218Sdas/*
2143218Sdas * ====================================================
3143218Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4143218Sdas *
5143218Sdas * Developed at SunPro, a Sun Microsystems, Inc. business.
6143218Sdas * Permission to use, copy, modify, and distribute this
7143218Sdas * software is freely granted, provided that this notice
8143218Sdas * is preserved.
9143218Sdas * ====================================================
10143218Sdas */
11143218Sdas
12176451Sdas#include <sys/cdefs.h>
13176451Sdas__FBSDID("$FreeBSD: releng/11.0/lib/msun/src/s_nexttowardf.c 218511 2011-02-10 07:38:38Z das $");
14143218Sdas
15143218Sdas#include <float.h>
16143218Sdas
17143218Sdas#include "fpmath.h"
18143218Sdas#include "math.h"
19143218Sdas#include "math_private.h"
20143218Sdas
21143218Sdas#define	LDBL_INFNAN_EXP	(LDBL_MAX_EXP * 2 - 1)
22143218Sdas
23143218Sdasfloat
24143218Sdasnexttowardf(float x, long double y)
25143218Sdas{
26143218Sdas	union IEEEl2bits uy;
27143218Sdas	volatile float t;
28143218Sdas	int32_t hx,ix;
29143218Sdas
30143218Sdas	GET_FLOAT_WORD(hx,x);
31143218Sdas	ix = hx&0x7fffffff;		/* |x| */
32143218Sdas	uy.e = y;
33143218Sdas
34143218Sdas	if((ix>0x7f800000) ||
35143218Sdas	   (uy.bits.exp == LDBL_INFNAN_EXP &&
36143218Sdas	    ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
37143218Sdas	   return x+y;	/* x or y is nan */
38143218Sdas	if(x==y) return (float)y;		/* x=y, return y */
39143218Sdas	if(ix==0) {				/* x == 0 */
40143218Sdas	    SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
41143218Sdas	    t = x*x;
42143218Sdas	    if(t==x) return t; else return x;	/* raise underflow flag */
43143218Sdas	}
44143218Sdas	if(hx>=0 ^ x < y)			/* x -= ulp */
45143218Sdas	    hx -= 1;
46143218Sdas	else					/* x += ulp */
47143218Sdas	    hx += 1;
48143218Sdas	ix = hx&0x7f800000;
49143218Sdas	if(ix>=0x7f800000) return x+x;	/* overflow  */
50143218Sdas	if(ix<0x00800000) {		/* underflow */
51143218Sdas	    t = x*x;
52143218Sdas	    if(t!=x) {		/* raise underflow flag */
53218511Sdas	        SET_FLOAT_WORD(x,hx);
54218511Sdas		return x;
55143218Sdas	    }
56143218Sdas	}
57143218Sdas	SET_FLOAT_WORD(x,hx);
58143218Sdas	return x;
59143218Sdas}
60