Moved converge to its own file to reduce REDUNDANCY

This commit is contained in:
Nikola
2026-09-01 18:46:07 +03:00
parent ccf306e288
commit f0207a75e8
6 changed files with 33 additions and 92 deletions
+4 -1
View File
@@ -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)
+24
View File
@@ -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
+1 -24
View File
@@ -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;
}
+1 -23
View File
@@ -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;
}
+1 -23
View File
@@ -1,8 +1,7 @@
#include "tricorn_V2.h"
#include <immintrin.h>
#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;
}
+2 -21
View File
@@ -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;