From f0207a75e85cbeac1d5c03a5aa353fb293c30466 Mon Sep 17 00:00:00 2001 From: Nikola Date: Tue, 1 Sep 2026 18:46:07 +0300 Subject: [PATCH] Moved converge to its own file to reduce REDUNDANCY --- Implementierung/Makefile | 5 ++++- Implementierung/converge_helper.h | 24 ++++++++++++++++++++++++ Implementierung/tricorn.c | 25 +------------------------ Implementierung/tricorn_V1.c | 24 +----------------------- Implementierung/tricorn_V2.c | 24 +----------------------- Implementierung/tricorn_V3.c | 23 ++--------------------- 6 files changed, 33 insertions(+), 92 deletions(-) create mode 100644 Implementierung/converge_helper.h diff --git a/Implementierung/Makefile b/Implementierung/Makefile index 2acc2e0..67fdf72 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -8,10 +8,13 @@ SRCS = $(wildcard *.c) OBJS = $(SRCS:.c=.o) DEPS = $(SRCS:.c=.d) -.PHONY: all clean +.PHONY: all clean run all: $(TARGET) +run: all + ./project -s -2.0,-1.5 -d 1200,900 -r 0.003 -n 256 -o tricorn.bmp -C + $(TARGET): $(OBJS) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) diff --git a/Implementierung/converge_helper.h b/Implementierung/converge_helper.h new file mode 100644 index 0000000..80e8a0d --- /dev/null +++ b/Implementierung/converge_helper.h @@ -0,0 +1,24 @@ + +#ifndef CONVERGE_HELPER_H +#define CONVERGE_HELPER_H + +static inline unsigned converge(float c_real, float c_imag, unsigned n) { + float real = 0.0f; + float imag = 0.0f; + + for (unsigned i = 0; i < n; i++) { + float real2 = real * real; + float imag2 = imag * imag; + + // If the pixel has diverged calculate the value and return it + if (real2 + imag2 > 4.0f) { + return 255 - 4 * (i % 64); + } + + // Calculate imaginary and real parts separately + imag = -2.0f * real * imag + c_imag; + real = real2 - imag2 + c_real; + } + return 0; +} +#endif \ No newline at end of file diff --git a/Implementierung/tricorn.c b/Implementierung/tricorn.c index dfc25e1..26e8617 100644 --- a/Implementierung/tricorn.c +++ b/Implementierung/tricorn.c @@ -1,8 +1,6 @@ #include "tricorn.h" - #include "color_table.h" -static unsigned converge(float c_real, float c_imag, unsigned n); - +#include "converge_helper.h" /** * @brief Generates a Tricorn fractal image using an optimized SIMD algorithm @@ -131,25 +129,4 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig c_imag += vertical_step; c_imag_vector = _mm_add_ps(c_imag_vector,vertical_step_vector); } -} - -// Iterative application of the equation given by the assignment -static unsigned converge(float c_real, float c_imag, unsigned n) { - float real = 0.0f; - float imag = 0.0f; - - for (unsigned i = 0; i < n; i++) { - float real2 = real * real; - float imag2 = imag * imag; - - // If the pixel has diverged calculate the value and return it - if (real2 + imag2 > 4.0f) { - return 255 - 4 * (i % 64); - } - - // Calculate imaginary and real parts separately - imag = -2.0f * real * imag + c_imag; - real = real2 - imag2 + c_real; - } - return 0; } \ No newline at end of file diff --git a/Implementierung/tricorn_V1.c b/Implementierung/tricorn_V1.c index 3787dbc..b8f8f46 100644 --- a/Implementierung/tricorn_V1.c +++ b/Implementierung/tricorn_V1.c @@ -1,7 +1,6 @@ #include "tricorn_V1.h" #include "color_table.h" - -static unsigned converge(float c_real, float c_imag, unsigned n); +#include "converge_helper.h" /** @@ -52,24 +51,3 @@ void tricorn_V1(float complex start, size_t width, ssize_t height, float res, un c_imag += vertical_step; // For reach row calculate the next value for the imaginary part by accumulation } } - -// Iterative application of the equation given by the assignment -static inline unsigned converge(float c_real, float c_imag, unsigned n) { - float real = 0.0f; - float imag = 0.0f; - - for (unsigned i = 0; i < n; i++) { - float real2 = real * real; - float imag2 = imag * imag; - - // If the pixel has diverged calculate the value and return it - if (real2 + imag2 > 4.0f) { - return 255 - 4 * (i % 64); - } - - // Calculate imaginary and real parts separately - imag = -2.0f * real * imag + c_imag; - real = real2 - imag2 + c_real; - } - return 0; -} \ No newline at end of file diff --git a/Implementierung/tricorn_V2.c b/Implementierung/tricorn_V2.c index 3754eb4..a954cbc 100644 --- a/Implementierung/tricorn_V2.c +++ b/Implementierung/tricorn_V2.c @@ -1,8 +1,7 @@ #include "tricorn_V2.h" #include #include "color_table.h" - -static unsigned converge(float c_rea, float c_imag, unsigned n); +#include "converge_helper.h" /** @@ -128,25 +127,4 @@ void tricorn_V2(float complex start, size_t width, ssize_t height, float res, un } } -} - -// Iterative application of the equation given by the assignment -static unsigned converge(float c_real, float c_imag, unsigned n) { - float real = 0.0f; - float imag = 0.0f; - - for (unsigned i = 0; i < n; i++) { - float real2 = real * real; - float imag2 = imag * imag; - - // If the pixel has diverged calculate the value and return it - if (real2 + imag2 > 4.0f) { - return 255 - 4 * (i % 64); - } - - // Calculate imaginary and real parts separately - imag = -2.0f * real * imag + c_imag; - real = real2 - imag2 + c_real; - } - return 0; } \ No newline at end of file diff --git a/Implementierung/tricorn_V3.c b/Implementierung/tricorn_V3.c index 3853701..7ba3484 100644 --- a/Implementierung/tricorn_V3.c +++ b/Implementierung/tricorn_V3.c @@ -1,8 +1,8 @@ #include "tricorn_V3.h" #include "color_table.h" +#include "converge_helper.h" static void* compute(void * args); -static unsigned converge(float c_real, float c_imag, unsigned n); static size_t get_thread_count(); // Argument structs passed to the threads for the caluclations @@ -38,7 +38,7 @@ typedef struct { */ void tricorn_V3(float complex start, size_t width, ssize_t height, float res, unsigned n, _Bool color, unsigned char* img){ - const ssize_t min_rows_per_thread = 10; + const size_t min_rows_per_thread = 10; const _Bool flipped = height < 0; const size_t abs_height = flipped ? -height : height; @@ -203,26 +203,7 @@ static void* compute(void * vargs){ return NULL; } -// Iterative application of the equation given by the assignment -static unsigned converge(float c_real, float c_imag, unsigned n) { - float real = 0.0f; - float imag = 0.0f; - for (unsigned i = 0; i < n; i++) { - float real2 = real * real; - float imag2 = imag * imag; - - // If the pixel has diverged calculate the value and return it - if (real2 + imag2 > 4.0f) { - return 255 - 4 * (i % 64); - } - - // Calculate imaginary and real parts separately - imag = -2.0f * real * imag + c_imag; - real = real2 - imag2 + c_real; - } - return 0; -} static inline size_t get_thread_count(){ #ifdef WIN32 SYSTEM_INFO sysinfo;