Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,25 @@ if(EXISTS "${INTAKE_PYTHON}")
add_test(NAME cpu_aot_x87_div COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/cpu/x87_div.py"
check "$<TARGET_FILE:cpu_x87_harness>" "$<TARGET_FILE:cpu_x87_div>"
"$<TARGET_FILE:cpu_x87_div_lifted>" ${x87_div_control_files})
cpu_asm_fixture(cpu_x87_sub x87_sub)
# The lifted DLL and seven mutated controls that the check requires to mismatch.
set(x87_sub_control_files "")
foreach(variant IN ITEMS lifted pc64 double nansign reverse cancel nosticky memquiet)
set(x87_sub_fixture "${CMAKE_CURRENT_BINARY_DIR}/cpu_x87_sub_${variant}.cpp")
add_custom_command(OUTPUT "${x87_sub_fixture}"
COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/cpu/x87_sub.py"
emit "$<TARGET_FILE:cpu_x87_sub>" "${x87_sub_fixture}" ${variant}
DEPENDS cpu_x87_sub tests/cpu/x87_sub.py tests/cpu/x87_div.py tests/cpu/x87_mul.py
tests/cpu/x87_add.py tools/ffx_version_query.py tools/x87_lower.py tools/x87_soft.h
tools/cpu_ranges.py tools/cpu_leaf_aot.py VERBATIM)
add_library(cpu_x87_sub_${variant} SHARED "${x87_sub_fixture}")
if(NOT variant STREQUAL "lifted")
list(APPEND x87_sub_control_files "$<TARGET_FILE:cpu_x87_sub_${variant}>")
endif()
endforeach()
add_test(NAME cpu_aot_x87_sub COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/cpu/x87_sub.py"
check "$<TARGET_FILE:cpu_x87_harness>" "$<TARGET_FILE:cpu_x87_sub>"
"$<TARGET_FILE:cpu_x87_sub_lifted>" ${x87_sub_control_files})
cpu_asm_fixture(cpu_create create_context)
set(create_fixture "${CMAKE_CURRENT_BINARY_DIR}/cpu_create_lifted.cpp")
add_custom_command(OUTPUT "${create_fixture}"
Expand Down
8 changes: 5 additions & 3 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,17 @@ The local Bink DLL imports ordinal 1 from XAudio2_9.dll. A read-only check confi

## x87 in the shared lowering

`lower(..., x87=True)` in `tools/ffx_version_query.py` accepts an opt-in x87 leaf-function profile. The default (`x87=False`) still rejects every x87 instruction, so existing callers are unchanged. The profile accepts `fld` and `fstp` with an m32, m64 or m80 memory operand, `faddp st(i), st` with `i` from 1 to 7 (`faddp st(0), st` is rejected), the multiplication forms `fmul m32`, `fmul m64`, `fmul st, st(i)`, `fmul st(i), st` and `fmulp st(i), st`, and the same five division forms for each of `fdiv` and `fdivr`, with `fdivp` and `fdivrp` (a popping form with `st(0)` as the destination is rejected). A function enters with control word 0x027F (53-bit precision, round to nearest-even, all exceptions masked), TOP 0, all eight registers empty and a clear status word, and it must return with the stack empty. The stack depth before each instruction is fixed at translation time (`tools/x87_lower.py`), so each register becomes a C++ slot and no TOP exists at run time. Translation rejects any other x87 instruction, including control-word, status-word, environment and `wait` access; a ninth push; a pop or `faddp` without enough values; depths that differ where control flow joins; an x87 instruction that is not reachable from the entry through direct edges; and a nonempty stack at a return, a call, an indirect branch or an external edge.
`lower(..., x87=True)` in `tools/ffx_version_query.py` accepts an opt-in x87 leaf-function profile. The default (`x87=False`) still rejects every x87 instruction, so existing callers are unchanged. The profile accepts `fld` and `fstp` with an m32, m64 or m80 memory operand, and five forms of each of `fadd`, `fsub`, `fsubr`, `fmul`, `fdiv` and `fdivr`: an m32 or m64 operand, `st, st(i)`, `st(i), st`, and the popping form (`faddp`, `fsubp`, `fsubrp`, `fmulp`, `fdivp`, `fdivrp`) with `st(i), st` for `i` from 1 to 7. A popping form with `st(0)` as the destination is rejected. A function enters with control word 0x027F (53-bit precision, round to nearest-even, all exceptions masked), TOP 0, all eight registers empty and a clear status word, and it must return with the stack empty. The stack depth before each instruction is fixed at translation time (`tools/x87_lower.py`), so each register becomes a C++ slot and no TOP exists at run time. Translation rejects any other x87 instruction, including control-word, status-word, environment and `wait` access; a ninth push; a pop or `faddp` without enough values; depths that differ where control flow joins; an x87 instruction that is not reachable from the entry through direct edges; and a nonempty stack at a return, a call, an indirect branch or an external edge.

The helpers in `tools/x87_soft.h` compute in integers and do not use host floating point. A register holds the full 80-bit encoding. m80 loads and stores copy all 80 bits, including NaN payloads, denormals, pseudo-denormals and unnormals. m32 and m64 loads convert exactly and quiet a signaling NaN. A sum rounds to 53 significand bits within the extended exponent range; a result below that range is denormalized first and then rounded at the same bit positions. The sum rounds again when it is stored to m32 or m64. NaN operands follow the x87 column of Intel SDM Vol. 1, Table 4-7: a QNaN beats an SNaN, and otherwise the larger significand wins. When two NaNs have equal significands, the positive one wins in either operand order. This tie rule was measured, not taken from the table. Infinities of opposite sign, unnormals, pseudo-infinities and pseudo-NaNs give the indefinite. An exact zero sum of nonzero addends is +0, and -0 + -0 is -0. The status word, exceptions and the other rounding modes are not modelled. Their absence is safe only because the profile rejects every instruction that could observe or change them.

`cpu_aot_x87_add` builds 16 exports from generated MASM source (`tests/cpu/x87_add.asm`, from `tests/cpu/x87_add.py asm`) and translates them. The exports cover the nine load/store width pairs, additions stored to each width, and a three-value chain that uses `faddp st(2), st`. A native-only harness (`tests/cpu/x87_harness.asm`) saves the caller's whole x87 state with `fnsave` (environment and register values), which also leaves TOP 0 and an empty stack. It then loads control word 0x027F, calls the export, records the control, status and tag words the export left, and restores the caller's state with `frstor`. A harness check enters each export with a live caller value (pi with all 64 significand bits) in the register the export reuses and control word 0x0F7F loaded. It requires the value bytes, the control word, TOP and the tags to come back unchanged. A control harness that keeps only the environment (`fnstenv`/`fldenv`) must lose the value around every export. Native and translated exports receive the same input bytes and run through that harness. The check compares each output block and the 64 guard bytes on each side of the input and output against an independent `Fraction` model. It requires every export to return with control word 0x027F, TOP 0 and all tags empty. The 8085 cases include all pairs of 34 special encodings, halfway sums that separate 53-bit from 64-bit precision, double rounding through m64, sums beyond the binary64 range, extended-range overflow and underflow, denormal results, cancellation, NaN ties and 700 random pairs. Six mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, m80 loads through binary64, a dropped sticky bit, the first NaN operand always winning, and -0 for an exact cancellation. On an AMD Ryzen 7 7700 (Windows 11 build 26200), every case matched in the native and translated DLLs, and the controls gave 1089, 3845, 1485, 48, 58 and 18 mismatches. The check also confirms that the default lowering rejects `fld` and that the profile rejects 16 sequences outside it. Only `faddp st(1), st` and `faddp st(2), st` are exercised. Intel CPUs have not been measured. Run `cmake --build --preset dev --target cpu_x87_add_lifted` and `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_add$' -V`.

`cpu_aot_x87_mul` checks the multiplication forms the same way, through the same harness, guards and live-value check, with 12 exports and 14951 cases. The product of the two 64-bit significands is exact in 128 bits and rounds like a sum. Its sign is the exclusive or of the operand signs, including for zero and infinite results, and infinity times zero gives the indefinite. An m32 or m64 operand of `fmul` converts exactly but keeps a signaling NaN signaling, so the NaN rules see the operand's own kind. A plain `fld` quiets it on load. The cases cover every pair of the 34 special encodings, each special m80 value times each of 17 m32 and 18 m64 special encodings, products that are exact halfway cases at 53 bits, products whose upper 64 bits sit exactly on a tie so that only the low 64 bits decide, `x * 1` rounding, products beyond the binary64 range, extended overflow and underflow thresholds, denormal results, the square through `fmul st, st(0)`, a three-value chain through `fmulp st(2), st`, a product followed by an add, and random operands. Six mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, dropping the low 64 bits of the product, the first operand's sign, infinity times zero giving infinity, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 4836, 8059, 62, 4409, 73 and 36 mismatches. The check also confirms that translation rejects nine sequences, including `fmulp st(0), st`, `fimul`, `fsub`, the still unsupported `fadd m32`, and multiplications without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_mul$' -V`.
`cpu_aot_x87_mul` checks the multiplication forms the same way, through the same harness, guards and live-value check, with 12 exports and 14951 cases. The product of the two 64-bit significands is exact in 128 bits and rounds like a sum. Its sign is the exclusive or of the operand signs, including for zero and infinite results, and infinity times zero gives the indefinite. An m32 or m64 operand of `fmul` converts exactly but keeps a signaling NaN signaling, so the NaN rules see the operand's own kind. A plain `fld` quiets it on load. The cases cover every pair of the 34 special encodings, each special m80 value times each of 17 m32 and 18 m64 special encodings, products that are exact halfway cases at 53 bits, products whose upper 64 bits sit exactly on a tie so that only the low 64 bits decide, `x * 1` rounding, products beyond the binary64 range, extended overflow and underflow thresholds, denormal results, the square through `fmul st, st(0)`, a three-value chain through `fmulp st(2), st`, a product followed by an add, and random operands. Six mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, dropping the low 64 bits of the product, the first operand's sign, infinity times zero giving infinity, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 4836, 8059, 62, 4409, 73 and 36 mismatches. The check also confirms that translation rejects nine sequences, including `fmulp st(0), st`, `fimul`, `fcomp`, `fiadd`, and multiplications without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_mul$' -V`.

`cpu_aot_x87_div` checks the division forms in the same way, with 17 exports and 23939 cases. `fdivr` and `fdivrp` divide the source by the destination. Each export's decoded x87 instructions must match the forms its sequence names, so an assembler that exchanged the `fdiv` and `fdivr` register encodings would fail before the run. The helper divides the 64-bit significands into a 64-bit quotient and folds a nonzero remainder into a sticky bit. That is enough to round to 53 bits, and the result goes through the same rounding as the sum and the product. Zero by zero and infinity by infinity give the indefinite. A finite nonzero value by zero gives a signed infinity (the masked divide-by-zero response). A finite value by infinity, and zero by a finite value, give a signed zero. The cases cover every pair of the 34 special encodings and every special m80 value against the m32 and m64 special encodings, in both orders. They also cover exact quotients halfway between 53-bit neighbours, quotients whose 64-bit significand ends exactly on a tie so that only the remainder decides, `x / x` through `fdiv st, st(0)`, quotients beyond the binary64 range, the extended overflow and underflow thresholds, denormal results, two three-value chains through `fdivp st(1), st`, `fdivp st(2), st` and `fdivrp st(1), st`, and random operands. Seven mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, an ignored remainder, the indefinite for a finite value by zero, `fdivr` in the `fdiv` order, an unsigned zero quotient, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 8785, 14373, 253, 602, 6829, 551 and 54 mismatches. Translation still rejects `fdivp st(0), st`, `fdivrp st(0), st`, `fidiv`, `fidivr`, `fsqrt`, `fprem`, `fadd m32` and divisions without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_div$' -V`.
`cpu_aot_x87_div` checks the division forms in the same way, with 17 exports and 23939 cases. `fdivr` and `fdivrp` divide the source by the destination. Each export's decoded x87 instructions must match the forms its sequence names, so an assembler that exchanged the `fdiv` and `fdivr` register encodings would fail before the run. The helper divides the 64-bit significands into a 64-bit quotient and folds a nonzero remainder into a sticky bit. That is enough to round to 53 bits, and the result goes through the same rounding as the sum and the product. Zero by zero and infinity by infinity give the indefinite. A finite nonzero value by zero gives a signed infinity (the masked divide-by-zero response). A finite value by infinity, and zero by a finite value, give a signed zero. The cases cover every pair of the 34 special encodings and every special m80 value against the m32 and m64 special encodings, in both orders. They also cover exact quotients halfway between 53-bit neighbours, quotients whose 64-bit significand ends exactly on a tie so that only the remainder decides, `x / x` through `fdiv st, st(0)`, quotients beyond the binary64 range, the extended overflow and underflow thresholds, denormal results, two three-value chains through `fdivp st(1), st`, `fdivp st(2), st` and `fdivrp st(1), st`, and random operands. Seven mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, an ignored remainder, the indefinite for a finite value by zero, `fdivr` in the `fdiv` order, an unsigned zero quotient, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 8785, 14373, 253, 602, 6829, 551 and 54 mismatches. Translation still rejects `fdivp st(0), st`, `fdivrp st(0), st`, `fidiv`, `fidivr`, `fsqrt`, `fprem`, `fiadd` and divisions without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_div$' -V`.

`cpu_aot_x87_sub` checks `fsub`, `fsubr`, `fsubp`, `fsubrp` and the `fadd` forms besides `faddp` in the same way, with 22 exports and 35023 cases. `fsubr` and `fsubrp` subtract the destination from the source. The helper computes `a - b` as `a + (-b)`, but it selects a NaN or invalid operand before negating, so a NaN result keeps its own sign. `x - x` is +0, and -0 - +0 is -0. The cases reuse the addition pairs of `cpu_aot_x87_add`, and again with the second operand negated. They also cover `x - x` and `x + x` through `st(0)`, m32 and m64 operands against the special encodings, `float` and `double` differences, a three-value chain through `fsubp st(2), st` and `fsubrp st(1), st`, and random operands. Seven mutated translations must each mismatch: 64-bit precision, the binary64 exponent range, negating a NaN subtrahend, `fsubr` in the `fsub` order, -0 for an exact cancellation, a dropped sticky bit, and quieting memory operands before the NaN rules. On the AMD Ryzen 7 7700 every case matched in the native and translated DLLs, and the controls gave 15680, 24289, 2022, 7753, 777, 108 and 81 mismatches. Translation still rejects `fsubp st(0), st`, `fsubrp st(0), st`, `fisub`, `fisubr`, `fchs`, `fabs`, `fcom` and arithmetic without enough stack values. Run `ctest --preset dev --no-tests=error -R '^cpu_aot_x87_sub$' -V`.

## Mapping an image without LoadLibrary

Expand Down
2 changes: 1 addition & 1 deletion tests/cpu/x87_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def attempt(hex_bytes, *, x87=True, calls=None, exits=None):
rejected = {
"the profile off (default)": (attempt(f"{fld} {fstp} c3", x87=False), "unsupported query operation"),
"fld st(1)": (attempt(f"{fld} d9 c1 de c1 de c1 {fstp} c3"), "unsupported x87 operand"),
"fadd st, st(1)": (attempt(f"{fld} {fld} d8 c1 de c1 {fstp} c3"), "fadd"),
"fcom st(1)": (attempt(f"{fld} {fld} d8 d1 de c1 {fstp} c3"), "fcom"),
"fnstsw ax": (attempt(f"{fld} df e0 {fstp} c3"), "unsupported x87 operation"),
"fldcw": (attempt(f"d9 29 {fld} {fstp} c3"), "fldcw"),
"fninit": (attempt(f"{fld} db e3 {fstp} c3"), "unsupported x87 operation"),
Expand Down
2 changes: 1 addition & 1 deletion tests/cpu/x87_div.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def attempt(hex_bytes):
"fidivr m16": (attempt(f"{fld} de 39 {fstp} c3"), "unsupported x87 operation"),
"fsqrt": (attempt(f"{fld} d9 fa {fstp} c3"), "unsupported x87 operation"),
"fprem": (attempt(f"{fld} {fld} d9 f8 {fstp} {fstp} c3"), "unsupported x87 operation"),
"fadd m32": (attempt(f"{fld} d8 01 {fstp} c3"), "unsupported x87 operation"),
"fiadd m32": (attempt(f"{fld} da 01 {fstp} c3"), "unsupported x87 operation"),
}
for case, (message, needle) in rejected.items():
if message is None or needle not in message:
Expand Down
4 changes: 2 additions & 2 deletions tests/cpu/x87_mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ def attempt(hex_bytes):
"fmulp with one value": (attempt(f"{fld} de c9 {fstp} c3"), "x87 stack underflow"),
"fimul m32": (attempt(f"{fld} da 09 {fstp} c3"), "unsupported x87 operation"),
"fimul m16": (attempt(f"{fld} de 09 {fstp} c3"), "unsupported x87 operation"),
"fsub st, st(1)": (attempt(f"{fld} {fld} d8 e1 {fstp} {fstp} c3"), "unsupported x87 operation"),
"fadd m32": (attempt(f"{fld} d8 01 {fstp} c3"), "unsupported x87 operation"),
"fcomp st(1)": (attempt(f"{fld} {fld} d8 d9 {fstp} c3"), "unsupported x87 operation"),
"fiadd m32": (attempt(f"{fld} da 01 {fstp} c3"), "unsupported x87 operation"),
}
for case, (message, needle) in rejected.items():
if message is None or needle not in message:
Expand Down
Loading