C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Integer overflow arithmetic with exceptions

From: (wrong string) Åberg <haberg_1_at_[hidden]>
Date: Fri, 25 Apr 2025 10:09:54 +0200
> On 24 Apr 2025, at 22:43, Tiago Freire <tmiguelf_at_[hidden]> wrote:
>
> A fused multiply add has been considered as well as a fused multiply double add (adds 2 numbers to the multiplication result instead of just one).
> While they make sense from a computer science perspective, hardware support for those operations is lacking.
> Thus, I opted to exclude them from the proposal in order to avoid it be shot down for adding too much unsound fluff. I feel it needs a better justification, such as a form of optimization that would otherwise be too cumbersome to achieve without compiler support.
> I'm open to add them, but you need to make your case.

With the function
  std::tuple<Word, Word> mul(Word a, Word b, Word c);
I can multiply vectors a, b into r by
  Word cr = 0; // Carry

  for (size_t i = 0; i < a.size(); ++i)
    for (size_t j = 0; j < b.size(); ++j) {
      Word p;
      std::tie(p, cr) = mul(a[i], b[j], cr);
      r[i+j] += p;
    }

  if (cr != 0)
    r[n-1] = cr; // Index is (a.size()-1)+(b.size()-1) + 1 = n - 1.

Without the carry in the C++ supplied function, I would probably have to write it anyway to simplify implementation.

Received on 2025-04-25 08:10:09