如何设置和保留 x87 浮点精度模式

问题描述

Q1:我有以下 C 代码:

#include <stdio.h>
#include <fpu_control.h>
#include <stdlib.h>

    fpu_control_t fpu_oldcw,fpu_cw = 0x0000007f;
    
    //get old control word
  
    _FPU_GETCW(fpu_oldcw);
    printf("Current control word is: 0x%.8x\n",fpu_oldcw);    
    
    //set the mode    
    _FPU_SETCW(fpu_cw);

    //check the changed mode
    _FPU_GETCW(fpu_oldcw);
    printf("Modified control word is: 0x%.8x\n",fpu_oldcw);    

输出符合预期:

Current control word is: 0x0000037f
Modified control word is: 0x0000007f

这正确设置了新的浮点精度模式。但是,一旦可执行文件完成,如果我从另一个 c 程序重新读取控制字,则模式为“0x0000037f”。这意味着不保留先前更改的值“0x0000007f”。那么基本上我们如何保留新模式?意味着一旦我们更改它,模式就会保持更新,直到我们手动将其更改回原始或其他任何内容。

Q2:有什么方法可以直接从 python 更改和保留模式?我目前的想法是,如果我有 getter/setter c 程序,那么我可以在 python 中调用如下代码:

from subprocess import call

call(["./set_precision"])
or 
call(["./get_precision"])

只有在两次调用 c 可执行文件(由 gcc 编译器生成)之间保留更新的模式时,这才有效。我正在考虑使用如下 ctypes 的另一种选择

libm = ctypes.CDLL("libm.so.6",ctypes.RTLD_GLOBAL)
old_rmode = libm.fegetround()
print( "old_rmode: ",hex(old_rmode) )

然而,在这种情况下,我只找到了舍入模式,但没有找到精度。

您能否建议在 x87 硬件上设置和保留精度模式的最佳方法是什么?谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...