1
0

add assembly loops tests

This commit is contained in:
Rokas Puzonas 2024-02-15 20:24:56 +02:00
parent ba3d3cabcb
commit 42d36ea3fb
8 changed files with 118 additions and 5 deletions

View File

@ -1,11 +1,14 @@
CFLAGS=-lm -g -Wall -O1 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 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 build/write_loops.o: src/write_loops.asm
nasm -g -f elf64 -o build/nop_loop.o src/nop_loop.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 run: ./build/main
./build/main $(TEST_NAME) ./build/main $(TEST_NAME)

View File

@ -13,6 +13,7 @@
#include "main_write_all_bytes.c" #include "main_write_all_bytes.c"
#include "main_malloc_read.c" #include "main_malloc_read.c"
#include "main_write_backward.c" #include "main_write_backward.c"
#include "main_write_loop.c"
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
@ -26,6 +27,8 @@ int main(int argc, char **argv) {
return main_test_write_bytes_asm(); return main_test_write_bytes_asm();
} else if (!strncmp(test_name, "write_all_bytes", sizeof("write_bytes"))) { } else if (!strncmp(test_name, "write_all_bytes", sizeof("write_bytes"))) {
return main_test_write_all_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"))) { } else if (!strncmp(test_name, "write_backward", sizeof("write_backward"))) {
return main_test_write_backward(); return main_test_write_backward();
} else if (!strncmp(test_name, "read_file", sizeof("read_file"))) { } else if (!strncmp(test_name, "read_file", sizeof("read_file"))) {

View File

@ -1,5 +1,5 @@
#include "repetition_tester.c" #include "repetition_tester.c"
#include "nop_loop.h" #include "multi_nop_loop.h"
int main_test_write_bytes_asm() { int main_test_write_bytes_asm() {
typedef void (*write_bytes_cb)(uint8_t *buffer, uint64_t byte_count); typedef void (*write_bytes_cb)(uint8_t *buffer, uint64_t byte_count);

58
src/main_write_loop.c Normal file
View File

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

43
src/write_loops.asm Normal file
View File

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

6
src/write_loops.h Normal file
View File

@ -0,0 +1,6 @@
#include <stdint.h>
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);