在gfortran冲洗到零 [英] Flush-to-zero in gfortran

查看:196
本文介绍了在gfortran冲洗到零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不相信这是有人第一次问这个问题,但是,我在任何地方都找不到任何东西。如果这是一个重复的话,则可以使用它。

解决方案

您可以使用支持Fortran 2003 IEEE模块的最新gfortran版本来完成此任务。该标准定义了两种下溢模式 - 渐变和突变。突变是你想要的那个,它将下溢设置为0并且表示下溢浮点异常。您可以使用函数 ieee_support_underflow_control(X)来测试控制下溢模式的支持,该函数测试真实X的下溢控制的下溢控制,并且如果它是支持的。如果支持,您可以调用ieee_set_underflow_mode(.false。)设置突发下溢模式。



以下是测试程序可以用来测试下溢控制支持的默认真实类型:

 程序测试
use,intrinsic: :ieee_arithmetic
use :: intrinsic :: iso_fortran_env,only:compiler_version,compiler_options
隐式无
逻辑:: underflow_support,渐进式,下溢
real :: fptest
整数: :i

print'(4a)','该文件由'编译',&
compiler_version(),'使用选项',&
编译器选项()
fptest = 0.0
underflow_support = ieee_support_underflow_control(fptest)
if(underflow_support)then
print *,'默认真实类型支持的下溢控制'
else
stop'没有下溢控制支持'
结束如果

调用ieee_set_underflow_mode(.false。)
调用ieee_get_underflow_mode(渐进)
如果(.not.gradual)那么
print *,'能够设置突发下溢模式'
else
stop'错误设置下溢模式'
如果
$结束b $ b fptest = 2e-36
do i = 1,50!最多50次迭代
fptest = fptest * 0.5
print'(e15.10)',fptest
调用ieee_get_flag(ieee_underflow,下溢)
if(underflow)print *,'Underflow例外信号'
if(fptest == 0.0)exit
end do

结束程序测试

使用gfortran 5.2.0版本,这个程序输出:

 编译这个文件通过GCC版本5.2.0使用选项-mtune = generic -march = x86-64 -fno-unsafe-math-optimizations -frounding-math -fsignaling-nans 
支持默认真实类型的下溢控制
能够设置abrubpt下溢模式
.1000000036E-35
.0000000080E-36
.2500000090E-36
.1250000045E-36
.6250000225E-37
.3125000112E-37
.1562500056E-37
.0000000000E + 00
下溢异常信令

编译器选项flags -fno-unsafe-math-optimizations -frounding-math -fsignaling-nans ar e由gfortran 5.2文档建议,随时使用IEEE模块来确保遵守标准。


Is there a way to force flush-to-zero of underflows in gfortran?

I can't believe this is the first time someone has asked this, but I couldn't find anything on it anywhere. Mea culpa if this is a duplicate.

解决方案

You can accomplish this with recent versions of gfortran that support the Fortran 2003 IEEE modules. The standard defines two underflow modes -- gradual and abrupt. Abrupt is the one you want which sets underflow to 0 and signals the underflow floating point exception. You can test for support of controlling the underflow mode with the function ieee_support_underflow_control(X) which tests for underflow control for the kind of real X is and returns a logical true if it is supported. If supported, you can then call ieee_set_underflow_mode(.false.) to set abrupt underflow mode.

Below is a test program you can use to test underflow control support for the default real kind:

program test
  use, intrinsic :: ieee_arithmetic
  use, intrinsic :: iso_fortran_env, only: compiler_version, compiler_options
  implicit none
  logical :: underflow_support, gradual, underflow
  real :: fptest
  integer :: i

  print '(4a)',  'This file was compiled by ', &
       compiler_version(), ' using the options ', &
       compiler_options()
  fptest = 0.0
  underflow_support = ieee_support_underflow_control(fptest)
  if (underflow_support) then
     print *,'Underflow control supported for the default real kind'
  else
     stop 'no underflow control support'
  end if

  call ieee_set_underflow_mode(.false.)
  call ieee_get_underflow_mode(gradual)
  if (.not.gradual) then 
     print *,'Able to set abrupt underflow mode'
  else
     stop 'error setting underflow mode'
  end if

  fptest = 2e-36
  do i=1,50 ! 50 iterations max
     fptest = fptest * 0.5
     print '(e15.10)',fptest
     call ieee_get_flag(ieee_underflow,underflow)
     if (underflow) print *,'Underflow exception signaling'
     if (fptest == 0.0) exit
  end do

end program test

Using gfortran version 5.2.0, this program outputs:

This file was compiled by GCC version 5.2.0 using the options -mtune=generic -march=x86-64 -fno-unsafe-math-optimizations -frounding-math -fsignaling-nans
 Underflow control supported for the default real kind
 Able to set abrubpt underflow mode
.1000000036E-35
.5000000180E-36
.2500000090E-36
.1250000045E-36
.6250000225E-37
.3125000112E-37
.1562500056E-37
.0000000000E+00
 Underflow exception signaling

The compiler option flags -fno-unsafe-math-optimizations -frounding-math -fsignaling-nans are suggested by the gfortran 5.2 documentation to be used anytime the IEEE modules are used to ensure adherence to the standard.

这篇关于在gfortran冲洗到零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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