Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@uenoku
Created December 24, 2018 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uenoku/7cdd36e5bd1bd8fc2fd664389dbe157b to your computer and use it in GitHub Desktop.
Save uenoku/7cdd36e5bd1bd8fc2fd664389dbe157b to your computer and use it in GitHub Desktop.
/* rustc mandelbrot.rs --emit=llvm-ir --target=riscv32imc-unknown-none-elf -C opt-level=3 */
#![no_std]
#![feature(lang_items, start)]
use core::panic::PanicInfo;
extern "C" {
fn print_int(x: i32);
fn print_newline();
}
fn dbl(x: f32) -> f32 {
x + x
}
unsafe fn mandelbrot(grid_size: i32) {
let grid_size_f32 = grid_size as f32;
for y in 0..grid_size {
for x in 0..grid_size {
let mut cr = dbl(x as f32) / grid_size_f32 - 1.5f32;
let mut ci = dbl(y as f32) / grid_size_f32 - 1.0f32;
let mut zi2 = 0.0f32;
let mut zr2 = 0.0f32;
let mut zr = 0.0f32;
let mut zi = 0.0f32;
for i in (0..1001).rev() {
if i == 0 {
print_int(1);
} else {
let tr = zr2 - zi2 + cr;
let ti = dbl(zr) * zi + ci;
zr = tr;
zi = ti;
zr2 = zr * zr;
zi2 = zi * zi;
if 4.0 < zr2 + zi2 {
print_int(0);
break;
}
}
}
}
print_newline();
}
}
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
unsafe {
mandelbrot(100);
}
0
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment