summaryrefslogtreecommitdiff
path: root/src/math/nexttowardf.c
blob: 821f72a5c109f22816b9a10ceeb564c66c9932dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "libm.h"

float nexttowardf(float x, long double y)
{
	union fshape ux;
	uint32_t e;

	if (isnan(x) || isnan(y))
		return x + y;
	if (x == y)
		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--;
	}
	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 */
	if (e == 0) {
		volatile float z = x*x + ux.value*ux.value;
	}
	return ux.value;
}