2012-03-29 14:03:18 +02:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
2012-03-13 01:17:53 -04:00
|
|
|
|
|
|
|
float modff(float x, float *iptr)
|
|
|
|
{
|
2012-03-29 14:03:18 +02:00
|
|
|
union {float x; uint32_t n;} u = {x};
|
|
|
|
uint32_t mask;
|
2012-03-19 23:27:45 +01:00
|
|
|
int e;
|
2012-03-13 01:17:53 -04:00
|
|
|
|
2012-03-29 14:03:18 +02:00
|
|
|
e = (int)(u.n>>23 & 0xff) - 0x7f;
|
2012-03-19 23:27:45 +01:00
|
|
|
|
|
|
|
/* no fractional part */
|
|
|
|
if (e >= 23) {
|
|
|
|
*iptr = x;
|
2012-03-29 14:03:18 +02:00
|
|
|
if (e == 0x80 && u.n<<9 != 0) { /* nan */
|
2012-03-13 01:17:53 -04:00
|
|
|
return x;
|
2012-03-29 14:03:18 +02:00
|
|
|
}
|
|
|
|
u.n &= 0x80000000;
|
|
|
|
return u.x;
|
2012-03-19 23:27:45 +01:00
|
|
|
}
|
|
|
|
/* no integral part */
|
|
|
|
if (e < 0) {
|
2012-03-29 14:03:18 +02:00
|
|
|
u.n &= 0x80000000;
|
|
|
|
*iptr = u.x;
|
2012-03-19 23:27:45 +01:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 0x007fffff>>e;
|
2012-03-29 14:03:18 +02:00
|
|
|
if ((u.n & mask) == 0) {
|
2012-03-19 23:27:45 +01:00
|
|
|
*iptr = x;
|
2012-03-29 14:03:18 +02:00
|
|
|
u.n &= 0x80000000;
|
|
|
|
return u.x;
|
2012-03-13 01:17:53 -04:00
|
|
|
}
|
2012-03-29 14:03:18 +02:00
|
|
|
u.n &= ~mask;
|
|
|
|
*iptr = u.x;
|
2012-03-19 23:27:45 +01:00
|
|
|
return x - *iptr;
|
2012-03-13 01:17:53 -04:00
|
|
|
}
|