about summary refs log tree commit diff
path: root/src/math/nextafterl.c
blob: c09d9dd0e0f2863722df950748ef1c0f16feed87 (plain) (blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "libm.h"

#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
long double nextafterl(long double x, long double y)
{
	return nextafter(x, y);
}
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
#define MSB ((uint64_t)1<<63)
long double nextafterl(long double x, long double y)
{
	union ldshape ux, uy;

	if (isnan(x) || isnan(y))
		return x + y;
	if (x == y)
		return y;
	ux.value = x;
	if (x == 0) {
		uy.value = y;
		ux.bits.m = 1;
		ux.bits.sign = uy.bits.sign;
	} else if (x < y ^ ux.bits.sign) {
		ux.bits.m++;
		if ((ux.bits.m & ~MSB) == 0) {
			ux.bits.m = MSB;
			ux.bits.exp++;
		}
	} else {
		if ((ux.bits.m & ~MSB) == 0) {
			ux.bits.exp--;
			if (ux.bits.exp)
				ux.bits.m = 0;
		}
		ux.bits.m--;
	}
	/* raise overflow if ux.value is infinite and x is finite */
	if (ux.bits.exp == 0x7fff)
		return x + x;
	/* raise underflow if ux.value is subnormal or zero */
	if (ux.bits.exp == 0) {
		volatile float z;
		z = x*x + ux.value*ux.value;
	}
	return ux.value;
}
#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
long double nextafterl(long double x, long double y)
{
	union ldshape ux, uy;

	if (isnan(x) || isnan(y))
		return x + y;
	if (x == y)
		return y;
	ux.value = x;
	if (x == 0) {
		uy.value = y;
		ux.bits.mlo = 1;
		ux.bits.sign = uy.bits.sign;
	} else if (x < y ^ ux.bits.sign) {
		ux.bits.mlo++;
		if (ux.bits.mlo == 0) {
			ux.bits.mhi++;
			if (ux.bits.mhi == 0)
				ux.bits.exp++;
		}
	} else {
		if (ux.bits.mlo == 0) {
			if (ux.bits.mhi == 0)
				ux.bits.exp--;
			ux.bits.mhi--;
		}
		ux.bits.mlo--;
	}
	/* raise overflow if ux.value is infinite and x is finite */
	if (ux.bits.exp == 0x7fff)
		return x + x;
	/* raise underflow if ux.value is subnormal or zero */
	if (ux.bits.exp == 0) {
		volatile float z;
		z = x*x + ux.value*ux.value;
	}
	return ux.value;
}
#endif