diff --git a/Makefile b/Makefile index f2852bf..dce53f3 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ C_OPT_FLAGS?= CXX_OPT_FLAGS?= -felide-constructors LD_OPT_FLAGS?=-O3 -flto +INCLUDE=$(shell pwd)/common/include + COMMON_FLAGS=-Wall -pedantic $(COMMON_OPT_FLAGS) CFLAGS+=$(COMMON_FLAGS) --std=gnu11 $(C_OPT_FLAGS) @@ -15,7 +17,7 @@ LDFLAGS+=$(LD_OPT_FLAGS) DAYS= $(wildcard day*) -ENV= CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" +ENV= CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" INCLUDE="$(INCLUDE)" .PHONY: all diff --git a/common/include/attrs.h b/common/include/attrs.h new file mode 100644 index 0000000..f1bcda7 --- /dev/null +++ b/common/include/attrs.h @@ -0,0 +1,19 @@ +#ifndef _ATTRS_H +#define _ATTRS_H + +#define pure __attribute__((pure)) +#define noglobal __attribute__((const)) +#define noinline __attribute__((noinline)) +#define cold __attribute__((cold)) + +#ifndef DEBUG +#define _force_inline __attribute__((gnu_inline)) inline extern +#else +#define _force_inline __attribute__((gnu_inline)) inline static +#endif + +#ifndef __cplusplus // sepples has [[noreturn]] +#define noreturn __attribute__((noreturn)) void +#endif + +#endif /* _ATTRS_H */ diff --git a/common/include/panic.h b/common/include/panic.h new file mode 100644 index 0000000..968ab1d --- /dev/null +++ b/common/include/panic.h @@ -0,0 +1,24 @@ +#ifndef _PANIC_H +#define _PANIC_H + +#include +#include +#include + +#include "attrs.h" + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wall" +noinline cold static noreturn panic(const char* msg, ...) +{ + va_list va; + va_start(va, msg); + fputs("Fatal error: ", stderr); + vfprintf(stderr, msg, va); + va_end(va); + fputs("\n", stderr); + abort(); +} +#pragma GCC diagnostic pop + +#endif /* _PANIC_H */ diff --git a/day6/Makefile b/day6/Makefile index 7eb034d..65b8d9c 100644 --- a/day6/Makefile +++ b/day6/Makefile @@ -7,10 +7,12 @@ OPT_FLAGS?= -O3 -march=native -pipe -flto \ CFLAGS?=$(OPT_FLAGS) LDFLAGS?=-O3 -flto -CFLAGS+= -Wall -pedantic --std=gnu11 +INCLUDE?=../common/include + +CFLAGS+= -Wall -Wextra -Wstrict-aliasing -pedantic --std=gnu11 -I$(INCLUDE) .PHONY: all -all: part1 +all: part1 part2 inpu%.h: inpu% @rm -f $@ diff --git a/day6/day6.c b/day6/day6.c index 1173274..a6701a9 100644 --- a/day6/day6.c +++ b/day6/day6.c @@ -8,6 +8,9 @@ #define DEBUG #endif +#include +#include + static const char* const answers[] = { #ifdef TEST #include "input-test.h" @@ -31,12 +34,11 @@ typedef struct answer table[NUM_QUESTIONS]; } answers_t; -inline static char assert_in_bound(char i) +noglobal inline static char assert_in_bound(char i) { register int x=(int)i; if(x<0 || x>=NUM_QUESTIONS) { - fprintf(stderr, "Fatal error: char '%c' (%d) is not in range 0..%d\n", i, x, NUM_QUESTIONS); - abort(); + panic("char '%c' (%d) is not in range 0..%d", i, x, NUM_QUESTIONS); } return i; } @@ -53,7 +55,7 @@ static void populate(const char* from, answers_t * restrict ans) //wtf is this s 1; } -static size_t count_ans(const answers_t* restrict ans) +pure static size_t count_ans(const answers_t* restrict ans) { register size_t j=0; for(register size_t i=0;i