简体   繁体   English

如何为浮点异常生成陷阱?

[英]How are traps generated for floating point exceptions?

I want to know which code and files in the glibc library are responsible for generating traps for floating point exceptions when traps are enabled. 我想知道glibc库中的哪些代码和文件负责在启用陷阱时为浮点异常生成陷阱。

Currently, GCC for RISC-V does not trap floating point exceptions. 目前,RISC-V的GCC不会捕获浮点异常。 I am interested in adding this feature. 我有兴趣添加此功能。 So, I was looking at how this functionality is implemented in GCC for x86. 所以,我正在研究如何在GCC for x86中实现此功能。

I am aware that we can trap signals as described in this [question] ( Trapping floating-point overflow in C ) but I want to know more details about how it works. 我知道我们可以捕获信号,如[问题]中描述的那样( 在C中捕获浮点溢出 ),但我想了解更多关于它是如何工作的细节。

I went through files in glibc/math which according to me are in some form responsible for generating traps like 我浏览了glibc / math中的文件,根据我的说法,它以某种形式负责生成陷阱

fenv.h
feenablxcpt.c
fegetexpect.c
feupdateenv.c

and many other files starting with fe . 以及以fe开头的许多其他文件。

All these files are also present in glibc for RISC-V. 所有这些文件也存在于glibc中,用于RISC-V。 I am not able to figure out how glibc for x86 is able to generate traps. 我无法弄清楚x86的glibc如何能够生成陷阱。

These traps are usually generated by the hardware itself , at the instruction set architecture (ISA) level. 这些陷阱通常由硬件本身指令集架构 (ISA)级别生成。 In particular on x86-64. 特别是在x86-64上。

I want to know which code and files in the glibc library are responsible for generating traps for floating point exceptions when traps are enabled. 我想知道glibc库中的哪些代码和文件负责在启用陷阱时为浮点异常生成陷阱。

So there are no such file. 所以没有这样的文件。 However, the operating system kernel (notably with signal(7) -s on Linux...) is translating traps to something else. 但是, 操作系统 内核 (特别是Linux上的signal(7) -s)正在将陷阱转换为其他内容。

Please read Operating Systems: Three Easy Pieces for more. 请阅读操作系统:三个简易件 And study the x86-64 instruction set in details. 并详细研究x86-64指令集。

A more familiar example is the integer division by zero. 更熟悉的例子是整数除以零。 On most hardware, that produces a machine trap (or machine exception ), handled by the kernel. 在大多数硬件上,它会产生由内核处理的机器陷阱(或机器异常 )。 On some hardware (IIRC, PowerPC ), its gives -1 as a result and sets some bit in a status register. 在某些硬件(IIRC, PowerPC )上,它给出-1作为结果并在状态寄存器中设置一些位。 Further machine code could test that bit. 更多机器代码可以测试该位。 I believe that the GCC compiler would, in some cases and with some optimizations disabled, generate such a test after every division. 我相信GCC编译器在某些情况下会禁用某些优化 ,在每次除法后都会产生这样的测试。 But it is not required to do that. 但并不要求这样做。

The C language (read n1570 , which practically is the C11 standard) has defined the notion of undefined behavior to handle such situations the most quickly and simply possible. C语言(读取n1570 ,实际上是C11标准)定义了未定义行为的概念,以最快速和简单的方式处理这种情况。 Read Lattner's What every C programmer should know about undefined behavior blog. 阅读Lattner's 每个C程序员应该了解未定义的行为博客。

Since you mention RISC-V , read about the RISC philosophy of the previous century, and be aware that designing out-of-order and super-scalar processors requires a lot of engineering efforts. 自提到RISC-V以来,请阅读上个世纪的RISC理念,并注意设计无序超标量处理器需要大量的工程工作。 My guess is that if you invest as much R&D (that means tens of billions of US$ or €) as Intel -or, to a lesser extent, AMD- did on x86-64 into a RISC-V chip, you could get comparable performance to current x86-64 processors. 我的猜测是,如果你投入尽可能多的研发费用(意味着数百亿美元或者欧元)作为英特尔 - 或者在较小程度上,AMD-在x86-64上投入到RISC-V芯片中,你可以获得可比性对当前x86-64处理器的性能。 Notice that SPARC or PowerPC (or perhaps ARM) chips are RISC-like, and their best processors are nearly comparable in performance to Intel chips but got probably ten times less R&D investment than what Intel put in its microprocessors. 请注意,SPARC或PowerPC(或ARM)芯片类似于RISC,其最佳处理器的性能几乎与英特尔芯片相当,但其研发投入可能比英特尔在微处理器中的投入少十倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM