parent
95c08c59e7
commit
04cc8891a1
@ -0,0 +1,99 @@
|
||||
# aoc2021-day2
|
||||
|
||||
PROJECT=day2
|
||||
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
|
||||
|
||||
SRC_C = $(wildcard src/*.c)
|
||||
SRC_CXX = $(wildcard src/*.cpp)
|
||||
|
||||
INCLUDE=include
|
||||
|
||||
INPUT_SOURCE?=input
|
||||
INPUT_DEST=include/raw_input.hh
|
||||
|
||||
COMMON_FLAGS+= -W -Wall -fno-strict-aliasing $(addprefix -I,$(INCLUDE))
|
||||
|
||||
COMMON_FLAGS+=-msse -msse2 -msse3
|
||||
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 \
|
||||
-fno-stack-check
|
||||
|
||||
CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors
|
||||
|
||||
CFLAGS += $(COMMON_FLAGS) --std=gnu17
|
||||
CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20
|
||||
LDFLAGS += -lfmt
|
||||
|
||||
STRIP=strip
|
||||
|
||||
RELEASE_COMMON_FLAGS+= -Werror
|
||||
DEBUG_COMMON_FLAGS+= -fanalyzer
|
||||
|
||||
ifneq ($(TARGET_SPEC_FLAGS),no)
|
||||
RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS)
|
||||
RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS)
|
||||
RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto
|
||||
|
||||
DEBUG_CFLAGS?= -Og -g
|
||||
DEBUG_CXXFLAGS?=-Og -g
|
||||
DEBUG_LDFLAGS?=
|
||||
endif
|
||||
|
||||
DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS)
|
||||
DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS)
|
||||
|
||||
RELEASE_CFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS)
|
||||
RELEASE_CXXFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS)
|
||||
|
||||
# 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 $(INPUT_DEST)
|
||||
$(MAKE) $(PROJECT)-release
|
||||
|
||||
.PHONY: debug
|
||||
debug: | dirs $(INPUT_DEST)
|
||||
$(MAKE) $(PROJECT)-debug
|
||||
|
||||
# Targets
|
||||
|
||||
dirs:
|
||||
@mkdir -p obj/c{,xx}/src
|
||||
|
||||
$(INPUT_DEST): $(INPUT_SOURCE)
|
||||
./mkinput $< > $@
|
||||
|
||||
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}
|
||||
rm -f $(INPUT_DEST)
|
||||
|
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
namespace input {
|
||||
constexpr const auto COLS = 6;
|
||||
constexpr const char DATA[][COLS] = {
|
||||
"00100",
|
||||
"11110",
|
||||
"10110",
|
||||
"10111",
|
||||
"10101",
|
||||
"01111",
|
||||
"00111",
|
||||
"11100",
|
||||
"10000",
|
||||
};
|
||||
constexpr const auto ROWS = sizeof(DATA)/sizeof(DATA[0]);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,9 @@
|
||||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
echo "#pragma once"
|
||||
echo ""
|
||||
|
||||
INPUT=${1:-input}
|
||||
|
||||
COLS=$(head -n 1 "$INPUT" | wc -c)
|
||||
echo "namespace input {"
|
||||
echo " constexpr const auto COLS = $COLS;"
|
||||
echo " constexpr const char DATA[][COLS] = {"
|
||||
while IFS= read -r line; do
|
||||
echo " \"$line\","
|
||||
done < $INPUT
|
||||
unset line
|
||||
echo " };"
|
||||
|
||||
echo " constexpr const auto ROWS = sizeof(DATA)/sizeof(DATA[0]);"
|
||||
echo "}"
|
@ -0,0 +1,93 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <climits>
|
||||
|
||||
#include <raw_input.hh>
|
||||
|
||||
typedef uint8_t ipack_t;
|
||||
|
||||
struct input_info {
|
||||
const static constexpr inline auto width = input::COLS;
|
||||
const static constexpr inline auto height = input::ROWS;
|
||||
bool data[width][height];
|
||||
|
||||
constexpr bool operator()(size_t col, size_t row) const
|
||||
{
|
||||
return data[col][row];
|
||||
}
|
||||
};
|
||||
|
||||
struct input_packed {
|
||||
const static constexpr inline auto width = input::COLS;
|
||||
const static constexpr inline auto height = input::ROWS / sizeof(ipack_t);
|
||||
ipack_t data[width][height];
|
||||
|
||||
constexpr ipack_t operator()(size_t w, size_t h) const
|
||||
{
|
||||
return data[w][h];
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(input::ROWS % sizeof(ipack_t)==0, "Cannot pack rows into ipack_t losslessly");
|
||||
|
||||
|
||||
|
||||
consteval input_info gen_input_bits()
|
||||
{
|
||||
input_info o;
|
||||
for(size_t i=0;i<input::ROWS;i++)
|
||||
for(size_t j=0;j<input::COLS;j++)
|
||||
{
|
||||
o.data[j][i] = input::DATA[i][j] == '0' ? false : true;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr void set_bit(T& data, size_t bit, bool value = true)
|
||||
{
|
||||
//constexpr const auto BITS = CHAR_BIT * sizeof(T);
|
||||
T mask = T(1) << bit;
|
||||
|
||||
if(!value && (data & mask)) // bit is set, unset it
|
||||
data ^= mask;
|
||||
else if(value) data |= mask; // set the bit
|
||||
}
|
||||
|
||||
consteval input_packed pack_input(const input_info& in)
|
||||
{
|
||||
input_packed o;
|
||||
//size_t k=0;
|
||||
constexpr const auto PACKED_BITS = CHAR_BIT * sizeof(ipack_t);
|
||||
for(size_t j=0;j<input::COLS;j++)
|
||||
for(size_t i=0;i<input::ROWS;i+=PACKED_BITS)
|
||||
{
|
||||
ipack_t bit = 0;
|
||||
size_t nbits = PACKED_BITS;
|
||||
|
||||
while(nbits --> 0)
|
||||
{
|
||||
// XXX: This sets the bits in reverse order, but that doesn't matter for this exersize
|
||||
if(in(j, i)) set_bit(bit, nbits);
|
||||
}
|
||||
o.data[j][i / sizeof(ipack_t)] = bit;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
consteval input_packed gen_input_packed()
|
||||
{
|
||||
return pack_input(gen_input_bits());
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
auto p = gen_input_packed();
|
||||
for(int i=0;i<input_packed::width;i++) {
|
||||
for(int j=0;j<input_packed::height;j++)
|
||||
fmt::print("{:8b}", p(i, j));
|
||||
fmt::print("\n");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue