This source file includes following definitions.
- draw_pixel
- real
- imag
- conjugate
- magnitude
- main
#include <stdio.h>
#include "Halide.h"
#include <math.h>
using namespace Halide;
#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif
int call_count = 0;
extern "C" DLLEXPORT int draw_pixel(int x, int y, int val) {
call_count++;
static int last_y = 0;
if (y != last_y) {
printf("\n");
last_y = y;
}
const char *code = " .:-~*={}&%#@";
if (val >= static_cast<int>(strlen(code))) {
val = static_cast<int>(strlen(code))-1;
}
printf("%c", code[val]);
return 0;
}
HalideExtern_3(int, draw_pixel, int, int, int);
class Complex {
Tuple t;
public:
Complex(Expr real, Expr imag) : t(real, imag) {}
Complex(Tuple tup) : t(tup) {}
Complex(FuncRef f) : t(Tuple(f)) {}
Expr real() const {return t[0];}
Expr imag() const {return t[1];}
operator Tuple() const {return t;}
};
Complex operator+(const Complex &a, const Complex &b) {
return Complex(a.real() + b.real(),
a.imag() + b.imag());
}
Complex operator-(const Complex &a, const Complex &b) {
return Complex(a.real() - b.real(),
a.imag() - b.imag());
}
Complex operator*(const Complex &a, const Complex &b) {
return Complex(a.real() * b.real() - a.imag() * b.imag(),
a.real() * b.imag() + a.imag() * b.real());
}
Complex conjugate(const Complex &a) {
return Complex(a.real(), -a.imag());
}
Expr magnitude(Complex a) {
return (a * conjugate(a)).real();
}
int main(int argc, char **argv) {
Var x, y;
Func mandelbrot;
Complex initial(x/20.0f, y/8.0f);
Var z;
mandelbrot(x, y, z) = Complex(0.0f, 0.0f);
RDom t(1, 40);
Complex current = mandelbrot(x, y, t-1);
mandelbrot(x, y, t) = current*current + initial;
Func count;
Tuple escape = argmin(magnitude(mandelbrot(x, y, t)) < 4);
count(x, y) = select(escape[1], 0, escape[0]);
RDom r(-45, 71, -10, 21);
Func render;
render() += draw_pixel(r.x, r.y, count(r.x, r.y));
mandelbrot.compute_at(render, r.x);
render.realize();
printf("\n");
if (call_count != 71*21) {
printf("Something went wrong\n");
return -1;
}
printf("Success!\n");
return 0;
}