Skip to content

Commit 6725b9e

Browse files
committed
---
yaml --- r: 101865 b: refs/heads/master c: 500d29b h: refs/heads/master i: 101863: e900571 v: v3
1 parent 99d8961 commit 6725b9e

File tree

3 files changed

+75
-51
lines changed

3 files changed

+75
-51
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: adea48abb730bfcfdc7008f6bc5964ed5b1e7ca6
2+
refs/heads/master: 500d29b589a608357c1b82e4203dc1b77454c78c
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6e7f170fedd3c526a643c0b2d13863acd982be02
55
refs/heads/try: a97642026c18a624ff6ea01075dd9550f8ed07ff

trunk/src/librustc/middle/trans/base.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,15 @@ pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
273273
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
274274
}
275275
}
276-
_ => {}
276+
_ => {
277+
// For non-immediate arguments the callee gets its own copy of
278+
// the value on the stack, so there are no aliases
279+
if !type_is_immediate(ccx, arg_ty) {
280+
unsafe {
281+
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
282+
}
283+
}
284+
}
277285
}
278286
}
279287

trunk/src/test/bench/noise.rs

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,112 +8,128 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Multi-language Perlin noise benchmark.
12-
// See https://github.com/nsf/pnoise for timings and alternative implementations.
11+
// Perlin noise benchmark from https://gist.github.com/1170424
1312

14-
use std::f32::consts::PI;
15-
use std::rand::{Rng, StdRng};
13+
use std::f64;
14+
use std::rand::Rng;
15+
use std::rand;
1616

1717
struct Vec2 {
1818
x: f32,
1919
y: f32,
2020
}
2121

22+
#[inline(always)]
2223
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
2324

25+
#[inline(always)]
2426
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
2527

26-
fn random_gradient<R: Rng>(r: &mut R) -> Vec2 {
27-
let v = PI * 2.0 * r.gen();
28-
Vec2 { x: v.cos(), y: v.sin() }
28+
fn random_gradient<R:Rng>(r: &mut R) -> Vec2 {
29+
let v = 2.0 * f64::consts::PI * r.gen();
30+
Vec2 {
31+
x: v.cos() as f32,
32+
y: v.sin() as f32,
33+
}
2934
}
3035

3136
fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
32-
(p.x - orig.x) * grad.x + (p.y - orig.y) * grad.y
37+
let sp = Vec2 {x: p.x - orig.x, y: p.y - orig.y};
38+
grad.x * sp.x + grad.y * sp.y
3339
}
3440

3541
struct Noise2DContext {
3642
rgradients: [Vec2, ..256],
37-
permutations: [i32, ..256],
43+
permutations: [int, ..256],
3844
}
3945

4046
impl Noise2DContext {
41-
fn new() -> Noise2DContext {
42-
let mut rng = StdRng::new();
43-
44-
let mut rgradients = [Vec2 { x: 0.0, y: 0.0 }, ..256];
45-
for x in rgradients.mut_iter() {
46-
*x = random_gradient(&mut rng);
47+
pub fn new() -> Noise2DContext {
48+
let mut r = rand::rng();
49+
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
50+
for i in range(0, 256) {
51+
rgradients[i] = random_gradient(&mut r);
4752
}
48-
49-
let mut permutations = [0i32, ..256];
50-
for (i, x) in permutations.mut_iter().enumerate() {
51-
*x = i as i32;
53+
let mut permutations = [ 0, ..256 ];
54+
for i in range(0, 256) {
55+
permutations[i] = i;
5256
}
53-
rng.shuffle_mut(permutations);
57+
r.shuffle_mut(permutations);
5458

55-
Noise2DContext { rgradients: rgradients, permutations: permutations }
59+
Noise2DContext {
60+
rgradients: rgradients,
61+
permutations: permutations,
62+
}
5663
}
5764

58-
fn get_gradient(&self, x: i32, y: i32) -> Vec2 {
65+
#[inline(always)]
66+
pub fn get_gradient(&self, x: int, y: int) -> Vec2 {
5967
let idx = self.permutations[x & 255] + self.permutations[y & 255];
6068
self.rgradients[idx & 255]
6169
}
6270

63-
fn get_gradients(&self, x: f32, y: f32) -> ([Vec2, ..4], [Vec2, ..4]) {
71+
#[inline]
72+
pub fn get_gradients(&self,
73+
gradients: &mut [Vec2, ..4],
74+
origins: &mut [Vec2, ..4],
75+
x: f32,
76+
y: f32) {
6477
let x0f = x.floor();
6578
let y0f = y.floor();
66-
let x1f = x0f + 1.0;
67-
let y1f = y0f + 1.0;
68-
69-
let x0 = x0f as i32;
70-
let y0 = y0f as i32;
79+
let x0 = x0f as int;
80+
let y0 = y0f as int;
7181
let x1 = x0 + 1;
7282
let y1 = y0 + 1;
7383

74-
([self.get_gradient(x0, y0), self.get_gradient(x1, y0),
75-
self.get_gradient(x0, y1), self.get_gradient(x1, y1)],
76-
[Vec2 { x: x0f, y: y0f }, Vec2 { x: x1f, y: y0f },
77-
Vec2 { x: x0f, y: y1f }, Vec2 { x: x1f, y: y1f }])
84+
gradients[0] = self.get_gradient(x0, y0);
85+
gradients[1] = self.get_gradient(x1, y0);
86+
gradients[2] = self.get_gradient(x0, y1);
87+
gradients[3] = self.get_gradient(x1, y1);
88+
89+
origins[0] = Vec2 {x: x0f + 0.0, y: y0f + 0.0};
90+
origins[1] = Vec2 {x: x0f + 1.0, y: y0f + 0.0};
91+
origins[2] = Vec2 {x: x0f + 0.0, y: y0f + 1.0};
92+
origins[3] = Vec2 {x: x0f + 1.0, y: y0f + 1.0};
7893
}
7994

80-
fn get(&self, x: f32, y: f32) -> f32 {
95+
#[inline]
96+
pub fn get(&self, x: f32, y: f32) -> f32 {
8197
let p = Vec2 {x: x, y: y};
82-
let (gradients, origins) = self.get_gradients(x, y);
83-
98+
let mut gradients = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
99+
let mut origins = [ Vec2 { x: 0.0, y: 0.0 }, ..4 ];
100+
self.get_gradients(&mut gradients, &mut origins, x, y);
84101
let v0 = gradient(origins[0], gradients[0], p);
85102
let v1 = gradient(origins[1], gradients[1], p);
86103
let v2 = gradient(origins[2], gradients[2], p);
87104
let v3 = gradient(origins[3], gradients[3], p);
88-
89105
let fx = smooth(x - origins[0].x);
90106
let vx0 = lerp(v0, v1, fx);
91107
let vx1 = lerp(v2, v3, fx);
92108
let fy = smooth(y - origins[0].y);
93-
94109
lerp(vx0, vx1, fy)
95110
}
96111
}
97112

98113
fn main() {
99-
let symbols = [' ', '░', '▒', '▓', '█', '█'];
114+
let symbols = [" ", "░", "▒", "▓", "█", "█"];
100115
let mut pixels = [0f32, ..256*256];
101-
let n2d = Noise2DContext::new();
102-
103-
for _ in range(0, 100) {
116+
let n2d = ~Noise2DContext::new();
117+
for _ in range(0, 100u) {
104118
for y in range(0, 256) {
105119
for x in range(0, 256) {
106-
let v = n2d.get(x as f32 * 0.1, y as f32 * 0.1);
107-
pixels[y*256+x] = v * 0.5 + 0.5;
108-
}
109-
}
110-
}
120+
let v = n2d.get(
121+
x as f32 * 0.1f32,
122+
y as f32 * 0.1f32
123+
) * 0.5f32 + 0.5f32;
124+
pixels[y*256+x] = v;
125+
};
126+
};
127+
};
111128

112129
for y in range(0, 256) {
113130
for x in range(0, 256) {
114-
let idx = (pixels[y*256+x] / 0.2) as uint;
115-
print!("{:c}", symbols[idx]);
131+
print!("{}", symbols[(pixels[y*256+x] / 0.2f32) as int]);
116132
}
117-
print!("\n");
133+
println!("");
118134
}
119135
}

0 commit comments

Comments
 (0)