LLVM优化器不支持使用Rust内联asm设置非默认的舍入模式吗? [英] Setting a non-default rounding mode with Rust inline asm isn't respected by the LLVM optimizer?

查看:141
本文介绍了LLVM优化器不支持使用Rust内联asm设置非默认的舍入模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rust板条箱,该板条会更改舍入模式(+ inf,-inf,最近或截断).

I am working on a Rust crate which changes the rounding mode (+inf, -inf, nearest, or truncate).

更改舍入模式的函数是使用内联汇编编写的:

The functions that change the rounding mode are written using inline assembly:

fn upward() {
    let cw: u32 = 0;
    unsafe {
    asm!("stmxcsr $0;
          mov $0, %eax;
          or $$0x4000, %eax;
          mov %eax, $0;
          ldmxcsr $0;"
          : "=*m"(&cw)
          : "*m"(&cw)
          : "{eax}"
        );
    }
}

当我在调试模式下编译代码时,它按预期工作,在朝正无穷大方向取整时,我得到0.3333333333337的三分之一,但是在释放模式下进行编译时,无论设置哪种取整模式,我都得到相同的结果.我猜这是由于LLVM后端所做的优化所致.

When I compile the code in debug mode it works as intended, I get 0.3333333333337 for one-third when rounding toward positive infinity, but when I compile in release mode I get the same result no matter what rounding mode I set. I guess this behavior is due to the optimizations that the LLVM backend does.

如果我知道哪个LLVM传递负责此优化,则可以将其禁用,因为目前没有其他解决方法.

If I knew which LLVM passes are responsible for this optimization, I can disable them as I don't see any other workaround at the moment.

推荐答案

基本上,您不能这样做. LLVM假定所有浮点运算都使用默认的舍入模式,并且永不读取或修改浮点控制寄存器.

Basically, you can't do this. LLVM assumes that all floating point operations use the default rounding mode, and that the floating-point control register is never read or modified.

最近在LLVM-上对此问题进行了一些讨论.开发邮件列表(如果您有兴趣).

There's been some discussion of this issue recently on the LLVM-dev mailing list, if you're interested.

同时,唯一可靠的解决方法是使用内联汇编,例如asm!("addsd $0, $1".

In the meantime, the only reliable workaround is to use inline assembly, like asm!("addsd $0, $1".

Rust的标准库还假定您不修改舍入模式(特别是,在浮点数和字符串之间转换的代码对此很敏感).

Rust's standard library also assumes that you don't modify the rounding mode (in particular, the code for converting between floating-point and strings is sensitive to this).

这篇关于LLVM优化器不支持使用Rust内联asm设置非默认的舍入模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆