Skip to content

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

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

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

Conversation

@forfudan
Copy link
Copy Markdown
Owner

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.

@forfudan forfudan merged commit afa1704 into main Mar 27, 2026
26 of 27 checks passed
@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.

1 participant