Skip to content

Commit

Permalink
refactor (julia): struct in ref, CMake lib
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorErin committed Feb 6, 2024
1 parent 576ad10 commit 431cb32
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ add_executable(test_algo ${PERF_SOURCES})
target_link_libraries(test_algo
algos
"${binary_dir}/lib/libopencv_ts.a"
"${binary_dir}/lib/libopencv_imgcodecs.so"
"${binary_dir}/install/lib/libopencv_imgcodecs.so"
pthread
)
target_include_directories(test_algo PUBLIC
Expand Down
73 changes: 45 additions & 28 deletions src/julia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,38 @@ using namespace Halide;
static const float ci = -0.75;
static const float cr = 0.0;

static const uint8_t step_bound = 255;
static const uint8_t upper_bound = 4;

// src :: Int -> fraction :: Float -> Float
Expr halide_julia_norm(Expr src, Expr fraction) {
return (((cast<float>(src)) * 4.0f) / fraction) - 2.0f;
}

struct Complex {
struct HComplex {
Expr real, imag;

Complex(Tuple t)
HComplex(Tuple t)
: real(t[0]), imag(t[1]) {
}

Complex(Expr r, Expr i)
HComplex(Expr r, Expr i)
: real(r), imag(i) {
}

Complex(FuncRef t)
: Complex(Tuple(t)) {
HComplex(FuncRef t)
: HComplex(Tuple(t)) {
}

operator Tuple() const {
return {real, imag};
}

Complex operator+(const Complex &other) const {
HComplex operator+(const HComplex &other) const {
return {real + other.real, imag + other.imag};
}

Complex operator*(const Complex &other) const {
HComplex operator*(const HComplex &other) const {
return {real * other.real - imag * other.imag,
real * other.imag + imag * other.real};
}
Expand All @@ -57,14 +60,13 @@ struct Complex {
};

void halide_julia(uint8_t* dst, int height, int width) {
static const uint8_t step_bound = 255;
static const uint8_t upper_bound = 4;

Buffer output(dst, width, height);

Buffer output(dst, {width, height});
#ifdef __riscv
// add later
#else
Var x, y;

Complex c(cr, ci);
HComplex c(cr, ci);

Expr x_ranged;
x_ranged = halide_julia_norm(x, height);
Expand All @@ -74,42 +76,57 @@ void halide_julia(uint8_t* dst, int height, int width) {

Func julia;
Var t;
julia(x, y, t) = Complex(x_ranged, y_ranged);
julia(x, y, t) = HComplex(x_ranged, y_ranged);

// loop
RDom index(1, step_bound);
Complex current = julia(x, y, index - 1);
HComplex current = julia(x, y, index - 1);
julia(x, y, index) = current * current + c;

Expr esc_cond = Complex(julia(x, y, index)).magnitude_squared() < upper_bound;
Expr esc_cond = HComplex(julia(x, y, index)).magnitude_squared() < upper_bound;
Tuple first_escape = argmin(esc_cond);

// proj to result
Func result;
result(x, y) = cast<uint8_t>(first_escape[0]);

result.realize(output);
#endif
}

struct Complex {
float real, imag;

Complex(float fst, float snd)
: real(fst), imag(snd) {
}

Complex operator+(const Complex &other) const {
return {real + other.real, imag + other.imag};
}

Complex operator*(const Complex &other) const {
return {real * other.real - imag * other.imag,
real * other.imag + imag * other.real};
}

float magnitude_squared() const {
return real * real + imag * imag;
}
};

float julia_norm(int src, float fraction) {
return ((((float)src) * 4.0) / fraction) - 2.0;
return ((((float)src) * 4.0f) / fraction) - 2.0f;
}

uint8_t julia_step(float x_src, float y_src) {
static const uint8_t count_bound = 255;
static const uint8_t upper_bound = 4;

float x = x_src;
float y = y_src;
static const Complex c(cr, ci);
Complex current(x_src, y_src);

int count = 0;

for (;count < count_bound && x * x + y * y < upper_bound; count++) {
float new_x = x * x - y * y + cr;
float new_y = 2 * x * y + ci;

x = new_x;
y = new_y;
for (;count < step_bound && current.magnitude_squared() <= upper_bound; count++) {
current = current * current + c;
}

return count;
Expand Down

0 comments on commit 431cb32

Please sign in to comment.