C++ Logo

std-proposals

Advanced search

[std-proposals] 回复: 回复: Proposal: Integer division/remainder/division-remainder functions with exception throwing behavior

From: None <a1343922569_at_[hidden]>
Date: Wed, 30 Apr 2025 17:49:46 +0000
I believe my point still stands for the following reasons (in my opinion):
1. Regarding branch prediction and performance:
Modern CPU pipelines are typically 14-20 stages deep. When a branch prediction fails, the entire pipeline needs to be flushed and refilled, causing a significant penalty of 20-30 clock cycles. This is particularly impactful in high-performance computing scenarios.
2. About implementation efficiency:
The proposed solution leverages CPU's native division-by-zero exception mechanism, which is fundamentally different from manual checking:
    No branch instructions in the main execution path.
    Zero runtime overhead for the non-zero case (>99.9% scenarios).
    Exception handling path is completely out of line.
    Modern CPUs have dedicated hardware for exception handling.
3. On "trivial implementation":
While users can implement basic checks, they cannot efficiently utilize CPU's built-in mechanisms without non-portable assembly code. A standardized solution would:
    Provide consistent, portable behavior.
    Enable compiler optimizations impossible with user-level code.

I believe these technical merits warrant consideration for standardization. Would you be interested in more detailed analysis of these aspects?

________________________________
From: Jan Schultke <janschultke_at_[hidden]>
Sent: Thursday, May 1, 2025 1:35 AM
To: std-proposals_at_[hidden]rg <std-proposals_at_[hidden]>
Cc: None <a1343922569_at_[hidden]>
Subject: Re: [std-proposals] 回复: Proposal: Integer division/remainder/division-remainder functions with exception throwing behavior

To clarify on the second point, you can already express a division by zero check with a throw on failure using an if statement. The language already has all the tools to let you do it.

If it was much faster to handle the CPU trap instead, compilers would likely already detect this pattern and transform the branch into an unconditional division. The core language already has all the tools, so this seems more like a quality-of-implementation problem than anything.

A much more interesting proposal would be to change division by zero globally so that it throws instead of being UB. Maybe this could be a conditionally supported feature. Maybe it's something that should be implemented in a compiler first. Maybe it's already implemented somewhere, not sure.


Received on 2025-04-30 17:49:51