are_we_fast_yet/mandelbrot.fz
# ported from: https://github.com/smarr/are-we-fast-yet/blob/master/benchmarks/Java/src/Mandelbrot.java
public mandelbrot =>
mandelbrot(size f64) =>
sum := mut 0
byte_acc := mut 0
bit_num := mut 0
y := mut 0.0
while y.get < size
ci := (2.0 * y.get / size) - 1.0
x := mut 0.0
while x.get < size
zr := mut 0.0
zrzr := mut 0.0
zi := mut 0.0
zizi := mut 0.0
cr := (2.0 * x.get / size) - 1.5
z := mut 0.0
not_done := mut true
escape := mut 0
while (not_done.get && z.get < 50)
zr <- zrzr.get - zizi.get + cr
zi <- 2.0 * zr.get * zi.get + ci
// preserve recalculation
zrzr <- zr.get * zr.get
zizi <- zi.get * zi.get
if (zrzr.get + zizi.get > 4.0)
not_done <- false
escape <- 1
z <- z.get + 1
byte_acc <- (byte_acc.get << 1) + escape.get
bit_num <- bit_num.get + 1
// Code is very similar for these cases, but using separate blocks
// ensures we skip the shifting when it's unnecessary, which is most cases.
if bit_num.get = 8
sum <- sum.get ^ byte_acc.get
byte_acc <- 0
bit_num <- 0
else if x.get = size - 1
byte_acc <- (byte_acc.get << (8 - bit_num.get))
sum <- sum.get ^ byte_acc.get
byte_acc <- 0
bit_num <- 0
x <- x.get + 1
y <- y.get + 1
sum.get
say (mandelbrot 500) # expected: 191
last changed: 2023-11-14