aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-03 13:37:05 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-03 13:37:05 -0500
commitbf5a7253458ec5b3f4fe054b3b2f5f80211c1892 (patch)
treee2ec63d23498c661bfda29dafa3e0311ed1140d1 /builtins
parentec7a9e5f107a68072b3a4dd84aa6a4b461657345 (diff)
Definitively go with "Int" and "Num" over "Int64" and "Num64", plus add
Int__bits()
Diffstat (limited to 'builtins')
-rw-r--r--builtins/array.h2
-rw-r--r--builtins/integers.c14
-rw-r--r--builtins/integers.h16
-rw-r--r--builtins/nums.c30
-rw-r--r--builtins/nums.h43
5 files changed, 53 insertions, 52 deletions
diff --git a/builtins/array.h b/builtins/array.h
index 76ab29fd..021166b9 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -12,7 +12,7 @@
const array_t $arr = x; int64_t $index = (int64_t)(i); \
int64_t $off = $index + ($index < 0) * ($arr.length + 1) - 1; \
if (__builtin_expect($off < 0 || $off >= $arr.length, 0)) \
- fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int64__as_str(&$index, USE_COLOR, NULL), $arr.length); \
+ fail_source(filename, start, end, "Invalid array index: %r (array has length %ld)\n", Int__as_str(&$index, USE_COLOR, NULL), $arr.length); \
(type*)($arr.data + $arr.stride * $off);})
#define $Array_get_unchecked(type, x, i) *({ const array_t $arr = x; int64_t $index = (int64_t)(i); \
int64_t $off = $index + ($index < 0) * ($arr.length + 1) - 1; \
diff --git a/builtins/integers.c b/builtins/integers.c
index ab61b3b9..65046ad3 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -6,9 +6,10 @@
#include "../SipHash/halfsiphash.h"
#include "array.h"
+#include "datatypes.h"
#include "integers.h"
-#include "types.h"
#include "string.h"
+#include "types.h"
#define xstr(a) str(a)
#define str(a) #a
@@ -37,6 +38,15 @@
const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo"; \
return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)i); \
} \
+ public array_t KindOfInt ## __bits(c_type x) { \
+ array_t bit_array = (array_t){.data=GC_MALLOC_ATOMIC(sizeof(bool)*8*sizeof(c_type)), .atomic=1, .stride=sizeof(bool), .length=8*sizeof(c_type)}; \
+ bool *bits = bit_array.data + sizeof(c_type)*8; \
+ for (size_t i = 0; i < 8*sizeof(c_type); i++) { \
+ *(bits--) = x & 1; \
+ x >>= 1; \
+ } \
+ return bit_array; \
+ } \
public c_type KindOfInt ## __random(int64_t min, int64_t max) { \
if (min > max) fail("Random min (%ld) is larger than max (%ld)", min, max); \
if (min < (int64_t)min_val) fail("Random min (%ld) is smaller than the minimum "#KindOfInt" value", min); \
@@ -55,7 +65,7 @@
.CustomInfo={.compare=(void*)KindOfInt##__compare, .as_str=(void*)KindOfInt##__as_str}, \
};
-DEFINE_INT_TYPE(int64_t, Int64, "ld", INT64_MIN, INT64_MAX);
+DEFINE_INT_TYPE(int64_t, Int, "ld", INT64_MIN, INT64_MAX);
DEFINE_INT_TYPE(int32_t, Int32, "d_i32", INT32_MIN, INT32_MAX);
DEFINE_INT_TYPE(int16_t, Int16, "d_i16", INT16_MIN, INT16_MAX);
DEFINE_INT_TYPE(int8_t, Int8, "d_i8", INT8_MIN, INT8_MAX);
diff --git a/builtins/integers.h b/builtins/integers.h
index 65a52a59..1b42ee1e 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -3,13 +3,13 @@
#include <stdbool.h>
#include <stdint.h>
+#include "datatypes.h"
#include "types.h"
-#define Int64_t int64_t
+#define Int_t int64_t
#define Int32_t int32_t
#define Int16_t int16_t
#define Int8_t int8_t
-#define Int_t int64_t
#define I64(x) ((int64_t)x)
#define I32(x) ((int32_t)x)
#define I16(x) ((int16_t)x)
@@ -21,28 +21,20 @@
CORD type_name ## __format(c_type i, int64_t digits); \
CORD type_name ## __hex(c_type i, int64_t digits, bool uppercase, bool prefix); \
CORD type_name ## __octal(c_type i, int64_t digits, bool prefix); \
+ array_t type_name ## __bits(c_type x); \
c_type type_name ## __random(int64_t min, int64_t max); \
extern const c_type type_name ## __min, type_name##__max; \
extern const TypeInfo type_name;
-DEFINE_INT_TYPE(int64_t, Int64);
+DEFINE_INT_TYPE(int64_t, Int);
DEFINE_INT_TYPE(int32_t, Int32);
DEFINE_INT_TYPE(int16_t, Int16);
DEFINE_INT_TYPE(int8_t, Int8);
#undef DEFINE_INT_TYPE
#define Int__abs(...) I64(labs(__VA_ARGS__))
-#define Int64__abs(...) I64(labs(__VA_ARGS__))
#define Int32__abs(...) I32(abs(__VA_ARGS__))
#define Int16__abs(...) I16(abs(__VA_ARGS__))
#define Int8__abs(...) I8(abs(__VA_ARGS__))
-#define Int__as_str Int64__as_str
-#define Int__compare Int64__compare
-#define Int__format Int64__format
-#define Int__hex Int64__hex
-#define Int__octal Int64__octal
-#define Int__random Int64__random
-#define Int Int64
-
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
diff --git a/builtins/nums.c b/builtins/nums.c
index 419a3667..456da93a 100644
--- a/builtins/nums.c
+++ b/builtins/nums.c
@@ -14,54 +14,54 @@
#include "string.h"
#include "types.h"
-public CORD Num64__as_str(const double *f, bool colorize, const TypeInfo *type) {
+public CORD Num__as_str(const double *f, bool colorize, const TypeInfo *type) {
(void)type;
- if (!f) return "Num64";
+ if (!f) return "Num";
CORD c;
if (colorize) CORD_sprintf(&c, "\x1b[35m%g\x1b[33;2m\x1b[m", *f);
else CORD_sprintf(&c, "%g", *f);
return c;
}
-public int32_t Num64__compare(const double *x, const double *y, const TypeInfo *type) {
+public int32_t Num__compare(const double *x, const double *y, const TypeInfo *type) {
(void)type;
return (*x > *y) - (*x < *y);
}
-public bool Num64__equal(const double *x, const double *y, const TypeInfo *type) {
+public bool Num__equal(const double *x, const double *y, const TypeInfo *type) {
(void)type;
return *x == *y;
}
-public CORD Num64__format(double f, int64_t precision) {
+public CORD Num__format(double f, int64_t precision) {
return CORD_asprintf("%.*f", (int)precision, f);
}
-public CORD Num64__scientific(double f, int64_t precision) {
+public CORD Num__scientific(double f, int64_t precision) {
return CORD_asprintf("%.*e", (int)precision, f);
}
-public double Num64__mod(double num, double modulus) {
+public double Num__mod(double num, double modulus) {
double result = fmod(num, modulus);
return (result < 0) != (modulus < 0) ? result + modulus : result;
}
-public double Num64__nan(CORD tag) {
+public double Num__nan(CORD tag) {
return nan(CORD_to_const_char_star(tag));
}
-public bool Num64__isinf(double n) { return isinf(n); }
-public bool Num64__finite(double n) { return finite(n); }
-public bool Num64__isnan(double n) { return isnan(n); }
+public bool Num__isinf(double n) { return isinf(n); }
+public bool Num__finite(double n) { return finite(n); }
+public bool Num__isnan(double n) { return isnan(n); }
-public const TypeInfo Num64 = {
+public const TypeInfo Num = {
.size=sizeof(double),
.align=__alignof__(double),
.tag=CustomInfo,
.CustomInfo={
- .compare=(void*)Num64__compare,
- .equal=(void*)Num64__equal,
- .as_str=(void*)Num64__as_str,
+ .compare=(void*)Num__compare,
+ .equal=(void*)Num__equal,
+ .as_str=(void*)Num__as_str,
},
};
diff --git a/builtins/nums.h b/builtins/nums.h
index aeeeaa85..e8fdceaa 100644
--- a/builtins/nums.h
+++ b/builtins/nums.h
@@ -6,40 +6,39 @@
#include "types.h"
-#define Num64_t double
-#define Num32_t float
#define Num_t double
+#define Num32_t float
-CORD Num64__as_str(const double *f, bool colorize, const TypeInfo *type);
-int32_t Num64__compare(const double *x, const double *y, const TypeInfo *type);
-bool Num64__equal(const double *x, const double *y, const TypeInfo *type);
-CORD Num64__format(double f, int64_t precision);
-CORD Num64__scientific(double f, int64_t precision);
-double Num64__mod(double num, double modulus);
-bool Num64__isinf(double n);
-bool Num64__finite(double n);
-bool Num64__isnan(double n);
-double Num64__nan(CORD tag);
+CORD Num__as_str(const double *f, bool colorize, const TypeInfo *type);
+int32_t Num__compare(const double *x, const double *y, const TypeInfo *type);
+bool Num__equal(const double *x, const double *y, const TypeInfo *type);
+CORD Num__format(double f, int64_t precision);
+CORD Num__scientific(double f, int64_t precision);
+double Num__mod(double num, double modulus);
+bool Num__isinf(double n);
+bool Num__finite(double n);
+bool Num__isnan(double n);
+double Num__nan(CORD tag);
// Constants:
-#define C(name) const double Num64__##name = M_##name;
+#define C(name) const double Num__##name = M_##name;
C(2_SQRTPI) C(E) C(PI_2) C(2_PI) C(1_PI) C(LN10) C(LN2) C(LOG2E) C(PI) C(PI_4) C(SQRT2) C(SQRT1_2)
-const double Num64__INF = INFINITY, Num64__TAU = 2.*M_PI;
+const double Num__INF = INFINITY, Num__TAU = 2.*M_PI;
#undef C
-double Num64__random(void);
-bool Num64__finite(double n);
-bool Num64__isinf(double n);
-bool Num64__isnan(double n);
-#define F(name) double (*Num64__##name)(double n) = name;
-double (*Num64__abs)(double) = fabs;
+double Num__random(void);
+bool Num__finite(double n);
+bool Num__isinf(double n);
+bool Num__isnan(double n);
+#define F(name) double (*Num__##name)(double n) = name;
+double (*Num__abs)(double) = fabs;
F(acos) F(acosh) F(asin) F(asinh) F(atan) F(atanh) F(cbrt) F(ceil) F(cos) F(cosh) F(erf) F(erfc)
F(exp) F(exp2) F(expm1) F(floor) F(j0) F(j1) F(log) F(log10) F(log1p) F(log2) F(logb)
F(rint) F(round) F(significand) F(sin) F(sinh) F(sqrt)
F(tan) F(tanh) F(tgamma) F(trunc) F(y0) F(y1)
#undef F
-#define F(name) double (*Num64__##name)(double x, double y) = name;
+#define F(name) double (*Num__##name)(double x, double y) = name;
F(atan2) F(copysign) F(fdim) F(hypot) F(nextafter) F(pow) F(remainder)
#undef F
-extern const TypeInfo Num64;
+extern const TypeInfo Num;
CORD Num32__as_str(const float *f, bool colorize, const TypeInfo *type);
int32_t Num32__compare(const float *x, const float *y, const TypeInfo *type);