应该FXAM单precision浮点值的工作? [英] Should fxam work for single precision floating point values?

查看:239
本文介绍了应该FXAM单precision浮点值的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题从<一起来href=\"http://stackoverflow.com/questions/31714128/why-is-isnormal-saying-a-value-is-normal-when-it-isnt/31715338?noredirect=1#comment51409140_31715338\">Why是isnormal()说的值是正常的,当它不是?

一个C编译器生成以下code这是为了检测是否32位浮法通过在正常与否:

A C compiler generates the following code which is supposed to detect if the 32-bit float passed in is Normal or not:

    flds    24(%esp)
    fxam; fstsw %ax;
    andw    $17664, %ax
    cmpw    $1024, %ax
    sete    %al

(满code可以被看作这里 )。

这是code正确吗?该程序似乎不正确的行为,说一个数字是正常时,它不是。我们认为,也许正在检查的数量为双在这里precision正常。

Is this code correct? The program appears to behave incorrectly, saying a number is Normal when it isn't. We think that perhaps the number is being checked for double-precision normality here.

推荐答案

我查了英特尔的<一href=\"https://www-ssl.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html\"相对=nofollow> insn的参考手册,从 http://stackoverflow.com/tags/x86/info链接

有只有一个版本的 FXAM 指令,它在80bit的寄存器进行操作。所以,是的,这(低效率)测试80bit的临时常态。 (更有效的将是测试$ 1024%EAX ,而不是掩盖,然后 CMP

There is only one version of the fxam instruction, and it operates on 80bit registers. So yes, that (inefficiently) tests the 80bit temporary for normality. (more efficient would be test $1024, %eax, rather than masking and then cmp.)

根据这一 FLDS 本身将引发非规格化异常。我认为,这意味着它的测试的实际源,转换为80bit的没有结果。该网页说,非正规异常将在状态字中设置的位。

According to this, flds itself will raise a Denormal exception. I think this means it's testing the actual source, not the result of conversion to 80bit. That page says the denormal exception will set bits in the status word.

英特尔的参考手册没有说关于什么FLD 设置状态字,有关设置C1标志,并留下C0只是东西,C2和C3未定义。它说,你可以得到一个#D FPU异常,如果来源是非正规的,但如果来源是80bit的格式,这将不会发生。

Intel's ref manual doesn't say anything about fld setting the status word, just stuff about setting the C1 flag, and leaving C0, C2, and C3 undefined. It does say you can get a #D FPU exception if the source is denormal, but that this won't happen if the source is in 80bit format.

我不知道,如果状态字会真正得到设定非正规如果未启用FPU异常。我不是这方面的专家。我此页面(和控制字部分)的是,FPU状态字大部分指令后更新。如果 D 位在控制寄存器设置(这是默认设置),则非规格化操作数设置 D 状态字位。这是未设置(揭露),异常会发生。

I don't know if the status word will actually get set for denormals if FPU exceptions aren't enabled. I'm not an expert on this. My reading of this page (and the control-word section) is that the FPU status word is updated after most instructions. If the D bit is set in the control register (which it is by default), then denormal operands set the D bit in the status word. It it was unset (unmasked), an exception would happen.

所以我觉得一个函数来测试一个浮动的非正规看起来像:

So I think a function to test a float for denormal would look like:

isdenormalf:
    flds (%rdi)   # sets FPU status based on the input to the 32->80bit conversion
    fstsw %ax
    fstp %st0     # pop
    test $2, %al  # avoid 16 bit ops (%ax), they're slow on Intel
    sete %al   #  or just branch on flags directly if your compiler's smart
    ret

我还没有试过,所以它可能是完全伪造的。在弹出,我们要保持加载可能是不平凡的内联数据空载/的方式写的。也许需要一个地址为arg,返回浮点(因此它可以在登记的x87),并与病情的输出ARG。

I haven't tried this, so it may be completely bogus. Writing this in a way that inlines without load/popping data that we want to keep loaded may be non-trivial. Maybe take an address arg, return a float (so it can be in an x87 register), and have an output arg with the condition.

我不明白,可以在上证所检查浮动注册非正规的指令。

I don't see an instruction that can check a float in an SSE register for denormal.

我觉得我有一个(慢)的方式来测试与SSE4.1或AVX的 ROUNDSS 非正规。您需要使用不同的版本根据输入的符号。

I think I have a (slow) way to test for denormals with SSE4.1 or AVX's ROUNDSS. You need to use a different version depending on the sign of the input.

对于正值:


  • 圆朝 + INF 与非正规数为零

  • 圆朝 + INF 没有非正规数为零。

  • 如果两个舍入结果是不同的,那么非正规数为零了影响(这意味着输入了非正规)

  • Round towards +inf with denormals-are-zero
  • Round towards +inf without denormals-are-zero.
  • If the two rounding results are different, then denormals-are-zero had an effect (meaning the input was denormal)

负数需要对-INF 要四舍五入,而不是 + INF ,否则 -0.xx 将始终围绕零。因此,这将有一个分公司,两个 ROUNDSS ES和比较。在IEEE浮点格式位黑客可能会更快。

Negative numbers need to be rounded towards -inf, not +inf, otherwise -0.xx will always round to zero. So this would have a branch, two ROUNDSSes, and a compare. Bit-hacks on the IEEE floating point format would probably be faster.

这篇关于应该FXAM单precision浮点值的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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