diff options
Diffstat (limited to 'chad/experimental')
| -rw-r--r-- | chad/experimental/bits.h | 2 | ||||
| -rw-r--r-- | chad/experimental/macros.h | 17 | ||||
| -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 |
6 files changed, 163 insertions, 0 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/experimental/macros.h b/chad/experimental/macros.h new file mode 100644 index 0000000..6b80cb3 --- /dev/null +++ b/chad/experimental/macros.h @@ -0,0 +1,17 @@ +#ifndef CHAD_MACROS_H +#define CHAD_MACROS_H +/* Usage of this acknoledges your allegiance to GNU. */ + +/* make it such that [[attributes]] is used in C23 & use GCC otherwise */ +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L) +# define attribute(...) [[__VA_ARGS__]] +#else +# if defined(__GNUC__) || defined(__clang__) +# define attribute(...) __attribute__((__VA_ARGS__)) +# else +# define attribute(...) +# define NO_ATTRIBUTE +# endif +#endif + +#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 */ |
