From 66df1b2e0a293a01b63fe5df6c29cfc84e739766 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 1 Dec 2021 14:43:17 +0000 Subject: [PATCH] day1: part2 complete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed function call in part2 hot loop. This enabled full parallelisation for part 1 and 2 Fortune for aoc2021's current commit: Small blessing − 小吉 --- day1/Makefile | 2 +- day1/include/input.h | 5 ----- day1/include/pairs.h | 24 ++++++++++++++++++++++++ day1/src/p1.c | 17 +++++++++++++---- day1/src/p2.c | 23 +++++++++++++++++++++-- 5 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 day1/include/pairs.h diff --git a/day1/Makefile b/day1/Makefile index 39f2ae3..42f564d 100644 --- a/day1/Makefile +++ b/day1/Makefile @@ -15,7 +15,7 @@ INPUT_DEST=src/input.c COMMON_FLAGS+= -W -Wall -fno-strict-aliasing $(addprefix -I,$(INCLUDE)) COMMON_FLAGS+=-msse -msse2 -msse3 -COMMON_FLAGS+=-D_PART1 +COMMON_FLAGS+=-D_PART1 -D_PART2 OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ diff --git a/day1/include/input.h b/day1/include/input.h index 7d203f7..938b75c 100644 --- a/day1/include/input.h +++ b/day1/include/input.h @@ -13,11 +13,6 @@ typedef uint16_t input_t; extern const size_t INPUT_SIZE; extern const input_t INPUT[]; -struct ipair { - input_t prev; - input_t next; -}; - #ifdef __cplusplus } #endif diff --git a/day1/include/pairs.h b/day1/include/pairs.h new file mode 100644 index 0000000..6ca3df4 --- /dev/null +++ b/day1/include/pairs.h @@ -0,0 +1,24 @@ +#ifndef _PAIRS_H +#define _PAIRS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct ipair { + input_t prev; + input_t next; +}; + +#ifdef __cplusplus +uint64_t chk_pair(const ipair* __restrict__ pair) __attribute__((pure)); +uint64_t sum_pairs(uint64_t length, const input_t input[const __restrict__ length]) __attribute__((pure)); +} +#else +uint64_t chk_pair(const struct ipair* restrict pair) __attribute__((pure)); +uint64_t sum_pairs(uint64_t length, const input_t input[const restrict length]) __attribute__((pure)); +#endif + +#endif /* _PAIRS_H */ diff --git a/day1/src/p1.c b/day1/src/p1.c index 0914b19..284761f 100644 --- a/day1/src/p1.c +++ b/day1/src/p1.c @@ -1,18 +1,27 @@ + #include +#include + __attribute__((pure)) uint64_t chk_pair(const struct ipair* restrict pair) { return pair->prev < pair->next; } -int part1(uint64_t* restrict _res) +__attribute__((pure)) +uint64_t sum_pairs(uint64_t length, const input_t input[const restrict length]) { uint64_t res = 0; - for(size_t i=0;i -#include +#include + +struct window { + input_t pre, cur, suc; +}; + +__attribute__((pure)) +inline static input_t sum_window(const struct window* restrict w) +{ + return w->pre + w->cur + w->suc; +} int part2(uint64_t* restrict _res) { uint64_t res=0; - TODO("unimplemented"); + + for(size_t i=0;i