gcc 触发函数的结果是否应该依赖于同一操作系统/编译器的硬件?

4zcjmb1e  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个简单的c++程序(foo.cxx):

#include <stdio.h>
#include <math.h>

int main()
{
    long int *p;

    double ang2 = -0.23202523431296057;
    p = (long int*)&ang2;
    printf("The bits of ang2 are %lx\n", *p);

    double sin_ang2 = sin(ang2);
    printf("sin_ang2 is %0.17f\n", sin_ang2);
    p = (long int*)&sin_ang2;
    printf("The bits of sin_ang2 are %lx\n", *p);
}

我有两台硬件不同的计算机,都是Ubuntu 20.04,都是gcc 9.3.0。在这两台计算机上,我使用以下命令编译上述代码:
g ++-浮动存储foo. cxx
在计算机1上,运行上述程序的结果为:

The bits of ang2 are bfcdb300bc9c468a
sin_ang2 is -0.22994895724656178
The bits of sin_ang2 are bfcd6ef7a98fc7ce

在计算机2上,运行上述程序的结果为:

The bits of ang2 are bfcdb300bc9c468a
sin_ang2 is -0.22994895724656181
The bits of sin_ang2 are bfcd6ef7a98fc7cf

请注意在这两台机器上调用sin()的结果有细微的差别。我的问题是这是否应该预料到。我意识到浮点运算有许多细微差别会导致不精确的结果,但这是一个例子吗?我的理解是gcc的-ffloat-store选项可以帮助在不同的机器上提供一致的结果,尽管它在这里似乎没有帮助:

  • 浮存
Do not store floating-point variables in registers, and inhibit other options that might change whether a floating-point value is taken from a register or memory.

This option prevents undesirable excess precision on machines such as the 68000 where the floating registers (of the 68881) keep more precision than a double is supposed to have. Similarly for the x86 architecture. For most programs, the excess precision does only good, but a few programs rely on the precise definition of IEEE floating point. Use -ffloat-store for such programs, after modifying them to store all pertinent intermediate computations into variables.

机器1(lscpu)的硬件为:

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   36 bits physical, 48 bits virtual
CPU(s):                          4
...
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           58
Model name:                      Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz
...
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtr
                                 r pge mca cmov pat pse36 clflush dts acpi mmx f
                                 xsr sse sse2 ss ht tm pbe syscall nx rdtscp lm 
                                 constant_tsc arch_perfmon pebs bts rep_good nop
                                 l xtopology nonstop_tsc cpuid aperfmperf pni pc
                                 lmulqdq dtes64 monitor ds_cpl vmx smx est tm2 s
                                 sse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic p
                                 opcnt tsc_deadline_timer aes xsave avx f16c rdr
                                 and lahf_lm cpuid_fault epb pti ssbd ibrs ibpb 
                                 stibp tpr_shadow vnmi flexpriority ept vpid fsg
                                 sbase smep erms xsaveopt dtherm ida arat pln pt
                                 s md_clear flush_l1d

机器2的硬件是:

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          16
...
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           158
Model name:                      Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
...
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtr
                                 r pge mca cmov pat pse36 clflush dts acpi mmx f
                                 xsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rd
                                 tscp lm constant_tsc art arch_perfmon pebs bts 
                                 rep_good nopl xtopology nonstop_tsc cpuid aperf
                                 mperf pni pclmulqdq dtes64 monitor ds_cpl vmx s
                                 mx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid s
                                 se4_1 sse4_2 x2apic movbe popcnt tsc_deadline_t
                                 imer aes xsave avx f16c rdrand lahf_lm abm 3dno
                                 wprefetch cpuid_fault epb invpcid_single ssbd i
                                 brs ibpb stibp ibrs_enhanced tpr_shadow vnmi fl
                                 expriority ept vpid ept_ad fsgsbase tsc_adjust 
                                 bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx
                                  smap clflushopt intel_pt xsaveopt xsavec xgetb
                                 v1 xsaves dtherm ida arat pln pts hwp hwp_notif
                                 y hwp_act_window hwp_epp md_clear flush_l1d arc
                                 h_capabilities

关于如何在这两台机器上获得一致的结果,有什么建议吗?

zpgglvta

zpgglvta1#

这些CPU的差异不足以对相同的指令产生不同的结果。您看到的差异来自libc中不同的sin实现。链接器根据您的CPU支持的内容(__sin_avx或__sin_fma)动态选择实现。
没有直接的方法可以禁用此功能:Disable AVX-optimized functions in glibc (LD_HWCAP_MASK, /etc/ld.so.nohwcap) for valgrind & gdb record

相关问题