From d8202d799486fbda05aa3a693ae95a31da5e5e09 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 7 Jul 2021 03:04:48 +0100 Subject: [PATCH] =?UTF-8?q?Fortune=20for=20naka's=20current=20commit:=20Sm?= =?UTF-8?q?all=20blessing=20=E2=88=92=20=E5=B0=8F=E5=90=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ Makefile | 79 +++++++++++++++++++++++++++ include/ints.h | 49 +++++++++++++++++ include/macros.h | 136 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 16 ++++++ 5 files changed, 283 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 include/ints.h create mode 100644 include/macros.h create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..245fe74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +obj/ +naka-* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bb82a59 --- /dev/null +++ b/Makefile @@ -0,0 +1,79 @@ +# Generic C and C++ Makefile project template +# Contains targets for `release', `debug', and `clean'. + +PROJECT=naka +DESCRIPTION="Find a file within another file" +AUTHOR=Avril (Flanchan) + +SRC_C = $(wildcard src/*.c) +SRC_CXX = $(wildcard src/*.cpp) + +INCLUDE=include + +COMMON_FLAGS+= -W -Wall -fno-strict-aliasing $(addprefix -I,$(INCLUDE)) + +ARCH?=native +OPT_FLAGS+= -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ + -fno-stack-check + +CXX_OPT_FLAGS+= $(OPT_FLAGS) -felide-constructors + +CFLAGS += $(COMMON_FLAGS) --std=gnu11 +CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -fno-exceptions +LDFLAGS += + +STRIP=strip + +RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) $(addprefix -march=,$(ARCH)) +RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) $(addprefix -march=,$(ARCH)) +RELEASE_LDFLAGS?= -O3 -flto + +DEBUG_CFLAGS?= -O0 -g -DDEBUG +DEBUG_CXXFLAGS?= $(DEBUG_CFLAGS) +DEBUG_LDFLAGS?= + +# Objects + +OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o)) +OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o)) +OBJ = $(OBJ_C) $(OBJ_CXX) + +# Phonies + +.PHONY: release +release: | dirs $(PROJECT)-release + +.PHONY: debug +debug: | dirs $(PROJECT)-debug + +# Targets + +dirs: + @mkdir -p obj/c{,xx}/src + +obj/c/%.o: %.c + $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) + +obj/cxx/%.o: %.cpp + $(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS) + +$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) +$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) +$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS) +$(PROJECT)-release: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) + $(STRIP) $@ + +$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) +$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) +$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS) +$(PROJECT)-debug: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) + +clean-rebuild: + rm -rf obj + +clean: clean-rebuild + rm -f $(PROJECT)-{release,debug,pgo} + diff --git a/include/ints.h b/include/ints.h new file mode 100644 index 0000000..c67c22c --- /dev/null +++ b/include/ints.h @@ -0,0 +1,49 @@ +#ifndef _INTS_H +#define _INTS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + + +#define DEF(s, n) typedef s ## int ## n ## _t s ## n +#define DEFINT(n) typedef uint ## n ## _t u ## n; \ + typedef int ## n ## _t i ## n + +DEFINT(8); +DEFINT(16); +DEFINT(32); +DEFINT(64); + +#ifdef __cplusplus +#else +#include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +typedef signed __int128 i128; +typedef unsigned __int128 u128; +#pragma GCC diagnostic pop +#endif +typedef float f32; +typedef double f64; + +typedef size_t usize; +typedef ssize_t isize; + +typedef bool u1; +typedef bool i1; + +typedef uintptr_t ptr_t; + +#undef DEFINT +#undef DEF + +#ifdef __cplusplus +} +#endif + +#endif /* _INTS_H */ diff --git a/include/macros.h b/include/macros.h new file mode 100644 index 0000000..97edf59 --- /dev/null +++ b/include/macros.h @@ -0,0 +1,136 @@ +/// Macros header +/// +/// # Features +/// * `_EVAL_DEBUG_ONLY_STMTS` - Evaluate expressions passed to macros that are no-ops when not in debug mode. +#ifndef _MACROS_H +#define _MACROS_H + +#include +#include +#include +#include + +// Attribute macros + +#define _pure __attribute__((const)) +#define _readonly __attribute__((pure)) + +#ifndef __cplusplus +#define noreturn __attribute__((noreturn)) +#define deprecated __attribute__((deprecated)) +#define deprecated_message(str) __attribute__((deprecated(str))) +#endif + +#define noinline __attribute__((noinline)) +#define always_inline __attribute__((always_inline)) + +#ifdef DEBUG +#define _mixin static inline always_inline +#else +#define _mixin extern inline always_inline +#endif + +#define unrestrict __attribute__((may_alias)) +#define _shared unrestrict + +#ifdef __cplusplus +#define restrict __restrict__ +#endif + +// Type macros + +#define AS(x, t) ((t)(x)) + +#ifdef __cplusplus +#define let auto +#define var(x) decltype(x) +#else +#define let __auto_type +#define var(x) __typeof((x)) +#endif + +// Control flow macros + +#define LIKELY(x) __builtin_expect(!!(x), 1) +#define UNLIKELY(x) __builtin_expect(!!(x), 0) + +/// Equivalent to GNU C `x ?: y` operator. +#define TERN_OR(x, y) ({ let _x = (x); _x ? _x : (y); }) + +// Statement macros + +#define _no_op ((void)0) +#define IGNORE(x) ((void)(x)) +_mixin void _drain_val(void* x, ...) { IGNORE(x); } +#define _drain(...) _drain_val(NULL, __VA_ARGS__) + +// Allocation macros + +#define box(t) aligned_alloc(_Alignof(t), sizeof(t)) +#define stackalloc(t) __builtin_alloca_with_align(sizeof(t), _Alignof(t)) + +// Function macros + +#define TRACEx(X, msg, ...) (fprintf(stderr, "[" X "] " __FILE__ ":%d->%s(): " msg "\n", __LINE__, __func__, ## __VA_ARGS__)) + +#ifdef DEBUG +#define TRACE(msg, ...) TRACEx("trace", msg, ## __VA_ARGS__) +#elif defined(_EVAL_DEBUG_ONLY_STMTS) +#define TRACE(msg, ...) _drain(msg, ## __VA_ARGS__) +#else +#define TRACE(msg, ...) _no_op +#endif +#define INFO(msg, ...) TRACEx("info", msg, ## __VA_ARGS__) +#define WARN(msg, ...) TRACEx("warning", msg, ## __VA_ARGS__) +#define ERROR(msg, ...) TRACEx("error", msg, ## __VA_ARGS__) + +#define FATAL(msg, ...) (TRACEx("FATAL", msg, ## __VA_ARGS__), abort()) + +// Assertion macros + +#ifdef DEBUG +#define debug_assert(x) assert((x)) +#elif defined(_EVAL_DEBUG_ONLY_STMTS) +#define debug_assert(x) IGNORE(x) +#else +#define debug_assert(x) _no_op +#endif + +#define assert_eq(x,y) assert((x) == (y)) +#define assert_ne(x,y) assert((x) != (y)) + +#define debug_assert_eq(x,y) debug_assert((x) == (y)) +#define debug_assert_ne(x,y) debug_assert((x) != (y)) + +#ifdef DEBUG +#define debug_static_assert(x, msg) _Static_assert((x), msg) +#else +#define debug_static_assert(x, msg) _Static_assert(1, msg) +#endif + +// Version macros + +#define _UI(x) ((uint32_t)(x)) + +#define VER_COMP_MAJ 24u +#define VER_COMP_MIN 16u +#define VER_COMP_BF 8u +#define VER_COMP_REV 0u + +/// Create a `uint32_t` from these version componants +#define VERSION(maj,min,bf,rev) _UI( _UI(maj) << 24u | _UI(min) << 16u | _UI(bf) << 8u | _UI(rev) ) + +/// Mask `ver` to retain only the version component specified by `mask` +/// +/// # Example +/// `_Static_assert( (VERSION_COMP(VERSION(1,2,3,4), VER_COMP_MIN) >> VER_COMP_MIN) == 2u)` +#define VERSION_COMP(ver, mask) _UI( _UI(ver) & (0xffu << _UI(mask)) /*>> _UI(mask)*/ ) + +#define VERSION_MAJ(ver) _UI(VERSION_COMP((ver), 24u) >> 24u) +#define VERSINO_MIN(ver) _UI(VERSION_COMP((ver), 16u) >> 16u) +#define VERSINO_BF(ver) _UI(VERSION_COMP((ver), 8u) >> 8u) +#define VERSINO_REV(ver) _UI(_UI(ver) & 0xffu) +_Static_assert( (VERSION_COMP(VERSION(1,2,3,4), VER_COMP_MIN) >> VER_COMP_MIN) == 2u, "invalid native version spec"); +#undef _UI + +#endif /* _MACROS_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9ee15a3 --- /dev/null +++ b/src/main.c @@ -0,0 +1,16 @@ +// *naka* - find a file within another file +#include +#include + +#include +#include + +int main(int argc, char** argv) +{ + IGNORE(argc); + IGNORE(argv); + + INFO("Hello world!"); + return 0; +} +