From 89ab7b172bfda17ddfd3395b9c7a157b32a52609 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sat, 26 Dec 2020 23:08:46 +0100 Subject: Add partial assembly sollution for day 10 --- 10/part2.c | 8 ++++++-- 10/possible_seq.asm | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 10/possible_seq.asm diff --git a/10/part2.c b/10/part2.c index ffe0f55..5e766d4 100644 --- a/10/part2.c +++ b/10/part2.c @@ -33,7 +33,10 @@ void insert_value_sorted(int *list, size_t *size, int value) list[low] = value; } -uint64_t pos_seq(const int *input, size_t input_size) +#ifdef USE_ASM +uint64_t possible_seq(const int *input, size_t input_size); +#else +uint64_t possible_seq(const int *input, size_t input_size) { // Use char to optimize for size const static char TRIB[] = { 1, 1, 2, 4, 7 }; @@ -53,6 +56,7 @@ uint64_t pos_seq(const int *input, size_t input_size) return res; } +#endif uint64_t solve(const char *filename) { @@ -74,7 +78,7 @@ uint64_t solve(const char *filename) fclose(file); - return pos_seq(input, input_size); + return possible_seq(input, input_size); } int main(int argc, char *argv[]) diff --git a/10/possible_seq.asm b/10/possible_seq.asm new file mode 100644 index 0000000..15c6586 --- /dev/null +++ b/10/possible_seq.asm @@ -0,0 +1,30 @@ +global possible_seq + +section .data + TRIB: dq 1, 1, 2, 4, 7 ; tribonacci sequence (without first 2 zeroes) + +section .text + +possible_seq: + mov eax, 1 ; set up return value + xor ecx, ecx ; # of connected elements counter + lea rdx, [rdi + 4 * rsi - 4] ; pointer to the last element + lea r8, [rel TRIB] + jmp .loop +.ncon: + imul rax, qword [r8 + 8 * rcx] + xor ecx, ecx + add rdi, 4 + cmp rdi, rdx + jae .return +.loop: + mov esi, dword [rdi + 4] + sub esi, dword [rdi] + cmp esi, 1 + jne .ncon + inc ecx + add rdi, 4 + cmp rdi, rdx + jb .loop +.return: + ret -- cgit v1.2.3