Skip to content

Initialize guest floating-point state explicitly#137

Merged
encounter merged 2 commits into
decompals:mainfrom
N7Alpha:fix/mwcc-codegen-divergence
Jul 12, 2026
Merged

Initialize guest floating-point state explicitly#137
encounter merged 2 commits into
decompals:mainfrom
N7Alpha:fix/mwcc-codegen-divergence

Conversation

@N7Alpha

@N7Alpha N7Alpha commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

It looks like you need to set a flag in a control register at least on my M4 MacBook Air. My only validation is this fixed an issue in a vibe code decomp I had for 7 functions. Late in the decomp I wanted to switch to this project from Wine because I was losing my mind with Wine, but I lost 7 matched functions. GPT 5.6 root-caused the fix and matched all 7 wibo with no regressions on >3200 functions.

GPT 5.6

Bug

wibo inherited the host thread's floating-point environment when entering a Win32 guest. On macOS under Rosetta, the x87 control word was 0x037f, which selects 64-bit extended precision. Win32 processes and threads start with 0x027f, which selects 53-bit precision.

This changed an internal optimizer comparison in Metrowerks mwccarm.exe. The compiler consequently emitted a semantically equivalent switch comparison tree in a different order, preventing byte-identical reproduction of ARM objects that match the original ROM under Wine.

Root cause

The x87 precision-control bit was the causal hidden-state difference. Immediately before guest entry, wibo reported 0x037f. Loading only 0x027f—without changing heap placement, allocation contents, or MXCSR—made the canonical Wine and wibo objects byte-identical.

Fix

Initialize the floating-point environment whenever a TIB is installed for a guest thread:

  • reset the x87 unit;
  • load the Win32 x87 control word 0x027f;
  • load the default MXCSR control state 0x1f80.

Doing this in per-thread setup covers the initial executable, DLL/TLS callbacks, and threads created through CreateThread.

A new fixture captures floating-point state from a process-attach TLS callback and a created thread. It checks exact x87 control state and the MXCSR control bits while allowing exception status flags that may be raised during loader work.

@encounter
encounter force-pushed the fix/mwcc-codegen-divergence branch from dd835d9 to 61cc9c1 Compare July 12, 2026 23:22
@encounter encounter changed the title [MacOS-Rosetta] Initialize guest floating-point state Initialize guest floating-point state explicitly Jul 12, 2026
@encounter

Copy link
Copy Markdown
Member

Thanks! I verified this as a bug and rewrote it to match the existing codebase style.

@encounter
encounter merged commit e8f4795 into decompals:main Jul 12, 2026
12 checks passed
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