From 748d0521bc82950b1d5c450615ab35d17f4a0e3d Mon Sep 17 00:00:00 2001 From: Nikola Date: Sun, 19 Jul 2026 19:06:27 +0200 Subject: [PATCH] Added more comments --- Implementierung/tricorn.c | 28 +++++++++++++++++++++++----- Implementierung/tricorn_V2.c | 32 ++++++++++++++++++++++++++------ Implementierung/tricorn_V3.c | 31 ++++++++++++++++++++++++------- 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/Implementierung/tricorn.c b/Implementierung/tricorn.c index c684b6b..fa5975e 100644 --- a/Implementierung/tricorn.c +++ b/Implementierung/tricorn.c @@ -32,7 +32,7 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig __m128 minus_two = _mm_set1_ps(-2.0f); float c_imag = c_start_imag; - __m128 c_imag_vector = _mm_set1_ps(c_imag); + __m128 c_imag_vector = _mm_set1_ps(c_imag); // Initialize imagiinary vector which will be acumulated float four_res = 4 * res; @@ -41,26 +41,33 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig for(ssize_t y = 0; y < actual_height;y++) { size_t x = 0; + // Initialize real vector which will be accumulated __m128 c_real_vector = _mm_setr_ps(c_start_real,c_start_real + 1.0f*res,c_start_real + 2.0f*res, c_start_real + 3.0f*res); for(; x < simd_width;x += 4) { //SIMD Loop + // Initialize start values for the calculation __m128 real = _mm_setzero_ps(); __m128 imag = _mm_setzero_ps(); __m128i iterations = _mm_setzero_si128(); - __m128i active_mask = _mm_set1_epi32(-1); + __m128i active_mask = _mm_set1_epi32(-1); // Active mask will track which pixels have digerved - 0 for diverged, 1 - for contained, at start 0xFFFF for(unsigned i = 0; i < n;i++) { + // Calculate squared values of real and imaginary __m128 real2 = _mm_mul_ps(real,real); __m128 imag2 = _mm_mul_ps(imag,imag); + // Detect which have diverged __m128 unbounded = _mm_cmple_ps(_mm_add_ps(real2,imag2),four); __m128i unbounded_i = _mm_castps_si128(unbounded); + // Update the active mask active_mask = _mm_and_si128(active_mask,unbounded_i); + // If all have diverged break if(_mm_test_all_zeros(active_mask,active_mask)) { break; } + // Add the iterations for each pixel only if it hasn't diverged iterations = _mm_sub_epi32(iterations,active_mask); imag = _mm_add_ps(c_imag_vector,_mm_mul_ps(minus_two, _mm_mul_ps(real,imag))); @@ -68,33 +75,43 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig } - __m128i mod64 = _mm_and_si128(iterations, _mm_set1_epi32(63)); - __m128i times4 = _mm_slli_epi32(mod64, 2); - __m128i values = _mm_sub_epi32(_mm_set1_epi32(255), times4); + // Calculate the value for the pixel + // value = 255 - 4 * (i % 64) + __m128i mod64 = _mm_and_si128(iterations, _mm_set1_epi32(63)); // mod is realised with a bitmask + __m128i times4 = _mm_slli_epi32(mod64, 2); // then multiplication by 4 is shift by 2 + __m128i values = _mm_sub_epi32(_mm_set1_epi32(255), times4); // subtraction from 255 + // pixels which have not escaped need to be colored black so value = 0 __m128i max_iter = _mm_set1_epi32(n); __m128i is_max = _mm_cmpeq_epi32(iterations, max_iter); values = _mm_andnot_si128(is_max, values); + // Convert vector to an array of ints unsigned int value_array[4]; _mm_storeu_si128((__m128i*)value_array,values); + // Write the 4 values in the image buffer for(unsigned i = 0 ; i < 4;i++) { unsigned int value = value_array[i]; size_t position = (y*width + (x+i)) * bytesPerColor; + // If grayscale just take value if(!color) { img[position] = value; } else { + // If colored use the lookup table to get the RGB values img[position] = color_lookup_table[value][0]; img[position + 1] = color_lookup_table[value][1]; img[position + 2] = color_lookup_table[value][2]; } } + // Accumulate the real part c_real_vector = _mm_add_ps(c_real_vector,_mm_set1_ps(four_res)); } + // Follow up loop for the remaining pixels + // Calculates them scalarly for(;x < width; x++) { float c_real = c_start_real + (x*res); unsigned int value = converge(c_real,c_imag,n); @@ -110,6 +127,7 @@ void tricorn(float complex start, size_t width, ssize_t height, float res, unsig img[position + 2] = ((value*5) % 255); } } + // Accumulate the imaginary part c_imag += vertical_step; c_imag_vector = _mm_add_ps(c_imag_vector,vertical_step_vector); } diff --git a/Implementierung/tricorn_V2.c b/Implementierung/tricorn_V2.c index dd76a7c..352e7b3 100644 --- a/Implementierung/tricorn_V2.c +++ b/Implementierung/tricorn_V2.c @@ -28,62 +28,80 @@ void tricorn_V2(float complex start, size_t width, ssize_t height, float res, un float c_start_real = creal(start); float c_start_imag = height > 0 ? cimag(start) : cimag(start) + (actual_height - 1) * res; + + // Prepared vectors for later __m128 four = _mm_set1_ps(4.0f); __m128 minus_two = _mm_set1_ps(-2.0f); for(ssize_t y = 0; y < actual_height;y++) { + // Calculate the imaginary part once per row and create a vector from it float c_imag = c_start_imag + (y * imag_direction) * res; __m128 c_imag_vector = _mm_set1_ps(c_imag); size_t x = 0; for(; x < simd_width;x += 4) { //SIMD Loop + // Calculate real part for the first pixel and then create vector for the group of for pixels float c_real = c_start_real + (x*res); __m128 c_real_vector = _mm_setr_ps(c_real,c_real+1.0f*res,c_real+2.0f*res,c_real+3.0f*res); + + // Initialize start values for the calculation __m128 real = _mm_setzero_ps(); __m128 imag = _mm_setzero_ps(); __m128i iterations = _mm_setzero_si128(); - __m128i active_mask = _mm_set1_epi32(-1); + __m128i active_mask = _mm_set1_epi32(-1); // Active mask will track which pixels have digerved - 0 for diverged, 1 - for contained, at start 0xFFFF for(unsigned i = 0; i < n;i++) { + // Calculate squared values of real and imaginary __m128 real2 = _mm_mul_ps(real,real); __m128 imag2 = _mm_mul_ps(imag,imag); + + // Detect which have diverged __m128 unbounded = _mm_cmple_ps(_mm_add_ps(real2,imag2),four); - __m128i unbounded_i = _mm_castps_si128(unbounded); + __m128i unbounded_i = _mm_castps_si128(unbounded); // Cast to __m128i + // Update the active mask active_mask = _mm_and_si128(active_mask,unbounded_i); + // If all have diverged break if(_mm_test_all_zeros(active_mask,active_mask)) { break; } + // Add the iterations for each pixel only if it hasn't diverged iterations = _mm_sub_epi32(iterations,active_mask); imag = _mm_add_ps(c_imag_vector,_mm_mul_ps(minus_two, _mm_mul_ps(real,imag))); real = _mm_add_ps(_mm_sub_ps(real2,imag2),c_real_vector); } + // Calculate the value for the pixel + // value = 255 - 4 * (i % 64) + __m128i mod64 = _mm_and_si128(iterations, _mm_set1_epi32(63)); // mod is realised with a bitmask + __m128i times4 = _mm_slli_epi32(mod64, 2); // then multiplication by 4 is shift by 2 + __m128i values = _mm_sub_epi32(_mm_set1_epi32(255), times4); // subtraction from 255 - __m128i mod64 = _mm_and_si128(iterations, _mm_set1_epi32(63)); - __m128i times4 = _mm_slli_epi32(mod64, 2); - __m128i values = _mm_sub_epi32(_mm_set1_epi32(255), times4); - + // pixels which have not escaped need to be colored black so value = 0 __m128i max_iter = _mm_set1_epi32(n); __m128i is_max = _mm_cmpeq_epi32(iterations, max_iter); values = _mm_andnot_si128(is_max, values); + // Convert vector to an array of ints unsigned int value_array[4]; _mm_storeu_si128((__m128i*)value_array,values); + // Write the 4 values in the image buffer for(unsigned i = 0 ; i < 4;i++) { unsigned int value = value_array[i]; size_t position = (y*width + (x+i)) * bytesPerColor; + // If grayscale just take value if(!color) { img[position] = value; } else { + // If colored use the lookup table to get the RGB values img[position] = color_lookup_table[value][0]; img[position + 1] = color_lookup_table[value][1]; img[position + 2] = color_lookup_table[value][2]; @@ -91,6 +109,8 @@ void tricorn_V2(float complex start, size_t width, ssize_t height, float res, un } } + // Follow up loop for the remaining pixels + // Calculates them scalarly for(;x < width; x++) { float c_real = c_start_real + (x*res); unsigned int value = converge(c_real,c_imag,n); diff --git a/Implementierung/tricorn_V3.c b/Implementierung/tricorn_V3.c index 6c36ceb..8c52fa5 100644 --- a/Implementierung/tricorn_V3.c +++ b/Implementierung/tricorn_V3.c @@ -101,7 +101,7 @@ static void* compute(void * vargs){ __m128 minus_two = _mm_set1_ps(-2.0f); float c_imag = args->start_i; - __m128 c_imag_vector = _mm_set1_ps(c_imag); + __m128 c_imag_vector = _mm_set1_ps(c_imag); // Initialize imagiinary vector which will be acumulated float four_res = 4 * c->res; float vertical_step = (c->flipped? -1.0f: 1.0f) * c->res; @@ -109,60 +109,76 @@ static void* compute(void * vargs){ for(size_t y = args->start_row; y < args->stop_row;y++) { size_t x = 0; + // Initialize real vector which will be accumulated __m128 c_real_vector = _mm_setr_ps(c_start_real,c_start_real + 1.0f*c->res,c_start_real + 2.0f*c->res, c_start_real + 3.0f*c->res); for(; x < simd_width;x += 4) { //SIMD Loop + // Initialize start values for the calculation __m128 real = _mm_setzero_ps(); __m128 imag = _mm_setzero_ps(); __m128i iterations = _mm_setzero_si128(); - __m128i active_mask = _mm_set1_epi32(-1); + __m128i active_mask = _mm_set1_epi32(-1); // Active mask will track which pixels have digerved - 0 for diverged, 1 - for contained, at start 0xFFFF for(unsigned i = 0; i < c->n;i++) { + // Calculate squared values of real and imaginary __m128 real2 = _mm_mul_ps(real,real); __m128 imag2 = _mm_mul_ps(imag,imag); + // Detect which have diverged __m128 unbounded = _mm_cmple_ps(_mm_add_ps(real2,imag2),four); __m128i unbounded_i = _mm_castps_si128(unbounded); + // Update the active mask active_mask = _mm_and_si128(active_mask,unbounded_i); + // If all have diverged break if(_mm_test_all_zeros(active_mask,active_mask)) { break; } + // Add the iterations for each pixel only if it hasn't diverged iterations = _mm_sub_epi32(iterations,active_mask); imag = _mm_add_ps(c_imag_vector,_mm_mul_ps(minus_two, _mm_mul_ps(real,imag))); real = _mm_add_ps(_mm_sub_ps(real2,imag2),c_real_vector); } + // Calculate the value for the pixel + // value = 255 - 4 * (i % 64) + __m128i mod64 = _mm_and_si128(iterations, _mm_set1_epi32(63)); // mod is realised with a bitmask + __m128i times4 = _mm_slli_epi32(mod64, 2); // then multiplication by 4 is shift by 2 + __m128i values = _mm_sub_epi32(_mm_set1_epi32(255), times4); // subtraction from 255 - __m128i mod64 = _mm_and_si128(iterations, _mm_set1_epi32(63)); - __m128i times4 = _mm_slli_epi32(mod64, 2); - __m128i values = _mm_sub_epi32(_mm_set1_epi32(255), times4); - + // pixels which have not escaped need to be colored black so value = 0 __m128i max_iter = _mm_set1_epi32(c->n); __m128i is_max = _mm_cmpeq_epi32(iterations, max_iter); values = _mm_andnot_si128(is_max, values); + // Convert vector to an array of ints unsigned int value_array[4]; _mm_storeu_si128((__m128i*)value_array,values); + // Write the 4 values in the image buffer for(unsigned i = 0 ; i < 4;i++) { unsigned int value = value_array[i]; size_t position = (y*c->width + (x+i)) * bytes_per_color; if(!c->color) { + // If grayscale just take value c->img[position] = value; } else { + // If colored use the lookup table to get the RGB values c->img[position] = color_lookup_table[value][0]; c->img[position + 1] = color_lookup_table[value][1]; c->img[position + 2] = color_lookup_table[value][2]; } } + // Accumulate the real part c_real_vector = _mm_add_ps(c_real_vector,_mm_set1_ps(four_res)); } + // Follow up loop for the remaining pixels + // Calculates them scalarly for(;x < c->width; x++) { float c_real = c_start_real + (x*c->res); unsigned int value = converge(c_real,c_imag,c->n); @@ -178,7 +194,8 @@ static void* compute(void * vargs){ c->img[position + 2] = color_lookup_table[value][2]; } } - c_imag += vertical_step; + // Accumulate the imaginary part + c_imag += vertical_step; c_imag_vector = _mm_add_ps(c_imag_vector,vertical_step_vector); } free(args);