在C ++中使用不同的IEEE浮点舍入模式 [英] Working with different IEEE floating-point rounding modes in C++

查看:277
本文介绍了在C ++中使用不同的IEEE浮点舍入模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哇是我,我必须确保GPU和CPU上的浮点结果相同。好吧,我明白IEEE已经照顾我,并提供了一个很好的标准坚持几个舍入选项;并且CUDA部分被排序(对于不同的舍入模式有内在性),所以这只是动机。

Woe is me, I have to ensure the same floating-point results on a GPU and on the CPU. Ok, I understand IEEE has taken care of me and provided a nice standard to adhere to with several rounding options; and the CUDA part is sorted out (there are intrinsics for the different rounding modes), so that's just motivation.

但是在主机端C ++代码 - 我如何执行浮点运算在一个特定的舍入模式(我的意思是在一个特定的语句,而不是整个我的翻译单元)?有没有包装函数使用装配在引擎盖下?是否有一组类用于具有不同舍入模式的浮点数代理?

But in host-side C++ code - how do I perform floating-point arithmetic in a specific rounding mode (and I mean in a specific statement, not throughout my translation unit)? Are there wrapper functions which use assembly under the hood? Is there a set of classes for floating point number proxies with the different rounding modes?

我也问同样的问题关于翻译单元级别。在编译转换单元时,如何使编译器(gcc / clang / MSVC)默认为某种舍入模式?

I'm also asking the same question about the translation-unit level. How do I make the compiler (gcc/clang/MSVC) default to a certain rounding mode when compiling a translation unit?

推荐答案

继来自@AndreasPapadopoulos的潜在客户后,它看起来像是有正式途径更改舍入模式:

Following a lead from @AndreasPapadopoulos , it looks like there is an official way to change the rounding mode:

int fesetround(int round)
int fegetround()

但有几个警告:


  1. 这是C ++ 11,而不是C ++ 98(虽然在实践中你可能只是使用你的系统的< fenv.h> 这是C99。 >
  2. 它需要通过 #pragma

  3. 沟通编译器
  4. 这种切换可能很慢。 / li>
  1. This is C++11, not C++98 (although in practice you could probably just use your system's <fenv.h> which is C99.
  2. It requires communicating with the compiler via #pragma 's
  3. This switching might be slow.

我不知道这在实践中是如何广泛使用(以及是否有更好的抽象, )。

I'm not sure how widely this is used in practice (and whether there are any nicer abstractions around it which are in more common use).

这篇关于在C ++中使用不同的IEEE浮点舍入模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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