Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile with "ffast-math"?

I'm trying to benchmark some Rust code, but I can't figure out how to set the "ffast-math" option.

% rustc -C opt-level=3 -C llvm-args='-enable-unsafe-fp-math' unrolled.rs
rustc: Unknown command line argument '-enable-unsafe-fp-math'.  Try: 'rustc -help'
rustc: Did you mean '-enable-load-pre'?

llvm-args='-ffast-math' and llvm-args='-fast' didn't work either. What flag should I be using?

like image 653
yong Avatar asked Jun 16 '15 09:06

yong


1 Answers

Rust issue #21690 talks about adding imprecise floating point operations. Linked from that issue is the addition of intrinsics that allow you to opt into looser rules on a per operation basis. For example, fadd_fast:

pub unsafe extern "rust-intrinsic" fn fadd_fast<T>(a: T, b: T) -> T

Using intrinsics requires a nightly compiler and unsafe code:

#![feature(core_intrinsics)]

use std::intrinsics::fadd_fast;

fn main() {
    let result = unsafe { fadd_fast(42.0, 31.0) };
    println!("{}", result);
}

Ultimately, this is a much better design than the all-or-nothing solution of a command line flag. Who knows if there is some floating point calculation that is critical to not use fast math, buried deep in your program? That doesn't help you when trying to compare against a C program that chose that, however!

like image 194
Shepmaster Avatar answered Sep 29 '22 20:09

Shepmaster