diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-12-21 13:36:58 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-12-21 13:36:58 -0500 |
| commit | e4d5bf73e4ad9dc51f923a32903011edfeae2908 (patch) | |
| tree | b0bd959d50bd7bf0c9751780288adb06a943787a /src/stdlib/bigint.h | |
| parent | f07c07e551b89b66b14936ae4573e656fbd7afb6 (diff) | |
Divide by zero checks
Diffstat (limited to 'src/stdlib/bigint.h')
| -rw-r--r-- | src/stdlib/bigint.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/stdlib/bigint.h b/src/stdlib/bigint.h index 5bf33629..2936f2cd 100644 --- a/src/stdlib/bigint.h +++ b/src/stdlib/bigint.h @@ -103,6 +103,7 @@ MACROLIKE Int_t Int$divided_by(Int_t x, Int_t y) { // https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf const int64_t D = (x.small >> 2L); const int64_t d = (y.small >> 2L); + if unlikely (d == 0) fail("Cannot divide by zero"); int64_t q = D / d, r = D % d; q -= (r < 0L) * (2L * (d > 0L) - 1L); if likely (q == (int32_t)q) return (Int_t){.small = (q << 2L) | 1L}; @@ -116,6 +117,7 @@ MACROLIKE Int_t Int$modulo(Int_t x, Int_t y) { // https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf const int64_t D = (x.small >> 2L); const int64_t d = (y.small >> 2L); + if unlikely (d == 0) fail("Cannot divide by zero"); int64_t r = D % d; r -= (r < 0L) * (2L * (d < 0L) - 1L) * d; return (Int_t){.small = (r << 2L) | 1L}; |
