nexttowardf.c 647 字节
Newer Older
R
Rich Felker 已提交
1 2 3 4
#include "libm.h"

float nexttowardf(float x, long double y)
{
N
nsz 已提交
5 6
	union fshape ux;
	uint32_t e;
R
Rich Felker 已提交
7

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