diff options
| author | Emil Williams | 2026-02-20 06:18:48 +0000 |
|---|---|---|
| committer | Emil Williams | 2026-02-20 06:18:48 +0000 |
| commit | 88e12f4a22111903fc23488898f073502639635c (patch) | |
| tree | 87963ce958b8a48be77bf9895f7527f532008dab | |
| parent | d352d3cf415083fd0f07321273d1d03b26a323e0 (diff) | |
| download | libchad-88e12f4a22111903fc23488898f073502639635c.tar.xz libchad-88e12f4a22111903fc23488898f073502639635c.tar.zst | |
Make experimental and librarize
| -rw-r--r-- | Makefile | 16 | ||||
| -rw-r--r-- | chad.h | 2 | ||||
| -rw-r--r-- | chad/experimental/bits.h | 2 | ||||
| -rw-r--r-- | chad/experimental/macros.h (renamed from chad/macros.h) | 13 | ||||
| -rw-r--r-- | chad/experimental/root.c | 13 | ||||
| -rw-r--r-- | chad/experimental/root.h | 5 | ||||
| -rw-r--r-- | chad/experimental/timespec.c | 86 | ||||
| -rw-r--r-- | chad/experimental/timespec.h | 40 | ||||
| -rw-r--r-- | chad/root.h | 17 | ||||
| -rw-r--r-- | chad/timespec.h | 114 | ||||
| -rw-r--r-- | test/test.c | 2 |
11 files changed, 159 insertions, 151 deletions
@@ -1,10 +1,14 @@ CFLAGS := -std=c23 -fPIC -CPPFLAGS := -Ichad -SOURCE.orig := $(wildcard extern/*.c) -SOURCE := $(SOURCE.orig:extern/%=%) -OBJECT := $(addprefix object/,$(SOURCE:.c=.o)) +CPPFLAGS := -I. -D_XOPEN_SOURCE=500 +SOURCE.orig := $(wildcard extern/*.c chad/*.c chad/experimental/*.c) +SOURCE.orig := $(SOURCE.orig:extern/%=%) +SOURCE.orig := $(SOURCE.orig:chad/experimental/%=%) +SOURCE.orig := $(SOURCE.orig:chad/%=%) +OBJECT := $(addprefix object/,$(SOURCE.orig:.c=.o)) vpath %.c extern +vpath %.c chad +vpath %.c chad/experimental object/%.o: %.c @echo "CC $<" @@ -14,11 +18,11 @@ object/%.o: %.c all: object/libchad.a object/libchad.so object/libchad.a: ${OBJECT} - @echo "AR $<" + @echo "AR $+" @ar rcs $@ $+ object/libchad.so: ${OBJECT} - @echo "SO $<" + @echo "SO $+" @${CC} ${CFLAGS} ${CPPFLAGS} -shared $+ -o $@ dist: @@ -1,6 +1,8 @@ #ifndef CHAD_H #define CHAD_H // internal code +#include "chad/experimental/macros.h" +#include "chad/experimental/timespec.h" #include "chad/experimental/bits.h" #include "chad/experimental/terminal.h" #include "chad/dictate.h" diff --git a/chad/experimental/bits.h b/chad/experimental/bits.h index a369ded..f2972a4 100644 --- a/chad/experimental/bits.h +++ b/chad/experimental/bits.h @@ -8,6 +8,8 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define CLAMP(a, b, c) ((a) < (b) ? (b) : (a) > (c) ? (c) : (a)) + /* Convert argument to a string literal. */ diff --git a/chad/macros.h b/chad/experimental/macros.h index 9d05083..6b80cb3 100644 --- a/chad/macros.h +++ b/chad/experimental/macros.h @@ -14,17 +14,4 @@ # endif #endif -#if !defined(NO_ATTRIBUTE) -# define always_inline static inline attribute(always_inline) -# define alias(x) attribute(alias(x)) -#else -# define always_inline static inline -# define NOT_ALWAYS_INLINE -# define NO_ALIASES -#endif - -#define MIN(a,b) ((a)<(b)?(a):(b)) -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define CLAMP(a,b,c) (a)<(b)?(b):(a)>(c)?(c):(a) - #endif /* CHAD_MACROS_H */ diff --git a/chad/experimental/root.c b/chad/experimental/root.c new file mode 100644 index 0000000..f7447eb --- /dev/null +++ b/chad/experimental/root.c @@ -0,0 +1,13 @@ +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <chad/experimental/root.h> +void Root(char * filename) { + char path[PATH_MAX], * terminator; + if (!realpath(filename, path)) { return; } + if ((terminator = strrchr(path, '/'))) { + *terminator = '\0'; + chdir(path); + } +} diff --git a/chad/experimental/root.h b/chad/experimental/root.h new file mode 100644 index 0000000..e54fd80 --- /dev/null +++ b/chad/experimental/root.h @@ -0,0 +1,5 @@ +#ifdef CHAD_ROOT_H +#define CHAD_ROOT_H +/* check errno -- chdir */ +void Root(char * filename); +#endif /* CHAD_ROOT_H */ diff --git a/chad/experimental/timespec.c b/chad/experimental/timespec.c new file mode 100644 index 0000000..dbcca82 --- /dev/null +++ b/chad/experimental/timespec.c @@ -0,0 +1,86 @@ +#include <chad/experimental/timespec.h> + +/* --- conversion --- */ + +f64 timespec_to_f64(timespec_t ts) +{ return (f64) ts.tv_sec + ((f64) ts.tv_nsec / TIMESPEC_HZ); } + +f32 timespec_to_f32(timespec_t ts) +{ return (f32) ts.tv_sec + ((f32) ts.tv_nsec / TIMESPEC_HZ); } + +double timespec_to_double(timespec_t ts) +{ return (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); } + +float timespec_to_float(timespec_t ts) +{ return (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); } + +/* --- from --- */ + +void f64_from_timespec(timespec_t ts, f64 * r) +{ *r = (f64) ts.tv_sec + ((f64) ts.tv_nsec / TIMESPEC_HZ); } + +void f32_from_timespec(timespec_t ts, f32 * r) +{ *r = (f32) ts.tv_sec + ((f32) ts.tv_nsec / TIMESPEC_HZ); } + +void double_from_timespec(timespec_t ts, double * r) +{ *r = (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); } + +void float_from_timespec(timespec_t ts, float * r) +{ *r = (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); } + +/* --- math --- */ + +timespec_t timespec_add(timespec_t a, timespec_t b) { + a.tv_sec += b.tv_sec; + a.tv_nsec += b.tv_nsec; + if (a.tv_nsec >= TIMESPEC_HZ) { + a.tv_sec++; + a.tv_nsec -= TIMESPEC_HZ; + } + return a; +} + +timespec_t timespec_sub(timespec_t a, timespec_t b) { + a.tv_sec -= b.tv_sec; + a.tv_nsec -= b.tv_nsec; + if (a.tv_nsec < 0) { + a.tv_sec--; + a.tv_nsec += TIMESPEC_HZ; + } + return a; +} + +int timespec_cmp(timespec_t a, timespec_t b) { + return a.tv_sec > b.tv_sec ? + (1) : + a.tv_sec < b.tv_sec ? + (-1) : + ( + a.tv_nsec > b.tv_nsec ? + (1) : + a.tv_nsec < b.tv_nsec ? + (-1) : 0 + ); +} + +timespec_t timespec_max(timespec_t a, timespec_t b) { + return a.tv_sec > b.tv_sec ? + a : + a.tv_sec < b.tv_sec ? + b : + ( + a.tv_nsec > b.tv_nsec ? + a : b + ); +} + +timespec_t timespec_min(timespec_t a, timespec_t b) { + return a.tv_sec < b.tv_sec ? + a : + a.tv_sec > b.tv_sec ? + b : + ( + a.tv_nsec < b.tv_nsec ? + a : b + ); +} diff --git a/chad/experimental/timespec.h b/chad/experimental/timespec.h new file mode 100644 index 0000000..f72f431 --- /dev/null +++ b/chad/experimental/timespec.h @@ -0,0 +1,40 @@ +#ifndef TIMESPEC_H +#define TIMESPEC_H +/* ripped partly from glibc */ +#include <time.h> +#include <chad/experimental/macros.h> +#include <chad/terry.h> + +/* -- pontentially over designed and undertested. */ +/* -- consider this "LIKELY TO BE REVISED OR REMOVED!" */ +/* -- ESPECIALLY the generics and the from group */ + +long TIMESPEC_HZ = 1000000000L; + +typedef struct timespec timespec_t; + +static const timespec_t one_second = {1, 0}, zero_seconds = {0, 0}; + +f64 timespec_to_f64(timespec_t ts); +f32 timespec_to_f32(timespec_t ts); +double timespec_to_double(timespec_t ts); +float timespec_to_float(timespec_t ts); + +#ifdef TERRY_SMALL_FLOAT_IMPRECISE +#define from_timespec(ts,to) _Generic((to), double *: double_from_timespec, float *: float_from_timespec)(ts,to) +#else +#define from_timespec(ts,to) _Generic((to), f64 *: f64_from_timespec, f32 *: f32_from_timespec, double *: double_from_timespec, float *: float_from_timespec)(ts,to) +#endif + +void f64_from_timespec(timespec_t ts, f64 * r); +void f32_from_timespec(timespec_t ts, f32 * r); +void double_from_timespec(timespec_t ts, double * r); +void float_from_timespec(timespec_t ts, float * r); + +timespec_t timespec_add(timespec_t a, timespec_t b); +timespec_t timespec_sub(timespec_t a, timespec_t b); +/* -1, 0, 1 */ +int timespec_cmp(timespec_t a, timespec_t b); +timespec_t timespec_max(timespec_t a, timespec_t b); +timespec_t timespec_min(timespec_t a, timespec_t b); +#endif /* TIMESPEC_H */ diff --git a/chad/root.h b/chad/root.h deleted file mode 100644 index 95a3f8b..0000000 --- a/chad/root.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifdef CHAD_ROOT_H -#define CHAD_ROOT_H -#include <limits.h> -#include "macros.h" -/* check errno -- chdir */ -always_inline void Root(char * filename) { - extern char * realpath(const char * restrict path, char * restrict resolved_path); - extern int chdir(const char * path); - attribute(noreturn) void abort(void); - char path[PATH_MAX], * terminator; - if (!realpath(filename, path)) { return; } - if ((terminator = strrchr(path, '/'))) { - *terminator = '\0'; - chdir(path); - } -} -#endif /* CHAD_ROOT_H */ diff --git a/chad/timespec.h b/chad/timespec.h deleted file mode 100644 index 6fbf5aa..0000000 --- a/chad/timespec.h +++ /dev/null @@ -1,114 +0,0 @@ -#ifndef TIMESPEC_H -#define TIMESPEC_H -/* ripped partly from glibc */ -#include <time.h> -#include "macros.h" -#include "terry.h" - -/* -- pontentially over designed and undertested. */ -/* -- consider this "LIKELY TO BE REVISED OR REMOVED!" */ -/* -- ESPECIALLY the generics and the from group */ - -long TIMESPEC_HZ = 1000000000L; - -typedef struct timespec timespec_t; - -static const timespec_t one_second = {1, 0}, zero_seconds = {0, 0}; - -always_inline f64 timespec_to_f64(timespec_t ts) -{ return (f64) ts.tv_sec + ((f64) ts.tv_nsec / TIMESPEC_HZ); } - -always_inline f32 timespec_to_f32(timespec_t ts) -{ return (f32) ts.tv_sec + ((f32) ts.tv_nsec / TIMESPEC_HZ); } - -#if defined(TERRY_SMALL_FLOAT_IMPRECISE) && !defined(NO_ALIASES) -always_inline double timespec_to_double(timespec_t ts) alias("timespec_to_f64"); -always_inline float timespec_to_float(timespec_t ts) alias("timespec_to_f32"); -#else -always_inline double timespec_to_double(timespec_t ts) -{ return (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); } -always_inline float timespec_to_float(timespec_t ts) -{ return (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); } -#endif - - -#ifdef TERRY_SMALL_FLOAT_IMPRECISE -#define _from_timespec(ts,to) _Generic((to), double *: double_from_timespec, float *: float_from_timespec)(ts,to) -#else -#define _from_timespec(ts,to) _Generic((to), f64 *: f64_from_timespec, f32 *: f32_from_timespec, double *: double_from_timespec, float *: float_from_timespec)(ts,to) -#endif - -always_inline void from_timespec(timespec_t ts, ) - -always_inline void f64_from_timespec(timespec_t ts, f64 * r) -{ *r = (f64) ts.tv_sec + ((f64) ts.tv_nsec / TIMESPEC_HZ); } - -always_inline void f32_from_timespec(timespec_t ts, f32 * r) -{ *r = (f32) ts.tv_sec + ((f32) ts.tv_nsec / TIMESPEC_HZ); } - -#if defined(TERRY_SMALL_FLOAT_IMPRECISE) && !defined(NO_ALIASES) -always_inline void double_from_timespec(timespec_t ts, double * r) alias("timespec_to_f64"); -always_inline void float_from_timespec(timespec_t ts, float * r) alias("timespec_to_f32"); -#else -always_inline void double_from_timespec(timespec_t ts, double * r) -{ *r = (double) ts.tv_sec + ((double) ts.tv_nsec / TIMESPEC_HZ); } -always_inline void float_from_timespec(timespec_t ts, float * r) -{ *r = (float) ts.tv_sec + ((float) ts.tv_nsec / TIMESPEC_HZ); } -#endif - -always_inline timespec_t timespec_add(timespec_t a, timespec_t b) { - a.tv_sec += b.tv_sec; - a.tv_nsec += b.tv_nsec; - if (a.tv_nsec >= TIMESPEC_HZ) { - a.tv_sec++; - a.tv_nsec -= TIMESPEC_HZ; - } - return a; -} - -always_inline timespec_t timespec_sub(timespec_t a, timespec_t b) { - a.tv_sec -= b.tv_sec; - a.tv_nsec -= b.tv_nsec; - if (a.tv_nsec < 0) { - a.tv_sec--; - a.tv_nsec += TIMESPEC_HZ; - } - return a; -} - -always_inline int timespec_cmp(timespec_t a, timespec_t b) { - return a.tv_sec > b.tv_sec ? - (1) : - a.tv_sec < b.tv_sec ? - (-1) : - ( - a.tv_nsec > b.tv_nsec ? - (1) : - a.tv_nsec < b.tv_nsec ? - (-1) : 0 - ); -} - -always_inline timespec_t timespec_max(timespec_t a, timespec_t b) { - return a.tv_sec > b.tv_sec ? - a : - a.tv_sec < b.tv_sec ? - b : - ( - a.tv_nsec > b.tv_nsec ? - a : b - ); -} - -always_inline timespec_t timespec_min(timespec_t a, timespec_t b) { - return a.tv_sec < b.tv_sec ? - a : - a.tv_sec > b.tv_sec ? - b : - ( - a.tv_nsec < b.tv_nsec ? - a : b - ); -} - -#endif /* TIMESPEC_H */ diff --git a/test/test.c b/test/test.c index 4f9df76..47abd32 100644 --- a/test/test.c +++ b/test/test.c @@ -1,4 +1,4 @@ -// @BAKE @CC -std=c23 ../object/libchad.a +// @BAKE @CC -std=c23 -I.. ../object/libchad.a #include "../chad.h" int main () { dictate ( "cogito ergo sum\n" ); |
