提问人:pmor 提问时间:9/5/2023 最后编辑:artless noisepmor 更新时间:9/18/2023 访问量:61
是否可以使用有限精度浮点运算实现基于 ARM 伪代码的符合 IEEE 754 的浮点运算?
Is it possible to implement IEEE 754 conforming floating-point arithmetic based on ARM pseudocode using finite precision floating-point arithmetic?
问:
背景:浮点运算通常使用整数算术(例如,Berkeley SoftFloat)实现。根据 ARM 伪代码 [1],浮点运算是使用无限精度浮点运算 (type) 实现的。real
我的 32 位浮点运算模型是用 C 语言编写的,基于 ARM 伪代码。该类型使用有限精度浮点运算实现:64 位或 80 位(在 x86_64 上)或 128 位(在 AArch64 上):real
double
long double
long double
typedef double Real;
//typedef long double Real;
在测试它时,我注意到一些失败:大多数与缺失和/或异常有关。在某些情况下,结果是 +/-1 位偏差。Inexact
Underflow
背景:与基于整数算术的实现(检查某些位是否为非零)相比,ARM 伪代码函数计算:FPRoundBase
error
// Get the unrounded mantissa as an integer, and the "units in last place" rounding error.
int_mant = RoundDown(mantissa * 2.0^F); // < 2.0^F if biased_exp == 0, >= 2.0^F if not
error = mantissa * 2.0^F - Real(int_mant);
提出和/或例外取决于以下情况:Inexact
Underflow
error
if !altfp && biased_exp == 0 && (error != 0.0 || trapped_UF) then
if fpexc then FPProcessException(FPExc_Underflow, fpcr);
...
if error != 0.0 then
if fpexc then FPProcessException(FPExc_Inexact, fpcr);
我的问题是:在某些情况下,为零,而预期为非零,导致丢失和/或异常。但是,请注意,在这些情况下,数值结果是正确的。下面是一个示例:error
Inexact
Underflow
x + y
x -4.96411207e-35 0x8683f7ff
y -3.98828101 0xc07f3fff
x after FPUnpack -4.9641120695506692e-35 0xb8d07effe0000000
y after FPUnpack -3.9882810115814209 0xc00fe7ffe0000000
x+y -3.9882810115814209 0xc00fe7ffe0000000
=== FPRoundBase ===
op -3.9882810115814209 0xc00fe7ffe0000000
exponent 1
min_exp -126
biased_exp 128
int_mant 16728063
mantissa 1.9941405057907104 0x3fffe7ffe0000000
frac_size 23
error 0 0x0
===
在这里,我们看到它是零,而它应该是非零。error
如果我们乘以 ,我们将得到 ,四舍五入为 ,并且是 。1.9941405057907104
2^23
16728062.9999999995871232
16728063
16728063 - 16728063
0
我试图在计算时局部提高精度:修复了一些故障,出现了新的故障。我还尝试了其他一些“怪癖和调整”,结果相同:修复了一些故障,出现了新的故障。error
请注意,对 (即 ) 的所有操作都是使用 完成的。Real
double
FE_TONEAREST
最后,我开始思考:是否有可能使用有限精度浮点运算实现基于 ARM 伪代码的符合 IEEE 754 的 32 位(例如)浮点运算?
[1] 探索工具(“Arm A64 指令集架构”部分,“下载 XML”按钮)、文件。ISA_A64_xml_A_profile-2023-03/ISA_A64_xml_A_profile-2023-03/xhtml/shared_pseudocode.html
UPD0 中。我注意到 128 位比 50 位少 64% 的故障。long double
double
UPD1 中。“无错误”意味着“符合 IEEE 754 标准”。更改为“符合 IEEE 754”。
答:
我开始使用 GNU MPFR:
typedef mpfr_t Real;
测试表明:
- 可以使用有限精度浮点运算实现基于 ARM 伪代码的符合 IEEE 754 的 32 位(例如)浮点运算;
- 对于每个 FP 操作,导致达到“符合 IEEE 754”属性的最小 MPFR 精度是不同的。要添加的示例。
UPD0:使用上面的测试,我发现了以下最小 MPFR 精度:
ADD 277
SUB 277
MUL 48
DIV 48
D2F 53
FMA 426
注意:由于未经详尽的测试,这些数字可能会更高。我无法解释这些数字。我发现了一个与许多浮点数的安全准确求和中的“展开求和解决方案”的相关性:
Single:单精度浮点数有 1 个符号位, 一个 8 位指数和一个 23 位尾数。因此,表示 这作为一个整数需要 1 + 28 + 23 = 280 位。
笔记:
- 这是否意味着根据“展开求和解决方案”,FMA 需要 280 * 2 = 560 位?
- 据我了解,“展开求和解决方案”与“ARM 伪代码解决方案”不同。前者使用整数算术,后者使用浮点运算。
评论