summaryrefslogtreecommitdiff
path: root/src/math/modff.c
diff options
context:
space:
mode:
authornsz <nsz@port70.net>2012-03-29 14:03:18 +0200
committernsz <nsz@port70.net>2012-03-29 14:03:18 +0200
commitf6ceccd92247575e4a35bc94f581a570b8052d43 (patch)
tree817b7531ea3b62c845f7e3731e48439d4be9745a /src/math/modff.c
parent9f58d06007818c43b9504e959ef81ab5f113b374 (diff)
downloadmusl-f6ceccd92247575e4a35bc94f581a570b8052d43.tar.gz
math: rewrite modf.c and clean up modff.c
cleaner implementation with unions and unsigned arithmetic
Diffstat (limited to 'src/math/modff.c')
-rw-r--r--src/math/modff.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/math/modff.c b/src/math/modff.c
index bf6e4ced..84d0b82a 100644
--- a/src/math/modff.c
+++ b/src/math/modff.c
@@ -1,33 +1,37 @@
-#include "libm.h"
+#include <math.h>
+#include <stdint.h>
float modff(float x, float *iptr)
{
- uint32_t u, mask;
+ union {float x; uint32_t n;} u = {x};
+ uint32_t mask;
int e;
- GET_FLOAT_WORD(u, x);
- e = (int)(u>>23 & 0xff) - 0x7f;
+ e = (int)(u.n>>23 & 0xff) - 0x7f;
/* no fractional part */
if (e >= 23) {
*iptr = x;
- if (e == 0x80 && u<<9 != 0) /* nan */
+ if (e == 0x80 && u.n<<9 != 0) { /* nan */
return x;
- SET_FLOAT_WORD(x, u & 0x80000000);
- return x;
+ }
+ u.n &= 0x80000000;
+ return u.x;
}
/* no integral part */
if (e < 0) {
- SET_FLOAT_WORD(*iptr, u & 0x80000000);
+ u.n &= 0x80000000;
+ *iptr = u.x;
return x;
}
mask = 0x007fffff>>e;
- if ((u & mask) == 0) {
+ if ((u.n & mask) == 0) {
*iptr = x;
- SET_FLOAT_WORD(x, u & 0x80000000);
- return x;
+ u.n &= 0x80000000;
+ return u.x;
}
- SET_FLOAT_WORD(*iptr, u & ~mask);
+ u.n &= ~mask;
+ *iptr = u.x;
return x - *iptr;
}