A high-performance C library for generating and rendering fractal sets (Mandelbrot, Julia, and Newton fractals) with multiple SIMD and parallelization optimizations.
- Efficient Fractal Rendering: Optimized implementations for Mandelbrot, Julia, and Newton fractals
- SIMD Optimizations:
- SSE2 intrinsics for vectorized computations
- AVX2 intrinsics for enhanced performance on modern CPUs
- Multithreading: OpenMP-based parallelization for multi-core utilization
- Border-Based Rendering: Smart boundary detection algorithm using deque-based BFS to minimize unnecessary computation
- Interactive Visualization: Real-time SDL2-based viewer for exploring fractals
- Multiple Precision Levels: Standard double precision and extended __float128 precision
- CMake >= 3.16
- C11-compatible compiler (GCC, Clang, or MSVC)
- SDL2 (only required if building test binaries)
- OpenMP (optional, for multithreading support)
- SSE2 instructions (standard on modern x86_64 CPUs)
- AVX2 instructions (optional, for maximum performance on supported CPUs)
sudo apt-get install build-essential cmake libsdl2-dev libomp-dev pkg-configbrew install cmake sdl2 libomp- Install Visual Studio with MSVC or use MinGW-w64
- Install SDL2 development libraries
- Ensure CMake is in your PATH
Create a build directory and configure with CMake:
mkdir build
cd build
cmake ..The following CMake options are available:
| Option | Default | Description |
|---|---|---|
ENABLE_AVX2 |
ON | Enable AVX2 optimizations (auto-detected if supported) |
BUILD_TESTS |
OFF | Build test and example binaries |
TEST_REALTIME |
OFF | Build interactive real-time viewer |
TEST_BENCHMARK |
OFF | Build benchmark utility |
TEST_VIDEO |
OFF | Build video/image generation utility |
By default, only the static library is built:
cd build
cmake ..
makeThis creates libmandelbrot_core.a which can be linked into your own projects.
To build specific test programs, enable the corresponding options:
The interactive viewer allows you to explore fractals in real-time with mouse and keyboard controls:
cd build
cmake .. -DBUILD_TESTS=ON -DTEST_REALTIME=ON
make
./test_realtimeControls:
- Mouse Scroll: Zoom in/out
- Left Click + Drag: Pan the view
- Right Click (on Mandelbrot): Generate Julia set for the clicked point
- P: Increase iteration count
- M: Decrease iteration count
- ESC: Return to Mandelbrot set
The benchmark tool measures rendering performance with different optimization combinations (SSE2, AVX2, multithreading):
cd build
cmake .. -DBUILD_TESTS=ON -DTEST_BENCHMARK=ON
make
./test_benchmarkOutput Example:
Borderbrot rendered in 3.456789 s
Borderbrot with sse2 rendered in 1.234567 s
Borderbrot with avx2 rendered in 0.789012 s
Borderbrot with multithreading rendered in 0.654321 s
Borderbrot with multithreading and avx2 rendered in 0.123456 s
The video utility generates high-resolution fractal images or video frames:
cd build
cmake .. -DBUILD_TESTS=ON -DTEST_VIDEO=ON
make
./test_videoTo build all test binaries at once:
cd build
cmake .. -DBUILD_TESTS=ON -DTEST_REALTIME=ON -DTEST_BENCHMARK=ON -DTEST_VIDEO=ON
makeOr simply:
cd build
cmake .. -DBUILD_TESTS=ON
makeIn your CMakeLists.txt:
find_package(mandelbrot_explorer REQUIRED)
target_link_libraries(your_target PRIVATE mandelbrot_core)Or by adding the subdirectory:
add_subdirectory(path/to/mandelbrot_explorer)
target_include_directories(your_target PRIVATE path/to/mandelbrot_explorer/include)
target_link_libraries(your_target PRIVATE mandelbrot_core)#include "mandelbrot.h"
#include "palette.h"
int main() {
int width = 1920, height = 1080;
int max_iterations = 6000;
int *output = malloc(width * height * sizeof(int));
int *palette = malloc(max_iterations * sizeof(int));
// Fill palette with colors
fill_plte1(palette, max_iterations);
// Render Mandelbrot set with optimizations
mandelbrot(
output, // output buffer
width, height, // dimensions
max_iterations, // iteration limit
-0.7, 0.0, // dx, dy (offset)
2.5, // zoom level
palette, // color palette
MANDELBROT_AVX2 | MANDELBROT_MULTITHREADING | MANDELBROT_FILL // flags
);
// Use output buffer (width*height ARGB color values)
free(output);
free(palette);
return 0;
}Available flags for mandelbrot() and julia():
MANDELBROT_MULTITHREADING: Use all available CPU cores via OpenMPMANDELBROT_SSE2: Enable SSE2 vector optimizations (always attempted if available)MANDELBROT_AVX2: Enable AVX2 vector optimizations (requires AVX2 support)MANDELBROT_SMOOTH: Enable smooth coloring for better gradientsMANDELBROT_FILL: Fill uncalculated areas intelligently
void mandelbrot(int *out, unsigned int width, unsigned int height,
unsigned int max, double dx, double dy, double zoom,
int *plte, int flags);void julia(int *out, unsigned int width, unsigned int height,
unsigned int max, double dx, double dy, double zoom,
int *plte, int flags, double x0, double y0);void newton(int *out, unsigned int width, unsigned int height,
unsigned int max, double dx, double dy, double zoom,
int flags, double complex z1, double complex z2, double complex z3);void precisebrot(int *out, unsigned int width, unsigned int height,
unsigned int max, __float128 dx, __float128 dy,
__float128 zoom, int *plte, int flags);mandelbrot_explorer/
├── include/ # Public headers
│ ├── deque.h # Double-ended queue data structure
│ ├── julia.h # Julia set generation
│ ├── mandelbrot.h # Mandelbrot and variants
│ ├── newton.h # Newton fractal
│ └── palette.h # Color palette utilities
├── src/ # Core implementation
│ ├── mandelbrot.c # Mandelbrot algorithm with optimizations
│ ├── julia.c # Julia set implementation
│ ├── newton.c # Newton fractal implementation
│ ├── deque.c # Deque data structure
│ ├── palette.c # Palette generation
│ └── precisebrot.c # High-precision variants
├── test/ # Test and example programs
│ ├── realtime.c # Interactive SDL2 viewer
│ ├── benchmark.c # Performance benchmarking
│ ├── video.c # High-res image/video generation
│ └── foto.c # Additional image utilities
├── CMakeLists.txt # Build configuration
└── README.md # This file