aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--src/stdlib/intX.h2
-rw-r--r--src/stdlib/num32.c4
-rw-r--r--src/stdlib/num64.c4
-rw-r--r--src/stdlib/numX.c.h195
-rw-r--r--src/stdlib/numX.h103
-rw-r--r--src/stdlib/nums.c269
-rw-r--r--src/stdlib/nums.h134
8 files changed, 317 insertions, 397 deletions
diff --git a/Makefile b/Makefile
index c6cd808e..6b71a346 100644
--- a/Makefile
+++ b/Makefile
@@ -163,6 +163,9 @@ config.mk: configure.sh
# Integer implementations depend on the shared header:
src/stdlib/int64.o src/stdlib/int32.o src/stdlib/int16.o src/stdlib/int8.o: src/stdlib/intX.c.h
+# Num implementations depend on the shared header:
+src/stdlib/num32.o src/stdlib/num64.o: src/stdlib/numX.c.h
+
# Specifically src/tomo.c needs to recompile if CHANGES.md changes:
src/tomo.o: src/tomo.c src/ast.h src/environment.h src/types.h config.mk src/changes.md.h
@$(ECHO) $(CC) $(CFLAGS_PLACEHOLDER) -c $< -o $@
diff --git a/src/stdlib/intX.h b/src/stdlib/intX.h
index 7b1f90d5..e0980923 100644
--- a/src/stdlib/intX.h
+++ b/src/stdlib/intX.h
@@ -1,5 +1,5 @@
// Integer type infos and methods
-// This file expects `c_type` and `type_name` to be defined before including:
+// This file expects `INTX_H__INT_BITS` to be defined before including:
//
// #define INTX_H__INT_BITS 64
// #include "intX.h"
diff --git a/src/stdlib/num32.c b/src/stdlib/num32.c
new file mode 100644
index 00000000..6d505f37
--- /dev/null
+++ b/src/stdlib/num32.c
@@ -0,0 +1,4 @@
+// Type infos and methods for Num32 (32-bit floating point) using a template file
+
+#define NUMX_C_H__BITS 32
+#include "numX.c.h"
diff --git a/src/stdlib/num64.c b/src/stdlib/num64.c
new file mode 100644
index 00000000..7fdc8f35
--- /dev/null
+++ b/src/stdlib/num64.c
@@ -0,0 +1,4 @@
+// Type infos and methods for Num (64-bit floating point) using a template file
+
+#define NUMX_C_H__BITS 64
+#include "numX.c.h"
diff --git a/src/stdlib/numX.c.h b/src/stdlib/numX.c.h
new file mode 100644
index 00000000..707af60b
--- /dev/null
+++ b/src/stdlib/numX.c.h
@@ -0,0 +1,195 @@
+// Type infos and methods for Nums (floating point)
+// This file is a template that expects `NUMSX_C_H__BITS` to be defined before including:
+//
+// #define NUMX_C_H__BITS 64
+// #include "numX.c.h"
+//
+#include <float.h>
+#include <gc.h>
+#include <math.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "fpconv.h"
+#include "nums.h"
+#include "string.h"
+#include "text.h"
+#include "types.h"
+
+#ifndef NUMX_C_H__BITS
+#define NUMX_C_H__BITS 64
+#endif
+
+#if NUMX_C_H__BITS == 64
+#define NUM_T double
+#define OPT_T double
+#define NAMESPACED(x) Num$##x
+#define TYPE_STR "Num"
+#define SUFFIXED(x) x
+#elif NUMX_C_H__BITS == 32
+#define NUM_T float
+#define OPT_T float
+#define NAMESPACED(x) Num32$##x
+#define TYPE_STR "Num32"
+#define SUFFIXED(x) x##f
+#else
+#error "Unsupported bit width for Num"
+#endif
+
+#if NUMX_C_H__BITS == 64
+public
+PUREFUNC Text_t NAMESPACED(value_as_text)(double x) {
+ char *str = GC_MALLOC_ATOMIC(24);
+ int len = fpconv_dtoa(x, str);
+ return Text$from_strn(str, (size_t)len);
+}
+public
+PUREFUNC Text_t NAMESPACED(as_text)(const void *x, bool colorize, const TypeInfo_t *info) {
+ (void)info;
+ if (!x) return Text("Num");
+ static const Text_t color_prefix = Text("\x1b[35m"), color_suffix = Text("\x1b[m");
+ Text_t text = NAMESPACED(value_as_text)(*(double *)x);
+ return colorize ? Texts(color_prefix, text, color_suffix) : text;
+}
+#elif NUMX_C_H__BITS == 32
+public
+PUREFUNC Text_t NAMESPACED(value_as_text)(float x) { return Num$value_as_text((double)x); }
+PUREFUNC Text_t NAMESPACED(as_text)(const void *x, bool colorize, const TypeInfo_t *info) {
+ (void)info;
+ if (!x) return Text("Num32");
+ static const Text_t color_prefix = Text("\x1b[35m"), color_suffix = Text("\x1b[m");
+ Text_t text = Num$value_as_text(*(double *)x);
+ return colorize ? Texts(color_prefix, text, color_suffix) : text;
+}
+#endif
+
+public
+PUREFUNC int32_t NAMESPACED(compare)(const void *x, const void *y, const TypeInfo_t *info) {
+ (void)info;
+ int64_t rx = *(int64_t *)x, ry = *(int64_t *)y;
+
+ if (rx == ry) return 0;
+
+ if (rx < 0) rx ^= INT64_MAX;
+ if (ry < 0) ry ^= INT64_MAX;
+
+ return (rx > ry) - (rx < ry);
+}
+
+public
+PUREFUNC bool NAMESPACED(equal)(const void *x, const void *y, const TypeInfo_t *info) {
+ (void)info;
+ return *(NUM_T *)x == *(NUM_T *)y;
+}
+
+public
+CONSTFUNC bool NAMESPACED(near)(NUM_T a, NUM_T b, NUM_T ratio, NUM_T absolute) {
+ if (ratio < 0) ratio = 0;
+ else if (ratio > 1) ratio = 1;
+
+ if (a == b) return true;
+
+ NUM_T diff = SUFFIXED(fabs)(a - b);
+ if (diff < absolute) return true;
+ else if (SUFFIXED(isnan)(diff)) return false;
+
+ NUM_T epsilon = SUFFIXED(fabs)(a * ratio) + SUFFIXED(fabs)(b * ratio);
+ if (SUFFIXED(isinf)(epsilon)) epsilon = DBL_MAX;
+ return (diff < epsilon);
+}
+
+public
+Text_t NAMESPACED(percent)(NUM_T x, NUM_T precision) {
+ NUM_T d = SUFFIXED(100.) * x;
+ d = NAMESPACED(with_precision)(d, precision);
+ return Texts(NAMESPACED(value_as_text)(d), Text("%"));
+}
+
+public
+CONSTFUNC NUM_T NAMESPACED(with_precision)(NUM_T num, NUM_T precision) {
+ if (precision == SUFFIXED(0.0)) return num;
+ // Precision will be, e.g. 0.01 or 100.
+ if (precision < SUFFIXED(1.)) {
+ NUM_T inv = SUFFIXED(round)(SUFFIXED(1.) / precision); // Necessary to make the math work
+ NUM_T k = num * inv;
+ return SUFFIXED(round)(k) / inv;
+ } else {
+ NUM_T k = num / precision;
+ return SUFFIXED(round)(k) * precision;
+ }
+}
+
+public
+CONSTFUNC NUM_T NAMESPACED(mod)(NUM_T num, NUM_T modulus) {
+ // Euclidean division, see:
+ // https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf
+ NUM_T r = remainder(num, modulus);
+ r -= (r < 0) * (2 * (modulus < 0) - 1) * modulus;
+ return r;
+}
+
+public
+CONSTFUNC NUM_T NAMESPACED(mod1)(NUM_T num, NUM_T modulus) {
+ return SUFFIXED(1.0) + NAMESPACED(mod)(num - SUFFIXED(1.0), modulus);
+}
+
+public
+CONSTFUNC NUM_T NAMESPACED(mix)(NUM_T amount, NUM_T x, NUM_T y) { return (SUFFIXED(1.0) - amount) * x + amount * y; }
+
+public
+CONSTFUNC bool NAMESPACED(is_between)(const NUM_T x, const NUM_T low, const NUM_T high) {
+ return low <= x && x <= high;
+}
+public
+CONSTFUNC NUM_T NAMESPACED(clamped)(NUM_T x, NUM_T low, NUM_T high) {
+ return (x <= low) ? low : (x >= high ? high : x);
+}
+
+public
+OPT_T NAMESPACED(parse)(Text_t text, Text_t *remainder) {
+ const char *str = Text$as_c_string(text);
+ char *end = NULL;
+ NUM_T d = strtod(str, &end);
+ if (end > str) {
+ if (remainder) *remainder = Text$from_str(end);
+ else if (*end != '\0') return nan("none");
+ return d;
+ } else {
+ if (remainder) *remainder = text;
+ return nan("none");
+ }
+}
+
+public
+CONSTFUNC bool NAMESPACED(is_none)(const void *n, const TypeInfo_t *info) {
+ (void)info;
+ return SUFFIXED(isnan)(*(Num_t *)n);
+}
+
+public
+CONSTFUNC bool NAMESPACED(isinf)(NUM_T n) { return (fpclassify(n) == FP_INFINITE); }
+public
+CONSTFUNC bool NAMESPACED(finite)(NUM_T n) { return (fpclassify(n) != FP_INFINITE); }
+public
+CONSTFUNC bool NAMESPACED(isnan)(NUM_T n) { return (fpclassify(n) == FP_NAN); }
+
+public
+const TypeInfo_t NAMESPACED(info) = {
+ .size = sizeof(NUM_T),
+ .align = __alignof__(NUM_T),
+ .metamethods =
+ {
+ .compare = NAMESPACED(compare),
+ .equal = NAMESPACED(equal),
+ .as_text = NAMESPACED(as_text),
+ .is_none = NAMESPACED(is_none),
+ },
+};
+
+#undef NUM_T
+#undef OPT_T
+#undef NAMESPACED
+#undef TYPE_STR
+#undef SUFFIXED
+#undef NUMX_C_H__BITS
diff --git a/src/stdlib/numX.h b/src/stdlib/numX.h
new file mode 100644
index 00000000..779c4e59
--- /dev/null
+++ b/src/stdlib/numX.h
@@ -0,0 +1,103 @@
+// Template header for 64 and 32 bit Nums
+// This file expects `NUMX_H__BITS` to be defined before including:
+//
+// #define NUMX_H__BITS 64
+// #include "numX.h"
+//
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "datatypes.h"
+#include "stdlib.h"
+#include "types.h"
+#include "util.h"
+
+#ifndef NUMX_H__BITS
+#define NUMX_H__BITS 64
+#endif
+
+#if NUMX_H__BITS == 64
+#define NUM_T double
+#define OPT_T double
+#define NAMESPACED(x) Num$##x
+#define TYPE_STR "Num"
+#define SUFFIXED(x) x
+#elif NUMX_H__BITS == 32
+#define NUM_T float
+#define OPT_T float
+#define NAMESPACED(x) Num32$##x
+#define TYPE_STR "Num32"
+#define SUFFIXED(x) x##f
+#else
+#error "Unsupported bit width for Num"
+#endif
+
+Text_t NAMESPACED(as_text)(const void *x, bool colorize, const TypeInfo_t *type);
+Text_t NAMESPACED(value_as_text)(NUM_T x);
+PUREFUNC int32_t NAMESPACED(compare)(const void *x, const void *y, const TypeInfo_t *type);
+PUREFUNC bool NAMESPACED(equal)(const void *x, const void *y, const TypeInfo_t *type);
+CONSTFUNC bool NAMESPACED(near)(NUM_T a, NUM_T b, NUM_T ratio, NUM_T absolute);
+Text_t NAMESPACED(percent)(NUM_T x, NUM_T precision);
+NUM_T CONSTFUNC NAMESPACED(with_precision)(NUM_T num, NUM_T precision);
+NUM_T NAMESPACED(mod)(NUM_T num, NUM_T modulus);
+NUM_T NAMESPACED(mod1)(NUM_T num, NUM_T modulus);
+CONSTFUNC bool NAMESPACED(isinf)(NUM_T n);
+CONSTFUNC bool NAMESPACED(finite)(NUM_T n);
+CONSTFUNC bool NAMESPACED(isnan)(NUM_T n);
+bool NAMESPACED(is_none)(const void *n, const TypeInfo_t *info);
+NUM_T NAMESPACED(nan)(Text_t tag);
+CONSTFUNC NUM_T NAMESPACED(mix)(NUM_T amount, NUM_T x, NUM_T y);
+OPT_T NAMESPACED(parse)(Text_t text, Text_t *remainder);
+CONSTFUNC bool NAMESPACED(is_between)(const NUM_T x, const NUM_T low, const NUM_T high);
+CONSTFUNC NUM_T NAMESPACED(clamped)(NUM_T x, NUM_T low, NUM_T high);
+
+#if NUMX_H__BITS == 64
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_num32)(float n) { return (NUM_T)n; }
+#elif NUMX_H__BITS == 32
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_num64)(double n) { return (NUM_T)n; }
+#endif
+
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wfloat-equal"
+#endif
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_int)(Int_t i, bool truncate) {
+ if likely (i.small & 0x1) {
+ NUM_T ret = (NUM_T)(i.small >> 2);
+ if unlikely (!truncate && (int64_t)ret != (i.small >> 2))
+ fail("Could not convert integer to " TYPE_STR " without losing precision: ", i.small >> 2);
+ return ret;
+ } else {
+ NUM_T ret = mpz_get_d(*i.big);
+ if (!truncate) {
+ mpz_t roundtrip;
+ mpz_init_set_d(roundtrip, ret);
+ if unlikely (mpz_cmp(*i.big, roundtrip) != 0)
+ fail("Could not convert integer to " TYPE_STR " without losing precision: ", i);
+ }
+ return ret;
+ }
+}
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_int64)(Int64_t i, bool truncate) {
+ NUM_T n = (NUM_T)i;
+ if unlikely (!truncate && (Int64_t)n != i)
+ fail("Could not convert integer to " TYPE_STR " without losing precision: ", i);
+ return n;
+}
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_int32)(Int32_t i) { return (NUM_T)i; }
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_int16)(Int16_t i) { return (NUM_T)i; }
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_int8)(Int8_t i) { return (NUM_T)i; }
+MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_byte)(Byte_t i) { return (NUM_T)i; }
+
+extern const TypeInfo_t NAMESPACED(info);
+
+#undef NUM_T
+#undef OPT_T
+#undef NAMESPACED
+#undef TYPE_STR
+#undef SUFFIXED
+#undef NUMX_H__BITS
diff --git a/src/stdlib/nums.c b/src/stdlib/nums.c
deleted file mode 100644
index 4bbb1f6a..00000000
--- a/src/stdlib/nums.c
+++ /dev/null
@@ -1,269 +0,0 @@
-// Type infos and methods for Nums (floating point)
-
-#include <float.h>
-#include <gc.h>
-#include <math.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdlib.h>
-
-#include "fpconv.h"
-#include "nums.h"
-#include "string.h"
-#include "text.h"
-#include "types.h"
-
-public
-PUREFUNC Text_t Num$value_as_text(double x) {
- char *str = GC_MALLOC_ATOMIC(24);
- int len = fpconv_dtoa(x, str);
- return Text$from_strn(str, (size_t)len);
-}
-
-public
-PUREFUNC Text_t Num$as_text(const void *x, bool colorize, const TypeInfo_t *info) {
- (void)info;
- if (!x) return Text("Num");
- static const Text_t color_prefix = Text("\x1b[35m"), color_suffix = Text("\x1b[m");
- Text_t text = Num$value_as_text(*(double *)x);
- return colorize ? Texts(color_prefix, text, color_suffix) : text;
-}
-
-public
-PUREFUNC int32_t Num$compare(const void *x, const void *y, const TypeInfo_t *info) {
- (void)info;
- int64_t rx = *(int64_t *)x, ry = *(int64_t *)y;
-
- if (rx == ry) return 0;
-
- if (rx < 0) rx ^= INT64_MAX;
- if (ry < 0) ry ^= INT64_MAX;
-
- return (rx > ry) - (rx < ry);
-}
-
-public
-PUREFUNC bool Num$equal(const void *x, const void *y, const TypeInfo_t *info) {
- (void)info;
- return *(double *)x == *(double *)y;
-}
-
-public
-CONSTFUNC bool Num$near(double a, double b, double ratio, double absolute) {
- if (ratio < 0) ratio = 0;
- else if (ratio > 1) ratio = 1;
-
- if (a == b) return true;
-
- double diff = fabs(a - b);
- if (diff < absolute) return true;
- else if (isnan(diff)) return false;
-
- double epsilon = fabs(a * ratio) + fabs(b * ratio);
- if (isinf(epsilon)) epsilon = DBL_MAX;
- return (diff < epsilon);
-}
-
-public
-Text_t Num$percent(double x, double precision) {
- double d = 100. * x;
- d = Num$with_precision(d, precision);
- return Texts(Num$value_as_text(d), Text("%"));
-}
-
-public
-CONSTFUNC double Num$with_precision(double num, double precision) {
- if (precision == 0.0) return num;
- // Precision will be, e.g. 0.01 or 100.
- if (precision < 1.) {
- double inv = round(1. / precision); // Necessary to make the math work
- double k = num * inv;
- return round(k) / inv;
- } else {
- double k = num / precision;
- return round(k) * precision;
- }
-}
-
-public
-CONSTFUNC double Num$mod(double num, double modulus) {
- // Euclidean division, see:
- // https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf
- double r = remainder(num, modulus);
- r -= (r < 0) * (2 * (modulus < 0) - 1) * modulus;
- return r;
-}
-
-public
-CONSTFUNC double Num$mod1(double num, double modulus) { return 1.0 + Num$mod(num - 1, modulus); }
-
-public
-CONSTFUNC double Num$mix(double amount, double x, double y) { return (1.0 - amount) * x + amount * y; }
-
-public
-CONSTFUNC bool Num$is_between(const double x, const double low, const double high) { return low <= x && x <= high; }
-public
-CONSTFUNC double Num$clamped(double x, double low, double high) { return (x <= low) ? low : (x >= high ? high : x); }
-
-public
-OptionalNum_t Num$parse(Text_t text, Text_t *remainder) {
- const char *str = Text$as_c_string(text);
- char *end = NULL;
- double d = strtod(str, &end);
- if (end > str) {
- if (remainder) *remainder = Text$from_str(end);
- else if (*end != '\0') return nan("none");
- return d;
- } else {
- if (remainder) *remainder = text;
- return nan("none");
- }
-}
-
-public
-CONSTFUNC bool Num$is_none(const void *n, const TypeInfo_t *info) {
- (void)info;
- return isnan(*(Num_t *)n);
-}
-
-public
-CONSTFUNC bool Num$isinf(double n) { return (fpclassify(n) == FP_INFINITE); }
-public
-CONSTFUNC bool Num$finite(double n) { return (fpclassify(n) != FP_INFINITE); }
-public
-CONSTFUNC bool Num$isnan(double n) { return (fpclassify(n) == FP_NAN); }
-
-public
-const TypeInfo_t Num$info = {
- .size = sizeof(double),
- .align = __alignof__(double),
- .metamethods =
- {
- .compare = Num$compare,
- .equal = Num$equal,
- .as_text = Num$as_text,
- .is_none = Num$is_none,
- },
-};
-
-public
-PUREFUNC Text_t Num32$value_as_text(float x) { return Num$value_as_text((double)x); }
-
-public
-PUREFUNC Text_t Num32$as_text(const void *x, bool colorize, const TypeInfo_t *info) {
- (void)info;
- if (!x) return Text("Num32");
- double d = (double)(*(float *)x);
- return Num$as_text(&d, colorize, &Num$info);
-}
-
-public
-PUREFUNC int32_t Num32$compare(const void *x, const void *y, const TypeInfo_t *info) {
- (void)info;
- return (*(float *)x > *(float *)y) - (*(float *)x < *(float *)y);
-}
-
-public
-PUREFUNC bool Num32$equal(const void *x, const void *y, const TypeInfo_t *info) {
- (void)info;
- return *(float *)x == *(float *)y;
-}
-
-public
-CONSTFUNC bool Num32$near(float a, float b, float ratio, float absolute) {
- if (ratio < 0) ratio = 0;
- else if (ratio > 1) ratio = 1;
-
- if (a == b) return true;
-
- float diff = fabsf(a - b);
- if (diff < absolute) return true;
- else if (isnan(diff)) return false;
-
- float epsilon = fabsf(a * ratio) + fabsf(b * ratio);
- if (isinf(epsilon)) epsilon = FLT_MAX;
- return (diff < epsilon);
-}
-
-public
-Text_t Num32$percent(float x, float precision) {
- double d = 100. * (double)x;
- d = Num$with_precision(d, (double)precision);
- return Texts(Num$value_as_text(d), Text("%"));
-}
-
-public
-CONSTFUNC float Num32$with_precision(float num, float precision) {
- if (precision == 0.0f) return num;
- // Precision will be, e.g. 0.01 or 100.
- if (precision < 1.f) {
- float inv = roundf(1.f / precision); // Necessary to make the math work
- float k = num * inv;
- return roundf(k) / inv;
- } else {
- float k = num / precision;
- return roundf(k) * precision;
- }
-}
-
-public
-CONSTFUNC float Num32$mod(float num, float modulus) {
- // Euclidean division, see:
- // https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf
- float r = remainderf(num, modulus);
- r -= (r < 0) * (2 * (modulus < 0) - 1) * modulus;
- return r;
-}
-
-public
-CONSTFUNC float Num32$mod1(float num, float modulus) { return 1.0f + Num32$mod(num - 1, modulus); }
-
-public
-CONSTFUNC float Num32$mix(float amount, float x, float y) { return (1.0f - amount) * x + amount * y; }
-
-public
-CONSTFUNC bool Num32$is_between(const float x, const float low, const float high) { return low <= x && x <= high; }
-
-public
-CONSTFUNC float Num32$clamped(float x, float low, float high) { return (x <= low) ? low : (x >= high ? high : x); }
-
-public
-OptionalNum32_t Num32$parse(Text_t text, Text_t *remainder) {
- const char *str = Text$as_c_string(text);
- char *end = NULL;
- double d = strtod(str, &end);
- if (end > str && end[0] == '\0') {
- if (remainder) *remainder = Text$from_str(end);
- else if (*end != '\0') return nan("none");
- return d;
- } else {
- if (remainder) *remainder = text;
- return nan("none");
- }
-}
-
-public
-CONSTFUNC bool Num32$is_none(const void *n, const TypeInfo_t *info) {
- (void)info;
- return isnan(*(Num32_t *)n);
-}
-
-public
-CONSTFUNC bool Num32$isinf(float n) { return (fpclassify(n) == FP_INFINITE); }
-public
-CONSTFUNC bool Num32$finite(float n) { return (fpclassify(n) != FP_INFINITE); }
-public
-CONSTFUNC bool Num32$isnan(float n) { return (fpclassify(n) == FP_NAN); }
-
-public
-const TypeInfo_t Num32$info = {
- .size = sizeof(float),
- .align = __alignof__(float),
- .metamethods =
- {
- .compare = Num32$compare,
- .equal = Num32$equal,
- .as_text = Num32$as_text,
- .is_none = Num32$is_none,
- },
-};
diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h
index 303aa362..8cea9e84 100644
--- a/src/stdlib/nums.h
+++ b/src/stdlib/nums.h
@@ -2,132 +2,12 @@
#pragma once
-#include <stdbool.h>
-#include <stdint.h>
-
-#include "datatypes.h"
-#include "stdlib.h"
-#include "types.h"
-#include "util.h"
-
-#define OptionalNum_t double
-#define OptionalNum32_t float
-#define N32(n) ((float)(n))
#define N64(n) ((double)(n))
+#define OptionalNum_t double
+#define NUMX_H__BITS 64
+#include "numX.h"
-Text_t Num$as_text(const void *x, bool colorize, const TypeInfo_t *type);
-Text_t Num$value_as_text(double x);
-PUREFUNC int32_t Num$compare(const void *x, const void *y, const TypeInfo_t *type);
-PUREFUNC bool Num$equal(const void *x, const void *y, const TypeInfo_t *type);
-CONSTFUNC bool Num$near(double a, double b, double ratio, double absolute);
-Text_t Num$percent(double x, double precision);
-double CONSTFUNC Num$with_precision(double num, double precision);
-double Num$mod(double num, double modulus);
-double Num$mod1(double num, double modulus);
-CONSTFUNC bool Num$isinf(double n);
-CONSTFUNC bool Num$finite(double n);
-CONSTFUNC bool Num$isnan(double n);
-bool Num$is_none(const void *n, const TypeInfo_t *info);
-double Num$nan(Text_t tag);
-CONSTFUNC double Num$mix(double amount, double x, double y);
-OptionalNum_t Num$parse(Text_t text, Text_t *remainder);
-CONSTFUNC bool Num$is_between(const double x, const double low, const double high);
-CONSTFUNC double Num$clamped(double x, double low, double high);
-MACROLIKE CONSTFUNC double Num$from_num32(Num32_t n) { return (double)n; }
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wfloat-equal"
-#endif
-MACROLIKE CONSTFUNC double Num$from_int(Int_t i, bool truncate) {
- if likely (i.small & 0x1) {
- double ret = (double)(i.small >> 2);
- if unlikely (!truncate && (int64_t)ret != (i.small >> 2))
- fail("Could not convert integer to 64-bit floating point without losing precision: ", i.small >> 2);
- return ret;
- } else {
- double ret = mpz_get_d(*i.big);
- if (!truncate) {
- mpz_t roundtrip;
- mpz_init_set_d(roundtrip, ret);
- if unlikely (mpz_cmp(*i.big, roundtrip) != 0)
- fail("Could not convert integer to 64-bit floating point without losing precision: ", i);
- }
- return ret;
- }
-}
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#endif
-MACROLIKE CONSTFUNC double Num$from_int64(Int64_t i, bool truncate) {
- double n = (double)i;
- if unlikely (!truncate && (Int64_t)n != i)
- fail("Could not convert integer to 64-bit floating point without losing precision: ", i);
- return n;
-}
-MACROLIKE CONSTFUNC double Num$from_int32(Int32_t i) { return (double)i; }
-MACROLIKE CONSTFUNC double Num$from_int16(Int16_t i) { return (double)i; }
-MACROLIKE CONSTFUNC double Num$from_int8(Int8_t i) { return (double)i; }
-MACROLIKE CONSTFUNC double Num$from_byte(Byte_t i) { return (double)i; }
-
-extern const TypeInfo_t Num$info;
-
-Text_t Num32$as_text(const void *x, bool colorize, const TypeInfo_t *type);
-Text_t Num32$value_as_text(float x);
-PUREFUNC int32_t Num32$compare(const void *x, const void *y, const TypeInfo_t *type);
-PUREFUNC bool Num32$equal(const void *x, const void *y, const TypeInfo_t *type);
-CONSTFUNC bool Num32$near(float a, float b, float ratio, float absolute);
-Text_t Num32$percent(float x, float precision);
-float CONSTFUNC Num32$with_precision(float num, float precision);
-float Num32$mod(float num, float modulus);
-float Num32$mod1(float num, float modulus);
-CONSTFUNC bool Num32$isinf(float n);
-CONSTFUNC bool Num32$finite(float n);
-CONSTFUNC bool Num32$isnan(float n);
-CONSTFUNC bool Num32$is_none(const void *n, const TypeInfo_t *info);
-CONSTFUNC float Num32$mix(float amount, float x, float y);
-OptionalNum32_t Num32$parse(Text_t text, Text_t *remainder);
-float Num32$nan(Text_t tag);
-CONSTFUNC bool Num32$is_between(const float x, const float low, const float high);
-CONSTFUNC float Num32$clamped(float x, float low, float high);
-MACROLIKE CONSTFUNC float Num32$from_num(Num_t n) { return (float)n; }
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wfloat-equal"
-#endif
-MACROLIKE CONSTFUNC float Num32$from_int(Int_t i, bool truncate) {
- if likely (i.small & 0x1) {
- float ret = (float)(i.small >> 2);
- if unlikely (!truncate && (int64_t)ret != (i.small >> 2))
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i.small >> 2);
- return ret;
- } else {
- float ret = (float)mpz_get_d(*i.big);
- if (!truncate) {
- mpz_t roundtrip;
- mpz_init_set_d(roundtrip, (double)ret);
- if unlikely (mpz_cmp(*i.big, roundtrip) != 0)
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i);
- }
- return ret;
- }
-}
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#endif
-MACROLIKE CONSTFUNC float Num32$from_int64(Int64_t i, bool truncate) {
- float n = (float)i;
- if unlikely (!truncate && (Int64_t)n != i)
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i);
- return n;
-}
-MACROLIKE CONSTFUNC float Num32$from_int32(Int32_t i, bool truncate) {
- float n = (float)i;
- if unlikely (!truncate && (Int32_t)n != i)
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i);
- return n;
-}
-MACROLIKE CONSTFUNC float Num32$from_int16(Int16_t i) { return (float)i; }
-MACROLIKE CONSTFUNC float Num32$from_int8(Int8_t i) { return (float)i; }
-MACROLIKE CONSTFUNC float Num32$from_byte(Byte_t i) { return (float)i; }
-
-extern const TypeInfo_t Num32$info;
+#define N32(n) ((float)(n))
+#define OptionalNum32_t float
+#define NUMX_H__BITS 32
+#include "numX.h"