summaryrefslogtreecommitdiff
path: root/lib/libm/fmodf.c
blob: 6bcd398caf94651419beb0bf5935abe25087e778 (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
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
#include <math.h>
#include <stdint.h>

float fmodf(float x, float y)
{
	union {
		float f;
		uint32_t i;
	} ux = { x }, uy = { y };
	int ex = ux.i >> 23 & 0xff;
	int ey = uy.i >> 23 & 0xff;
	uint32_t sx = ux.i & 0x80000000;
	uint32_t i;
	uint32_t uxi = ux.i;

	if (uy.i << 1 == 0 || isnan(y) || ex == 0xff)
		return (x * y) / (x * y);
	if (uxi << 1 <= uy.i << 1) {
		if (uxi << 1 == uy.i << 1)
			return 0 * x;
		return x;
	}

	/* normalize x and y */
	if (!ex) {
		for (i = uxi << 9; i >> 31 == 0; ex--, i <<= 1)
			;
		uxi <<= -ex + 1;
	} else {
		uxi &= -1U >> 9;
		uxi |= 1U << 23;
	}
	if (!ey) {
		for (i = uy.i << 9; i >> 31 == 0; ey--, i <<= 1)
			;
		uy.i <<= -ey + 1;
	} else {
		uy.i &= -1U >> 9;
		uy.i |= 1U << 23;
	}

	/* x mod y */
	for (; ex > ey; ex--) {
		i = uxi - uy.i;
		if (i >> 31 == 0) {
			if (i == 0)
				return 0 * x;
			uxi = i;
		}
		uxi <<= 1;
	}
	i = uxi - uy.i;
	if (i >> 31 == 0) {
		if (i == 0)
			return 0 * x;
		uxi = i;
	}
	for (; uxi >> 23 == 0; uxi <<= 1, ex--)
		;

	/* scale result up */
	if (ex > 0) {
		uxi -= 1U << 23;
		uxi |= (uint32_t)ex << 23;
	} else {
		uxi >>= -ex + 1;
	}
	uxi |= sx;
	ux.i = uxi;
	return ux.f;
}