blob: 05c89f4554ec78bf01590e141df3522f696884b7 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4 *
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
8 * is preserved.
9 * ====================================================
10 */
11
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080012#include <float.h>
13
14#include "fpmath.h"
15#include "math.h"
16#include "math_private.h"
17
18#define LDBL_INFNAN_EXP (LDBL_MAX_EXP * 2 - 1)
19
20float
21nexttowardf(float x, long double y)
22{
23 union IEEEl2bits uy;
24 volatile float t;
25 int32_t hx,ix;
26
27 GET_FLOAT_WORD(hx,x);
28 ix = hx&0x7fffffff; /* |x| */
29 uy.e = y;
30
31 if((ix>0x7f800000) ||
32 (uy.bits.exp == LDBL_INFNAN_EXP &&
33 ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
34 return x+y; /* x or y is nan */
35 if(x==y) return (float)y; /* x=y, return y */
36 if(ix==0) { /* x == 0 */
37 SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
38 t = x*x;
39 if(t==x) return t; else return x; /* raise underflow flag */
40 }
41 if(hx>=0 ^ x < y) /* x -= ulp */
42 hx -= 1;
43 else /* x += ulp */
44 hx += 1;
45 ix = hx&0x7f800000;
46 if(ix>=0x7f800000) return x+x; /* overflow */
47 if(ix<0x00800000) { /* underflow */
48 t = x*x;
49 if(t!=x) { /* raise underflow flag */
Elliott Hughesa0ee0782013-01-30 19:06:37 -080050 SET_FLOAT_WORD(x,hx);
51 return x;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052 }
53 }
54 SET_FLOAT_WORD(x,hx);
55 return x;
56}