Skip to content

fix mul_with_overflow for unsigned types larger than 16 bytes#59

Merged
rfourquet merged 5 commits into
rfourquet:masterfrom
EdsterG:fix_mul_with_overflow
Jan 7, 2026
Merged

fix mul_with_overflow for unsigned types larger than 16 bytes#59
rfourquet merged 5 commits into
rfourquet:masterfrom
EdsterG:fix_mul_with_overflow

Conversation

@EdsterG

@EdsterG EdsterG commented Jan 7, 2026

Copy link
Copy Markdown
Contributor

Following #48, I believe mul_with_overflow can also use checked_smul_int/checked_umul_int, starting from v1.11.

@EdsterG
EdsterG force-pushed the fix_mul_with_overflow branch from e55ef6c to cfe3300 Compare January 7, 2026 02:47

@rfourquet rfourquet left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM, with a small inline comment to resolve.
Could you please also add a small changelog line to the README, like

### v0.3.7

*  fix `mul_with_overflow` for unsigned types larger than 16 bytes ([#59](https://github.com/rfourquet/BitIntegers.jl/pull/59))

And bump the version number. So that i can release the fix after merging.

Comment thread test/runtests.jl Outdated
Comment thread test/runtests.jl
for X in XInts
if sizeof(X) != 3 # bug with [U]Int24, cf. Julia issue #34288
for X in (XInts..., Int256, UInt256)
if VERSION >= v"1.8" || sizeof(X) != 3 # bug with [U]Int24, cf. Julia issue #34288

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this seems to have been fixed earlier, but it doesn't matter because we don't test all the versions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version I put here was based on these two comments: JuliaLang/julia#34288 (comment) and JuliaLang/julia#45460 (comment). I was unable to test with 1.7 locally because Juliaup +1.7 crashes on my machine.

Comment thread src/BitIntegers.jl Outdated
mul_with_overflow(x::T, y::T) where {T<:XBS} = sizeof(T) >= 16 ? broken_mul_with_overflow(x, y) : checked_smul_int(x, y)
mul_with_overflow(x::T, y::T) where {T<:XBU} = sizeof(T) >= 16 ? broken_mul_with_overflow(x, y) : checked_umul_int(x, y)
mul_with_overflow(x::T, y::T) where {T<:XBS} =
(sizeof(T) > 16 && VERSION < v"1.11-") ? broken_mul_with_overflow(x, y) : checked_smul_int(x, y)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I hadn't noticed you changed sizeof(T) >= 16 to sizeof(T) > 16. Maybe we should be conservative here, and keep >=. In older versions, on some systems, the intrinsic might fail on 128 bit integers?

Also: the code in base/checked.jl suggests that these intrinsics have been prone to bugs, and perhaps some of them still exist on some systems. Let's merge this PR anyway, and wait for possible bug reports to see if more tweaks are needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, this change was to keep it consistent with #48. The two were inconsistent pre #48, however I don't have any context beyond that.

@rfourquet
rfourquet merged commit 1f3b3f0 into rfourquet:master Jan 7, 2026
6 checks passed
@EdsterG
EdsterG deleted the fix_mul_with_overflow branch January 7, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants