aboutsummaryrefslogtreecommitdiff
path: root/src/base/math/nan.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2021-12-05 16:16:21 -0800
committerNicholas Noll <nbnoll@eml.cc>2021-12-05 16:16:21 -0800
commit07e77936d535e58b0aeb4f2a11400c1050556739 (patch)
treefb50fad6436ecbc159505bee73a92cd289b99a07 /src/base/math/nan.c
parentb48327d357e0818d1a6ae2a064cfa7d1567e1242 (diff)
Feat: added math library
Used Plan9's libc as starting point. This cleans up dangling references due to loss of libc.
Diffstat (limited to 'src/base/math/nan.c')
-rw-r--r--src/base/math/nan.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/base/math/nan.c b/src/base/math/nan.c
new file mode 100644
index 0000000..c1f85c2
--- /dev/null
+++ b/src/base/math/nan.c
@@ -0,0 +1,54 @@
+#include <u.h>
+#include <base.h>
+
+#define NANEXP (2047<<20)
+#define NANMASK (2047<<20)
+#define NANSIGN (1<<31)
+
+double
+math·NaN(void)
+{
+ sys·DoubleWord a;
+
+ a.hi = NANEXP;
+ a.lo = 1;
+ return a.x;
+}
+
+int
+math·isNaN(double d)
+{
+ sys·DoubleWord a;
+
+ a.x = d;
+ if((a.hi & NANMASK) != NANEXP)
+ return 0;
+ return !math·isInf(d, 0);
+}
+
+double
+math·Inf(int sign)
+{
+ sys·DoubleWord a;
+
+ a.hi = NANEXP;
+ a.lo = 0;
+ if(sign < 0)
+ a.hi |= NANSIGN;
+ return a.x;
+}
+
+int
+math·isInf(double d, int sign)
+{
+ sys·DoubleWord a;
+
+ a.x = d;
+ if(a.lo != 0)
+ return 0;
+ if(a.hi == NANEXP)
+ return sign >= 0;
+ if(a.hi == (NANEXP|NANSIGN))
+ return sign <= 0;
+ return 0;
+}