Skip to content

[integer] Rewrite and fix type conversion from all integral scalar types#188

Merged
forfudan merged 8 commits intomainfrom
update
Mar 26, 2026
Merged

[integer] Rewrite and fix type conversion from all integral scalar types#188
forfudan merged 8 commits intomainfrom
update

Conversation

@forfudan
Copy link
Copy Markdown
Owner

@forfudan forfudan commented Mar 24, 2026

Fix broken conversion from Int128 and add proper support for all integral types (Int128, Int256, UInt128, UInt256, platform-sized Int/UInt).

The old code cast everything through UInt64, silently truncating wide integers. The Int.MIN special-case was also fragile.

Now from_integral_scalar dispatches per dtype at comptime:

  • Small unsigned types → single word
  • UInt64 → 2-word split
  • UInt128/UInt256 → loop with bit ops
  • Signed ≤64-bit → unsigned subtraction for magnitude
  • Int128/Int256 → division-based chunk extraction (right-shift on signed wide ints was broken)
  • Platform Int/UInt → size_of dispatch

Removed standalone from_uint64 and from_uint128 (folded into from_integral_scalar). Updated sqrt call sites accordingly.

Added tests for Int.MIN/MAX edge cases, Int128/Int256 MIN/MAX, UInt128.MAX, UInt256, platform-sized types, and zero across all types.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes BigInt construction from 128-bit scalar integer types (Int128/UInt128) by updating the generic scalar conversion path and extending the conversion test suite accordingly.

Changes:

  • Extend test_from_integral_scalar to cover UInt128 and Int128 (including MIN/MAX).
  • Rework BigInt.from_integral_scalar word-extraction logic to handle wider scalar types beyond 64-bit.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/bigint/test_bigint_conversion.mojo Adds coverage for UInt128/Int128 scalar-to-BigInt conversions (small/large, MIN/MAX).
src/decimo/bigint/bigint.mojo Updates scalar conversion implementation to extract 32-bit limbs from wider scalar types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/decimo/bigint/bigint.mojo Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/bigint/test_bigint_conversion.mojo Outdated
@forfudan forfudan changed the title [integer][fix] Fix errors in type conversion from Int128 [integer][fix] Rewrite and fix type conversion from all integral scalar types Mar 26, 2026
@forfudan forfudan changed the title [integer][fix] Rewrite and fix type conversion from all integral scalar types [integer] Rewrite and fix type conversion from all integral scalar types Mar 26, 2026
@forfudan forfudan merged commit 9df8853 into main Mar 26, 2026
9 checks passed
@forfudan forfudan deleted the update branch March 26, 2026 16:42
@forfudan forfudan restored the update branch March 27, 2026 09:02
@forfudan forfudan deleted the update branch March 27, 2026 16:47
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