Skip to content

Commit

Permalink
Fix sum calculation using f64/double
Browse files Browse the repository at this point in the history
f32/float wasn't enough to hold the sum of all pixels so the result
was wrong making it seem like -ffast-math was wrong. As it turns out
after that's fixed it works fine.
  • Loading branch information
pedrocr committed Jun 8, 2017
1 parent f81e57c commit 8169fa3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions runbench
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ bins = []
{
"base" => "-O3",
"march-native" => "-O3 -march=native",
# Fast math seems to return broken results and not be a benefit
# "ffast-math" => "-O3 -march=native -ffast-math",
"ffast-math" => "-O3 -march=native -ffast-math",
}.each do |name, opts|
bench = "#{BINDIR}/clangbench-#{name}"
if !File.exist?(bench)
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ fn main() {
pixout[2] = r * matrix[2][0] + g * matrix[2][1] + b * matrix[2][2] + e * matrix[2][3];
}
let to_time = time::precise_time_ns();
let mut sum = 0f32;
let mut sum = 0f64;
for v in out {
sum += v;
sum += v as f64;
}
println!("{:.2} ms/megapixel (sum is {})",
((to_time - from_time) as f32)/((num_pixels as f32)),
Expand All @@ -70,9 +70,9 @@ fn main() {
pixout[2] = z_comps.extract(0) + z_comps.extract(1) + z_comps.extract(2);
}
let to_time = time::precise_time_ns();
let mut sum = 0f32;
let mut sum = 0f64;
for v in out {
sum += v;
sum += v as f64;
}
println!("{:.2} ms/megapixel (sum is {}) (explicit simd)",
((to_time - from_time) as f32)/((num_pixels as f32)),
Expand Down
2 changes: 1 addition & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main(void) {
int64_t to_time = time_in_micros();

// Calculate the pixel average
float sum = 0.0f;
double sum = 0.0f;
for(int i=0; i<NUM_PIXELS*3; i++) {
sum += out[i];
}
Expand Down

0 comments on commit 8169fa3

Please sign in to comment.