nextafter.c 709 字节
Newer Older
R
Rich Felker 已提交
1 2
#include "libm.h"

N
nsz 已提交
3 4
#define SIGN ((uint64_t)1<<63)

R
Rich Felker 已提交
5 6
double nextafter(double x, double y)
{
N
nsz 已提交
7 8 9
	union dshape ux, uy;
	uint64_t ax, ay;
	int e;
R
Rich Felker 已提交
10

N
nsz 已提交
11 12 13 14 15
	if (isnan(x) || isnan(y))
		return x + y;
	ux.value = x;
	uy.value = y;
	if (ux.bits == uy.bits)
R
Rich Felker 已提交
16
		return y;
N
nsz 已提交
17 18 19 20
	ax = ux.bits & ~SIGN;
	ay = uy.bits & ~SIGN;
	if (ax == 0) {
		if (ay == 0)
R
Rich Felker 已提交
21
			return y;
N
nsz 已提交
22 23 24 25 26 27 28 29 30 31
		ux.bits = (uy.bits & SIGN) | 1;
	} else if (ax > ay || ((ux.bits ^ uy.bits) & SIGN))
		ux.bits--;
	else
		ux.bits++;
	e = ux.bits >> 52 & 0x7ff;
	/* raise overflow if ux.value is infinite and x is finite */
	if (e == 0x7ff)
		return x + x;
	/* raise underflow if ux.value is subnormal or zero */
32 33
	if (e == 0)
		FORCE_EVAL(x*x + ux.value*ux.value);
N
nsz 已提交
34
	return ux.value;
R
Rich Felker 已提交
35
}