From a351f6279aae853968b7a40400da4edcc67c4e5d Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 30 Mar 2020 22:47:45 +0800 Subject: [PATCH] mandelbrot demo --- demo.c | 25 +++++++++++++++++++++++++ mandelbrot.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++- test.py | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 demo.c create mode 100644 mandelbrot.py diff --git a/demo.c b/demo.c new file mode 100644 index 000000000..d856a2872 --- /dev/null +++ b/demo.c @@ -0,0 +1,25 @@ +// gcc -Wall -o demo demo.c test.o + +#include +#include + +int output(int x) { + static char chars[] = " .,-:;i+hHM$*#@ "; + if(x < 0) { + putchar('\n'); + } else { + if(x < strlen(chars)) { + putchar(chars[x]); + } else { + printf("ERROR\n"); + } + } + return 0; +} + +extern int run(); + +int main() { + run(); + return 0; +} diff --git a/mandelbrot.py b/mandelbrot.py new file mode 100644 index 000000000..4d69fb28b --- /dev/null +++ b/mandelbrot.py @@ -0,0 +1,44 @@ +from numpy import int32, float64 +import sys + + +def run() -> int32: + minX = -2.0 + maxX = 1.0 + width = 78 + height = 36 + aspectRatio = 2.0 + + yScale = float64(maxX-minX)*(float64(height)/float64(width))*aspectRatio + + y = 0 + while y < height: + x = 0 + while x < width: + c_r = minX+float64(x)*(maxX-minX)/float64(width) + c_i = float64(y)*yScale/float64(height)-yScale/2.0 + z_r = c_r + z_i = c_i + i = 0 + while i < 16: + if z_r*z_r + z_i*z_i > 4.0: + break + new_z_r = (z_r*z_r)-(z_i*z_i) + c_r + z_i = 2.0*z_r*z_i + c_i + z_r = new_z_r + i = i + 1 + output(i) + x = x + 1 + output(-1) + y = y + 1 + return 0 + + + +def output(i): + if i >= 0: + sys.stdout.write(" .,-:;i+hHM$*#@ "[i]) + else: + print("") + +run() diff --git a/src/main.rs b/src/main.rs index 7065da06a..7afecd720 100644 --- a/src/main.rs +++ b/src/main.rs @@ -561,7 +561,8 @@ impl<'ctx> CodeGen<'ctx> { } fn output(&self) { - let triple = TargetTriple::create("riscv32-none-linux-gnu"); + //let triple = TargetTriple::create("riscv32-none-linux-gnu"); + let triple = TargetMachine::get_default_triple(); let target = Target::from_triple(&triple) .expect("couldn't create target from target triple"); diff --git a/test.py b/test.py index 57aa3ddf5..63fee33bc 100644 --- a/test.py +++ b/test.py @@ -1,2 +1,30 @@ -def foo(x: int32, y: int32) -> int32: - return x + y +def run() -> int32: + minX = -2.0 + maxX = 1.0 + width = 78 + height = 36 + aspectRatio = 2.0 + + yScale = float64(maxX-minX)*(float64(height)/float64(width))*aspectRatio + + y = 0 + while y < height: + x = 0 + while x < width: + c_r = minX+float64(x)*(maxX-minX)/float64(width) + c_i = float64(y)*yScale/float64(height)-yScale/2.0 + z_r = c_r + z_i = c_i + i = 0 + while i < 16: + if z_r*z_r + z_i*z_i > 4.0: + break + new_z_r = (z_r*z_r)-(z_i*z_i) + c_r + z_i = 2.0*z_r*z_i + c_i + z_r = new_z_r + i = i + 1 + output(i) + x = x + 1 + output(-1) + y = y + 1 + return 0