rust/src/test/bench/shootout-mandelbrot.rs
blake2-ppc 78cde5b9fb std: Change Times trait to use do instead of for
Change the former repetition::

    for 5.times { }

to::

    do 5.times { }

.times() cannot be broken with `break` or `return` anymore; for those
cases, use a numerical range loop instead.
2013-08-01 16:54:22 +02:00

65 lines
1.7 KiB
Rust

use std::cast::transmute;
use std::from_str::FromStr;
use std::i32::range;
use std::libc::{STDOUT_FILENO, c_int, fdopen, fputc};
use std::os;
static ITER: uint = 50;
static LIMIT: f64 = 2.0;
#[fixed_stack_segment]
fn main() {
unsafe {
let w: i32 = FromStr::from_str(os::args()[1]).get();
let h = w;
let mut byte_acc: i8 = 0;
let mut bit_num: i32 = 0;
printfln!("P4\n%d %d", w as int, h as int);
let mode = "w";
let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0]));
for range(0, h) |y| {
let y = y as f64;
for range(0, w) |x| {
let mut Zr = 0f64;
let mut Zi = 0f64;
let mut Tr = 0f64;
let mut Ti = 0f64;
let Cr = 2.0 * (x as f64) / (w as f64) - 1.5;
let Ci = 2.0 * (y as f64) / (h as f64) - 1.0;
for range(0, ITER as i32) |_| {
if Tr + Ti > LIMIT * LIMIT {
break;
}
Zi = 2.0*Zr*Zi + Ci;
Zr = Tr - Ti + Cr;
Tr = Zr * Zr;
Ti = Zi * Zi;
}
byte_acc <<= 1;
if Tr + Ti <= LIMIT * LIMIT {
byte_acc |= 1;
}
bit_num += 1;
if bit_num == 8 {
fputc(byte_acc as c_int, stdout);
byte_acc = 0;
bit_num = 0;
} else if x == w - 1 {
byte_acc <<= 8 - w%8;
fputc(byte_acc as c_int, stdout);
byte_acc = 0;
bit_num = 0;
}
}
}
}
}