79252394

Date: 2024-12-04 18:50:19
Score: 0.5
Natty:
Report link

Originally, I had gone with the option suggested in the comments of checking which compiler was being used, then typedef whatever that particular compiler called its 128-bit integer. Then, I found a much better answer, entirely by accident, when I was looking up something else.

The answer is to switch to the C++23 standard, which allows me to use the new _BitInt keyword to just make up an integer of whatever size I want, and let the compiler handle it. So, now my code looks something like this:

using value_t = int64_t;
int constexpr max_bits_math = std::numeric_limits<value_t>::digits * 2;
using math_value_t = _BitInt(max_bits_math);
int constexpr bit_shift = 8;

value_t operator*(value_t lhs, value_t rhs) {
    math_value_t answer = static_cast<math_value_t>(lhs) * static_cast<math_value_t>(rhs);
    return static_cast<value_t>(answer >> bit_shift);
}

Yes, I know that not a lot of compilers support _BitInt yet, because it's so new. Then again, I'm still in the very early stages of this project, so I'm confident support will be more widespread when I'm ready to release.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: HiddenWindshield