-
Notifications
You must be signed in to change notification settings - Fork 13.5k
resurect shootout-mandelbrot.rs #10531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,79 @@ | ||
// xfail-test reading from os::args()[1] - bogus! | ||
// 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::cast::transmute; | ||
use std::from_str::FromStr; | ||
use std::libc::{STDOUT_FILENO, c_int, fdopen, fputc}; | ||
use std::os; | ||
use std::io::buffered::BufferedWriter; | ||
|
||
static ITER: uint = 50; | ||
struct DummyWriter; | ||
impl Writer for DummyWriter { | ||
fn write(&mut self, _: &[u8]) {} | ||
} | ||
|
||
static ITER: int = 50; | ||
static LIMIT: f64 = 2.0; | ||
|
||
fn main() { | ||
unsafe { | ||
let w: i32 = FromStr::from_str(os::args()[1]).unwrap(); | ||
let h = w; | ||
let mut byte_acc: i8 = 0; | ||
let mut bit_num: i32 = 0; | ||
|
||
println!("P4\n{} {}", w, h); | ||
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(), | ||
~BufferedWriter::new(std::io::stdout()) as ~Writer) | ||
}; | ||
let h = w; | ||
let mut byte_acc = 0u8; | ||
let mut bit_num = 0; | ||
|
||
let mode = "w"; | ||
let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0])); | ||
writeln!(out, "P4\n{} {}", w, h); | ||
|
||
for y in range(0i32, h) { | ||
let y = y as f64; | ||
for x in range(0i32, w) { | ||
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 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; | ||
|
||
for _ in range(0i32, 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; | ||
for _ in range(0, ITER) { | ||
if t_r + t_i > LIMIT * LIMIT { | ||
break; | ||
} | ||
|
||
byte_acc <<= 1; | ||
if Tr + Ti <= LIMIT * LIMIT { | ||
byte_acc |= 1; | ||
} | ||
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; | ||
} | ||
|
||
bit_num += 1; | ||
byte_acc <<= 1; | ||
if t_r + t_i <= LIMIT * LIMIT { | ||
byte_acc |= 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; | ||
} | ||
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; | ||
} | ||
} | ||
} | ||
|
||
out.flush(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can break strings over multiple lines, and escape whitespace (i.e. the
\n
and the leading spaces of the next line) with\
, i.e.