aboutsummaryrefslogtreecommitdiff
path: root/chad
diff options
context:
space:
mode:
authorEmil Williams2026-02-20 06:18:48 +0000
committerEmil Williams2026-02-20 06:18:48 +0000
commit88e12f4a22111903fc23488898f073502639635c (patch)
tree87963ce958b8a48be77bf9895f7527f532008dab /chad
parentd352d3cf415083fd0f07321273d1d03b26a323e0 (diff)
downloadlibchad-88e12f4a22111903fc23488898f073502639635c.tar.xz
libchad-88e12f4a22111903fc23488898f073502639635c.tar.zst
Make experimental and librarize
Diffstat (limited to 'chad')
-rw-r--r--chad/experimental/bits.h2
-rw-r--r--chad/experimental/macros.h (renamed from chad/macros.h)13
-rw-r--r--chad/experimental/root.c13
-rw-r--r--chad/experimental/root.h5
-rw-r--r--chad/experimental/timespec.c86
-rw-r--r--chad/experimental/timespec.h40
-rw-r--r--chad/root.h17
-rw-r--r--chad/timespec.h114
8 files changed, 146 insertions, 144 deletions
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 */