Skip to content

[runtime] Fix 8, 16, and 32 bit ToInt and ToUint conversions for all cases#363

Merged
Hans-Halverson merged 1 commit into
masterfrom
fix-int-conversion
Jun 19, 2026
Merged

[runtime] Fix 8, 16, and 32 bit ToInt and ToUint conversions for all cases#363
Hans-Halverson merged 1 commit into
masterfrom
fix-int-conversion

Conversation

@Hans-Halverson

@Hans-Halverson Hans-Halverson commented Jun 19, 2026

Copy link
Copy Markdown
Owner

Summary

The ToInt8, ToUint8, ToInt16, ToUint16, ToInt32, and ToUint32 conversions shared a bug where f64_number.abs().floor() as i64 was prematurely truncating values outside the i64 range. Rewrite these conversions to fix this bug and to be simpler, clearer, and more efficient.

Improvements:

  • Fast path covers all smis - even negative values can be directly cast
  • Non-smi fast path if rounded f64 is in range for the target int
  • Less common finite check and modulo are only applied for non-smis out of the target int range
  • For signed conversions we can first cast to an unsigned int of the same width then reinterpret as a signed int

Tests

  • Added thorough (LLM-generated) tests for each of these conversions

@Hans-Halverson Hans-Halverson merged commit 596ca7a into master Jun 19, 2026
9 checks passed
@Hans-Halverson Hans-Halverson deleted the fix-int-conversion branch June 19, 2026 09:11
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