在哪些平台上通过过零触发一个浮点异常呢整数除法? [英] On which platforms does integer divide by zero trigger a floating point exception?

查看:499
本文介绍了在哪些平台上通过过零触发一个浮点异常呢整数除法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另外一个问题,有人不知道,而实际上他们有一个整数除以零的C ++程序,为什么他们得到一个浮点错误。讨论零起来解决这个问题,一些声称浮点异常实际上从未被零上调的浮动的鸿沟,但仅限于的整数的出现鸿沟。

In another question, someone was wondering why they were getting a "floating point error" when in fact they had an integer divide-by-zero in their C++ program. A discussion arose around this, with some asserting that floating point exceptions are in fact never raised for float divide by zero, but only arise on integer divide by zero.

这听起来很奇怪,我,因为我知道:

This sounds strange to me, because I know that:


  1. 在x86和x64的所有Windows平台

    MSVC编译code报告由零一个int鸿沟0xc0000094:整数除以零,并作为0xC000008E浮点除法由零浮动鸿沟零(启用时)

  1. MSVC-compiled code on x86 and x64 on all Windows platforms reports an int divide by zero as "0xc0000094: Integer division by zero", and float divide by zero as 0xC000008E "Floating-point division by zero" (when enabled)

<一个href=\"http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf\"相对=nofollow> IA-32和AMD64 国际检索单位指定 #DE (整数除法除外)作为中断0浮点异常触发中断16(的x87浮点运算)或中断19(SIMD浮点)。

IA-32 and AMD64 ISAs specify #DE (integer divide exception) as interrupt 0. Floating-point exceptions trigger interrupt 16 (x87 floating-point) or interrupt 19 (SIMD floating-point).

其他硬件方面也有类似的不同中断(例如的PPC提高0x7000上浮动的div被零,并且不陷阱INT / 0的话)。

Other hardware have similarly different interrupts (eg PPC raises 0x7000 on float-div-by-zero and doesn't trap for int/0 at all).

我们的应用程序取消屏蔽浮点例外除以零与 _controlfp_s 征(最终 stmxcsr OP),然后捉住他们用于调试目的。所以我肯定见过IEEE754除以零在实践中的例外。

Our application unmasks floating-point exceptions for divide-by-zero with the _controlfp_s intrinsic (ultimately stmxcsr op) and then catches them for debugging purposes. So I have definitely seen IEEE754 divide-by-zero exceptions in practice.

所以,我的结论是有一些平台,报告INT例外浮点异常,如<一个href=\"http://stackoverflow.com/questions/16928942/why-does-integer-division-by-zero-result-in-a-floating-point-exception\">x64 Linux操作系统(所有算术错误,提高SIGFPE无论ALU管)。

So I conclude that there are some platforms that report int exceptions as floating point exceptions, such as x64 Linux (raising SIGFPE for all arithmetic errors regardless of ALU pipe).

还有什么其他的操作系统(如果你的或C / C ++运行时是的操作系统)的报告整数格按零浮点异常?

What other operating systems (or C/C++ runtimes if you are the operating system) report integer div-by-zero as a floating point exception?

推荐答案

我不知道目前的情况是怎么来的,但它是目前唯一FP异常检测支持是整数非常不同的情况。它是整数除法陷阱常见。 POSIX需要它来筹集 SIGFPE 如果它抛出一个异常都没有。

I'm not sure how the current situation came to be, but it's currently the case that FP exception detection support is very different from integer. It's common for integer division to trap. POSIX requires it to raise SIGFPE if it raises an exception at all.

不过,您可以挑选出地看到,它实际上是一个部门的例外是什么样SIGFPE是的。 (不一定除以零,虽然:2的补 INT_MIN / 1 分工陷阱和的 86的 DIV IDIV 也陷阱时,64B / 32B除法的商不适合在32B输出寄存器)。

However, you can sort out what kind of SIGFPE it was, to see that it was actually a division exception. (Not necessarily divide-by-zero, though: 2's complement INT_MIN / -1 division traps, and x86's div and idiv also trap when the quotient of 64b/32b division doesn't fit in the 32b output register.)

借助 glibc的手册解释说 BSD和GNU系统提供额外的ARG的信号处理程序 SIGFPE ,这将是 FPE_INTDIV_TRAP 为除零。 POSIX文件 FPE_INTDIV_TRAP 作为一个可能值 siginfo_t INT SI_ code 字段,在系统中 siginfo_t 包括成员。

The glibc manual explains that BSD and GNU systems deliver an extra arg to the signal handler for SIGFPE, which will be FPE_INTDIV_TRAP for divide by zero. POSIX documents FPE_INTDIV_TRAP as a possible value for siginfo_t's int si_code field, on systems where siginfo_t includes that member.

IDK Windows是否在第一时间提供了一个不同的异常,或者如果它的东西捆绑到同一个算术异常类似Unix的做的不同口味。如果是这样,默认的处理程序去codeS额外的信息来告诉你什么样的例外是。

IDK if Windows delivers a different exception in the first place, or if it bundles things into different flavours of the same arithmetic exception like Unix does. If so, the default handler decodes the extra info to tell you what kind of exception it was.

POSIX和Windows都使用短语除以零,以涵盖所​​有整数除法异常,因此显然这是很常见的简写。对于人们所知道的关于关于INT_MIN / -1(与2的补数)是一个问题,那句除以零可以作为代名词鸿沟例外。这句话立刻指出了人们共同的情况下,不知道为什么整数除法可能是一个问题。

POSIX and Windows both use the phrase "division by zero" to cover all integer division exceptions, so apparently this is common shorthand. For people that do know about about INT_MIN / -1 (with 2's complement) being a problem, the phrase "division by zero" can be taken as synonymous with a divide exception. The phrase immediately points out the common case for people that don't know why integer division might be a problem.

FP异常被大多数操作系统中默认为用户空间进程屏蔽/ C ABI的。

FP exceptions are masked by default for user-space processes in most operating systems / C ABIs.

这是有道理的,因为IEEE浮点可以重新present无穷大,并具有NaN的使用值的误差传播到所有将来的计算。

This makes sense, because IEEE floating point can represent infinities, and has NaN to propagate the error to all future calculations using the value.


  • 0.0 / 0.0 => NaN的

  • 如果 X 是有限的: X / 0.0 => +/-天道酬勤为x
  • 0.0/0.0 => NaN
  • If x is finite: x/0.0 => +/-Inf with the sign of x

这甚至允许这样的事情产生时异常都将屏蔽一个合理的结果:

This even allows things like this to produce a sensible result when exceptions are masked:

double x = 0.0;
double y = 1.0/x;   // y = +Inf
double z = 1.0/y;   // z = 1/Inf = 0.0, no FP exception


FP与整数错误检测

检测错误的FP方法是pretty良好:当异常被屏蔽,它们在FP状态寄存器,而不是捕获设置一个标志。 (例如86的MXCSR的SSE指令)。该标志保持设置,直到手动清零,这样你就可以(在例如环)检查一次,看看哪些异常发生,但不是在那里发生的事情。


FP vs. integer error detection

The FP way of detecting errors is pretty good: when exceptions are masked, they set a flag in the FP status register instead of trapping. (e.g. x86's MXCSR for SSE instructions). The flag stays set until manually cleared, so you can check once (after a loop for example) to see which exceptions happened, but not where they happened.

有过建议溢出标志的记录,如果溢出计算的序列中发生的任何一点。允许整数除法异常被掩蔽会在某些情况下很好,但在其他情况下是危险的(例如在地址计算,应陷阱而不是潜在地存储到一个虚假的位置)。

There have been proposals for having similar "sticky" integer-overflow flags to record if overflow happened at any point during a sequence of computations. Allowing integer division exceptions to be masked would be nice in some cases, but dangerous in other cases (e.g. in an address calculation, you should trap instead of potentially storing to a bogus location).

在x86的,不过,如果检测计算的序列中的整数溢出发生,需要把一个条件分支他们每个人之后,因为标志只是覆盖。 MIPS有一个添加指令,该指令将捕获签署溢出,无符号指令,从来没有陷阱。所以,整数异常检测和处理是少了很多标准化的。

On x86, though, detecting if integer overflow happened during a sequence of calculations requires putting a conditional branch after every one of them, because flags are just overwritten. MIPS has an add instruction that will trap on signed overflow, and an unsigned instruction that never traps. So integer exception detection and handling is a lot less standardized.

整数除法没有产生NaN或天道酬勤结果的选项,所以这是有道理的,它的工作这样

Integer division doesn't have the option of producing NaN or Inf results, so it makes sense for it to work this way.

整数除法产生的任何整数位模式将是错误的,因为它会重新present特定的有限值。

Any integer bit pattern produced by integer division will be wrong, because it will represent a specific finite value.

然而,在x86,转换一个超出范围的浮点值整数, cvtsd2si 或者如果浮点无效异常的掩码生产的整不定的价值任何类似的转换指令。该值是除符号位全零。即 INT_MIN

However, on x86, converting an out-of-range floating point value to integer with cvtsd2si or any similar conversion instruction produces the "integer indefinite" value if the "floating-point invalid" exception is masked. The value is all-zero except the sign bit. i.e. INT_MIN.

(请参见英特尔的手册,在 86 标签维基。

(See the Intel manuals, links in the x86 tag wiki.

这篇关于在哪些平台上通过过零触发一个浮点异常呢整数除法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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