summaryrefslogtreecommitdiff
path: root/src/math/modff.c
blob: bf6e4ced1a8d461946834c1ec26481db29106dcb (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
#include "libm.h"

float modff(float x, float *iptr)
{
	uint32_t u, mask;
	int e;

	GET_FLOAT_WORD(u, x);
	e = (int)(u>>23 & 0xff) - 0x7f;

	/* no fractional part */
	if (e >= 23) {
		*iptr = x;
		if (e == 0x80 && u<<9 != 0) /* nan */
			return x;
		SET_FLOAT_WORD(x, u & 0x80000000);
		return x;
	}
	/* no integral part */
	if (e < 0) {
		SET_FLOAT_WORD(*iptr, u & 0x80000000);
		return x;
	}

	mask = 0x007fffff>>e;
	if ((u & mask) == 0) {
		*iptr = x;
		SET_FLOAT_WORD(x, u & 0x80000000);
		return x;
	}
	SET_FLOAT_WORD(*iptr, u & ~mask);
	return x - *iptr;
}