summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2021-01-23 20:03:07 -0800
committerOri Bernstein <ori@eigenstate.org>2021-01-23 20:03:07 -0800
commitf76e28cb71fd7f45eda35f68c89a6ba151625313 (patch)
treee5bc61d6723fb84b87e3f3111dc3820b13768910
parent988bdd05a326687daa1852c9ff395230743b30d8 (diff)
downloadplan9front-f76e28cb71fd7f45eda35f68c89a6ba151625313.tar.xz
ape/libm: add back fmod, remove modf
We removed the wrong file. Put it back.
-rw-r--r--sys/src/ape/lib/ap/math/fmod.c27
-rw-r--r--sys/src/ape/lib/ap/math/mkfile1
-rw-r--r--sys/src/ape/lib/ap/math/modf.c53
3 files changed, 28 insertions, 53 deletions
diff --git a/sys/src/ape/lib/ap/math/fmod.c b/sys/src/ape/lib/ap/math/fmod.c
new file mode 100644
index 000000000..876c64c39
--- /dev/null
+++ b/sys/src/ape/lib/ap/math/fmod.c
@@ -0,0 +1,27 @@
+/* floating-point mod function without infinity or NaN checking */
+#include <math.h>
+double
+fmod (double x, double y)
+{
+ int sign = 0, yexp;
+ double r, yfr;
+
+ if (y == 0)
+ return 0;
+ if (y < 0)
+ y = -y;
+ yfr = frexp (y, &yexp);
+ if (x < 0) {
+ sign = 1;
+ r = -x;
+ } else
+ r = x;
+ while (r >= y) {
+ int rexp;
+ double rfr = frexp (r, &rexp);
+ r -= ldexp (y, rexp - yexp - (rfr < yfr));
+ }
+ if (sign)
+ r = -r;
+ return r;
+}
diff --git a/sys/src/ape/lib/ap/math/mkfile b/sys/src/ape/lib/ap/math/mkfile
index b68a9511b..7d0171dec 100644
--- a/sys/src/ape/lib/ap/math/mkfile
+++ b/sys/src/ape/lib/ap/math/mkfile
@@ -10,6 +10,7 @@ OFILES=\
fabs.$O\
floor.$O\
fmin.$O\
+ fmod.$O\
gamma.$O\
hypot.$O\
j0.$O\
diff --git a/sys/src/ape/lib/ap/math/modf.c b/sys/src/ape/lib/ap/math/modf.c
deleted file mode 100644
index 4326cf44a..000000000
--- a/sys/src/ape/lib/ap/math/modf.c
+++ /dev/null
@@ -1,53 +0,0 @@
-#include <math.h>
-#include <errno.h>
-
-/* modf suitable for IEEE double-precision */
-
-#define MASK 0x7ffL
-#define SIGN 0x80000000
-#define SHIFT 20
-#define BIAS 1022L
-
-typedef union
-{
- double d;
- struct
- {
- long ms;
- long ls;
- } i;
-} Cheat;
-
-double
-modf(double d, double *ip)
-{
- Cheat x;
- int e;
-
- if(-1 < d && d < 1) {
- *ip = 0;
- return d;
- }
- x.d = d;
- x.i.ms &= ~SIGN;
- e = (x.i.ms >> SHIFT) & MASK;
- if(e == MASK || e == 0){
- errno = EDOM;
- *ip = (d > 0)? HUGE_VAL : -HUGE_VAL;
- return 0;
- }
- e -= BIAS;
- if(e <= SHIFT+1) {
- x.i.ms &= ~(0x1fffffL >> e);
- x.i.ls = 0;
- } else
- if(e <= SHIFT+33)
- x.i.ls &= ~(0x7fffffffL >> (e-SHIFT-2));
- if(d > 0){
- *ip = x.d;
- return d - x.d;
- }else{
- *ip = -x.d;
- return d + x.d;
- }
-}