Szabolcs Nagy | c4359e0 | 2012-11-13 10:55:35 +0100 | [diff] [blame] | 1 | #include "libm.h" |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 2 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 3 | float modff(float x, float *iptr) |
4 | { | ||||
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 5 | union {float f; uint32_t i;} u = {x}; |
nsz | f6ceccd | 2012-03-29 14:03:18 +0200 | [diff] [blame] | 6 | uint32_t mask; |
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 7 | int e = (int)(u.i>>23 & 0xff) - 0x7f; |
nsz | 7548349 | 2012-03-19 23:27:45 +0100 | [diff] [blame] | 8 | |
9 | /* no fractional part */ | ||||
10 | if (e >= 23) { | ||||
11 | *iptr = x; | ||||
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 12 | if (e == 0x80 && u.i<<9 != 0) { /* nan */ |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 13 | return x; |
nsz | f6ceccd | 2012-03-29 14:03:18 +0200 | [diff] [blame] | 14 | } |
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 15 | u.i &= 0x80000000; |
16 | return u.f; | ||||
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 17 | } |
nsz | 7548349 | 2012-03-19 23:27:45 +0100 | [diff] [blame] | 18 | /* no integral part */ |
19 | if (e < 0) { | ||||
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 20 | u.i &= 0x80000000; |
21 | *iptr = u.f; | ||||
nsz | 7548349 | 2012-03-19 23:27:45 +0100 | [diff] [blame] | 22 | return x; |
23 | } | ||||
24 | |||||
25 | mask = 0x007fffff>>e; | ||||
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 26 | if ((u.i & mask) == 0) { |
nsz | 7548349 | 2012-03-19 23:27:45 +0100 | [diff] [blame] | 27 | *iptr = x; |
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 28 | u.i &= 0x80000000; |
29 | return u.f; | ||||
nsz | 7548349 | 2012-03-19 23:27:45 +0100 | [diff] [blame] | 30 | } |
Szabolcs Nagy | ee2ee92 | 2013-09-03 04:09:12 +0000 | [diff] [blame] | 31 | u.i &= ~mask; |
32 | *iptr = u.f; | ||||
33 | return x - u.f; | ||||
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 34 | } |