blob: 639514effafcc91997d53f4b8528e16b21440a9e [file] [log] [blame]
Szabolcs Nagyc4359e02012-11-13 10:55:35 +01001#include "libm.h"
Rich Felkerb69f6952012-03-13 01:17:53 -04002
Rich Felkerb69f6952012-03-13 01:17:53 -04003float modff(float x, float *iptr)
4{
Szabolcs Nagyee2ee922013-09-03 04:09:12 +00005 union {float f; uint32_t i;} u = {x};
nszf6ceccd2012-03-29 14:03:18 +02006 uint32_t mask;
Szabolcs Nagyee2ee922013-09-03 04:09:12 +00007 int e = (int)(u.i>>23 & 0xff) - 0x7f;
nsz75483492012-03-19 23:27:45 +01008
9 /* no fractional part */
10 if (e >= 23) {
11 *iptr = x;
Szabolcs Nagyee2ee922013-09-03 04:09:12 +000012 if (e == 0x80 && u.i<<9 != 0) { /* nan */
Rich Felkerb69f6952012-03-13 01:17:53 -040013 return x;
nszf6ceccd2012-03-29 14:03:18 +020014 }
Szabolcs Nagyee2ee922013-09-03 04:09:12 +000015 u.i &= 0x80000000;
16 return u.f;
Rich Felkerb69f6952012-03-13 01:17:53 -040017 }
nsz75483492012-03-19 23:27:45 +010018 /* no integral part */
19 if (e < 0) {
Szabolcs Nagyee2ee922013-09-03 04:09:12 +000020 u.i &= 0x80000000;
21 *iptr = u.f;
nsz75483492012-03-19 23:27:45 +010022 return x;
23 }
24
25 mask = 0x007fffff>>e;
Szabolcs Nagyee2ee922013-09-03 04:09:12 +000026 if ((u.i & mask) == 0) {
nsz75483492012-03-19 23:27:45 +010027 *iptr = x;
Szabolcs Nagyee2ee922013-09-03 04:09:12 +000028 u.i &= 0x80000000;
29 return u.f;
nsz75483492012-03-19 23:27:45 +010030 }
Szabolcs Nagyee2ee922013-09-03 04:09:12 +000031 u.i &= ~mask;
32 *iptr = u.f;
33 return x - u.f;
Rich Felkerb69f6952012-03-13 01:17:53 -040034}