-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from stevenewald/simd
Add AVX512 equations
- Loading branch information
Showing
12 changed files
with
198 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
#pragma once | ||
|
||
#include "config.hpp" | ||
#include "coordinates.hpp" | ||
|
||
namespace fractal { | ||
display_coordinate calculate_rectangle_end_point( | ||
display_coordinate start, display_coordinate current, | ||
float target_aspect_ratio = 800.0f / 600.0f | ||
float target_aspect_ratio = static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT | ||
); | ||
} // namespace fractal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "equations.hpp" | ||
|
||
#include "config.hpp" | ||
|
||
namespace fractal { | ||
// https://en.wikipedia.org/wiki/Mandelbrot_set#Formal_definition | ||
std::complex<complex_underlying> | ||
step(std::complex<complex_underlying> z_n, std::complex<complex_underlying> constant) | ||
{ | ||
return z_n * z_n + constant; | ||
} | ||
|
||
iteration_count compute_iterations( | ||
std::complex<complex_underlying> z_0, std::complex<complex_underlying> constant, | ||
iteration_count max_iters | ||
) | ||
{ | ||
iteration_count iterations = 0; | ||
std::complex<complex_underlying> z_n = z_0; | ||
|
||
while (iterations < max_iters && std::norm(z_n) < MANDELBROT_DIVERGENCE_NORM) { | ||
z_n = step(z_n, constant); | ||
++iterations; | ||
} | ||
|
||
return iterations; | ||
} | ||
} // namespace fractal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,14 @@ | ||
#pragma once | ||
|
||
#include "config.hpp" | ||
#include "units.hpp" | ||
|
||
namespace fractal { | ||
// https://en.wikipedia.org/wiki/Mandelbrot_set#Formal_definition | ||
inline std::complex<complex_underlying> | ||
step(std::complex<complex_underlying> z_n, std::complex<complex_underlying> constant) | ||
{ | ||
return z_n * z_n + constant; | ||
} | ||
std::complex<complex_underlying> | ||
step(std::complex<complex_underlying> z_n, std::complex<complex_underlying> constant); | ||
|
||
inline iteration_count compute_iterations( | ||
iteration_count compute_iterations( | ||
std::complex<complex_underlying> z_0, std::complex<complex_underlying> constant, | ||
iteration_count max_iters | ||
) | ||
{ | ||
iteration_count iterations = 0; | ||
std::complex<complex_underlying> z_n = z_0; | ||
|
||
while (iterations < max_iters && std::norm(z_n) < MANDELBROT_DIVERGENCE_NORM) { | ||
z_n = step(z_n, constant); | ||
++iterations; | ||
} | ||
|
||
return iterations; | ||
} | ||
); | ||
} // namespace fractal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "config.hpp" | ||
#include "equations.hpp" | ||
#include "units.hpp" | ||
|
||
#include <immintrin.h> | ||
|
||
namespace fractal { | ||
std::array<iteration_count, 8> compute_iterations( | ||
const avx512_complex& z_0, const avx512_complex& constant, iteration_count max_iters | ||
) | ||
{ | ||
static const auto SQUARED_DIVERGENCE = | ||
MANDELBROT_DIVERGENCE_NORM * MANDELBROT_DIVERGENCE_NORM; | ||
|
||
alignas(64) std::array<double, 8> reals = z_0.real; | ||
alignas(64) std::array<double, 8> imags = z_0.imaginary; | ||
alignas(64) std::array<double, 8> const_reals = constant.real; | ||
alignas(64) std::array<double, 8> const_imags = constant.imaginary; | ||
|
||
std::array<iteration_count, 8> solved_its = {0}; | ||
|
||
__m512d input_vec_real = _mm512_load_pd(reals.data()); | ||
__m512d input_vec_imag = _mm512_load_pd(imags.data()); | ||
__m512d input_vec_constant_imags = _mm512_load_pd(const_imags.data()); | ||
__m512d input_vec_constant_reals = _mm512_load_pd(const_reals.data()); | ||
__m512i solved_its_vec = _mm512_loadu_epi16(solved_its.data()); | ||
|
||
for (iteration_count iterations = 0; iterations < max_iters; iterations++) { | ||
// Square real | ||
__m512d squared_vec_real = _mm512_mul_pd(input_vec_real, input_vec_real); | ||
|
||
// Square imag | ||
__m512d squared_vec_imag = _mm512_mul_pd(input_vec_imag, input_vec_imag); | ||
|
||
// Create imags | ||
__m512d real_x2 = _mm512_mul_pd(input_vec_real, _mm512_set1_pd(2)); | ||
input_vec_imag = | ||
_mm512_fmadd_pd(real_x2, input_vec_imag, input_vec_constant_imags); | ||
|
||
// Create reals | ||
__m512d subtracted_squared = _mm512_sub_pd(squared_vec_real, squared_vec_imag); | ||
input_vec_real = _mm512_add_pd(subtracted_squared, input_vec_constant_reals); | ||
|
||
// Create squared norms | ||
__m512d squared_norms_vec = _mm512_add_pd(squared_vec_real, squared_vec_imag); | ||
__mmask8 solved_mask = _mm512_cmp_pd_mask( | ||
squared_norms_vec, _mm512_set1_pd(SQUARED_DIVERGENCE), _CMP_GT_OS | ||
); | ||
|
||
uint32_t solved = _cvtmask8_u32(solved_mask); | ||
solved_its_vec = _mm512_mask_blend_epi16( | ||
solved_mask, solved_its_vec, | ||
_mm512_set1_epi16(static_cast<int16_t>(iterations)) | ||
); | ||
if (solved == 0xFF) [[unlikely]] | ||
break; | ||
} | ||
|
||
__mmask32 mask = _mm512_cmpeq_epi16_mask(solved_its_vec, _mm512_set1_epi16(0)); | ||
solved_its_vec = _mm512_mask_mov_epi16( | ||
solved_its_vec, mask, _mm512_set1_epi16(static_cast<int16_t>(max_iters)) | ||
); | ||
_mm512_storeu_epi16(solved_its.data(), solved_its_vec); | ||
|
||
return solved_its; | ||
} | ||
} // namespace fractal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include "config.hpp" | ||
#include "units.hpp" | ||
|
||
namespace fractal { | ||
|
||
std::array<iteration_count, 8> compute_iterations( | ||
const avx512_complex& z_0, const avx512_complex& constant, iteration_count max_iters | ||
); | ||
} // namespace fractal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters