rust/src/test/bench/shootout-mandelbrot.rs

80 lines
2.2 KiB
Rust
Raw Normal View History

// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::io;
struct DummyWriter;
impl Writer for DummyWriter {
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
}
static ITER: int = 50;
2013-04-18 23:44:50 -05:00
static LIMIT: f64 = 2.0;
2013-04-18 23:44:50 -05:00
fn main() {
let args = std::os::args();
let (w, mut out) = if args.len() < 2 {
println!("Test mode: do not dump the image because it's not utf8, \
which interferes with the test runner.");
(1000, ~DummyWriter as ~Writer)
} else {
(from_str(args[1]).unwrap(),
~std::io::stdout() as ~Writer)
};
let h = w;
let mut byte_acc = 0u8;
let mut bit_num = 0;
2013-04-18 23:44:50 -05:00
writeln!(out, "P4\n{} {}", w, h);
2013-04-18 23:44:50 -05:00
for y in range(0, h) {
let y = y as f64;
for x in range(0, w) {
let mut z_r = 0f64;
let mut z_i = 0f64;
let mut t_r = 0f64;
let mut t_i = 0f64;
let c_r = 2.0 * (x as f64) / (w as f64) - 1.5;
let c_i = 2.0 * (y as f64) / (h as f64) - 1.0;
2013-04-18 23:44:50 -05:00
for _ in range(0, ITER) {
if t_r + t_i > LIMIT * LIMIT {
break;
2013-04-18 23:44:50 -05:00
}
z_i = 2.0 * z_r * z_i + c_i;
z_r = t_r - t_i + c_r;
t_r = z_r * z_r;
t_i = z_i * z_i;
}
2013-04-18 23:44:50 -05:00
byte_acc <<= 1;
if t_r + t_i <= LIMIT * LIMIT {
byte_acc |= 1;
}
2013-04-18 23:44:50 -05:00
bit_num += 1;
if bit_num == 8 {
out.write_u8(byte_acc);
byte_acc = 0;
bit_num = 0;
} else if x == w - 1 {
byte_acc <<= 8 - w % 8;
out.write_u8(byte_acc);
byte_acc = 0;
bit_num = 0;
2013-04-18 23:44:50 -05:00
}
2012-02-03 21:50:32 -06:00
}
}
out.flush();
}