| 139 | |
| 140 | template <typename Int> |
| 141 | [[nodiscard]] bool ModuloWithOverflowGeneric(Int u, Int v, Int* out) { |
| 142 | if (v == 0) { |
| 143 | *out = Int{}; |
| 144 | return true; |
| 145 | } |
| 146 | // INT_MIN % -1 is undefined behavior (the quotient is not representable) and |
| 147 | // traps on some targets, but the mathematical result is 0. |
| 148 | if constexpr (std::is_signed_v<Int>) { |
| 149 | constexpr auto kMin = std::numeric_limits<Int>::min(); |
| 150 | if (u == kMin && v == -1) { |
| 151 | *out = 0; |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | *out = u % v; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Define non-generic versions of the above so as to benefit from automatic |
| 160 | // integer conversion, to allow for mixed-type calls such as |
nothing calls this directly
no outgoing calls
no test coverage detected