Overflow Detections

image.png

$(¬(sign_A⊕sign_B))∧(sign_A⊕new_{sign})$

Dealing with Overflow

Some languages (比如C) ignore overflow: Let compilers or programmers decide what to do

Other language (e.g., Ada, Fortran) require an exception to be raised. If the hardware detects overflow/underflow, it raises an exception and invokes the exception handler.

Internal overflow (instruction overflow): handle immediately

External overflow:

Why not HW traps on Overflow

  1. Performance Impact (check overflow, traps are expensive to handle, rare)
  2. Exception handling increases the complexity of high performance implementations (pipelining, out-of-order execution).
  3. Flexibility: some applications want the warp around behavior of the unsigned integers

The potential performance impact and added complexity of hardware traps have led to alternative approaches that rely on software or compiler support for overflow detection and handling.

由于检查每次运算是否溢出会带来明显的性能下降和系统复杂度增加,并且大部分情况下并不需要处理溢出问题,所以硬件层面通常不采用对溢出触发陷阱的方式,而是依赖软件或编译器在需要的时候才通过特殊检测手段来处理溢出问题。这样既能保持高性能,又能在必要时提供对溢出的恰当处理。

Overflow Handling in RISC-V