Skip to content

Refactor Int32 to use Int32Native pattern matching Int64 architecture#12828

Merged
Simn merged 5 commits intohaxe-numericfrom
copilot/sub-pr-12825
Mar 17, 2026
Merged

Refactor Int32 to use Int32Native pattern matching Int64 architecture#12828
Simn merged 5 commits intohaxe-numericfrom
copilot/sub-pr-12825

Conversation

Copy link
Contributor

Copilot AI commented Mar 17, 2026

Reworks haxe.Int32 to follow the same Native delegation pattern established for Int64 in #12826, verifying the system's flexibility for future numeric type unification.

Architecture

  • haxe/Int32.hx → thin @:op abstract over Int32Native, no platform logic. Documents platform representations (native 32-bit on C++/JVM/HL, clamped on JS/PHP/Python/Lua, Float auto-promoted on Neko).
  • haxe/numeric/Int32Native.hx → cross-platform abstract(Int) with masking (JS |0, PHP sign-extend, Python modular arithmetic, Lua clampInt32). Also used by Neko, where NekoVM auto-promotes values exceeding 31-bit range to Float while preserving correct 32-bit arithmetic.
  • haxe/numeric/Int32Direct.hx → shared no-masking implementation for targets where Int is natively 32-bit. Uses ~x+1 for negation to avoid a CPPIA unary-minus overflow bug.
  • {jvm,hl,cpp}/_std/haxe/numeric/Int32Native.hx → single-line overrides: typedef Int32Native = Int32Direct.
  • {jvm,hl,cpp}/_std/haxe/numeric/Int64Native.hx → removed unnecessary @:coreApi(check = Off) (private types get the Implied check which silently ignores failures).

Tests

Example

// Int32.hx is now a pure delegation layer:
@:op(A + B) private static inline function add(a:Int32, b:Int32):Int32
    return Int32Native.add(a, b);

// Int32Direct provides the shared native implementation (C++/JVM/HL):
public static inline function add(a:Int32Direct, b:Int32Direct):Int32Direct
    return cast((a : Int) + (b : Int));

// Int32Native (cross-platform) provides masking for scripting targets:
public static inline function add(a:Int32Native, b:Int32Native):Int32Native
    return clamp((a : Int) + (b : Int));

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits March 17, 2026 07:28
- Create std/haxe/numeric/Int32Native.hx as cross-platform abstract(Int) with masking
- Create JVM, HL, C++, Neko target overrides in _std directories
- Refactor std/haxe/Int32.hx to thin @:op layer delegating to Int32Native
- Add comprehensive TestInt32 unit test class
- All tests passing on interp, JS, JVM, Neko, Python, PHP

Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Copilot AI changed the title [WIP] Unify numeric types API across all targets Refactor Int32 to use Int32Native pattern matching Int64 architecture Mar 17, 2026
Copilot AI requested a review from Simn March 17, 2026 07:30
Copilot finished work on behalf of Simn March 17, 2026 07:30
…docs and @:coreApi

- Move #if !cpp to cover entire testArrayIndexWithInt32 function (avoids utest "no assertions")
- Remove Neko-specific Int32Native override (cross-platform version works correctly via Float auto-promotion)
- Remove doc comments from target-specific Int32Native files (docs belong on main Int32 type)
- Remove unnecessary @:coreApi(check = Off) from private types (Implied check handles these)
- Add platform representation docs to main Int32.hx

Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Copilot AI requested a review from Simn March 17, 2026 08:14
Copilot finished work on behalf of Simn March 17, 2026 08:14
@Simn
Copy link
Member

Simn commented Mar 17, 2026

@copilot

  1. Let's really remove the unnecessary @:coreApi from Int64Native files. I know it's technically more about the parent PR but that's fine.
  2. I'm noticing that we have code duplication for Int32Native on C++, HL and JVM now. Since we are typedeffing it anyway, let's add std/haxe/numeric/Int32Direct.hx with the common implementation and then point all 3 targets to that. I don't insist on the "Direct" suffix so if you have a better name use that instead!

The only failing target is now C++ which we'll have to look into still. We first need to identify what actually changed because this was fine before. Along those lines, I've checked our GitHub issues list for int32 occurrences and paste the links here so we can check if all these cases are covered by the tests:

#10780
#10943
#10995
#5938
#8879

@Simn
Copy link
Member

Simn commented Mar 17, 2026

@copilot Please continue!

…ove @:coreApi from Int64Native, add issue regression tests

Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 17, 2026

@copilot Please continue!

Done in 6dd7641:

  1. @:coreApi(check = Off) removed from all three Int64Native target files (JVM, HL, C++).

  2. std/haxe/numeric/Int32Direct.hx created with the shared no-masking implementation. All three target overrides (cpp/jvm/hl/_std/haxe/numeric/Int32Native.hx) are now a single line each: typedef Int32Native = Int32Direct.

  3. C++ CPPIA bug fixed: the root cause was that CPPIA's unary minus -(x:Int) can return a value wider than 32 bits for INT_MIN, giving 2147483648 instead of wrapping to -2147483648. Int32Direct.neg now uses ~x+1 (the same formula the original Int32 used), which stays within the native 32-bit range on CPPIA.

  4. Regression tests added for the linked issues:

    • testMulOverflow_Issue10780: multiplication overflow on C++ (257 * 0x01010101)
    • testXorWithDynamicShift_Issue10995: XOR with dynamic shift on Python
    • testOrEqualsNotClamping_Issue5938: |= not clamping on Python

    Issues [CPP] Int/Int32 inconsistent results for shift left operations #10943 and [cpp] wierd addition results upon int overflow #8879 relate to compiler/optimizer behaviour rather than the library implementation, so no specific tests were added for those.

Copilot finished work on behalf of Simn March 17, 2026 11:47
@Simn Simn marked this pull request as ready for review March 17, 2026 12:16
@Simn Simn merged commit 338bed4 into haxe-numeric Mar 17, 2026
61 checks passed
@Simn Simn deleted the copilot/sub-pr-12825 branch March 17, 2026 12: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