diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-10-03 14:19:23 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-10-03 14:19:23 -0400 |
| commit | 8f346b48aa49ac0590c9c77edb75c63560398e1a (patch) | |
| tree | 4aa6315dc6a43e0118b6cb12efbb6193bea351c0 /stdlib | |
| parent | 35a19a2d1bb7605fa6ceb038b2b8afee760cd11c (diff) | |
Fix up some compiler flags around floating point numbers so they work
better with -Ofast and have more standardized behavior
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/integers.c | 12 | ||||
| -rw-r--r-- | stdlib/integers.h | 12 | ||||
| -rw-r--r-- | stdlib/nums.c | 16 | ||||
| -rw-r--r-- | stdlib/siphash.c | 2 | ||||
| -rw-r--r-- | stdlib/siphash.h | 4 |
5 files changed, 24 insertions, 22 deletions
diff --git a/stdlib/integers.c b/stdlib/integers.c index b0b19798..e6d466aa 100644 --- a/stdlib/integers.c +++ b/stdlib/integers.c @@ -406,7 +406,7 @@ public const TypeInfo_t Int$info = { }; -#define DEFINE_INT_TYPE(c_type, KindOfInt, fmt, min_val, max_val)\ +#define DEFINE_INT_TYPE(c_type, KindOfInt, fmt, min_val, max_val, to_attr)\ public Text_t KindOfInt ## $as_text(const c_type *i, bool colorize, const TypeInfo_t *type) { \ (void)type; \ if (!i) return Text(#KindOfInt); \ @@ -458,7 +458,7 @@ public const TypeInfo_t Int$info = { } \ return (c_type)((uint64_t)min + (r % range)); \ } \ - public Range_t KindOfInt ## $to(c_type from, c_type to) { \ + public to_attr Range_t KindOfInt ## $to(c_type from, c_type to) { \ return (Range_t){Int64_to_Int(from), Int64_to_Int(to), to >= from ? (Int_t){.small=(1<<2)&1} : (Int_t){.small=(1<<2)&1}}; \ } \ public PUREFUNC Optional ## KindOfInt ## _t KindOfInt ## $from_text(Text_t text) { \ @@ -481,10 +481,10 @@ public const TypeInfo_t Int$info = { .CustomInfo={.compare=(void*)KindOfInt##$compare, .as_text=(void*)KindOfInt##$as_text}, \ }; -DEFINE_INT_TYPE(int64_t, Int64, "ld[64]", INT64_MIN, INT64_MAX) -DEFINE_INT_TYPE(int32_t, Int32, "d[32]", INT32_MIN, INT32_MAX) -DEFINE_INT_TYPE(int16_t, Int16, "d[16]", INT16_MIN, INT16_MAX) -DEFINE_INT_TYPE(int8_t, Int8, "d[8]", INT8_MIN, INT8_MAX) +DEFINE_INT_TYPE(int64_t, Int64, "ld[64]", INT64_MIN, INT64_MAX, __attribute__(())) +DEFINE_INT_TYPE(int32_t, Int32, "d[32]", INT32_MIN, INT32_MAX, CONSTFUNC) +DEFINE_INT_TYPE(int16_t, Int16, "d[16]", INT16_MIN, INT16_MAX, CONSTFUNC) +DEFINE_INT_TYPE(int8_t, Int8, "d[8]", INT8_MIN, INT8_MAX, CONSTFUNC) #undef DEFINE_INT_TYPE // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/integers.h b/stdlib/integers.h index 8e98a126..a1eca956 100644 --- a/stdlib/integers.h +++ b/stdlib/integers.h @@ -22,7 +22,7 @@ #define I16(x) ((int16_t)x) #define I8(x) ((int8_t)x) -#define DEFINE_INT_TYPE(c_type, type_name) \ +#define DEFINE_INT_TYPE(c_type, type_name, to_attr) \ typedef struct { \ c_type i; \ bool is_null:1; \ @@ -35,7 +35,7 @@ Text_t type_name ## $octal(c_type i, Int_t digits, bool prefix); \ Array_t type_name ## $bits(c_type x); \ c_type type_name ## $random(c_type min, c_type max); \ - Range_t type_name ## $to(c_type from, c_type to); \ + to_attr Range_t type_name ## $to(c_type from, c_type to); \ PUREFUNC Optional ## type_name ## _t type_name ## $from_text(Text_t text); \ PUREFUNC static inline c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \ return x < min ? min : (x > max ? max : x); \ @@ -62,10 +62,10 @@ return type_name ## $modulo(D-1, d) + 1; \ } -DEFINE_INT_TYPE(int64_t, Int64) -DEFINE_INT_TYPE(int32_t, Int32) -DEFINE_INT_TYPE(int16_t, Int16) -DEFINE_INT_TYPE(int8_t, Int8) +DEFINE_INT_TYPE(int64_t, Int64, __attribute__(())) +DEFINE_INT_TYPE(int32_t, Int32, CONSTFUNC) +DEFINE_INT_TYPE(int16_t, Int16, CONSTFUNC) +DEFINE_INT_TYPE(int8_t, Int8, CONSTFUNC) #undef DEFINE_INT_TYPE #define NULL_INT64 ((OptionalInt64_t){.is_null=true}) diff --git a/stdlib/nums.c b/stdlib/nums.c index 1eba2862..1a8fec21 100644 --- a/stdlib/nums.c +++ b/stdlib/nums.c @@ -52,7 +52,7 @@ public Text_t Num$scientific(double f, Int_t precision) { return Text$format("%.*e", (int)Int_to_Int64(precision, false), f); } -public double Num$mod(double num, double modulus) { +public CONSTFUNC double Num$mod(double num, double modulus) { double result = fmod(num, modulus); return (result < 0) != (modulus < 0) ? result + modulus : result; } @@ -79,9 +79,9 @@ public double Num$nan(Text_t tag) { return nan(Text$as_c_string(tag)); } -public CONSTFUNC bool Num$isinf(double n) { return !!isinf(n); } -public CONSTFUNC bool Num$finite(double n) { return !!finite(n); } -public CONSTFUNC bool Num$isnan(double n) { return !!isnan(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), @@ -133,7 +133,7 @@ public Text_t Num32$scientific(float f, Int_t precision) { return Text$format("%.*e", (int)Int_to_Int64(precision, false), (double)f); } -public float Num32$mod(float num, float modulus) { +public CONSTFUNC float Num32$mod(float num, float modulus) { float result = fmodf(num, modulus); return (result < 0) != (modulus < 0) ? result + modulus : result; } @@ -160,9 +160,9 @@ public float Num32$nan(Text_t tag) { return nanf(Text$as_c_string(tag)); } -public CONSTFUNC bool Num32$isinf(float n) { return isinf(n); } -public CONSTFUNC bool Num32$finite(float n) { return finite(n); } -public CONSTFUNC bool Num32$isnan(float n) { return isnan(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), diff --git a/stdlib/siphash.c b/stdlib/siphash.c index 671fbad6..a8e64c8e 100644 --- a/stdlib/siphash.c +++ b/stdlib/siphash.c @@ -46,7 +46,7 @@ public uint64_t TOMO_HASH_KEY[2] = {23, 42}; // Randomized in tomo_init() #include "siphash-internals.h" -public uint64_t siphash24(const uint8_t *src, size_t src_sz) { +PUREFUNC public uint64_t siphash24(const uint8_t *src, size_t src_sz) { siphash sh; if ((uint64_t)src % __alignof__(uint64_t) == 0) { #pragma GCC diagnostic ignored "-Wcast-align" diff --git a/stdlib/siphash.h b/stdlib/siphash.h index 8104a306..67bad582 100644 --- a/stdlib/siphash.h +++ b/stdlib/siphash.h @@ -5,9 +5,11 @@ #include <stdint.h> #include <stddef.h> +#include "util.h" + // This value will be randomized on startup in tomo_init(): extern uint64_t TOMO_HASH_KEY[2]; -uint64_t siphash24(const uint8_t *src, size_t src_sz); +PUREFUNC uint64_t siphash24(const uint8_t *src, size_t src_sz); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
