day6: added attrs

master
Avril 4 years ago
parent f456c90a9b
commit 3a32d0929e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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

@ -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 */

@ -0,0 +1,24 @@
#ifndef _PANIC_H
#define _PANIC_H
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#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 */

@ -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 $@

@ -8,6 +8,9 @@
#define DEBUG
#endif
#include <attrs.h>
#include <panic.h>
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<NUM_QUESTIONS;i++)

Loading…
Cancel
Save