Fix up some compiler flags around floating point numbers so they work

better with -Ofast and have more standardized behavior
This commit is contained in:
Bruce Hill 2024-10-03 14:19:23 -04:00
parent 35a19a2d1b
commit 8f346b48aa
9 changed files with 33 additions and 30 deletions

View File

@ -2,7 +2,8 @@ PREFIX=/usr
VERSION=0.0.1
CC=gcc
CCONFIG=-std=c23 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC -I. \
-fsanitize=signed-integer-overflow -fno-sanitize-recover -fvisibility=hidden -fdollars-in-identifiers
-fno-signed-zeros -fno-finite-math-only -fno-signaling-nans -fno-trapping-math \
-fsanitize=signed-integer-overflow -fno-sanitize-recover -fvisibility=hidden -fdollars-in-identifiers
LTO=-flto=auto -fno-fat-lto-objects -Wl,-flto
LDFLAGS=
# MAKEFLAGS := --jobs=$(shell nproc) --output-sync=target
@ -41,8 +42,8 @@ tomo: tomo.o $(BUILTIN_OBJS) ast.o parse.o environment.o types.o typecheck.o str
@$(CC) $(CFLAGS) $(CWARN) $(LDFLAGS) $^ $(LDLIBS) -o $@
libtomo.so: $(BUILTIN_OBJS)
@echo $(CC) $^ $(CFLAGS_PLACEHOLDER) $(G) $(O) $(OSFLAGS) -lgc -lcord -lm -lunistring -lgmp -ldl -Wl,-soname,libtomo.so -shared -o $@
@$(CC) $^ $(CFLAGS) $(CWARN) $(G) $(O) $(OSFLAGS) -lgc -lcord -lm -lunistring -lgmp -ldl -Wl,-soname,libtomo.so -shared -o $@
@echo $(CC) $^ $(CFLAGS_PLACEHOLDER) $(OSFLAGS) -lgc -lcord -lm -lunistring -lgmp -ldl -Wl,-soname,libtomo.so -shared -o $@
@$(CC) $^ $(CFLAGS) $(CWARN) $(OSFLAGS) -lgc -lcord -lm -lunistring -lgmp -ldl -Wl,-soname,libtomo.so -shared -o $@
tags:
ctags *.[ch] **/*.[ch]

View File

@ -2425,7 +2425,7 @@ PARSER(parse_use) {
if (m.length >= 0) {
text = Text$trim(text, Pattern("http{0-1 s}://"), true, false);
FILE *shasum = popen(heap_strf("echo -n '%s' | sha256sum", Text$as_c_string(text)), "r");
const int HASH_LEN = 32;
const size_t HASH_LEN = 32;
char *hash = GC_MALLOC_ATOMIC(HASH_LEN + 1);
size_t just_read = fread(hash, sizeof(char), HASH_LEN, shasum);
if (just_read < HASH_LEN)

View File

@ -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

View File

@ -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})

View File

@ -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),

View File

@ -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"

View File

@ -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

5
tomo.c
View File

@ -43,6 +43,7 @@ static OptionalBool_t verbose = false,
static OptionalText_t autofmt = Text("sed '/^\\s*$/d' | indent -kr -l100 -nbbo -nut -sob"),
cflags = Text("-fdollars-in-identifiers -std=gnu11 -Wno-trigraphs -fsanitize=signed-integer-overflow -fno-sanitize-recover"
" -fno-signed-zeros -fno-finite-math-only -fno-signaling-nans -fno-trapping-math -fno-finite-math-only"
" -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE -fPIC -ggdb"
" -I$HOME/.local/share/tomo/installed"),
ldlibs = Text("-lgc -lgmp -lm -ltomo"),
@ -292,8 +293,8 @@ void build_library(Text_t lib_dir_name)
globfree(&tm_files);
prog = run_cmd("%k %k -O%k %k %k %s -Wl,-soname='lib%k.so' -shared %s -o 'lib%k.so'",
&cc, &cflags, &optimization, &ldflags, &ldlibs, array_str(extra_ldlibs), &lib_dir_name,
prog = run_cmd("%k -O%k %k %k %k %s -Wl,-soname='lib%k.so' -shared %s -o 'lib%k.so'",
&cc, &optimization, &cflags, &ldflags, &ldlibs, array_str(extra_ldlibs), &lib_dir_name,
array_str(object_files), &lib_dir_name);
if (!prog)
errx(1, "Failed to run C compiler: %k", &cc);

View File

@ -130,9 +130,8 @@ PUREFUNC type_t *get_math_type(env_t *env, ast_t *ast, type_t *lhs_t, type_t *rh
switch (compare_precision(lhs_t, rhs_t)) {
case NUM_PRECISION_EQUAL: case NUM_PRECISION_MORE: return lhs_t;
case NUM_PRECISION_LESS: return rhs_t;
default: return NULL;
default: code_err(ast, "Math operations between %T and %T are not supported", lhs_t, rhs_t);
}
code_err(ast, "Math operations between %T and %T are not supported", lhs_t, rhs_t);
}
static env_t *load_module(env_t *env, ast_t *module_ast)