#!/usr/bin/env python3 """Original ARM, independent math and native x86 SIMD/FP verification. Reviewed single B/H/S/D/Q transfers, MOVI D/2D and paired S/D/Q transfers are tested. Single-operation leaf mini-functions expose each result in a fresh complete state; mixed chains additionally compare final state and RAM. Representative block cases execute the original RET and native block exit ABI. Scalar numerical blocks use pinned original/core outcomes and actual Rust runtime completion; unsupported controls remain distinct from successful runs. """ import argparse from collections import Counter from contextlib import ExitStack import ctypes from dataclasses import dataclass import hashlib import importlib.util import json import mmap import math import multiprocessing import os import shutil import socket from pathlib import Path import random import signal import sys import struct import subprocess import tempfile import time from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM, CS_ARCH_X86, CS_MODE_64 from unicorn import Uc, UcError, UC_ARCH_ARM64, UC_ARCH_X86, UC_MODE_ARM, UC_MODE_64, UC_HOOK_CODE, UC_ERR_FETCH_UNMAPPED from unicorn import arm64_const as arm from unicorn import x86_const as intel from unicorn import __version__ as unicorn_version from differential import (ROOT, BASE, MASK, State, states, executable, initialize_arm, capture_arm, state_dict, seed_extended_state, parse_fault_record, move) from evidence import retain_native_evidence, write_evidence from simd_fixtures import FAMILIES as FIXTURE_FAMILIES import simd_fixture_runner RET = 0xd65f03c0 STOP = 0x220000100000 RAM_BYTES = 0x4000 LEAF_ABI = 'x86_64-sysv-guest-state-v3-direct-ram' BLOCK_ABI = 'x86_64-sysv-guest-state-v2-block-exit-direct-ram' WIDTHS = (1, 2, 4, 8, 16) MODES = ('unsigned', 'unscaled', 'pre', 'post') PAIR_WIDTHS = (4, 8, 16) PAIR_MODES = ('offset', 'pre', 'post') class BlockExit(ctypes.Structure): _fields_ = [('next_pc', ctypes.c_uint64), ('kind', ctypes.c_uint64)] def digest(data): return hashlib.sha256(data).hexdigest() def memory_encoding(width, loading, rt, rn, offset, mode): if width not in WIDTHS or not 0 <= rt < 32 or not 0 <= rn < 32: raise ValueError('invalid SIMD memory width/register') size = 0 if width == 16 else width.bit_length() - 1 opcode = int(loading) | (2 if width == 16 else 0) if mode == 'unsigned': if offset < 0 or offset % width or offset // width > 4095: raise ValueError('SIMD unsigned offset outside scaled12 range') return (size << 30) | 0x3d000000 | (opcode << 22) | ((offset // width) << 10) | (rn << 5) | rt if mode not in MODES or not -256 <= offset <= 255: raise ValueError('SIMD signed offset outside signed9 range') indexing = {'unscaled': 0, 'post': 1, 'pre': 3}[mode] return (size << 30) | 0x3c000000 | (opcode << 22) | ((offset & 511) << 12) | (indexing << 10) | (rn << 5) | rt def pair_encoding(width, loading, rt, rt2, rn, offset, mode): if width not in PAIR_WIDTHS or any(not 0 <= register < 32 for register in (rt, rt2, rn)): raise ValueError('invalid SIMD pair width/register') if mode not in PAIR_MODES or offset % width or not -64 <= offset // width <= 63: raise ValueError('SIMD pair offset outside signed7 scaled range') opcode = {4: 0, 8: 1, 16: 2}[width] index = {'post': 1, 'offset': 2, 'pre': 3}[mode] return (opcode << 30) | 0x2c000000 | (index << 23) | (int(loading) << 22) | (((offset // width) & 127) << 15) | (rt2 << 10) | (rn << 5) | rt def pair_op(width, loading, rt, rt2, rn, offset, mode): return {'kind': 'pair', 'width': width, 'loading': loading, 'rt': rt, 'rt2': rt2, 'rn': rn, 'offset': offset, 'mode': mode, 'word': pair_encoding(width, loading, rt, rt2, rn, offset, mode)} def movi_encoding(two_lanes, mask, rt): if not 0 <= mask < 256 or not 0 <= rt < 32: raise ValueError('invalid MOVI mask/register') return 0x2f00e400 | (int(two_lanes) << 30) | ((mask >> 5) << 16) | ((mask & 31) << 5) | rt def memory_op(width, loading, rt, rn, offset, mode): return {'kind': 'memory', 'width': width, 'loading': loading, 'rt': rt, 'rn': rn, 'offset': offset, 'mode': mode, 'word': memory_encoding(width, loading, rt, rn, offset, mode)} def movi_op(two_lanes, mask, rt): return {'kind': 'movi', 'two_lanes': two_lanes, 'mask': mask, 'rt': rt, 'word': movi_encoding(two_lanes, mask, rt)} def canonical_form(operation): if operation['kind'] == 'movi': return 'MOVI_2D' if operation['two_lanes'] else 'MOVI_D' if operation['kind'] == 'pair': return f"{'LDP' if operation['loading'] else 'STP'}_{operation['width']}_{operation['mode']}" return f"{'LDR' if operation['loading'] else 'STR'}_{operation['width']}_{operation['mode']}" def programs(): result = [] for two_lanes in (False, True): for mask in range(256): for rt in (0, 31): result.append({'kind': 'exhaustive_movi', 'operations': [movi_op(two_lanes, mask, rt)], 'flags': 16}) for two_lanes in (False, True): for rt in range(32): result.append({'kind': 'all_movi_registers', 'operations': [movi_op(two_lanes, 0xa5, rt)], 'flags': 1}) roles = ((0, 0), (31, 31), (7, 7), (31, 0), (0, 31), (31, 29)) for width in WIDTHS: for loading in (False, True): for mode in MODES: offsets = (0, width, 4095 * width) if mode == 'unsigned' else (-256, -1, 0, 255) for offset in offsets: for rt, rn in roles: result.append({'kind': 'memory_boundaries', 'operations': [memory_op(width, loading, rt, rn, offset, mode)], 'flags': 16}) for rt in range(32): result.append({'kind': 'all_vector_registers', 'operations': [memory_op(width, loading, rt, (rt + 1) % 32, 0, mode)], 'flags': 1}) for rn in range(32): result.append({'kind': 'all_base_registers', 'operations': [memory_op(width, loading, rn, rn, 16, 'pre' if rn % 2 else 'post')], 'flags': 1}) # Bounded mixed-width/mask chains observe scalar upper-zero, full Q storage, # cross-bank aliases, independent writebacks and original aligned SP recovery. for rt in (0, 31): result.extend([ {'kind': 'mixed_chain', 'operations': [movi_op(True, 0xa5, rt), memory_op(16, False, rt, 0, 0, 'unsigned'), memory_op(1, True, rt, 0, 0, 'unsigned'), memory_op(16, False, rt, 0, 16, 'unsigned')], 'flags': 16}, {'kind': 'mixed_chain', 'operations': [movi_op(True, 0xff, rt), memory_op(8, True, rt, 0, 0, 'unsigned'), memory_op(16, False, rt, 0, 16, 'unsigned')], 'flags': 16}, {'kind': 'stack_chain', 'operations': [movi_op(True, 0x81, rt), memory_op(16, False, rt, 31, -16, 'pre'), memory_op(16, True, rt, 31, 16, 'post')], 'flags': 16}, {'kind': 'mixed_chain', 'operations': [movi_op(True, 0x80, rt), movi_op(False, 0x01, rt), memory_op(16, False, rt, 0, 0, 'unsigned')], 'flags': 16}, ]) blocks = [] for width in WIDTHS: for loading in (False, True): for mode in MODES: blocks.append({'kind': 'block', 'operations': [memory_op(width, loading, 31, 0, 16, mode)], 'flags': 16}) for two_lanes in (False, True): blocks.append({'kind': 'block', 'operations': [movi_op(two_lanes, 0xa5, 31)], 'flags': 16}) return result, blocks def pair_programs(): result, blocks = [], [] roles = ((0, 1, 0), (31, 0, 31), (0, 31, 31), (31, 30, 0), (7, 7, 7), (0, 0, 0), (31, 31, 31), (0, 7, 7)) for width in PAIR_WIDTHS: for loading in (False, True): for mode in PAIR_MODES: for scaled in (-64, -1, 0, 1, 63): for rt, rt2, rn in roles: if loading and rt == rt2: continue result.append({'kind': 'pair_boundaries', 'operations': [pair_op(width, loading, rt, rt2, rn, scaled * width, mode)], 'flags': 16}) for register in range(32): for position in (0, 1): rt, rt2 = (register, (register + 11) % 32) if position == 0 else ((register + 11) % 32, register) result.append({'kind': 'pair_all_vector_positions', 'operations': [pair_op(width, loading, rt, rt2, (register + 1) % 32, 0, mode)], 'flags': 1}) result.append({'kind': 'pair_all_bases', 'operations': [pair_op(width, loading, register, (register + 1) % 32, register, 2 * width, mode)], 'flags': 1}) if not loading: result.append({'kind': 'pair_duplicate_stores', 'operations': [pair_op(width, False, register, register, register, 0, mode)], 'flags': 1}) blocks.append({'kind': 'pair_block', 'operations': [pair_op(width, loading, 0, 31, 0, 16, mode)], 'flags': 16}) frame = max(16, 2 * width) result.append({'kind': 'pair_stack_chain', 'operations': [movi_op(True, 0xa5, 0), movi_op(True, 0x5a, 31), pair_op(width, False, 0, 31, 31, -frame, 'pre'), memory_op(1, True, 0, 31, 0, 'unsigned'), pair_op(width, True, 0, 31, 31, frame, 'post')], 'flags': 16}) result.append({'kind': 'pair_mixed_chain', 'operations': [pair_op(width, False, 0, 31, 0, 0, 'offset'), memory_op(16, True, 7, 0, 0, 'unsigned'), pair_op(16, False, 7, 7, 0, 64, 'offset'), pair_op(width, True, 0, 31, 0, 0, 'offset')], 'flags': 16}) return result, blocks def mathematical(operations, initial, memory, ram_address): state = State.from_buffer_copy(initial) ram = bytearray(memory) for operation in operations: rt = operation['rt'] if operation['kind'] == 'movi': # Per-byte independent expansion, separate from compiler bit masks. lane = sum((255 if operation['mask'] & (1 << byte) else 0) << (8 * byte) for byte in range(8)) state.v[rt][:] = [lane, lane if operation['two_lanes'] else 0] continue rn, width, mode, offset = (operation[k] for k in ('rn', 'width', 'mode', 'offset')) base = state.sp if rn == 31 else state.x[rn] if rn == 31 and base % 16: raise ValueError('oracle received misaligned incoming SP') effective = (base + (0 if mode == 'post' else offset)) & MASK position = effective - ram_address registers = [rt, operation['rt2']] if operation['kind'] == 'pair' else [rt] if operation['loading'] and len(registers) == 2 and registers[0] == registers[1]: raise ValueError('constrained-unpredictable duplicate SIMD pair load') if not 0 <= position <= len(ram) - width * len(registers): raise ValueError('oracle memory access outside owned RAM') if operation['loading']: values = [int.from_bytes(ram[position + slot * width:position + (slot + 1) * width], 'little') for slot in range(len(registers))] for register, value in zip(registers, values): state.v[register][:] = [value & MASK, value >> 64] else: values = [state.v[register][0] | (state.v[register][1] << 64) for register in registers] for slot, value in enumerate(values): ram[position + slot * width:position + (slot + 1) * width] = value.to_bytes(16, 'little')[:width] if mode in ('pre', 'post'): if rn == 31: state.sp = (base + offset) & MASK else: state.x[rn] = (base + offset) & MASK return state, bytes(ram) def validate_manifest(manifest, source, native, block, target_cpu=None, guest_base=BASE): if target_cpu is not None and manifest.get("target_cpu") != target_cpu: raise AssertionError("SIMD manifest CPU requirements differ from selection") consumed = manifest['source_bytes'] if block else manifest['guest_instructions'] * 4 if (not isinstance(consumed, int) or not 0 < consumed <= len(source) or consumed % 4 or (not block and consumed != len(source))): raise AssertionError('SIMD manifest has an invalid consumed source prefix') compiled_source = source[:consumed] if (manifest['schema_version'] != 1 or manifest['abi'] != (BLOCK_ABI if block else LEAF_ABI) or manifest['guest_base'] != guest_base or manifest['source_sha256'] != digest(compiled_source) or manifest['native_sha256'] != digest(native) or manifest['native_bytes'] != len(native) or manifest['verified'] is not False): raise AssertionError('SIMD manifest does not bind original/native bytes and ABI') mapping = manifest['source_map'] if ([row[0] for row in mapping] != list(range(guest_base, guest_base + consumed, 4)) or any(not 0 <= row[1] < len(native) for row in mapping) or any(a[1] >= b[1] for a, b in zip(mapping, mapping[1:]))): raise AssertionError('SIMD source map omits or merges original instructions') return [row[1] for row in mapping] def compile_batch(cli, directory, functions, index, block=False, cpu_target="baseline", target_id="x86_64-baseline", guest_base=BASE, allowed_mnemonics=None): if block and len(functions) != 1: raise ValueError('A block compilation batch must have exactly one entry') words, entries = [], [] for function in functions: entries.append(len(words)) words.extend(operation['word'] for operation in function['operations']) words.append(numerical_return_word(function['operations'][0]) if len(function['operations']) == 1 else RET) source = struct.pack('<' + 'I' * len(words), *words) disassembly = list(Cs(CS_ARCH_ARM64, CS_MODE_ARM).disasm(source, BASE)) if len(disassembly) != len(words) or any(i.mnemonic not in ({'ldr', 'str', 'ldur', 'stur', 'ldp', 'stp', 'movi', 'ret', 'and', 'bic', 'orr', 'orn', 'eor', 'bsl', 'bit', 'bif', 'add', 'sub', 'abs', 'neg', 'mov', 'cmeq', 'cmgt', 'cmge', 'cmhi', 'cmhs', 'cmlt', 'cmle', 'cmtst', 'dup', 'ins', 'smov', 'umov', 'ext', 'rev16', 'rev32', 'rev64', 'zip1', 'zip2', 'uzp1', 'uzp2', 'trn1', 'trn2', 'tbl', 'tbx', 'shl', 'sshr', 'ushr', 'srshr', 'urshr', 'ssra', 'usra', 'srsra', 'ursra', 'sli', 'sri', 'shrn', 'shrn2', 'rshrn', 'rshrn2', 'sshll', 'sshll2', 'ushll', 'ushll2', 'sxtl', 'sxtl2', 'uxtl', 'uxtl2', 'smin', 'smax', 'umin', 'umax', 'sminp', 'smaxp', 'uminp', 'umaxp', 'sminv', 'smaxv', 'uminv', 'umaxv', 'addv', 'addp', 'saddlv', 'uaddlv', 'cnt', 'clz', 'cls', 'rbit', 'mvn', 'not', 'sqadd', 'uqadd', 'sqsub', 'uqsub', 'sqabs', 'sqneg', 'suqadd', 'usqadd', 'sqxtn', 'sqxtn2', 'uqxtn', 'uqxtn2', 'sqxtun', 'sqxtun2', 'mul', 'mla', 'mls', 'smull', 'smull2', 'umull', 'umull2', 'smlal', 'smlal2', 'umlal', 'umlal2', 'smlsl', 'smlsl2', 'umlsl', 'umlsl2', 'saddl', 'saddl2', 'uaddl', 'uaddl2', 'ssubl', 'ssubl2', 'usubl', 'usubl2', 'saddw', 'saddw2', 'uaddw', 'uaddw2', 'ssubw', 'ssubw2', 'usubw', 'usubw2', 'sabd', 'uabd', 'saba', 'uaba', 'sabdl', 'sabdl2', 'uabdl', 'uabdl2', 'sabal', 'sabal2', 'uabal', 'uabal2', 'shadd', 'uhadd', 'srhadd', 'urhadd', 'shsub', 'uhsub', 'addhn', 'addhn2', 'raddhn', 'raddhn2', 'subhn', 'subhn2', 'rsubhn', 'rsubhn2', 'saddlp', 'uaddlp', 'sadalp', 'uadalp', 'xtn', 'xtn2', 'shll', 'shll2', 'mvni', 'fmov', 'pmul', 'pmull', 'pmull2', 'sqdmulh', 'sqrdmulh', 'sqdmull', 'sqdmull2', 'sqdmlal', 'sqdmlal2', 'sqdmlsl', 'sqdmlsl2', 'sshl', 'ushl', 'srshl', 'urshl', 'sqshl', 'uqshl', 'sqrshl', 'uqrshl', 'sqshlu', 'fcsel', 'fabs', 'fneg', 'sqshrn', 'sqshrn2', 'sqrshrn', 'sqrshrn2', 'uqshrn', 'uqshrn2', 'uqrshrn', 'uqrshrn2', 'sqshrun', 'sqshrun2', 'sqrshrun', 'sqrshrun2', 'sdot', 'udot', 'sqrdmlah', 'sqrdmlsh', 'fadd', 'fsub', 'fmul', 'fdiv', 'mrs', 'msr', 'fmadd', 'fmsub', 'fnmadd', 'fnmsub', 'fmla', 'fmls', 'fcvt', 'fcvtl', 'fcvtl2', 'fcvtn', 'fcvtn2', 'fcvtxn', 'fcvtxn2', 'fcvtns', 'fcvtnu', 'fcvtps', 'fcvtpu', 'fcvtms', 'fcvtmu', 'fcvtzs', 'fcvtzu', 'fcvtas', 'fcvtau', 'scvtf', 'ucvtf', 'fjcvtzs', 'fcmp', 'fcmpe', 'fccmp', 'fccmpe', 'facge', 'facgt', 'fcmeq', 'fcmge', 'fcmgt', 'fcmle', 'fcmlt', 'fmax', 'fmin', 'fmaxnm', 'fminnm', 'fmaxp', 'fminp', 'fmaxnmp', 'fminnmp', 'fmaxv', 'fminv', 'fmaxnmv', 'fminnmv', 'frintn', 'frintp', 'frintm', 'frintz', 'frinta', 'frintx', 'frinti', 'frint32z', 'frint32x', 'frint64z', 'frint64x', 'fsqrt', 'frecpe', 'frsqrte', 'frecpx', 'urecpe', 'ursqrte', 'frecps', 'frsqrts', 'fnmul', 'fabd', 'faddp', 'fmulx', 'fmlal', 'fmlal2', 'fmlsl', 'fmlsl2', 'bfcvt', 'bfcvtn', 'bfcvtn2', 'bfdot', 'bfmlalb', 'bfmlalt', 'bfmmla', 'fcadd', 'fcmla'} if allowed_mnemonics is None else set(allowed_mnemonics) | {'ret'}) for i in disassembly): raise AssertionError('Independent decoder declined SIMD fixture') path = directory / f'program-{index}.arm64' path.write_bytes(source) output = directory / f'native-{index}' manifest = json.loads(subprocess.check_output([str(cli), 'compile-block' if block else 'compile-leaf', str(path), '--guest-base', hex(guest_base), '--output-dir', str(output), '--cpu-target', cpu_target], timeout=60)) native = (output / 'native.bin').read_bytes() offsets = validate_manifest(manifest, source, native, block, target_id, guest_base) consumed = manifest['source_bytes'] if block else len(source) compiled_source = source[:consumed] if not block: for entry, function in zip(entries, functions): if native[offsets[entry + len(function['operations'])]] != 0xc3: raise AssertionError('Independent mini-function has no exact host RET boundary') return {'functions': functions, 'source': source, 'compiled_source': compiled_source, 'native': native, 'entries': entries, 'offsets': [offsets[entry] for entry in entries], 'block': block, 'compiler': manifest['compiler_source_sha256'],'manifest':manifest,'instruction_offsets':offsets, 'receipt': {'functions': len(functions), 'source_bytes': consumed, 'source_sha256': digest(compiled_source), 'input_source_bytes': len(source), 'input_source_sha256': digest(source), 'native_bytes': len(native), 'native_sha256': digest(native), 'source_map_sha256': digest(json.dumps(manifest['source_map']).encode()), 'block': block, 'target_cpu': manifest['target_cpu']}} def initial_case(function, index, flags, ram_address, original_ram, templates): initial = State.from_buffer_copy(templates[flags % 16]) initial.nzcv = flags << 28 initial.x[30] = STOP seed_extended_state(initial, (index << 8) | flags) ram = bytearray(original_ram) memories = [operation for operation in function['operations'] if operation['kind'] in ('memory', 'pair')] if len(memories) == 1: operation = memories[0] rn, offset, mode, width = (operation[k] for k in ('rn', 'offset', 'mode', 'width')) position = (0x600, 0x601, 0xfff, 0xff9, 0x1000, 0x1001)[flags % 6] if rn == 31: base = (ram_address + position - (0 if mode == 'post' else offset)) & ~15 else: base = ram_address + position - (0 if mode == 'post' else offset) if rn == 31: initial.sp = base else: initial.x[rn] = base effective = base + (0 if mode == 'post' else offset) position = effective - ram_address value = (0, (1 << 128) - 1, 0x80000000000000018000000000000001, 0x0123456789abcdeffedcba9876543210)[flags % 4] for slot in range(2 if operation['kind'] == 'pair' else 1): # Distinct neighboring values prove both pair positions and order. element = value if slot == 0 else value ^ 0xfedcba98765432100123456789abcdef ram[position + slot * width:position + (slot + 1) * width] = element.to_bytes(16, 'little')[:width] elif memories: initial.x[0] = ram_address + 0x600 initial.sp = ram_address + 0x1000 return initial, bytes(ram) def execute_batches(batches, pipe): cohort_counts = {'single': Counter(), 'pair': Counter()} corpus_by_cohort = {'single': hashlib.sha256(), 'pair': hashlib.sha256()} pair_forms, pair_registers, pair_bases, duplicate_stores = set(), {}, {}, {} observed_forms, masks, vectors, bases = set(), {False: set(), True: set()}, set(), set() movi_destinations = {False: set(), True: set()} templates = states(all_flags=True) original_ram = random.Random(0x51d5c0de).randbytes(RAM_BYTES) with mmap.mmap(-1, RAM_BYTES, prot=mmap.PROT_READ | mmap.PROT_WRITE) as ram: marker = ctypes.c_char.from_buffer(ram) address = ctypes.addressof(marker) del marker for batch_index, batch in enumerate(batches): cohort = batch['cohort'] counts = cohort_counts[cohort] machine = Uc(UC_ARCH_ARM64, UC_MODE_ARM) machine.mem_map(BASE, ((len(batch['source']) + 4095) // 4096) * 4096) machine.mem_write(BASE, batch['source']) machine.mem_map(address, RAM_BYTES) with executable(batch['native']) as native_address: for function_index, (function, entry, offset) in enumerate(zip(batch['functions'], batch['entries'], batch['offsets'])): original_entry = BASE + entry * 4 native = (ctypes.CFUNCTYPE(BlockExit, ctypes.POINTER(State)) if batch['block'] else ctypes.CFUNCTYPE(None, ctypes.POINTER(State)))(native_address + offset) for flags_index in range(function['flags']): flags = flags_index if function['flags'] == 16 else (batch_index + function_index) % 16 initial, memory = initial_case(function, batch_index * 512 + function_index, flags, address, original_ram, templates) expected, expected_ram = mathematical(function['operations'], initial, memory, address) initialize_arm(machine, initial) machine.mem_write(address, memory) boundary = expected.x[30] if batch['block'] else original_entry + len(function['operations']) * 4 try: machine.emu_start(original_entry, boundary, count=len(function['operations']) + int(batch['block'])) except UcError as error: if not batch['block'] or error.errno != UC_ERR_FETCH_UNMAPPED or machine.reg_read(arm.UC_ARM64_REG_PC) != boundary: raise if machine.reg_read(arm.UC_ARM64_REG_PC) != boundary: raise AssertionError('Original SIMD program missed its exact boundary') original = capture_arm(machine, initial) original_memory = bytes(machine.mem_read(address, RAM_BYTES)) actual = State.from_buffer_copy(initial) ram[:] = memory returned = native(ctypes.byref(actual)) if (bytes(original) != bytes(expected) or bytes(actual) != bytes(expected) or original_memory != expected_ram or ram[:] != expected_ram): raise AssertionError(json.dumps({'function': function, 'flags': flags, 'initial': state_dict(initial), 'expected': state_dict(expected), 'original': state_dict(original), 'native': state_dict(actual), 'RAM_sha256': [digest(x) for x in (expected_ram, original_memory, ram[:])]}, indent=2)) if batch['block']: if (returned.next_pc, returned.kind) != (expected.x[30], 4): raise AssertionError('Native SIMD block did not return actual RET tuple') counts['native_block_exit_checks'] += 1 corpus_by_cohort[cohort].update(bytes(initial)) counts[function['kind']] += 1 for operation in function['operations']: form = canonical_form(operation) if cohort == 'pair' and operation['kind'] == 'pair': pair_forms.add(form) positions = pair_registers.setdefault(form, [set(), set()]) positions[0].add(operation['rt']) positions[1].add(operation['rt2']) pair_bases.setdefault(form, set()).add(operation['rn']) if not operation['loading'] and operation['rt'] == operation['rt2']: duplicate_stores.setdefault(form, set()).add(operation['rt']) elif cohort == 'single': observed_forms.add(form) vectors.add(operation['rt']) if operation['kind'] == 'movi': masks[operation['two_lanes']].add(operation['mask']) movi_destinations[operation['two_lanes']].add(operation['rt']) else: bases.add(operation['rn']) pipe.send({'progress': batch_index + 1, 'batches': len(batches), 'comparisons': sum(v for counter in cohort_counts.values() for k, v in counter.items() if k != 'native_block_exit_checks')}) if len(observed_forms) != 42 or any(values != set(range(256)) for values in masks.values()) or any(values != set(range(32)) for values in movi_destinations.values()) or vectors != set(range(32)) or bases != set(range(32)): raise AssertionError('Not all canonical forms/masks/register roles were observed') if (len(pair_forms) != 18 or any(position != set(range(32)) for positions in pair_registers.values() for position in positions) or any(values != set(range(32)) for values in pair_bases.values()) or len(duplicate_stores) != 9 or any(values != set(range(32)) for values in duplicate_stores.values())): raise AssertionError('Not every pair form/register position/base/duplicate-store role was observed') single, pair = cohort_counts['single'], cohort_counts['pair'] total = sum(v for k, v in single.items() if k != 'native_block_exit_checks') pair_total = sum(v for k, v in pair.items() if k != 'native_block_exit_checks') return {'three_way_register_state_comparisons': total, 'state_memory_comparisons': total, 'native_block_exit_checks': single.pop('native_block_exit_checks'), 'comparisons_by_kind': dict(single), 'pair_three_way_register_state_comparisons': pair_total, 'pair_state_memory_comparisons': pair_total, 'pair_native_block_exit_checks': pair.pop('native_block_exit_checks'), 'pair_comparisons_by_kind': dict(pair), 'total_three_way_register_state_comparisons': total + pair_total, 'canonical_forms_observed': sorted(observed_forms), 'pair_canonical_forms_observed': sorted(pair_forms), 'movi_masks_observed_per_width': {str(k): len(v) for k, v in masks.items()}, 'movi_destinations_observed_per_width': {str(k): sorted(v) for k, v in movi_destinations.items()}, 'vector_registers_observed': sorted(vectors), 'base_registers_observed': sorted(bases), 'pair_vector_positions_by_form': {form: [sorted(v) for v in positions] for form, positions in sorted(pair_registers.items())}, 'pair_base_registers_by_form': {form: sorted(v) for form, v in sorted(pair_bases.items())}, 'duplicate_store_registers_by_form': {form: sorted(v) for form, v in sorted(duplicate_stores.items())}, 'state_corpus_sha256': corpus_by_cohort['single'].hexdigest(), 'pair_state_corpus_sha256': corpus_by_cohort['pair'].hexdigest()} def worker(pipe, batches): try: if batches[0].get('cohort') in FIXTURE_FAMILIES: result = simd_fixture_runner.execute_batches(sys.modules[__name__], batches) elif batches[0].get('cohort') == 'scalar_numerical': result = execute_numerical_batches(batches) elif batches[0].get('cohort') == 'fp_registers': result = execute_fp_register_batches(batches) elif batches[0].get('cohort') in FULL_FRAME_NUMERICAL_COHORTS: family = FULL_FRAME_NUMERICAL_COHORTS[batches[0]['cohort']] result = execute_numerical_batches(batches, family['loader'](batches[0]['oracle_path'])) else: result = execute_register_batches(batches) if batches[0].get('cohort') in REGISTER_COHORTS else execute_batches(batches, pipe) pipe.send({'result': result}) except BaseException as error: pipe.send({'error': repr(error)}) finally: pipe.close() def bounded_cases(batches): context = multiprocessing.get_context('spawn') receive, send = context.Pipe(duplex=False) process = context.Process(target=worker, args=(send, batches)) process.start() send.close() try: deadline = time.monotonic() + 600 while True: if receive.poll(1): message = receive.recv() if 'progress' in message: print(f"SIMD {message['progress']}/{message['batches']}: {message['comparisons']} complete states+RAM", flush=True) continue break if process.exitcode is not None or time.monotonic() >= deadline: raise AssertionError(f'SIMD worker failed/timed out: {process.exitcode}') process.join(timeout=2) if process.exitcode != 0 or 'error' in message: raise AssertionError(f'SIMD worker failed: {message}, exit={process.exitcode}') return message['result'] finally: if process.is_alive(): process.kill() process.join() receive.close() def fault_cases(): cases = [] for width in WIDTHS: for loading in (False, True): for mode in MODES: offset = {'unsigned': 0, 'unscaled': -16, 'pre': -16, 'post': 16}[mode] for rn in (0, 31): cases.append({'width': width, 'loading': loading, 'mode': mode, 'offset': offset, 'rn': rn, 'protection': 'guard'}) if not loading: cases.append({'width': width, 'loading': loading, 'mode': mode, 'offset': offset, 'rn': 0, 'protection': 'readonly'}) for mode in ('unsigned', 'pre', 'post'): cases.append({'width': width, 'loading': loading, 'mode': mode, 'offset': -16 if mode == 'pre' else 16 if mode == 'post' else 0, 'rn': 31, 'protection': 'sp_alignment'}) for mode in MODES: cases.append({'width': 16, 'loading': True, 'mode': mode, 'offset': 0 if mode == 'unsigned' else -16, 'rn': 0, 'protection': 'crossing'}) return cases def pair_fault_cases(): cases = [] for width in PAIR_WIDTHS: for loading in (False, True): for mode in PAIR_MODES: for rn in (0, 31): cases.append({'pair': True, 'width': width, 'loading': loading, 'rt': 0, 'rt2': 31, 'rn': rn, 'mode': mode, 'offset': -16 if mode != 'post' else 16, 'protection': 'guard'}) # S/D post-indexed second-element boundary cannot have an # aligned incoming SP; pre/offset can compensate by -width. if rn != 31 or width == 16 or mode != 'post': cases.append({'pair': True, 'width': width, 'loading': loading, 'rt': 0, 'rt2': 31, 'rn': rn, 'mode': mode, 'offset': -width if mode != 'post' else 16, 'protection': 'pair_second'}) if rn == 31: cases.append({'pair': True, 'width': width, 'loading': loading, 'rt': 0, 'rt2': 31, 'rn': rn, 'mode': mode, 'offset': -16 if mode == 'pre' else 0, 'protection': 'sp_alignment'}) if not loading: cases.append({'pair': True, 'width': width, 'loading': False, 'rt': 0, 'rt2': 31, 'rn': 0, 'mode': mode, 'offset': -16 if mode != 'post' else 16, 'protection': 'readonly'}) for rt, rt2 in ((0, 31), (0, 0), (31, 31)): cases.append({'pair': True, 'width': width, 'loading': False, 'rt': rt, 'rt2': rt2, 'rn': 0, 'mode': mode, 'offset': -width if mode != 'post' else 16, 'protection': 'pair_readonly'}) return cases def declined_pair_encodings(): result = [] for width in PAIR_WIDTHS: for mode in PAIR_MODES: for register in (0, 7, 31): result.append({'reason': 'constrained_duplicate_load', 'word': pair_encoding(width, True, register, register, 0, 0, mode)}) for mode in PAIR_MODES: result.append({'reason': 'reserved_opc', 'word': (pair_encoding(16, True, 0, 1, 0, 0, mode) & ~(3 << 30)) | (3 << 30)}) for width in PAIR_WIDTHS: result.append({'reason': 'non_temporal_pair_outside_profile', 'word': pair_encoding(width, True, 0, 1, 0, 0, 'offset') & ~(3 << 23)}) return result def verify_declined_pairs(cli, directory): results = [] for index, case in enumerate(declined_pair_encodings()): path, output = directory / f'declined-{index}.arm64', directory / f'declined-{index}.native' path.write_bytes(struct.pack('<2I', case['word'], RET)) completed = subprocess.run([str(cli), 'compile-leaf', str(path), '--guest-base', hex(BASE), '--output-dir', str(output)], capture_output=True, text=True, timeout=30) if completed.returncode == 0 or (output / 'native.bin').exists(): raise AssertionError(f'Unsupported SIMD pair was emitted: {case}') results.append({**case, 'source_sha256': digest(path.read_bytes()), 'error': completed.stderr.strip()}) return {'declined_pair_inputs_before_execution': len(results), 'declined_pair_results': results} def validate_pair_fault_state(case, initial, native, original, first_element): """Keep staged native policy separate from exact Unicorn observations.""" staged = State.from_buffer_copy(initial) staged.x[7] = 99 if bytes(native) != bytes(staged): raise AssertionError('Failed pair committed a V destination, base or other state') if case['protection'] == 'sp_alignment': if original is not None: raise AssertionError('SP alignment policy must not claim original execution') return 'sp_policy_only' if original is None: raise AssertionError('Pair memory fault is missing original observation') expected_original = State.from_buffer_copy(staged) second_load = case['loading'] and case['protection'] == 'pair_second' if second_load: if len(first_element) != case['width']: raise AssertionError('First-load observation has incorrect byte extent') value = int.from_bytes(first_element, 'little') expected_original.v[case['rt']][:] = [value & MASK, value >> 64] if bytes(original) != bytes(expected_original): raise AssertionError('Original pair fault differs beyond its exact observed first-load effect') return 'unicorn_second_load_divergence' if second_load else 'original_native_full_state_equal' def verify_faults(cli, directory, compiler): spec = importlib.util.spec_from_file_location('simd_original_faults', ROOT / 'dev/memory-faults.py') faults = importlib.util.module_from_spec(spec) spec.loader.exec_module(faults) observer = directory / 'simd-fault-observer' source_path = ROOT / 'dev/native-memory-faults.c' subprocess.run(['clang', '-std=c11', '-Wall', '-Wextra', '-Werror', '-O2', str(source_path), '-o', str(observer)], check=True) observations = {'single': [], 'pair': []} partial_stores = 0 pair_classifications = Counter() for index, case in enumerate(fault_cases() + pair_fault_cases()): paired = case.get('pair', False) operation = (pair_op(case['width'], case['loading'], case['rt'], case['rt2'], case['rn'], case['offset'], case['mode']) if paired else memory_op(case['width'], case['loading'], 31, case['rn'], case['offset'], case['mode'])) source = struct.pack('<3I', move(2, 1, 7, 99), operation['word'], RET) path, output = directory / f'fault-{index}.arm64', directory / f'fault-{index}.native' path.write_bytes(source) manifest = json.loads(subprocess.check_output([str(cli), 'compile-leaf', str(path), '--guest-base', hex(BASE), '--output-dir', str(output)], timeout=30)) native = (output / 'native.bin').read_bytes() validate_manifest(manifest, source, native, False) if manifest['compiler_source_sha256'] != compiler: raise AssertionError('Fault compiler identity changed') pre_offset = case['offset'] if case['mode'] in ('unscaled', 'offset', 'pre') else 0 arguments = [str(observer), str(output / 'native.bin'), case['protection'], str(case['rn']), str(pre_offset)] if case['protection'] in ('pair_second', 'pair_readonly'): arguments.append(str(case['width'])) data = subprocess.check_output(arguments, timeout=10) (code_address, ram_address, page_size), initial, (number, native_pc, fault_address), actual, ram = parse_fault_record(data) observed_bytes = page_size * (2 if case['protection'] == 'pair_readonly' else 1) if len(ram) != observed_bytes or not number: raise AssertionError('SIMD fault observer did not report bounded fault RAM') native_offset = native_pc - code_address mapped = [guest for guest, offset in manifest['source_map'] if offset <= native_offset] if not mapped or mapped[-1] != BASE + 4: raise AssertionError('SIMD fault source map did not identify memory instruction') if case['protection'] == 'sp_alignment': expected = State.from_buffer_copy(initial) expected.x[7] = 99 expected_ram, original_faults = b'\xa5' * observed_bytes, None if number != signal.SIGILL: raise AssertionError('SIMD incoming SP check did not precede access') else: if number not in (signal.SIGSEGV, signal.SIGBUS): raise AssertionError('SIMD memory fault has unexpected host signal') expected, expected_ram, original_faults = faults.arm_fault(source, initial, ram_address, page_size, case['protection'] == 'readonly', second_readonly=case['protection'] == 'pair_readonly') if any(item['pc'] != BASE + 4 for item in original_faults): raise AssertionError('Original SIMD fault PC differs') base = initial.sp if case['rn'] == 31 else initial.x[case['rn']] effective = base + (0 if case['mode'] == 'post' else case['offset']) wanted_address = ram_address + page_size if case['protection'] in ('crossing', 'pair_second', 'pair_readonly') else effective if fault_address != wanted_address: raise AssertionError('SIMD host fault address differs') if ram != expected_ram or (not paired and bytes(actual) != bytes(expected)): raise AssertionError(f'SIMD fault changed V/GPR/base state or RAM: {case}') classification = None if paired: classification = validate_pair_fault_state(case, initial, actual, expected if original_faults is not None else None, b'\xa5' * case['width']) pair_classifications[classification] += 1 expect_partial = not case['loading'] and case['protection'] in ('pair_second', 'pair_readonly') changed = ram != b'\xa5' * observed_bytes if changed != expect_partial: raise AssertionError('Pair fault has unexpected first-element visibility') if expect_partial: first = (initial.v[case['rt']][0] | initial.v[case['rt']][1] << 64).to_bytes(16, 'little')[:case['width']] if ram[page_size - case['width']:page_size] != first: raise AssertionError('Second-element fault did not retain exact first store') partial_stores += 1 observations['pair' if paired else 'single'].append({**case, **({'native_fault_policy_verified': True} if paired else {'passed': True}), 'source_sha256': digest(source), 'native_sha256': digest(native), 'guest_fault_pc': BASE + 4, 'native_fault_offset': native_offset, 'native_signal': number, 'original_faults': original_faults, **({'fault_state_classification': classification, 'original_native_full_state_equal': bytes(actual) == bytes(expected) if original_faults is not None else None, 'initial_state_hex': bytes(initial).hex(), 'native_state_hex': bytes(actual).hex(), 'original_state_hex': bytes(expected).hex() if original_faults is not None else None, 'native_ram_sha256': digest(ram), 'original_ram_sha256': digest(expected_ram) if original_faults is not None else None} if paired else {})}) if pair_classifications != Counter({'original_native_full_state_equal': 88, 'unicorn_second_load_divergence': 16, 'sp_policy_only': 18}): raise AssertionError(f'Unexpected pair fault observation classes: {pair_classifications}') return {'fault_boundary_comparisons': len(observations['single']), 'fault_results': observations['single'], 'pair_fault_boundary_comparisons': len(observations['pair']), 'pair_fault_results': observations['pair'], 'pair_partial_first_store_comparisons': partial_stores, 'pair_native_staged_fault_policy_comparisons': len(observations['pair']), 'pair_original_native_fault_full_state_equal_comparisons': pair_classifications['original_native_full_state_equal'], 'pair_unicorn_second_load_fault_divergences': pair_classifications['unicorn_second_load_divergence'], 'pair_sp_alignment_policy_only_comparisons': pair_classifications['sp_policy_only'], 'fault_observer_source_sha256': digest(source_path.read_bytes()), 'fault_observer_executable_sha256': digest(observer.read_bytes()), 'shared_fault_oracle_sha256': digest((ROOT / 'dev/memory-faults.py').read_bytes())} INTEGER_BASES = {'and':0x0e201c00,'bic':0x0e601c00,'orr':0x0ea01c00,'orn':0x0ee01c00, 'eor':0x2e201c00,'bsl':0x2e601c00,'bit':0x2ea01c00,'bif':0x2ee01c00, 'add':0x0e208400,'sub':0x2e208400,'abs':0x0e20b800,'neg':0x2e20b800} def integer_programs(): configurations=[] for name,base in INTEGER_BASES.items(): if name in ('add','sub','abs','neg'): configurations.extend((name,base|(q<<30)|(size<<22),8<=2+q: continue if name=='umov' and ((q==1)!=(size==3)): continue indices={0,15,(imm5>>(size+1))*lane,lane-1} if name=='insv' else {0} for imm4 in sorted(indices): configurations.append({'name':name,'base':base|(q<<30)|(imm5<<16)|(imm4<<11), 'lane':lane,'bytes':8<>(size+1),'source_index':imm4>>size}) for q in (0,1): for offset in range(8<>=64 for index in range(width//out_lane): value=source>>(index*in_bits)&((1<>(index*out_bits)&((1<>amount if name in ('ssra','usra','srsra','ursra'): shifted+=previous elif name=='sli': shifted|=previous&((1<>64] for v in vectors]} def register_shift_programs(): bases={'sshl':0x0e204400,'ushl':0x2e204400,'srshl':0x0e205400,'urshl':0x2e205400, 'sqshl':0x0e204c00,'uqshl':0x2e204c00,'sqrshl':0x0e205c00,'uqrshl':0x2e205c00} configurations=[] for name,base in bases.items(): saturating='q' in name for scalar in (False,True): for q in ((1,) if scalar else (0,1)): for size in (range(4) if saturating or not scalar else (3,)): if not q and size==3: continue configurations.append({'name':name,'base':base|(q<<30)|(int(scalar)<<28)|(size<<22), 'lane':1<>64] if op['immediate'] and op['amount']==1 and 6<=pattern<10: # Exact last-valid / first-saturating inputs for doubled signed and # unsigned results. Uniform active lanes also expose scalar and Q=0 QC. if op['unsigned_result'] and not op['signed']: boundaries=((1<<(bits-1))-1,1<<(bits-1),(1<<(bits-1))+1,0) else: half=1<<(bits-2) boundaries=(-half,-half-1,half-1,half) value=boundaries[pattern-6]&mask repeated=sum(value<<(index*bits) for index in range(128//bits)) for register in range(32): initial.v[register][:]=[repeated&MASK,repeated>>64] counts=register_shift_counts(bits) if not op['immediate']: for operation in operations: value=0 for index in range(128//bits): count=counts[(pattern+index)%len(counts)] # Deliberately unrelated high count bits; only the signed low byte counts. poison=(0xa5c37e19d2b48000^(operation['m']<<8)^(index<<16))&mask&~255 value|=(poison|(count&255))<<(index*bits) initial.v[operation['m']][:]=[value&MASK,value>>64] if pattern==register_shift_pattern_count(operations)-2: # Active source zero cannot saturate even at a huge positive count; # inactive MIN lanes must never publish QC. for operation in operations: if operation['n']==operation['m'] and not op['immediate']: continue value=sum((1<<(bits-1))<<(index*bits) for index in range(op['bytes']//lane,128//bits)) initial.v[operation['n']][:]=[value&MASK,value>>64] if not op['immediate']: count_value=sum((127 if index>64] initial.fpsr=(initial.fpsr&~(1<<27))|((1<<27) if flags else 0) def register_shift_mathematical(operations,initial): vectors=[lo|(hi<<64) for lo,hi in initial.v];fpsr=initial.fpsr for op in operations: bits=op['lane']*8;mask=(1<>(index*bits))&mask if op['signed'] and value&(1<<(bits-1)): value-=1<>(index*bits))&255 if not op['immediate'] and count>=128: count-=256 # Arbitrary precision math independently models the unclamped shift; # ShiftSat only reduces counts that already have identical outcomes. shifted=value<=0 else (value+((1<<(-count-1)) if op['round'] else 0))>>-count if op['saturating']: low,high=(0,mask) if op['unsigned_result'] else (-(1<<(bits-1)),(1<<(bits-1))-1) bounded=max(low,min(high,shifted)) if bounded!=shifted: fpsr|=1<<27 shifted=bounded result|=(shifted&mask)<<(index*bits) vectors[op['d']]=result return {**state_dict(initial),'v':[[v&MASK,v>>64] for v in vectors],'fpsr':fpsr} def reduction_programs(): configurations=[] bases={'smin':0x0e206c00,'smax':0x0e206400,'umin':0x2e206c00,'umax':0x2e206400, 'sminp':0x0e20ac00,'smaxp':0x0e20a400,'uminp':0x2e20ac00,'umaxp':0x2e20a400, 'sminv':0x0e31a800,'smaxv':0x0e30a800,'uminv':0x2e31a800,'umaxv':0x2e30a800, 'addv':0x0e31b800,'addp':0x0e20bc00,'saddlv':0x0e303800,'uaddlv':0x2e303800, 'cnt':0x0e205800,'clz':0x2e204800,'cls':0x0e204800,'rbit':0x2e605800,'not':0x2e205800} for name,base in bases.items(): for q in (0,1): for size in (range(4) if name=='addp' else (0,) if name in ('cnt','rbit','not') else range(3)): if size==3 and not q: continue if name.endswith('v') and size==2 and not q: continue configurations.append({'name':name,'base':base|(q<<30)|(size<<22),'lane':1<>offset&mask for offset in range(0,width*8,bits)] return [v-(1<>64] for v in vectors]} def saturating_programs(): bases={'sqadd':0x0e200c00,'uqadd':0x2e200c00,'sqsub':0x0e202c00,'uqsub':0x2e202c00, 'sqabs':0x0e207800,'sqneg':0x2e207800,'suqadd':0x0e203800,'usqadd':0x2e203800, 'sqxtn':0x0e214800,'uqxtn':0x2e214800,'sqxtun':0x2e212800} programs=[] for name,base in bases.items(): narrow=name in ('sqxtn','uqxtn','sqxtun') for scalar in (False,True): for q in ((1,) if scalar else (0,1)): for size in range(3 if narrow else 4): if not scalar and not narrow and not q and size==3: continue lane=1<>(index*in_bits)&in_mask;right=b>>(index*in_bits)&in_mask previous=old>>(index*out_bits)&out_mask if name.startswith('sq'): left=signed(left,in_bits) if name in ('sqadd','sqsub'): right=signed(right,in_bits) if name in ('sqadd','uqadd'): value=left+right elif name in ('sqsub','uqsub'): value=left-right elif name=='sqabs': value=abs(left) elif name=='sqneg': value=-left elif name=='suqadd': value=left+signed(previous,out_bits) elif name=='usqadd': value=signed(left,in_bits)+previous else: value=left clamped=min(high,max(low,value)) if clamped!=value: fpsr|=1<<27 result|=(clamped&out_mask)<<(index*out_bits) if op['upper']: result=(old&MASK)|(result<<64) vectors[d]=result return {**state_dict(initial),'v':[[v&MASK,v>>64] for v in vectors],'fpsr':fpsr} def product_programs(): bases={'mul':0x0e209c00,'mla':0x0e209400,'mls':0x2e209400, 'smull':0x0e20c000,'umull':0x2e20c000,'smlal':0x0e208000,'umlal':0x2e208000,'smlsl':0x0e20a000,'umlsl':0x2e20a000, 'saddl':0x0e200000,'uaddl':0x2e200000,'ssubl':0x0e202000,'usubl':0x2e202000, 'saddw':0x0e201000,'uaddw':0x2e201000,'ssubw':0x0e203000,'usubw':0x2e203000, 'sabd':0x0e207400,'uabd':0x2e207400,'saba':0x0e207c00,'uaba':0x2e207c00, 'sabdl':0x0e207000,'uabdl':0x2e207000,'sabal':0x0e205000,'uabal':0x2e205000} programs=[] for name,base in bases.items(): widen=name not in ('mul','mla','mls','sabd','uabd','saba','uaba') product=name in ('mul','mla','mls','smull','umull','smlal','umlal','smlsl','umlsl') difference=name in ('sabd','uabd','saba','uaba','sabdl','uabdl','sabal','uabal') accumulation='add' if name in ('mla','smlal','umlal','saba','uaba','sabal','uabal') else 'sub' if name in ('mls','smlsl','umlsl') else None for q in (0,1): for size in range(3): lane=1<>=64 right>>=64 result=0 for index in range(width//out_lane): a=left>>(index*left_bits)&((1<>(index*in_bits)&((1<>(index*out_bits)&((1<>64] for v in vectors]} def halving_programs(): bases={'shadd':0x0e200400,'uhadd':0x2e200400,'srhadd':0x0e201400,'urhadd':0x2e201400,'shsub':0x0e202400,'uhsub':0x2e202400, 'addhn':0x0e204000,'raddhn':0x2e204000,'subhn':0x0e206000,'rsubhn':0x2e206000, 'saddlp':0x0e202800,'uaddlp':0x2e202800,'sadalp':0x0e206800,'uadalp':0x2e206800,'xtn':0x0e212800,'shll':0x2e213800} programs=[] for name,base in bases.items(): high=name.endswith('hn');pair=name.endswith('lp');truncate=name=='xtn';shift=name=='shll' for q in (0,1): for size in range(3): lane=1<>=64 def value(vector,index): v=vector>>(index*in_bits)&((1<>=amount if op['accumulate']: v+=old>>(index*out_bits)&((1<>64] for v in vectors]} def immediate_programs(): programs=[] for q in (0,1): for op in (0,1): for cmode in range(16): if cmode==15 and op and not q: continue for imm in range(256): d=imm%32 word=0x0f000400|(q<<30)|(op<<29)|(cmode<<12)|((imm>>5)<<16)|((imm&31)<<5)|d operation={'kind':'constant','format':'modified','word':word,'d':d,'lane':1,'bytes':8<>5)<<16)|((imm&31)<<5)|d programs.append({'operations':[{'kind':'constant','format':'modified','word':word,'d':d,'lane':2,'bytes':8<>4)&7 exponent=exponent+1 if exponent<4 else exponent-7 value=math.ldexp(1+(imm&15)/16,exponent) if imm&128: value=-value return int.from_bytes(struct.pack({2:'>(64 if op['upper'] else 0)&mask if d!=31: gprs[d]=value else: value=(0 if n==31 else gprs[n])&mask if op['direction']=='to_vector' else vectors[n]&mask vectors[d]=(vectors[d]&MASK)|(value<<64) if op['upper'] else value continue imm,width=op['imm'],op['bytes'] action='move' if op['format']=='scalar_float': value=immediate_float_bits(imm,lane) else: cmode=op['cmode'];inverse=op['op'] if op['half'] or cmode==15: element_bytes=2 if op['half'] else 8 if inverse else 4 element=immediate_float_bits(imm,element_bytes) elif cmode<8: element_bytes=4;element=imm<<((cmode//2)*8) elif cmode<12: element_bytes=2;element=imm<<(((cmode-8)//2)*8) elif cmode<14: element_bytes=4;ones_bytes=1 if cmode==12 else 2 element=int.from_bytes(b'\xff'*ones_bytes+bytes([imm])+b'\0'*(3-ones_bytes),'little') elif inverse: element_bytes=8;element=int.from_bytes(bytes(255 if imm&(1<>64] for v in vectors]} def integer_mathematical(operations,initial): vectors=[lo|(hi<<64) for lo,hi in initial.v] for op in operations: name,width,lane,d,n,m=(op[k] for k in ('name','width','lane','d','n','m')) mask=(1<<(width*8))-1 a,b,old=(vectors[r]&mask for r in (n,m,d)) logical={'and':a&b,'bic':a&~b,'orr':a|b,'orn':a|~b,'eor':a^b, 'bsl':(old&a)|(~old&b),'bit':(old&~b)|(a&b),'bif':(old&b)|(a&~b)} if name in logical: value=logical[name]&mask else: value=0;bits=lane*8;lane_mask=(1<>offset&lane_mask for v in (a,b)) signed=left-(1<signed_right,'cmge':signed>=signed_right, 'cmhi':left>right,'cmhs':left>=right,'cmlt':signed<0,'cmle':signed<=0, 'cmtst':bool(left&right)}[name.rstrip('0')] result=lane_mask if condition else 0 else: result=left+right if name=='add' else left-right if name=='sub' else abs(signed) if name=='abs' else -signed value|=(result&lane_mask)<>64] for v in vectors]} def doubling_product_programs(): configurations=[] bases={'sqdmulh':(0x0e20b400,0x0f00c000),'sqrdmulh':(0x2e20b400,0x0f00d000), 'sqdmull':(0x0e20d000,0x0f00b000),'sqdmlal':(0x0e209000,0x0f003000),'sqdmlsl':(0x0e20b000,0x0f007000)} for name,(register,indexed) in bases.items(): high=name.endswith('h') for scalar in (False,True): for element in (False,True): for q in ((1,) if scalar else (0,1)): for size in (1,2): lane=1<> (2 if size==1 else 1))<<11)|(((index>>1) if size==1 else index)&1)<<21 if size==1: base|=(index&1)<<20 configurations.append({'name':name,'base':base,'lane':lane,'output_lane':lane if high else lane*2, 'bytes':(lane if high else lane*2) if scalar else 8<>=64 if op['upper'] and op['index'] is None: b>>=64 def signed(value,width): return value-(1<>(i*bits)&((1<>((op['index'] if op['index'] is not None else i)*bits)&((1<>bits,bits) else: value=clamp(value,out_bits) if name in ('sqdmlal','sqdmlsl'): previous=signed(old>>(i*out_bits)&((1<>64] for v in vectors],'fpsr':fpsr} def seed_doubling_products(initial,operations,pattern,flags): bits=operations[0]['lane']*8;mask=(1<>64] initial.fpsr=(initial.fpsr&~(1<<27))|((1<<27) if flags else 0) FPSR_QC = 1 << 27 VECTOR_BYTES = 16 VECTOR_HALF_BYTES = 8 SIMD_Q_BIT = 1 << 30 SIMD_SCALAR_BIT = 1 << 28 NARROW_SHIFT_BASES = { 'sqshrn': 0x0f009400, 'sqrshrn': 0x0f009c00, 'uqshrn': 0x2f009400, 'uqrshrn': 0x2f009c00, 'sqshrun': 0x2f008400, 'sqrshrun': 0x2f008c00, } def narrowing_shift_programs(): """All legal immediates; bounded aliases and boundary register chains.""" programs = [] for name, base in NARROW_SHIFT_BASES.items(): for output_lane in (1, 2, 4): output_bits = output_lane * 8 for scalar, upper in ((False, False), (False, True), (True, False)): for amount in range(1, output_bits + 1): encoded = base | ((2 * output_bits - amount) << 16) if scalar: encoded |= SIMD_Q_BIT | SIMD_SCALAR_BIT elif upper: encoded |= SIMD_Q_BIT config = { 'kind': 'narrowing_shift', 'name': name, 'lane': output_lane * 2, 'output_lane': output_lane, 'bytes': output_lane if scalar else VECTOR_HALF_BYTES, 'scalar': scalar, 'upper': upper, 'amount': amount, 'signed': not name.startswith('u'), 'unsigned_result': name.startswith('u') or name.endswith('un'), 'round': 'rsh' in name, } def operation(d, n): return {**config, 'd': d, 'n': n, 'word': encoded | (n << 5) | d} for d, n in ((0, 1), (31, 31), (31, 0)): programs.append({'operations': [operation(d, n)]}) if amount in (1, output_bits // 2, output_bits): programs.append({'operations': [operation(d, (d + 7) % 32) for d in range(32)]}) return programs def narrowing_shift_mathematical(operations, initial): vectors = [lo | (hi << 64) for lo, hi in initial.v] fpsr = initial.fpsr for op in operations: input_bits, output_bits = op['lane'] * 8, op['output_lane'] * 8 source, previous = vectors[op['n']], vectors[op['d']] lower = 0 if op['unsigned_result'] else -(1 << (output_bits - 1)) upper = (1 << (output_bits - int(not op['unsigned_result']))) - 1 result = 0 for index in range(op['bytes'] // op['output_lane']): value = (source >> (index * input_bits)) & ((1 << input_bits) - 1) if op['signed'] and value & (1 << (input_bits - 1)): value -= 1 << input_bits if op['round']: value += 1 << (op['amount'] - 1) shifted = value >> op['amount'] saturated = min(upper, max(lower, shifted)) if shifted != saturated: fpsr |= FPSR_QC result |= (saturated & ((1 << output_bits) - 1)) << (index * output_bits) if op['upper']: result = (previous & MASK) | (result << 64) vectors[op['d']] = result return {**state_dict(initial), 'v': [[value & MASK, value >> 64] for value in vectors], 'fpsr': fpsr} def seed_narrowing_shifts(initial, operations, pattern, flags): op = operations[0] bits, output_bits, amount = op['lane'] * 8, op['output_lane'] * 8, op['amount'] mask = (1 << bits) - 1 rounding = (1 << (amount - 1)) if op['round'] else 0 low = 0 if op['unsigned_result'] else -(1 << (output_bits - 1)) high = (1 << (output_bits - int(not op['unsigned_result']))) - 1 thresholds = tuple((bound << amount) - rounding + delta for bound in (low, high, high + 1) for delta in (-1, 0, 1)) values = thresholds + (0, 1, -1, 1 << (bits - 1), (1 << (bits - 1)) - 1, mask) for register in range(32): if pattern < 9: lanes = [thresholds[(pattern + i) % len(thresholds)] & mask for i in range(128 // bits)] elif pattern == 9: lanes = [values[9 + (register + i) % 6] & mask for i in range(128 // bits)] elif pattern == 10: # Every active source is zero; inactive scalar lanes would saturate. active = op['bytes'] // op['output_lane'] lanes = [0 if i < active else 1 << (bits - 1) for i in range(128 // bits)] else: continue # Retain independently seeded random full-state inputs. value = sum(lane << (i * bits) for i, lane in enumerate(lanes)) initial.v[register][:] = [value & MASK, value >> 64] initial.fpsr = (initial.fpsr & ~FPSR_QC) | (FPSR_QC if flags else 0) DOT_REGISTER_BASES = {'sdot': 0x0e009400, 'udot': 0x2e009400} DOT_INDEXED_BASES = {'sdot': 0x0f00e000, 'udot': 0x2f00e000} ROUND_PRODUCT_REGISTER_BASES = {'sqrdmlah': 0x2e008400, 'sqrdmlsh': 0x2e008c00} ROUND_PRODUCT_INDEXED_BASES = {'sqrdmlah': 0x2f00d000, 'sqrdmlsh': 0x2f00f000} def dot_round_product_programs(): programs = [] configurations = [] for name in DOT_REGISTER_BASES: for q in (0, 1): for index in (None, 0, 1, 2, 3): encoded = (DOT_REGISTER_BASES if index is None else DOT_INDEXED_BASES)[name] encoded |= (2 << 22) | (q << 30) if index is not None: encoded |= ((index >> 1) << 11) | ((index & 1) << 21) configurations.append((encoded, { 'kind': 'dot_round_product', 'name': name, 'lane': 1, 'output_lane': 4, 'bytes': 8 << q, 'scalar': False, 'index': index, 'upper': False, })) for name in ROUND_PRODUCT_REGISTER_BASES: for scalar in (False, True): for q in ((1,) if scalar else (0, 1)): for size in (1, 2): lane = 1 << size for index in (None, *range(VECTOR_BYTES // lane)): encoded = (ROUND_PRODUCT_REGISTER_BASES if index is None else ROUND_PRODUCT_INDEXED_BASES)[name] encoded |= (size << 22) | (q << 30) | (SIMD_SCALAR_BIT if scalar else 0) if index is not None: encoded |= (index >> (2 if lane == 2 else 1)) << 11 encoded |= (((index >> 1) if lane == 2 else index) & 1) << 21 if lane == 2: encoded |= (index & 1) << 20 configurations.append((encoded, { 'kind': 'dot_round_product', 'name': name, 'lane': lane, 'output_lane': lane, 'bytes': lane if scalar else 8 << q, 'scalar': scalar, 'index': index, 'upper': False, })) for encoded, config in configurations: def operation(d, n, m): if config['index'] is not None and config['lane'] == 2: m &= 15 return {**config, 'd': d, 'n': n, 'm': m, 'word': encoded | (m << 16) | (n << 5) | d} for d, n, m in ((0, 1, 2), (1, 1, 2), (2, 1, 2), (0, 1, 1), (31, 31, 31), (31, 29, 31)): programs.append({'operations': [operation(d, n, m)]}) programs.append({'operations': [operation(d, (d + 5) % 32, (d + 13) % 32) for d in range(32)]}) return programs def dot_round_product_mathematical(operations, initial): vectors = [lo | (hi << 64) for lo, hi in initial.v] fpsr = initial.fpsr def signed(value, bits): return value - (1 << bits) if value & (1 << (bits - 1)) else value for op in operations: left, right, previous = (vectors[op[key]] for key in ('n', 'm', 'd')) bits, output_bits = op['lane'] * 8, op['output_lane'] * 8 input_mask, output_mask = (1 << bits) - 1, (1 << output_bits) - 1 result = 0 for lane in range(op['bytes'] // op['output_lane']): accumulator = (previous >> (lane * output_bits)) & output_mask if op['name'] in DOT_REGISTER_BASES: value = accumulator for byte in range(op['output_lane']): a = (left >> ((lane * op['output_lane'] + byte) * bits)) & input_mask right_lane = lane if op['index'] is None else op['index'] b = (right >> ((right_lane * op['output_lane'] + byte) * bits)) & input_mask if op['name'] == 'sdot': a, b = signed(a, bits), signed(b, bits) value += a * b else: a = signed((left >> (lane * bits)) & input_mask, bits) right_lane = lane if op['index'] is None else op['index'] b = signed((right >> (right_lane * bits)) & input_mask, bits) product = 2 * a * b if op['name'] == 'sqrdmlsh': product = -product # One unbounded fused expression: no intermediate saturation or rounding. rounded = ((signed(accumulator, bits) << bits) + product + (1 << (bits - 1))) >> bits value = min((1 << (bits - 1)) - 1, max(-(1 << (bits - 1)), rounded)) if value != rounded: fpsr |= FPSR_QC result |= (value & output_mask) << (lane * output_bits) vectors[op['d']] = result return {**state_dict(initial), 'v': [[value & MASK, value >> 64] for value in vectors], 'fpsr': fpsr} def seed_dot_round_products(initial, operations, pattern, flags): op = operations[0] bits = op['lane'] * 8 minimum, mask = 1 << (bits - 1), (1 << bits) - 1 values = (0, 1, mask, minimum, minimum - 1, minimum + 1, mask - 1, 1 << (bits - 2)) for register in range(32): if pattern < 6: lanes = [values[(pattern + register + i) % len(values)] for i in range(128 // bits)] elif pattern in (6, 7): lanes = [minimum] * (128 // bits) elif pattern == 8: # Saturation in inactive scalar/Q=0 source lanes must not set QC. lanes = [0 if i * op['lane'] < op['bytes'] else minimum for i in range(128 // bits)] else: continue value = sum(value << (i * bits) for i, value in enumerate(lanes)) initial.v[register][:] = [value & MASK, value >> 64] if pattern in (6, 7) and op['name'] in ROUND_PRODUCT_REGISTER_BASES: # With distinct registers: MIN*MIN cancels a negative accumulator on add, # or positive accumulator on subtract; staged product saturation is wrong. accumulator = mask if op['name'] == 'sqrdmlah' else minimum - 1 if pattern == 7: accumulator = minimum - 1 if op['name'] == 'sqrdmlah' else minimum value = sum(accumulator << (i * bits) for i in range(128 // bits)) initial.v[op['d']][:] = [value & MASK, value >> 64] if pattern == 9 and op['name'] in ROUND_PRODUCT_REGISTER_BASES: # Exact half-product: subtraction rounds the negative product as part # of the fused expression; subtracting an already-rounded product fails. for key, lane_value in (('d', 0), ('n', 1), ('m', 1 << (bits - 2))): value = sum(lane_value << (i * bits) for i in range(128 // bits)) initial.v[op[key]][:] = [value & MASK, value >> 64] initial.fpsr = (initial.fpsr & ~FPSR_QC) | (FPSR_QC if flags else 0) def indexed_product_programs(): # Explicit Arm indexed encodings, independent from the Rust decoder catalog. bases={'mul':0x0f008000,'mla':0x2f000000,'mls':0x2f004000, 'smull':0x0f00a000,'umull':0x2f00a000,'smlal':0x0f002000, 'umlal':0x2f002000,'smlsl':0x0f006000,'umlsl':0x2f006000} programs=[] for name,base in bases.items(): widen=name not in ('mul','mla','mls') for q in (0,1): for size in (1,2): lane=1<>(2 if lane==2 else 1) l=(index>>(1 if lane==2 else 0))&1 encoded=base|(q<<30)|(size<<22)|(h<<11)|(l<<21) if lane==2: encoded|=(index&1)<<20 config={'kind':'indexed_product','name':name,'lane':lane, 'output_lane':lane*2 if widen else lane,'bytes':16 if widen else 8<>=64 selected=right>>(op['index']*bits)&mask if op['signed'] and selected&(1<<(bits-1)): selected-=1<>(i*bits)&mask if op['signed'] and value&(1<<(bits-1)): value-=1<>(i*out_bits)&out_mask if op['accumulate']=='add': value=previous+value elif op['accumulate']=='sub': value=previous-value result|=(value&out_mask)<<(i*out_bits) vectors[op['d']]=result return {**state_dict(initial),'v':[[v&MASK,v>>64] for v in vectors]} def seed_indexed_products(initial,operations,pattern,flags): bits=operations[0]['lane']*8;mask=(1<>64] initial.fpsr=(initial.fpsr&~(1<<27))|((1<<27) if flags else 0) def floating_bit_programs(): """Canonical forms have multiple arrangements; keep both counts explicit.""" configurations=[] for ftype,lane in ((0,4),(1,8),(3,2)): for condition in range(16): configurations.append({'name':'fcsel','base':0x1e200c00|(ftype<<22)|(condition<<12), 'lane':lane,'bytes':lane,'scalar':True,'condition':condition}) for name,base in (('fabs',0x1e20c000),('fneg',0x1e214000)): configurations.append({'name':name,'base':base|(ftype<<22),'lane':lane,'bytes':lane,'scalar':True}) for name,half,ordinary in (('fabs',0x0ef8f800,0x0ea0f800),('fneg',0x2ef8f800,0x2ea0f800)): for lane in (2,4,8): for q in ((1,) if lane==8 else (0,1)): base=(half if lane==2 else ordinary|((lane==8)<<22))|(q<<30) configurations.append({'name':name,'base':base,'lane':lane,'bytes':8<>28;n,z,c,v=(bool(flags&(1<>(index*bits)&mask exponent=value>>fraction_bits&((1<>64] for value in vectors]} def seed_floating_bits(initial,operations,pattern): bits=operations[0]['lane']*8;fraction=bits-{16:5,32:8,64:11}[bits]-1 infinity=((1<<(bits-fraction-1))-1)<>64] def floating_bit_cases(functions,exhaustive_half=True): templates=states(all_flags=True) for index,function in enumerate(functions): operations=function['operations'];select=operations[0]['name']=='fcsel' controls=(0,7,0x07c80000,0x07c80007) if select else tuple(low|high for high in (0,0x07c80000) for low in range(8)) for control in controls: for pattern in range(15): for flags in (range(16) if select and pattern==0 else (0,15)): initial=State.from_buffer_copy(templates[flags]);seed_extended_state(initial,index*65536+pattern*16+flags) initial.reserved=0x51ad1234;initial.nzcv=flags<<28;initial.fpcr=control initial.fpsr=(1<<27)|0x9f if flags else 0 seed_floating_bits(initial,operations,pattern) yield index,initial if exhaustive_half: for name in ('fabs','fneg'): index=next(i for i,f in enumerate(functions) if len(f['operations'])==1 and all(f['operations'][0].get(k)==v for k,v in {'name':name,'lane':2,'bytes':16,'scalar':False,'d':0,'n':1}.items())) for ah in (0,2): for first in range(0,65536,8): initial=State();seed_extended_state(initial,first+ah);initial.fpcr=0x07c80005|ah;initial.fpsr=(1<<27)|0x9f value=sum((first+lane)<<(lane*16) for lane in range(8)) initial.v[1][:]=[value&MASK,value>>64] yield index,initial def floating_qemu_source(functions,count): """Owned bare-metal transport. Only .inst lines are acceptance instructions.""" lines=['.arch armv8.7-a+fp16','.text','.global _start','_start:', 'mov x0,#0x300000','msr cpacr_el1,x0','isb', 'mov x19,#0x41000000','mov x20,#0x49000000','mov x23,#0x40070000','add x23,x23,#0x1000', 'adrp x21,functions','add x21,x21,:lo12:functions',f'add x24,x21,#{len(functions)*8}', f'movz x22,#{count&65535}',f'movk x22,#{count>>16},lsl#16','again:'] for table in ('x21','x24'): lines+=['ldr w0,[x19,#264]','msr nzcv,x0','ldr w0,[x19,#784]','msr fpcr,x0','ldr w0,[x19,#788]','msr fpsr,x0'] lines.extend(f'ldr q{i},[x19,#{272+i*16}]' for i in range(32)) lines+=['ldr x0,[x19]',f'ldr x0,[{table},x0,lsl#3]','blr x0'] lines.extend(f'str q{i},[x20,#{i*16}]' for i in range(32)) lines+=['mrs x0,nzcv','str w0,[x20,#512]','mrs x0,fpcr','str w0,[x20,#516]', 'mrs x0,fpsr','str w0,[x20,#520]','mov w0,#0x1234','str w0,[x20,#524]','add x20,x20,#528'] lines+=['add x19,x19,#800','subs x22,x22,#1','b.ne again', 'mov x0,#0x1234','mov x1,#0x40070000','str x0,[x1]','completed: b completed'] for index,function in enumerate(functions): lines.append(f'function_{index}:') lines.extend(f'.inst 0x{op["word"]:08x}' for op in function['operations']) lines.append('ret') for index,function in enumerate(functions): lines.append(f'component_{index}:') for op in function['operations']: if op['scalar']: lines.append(f'.inst 0x{op["word"]:08x}') continue # Independent original scalar FPAbs/FPNeg checks each vector lane. # Preserve the unmodified original-vector result in its own record; # this decomposition is explicitly a component oracle, not equality # between the original vector instruction and the QEMU model. lines+=['str q31,[x23]','str q30,[x23,#16]',f'str q{op["n"]},[x23,#32]','movi v31.2d,#0'] lane=op['lane'];suffix={2:'h',4:'s',8:'d'}[lane];ftype={2:3,4:0,8:1}[lane] scalar=(0x1e20c000 if op['name']=='fabs' else 0x1e214000)|(ftype<<22)|(30<<5)|30 for element in range(op['bytes']//lane): lines.extend([f'ldr {suffix}30,[x23,#{32+element*lane}]',f'.inst 0x{scalar:08x}', f'ins v31.{suffix}[{element}],v30.{suffix}[0]']) lines+=['str q31,[x23,#64]','ldr q31,[x23]','ldr q30,[x23,#16]',f'ldr q{op["d"]},[x23,#64]'] lines.append('ret') lines+=['.section .rodata','.balign 8','functions:'] lines.extend(f'.quad function_{index}' for index in range(len(functions))) lines.extend(f'.quad component_{index}' for index in range(len(functions))) return '\n'.join(lines)+'\n' def floating_qemu_results(functions): """Execute unchanged original A64 with AFP; cache only hash-bound results.""" qemu=ROOT/'.tools/qemu/bin/qemu-system-aarch64' if not qemu.exists(): found=shutil.which('qemu-system-aarch64') if not found: raise AssertionError('AFP verification requires qemu-system-aarch64 with FEAT_AFP') qemu=Path(found) version=subprocess.check_output([str(qemu),'--version'],text=True).splitlines()[0] binaries=[qemu,*sorted((ROOT/'.tools/qemu/Cellar/qemu').glob('*/bin/qemu-system-aarch64'))] identities={str(path.relative_to(ROOT)) if path.is_relative_to(ROOT) else str(path):digest(path.read_bytes()) for path in binaries} inputs=bytearray();count=0 for index,initial in floating_bit_cases(functions): inputs+=struct.pack('deadline: raise AssertionError('QEMU failed before QMP connection') time.sleep(0.02) connection=socket.socket(socket.AF_UNIX,socket.SOCK_STREAM);connection.settimeout(10);connection.connect(str(endpoint)) stream=connection.makefile('rwb');json.loads(stream.readline()) def request(name,arguments=None): data={'execute':name} if arguments is not None: data['arguments']=arguments stream.write(json.dumps(data).encode()+b'\n');stream.flush() while True: reply=json.loads(stream.readline()) if 'error' in reply: raise AssertionError(reply) if 'return' in reply: return reply['return'] request('qmp_capabilities');request('cont');deadline=time.monotonic()+60 while True: marker=request('human-monitor-command',{'command-line':'xp /1gx 0x40070000'}) if marker.strip().endswith('0x0000000000001234'): break if process.poll() is not None or time.monotonic()>deadline: raise AssertionError(f'QEMU original fixture failed or timed out: {marker}') time.sleep(0.02) request('stop') request('human-monitor-command',{'command-line':f'pmemsave 0x49000000 {count*1056} "{result_path}"'}) request('quit');process.wait(timeout=5) finally: if stream is not None: stream.close() if connection is not None: connection.close() if process.poll() is None: process.kill();process.wait(timeout=5) results=result_path.read_bytes() if len(results)!=count*1056: raise AssertionError('QEMU returned a truncated register result stream') files={name:{'path':str((directory/name).relative_to(ROOT)),'sha256':digest((directory/name).read_bytes()), 'bytes':(directory/name).stat().st_size} for name in ('inputs.bin','guest.S','guest.elf','results.bin','command.json','stderr.log')} receipt={'engine':version,'cpu':'max','transport':'owned bare-metal A64 fixture, QMP physical RAM dump', 'qemu_executable_sha256':identities,'cases':count,'files':files, 'limits':'Original emulator execution, no Apple hardware claim. Captures all V registers, NZCV, FPCR and FPSR after each fixture; GPRs/SP are harness transport, not original architectural comparisons. Native/math comparisons additionally check every byte of GuestState. No disabled-FP trap or SME streaming claim.'} receipt_path.write_text(json.dumps(receipt,indent=2)+'\n') return results,receipt def execute_floating_bit_batches(batches): corpus=hashlib.sha256();comparisons=0;unicorn_comparisons=0;original_equal=0;component_equal=0;divergences=[];divergence_count=0;dynamic=[] receipts=[] for batch in batches: results,receipt=floating_qemu_results(batch['functions']);receipts.append(receipt) machine=Uc(UC_ARCH_ARM64,UC_MODE_ARM);machine.ctl_set_cpu_model(arm.UC_CPU_ARM64_MAX) machine.mem_map(BASE,(len(batch['source'])+4095)&~4095);machine.mem_write(BASE,batch['source']) with executable(batch['native']) as address: natives=[ctypes.CFUNCTYPE(None,ctypes.POINTER(State))(address+offset) for offset in batch['offsets']] for case,(index,initial) in enumerate(floating_bit_cases(batch['functions'])): operations=batch['functions'][index]['operations'] expected=floating_bit_mathematical(operations,initial) observations=[] for component in (0,1): original=State.from_buffer_copy(initial) fields=struct.unpack_from('<64Q4I',results,case*1056+component*528) for register in range(32): original.v[register][:]=fields[register*2:register*2+2] original.nzcv,original.fpcr,original.fpsr=fields[64:67] if fields[67]!=0x1234: raise AssertionError('QEMU result sentinel missing') observations.append(state_dict(original)) original,component=observations actual=State.from_buffer_copy(initial);natives[index](ctypes.byref(actual)) if component!=expected or state_dict(actual)!=expected: raise AssertionError(json.dumps({'operations':operations,'initial':state_dict(initial), 'math':expected,'qemu_original':original,'qemu_scalar_component':component,'native':state_dict(actual)})) component_equal+=1 if original==expected: original_equal+=1 else: if operations[0]['scalar'] or not initial.fpcr&2: raise AssertionError('Unexpected original QEMU divergence') legacy_state=State.from_buffer_copy(initial);legacy_state.fpcr&=~2 legacy=floating_bit_mathematical(operations,legacy_state);legacy['fpcr']=initial.fpcr if original!=legacy: raise AssertionError('QEMU difference is larger than exact vector AH NaN sign handling') divergence_count+=1 if len(divergences)<32: divergences.append({'operations':operations,'initial_state_sha256':digest(bytes(initial)), 'requested_fpcr':initial.fpcr,'original_v':original['v'],'spec_native_v':expected['v'], 'classification':'QEMU10 vector FABS/FNEG ignores AH for NaN signs; scalar component oracle matches pinned specification'}) # Unicorn MAX implements FP16/FZ16 but not AFP. Never erase controls to # turn a divergent original observation into an equality claim. if initial.fpcr==0: initialize_arm(machine,initial);entry=batch['entries'][index];end=BASE+(entry+len(operations))*4 machine.emu_start(BASE+entry*4,end,count=len(operations)+1) if machine.reg_read(arm.UC_ARM64_REG_PC)!=end or state_dict(capture_arm(machine,initial))!=expected: raise AssertionError('Unicorn baseline differs from pinned FP bit semantics') unicorn_comparisons+=1 corpus.update(struct.pack('>64] if bit_bytes: for register in range(32): values=bytes((pattern*operations[0]['bytes']+i+register)%256 for i in range(16)) initial.v[register][:]=struct.unpack('<2Q',values) if leading and pattern<=bits: # Every zero/sign leading count, plus dirty upper lanes and all-one input. lane_mask=(1<>64] if table: # The output lanes collectively cover every unsigned index 0..255, including all table edges. for op in operations: indices=bytes((pattern*op['bytes']+i)%256 for i in range(16)) initial.v[op['m']][:]=struct.unpack('<2Q',indices) if saturation: out_bits=operations[0]['output_lane']*8 if pattern<5: minimum=1<<(out_bits-1);maximum=minimum-1;unsigned_maximum=(1<>64] elif pattern==5: # Zero every active source lane; force MIN/all-one in inactive lanes. active=operations[0]['source_bytes'] for register in range(32): values=bytearray(b'\xff'*16);values[:active]=b'\0'*active for byte in range(active,16,lane): values[byte:byte+lane]=(1<<(bits-1)).to_bytes(lane,'little') initial.v[register][:]=struct.unpack('<2Q',values) initial.fpsr=(initial.fpsr&~(1<<27))|((1<<27) if flags else 0) if products: if pattern<5: values=(0,1,(1<>64] elif pattern==5: for register in range(32): low=sum(((register+i+1)&((1<>64] elif pattern==7: for register in range(32): low=sum(((register+i+1)&((1<>64] if register<31: initial.x[register]=(payload|((0xdeadbeefdeadbeef<code_base+end: raise AssertionError('Generated x86 trace escaped its exact mini-function boundary') offset=pc-code_base;counts[offset]+=1 if previous is not None and offset> 26 != expected_ec or proof['exception_class'] != expected_ec or int(proof['verified_elr'], 16) != elr or elr != pc + (8 if expected_ec == FP_REGISTER_EC_SVC else 0) or fp_register_elf_word(elf, pc) != case['word'] or fp_register_elf_word(elf, pc + 4) != FP_REGISTER_SVC or current_el != 4 or cpacr != case['fpen'] << 20 or marker != 0x1234): raise AssertionError(f'Original FP register instruction/exception evidence changed: {index}') if expected_ec != FP_REGISTER_EC_SVC: if bytes(original) != bytes(initial): raise AssertionError('Original disabled/undefined FP access changed state') (undefined if expected_ec == 0 else access_traps).append({'case': index, 'word': case['word'], 'fpen': case['fpen'], 'subject_pc': pc, 'exception_pc': elr, 'esr': esr}) continue operation = fp_register_operation(case['name'], case['rt']) if operation['word'] != case['word'] or bytes(original) != bytes(fp_register_expected(operation, initial, FP_REGISTER_QEMU_MASKS)): raise AssertionError(f'Original FP register full state disagrees with its declared observed policy: {index}') expected = fp_register_expected(operation, initial) fields = [] if bytes(expected) != bytes(original): if (list(expected.x) != list(original.x) or (expected.fpcr ^ original.fpcr) & ~(FP_REGISTER_MASKS['fpcr'] ^ FP_REGISTER_QEMU_MASKS['fpcr']) or (expected.fpsr ^ original.fpsr) & ~(FP_REGISTER_MASKS['fpsr'] ^ FP_REGISTER_QEMU_MASKS['fpsr'])): raise AssertionError('Original FP register differs outside declared policy fields') for field, (register, mask) in FP_REGISTER_POLICY_FIELDS.items(): if (getattr(expected, register) ^ getattr(original, register)) & mask: fields.append(field) if not fields: raise AssertionError('Unclassified original FP register policy mismatch') classifications.append({'case': index, 'fields': fields, 'requested': case['value'], 'original_fpcr': original.fpcr, 'native_policy_fpcr': expected.fpcr, 'original_fpsr': original.fpsr, 'native_policy_fpsr': expected.fpsr}) records.append({'function': functions[(case['name'], case['rt'])], 'initial': bytes(initial), 'expected': bytes(expected), 'original': bytes(original), 'case': index, 'fields': fields}) if len(records) != 274 or len(access_traps) != 36 or len(undefined) != 4: raise AssertionError('Original FP register success/trap classification changed') return {'records': records, 'classifications': classifications, 'disabled_fp_access_traps': access_traps, 'undefined_fpmr_traps': undefined, 'review_sha256': review_hash, 'receipt': {'path': str(receipt_path.relative_to(ROOT)), 'sha256': receipt_hash, 'files': receipt['files'], 'historical_harness_sha256': receipt['harness_sha256'], 'cpu': receipt['cpu'], 'qemu_version': receipt['qemu_version']}, 'validation_sha256': validation_hash} def fp_register_policy_cases(): records = [] variants = ((0, 0, 0), (MASK, 0xffffffff, 0xffffffff), (0xfedcba9800000000, ~FP_REGISTER_MASKS['fpcr'] & 0xffffffff, ~FP_REGISTER_MASKS['fpsr'] & 0xffffffff), (0x1234567807c89f07, 0x9f00, 0x0800009f)) for index, function in enumerate(fp_register_functions()): operation = function['operations'][0] for variant, (value, fpcr, fpsr) in enumerate(variants): initial = State.from_buffer_copy(states()[0]) seed_extended_state(initial, index * len(variants) + variant) initial.fpcr, initial.fpsr, initial.nzcv = fpcr, fpsr, ((index + variant) % 16) << 28 if operation['rt'] != 31: initial.x[operation['rt']] = value records.append({'function': index, 'initial': bytes(initial), 'expected': bytes(fp_register_expected(operation, initial)), 'variant': variant}) for register in ('fpcr', 'fpsr'): operation = fp_register_operation('msr_' + register, 0) index = list(FP_REGISTER_WORDS).index(operation['name']) * 32 for bit in range(64): initial = State.from_buffer_copy(states()[0]) seed_extended_state(initial, bit) initial.fpcr, initial.fpsr, initial.x[0] = 0xffffffff, 0xffffffff, 1 << bit records.append({'function': index, 'initial': bytes(initial), 'expected': bytes(fp_register_expected(operation, initial)), 'bit': bit}) return records def execute_fp_register_batches(batches): batch = batches[0] oracle = load_fp_register_oracle() records = oracle['records'] + fp_register_policy_cases() counts, corpus = Counter(), hashlib.sha256() with executable(batch['native']) as address, executable(NUMERICAL_MXCSR_WRAPPER) as wrapper_address: wrapper = ctypes.CFUNCTYPE(None, ctypes.POINTER(State), ctypes.c_void_p, ctypes.c_uint32, ctypes.POINTER(NumericalNativeExit))(wrapper_address) guard = ctypes.create_string_buffer(NUMERICAL_GUARD_BYTES * 2 + ctypes.sizeof(State)) state_address = ctypes.addressof(guard) + NUMERICAL_GUARD_BYTES native_state = ctypes.cast(state_address, ctypes.POINTER(State)) def invoke(record, mxcsr): ctypes.memmove(guard, NUMERICAL_GUARD + record['initial'] + NUMERICAL_GUARD, len(guard)) observed = NumericalNativeExit() wrapper(native_state, address + batch['offsets'][record['function']], mxcsr, ctypes.byref(observed)) actual = ctypes.string_at(state_address, ctypes.sizeof(State)) if (actual != record['expected'] or observed.mxcsr_before != mxcsr or observed.mxcsr_after != mxcsr or guard.raw[:NUMERICAL_GUARD_BYTES] != NUMERICAL_GUARD or guard.raw[-NUMERICAL_GUARD_BYTES:] != NUMERICAL_GUARD): failure = {'kind': 'fp_register_native_failure', 'record': {key: value.hex() if isinstance(value, bytes) else value for key, value in record.items()}, 'actual': actual.hex(), 'requested_mxcsr': mxcsr, 'observed_mxcsr': [observed.mxcsr_before, observed.mxcsr_after]} write_evidence(ROOT / '.stela/simd-fp_registers-failure.json', failure) raise AssertionError('FP register native state/MXCSR/canary mismatch; immutable failure retained') for record in records: invoke(record, NUMERICAL_MXCSR_ALL_MASKS) corpus.update(record['initial']); corpus.update(struct.pack('= len(source) or pc in entries or identity['source_sha256'] != digest(prefix) or identity['compiler_source_sha256'] != compiler or identity['abi'] != BLOCK_ABI or identity['target_cpu'] != 'x86_64-baseline' or manifest['source_map'] != [[pc, 0]] or manifest['verified'] is not False): raise AssertionError('Half chain cache contains an incorrect source prefix or identity') entries.add(pc) native = (path.parent / 'native.bin').read_bytes() expected_bytes = NUMERICAL_RETURN_NATIVE_BYTES if position == len(source) - 4 else NUMERICAL_HELPER_NATIVE_BYTES if len(native) != expected_bytes or manifest['native_bytes'] != len(native) or manifest['native_sha256'] != digest(native): raise AssertionError('Half chain retained dead suffix bytes or changed its native body') accounting = {'guest_pc': pc, 'compiled_source_bytes': len(prefix), 'native_bytes': len(native), 'native_instructions': len(decode_native_stream(native))} retained = retain_native_evidence('simd-scalar_numerical-half-chain', prefix, native, manifest, accounting, Path(__file__).read_bytes(), root=evidence_root) artifacts.append({**accounting, 'files': retained}) if entries != set(range(base, base + len(source), 4)): raise AssertionError('Half chain cache omitted a source instruction') return artifacts def numerical_runtime_cohort(cli, directory, oracle, compiler): source = b''.join(struct.pack('> NUMERICAL_EXCEPTION_ENABLE_SHIFT) & NUMERICAL_EXCEPTION_FLAGS} if reason != expected or report['pc'] != entry or bytes(actual) != record['initial']: raise AssertionError(json.dumps({'unsupported_case': record['case'], 'expected_reason': expected, 'report': report})) counts['typed_unsupported_state_checks'] += 1 else: if (reason['kind'] != 'stop_address' or report['pc'] != STOP or bytes(actual) != record['expected'] or report['outcome']['last_exit'] != [STOP, NUMERICAL_RETURN_EXIT]): raise AssertionError(json.dumps({'runtime_case': record['case'], 'producer': record['producer'], 'expected': state_dict(State.from_buffer_copy(record['expected'])), 'report': report})) counts['completed_state_checks'] += 1 # Each shape has a genuine helper-only zero/zero case. Budget one must stop # after completing that operation, and resume at the real guest RET. resume_cases = {} for record in oracle['records']: initial = State.from_buffer_copy(record['initial']) if record['label'] == 'zero_pp' and initial.fpcr == 0 and initial.fpsr == 0: op = record['operation'] resume_cases.setdefault((op['name'], op['lane']), record) if len(resume_cases) != 12: raise AssertionError('Numerical budget corpus omits an arithmetic form') for index, record in enumerate(resume_cases.values()): entry = BASE + record['function'] * 8 initial = State.from_buffer_copy(record['initial']) resume_cache = directory / f'resume-cache-{index}' first, intermediate = invoke(source, BASE, entry, initial, budget=1, cache_dir=resume_cache) if (first['outcome']['reason']['kind'] != 'budget_exhausted' or first['pc'] != entry + 4 or bytes(intermediate) != record['expected'] or first['outcome']['floating_point_helpers'] != 1): raise AssertionError('Numerical helper budget boundary lost or repeated an instruction') final, actual = invoke(source, BASE, first['pc'], intermediate, budget=1, cache_dir=resume_cache) if (final['outcome']['reason']['kind'] != 'stop_address' or final['pc'] != STOP or bytes(actual) != record['expected'] or final['outcome']['floating_point_helpers'] != 0): raise AssertionError('Numerical budget resume did not execute exactly the guest RET') warm, actual = invoke(source, BASE, entry, initial, cache_dir=resume_cache) if bytes(actual) != record['expected'] or warm['outcome']['disk_cache_hits'] < 2: raise AssertionError('Numerical warm runtime did not reuse both cached blocks') counts['budget_resume_checks'] += 1 counts['disk_cache_resume_checks'] += 1 # Interior helper PCs must retain already committed GPR work and resume # at the following instruction. ADD preserves NZCV and FP state. mixed = struct.pack('<4I', NUMERICAL_ADD_X7_ONE, record['operation']['word'], NUMERICAL_ADD_X8_TWO, RET) mixed_base = BASE + 0x1000 + index * 0x100 expected = State.from_buffer_copy(record['expected']) expected.x[7] = (expected.x[7] + 1) & MASK expected.x[8] = (expected.x[8] + 2) & MASK completed, actual = invoke(mixed, mixed_base, mixed_base, initial) if (completed['pc'] != STOP or completed['outcome']['reason']['kind'] != 'stop_address' or bytes(actual) != bytes(expected) or completed['outcome']['floating_point_helpers'] != 1): raise AssertionError('Interior numerical helper lost prefix or suffix effects') original.mem_write(mixed_base, mixed) original.ctl_remove_cache(mixed_base, mixed_base + len(mixed)) initialize_arm(original, initial) original.emu_start(mixed_base, STOP, count=8) if original.reg_read(arm.UC_ARM64_REG_PC) != STOP or bytes(capture_arm(original, initial)) != bytes(expected): raise AssertionError('Original interior fixture differs from complete runtime state') counts['synthetic_original_complete_state_checks'] += 1 declined_initial = State.from_buffer_copy(initial) declined_initial.fpcr |= NUMERICAL_INVALID_ENABLE declined_expected = State.from_buffer_copy(declined_initial) declined_expected.x[7] = (declined_expected.x[7] + 1) & MASK declined, actual = invoke(mixed, mixed_base, mixed_base, declined_initial) declined_reason = {'kind': 'floating_point_unsupported', 'instruction_pc': mixed_base + 4, 'fpcr': declined_initial.fpcr, 'enabled_exceptions': 1} if (declined['pc'] != mixed_base + 4 or bytes(actual) != bytes(declined_expected) or declined['outcome']['reason'] != declined_reason): raise AssertionError('Interior numerical refusal changed state at the unsupported instruction') counts['interior_helper_state_checks'] += 1 counts['interior_decline_state_checks'] += 1 loop = struct.pack('<4I', record['operation']['word'], NUMERICAL_SUBS_X10_ONE, NUMERICAL_CBNZ_X10_BACK_TWO, RET) loop_initial = State.from_buffer_copy(initial) loop_initial.x[10] = NUMERICAL_CACHE_LOOP_ITERATIONS loop_expected = State.from_buffer_copy(record['expected']) loop_expected.x[10], loop_expected.nzcv = 0, NUMERICAL_ZERO_CARRY_FLAGS loop_report, actual = invoke(loop, mixed_base, mixed_base, loop_initial, budget=16) if (loop_report['pc'] != STOP or bytes(actual) != bytes(loop_expected) or loop_report['outcome']['floating_point_helpers'] != NUMERICAL_CACHE_LOOP_ITERATIONS or loop_report['outcome']['memory_cache_hits'] < 2 * (NUMERICAL_CACHE_LOOP_ITERATIONS - 1)): raise AssertionError('Numerical loop did not preserve repeated helper/cache effects') original.mem_write(mixed_base, loop) original.ctl_remove_cache(mixed_base, mixed_base + len(loop)) initialize_arm(original, loop_initial) original.emu_start(mixed_base, STOP, count=16) if original.reg_read(arm.UC_ARM64_REG_PC) != STOP or bytes(capture_arm(original, loop_initial)) != bytes(loop_expected): raise AssertionError('Original loop fixture differs from complete runtime state') counts['memory_cache_loop_checks'] += 1 counts['synthetic_original_complete_state_checks'] += 1 half_record = next(record for record in oracle['records'] if record['operation']['name'] == 'fadd' and record['operation']['lane'] == 2 and record['label'] == 'one_one' and State.from_buffer_copy(record['initial']).fpcr == 0 and State.from_buffer_copy(record['initial']).fpsr == 0) half_initial = State.from_buffer_copy(half_record['initial']) half_expected = State.from_buffer_copy(half_record['expected']) half_source, repeated_source = numerical_half_chain_sources(half_record['operation']) half_base = BASE + 0x1c00 half_cache = directory / 'many-half-cache' cold, actual = invoke(half_source, half_base, half_base, half_initial, budget=NUMERICAL_MANY_HALF_OPERATIONS + 1, cache_dir=half_cache) if (cold['pc'] != STOP or bytes(actual) != bytes(half_expected) or cold['outcome']['compiled_blocks'] != NUMERICAL_MANY_HALF_OPERATIONS + 1 or cold['outcome']['floating_point_helpers'] != NUMERICAL_MANY_HALF_OPERATIONS): raise AssertionError('Many-H cold runtime changed full state or prepared a dead suffix') half_artifacts = numerical_half_cache_artifacts(half_cache, half_source, half_base, compiler) half_bytes = sum(artifact['native_bytes'] for artifact in half_artifacts) if half_bytes != NUMERICAL_MANY_HALF_OPERATIONS * NUMERICAL_HELPER_NATIVE_BYTES + NUMERICAL_RETURN_NATIVE_BYTES: raise AssertionError('Many-H cached native aggregate is not exactly141bytes') warm, actual = invoke(half_source, half_base, half_base, half_initial, budget=NUMERICAL_MANY_HALF_OPERATIONS + 1, cache_dir=half_cache) if (warm['pc'] != STOP or bytes(actual) != bytes(half_expected) or warm['outcome']['compiled_blocks'] != 0 or warm['outcome']['disk_cache_hits'] != NUMERICAL_MANY_HALF_OPERATIONS + 1): raise AssertionError('Many-H fresh runtime did not reuse all nine disk artifacts') budget_cache = directory / 'many-half-budget-cache' partial, intermediate = invoke(half_source, half_base, half_base, half_initial, budget=NUMERICAL_CACHE_LOOP_ITERATIONS, cache_dir=budget_cache) resume_pc = half_base + NUMERICAL_CACHE_LOOP_ITERATIONS * 4 if (partial['outcome']['reason']['kind'] != 'budget_exhausted' or partial['pc'] != resume_pc or bytes(intermediate) != bytes(half_expected) or partial['outcome']['floating_point_helpers'] != NUMERICAL_CACHE_LOOP_ITERATIONS): raise AssertionError('Many-H budget boundary skipped or repeated a helper') resumed, actual = invoke(half_source, half_base, resume_pc, intermediate, budget=NUMERICAL_MANY_HALF_OPERATIONS - NUMERICAL_CACHE_LOOP_ITERATIONS + 1, cache_dir=budget_cache) if (resumed['pc'] != STOP or bytes(actual) != bytes(half_expected) or resumed['outcome']['floating_point_helpers'] != NUMERICAL_MANY_HALF_OPERATIONS - NUMERICAL_CACHE_LOOP_ITERATIONS): raise AssertionError('Many-H budget resume failed to complete the suffix and RET') repeated_initial = State.from_buffer_copy(half_initial) repeated_initial.x[11], repeated_initial.x[30] = STOP, half_base + len(half_source) repeated_expected = State.from_buffer_copy(half_expected) repeated_expected.x[11] = STOP repeated, actual = invoke(repeated_source, half_base, half_base, repeated_initial, budget=2 * (NUMERICAL_MANY_HALF_OPERATIONS + 1) + 1, cache_dir=directory / 'many-half-live-cache') if (repeated['pc'] != STOP or bytes(actual) != bytes(repeated_expected) or repeated['outcome']['floating_point_helpers'] != 2 * NUMERICAL_MANY_HALF_OPERATIONS or repeated['outcome']['memory_cache_hits'] != NUMERICAL_MANY_HALF_OPERATIONS + 1): raise AssertionError('Many-H live runtime did not reuse exactly nine loaded entries') for code, initial, expected in ((half_source, half_initial, half_expected), (repeated_source, repeated_initial, repeated_expected)): original.mem_write(half_base, code) original.ctl_remove_cache(half_base, half_base + len(code)) initialize_arm(original, initial) original.emu_start(half_base, STOP, count=32) if original.reg_read(arm.UC_ARM64_REG_PC) != STOP or bytes(capture_arm(original, initial)) != bytes(expected): raise AssertionError('Many-H original instruction chain differs from native runtime state') counts['synthetic_original_complete_state_checks'] += 1 unsupported_source = half_source[:-4] + struct.pack('> 64] return initial def vector_numerical_corpus(foundation): """Pack the retained scalar directed pairs into active lanes. No native result or host floating-point arithmetic participates in input generation. """ functions = vector_numerical_functions() directed, controls = {}, set() for record in foundation['records']: operation = record['operation'] initial = State.from_buffer_copy(record['initial']) if (operation['d'], operation['n'], operation['m']) != (0, 1, 2) or record['unsupported'] or initial.fpsr: continue if record['label'] == 'zero_pp': controls.add(initial.fpcr) if initial.fpcr or record['label'].startswith(('alias_', 'bounded_', 'trap_')): continue mask = (1 << (operation['lane'] * 8)) - 1 pair = (initial.v[1][0] & mask, initial.v[2][0] & mask) directed.setdefault((operation['name'], operation['lane']), {}).setdefault(record['label'], pair) if len(controls) != 30 or any(len(pairs) != 64 for pairs in directed.values()) or len(directed) != 12: raise AssertionError('Vector numerical corpus needs the complete retained scalar directed foundation') records = [] def append(index, label, pairs, control=0, sticky=False): operation = functions[index] initial = vector_numerical_state(operation, len(records), pairs, control, sticky) records.append({'function': index, 'function_index': index, 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) rng = random.Random(0x564543465052414e) for index in range(0, len(functions), len(VECTOR_NUMERICAL_ROLES)): operation = functions[index] count, bits = operation['bytes'] // operation['lane'], operation['lane'] * 8 pairs = directed[(operation['name'], operation['lane'])] values = list(pairs.values()) for control in sorted(controls): for first in range(0, len(values), count): append(index, f'directed_pack_{first}', values[first:first + count], control) append(index, 'sticky_pack', values[:count], control, True) patterns = (('one_positive_halfulp', 'negative_one_positive_halfulp'), ('qnan_snan', 'reverse_qnan_qnan'), ('normal_boundary_midpoint', 'minimum_normal_half')) for role in range(1, len(VECTOR_NUMERICAL_ROLES)): for control in VECTOR_NUMERICAL_CONTROLS: for pattern, labels in enumerate(patterns): active = [pairs[labels[element % len(labels)]] for element in range(count)] for sticky in (False, True): append(index + role, f'alias_pattern_{pattern}', active, control, sticky) one = pairs['one_one'][0] half = pairs['minimum_normal_half'][1] for control in VECTOR_NUMERICAL_CONTROLS: for label in ('snan_left', 'minimum_normal_half', 'maximum_maximum'): for position in range(count): active = [(one, half)] * count active[position] = pairs[label] append(index, f'active_{label}_{position}', active, control) for iteration in range(16): active = [(rng.getrandbits(bits), rng.getrandbits(bits)) for _ in range(count)] append(index, 'bounded_random', active, sorted(controls)[iteration]) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(index, 'trap_enable_readback_only', [(one, half)] * count, control) if len(records) != VECTOR_NUMERICAL_CASES: raise AssertionError('Vector numerical corpus exceeded its approved bounded scope') return functions, records def prepare_vector_numerical_oracle(directory, numerical_path=NUMERICAL_FOUNDATION): foundation = load_numerical_oracle(numerical_path) functions, records = vector_numerical_corpus(foundation) directory = Path(directory) directory.mkdir(parents=True, exist_ok=True) inputs = b''.join(struct.pack('> 26 != FP_REGISTER_EC_SVC or current_el != 4 or cpacr != VECTOR_NUMERICAL_CPACR_FPEN or marker != VECTOR_NUMERICAL_ORIGINAL_MARKER or fp_register_elf_word(elf, elr - 8) != operation['word'] or fp_register_elf_word(elf, elr - 4) != FP_REGISTER_SVC or original.fpcr != actual_fpcr or actual_fpsr != initial.fpsr or actual_fpcr != initial.fpcr & ~NUMERICAL_TRAP_ENABLE_MASK): raise AssertionError(f'Full-frame original instruction/EL0/control evidence changed: {index}') untouched = State.from_buffer_copy(original) if family.get('original_effects'): family['original_effects'](initial, original, operation) untouched.v[operation['d']][:] = initial.v[operation['d']] elif focus in (ROUND_INTEGRAL_FOCUS, SQRT_NUMERICAL_FOCUS): validate_numerical_unary_original_effects(initial, original, operation) untouched.v[operation['d']][:] = initial.v[operation['d']] elif focus == COMPARISON_MINMAX_FOCUS: validate_comparison_minmax_original_effects(initial, original, operation) if operation['output_kind'] == 'flags': untouched.nzcv = initial.nzcv else: untouched.v[operation['d']][:] = initial.v[operation['d']] elif focus == CONVERSION_NUMERICAL_FOCUS: validate_conversion_original_effects(initial, original, operation) if operation['destination_bank'] == 'general': if operation['d'] != 31: untouched.x[operation['d']] = initial.x[operation['d']] else: untouched.v[operation['d']][:] = initial.v[operation['d']] if operation['writes_nzcv']: untouched.nzcv = initial.nzcv else: untouched.v[operation['d']][:] = initial.v[operation['d']] untouched.fpcr, untouched.fpsr = initial.fpcr, initial.fpsr if bytes(untouched) != bytes(initial) or original.fpsr & ~NUMERICAL_EXCEPTION_FLAGS != initial.fpsr & ~NUMERICAL_EXCEPTION_FLAGS: raise AssertionError('Original vector numerical operation changed unrelated state or QC') if (focus not in (CONVERSION_NUMERICAL_FOCUS, COMPARISON_MINMAX_FOCUS) and not operation['scalar'] and operation['bytes'] == 8 and original.v[operation['d']][1] != 0): raise AssertionError('Original Q0 numerical instruction did not clear upper 64 bits') if operation['scalar'] and 'a' in operation: active = (1 << (operation['lane'] * 8)) - 1 result = original.v[operation['d']][0] | original.v[operation['d']][1] << 64 addend = initial.v[operation['a']][0] | initial.v[operation['a']][1] << 64 merge = addend if initial.fpcr & NUMERICAL_NEP else 0 if result & ~active != merge & ~active: raise AssertionError('Original scalar fused upper state did not follow addend NEP merge') readbacks[(initial.fpcr, actual_fpcr)] += 1 # Only LR changes to let the native/runtime and Unicorn paths execute # the actual RET. The original subject instruction left every GPR intact. profile_expected = family['profile_expectation'](operation, initial, original, receipt) if family.get('profile_expectation') else None enabled = bool(initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK) enabled_success = enabled and bool(family.get('accepts_enabled_control') and family['accepts_enabled_control'](operation, initial)) policy_success = enabled_success if (not enabled or enabled_success) and family.get('original_control_policy'): policy_success |= bool(family['original_control_policy'](operation, initial, receipt)) return_register = operation.get('return_register', 30) initial.x[return_register] = original.x[return_register] = STOP primary_expected = bytes(original) if profile_expected is not None: profile_expected.x[return_register] = STOP expected = bytes(profile_expected) basis = 'standard_profile_model' else: expected_state = State.from_buffer_copy(original) if policy_success: expected_state.fpcr = initial.fpcr expected = bytes(expected_state) basis = 'requested_control_policy' if policy_success else 'direct_original' records.append({'function': function, 'initial': bytes(initial), 'expected': expected, 'primary_expected': primary_expected, 'profile_expected': bytes(profile_expected) if profile_expected is not None else None, 'equality_basis': basis, 'control_policy_success': policy_success, 'operation': operation, 'case': index, 'label': case['label'], 'producer': 'vector_el0' if focus == VECTOR_NUMERICAL_FOCUS else focus + '_el0', 'unsupported': enabled and not enabled_success}) return {'records': records, 'functions': expected_functions, 'receipt_sha256': receipt_hash, 'pointer_sha256': pointer_hash, 'producer': receipt, 'control_readbacks': [{'requested': requested, 'actual': actual, 'cases': count} for (requested, actual), count in sorted(readbacks.items())], 'input_derivation': ('Only the per-function unused return register becomes STOP for actual guest RET. Conversion input and output GPRs remain unchanged by transport derivation. ' if focus == CONVERSION_NUMERICAL_FOCUS else 'Only X30 becomes STOP for actual guest RET. All other imported bytes remain unchanged. ') + 'Original requested trap enables are retained. QEMU RAZ/WI readbacks, accepted requested-control policy and standard-versus-enhanced profile expectations are classified separately from direct original equality.'} def vector_numerical_runtime_cases(records): selected = {} for record in records: operation, initial = record['operation'], State.from_buffer_copy(record['initial']) shape = (operation['name'], operation['lane'], operation['bytes']) if record['unsupported']: selected.setdefault(('trap', *shape, initial.fpcr), record) elif initial.fpsr == 0: if initial.fpcr in VECTOR_NUMERICAL_CONTROLS: selected.setdefault(('control', *shape, initial.fpcr), record) if initial.fpcr == NUMERICAL_NEP: selected.setdefault(('alias', record['function']), record) flush = NUMERICAL_FZ16 if operation['lane'] == 2 else NUMERICAL_FZ if initial.fpcr in (0, NUMERICAL_AH, flush) and record['label'] in ( 'active_snan_left_0', 'active_minimum_normal_half_0', 'active_maximum_maximum_0'): selected.setdefault(('semantic', *shape, initial.fpcr, record['label']), record) return list({record['case']: record for record in selected.values()}.values()) FUSED_NUMERICAL_FOCUS = 'fused_numerical' FUSED_NUMERICAL_ORACLE = ROOT / '.stela/research/fused-numerical/oracle/receipt.json' FUSED_SCALAR_BASES = {'fmadd': 0x1f000000, 'fmsub': 0x1f008000, 'fnmadd': 0x1f200000, 'fnmsub': 0x1f208000} FUSED_VECTOR_BASES = {'fmla': (0x0e400c00, 0x0e20cc00), 'fmls': (0x0ec00c00, 0x0ea0cc00)} FUSED_INDEXED_BASES = {'fmla': 0x0f001000, 'fmls': 0x0f005000} FUSED_NATIVE_PREFIXES = ('vfmadd', 'vfmsub', 'vfnmadd', 'vfnmsub') FUSED_NATIVE_OPERATIONS = {'fmadd': 'vfmadd', 'fmla': 'vfmadd', 'fmsub': 'vfnmadd', 'fmls': 'vfnmadd', 'fnmadd': 'vfnmsub', 'fnmsub': 'vfmsub'} FUSED_SCALAR_ROLES = ((0, 1, 2, 3), (1, 1, 2, 3), (2, 1, 2, 3), (3, 1, 2, 3), (0, 1, 1, 3), (0, 1, 2, 1), (0, 1, 2, 2), (31, 31, 31, 31), (31, 30, 29, 28)) FUSED_ACCUMULATE_ROLES = ((0, 1, 2), (1, 1, 2), (2, 1, 2), (0, 1, 1), (31, 31, 31), (31, 30, 29)) FUSED_ROUNDING_CONTROLS = tuple(mode << 22 for mode in range(4)) FUSED_EXTRA_CONTROLS = (NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_NEP, NUMERICAL_AH | NUMERICAL_FIZ | NUMERICAL_NEP, NUMERICAL_FZ16, NUMERICAL_FZ, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_DN) def fused_numerical_operation(name, lane, width, scalar, d, n, m, a=None, index=None): if lane not in (2, 4, 8) or any(not 0 <= register < 32 for register in (d, n, m)): raise ValueError('Invalid fused format or register') if name in FUSED_SCALAR_BASES: if not scalar or width != lane or index is not None or a is None or not 0 <= a < 32: raise ValueError('Invalid scalar fused operand') word = FUSED_SCALAR_BASES[name] | {2: 3, 4: 0, 8: 1}[lane] << 22 | a << 10 elif name in FUSED_VECTOR_BASES: if a is not None and a != d: raise ValueError('FMLA/FMLS addend must be destination') a = d if scalar: if width != lane or index is None: raise ValueError('Scalar FMLA/FMLS requires an indexed operand') elif (lane, width) not in VECTOR_NUMERICAL_SHAPES: raise ValueError('Invalid fused vector arrangement') if index is None: half, standard = FUSED_VECTOR_BASES[name] word = half if lane == 2 else standard | int(lane == 8) << 22 word |= int(width == 16) << 30 else: if not 0 <= index < 16 // lane or lane == 2 and m >= 16: raise ValueError('Invalid fused indexed register or lane') word = FUSED_INDEXED_BASES[name] | int(scalar) << 28 word |= int(scalar or width == 16) << 30 if lane == 2: word |= ((index >> 2) & 1) << 11 | (index & 3) << 20 elif lane == 4: word |= 1 << 23 | ((index >> 1) & 1) << 11 | (index & 1) << 21 else: word |= 3 << 22 | index << 11 else: raise ValueError('Invalid fused mnemonic') word |= m << 16 | n << 5 | d return {'name': name, 'lane': lane, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'm': m, 'a': a, 'index': index, 'word': word} def fused_numerical_functions(): functions = [fused_numerical_operation(name, lane, lane, True, *roles) for name in FUSED_SCALAR_BASES for lane in (2, 4, 8) for roles in FUSED_SCALAR_ROLES] for name in FUSED_VECTOR_BASES: for lane, width in VECTOR_NUMERICAL_SHAPES: functions.extend(fused_numerical_operation(name, lane, width, False, *roles) for roles in FUSED_ACCUMULATE_ROLES) for scalar, lane, width in ([(True, lane, lane) for lane in (2, 4, 8)] + [(False, lane, width) for lane, width in VECTOR_NUMERICAL_SHAPES]): for index in range(16 // lane): for d, n, m in FUSED_ACCUMULATE_ROLES: # FP16 indexed Rm has four bits; Vd/Vn still reach V31. if lane == 2: m &= 15 functions.append(fused_numerical_operation(name, lane, width, scalar, d, n, m, index=index)) if len({operation['word'] for operation in functions}) != len(functions): raise AssertionError('Fused exact subject words must be unique') return functions def fused_numerical_triples(lane): bits, fraction = lane * 8, {2: 10, 4: 23, 8: 52}[lane] exponent_bits = bits - fraction - 1 bias = (1 << (exponent_bits - 1)) - 1 sign = 1 << (bits - 1) one, half, two = bias << fraction, (bias - 1) << fraction, (bias + 1) << fraction infinity = ((1 << exponent_bits) - 1) << fraction maximum, minimum = infinity - 1, 1 << fraction quiet, signaling = infinity | 1 << (fraction - 1) | 0x15, infinity | 0x2b cases = [('one_one_one', one, one, one), ('exact_product', one, two, one), ('single_round_residual', one | sign, one + 1, one - 2), ('single_round_positive', (one + 2) | sign, one + 1, one + 1), ('product_overflow_cancel', maximum | sign, maximum, two), ('product_underflow_accumulate', minimum, minimum, half), ('normal_boundary', minimum - 1, minimum, half), ('minimum_subnormal', 1, minimum, 1), ('maximum_finite', maximum, maximum, one), ('rounding_tie', one, (bias - fraction - 1) << fraction, one), ('tiny_residual', 1 | sign, 1, one + 1), ('inf_zero_qnan_addend', quiet, infinity, 0), ('inf_zero_finite_addend', one, infinity, 0), ('opposed_infinities', infinity | sign, infinity, one), ('same_infinities', infinity, infinity, one), ('snan_addend', signaling, one, one), ('snan_left', one, signaling, one), ('snan_right', one, one, signaling), ('qnan_addend_snan_left', quiet, signaling, one), ('qnan_addend_snan_right', quiet, one, signaling), ('qnan_left_snan_right', one, quiet, signaling), ('qnan_priority', quiet, quiet | sign | 0x100, quiet | 0x200), ('snan_priority', signaling, signaling | sign | 0x100, signaling | 0x200), ('negative_qnan', quiet | sign, one, one), ('denormal_addend', minimum - 1, one, half), ('denormal_left', one, minimum - 1, half), ('denormal_right', one, half, minimum - 1)] cases.extend((f'zero_signs_{signs}', sign if signs & 1 else 0, sign if signs & 2 else 0, sign if signs & 4 else 0) for signs in range(8)) return cases def fused_numerical_state(operation, index, triples, control=0, sticky=False): initial = State() rng = random.Random(0x46555345444650 + index) initial.x[:] = [rng.getrandbits(64) for _ in range(31)] initial.sp = 0x48010000 + index % 16 * 16 seed_extended_state(initial, index * 31 + 17) initial.reserved, initial.nzcv = VECTOR_NUMERICAL_CANARY, index % 16 << 28 initial.fpcr, initial.fpsr = control, FP_REGISTER_MASKS['fpsr'] if sticky else 0 lane, width = operation['lane'], operation['bytes'] bits, mask = lane * 8, (1 << (lane * 8)) - 1 count = 1 if operation['scalar'] else width // lane if len(triples) != count or any(not 0 <= value <= mask for triple in triples for value in triple): raise ValueError('Invalid fused input lane count/value') signaling = next(triple[1] for triple in fused_numerical_triples(lane) if triple[0] == 'snan_addend') poison = sum(signaling << (element * bits) for element in range(16 // lane)) # Deliberate aliases are resolved by these ordered writes. The frozen input # frame, rather than the requested triple, is the original/native oracle input. for register, side in ((operation['a'], 0), (operation['n'], 1), (operation['m'], 2)): value = poison if side == 2 and operation['index'] is not None: element = operation['index'] value = (value & ~(mask << (element * bits))) | triples[0][side] << (element * bits) else: for element, triple in enumerate(triples): value = (value & ~(mask << (element * bits))) | triple[side] << (element * bits) initial.v[register][:] = [value & MASK, value >> 64] return initial def fused_numerical_corpus(): functions, records = fused_numerical_functions(), [] primary = set() def append(function, label, triples, control=0, sticky=False): initial = fused_numerical_state(functions[function], len(records), triples, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function, operation in enumerate(functions): shape = tuple(operation[key] for key in ('name', 'lane', 'bytes', 'scalar')) + (operation['index'] is not None,) count = 1 if operation['scalar'] else operation['bytes'] // operation['lane'] triples = fused_numerical_triples(operation['lane']) directed = {label: (a, n, m) for label, a, n, m in triples} if shape not in primary: primary.add(shape) for control in FUSED_ROUNDING_CONTROLS: for label, a, n, m in triples: append(function, label, [(a, n, m)] * count, control) for control in FUSED_EXTRA_CONTROLS: for label, a, n, m in triples: if label.startswith(('snan_', 'qnan_', 'denormal_', 'single_round_', 'product_', 'zero_signs_')): append(function, label, [(a, n, m)] * count, control) a, n, m = directed['snan_addend'] append(function, 'sticky_snan_addend', [(a, n, m)] * count, control, True) if not operation['scalar']: for control in (0, NUMERICAL_AH, NUMERICAL_FZ16 if operation['lane'] == 2 else NUMERICAL_FZ): for position in range(count): for label in ('single_round_residual', 'product_underflow_accumulate', 'snan_addend'): active = [directed['one_one_one']] * count active[position] = directed[label] append(function, f'active_{label}_{position}', active, control) for enable in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): a, n, m = directed['one_one_one'] append(function, 'trap_enable_readback_only', [(a, n, m)] * count, enable) # Every alias and indexed selector sees exact arithmetic, single-round # cancellation and signaling input under both scalar merge policies. for control in (0, NUMERICAL_NEP): for label in ('one_one_one', 'single_round_residual', 'snan_addend'): a, n, m = directed[label] append(function, 'roles_' + label, [(a, n, m)] * count, control) if len(records) > 20000: raise AssertionError('Fused corpus exceeded approved bounded scope') return functions, records def prepare_full_frame_numerical_oracle(directory, functions, records, focus): directory = Path(directory) directory.mkdir(parents=True, exist_ok=True) files = {'functions.json': (json.dumps([{**operation, 'source_hex': struct.pack('= maximum_exponent: return None shift = leading - fraction if biased > 0 else 1 - bias - exponent - fraction if shift > 0 and significand & ((1 << shift) - 1): return None encoded = significand >> shift if shift > 0 else significand << -shift if biased <= 0: return encoded if 0 < encoded < 1 << fraction else None return biased << fraction | encoded & ((1 << fraction) - 1) def conversion_numerical_values(operation): bits, lane = operation['source_lane'] * 8, operation['source_lane'] if operation['name'] in ('scvtf', 'ucvtf'): sign, mask = 1 << (bits - 1), (1 << bits) - 1 precision = {2: 11, 4: 24, 8: 53}[operation['destination_lane']] values = [('exact', 2), ('zero', 0), ('one', 1), ('minus_one', mask), ('input_min', sign), ('input_max', mask if operation['unsigned'] else sign - 1), ('signed_max', sign - 1), ('signed_min_plus_one', sign + 1), ('unsigned_max_minus_one', mask - 1)] if precision < bits: values.extend((label, (1 << precision) + increment) for label, increment in (('inexact', 1), ('tie_even', 1), ('tie_odd', 3), ('above_tie', 5))) else: values.append(('inexact', sign - 1)) return values fraction = {2: 10, 4: 23, 8: 52}[lane] bias = {2: 15, 4: 127, 8: 1023}[lane] sign, one = 1 << (bits - 1), bias << fraction infinity = ((1 << (bits - fraction - 1)) - 1) << fraction values = [('exact', one + (1 << fraction)), ('zero', 0), ('negative_zero', sign), ('one', one), ('minus_one', one | sign), ('half', (bias - 1) << fraction), ('negative_half', ((bias - 1) << fraction) | sign), ('tie_even', one | (1 << (fraction - 1))), ('tie_odd', one + (1 << fraction) + (1 << (fraction - 1))), ('above_tie', one | (1 << (fraction - 1)) | 1), ('below_tie', (one | (1 << (fraction - 1))) - 1), ('inexact', one + 1), ('minimum_normal', 1 << fraction), ('maximum_subnormal', (1 << fraction) - 1), ('minimum_subnormal', 1), ('negative_subnormal', sign | 1), ('maximum_finite', infinity - 1), ('positive_infinity', infinity), ('negative_infinity', infinity | sign), ('qnan', infinity | 1 << (fraction - 1) | 0x15), ('negative_qnan', infinity | sign | 1 << (fraction - 1) | 0x15), ('snan', infinity | 0x2b), ('negative_snan', infinity | sign | 0x2b)] exponents = ({2: 10, 4: 23, 8: 52}[operation['destination_lane']] + 1,) if operation['name'] in ('fcvt', 'fcvtn', 'fcvtxn') else ( operation['integer_bits'] - int(not operation['unsigned']) - operation['fracbits'],) for exponent in exponents: biased = exponent + bias if 0 < biased < infinity >> fraction: boundary = biased << fraction values.extend((f'range_{side}_{offset:+}', ((boundary + offset) | (sign if side == 'negative' else 0))) for side in ('positive', 'negative') for offset in (-1, 0, 1)) if operation['name'] in ('fcvt', 'fcvtl', 'fcvtn', 'fcvtxn'): for exponent in (-25, -24, -14, 15, 16, 17, -150, -149, -126, 127, 128): if 0 < exponent + bias < infinity >> fraction: boundary = (exponent + bias) << fraction values.extend((f'precision_boundary_{exponent}_{offset:+}', boundary + offset) for offset in (-1, 0, 1)) dyadics = [] if operation['name'] in ('fcvt', 'fcvtn', 'fcvtxn'): target_fraction, target_bias = {2: (10, 15), 4: (23, 127), 8: (52, 1023)}[operation['destination_lane']] dyadics.extend((('target_max_finite', (1 << (target_fraction + 1)) - 1, target_bias - target_fraction), ('target_overflow_tie', (1 << (target_fraction + 2)) - 1, target_bias - target_fraction - 1), ('target_zero_tie', 1, 1 - target_bias - target_fraction - 1), ('target_normal_tie', (1 << (target_fraction + 1)) - 1, 1 - target_bias - target_fraction - 1))) if operation['destination_lane'] == 2: dyadics.extend((('alternative_half_max', 131008, 0), ('alternative_half_overflow_tie', 131040, 0))) elif operation['name'] not in ('scvtf', 'ucvtf', 'fcvtl'): integer_bits = 32 if operation['writes_nzcv'] else operation['integer_bits'] limit = integer_bits - int(not operation['unsigned']) scale = operation['fracbits'] dyadics.extend((('integer_max', (1 << limit) - 1, -scale), ('integer_limit_below_tie', (1 << (limit + 1)) - 1, -scale - 1), ('integer_limit_above_tie', (1 << (limit + 1)) + 1, -scale - 1))) for label, significand, exponent in dyadics: raw = conversion_exact_positive_bits(lane, significand, exponent) if raw is not None: values.extend(((label, raw), ('negative_' + label, raw | sign))) return values def conversion_numerical_state(operation, index, values, control=0, sticky=False): initial = State() rng = random.Random(0x434f4e56455254 + index) initial.x[:] = [rng.getrandbits(64) for _ in range(31)] initial.sp = 0x48010000 + index % 16 * 16 seed_extended_state(initial, index * 31 + 19) initial.reserved, initial.nzcv = VECTOR_NUMERICAL_CANARY, index % 16 << 28 initial.fpcr, initial.fpsr = control, FP_REGISTER_MASKS['fpsr'] if sticky else 0 lane, bits = operation['source_lane'], operation['source_lane'] * 8 count = 1 if operation['scalar'] else operation['source_bytes'] // lane if len(values) != count or any(not 0 <= value < 1 << bits for value in values): raise ValueError('Invalid conversion input lanes') if operation['source_bank'] == 'general': if operation['n'] != 31: initial.x[operation['n']] = values[0] | (rng.getrandbits(32) << 32 if bits == 32 else 0) else: poison = ((1 << bits) - 1) if operation['name'] in ('scvtf', 'ucvtf') else next( value for label, value in conversion_numerical_values(operation) if label == 'snan') packed = sum(poison << (position * bits) for position in range(16 // lane)) start = operation['source_part'] * (8 // lane) for position, value in enumerate(values, start): mask = ((1 << bits) - 1) << (position * bits) packed = (packed & ~mask) | value << (position * bits) initial.v[operation['n']][:] = [packed & MASK, packed >> 64] return initial def conversion_numerical_corpus(): functions, records, primary, declines = conversion_numerical_functions(), [], set(), set() def append(function, label, values, control=0, sticky=False): initial = conversion_numerical_state(functions[function], len(records), values, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function, operation in enumerate(functions): shape = conversion_numerical_shape(operation) count = 1 if operation['scalar'] else operation['source_bytes'] // operation['source_lane'] values = dict(conversion_numerical_values(operation)) if shape not in primary: primary.add(shape) for label, value in values.items(): append(function, label, [value] * count) for control in FUSED_ROUNDING_CONTROLS[1:]: for label in ('half', 'negative_half', 'tie_even', 'tie_odd', 'inexact'): if label in values: append(function, label, [values[label]] * count, control) for label, value in values.items(): if label.startswith(('target_', 'negative_target_')): for control in FUSED_ROUNDING_CONTROLS[1:]: append(function, label, [value] * count, control) if label.startswith(('alternative_half_', 'negative_alternative_half_')): for control in FUSED_ROUNDING_CONTROLS: append(function, label, [value] * count, control | CONVERSION_AHP) for control in CONVERSION_EXTRA_CONTROLS: for label in (('input_max', 'input_min', 'inexact') if operation['name'] in ('scvtf', 'ucvtf') else ('snan', 'minimum_subnormal', 'inexact')): append(function, label, [values[label]] * count, control) for control in (0, NUMERICAL_NEP): label = 'input_max' if operation['name'] in ('scvtf', 'ucvtf') else 'snan' append(function, 'sticky_' + label, [values[label]] * count, control, True) if count > 1: for position in range(count): active = [values['exact']] * count label = 'input_max' if operation['name'] in ('scvtf', 'ucvtf') else 'snan' active[position] = values[label] append(function, f'active_{label}_{position}', active) form = operation['canonical_id'] if form not in declines: declines.add(form) for enable in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_enable_readback_only', [values['exact']] * count, enable) for control in (0, NUMERICAL_NEP): for label in ('exact', 'inexact'): append(function, 'roles_' + label, [values[label]] * count, control) if len(records) > 35000: raise AssertionError('Conversion corpus exceeded the approved bounded scope') return functions, records def prepare_conversion_numerical_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *conversion_numerical_corpus(), CONVERSION_NUMERICAL_FOCUS) def load_conversion_numerical_oracle(path=CONVERSION_NUMERICAL_ORACLE): functions, records = conversion_numerical_corpus() return load_full_frame_numerical_oracle(path, functions, len(records), CONVERSION_NUMERICAL_FOCUS) def conversion_unicorn_flushed_javascript_difference(operation, initial, observed, expected): if (operation['name'] != 'fjcvtzs' or not initial.fpcr & NUMERICAL_FZ or initial.fpcr & NUMERICAL_AH or observed.nzcv != 0x40000000 or expected.nzcv != 0): return False source = initial.v[operation['n']][0] fraction_mask, exponent_mask = (1 << 52) - 1, 0x7ff << 52 if source & exponent_mask or not source & fraction_mask: return False copied = State.from_buffer_copy(observed) copied.nzcv = expected.nzcv return bytes(copied) == bytes(expected) def conversion_unicorn_difference(operation, initial, observed, expected): if conversion_unicorn_flushed_javascript_difference(operation, initial, observed, expected): return 'Unicorn FJCVTZS sets Z for a flushed subnormal; pinned source and full-frame QEMU clear Z' if (operation['name'] != 'fcvtzs' or not operation.get('fracbits') or operation.get('source_lane') != 2 or operation.get('destination_lane') != 2 or operation.get('destination_bank') != 'simd' or not operation['scalar'] or initial.fpcr & NUMERICAL_NEP): return None destination = operation['d'] result = expected.v[destination][0] if not result & (1 << 15) or result >> 16 or observed.v[destination][0] != result | 0xffff0000: return None copied = State.from_buffer_copy(observed) copied.v[destination][0] = result if bytes(copied) != bytes(expected): return None return 'Unicorn signed scalar FP16 fixed FCVTZS sign-extends through bit31; pinned source and full-frame QEMU clear inactive bits' def validate_conversion_original_effects(initial, original, operation): destination, lane = operation['d'], operation['destination_lane'] if operation['destination_bank'] == 'general': if destination != 31 and lane == 4 and original.x[destination] >> 32: raise AssertionError('Original W conversion did not zero-extend') if operation['writes_nzcv'] and original.nzcv not in (0, 0x40000000): raise AssertionError('FJCVTZS wrote NZCV bits other than Z') return original_bytes = struct.pack('<2Q', *original.v[destination]) initial_bytes = struct.pack('<2Q', *initial.v[destination]) if operation['scalar']: upper = initial_bytes[lane:] if initial.fpcr & NUMERICAL_NEP else bytes(16 - lane) if original_bytes[lane:] != upper: raise AssertionError('Original scalar conversion did not merge old Vd or clear upper bits') elif operation['destination_part']: if original_bytes[:8] != initial_bytes[:8]: raise AssertionError('Original narrowing2 conversion changed preserved low64') elif operation['bytes'] == 8 and original_bytes[8:] != bytes(8): raise AssertionError('Original vector64 conversion did not clear upper64') def conversion_numerical_runtime_cases(records): selected = {} for record in records: operation, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) form, shape = operation['canonical_id'], conversion_numerical_shape(operation) direction = ('precision' if operation['name'] in ('fcvt', 'fcvtl', 'fcvtn', 'fcvtxn') else 'from_integer' if operation['name'] in ('scvtf', 'ucvtf') else 'javascript' if operation['writes_nzcv'] else 'to_integer') if record['unsupported']: if initial.fpcr == NUMERICAL_TRAP_ENABLE_MASK: selected.setdefault(('decline_form', form), record) else: selected.setdefault(('decline_control', initial.fpcr), record) elif initial.fpsr == 0: if label == 'exact' and initial.fpcr == 0: selected.setdefault(('shape', shape), record) if label == 'inexact' and initial.fpcr == 0: selected.setdefault(('corner', form), record) if label in ('snan', 'input_max') and initial.fpcr == 0: selected.setdefault(('status', direction, operation['source_lane'], operation['destination_lane']), record) if label == 'inexact' and initial.fpcr in (NUMERICAL_NEP, NUMERICAL_AH, CONVERSION_AHP): selected.setdefault(('control', direction, operation['source_lane'], operation['destination_lane'], initial.fpcr), record) if operation['return_register'] != 30 and label == 'roles_inexact' and initial.fpcr == 0: selected.setdefault(('lr_operand', form), record) return list({record['case']: record for record in selected.values()}.values()) def conversion_numerical_helpers(records): selected = {} for record in records: operation = record['operation'] if operation['d'] != 0 or operation['n'] != 1 or State.from_buffer_copy(record['initial']).fpcr != 0: continue if operation['name'] in ('scvtf', 'ucvtf') or record['label'] != 'snan': continue key = (operation['source_bank'], operation['destination_bank'], operation['source_lane'], operation['destination_lane'], operation['scalar'], operation['source_part'], operation['destination_part']) selected.setdefault(key, record) return list(selected.values()) COMPARISON_MINMAX_FOCUS = 'comparison_minmax_numerical' COMPARISON_MINMAX_ORACLE = ROOT / '.stela/research/comparison-minmax/oracle/receipt.json' COMPARISON_REGISTER_BASES = {'facge': (0x2e402c00, 0x2e20ec00), 'facgt': (0x2ec02c00, 0x2ea0ec00), 'fcmeq': (0x0e402400, 0x0e20e400), 'fcmge': (0x2e402400, 0x2e20e400), 'fcmgt': (0x2ec02400, 0x2ea0e400)} COMPARISON_ZERO_BASES = {'fcmeq': 0x0ea0d800, 'fcmge': 0x2ea0c800, 'fcmgt': 0x0ea0c800, 'fcmle': 0x2ea0d800, 'fcmlt': 0x0ea0e800} MINMAX_NAMES = ('fmax', 'fmin', 'fmaxnm', 'fminnm') COMPARISON_MINMAX_BINARY_ROLES = ((0, 1, 2), (1, 1, 2), (2, 1, 2), (31, 31, 31), (31, 30, 29), (0, 1, 1)) COMPARISON_MINMAX_UNARY_ROLES = ((0, 1), (1, 1), (31, 31), (31, 30)) COMPARISON_FLAG_ROLES = ((1, 2), (31, 31), (30, 29)) COMPARISON_MINMAX_CONTROLS = (NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_NEP, NUMERICAL_FZ, NUMERICAL_FZ16, NUMERICAL_DN, CONVERSION_AHP, NUMERICAL_AH | NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_FIZ) COMPARISON_MINMAX_ZERO_CONTROLS = (0, *COMPARISON_MINMAX_CONTROLS[:-1]) def numerical_condition_holds(condition, nzcv): if not 0 <= condition < 16: raise ValueError('Invalid condition field') n, z, c, v = (bool(nzcv & (1 << bit)) for bit in (31, 30, 29, 28)) base = (z, c, n, v, c and not z, n == v, n == v and not z, True)[condition >> 1] return not base if condition & 1 and condition != 15 else base def comparison_minmax_operation(name, lane, width, scalar, d=0, n=1, m=2, family='predicate', zero=False, condition=None, immediate=0): if lane not in (2, 4, 8) or any(not 0 <= register < 32 for register in (d, n, m)): raise ValueError('Invalid FP comparison/minmax format or register') letter = {2: 'H', 4: 'S', 8: 'D'}[lane] ftype = {2: 3, 4: 0, 8: 1}[lane] merge, output_kind = 'none', 'vector' if family == 'flag_compare': if not scalar or width != lane or name not in ('fcmp', 'fcmpe', 'fccmp', 'fccmpe'): raise ValueError('Invalid flag comparison shape') output_kind = 'flags' if name.startswith('fccmp'): if zero or condition is None or not 0 <= condition < 16 or not 0 <= immediate < 16: raise ValueError('Invalid conditional comparison fields') word = 0x1e200400 | int(name == 'fccmpe') << 4 | condition << 12 | immediate canonical = f'{name.upper()}_{letter}_floatccmp' else: if condition is not None or immediate: raise ValueError('Unconditional comparison has no condition/immediate') word = 0x1e202000 | int(name == 'fcmpe') << 4 | int(zero) << 3 canonical = f"{name.upper()}_{letter}{'Z' if zero else ''}_floatcmp" word |= ftype << 22 | m << 16 | n << 5 source_lanes = 1 elif family == 'predicate': if condition is not None or immediate: raise ValueError('Predicate has no conditional flag fields') if not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES or scalar and width != lane: raise ValueError('Unallocated predicate arrangement') if zero: if name not in COMPARISON_ZERO_BASES: raise ValueError('Unallocated zero predicate') word = COMPARISON_ZERO_BASES[name] | (0x00580000 if lane == 2 else int(lane == 8) << 22) suffix = ('asisdmisc' if scalar else 'asimdmisc') + ('fp16_FZ' if lane == 2 else '_FZ') else: if name not in COMPARISON_REGISTER_BASES: raise ValueError('Unallocated register predicate') half, standard = COMPARISON_REGISTER_BASES[name] word = half if lane == 2 else standard | int(lane == 8) << 22 word |= m << 16 suffix = ('asisdsame' if scalar else 'asimdsame') + ('fp16_only' if lane == 2 else '_only') merge = 'right_if_nep' if scalar else 'none' word |= 0x50000000 if scalar else int(width == 16) << 30 word |= n << 5 | d canonical, source_lanes = name.upper() + '_' + suffix, 1 if scalar else width // lane elif family in ('minmax_binary', 'minmax_pair', 'minmax_reduce'): base = next((candidate for candidate in MINMAX_NAMES if name == candidate or name == candidate + 'p' or name == candidate + 'v'), None) if base is None or zero or condition is not None or immediate: raise ValueError('Invalid minmax mnemonic or flag fields') minimum, number = base.startswith('fmin'), base.endswith('nm') if family == 'minmax_binary': if name != base or scalar and width != lane or not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES: raise ValueError('Unallocated binary minmax shape') if scalar: word = (0x1e206800 if number else 0x1e204800) | int(minimum) << 12 | ftype << 22 canonical, merge = f'{name.upper()}_{letter}_floatdp2', 'left_if_nep' else: word = (0x0e400400 if number else 0x0e403400) if lane == 2 else (0x0e20c400 if number else 0x0e20f400) | int(lane == 8) << 22 word |= int(minimum) << 23 | int(width == 16) << 30 canonical = name.upper() + ('_asimdsamefp16_only' if lane == 2 else '_asimdsame_only') word |= m << 16 source_lanes = 1 if scalar else width // lane elif family == 'minmax_pair': if name != base + 'p': raise ValueError('Invalid pairwise minmax mnemonic') if scalar: if width != lane: raise ValueError('Scalar pair reads exactly two source lanes') word = (0x5e30c800 if number else 0x5e30f800) if lane == 2 else (0x7e30c800 if number else 0x7e30f800) | int(lane == 8) << 22 word |= int(minimum) << 23 canonical, source_lanes = name.upper() + ('_asisdpair_only_H' if lane == 2 else '_asisdpair_only_SD'), 2 else: if (lane, width) not in VECTOR_NUMERICAL_SHAPES: raise ValueError('Invalid vector pairwise arrangement') word = (0x2e400400 if number else 0x2e403400) if lane == 2 else (0x2e20c400 if number else 0x2e20f400) | int(lane == 8) << 22 word |= int(minimum) << 23 | int(width == 16) << 30 | m << 16 canonical, source_lanes = name.upper() + ('_asimdsamefp16_only' if lane == 2 else '_asimdsame_only'), width // lane else: if name != base + 'v' or not scalar or (lane, width) not in ((2, 8), (2, 16), (4, 16)): raise ValueError('Invalid across-vector minmax arrangement') word = ((0x0e30c800 if number else 0x0e30f800) | int(width == 16) << 30) if lane == 2 else (0x6e30c800 if number else 0x6e30f800) word |= int(minimum) << 23 canonical, source_lanes = name.upper() + ('_asimdall_only_H' if lane == 2 else '_asimdall_only_SD'), width // lane word |= n << 5 | d else: raise ValueError('Unknown comparison/minmax family') return {'name': name, 'canonical_id': canonical, 'cohort_family': 'comparison' if family in ('flag_compare', 'predicate') else 'minmax', 'family': family, 'lane': lane, 'bytes': lane if scalar else width, 'source_bytes': source_lanes * lane, 'scalar': scalar, 'source_lane_count': source_lanes, 'output_kind': output_kind, 'merge': merge, 'd': d, 'n': n, 'm': m, 'zero_form': zero, 'condition': condition, 'immediate_nzcv': immediate, 'word': word, 'return_register': 30} def comparison_minmax_functions(): functions = [] for name in ('fcmp', 'fcmpe'): for lane in (2, 4, 8): for n, m in COMPARISON_FLAG_ROLES: functions.append(comparison_minmax_operation(name, lane, lane, True, n=n, m=m, family='flag_compare')) for n, m in ([(1, m) for m in range(32)] + [(n, m) for n in (31, 30) for m in (0, 31)]): functions.append(comparison_minmax_operation(name, lane, lane, True, n=n, m=m, family='flag_compare', zero=True)) for name in ('fccmp', 'fccmpe'): for lane in (2, 4, 8): for condition in range(16): for immediate in range(16): functions.append(comparison_minmax_operation(name, lane, lane, True, family='flag_compare', condition=condition, immediate=immediate)) for n, m in COMPARISON_FLAG_ROLES[1:]: functions.append(comparison_minmax_operation(name, lane, lane, True, n=n, m=m, family='flag_compare', condition=0)) arrangements = [(lane, lane, True) for lane in (2, 4, 8)] + [(lane, width, False) for lane, width in VECTOR_NUMERICAL_SHAPES] for zero, names in ((False, COMPARISON_REGISTER_BASES), (True, COMPARISON_ZERO_BASES)): for name in names: for lane, width, scalar in arrangements: roles = [(d, n, 0) for d, n in COMPARISON_MINMAX_UNARY_ROLES] if zero else COMPARISON_MINMAX_BINARY_ROLES functions.extend(comparison_minmax_operation(name, lane, width, scalar, d, n, m, zero=zero) for d, n, m in roles) for base in MINMAX_NAMES: for lane, width, scalar in arrangements: functions.extend(comparison_minmax_operation(base, lane, width, scalar, d, n, m, 'minmax_binary') for d, n, m in COMPARISON_MINMAX_BINARY_ROLES) roles = [(d, n, 0) for d, n in COMPARISON_MINMAX_UNARY_ROLES] if scalar else COMPARISON_MINMAX_BINARY_ROLES functions.extend(comparison_minmax_operation(base + 'p', lane, width, scalar, d, n, m, 'minmax_pair') for d, n, m in roles) for lane, width in ((2, 8), (2, 16), (4, 16)): functions.extend(comparison_minmax_operation(base + 'v', lane, width, True, d, n, 0, 'minmax_reduce') for d, n in COMPARISON_MINMAX_UNARY_ROLES) if len(functions) != 2590 or len({op['word'] for op in functions}) != len(functions) or len({op['canonical_id'] for op in functions}) != 102: raise AssertionError('Comparison/minmax complete form inventory changed') return functions def comparison_minmax_shape(operation): return tuple(operation[key] for key in ('name', 'family', 'lane', 'bytes', 'source_bytes', 'scalar', 'zero_form')) def comparison_minmax_values(lane): fraction, bias = {2: (10, 15), 4: (23, 127), 8: (52, 1023)}[lane] sign, one = 1 << (lane * 8 - 1), bias << fraction infinity = ((1 << (lane * 8 - fraction - 1)) - 1) << fraction return [0, sign, one, one | sign, infinity, infinity | sign, infinity | 1 << (fraction - 1) | 0x15, infinity | sign | 1 << (fraction - 1) | 0x29, infinity | 0x2b, infinity | sign | 0x35], [1, sign | 1, (1 << fraction) - 1, sign | ((1 << fraction) - 1)] def comparison_minmax_layouts(operation): values, denormals = comparison_minmax_values(operation['lane']) count = operation['source_lane_count'] if count == 2: return [(list(pair), list(reversed(pair))) for pair in [(left, right) for left in values for right in values][:64]] layouts = [] for position in range(count): for special in values[6:]: source = [values[2]] * count source[position] = special layouts.append((source, list(reversed(source)))) # Distinct payloads and zeros straddle every tree level. Pair-vector layouts # retain separate sources so neither the operand order nor output halves fold. fraction = {2: 10, 4: 23, 8: 52}[operation['lane']] two, three = values[2] + (1 << fraction), values[2] + (1 << fraction) + (1 << (fraction - 1)) patterns = [values, list(reversed(values)), [values[8], values[2], two, three], [values[8], three, values[2], two], [values[0], values[1]], [values[1], values[0]], denormals, list(reversed(denormals))] for index in range(64 - len(layouts)): pattern = patterns[index % len(patterns)] source = [pattern[(position + index // len(patterns)) % len(pattern)] for position in range(count)] right = [pattern[(count - position + index + 1) % len(pattern)] for position in range(count)] layouts.append((source, right)) return layouts def comparison_minmax_state(operation, index, left, right=None, control=0, flags=None, sticky=False): initial = State() rng = random.Random(0x434f4d504d494e + index) initial.x[:] = [rng.getrandbits(64) for _ in range(31)] initial.sp = 0x48010000 + index % 16 * 16 seed_extended_state(initial, index * 31 + 23) initial.reserved, initial.nzcv = VECTOR_NUMERICAL_CANARY, (index % 16 << 28) if flags is None else flags initial.fpcr, initial.fpsr = control, FP_REGISTER_MASKS['fpsr'] if sticky else 0 count, bits = operation['source_lane_count'], operation['lane'] * 8 if len(left) != count or right is not None and len(right) != count: raise ValueError('Incorrect comparison/minmax source lane count') poison = comparison_minmax_values(operation['lane'])[0][8] for register, lanes in ((operation['n'], left), (operation['m'], right)): if lanes is None: continue packed = sum(poison << (position * bits) for position in range(16 // operation['lane'])) for position, value in enumerate(lanes): if not 0 <= value < 1 << bits: raise ValueError('Invalid comparison/minmax input lane') mask = ((1 << bits) - 1) << (position * bits) packed = packed & ~mask | value << (position * bits) initial.v[register][:] = [packed & MASK, packed >> 64] return initial def comparison_minmax_corpus(): functions, records, primary, ah_primary, enable_forms = comparison_minmax_functions(), [], set(), set(), set() def append(function, label, left, right=None, control=0, flags=None, sticky=False): initial = comparison_minmax_state(functions[function], len(records), left, right, control, flags, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function, op in enumerate(functions): values, denormals = comparison_minmax_values(op['lane']) count, shape = op['source_lane_count'], comparison_minmax_shape(op) pair = lambda left, right: ([left] * count, [right] * count) controls = COMPARISON_MINMAX_CONTROLS direct_pairs = [(values[8], values[6]), (values[6], values[8]), (values[6], denormals[0]), (denormals[0], values[6]), (values[2], denormals[0]), (denormals[0], values[2]), (values[0], values[1]), (values[1], values[0]), (values[6], values[7]), (values[9], values[6])] if op['condition'] is not None: passing = next(flags << 28 for flags in range(16) if numerical_condition_holds(op['condition'], flags << 28)) failing = next((flags << 28 for flags in range(16) if not numerical_condition_holds(op['condition'], flags << 28)), None) if op['n'] == 1 and op['m'] == 2: append(function, 'condition_taken', *pair(values[2], values[2]), flags=passing) if failing is not None: append(function, 'condition_failed_snan', *pair(values[8], denormals[0]), flags=failing) if op['immediate_nzcv'] == 0: for flags in range(16): append(function, 'all_incoming_nzcv', *pair(values[8], denormals[0]), flags=flags << 28) for enable in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'enabled_condition_taken', *pair(values[8], denormals[0]), control=enable, flags=passing) if failing is not None: append(function, 'enabled_condition_failed', *pair(values[8], denormals[0]), control=enable, flags=failing) if op['condition'] == 0: for control in controls: for left, right in (direct_pairs + [(values[6], values[2]), (values[2], values[6]), (denormals[1], values[0]), (values[0], denormals[1])]): append(function, 'conditional_semantic', *pair(left, right), control=control, flags=passing) else: for control in (0, NUMERICAL_TRAP_ENABLE_MASK): append(function, 'conditional_roles_taken', *pair(values[8], denormals[0]), control=control, flags=passing) append(function, 'conditional_roles_failed', *pair(values[8], denormals[0]), control=control, flags=failing) continue if op['canonical_id'] not in enable_forms: enable_forms.add(op['canonical_id']) for enable in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'enabled_unconditional', [values[8]] * count, None if op['zero_form'] or op['scalar'] and op['family'] == 'minmax_pair' or op['family'] == 'minmax_reduce' else [denormals[0]] * count, enable) if shape not in primary: primary.add(shape) if op['zero_form']: unary = values + denormals for control in COMPARISON_MINMAX_ZERO_CONTROLS: for index, value in enumerate(unary if op['family'] == 'flag_compare' else unary[:12]): append(function, f'zero_value_{index}', [value] * count, control=control) elif op['family'] in ('minmax_pair', 'minmax_reduce'): layouts = comparison_minmax_layouts(op) for index, (left, right) in enumerate(layouts): append(function, f'layout_{index}', left, right if not op['scalar'] else None) for control in controls: for index, (left, right) in enumerate([*layouts[:4], *layouts[-2:]]): append(function, f'control_layout_{index}', left, right if not op['scalar'] else None, control, sticky=control == NUMERICAL_NEP and index == 3) else: for left_index, left in enumerate(values): for right_index, right in enumerate(values): append(function, f'matrix_{left_index}_{right_index}', *pair(left, right)) if op['family'] != 'flag_compare' and (op['name'], op['lane']) not in ah_primary: ah_primary.add((op['name'], op['lane'])) for left_index, left in enumerate(values): for right_index, right in enumerate(values): append(function, f'ah_matrix_{left_index}_{right_index}', *pair(left, right), control=NUMERICAL_AH) special_pairs = direct_pairs + ([(values[6], values[2]), (values[2], values[6])] if op['family'] == 'flag_compare' else []) for control in controls: for index, (left, right) in enumerate(special_pairs): append(function, f'control_pair_{index}', *pair(left, right), control) denormal_pairs = [(left, right) for left in denormals for right in (values[0], values[2], values[6], values[8])] for index, (left, right) in enumerate(denormal_pairs[:12 if op['family'] == 'flag_compare' else 16]): append(function, f'denormal_pair_{index}', *pair(left, right)) else: for control in (0, NUMERICAL_NEP): append(function, 'roles_snan' if control else 'roles_exact', [values[8] if control else values[2]] * count, None if op['zero_form'] or op['scalar'] and op['family'] == 'minmax_pair' or op['family'] == 'minmax_reduce' else [values[2]] * count, control, sticky=control == NUMERICAL_NEP) if len(records) != 39724: raise AssertionError(f'Comparison/minmax planned corpus framing changed: {len(records)}') return functions, records def prepare_comparison_minmax_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *comparison_minmax_corpus(), COMPARISON_MINMAX_FOCUS) def load_comparison_minmax_oracle(path=COMPARISON_MINMAX_ORACLE): functions, records = comparison_minmax_corpus() return load_full_frame_numerical_oracle(path, functions, len(records), COMPARISON_MINMAX_FOCUS) def validate_comparison_minmax_original_effects(initial, original, operation): if operation['output_kind'] == 'flags': if operation['condition'] is not None and not numerical_condition_holds(operation['condition'], initial.nzcv): if original.nzcv != operation['immediate_nzcv'] << 28 or original.fpsr != initial.fpsr: raise AssertionError('Failed conditional comparison evaluated arithmetic or lost fallback NZCV') return lane, destination = operation['lane'], operation['d'] if operation['scalar']: merge_register = operation['m'] if operation['merge'] == 'right_if_nep' else operation['n'] merge = struct.pack('<2Q', *initial.v[merge_register]) if operation['merge'] != 'none' and initial.fpcr & NUMERICAL_NEP else bytes(16) if struct.pack('<2Q', *original.v[destination])[lane:] != merge[lane:]: raise AssertionError('Comparison/minmax scalar inactive destination bits violated merge contract') elif operation['bytes'] == 8 and original.v[destination][1] != 0: raise AssertionError('Comparison/minmax vector64 did not clear upper64') def fused_numerical_shape(operation): return tuple(operation[key] for key in ('name', 'lane', 'bytes', 'scalar')) + (operation['index'] is not None,) def fused_numerical_form(operation): if operation['name'] in FUSED_SCALAR_BASES: return operation['name'], operation['lane'] return (operation['name'], operation['scalar'], operation['index'] is not None, 'h' if operation['lane'] == 2 else 'sd') def fused_numerical_runtime_cases(records): selected = {} for record in records: operation = record['operation'] initial = State.from_buffer_copy(record['initial']) control, label = initial.fpcr, record['label'] form, shape = fused_numerical_form(operation), fused_numerical_shape(operation) if record['unsupported']: if control == NUMERICAL_TRAP_ENABLE_MASK: selected.setdefault(('trap_form', form), record) else: selected.setdefault(('trap_format', operation['lane'], control), record) elif initial.fpsr == 0: if label == 'one_one_one' and control == 0: selected.setdefault(('shape', shape), record) if label == 'single_round_residual' and control in (*FUSED_ROUNDING_CONTROLS, NUMERICAL_AH, NUMERICAL_NEP): selected.setdefault(('fused_control', form, control), record) if label in ('snan_addend', 'product_overflow_cancel') and control == 0: selected.setdefault(('semantic', form, label), record) if label == 'product_underflow_accumulate' and control == 0 and operation['name'] == 'fmadd': selected.setdefault(('underflow_format', operation['lane']), record) if not operation['scalar'] and control == 0 and label.startswith('active_snan_addend_'): selected[('mixed_status', shape)] = record if (operation['name'] == 'fmla' and operation['index'] not in (None, 0) and (operation['scalar'] or operation['bytes'] == 16) and control == 0 and label == 'roles_one_one_one'): selected.setdefault(('index', shape, operation['index']), record) if operation['name'] == 'fmadd' and operation['lane'] == 4 and label == 'roles_single_round_residual' and control == NUMERICAL_NEP: selected.setdefault(('alias', record['function']), record) return list({record['case']: record for record in selected.values()}.values()) def fused_numerical_helpers(records): selected = {} for record in records: operation = record['operation'] if record['label'] == 'snan_addend' and State.from_buffer_copy(record['initial']).fpcr == 0: # One scalar, vector, scalar-indexed and vector-indexed representative # per format covers each numerical boundary without duplicating aliases. kind = ('scalar' if operation['name'] in FUSED_SCALAR_BASES else ('scalar_indexed' if operation['scalar'] else 'vector_indexed' if operation['index'] is not None else 'vector')) selected.setdefault((kind, operation['lane']), record) return list(selected.values()) def comparison_minmax_runtime_cases(records): selected = {} for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) form, shape = op['canonical_id'], comparison_minmax_shape(op) if record['unsupported']: if initial.fpcr == NUMERICAL_TRAP_ENABLE_MASK: selected.setdefault(('refusal_form', form), record) selected.setdefault(('refusal_mask', op['cohort_family'], initial.fpcr), record) elif record.get('control_policy_success'): selected.setdefault(('failed_enabled_condition', op['condition'], initial.fpcr), record) selected.setdefault(('failed_enabled_form', form), record) elif op['condition'] is not None: if op['immediate_nzcv'] == 0 and label in ('condition_taken', 'condition_failed_snan'): selected.setdefault(('conditional_path', form, op['condition'], label), record) if op['condition'] == 0 and label == 'condition_failed_snan': selected.setdefault(('fallback', form, op['immediate_nzcv']), record) else: if initial.fpcr == 0 and initial.fpsr == 0: selected.setdefault(('shape', shape), record) if label in ('matrix_8_6', 'matrix_6_8', 'ah_matrix_8_6', 'ah_matrix_6_8'): selected.setdefault(('nan_priority', form, label), record) if op['merge'] != 'none' and initial.fpcr == NUMERICAL_NEP: selected.setdefault(('merge', form), record) if op['family'] == 'minmax_reduce' and label in ('layout_18', 'layout_19', 'layout_34', 'layout_35'): selected.setdefault(('tree', shape, label), record) if initial.fpsr: selected.setdefault(('sticky', form), record) return list({record['case']: record for record in selected.values()}.values()) def comparison_minmax_helpers(records, case_ids=None): selected = {} for record in records: op = record['operation'] initial = State.from_buffer_copy(record['initial']) if (case_ids is not None and record['case'] not in case_ids or initial.fpcr or initial.fpsr or op['condition'] is not None or op['d'] != 0 or op['n'] != 1): continue selected.setdefault((op['family'], op['lane'], op['scalar'], op['bytes'], op['source_bytes'], op['zero_form']), record) return list(selected.values()) def vector_numerical_helpers(records): return [record for record in records if record['label'] == 'active_snan_left_0' and State.from_buffer_copy(record['initial']).fpcr == 0] ROUND_INTEGRAL_FOCUS = 'round_integral_numerical' ROUND_INTEGRAL_ORACLE = ROOT / '.stela/research/round-integral/oracle/receipt.json' ROUND_INTEGRAL_SCALAR_BASES = {'frintn': 0x1e244000, 'frintp': 0x1e24c000, 'frintm': 0x1e254000, 'frintz': 0x1e25c000, 'frinta': 0x1e264000, 'frintx': 0x1e274000, 'frinti': 0x1e27c000, 'frint32z': 0x1e284000, 'frint32x': 0x1e28c000, 'frint64z': 0x1e294000, 'frint64x': 0x1e29c000} ROUND_INTEGRAL_VECTOR_BASES = {'frintn': 0x0e218800, 'frintp': 0x0ea18800, 'frintm': 0x0e219800, 'frintz': 0x0ea19800, 'frinta': 0x2e218800, 'frintx': 0x2e219800, 'frinti': 0x2ea19800, 'frint32z': 0x0e21e800, 'frint32x': 0x2e21e800, 'frint64z': 0x0e21f800, 'frint64x': 0x2e21f800} NUMERICAL_UNARY_ROLES = ((0, 1), (1, 1), (31, 31), (31, 30)) NUMERICAL_UNARY_CONTROLS = (NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_FZ, NUMERICAL_FZ16, NUMERICAL_DN, CONVERSION_AHP, NUMERICAL_NEP, NUMERICAL_AH | NUMERICAL_FIZ, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_DN, NUMERICAL_FIZ | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_FIZ | NUMERICAL_FZ) ROUND_INTEGRAL_CASES = 27281 NUMERICAL_ROUND_SHIFT = 22 ROUND_INTEGRAL_HALF_VECTOR_OFFSET = 0x00580000 def round_integral_operation(name, lane, width, scalar, d=0, n=1): bounded = int(name[5:7]) if name.startswith(('frint32', 'frint64')) else None if (name not in ROUND_INTEGRAL_SCALAR_BASES or lane not in (2, 4, 8) or bounded and lane == 2 or any(not 0 <= register < 32 for register in (d, n)) or scalar and width != lane or not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES): raise ValueError('Unallocated integral rounding operation') if scalar: word = ROUND_INTEGRAL_SCALAR_BASES[name] | {2: 3, 4: 0, 8: 1}[lane] << 22 canonical = name.upper() + "_" + {2: "H", 4: "S", 8: "D"}[lane] + "_floatdp1" else: word = ROUND_INTEGRAL_VECTOR_BASES[name] | (ROUND_INTEGRAL_HALF_VECTOR_OFFSET if lane == 2 else int(lane == 8) << 22) word |= int(width == 16) << 30 canonical = f'{name.upper()}_asimdmisc{"fp16" if lane == 2 else ""}_R' return {'name': name, 'lane': lane, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'word': word | n << 5 | d, 'canonical_id': canonical, 'bounded_bits': bounded, 'source_lane_count': 1 if scalar else width // lane, 'output_kind': 'vector', 'merge': 'old_destination_if_nep' if scalar else 'none', 'cohort_family': 'round_integral'} def round_integral_functions(): functions = [] for name in ROUND_INTEGRAL_SCALAR_BASES: bounded = name.startswith(('frint32', 'frint64')) shapes = [(lane, lane, True) for lane in ((4, 8) if bounded else (2, 4, 8))] shapes += [(lane, width, False) for lane, width in VECTOR_NUMERICAL_SHAPES if not bounded or lane != 2] for lane, width, scalar in shapes: functions.extend(round_integral_operation(name, lane, width, scalar, d, n) for d, n in NUMERICAL_UNARY_ROLES) return functions def numerical_unary_shape(operation): return tuple(operation[key] for key in ('name', 'lane', 'bytes', 'scalar')) def numerical_unary_values(lane): fraction, bias = {2: (10, 15), 4: (23, 127), 8: (52, 1023)}[lane] sign, infinity = 1 << (lane * 8 - 1), (bias * 2 + 1) << fraction positive = [('zero', 0)] dyadics = (('quarter', 1, -2), ('half', 1, -1), ('threequarter', 3, -2), ('one', 1, 0), ('onehalf', 3, -1), ('two', 2, 0), ('twohalf', 5, -1), ('threehalf', 7, -1), ('fourhalf', 9, -1)) positive += [(label, conversion_exact_positive_bits(lane, number, exponent)) for label, number, exponent in dyadics] for label, number in (('half', 1), ('onehalf', 3), ('twohalf', 5)): bits = conversion_exact_positive_bits(lane, number, -1) positive.extend(((label + '_below', bits - 1), (label + '_above', bits + 1))) last_fraction = conversion_exact_positive_bits(lane, (1 << (fraction + 1)) - 1, -1) integral = conversion_exact_positive_bits(lane, 1, fraction) positive += [('last_fraction', last_fraction), ('integral_threshold', integral), ('integral_next', integral + 1), ('minsubnormal', 1), ('maxsubnormal', (1 << fraction) - 1), ('minnormal', 1 << fraction), ('maxfinite', infinity - 1), ('infinity', infinity), ('qnan', infinity | 1 << (fraction - 1) | 3), ('snan', infinity | 5), ('qnan_payload', infinity | 1 << (fraction - 1) | 7)] return [(label + suffix, bits | signed) for label, bits in positive for suffix, signed in (('_positive', 0), ('_negative', sign))] def round_integral_bound_values(operation): if not operation['bounded_bits']: return [] lane, bound = operation['lane'], operation['bounded_bits'] - 1 sign = 1 << (lane * 8 - 1) limit = conversion_exact_positive_bits(lane, 1, bound) values = [(label + suffix, bits | signed) for label, bits in (('bound_below', limit - 1), ('bound_exact', limit), ('bound_above', limit + 1)) for suffix, signed in (('_positive', 0), ('_negative', sign))] for label, number, exponent in (('bound_maximum', (1 << bound) - 1, 0), ('bound_half_below', (1 << (bound + 1)) - 1, -1), ('bound_half_above', (1 << (bound + 1)) + 1, -1)): bits = conversion_exact_positive_bits(lane, number, exponent) if bits is not None: values.extend((label + suffix, bits | signed) for suffix, signed in (('_positive', 0), ('_negative', sign))) return values def numerical_unary_state(operation, index, values, control=0, sticky=0): initial = State() rng = random.Random(0x524f554e44494e54 + index) initial.x[:] = [rng.getrandbits(64) for _ in range(31)] initial.sp, initial.nzcv = 0x48010000 + index % 16 * 16, (index % 16) << 28 seed_extended_state(initial, index * 31 + 29) initial.reserved, initial.fpcr, initial.fpsr = VECTOR_NUMERICAL_CANARY, control, sticky lane, count = operation['lane'], operation['source_lane_count'] if len(values) != count: raise ValueError('Incorrect integral rounding source lane count') bits = lane * 8 poison = dict(numerical_unary_values(lane))['snan_negative'] packed = sum(poison << (position * bits) for position in range(16 // lane)) for position, value in enumerate(values): if not 0 <= value < 1 << bits: raise ValueError('Invalid integral rounding source lane') mask = ((1 << bits) - 1) << (position * bits) packed = packed & ~mask | value << (position * bits) initial.v[operation['n']][:] = [packed & MASK, packed >> 64] return initial def round_integral_corpus(): functions, records, enable_forms = round_integral_functions(), [], set() def append(function, label, values, control=0, sticky=0): initial = numerical_unary_state(functions[function], len(records), values, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function in range(0, len(functions), len(NUMERICAL_UNARY_ROLES)): op = functions[function] values, count = numerical_unary_values(op['lane']), op['source_lane_count'] catalog = dict(values) bounds = round_integral_bound_values(op) for label, value in values + bounds: append(function, 'value_' + label, [value] * count) ties = [(label, value) for label, value in values if label.split('_')[0] in ('half', 'onehalf', 'twohalf')] # Fourteen signed values: exact ties and immediate neighbors of half/onehalf. ties = [(label, value) for label, value in ties if not label.startswith(('twohalf_below', 'twohalf_above'))] for control in (mode << NUMERICAL_ROUND_SHIFT for mode in range(4)): for label, value in ties + bounds: append(function, 'round_' + label, [value] * count, control) controlled = [(label, catalog[label]) for stem in ('qnan', 'snan', 'minsubnormal', 'maxsubnormal', 'half') for label in (stem + '_positive', stem + '_negative')] for control in NUMERICAL_UNARY_CONTROLS: for label, value in controlled: append(function, 'control_' + label, [value] * count, control) for ah in (NUMERICAL_AH, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_FIZ): for mode in range(4): for stem in ('half', 'onehalf', 'minsubnormal'): for suffix in ('_positive', '_negative'): label = stem + suffix append(function, 'ah_round_' + label, [catalog[label]] * count, ah | mode << NUMERICAL_ROUND_SHIFT) if op['canonical_id'] not in enable_forms: enable_forms.add(op['canonical_id']) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_enable_readback_only', [catalog['half_positive']] * count, control) for role in range(1, len(NUMERICAL_UNARY_ROLES)): for label, control in (('snan_positive', 0), ('half_negative', NUMERICAL_NEP), ('minsubnormal_negative', NUMERICAL_AH | NUMERICAL_FIZ), ('qnan_negative', NUMERICAL_DN)): append(function + role, 'alias_' + label, [catalog[label]] * count, control) if not op['scalar']: patterns = ('snan_positive', 'qnan_negative', 'minsubnormal_negative', 'infinity_positive') for control in (0, NUMERICAL_AH, NUMERICAL_FZ16 if op['lane'] == 2 else NUMERICAL_FZ): for pattern, label in enumerate(patterns): for position in range(count): active = [catalog['half_positive']] * count active[position] = catalog[label] append(function, f'mixed_{pattern}_{position}', active, control) for sticky in (1 << 4, 1, 1 << 27, FP_REGISTER_MASKS['fpsr']): append(function, 'sticky_status', [catalog['half_negative']] * count, sticky=sticky) if len(records) != ROUND_INTEGRAL_CASES: raise AssertionError(f'Integral rounding corpus exceeded approved scope: {len(records)}') return functions, records def prepare_round_integral_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *round_integral_corpus(), ROUND_INTEGRAL_FOCUS) def load_round_integral_oracle(path=ROUND_INTEGRAL_ORACLE): return load_full_frame_numerical_oracle(path, round_integral_functions(), ROUND_INTEGRAL_CASES, ROUND_INTEGRAL_FOCUS) def validate_numerical_unary_original_effects(initial, original, operation): if operation['scalar']: lane, destination = operation['lane'], operation['d'] merge = struct.pack('<2Q', *initial.v[destination]) if initial.fpcr & NUMERICAL_NEP else bytes(16) if struct.pack('<2Q', *original.v[destination])[lane:] != merge[lane:]: raise AssertionError('Unary numerical scalar inactive bits violated old-Vd NEP merge') elif operation['bytes'] == 8 and original.v[operation['d']][1] != 0: raise AssertionError('Unary numerical vector64 did not clear upper64') def round_integral_runtime_cases(records): selected = {} for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) form, shape = op['canonical_id'], numerical_unary_shape(op) if record['unsupported']: selected.setdefault(('enable', form if initial.fpcr == NUMERICAL_TRAP_ENABLE_MASK else initial.fpcr), record) elif label == 'value_half_negative': selected.setdefault(('shape', shape), record) elif label == 'round_half_negative': selected.setdefault(('rounding', form, initial.fpcr), record) elif label == 'round_bound_half_below_negative': selected.setdefault(('post_round_range', op['name'], initial.fpcr), record) elif label == 'ah_round_half_negative' and op['name'] in ('frinti', 'frintx', 'frint32x', 'frint64x'): selected.setdefault(('ah_rounding', op['name'], initial.fpcr), record) elif label in ('value_bound_exact_positive', 'value_bound_exact_negative'): selected.setdefault(('sentinel_range', op['name'], label), record) elif label == 'control_snan_positive': selected.setdefault(('control', op['name'], initial.fpcr), record) elif label == 'control_minsubnormal_negative' and initial.fpcr in (NUMERICAL_AH, NUMERICAL_AH | NUMERICAL_FIZ, NUMERICAL_FZ16, NUMERICAL_FZ): selected.setdefault(('denormal', op['lane'], op['name'], initial.fpcr), record) elif label == 'control_half_negative' and initial.fpcr == NUMERICAL_NEP and op['scalar']: selected.setdefault(('merge', form), record) elif label == 'sticky_status': selected.setdefault(('sticky', op['name']), record) elif label.startswith('alias_'): selected.setdefault(('alias', op['name'], op['d'], op['n']), record) elif label == 'mixed_3_0' and op['bounded_bits'] and initial.fpcr == 0: selected.setdefault(('mixed_invalid_inexact', shape), record) return list({record['case']: record for record in selected.values()}.values()) def round_integral_helpers(records, case_ids=None): selected = {} for record in records: op, initial = record['operation'], State.from_buffer_copy(record['initial']) if (case_ids is not None and record['case'] not in case_ids or initial.fpcr or initial.fpsr or op['d'] != 0 or op['n'] != 1): continue selected.setdefault((op['bounded_bits'], op['lane'], op['scalar'], op['bytes']), record) return list(selected.values()) SQRT_NUMERICAL_FOCUS = 'sqrt_numerical' SQRT_NUMERICAL_ORACLE = ROOT / '.stela/research/sqrt/oracle/receipt.json' SQRT_SCALAR_BASE = 0x1e21c000 SQRT_VECTOR_BASE = 0x2ea1f800 SQRT_HALF_VECTOR_BASE = 0x2ef9f800 SQRT_NUMERICAL_CASES = 11123 SQRT_RANDOM_SEED = 0x5351525443415345 def sqrt_numerical_operation(lane, width, scalar, d=0, n=1): if (lane not in (2, 4, 8) or any(not 0 <= register < 32 for register in (d, n)) or scalar and width != lane or not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES): raise ValueError('Unallocated square-root arrangement') if scalar: word = SQRT_SCALAR_BASE | {2: 3, 4: 0, 8: 1}[lane] << 22 canonical = 'FSQRT_' + {2: 'H', 4: 'S', 8: 'D'}[lane] + '_floatdp1' else: word = (SQRT_HALF_VECTOR_BASE if lane == 2 else SQRT_VECTOR_BASE | int(lane == 8) << 22) | int(width == 16) << 30 canonical = 'FSQRT_asimdmisc' + ('fp16' if lane == 2 else '') + '_R' return {'name': 'fsqrt', 'lane': lane, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'word': word | n << 5 | d, 'canonical_id': canonical, 'source_lane_count': 1 if scalar else width // lane, 'output_kind': 'vector', 'merge': 'old_destination_if_nep' if scalar else 'none', 'cohort_family': 'sqrt'} def sqrt_numerical_functions(): shapes = [(lane, lane, True) for lane in (2, 4, 8)] shapes += [(lane, width, False) for lane, width in VECTOR_NUMERICAL_SHAPES] return [sqrt_numerical_operation(lane, width, scalar, d, n) for lane, width, scalar in shapes for d, n in NUMERICAL_UNARY_ROLES] def sqrt_numerical_values(lane): values = numerical_unary_values(lane) sign = 1 << (lane * 8 - 1) four = conversion_exact_positive_bits(lane, 4, 0) positive = [('square_' + str(number), conversion_exact_positive_bits(lane, number, 0)) for number in (9, 16, 25, 49, 64)] positive += [('four_below', four - 1), ('four', four), ('four_above', four + 1), ('three', conversion_exact_positive_bits(lane, 3, 0))] return values + [(label + suffix, bits | signed) for label, bits in positive for suffix, signed in (('_positive', 0), ('_negative', sign))] def sqrt_numerical_midpoints(lane): """Neighboring representable inputs around exact squared root midpoints.""" fraction = {2: 10, 4: 23, 8: 52}[lane] values = [] for index, root in enumerate((1 << fraction, (1 << fraction) + 1, 3 << (fraction - 1), (1 << (fraction + 1)) - 2)): numerator, exponent = (root * 2 + 1) ** 2, -2 * fraction - 2 shift = numerator.bit_length() - 1 - fraction floor = conversion_exact_positive_bits(lane, numerator >> shift, exponent + shift) if floor is None or numerator & ((1 << shift) - 1) == 0: raise AssertionError('Squared root midpoint must lie between representable inputs') values.extend(((f'midpoint_{index}_below', floor), (f'midpoint_{index}_above', floor + 1))) return values def sqrt_numerical_corpus(): functions, records, enable_forms = sqrt_numerical_functions(), [], set() def append(function, label, values, control=0, sticky=0): initial = numerical_unary_state(functions[function], len(records), values, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) rng = random.Random(SQRT_RANDOM_SEED) for function in range(0, len(functions), len(NUMERICAL_UNARY_ROLES)): op = functions[function] values, count = sqrt_numerical_values(op['lane']), op['source_lane_count'] catalog = dict(values) for mode in range(4): for label, value in values + sqrt_numerical_midpoints(op['lane']): append(function, 'value_' + label, [value] * count, mode << NUMERICAL_ROUND_SHIFT) controlled = [(label, catalog[label]) for stem in ('qnan', 'snan', 'minsubnormal', 'maxsubnormal', 'minnormal', 'zero', 'infinity', 'two') for label in (stem + '_positive', stem + '_negative')] for control in NUMERICAL_UNARY_CONTROLS: for mode in range(4): for label, value in controlled: append(function, 'control_' + label, [value] * count, control | mode << NUMERICAL_ROUND_SHIFT) for role in range(1, len(NUMERICAL_UNARY_ROLES)): for control in (0, NUMERICAL_NEP, NUMERICAL_AH, NUMERICAL_FZ16): for label in ('one_positive', 'two_positive', 'snan_negative', 'minsubnormal_negative'): append(function + role, 'alias_' + label, [catalog[label]] * count, control) if not op['scalar']: for control in (0, NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_FZ, NUMERICAL_FZ16, NUMERICAL_AH | NUMERICAL_FZ): for pattern, label in enumerate(('qnan_negative', 'snan_positive', 'two_negative', 'minsubnormal_negative', 'minsubnormal_positive', 'infinity_positive')): for position in range(count): active = [catalog['two_positive'] if element % 2 == 0 else catalog['square_9_positive'] for element in range(count)] active[position] = catalog[label] append(function, f'mixed_{pattern}_{position}', active, control) for sticky in (1 << 4, 1, 1 << 27, FP_REGISTER_MASKS['fpsr']): for control in (0, NUMERICAL_AH): for label in ('one_positive', 'two_positive', 'two_negative', 'minsubnormal_positive'): append(function, 'sticky_' + label, [catalog[label]] * count, control, sticky) if op['canonical_id'] not in enable_forms: enable_forms.add(op['canonical_id']) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_enable_readback_only', [catalog['two_positive']] * count, control) random_values = [rng.getrandbits(op['lane'] * 8) for _ in range(32)] for mode in range(4): for index, value in enumerate(random_values): append(function, f'bounded_random_{index}', [value] * count, mode << NUMERICAL_ROUND_SHIFT) if len(records) != SQRT_NUMERICAL_CASES: raise AssertionError(f'Square-root corpus exceeded approved scope: {len(records)}') return functions, records def prepare_sqrt_numerical_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *sqrt_numerical_corpus(), SQRT_NUMERICAL_FOCUS) def load_sqrt_numerical_oracle(path=SQRT_NUMERICAL_ORACLE): return load_full_frame_numerical_oracle(path, sqrt_numerical_functions(), SQRT_NUMERICAL_CASES, SQRT_NUMERICAL_FOCUS) def sqrt_numerical_runtime_cases(records): selected = {} directed = ('zero_negative', 'one_positive', 'two_negative', 'infinity_positive', 'infinity_negative', 'four_below_positive', 'maxfinite_positive', 'snan_negative') for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) form, shape = op['canonical_id'], numerical_unary_shape(op) if record['unsupported']: selected.setdefault(('enable', form if initial.fpcr == NUMERICAL_TRAP_ENABLE_MASK else initial.fpcr), record) elif label == 'value_two_positive': selected.setdefault(('rounding', shape, initial.fpcr), record) elif label in tuple('value_' + name for name in directed) and initial.fpcr == 0: selected.setdefault(('directed', shape, label), record) elif label in ('value_midpoint_0_below', 'value_midpoint_0_above') and initial.fpcr == 0: selected.setdefault(('near_midpoint', shape, label), record) elif label in ('control_minsubnormal_negative', 'control_qnan_positive') and initial.fpcr in NUMERICAL_UNARY_CONTROLS: selected.setdefault(('control', op['lane'], initial.fpcr, label), record) elif label == 'control_minsubnormal_positive' and initial.fpcr in (NUMERICAL_AH, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_FIZ, NUMERICAL_FZ, NUMERICAL_FZ16): selected.setdefault(('positive_denormal', op['lane'], initial.fpcr), record) elif label == 'control_snan_positive' and initial.fpcr in (NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): selected.setdefault(('signaling_dn', op['lane'], initial.fpcr), record) elif label == 'control_two_positive' and initial.fpcr == NUMERICAL_NEP and op['scalar']: selected.setdefault(('merge', form), record) elif label.startswith('alias_') and initial.fpcr == NUMERICAL_NEP: selected.setdefault(('alias', op['lane'], op['d'], op['n']), record) elif label == 'sticky_two_positive': selected.setdefault(('sticky', form), record) elif label in ('mixed_2_0', 'mixed_4_0') and initial.fpcr == NUMERICAL_AH: selected.setdefault(('mixed', shape, label), record) return list({record['case']: record for record in selected.values()}.values()) def sqrt_numerical_helpers(records, case_ids=None): selected = {} for record in records: op, initial = record['operation'], State.from_buffer_copy(record['initial']) if (case_ids is not None and record['case'] not in case_ids or initial.fpcr or initial.fpsr or op['d'] != 0 or op['n'] != 1): continue selected.setdefault((op['lane'], op['scalar'], op['bytes']), record) return list(selected.values()) ESTIMATE_NUMERICAL_FOCUS = 'estimate_numerical' REFINEMENT_NUMERICAL_FOCUS = 'refinement_numerical' ESTIMATE_NUMERICAL_ORACLE = ROOT / '.stela/research/estimates/oracle/receipt.json' REFINEMENT_NUMERICAL_ORACLE = ROOT / '.stela/research/refinements/oracle/receipt.json' ESTIMATE_BASES = {'frecpe': (0x5ea1d800, 0x5ef9d800, 0x0ea1d800, 0x0ef9d800), 'frsqrte': (0x7ea1d800, 0x7ef9d800, 0x2ea1d800, 0x2ef9d800), 'frecpx': (0x5ea1f800, 0x5ef9f800, None, None), 'urecpe': (None, None, 0x0ea1c800, None), 'ursqrte': (None, None, 0x2ea1c800, None)} REFINEMENT_BASES = {'frecps': (0x0e20fc00, 0x0e403c00), 'frsqrts': (0x0ea0fc00, 0x0ec03c00)} ESTIMATE_NUMERICAL_CASES = 36734 REFINEMENT_NUMERICAL_CASES = 12720 ESTIMATE_RPRES_SHIFT = 4 ESTIMATE_RPRES_MASK = 15 ESTIMATE_SINGLE_BITS = 32 ESTIMATE_SINGLE_FRACTION_BITS = 23 ESTIMATE_SINGLE_EXPONENT_MASK = 255 ESTIMATE_SINGLE_SIGN = 1 << (ESTIMATE_SINGLE_BITS - 1) ESTIMATE_SINGLE_MASK = (1 << ESTIMATE_SINGLE_BITS) - 1 ESTIMATE_SINGLE_FRACTION_MASK = (1 << ESTIMATE_SINGLE_FRACTION_BITS) - 1 ESTIMATE_SINGLE_QUIET = 1 << (ESTIMATE_SINGLE_FRACTION_BITS - 1) ESTIMATE_SINGLE_INFINITY = ESTIMATE_SINGLE_EXPONENT_MASK << ESTIMATE_SINGLE_FRACTION_BITS ESTIMATE_SINGLE_NEGATIVE_DEFAULT_NAN = ESTIMATE_SINGLE_SIGN | ESTIMATE_SINGLE_INFINITY | ESTIMATE_SINGLE_QUIET ESTIMATE_SINGLE_RECIP_EXPONENT_BASE = 2 * 127 - 1 ESTIMATE_SINGLE_RSQRT_EXPONENT_BASE = 3 * 127 - 1 ESTIMATE_TABLE_FRACTION_BITS = {False: 8, True: 11} ESTIMATE_RESULT_FRACTION_BITS = {False: 8, True: 12} ESTIMATE_RECIP_NUMERATOR_BITS = {False: 19, True: 26} ESTIMATE_RSQRT_NUMERATOR_BITS = {False: 28, True: 39} ESTIMATE_RANDOM_SEED = 0x455354494d415445 REFINEMENT_RANDOM_SEED = 0x524546494e454d45 ESTIMATE_UNSIGNED_VALUES = (0, 1, 0x3fffffff, 0x40000000, 0x7fffffff, 0x80000000, 0xff800000, 0xffffffff) def estimate_operation(name, lane, width, scalar, d=0, n=1): unsigned = name.startswith('u') if (name not in ESTIMATE_BASES or lane not in (2, 4, 8) or any(not 0 <= register < 32 for register in (d, n)) or scalar and width != lane or not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES or unsigned and (scalar or lane != 4) or name == 'frecpx' and not scalar): raise ValueError('Unallocated estimate arrangement') base = ESTIMATE_BASES[name][(0 if scalar else 2) + int(lane == 2)] if base is None: raise ValueError('Unallocated estimate format') word = base | (int(lane == 8) << 22) | (int(not scalar and width == 16) << 30) | n << 5 | d canonical = name.upper() + ('_asisdmisc' if scalar else '_asimdmisc') + ('fp16_R' if lane == 2 else '_R') return {'name': name, 'word': word, 'lane': lane, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'is_unsigned': unsigned, 'canonical_id': canonical, 'source_lane_count': 1 if scalar else width // lane, 'output_kind': 'vector', 'merge': 'old_destination_if_nep' if scalar else 'none', 'cohort_family': 'estimate'} def refinement_operation(name, lane, width, scalar, d=0, n=1, m=2): if (name not in REFINEMENT_BASES or lane not in (2, 4, 8) or any(not 0 <= register < 32 for register in (d, n, m)) or scalar and width != lane or not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES): raise ValueError('Unallocated refinement arrangement') word = REFINEMENT_BASES[name][int(lane == 2)] | int(lane == 8) << 22 word |= 0x50000000 if scalar else int(width == 16) << 30 word |= m << 16 | n << 5 | d canonical = name.upper() + ('_asisdsame' if scalar else '_asimdsame') + ('fp16_only' if lane == 2 else '_only') return {'name': name, 'word': word, 'lane': lane, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'm': m, 'canonical_id': canonical, 'source_lane_count': 1 if scalar else width // lane, 'output_kind': 'vector', 'merge': 'left_if_nep' if scalar else 'none', 'cohort_family': 'refinement'} def estimate_functions(): functions = [] for name in ESTIMATE_BASES: shapes = [(lane, lane, True) for lane in (2, 4, 8)] if not name.startswith('u') else [] if name != 'frecpx': shapes += [(lane, width, False) for lane, width in VECTOR_NUMERICAL_SHAPES if not name.startswith('u') or lane == 4] functions.extend(estimate_operation(name, lane, width, scalar, d, n) for lane, width, scalar in shapes for d, n in NUMERICAL_UNARY_ROLES) return functions def refinement_functions(): shapes = [(lane, lane, True) for lane in (2, 4, 8)] + [(lane, width, False) for lane, width in VECTOR_NUMERICAL_SHAPES] return [refinement_operation(name, lane, width, scalar, d, n, m) for name in REFINEMENT_BASES for lane, width, scalar in shapes for d, n, m in COMPARISON_MINMAX_BINARY_ROLES] def estimate_table_input(operation, key, poison): if operation['is_unsigned']: return key << 23 | ((1 << 23) - 1 if poison else 0) fraction, bias = {2: (10, 15), 4: (23, 127), 8: (52, 1023)}[operation['lane']] reciprocal = operation['name'] == 'frecpe' shift = fraction - (8 if reciprocal or key >= 256 else 7) base = 256 if reciprocal or key >= 256 else 128 exponent = bias if reciprocal or key < 256 else bias + 1 return exponent << fraction | (key - base) << shift | ((1 << shift) - 1 if poison else 0) def numerical_suppressed_enables(operation, initial): return operation.get('is_unsigned', False) or bool(initial.fpcr & NUMERICAL_AH) def comparison_minmax_enabled_control(operation, initial): return operation['condition'] is not None and not numerical_condition_holds(operation['condition'], initial.nzcv) def estimate_single_profile_state(operation, initial, enhanced): """Closed integer rules for the AH Single profile split; no host FP.""" if operation['lane'] != 4 or operation['name'] not in ('frecpe', 'frsqrte') or not initial.fpcr & NUMERICAL_AH: raise ValueError('Only AH Single estimates have this profile split') expected = State.from_buffer_copy(initial) packed = 0 input_precision = ESTIMATE_TABLE_FRACTION_BITS[enhanced] precision = ESTIMATE_RESULT_FRACTION_BITS[enhanced] for position in range(operation['source_lane_count']): source = initial.v[operation['n']][position // 2] >> (position % 2 * ESTIMATE_SINGLE_BITS) & ESTIMATE_SINGLE_MASK sign, exponent, fraction = source & ESTIMATE_SINGLE_SIGN, source >> ESTIMATE_SINGLE_FRACTION_BITS & ESTIMATE_SINGLE_EXPONENT_MASK, source & ESTIMATE_SINGLE_FRACTION_MASK if exponent == ESTIMATE_SINGLE_EXPONENT_MASK and fraction: result = ESTIMATE_SINGLE_NEGATIVE_DEFAULT_NAN if initial.fpcr & NUMERICAL_DN else source | ESTIMATE_SINGLE_QUIET elif exponent == 0: result = sign | ESTIMATE_SINGLE_INFINITY # AH forces FIZ, including every subnormal. elif operation['name'] == 'frecpe': if exponent == ESTIMATE_SINGLE_EXPONENT_MASK or exponent >= ESTIMATE_SINGLE_RECIP_EXPONENT_BASE: result = sign # AH forces output FZ and suppresses UFC. else: scaled = (1 << input_precision) + (fraction >> (ESTIMATE_SINGLE_FRACTION_BITS - input_precision)) estimate = (((1 << ESTIMATE_RECIP_NUMERATOR_BITS[enhanced]) // (2 * scaled + 1)) + 1) // 2 result = sign | (ESTIMATE_SINGLE_RECIP_EXPONENT_BASE - exponent) << ESTIMATE_SINGLE_FRACTION_BITS | (estimate & ((1 << precision) - 1)) << (ESTIMATE_SINGLE_FRACTION_BITS - precision) elif sign: result = ESTIMATE_SINGLE_NEGATIVE_DEFAULT_NAN elif exponent == ESTIMATE_SINGLE_EXPONENT_MASK: result = 0 else: if exponent & 1: scaled = (1 << (input_precision - 1)) + (fraction >> (ESTIMATE_SINGLE_FRACTION_BITS - input_precision + 1)) transformed = 2 * scaled + 1 else: scaled = (1 << input_precision) + (fraction >> (ESTIMATE_SINGLE_FRACTION_BITS - input_precision)) transformed = 2 * ((scaled & ~1) + 1) estimate = (math.isqrt(((1 << ESTIMATE_RSQRT_NUMERATOR_BITS[enhanced]) - 1) // transformed) + 1) // 2 result = ((ESTIMATE_SINGLE_RSQRT_EXPONENT_BASE - exponent) // 2) << ESTIMATE_SINGLE_FRACTION_BITS | (estimate & ((1 << precision) - 1)) << (ESTIMATE_SINGLE_FRACTION_BITS - precision) packed |= result << (position * ESTIMATE_SINGLE_BITS) if operation['scalar'] and initial.fpcr & NUMERICAL_NEP: old = initial.v[operation['d']][0] | initial.v[operation['d']][1] << 64 packed |= old & ~ESTIMATE_SINGLE_MASK expected.v[operation['d']][:] = [packed & MASK, packed >> 64] return expected def estimate_profile_expectation(operation, initial, original, producer): if operation['lane'] != 4 or operation['name'] not in ('frecpe', 'frsqrte') or not initial.fpcr & NUMERICAL_AH: return None rpres = int(producer['cpu_registers']['ID_AA64ISAR2_EL1'], 16) >> ESTIMATE_RPRES_SHIFT & ESTIMATE_RPRES_MASK if rpres != 1: raise AssertionError('Estimate original capture no longer has calibrated RPRES1 profile') enhanced = estimate_single_profile_state(operation, initial, True) enhanced.fpcr = original.fpcr if bytes(enhanced) != bytes(original): raise AssertionError('Raw enhanced original estimate disagrees with independent full-state integer model') return estimate_single_profile_state(operation, initial, False) def validate_refinement_original_effects(initial, original, operation): if operation['scalar']: lane = operation['lane'] merge = struct.pack('<2Q', *initial.v[operation['n']]) if initial.fpcr & NUMERICAL_NEP else bytes(16) if struct.pack('<2Q', *original.v[operation['d']])[lane:] != merge[lane:]: raise AssertionError('Scalar refinement did not merge original Vn') elif operation['bytes'] == 8 and original.v[operation['d']][1]: raise AssertionError('Vector64 refinement did not clear upper64') if initial.fpcr & NUMERICAL_AH and original.fpsr != initial.fpsr: raise AssertionError('AH refinement generated unsuppressed status') def validate_estimate_original_effects(initial, original, operation): validate_numerical_unary_original_effects(initial, original, operation) if numerical_suppressed_enables(operation, initial) and original.fpsr != initial.fpsr: raise AssertionError('Unsigned or AH estimate generated unsuppressed status') def estimate_corpus(): functions, records, enable_forms = estimate_functions(), [], set() rng = random.Random(ESTIMATE_RANDOM_SEED) def append(function, label, values, control=0, sticky=0): initial = numerical_unary_state(functions[function], len(records), values, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function in range(0, len(functions), len(NUMERICAL_UNARY_ROLES)): op = functions[function] count, lane, unsigned = op['source_lane_count'], op['lane'], op['is_unsigned'] fraction, bias = {2: (10, 15), 4: (23, 127), 8: (52, 1023)}[lane] values = [(f'unsigned_{i}', value) for i, value in enumerate(ESTIMATE_UNSIGNED_VALUES)] if unsigned else numerical_unary_values(lane) catalog = dict(values) if op['name'] != 'frecpx': start = 256 if op['name'] in ('frecpe', 'urecpe') else 128 for key in range(start, 512): for poison in (False, True): append(function, f'table_{key}_{int(poison)}', [estimate_table_input(op, key, poison)] * count) else: for exponent in range((bias + 1) * 2): for tail in (0, 1): for sign in (0, 1 << (lane * 8 - 1)): append(function, f'exponent_{exponent}_{tail}_{int(bool(sign))}', [sign | exponent << fraction | tail]) if not unsigned: for mode in range(4): for label, value in values: append(function, 'value_' + label, [value] * count, mode << NUMERICAL_ROUND_SHIFT) controlled = [(label, catalog[label]) for stem in ('qnan', 'snan', 'minsubnormal', 'maxsubnormal', 'minnormal', 'zero', 'infinity', 'one') for label in (stem + '_positive', stem + '_negative')] for control in NUMERICAL_UNARY_CONTROLS: for label, value in controlled: append(function, 'control_' + label, [value] * count, control) for mode in range(4): for label in ('one_positive', 'one_negative', 'minsubnormal_positive', 'minsubnormal_negative', 'snan_positive', 'snan_negative', 'maxfinite_positive', 'maxfinite_negative'): append(function, 'ah_round_' + label, [catalog[label]] * count, NUMERICAL_AH | mode << NUMERICAL_ROUND_SHIFT) if op['name'] == 'frecpe': flush = NUMERICAL_FZ16 if lane == 2 else NUMERICAL_FZ for boundary, exponent in (('overflow', {2: -16, 4: -128, 8: -1024}[lane]), ('flush', {2: 14, 4: 126, 8: 1022}[lane])): threshold = conversion_exact_positive_bits(lane, 1, exponent) for offset in (-1, 0, 1): for sign in (0, 1 << (lane * 8 - 1)): for control in (0, flush, NUMERICAL_AH, NUMERICAL_AH | flush): for mode in range(4): append(function, f'threshold_{boundary}_{offset}_{int(bool(sign))}', [(threshold + offset) | sign] * count, control | mode << NUMERICAL_ROUND_SHIFT) if op['name'] == 'frsqrte': for position in range(fraction): for sign in (0, 1 << (lane * 8 - 1)): for control in (0, NUMERICAL_AH, NUMERICAL_FZ16 if lane == 2 else NUMERICAL_FZ): append(function, f'leading_{position}_{int(bool(sign))}', [(1 << position) | sign] * count, control) else: for control in NUMERICAL_UNARY_CONTROLS: for label, value in values: append(function, 'control_' + label, [value] * count, control) selected_values = values[:4] if unsigned else [(label, catalog[label]) for label in ('one_positive', 'snan_negative', 'minsubnormal_negative', 'zero_negative')] if op['canonical_id'] not in enable_forms: enable_forms.add(op['canonical_id']) for enable in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): for ah in (0, NUMERICAL_AH): append(function, 'enabled_suppression', [selected_values[1][1]] * count, enable | ah) for role in range(1, len(NUMERICAL_UNARY_ROLES)): for control in (0, NUMERICAL_NEP, NUMERICAL_AH): for label, value in selected_values: append(function + role, 'alias_' + label, [value] * count, control) if not op['scalar']: patterns = values[:4] if unsigned else [(label, catalog[label]) for label in ('qnan_negative', 'snan_positive', 'minsubnormal_negative', 'infinity_positive')] for control in (0, NUMERICAL_AH, NUMERICAL_FZ16 if lane == 2 else NUMERICAL_FZ): for pattern, (_, value) in enumerate(patterns): for position in range(count): active = [values[1][1] if unsigned else catalog['one_positive']] * count active[position] = value append(function, f'mixed_{pattern}_{position}', active, control) for sticky in (1, 1 << 4, 1 << 27, FP_REGISTER_MASKS['fpsr']): for control in (0, NUMERICAL_AH): for label, value in selected_values: append(function, 'sticky_' + label, [value] * count, control, sticky) for iteration in range(16): value = rng.getrandbits(lane * 8) for control in (0, NUMERICAL_AH): append(function, f'bounded_random_{iteration}', [value] * count, control) if len(records) != ESTIMATE_NUMERICAL_CASES: raise AssertionError(f'Estimate corpus exceeded approved scope: {len(records)}') return functions, records def refinement_pairs(lane): v = dict(numerical_unary_values(lane)) def positive(label): return v[label + '_positive'] pairs = [] for zero in (v['zero_positive'], v['zero_negative']): for infinity in (v['infinity_positive'], v['infinity_negative']): pairs.extend(((zero, infinity), (infinity, zero))) for one in (v['one_positive'], v['one_negative']): for infinity in (v['infinity_positive'], v['infinity_negative']): pairs.extend(((one, infinity), (infinity, one))) for left, right in (('one_positive', 'one_positive'), ('two_positive', 'one_positive'), ('one_positive', 'two_positive'), ('one_positive', 'onehalf_positive'), ('one_negative', 'one_positive'), ('one_positive', 'one_negative'), ('two_negative', 'one_negative'), ('half_positive', 'half_positive'), ('onehalf_positive', 'two_positive'), ('quarter_positive', 'fourhalf_positive'), ('fourhalf_positive', 'twohalf_positive'), ('two_positive', 'two_positive')): pairs.append((v[left], v[right])) one, two = positive('one'), positive('two') three = conversion_exact_positive_bits(lane, 3, 0) pairs.extend(((three, one), (one, three), (one+1, one-1), (one-1, one+1), (two+1, one), (three+1, one))) overflow = {2: 0x5c00, 4: 0x5f800000, 8: 0x5ff0000000000000}[lane] pairs.extend(((one+1, two-2), (one+2, three-3), (overflow, overflow), (positive('minsubnormal'), positive('maxfinite')), (positive('minnormal'), positive('maxfinite')), (positive('maxfinite'), positive('maxfinite')))) for label in ('qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): pairs.extend(((v[label], one), (one, v[label]))) if len(pairs) != 48: raise AssertionError('Refinement directed foundation changed') return pairs def refinement_corpus(): functions, records, enable_forms = refinement_functions(), [], set() rng = random.Random(REFINEMENT_RANDOM_SEED) def append(function, label, pairs, control=0, sticky=0): initial = vector_numerical_state(functions[function], len(records), pairs, control) initial.fpsr = sticky records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function in range(0, len(functions), len(COMPARISON_MINMAX_BINARY_ROLES)): op = functions[function] count, lane = op['source_lane_count'], op['lane'] pairs, v = refinement_pairs(lane), dict(numerical_unary_values(lane)) for control in (0, NUMERICAL_AH): for mode in range(4): for index, pair in enumerate(pairs): append(function, f'directed_{index}', [pair] * count, control | mode << NUMERICAL_ROUND_SHIFT) matrix = [v[label] for label in ('qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative', 'one_positive', 'one_negative')] for control in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): for left, x in enumerate(matrix): for right, y in enumerate(matrix): append(function, f'nan_{left}_{right}', [(x, y)] * count, control) boundaries = [pairs[0], (v['minsubnormal_positive'], v['infinity_positive']), (v['minsubnormal_negative'], v['infinity_positive']), (v['infinity_positive'], v['minsubnormal_positive']), pairs[30], pairs[34 if op['name'] == 'frecps' else 35], pairs[36], (v['qnan_positive'], v['snan_negative'])] for control in NUMERICAL_UNARY_CONTROLS: for index, pair in enumerate(boundaries): append(function, f'control_{index}', [pair] * count, control) for role in range(1, len(COMPARISON_MINMAX_BINARY_ROLES)): for control in (0, NUMERICAL_NEP, NUMERICAL_AH): for index in (0, 30, 36, 40): append(function + role, f'alias_{index}', [pairs[index]] * count, control) if not op['scalar']: flush = NUMERICAL_FZ16 if lane == 2 else NUMERICAL_FZ for control in (0, NUMERICAL_AH, flush, NUMERICAL_AH | flush): for pattern, pair in enumerate((pairs[39], pairs[44], boundaries[1], pairs[34 if op['name'] == 'frecps' else 35])): for position in range(count): active = [pairs[16]] * count active[position] = pair append(function, f'mixed_{pattern}_{position}', active, control) if op['canonical_id'] not in enable_forms: enable_forms.add(op['canonical_id']) for enable in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): for ah in (0, NUMERICAL_AH): append(function, 'enabled_suppression', [pairs[44]] * count, enable | ah) for sticky in (1, 1 << 4, 1 << 27, FP_REGISTER_MASKS['fpsr']): for control in (0, NUMERICAL_AH): for index in (0, 30, 36, 44): append(function, f'sticky_{index}', [pairs[index]] * count, control, sticky) for iteration in range(16): pair = (rng.getrandbits(lane * 8), rng.getrandbits(lane * 8)) for control in (0, NUMERICAL_AH): append(function, f'bounded_random_{iteration}', [pair] * count, control) if len(records) != REFINEMENT_NUMERICAL_CASES: raise AssertionError(f'Refinement corpus exceeded approved scope: {len(records)}') return functions, records def prepare_estimate_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *estimate_corpus(), ESTIMATE_NUMERICAL_FOCUS) def prepare_refinement_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *refinement_corpus(), REFINEMENT_NUMERICAL_FOCUS) def load_estimate_oracle(path=ESTIMATE_NUMERICAL_ORACLE): return load_full_frame_numerical_oracle(path, estimate_functions(), ESTIMATE_NUMERICAL_CASES, ESTIMATE_NUMERICAL_FOCUS) def load_refinement_oracle(path=REFINEMENT_NUMERICAL_ORACLE): return load_full_frame_numerical_oracle(path, refinement_functions(), REFINEMENT_NUMERICAL_CASES, REFINEMENT_NUMERICAL_FOCUS) def estimate_refinement_helpers(records, case_ids=None): selected = {} for record in records: op, initial = record['operation'], State.from_buffer_copy(record['initial']) if (case_ids is not None and record['case'] not in case_ids or initial.fpcr or initial.fpsr or op['d'] != 0 or op['n'] != 1): continue selected.setdefault(numerical_unary_shape(op), record) return list(selected.values()) def estimate_runtime_cases(records): selected = {} for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) form, shape = op['canonical_id'], numerical_unary_shape(op) if label == 'enabled_suppression': key = ('enable', form if initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK == NUMERICAL_TRAP_ENABLE_MASK else op['name'], initial.fpcr) selected.setdefault(key, record) elif initial.fpcr == 0 and initial.fpsr == 0 and label in ('table_256_0', 'exponent_0_0_0'): selected.setdefault(('shape', shape), record) elif label == 'control_snan_negative' and initial.fpcr in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): selected.setdefault(('nan', form, initial.fpcr), record) elif label in ('control_minsubnormal_negative', 'control_one_positive') and initial.fpcr in (NUMERICAL_NEP, NUMERICAL_FZ16, NUMERICAL_FZ, NUMERICAL_FIZ): selected.setdefault(('control', op['name'], initial.fpcr, label), record) elif label.startswith('threshold_') and initial.fpcr in (0, NUMERICAL_AH): selected.setdefault(('threshold', op['lane'], label, initial.fpcr), record) elif label == 'ah_round_one_positive': selected.setdefault(('ah_mode', op['name'], initial.fpcr), record) elif label.startswith('alias_'): selected.setdefault(('alias', op['name'], op['d'], op['n']), record) elif label.startswith('sticky_'): selected.setdefault(('sticky', form), record) return list({record['case']: record for record in selected.values()}.values()) def refinement_runtime_cases(records): selected = {} for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) form, shape = op['canonical_id'], numerical_unary_shape(op) if label == 'enabled_suppression': selected.setdefault(('enable', form if initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK == NUMERICAL_TRAP_ENABLE_MASK else 'all', initial.fpcr), record) elif label in ('directed_17', 'directed_28', 'directed_36') or label in ('directed_34', 'directed_35') and op['lane'] == 2 and not initial.fpcr & NUMERICAL_AH: selected.setdefault(('directed', op['name'], op['lane'], label, initial.fpcr), record) elif label in ('nan_0_3', 'nan_2_1'): selected.setdefault(('nan', op['name'], op['lane'], label, initial.fpcr), record) elif (label in ('control_1', 'control_2') or label == 'control_7' and initial.fpcr == NUMERICAL_NEP) and initial.fpcr in (NUMERICAL_NEP, NUMERICAL_FZ16, NUMERICAL_FZ, NUMERICAL_FIZ): selected.setdefault(('control', op['name'], op['lane'], label, initial.fpcr), record) elif label.startswith('alias_') and initial.fpcr == NUMERICAL_NEP: selected.setdefault(('alias', op['name'], op['d'], op['n'], op['m']), record) elif label.startswith('sticky_'): selected.setdefault(('sticky', op['name'], op['lane']), record) elif label == 'mixed_3_0' and op['lane'] == 2 and initial.fpcr in (NUMERICAL_FZ16, NUMERICAL_AH | NUMERICAL_FZ16): selected.setdefault(('half_output_flush', shape, initial.fpcr), record) elif label == 'mixed_0_0' and initial.fpcr == 0: selected.setdefault(('shape', shape), record) return list({record['case']: record for record in selected.values()}.values()) BINARY_REMAINING_FOCUS = 'binary_remaining_numerical' BINARY_REMAINING_ORACLE = ROOT / '.stela/research/fp-binary-remaining/oracle/receipt.json' BINARY_REMAINING_REGISTER_BASES = {'fabd': (0x2ec01400, 0x2ea0d400), 'faddp': (0x2e401400, 0x2e20d400), 'fmulx': (0x0e401c00, 0x0e20dc00)} BINARY_REMAINING_INDEXED_BASES = {'fmul': 0x0f009000, 'fmulx': 0x2f009000} BINARY_REMAINING_NEGATED_BASE = 0x1e208800 BINARY_REMAINING_PAIR_BASES = {2: 0x5e30d800, 4: 0x7e30d800, 8: 0x7e70d800} BINARY_REMAINING_CONTROLS = (NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_NEP, NUMERICAL_FZ, NUMERICAL_FZ16, NUMERICAL_DN, CONVERSION_AHP, NUMERICAL_AH | NUMERICAL_FIZ, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_FZ16, NUMERICAL_AH | NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_NEP) BINARY_REMAINING_CASES = 33518 BINARY_REMAINING_RANDOM_SEED = 0x42494e4152594650 def binary_remaining_operation(name, lane, width, scalar, d=0, n=1, m=2, index=None): if (lane not in (2, 4, 8) or any(not 0 <= register < 32 for register in (d, n)) or scalar and width != lane or not scalar and (lane, width) not in VECTOR_NUMERICAL_SHAPES): raise ValueError('Unallocated remaining binary arrangement') pair = name == 'faddp' if pair and scalar: if index is not None or m is not None: raise ValueError('Scalar pair has exactly one source register') word = BINARY_REMAINING_PAIR_BASES[lane] canonical = 'FADDP_asisdpair_only_' + ('H' if lane == 2 else 'SD') elif name == 'fnmul': if not scalar or index is not None: raise ValueError('FNMUL is scalar and non-indexed') word = BINARY_REMAINING_NEGATED_BASE | {2: 3, 4: 0, 8: 1}[lane] << 22 canonical = 'FNMUL_' + {2: 'H', 4: 'S', 8: 'D'}[lane] + '_floatdp2' elif index is not None: if (name not in BINARY_REMAINING_INDEXED_BASES or not 0 <= index < 16 // lane or m is None or lane == 2 and m >= 16): raise ValueError('Unallocated indexed source') word = BINARY_REMAINING_INDEXED_BASES[name] | int(scalar) << 28 | int(scalar or width == 16) << 30 if lane == 2: word |= ((index >> 2) & 1) << 11 | (index & 3) << 20 elif lane == 4: word |= 1 << 23 | ((index >> 1) & 1) << 11 | (index & 1) << 21 else: word |= 3 << 22 | index << 11 canonical = name.upper() + ('_asisdelem_' if scalar else '_asimdelem_') + ('RH_H' if lane == 2 else 'R_SD') elif name in BINARY_REMAINING_REGISTER_BASES: if name == 'faddp' and scalar: raise ValueError('Scalar pair requires its one-source form') half, standard = BINARY_REMAINING_REGISTER_BASES[name] word = (half if lane == 2 else standard | int(lane == 8) << 22) word |= int(scalar) << 28 | int(scalar or width == 16) << 30 canonical = name.upper() + ('_asisdsame' if scalar else '_asimdsame') + ('fp16_only' if lane == 2 else '_only') else: raise ValueError('Unknown remaining binary mnemonic') if m is not None: if not 0 <= m < 32: raise ValueError('Invalid right source register') word |= m << 16 elif not (pair and scalar): raise ValueError('Binary instruction requires a right source') return {'name': name, 'lane': lane, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'm': m, 'index': index, 'word': word | n << 5 | d, 'canonical_id': canonical, 'source_lane_count': 2 if pair and scalar else 1 if scalar else width // lane, 'output_kind': 'vector', 'input_kind': 'scalar_pair' if pair and scalar else 'vector_pair' if pair else 'indexed' if index is not None else 'register', 'merge': 'left_if_nep' if scalar and not pair else 'none', 'cohort_family': 'binary_remaining'} def binary_remaining_functions(): functions = [] for name in ('fnmul', 'fabd', 'faddp', 'fmulx', 'fmul'): indexed_modes = (False, True) if name == 'fmulx' else (True,) if name == 'fmul' else (False,) shapes = [(lane, lane, True) for lane in (2, 4, 8)] if name != 'fnmul': shapes += [(lane, width, False) for lane, width in VECTOR_NUMERICAL_SHAPES] for indexed in indexed_modes: for lane, width, scalar in shapes: for index in (range(16 // lane) if indexed else (None,)): roles = [(d, n, None) for d, n in NUMERICAL_UNARY_ROLES] if name == 'faddp' and scalar else COMPARISON_MINMAX_BINARY_ROLES for d, n, m in roles: if indexed and lane == 2: m &= 15 functions.append(binary_remaining_operation(name, lane, width, scalar, d, n, m, index)) if len(functions) != len({op['word'] for op in functions}): raise AssertionError('Remaining binary subject words must be unique') return functions def binary_remaining_pairs(lane): v = dict(numerical_unary_values(lane)) one, two, half = (v[name + '_positive'] for name in ('one', 'two', 'half')) sign, fraction = 1 << (lane * 8 - 1), {2: 10, 4: 23, 8: 52}[lane] pairs = [('one_one', one, one), ('one_two', one, two), ('opposed_one', one, one | sign), ('product_residual', one + 1, one - 1), ('product_round', one + 1, one + 1), ('sum_tie_even', one, ({2: 15, 4: 127, 8: 1023}[lane] - fraction - 1) << fraction), ('sum_tie_odd', one + 1, ({2: 15, 4: 127, 8: 1023}[lane] - fraction - 1) << fraction), ('negative_round', one | sign, one + 1), ('normal_boundary', v['minnormal_positive'], half), ('overflow', v['maxfinite_positive'], two), ('maximum', v['maxfinite_positive'], v['maxfinite_positive']), ('tiny', v['minsubnormal_positive'], half)] for left, right in (('zero', 'zero'), ('zero', 'infinity'), ('infinity', 'zero'), ('infinity', 'infinity')): for ls, rs in (('positive', 'positive'), ('positive', 'negative'), ('negative', 'positive'), ('negative', 'negative')): pairs.append((left + '_' + ls + '_' + right + '_' + rs, v[left + '_' + ls], v[right + '_' + rs])) for label in ('minsubnormal_positive', 'minsubnormal_negative', 'maxsubnormal_positive', 'minnormal_positive', 'qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): pairs.extend(((label + '_left', v[label], one), (label + '_right', one, v[label]))) pairs.extend((('denormal_infinity', v['minsubnormal_positive'], v['infinity_negative']), ('infinity_denormal', v['infinity_negative'], v['minsubnormal_positive']), ('nan_denormal', v['qnan_negative'], v['minsubnormal_positive']), ('denormal_nan', v['minsubnormal_positive'], v['qnan_negative']))) if len(pairs) != 48: raise AssertionError('Remaining binary directed catalog changed') return pairs def binary_remaining_state(op, case, pairs, control=0, sticky=0): if len(pairs) != (1 if op['input_kind'] == 'scalar_pair' else op['bytes'] // op['lane']): raise ValueError('Incorrect remaining binary active pair count') # The common binary state generator owns full-state canaries and poison. shape = dict(op, bytes=op['lane'] if op['scalar'] else op['bytes'], m=op['n'] if op['m'] is None else op['m']) initial = vector_numerical_state(shape, case, [(left, right) for left, right in pairs], control) initial.fpsr = sticky lane, bits = op['lane'], op['lane'] * 8 poison = dict(numerical_unary_values(lane))['snan_positive'] def store(register, values): packed = sum(poison << (position * bits) for position in range(16 // lane)) for position, value in values: if not 0 <= value < 1 << bits: raise ValueError('Invalid remaining binary input bits') mask = ((1 << bits) - 1) << (position * bits) packed = packed & ~mask | value << (position * bits) initial.v[register][:] = [packed & MASK, packed >> 64] if op['input_kind'] == 'scalar_pair': store(op['n'], enumerate(pairs[0])) elif op['input_kind'] == 'vector_pair': half = len(pairs) // 2 for register, active in ((op['n'], pairs[:half]), (op['m'], pairs[half:])): store(register, enumerate(value for pair in active for value in pair)) elif op['index'] is not None: store(op['n'], enumerate(left for left, _ in pairs)) store(op['m'], [(op['index'], pairs[0][1])]) return initial def binary_remaining_shape(op): return op['name'], op['lane'], op['bytes'], op['scalar'], op['input_kind'] def binary_remaining_corpus(): functions, records, primary, enabled = binary_remaining_functions(), [], set(), set() rng = random.Random(BINARY_REMAINING_RANDOM_SEED) def append(function, label, pairs, control=0, sticky=0): initial = binary_remaining_state(functions[function], len(records), pairs, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function, op in enumerate(functions): shape = binary_remaining_shape(op) count = 1 if op['input_kind'] == 'scalar_pair' else op['bytes'] // op['lane'] pairs = binary_remaining_pairs(op['lane']) catalog = {label: (left, right) for label, left, right in pairs} v = dict(numerical_unary_values(op['lane'])) if shape not in primary: primary.add(shape) for mode in range(4): for label, left, right in pairs: append(function, 'directed_' + label, [(left, right)] * count, mode << NUMERICAL_ROUND_SHIFT) for mode in range(4): for label in ('product_round', 'negative_round', 'sum_tie_even', 'sum_tie_odd', 'opposed_one', 'tiny', 'overflow', 'normal_boundary'): append(function, 'ah_round_' + label, [catalog[label]] * count, NUMERICAL_AH | mode << NUMERICAL_ROUND_SHIFT) matrix = [v['qnan_positive'], v['qnan_negative'], v['qnan_positive'] + 7, v['qnan_negative'] + 7, v['snan_positive'], v['snan_negative']] for control in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): for left, x in enumerate(matrix): for right, y in enumerate(matrix): append(function, f'nan_matrix_{left}_{right}', [(x, y)] * count, control) for control in BINARY_REMAINING_CONTROLS: for label in ('product_round', 'negative_round', 'tiny', 'normal_boundary', 'denormal_infinity', 'infinity_denormal', 'nan_denormal', 'denormal_nan'): append(function, 'control_' + label, [catalog[label]] * count, control) for sticky in (1, 1 << 4, 1 << 27, FP_REGISTER_MASKS['fpsr']): for control in (0, NUMERICAL_AH): for label in ('one_one', 'product_round', 'snan_negative_left', 'tiny'): append(function, 'sticky_' + label, [catalog[label]] * count, control, sticky) for mode in range(4): for iteration in range(8): active = [(rng.getrandbits(op['lane'] * 8), rng.getrandbits(op['lane'] * 8)) for _ in range(count)] append(function, f'bounded_random_{iteration}', active, mode << NUMERICAL_ROUND_SHIFT) if not op['scalar']: for control in (0, NUMERICAL_AH, NUMERICAL_FZ16, NUMERICAL_AH | NUMERICAL_FZ): for label in ('one_two', 'snan_negative_left', 'tiny', 'denormal_infinity', 'negative_round', 'overflow'): for position in range(count): active = [catalog['one_one']] * count active[position] = catalog[label] append(function, f'mixed_{label}_{position}', active, control) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_enable_readback_only', [catalog['snan_negative_left']] * count, control) if op['canonical_id'] not in enabled: enabled.add(op['canonical_id']) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_ah_enable_readback_only', [catalog['one_one']] * count, control | NUMERICAL_AH) if op['index'] not in (None, 0): for control in (0, NUMERICAL_AH, NUMERICAL_NEP): for label in ('one_two', 'product_round', 'snan_negative_right', 'denormal_infinity'): append(function, 'indexed_' + label, [catalog[label]] * count, control) elif (op['d'], op['n'], op['m']) != ((0, 1, None) if op['input_kind'] == 'scalar_pair' else (0, 1, 2)): for control in (0, NUMERICAL_AH, NUMERICAL_NEP, NUMERICAL_FZ16): for label in ('one_two', 'product_round', 'snan_negative_right', 'tiny'): append(function, 'alias_' + label, [catalog[label]] * count, control) if len(records) != BINARY_REMAINING_CASES: raise AssertionError('Remaining binary corpus exceeded approved bounded scope') return functions, records def binary_remaining_runtime_cases(records): selected = {} for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) shape = binary_remaining_shape(op) if initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK: enabled = initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK selected.setdefault(('enable', op['canonical_id'] if enabled == NUMERICAL_TRAP_ENABLE_MASK else enabled, bool(initial.fpcr & NUMERICAL_AH)), record) elif label == 'directed_one_one' and initial.fpcr == 0: selected.setdefault(('shape', shape), record) elif label == 'directed_product_round': selected.setdefault(('round', op['name'], op['lane'], initial.fpcr), record) elif label in ('directed_zero_positive_infinity_positive', 'directed_snan_negative_left', 'directed_denormal_infinity', 'directed_sum_tie_odd') and initial.fpcr == 0: selected.setdefault(('semantic', op['name'], op['lane'], label), record) elif label == 'indexed_one_two' and initial.fpcr == 0: selected.setdefault(('index', shape, op['index']), record) elif label == 'control_denormal_nan': selected.setdefault(('control', op['lane'], initial.fpcr), record) elif label == 'alias_one_two' and initial.fpcr == NUMERICAL_NEP: selected.setdefault(('alias', op['name'], op['d'], op['n'], op['m']), record) elif label == 'sticky_product_round' and initial.fpcr == 0: selected.setdefault(('sticky', op['name']), record) elif label == 'mixed_one_two_0' and op['name'] == 'faddp' and initial.fpcr == 0: selected.setdefault(('pair_order', shape), record) return list({record['case']: record for record in selected.values()}.values()) def binary_remaining_helpers(records, case_ids=None): selected = {} for record in records: op, initial = record['operation'], State.from_buffer_copy(record['initial']) if (case_ids is not None and record['case'] not in case_ids or initial.fpcr or initial.fpsr or op['d'] != 0 or op['n'] != 1 or op['m'] not in (None, 2) or op['index'] not in (None, 0)): continue selected.setdefault(binary_remaining_shape(op), record) return list(selected.values()) def validate_binary_remaining_original_effects(initial, original, operation): if operation['scalar']: source = initial.v[operation['n']] if operation['merge'] != 'none' and initial.fpcr & NUMERICAL_NEP else (0, 0) merge = struct.pack('<2Q', *source) if struct.pack('<2Q', *original.v[operation['d']])[operation['lane']:] != merge[operation['lane']:]: raise AssertionError('Remaining binary scalar inactive bits violated merge/clear contract') def prepare_binary_remaining_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *binary_remaining_corpus(), BINARY_REMAINING_FOCUS) def load_binary_remaining_oracle(path=BINARY_REMAINING_ORACLE): return load_full_frame_numerical_oracle(path, binary_remaining_functions(), BINARY_REMAINING_CASES, BINARY_REMAINING_FOCUS) WIDENING_NUMERICAL_FOCUS = 'widening_numerical' WIDENING_NUMERICAL_ORACLE = ROOT / '.stela/research/fhm-numerical/harness/oracle/receipt.json' WIDENING_REGISTER_BASES = {'fmlal': 0x0e20ec00, 'fmlal2': 0x2e20cc00, 'fmlsl': 0x0ea0ec00, 'fmlsl2': 0x2ea0cc00} WIDENING_INDEXED_BASES = {'fmlal': 0x0f800000, 'fmlal2': 0x2f808000, 'fmlsl': 0x0f804000, 'fmlsl2': 0x2f80c000} WIDENING_INDEXED_ROLES = ((0, 1, 2), (1, 1, 2), (2, 1, 2), (0, 1, 1), (15, 15, 15), (31, 31, 15), (31, 30, 14)) WIDENING_CONTROLS = (*BINARY_REMAINING_CONTROLS, NUMERICAL_FIZ | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_FIZ | NUMERICAL_FZ) WIDENING_NUMERICAL_CASES = 21256 WIDENING_RANDOM_SEED = 0x574944454e494e47 def widening_operation(name, width, d=0, n=1, m=2, index=None): if (name not in WIDENING_REGISTER_BASES or width not in (8, 16) or any(not 0 <= register < 32 for register in (d, n, m)) or index is not None and (m >= 16 or not 0 <= index < 8)): raise ValueError('Unallocated widening FP16 operand') word = (WIDENING_REGISTER_BASES if index is None else WIDENING_INDEXED_BASES)[name] if index is not None: word |= ((index >> 2) & 1) << 11 | ((index >> 1) & 1) << 21 | (index & 1) << 20 upper = name.endswith('2') return {'name': name, 'lane': 4, 'source_lane': 2, 'bytes': width, 'scalar': False, 'd': d, 'a': d, 'n': n, 'm': m, 'index': index, 'upper': upper, 'source_lane_offset': width // 4 if upper else 0, 'source_lane_count': width // 4, 'word': word | int(width == 16) << 30 | m << 16 | n << 5 | d, 'canonical_id': name.upper() + ('_asimdsame_F' if index is None else '_asimdelem_LH'), 'output_kind': 'vector', 'input_kind': 'register' if index is None else 'indexed', 'merge': 'none', 'cohort_family': 'widening'} def widening_functions(): functions = [] for name in WIDENING_REGISTER_BASES: for width in (8, 16): for index in (None, *range(8)): roles = FUSED_ACCUMULATE_ROLES if index is None else WIDENING_INDEXED_ROLES functions.extend(widening_operation(name, width, d, n, m, index) for d, n, m in roles) if len(functions) != len({op['word'] for op in functions}): raise AssertionError('Widening subject words must be unique') return functions def widening_triples(): h, s = dict(numerical_unary_values(2)), dict(numerical_unary_values(4)) one, half = h['one_positive'], h['half_positive'] small = conversion_exact_positive_bits(2, 1, -12) triples = [('one_one_one', s['one_positive'], one, one), ('two_two_one', s['two_positive'], h['two_positive'], one), ('cancel_negative', s['one_negative'], one + 1, one - 2), ('cancel_positive', s['one_positive'], one + 1, one - 2), ('tie_even', s['one_positive'], small, small), ('tie_odd', s['one_positive'] + 1, small, small), ('tie_above', s['one_positive'], small + 1, small), ('tie_below', s['one_positive'], small - 1, small), ('negative_tie', s['one_negative'], small | h['zero_negative'], small), ('max_half_product', 0, h['maxfinite_positive'], h['maxfinite_positive']), ('max_single_add', s['maxfinite_positive'], one, one), ('min_half_product', 0, h['minsubnormal_positive'], h['minsubnormal_positive']), ('min_half_cancel', conversion_exact_positive_bits(4, 1, -48) | s['zero_negative'], 1, 1), ('single_subnormal', s['minsubnormal_positive'], 0, one), ('negative_single_subnormal', s['minsubnormal_negative'], 0, one), ('max_single_subnormal', s['maxsubnormal_positive'], 0, one)] for addend in ('positive', 'negative'): for left in ('positive', 'negative'): triples.append((f'zero_{addend}_{left}', s['zero_' + addend], h['zero_' + left], one)) for left, right in (('infinity_positive', 'zero_positive'), ('zero_negative', 'infinity_positive'), ('infinity_negative', 'one_positive'), ('one_positive', 'infinity_negative')): triples.append(('special_' + left + '_' + right, s['one_positive'], h[left], h[right])) for label in ('qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): triples.extend((('addend_' + label, s[label], one, one), ('left_' + label, s['one_positive'], h[label], one), ('right_' + label, s['one_positive'], one, h[label]))) triples.extend((('qnan_invalid_product', s['qnan_negative'], h['infinity_positive'], 0), ('opposed_infinity', s['infinity_negative'], h['infinity_positive'], one), ('same_infinity', s['infinity_positive'], h['infinity_positive'], one), ('half_denormal', s['one_positive'], h['minsubnormal_positive'], one), ('half_denormal_negative', s['one_positive'], h['minsubnormal_negative'], one), ('half_denormal_nan', s['qnan_negative'], h['minsubnormal_positive'], one), ('single_denormal_nan', s['minsubnormal_positive'], h['qnan_negative'], one), ('single_denormal_invalid', s['minsubnormal_positive'], h['infinity_positive'], 0), ('single_denormal_infinity', s['minsubnormal_positive'], h['infinity_positive'], one), ('half_denormal_infinity', 0, h['minsubnormal_positive'], h['infinity_positive']), ('both_denormals', s['minsubnormal_positive'], h['minsubnormal_positive'], one), ('exact_half_subnormal', 0, h['minsubnormal_positive'], one))) return triples def widening_state(op, case, triples, control=0, sticky=0): if (len(triples) != op['source_lane_count'] or any(not 0 <= addend < 1 << 32 or not 0 <= left < 1 << 16 or not 0 <= right < 1 << 16 for addend, left, right in triples)): raise ValueError('Invalid widening mixed-width input') initial = fused_numerical_state(dict(op, index=None), case, triples, control) initial.fpsr = sticky def store(register, lane, values): bits = lane * 8 poison = dict(numerical_unary_values(lane))['snan_positive'] packed = sum(poison << (position * bits) for position in range(16 // lane)) for position, value in values: mask = ((1 << bits) - 1) << (position * bits) packed = packed & ~mask | value << (position * bits) initial.v[register][:] = [packed & MASK, packed >> 64] # Aliases resolve through complete source writes; the retained frame owns # the actual accumulator/multiplicands, rather than the requested triples. store(op['d'], 4, enumerate(addend for addend, _, _ in triples)) store(op['n'], 2, enumerate((left for _, left, _ in triples), op['source_lane_offset'])) right = [(op['index'], triples[0][2])] if op['index'] is not None else enumerate((right for _, _, right in triples), op['source_lane_offset']) store(op['m'], 2, right) return initial def widening_shape(op): return op['name'], op['bytes'], op['index'] is not None def widening_corpus(): functions, records, primary, enabled = widening_functions(), [], set(), set() triples = widening_triples() catalog = {label: (addend, left, right) for label, addend, left, right in triples} h, s = dict(numerical_unary_values(2)), dict(numerical_unary_values(4)) rng = random.Random(WIDENING_RANDOM_SEED) def append(function, label, active, control=0, sticky=0): initial = widening_state(functions[function], len(records), active, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) for function, op in enumerate(functions): shape, count = widening_shape(op), op['source_lane_count'] if shape not in primary: primary.add(shape) for mode in range(4): for label, addend, left, right in triples: append(function, 'directed_' + label, [(addend, left, right)] * count, mode << NUMERICAL_ROUND_SHIFT) for mode in range(4): for label in ('cancel_negative', 'cancel_positive', 'tie_even', 'tie_odd', 'tie_above', 'negative_tie', 'max_single_add', 'single_subnormal', 'negative_single_subnormal', 'half_denormal', 'single_denormal_nan', 'qnan_invalid_product'): append(function, 'ah_round_' + label, [catalog[label]] * count, NUMERICAL_AH | mode << NUMERICAL_ROUND_SHIFT) for variant in range(2): addends = [s['one_positive'], s['qnan_positive'] + variant * 7, s['snan_negative'] + variant * 11] lefts = [h['one_positive'], h['qnan_negative'] + variant * 3, h['snan_positive'] + variant * 5] rights = [h['two_positive'], h['qnan_positive'] + variant * 9, h['snan_negative'] + variant * 13] for a, addend in enumerate(addends): for n, left in enumerate(lefts): for m, right in enumerate(rights): if not (a or n or m): continue for control in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): append(function, f'nan_matrix_{variant}_{a}_{n}_{m}', [(addend, left, right)] * count, control) for control in WIDENING_CONTROLS: for label in ('tie_odd', 'max_single_add', 'single_subnormal', 'negative_single_subnormal', 'half_denormal', 'half_denormal_negative', 'half_denormal_nan', 'single_denormal_nan', 'single_denormal_invalid', 'single_denormal_infinity', 'half_denormal_infinity', 'both_denormals', 'left_snan_positive', 'right_snan_negative'): append(function, 'control_' + label, [catalog[label]] * count, control) for sticky in (1, 1 << 4, 1 << 27, FP_REGISTER_MASKS['fpsr']): for control in (0, NUMERICAL_AH): for label in ('one_one_one', 'tie_odd', 'left_snan_positive', 'single_subnormal'): append(function, 'sticky_' + label, [catalog[label]] * count, control, sticky) for control in (0, NUMERICAL_AH, NUMERICAL_FZ16, NUMERICAL_AH | NUMERICAL_FZ): for label in ('tie_odd', 'left_snan_positive', 'single_subnormal', 'half_denormal', 'max_single_add', 'qnan_invalid_product'): for position in range(count): active = [catalog['one_one_one']] * count active[position] = catalog[label] append(function, f'mixed_{label}_{position}', active, control) for mode in range(4): for iteration in range(16): active = [(rng.getrandbits(32), rng.getrandbits(16), rng.getrandbits(16)) for _ in range(count)] append(function, f'bounded_random_{iteration}', active, mode << NUMERICAL_ROUND_SHIFT) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_enable_readback_only', [catalog['one_one_one']] * count, control) if op['canonical_id'] not in enabled: enabled.add(op['canonical_id']) for control in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): append(function, 'trap_ah_enable_readback_only', [catalog['left_snan_positive']] * count, control | NUMERICAL_AH) witness = [(conversion_exact_positive_bits(4, position + 1, 0), conversion_exact_positive_bits(2, position + 2, 0), h['half_positive']) for position in range(count)] for mode in range(4): append(function, 'layout_witness', witness, mode << NUMERICAL_ROUND_SHIFT) if op['index'] not in (None, 0): for control in (0, NUMERICAL_AH, NUMERICAL_NEP): for label in ('one_one_one', 'cancel_negative', 'left_snan_positive', 'single_denormal_nan'): append(function, 'indexed_' + label, [catalog[label]] * count, control) elif (op['d'], op['n'], op['m']) != (0, 1, 2): for control in (0, NUMERICAL_AH, NUMERICAL_NEP, NUMERICAL_FZ16): for label in ('one_one_one', 'cancel_negative', 'left_snan_positive', 'single_subnormal'): append(function, 'alias_' + label, [catalog[label]] * count, control) if len(records) != WIDENING_NUMERICAL_CASES: raise AssertionError("Widening corpus exceeded approved bounded scope") return functions, records def widening_runtime_cases(records): selected = {} for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) shape = widening_shape(op) if initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK: enables = initial.fpcr & NUMERICAL_TRAP_ENABLE_MASK selected.setdefault(('enable', op['canonical_id'] if enables == NUMERICAL_TRAP_ENABLE_MASK else enables, bool(initial.fpcr & NUMERICAL_AH)), record) elif label == 'directed_one_one_one' and initial.fpcr == 0: selected.setdefault(('shape', shape), record) elif label == 'layout_witness' and initial.fpcr == 0 and (op['d'], op['n'], op['m']) == (0, 1, 2): selected.setdefault(('layout', shape, op['index']), record) elif label == 'directed_tie_odd': selected.setdefault(('round', op['name'], initial.fpcr), record) elif label in ('directed_cancel_negative', 'directed_max_single_add', 'directed_single_subnormal', 'directed_left_snan_positive', 'directed_qnan_invalid_product') and initial.fpcr == 0: selected.setdefault(('semantic', op['name'], label), record) elif label in ('control_single_subnormal', 'control_single_denormal_nan'): selected.setdefault(('control', initial.fpcr, label), record) elif label == 'alias_one_one_one' and initial.fpcr == NUMERICAL_NEP: selected.setdefault(('alias', op['d'], op['n'], op['m']), record) elif label == 'sticky_tie_odd' and initial.fpcr == 0: selected.setdefault(('sticky', op['name']), record) elif label == 'mixed_left_snan_positive_0' and initial.fpcr == NUMERICAL_AH: selected.setdefault(('mixed_status', shape), record) elif label == 'nan_matrix_0_2_1_0' and initial.fpcr in (0, NUMERICAL_AH): selected.setdefault(('mixed_nan_priority', op['name'], initial.fpcr), record) return list({record['case']: record for record in selected.values()}.values()) def widening_helpers(records, case_ids=None): selected = {} for record in records: op, initial = record['operation'], State.from_buffer_copy(record['initial']) if (record['label'] != 'directed_addend_qnan_positive' or case_ids is not None and record['case'] not in case_ids or initial.fpcr or initial.fpsr or (op['d'], op['n'], op['m']) != (0, 1, 2) or op['index'] not in (None, 0)): continue selected.setdefault(widening_shape(op), record) return list(selected.values()) def prepare_widening_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *widening_corpus(), WIDENING_NUMERICAL_FOCUS) def load_widening_oracle(path=WIDENING_NUMERICAL_ORACLE): return load_full_frame_numerical_oracle(path, widening_functions(), WIDENING_NUMERICAL_CASES, WIDENING_NUMERICAL_FOCUS) BFLOAT16_NUMERICAL_FOCUS = 'bfloat16_numerical' BFLOAT16_NUMERICAL_ORACLE = ROOT / '.stela/research/bfloat16-numerical/harness/oracle/receipt.json' BFLOAT16_NUMERICAL_CASES = 21002 BFLOAT16_BITS = 16 BFLOAT16_FRACTION_BITS = 7 BFLOAT16_EXPONENT_MASK = 255 BFLOAT16_SIGN = 1 << 15 BFLOAT16_QUIET = 1 << 6 BFLOAT16_INFINITY = 255 << BFLOAT16_FRACTION_BITS BFLOAT16_ONE = 127 << BFLOAT16_FRACTION_BITS BFLOAT16_EBF = 1 << 13 BFLOAT16_SCALAR_CONVERT = 0x1e634000 BFLOAT16_NARROW_CONVERT = 0x0ea16800 BFLOAT16_DOT_VECTOR = 0x2e40fc00 BFLOAT16_DOT_INDEXED = 0x0f40f000 BFLOAT16_MLA_VECTOR = 0x2ec0fc00 BFLOAT16_MLA_INDEXED = 0x0fc0f000 BFLOAT16_MATRIX = 0x6e40ec00 BFLOAT16_RANDOM_SEED = 0x42464c4f41543136 BFLOAT16_CONTROLS = (*WIDENING_CONTROLS, NUMERICAL_AH | NUMERICAL_DN | NUMERICAL_FZ) def bfloat16_operation(kind, width, d=0, n=1, m=None, index=None, upper=False, parity=0): if any(not 0 <= register < 32 for register in (d, n)) or m is not None and not 0 <= m < 32: raise ValueError('Invalid BF16 register') if kind == 'conversion': if width not in (2, 8, 16) or m is not None or index is not None or parity or upper != (width == 16): raise ValueError('Invalid BF16 conversion shape') scalar = width == 2 word = BFLOAT16_SCALAR_CONVERT if scalar else BFLOAT16_NARROW_CONVERT | int(upper) << 30 name = 'bfcvt' if scalar else 'bfcvtn2' if upper else 'bfcvtn' canonical = 'BFCVT_BS_floatdp1' if scalar else 'BFCVTN_asimdmisc_4S' elif kind == 'dot': if width not in (8, 16) or m is None or upper or parity or index is not None and not 0 <= index < 4: raise ValueError('Invalid BF16 dot shape') scalar, name = False, 'bfdot' word = (BFLOAT16_DOT_VECTOR if index is None else BFLOAT16_DOT_INDEXED) | int(width == 16) << 30 if index is not None: word |= (index & 1) << 21 | (index >> 1) << 11 canonical = 'BFDOT_asimdsame2_D' if index is None else 'BFDOT_asimdelem_E' elif kind == 'multiply': if width != 16 or m is None or upper or parity not in (0, 1) or index is not None and (m >= 16 or not 0 <= index < 8): raise ValueError('Invalid BF16 multiply-add shape') scalar, name = False, 'bfmlalt' if parity else 'bfmlalb' word = (BFLOAT16_MLA_VECTOR if index is None else BFLOAT16_MLA_INDEXED) | parity << 30 if index is not None: word |= ((index >> 2) & 1) << 11 | ((index >> 1) & 1) << 21 | (index & 1) << 20 canonical = 'BFMLAL_asimdsame2_F_' if index is None else 'BFMLAL_asimdelem_F' elif kind == 'matrix': if width != 16 or m is None or upper or parity or index is not None: raise ValueError('Invalid BF16 matrix shape') scalar, name, word, canonical = False, 'bfmmla', BFLOAT16_MATRIX, 'BFMMLA_asimdsame2_E' else: raise ValueError('Unknown BF16 family') result_count = 1 if scalar else 4 if kind == 'conversion' else width // 4 operation = {'kind': kind, 'name': name, 'lane': 2 if kind == 'conversion' else 4, 'source_lane': 4 if kind == 'conversion' else 2, 'bytes': width, 'scalar': scalar, 'd': d, 'n': n, 'm': m, 'index': index, 'upper': upper, 'parity': parity, 'word': word | n << 5 | d | (0 if m is None else m << 16), 'canonical_id': canonical, 'source_lane_count': result_count if kind == 'conversion' else result_count * 2 if kind == 'dot' else 8 if kind == 'matrix' else 4, 'result_lane_count': result_count, 'output_kind': 'vector', 'input_kind': 'single' if kind == 'conversion' else 'indexed' if index is not None else 'register', 'merge': 'old_destination_if_nep' if scalar else 'preserve_low64' if upper else 'none', 'cohort_family': 'bfloat16'} if kind != 'conversion': operation['a'] = d return operation def bfloat16_functions(): functions = [bfloat16_operation('conversion', width, d, n, upper=width == 16) for width in (2, 8, 16) for d, n in NUMERICAL_UNARY_ROLES] for width in (8, 16): for index in (None, *range(4)): functions.extend(bfloat16_operation('dot', width, d, n, m, index) for d, n, m in FUSED_ACCUMULATE_ROLES) for parity in (0, 1): for index in (None, *range(8)): roles = FUSED_ACCUMULATE_ROLES if index is None else WIDENING_INDEXED_ROLES functions.extend(bfloat16_operation('multiply', 16, d, n, m, index, parity=parity) for d, n, m in roles) functions.extend(bfloat16_operation('matrix', 16, d, n, m) for d, n, m in FUSED_ACCUMULATE_ROLES) if len(functions) != len({op['word'] for op in functions}): raise AssertionError('BF16 subject words must be unique') return functions def bfloat16_values(): one, infinity = BFLOAT16_ONE, BFLOAT16_INFINITY return {'zero_positive': 0, 'zero_negative': BFLOAT16_SIGN, 'one_positive': one, 'one_negative': one | BFLOAT16_SIGN, 'two_positive': one + (1 << BFLOAT16_FRACTION_BITS), 'half_positive': one - (1 << BFLOAT16_FRACTION_BITS), 'minsubnormal_positive': 1, 'minsubnormal_negative': 1 | BFLOAT16_SIGN, 'maxsubnormal_positive': (1 << BFLOAT16_FRACTION_BITS) - 1, 'minnormal_positive': 1 << BFLOAT16_FRACTION_BITS, 'maxfinite_positive': infinity - 1, 'infinity_positive': infinity, 'infinity_negative': infinity | BFLOAT16_SIGN, 'qnan_positive': infinity | BFLOAT16_QUIET | 3, 'qnan_negative': infinity | BFLOAT16_QUIET | BFLOAT16_SIGN | 5, 'snan_positive': infinity | 1, 'snan_negative': infinity | BFLOAT16_SIGN | 7} def bfloat16_conversion_values(): values = list(numerical_unary_values(4)) for top in (0, 1, 0x007f, 0x0080, 0x3f80, 0x3f81, 0x7f7f): for low in (0, 0x7fff, 0x8000, 0x8001, 0xffff): for sign in (0, 1 << 31): values.append((f'bf_round_{top}_{low}_{bool(sign)}', top << 16 | low | sign)) return values def bfloat16_triples(): b, s = bfloat16_values(), dict(numerical_unary_values(4)) one, small = b['one_positive'], (127 - 12) << BFLOAT16_FRACTION_BITS triples = [('one_one_one', s['one_positive'], one, one), ('cancel_negative', s['one_negative'], one, one), ('cancel_positive', s['one_positive'], one | BFLOAT16_SIGN, one), ('single_round_residual', s['one_negative'], one + 1, one - 2), ('tie_even', s['one_positive'], small, small), ('tie_odd', s['one_positive'] + 1, small, small), ('tie_above', s['one_positive'], small + 1, small), ('tie_below', s['one_positive'], small - 1, small), ('maximum_product', 0, b['maxfinite_positive'], b['maxfinite_positive']), ('overflow_cancel', s['maxfinite_positive'], b['maxfinite_positive'] | BFLOAT16_SIGN, b['two_positive']), ('tiny_product', 0, b['minnormal_positive'], b['minnormal_positive']), ('subnormal_product', 0, b['minsubnormal_positive'], b['one_positive']), ('single_subnormal', s['minsubnormal_positive'], 0, one), ('negative_single_subnormal', s['minsubnormal_negative'], 0, one), ('single_denormal_nan', s['minsubnormal_positive'], b['qnan_negative'], one), ('single_denormal_invalid', s['minsubnormal_positive'], b['infinity_positive'], 0), ('qnan_invalid_product', s['qnan_negative'], b['infinity_positive'], 0), ('half_denormal_nan', s['qnan_negative'], b['minsubnormal_positive'], one), ('opposed_infinity', s['infinity_negative'], b['infinity_positive'], one), ('same_infinity', s['infinity_positive'], b['infinity_positive'], one)] for addend in ('positive', 'negative'): for left in ('positive', 'negative'): triples.append((f'zero_{addend}_{left}', s['zero_' + addend], b['zero_' + left], one)) for label in ('minsubnormal_positive', 'minsubnormal_negative', 'maxsubnormal_positive', 'minnormal_positive', 'infinity_positive', 'infinity_negative', 'qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): triples.extend((('left_' + label, s['one_positive'], b[label], one), ('right_' + label, s['one_positive'], one, b[label]))) for label in ('qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): triples.append(('addend_' + label, s[label], one, one)) return triples def bfloat16_state(op, case, addends, left=None, right=None, control=0, sticky=0): if op['kind'] == 'conversion': count = op['source_lane_count'] if len(addends) != count: raise ValueError('Incorrect BF16 conversion source count') initial = vector_numerical_state(dict(op, lane=4, bytes=count * 4, m=op['n']), case, [(value, value) for value in addends], control) initial.fpsr = sticky return initial count = op['result_lane_count'] left_count = op['source_lane_count'] right_count = (2 if op['kind'] == 'dot' else 1) if op['index'] is not None else left_count if (len(addends) != count or left is None or len(left) != left_count or right is None or len(right) != right_count or any(not 0 <= value < 1 << 32 for value in addends) or any(not 0 <= value < 1 << 16 for value in (*left, *right))): raise ValueError('Incorrect BF16 arithmetic source layouts') seed = [(addends[position], left[position], right[position % len(right)]) for position in range(count)] initial = fused_numerical_state(dict(op, index=None), case, seed, control) initial.fpsr = sticky def store(register, lane, positions, values): bits = lane * 8 poison = BFLOAT16_INFINITY | 1 if lane == 2 else dict(numerical_unary_values(4))['snan_positive'] packed = sum(poison << (position * bits) for position in range(16 // lane)) for position, value in zip(positions, values): mask = ((1 << bits) - 1) << (position * bits) packed = packed & ~mask | value << (position * bits) initial.v[register][:] = [packed & MASK, packed >> 64] store(op['d'], 4, range(count), addends) positions = [2 * position + op['parity'] for position in range(4)] if op['kind'] == 'multiply' else range(left_count) store(op['n'], 2, positions, left) if op['index'] is not None: positions = range(2 * op['index'], 2 * op['index'] + 2) if op['kind'] == 'dot' else [op['index']] store(op['m'], 2, positions, right) return initial def bfloat16_shape(op): return op['kind'], op['name'], op['bytes'], op['index'] is not None def bfloat16_accepts_enabled_control(op, initial): return op['kind'] in ('dot', 'matrix') or bool(initial.fpcr & NUMERICAL_AH) def bfloat16_control_policy(op, initial, producer): return bool(initial.fpcr & BFLOAT16_EBF) and op['kind'] in ('conversion', 'multiply') def validate_bfloat16_original_effects(initial, original, op): if op['kind'] == 'conversion': if op['scalar']: merged = struct.pack('<2Q', *initial.v[op['d']]) if initial.fpcr & NUMERICAL_NEP else bytes(16) if struct.pack('<2Q', *original.v[op['d']])[2:] != merged[2:]: raise AssertionError('BF16 scalar old-Vd merge changed') elif op['upper'] and original.v[op['d']][0] != initial.v[op['d']][0]: raise AssertionError('BF16 narrowing upper lost old low64') if bfloat16_accepts_enabled_control(op, initial) and original.fpsr != initial.fpsr: raise AssertionError('BF16 suppressed-status operation generated flags') BFLOAT16_WITNESS_BF16_FRACTION_BITS = 7 BFLOAT16_WITNESS_SINGLE_FRACTION_BITS = 23 BFLOAT16_WITNESS_EXPONENT_BITS = 8 BFLOAT16_WITNESS_EXPONENT_MASK = (1 << BFLOAT16_WITNESS_EXPONENT_BITS) - 1 BFLOAT16_WITNESS_EXPONENT_BIAS = (1 << (BFLOAT16_WITNESS_EXPONENT_BITS - 1)) - 1 BFLOAT16_WITNESS_MINIMUM_NORMAL_EXPONENT = 1 - BFLOAT16_WITNESS_EXPONENT_BIAS BFLOAT16_WITNESS_MAXIMUM_NORMAL_EXPONENT = BFLOAT16_WITNESS_EXPONENT_BIAS BFLOAT16_WITNESS_SINGLE_SIGN = 1 << (BFLOAT16_WITNESS_SINGLE_FRACTION_BITS + BFLOAT16_WITNESS_EXPONENT_BITS) BFLOAT16_WITNESS_SINGLE_FRACTION_MASK = (1 << BFLOAT16_WITNESS_SINGLE_FRACTION_BITS) - 1 BFLOAT16_WITNESS_SINGLE_HIDDEN_BIT = 1 << BFLOAT16_WITNESS_SINGLE_FRACTION_BITS BFLOAT16_WITNESS_FPCR_AH = 1 << 1 BFLOAT16_WITNESS_FPCR_ROUNDING_SHIFT = 22 BFLOAT16_WITNESS_FPCR_ROUNDING_MASK = 3 BFLOAT16_WITNESS_ROUND_NEAREST_EVEN = 0 BFLOAT16_WITNESS_ROUND_POSITIVE = 1 BFLOAT16_WITNESS_ROUND_NEGATIVE = 2 BFLOAT16_WITNESS_ROUND_ZERO = 3 BFLOAT16_WITNESS_ROUND_ODD = 4 class Bfloat16ModelDomainError(ValueError): """The requested observation is outside the explicitly reviewed witness domain.""" @dataclass(frozen=True) class Bfloat16Dyadic: significand: int exponent: int negative_zero: bool = False @property def negative(self) -> bool: return self.significand < 0 or self.significand == 0 and self.negative_zero def _bfloat16_witness_decode(bits: int, fraction_bits: int) -> Bfloat16Dyadic: width = 1 + BFLOAT16_WITNESS_EXPONENT_BITS + fraction_bits if not isinstance(bits, int) or not 0 <= bits < 1 << width: raise Bfloat16ModelDomainError(f'Invalid {width}-bit input') sign = bool(bits >> (width - 1)) exponent = (bits >> fraction_bits) & BFLOAT16_WITNESS_EXPONENT_MASK fraction = bits & ((1 << fraction_bits) - 1) if exponent == BFLOAT16_WITNESS_EXPONENT_MASK: raise Bfloat16ModelDomainError('NaN/infinity inputs are outside finite witnesses') if exponent == 0: if fraction: raise Bfloat16ModelDomainError('Input subnormals are outside finite normal witnesses') return Bfloat16Dyadic(0, 0, sign) magnitude = (1 << fraction_bits) | fraction return Bfloat16Dyadic(-magnitude if sign else magnitude, exponent - BFLOAT16_WITNESS_EXPONENT_BIAS - fraction_bits) def _bfloat16_witness_product(left: Bfloat16Dyadic, right: Bfloat16Dyadic) -> Bfloat16Dyadic: value = left.significand * right.significand return Bfloat16Dyadic(value, left.exponent + right.exponent, value == 0 and left.negative != right.negative) def _bfloat16_witness_sum(left: Bfloat16Dyadic, right: Bfloat16Dyadic, mode: int) -> Bfloat16Dyadic: base = min(left.exponent, right.exponent) value = ((left.significand << (left.exponent - base)) + (right.significand << (right.exponent - base))) if value: return Bfloat16Dyadic(value, base) if left.significand == right.significand == 0 and left.negative == right.negative: negative = left.negative else: negative = mode == BFLOAT16_WITNESS_ROUND_NEGATIVE return Bfloat16Dyadic(0, base, negative) def _bfloat16_witness_round_single(value: Bfloat16Dyadic, mode: int) -> int: if mode not in (BFLOAT16_WITNESS_ROUND_NEAREST_EVEN, BFLOAT16_WITNESS_ROUND_POSITIVE, BFLOAT16_WITNESS_ROUND_NEGATIVE, BFLOAT16_WITNESS_ROUND_ZERO, BFLOAT16_WITNESS_ROUND_ODD): raise Bfloat16ModelDomainError('Unknown witness rounding mode') sign = BFLOAT16_WITNESS_SINGLE_SIGN if value.negative else 0 magnitude = abs(value.significand) if not magnitude: return sign leading = magnitude.bit_length() - 1 exponent = leading + value.exponent if exponent < BFLOAT16_WITNESS_MINIMUM_NORMAL_EXPONENT: if mode == BFLOAT16_WITNESS_ROUND_ODD: return sign # Classic BFRound flushes before any retained-bit jam. raise Bfloat16ModelDomainError('Extended tiny/subnormal stage is outside witnesses') if exponent > BFLOAT16_WITNESS_MAXIMUM_NORMAL_EXPONENT: raise Bfloat16ModelDomainError('Overflow stage is outside finite witnesses') shift = leading - BFLOAT16_WITNESS_SINGLE_FRACTION_BITS if shift <= 0: mantissa = magnitude << -shift else: divisor = 1 << shift mantissa, remainder = divmod(magnitude, divisor) if mode == BFLOAT16_WITNESS_ROUND_ODD: mantissa |= bool(remainder) elif mode == BFLOAT16_WITNESS_ROUND_NEAREST_EVEN: mantissa += (remainder * 2 > divisor or remainder * 2 == divisor and mantissa & 1) elif mode == BFLOAT16_WITNESS_ROUND_POSITIVE: mantissa += bool(remainder) and not value.negative elif mode == BFLOAT16_WITNESS_ROUND_NEGATIVE: mantissa += bool(remainder) and value.negative # Toward-zero leaves the truncated magnitude untouched. if mantissa >= BFLOAT16_WITNESS_SINGLE_HIDDEN_BIT << 1: mantissa >>= 1 exponent += 1 if exponent > BFLOAT16_WITNESS_MAXIMUM_NORMAL_EXPONENT: raise Bfloat16ModelDomainError('Rounded overflow is outside finite witnesses') return (sign | ((exponent + BFLOAT16_WITNESS_EXPONENT_BIAS) << BFLOAT16_WITNESS_SINGLE_FRACTION_BITS) | (mantissa & BFLOAT16_WITNESS_SINGLE_FRACTION_MASK)) def _bfloat16_witness_inputs(fpcr: int, addend: int, left: tuple[int, int], right: tuple[int, int]): if not isinstance(fpcr, int) or not 0 <= fpcr < 1 << 32: raise Bfloat16ModelDomainError('Invalid FPCR witness input') if len(left) != 2 or len(right) != 2: raise Bfloat16ModelDomainError('A dot witness needs exactly two products') return (_bfloat16_witness_decode(addend, BFLOAT16_WITNESS_SINGLE_FRACTION_BITS), tuple(_bfloat16_witness_decode(bits, BFLOAT16_WITNESS_BF16_FRACTION_BITS) for bits in left), tuple(_bfloat16_witness_decode(bits, BFLOAT16_WITNESS_BF16_FRACTION_BITS) for bits in right)) def bfloat16_standard_dot(fpcr: int, addend: int, left: tuple[int, int], right: tuple[int, int]) -> int: """Classic BFMulH twice, odd pair addition, then odd accumulator addition. FPCR is validated but intentionally has no numeric effect in this domain. In particular raw EBF bit 13 cannot select another algorithm in this API. """ accumulator, lhs, rhs = _bfloat16_witness_inputs(fpcr, addend, left, right) products = tuple(_bfloat16_witness_decode(_bfloat16_witness_round_single(_bfloat16_witness_product(lhs[i], rhs[i]), BFLOAT16_WITNESS_ROUND_ODD), BFLOAT16_WITNESS_SINGLE_FRACTION_BITS) for i in range(2)) pair = _bfloat16_witness_decode(_bfloat16_witness_round_single(_bfloat16_witness_sum(*products, BFLOAT16_WITNESS_ROUND_ODD), BFLOAT16_WITNESS_ROUND_ODD), BFLOAT16_WITNESS_SINGLE_FRACTION_BITS) return _bfloat16_witness_round_single(_bfloat16_witness_sum(accumulator, pair, BFLOAT16_WITNESS_ROUND_ODD), BFLOAT16_WITNESS_ROUND_ODD) def bfloat16_enhanced_dot(fpcr: int, addend: int, left: tuple[int, int], right: tuple[int, int]) -> int: """Genuine FPDot fused pair, then separate FPAdd, for finite witnesses. This is an independent enhanced-policy model, never a Stela support claim. Forced DN and AH/FIZ/FZ effects on exceptional/tiny inputs are outside this API's domain and cause refusal rather than a successful substitute result. """ accumulator, lhs, rhs = _bfloat16_witness_inputs(fpcr, addend, left, right) mode = (fpcr >> BFLOAT16_WITNESS_FPCR_ROUNDING_SHIFT) & BFLOAT16_WITNESS_FPCR_ROUNDING_MASK exact_products = tuple(_bfloat16_witness_product(lhs[i], rhs[i]) for i in range(2)) pair = _bfloat16_witness_decode(_bfloat16_witness_round_single(_bfloat16_witness_sum(*exact_products, mode), mode), BFLOAT16_WITNESS_SINGLE_FRACTION_BITS) return _bfloat16_witness_round_single(_bfloat16_witness_sum(accumulator, pair, mode), mode) def bfloat16_dot_profile_state(op, initial, enhanced): """Closed finite witnesses for the explicitly absent EBF16 profile.""" if op['kind'] not in ('dot', 'matrix'): raise ValueError('Only BF16 dot/matrix have an EBF profile split') left = struct.unpack('<8H', struct.pack('<2Q', *initial.v[op['n']])) right = struct.unpack('<8H', struct.pack('<2Q', *initial.v[op['m']])) addends = struct.unpack('<4I', struct.pack('<2Q', *initial.v[op['d']])) dot = bfloat16_enhanced_dot if enhanced else bfloat16_standard_dot values = [] for position in range(op['result_lane_count']): value = addends[position] if op['kind'] == 'dot': source = 2 * position selected = source if op['index'] is None else 2 * op['index'] value = dot(initial.fpcr, value, left[source:source + 2], right[selected:selected + 2]) else: row, column = divmod(position, 2) for stage in range(2): lhs, rhs = 4 * row + 2 * stage, 4 * column + 2 * stage value = dot(initial.fpcr, value, left[lhs:lhs + 2], right[rhs:rhs + 2]) values.append(value) expected = State.from_buffer_copy(initial) packed = struct.pack('<4I', *(values + [0] * (4 - len(values)))) expected.v[op['d']][:] = struct.unpack('<2Q', packed) return expected def bfloat16_profile_expectation(op, initial, original, producer): if op['kind'] not in ('dot', 'matrix') or not initial.fpcr & BFLOAT16_EBF: return None if producer.get('cpu_registers', {}).get('ID_AA64ISAR1_EL1') != '0x111211110211502': raise AssertionError('BF16 enhanced oracle must match calibrated original CPU') enhanced = bfloat16_dot_profile_state(op, initial, True) enhanced.fpcr = original.fpcr if bytes(enhanced) != bytes(original): raise AssertionError('Raw EBF1 original does not match independent enhanced witness') return bfloat16_dot_profile_state(op, initial, False) def bfloat16_corpus(): functions, records, primary = bfloat16_functions(), [], set() rng = random.Random(BFLOAT16_RANDOM_SEED) b, s = bfloat16_values(), dict(numerical_unary_values(4)) triples = bfloat16_triples() catalog = {label: (addend, left, right) for label, addend, left, right in triples} def append(function, label, addends, left=None, right=None, control=0, sticky=0): initial = bfloat16_state(functions[function], len(records), addends, left, right, control, sticky) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial_state_sha256': digest(bytes(initial)), 'initial': bytes(initial)}) def repeated(function, label, triple, control=0, sticky=0): op = functions[function] addend, left, right = triple right_count = (2 if op['kind'] == 'dot' else 1) if op['index'] is not None else op['source_lane_count'] append(function, label, [addend] * op['result_lane_count'], [left] * op['source_lane_count'], [right] * right_count, control, sticky) for function, op in enumerate(functions): shape, count = bfloat16_shape(op), op['result_lane_count'] first = shape not in primary if first: primary.add(shape) if op['kind'] == 'conversion': for ah in (0, NUMERICAL_AH): for mode in range(4): for label, value in bfloat16_conversion_values(): append(function, 'value_' + label, [value] * count, control=ah | mode << NUMERICAL_ROUND_SHIFT) for control in BFLOAT16_CONTROLS: for label in ('one_positive', 'minsubnormal_positive', 'minsubnormal_negative', 'maxsubnormal_positive', 'minnormal_positive', 'maxfinite_positive', 'qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): append(function, 'control_' + label, [s[label]] * count, control=control) for sticky in (1, 1 << 4, 1 << 27, FP_REGISTER_MASKS['fpsr']): for ah in (0, NUMERICAL_AH): append(function, 'sticky_inexact', [s['one_positive'] + 1] * count, control=ah, sticky=sticky) for mode in range(4): for iteration in range(16): append(function, f'bounded_random_{iteration}', [rng.getrandbits(32) for _ in range(count)], control=mode << NUMERICAL_ROUND_SHIFT) append(function, 'helper_quiet_nan', [s['qnan_positive']] * count) else: for ah in (0, NUMERICAL_AH): for mode in range(4): for label, addend, left, right in triples: repeated(function, 'directed_' + label, (addend, left, right), ah | mode << NUMERICAL_ROUND_SHIFT) if op['kind'] in ('dot', 'matrix'): for ah in (0, NUMERICAL_AH): for mode in range(4): control = ah | mode << NUMERICAL_ROUND_SHIFT cancelled = 0xc0000000 if op['kind'] == 'dot' else 0xc0800000 repeated(function, 'exact_cancel', (cancelled, BFLOAT16_ONE, BFLOAT16_ONE), control) left = [BFLOAT16_ONE, (127 - 12) << BFLOAT16_FRACTION_BITS] * (op['source_lane_count'] // 2) right = [BFLOAT16_ONE, (127 - 13) << BFLOAT16_FRACTION_BITS] append(function, 'fixed_odd_residual', [s['one_negative']] * count, left, right if op['index'] is not None else right * (op['source_lane_count'] // 2), control) if op['kind'] == 'matrix': for ah in (0, NUMERICAL_AH): for mode in range(4): left = [BFLOAT16_ONE | BFLOAT16_SIGN, 0, (127 - 12) << BFLOAT16_FRACTION_BITS, 0] * 2 right = [BFLOAT16_ONE, 0, (127 - 13) << BFLOAT16_FRACTION_BITS, 0] * 2 append(function, 'matrix_stage_order', [s['one_positive']] * 4, left, right, ah | mode << NUMERICAL_ROUND_SHIFT) for control in BFLOAT16_CONTROLS: for label in ('tie_odd', 'single_subnormal', 'negative_single_subnormal', 'single_denormal_nan', 'single_denormal_invalid', 'qnan_invalid_product', 'left_minsubnormal_positive', 'right_minsubnormal_negative', 'left_snan_positive', 'right_snan_negative', 'addend_qnan_positive', 'addend_snan_negative'): repeated(function, 'control_' + label, catalog[label], control) if op['kind'] == 'multiply': for variant in range(2): choices = ([s['one_positive'], s['qnan_positive'] + variant * 3, s['snan_negative'] + variant * 7], [b['one_positive'], b['qnan_negative'] + variant * 3, b['snan_positive'] + variant * 5], [b['one_positive'], b['qnan_positive'] + variant * 7, b['snan_negative'] + variant * 3]) for a, addend in enumerate(choices[0]): for n, left in enumerate(choices[1]): for m, right in enumerate(choices[2]): if a or n or m: for control in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): repeated(function, f'nan_matrix_{variant}_{a}_{n}_{m}', (addend, left, right), control) elif op['kind'] == 'dot': a_values = [s['one_positive'], s['qnan_negative'], s['snan_positive']] bf_values = [b['one_positive'], b['qnan_positive'], b['snan_negative']] for a, addend in enumerate(a_values): for code in range(81): indices = [(code // (3 ** position)) % 3 for position in range(4)] if not a and not any(indices): continue left, right = [bf_values[index] for index in indices[:2]], [bf_values[index] for index in indices[2:]] for control in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): append(function, f'dot_nan_{a}_{code}', [addend] * count, left * count, right if op['index'] is not None else right * count, control) else: for bank, positions in (('addend', 4), ('left', 8), ('right', 8)): for position in range(positions): for label in ('qnan_positive', 'qnan_negative', 'snan_positive', 'snan_negative'): for control in (0, NUMERICAL_AH, NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_DN): addends, left, right = [s['one_positive']] * 4, [b['one_positive']] * 8, [b['one_positive']] * 8 {'addend': addends, 'left': left, 'right': right}[bank][position] = s[label] if bank == 'addend' else b[label] append(function, f'matrix_nan_{bank}_{position}_{label}', addends, left, right, control) for n in range(8): for m in range(8): for ah in (0, NUMERICAL_AH): for mode in range(4): left, right = [0] * 8, [0] * 8 left[n], right[m] = b['one_positive'], b['one_positive'] append(function, f'matrix_basis_{n}_{m}', [0] * 4, left, right, ah | mode << NUMERICAL_ROUND_SHIFT) for sticky in (1, 1 << 4, 1 << 27, FP_REGISTER_MASKS['fpsr']): for ah in (0, NUMERICAL_AH): for label in ('one_one_one', 'tie_odd', 'left_snan_positive', 'single_subnormal'): repeated(function, 'sticky_' + label, catalog[label], ah, sticky) for control in (0, NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_FZ): for label in ('tie_odd', 'left_snan_positive', 'single_subnormal', 'left_minsubnormal_positive'): for position in range(count): addends = [s['one_positive']] * count left = [b['one_positive']] * op['source_lane_count'] right_count = (2 if op['kind'] == 'dot' else 1) if op['index'] is not None else op['source_lane_count'] right = [b['one_positive']] * right_count addends[position] = catalog[label][0] left[min(position, len(left) - 1)] = catalog[label][1] append(function, f'mixed_{label}_{position}', addends, left, right, control) for mode in range(4): for iteration in range(8): right_count = (2 if op['kind'] == 'dot' else 1) if op['index'] is not None else op['source_lane_count'] append(function, f'bounded_random_{iteration}', [rng.getrandbits(32) for _ in range(count)], [rng.getrandbits(16) for _ in range(op['source_lane_count'])], [rng.getrandbits(16) for _ in range(right_count)], mode << NUMERICAL_ROUND_SHIFT) repeated(function, 'helper_quiet_nan', (0x7fc00000, b['one_positive'], b['one_positive'])) for ah in (0, NUMERICAL_AH): for enables in (*(1 << bit for bit in (8, 9, 10, 11, 12, 15)), NUMERICAL_TRAP_ENABLE_MASK): if op['kind'] == 'conversion': append(function, 'retained_enable', [s['snan_negative']] * count, control=ah | enables) else: repeated(function, 'retained_enable', catalog['left_snan_positive'], ah | enables) base_roles = (op['d'], op['n']) == (0, 1) if op['kind'] == 'conversion' else (op['d'], op['n'], op['m']) == (0, 1, 2) for mode in range(4): if op['kind'] == 'conversion': append(function, 'layout_witness', [conversion_exact_positive_bits(4, position + 1, 0) for position in range(count)], control=mode << NUMERICAL_ROUND_SHIFT) else: right_count = (2 if op['kind'] == 'dot' else 1) if op['index'] is not None else op['source_lane_count'] append(function, 'layout_witness', [conversion_exact_positive_bits(4, position + 1, 0) for position in range(count)], [BFLOAT16_ONE + position for position in range(op['source_lane_count'])], [BFLOAT16_ONE + position + 9 for position in range(right_count)], mode << NUMERICAL_ROUND_SHIFT) if not base_roles: for control in (0, NUMERICAL_AH, NUMERICAL_NEP, NUMERICAL_FZ): if op['kind'] == 'conversion': for label in ('one_positive', 'qnan_negative', 'snan_positive', 'minsubnormal_positive'): append(function, 'alias_' + label, [s[label]] * count, control=control) else: for label in ('one_one_one', 'cancel_negative', 'left_snan_positive', 'single_subnormal'): repeated(function, 'alias_' + label, catalog[label], control) if op['index'] not in (None, 0): for control in (0, NUMERICAL_AH, NUMERICAL_NEP): for label in ('one_one_one', 'cancel_negative', 'left_snan_positive', 'single_subnormal'): repeated(function, 'indexed_' + label, catalog[label], control) if base_roles: for ah in (0, NUMERICAL_AH): for mode in range(4): control = BFLOAT16_EBF | ah | mode << NUMERICAL_ROUND_SHIFT if op['kind'] == 'conversion': append(function, 'raw_ebf_import', [s['one_positive'] + 1] * count, control=control) elif op['kind'] == 'multiply': repeated(function, 'raw_ebf_import', catalog['single_round_residual'], control) else: addends = [s['one_negative']] * count left = [BFLOAT16_ONE, (127 - 12) << BFLOAT16_FRACTION_BITS] * (op['source_lane_count'] // 2) right = [BFLOAT16_ONE, (127 - 13) << BFLOAT16_FRACTION_BITS] append(function, 'raw_ebf_import', addends, left, right if op['index'] is not None else right * (op['source_lane_count'] // 2), control) return functions, records def bfloat16_runtime_cases(records): selected = {} important_controls = BFLOAT16_CONTROLS for record in records: op, label = record['operation'], record['label'] initial = State.from_buffer_copy(record['initial']) shape, control = bfloat16_shape(op), initial.fpcr base = (op['d'], op['n']) == (0, 1) if op['kind'] == 'conversion' else (op['d'], op['n'], op['m']) == (0, 1, 2) if control & NUMERICAL_TRAP_ENABLE_MASK: enables = control & NUMERICAL_TRAP_ENABLE_MASK selected.setdefault(('enable', op['canonical_id'] if enables == NUMERICAL_TRAP_ENABLE_MASK else op['kind'], enables, bool(control & NUMERICAL_AH)), record) elif label == 'layout_witness' and control == 0 and base: selected.setdefault(('layout', shape, op['index']), record) elif label == 'value_bf_round_16256_32768_False': selected.setdefault(('conversion_round', shape, control), record) elif label in ('exact_cancel', 'fixed_odd_residual', 'matrix_stage_order', 'directed_cancel_negative'): selected.setdefault(('stage', op['kind'], op['name'], label, control), record) elif label == 'raw_ebf_import' and control in (BFLOAT16_EBF, BFLOAT16_EBF | NUMERICAL_AH): selected.setdefault(('raw_ebf', shape, control), record) elif label == ('control_minsubnormal_positive' if op['kind'] == 'conversion' else 'control_single_denormal_nan') and control in important_controls: selected.setdefault(('control', op['kind'], control), record) elif label in ('value_snan_negative', 'directed_left_snan_positive') and control in (0, NUMERICAL_AH): selected.setdefault(('snan', op['kind'], control), record) elif label.startswith('alias_') and control == NUMERICAL_NEP: selected.setdefault(('alias', op['kind'], op['d'], op['n'], op['m']), record) elif label.startswith('sticky_') and control == 0: selected.setdefault(('sticky', op['kind'], op['name']), record) elif label in ('matrix_basis_0_0', 'matrix_basis_2_2', 'matrix_basis_4_4', 'matrix_basis_6_6', 'matrix_basis_0_1') and control == 0: selected.setdefault(('basis', label), record) return list({record['case']: record for record in selected.values()}.values()) def bfloat16_helpers(records, case_ids=None): selected = {} for record in records: op, initial = record['operation'], State.from_buffer_copy(record['initial']) if (record['label'] != 'helper_quiet_nan' or initial.fpcr or initial.fpsr or case_ids is not None and record['case'] not in case_ids): continue selected.setdefault(bfloat16_shape(op), record) return list(selected.values()) def prepare_bfloat16_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *bfloat16_corpus(), BFLOAT16_NUMERICAL_FOCUS) def load_bfloat16_oracle(path=BFLOAT16_NUMERICAL_ORACLE): return load_full_frame_numerical_oracle(path, bfloat16_functions(), BFLOAT16_NUMERICAL_CASES, BFLOAT16_NUMERICAL_FOCUS) COMPLEX_NUMERICAL_FOCUS = 'complex_numerical' COMPLEX_NUMERICAL_CASES = 14919 COMPLEX_NUMERICAL_ORACLE = ROOT / '.stela/research/complex-fp-numerical/harness/oracle/receipt.json' COMPLEX_ADD_VECTOR = 0x2e00e400 COMPLEX_MLA_VECTOR = 0x2e00c400 COMPLEX_MLA_INDEXED = 0x2f001000 COMPLEX_ROTATIONS = (0, 90, 180, 270) COMPLEX_CONTROLS = (0, NUMERICAL_AH, NUMERICAL_FIZ, NUMERICAL_FZ, NUMERICAL_FZ16, NUMERICAL_DN, CONVERSION_AHP, NUMERICAL_NEP, NUMERICAL_AH | NUMERICAL_FIZ, NUMERICAL_AH | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_DN, NUMERICAL_AH | NUMERICAL_FZ16, NUMERICAL_FIZ | NUMERICAL_FZ, NUMERICAL_AH | NUMERICAL_FIZ | NUMERICAL_FZ) COMPLEX_RANDOM_SEED = 0x434f4d504c455846 def complex_operation(name, lane, width, rotation, d=0, n=1, m=2, index=None): if (name not in ('fcadd', 'fcmla') or (lane, width) not in VECTOR_NUMERICAL_SHAPES or any(not 0 <= register < 32 for register in (d, n, m)) or rotation not in ((90, 270) if name == 'fcadd' else COMPLEX_ROTATIONS)): raise ValueError('Invalid complex FP operation/shape/rotation/register') if index is not None and (name != 'fcmla' or lane == 8 or lane == 4 and width != 16 or not 0 <= index < width // (2 * lane)): raise ValueError('Invalid complex pair index') size = {2: 1, 4: 2, 8: 3}[lane] if name == 'fcadd': word = COMPLEX_ADD_VECTOR | int(rotation == 270) << 12 canonical = 'FCADD_asimdsame2_C' elif index is None: word = COMPLEX_MLA_VECTOR | (rotation // 90) << 11 canonical = 'FCMLA_asimdsame2_C' else: word = COMPLEX_MLA_INDEXED | (rotation // 90) << 13 word |= ((index & 1) << 21 | (index >> 1) << 11) if lane == 2 else index << 11 canonical = 'FCMLA_advsimd_elt' word |= int(width == 16) << 30 | size << 22 | m << 16 | n << 5 | d operation = {'name': name, 'kind': 'add' if name == 'fcadd' else 'multiply_add', 'lane': lane, 'bytes': width, 'scalar': False, 'd': d, 'n': n, 'm': m, 'index': index, 'rotation': rotation, 'selected_left_component': int(rotation in (90, 270)), 'complex_pair_count': width // (2 * lane), 'source_lane_count': width // lane, 'result_lane_count': width // lane, 'output_kind': 'vector', 'input_kind': 'vector' if index is None else 'indexed', 'merge': 'none', 'canonical_id': canonical, 'word': word, 'cohort_family': 'complex'} if name == 'fcmla': operation['a'] = d return operation def complex_functions(): layouts = [(name, lane, width, rotation, None) for name in ('fcadd', 'fcmla') for lane, width in VECTOR_NUMERICAL_SHAPES for rotation in ((90, 270) if name == 'fcadd' else COMPLEX_ROTATIONS)] layouts += [('fcmla', lane, width, rotation, index) for lane, width in ((2, 8), (2, 16), (4, 16)) for rotation in COMPLEX_ROTATIONS for index in range(width // (2 * lane))] return [complex_operation(name, lane, width, rotation, d, n, m, index) for name, lane, width, rotation, index in layouts for d, n, m in FUSED_ACCUMULATE_ROLES] def complex_shape(op): return op['name'], op['lane'], op['bytes'], op['index'] is not None def complex_state(op, case, pairs, control=0, sticky=0, poison_unselected=False): if len(pairs) != op['complex_pair_count'] or any(len(pair) != 6 for pair in pairs): raise ValueError('Complex state needs addend/N/M real-imaginary tuples') lane, count = op['lane'], op['source_lane_count'] initial = fused_numerical_state(dict(op, a=op['d'], index=None), case, [(pair[component], pair[2 + component], pair[4 + component]) for pair in pairs for component in range(2)], control) initial.fpsr = sticky values = dict(numerical_unary_values(lane)) bits, poison = lane * 8, values['snan_positive'] mask = (1 << bits) - 1 def store(register, positions, items): packed = sum(poison << (position * bits) for position in range(16 // lane)) for position, value in zip(positions, items): if not 0 <= value <= mask: raise ValueError('Complex source lane outside declared width') packed = packed & ~(mask << (position * bits)) | value << (position * bits) initial.v[register][:] = packed & MASK, packed >> 64 store(op['d'], range(count), [value for pair in pairs for value in pair[:2]]) left = [value for pair in pairs for value in pair[2:4]] if poison_unselected and op['name'] == 'fcmla': for position in range(count): if position % 2 != op['selected_left_component']: left[position] = poison store(op['n'], range(count), left) positions = range(count) if op['index'] is None else range(2 * op['index'], 2 * op['index'] + 2) right = [value for pair in pairs for value in pair[4:6]] if op['index'] is None else pairs[0][4:6] store(op['m'], positions, right) return initial def complex_inverse_rotation(rotation, real, imaginary, sign): """Prepare finite right inputs whose architectural rotation gives this pair.""" return ((real, imaginary) if rotation == 0 else (imaginary, real ^ sign) if rotation == 90 else (real ^ sign, imaginary ^ sign) if rotation == 180 else (imaginary ^ sign, real)) def complex_corpus(): functions, records = complex_functions(), [] rng = random.Random(COMPLEX_RANDOM_SEED) shapes = set() def append(function, label, pair, control=0, sticky=0, poison=False, asymmetric=False): op = functions[function] pairs = [pair] * op['complex_pair_count'] if asymmetric: values = dict(numerical_unary_values(op['lane'])) choices = (values['one_positive'], values['two_positive'], values['half_positive']) pairs = [tuple(choices[(position + lane) % 3] for lane in range(6)) for position in range(op['complex_pair_count'])] initial = complex_state(op, len(records), pairs, control, sticky, poison) records.append({'function': function, 'function_index': function, 'case': len(records), 'label': label, 'initial': bytes(initial), 'initial_state_sha256': digest(bytes(initial))}) for function, op in enumerate(functions): v = dict(numerical_unary_values(op['lane'])) one, zero, sign = v['one_positive'], v['zero_positive'], 1 << (op['lane'] * 8 - 1) normal = (one, v['two_positive'], one, v['half_positive'], one, v['two_positive']) for control in (NUMERICAL_NEP, NUMERICAL_AH): for label, pair in (('normal', normal), ('qnan', (v['qnan_positive'], v['qnan_negative'], *normal[2:])), ('subnormal', (one, one, 1, sign | 1, one, one))): append(function, 'alias_' + label, pair, control) base = (op['d'], op['n'], op['m']) == (0, 1, 2) if not base: continue for ah in (0, NUMERICAL_AH): for mode in range(4): append(function, 'layout_witness', normal, ah | mode << NUMERICAL_ROUND_SHIFT, asymmetric=True) for _ in range(8): pair = tuple(rng.getrandbits(op['lane'] * 8) for _ in range(6)) append(function, 'bounded_random', pair) append(function, 'mixed_component', (one, v['qnan_negative'], 1, one, v['snan_positive'], one)) append(function, 'mixed_infinity', (one, one, v['infinity_positive'], one, zero, v['minnormal_positive'])) if op['index'] not in (None, 0): continue finite_nan_classes = (one, v['qnan_positive'], v['snan_negative']) for ah in (0, NUMERICAL_AH): for dn in (0, NUMERICAL_DN): for left in finite_nan_classes: for right in finite_nan_classes: addends = finite_nan_classes if op['name'] == 'fcmla' else (one,) for addend in addends: append(function, 'nan_priority', (addend, addend, left, left, right, right), ah | dn) rotated_one = complex_inverse_rotation(op['rotation'], one, one, sign) rotated_product = complex_inverse_rotation(op['rotation'], one - 2, one - 2, sign) directed = [('normal', normal), ('exact_cancel', (one ^ sign, one ^ sign, one, one, *rotated_one)), ('fused_residual', (one ^ sign, one ^ sign, one + 1, one + 1, *rotated_product)), ('zero_product_qnan', (v['qnan_positive'], v['qnan_positive'], zero, zero, v['infinity_positive'], v['infinity_positive'])), ('minimum_subnormal', (one, one, 1, 1, one, one)), ('negative_subnormal', (one, one, sign | 1, sign | 1, one, one)), ('maximum', (v['maxfinite_positive'], v['maxfinite_positive'], v['maxfinite_positive'], one, one, v['two_positive'])), ('minimum_normal', (v['minnormal_positive'], v['minnormal_positive'], v['minnormal_positive'], one, one, v['half_positive'])), ('opposed_infinity', (v['infinity_positive'], v['infinity_negative'], v['infinity_positive'], v['infinity_negative'], v['infinity_negative'], v['infinity_positive'])), ('left_snan', (one, one, v['snan_negative'], v['snan_positive'], one, one)), ('right_qnan', (one, one, one, one, v['qnan_negative'], v['qnan_positive'])), ('addend_snan', (v['snan_negative'], v['snan_positive'], one, one, one, one)), ('left_qnan', (one, one, v['qnan_negative'], v['qnan_positive'], one, one)), ('right_snan', (one, one, one, one, v['snan_positive'], v['snan_negative']))] if op['name'] == 'fcadd': directed[1] = ('exact_cancel', (one, one, one ^ sign, one ^ sign, *rotated_one)) fraction = {2: 10, 4: 23, 8: 52}[op['lane']] bias = (1 << ({2: 5, 4: 8, 8: 11}[op['lane']] - 1)) - 1 half_ulp = (bias - fraction - 1) << fraction rotated_tie = complex_inverse_rotation(op['rotation'], half_ulp, half_ulp, sign) directed[2] = ('rounding_boundary', (one, one, one, one, *rotated_tie)) for label, pair in directed: for ah in (0, NUMERICAL_AH): for mode in range(4): append(function, 'directed_' + label, pair, ah | mode << NUMERICAL_ROUND_SHIFT) for control in COMPLEX_CONTROLS: for label, pair in (('subnormal', directed[4][1]), ('negative_subnormal', directed[5][1]), ('nan', directed[10][1]), ('minimum_normal', directed[7][1])): append(function, 'control_' + label, pair, control) for flag in (1, 2, 4, 8, 16, 128): append(function, 'sticky_status', normal, sticky=(1 << 27) | flag) for ah in (0, NUMERICAL_AH): for label, pair in (('normal', normal), ('zero', (zero,) * 6), ('subnormal', directed[4][1])): append(function, 'unselected_poison_' + label, pair, ah, poison=True) shape = complex_shape(op) if shape not in shapes: shapes.add(shape) quiet = v['qnan_positive'] helper = (quiet, quiet, one, one, one, one) if op['name'] == 'fcmla' else (one, one, quiet, quiet, one, one) append(function, 'helper_quiet_nan', helper) for ah in (0, NUMERICAL_AH): for enables in tuple(1 << bit for bit in (8, 9, 10, 11, 12, 15)) + (NUMERICAL_TRAP_ENABLE_MASK,): append(function, 'retained_enable_refusal', normal, ah | enables) return functions, records def complex_runtime_cases(records): selected = {} for record in records: op, initial, label = record['operation'], State.from_buffer_copy(record['initial']), record['label'] key = None if label == 'retained_enable_refusal': key = 'enable', op['canonical_id'], initial.fpcr elif label == 'layout_witness' and initial.fpcr == 0: key = 'layout', complex_shape(op), op['rotation'], op['index'] elif label == 'control_subnormal': key = 'control', op['name'], op['lane'], initial.fpcr elif label == 'alias_normal' and initial.fpcr == NUMERICAL_NEP: key = 'alias', op['name'], op['lane'], op['d'], op['n'], op['m'] elif label in ('directed_exact_cancel', 'directed_fused_residual', 'directed_rounding_boundary', 'directed_right_snan') and not initial.fpcr & NUMERICAL_AH: key = 'directed', op['name'], op['lane'], label, initial.fpcr elif label == 'sticky_status': key = 'sticky', op['name'], op['lane'] elif label == 'unselected_poison_normal': key = 'unselected', op['name'], op['lane'], initial.fpcr elif label == 'mixed_component': key = 'mixed', complex_shape(op) if key is not None: selected.setdefault(key, record) return list({record['case']: record for record in selected.values()}.values()) def complex_helpers(records, case_ids=None): return [record for record in records if record['label'] == 'helper_quiet_nan' and (case_ids is None or record['case'] in case_ids)] def complex_native_fma_expectation(op, target_id): if op['name'] != 'fcmla' or op['lane'] == 2 or 'fma' not in target_id.split('+')[1:]: return {} mnemonic = 'vfnmadd' if op['rotation'] == 180 else 'vfmadd' return {mnemonic + '132p' + ('s' if op['lane'] == 4 else 'd'): 1} def prepare_complex_oracle(directory): return prepare_full_frame_numerical_oracle(directory, *complex_corpus(), COMPLEX_NUMERICAL_FOCUS) def load_complex_oracle(path=COMPLEX_NUMERICAL_ORACLE): return load_full_frame_numerical_oracle(path, complex_functions(), COMPLEX_NUMERICAL_CASES, COMPLEX_NUMERICAL_FOCUS) FULL_FRAME_NUMERICAL_COHORTS = { COMPLEX_NUMERICAL_FOCUS: {'loader': load_complex_oracle, 'runtime_cases': complex_runtime_cases, 'supplementary_original_available': False, 'supplementary_original_evidence': {'path': '.stela/evidence/unicorn-review/ade2714deaff2e35245ecaddf0ae22c3d62d5dd385cdeb3b60b279f2bddfaa60.json', 'sha256': 'ade2714deaff2e35245ecaddf0ae22c3d62d5dd385cdeb3b60b279f2bddfaa60'}, 'helpers': complex_helpers, 'forms': 3, 'actual_helpers': True, 'status_merge_source': 'd', 'native_fma_expectation': complex_native_fma_expectation, 'limits': 'Enabled nonstreaming H/S/D FCADD and register/indexed FCMLA with FP16/AFP/FCMA only. All retained enables precisely refuse under AH0/1; QEMU RAZ/WI observations receive no successful equality credit. Full original snapshots retain rotations, pair selection, NaNs, inactive state and fresh/sticky status. Unicorn executes FCMA but has extra-IOC defects for Q0 indexed Half; its 5,941 matching observations and 418 differences remain separately retained, with zero supplementary raw/joined equality credit. Strict runtime/cache comparisons retain primary single-instruction expectations. No disabled-feature/access, enabled trap delivery, Apple hardware, application, OS or performance claim.'}, BFLOAT16_NUMERICAL_FOCUS: {'loader': load_bfloat16_oracle, 'runtime_cases': bfloat16_runtime_cases, 'supplementary_original_available': False, 'supplementary_original_evidence': {'path': '.stela/evidence/unicorn-review/838e4d9f084c88f790eacdb952da88255b1d389e3c10e1c91afe381f9f8723b3.json', 'sha256': '838e4d9f084c88f790eacdb952da88255b1d389e3c10e1c91afe381f9f8723b3'}, 'helpers': bfloat16_helpers, 'forms': 7, 'actual_helpers': True, 'status_merge_source': 'd', 'original_effects': validate_bfloat16_original_effects, 'accepts_enabled_control': bfloat16_accepts_enabled_control, 'original_control_policy': bfloat16_control_policy, 'profile_expectation': bfloat16_profile_expectation, 'native_fma_operations': {'bfmlalb': 'vfmadd', 'bfmlalt': 'vfmadd'}, 'native_fma_optional': True, 'limits': 'Enabled nonstreaming BF16 conversion/dot/MLA/matrix with EBF16 explicitly absent. Every raw EBF1 dot/matrix case, including coincident results, uses a separately counted closed integer standard-profile model after validating the unchanged enhanced original. Raw EBF1 conversion/MLA and accepted retained enables use separate requested-control policy comparisons. AH0 conversion/MLA enables precisely refuse. Raw originals remain immutable. Supplementary Unicorn cannot execute BF16, including joined helper/status sequences; strict runtime/cache comparisons retain the primary single-instruction expectations. All otherwise eligible states are separately counted unavailable with zero equality credit and a retained inventory. No enabled trap delivery, EBF16-enabled guest profile, Apple hardware, process/OS or performance claim.'}, WIDENING_NUMERICAL_FOCUS: {'loader': load_widening_oracle, 'runtime_cases': widening_runtime_cases, 'helpers': widening_helpers, 'forms': 8, 'actual_helpers': True, 'status_merge_source': 'd', 'native_fma_operations': {'fmlal': 'vfmadd', 'fmlal2': 'vfmadd', 'fmlsl': 'vfnmadd', 'fmlsl2': 'vfnmadd'}, 'native_fma_optional': True, 'limits': 'Enabled nonstreaming FHM Half multiplicands and Single accumulator/results only. All retained enables precisely refuse, including AH1. QEMU RAZ/WI observations receive no successful equality credit. Full original states and supplementary oracle limitations remain explicit. No trap delivery, disabled-feature, Apple hardware, application, OS or performance claim.'}, BINARY_REMAINING_FOCUS: {'loader': load_binary_remaining_oracle, 'runtime_cases': binary_remaining_runtime_cases, 'helpers': binary_remaining_helpers, 'forms': 23, 'actual_helpers': True, 'original_effects': validate_binary_remaining_original_effects, 'status_merge_source': 'n', 'limits': 'Enabled nonstreaming H/S/D scalar FNMUL, scalar/vector FABD/FMULX/FADDP and indexed FMUL/FMULX only. All retained enables precisely refuse, including AH1; QEMU RAZ/WI observations receive no successful equality credit. Full original states and supplementary oracle limitations remain explicit. No trap delivery, disabled-feature, Apple hardware, application, OS or performance claim.'}, VECTOR_NUMERICAL_FOCUS: {'loader': load_vector_numerical_oracle, 'runtime_cases': vector_numerical_runtime_cases, 'helpers': vector_numerical_helpers, 'forms': 8, 'limits': 'Enabled nonstreaming H/S/D vector arithmetic only. Full-frame original captures and retained-enable refusals remain separate; no enabled FP trap delivery, Apple hardware, application, OS or performance claim.'}, FUSED_NUMERICAL_FOCUS: {'loader': load_fused_numerical_oracle, 'runtime_cases': fused_numerical_runtime_cases, 'native_fma_operations': FUSED_NATIVE_OPERATIONS, 'helpers': fused_numerical_helpers, 'forms': 24, 'limits': 'Enabled nonstreaming scalar/vector/indexed H/S/D fused arithmetic only. Full-frame original observations compare under compatible controls; trap-delivery, disabled-feature, Apple hardware, complete application, OS and performance claims remain outside this cohort.'}, CONVERSION_NUMERICAL_FOCUS: {'loader': load_conversion_numerical_oracle, 'runtime_cases': conversion_numerical_runtime_cases, 'helpers': conversion_numerical_helpers, 'forms': 163, 'limits': 'Enabled nonstreaming classic precision/integer/fixed-point conversion and JSCVT profile only. Full-frame original captures and retained-enable refusals remain separate; no enabled FP trap delivery, Apple hardware, application, OS or performance claim.'}, COMPARISON_MINMAX_FOCUS: {'loader': load_comparison_minmax_oracle, 'runtime_cases': comparison_minmax_runtime_cases, 'accepts_enabled_control': comparison_minmax_enabled_control, 'actual_helpers': True, 'helpers': comparison_minmax_helpers, 'forms': 102, 'limits': 'Enabled nonstreaming scalar/AdvSIMD comparison and minmax only. Direct full-frame original equality excludes failed-FCCMP retained-enable control-policy successes. No enabled trap delivery, disabled FP access, Apple hardware, process, OS or performance claim.'}, ROUND_INTEGRAL_FOCUS: {'loader': load_round_integral_oracle, 'runtime_cases': round_integral_runtime_cases, 'actual_helpers': True, 'helpers': round_integral_helpers, 'forms': 47, 'limits': 'Enabled nonstreaming scalar/AdvSIMD H/S/D integral rounding, including bounded 32/64-bit rounding, only. Full-frame original captures and retained-enable refusals remain separate; no enabled FP trap delivery, disabled FP access, Apple hardware, application, OS or performance claim.'}, SQRT_NUMERICAL_FOCUS: {'loader': load_sqrt_numerical_oracle, 'runtime_cases': sqrt_numerical_runtime_cases, 'actual_helpers': True, 'helpers': sqrt_numerical_helpers, 'forms': 5, 'limits': 'Enabled nonstreaming scalar/AdvSIMD H/S/D square root only. Full-frame original captures and retained-enable refusals remain separate; no enabled FP trap delivery, disabled FP access, Apple hardware, application, OS or performance claim.'}, ESTIMATE_NUMERICAL_FOCUS: {'loader': load_estimate_oracle, 'runtime_cases': estimate_runtime_cases, 'helpers': estimate_refinement_helpers, 'forms': 12, 'actual_helpers': True, 'original_effects': validate_estimate_original_effects, 'accepts_enabled_control': numerical_suppressed_enables, 'profile_expectation': estimate_profile_expectation, 'status_merge_source': 'd', 'limits': 'Enabled nonstreaming standard RPRES-absent H/S/D estimates/exponent and unsigned 32-bit estimates only. All AH1 Single FRECPE/FRSQRTE states are separately counted standard-profile model comparisons against preserved, independently validated enhanced QEMU observations, including coincident results. Accepted AH1/unsigned retained enables use separate requested-FPCR control-policy comparisons; AH0 floating enables precisely refuse. No enabled trap delivery, RPRES-enabled guest CPU, Apple hardware, process/OS or performance claim.'}, REFINEMENT_NUMERICAL_FOCUS: {'loader': load_refinement_oracle, 'runtime_cases': refinement_runtime_cases, 'native_fma_operations': {'frecps': 'vfnmadd', 'frsqrts': 'vfnmadd'}, 'helpers': estimate_refinement_helpers, 'forms': 8, 'actual_helpers': True, 'original_effects': validate_refinement_original_effects, 'accepts_enabled_control': numerical_suppressed_enables, 'status_merge_source': 'n', 'limits': 'Enabled nonstreaming H/S/D fused reciprocal and reciprocal-square-root refinements only. AH1 suppresses fresh status and accepts retained enables with separately counted requested-FPCR control-policy comparisons; AH0 enables precisely refuse. Original full frames and source/profile/control expectations remain distinct. No enabled trap delivery, Apple hardware, process/OS or performance claim.'}, } def vector_numerical_runtime_cohort(cli, directory, oracle, compiler): return full_frame_numerical_runtime_cohort(cli, directory, oracle, compiler) def full_frame_numerical_runtime_cohort(cli, directory, oracle, compiler, focus=VECTOR_NUMERICAL_FOCUS, cpu_target='baseline', target_id='x86_64-baseline'): fused = focus == FUSED_NUMERICAL_FOCUS conversion = focus == CONVERSION_NUMERICAL_FOCUS comparison_minmax = focus == COMPARISON_MINMAX_FOCUS family = FULL_FRAME_NUMERICAL_COHORTS[focus] unary = focus in (ROUND_INTEGRAL_FOCUS, SQRT_NUMERICAL_FOCUS) merge_source = family.get('status_merge_source') modern = conversion or comparison_minmax or unary or bool(merge_source) reuse_reference = comparison_minmax or unary or bool(merge_source) source = b''.join(struct.pack('> NUMERICAL_EXCEPTION_ENABLE_SHIFT) & NUMERICAL_EXCEPTION_FLAGS}: raise AssertionError('Vector requested trap enables were lost or refusal was imprecise') else: require(report, actual, State.from_buffer_copy(record['expected'])) if record.get('equality_basis') == 'standard_profile_model': counts['profile_model_success_checks'] += 1 elif record.get('control_policy_success'): counts['control_policy_success_checks'] += 1 helpers = (family['helpers'](oracle['records'], oracle.get('raw_helper_case_ids')) if family.get('actual_helpers') else family['helpers'](oracle['records'])) if not family.get('actual_helpers') and (not helpers or (not conversion and len(helpers) != (12 if fused else len(VECTOR_NUMERICAL_BASES) * len(VECTOR_NUMERICAL_SHAPES)))): raise AssertionError('Numerical helper boundary fixture omits an arrangement') for record in helpers: initial, expected = State.from_buffer_copy(record['initial']), State.from_buffer_copy(record['expected']) entry = BASE + record['function'] * 8 first, actual = invoke(source, initial, entry, budget=1) require(first, actual, expected, entry + 4, 'budget_exhausted') if first['outcome']['floating_point_helpers'] != 1: raise AssertionError('Vector budget fixture did not complete exactly one helper') final, actual = invoke(source, actual, entry + 4, budget=1) require(final, actual, expected) counts['budget_helper_resume_checks'] += 1 warm, actual = invoke(source, initial, entry) require(warm, actual, expected) if warm['outcome']['compiled_blocks'] != 0 or warm['outcome']['disk_cache_hits'] < 2: raise AssertionError('Vector helper/RET did not reuse persistent cache') counts['disk_cache_checks'] += 1 operation = record['operation'] loop = struct.pack('<4I', operation['word'], NUMERICAL_SUBS_X10_ONE, NUMERICAL_CBNZ_X10_BACK_TWO, RET) loop_initial, loop_expected = State.from_buffer_copy(initial), State.from_buffer_copy(expected) loop_initial.x[10] = NUMERICAL_CACHE_LOOP_ITERATIONS loop_expected.x[10], loop_expected.nzcv = 0, NUMERICAL_ZERO_CARRY_FLAGS repeated, actual = invoke(loop, loop_initial, BASE, budget=16) require(repeated, actual, loop_expected) if repeated['outcome']['floating_point_helpers'] != NUMERICAL_CACHE_LOOP_ITERATIONS or repeated['outcome']['memory_cache_hits'] < 2: raise AssertionError('Vector loop did not repeat helper and live cache effects') check_original(loop, loop_initial, loop_expected) counts['memory_cache_loop_checks'] += 1 for control in ((NUMERICAL_NEP, 0) if modern else (NUMERICAL_NEP, VECTOR_NUMERICAL_ROUND_ZERO)): reference = record if reuse_reference else next(candidate for candidate in oracle['records'] if candidate['function'] == record['function'] and candidate['label'] == record['label'] and State.from_buffer_copy(candidate['initial']).fpcr == control) chain_initial = State.from_buffer_copy(reference['initial']) control_register, status_register, clear_register = (20, 21, 22) if modern else (0, 3, 4) chain_initial.x[control_register] = control chain_initial.fpcr, chain_initial.fpsr = 0, FP_REGISTER_MASKS['fpsr'] chain_expected = State.from_buffer_copy(reference['expected']) if modern: chain_expected.x[control_register] = control if reuse_reference: chain_expected.fpcr = control if (unary or merge_source) and operation['scalar'] and operation['merge'] != 'none' and control & NUMERICAL_NEP: bits = operation['lane'] * 8 active_mask = (1 << bits) - 1 merge_register = operation[merge_source] if merge_source else operation['d'] merged = chain_initial.v[merge_register][0] | chain_initial.v[merge_register][1] << 64 result = chain_expected.v[operation['d']][0] & active_mask | merged & ~active_mask chain_expected.v[operation['d']][:] = [result & MASK, result >> 64] elif operation['output_kind'] == 'vector' and operation['scalar'] and operation['merge'] != 'none' and control & NUMERICAL_NEP: bits = operation['lane'] * 8 active_mask = (1 << bits) - 1 merge_register = operation['m'] if operation['merge'] == 'right_if_nep' else operation['n'] merged = chain_initial.v[merge_register][0] | chain_initial.v[merge_register][1] << 64 result = chain_expected.v[operation['d']][0] & active_mask | merged & ~active_mask chain_expected.v[operation['d']][:] = [result & MASK, result >> 64] else: chain_expected.x[:] = chain_initial.x chain_expected.x[status_register], chain_expected.x[clear_register], chain_expected.fpsr = chain_expected.fpsr, 0, 0 chain = struct.pack('<7I', fp_register_operation('msr_fpcr', control_register)['word'], fp_register_operation('msr_fpsr', 31)['word'], operation['word'], fp_register_operation('mrs_fpsr', status_register)['word'], fp_register_operation('msr_fpsr', 31)['word'], fp_register_operation('mrs_fpsr', clear_register)['word'], numerical_return_word(operation)) joined, actual = invoke(chain, chain_initial, BASE) require(joined, actual, chain_expected) if control & 7 == 0: check_original(chain, chain_initial, chain_expected) counts['control_status_chain_checks'] += 1 if fused: record = next(record for record in oracle['records'] if record['operation']['name'] == 'fmadd' and record['operation']['lane'] == 4 and record['label'] == 'single_round_residual' and State.from_buffer_copy(record['initial']).fpcr == 0) operation = record['operation'] chain = struct.pack('<4I', NUMERICAL_ADD_X7_ONE, operation['word'], NUMERICAL_ADD_X8_TWO, RET) initial = State.from_buffer_copy(record['initial']) expected = State.from_buffer_copy(record['expected']) expected.x[7], expected.x[8] = (expected.x[7] + 1) & MASK, (expected.x[8] + 2) & MASK report, actual = invoke(chain, initial, BASE) require(report, actual, expected) check_original(chain, initial, expected) counts['interior_fused_prefix_suffix_checks'] += 1 initial.fpcr = NUMERICAL_INVALID_ENABLE expected = State.from_buffer_copy(initial) expected.x[7] = (expected.x[7] + 1) & MASK report, actual = invoke(chain, initial, BASE) require(report, actual, expected, BASE + 4, 'floating_point_unsupported') if report['outcome']['reason'] != {'kind': 'floating_point_unsupported', 'instruction_pc': BASE + 4, 'fpcr': NUMERICAL_INVALID_ENABLE, 'enabled_exceptions': 1}: raise AssertionError('Interior fused refusal did not preserve exact pending instruction and enable') counts['interior_fused_precise_refusal_checks'] += 1 transcript_name = f'{focus}-{target_id}' if fused or modern else focus for name in ('budget_helper_resume_checks', 'disk_cache_checks', 'memory_cache_loop_checks', 'control_status_chain_checks', 'control_policy_success_checks', 'profile_model_success_checks'): counts.setdefault(name, 0) counts.setdefault('original_joined_unavailable_cases', 0) snapshot = write_evidence(ROOT / f'.stela/simd-{transcript_name}-runtime-records.json', transcripts) return {**dict(counts), 'ram_bytes_per_invocation': len(ram), 'transcript': {'path': str(snapshot.relative_to(ROOT)), 'sha256': digest(snapshot.read_bytes())}} def run_fused_numerical_cohort(cli, oracle_path): return run_full_frame_numerical_cohort(cli, oracle_path, FUSED_NUMERICAL_FOCUS) def run_conversion_numerical_cohort(cli, oracle_path): return run_full_frame_numerical_cohort(cli, oracle_path, CONVERSION_NUMERICAL_FOCUS) def numerical_fma_expectation(operation, target_id, operations): if not operations or operation['name'] not in operations or 'fma' not in target_id.split('+')[1:] or operation['lane'] == 2: return {} suffix = ('s' if operation['scalar'] else 'p') + ('s' if operation['lane'] == 4 else 'd') return {operations[operation['name']] + '132' + suffix: 1} def numerical_fma_accounting_matches(operation, target_id, mnemonics, family): expected = (family['native_fma_expectation'](operation, target_id) if family.get('native_fma_expectation') else numerical_fma_expectation(operation, target_id, family.get('native_fma_operations'))) return mnemonics == expected or bool(family.get('native_fma_optional') and not mnemonics) def run_full_frame_numerical_cohort(cli, oracle_path, focus): fused = focus == FUSED_NUMERICAL_FOCUS family = FULL_FRAME_NUMERICAL_COHORTS[focus] oracle = family['loader'](oracle_path) supplementary = family.get('supplementary_original_evidence') if supplementary: numerical_json(ROOT / supplementary['path'], supplementary['sha256']) started = time.monotonic() cli_hash, harness_hash = digest(cli.read_bytes()), digest(Path(__file__).read_bytes()) shared_paths = ('dev/differential.py', 'dev/evidence.py', 'dev/test_simd_differential.py', 'crates/stela-runtime/src/floating.rs', 'crates/stela-runtime/src/floating/comparison.rs', 'crates/stela-runtime/src/floating/minmax.rs', 'crates/stela-runtime/src/floating/rounding.rs', 'crates/stela-runtime/src/floating/sqrt.rs', 'crates/stela-runtime/src/floating/estimate.rs', 'crates/stela-runtime/src/floating/refinement.rs', 'crates/stela-runtime/src/floating/arithmetic.rs', 'crates/stela-runtime/src/floating/widening.rs', 'crates/stela-runtime/src/floating/bfloat16.rs', 'crates/stela-runtime/src/floating/complex.rs', 'crates/stela-runtime/src/blocks.rs', 'crates/stela-runtime/src/lib.rs', 'crates/stela-runtime/src/execution.rs') shared_hashes = {name: digest((ROOT / name).read_bytes()) for name in shared_paths} cpu_info = json.loads(subprocess.check_output([str(cli), 'cpu-info'])) targets = [('baseline', 'x86_64-baseline')] if cpu_info['detected_target'] != 'x86_64-baseline': targets.append(('native', cpu_info['detected_target'])) profiles, compiler = [], None with tempfile.TemporaryDirectory(prefix=f'stela-{focus}-') as temporary: for cpu_target, target_id in targets: directory = Path(temporary) / target_id directory.mkdir() batches, artifacts = [], [] for index, operation in enumerate(oracle['functions']): batch = compile_batch(cli, directory, [{'operations': [operation]}], index, block=True, cpu_target=cpu_target, target_id=target_id, guest_base=BASE + index * 8) batch.update(cohort=focus, oracle_path=str(oracle_path)) if compiler is not None and batch['compiler'] != compiler: raise AssertionError('Fused compiler changed during preparation') compiler = batch['compiler'] accounting = numerical_native_accounting(batch) if fused and operation['lane'] == 2 and (len(batch['compiled_source']) != 4 or len(batch['native']) != NUMERICAL_HELPER_NATIVE_BYTES): raise AssertionError('Fused half instruction is not a terminal helper') if fused and operation['lane'] != 2 and len(batch['compiled_source']) != 8: raise AssertionError('Fused S/D instruction does not retain conditional fast path and guest RET') retained = retain_native_evidence(f'simd-{focus}', batch['compiled_source'], batch['native'], batch['manifest'], accounting, Path(__file__).read_bytes()) batches.append(batch) artifacts.append({'function': index, 'compilation': batch['receipt'], 'accounting': accounting, 'files': retained}) optional = {opcode['mnemonic'] for opcode in cpu_info['simd_opcodes'] if opcode['minimum_target'] != 'x86_64-baseline'} if cpu_target == 'baseline' and any(optional & artifact['accounting']['static_opcode_histogram'].keys() for artifact in artifacts): raise AssertionError('Baseline fused code contains optional host opcodes') for artifact in artifacts: operation = artifact['accounting']['shape'] histogram = artifact['accounting']['static_opcode_histogram'] fused_mnemonics = {mnemonic: count for mnemonic, count in histogram.items() if mnemonic.startswith(FUSED_NATIVE_PREFIXES)} if not numerical_fma_accounting_matches(operation, target_id, fused_mnemonics, family): raise AssertionError(f'FMA3 opcode/feature guard failed: {target_id} {operation} {fused_mnemonics}') raw = bounded_cases(batches) raw['limits'] = 'Original QEMU captures every architectural register before transport clobbers. Raw helper exits verify exact PC and unchanged full state; actual Rust runtime completion is separate. Requested enables remain intact in native inputs. QEMU RAZ/WI observations receive no direct-original equality credit; accepted control-policy and standard-profile model successes are counted separately.' if family.get('actual_helpers'): oracle['raw_helper_case_ids'] = set(raw['raw_helper_case_ids']) runtime = full_frame_numerical_runtime_cohort(cli, directory, oracle, compiler, focus, cpu_target, target_id) profiles.append({'target_cpu': target_id, 'raw_native': raw, 'rust_runtime': runtime, ('fma3_instruction_checks' if fused else 'feature_emission_checks'): len(artifacts), 'native_artifacts': artifacts}) if (digest(cli.read_bytes()) != cli_hash or digest(Path(__file__).read_bytes()) != harness_hash or any(digest((ROOT / name).read_bytes()) != value for name, value in shared_hashes.items())): raise AssertionError('Fused executable, harness or shared source changed during verification') def sums(section, key): return sum(profile[section][key] for profile in profiles) report = {'kind': f'simd_{focus}_arm_vs_native_x86', 'passed': True, 'abi': BLOCK_ABI, 'state_comparisons': sums('rust_runtime', 'complete_state_comparisons'), 'runtime_complete_state_comparisons': sums('rust_runtime', 'complete_state_comparisons'), 'runtime_typed_unsupported_state_checks': sums('rust_runtime', 'typed_unsupported_state_checks'), 'runtime_budget_resume_checks': sums('rust_runtime', 'budget_helper_resume_checks'), 'runtime_disk_cache_checks': sums('rust_runtime', 'disk_cache_checks'), 'runtime_memory_cache_loop_checks': sums('rust_runtime', 'memory_cache_loop_checks'), 'runtime_control_status_chain_checks': sums('rust_runtime', 'control_status_chain_checks'), 'raw_native_state_and_exit_comparisons': sums('raw_native', 'raw_native_state_and_exit_comparisons'), 'raw_original_complete_state_comparisons': sums('raw_native', 'raw_inline_original_complete_state_comparisons'), 'raw_inline_complete_state_comparisons': sums('raw_native', 'raw_inline_complete_state_comparisons'), 'raw_helper_unchanged_state_comparisons': sums('raw_native', 'raw_helper_unchanged_state_comparisons'), 'raw_unsupported_unchanged_state_comparisons': sums('raw_native', 'raw_unsupported_unchanged_state_comparisons'), 'original_full_frame_captures': len(oracle['records']), 'canonical_forms': family['forms'], 'original_reference_classifications': dict(Counter('precise_refusal' if record['unsupported'] else record.get('equality_basis', 'direct_original') for record in oracle['records'])), 'native_mxcsr_preservation_comparisons': sums('raw_native', 'caller_mxcsr_preservation_comparisons'), 'compiler_source_sha256': compiler, 'compiler_executable_sha256': cli_hash, 'harness_source_sha256': harness_hash, 'shared_sources_sha256': shared_hashes, 'original_oracle_receipt_sha256': oracle['receipt_sha256'], 'original_producer': oracle['producer'], 'control_readbacks': oracle['control_readbacks'], 'input_derivation': oracle['input_derivation'], 'profiles': profiles, 'caller_mxcsr_wrapper_sha256': digest(NUMERICAL_MXCSR_WRAPPER), 'verification_wall_seconds': time.monotonic() - started, 'limits': family['limits']} if supplementary: report['supplementary_original_evidence'] = supplementary report['unicorn_original_unavailable_cases'] = sums('raw_native', 'unicorn_original_unavailable_cases') report['runtime_joined_original_unavailable_cases'] = sums('rust_runtime', 'original_joined_unavailable_cases') if family.get('accepts_enabled_control'): report['runtime_control_policy_success_checks'] = sums('rust_runtime', 'control_policy_success_checks') report['raw_inline_control_policy_comparisons'] = sums('raw_native', 'raw_inline_control_policy_comparisons') report['runtime_profile_model_success_checks'] = sums('rust_runtime', 'profile_model_success_checks') report['raw_inline_profile_model_comparisons'] = sums('raw_native', 'raw_inline_profile_model_comparisons') write_evidence(ROOT / f'.stela/simd-{focus}-differential-report.json', report) print(json.dumps(report, indent=2)) def run_vector_numerical_cohort(cli, oracle_path): oracle = load_vector_numerical_oracle(oracle_path) started = time.monotonic() cli_hash, harness_hash = digest(cli.read_bytes()), digest(Path(__file__).read_bytes()) shared_paths = ('dev/differential.py', 'dev/evidence.py', 'dev/test_simd_differential.py', 'crates/stela-runtime/src/floating.rs', 'crates/stela-runtime/src/floating/comparison.rs', 'crates/stela-runtime/src/floating/minmax.rs', 'crates/stela-runtime/src/floating/rounding.rs', 'crates/stela-runtime/src/floating/sqrt.rs', 'crates/stela-runtime/src/floating/estimate.rs', 'crates/stela-runtime/src/floating/refinement.rs', 'crates/stela-runtime/src/floating/arithmetic.rs', 'crates/stela-runtime/src/floating/widening.rs', 'crates/stela-runtime/src/floating/bfloat16.rs', 'crates/stela-runtime/src/floating/complex.rs', 'crates/stela-runtime/src/blocks.rs', 'crates/stela-runtime/src/lib.rs', 'crates/stela-runtime/src/execution.rs') shared_hashes = {name: digest((ROOT / name).read_bytes()) for name in shared_paths} batches, artifacts, checked, compiler = [], [], [], None cpu_info = json.loads(subprocess.check_output([str(cli), 'cpu-info'])) with tempfile.TemporaryDirectory(prefix='stela-vector-numerical-') as temporary: directory = Path(temporary) for index, operation in enumerate(oracle['functions']): batch = compile_batch(cli, directory, [{'operations': [operation]}], index, block=True, guest_base=BASE + index * 8) batch.update(cohort=VECTOR_NUMERICAL_FOCUS, oracle_path=str(oracle_path)) if compiler is not None and batch['compiler'] != compiler: raise AssertionError('Vector compiler changed during preparation') compiler = batch['compiler'] accounting = numerical_native_accounting(batch) retained = retain_native_evidence('simd-vector_numerical', batch['compiled_source'], batch['native'], batch['manifest'], accounting, Path(__file__).read_bytes()) batches.append(batch) artifacts.append({'function': index, 'compilation': batch['receipt'], 'accounting': accounting, 'files': retained}) optional = {opcode['mnemonic'] for opcode in cpu_info['simd_opcodes'] if opcode['minimum_target'] != 'x86_64-baseline'} if any(optional & artifact['accounting']['static_opcode_histogram'].keys() for artifact in artifacts): raise AssertionError('Baseline vector numerical code contains optional host opcodes') if cpu_info['detected_target'] != 'x86_64-baseline': for index in (0, 14, 28): probe = compile_batch(cli, directory, [{'operations': [oracle['functions'][index]]}], 1000 + index, block=True, cpu_target='native', target_id=cpu_info['detected_target'], guest_base=BASE + index * 8) if probe['compiler'] != compiler or probe['native'] != batches[index]['native']: raise AssertionError('Checked vector numerical recipe differs from baseline') checked.append({'function': index, 'target_cpu': cpu_info['detected_target'], 'native_sha256': digest(probe['native']), 'baseline_bytes_identical': True}) raw = bounded_cases(batches) raw['limits'] = 'QEMU original captures all GPR/SP/V/NZCV/FPCR/FPSR before transport clobbers. Reserved GuestState bytes are metadata canaries. Raw helper exits verify unchanged state and exact instruction PC; actual Rust runtime separately completes helpers. Requested controls remain unchanged; QEMU trap-enable RAZ/WI is excluded from successful native equality.' runtime = vector_numerical_runtime_cohort(cli, directory, oracle, compiler) if (digest(cli.read_bytes()) != cli_hash or digest(Path(__file__).read_bytes()) != harness_hash or any(digest((ROOT / name).read_bytes()) != value for name, value in shared_hashes.items())): raise AssertionError('Vector executable, harness or shared source changed during verification') report = {'kind': 'simd_vector_numerical_arm_vs_native_x86', 'passed': True, 'abi': BLOCK_ABI, 'state_comparisons': runtime['complete_state_comparisons'], 'runtime_complete_state_comparisons': runtime['complete_state_comparisons'], 'runtime_typed_unsupported_state_checks': runtime['typed_unsupported_state_checks'], 'runtime_budget_resume_checks': runtime['budget_helper_resume_checks'], 'runtime_disk_cache_checks': runtime['disk_cache_checks'], 'runtime_memory_cache_loop_checks': runtime['memory_cache_loop_checks'], 'runtime_control_status_chain_checks': runtime['control_status_chain_checks'], 'raw_native_state_and_exit_comparisons': raw['raw_native_state_and_exit_comparisons'], 'raw_original_complete_state_comparisons': raw['raw_inline_complete_state_comparisons'], 'raw_inline_complete_state_comparisons': raw['raw_inline_complete_state_comparisons'], 'raw_helper_unchanged_state_comparisons': raw['raw_helper_unchanged_state_comparisons'], 'raw_unsupported_unchanged_state_comparisons': raw['raw_unsupported_unchanged_state_comparisons'], 'original_full_frame_captures': len(oracle['records']), 'native_mxcsr_preservation_comparisons': raw['caller_mxcsr_preservation_comparisons'], 'compiler_source_sha256': compiler, 'compiler_executable_sha256': cli_hash, 'harness_source_sha256': harness_hash, 'shared_sources_sha256': shared_hashes, 'original_oracle_receipt_sha256': oracle['receipt_sha256'], 'original_producer': oracle['producer'], 'control_readbacks': oracle['control_readbacks'], 'input_derivation': oracle['input_derivation'], 'raw_native': raw, 'rust_runtime': runtime, 'native_artifacts': artifacts, 'checked_host_emission_identity': checked, 'caller_mxcsr_wrapper_sha256': digest(NUMERICAL_MXCSR_WRAPPER), 'verification_wall_seconds': time.monotonic() - started, 'limits': 'Enabled nonstreaming AdvSIMD H/S/D binary arithmetic only. Full-frame unchanged original instruction captures receive direct equality credit only under actual compatible controls. No numerical trap delivery, disabled-feature, Apple hardware, complete application, OS or performance claim.'} write_evidence(ROOT / '.stela/simd-vector_numerical-differential-report.json', report) print(json.dumps(report, indent=2)) def fp_register_runtime_cases(oracle): controls = (0, 1 << 22, 2 << 22, 3 << 22, NUMERICAL_NEP) helper_labels = {'fadd': 'one_opposite_one', 'fsub': 'one_one', 'fmul': 'minimum_normal_half', 'fdiv': 'denormal_zero_divisor'} selected = {} for record in oracle['records']: operation = record['operation'] initial = State.from_buffer_copy(record['initial']) if ((operation['d'], operation['n'], operation['m']) != (0, 1, 2) or initial.fpcr not in controls or initial.fpsr != 0 or record['label'] not in (helper_labels[operation['name']], 'one_positive_halfulp')): continue key = (operation['name'], operation['lane'], initial.fpcr, record['label']) selected.setdefault(key, record) if len(selected) != 4 * 3 * len(controls) * 2: raise AssertionError('FP register runtime selection omits a format, control or arithmetic boundary') return list(selected.values()), helper_labels def fp_register_chain(record): words = [fp_register_operation('msr_fpcr', 0)['word'], fp_register_operation('msr_fpsr', 31)['word'], record['operation']['word'], fp_register_operation('mrs_fpsr', 3)['word'], fp_register_operation('msr_fpsr', 31)['word'], fp_register_operation('mrs_fpsr', 4)['word'], record['operation']['word'], fp_register_operation('mrs_fpsr', 5)['word'], fp_register_operation('msr_fpsr', 31)['word'], fp_register_operation('mrs_fpsr', 6)['word'], RET] initial = State.from_buffer_copy(record['initial']) control = initial.fpcr initial.x[0] = 0xfeedface00000000 | control initial.fpcr = control ^ (3 << 22) initial.fpsr = FP_REGISTER_MASKS['fpsr'] intermediate = State.from_buffer_copy(record['expected']) intermediate.x[:] = initial.x intermediate.fpcr = control expected = State.from_buffer_copy(intermediate) expected.x[3] = expected.x[5] = expected.fpsr & FP_REGISTER_MASKS['fpsr'] expected.x[4] = expected.x[6] = expected.fpsr = 0 return struct.pack('<' + 'I' * len(words), *words), initial, intermediate, expected def fp_register_runtime_cohort(cli, directory, numerical, compiler): records, helper_labels = fp_register_runtime_cases(numerical) counts, transcripts = Counter(), [] cache = directory / 'fp-register-runtime-cache' source_path, state_path = directory / 'fp-register-chain.arm64', directory / 'fp-register-state.json' ram_address, ram = 0x240000000000, NUMERICAL_GUARD * 16 memory_path = directory / 'fp-register-memory.json' memory_path.write_text(json.dumps({'regions': [{'guest_address': ram_address, 'virtual_bytes': len(ram), 'initial_bytes': list(ram), 'readable': True, 'writable': True}]})) original = Uc(UC_ARCH_ARM64, UC_MODE_ARM) original.ctl_set_cpu_model(arm.UC_CPU_ARM64_MAX) base = BASE + 0x4000 original.mem_map(base, 4096) original.mem_map(STOP, 4096) def invoke(code, initial, entry=base, budget=16, cache_dir=cache): source_path.write_bytes(code) state_path.write_text(json.dumps(state_dict(initial))) report = json.loads(subprocess.check_output([str(cli), 'run-blocks', str(source_path), '--guest-base', hex(base), '--entry', hex(entry), '--stop-pc', hex(STOP), '--cpu-target', 'baseline', '--state', str(state_path), '--block-budget', str(budget), '--cache-dir', str(cache_dir), '--memory', str(memory_path), '--read-memory', f'{hex(ram_address)}:{len(ram)}'], timeout=30)) if (report['kind'] != 'native_block_run' or report['source_sha256'] != digest(code) or report['compiler_source_sha256'] != compiler or report['target_cpu'] != 'x86_64-baseline'): raise AssertionError('FP register runtime report source/compiler/target differs') memory = report['memory'] if memory['memory_before'] != memory['memory_after'] or memory['memory_reads'][0]['hex'] != ram.hex(): raise AssertionError('FP register runtime changed owned guest RAM') for name in ('compiled_blocks', 'disk_cache_hits', 'memory_cache_hits', 'floating_point_helpers'): value = report['outcome'][name] if not isinstance(value, int) or value < 0: raise AssertionError(f'FP register runtime omitted {name}') counts[name] += value transcripts.append({'source_hex': code.hex(), 'initial_state_hex': bytes(initial).hex(), 'entry': entry, 'budget': budget, 'report': report}) counts['invocations'] += 1 return report, numerical_report_state(report['state']) def require(report, actual, expected, pc=STOP, reason='stop_address'): if report['pc'] != pc or report['outcome']['reason']['kind'] != reason or bytes(actual) != bytes(expected): write_evidence(ROOT / '.stela/simd-fp_registers-runtime-failure.json', {'kind': 'fp_register_runtime_failure', 'transcripts': transcripts, 'expected_state_hex': bytes(expected).hex(), 'expected_pc': pc, 'expected_reason': reason}) raise AssertionError('FP register runtime complete-state mismatch; immutable failure retained') if reason == 'stop_address' and report['outcome']['last_exit'] != [STOP, NUMERICAL_RETURN_EXIT]: raise AssertionError('FP register runtime did not execute actual guest RET') counts['complete_state_comparisons'] += 1 def check_original(code, initial, expected): original.mem_write(base, code) original.ctl_remove_cache(base, base + len(code)) initialize_arm(original, initial) original.emu_start(base, STOP, count=128) if original.reg_read(arm.UC_ARM64_REG_PC) != STOP or bytes(capture_arm(original, initial)) != bytes(expected): raise AssertionError('FP register joined original complete state differs') counts['original_joined_complete_state_comparisons'] += 1 for record in records: code, initial, intermediate, expected = fp_register_chain(record) report, actual = invoke(code, initial) require(report, actual, expected) counts['status_clear_repeat_checks'] += 1 if State.from_buffer_copy(record['initial']).fpcr & 7 == 0: check_original(code, initial, expected) if record['label'] != helper_labels[record['operation']['name']]: continue if State.from_buffer_copy(record['initial']).fpcr != 0: continue first, actual = invoke(code, initial, budget=1, cache_dir=directory / f"fp-resume-{record['function']}") require(first, actual, intermediate, base + 12, 'budget_exhausted') if first['outcome']['floating_point_helpers'] != 1: raise AssertionError('FP register budget fixture did not cross the actual arithmetic helper') final, actual = invoke(code, intermediate, entry=base + 12) require(final, actual, expected) counts['budget_helper_resume_checks'] += 1 warm, actual = invoke(code, initial) require(warm, actual, expected) if warm['outcome']['compiled_blocks'] != 0 or warm['outcome']['disk_cache_hits'] < 2: raise AssertionError('FP register joined chain did not reuse its persistent cache') counts['disk_cache_checks'] += 1 loop_words = list(struct.unpack('<' + 'I' * (len(code) // 4), code))[:-1] loop_words += [NUMERICAL_SUBS_X10_ONE, 0xb5000000 | ((-(len(loop_words) + 1) & ((1 << 19) - 1)) << 5) | 10, RET] loop = struct.pack('<' + 'I' * len(loop_words), *loop_words) loop_initial = State.from_buffer_copy(initial) loop_initial.x[10] = NUMERICAL_CACHE_LOOP_ITERATIONS loop_expected = State.from_buffer_copy(expected) loop_expected.x[10], loop_expected.nzcv = 0, NUMERICAL_ZERO_CARRY_FLAGS repeated, actual = invoke(loop, loop_initial, budget=32) require(repeated, actual, loop_expected) if repeated['outcome']['memory_cache_hits'] < NUMERICAL_CACHE_LOOP_ITERATIONS - 1: raise AssertionError('FP register joined loop did not exercise live cache reuse') check_original(loop, loop_initial, loop_expected) counts['memory_cache_loop_checks'] += 1 for bit in (8, 9, 10, 11, 12, 15): trap_initial = State.from_buffer_copy(initial) trap_initial.x[0] |= 1 << bit trap_expected = State.from_buffer_copy(trap_initial) trap_expected.fpcr = trap_initial.x[0] & FP_REGISTER_MASKS['fpcr'] trap_expected.fpsr = 0 declined, actual = invoke(code, trap_initial) require(declined, actual, trap_expected, base + 8, 'floating_point_unsupported') if declined['outcome']['reason'] != {'kind': 'floating_point_unsupported', 'instruction_pc': base + 8, 'fpcr': trap_expected.fpcr, 'enabled_exceptions': 1 << (bit - NUMERICAL_EXCEPTION_ENABLE_SHIFT)}: raise AssertionError('MSR FPCR lost requested enable or imprecise arithmetic refusal') counts['typed_unsupported_state_checks'] += 1 snapshot = write_evidence(ROOT / '.stela/simd-fp_registers-runtime-records.json', transcripts) return {**dict(counts), 'ram_bytes_per_invocation': len(ram), 'transcript': {'path': str(snapshot.relative_to(ROOT)), 'sha256': digest(snapshot.read_bytes())}} def run_fp_register_cohort(cli, numerical_path): started = time.monotonic() oracle, numerical = load_fp_register_oracle(), load_numerical_oracle(numerical_path) cli_hash, harness_hash = digest(cli.read_bytes()), digest(Path(__file__).read_bytes()) shared_paths = ('dev/differential.py', 'dev/evidence.py', 'dev/test_simd_differential.py', 'crates/stela-runtime/src/floating.rs', 'crates/stela-runtime/src/floating/comparison.rs', 'crates/stela-runtime/src/floating/minmax.rs', 'crates/stela-runtime/src/floating/rounding.rs', 'crates/stela-runtime/src/floating/sqrt.rs', 'crates/stela-runtime/src/floating/estimate.rs', 'crates/stela-runtime/src/floating/refinement.rs', 'crates/stela-runtime/src/floating/arithmetic.rs', 'crates/stela-runtime/src/floating/widening.rs', 'crates/stela-runtime/src/floating/bfloat16.rs', 'crates/stela-runtime/src/floating/complex.rs', 'crates/stela-runtime/src/blocks.rs', 'crates/stela-runtime/src/lib.rs', 'crates/stela-runtime/src/execution.rs') shared_hashes = {name: digest((ROOT / name).read_bytes()) for name in shared_paths} with tempfile.TemporaryDirectory(prefix='stela-fp-registers-') as temporary: directory = Path(temporary) batch = compile_batch(cli, directory, fp_register_functions(), 0) batch['cohort'] = FP_REGISTER_FOCUS compiler = batch['compiler'] accounting = native_binary_accounting(batch) artifacts = retain_native_evidence('simd-fp_registers', batch['source'], batch['native'], batch['manifest'], accounting, Path(__file__).read_bytes()) raw = bounded_cases([batch]) runtime = fp_register_runtime_cohort(cli, directory, numerical, compiler) if (digest(cli.read_bytes()) != cli_hash or digest(Path(__file__).read_bytes()) != harness_hash or any(digest((ROOT / name).read_bytes()) != value for name, value in shared_hashes.items())): raise AssertionError('FP register executable, harness or shared source changed during verification') report = {'kind': 'simd_fp_registers_arm_vs_native_x86', 'passed': True, 'abi': LEAF_ABI, 'state_comparisons': raw['complete_state_comparisons'], 'compatible_original_native_full_state_comparisons': raw['direct_original_complete_state_comparisons'], 'native_policy_state_comparisons': raw['complete_state_comparisons'] - raw['direct_original_complete_state_comparisons'], 'policy_difference_classifications': len(oracle['classifications']), 'runtime_state_comparisons': runtime['complete_state_comparisons'] - runtime['typed_unsupported_state_checks'], 'runtime_complete_state_comparisons': runtime['complete_state_comparisons'] - runtime['typed_unsupported_state_checks'], 'runtime_typed_unsupported_state_checks': runtime['typed_unsupported_state_checks'], 'runtime_budget_resume_checks': runtime['budget_helper_resume_checks'], 'runtime_disk_cache_checks': runtime['disk_cache_checks'], 'runtime_memory_cache_loop_checks': runtime['memory_cache_loop_checks'], 'native_mxcsr_preservation_comparisons': raw['caller_mxcsr_preservation_comparisons'], 'native_caller_mxcsr_checks': raw['caller_mxcsr_preservation_comparisons'], 'compiler_source_sha256': compiler, 'compiler_executable_sha256': cli_hash, 'harness_source_sha256': harness_hash, 'shared_sources_sha256': shared_hashes, 'original_oracle': {key: value for key, value in oracle.items() if key != 'records'}, 'numerical_foundation_receipt_sha256': numerical['foundation_sha256'], 'policy_masks': FP_REGISTER_MASKS, 'operand_combinations': len(batch['functions']), 'raw_native': raw, 'rust_runtime': runtime, 'native_accounting': accounting, 'native_artifacts': artifacts, 'caller_mxcsr_wrapper_sha256': digest(NUMERICAL_MXCSR_WRAPPER), 'verification_wall_seconds': time.monotonic() - started, 'limits': 'Enabled AArch64 nonstreaming FP/FP16/AFP software policy only. Original EL0 inputs remain unchanged; exact QEMU RAZ/WI and AArch32/EBF differences receive separate policy credit. Disabled FP and absent FPMR traps are original-only evidence. Requested numerical enables precisely decline arithmetic; no exception delivery, generic MRS/MSR form completion, Apple hardware or application claim.'} write_evidence(ROOT / '.stela/simd-fp_registers-differential-report.json', report) print(json.dumps(report, indent=2)) def run_register_cohort(cli, focus): functions=REGISTER_COHORTS[focus][0]();started=time.monotonic() cli_hash,harness_hash=digest(cli.read_bytes()),digest(Path(__file__).read_bytes()) info=json.loads(subprocess.check_output([str(cli),'cpu-info'])) selections=[('baseline','x86_64-baseline')] if focus not in ('shifts','immediates') and info['detected_target']!='x86_64-baseline': selections.append(('native',info['detected_target'])) profiles={} with tempfile.TemporaryDirectory(prefix='stela-simd-integer-') as temporary: for index,(selection,target_id) in enumerate(selections): batch=compile_batch(cli,Path(temporary),functions,index,cpu_target=selection,target_id=target_id) batch['cohort']=focus result=bounded_cases([batch]) decoded=decode_native_stream(batch['native']) accounting=native_binary_accounting(batch,decoded) retained=retain_native_binary_evidence(batch,accounting,result['dynamic_x86_trace']) mnemonics=Counter(native_mnemonic(i.mnemonic) for i in decoded) optional={o['mnemonic'] for o in info['simd_opcodes'] if o['minimum_target']!='x86_64-baseline'} used=sorted(optional & mnemonics.keys()) expected=set() if focus=='integer': if '+ssse3' in target_id: expected.update(('pabsb','pabsw','pabsd')) if '+sse4.1' in target_id: expected.add('pcmpeqq') if '+sse4.2' in target_id: expected.add('pcmpgtq') elif focus=='rearrange' and '+ssse3' in target_id: expected.add('pshufb') elif focus=='reductions': if '+ssse3' in target_id: expected.add('pshufb') if '+sse4.1' in target_id: expected.update(('pminsb','pmaxsb','pminuw','pmaxuw','pminsd','pmaxsd','pminud','pmaxud')) elif focus=='saturating': if '+ssse3' in target_id: expected.update(('pabsb','pabsw','pabsd')) if '+sse4.1' in target_id: expected.add('packusdw') if '+sse4.2' in target_id: expected.add('pcmpgtq') elif focus=='indexed_products': if '+sse4.1' in target_id: expected.update(('pmulld','pmuldq','pmovsxwd','pmovzxwd')) elif focus=='products': if '+sse4.1' in target_id: expected.update(('pmulld','pmuldq')) if '+sse4.2' in target_id: expected.add('pcmpgtq') elif focus=='doubling_products': if '+ssse3' in target_id: expected.add('pmulhrsw') if '+sse4.1' in target_id: expected.update(('pmuldq','pmulld')) if '+pclmulqdq' in target_id: expected.add('pclmulqdq') elif focus=='narrowing_shifts': if '+sse4.1' in target_id: expected.add('packusdw') if '+sse4.2' in target_id: expected.add('pcmpgtq') elif focus=='dot_round_products': if '+sse4.1' in target_id: expected.add('pmuldq') elif focus=='floating_bits' and '+sse4.2' in target_id: expected.add('pcmpgtq') elif focus=='halving': if '+ssse3' in target_id: expected.add('pmaddubsw') if '+sse4.1' in target_id: expected.update(('pmovsxdq','pmovzxdq')) assert set(used)==expected,(target_id,used,expected) profiles[selection]={**result,'compilation':batch['receipt'],'optional_opcodes_used':used, 'native_binary_accounting':accounting,'retained_native_evidence':retained, 'packed_shifts_used':sorted({o['mnemonic'] for o in info['simd_immediate_shifts']} & mnemonics.keys())} assert digest(cli.read_bytes())==cli_hash and digest(Path(__file__).read_bytes())==harness_hash baseline=profiles['baseline'] report={'kind':f'simd_{focus}_arm_vs_native_x86','passed':True, 'three_way_register_state_comparisons':baseline['three_way_register_state_comparisons'], 'state_corpus_sha256':baseline['state_corpus_sha256'],'programs':len(functions), 'compiler_source_sha256':batch['compiler'],'compiler_executable_sha256':cli_hash, 'harness_source_sha256':harness_hash,'shared_runner_sha256':digest((ROOT/'dev/differential.py').read_bytes()), 'shared_sources_sha256':{name:digest((ROOT/name).read_bytes()) for name in ('dev/differential.py','dev/evidence.py')}, 'corpus_sha256':digest(batch['source']),'compilation':baseline['compilation'],'cpu_profiles':profiles, 'original_cpu_model':('QEMU10 max with AFP/FP16 plus classified Unicorn MAX baseline' if focus=='floating_bits' else 'MAX (FP16 enabled)' if focus=='immediates' else 'MAX (DotProd and RDM enabled)' if focus=='dot_round_products' else 'default'), 'total_profile_comparisons':sum(p['three_way_register_state_comparisons'] for p in profiles.values()), 'verification_wall_seconds':time.monotonic()-started, 'limits':f'Focused {focus} SIMD semantics with FP/AdvSIMD enabled; no FP arithmetic, traps, disabled-feature behavior or application-performance claim. Leaf RET is harness-owned.'} if focus in ('narrowing_shifts','dot_round_products'): report['architectural_state_bytes']=ctypes.sizeof(State) report['original_oracle_limits']=( 'Original A64 words execute unmodified under Unicorn with FP/AdvSIMD enabled. ' 'The selected CPU model enables DotProd/RDM where needed. All 784 architectural ' 'state bytes, including NZCV, FPCR, FPSR and every vector lane, compare against ' 'independent unbounded-integer math and actual emitted x86 execution. ' 'This is emulator conformance evidence; unavailable features, disabled FP access, ' 'FP traps and hardware-specific exception delivery remain outside this cohort.') write_evidence(ROOT/f'.stela/simd-{focus}-differential-report.json',report) print(json.dumps(report,indent=2)) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--no-build', action='store_true') parser.add_argument('--focus', choices=[*REGISTER_COHORTS, *FIXTURE_FAMILIES, NUMERICAL_FOCUS, FP_REGISTER_FOCUS, VECTOR_NUMERICAL_FOCUS, FUSED_NUMERICAL_FOCUS, CONVERSION_NUMERICAL_FOCUS, COMPARISON_MINMAX_FOCUS, ROUND_INTEGRAL_FOCUS, SQRT_NUMERICAL_FOCUS, ESTIMATE_NUMERICAL_FOCUS, REFINEMENT_NUMERICAL_FOCUS, BINARY_REMAINING_FOCUS, WIDENING_NUMERICAL_FOCUS, BFLOAT16_NUMERICAL_FOCUS, COMPLEX_NUMERICAL_FOCUS], help='Only the focused SIMD implementation cohort') parser.add_argument('--fixture-oracle', type=Path, help='Full-frame primary original pointer for an integer/crypto fixture family') parser.add_argument('--prepare-fixture-oracle', type=Path, help='Freeze original-only fixture inputs in this directory') parser.add_argument('--numerical-oracle', type=Path, default=NUMERICAL_FOUNDATION, help='Pinned numerical foundation receipt for scalar_numerical') parser.add_argument('--vector-oracle', type=Path, default=VECTOR_NUMERICAL_ORACLE, help='Pinned full-frame original vector numerical receipt') parser.add_argument('--fused-oracle', type=Path, default=FUSED_NUMERICAL_ORACLE, help='Pinned full-frame original fused numerical receipt') parser.add_argument('--conversion-oracle', type=Path, default=CONVERSION_NUMERICAL_ORACLE, help='Pinned full-frame original precision/integer conversion receipt') parser.add_argument('--comparison-minmax-oracle', type=Path, default=COMPARISON_MINMAX_ORACLE, help='Pinned full-frame original comparison/minmax receipt') parser.add_argument('--round-integral-oracle', type=Path, default=ROUND_INTEGRAL_ORACLE, help='Pinned full-frame original integral rounding receipt') parser.add_argument('--sqrt-oracle', type=Path, default=SQRT_NUMERICAL_ORACLE, help='Pinned full-frame original square-root receipt') parser.add_argument('--complex-oracle', type=Path, default=COMPLEX_NUMERICAL_ORACLE) parser.add_argument('--bfloat16-oracle', type=Path, default=BFLOAT16_NUMERICAL_ORACLE) parser.add_argument('--widening-oracle', type=Path, default=WIDENING_NUMERICAL_ORACLE) parser.add_argument('--binary-remaining-oracle', type=Path, default=BINARY_REMAINING_ORACLE) parser.add_argument('--estimate-oracle', type=Path, default=ESTIMATE_NUMERICAL_ORACLE) parser.add_argument('--refinement-oracle', type=Path, default=REFINEMENT_NUMERICAL_ORACLE) arguments = parser.parse_args() if arguments.prepare_fixture_oracle: if arguments.focus not in FIXTURE_FAMILIES: parser.error('--prepare-fixture-oracle requires an integer/crypto fixture focus') prepared = simd_fixture_runner.prepare_original(sys.modules[__name__], simd_fixture_runner.family_module(arguments.focus), arguments.prepare_fixture_oracle) print(json.dumps(prepared, indent=2)) return if not arguments.no_build: subprocess.run([str(ROOT / 'dev/cargo'), 'build', '--locked', '-p', 'stela-cli'], check=True) cli = ROOT / 'target/debug/stela' if arguments.focus in FIXTURE_FAMILIES: _, report = simd_fixture_runner.run(sys.modules[__name__], cli, arguments.focus, arguments.fixture_oracle) print(json.dumps(report, indent=2)) return if arguments.focus == NUMERICAL_FOCUS: run_scalar_numerical_cohort(cli, arguments.numerical_oracle) return if arguments.focus == FP_REGISTER_FOCUS: run_fp_register_cohort(cli, arguments.numerical_oracle) return if arguments.focus == COMPLEX_NUMERICAL_FOCUS: run_full_frame_numerical_cohort(cli, arguments.complex_oracle, arguments.focus) return if arguments.focus == BFLOAT16_NUMERICAL_FOCUS: run_full_frame_numerical_cohort(cli, arguments.bfloat16_oracle, arguments.focus) return if arguments.focus == WIDENING_NUMERICAL_FOCUS: run_full_frame_numerical_cohort(cli, arguments.widening_oracle, arguments.focus) return if arguments.focus == BINARY_REMAINING_FOCUS: run_full_frame_numerical_cohort(cli, arguments.binary_remaining_oracle, arguments.focus) return if arguments.focus in (ESTIMATE_NUMERICAL_FOCUS, REFINEMENT_NUMERICAL_FOCUS): oracle_path = arguments.estimate_oracle if arguments.focus == ESTIMATE_NUMERICAL_FOCUS else arguments.refinement_oracle run_full_frame_numerical_cohort(cli, oracle_path, arguments.focus) return if arguments.focus == SQRT_NUMERICAL_FOCUS: run_full_frame_numerical_cohort(cli, arguments.sqrt_oracle, SQRT_NUMERICAL_FOCUS) return if arguments.focus == ROUND_INTEGRAL_FOCUS: run_full_frame_numerical_cohort(cli, arguments.round_integral_oracle, ROUND_INTEGRAL_FOCUS) return if arguments.focus == COMPARISON_MINMAX_FOCUS: run_full_frame_numerical_cohort(cli, arguments.comparison_minmax_oracle, COMPARISON_MINMAX_FOCUS) return if arguments.focus == CONVERSION_NUMERICAL_FOCUS: run_conversion_numerical_cohort(cli, arguments.conversion_oracle) return if arguments.focus == FUSED_NUMERICAL_FOCUS: run_fused_numerical_cohort(cli, arguments.fused_oracle) return if arguments.focus == VECTOR_NUMERICAL_FOCUS: run_vector_numerical_cohort(cli, arguments.vector_oracle) return if arguments.focus: run_register_cohort(cli, arguments.focus) return cli_hash = digest(cli.read_bytes()) source_paths = [Path(__file__), ROOT / 'dev/differential.py', ROOT / 'dev/memory-faults.py', ROOT / 'dev/native-memory-faults.c'] identities = {str(path): digest(path.read_bytes()) for path in source_paths} leaf, blocks = programs() pair_leaf, pair_blocks = pair_programs() batches, compiler = [], None destination = ROOT / '.stela/simd-differential-report.json' previous_evidence = digest(destination.read_bytes()) if destination.exists() else None with tempfile.TemporaryDirectory(prefix='stela-simd-') as temporary: directory = Path(temporary) jobs = [] for cohort, functions, representative in [('single', leaf, blocks), ('pair', pair_leaf, pair_blocks)]: jobs.extend((cohort, functions[i:i + 512], False) for i in range(0, len(functions), 512)) jobs.extend((cohort, [case], True) for case in representative) for index, (cohort, functions, block) in enumerate(jobs): batch = compile_batch(cli, directory, functions, index, block) batch['cohort'] = cohort batch['receipt']['cohort'] = cohort if compiler is not None and compiler != batch['compiler']: raise AssertionError('Compiler changed during SIMD suite') compiler = batch['compiler'] batches.append(batch) result = bounded_cases(batches) result.update(verify_faults(cli, directory, compiler)) result.update(verify_declined_pairs(cli, directory)) if digest(cli.read_bytes()) != cli_hash or any(digest(path.read_bytes()) != identities[str(path)] for path in source_paths): raise AssertionError('SIMD compiler executable or shared sources changed during evidence collection') report = {'kind': 'simd_arm_vs_native_x86', 'passed': True, **result, 'leaf_mini_functions': len(leaf), 'block_programs': len(blocks), 'pair_leaf_mini_functions': len(pair_leaf), 'pair_block_programs': len(pair_blocks), 'previous_evidence_sha256': previous_evidence, 'cohort_source_sha256': {cohort: digest(b''.join(batch['source'] for batch in batches if batch['cohort'] == cohort)) for cohort in ('single', 'pair')}, 'compilation_batches': [batch['receipt'] for batch in batches], 'compiler_source_sha256': compiler, 'compiler_executable_sha256': cli_hash, 'harness_source_sha256': identities[str(Path(__file__))], 'shared_runner_sha256': identities[str(ROOT / 'dev/differential.py')], 'shared_sources_sha256': {str(path.relative_to(ROOT)): identities[str(path)] for path in source_paths[1:]}, 'guest_state_bytes': ctypes.sizeof(State), 'leaf_abi': LEAF_ABI, 'block_abi': BLOCK_ABI, 'tested_state': ['x0..x30', 'SP', 'NZCV', 'reserved metadata', 'V0..V31 full128', 'FPCR', 'FPSR', 'all private RAM bytes'], 'limits': 'Separate42single/MOVI and18paired data-movement forms, not SIMD arithmetic/FP arithmetic/PAC/MMIO/atomics/concurrency/process loading. Native execution is isolated and bounded. Leaf RET is harness-owned; representative block cases execute originalRET and check native return tuple. IncomingSPalignment faults follow declared enabled policy; Unicorn does not enforce that check. Pair loads stage both destinations under the declared native fault policy:122 native-policy checks,88 original/native full-state equal observations,16 explicitly retained Unicorn second-load divergences,18 SP-policy-only checks. Arm ASL reads whole Mem before vector assignments; Unicorn/current QEMU vector paths perform sequential loads. These observations establish neither an emulator defect nor Apple hardware/guest-fault conformance. Second-store faults preserve the completed first element under this private-RAM profile. Qstores have no128-bit atomicity/rollback claim; fault tests use wholly inaccessible/readonly stores and crosspageQloads. No guest syndrome/exception delivery or full binary verification.'} destination = ROOT / '.stela/simd-differential-report.json' snapshot = write_evidence(destination, report) print(json.dumps({key: value for key, value in report.items() if key not in ('fault_results', 'pair_fault_results', 'declined_pair_results', 'compilation_batches')}, indent=2)) if __name__ == '__main__': main()