From 42d36ea3fb50f5a527bd847ecbde14765426133b Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 15 Feb 2024 20:24:56 +0200 Subject: [PATCH] add assembly loops tests --- Makefile | 11 +++-- src/main.c | 3 ++ src/main_write_bytes_asm.c | 2 +- src/main_write_loop.c | 58 ++++++++++++++++++++++++ src/{nop_loop.asm => multi_nop_loop.asm} | 0 src/{nop_loop.h => multi_nop_loop.h} | 0 src/write_loops.asm | 43 ++++++++++++++++++ src/write_loops.h | 6 +++ 8 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 src/main_write_loop.c rename src/{nop_loop.asm => multi_nop_loop.asm} (100%) rename src/{nop_loop.h => multi_nop_loop.h} (100%) create mode 100644 src/write_loops.asm create mode 100644 src/write_loops.h diff --git a/Makefile b/Makefile index bb8bf9b..6694c52 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,14 @@ CFLAGS=-lm -g -Wall -O1 -build/main: src/main.c src/repetition_tester.c build/nop_loop.o src/rprof.h +build/main: src/main.c src/repetition_tester.c build/multi_nop_loop.o build/write_loops.o src/rprof.h mkdir -p build - gcc -o build/main src/main.c build/nop_loop.o $(CFLAGS) + gcc -o build/main src/main.c build/multi_nop_loop.o build/write_loops.o $(CFLAGS) -build/nop_loop.o: src/nop_loop.asm - nasm -g -f elf64 -o build/nop_loop.o src/nop_loop.asm +build/write_loops.o: src/write_loops.asm + nasm -g -f elf64 -o build/write_loops.o src/write_loops.asm + +build/multi_nop_loop.o: src/multi_nop_loop.asm + nasm -g -f elf64 -o build/multi_nop_loop.o src/multi_nop_loop.asm run: ./build/main ./build/main $(TEST_NAME) diff --git a/src/main.c b/src/main.c index f5b551b..dc1063e 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ #include "main_write_all_bytes.c" #include "main_malloc_read.c" #include "main_write_backward.c" +#include "main_write_loop.c" int main(int argc, char **argv) { if (argc < 2) { @@ -26,6 +27,8 @@ int main(int argc, char **argv) { return main_test_write_bytes_asm(); } else if (!strncmp(test_name, "write_all_bytes", sizeof("write_bytes"))) { return main_test_write_all_bytes(); + } else if (!strncmp(test_name, "write_loop", sizeof("write_loop"))) { + return main_test_write_loop(); } else if (!strncmp(test_name, "write_backward", sizeof("write_backward"))) { return main_test_write_backward(); } else if (!strncmp(test_name, "read_file", sizeof("read_file"))) { diff --git a/src/main_write_bytes_asm.c b/src/main_write_bytes_asm.c index b6aee6c..09a9e44 100644 --- a/src/main_write_bytes_asm.c +++ b/src/main_write_bytes_asm.c @@ -1,5 +1,5 @@ #include "repetition_tester.c" -#include "nop_loop.h" +#include "multi_nop_loop.h" int main_test_write_bytes_asm() { typedef void (*write_bytes_cb)(uint8_t *buffer, uint64_t byte_count); diff --git a/src/main_write_loop.c b/src/main_write_loop.c new file mode 100644 index 0000000..e3d17b8 --- /dev/null +++ b/src/main_write_loop.c @@ -0,0 +1,58 @@ +#include "repetition_tester.c" +#include "write_loops.h" + +int main_test_write_loop() { + struct repetitor repetitor = {}; + repetitor_init(&repetitor); + printf("CPU Frequency: %ldHz (~%.2fGHz)\n", repetitor.cpu_freq, (float)repetitor.cpu_freq/(1000*1000*1000)); + + uint64_t byte_count = 4096 * 512; + uint8_t *buffer = malloc(sizeof(uint8_t) * byte_count); + + while (repeat_cycle_times(&repetitor, 3)) { + repetitor_clear(&repetitor); + while (repetitor_repeat(&repetitor, 2)) { + repetitor_start(&repetitor); + repetitor_measure_start(&repetitor); + mov_all_bytes_asm(buffer, byte_count); + repetitor_measure_stop(&repetitor, byte_count); + repetitor_stop(&repetitor); + } + repetitor_print_results_label(&repetitor, "mov_all_bytes"); + + repetitor_clear(&repetitor); + while (repetitor_repeat(&repetitor, 2)) { + repetitor_start(&repetitor); + repetitor_measure_start(&repetitor); + nop_all_bytes_asm(byte_count); + repetitor_measure_stop(&repetitor, byte_count); + repetitor_stop(&repetitor); + } + repetitor_print_results_label(&repetitor, "nop_all_bytes"); + + repetitor_clear(&repetitor); + while (repetitor_repeat(&repetitor, 2)) { + repetitor_start(&repetitor); + repetitor_measure_start(&repetitor); + cmp_all_bytes_asm(byte_count); + repetitor_measure_stop(&repetitor, byte_count); + repetitor_stop(&repetitor); + } + repetitor_print_results_label(&repetitor, "cmp_all_bytes"); + + repetitor_clear(&repetitor); + while (repetitor_repeat(&repetitor, 2)) { + repetitor_start(&repetitor); + repetitor_measure_start(&repetitor); + dec_all_bytes_asm(byte_count); + repetitor_measure_stop(&repetitor, byte_count); + repetitor_stop(&repetitor); + } + repetitor_print_results_label(&repetitor, "dec_all_bytes"); + + } + + return 0; + + return 0; +} diff --git a/src/nop_loop.asm b/src/multi_nop_loop.asm similarity index 100% rename from src/nop_loop.asm rename to src/multi_nop_loop.asm diff --git a/src/nop_loop.h b/src/multi_nop_loop.h similarity index 100% rename from src/nop_loop.h rename to src/multi_nop_loop.h diff --git a/src/write_loops.asm b/src/write_loops.asm new file mode 100644 index 0000000..e5448c3 --- /dev/null +++ b/src/write_loops.asm @@ -0,0 +1,43 @@ +global mov_all_bytes_asm +global nop_all_bytes_asm +global cmp_all_bytes_asm +global dec_all_bytes_asm + +section .text + +; rsi - byte_count +; rdi - buffer +mov_all_bytes_asm: + xor rax, rax +.loop: + mov [rdi + rax], al + inc rax + cmp rax, rsi + jb .loop + ret + +; rdi - byte_count +nop_all_bytes_asm: + xor rax, rax +.loop: + db 0x0f, 0x1f, 0x00 + inc rax + cmp rax, rdi + jb .loop + ret + +; rdi - byte_count +cmp_all_bytes_asm: + xor rax, rax +.loop: + inc rax + cmp rax, rdi + jb .loop + ret + +; rdi - byte_count +dec_all_bytes_asm: +.loop: + dec rdi + jnz .loop + ret diff --git a/src/write_loops.h b/src/write_loops.h new file mode 100644 index 0000000..8f1a133 --- /dev/null +++ b/src/write_loops.h @@ -0,0 +1,6 @@ +#include + +void mov_all_bytes_asm(uint8_t *buffer, uint64_t byte_count); +void nop_all_bytes_asm(uint64_t byte_count); +void cmp_all_bytes_asm(uint64_t byte_count); +void dec_all_bytes_asm(uint64_t byte_count);