在iOS ARM设备(iPhone 4)上支持非正常IEEE 754浮点数

前端之家收集整理的这篇文章主要介绍了在iOS ARM设备(iPhone 4)上支持非正常IEEE 754浮点数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在将应用程序从 Linux x86移植到iOS ARM(iPhone 4)时,我发现了
浮点运算和小值行为的差异.

64bits floating point numbers (double)小于[/-]2.2250738585072014E-308在IEEE 754-1985/IEEE 754-2008标准中称为denormal/denormalized/subnormal号.

在iPhone 4上,这么小的数字被视为零(0),而在x86上,可以使用次正规数字进行计算.

对于Apple的文档Mac OS X Manual Page For float(3),我无法找到符合IEEE-754标准的任何解释.

但是,由于Stack Overflow(flush-to-zero behavior in floating-point arithmetic,Double vs float on the iPhone)的一些答案,我发现了一些线索.

根据一些搜索,似乎沿着ARM内核使用的VFP(或NEON)数学协处理器正在使用Flush-To-Zero(FTZ)模式(例如,在输出时将次正规值转换为0)和Denormals-Are-Zero DAZ)模式(例如,当用作输入参数时,将非正规值转换为0),以提供快速硬件处理的IEEE 754计算.

  • Full IEEE754 compliance with ARM support code
  • Run-Fast mode for near IEEE754 compliance (hardware only)

对自由贸易区和大都市的一个很好的解释可以在这里找到
x87 and SSE Floating Point Assists in IA-32: Flush-To-Zero (FTZ) and Denormals-Are-Zero (DAZ)

FTZ and DAZ modes both handle the cases when invalid floating-point data occurs or is
processed with underflow or denormal conditions. […]. The difference between a number
that is handled by FTZ and DAZ is very subtle. FTZ handles underflow conditions while
DAZ handles denormals. An underflow condition occurs when a computation results in a
denormal. In this case,FTZ mode sets the output to zero. DAZ fixes the cases when
denormals are used as input,either as constants or by reading invalid memory into
registers. DAZ mode sets the inputs of the calculation to zero before computation. FTZ
can then be said to handle [output] while DAZ handles [input].

关于自由贸易区在苹果开发商网站上唯一的事情似乎是在iOS ABI Function Call Guide

VFP status register |
FPSCR |
Special |
Condition code bits (28-31) and saturation bits (0-4) are not preserved by function calls. Exception control (8-12),rounding mode (22-23),and flush-to-zero (24) bits should be modified only by specific routines that affect the application state (including framework API functions). Short vector length (16-18) and stride (20-21) bits must be zero on function entry and exit. All other bits must not be modified.

根据ARM1176JZF-S Technical Reference Manual,18.5
Modes of operation
(第一个iPhone处理器),VFP可以配置为完全支持IEEE 754(子正常算术),但在这种情况下,它将需要一些软件支持(陷入内核以在软件中进行计算).

注意:我也读过Debian的ARM Hard Float PortVFP comparison页.

我的问题是:

>哪些人可以在iOS设备上找到关于超常数字处理的明确答案?
>可以将iOS系统设置为提供对正常数量支持,而不要求编译器仅生成完整的软件浮点代码

谢谢.

解决方法

Can one set the iOS system to provide support for subnormal number without asking the compiler to produce only full software floating point code?

是.这可以通过将FPSCR中的FZ位设置为零来实现:

static inline void DisableFZ( )
{
    __asm__ volatile("vmrs r0,fpscr\n"
                     "bic r0,$(1 << 24)\n"
                     "vmsr fpscr,r0" : : : "r0");
}@H_404_68@ 
 

请注意,当遇到可观量的非正常值时,这可能会导致应用程序性能的显着下降.您可以(并且应该)恢复默认浮点状态,然后再调用任何不使ABI保证在非默认模式下正常工作的代码

static inline void RestoreFZ( ) {
    __asm__ volatile("vmrs r0,fpscr\n"
                     "orr r0,r0" : : : "r0");
}@H_404_68@ 
 

请提交bug report请求,为iOS中的FP操作模式提供更好的文档.

原文链接:https://www.f2er.com/iOS/329785.html

猜你在找的iOS相关文章