From f71bfa68f6bb92fd9ba6a0e997dbf713658037e5 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Fri, 19 Dec 2025 16:05:45 +0100 Subject: [PATCH] Added extra runner on Makefile --- Makefile | 11 +++++- example/alignment.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 example/alignment.c diff --git a/Makefile b/Makefile index 08b0893..abac185 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ $(OUT): $(SRC) $(LD_SCRIPT) clean: rm -f $(OUT) kernel.map -run: $(OUT) +run-container: $(OUT) distrobox enter archlinux -- qemu-system-riscv32 \ -machine virt \ -bios default \ @@ -24,3 +24,12 @@ run: $(OUT) -serial mon:stdio \ -no-reboot \ -kernel $(OUT) + +run: $(OUT) + qemu-system-riscv32 \ + -machine virt \ + -bios default \ + -nographic \ + -serial mon:stdio \ + -no-reboot \ + -kernel $(OUT) diff --git a/example/alignment.c b/example/alignment.c new file mode 100644 index 0000000..84ba251 --- /dev/null +++ b/example/alignment.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +// Define the number of iterations for the benchmark +#define ARRAY_SIZE 50000000 // 50 Million random data points + +size_t align_up(size_t value, size_t align) +{ + size_t remainder = value % align; + if (remainder == 0) + return value; + return value + (align - remainder); +} + +size_t align_arithmetic(size_t value, size_t alignment) +{ + return ((value + alignment - 1) / alignment) * alignment; +} + +size_t align_bitwise(size_t value, size_t alignment) +{ + // TODO: assumendo che alignment รจ una potenza di 2 + return (value + alignment - 1) & ~(alignment - 1); +} + +void benchmark(const char *name, + size_t (*func)(size_t, size_t), + size_t alignment, + const size_t *data_array, + long long num_elements) +{ + clock_t start, end; + double cpu_time_used; + size_t result_checksum = 0; + + start = clock(); + for (long long i = 0; i < num_elements; i++) { + result_checksum += func(data_array[i], alignment); + } + end = clock(); + cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; + printf(" %-30s: %.4f seconds (Checksum: %zu)\n", name, cpu_time_used, result_checksum); +} + +int main(void) +{ + size_t alignment_pot = 16; + size_t alignment_non_pot = 7; + + printf("Generating %lld random input values...\n", (long long)ARRAY_SIZE); + srand((unsigned int)time(NULL)); + size_t *random_data = (size_t *)malloc(ARRAY_SIZE * sizeof(size_t)); + if (random_data == NULL) { + fprintf(stderr, "Memory allocation failed!\n"); + return 1; + } + for (long long i = 0; i < ARRAY_SIZE; i++) { + // pseudo-random values up to 1MB + random_data[i] = rand() % (1024 * 1204); + } + printf("Data generation complete. \n\n"); + printf("--- Alignment Function Stress Benchmark (%lld Random Inputs) --\n\n", (long long)ARRAY_SIZE); + + // ================================== SCENARIO 1: Power-of-Two Alignment ====================== + printf("Scenario 1: Alignment = %zu (Power of Two, Stress Test)\n", alignment_pot); + + benchmark("1. Bitwise (PoT Specialized)", align_bitwise, alignment_pot, random_data, ARRAY_SIZE); + benchmark("2. Arithmetic (General)", align_arithmetic, alignment_pot, random_data, ARRAY_SIZE); + benchmark("3. Modulo (General)", align_up, alignment_pot, random_data, ARRAY_SIZE); + +// ================================== SCENARIO 2: Non-Power-of-Two Alignment ====================== + printf("Scenario 2: Alignment = %zu (Non-Power of Two, Stress Test)\n", alignment_non_pot); + + //benchmark("1. Bitwise (PoT Specialized)", align_bitwise, alignment_pot, random_data, ARRAY_SIZE); + benchmark("2. Arithmetic (General)", align_arithmetic, alignment_non_pot, random_data, ARRAY_SIZE); + benchmark("3. Modulo (General)", align_up, alignment_non_pot, random_data, ARRAY_SIZE); + + free(random_data); + return 0; +}