Skip to content

Commit 1bc8868

Browse files
Rymanalexcrichton
authored andcommitted
---
yaml --- r: 113533 b: refs/heads/snap-stage3 c: 03f4853 h: refs/heads/master i: 113531: 7320286 v: v3
1 parent 1849cd8 commit 1bc8868

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: abdacecdf86b4b5a4f432560445a24e1c5f4751b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: a7bee7b05d59467bc6304b32eb14e617c2efbfc9
4+
refs/heads/snap-stage3: 03f48534b3ac38bc3792019cdd2b8a24eaebee50
55
refs/heads/try: 7c6c492fb2af9a85f21ff952942df3523b22fd17
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/test/bench/shootout-mandelbrot.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10+
#![feature(macro_rules)]
1011

1112
// ignore-pretty very bad with line comments
1213

@@ -18,41 +19,72 @@ use sync::Future;
1819
static ITER: int = 50;
1920
static LIMIT: f64 = 2.0;
2021

22+
macro_rules! core_loop(
23+
($pow:expr ~ $mask:expr: $ctx:ident, $b:ident) => (
24+
{
25+
let r = $ctx.r;
26+
let i = $ctx.i;
27+
28+
$ctx.r = r * r - i * i + $ctx.init_r;
29+
$ctx.i = 2.0 * r * i + $ctx.init_i;
30+
31+
if r * r + i * i > LIMIT * LIMIT {
32+
$b |= $pow;
33+
if $b == $mask { break; }
34+
}
35+
}
36+
);
37+
)
38+
39+
#[inline(always)]
2140
fn write_line(init_i: f64, vec_init_r: &[f64], res: &mut Vec<u8>) {
41+
struct Context { r: f64, i: f64, init_i: f64, init_r: f64 }
42+
impl Context {
43+
#[inline(always)]
44+
fn new(i: f64, r: f64) -> Context {
45+
Context { r: r, i: i, init_r: r, init_i: i }
46+
}
47+
}
48+
49+
let mut cur_byte;
50+
let mut i;
51+
let mut bit_1;
52+
let mut bit_2;
53+
let mut b;
2254
for chunk_init_r in vec_init_r.chunks(8) {
23-
let mut cur_byte = 0xff;
24-
let mut cur_bitmask = 0x80;
25-
for &init_r in chunk_init_r.iter() {
26-
let mut cur_r = init_r;
27-
let mut cur_i = init_i;
55+
cur_byte = 0xff;
56+
i = 0;
57+
58+
while i < 8 {
59+
bit_1 = Context::new(init_i, chunk_init_r[i]);
60+
bit_2 = Context::new(init_i, chunk_init_r[i + 1]);
61+
62+
b = 0;
2863
for _ in range(0, ITER) {
29-
let r = cur_r;
30-
let i = cur_i;
31-
cur_r = r * r - i * i + init_r;
32-
cur_i = 2.0 * r * i + init_i;
33-
34-
if r * r + i * i > LIMIT * LIMIT {
35-
cur_byte &= !cur_bitmask;
36-
break;
37-
}
64+
core_loop!(2 ~ 3: bit_1, b);
65+
core_loop!(1 ~ 3: bit_2, b);
3866
}
39-
cur_bitmask >>= 1;
67+
68+
cur_byte = (cur_byte << 2) + b;
69+
i += 2;
4070
}
41-
res.push(cur_byte);
71+
res.push(cur_byte^-1);
4272
}
4373
}
4474

4575
fn mandelbrot<W: io::Writer>(w: uint, mut out: W) -> io::IoResult<()> {
4676
// Ensure w and h are multiples of 8.
4777
let w = (w + 7) / 8 * 8;
4878
let h = w;
49-
let chunk_size = h / 8;
79+
let inverse_w_doubled = 2.0 / w as f64;
80+
let inverse_h_doubled = 2.0 / h as f64;
81+
let chunk_size = h / 16;
5082

51-
let data: Vec<Future<Vec<u8>>> = range(0u, 8).map(|i| Future::spawn(proc () {
52-
let vec_init_r = Vec::from_fn(w, |x| 2.0 * (x as f64) / (w as f64) - 1.5);
83+
let data: Vec<Future<Vec<u8>>> = range(0u, 16).map(|i| Future::spawn(proc () {
84+
let vec_init_r = Vec::from_fn(w, |x| (x as f64) * inverse_w_doubled - 1.5);
5385
let mut res: Vec<u8> = Vec::with_capacity((chunk_size * w) / 8);
5486
for y in range(i * chunk_size, (i + 1) * chunk_size) {
55-
let init_i = 2.0 * (y as f64) / (h as f64) - 1.0;
87+
let init_i = (y as f64) * inverse_h_doubled - 1.0;
5688
write_line(init_i, vec_init_r.as_slice(), &mut res);
5789
}
5890
res

0 commit comments

Comments
 (0)