[
  {
    "path": ".cargo/config.toml",
    "content": "[build]\ntarget-dir = \"build\"\n"
  },
  {
    "path": ".devcontainer/Dockerfile",
    "content": "FROM library/node:lts-bookworm\n\nARG DEBIAN_FRONTEND=noninteractive\nRUN apt update && \\\n    apt-get install -y nodejs nasm gdb unzip p7zip-full openjdk-17-jre wget python3 qemu-system-x86 git-core build-essential libc6-dev-i386-cross libc6-dev-i386 clang curl time\nRUN npm install -g jshint\nRUN curl https://sh.rustup.rs -sSf | sh -s -- -y\nRUN /root/.cargo/bin/rustup toolchain install stable && \\\n    /root/.cargo/bin/rustup target add wasm32-unknown-unknown && \\\n    /root/.cargo/bin/rustup component add rustfmt-preview && \\\n    /root/.cargo/bin/rustup update && /root/.cargo/bin/rustup update nightly\n"
  },
  {
    "path": ".devcontainer/devcontainer.json",
    "content": "{\n\t\"name\": \"v86 dev container\",\n\t\"build\": {\n\t\t\"dockerfile\": \"Dockerfile\",\n\t\t\"args\": {\n\t\t\t\"VARIANT\": \"ubuntu\"\n\t\t}\n\t},\n\t\"features\": {\n\t\t\"ghcr.io/devcontainers-contrib/features/curl-apt-get:1\": {},\n\t\t\"ghcr.io/jungaretti/features/ripgrep:1\": {}\n\t},\n\t\"customizations\": {\n\t\t\"vscode\": {\n\t\t\t\"extensions\": [\n\t\t\t\t\"ms-azuretools.vscode-docker\",\n\t\t\t\t\"yzhang.markdown-all-in-one\",\n\t\t\t\t\"DavidAnson.vscode-markdownlint\",\n\t\t\t\t\"christian-kohler.npm-intellisense\",\n\t\t\t\t\"dbaeumer.vscode-eslint\",\n\t\t\t\t\"dustypomerleau.rust-syntax\",\n\t\t\t\t\"serayuzgur.crates\",\n\t\t\t\t\"ms-vscode.makefile-tools\",\n\t\t\t\t\"rust-lang.rust-analyzer\"\n\t\t\t]\n\t\t}\n\t}\n}"
  },
  {
    "path": ".editorconfig",
    "content": "root=true\n\n[*.js]\ncharset = utf-8\nindent_style = space\nindent_size = 4\n"
  },
  {
    "path": ".gitattributes",
    "content": "lib/zstd/* linguist-vendored\nlib/softfloat/* linguist-vendored\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/issue.md",
    "content": "---\nname: Issue\nabout: Bug reports or feature requests\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n\n<!--\nWelcome to v86's issue tracker!\n\nWe use this tracker for bug reports or feature requests. For user support, questions or general comments, use the chat at https://gitter.im/copy/v86 or the forum at https://github.com/copy/v86/discussions\n\nPlease don't ask for support for any version of Windows. There are many existing issues at https://github.com/copy/v86/issues?q=is%253Aissue+windows. See also docs/windows-nt.md and docs/windows-9x.md.\n\nBefore reporting OS incompatibilities, check existing issues and the compatibility section of the readme.\n\nWhen reporting issues, please provide the following information:\n- Version of v86 (see the bottom of index.html or the output of `git describe --tags HEAD`)\n- Browser/OS\n- The browser's console output: Open debug.html and press ctrl+shift+k (firefox) or ctrl+shift+j (chromium)\n- If relevant, upload the disk images to allow developers to reproduce the problem locally\n-->\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "name: CI\n\non:\n  push:\n  pull_request:\n\njobs:\n  eslint:\n    name: eslint\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n\n      - name: Run eslint\n        run: sudo npm install -g eslint; make eslint\n\n  test:\n    runs-on: ubuntu-latest\n    name: Build and test\n    timeout-minutes: 60\n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n        with:\n          fetch-depth: 0\n          filter: tree:0\n\n      - name: Setup Node.js version\n        uses: actions/setup-node@v4\n        with:\n          node-version: '20'\n          check-latest: true\n          registry-url: 'https://registry.npmjs.org'\n\n      - name: Setup toolchain\n        run: |\n          rustup toolchain install stable --profile minimal\n          rustup target add wasm32-unknown-unknown\n          rustup component add rustfmt\n\n      - name: Install APT packages\n        run: |\n          sudo apt-get update -y\n          sudo apt-get install nasm gdb qemu-system-x86 libc6-dev-i386 -y\n\n      - name: Build all-debug\n        run: make all-debug\n\n      - name: Build all\n        run: make all\n\n      - name: Build fallback\n        run: make build/v86-fallback.wasm\n\n      - name: rustfmt check\n        run: make rustfmt\n\n      - name: Fetch kvm-unit-test cache\n        uses: actions/cache@v4\n        id: cache-kvm-unit-test\n        with:\n          path: tests/kvm-unit-tests/\n          key: ${{ runner.os }}-kvm-unit-test\n\n      - name: Build kvm-unit-test\n        if: steps.cache-kvm-unit-test.outputs.cache-hit != 'true'\n        run: (cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)\n\n      - name: Run kvm-unit-test\n        run: tests/kvm-unit-tests/run.mjs tests/kvm-unit-tests/x86/realmode.flat\n\n      - name: Fetch namsmtests cache\n        uses: actions/cache@v4\n        id: cache-nasmtests\n        with:\n          path: tests/nasm/build/\n          key: ${{ runner.os }}-nasmtests\n\n      - name: Run nasmtests\n        run: MAX_PARALLEL_TESTS=1 make nasmtests\n\n      - name: Run nasmtests-force-jit\n        run: MAX_PARALLEL_TESTS=1 make nasmtests-force-jit\n\n      - name: Run rust-test\n        run: make rust-test\n\n      - name: Fetch image cache\n        uses: actions/cache@v4\n        id: cache-images\n        with:\n          path: images/\n          key: ${{ runner.os }}-images-v2\n\n      - name: Download uncached images\n        if: steps.cache-images.outputs.cache-hit != 'true'\n        run: mkdir -p images && curl --compressed --output-dir images --remote-name-all https://i.copy.sh/{linux.iso,linux3.iso,linux4.iso,buildroot-bzimage68.bin,TinyCore-11.0.iso,oberon.img,msdos.img,openbsd-floppy.img,kolibri.img,windows101.img,os8.img,freedos722.img,mobius-fd-release5.img,msdos622.img}\n\n      - name: Run api-tests\n        run: make api-tests\n\n      - name: Run qemutests\n        run: make qemutests\n\n      - name: Run qemutests-release\n        run: make qemutests-release\n\n      - name: Run jitpagingtests\n        run: make jitpagingtests\n\n      - name: Run integration tests\n        run: MAX_PARALLEL_TESTS=1 make tests\n\n      - name: Run devices tests\n        run: make devices-test\n\n      - name: Run expect tests\n        run: make expect-tests\n\n      - name: Update package.json version\n        run: make update-package-json-version\n\n      - name: Upload the artifact\n        uses: actions/upload-artifact@v4\n        with:\n          name: v86\n          path: |\n            package.json\n            LICENSE\n            Readme.md\n            build/libv86*.js\n            build/libv86*.js.map\n            build/v86*.wasm\n            build/*.mjs\n\n  upload:\n    name: Upload release\n    runs-on: ubuntu-latest\n    needs: test\n    if: github.ref == 'refs/heads/master'\n\n    permissions:\n      id-token: write\n      contents: write\n\n    steps:\n      - name: Delete old release and tag\n        uses: dev-drprasad/delete-tag-and-release@v1.0.1\n        with:\n          delete_release: true\n          tag_name: latest\n          github_token: ${{ secrets.GITHUB_TOKEN }}\n\n      - name: Get artifacts\n        uses: actions/download-artifact@v4\n        with:\n          name: v86\n\n      - name: Display structure of downloaded files\n        run: ls -R\n\n      - uses: actions/setup-node@v4\n        with:\n          node-version: '20.x'\n          registry-url: 'https://registry.npmjs.org'\n\n      - name: Release to GitHub\n        uses: ncipollo/release-action@v1\n        with:\n          name: Latest Release\n          tag: latest\n          commit: master\n          body: ${{ github.event.head_commit.message }}\n          artifacts: \"build/libv86*.js,build/libv86*.js.map,build/*.mjs,build/v86*.wasm\"\n          prerelease: true\n\n      - run: npm publish --provenance --access public\n        env:\n          NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}\n"
  },
  {
    "path": ".gitignore",
    "content": "*.swp\n*.swo\ntests/qemu/test-i386\ntests/jit-paging/test-jit\n*.map\nbuild/\nclosure-compiler/\nimages/\n*.bak\n*.orig\n*.wasm\n*.o\n*.bin\n*.img\n*.fixture\n*.fuse_hidden*\n*.DS_Store\nnode_modules/\nCargo.lock\nbuild-head\nsrc/rust/gen/interpreter.rs\nsrc/rust/gen/interpreter0f.rs\nsrc/rust/gen/analyzer.rs\nsrc/rust/gen/analyzer0f.rs\nsrc/rust/gen/jit.rs\nsrc/rust/gen/jit0f.rs\nbios/seabios\nbench-results\n"
  },
  {
    "path": ".rustfmt.toml",
    "content": "use_field_init_shorthand = true\nmatch_block_trailing_comma = true\nfn_single_line = true\nimports_indent = \"Block\"\ncontrol_brace_style = \"ClosingNextLine\"\nsingle_line_if_else_max_width = 92\n"
  },
  {
    "path": ".vscode/tasks.json",
    "content": "{\n    // See https://go.microsoft.com/fwlink/?LinkId=733558\n    // for the documentation about the tasks.json format\n    \"version\": \"2.0.0\",\n    \"tasks\": [\n        {\n            \"label\": \"Fetch images\",\n            \"type\": \"shell\",\n            \"command\": \"wget -P images/ https://i.copy.sh/{linux3.iso,linux.iso,linux4.iso,buildroot-bzimage.bin,openbsd-floppy.img,kolibri.img,windows101.img,os8.img,freedos722.img}\"\n        }\n    ]\n}\n"
  },
  {
    "path": "Cargo.toml",
    "content": "[package]\nname = \"v86\"\nversion = \"0.1.0\"\npublish = false\nedition = \"2021\"\n\n[features]\ndefault = []\nprofiler = []\n\n[lib]\ncrate-type = [\"cdylib\"]\npath = \"src/rust/lib.rs\"\n\n[profile.test]\nlto = false\nopt-level = 2\noverflow-checks = false\n\n[profile.dev]\nlto = true\nopt-level = 2\npanic = \"abort\"\noverflow-checks = false\n\n[profile.release]\nlto = true\nopt-level = 3\nincremental = false\npanic = \"abort\"\n"
  },
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2012, The v86 contributors\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n1. Redistributions of source code must retain the above copyright notice, this\n   list of conditions and the following disclaimer.\n2. Redistributions in binary form must reproduce the above copyright notice,\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "LICENSE.MIT",
    "content": "QEMU Floppy disk emulator (Intel 82078)\n\nCopyright (c) 2003, 2007 Jocelyn Mayer\nCopyright (c) 2008 Hervé Poussineau\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\nTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "CLOSURE_DIR=closure-compiler\nCLOSURE=$(CLOSURE_DIR)/compiler.jar\nNASM_TEST_DIR=./tests/nasm\n\nINSTRUCTION_TABLES=src/rust/gen/jit.rs src/rust/gen/jit0f.rs \\\n\t\t   src/rust/gen/interpreter.rs src/rust/gen/interpreter0f.rs \\\n\t\t   src/rust/gen/analyzer.rs src/rust/gen/analyzer0f.rs \\\n\n# Only the dependencies common to both generate_{jit,interpreter}.js\nGEN_DEPENDENCIES=$(filter-out gen/generate_interpreter.js gen/generate_jit.js gen/generate_analyzer.js, $(wildcard gen/*.js))\nJIT_DEPENDENCIES=$(GEN_DEPENDENCIES) gen/generate_jit.js\nINTERPRETER_DEPENDENCIES=$(GEN_DEPENDENCIES) gen/generate_interpreter.js\nANALYZER_DEPENDENCIES=$(GEN_DEPENDENCIES) gen/generate_analyzer.js\n\nSTRIP_DEBUG_FLAG=\nifeq ($(STRIP_DEBUG),true)\nSTRIP_DEBUG_FLAG=--v86-strip-debug\nendif\n\nWASM_OPT ?= false\n\ndefault: build/v86-debug.wasm\nall: build/v86_all.js build/libv86.js build/libv86.mjs build/v86.wasm\nall-debug: build/libv86-debug.js build/libv86-debug.mjs build/v86-debug.wasm\nbrowser: build/v86_all.js\n\n# Used for nodejs builds and in order to profile code.\n# `debug` gives identifiers a readable name, make sure it doesn't have any side effects.\nCLOSURE_READABLE=--formatting PRETTY_PRINT --debug\n\nCLOSURE_SOURCE_MAP=\\\n\t\t--source_map_format V3\\\n\t\t--create_source_map '%outname%.map'\n\nCLOSURE_FLAGS=\\\n\t\t--generate_exports\\\n\t\t--externs src/externs.js\\\n\t\t--warning_level VERBOSE\\\n\t\t--jscomp_error accessControls\\\n\t\t--jscomp_error checkRegExp\\\n\t\t--jscomp_error checkTypes\\\n\t\t--jscomp_error checkVars\\\n\t\t--jscomp_error conformanceViolations\\\n\t\t--jscomp_error const\\\n\t\t--jscomp_error constantProperty\\\n\t\t--jscomp_error deprecated\\\n\t\t--jscomp_error deprecatedAnnotations\\\n\t\t--jscomp_error duplicateMessage\\\n\t\t--jscomp_error es5Strict\\\n\t\t--jscomp_error externsValidation\\\n\t\t--jscomp_error globalThis\\\n\t\t--jscomp_error invalidCasts\\\n\t\t--jscomp_error misplacedTypeAnnotation\\\n\t\t--jscomp_error missingProperties\\\n\t\t--jscomp_error missingReturn\\\n\t\t--jscomp_error msgDescriptions\\\n\t\t--jscomp_error nonStandardJsDocs\\\n\t\t--jscomp_error suspiciousCode\\\n\t\t--jscomp_error strictModuleDepCheck\\\n\t\t--jscomp_error typeInvalidation\\\n\t\t--jscomp_error undefinedVars\\\n\t\t--jscomp_error unknownDefines\\\n\t\t--jscomp_error visibility\\\n\t\t--use_types_for_optimization\\\n\t\t--assume_function_wrapper\\\n\t\t--summary_detail_level 3\\\n\t\t--language_in ECMASCRIPT_2020\\\n\t\t--language_out ECMASCRIPT_2020\n\nCARGO_FLAGS_SAFE=\\\n\t\t--target wasm32-unknown-unknown \\\n\t\t-- \\\n\t\t-C linker=tools/rust-lld-wrapper \\\n\t\t-C link-args=\"--import-table --global-base=4096 $(STRIP_DEBUG_FLAG)\" \\\n\t\t-C link-args=\"build/softfloat.o\" \\\n\t\t-C link-args=\"build/zstddeclib.o\" \\\n\t\t--verbose\n\nCARGO_FLAGS=$(CARGO_FLAGS_SAFE) -C target-feature=+bulk-memory -C target-feature=+multivalue -C target-feature=+simd128\n\nCORE_FILES=cjs.js const.js io.js main.js lib.js buffer.js ide.js pci.js floppy.js \\\n\t   dma.js pit.js vga.js ps2.js rtc.js uart.js \\\n\t   acpi.js iso9660.js \\\n\t   state.js ne2k.js sb16.js virtio.js virtio_console.js virtio_net.js virtio_balloon.js \\\n\t   bus.js log.js cpu.js \\\n\t   elf.js kernel.js\nLIB_FILES=9p.js filesystem.js marshall.js\nBROWSER_FILES=screen.js keyboard.js mouse.js speaker.js serial.js \\\n\t      network.js starter.js worker_bus.js dummy_screen.js \\\n\t      inbrowser_network.js fake_network.js wisp_network.js fetch_network.js \\\n          print_stats.js filestorage.js\n\nRUST_FILES=$(shell find src/rust/ -name '*.rs') \\\n\t   src/rust/gen/interpreter.rs src/rust/gen/interpreter0f.rs \\\n\t   src/rust/gen/jit.rs src/rust/gen/jit0f.rs \\\n\t   src/rust/gen/analyzer.rs src/rust/gen/analyzer0f.rs\n\nCORE_FILES:=$(addprefix src/,$(CORE_FILES))\nLIB_FILES:=$(addprefix lib/,$(LIB_FILES))\nBROWSER_FILES:=$(addprefix src/browser/,$(BROWSER_FILES))\n\nbuild/v86_all.js: $(CLOSURE) src/*.js src/browser/*.js lib/*.js\n\tmkdir -p build\n\t-ls -lh build/v86_all.js\n\tjava -jar $(CLOSURE) \\\n\t\t--js_output_file build/v86_all.js\\\n\t\t--define=DEBUG=false\\\n\t\t$(CLOSURE_SOURCE_MAP)\\\n\t\t$(CLOSURE_FLAGS)\\\n\t\t--compilation_level ADVANCED\\\n\t\t--js $(CORE_FILES)\\\n\t\t--js $(LIB_FILES)\\\n\t\t--js $(BROWSER_FILES)\\\n\t\t--js src/browser/main.js\n\tls -lh build/v86_all.js\n\nbuild/v86_all_debug.js: $(CLOSURE) src/*.js src/browser/*.js lib/*.js\n\tmkdir -p build\n\tjava -jar $(CLOSURE) \\\n\t\t--js_output_file build/v86_all_debug.js\\\n\t\t--define=DEBUG=true\\\n\t\t$(CLOSURE_SOURCE_MAP)\\\n\t\t$(CLOSURE_FLAGS)\\\n\t\t--compilation_level ADVANCED\\\n\t\t--js $(CORE_FILES)\\\n\t\t--js $(LIB_FILES)\\\n\t\t--js $(BROWSER_FILES)\\\n\t\t--js src/browser/main.js\n\nbuild/libv86.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js\n\tmkdir -p build\n\t-ls -lh build/libv86.js\n\tjava -jar $(CLOSURE) \\\n\t\t--js_output_file build/libv86.js\\\n\t\t--define=DEBUG=false\\\n\t\t$(CLOSURE_FLAGS)\\\n\t\t--compilation_level SIMPLE\\\n\t\t--jscomp_off=missingProperties\\\n\t\t--output_wrapper ';(function(){%output%}).call(this);'\\\n\t\t--js $(CORE_FILES)\\\n\t\t--js $(BROWSER_FILES)\\\n\t\t--js $(LIB_FILES)\n\tls -lh build/libv86.js\n\nbuild/libv86.mjs: $(CLOSURE) src/*.js lib/*.js src/browser/*.js\n\tmkdir -p build\n\t-ls -lh build/libv86.js\n\tjava -jar $(CLOSURE) \\\n\t\t--js_output_file build/libv86.mjs\\\n\t\t--define=DEBUG=false\\\n\t\t$(CLOSURE_FLAGS)\\\n\t\t--compilation_level SIMPLE\\\n\t\t--jscomp_off=missingProperties\\\n\t\t--output_wrapper ';let module = {exports:{}}; %output%; export default module.exports.V86; export let {V86, CPU} = module.exports;'\\\n\t\t--js $(CORE_FILES)\\\n\t\t--js $(BROWSER_FILES)\\\n\t\t--js $(LIB_FILES)\\\n\t\t--chunk_output_type=ES_MODULES\\\n\t\t--emit_use_strict=false\n\tls -lh build/libv86.mjs\n\nbuild/libv86-debug.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js\n\tmkdir -p build\n\tjava -jar $(CLOSURE) \\\n\t\t--js_output_file build/libv86-debug.js\\\n\t\t--define=DEBUG=true\\\n\t\t$(CLOSURE_FLAGS)\\\n\t\t$(CLOSURE_READABLE)\\\n\t\t--compilation_level SIMPLE\\\n\t\t--jscomp_off=missingProperties\\\n\t\t--output_wrapper ';(function(){%output%}).call(this);'\\\n\t\t--js $(CORE_FILES)\\\n\t\t--js $(BROWSER_FILES)\\\n\t\t--js $(LIB_FILES)\n\tls -lh build/libv86-debug.js\n\nbuild/libv86-debug.mjs: $(CLOSURE) src/*.js lib/*.js src/browser/*.js\n\tmkdir -p build\n\tjava -jar $(CLOSURE) \\\n\t\t--js_output_file build/libv86-debug.mjs\\\n\t\t--define=DEBUG=true\\\n\t\t$(CLOSURE_FLAGS)\\\n\t\t$(CLOSURE_READABLE)\\\n\t\t--compilation_level SIMPLE\\\n\t\t--jscomp_off=missingProperties\\\n\t\t--output_wrapper ';let module = {exports:{}}; %output%; export default module.exports.V86; export let {V86, CPU} = module.exports;'\\\n\t\t--js $(CORE_FILES)\\\n\t\t--js $(BROWSER_FILES)\\\n\t\t--js $(LIB_FILES)\\\n\t\t--chunk_output_type=ES_MODULES\\\n\t\t--emit_use_strict=false\n\tls -lh build/libv86-debug.mjs\n\nsrc/rust/gen/jit.rs: $(JIT_DEPENDENCIES)\n\t./gen/generate_jit.js --output-dir build/ --table jit\nsrc/rust/gen/jit0f.rs: $(JIT_DEPENDENCIES)\n\t./gen/generate_jit.js --output-dir build/ --table jit0f\n\nsrc/rust/gen/interpreter.rs: $(INTERPRETER_DEPENDENCIES)\n\t./gen/generate_interpreter.js --output-dir build/ --table interpreter\nsrc/rust/gen/interpreter0f.rs: $(INTERPRETER_DEPENDENCIES)\n\t./gen/generate_interpreter.js --output-dir build/ --table interpreter0f\n\nsrc/rust/gen/analyzer.rs: $(ANALYZER_DEPENDENCIES)\n\t./gen/generate_analyzer.js --output-dir build/ --table analyzer\nsrc/rust/gen/analyzer0f.rs: $(ANALYZER_DEPENDENCIES)\n\t./gen/generate_analyzer.js --output-dir build/ --table analyzer0f\n\nbuild/v86.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml\n\tmkdir -p build/\n\t-BLOCK_SIZE=K ls -l build/v86.wasm\n\tcargo rustc --release $(CARGO_FLAGS)\n\tcp build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm\n\t-$(WASM_OPT) && wasm-opt -O2 --strip-debug build/v86.wasm -o build/v86.wasm\n\tBLOCK_SIZE=K ls -l build/v86.wasm\n\nbuild/v86-debug.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml\n\tmkdir -p build/\n\t-BLOCK_SIZE=K ls -l build/v86-debug.wasm\n\tcargo rustc $(CARGO_FLAGS)\n\tcp build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm\n\tBLOCK_SIZE=K ls -l build/v86-debug.wasm\n\nbuild/v86-fallback.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml\n\tmkdir -p build/\n\tcargo rustc --release $(CARGO_FLAGS_SAFE)\n\tcp build/wasm32-unknown-unknown/release/v86.wasm build/v86-fallback.wasm || true\n\ndebug-with-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml\n\tmkdir -p build/\n\tcargo rustc --features profiler $(CARGO_FLAGS)\n\tcp build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm || true\n\nwith-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml\n\tmkdir -p build/\n\tcargo rustc --release --features profiler $(CARGO_FLAGS)\n\tcp build/wasm32-unknown-unknown/release/v86.wasm build/v86.wasm || true\n\nwatch:\n\tcargo watch -x 'rustc $(CARGO_FLAGS)' -s 'cp build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm'\n\nbuild/softfloat.o: lib/softfloat/softfloat.c\n\tmkdir -p build\n\tclang -c -Wall \\\n\t    --target=wasm32 -O3 -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \\\n\t    -DSOFTFLOAT_FAST_INT64 -DINLINE_LEVEL=5 -DSOFTFLOAT_FAST_DIV32TO16 -DSOFTFLOAT_FAST_DIV64TO32 \\\n\t    -o build/softfloat.o \\\n\t    lib/softfloat/softfloat.c\n\nbuild/zstddeclib.o: lib/zstd/zstddeclib.c\n\tmkdir -p build\n\tclang -c -Wall \\\n\t    --target=wasm32 -O3 -flto -nostdlib -fvisibility=hidden -ffunction-sections -fdata-sections \\\n\t    -DZSTDLIB_VISIBILITY=\"\" \\\n\t    -o build/zstddeclib.o \\\n\t    lib/zstd/zstddeclib.c\n\nclean:\n\t-rm build/libv86.js\n\t-rm build/libv86.mjs\n\t-rm build/libv86-debug.js\n\t-rm build/libv86-debug.mjs\n\t-rm build/v86_all.js\n\t-rm build/v86.wasm\n\t-rm build/v86-debug.wasm\n\t-rm $(INSTRUCTION_TABLES)\n\t-rm build/*.map\n\t-rm build/*.wast\n\t-rm build/*.o\n\t$(MAKE) -C $(NASM_TEST_DIR) clean\n\nrun:\n\tpython3 -m http.server 2> /dev/null\n\nupdate_version:\n\tset -e ;\\\n\tCOMMIT=`git log --format=\"%h\" -n 1` ;\\\n\tDATE=`git log --date=\"format:%b %e, %Y %H:%m\" --format=\"%cd\" -n 1` ;\\\n\tSEARCH='<code>Version: <a id=\"version\" href=\"https://github.com/copy/v86/commits/[a-f0-9]\\+\">[a-f0-9]\\+</a> ([^(]\\+)</code>' ;\\\n\tREPLACE='<code>Version: <a id=\"version\" href=\"https://github.com/copy/v86/commits/'$$COMMIT'\">'$$COMMIT'</a> ('$$DATE')</code>' ;\\\n\tsed -i \"s@$$SEARCH@$$REPLACE@g\" index.html ;\\\n\tSEARCH='<script src=\"build/v86_all.js?[a-f0-9]\\+\"></script>' ;\\\n\tREPLACE='<script src=\"build/v86_all.js?'$$COMMIT'\"></script>' ;\\\n\tsed -i \"s@$$SEARCH@$$REPLACE@g\" index.html ;\\\n\tgrep $$COMMIT index.html\n\n\n$(CLOSURE):\n\tmkdir -p $(CLOSURE_DIR)\n\t# don't upgrade until https://github.com/google/closure-compiler/issues/3972 is fixed\n\twget -nv -O $(CLOSURE) https://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20210601/closure-compiler-v20210601.jar\n\nbuild/integration-test-fs/fs.json: images/buildroot-bzimage68.bin\n\tmkdir -p build/integration-test-fs/flat\n\tcp images/buildroot-bzimage68.bin build/integration-test-fs/bzImage\n\ttouch build/integration-test-fs/initrd\n\tcd build/integration-test-fs && tar cfv fs.tar bzImage initrd\n\t./tools/fs2json.py build/integration-test-fs/fs.tar --out build/integration-test-fs/fs.json\n\t./tools/copy-to-sha256.py build/integration-test-fs/fs.tar build/integration-test-fs/flat\n\trm build/integration-test-fs/fs.tar build/integration-test-fs/bzImage build/integration-test-fs/initrd\n\ntests: build/v86-debug.wasm build/integration-test-fs/fs.json\n\tLOG_LEVEL=3 ./tests/full/run.js\n\ntests-release: build/libv86.js build/v86.wasm build/integration-test-fs/fs.json\n\tTEST_RELEASE_BUILD=1 ./tests/full/run.js\n\nnasmtests: build/v86-debug.wasm\n\t$(NASM_TEST_DIR)/create_tests.js\n\t$(NASM_TEST_DIR)/gen_fixtures.js\n\t$(NASM_TEST_DIR)/run.js\n\nnasmtests-force-jit: build/v86-debug.wasm\n\t$(NASM_TEST_DIR)/create_tests.js\n\t$(NASM_TEST_DIR)/gen_fixtures.js\n\t$(NASM_TEST_DIR)/run.js --force-jit\n\njitpagingtests: build/v86-debug.wasm\n\t$(MAKE) -C tests/jit-paging test-jit\n\t./tests/jit-paging/run.js\n\nqemutests: build/v86-debug.wasm\n\t$(MAKE) -C tests/qemu test-i386\n\tLOG_LEVEL=3 ./tests/qemu/run.js build/qemu-test-result\n\t./tests/qemu/run-qemu.js > build/qemu-test-reference\n\tdiff build/qemu-test-result build/qemu-test-reference\n\nqemutests-release: build/libv86.mjs build/v86.wasm\n\t$(MAKE) -C tests/qemu test-i386\n\tTEST_RELEASE_BUILD=1 time ./tests/qemu/run.js build/qemu-test-result\n\t./tests/qemu/run-qemu.js > build/qemu-test-reference\n\tdiff build/qemu-test-result build/qemu-test-reference\n\nkvm-unit-test: build/v86-debug.wasm\n\t(cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)\n\ttests/kvm-unit-tests/run.mjs tests/kvm-unit-tests/x86/realmode.flat\n\nkvm-unit-test-release: build/libv86.mjs build/v86.wasm\n\t(cd tests/kvm-unit-tests && ./configure && make x86/realmode.flat)\n\tTEST_RELEASE_BUILD=1 tests/kvm-unit-tests/run.mjs tests/kvm-unit-tests/x86/realmode.flat\n\nexpect-tests: build/v86-debug.wasm build/libwabt.cjs\n\tmake -C tests/expect/tests\n\t./tests/expect/run.js\n\ndevices-test: build/v86-debug.wasm\n\t./tests/devices/virtio_9p.js\n\t./tests/devices/virtio_console.js\n\t./tests/devices/fetch_network.js\n\tUSE_VIRTIO=1 ./tests/devices/fetch_network.js\n\t./tests/devices/wisp_network.js\n\t./tests/devices/virtio_balloon.js\n\nrust-test: $(RUST_FILES)\n\tenv RUSTFLAGS=\"-D warnings\" RUST_BACKTRACE=full RUST_TEST_THREADS=1 cargo test -- --nocapture\n\t./tests/rust/verify-wasmgen-dummy-output.js\n\nrust-test-intensive:\n\tQUICKCHECK_TESTS=100000000 make rust-test\n\napi-tests: build/v86-debug.wasm\n\t./tests/api/clean-shutdown.js\n\t./tests/api/state.js\n\t./tests/api/reset.js\n\t./tests/api/floppy.js\n\t./tests/api/cdrom-insert-eject.js\n\t./tests/api/serial.js\n\t./tests/api/reboot.js\n\t./tests/api/pic.js\n\nall-tests: eslint kvm-unit-test qemutests qemutests-release jitpagingtests api-tests nasmtests nasmtests-force-jit rust-test tests expect-tests\n\t# Skipping:\n\t# - devices-test (hangs)\n\neslint:\n\teslint src tests gen lib examples tools\n\nrustfmt: $(RUST_FILES)\n\tcargo fmt --all -- --check --config fn_single_line=true,control_brace_style=ClosingNextLine\n\nbuild/capstone-x86.min.js:\n\tmkdir -p build\n\twget -nv -P build https://github.com/AlexAltea/capstone.js/releases/download/v3.0.5-rc1/capstone-x86.min.js\n\nbuild/libwabt.cjs:\n\tmkdir -p build\n\twget -nv -P build https://github.com/WebAssembly/wabt/archive/1.0.6.zip\n\tunzip -j -d build/ build/1.0.6.zip wabt-1.0.6/demo/libwabt.js\n\tmv build/libwabt.js build/libwabt.cjs\n\trm build/1.0.6.zip\n\nbuild/xterm.js:\n\tcurl https://cdn.jsdelivr.net/npm/xterm@5.2.1/lib/xterm.min.js > build/xterm.js\n\tcurl https://cdn.jsdelivr.net/npm/xterm@5.2.1/lib/xterm.js.map > build/xterm.js.map\n\tcurl https://cdn.jsdelivr.net/npm/xterm@5.2.1/css/xterm.css > build/xterm.css\n\nupdate-package-json-version:\n\tgit describe --tags --exclude latest | sed 's/-/./' | tr - + | tee build/version\n\tjq --arg version \"$$(cat build/version)\" '.version = $$version' package.json > package.json.tmp\n\tmv package.json.tmp package.json\n"
  },
  {
    "path": "Readme.md",
    "content": "[![Join the chat at https://gitter.im/copy/v86](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/copy/v86) or #v86 on [irc.libera.chat](https://libera.chat/)\n\nv86 emulates an x86-compatible CPU and hardware. Machine code is translated to\nWebAssembly modules at runtime in order to achieve decent performance. Here's a\nlist of emulated hardware:\n\n- An x86-compatible CPU. The instruction set is around Pentium 4 level,\n  including full SSE3 support. Some features are missing, in particular:\n  - Task gates, far calls in protected mode\n  - Some 16 bit protected mode features\n  - Single stepping (trap flag, debug registers)\n  - Some exceptions, especially floating point and SSE\n  - Multicore\n  - 64-bit extensions\n- A floating point unit (FPU). Calculations are done using the Berkeley\n  SoftFloat library and therefore should be precise (but slow). Trigonometric\n  and log functions are emulated using 64-bit floats and may be less precise.\n  Not all FPU exceptions are supported.\n- A floppy disk controller (8272A).\n- An 8042 Keyboard Controller, PS2. With mouse support.\n- An 8254 Programmable Interval Timer (PIT).\n- An 8259 Programmable Interrupt Controller (PIC).\n- Partial APIC support.\n- A CMOS Real Time Clock (RTC).\n- A generic VGA card with SVGA support and Bochs VBE Extensions.\n- A PCI bus. This one is partly incomplete and not used by every device.\n- An IDE disk controller.\n  - A built-in ISO 9660 CD-ROM generator.\n- An NE2000 (RTL8390) PCI network card.\n- Various virtio devices: Filesystem, network and balloon.\n- A SoundBlaster 16 sound card.\n\n## Demos\n\n[9front](https://copy.sh/v86/?profile=9front) —\n[Arch Linux](https://copy.sh/v86/?profile=archlinux) —\n[Android-x86 1.6-r2](https://copy.sh/v86?profile=android) —\n[Android-x86 4.4-r2](https://copy.sh/v86?profile=android4) —\n[BasicLinux](https://copy.sh/v86/?profile=basiclinux) —\n[Buildroot Linux](https://copy.sh/v86/?profile=buildroot) —\n[Damn Small Linux](https://copy.sh/v86/?profile=dsl) —\n[ELKS](https://copy.sh/v86/?profile=elks) —\n[FreeDOS](https://copy.sh/v86/?profile=freedos) —\n[FreeBSD](https://copy.sh/v86/?profile=freebsd) —\n[FiwixOS](https://copy.sh/v86/?profile=fiwix) —\n[Haiku](https://copy.sh/v86/?profile=haiku) —\n[SkiffOS](https://copy.sh/v86/?profile=copy/skiffos) —\n[ReactOS](https://copy.sh/v86/?profile=reactos) —\n[Windows 2000](https://copy.sh/v86/?profile=windows2000) —\n[Windows 98](https://copy.sh/v86/?profile=windows98) —\n[Windows 95](https://copy.sh/v86/?profile=windows95) —\n[Windows 1.01](https://copy.sh/v86/?profile=windows1) —\n[MS-DOS 6.22](https://copy.sh/v86/?profile=msdos) —\n[OpenBSD](https://copy.sh/v86/?profile=openbsd) —\n[Oberon](https://copy.sh/v86/?profile=oberon) —\n[KolibriOS](https://copy.sh/v86/?profile=kolibrios) —\n[SkiftOS](https://copy.sh/v86?profile=skift) —\n[QNX](https://copy.sh/v86/?profile=qnx)\n\n## Documentation\n\n[How it works](docs/how-it-works.md) —\n[Networking](docs/networking.md) —\n[Alpine Linux guest setup](tools/docker/alpine/) —\n[Arch Linux guest setup](docs/archlinux.md) —\n[Windows NT guest setup](docs/windows-nt.md) —\n[Windows 9x guest setup](docs/windows-9x.md) —\n[9p filesystem](docs/filesystem.md) —\n[Linux rootfs on 9p](docs/linux-9p-image.md) —\n[Profiling](docs/profiling.md) —\n[CPU Idling](docs/cpu-idling.md)\n\n## Compatibility\n\nHere's an overview of the operating systems supported in v86:\n\n- Linux works pretty well. 64-bit kernels are not supported.\n  - [Buildroot](https://buildroot.org/) can be used to build a minimal image.\n    [humphd/browser-vm](https://github.com/humphd/browser-vm) and\n    [darin755/browser-buildroot](https://github.com/Darin755/browser-buildroot) have some useful scripts for building one.\n  - [SkiffOS](https://github.com/skiffos/SkiffOS/tree/master/configs/browser/v86) (based on Buildroot) can cross-compile a custom image.\n  - Ubuntu and other Debian derivatives works up to the latest version that supported i386 (16.04 LTS or 18.04 LTS for some variants).\n  - Alpine Linux works. An image can be built from a Dockerfile, see [tools/docker/alpine/](tools/docker/alpine/).\n  - Arch Linux 32 works. See [archlinux.md](docs/archlinux.md) for building an image.\n- ReactOS works.\n- FreeDOS, Windows 1.01 and MS-DOS run very well.\n- KolibriOS works.\n- Haiku works.\n- Android-x86 has been tested up to 4.4-r2.\n- Windows 1, 3.x, 95, 98, ME, NT and 2000 work reasonably well.\n  - In Windows 2000 and higher the PC type has to be changed from ACPI PC to Standard PC\n  - There are some known boot issues ([#250](https://github.com/copy/v86/issues/250), [#433](https://github.com/copy/v86/issues/433), [#507](https://github.com/copy/v86/issues/507), [#555](https://github.com/copy/v86/issues/555), [#620](https://github.com/copy/v86/issues/620), [#645](https://github.com/copy/v86/issues/645))\n  - See [Windows 9x guest setup](docs/windows-9x.md)\n- Windows XP, Vista and 8 work under certain conditions (see [#86](https://github.com/copy/v86/issues/86), [#208](https://github.com/copy/v86/issues/208))\n  - See [Windows NT guest setup](docs/windows-nt.md)\n- Many hobby operating systems work.\n- 9front works.\n- Plan 9 doesn't work.\n- QNX works.\n- OS/2 doesn't work.\n- FreeBSD works.\n- OpenBSD works with a specific boot configuration. At the `boot>` prompt type\n  `boot -c`, then at the `UKC>` prompt `disable mpbios` and `exit`.\n- NetBSD works only with a custom kernel, see [#350](https://github.com/copy/v86/issues/350).\n- SerenityOS works (only 32-bit versions).\n- [SkiftOS](https://skiftos.org/) works.\n\nYou can get some information on the disk images here: https://github.com/copy/images.\n\n## How to build, run and embed?\n\nYou need:\n\n- make\n- Rust with the wasm32-unknown-unknown target\n- A version of clang compatible with Rust\n- java (for Closure Compiler, not necessary when using `debug.html`)\n- nodejs (a recent version is required, v16.11.1 is known to be working)\n- To run tests: nasm, gdb, qemu-system, gcc, libc-i386 and rustfmt\n\nSee [tools/docker/test-image/Dockerfile](tools/docker/test-image/Dockerfile)\nfor a full setup on Debian or\n[WSL](https://docs.microsoft.com/en-us/windows/wsl/install).\n\n- Run `make` to build the debug build (at `debug.html`).\n- Run `make all` to build the optimized build (at `index.html`).\n- ROM and disk images are loaded via XHR, so if you want to try out `index.html`\n  locally, make sure to serve it from a local webserver. You can use `make run`\n  to serve the files using Python's http module.\n- If you only want to embed v86 in a webpage you can use `libv86.js`. For usage,\n  check out the [examples](examples/). You can download it from the [release section](https://github.com/copy/v86/releases).\n- For bundler-based setups (Vite/React/Next/Webpack), there is also an official npm package:\nhttps://www.npmjs.com/package/v86\n\n  This package was originally maintained by [@giulioz](https://github.com/giulioz) (bundler-optimized fork) and was made \"official\" for this repo by [@basicer](https://github.com/basicer) with the author's permission.\n  It is published automatically from this repository via GitHub Actions ([.github/workflows/ci.yml](.github/workflows/ci.yml), Upload release job) on pushes to `master` and uses `npm publish --provenance`.\n  \n  Install: `npm install v86`\n\n### Alternatively, to build using Docker\n\n- If you have Docker installed, you can run the whole system inside a container.\n- See `tools/docker/exec` to find the Dockerfile required for this.\n- You can run `docker build -f tools/docker/exec/Dockerfile -t v86:alpine-3.19 .` from the root directory to generate docker image.\n- Then you can simply run `docker run -it -p 8000:8000 v86:alpine-3.19` to start the server.\n- Check `localhost:8000` for hosted server.\n\n### Running via Dev Container\n\n- If you are using an IDE that supports Dev Containers, such as GitHub Codespaces, the Visual Studio Code Remote Container extension, or possibly others such as Jetbrains' IntelliJ IDEA, you can setup the development environment in a Dev Container.\n- Follow the instructions from your development environment to setup the container.\n- Run the Task \"Fetch images\" in order to download images for testing.\n\n## Testing\n\nThe disk images for testing are not included in this repository. You can\ndownload them directly from the website using:\n\n`curl --compressed --output-dir images/ --remote-name-all https://i.copy.sh/{linux.iso,linux3.iso,linux4.iso,buildroot-bzimage68.bin,TinyCore-11.0.iso,oberon.img,msdos.img,openbsd-floppy.img,kolibri.img,windows101.img,os8.img,freedos722.img,mobius-fd-release5.img,msdos622.img}`\n\nRun integration tests: `make tests`\n\nRun all tests: `make jshint rustfmt kvm-unit-test nasmtests nasmtests-force-jit expect-tests jitpagingtests qemutests rust-test tests`\n\nSee [tests/Readme.md](tests/Readme.md) for more information.\n\n## API examples\n\n- [Basic](examples/basic.html)\n- [Programatically using the serial terminal](examples/serial.html)\n- [A Lua interpreter](examples/lua.html)\n- [Two instances in one window](examples/two_instances.html)\n- [Networking between browser windows/tabs using the Broadcast Channel API](examples/broadcast-network.html)\n- [TCP Terminal (fetch-based networking)](examples/tcp_terminal.html)\n- [Saving and restoring emulator state](examples/save_restore.html)\n\nUsing v86 for your own purposes is as easy as:\n\n```javascript\nvar emulator = new V86({\n    screen_container: document.getElementById(\"screen_container\"),\n    bios: {\n        url: \"../../bios/seabios.bin\",\n    },\n    vga_bios: {\n        url: \"../../bios/vgabios.bin\",\n    },\n    cdrom: {\n        url: \"../../images/linux.iso\",\n    },\n    autostart: true,\n});\n```\n\nSee [starter.js](src/browser/starter.js).\n\n## License\n\nv86 is distributed under the terms of the Simplified BSD License, see\n[LICENSE](LICENSE). The following third-party dependencies are included in the\nrepository under their own licenses:\n\n- [`lib/softfloat/softfloat.c`](lib/softfloat/softfloat.c)\n- [`lib/zstd/zstddeclib.c`](lib/zstd/zstddeclib.c)\n- [`tests/kvm-unit-tests/`](tests/kvm-unit-tests)\n- [`tests/qemutests/`](tests/qemutests)\n- [`src/floppy.js/`](src/floppy.js) contains parts ported from qemu under the MIT license, see LICENSE.MIT.\n\n## Credits\n\n- CPU test cases via [QEMU](https://wiki.qemu.org/Main_Page)\n- More tests via [kvm-unit-tests](https://www.linux-kvm.org/page/KVM-unit-tests)\n- [zstd](https://github.com/facebook/zstd) support is included for better compression of state images\n- [Berkeley SoftFloat](http://www.jhauser.us/arithmetic/SoftFloat.html) is included to precisely emulate 80-bit floating point numbers\n- [The jor1k project](https://github.com/s-macke/jor1k) for 9p, filesystem and uart drivers\n- [WinWorld](https://winworldpc.com/) sources of some old operating systems\n- [OS/2 Museum](https://www.os2museum.com/) sources of some old operating systems\n- [ArchiveOS](https://archiveos.org/) sources of several operating systems\n\n## More questions?\n\nShoot me an email to `copy@copy.sh`. Please report bugs on GitHub.\n"
  },
  {
    "path": "bios/.gitignore",
    "content": "*.gz\n"
  },
  {
    "path": "bios/COPYING.LESSER",
    "content": "\t\t   GNU LESSER GENERAL PUBLIC LICENSE\n                       Version 3, 29 June 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n\n  This version of the GNU Lesser General Public License incorporates\nthe terms and conditions of version 3 of the GNU General Public\nLicense, supplemented by the additional permissions listed below.\n\n  0. Additional Definitions. \n\n  As used herein, \"this License\" refers to version 3 of the GNU Lesser\nGeneral Public License, and the \"GNU GPL\" refers to version 3 of the GNU\nGeneral Public License.\n\n  \"The Library\" refers to a covered work governed by this License,\nother than an Application or a Combined Work as defined below.\n\n  An \"Application\" is any work that makes use of an interface provided\nby the Library, but which is not otherwise based on the Library.\nDefining a subclass of a class defined by the Library is deemed a mode\nof using an interface provided by the Library.\n\n  A \"Combined Work\" is a work produced by combining or linking an\nApplication with the Library.  The particular version of the Library\nwith which the Combined Work was made is also called the \"Linked\nVersion\".\n\n  The \"Minimal Corresponding Source\" for a Combined Work means the\nCorresponding Source for the Combined Work, excluding any source code\nfor portions of the Combined Work that, considered in isolation, are\nbased on the Application, and not on the Linked Version.\n\n  The \"Corresponding Application Code\" for a Combined Work means the\nobject code and/or source code for the Application, including any data\nand utility programs needed for reproducing the Combined Work from the\nApplication, but excluding the System Libraries of the Combined Work.\n\n  1. Exception to Section 3 of the GNU GPL.\n\n  You may convey a covered work under sections 3 and 4 of this License\nwithout being bound by section 3 of the GNU GPL.\n\n  2. Conveying Modified Versions.\n\n  If you modify a copy of the Library, and, in your modifications, a\nfacility refers to a function or data to be supplied by an Application\nthat uses the facility (other than as an argument passed when the\nfacility is invoked), then you may convey a copy of the modified\nversion:\n\n   a) under this License, provided that you make a good faith effort to\n   ensure that, in the event an Application does not supply the\n   function or data, the facility still operates, and performs\n   whatever part of its purpose remains meaningful, or\n\n   b) under the GNU GPL, with none of the additional permissions of\n   this License applicable to that copy.\n\n  3. Object Code Incorporating Material from Library Header Files.\n\n  The object code form of an Application may incorporate material from\na header file that is part of the Library.  You may convey such object\ncode under terms of your choice, provided that, if the incorporated\nmaterial is not limited to numerical parameters, data structure\nlayouts and accessors, or small macros, inline functions and templates\n(ten or fewer lines in length), you do both of the following:\n\n   a) Give prominent notice with each copy of the object code that the\n   Library is used in it and that the Library and its use are\n   covered by this License.\n\n   b) Accompany the object code with a copy of the GNU GPL and this license\n   document.\n\n  4. Combined Works.\n\n  You may convey a Combined Work under terms of your choice that,\ntaken together, effectively do not restrict modification of the\nportions of the Library contained in the Combined Work and reverse\nengineering for debugging such modifications, if you also do each of\nthe following:\n\n   a) Give prominent notice with each copy of the Combined Work that\n   the Library is used in it and that the Library and its use are\n   covered by this License.\n\n   b) Accompany the Combined Work with a copy of the GNU GPL and this license\n   document.\n\n   c) For a Combined Work that displays copyright notices during\n   execution, include the copyright notice for the Library among\n   these notices, as well as a reference directing the user to the\n   copies of the GNU GPL and this license document.\n\n   d) Do one of the following:\n\n       0) Convey the Minimal Corresponding Source under the terms of this\n       License, and the Corresponding Application Code in a form\n       suitable for, and under terms that permit, the user to\n       recombine or relink the Application with a modified version of\n       the Linked Version to produce a modified Combined Work, in the\n       manner specified by section 6 of the GNU GPL for conveying\n       Corresponding Source.\n\n       1) Use a suitable shared library mechanism for linking with the\n       Library.  A suitable mechanism is one that (a) uses at run time\n       a copy of the Library already present on the user's computer\n       system, and (b) will operate properly with a modified version\n       of the Library that is interface-compatible with the Linked\n       Version. \n\n   e) Provide Installation Information, but only if you would otherwise\n   be required to provide such information under section 6 of the\n   GNU GPL, and only to the extent that such information is\n   necessary to install and execute a modified version of the\n   Combined Work produced by recombining or relinking the\n   Application with a modified version of the Linked Version. (If\n   you use option 4d0, the Installation Information must accompany\n   the Minimal Corresponding Source and Corresponding Application\n   Code. If you use option 4d1, you must provide the Installation\n   Information in the manner specified by section 6 of the GNU GPL\n   for conveying Corresponding Source.)\n\n  5. Combined Libraries.\n\n  You may place library facilities that are a work based on the\nLibrary side by side in a single library together with other library\nfacilities that are not Applications and are not covered by this\nLicense, and convey such a combined library under terms of your\nchoice, if you do both of the following:\n\n   a) Accompany the combined library with a copy of the same work based\n   on the Library, uncombined with any other library facilities,\n   conveyed under the terms of this License.\n\n   b) Give prominent notice with the combined library that part of it\n   is a work based on the Library, and explaining where to find the\n   accompanying uncombined form of the same work.\n\n  6. Revised Versions of the GNU Lesser General Public License.\n\n  The Free Software Foundation may publish revised and/or new versions\nof the GNU Lesser General Public License from time to time. Such new\nversions will be similar in spirit to the present version, but may\ndiffer in detail to address new problems or concerns.\n\n  Each version is given a distinguishing version number. If the\nLibrary as you received it specifies that a certain numbered version\nof the GNU Lesser General Public License \"or any later version\"\napplies to it, you have the option of following the terms and\nconditions either of that published version or of any later version\npublished by the Free Software Foundation. If the Library as you\nreceived it does not specify a version number of the GNU Lesser\nGeneral Public License, you may choose any version of the GNU Lesser\nGeneral Public License ever published by the Free Software Foundation.\n\n  If the Library as you received it specifies that a proxy can decide\nwhether future versions of the GNU Lesser General Public License shall\napply, that proxy's public statement of acceptance of any version is\npermanent authorization for you to choose that version for the\nLibrary.\n"
  },
  {
    "path": "bios/fetch-and-build-seabios.sh",
    "content": "set -e\ngit clone https://git.seabios.org/seabios.git || true\n(cd seabios && git checkout rel-1.16.2)\n\ncp seabios.config seabios/.config\nmake -C seabios\ncp seabios/out/bios.bin seabios.bin\ncp seabios/out/vgabios.bin vgabios.bin\n\ncp seabios-debug.config seabios/.config\nmake -C seabios\ncp seabios/out/bios.bin seabios-debug.bin\ncp seabios/out/vgabios.bin vgabios-debug.bin\n"
  },
  {
    "path": "bios/seabios-debug.config",
    "content": "#\n# Automatically generated file; DO NOT EDIT.\n# SeaBIOS Configuration\n#\n\n#\n# General Features\n#\n# CONFIG_COREBOOT is not set\nCONFIG_QEMU=y\n# CONFIG_CSM is not set\nCONFIG_QEMU_HARDWARE=y\nCONFIG_XEN=y\nCONFIG_THREADS=y\n# CONFIG_RELOCATE_INIT is not set\n# CONFIG_BOOTMENU is not set\nCONFIG_BOOTORDER=y\nCONFIG_HOST_BIOS_GEOMETRY=y\nCONFIG_ENTRY_EXTRASTACK=y\nCONFIG_MALLOC_UPPERMEMORY=y\nCONFIG_ROM_SIZE=0\n\n#\n# Hardware support\n#\nCONFIG_ATA=y\nCONFIG_ATA_DMA=y\nCONFIG_ATA_PIO32=y\nCONFIG_AHCI=y\nCONFIG_SDCARD=y\nCONFIG_VIRTIO_BLK=y\nCONFIG_VIRTIO_SCSI=y\nCONFIG_PVSCSI=y\nCONFIG_ESP_SCSI=y\nCONFIG_LSI_SCSI=y\nCONFIG_MEGASAS=y\nCONFIG_MPT_SCSI=y\nCONFIG_FLOPPY=y\nCONFIG_FLASH_FLOPPY=y\n# CONFIG_NVME is not set\nCONFIG_PS2PORT=y\n# CONFIG_USB is not set\nCONFIG_SERIAL=y\n# CONFIG_SERCON is not set\nCONFIG_LPT=y\nCONFIG_RTC_TIMER=y\nCONFIG_HARDWARE_IRQ=y\nCONFIG_USE_SMM=y\nCONFIG_CALL32_SMM=y\nCONFIG_MTRR_INIT=y\nCONFIG_PMTIMER=y\nCONFIG_TSC_TIMER=y\n\n#\n# BIOS interfaces\n#\nCONFIG_DRIVES=y\nCONFIG_CDROM_BOOT=y\nCONFIG_CDROM_EMU=y\nCONFIG_PCIBIOS=y\nCONFIG_APMBIOS=y\nCONFIG_PNPBIOS=y\nCONFIG_OPTIONROMS=y\nCONFIG_PMM=y\nCONFIG_BOOT=y\nCONFIG_KEYBOARD=y\nCONFIG_KBD_CALL_INT15_4F=y\nCONFIG_MOUSE=y\nCONFIG_S3_RESUME=y\nCONFIG_VGAHOOKS=y\n# CONFIG_DISABLE_A20 is not set\n# CONFIG_WRITABLE_UPPERMEMORY is not set\nCONFIG_TCGBIOS=y\n\n#\n# BIOS Tables\n#\nCONFIG_PIRTABLE=y\nCONFIG_MPTABLE=y\n# CONFIG_SMBIOS is not set\nCONFIG_ACPI=y\nCONFIG_ACPI_DSDT=y\nCONFIG_FW_ROMFILE_LOAD=y\nCONFIG_ACPI_PARSE=y\n\n#\n# VGA ROM\n#\n# CONFIG_NO_VGABIOS is not set\n# CONFIG_VGA_STANDARD_VGA is not set\n# CONFIG_VGA_CIRRUS is not set\n# CONFIG_VGA_ATI is not set\nCONFIG_VGA_BOCHS=y\n# CONFIG_VGA_GEODEGX2 is not set\n# CONFIG_VGA_GEODELX is not set\n# CONFIG_DISPLAY_BOCHS is not set\n# CONFIG_VGA_RAMFB is not set\nCONFIG_VGA_BOCHS_STDVGA=y\n# CONFIG_VGA_BOCHS_VMWARE is not set\n# CONFIG_VGA_BOCHS_QXL is not set\n# CONFIG_VGA_BOCHS_VIRTIO is not set\nCONFIG_BUILD_VGABIOS=y\nCONFIG_VGA_STDVGA_PORTS=y\nCONFIG_VGA_FIXUP_ASM=y\nCONFIG_VGA_ALLOCATE_EXTRA_STACK=y\nCONFIG_VGA_EXTRA_STACK_SIZE=512\nCONFIG_VGA_VBE=y\nCONFIG_VGA_PCI=y\nCONFIG_OVERRIDE_PCI_ID=y\nCONFIG_VGA_VID=0x1234\nCONFIG_VGA_DID=0x1111\n\n#\n# Debugging\n#\nCONFIG_DEBUG_LEVEL=8\n# CONFIG_DEBUG_SERIAL is not set\n# CONFIG_DEBUG_SERIAL_MMIO is not set\nCONFIG_DEBUG_IO=y\n"
  },
  {
    "path": "bios/seabios.config",
    "content": "#\n# Automatically generated file; DO NOT EDIT.\n# SeaBIOS Configuration\n#\n\n#\n# General Features\n#\n# CONFIG_COREBOOT is not set\nCONFIG_QEMU=y\n# CONFIG_CSM is not set\nCONFIG_QEMU_HARDWARE=y\nCONFIG_XEN=y\nCONFIG_THREADS=y\n# CONFIG_RELOCATE_INIT is not set\n# CONFIG_BOOTMENU is not set\nCONFIG_BOOTORDER=y\nCONFIG_HOST_BIOS_GEOMETRY=y\nCONFIG_ENTRY_EXTRASTACK=y\nCONFIG_MALLOC_UPPERMEMORY=y\nCONFIG_ROM_SIZE=0\n\n#\n# Hardware support\n#\nCONFIG_ATA=y\nCONFIG_ATA_DMA=y\nCONFIG_ATA_PIO32=y\nCONFIG_AHCI=y\nCONFIG_SDCARD=y\nCONFIG_VIRTIO_BLK=y\nCONFIG_VIRTIO_SCSI=y\nCONFIG_PVSCSI=y\nCONFIG_ESP_SCSI=y\nCONFIG_LSI_SCSI=y\nCONFIG_MEGASAS=y\nCONFIG_MPT_SCSI=y\nCONFIG_FLOPPY=y\nCONFIG_FLASH_FLOPPY=y\n# CONFIG_NVME is not set\nCONFIG_PS2PORT=y\n# CONFIG_USB is not set\nCONFIG_SERIAL=y\n# CONFIG_SERCON is not set\nCONFIG_LPT=y\nCONFIG_RTC_TIMER=y\nCONFIG_HARDWARE_IRQ=y\nCONFIG_USE_SMM=y\nCONFIG_CALL32_SMM=y\nCONFIG_MTRR_INIT=y\nCONFIG_PMTIMER=y\nCONFIG_TSC_TIMER=y\n\n#\n# BIOS interfaces\n#\nCONFIG_DRIVES=y\nCONFIG_CDROM_BOOT=y\nCONFIG_CDROM_EMU=y\nCONFIG_PCIBIOS=y\nCONFIG_APMBIOS=y\nCONFIG_PNPBIOS=y\nCONFIG_OPTIONROMS=y\nCONFIG_PMM=y\nCONFIG_BOOT=y\nCONFIG_KEYBOARD=y\nCONFIG_KBD_CALL_INT15_4F=y\nCONFIG_MOUSE=y\nCONFIG_S3_RESUME=y\nCONFIG_VGAHOOKS=y\n# CONFIG_DISABLE_A20 is not set\n# CONFIG_WRITABLE_UPPERMEMORY is not set\nCONFIG_TCGBIOS=y\n\n#\n# BIOS Tables\n#\nCONFIG_PIRTABLE=y\nCONFIG_MPTABLE=y\n# CONFIG_SMBIOS is not set\nCONFIG_ACPI=y\nCONFIG_ACPI_DSDT=y\nCONFIG_FW_ROMFILE_LOAD=y\nCONFIG_ACPI_PARSE=y\n\n#\n# VGA ROM\n#\n# CONFIG_NO_VGABIOS is not set\n# CONFIG_VGA_STANDARD_VGA is not set\n# CONFIG_VGA_CIRRUS is not set\n# CONFIG_VGA_ATI is not set\nCONFIG_VGA_BOCHS=y\n# CONFIG_VGA_GEODEGX2 is not set\n# CONFIG_VGA_GEODELX is not set\n# CONFIG_DISPLAY_BOCHS is not set\n# CONFIG_VGA_RAMFB is not set\nCONFIG_VGA_BOCHS_STDVGA=y\n# CONFIG_VGA_BOCHS_VMWARE is not set\n# CONFIG_VGA_BOCHS_QXL is not set\n# CONFIG_VGA_BOCHS_VIRTIO is not set\nCONFIG_BUILD_VGABIOS=y\nCONFIG_VGA_STDVGA_PORTS=y\nCONFIG_VGA_FIXUP_ASM=y\nCONFIG_VGA_ALLOCATE_EXTRA_STACK=y\nCONFIG_VGA_EXTRA_STACK_SIZE=512\nCONFIG_VGA_VBE=y\nCONFIG_VGA_PCI=y\nCONFIG_OVERRIDE_PCI_ID=y\nCONFIG_VGA_VID=0x1234\nCONFIG_VGA_DID=0x1111\n\n#\n# Debugging\n#\nCONFIG_DEBUG_LEVEL=0\n"
  },
  {
    "path": "debug.html",
    "content": "<!doctype html>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n\n<title>v86 (debug)</title>\n<meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,interactive-widget=resizes-content\">\n\n<script src=\"build/capstone-x86.min.js\"></script>\n<script src=\"build/libwabt.cjs\"></script>\n<script type=\"module\" src=\"src/browser/main.js\"></script>\n\n<link rel=\"stylesheet\" href=\"v86.css\">\n\n<div>\n    <div id=\"boot_options\">\n        <h4>Debugger</h4>\n        <button id=\"start_reactos\">ReactOS</button>\n        <button id=\"start_reactos-boot\">ReactOS (boot)</button>\n        <br>\n\n        <button id=\"start_msdos\">MS-DOS</button>\n        <button id=\"start_freedos\">FreeDOS</button>\n        <button id=\"start_freegem\">FreeDOS with FreeGEM</button>\n        <br>\n\n        <button id=\"start_windows1\">Windows 1.01</button>\n        <button id=\"start_windows30\">Windows 3.0</button>\n        <button id=\"start_windows31\">Windows 3.1</button>\n        <button id=\"start_windows95\">Windows 95</button>\n        <button id=\"start_windows95-boot\">Windows 95 (boot)</button>\n        <br>\n        <button id=\"start_windows98\">Windows 98</button>\n        <button id=\"start_windows98-boot\">Windows 98 (boot)</button>\n        <button id=\"start_windowsnt3\">Windows NT 3.1</button>\n        <button id=\"start_windowsnt4\">Windows NT 4.0</button>\n        <button id=\"start_windows2000\">Windows 2000</button>\n        <button id=\"start_windows2000-boot\">Windows 2000 (boot)</button>\n        <br>\n\n        <button id=\"start_linux26\">Linux 2.6 (Buildroot)</button>\n        <button id=\"start_linux3\">Linux 3.18 (Buildroot)</button>\n        <button id=\"start_linux4\">Linux 4.16 (Buildroot)</button>\n        <button id=\"start_buildroot\">Linux 5.6 (Buildroot)</button>\n        <button id=\"start_tinycore\">Tiny Core</button>\n        <button id=\"start_openwrt\">OpenWRT</button>\n        <br>\n        <button id=\"start_archlinux\">Arch Linux</button>\n        <button id=\"start_archlinux-boot\">Arch Linux (boot)</button>\n        <button id=\"start_dsl\">Damn Small Linux</button>\n        <br>\n\n        <button id=\"start_freebsd\">FreeBSD</button>\n        <button id=\"start_freebsd-boot\">FreeBSD (boot)</button>\n        <button id=\"start_openbsd\">OpenBSD</button>\n        <button id=\"start_netbsd\">NetBSD</button>\n        <br>\n\n        <button id=\"start_haiku\">Haiku</button>\n        <button id=\"start_haiku-boot\">Haiku (boot)</button>\n        <button id=\"start_minix\">Minix</button>\n        <button id=\"start_serenity\">SerenityOS</button>\n        <button id=\"start_qnx\">QNX</button>\n        <button id=\"start_9front\">9front</button>\n        <br>\n\n        <button id=\"start_oberon\">Oberon</button>\n        <button id=\"start_kolibrios\">KolibriOS</button>\n        <button id=\"start_solos\">Solar OS</button>\n        <button id=\"start_bootchess\">Bootchess</button>\n        <button id=\"start_helenos\">HelenOS</button>\n        <br>\n\n        <!--\n        <hr>\n        Restore state: <input type=\"file\" id=\"restore_state\">\n        -->\n\n        <br>\n        <hr>\n\n        <table>\n            <tr>\n                <td width=\"350\"><label for=\"cdrom_image\">CD image</label></td>\n                <td>\n                    <input type=\"file\" id=\"cdrom_image\">\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"floppy_image\">Floppy disk image</label></td>\n                <td><input type=\"file\" id=\"fda_image\"> or <a id=\"fda_toggle_empty_disk\">create empty floppy disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"floppy_image\">Second floppy disk image</label></td>\n                <td><input type=\"file\" id=\"fdb_image\"> or <a id=\"fdb_toggle_empty_disk\">create empty floppy disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"hda_image\">Hard disk image</label></td>\n                <td><input type=\"file\" id=\"hda_image\"> or <a id=\"hda_toggle_empty_disk\">create empty hard disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td>Second hard disk image</td>\n                <td><input type=\"file\" id=\"hdb_image\"> or <a id=\"hdb_toggle_empty_disk\">create empty hard disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"multiboot_image\">Multiboot kernel image (experimental)</label></td>\n                <td><input type=\"file\" id=\"multiboot_image\"><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"bzimage\">Kernel image (bzimage)</label></td>\n                <td><input type=\"file\" id=\"bzimage\"><br></td>\n            </tr>\n            <tr>\n                <td><label for=\"initrd\">initrd</label></td>\n                <td><input type=\"file\" id=\"initrd\"><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"bios\">BIOS</label></td>\n                <td><input type=\"file\" id=\"bios\"><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"vga_bios\">VGA BIOS</label></td>\n                <td><input type=\"file\" id=\"vga_bios\"><br></td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><small>Disk images are not uploaded to the server</small><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"memory_size\">Memory size</label></td>\n                <td>\n                    <input id=\"memory_size\" type=\"number\" value=\"128\" min=\"16\" max=\"2048\" step=\"16\"> MB<br>\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"vga_memory_size\">Video Memory size</label></td>\n                <td>\n                    <input id=\"vga_memory_size\" type=\"number\" value=\"8\" min=\"1\" max=\"128\" step=\"1\"> MB<br>\n                </td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"relay_url\">Networking proxy (leave blank to disable)</label></td>\n                <td>\n                    <input id=\"relay_url\" type=\"text\" value=\"wss://relay.widgetry.org/\">\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"net_device_type\">Network Device Type</label></td>\n                <td>\n                    <select id=\"net_device_type\">\n                        <option value=\"ne2k\">ne2k</option>\n                        <option value=\"virtio\">virtio</option>\n                    </select>\n                 </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"mtu\">Network MTU (only used for virtio)</label></td>\n                <td>\n                    <input id=\"mtu\" type=\"number\" value=\"1500\" min=\"68\" max=\"65535\" step=\"1\"> B<br>\n                 </td>\n            </tr>\n\n\n            <tr>\n                <td colspan=\"2\"><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"disable_audio\">Disable audio</label></td>\n                <td>\n                    <input id=\"disable_audio\" type=\"checkbox\"><br>\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"acpi\">Enable ACPI (experimental)</label></td>\n                <td>\n                    <input id=\"acpi\" type=\"checkbox\"><br>\n                </td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"boot_order\">Boot order</label></td>\n                <td>\n                    <select id=\"boot_order\">\n                        <option value=\"0\">Auto</option>\n                        <option value=\"213\">CD / Floppy / Hard Disk</option>\n                        <option value=\"123\">CD / Hard Disk / Floppy</option>\n                        <option value=\"231\">Floppy / CD / Hard Disk</option>\n                        <option value=\"321\">Floppy / Hard Disk / CD</option>\n                        <option value=\"312\">Hard Disk / Floppy / CD</option>\n                        <option value=\"132\">Hard Disk / CD / Floppy</option>\n                    </select>\n                 </td>\n            </tr>\n        </table>\n\n        <br>\n        <button id=\"start_emulation\">Start Emulation</button>\n        <br>\n        <br>\n    </div>\n\n    <div id=\"runtime_options\" style=\"display: none\">\n        <button id=\"dump_regs\">Dump Registers</button>\n        <button id=\"dump_gdt\">Dump GDT/LDT</button>\n        <button id=\"dump_idt\">Dump IDT</button>\n        <button id=\"dump_pt\">Dump page tables</button>\n        <button id=\"dump_log\">Dump log</button>\n        <br>\n\n        <button id=\"run\">Pause</button>\n        <button id=\"reset\">Reset</button>\n        <button id=\"exit\">Exit</button>\n        <button id=\"ctrlaltdel\">Send Ctrl-Alt-Del</button>\n        <button id=\"alttab\">Send Alt-Tab</button>\n        <button id=\"get_fda_image\">Get floppy image</button>\n        <button id=\"get_fdb_image\">Get second floppy image</button>\n        <button id=\"get_hda_image\">Get hard disk image</button>\n        <button id=\"get_hdb_image\">Get second hard disk image</button>\n        <button id=\"get_cdrom_image\">Get CD-ROM image</button>\n        <button id=\"change_fda_image\">Insert floppy image</button>\n        <button id=\"change_fdb_image\">Insert second floppy image</button>\n        <button id=\"change_cdrom_image\">Insert CD image</button>\n        <button id=\"save_state\">Save State</button>\n        <button id=\"load_state\">Load State</button>\n        <button id=\"memory_dump\">Memory Dump</button>\n        <button id=\"capture_network_traffic\">Capture network traffic</button>\n        <button id=\"toggle_mouse\">Disable mouse</button>\n        <button id=\"lock_mouse\">Lock mouse</button>\n        <button id=\"fullscreen\">Go fullscreen</button>\n        <button id=\"take_screenshot\">Take screenshot</button>\n        <button id=\"mute\">Mute</button>\n        <button id=\"toggle_theatre\">Enable theatre mode</button>\n        <button style=\"display: none\" id=\"toggle_zoom_to_fit\">Enable zoom to fit</button>\n\n        <label>\n            Scale:\n            <input type=\"number\" min=\"0.25\" step=\"0.25\" value=\"1.0\" id=\"scale\" style=\"width: 50px\">\n        </label>\n    </div>\n\n    <pre style=\"margin: 0\" id=\"log_levels\"></pre>\n    <pre style=\"display: none\" id=\"loading\"></pre>\n    <br>\n</div>\n\n<div id=\"screen_container\" style=\"display: none\">\n    <div id=\"screen\"></div>\n    <canvas id=\"vga\"></canvas>\n\n    <div style=\"position: absolute; top: 0; z-index: 10\">\n        <textarea class=\"phone_keyboard\"></textarea>\n    </div>\n</div>\n\n\n<div id=\"runtime_infos\" style=\"display: none\">\n    Running: <span id=\"running_time\">0s</span> <br>\n    Speed: <span id=\"speed\">0</span> mIPS<br>\n    Avg speed: <span id=\"avg_speed\">0</span> mIPS<br>\n    <br>\n    <div id=\"info_storage\" style=\"display: none\">\n        <b>IDE device<span id=\"ide_type\"></span></b><br>\n        Sectors read: <span id=\"info_storage_sectors_read\">0</span><br>\n        Bytes read: <span id=\"info_storage_bytes_read\">0</span><br>\n        Sectors written: <span id=\"info_storage_sectors_written\">0</span><br>\n        Bytes written: <span id=\"info_storage_bytes_written\">0</span><br>\n        Status: <span id=\"info_storage_status\"></span><br>\n        <br>\n    </div>\n    <div id=\"info_filesystem\" style=\"display: none\">\n        <b>9p Filesystem</b><br>\n        Bytes read: <span id=\"info_filesystem_bytes_read\">0</span><br>\n        Bytes written: <span id=\"info_filesystem_bytes_written\">0</span><br>\n        <div style=\"white-space: nowrap; overflow-x: hidden\">Last file: <span id=\"info_filesystem_last_file\"></span></div>\n        Status: <span id=\"info_filesystem_status\"></span><br>\n        <br>\n    </div>\n    <div id=\"info_network\" style=\"display: none\">\n        <b>Network</b><br>\n        Bytes received: <span id=\"info_network_bytes_received\">0</span><br>\n        Bytes transmitted: <span id=\"info_network_bytes_transmitted\">0</span><br>\n        <br>\n    </div>\n    <b>VGA</b><br>\n    Mode: <span id=\"info_vga_mode\"></span><br>\n    Resolution: <span id=\"info_res\">-</span><br>\n    <br>\n    Mouse: <span id=\"info_mouse_enabled\">No</span><br>\n</div>\n\n<div id=\"filesystem_panel\" style=\"display: none\">\n    <label>\n        Send files to emulator<br>\n        <input type=\"file\" id=\"filesystem_send_file\" multiple>\n    </label>\n    <br><br>\n    <label>\n        Get file from emulator<br>\n        <input type=\"text\" id=\"filesystem_get_file\" placeholder=\"Absolute path\">\n    </label>\n</div>\n\n<div id=\"debug_panel\" style=\"display: none\">\n</div>\n\n<br style=\"clear: both\"><br>\n\n<textarea spellcheck=\"false\" cols=\"40\" rows=\"12\" id=\"serial\" style=\"display: none\">\n</textarea>\n\n<div id=\"terminal\"></div>\n\n<button id=\"toggle_ui\" style=\"display: none\">Hide UI</button>\n<div id=\"theatre_background\" style=\"display: none\"></div>\n"
  },
  {
    "path": "docs/.gitignore",
    "content": "node_modules\n"
  },
  {
    "path": "docs/archlinux.md",
    "content": "See [Alpine setup](../tools/docker/alpine/Readme.md) for a more reliable and\nfaster way to automatically build Linux images for v86.\n\n(This document partly also applies to other Linuxes)\n\nChoosing an installer ISO\n-------------------------\n\nDownload Arch Linux 32 from  https://archlinux32.org.\n\nBasic installation using QEMU\n-----------------------\n\nInstalling Arch Linux with these instructions will result in a raw disk image that can be booted by v86.\n\n```sh\n# fetch archlinux32 installer\nwget https://mirror.archlinux32.org/archisos/archlinux32-2021.12.01-i686.iso\n\n# Create a 10 gigabyte disk image. If you intend to pacstrap only 'base' then 1.5G should be fine also.\nqemu-img create arch.img 10G\n\n# Follow the normal installation process (you can add accel=kvm if your system supports it to speed up the installation)\nqemu-system-x86_64 -m 256 -drive file=arch.img,format=raw -cdrom archlinux32-2021.12.01-i686.iso\n```\n\nFor keyboard support, it is necessary to open /etc/mkinitcpio.conf and edit the following line:\n\n```sh\nMODULES=\"atkbd i8042\"\n```\n\nFor the changes to take effect, you need to regenerate the initial ramdisk with `mkinitcpio -p linux`\n\nThe resulting `arch.img` file is a bootable disk image for v86.\n\nScripting image creation for v86\n--------------------------------\n\nInstalling the ISO by hand takes a long time if you intend to recreate the image many times. There are various reasons why you might want to do this more than once. For example: because the emulator is slow you might want to compile any new software release in QEMU which is much faster and then use the resulting image in v86 instead of making the emulator compile the software. Another reason is that the build progress potentially takes long and if you want to do automated builds in parallel to find out what configurations do and don't work you can just throw more computing power at the problem in order to solve it. This example requires that you have `packer`, `qemu` and `kpartx` installed.\n\n### Creating a packer template\n\n[Packer](https://www.packer.io/docs/builders/qemu.html) is a tool that lets you boot an ISO in any of multiple emulators (so QEMU in our case) and send pre-scripted keystrokes to bootstrap an SSH server. Once the SSH connection is established a script can be started for further provisioning.\n\nCreate a template for automating the base installation:\n```sh\nmkdir -p packer\ncat > packer/template.json << 'EOF'\n{\n  \"provisioners\": [\n    {\n      \"type\": \"shell\",\n      \"override\": {\n        \"qemu\": {\n          \"scripts\": [\"scripts/provision.sh\"]\n        }\n      }\n    }\n  ],\n  \"builders\": [\n    {\n      \"accelerator\": \"kvm\",\n      \"type\": \"qemu\",\n      \"boot_command\": [\n        \"<enter><wait30><enteropenssl passwd help<wait10>\",\n        \"dhcpcd<enter><wait5>\",\n        \"echo root:root | chpasswd<enter><wait5>\",\n        \"systemctl start sshd<enter>\"\n      ],\n      \"headless\": true,\n      \"boot_wait\": \"10s\",\n      \"disk_size\": 1500,\n      \"disk_interface\": \"ide\",\n      \"iso_url\": \"https://mirror.archlinux32.org/archisos/archlinux32-2021.12.01-i686.iso\",\n      \"iso_checksum\": \"90c6f5aecb095d5578f6c9970539da7c5e9324ec\",\n      \"iso_checksum_type\": \"sha1\",\n      \"ssh_wait_timeout\": \"120s\",\n      \"ssh_pty\": true,\n      \"ssh_username\": \"root\",\n      \"ssh_password\": \"root\",\n      \"ssh_port\": 22,\n      \"format\": \"raw\",\n      \"vm_name\": \"archlinux\",\n      \"disk_detect_zeroes\": \"unmap\",\n      \"memory\": 2048,\n      \"vnc_bind_address\": \"0.0.0.0\"\n    }\n  ]\n}\nEOF\n```\n\nYou can tweak the options a bit to match your situation. For debugging, you can set `headless` to `false`. That will show you the VNC connection instead of running the `boot_command` in the background. For a `base` pacstrap, using a 2 GB disk image should be sufficient. The `raw` disk format is important. v86 does not read qcow2 images, only raw disk images. If your system does not support KVM (the default accelerator), you can change `\"accelerator\": \"none\"` to the settings, in macos you may use `\"accelerator\": \"hvf\"`. Other accelerator options can be found [here](https://www.packer.io/docs/builders/qemu.html#accelerator).\n\nAfter gaining SSH connectivity to the VM, packer will run the `scripts/provisioning.sh` script in the guest.\n\n### Creating the Arch Linux installation script\n\nCreate a script for your Arch Linux installation. This runs in the live Arch Linux environment, so you need to partition the disk, do a pacstrap, and install a bootloader.\n```sh\nmkdir -p packer/scripts\n### Write your own or copy paste the example below\nvim packer/scripts/provision.sh\n```\n\nAn example script to install Arch Linux with the root mounted using the 9p network filesystem:\n```sh\n#!/bin/bash\necho \"Creating a GPT partition on /dev/sda1\"\necho -e \"g\\nn\\n\\n\\n\\nw\" | fdisk /dev/sda\n\n# In case you might want to create a DOS partition instead. It doesn't really matter.\n#echo \"Creating a DOS partition on /dev/sda1\"\n#echo -e \"o\\nn\\np\\n1\\n\\n\\nw\" | fdisk /dev/sda\n\necho \"Formatting /dev/sda1 to ext4\"\nmkfs -t ext4 /dev/sda1\n\necho \"Mounting new filesystem\"\nmount -t ext4 /dev/sda1 /mnt\n\necho \"Create pacman package cache dir\"\nmkdir -p /mnt/var/cache/pacman/pkg\n\n# We don't want the pacman cache to fill up the image. After reboot whatever tarballs pacman has cached are gone.\necho \"Mount the package cache dir in memory so it doesn't fill up the image\"\nmount -t tmpfs none /mnt/var/cache/pacman/pkg\n\necho \"Updating archlinux-keyring\"\npacman -Sy archlinux-keyring --noconfirm\n\n# uncomment to remove signing if unable to resolve signing errors\nsed -i 's/SigLevel.*/SigLevel = Never/g' /etc/pacman.conf\n\n# Install the Arch Linux base system, feel free to add packages you need here\necho \"Performing pacstrap\"\npacstrap -i /mnt base linux dhcpcd curl openssh --noconfirm\n\necho \"Writing fstab\"\ngenfstab -p /mnt >> /mnt/etc/fstab\n\n# When the Linux boots we want it to automatically log in on tty1 as root\necho \"Ensuring root autologin on tty1\"\nmkdir -p /mnt/etc/systemd/system/getty@tty1.service.d\ncat << 'EOF' > /mnt/etc/systemd/system/getty@tty1.service.d/override.conf\n[Service]\nExecStart=\nExecStart=-/usr/bin/agetty --autologin root --noclear %I $TERM\nEOF\n\n# This is the tricky part. The current root will be mounted on /dev/sda1 but after we reboot\n# it will try to mount root during boot using the 9p network filesystem. This means the emulator\n# will request all files over the network using XMLHttpRequests from the server. This is great\n# because then you only need to provide the client with a saved state (the memory) and the\n# session will start instantly and load needed files on the fly. This is fast and it saves bandwidth.\necho \"Ensuring root is remounted using 9p after reboot\"\nmkdir -p /mnt/etc/initcpio/hooks\ncat << 'EOF' > /mnt/etc/initcpio/hooks/9p_root\nrun_hook() {\n    mount_handler=\"mount_9p_root\"\n}\n\nmount_9p_root() {\n    msg \":: mounting '$root' on real root (9p)\"\n    # Note the host9p. We won't mount /dev/sda1 on root anymore,\n    # instead we mount the network filesystem and the emulator will\n    # retrieve the files on the fly.\n    if ! mount -t 9p host9p \"$1\"; then\n        echo \"You are now being dropped into an emergency shell.\"\n        launch_interactive_shell\n        msg \"Trying to continue (this will most likely fail) ...\"\n    fi\n}\nEOF\n\necho \"Adding initcpio build hook for 9p root remount\"\nmkdir -p /mnt/etc/initcpio/install\ncat << 'EOF' > /mnt/etc/initcpio/install/9p_root\n#!/bin/bash\nbuild() {\n\tadd_runscript\n}\nEOF\n\n# We need to load some modules into the kernel for it to play nice with the emulator\n# The atkbd and i8042 modules are for keyboard input in the browser. If you do not\n# want to use the network filesystem you only need these. The 9p, 9pnet and 9pnet_virtio\n# modules are needed for being able to mount 9p network filesystems using the emulator.\necho \"Configure mkinitcpio for 9p\"\nsed -i 's/MODULES=()/MODULES=(atkbd i8042 libps2 serio serio_raw psmouse virtio_pci virtio_pci_modern_dev 9p 9pnet 9pnet_virtio fscache netfs)/g' /mnt/etc/mkinitcpio.conf\n\n# Because we want to mount the root filesystem over the network during boot, we need to\n# hook into initcpio. If you do not want to mount the root filesystem during boot but\n# only want to mount a 9p filesystem later, you can leave this out. Once the system\n# has been booted you should be able to mount 9p filesystems with mount -t 9p host9p /blabla\n# without this hook.\nsed -i 's/fsck\"/fsck 9p_root\"/g' /mnt/etc/mkinitcpio.conf\n\n# enable ssh password auth and root login\nsed -i 's/#PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config\nsed -i 's/#PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config\n\necho \"Writing the installation script\"\ncat << 'EOF' > /mnt/bootstrap.sh\n#!/usr/bin/bash\necho \"Re-generate initial ramdisk environment\"\nmkinitcpio -p linux\n\n# uncomment to remove signing if you are unable to resolve signing errors otherwise\nsed -i 's/SigLevel.*/SigLevel = Never/g' /etc/pacman.conf\n\npacman -S --noconfirm syslinux gptfdisk\nsyslinux-install_update -i -a -m\n\n# disabling ldconfig to speed up boot (to remove Rebuild dynamic linker cache...)\n# you may want to comment this out\necho \"Disabling ldconfig service\"\nsystemctl mask ldconfig.service\n\nsync\nEOF\n\necho \"Chrooting and bootstrapping the installation\"\narch-chroot /mnt bash bootstrap.sh\n\n\ncat << 'EOF' > /mnt/boot/syslinux/syslinux.cfg\n# Config file for Syslinux -\n# /boot/syslinux/syslinux.cfg\n#\n# Comboot modules:\n#   * menu.c32 - provides a text menu\n#   * vesamenu.c32 - provides a graphical menu\n#   * chain.c32 - chainload MBRs, partition boot sectors, Windows bootloaders\n#   * hdt.c32 - hardware detection tool\n#   * reboot.c32 - reboots the system\n#\n# To Use: Copy the respective files from /usr/lib/syslinux to /boot/syslinux.\n# If /usr and /boot are on the same file system, symlink the files instead\n# of copying them.\n#\n# If you do not use a menu, a 'boot:' prompt will be shown and the system\n# will boot automatically after 5 seconds.\n#\n# Please review the wiki: https://wiki.archlinux.org/index.php/Syslinux\n# The wiki provides further configuration examples\n\nDEFAULT arch\nPROMPT 0        # Set to 1 if you always want to display the boot: prompt\nTIMEOUT 100\n\n# Menu Configuration\n# Either menu.c32 or vesamenu32.c32 must be copied to /boot/syslinux\nUI menu.c32\n#UI vesamenu.c32\n\n# Refer to http://syslinux.zytor.com/wiki/index.php/Doc/menu\nMENU TITLE Arch Linux\n#MENU BACKGROUND splash.png\nMENU COLOR border       30;44   #40ffffff #a0000000 std\nMENU COLOR title        1;36;44 #9033ccff #a0000000 std\nMENU COLOR sel          7;37;40 #e0ffffff #20ffffff all\nMENU COLOR unsel        37;44   #50ffffff #a0000000 std\nMENU COLOR help         37;40   #c0ffffff #a0000000 std\nMENU COLOR timeout_msg  37;40   #80ffffff #00000000 std\nMENU COLOR timeout      1;37;40 #c0ffffff #00000000 std\nMENU COLOR msg07        37;40   #90ffffff #a0000000 std\nMENU COLOR tabmsg       31;40   #30ffffff #00000000 std\n\n# boot sections follow\n#\n# TIP: If you want a 1024x768 framebuffer, add \"vga=773\" to your kernel line.\n#\n#-*\n\nLABEL arch\n    MENU LABEL Arch Linux 9p\n    LINUX ../vmlinuz-linux\n    APPEND root=/dev/sda1 rw quiet\n    INITRD ../initramfs-linux.img\n\nLABEL arch2\n    MENU LABEL Arch Linux Disk\n    LINUX ../vmlinuz-linux\n    APPEND root=/dev/sda1 rw quiet disablehooks=9p_root\n    INITRD ../initramfs-linux.img\n\nLABEL hdt\n        MENU LABEL HDT (Hardware Detection Tool)\n        COM32 hdt.c32\n\nLABEL reboot\n        MENU LABEL Reboot\n        COM32 reboot.c32\n\nLABEL poweroff\n        MENU LABEL Poweroff\n        COM32 poweroff.c32\nEOF\numount -R /mnt\n```\n\nWith the packer template and the script you have enough to create an image that can be booted by v86. But because this example script installs an Arch Linux that wants to mount root over the network with 9p, we need to host that filesystem first. If you do not want to use 9p, you can just run `(cd packer && packer build -force template.json)` to build the image.\n\n### Creating the 9p filesystem\n\nNow that we have an image that contains a filesystem, we can convert that filesystem into something we can host on the webserver together with the v86 library.\n\nTo do so, we need to mount the image once and create a json mapping of the filesystem. The following script shows how to map the filesystem in an automated fashion.\n\nCreate a script to builds the image and then creates v86 compatible artifacts:\n```sh\nvim build.sh\n```\n\nExample script:\n\n```sh\n#!/bin/sh\n\nSRC=packer\nTARGET=output\n\n# build the boxfile from the iso\n(cd $SRC && sudo PACKER_LOG=1 PACKER_LOG_PATH=\"./packer.log\" packer build -force template.json)\n\n# test if there is a boxfile where we expected it\nif [ ! -f $SRC/output-qemu/archlinux ]; then\n    echo \"Looks like something went wrong building the image, maybe try again?\"\n    exit 1\nfi;\n\n# clean up any previous loops and mounts\necho \"Making sure mountpoint is empty\"\nLOOP_DEV=$(sudo losetup -f)\n\nsudo umount diskmount -f || /bin/true\nsudo kpartx -d $LOOP_DEV || /bin/true\nsudo losetup -d $LOOP_DEV || /bin/true\n\n# mount the generated raw image, we do that so we can create\n# a json mapping of it and copy it to host on the webserver\nmkdir -p diskmount\necho \"Mounting the created image so we can convert it to a p9 image\"\nsudo losetup $LOOP_DEV $SRC/output-qemu/archlinux\nsudo kpartx -a $LOOP_DEV\nsudo mount /dev/mapper/$(basename $LOOP_DEV)p1 diskmount\n\n# make images dir\nmkdir -p $TARGET\nmkdir -p $TARGET/images\nmkdir -p $TARGET/images/arch\n\n# map the filesystem to json with fs2json\nsudo ./tools/fs2json.py --out $TARGET/images/fs.json diskmount\nsudo ./tools/copy-to-sha256.py diskmount $TARGET/images/arch\n\n# copy the filesystem and chown to nonroot user\necho \"Copying the filesystem to $TARGET/arch\"\nmkdir $TARGET/arch -p\nsudo rsync -q -av diskmount/ $TARGET/arch\nsudo chown -R $(whoami):$(whoami) $TARGET/arch\n\n# clean up mount\necho \"Cleaning up mounts\"\nsudo umount diskmount -f\nsudo kpartx -d $LOOP_DEV\nsudo losetup -d $LOOP_DEV\n\n# Move the image to the images dir\nsudo mv $SRC/output-qemu/archlinux $TARGET/images/arch.img\n```\n\nGiven that the packer template and provision.sh is rooted at `packer` (adjust the value of `$SRC` otherwise), run the `build.sh` at root of your `v86` repo:\n\n```\nchmod +x build.sh\n./build.sh\n```\n\nGenerated artifacts are now available for serving from `output`.\n\n### Using the created artifacts in v86\n\nNow that we have everything we need to host a server that serves an Arch Linux environment over the network.\n\nCreate a checkout of v86 and run `make build/libv86.js`.\nWe can then edit `examples/arch.html`, we have two options:\n\n1. Boot Arch Linux from the 9p filesystem (generated .bin artifacts at `/output/images/arch`):\n\n  ```sh\n  filesystem: {\n    baseurl: \"../output/images/arch/\",\n    basefs: \"../output/images/fs.json\",\n  },\n\n  bzimage_initrd_from_filesystem: true,\n\n  cmdline: [\n    \"rw\",\n    \"root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose\",\n  ].join(\" \"),\n\n  acpi: false,\n  autostart: true,\n  ```\n2. Boot the Arch Linux from the qemu raw disk image:\n\n  ```sh\n  hda: {\n      url: \"../output/images/arch.img\",\n      # set to true if you want to load it asynchrously during runtime (for this option we need to run a webserver that supports the Range header)\n      # NOTE: async: false is slow but proved to be more realiable\n      async: false,\n\n      # This needs to be the size of the raw disk.\n      size: 1.5 * 1024 * 1024 * 1024,\n      # See the `disk_size` item in the packer template.\n  },\n\n  acpi: false,\n  autostart: true,\n  ```\n\nNext, we need a webserver that supports the Range header. For example [this extension of the SimpleHTTPServer](https://github.com/smgoller/rangehttpserver). At your `v86` root, run:\n\n```sh\nwget https://raw.githubusercontent.com/smgoller/rangehttpserver/master/RangeHTTPServer.py\npython2 RangeHTTPServer.py\n```\n\nNow that the webserver is running, point your browser to `http://localhost:8000/examples/arch.html`. Wait for the Linux to boot. When the system is up, click 'Save state to file'. Your browser will download a `v86state.bin` file. Copy that file to `/your/v86/dir/images`. You can then edit `examples/arch.html` again and add a 'state' key to the `V86` array.\n\n```sh\ninitial_state: {\n    \"url\": \"http://localhost:8000/images/v86state.bin\",\n},\n```\n\nIf you refresh `http://localhost:8000/examples/arch.html` you will see that the state is restored instantly and all required files are loaded over the network on the fly.\n\n### Networking\n\nThe emulator can emulate a network card. For more information [look at the networking documentation](https://github.com/copy/v86/blob/master/docs/networking.md). To set up networking in the VM, add the following item to the `V86` array in the `examples/arch.html` file:\n```sh\nnetwork_relay_url: \"ws://localhost:8080/\",\n```\n\nThis will make the emulator try to connect to a [WebSockets proxy](https://github.com/benjamincburns/websockproxy). Running the proxy is very easy if you use the Docker container.\n\n```sh\nsudo docker run --privileged -p 8080:80 --name relay bennottelling/websockproxy\n```\n**NOTE:** original `benjamincburns/jor1k-relay:latest` has throttling built-in by default which will degrade the networking. `bennottelling/websockproxy` has this throttling removed via [websockproxy/issues/4#issuecomment-317255890](https://github.com/benjamincburns/websockproxy/issues/4#issuecomment-317255890).\n\nYou can check if the relay is running correctly by going to `http://localhost:8080/` in your browser. There you should see a message that reads `Can \"Upgrade\" only to \"Websocket\".`.\n\nNow you should be able to get network connectivity in the virtual machine. If you are restoring from a saved state, you might need to first run:\n```sh\nip link set enp0s5 down\nrmmod ne2k-pci\n```\n\nTo bring the network up, run:\n```sh\nmodprobe ne2k-pci\ndhcpcd -w4 enp0s5\n```\n\nIt might take a while for a carrier to become available on the interface. If the `dhcpcd` command fails shortly after booting, wait a bit and try again a bit later. If you are using the 9p network filesystem you can use the developer tools networking tab (in chrome) to get a sense of what is going on by looking at the files that are being downloaded.\n\nWhen the network is up you should be able to curl a website. To check, run `curl icanhazip.com`. There you should see the public IP of the machine running the proxy.\n\nYou can't do inbound traffic into the VM with the websockproxy Docker container because it uses a basic NAT. To SSH into the VM running in the browser, you can create a reverse SSH tunnel to expose the SSH port of the sshd in the VM to the outside world. You may need to start `sshd` first, it may also be reasonable to change root password:\n\n```sh\npasswd root\nsystemctl start sshd\n```\n\nthen create a reverse SSH tunnel:\n\n```sh\n# This will create a port 1122 on the example.com server\n# which forwards to the SSH in the VM\nssh root@example.com -R 1122:localhost:22\n```\n\nNow on the `example.com` server you should be able to SSH into your browser tab by running `ssh root@localhost -p 1122`.\n"
  },
  {
    "path": "docs/cpu-idling.md",
    "content": "Some operating systems don't support `hlt` instruction, because of this, the CPU spin loops instead of idling.\nHere are some solutions for different OSes:\n\n## MS-DOS (using DOSIdle)\n1. Download `DOSID251.zip` from https://www.vogons.org/viewtopic.php?p=438763#p438763\n2. Unzip DOSIDLE.EXE from archive in any location (recommended to root of C:).\n3. Run `edit C:\\autoexec.bat`\n4. Add to file: `C:\\path\\to\\dosidle.exe`\n5. Save changes (*press Alt + F and x*) and restart the VM.\n\n**Note:** To hide output when starting DOSIdle, change `C:\\path\\to\\dosidle.exe` to `C:\\path\\to\\dosidle.exe > nul` on step №4.\n\n## FreeDOS ([source](https://narkive.com/UGrcO8wU.2))\n1. Run `edit C:\\fdconfig.sys` (or `edit C:\\config.sys`)\n2. Add to file: `IDLEHALT=1`\n3. Save changes (*press Alt + F and x*) and restart FreeDOS.\n\n## Windows 9x (using AmnHLT)\n1. Download `amnhltm.zip` from http://toogam.com/software/archive/drivers/cpu/cpuidle/amnhltm.zip ([mirror](https://web.archive.org/web/20060212132151/http://www.user.cityline.ru/~maxamn/amnhltm.zip))\n2. Unzip the archive in any location.\n3. **Note**: If you have installed VBE9x, restart Windows, press F8 on boot, select *Command prompt only*, run `cd C:\\path\\to\\amnhlt\\`, and follow to the next step.\n4. Run `AMNHLT.BAT`\n5. Restart Windows, and AmnHLT will start automatically on next boot (you can safely delete archive and unpacked folder).\n\n## Windows 98+ and Unix-like\nThese systems are already supports `hlt`, no further action is required.\n"
  },
  {
    "path": "docs/filesystem.md",
    "content": "A 9p filesystem is supported by v86, using a virtio transport. There are several\nways it can be set up.\n\n### Guest mount\n\nIn all cases, the filesystem is mounted in the guest system using the `9p`\nfilesystem type and the `host9p` device tag. Typically you want to be specific\nwith the version and transport options:\n\n```sh\nmount -t 9p -o trans=virtio,version=9p2000.L host9p /mnt/9p/\n```\n\nHere are kernel arguments you can use to boot directly off the 9p filesystem:\n\n```\nrw root=host9p rootfstype=9p rootflags=trans=virtio,version=9p2000.L\n```\n\nThe `aname` option can be used to pick the directory from 9p to mount. The `rw`\nargument makes this a read-write root filesystem.\n\n\n### JSON/HTTP Filesystem\n\nThis is the standard way to setup the 9p filesystem. It loads files over\nHTTP on-demand into an in-memory filesystem in JS. This allows files to be\nexchanged with the guest OS. See `create_file` and `read_file` in\n[`starter.js`](https://github.com/copy/v86/blob/master/src/browser/starter.js).\n\nThis mode is enabled by passing the following options to `V86`:\n\n```javascript\nfilesystem: {\n    basefs: \"../9p/fs.json\",\n    baseurl: \"../9p/base/\",\n}\n```\n\nHere, `basefs` is a json file created using\n[fs2json.py](tools/fs2json.py) and the `baseurl` directory is created using\n[copy-to-sha256.py](tools/copy-to-sha256.py).\n\nIf `basefs` and `baseurl` are omitted, an empty 9p filesystem is created. Unless\nyou configure one of the alternative modes.\n\n\n### Function Handler\n\nYou can handle 9p messages directly in JavaScript yourself by providing a\nfunction as `handle9p` under `filesystem`:\n\n```javascript\nfilesystem: {\n    handle9p: async (reqBuf, reply) => {\n        // reqBuf is a Uint8Array of the entire request frame.\n        // you can parse these bytes using a library or reading the 9p spec.\n        // once you formulate a response, you send the reply frame as a\n        // Uint8Array by passing it to reply: reply(respBuf)\n    }\n}\n```\n\nThis allows you to implement a 9p server or custom proxy in JS. However, this\nfilesystem will not be cached (unless cached in the guest OS), functions like\n`create_file` and `read_file` will not be available, and you will be responsible\nfor keeping its state in sync with any machine save states.\n\n\n### WebSocket Proxy\n\nYou can also back the 9p virtio filesystem with a 9p server over WebSocket by\nproviding a WS proxy URL:\n\n```javascript\nfilesystem: {\n    proxy_url: \"ws://localhost:8080/\"\n}\n```\n\nSimlar to using `handle9p`, this filesystem will not be available in JS and\nwill need to be re-mounted after restoring state.\n\nThe WS proxy just needs to hand off messages with a connection to a normal 9p\nserver. Each binary WebSocket message is the full buffer of a request or a\nreply.\n\nTo implement, request message bytes can just be sent directly to the 9p\nconnection, but the 9p reply stream needs to be buffered into a single binary\nWebSocket message. The proxy must at least parse the first 4 bytes to get the\nmessage size and use it to buffer a full message before sending over WebSocket.\n\nThe [wanix](https://github.com/tractordev/wanix) CLI has a `serve` command that\nnot only serves a directory over HTTP, but also over 9P via WebSocket. You can\nsee how it [implements a proxy][1] in Go.\n\n[1]: https://github.com/tractordev/wanix/blob/main/cmd/wanix/serve.go#L117-L177\n"
  },
  {
    "path": "docs/how-it-works.md",
    "content": "Here's an overview of v86's workings. For details, check the\n[source](https://github.com/copy/v86/tree/master/src).\n\nThe major limitations of WebAssembly are (for the purpose of making emulators with jit):\n\n- structured control flow (no arbitrary jumps)\n- no control over registers (you can't keep hardware registers in wasm locals across functions)\n- no mmap (paging needs to be fully emulated)\n- no patching\n- module generation is fairly slow, but at least it's asynchronous, so other things can keep running\n- there is some memory overhead per module, so you can't generate more than a few thousand\n\nv86 has an interpreted mode, which collects entry points (targets of function\ncalls and indirect jumps). It also measures the hotness per page, so that\ncompilation is focused on code that is often executed. Once a page is\nconsidered hot, code is generated for the entire page and up to `MAX_PAGES`\nthat are directly reachable from it.\n\nv86 generates a single function with a big switch statement (brtable), to\nensure that all functions and targets of indirect jumps are reachable from\nother modules. The remaining control flow is handled using the \"stackifier\"\nalgorithm (well-explained in\n[this blog post](https://medium.com/leaningtech/solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2)).\nAt the moment, there is no linking of wasm modules. The current module is\nexited, and the main loop detects if a new module can be entered.\n\nIn practice, I found that browsers don't handle this structure (deep brtables,\nwith locals being used across the entire function) very well, and `MAX_PAGES`\nhas to be set to fairly low, otherwise memory usage blows up. It's likely that\nimprovements are possible (generating fewer entry points, splitting code across\nmultiple functions).\n\nCode-generation happens in two passes. The first pass finds all basic block\nboundaries, the second generates code for each basic block. Instruction\ndecoding is generated by a [set of\nscripts](https://github.com/copy/v86/tree/master/gen) from a [table of\ninstructions](https://github.com/copy/v86/blob/master/gen/x86_table.js). It's\nalso used to [generate\ntests](https://github.com/copy/v86/blob/master/tests/nasm/create_tests.js).\n\nTo handle paging, v86 generates code similar to this (see `gen_safe_read`):\n\n```\nentry <- tlb[addr >> 12 << 2]\nif entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0xFFC - bytes: goto fast\nentry <- safe_read_jit_slow(addr, instruction_pointer)\nif page_fault: goto exit-with-pagefault\nfast: mem[(entry & ~0xFFF) ^ addr]\n```\n\nThere is a 4 MB cache that acts like a tlb. It contains the physical address,\nread-only bit, whether the page contains code (in order to invalidate it on\nwrite) and whether the page points to mmio. Any of those cases are handled in\nthe slow path (`safe_read_jit_slow`), as well as walking the page tables and\ntriggering page faults. The fast path is taken in the vast majority of times.\n\nThe remaining code generation is mostly a straight-forward, 1-to-1 translation\nof x86 to wasm. The only analysis done is to optimise generation of condional\njumps immediately after arithmetic instructions, e.g.:\n\n```\ncmp eax, 52\nsetb eax\n```\n\nbecomes:\n\n```\n... // code for cmp\neax <- eax < 52\n```\n\nA lazy flag mechanism is used to speed arithmetic (applies to both jit and\ninterpreted mode, see\n[`arith.rs`](https://github.com/copy/v86/blob/master/src/rust/cpu/arith.rs) and\n[`misc_instr.rs`](https://github.com/copy/v86/blob/master/src/rust/cpu/misc_instr.rs)).\nThere's a wip that tries to elide most lazy-flags updates:\nhttps://github.com/copy/v86/pull/466\n\nFPU instructions are emulated using softfloat (very slow, but unfortunately\nsome code relies on 80 bit floats).\n"
  },
  {
    "path": "docs/linux-9p-image.md",
    "content": "In order to create a Linux image that can mount the 9p file system, add the following lines to the kernel configuration:\n\n```\nCONFIG_NET_9P=y\nCONFIG_NET_9P_VIRTIO=y\nCONFIG_NET_9P_DEBUG=y\nCONFIG_VIRTIO=y\nCONFIG_VIRTIO_PCI=y\nCONFIG_9P_FS=y\nCONFIG_9P_FSCACHE=y\nCONFIG_9P_FS_POSIX_ACL=y\n```\n\nA Dockerfile for this build is here: https://github.com/ysangkok/build-v86-9p-linux\n\nUsing initcpio\n--------------\n\nThis allows you to remount the root file system using 9p. No changes are necessary if you only want to mount a 9p filesystem after booting.\n\nAdd the following files:\n\n`/etc/initcpio/hooks/9p_root`\n\n```bash\n#!/usr/bin/bash\n\nrun_hook() {\n    mount_handler=\"mount_9p_root\"\n}\n\nmount_9p_root() {\n    msg \":: mounting '$root' on real root (9p)\"\n    if ! mount -t 9p host9p \"$1\"; then\n        echo \"You are now being dropped into an emergency shell.\"\n        launch_interactive_shell\n        msg \"Trying to continue (this will most likely fail) ...\"\n    fi\n}\n```\n\n<hr>\n\n`/etc/initcpio/install/9p_root`\n\n```bash\n#!/bin/bash\nbuild() {\n\tadd_runscript\n}\n```\n\nChange the following options in `/etc/mkinitcpio.conf`:\n\n```bash\nMODULES=\"virtio_pci 9p 9pnet 9pnet_virtio\"\nHOOKS=\"base udev autodetect modconf block filesystems keyboard fsck 9p_root\" # appended 9p_root\n```\n"
  },
  {
    "path": "docs/networking.md",
    "content": "# v86 networking\n\nUser guide to networking in v86.\n\n## Introduction\n\nOn the most basic level, networking in v86 is comprised of two components:\n\n* an emulation of the guest's [Network Interface Controller](https://en.wikipedia.org/wiki/Network_interface_controller) (NIC), and\n* a network backend which passes ethernet frames between the NIC and a virtual and/or physical [ethernet network](https://en.wikipedia.org/wiki/Ethernet).\n\nThere are two **NIC emulations** supported by v86 to chose from, either **`ne2k`** (NE2000/RTL8390-compatible NIC) or **`virtio`** ([VirtIO](http://wiki.osdev.org/Virtio)-compatible device). The right choice simply depends on the driver support in the guest OS, older OSes like FreeDOS only support NE2000, more modern ones usually support VirtIO. In case both are supported by the guest OS it's recommended to try VirtIO first and only fall back to NE2000 if that fails. In some cases it will also be necessary to manually load a driver or similar to activate the NIC and/or networking in the guest OS, yet modern systems usually auto-detect an available NIC during installation and when booting.\n\nThere are also several **network backends** to chose from which emulate the network that the virtual NIC is connected to, they differ in their requirements and in the degree of network access and services provided to guests. Some provide only limited network access, others full access to a physical network which may include a gateway to the Internet. Backends may require a specific type of proxy server to operate.\n\n### Backend URL schemes\n\nThe active network backend is configured through a user-specified **backend URL**. Each backend has at least one distinct URL scheme as specified below, where `PROXY` is the DNS hostname or IP address of a compatible proxy server, `PORT` its optional TCP port number (and its default), and `QUERY` is some HTTP query field fragment:\n\n| Backend | Backend URL scheme(s) | Example(s) |\n| :------ | :-------------------- | :--------- |\n| **[inbrowser](#the-inbrowser-backend)** | `inbrowser` | `inbrowser` |\n| **[wsproxy](#the-wsproxy-backend)** | `\"ws://\" PROXY [\":\" PORT=80] [\"/\" ...]`<br>`\"wss://\" PROXY [\":\" PORT=443] [\"/\" ...]` | `wss://relay.widgetry.org/` |\n| **[wisp](#the-wisp-backend)** | `\"wisp://\" PROXY [\":\" PORT=80] [\"/\" ...]`<br>`\"wisps://\" PROXY [\":\" PORT=443] [\"/\" ...]` | `wisp://localhost:12345` |\n| **[fetch](#the-fetch-backend)** | `\"fetch\" [ \"://\" PROXY [\":\" PORT] [\"/\" QUERY] ]` | `fetch`<br>`fetch://localhost:1234/?url=` |\n\nNote that `wss://` and `wisps://` are the TLS-secured transports of `ws://` and `wisp://`, respectively.\n\n> [!TIP]\n> Since public proxy servers provide only limited bandwidth it is recommended to install and use a local, private proxy server for best results. All proxy servers allow to be executed on the local machine, in a VM running on the local machine, in a local network or even publicly on the Internet (which is generally not recommended, of course). Many proxy servers are distributed as Docker containers which is the recommended way of installing them, otherwise install into a VM.\n\n## Network setup\n\n### Web interface setup\n\nNetwork setup in the v86 web interface at **https://copy.sh/v86/** is straightforward, simply copy the backend URL into the text box named **`Networking proxy`** or select one of the presets to configure the network backend. The guest's NIC emulation (NE2000 or VirtIO) is automatically configured when selecting the guest image in the web interface.\n\n### Embedded v86 setup\n\nJavaScript applications that do not use the v86 web interface (but instead embed V86 into their architecture) setup their network by using the common `config` object that they pass to the V86 constructor. Network settings are members of the object **`config.net_device`**, all settings are optional except for `relay_url`.\n\n#### General `net_device` settings\n\nCommon options in `config.net_device`:\n\n| net_device    | type | description |\n| :------------ | :--- | :--- |\n| **type**      | str  | The type of emulated NIC provided to the guest OS, either `ne2k` or `virtio`. Default: `ne2k`. |\n| **relay_url** | str  | The network backend URL, see [Backend URL schemes](#backend-url-schemes) for details. Note that the CORS proxy server of the `fetch` backend is defined in field `cors_proxy` below. This option is required. |\n| **id**        | int  | Network id, all v86 network instances with the same id share the same network namespace. Default: `0`.<br>*(TODO: class `NetworkAdapter` should also get options.net_device as an argument, at least options.net_device.id).* |\n\n#### Special `net_device` settings\n\nBackends `fetch` and `wisp` support a couple of special settings in `config.net_device` to control virtual network components emulated by the backend:\n\n| net_device     | type | description |\n| :------------- | :--- | :--- |\n| **router_mac** | str  | MAC address of virtual network peers (ARP, PING, DHCP, DNS, NTP, UDP echo and TCP peers) in common MAC address notation. Default `52:54:0:1:2:3`. |\n| **router_ip**  | str  | IP address of virtual network peers (ARP, PING, DHCP, DNS and TCP peers) in dotted IP notation. Default `192.168.86.1`. |\n| **vm_ip**      | str  | IP address to be assigned to the guest by DHCP in dotted IP notation. Default `192.168.86.100`. |\n| **masquerade** | bool | If `True`, announce `router_ip` as the router's and DNS server's IP addresses in generated DHCP replies, and also generate ARP replies to IPs outside the router's subnet `255.255.255.0`. Default: `True`. |\n| **dns_method** | str  | DNS method to use, either `static` or `doh`. `static`: use built-in DNS server, `doh`: use [DNS-over-HTTPS](https://en.wikipedia.org/wiki/DNS_over_HTTPS) (DoH). Defaults to `static` for `fetch` and to `doh` for `wisp` backend. |\n| **doh_server** | str  | Host name or IP address (and optional port number) of the DoH server if `dns_method` is `doh`. The value is expanded to the URL `https://DOH_SERVER/dns-query`. Default: `cloudflare-dns.com`. |\n| **cors_proxy** | str  | CORS proxy server URL, do not use a proxy if undefined. Default: undefined (`fetch` backend only). |\n| **mtu**        | int  | The MTU used for the virtual network. Increasing it can improve performance. This only works if the NIC type is `virtio`. Default: `1500` |\n\n#### Example `net_device` settings\n\n* **Example 1:** Provide an emulated NE2000 NIC to the guest OS and use the `wsproxy` backend with a secure wsproxy server at public host `relay.widgetry.org` listening at default TLS port 443:\n   ```javascript\n   let example_1 = new V86({\n       net_device: {\n           relay_url: \"wss://relay.widgetry.org/\"\n       },\n       // ...\n   });\n   ```\n\n* **Example 2:** Provide a VirtIO NIC to the guest OS and use the `fetch` backend with a CORS proxy server at the local machine listening at port number 23456:\n   ```javascript\n   let example_2 = new V86({\n       net_device: {\n           type: \"virtio\",\n           relay_url: \"fetch\",\n           cors_proxy: \"https://localhost:23456/?url=\"\n       },\n       // ...\n   });\n   ```\n\n## Network backends\n\nOne way to compare the different network backends is how they operate on different layers of the TCP/IP Model (see [Wikipedia](https://en.wikipedia.org/wiki/OSI_model#Comparison_with_TCP/IP_model) for a comparison to the OSI model), approximately:\n\n     Network Peer              Backend                 v86 Guest\n\n    [ Application ] <---- fetch ----> +-----+       [ Application ]\n    [   Transport ] <---- wisp -----> | v86 |       [ Transport   ]\n    [     Network ]                   |     |       [ Network     ]\n    [      Access ] <--- wsproxy ---> +-----+ <---> [ Access      ]\n                      and inbrowser\n\n                    Fig. 1: Network backends in v86\n\nv86 guests strictly expect to exchange raw ethernet frames with their (emulated) network card, hence the higher the layer that a v86 network backend operates on the more virtualized the network becomes and the more work has to be done by the backend to fill in for the missing layers.\n\nIn order to facilitate this for backend implementations, v86 provides helper functions to encode/decode ethernet frames, ARP and IPv4 packets, UDP datagrams, TCP streams and HTTP requests/responses. v86 can also provide minimal but sufficient ARP, ICMP-echo, DHCP, DNS (including DoH) and NTP services to guests.\n\nThe v86 architecture is open for additional network backend implementations, for a basic example see [examples/two_instances.html](../examples/two_instances.html).\n\n### The `inbrowser` backend\n\nThis backend provides raw ethernet services for multiple v86 guests running within the same browser process (meaning within the same web page and/or in separate browser tabs). It works standalone without a proxy server, but it also does not provide any access to external networks.\n\nThe `inbrowser` backend is implemented using the browser-internal [BroadcastChannel](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel) API, due to its simplicity it is the most efficient backend, however all VMs have to share the same browser resources.\n\n### The `wsproxy` backend\n\nA backend based on a proxy server that provides raw ethernet services to guests using the [WebSocket](https://en.wikipedia.org/wiki/WebSocket) protocol for transport. It depends on the specific proxy server what kind of network configuration it presents to guests, but usually a separate IP subnet with DHCP and DNS services and optional access to the server's physical network and possibly Internet are provided.\n\nSince this backend (including its proxy server) only forwards unmodified ethernet frames it is inherently efficient while providing full physical network emulation to guests.\n\n**Proxy server**\n\n* **[websockproxy](https://github.com/benjamincburns/websockproxy)** -- one TAP device for all clients, integrates dnsmasq for DHCP/DNS, no TLS, original server by benjamincburns\n  * Docker container [`benjamincburns/jor1k-relay`](https://hub.docker.com/r/benjamincburns/jor1k-relay) is throttled, see [this comment](https://github.com/benjamincburns/websockproxy/issues/4#issuecomment-317255890)\n  * Docker container [`bellenottelling/websockproxy`](https://hub.docker.com/r/bellenottelling/websockproxy) is unthrottled\n  * [See here](https://github.com/copy/v86/discussions/1175#discussioncomment-11199254) for step-by-step instructions on how to unthrottle websockproxy manually.\n* **[go-websockproxy](https://github.com/gdm85/go-websockproxy)** -- one TAP device for all clients, written in Go, without integraded DHCP but with integrated TLS support\n* **[node-relay](https://github.com/krishenriksen/node-relay)** -- like websockproxy but written for NodeJS (dnsmasq/no TLS), see [New websocket ethernet switch built using Node.js #777](https://github.com/copy/v86/discussions/777)\n* **[wsnic](https://github.com/chschnell/wsnic)** -- uses a single bridge and one TAP device per client, integrates dnsmasq for DHCP/DNS and stunnel for TLS\n* **[RootlessRelay](https://github.com/obegron/rootlessRelay)** -- uses its own network stack that doesn't require TUN/TAP devices, has a built-in reverse proxy and admin interface, see [RootlessRelay #1442](https://github.com/copy/v86/discussions/1442)\n\n[See here](https://github.com/copy/v86/discussions/1199#discussioncomment-12026845) for a benchmark comparing the download performance of these proxy servers.\n\n### The `wisp` backend\n\nThe `wisp` backend implements the client side of the [WISP protocol](https://github.com/MercuryWorkshop/wisp-protocol). WISP is a client/server protocol designed to exchange messages containing UDP and TCP payloads between a WebSocket client and a WISP-compatible proxy server. Note that WISP transports only the packet payloads, not the raw UDP or TCP packets.\n\nThis backend monitors outbound traffic from guests and wraps/unwraps TCP payload data in WISP messages. A TCP state machine is included to terminate the guest's TCP stream. In addition to the TCP stream, virtual replies to ARP, DHCP, DNS, NTP, ICMP-Ping and UDP-Echo requests from guests are generated (to a certain degree). See PR [#1097](https://github.com/copy/v86/pull/1097) for additional information about this backend.\n\nv86 guests are isolated from each other when using the `wisp` backend.\n\n**WISP-compatible proxy server**\n\n* **[wisp-js](https://www.npmjs.com/package/@mercuryworkshop/wisp-js)**\n* **[epoxy-tls](https://github.com/MercuryWorkshop/epoxy-tls)**\n\n> [!NOTE]\n> The WISP protocol only supports UDP and TCP client sockets in the v86 guest, any server sockets listening in the guest are not supported.\n\n> [!NOTE]\n> This WISP implementation does not support UDP, only TCP. Once UDP is added, regular DNS over UDP will become the default (instead of DoH), and the builtin NTP and UDP-Echo servers will be removed.\n\n### The `fetch` backend\n\nThe `fetch` backend uses the browser's [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API to allow guests to send HTTP requests to external HTTP servers and to receive related HTTP responses. This is however complicated by the fact that browsers add [CORS headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) to HTTP requests initiated by `fetch()`, and that they check the CORS headers of related HTTP responses to block access to external web resources not authorized to `fetch()` in the given context.\n\nThis backend is efficient and very useful in cases where CORS is not in the way, otherwise (and in general) a CORS proxy server must be used to provide access to HTTP servers on the open Internet.\n\nLike the [`wisp`](#the-wisp-backend) backend, the `fetch` backend handles DHCP and ARP requests from guests internally, and additionally monitors the guest's outbound traffic for HTTP requests which it translates into calls to `fetch()`. NTP, ICMP pings and UDP echo packets are handled to a certain degree. Note that `fetch()` performs the DNS lookup using the browser's internal DNS client. See PR [#1061](https://github.com/copy/v86/pull/1061) for additional technical details.\n\nStarting with PR [#1233](https://github.com/copy/v86/pull/1233), the TCP guest listener can be accessed from JS API, see the [examples/tcp_terminal.html](../examples/tcp_terminal.html) example.\n\nv86 guests are isolated from each other when using the `fetch` backend.\n\nv86 guests have HTTP access to the host's `localhost` using the URL `http://<port>.external` (e.g. `1234.external` -> `localhost:1234`).\n\n**CORS proxy server**\n\n* **[cors-anywhere](https://github.com/Rob--W/cors-anywhere)** -- NodeJS\n* **[uncors](https://github.com/chschnell/uncors)** -- A simple PHP-based CORS proxy server for Apache2.\n\n> [!TIP]\n> You can pass the following flags to **chromium** to allow browsing without restrictions when using the fetch backend:\n>\n>     --disable-web-security --user-data-dir=/tmp/test\n>\n> **NOTE:** This turns off the same-origin policy and should only be used temporarily!\n\n## Related topics\n\n### v86 run-time state images\n\nv86 supports saving and restoring the guest's and emulator's run-time state in **state image** files.\n\nWhen restoring a state image, v86 randomises the restored guest's MAC address to make sure that multiple VMs restored from the same state image use different MAC addresses. However, the restored guest OS is unaware that its NIC's MAC address has changed which prevents it from sending and receiving packets correctly. There are several workarounds:\n\n- Unload the network driver before saving the state. On Linux, unloading can be\n  done using `rmmod ne2k-pci` or `echo 0000:00:05.0 >\n  /sys/bus/pci/drivers/ne2k-pci/unbind` and loading (after the state has been\n  loaded) using `modprobe ne2k-pci` or `echo 0000:00:05.0 >\n  /sys/bus/pci/drivers/ne2k-pci/bind`\n- Pass `preserve_mac_from_state_image: true` to the V86 constructor. This\n  causes MAC addresses to be shared between all VMs with the same state image.\n- Pass `mac_address_translation: true` to the V86 constructor. This causes v86\n  to present the old MAC address to the guest OS, but translate it to a\n  randomised MAC address in outgoing packets (and vice-versa for incoming\n  packets). This mechanism currently only supports the ethernet, ipv4, dhcp and\n  arp protcols. See `translate_mac_address` in\n  [`src/ne2k.js`](https://github.com/copy/v86/blob/master/src/ne2k.js). This is\n  currently used in Windows, ReactOS and SerenityOS profiles.\n- Some OSes don't cache the MAC address when the driver loads and therefore\n  don't need any of the above workarounds. This seems to be the case for Haiku,\n  OpenBSD and FreeBSD.\n\nNote that the same applies to IP addresses, so a DHCP client should only be run\nafter the state has been loaded.\n\n### NodeJS\n\nNetwork backends `wsproxy` and `wisp` depend on a browser-compatible `WebSocket` constructor being present which is the case since NodeJS v22, backends `inbrowser` and `fetch` should work straightaway.\n\n## Links\n\n* [`examples/two_instances.html`](../examples/two_instances.html), example code that shows how to connect two VMs in a web page with a virtual ethernet crossover cable.\n* [`examples/broadcast-network.html`](../examples/broadcast-network.html), example code that shows the raw packet API.\n* [`examples/tcp_terminal.html`](../examples/tcp_terminal.html), example code that shows how to communicate with a guest TCP port on the `fetch` backend.\n* [DC through windows OS for experimental lab #1195](https://github.com/copy/v86/discussions/1195), demonstrates how to setup a Domain Controller for two Windows VMs (XP and Server 2003) using a virtual crossover cable.\n* [Working on a new cross-platform network relay that is a full virtualized network #1064](https://github.com/copy/v86/discussions/1064) (used in [env86 #1085](https://github.com/copy/v86/discussions/1085))\n"
  },
  {
    "path": "docs/profiling.md",
    "content": "v86 has a built-in profiler, which instruments generated code to count certain\nevents and types of instructions. It can be used by building with `make\ndebug-with-profiler` and opening debug.html.\n\nFor debugging networking, packet logging is available in the UI in both debug\nand release builds. The resulting `traffic.hex` file can be loaded in Wireshark\nusing file -> import from hex -> tick direction indication, timestamp %s.%f.\n"
  },
  {
    "path": "docs/sse-shifts.txt",
    "content": "   0F F1     PSLLW \tmm \tmm/m64 \t       mmx \t       Shift Packed Data Left Logical\n66 0F F1     PSLLW \txmm \txmm/m128       sse2 \t       Shift Packed Data Left Logical\n   0F F2     PSLLD \tmm \tmm/m64 \t       mmx \t       Shift Packed Data Left Logical\n66 0F F2     PSLLD \txmm \txmm/m128       sse2 \t       Shift Packed Data Left Logical\n   0F F3     PSLLQ \tmm \tmm/m64 \t       mmx \t       Shift Packed Data Left Logical\n66 0F F3     PSLLQ \txmm \txmm/m128       sse2 \t       Shift Packed Data Left Logical\n\n   0F E1     PSRAW \tmm \tmm/m64 \t       mmx             Shift Packed Data Right Arithmetic\n66 0F E1     PSRAW \txmm \txmm/m128       sse2\t       Shift Packed Data Right Arithmetic\n   0F E2     PSRAD \tmm \tmm/m64 \t       mmx\t       Shift Packed Data Right Arithmetic\n66 0F E2     PSRAD \txmm \txmm/m128       sse2\t       Shift Packed Data Right Arithmetic\n\n   0F D1     PSRLW \tmm \tmm/m64 \t       mmx \t       Shift Packed Data Right Logical\n66 0F D1     PSRLW \txmm \txmm/m128       sse2 \t       Shift Packed Data Right Logical\n   0F D2     PSRLD \tmm \tmm/m64 \t       mmx \t       Shift Packed Data Right Logical\n66 0F D2     PSRLD \txmm \txmm/m128       sse2 \t       Shift Packed Data Right Logical\n   0F D3     PSRLQ \tmm \tmm/m64 \t       mmx \t       Shift Packed Data Right Logical\n66 0F D3     PSRLQ \txmm \txmm/m128       sse2 \t       Shift Packed Data Right Logical\n\n   0F 71     PSRLW \tmm \timm8 \t       mmx \t       Shift Packed Data Right Logical\n66 0F 71     PSRLW \txmm \timm8 \t       sse2 \t       Shift Packed Data Right Logical\n   0F 71     PSRAW \tmm \timm8 \t       mmx \t       Shift Packed Data Right Arithmetic\n66 0F 71     PSRAW \txmm \timm8 \t       sse2 \t       Shift Packed Data Right Arithmetic\n   0F 71     PSLLW \tmm \timm8 \t       mmx \t       Shift Packed Data Left Logical\n66 0F 71     PSLLW \txmm \timm8 \t       sse2 \t       Shift Packed Data Left Logical\n\n   0F 72     PSRLD \tmm \timm8 \t       mmx \t       Shift Double Quadword Right Logical\n66 0F 72     PSRLD \txmm \timm8 \t       sse2 \t       Shift Double Quadword Right Logical\n   0F 72     PSRAD \tmm \timm8 \t       mmx \t       Shift Packed Data Right Arithmetic\n66 0F 72     PSRAD \txmm \timm8 \t       sse2 \t       Shift Packed Data Right Arithmetic\n   0F 72     PSLLD \tmm \timm8 \t       mmx \t       Shift Packed Data Left Logical\n66 0F 72     PSLLD \txmm \timm8 \t       sse2 \t       Shift Packed Data Left Logical\n\n   0F 73     PSRLQ \tmm \timm8 \t       mmx \t       Shift Packed Data Right Logical\n66 0F 73     PSRLQ \txmm \timm8 \t       sse2 \t       Shift Packed Data Right Logical\n66 0F 73     PSRLDQ \txmm \timm8 \t       sse2 \t       Shift Double Quadword Right Logical\n   0F 73     PSLLQ \tmm \timm8 \t       mmx \t       Shift Packed Data Left Logical\n66 0F 73     PSLLQ \txmm \timm8 \t       sse2 \t       Shift Packed Data Left Logical\n66 0F 73     PSLLDQ \txmm \timm8 \t       sse2 \t       Shift Double Quadword Left Logical\n"
  },
  {
    "path": "docs/windows-9x.md",
    "content": "## Installing using QEMU\n\nRecommended versions:\n - Windows 95 OSR2(.5)\n - Windows 98 Second Edition (SE)\n\n-------------\n\n1. Create a disk image (up to 2 GB):\n```sh\nqemu-img create -f raw hdd.img <size in megabytes>M\n```\n2. Run QEMU with the following settings:\n```sh\nqemu-system-i386 -m 128 -M pc,acpi=off -drive file=hdd.img,format=raw\n```\n - add `-cdrom /path/to/installCD.iso`, if you use a CD version.\n - add `-fda /path/to/boot_floppy.img -boot a`, if you use a floppy version or your install CD is non-bootable.\n - (optionally) add `-device sb16` to enable sound\n - (optionally) add `-nic user,model=ne2k_pci` or `-device ne2k_pci,netdev=<...>` to enable networking\n\n3. For Windows 98: select \"Start Windows 98 Setup from CD-ROM\". For Windows 95: select \"Load NEC IDE CDROM driver\" and run `fdisk` to create partition, restart emulator, run `format c:` and `D:\\WIN95\\SETUP`.\n\n4. To change floppy disk, press *Ctrl+Alt+2* to switch to the QEMU Monitor, run `change floppy0 /path/to/new_floppy_image` and press *Ctrl+Alt+1* to switch to VGA.\n5. Follow the installation guide on the screen.\n\n> [!TIP]\n> For transfer files from host to guest, use [genisoimage](https://wiki.debian.org/genisoimage) ([UltraISO](https://www.ultraiso.com/) and [PowerISO](https://www.poweriso.com/) for Windows and Mac) for creating CD-ISO image or [dosfstools](https://github.com/dosfstools/dosfstools) ([WinImage](https://www.winimage.com/download.htm) for Windows) for creating floppy disk images, then mount the created image to QEMU.\n\n## Troubleshooting\n\n### \"Windows protection\" errors during startup\n\nApply [FIX95CPU](http://lonecrusader.x10host.com/fix95cpu.html) or [patcher9x](https://github.com/JHRobotics/patcher9x#installation).\n\n### \"VFBACKUP could no load VFD.VXD\" on startup (Windows 95)\n\n**Workaround #1**:\n*Source: [#1185](https://github.com/copy/v86/issues/1185)*\n\n1. Mount the installation CD (or `Disk 3` for the RTM version on floppy disks).\n2. Open the \"MS-DOS prompt\" and run:\n\nFor the CD version:\n```bat\nextract /a /l C:\\Windows\\System <cd-rom letter>:\\WIN95\\WIN95_02.CAB vfd.vxd\n```\n\nFor the floppy version:\n```bat\nextract /a /l C:\\Windows\\System <floppy drive letter>:\\WIN95_03.CAB vfd.vxd\n```\n\n**Workaround #2**:\n*Source: [#289](https://github.com/copy/v86/issues/289)*\n\n1. Open the Start menu, click on \"Run\" and run `sysedit`.\n2. Find `C:\\AUTOEXEC.BAT` and add `smartdrv` to the top of the file.\n3. Press File -> Save.\n\n## Floppy disk support\n\nCurrently, the floppy drive in v86 works only with MS-DOS compatibility mode.\n\nTo check this: open the Start menu, click on \"Control Panel\" and \"System\", select \"Performance\" tab.\nIf it says *\"Drive A is using MS-DOS compatibility mode file system\"*, the floppy drive should work properly in v86. If not, try this solution:\n\n1. Click on \"Device Manager\" in \"System Properties\".\n2. Open \"Floppy disk controllers\", select \"Standard Floppy Disk Controller\" and press \"Remove\" at the bottom.\n3. Restart Windows.\n\n## Enabling True Color (32 bpp)\n\nThe default VGA display driver only supports 640x480x4 video mode, to fix this, you can install **Universal VBE9x Video Display Driver** or **VMDisp9x**.\n\n### Universal VBE9x Video Display Driver\n\n> [!WARNING]\n> After installing, DOS Mode (and other programs and games that require it) may not work properly.\n> This is a problem in VBE9x, not v86, see [#110](https://github.com/copy/v86/issues/110).\n> Also, this driver doesn't support DirectX, DirectDraw and OpenGL.\n\n1. Download driver from https://bearwindows.zcm.com.au/vbe9x.htm and unpack into Windows.\n2. Right-click on the Desktop, click on \"Properties\".\n3. Click \"Advanced\" > \"Adapter\" > \"Change\".\n4. Press \"Next\", select \"Display a of all the drivers in a specific location...\" and press again \"Next\".\n5. Press \"Have Disk...\", click \"Browse\" and go to folder with unpacked driver. Inside the folder with driver, should be folders like `032mb`, `064mb`, `128mb`. Choose a version based on needed video memory size (for example, `032mb`), then select `vbemp.inf` inside.\n6. Select \"VBE Miniport\" adapter, press \"OK\" and \"Next\".\n7. After installing, restart Windows.\n\n### VMDisp9x (Windows 95)\n\n> [!WARNING]\n> This driver can run DOS Mode with some graphical glitches. However, DirectX and/or DirectDraw may not work properly with this driver.\n> Also, this driver doesn't support OpenGL.\n\n1. Download `vmdisp9x-<...>-driver-2d.img` from https://github.com/JHRobotics/vmdisp9x/releases.\n2. Mount as floppy image, right-click on the Desktop, click on \"Properties\".\n3. Click \"Advanced\" > \"Adapter\" > \"Change\".\n4. Press \"Have Disk...\", click \"Browse\" and go to the floppy.\n5. Select \"VESA ISA\" adapter and press \"OK\".\n6. After installing, restart Windows.\n\n## CPU idling on Windows 95\nSee about [installing AmnHLT](cpu-idling.md#windows-9x-using-amnhlt).\n\n## Enabling networking on Windows 95 (requires install CD)\n\n1. Open the Start menu, click on \"Control Panel\" and \"Add New Hardware\".\n2. Press \"Next\", select \"No\" and select next options:\n\n| Option        | Value             |\n|:--------------|:------------------|\n| Hardware type | Network adapters  |\n| Manufacturers | Novell            |\n| Models        | NE2000 Compatible |\n\n3. Press \"Next\" and restart Windows.\n4. After restarting, right-click on \"My computer\", select \"Propeties\".\n5. Open \"Device Manager\" tab, select \"NE2000 Compatible\" (in \"Network adapters\") and press \"Properties\"\n6. Open \"Resources\", change values by selecting the properties and click on \"Change Setting\":\n\n| Option             | Value       |\n|:-------------------|:------------|\n| Interrupt Request  | 10          |\n| Input/Output Range | 0300 - 031F |\n\n7. In \"Control Panel\", open \"Network\", click on \"Add\", choose \"Protocol\" and select the following options:\n\n| Option            | Value     |\n|:------------------|:----------|\n| Manufacturers     | Microsoft |\n| Network Protocols | TCP/IP    |\n\n8. (optionally) Set \"Primary Network Logon\" to `Windows Logon`.\n\n## Enabling sound manually\n\n> [!NOTE]\n> If you don't have an install CD, use the Sound Blaster 16 driver from https://web.archive.org/web/20210814023225/https://www.claunia.com/qemu/drivers/index.html (unpack `sbw9xup.exe` as a zip archive).\n\n1. Open \"Start\" menu, click on \"Control Panel\" and \"Add New Hardware\".\n2. Press \"Next\", select \"No\" and select the following options:\n\n| Option        | Value                                    |\n|:--------------|:-----------------------------------------|\n| Hardware type | Sound, video and game cotrollers         |\n| Manufacturers | Creative Labs                            |\n| Models        | Creative Labs Sound Blaster 16 or AWE-32 |\n\n3. Restart Windows.\n"
  },
  {
    "path": "docs/windows-nt.md",
    "content": "\n - [Windows NT 3.1](#windows-nt-31) / [3.51](#windows-nt-351) / [4.0](#windows-nt-40)\n - [Windows 2000/XP](#windows-2000xp)\n - [Windows Vista and newer](#windows-vista-and-newer)\n\n------------------------\n## Windows NT 3.1\n\n### Installing using QEMU\n\n1. Install MS-DOS and [Oak CD-ROM Driver](https://www.dosdays.co.uk/topics/Software/optical_downloads.php).\n2. Create 4 blank floppy disk images:\n\n - run `qemu-img create -f raw floppy.img 1440K`\n - mount (`-fda floppy.img`) and run `format A:` in a VM\n\n3. Run QEMU with the following settings for installation:\n\n```sh\nqemu-system-i386 -m 64 -drive file=hdd.img,format=raw -cpu pentium -M pc,acpi=off -cdrom InstallCD.iso\n```\n\n4. Run `xcopy /v <CD-ROM letter>:\\I386\\ C:\\install\\` in a VM to copy all files, disable the CD-ROM driver.\n5. Run QEMU with the following settings:\n\n```sh\nqemu-system-i386 -m 64 -drive file=hdd.img,format=raw -cpu pentium -M pc,acpi=off\n```\n\n6. Run `C:\\install\\winnt /F /C` in a VM.\n7. Follow the setup instructions. To change floppy disk, press *Ctrl+Alt+2* to switch to the QEMU Monitor, run `change floppy0 /path/to/new_floppy_image` and press *Ctrl+Alt+1* to switch to VGA.\n\n\n## Windows NT 3.51\n\n### Installing\n\n> [!NOTE]\n> In newer versions of QEMU, the Windows Setup may not work, you can use an older version of QEMU, PCem, 86Box or PCBox instead.\n\n1. If you install via MS-DOS, install [the Oak CD-ROM Driver](https://www.dosdays.co.uk/topics/Software/optical_downloads.php) and run `<CD-ROM letter>:\\I386\\WINNT /B`.\n2. Follow the setup instructions.\n3. After installing, download NT 3.51 SuperPack ([here](https://bearwindows.zcm.com.au/winnt351.htm#4) or [here](https://alter.org.ua/en/soft/nt_spack/nt3/)), unpack the archive into a Windows and copy files from `FAT32` (`SYS\\FAT32`) and `RENEW` (`SYS\\RENEW`) folders in `C:\\WINNT35\\system32\\drivers` with replacing.\n\n### Enabling networking\n\n1. Open \"Control Panel\" > \"Network\", install Windows NT Networking (installation CD required).\n2. In \"Network Adapter Card Detection\", press Continue three times, set `Network Adapter Card: Novell NE2000 Compatible Adapter`.\n3. Set the following settings and click Continue:\n\n```\nIRQ Level: 10\nI/O Port Address: 0x300\n```\n\n4. In \"Bus Location\", press OK. Check the boxes \"TCP/IP Transport\" and \"Enable Automatic DHCP Configuration\" in the next window.\n5. In \"TCP/IP Configuration\", check the box \"Enable Automatic DHCP Configuration\".\n6. Restart the VM.\n\n\n## Windows NT 4.0\n\nRecommended version: Windows NT 4.0 SP1\n\n### Installing using QEMU\n\n1. Run QEMU with the following settings for installation:\n\n```sh\nqemu-system-i386 -m 64 -drive file=hdd.img,format=raw -cdrom InstallCD.iso -cpu pentium -M pc,acpi=off\n```\n\n2. On setup startup, press F5 and select \"Standard PC\".\n3. Follow the setup instructions.\n\n### Running in v86\n\nDue to a problem with CPUID, you need to add `cpuid_level: 2` and `acpi: false` to the V86 constructor (not supported in the UI):\n\n```js\nvar emulator = new V86({\n    ...\n    cpuid_level: 2,\n    acpi: false\n});\n```\n\n\n## Windows 2000/XP\n\n### Installing using QEMU\n\n1. Run QEMU with the following settings for installation:\n\n```sh\nqemu-system-i386 -m 512 -drive file=hdd.img,format=raw -cdrom InstallCD.iso\n```\n\nOptional:\n - add `-device sb16` to enable sound\n - add `-nic user,model=ne2k_pci` or `-device ne2k_pci,netdev=<...>` to enable networking\n\n2. Follow the setup instructions.\n3. This step fixes the error `Uncaught RangeError: Maximum call stack size exceeded` in Chromium during Windows 2000/XP startup in v86.\n\nAfter installation, change the computer type to \"Standard PC\" as described [here](http://web.archive.org/web/20220528021535/https://www.scm-pc-card.de/file/manual/FAQ/acpi_uninstallation_windows_xp_english.pdf):\n1. Open Start menu, right-click on \"My Computer\", select \"Manage\"\n2. Open Device Manager, open Computer, right-click on \"ACPI Uniprocessor PC\"\n3. Select \"Update Driver...\" > \"No, not this time\"\n4. Select \"Install from a list or specific location (Advanced)\" > Next > \"Don't search. I will choose the driver to install.\"\n5. Choose \"Standard PC\", press Next > Finish.\n6. Restart the VM, follow multiple \"Found New Hardware Wizard\" dialogs with default options.\n\n### Enabling True Color (for Windows 2000)\n\n> [!NOTE]\n> This driver doesn't support DirectX, DirectDraw and OpenGL.\n\n1. Download driver from https://bearwindows.zcm.com.au/vbemp.htm and unpack into Windows.\n2. Open Start menu, right-click on \"My Computer\", select \"Manage\"\n3. Open Device Manager, open Computer and right-click on \"Video Controller\".\n4. Press \"Properties\", select \"Driver\" tab and press \"Update Driver\".\n5. Select \"Display a list of the known drivers for this device...\", choose \"Display adapters\".\n5. Press \"Have Disk...\", click \"Browse\" and go to folder with unpacked driver. Go to `VBE20\\W2K\\PNP`, then select `vbemppnp.inf` inside.\n6. Select \"VBE Miniport\" adapter, press \"Yes\" and \"Next\".\n7. After installing, restart the VM.\n\n### Enabling sound\n\n*Source: [#1049](https://github.com/copy/v86/issues/1049)*\n\n1. Right-click on \"My computer\" > \"System Properties\", select \"Hardware\" tab, press \"Hardware Wizard\"\n2. Press \"Next\" > \"Add/Troubleshoot a device\" > \"Add a new device\"\n3. Select \"No, I want to select the hardware from a list\" > \"Sound, video and game controllers\"\n4. Select the following options and press \"Next\":\n\n```\nHardware type: Sound, video and game cotrollers\nManufacturers: Creative Technology Ltd.\nModels: Sound Blaster 16 or AWE32 or compatible (WDM)\n```\n\n\n## Windows Vista and newer\n\n### Installing using QEMU\n\n1. Run QEMU with the following settings for installation:\n\n```sh\nqemu-system-i386 -m 1024 -drive file=hdd.img,format=raw -cdrom InstallCD.iso\n```\n\nOptionally add `-accel kvm` (for Linux host), `-accel whpx` (for Windows host) or `-accel hvf` (for MacOS host) to use hypervisor acceleration.\n\n2. Follow the setup instructions.\n\n### Running in v86\n\nEnable ACPI and set the memory size to 512 MB or more.\n\n### Enabling networking (ne2k)\n\n*Source: https://phaq.phunsites.net/2007/05/21/vista-on-xen-using-ne2000-in-favor-to-rtl8139/*\n\n1. Download https://phaq.phunsites.net/files/2007/05/drivercd.iso_.zip, unpack the archive, mount the ISO to the VM (`-cdrom path/to/drivercd.iso` or `change ide1-cd0 path/to/drivercd.iso` in QEMU Monitor), unpack the archive from CDROM into Windows.\n2. Open Start Menu > \"Control Panel\" > \"System\" > \"Device Manager\"\n3. Right-click on \"Ethernet Controller\" > \"Update Driver Software\", press \"Browse my computer for driver software\".\n4. Click \"Browse\" and go to folder with unpacked driver, select `WIN2000` folder, press \"Install this driver software anyway\".\n"
  },
  {
    "path": "eslint.config.mjs",
    "content": "export default [\n    {\n        \"languageOptions\": {\n            \"globals\": {\n                \"process\": \"readonly\",\n                \"window\": \"writable\",\n                \"navigator\": \"writable\",\n                \"location\": \"writable\",\n                \"document\": \"readonly\",\n                \"console\": \"readonly\",\n                \"crypto\": \"readonly\",\n                \"alert\": \"readonly\",\n                \"performance\": \"readonly\",\n                \"URL\": \"readonly\",\n                \"WebAssembly\": \"readonly\",\n\n                \"setTimeout\": \"readonly\",\n                \"setInterval\": \"readonly\",\n                \"clearTimeout\": \"readonly\",\n                \"clearInterval\": \"readonly\",\n                \"requestAnimationFrame\": \"readonly\",\n                \"cancelAnimationFrame\": \"readonly\",\n\n                \"Buffer\": \"readonly\",\n                \"FileReader\": \"readonly\",\n                \"TextEncoder\": \"readonly\",\n                \"TextDecoder\": \"readonly\",\n                \"fetch\": \"readonly\",\n                \"Headers\": \"readonly\",\n                \"Response\": \"readonly\",\n                \"WebSocket\": \"readonly\",\n                \"Blob\": \"readonly\",\n                \"File\": \"readonly\",\n                \"XMLHttpRequest\": \"readonly\",\n                \"URLSearchParams\": \"readonly\",\n                \"ImageData\": \"readonly\",\n                \"Image\": \"readonly\",\n                \"OffscreenCanvas\": \"readonly\",\n                \"BroadcastChannel\": \"readonly\",\n                \"HTMLElement\": \"readonly\",\n                \"HTMLTextAreaElement\": \"readonly\",\n\n                \"AudioContext\": \"readonly\",\n                \"AudioWorkletProcessor\": \"readonly\",\n                \"webkitAudioContext\": \"readonly\",\n                \"AudioWorkletNode\": \"readonly\",\n                \"Worker\": \"readonly\",\n                \"postMessage\": \"readonly\",\n                \"importScripts\": \"readonly\",\n\n                \"DEBUG\": \"writable\"\n            }\n        },\n        rules: {\n            \"eol-last\": \"error\",\n            //\"no-extra-parens\": \"error\",\n            \"no-trailing-spaces\": \"error\",\n            \"no-extra-semi\": \"error\",\n            \"no-tabs\": \"error\",\n            \"quotes\": [\"error\", \"double\", { \"avoidEscape\": true, \"allowTemplateLiterals\": true }],\n            \"keyword-spacing\": [\"error\", { \"overrides\": {\n                \"if\": { \"after\": false },\n                \"for\": { \"after\": false },\n                \"while\": { \"after\": false },\n                \"switch\": { \"after\": false },\n                \"catch\": { \"after\": false },\n            } }],\n            \"semi\": \"error\",\n            \"no-useless-return\": \"error\",\n            \"eqeqeq\": \"error\",\n            \"no-multiple-empty-lines\": [\"error\", { \"max\": 2, \"maxBOF\": 0, \"maxEOF\": 0 }],\n            //\"no-var\": \"error\",\n            \"radix\": \"error\",\n            \"comma-style\": [\"error\", \"last\"],\n            //\"comma-dangle\": [\"error\", \"always-multiline\"],\n            \"no-sequences\": \"error\",\n            //\"one-var\": [\"error\", \"never\"],\n            \"constructor-super\": \"error\",\n            \"for-direction\": \"error\",\n            \"getter-return\": \"error\",\n            \"no-async-promise-executor\": \"error\",\n            //\"no-case-declarations\": \"error\",\n            \"no-class-assign\": \"error\",\n            \"no-compare-neg-zero\": \"error\",\n            \"no-cond-assign\": \"error\",\n            \"no-const-assign\": \"error\",\n            \"no-constant-binary-expression\": \"error\",\n            //\"no-constant-condition\": \"error\",\n            //\"no-control-regex\": \"error\",\n            //\"no-debugger\": \"error\",\n            \"no-delete-var\": \"error\",\n            \"no-dupe-args\": \"error\",\n            \"no-dupe-class-members\": \"error\",\n            //\"no-dupe-else-if\": \"error\",\n            \"no-dupe-keys\": \"error\",\n            \"no-duplicate-case\": \"error\",\n            //\"no-empty\": \"error\",\n            \"no-empty-character-class\": \"error\",\n            \"no-empty-pattern\": \"error\",\n            \"no-empty-static-block\": \"error\",\n            \"no-ex-assign\": \"error\",\n            \"no-extra-boolean-cast\": \"error\",\n            \"no-fallthrough\": \"error\",\n            \"no-func-assign\": \"error\",\n            \"no-global-assign\": \"error\",\n            \"no-import-assign\": \"error\",\n            \"no-invalid-regexp\": \"error\",\n            \"no-irregular-whitespace\": \"error\",\n            \"no-loss-of-precision\": \"error\",\n            \"no-misleading-character-class\": \"error\",\n            \"no-new-native-nonconstructor\": \"error\",\n            \"no-nonoctal-decimal-escape\": \"error\",\n            \"no-obj-calls\": \"error\",\n            \"no-octal\": \"error\",\n            \"no-prototype-builtins\": \"error\",\n            //\"no-redeclare\": \"error\",\n            \"no-regex-spaces\": \"error\",\n            \"no-self-assign\": \"error\",\n            \"no-setter-return\": \"error\",\n            \"no-shadow-restricted-names\": \"error\",\n            \"no-sparse-arrays\": \"error\",\n            \"no-this-before-super\": \"error\",\n            \"no-undef\": \"error\",\n            \"no-unexpected-multiline\": \"error\",\n            //\"no-unreachable\": \"error\",\n            \"no-unsafe-finally\": \"error\",\n            \"no-unsafe-negation\": \"error\",\n            \"no-unsafe-optional-chaining\": \"error\",\n            \"no-unused-labels\": \"error\",\n            \"no-unused-private-class-members\": \"error\",\n            //\"no-unused-vars\": \"error\",\n            \"no-useless-backreference\": \"error\",\n            \"no-useless-catch\": \"error\",\n            //\"no-useless-escape\": \"error\",\n            \"no-with\": \"error\",\n            \"require-yield\": \"error\",\n            \"use-isnan\": \"error\",\n            \"valid-typeof\": \"error\",\n            \"strict\": \"error\"\n        }\n    }\n];\n"
  },
  {
    "path": "examples/alpine.html",
    "content": "<!doctype html>\n<title>Alpine</title>\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 512 * 1024 * 1024,\n        vga_memory_size: 8 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: { url: \"../bios/seabios.bin\" },\n        vga_bios: { url: \"../bios/vgabios.bin\" },\n        filesystem: {\n            baseurl: \"../images/alpine-rootfs-flat\",\n            basefs: \"../images/alpine-fs.json\",\n        },\n        autostart: true,\n        bzimage_initrd_from_filesystem: true,\n        cmdline: \"rw root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose modules=virtio_pci tsc=reliable\",\n        //initial_state: { url: \"../images/alpine-state.bin\" },\n    });\n};\n</script>\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/arch.html",
    "content": "<!doctype html>\n<title>Archlinux</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 512 * 1024 * 1024,\n        vga_memory_size: 8 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        filesystem: {\n            baseurl: \"../images/arch/\",\n            basefs: \"../images/fs.json\",\n        },\n        autostart: true,\n        bzimage_initrd_from_filesystem: true,\n        cmdline: [\n            \"rw\",\n            \"root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose\",\n            \"init=/usr/bin/init-openrc\",\n        ].join(\" \"),\n    });\n\n    document.getElementById(\"save_file\").onclick = async function()\n    {\n        const new_state = await emulator.save_state();\n\n        var a = document.createElement(\"a\");\n        a.download = \"v86state.bin\";\n        a.href = window.URL.createObjectURL(new Blob([new_state]));\n        a.dataset.downloadurl = \"application/octet-stream:\" + a.download + \":\" + a.href;\n        a.click();\n\n        this.blur();\n    };\n\n    document.getElementById(\"restore_file\").onchange = function()\n    {\n        if(this.files.length)\n        {\n            var filereader = new FileReader();\n            emulator.stop();\n\n            filereader.onload = async function(e)\n            {\n                await emulator.restore_state(e.target.result);\n                emulator.run();\n            };\n\n            filereader.readAsArrayBuffer(this.files[0]);\n\n            this.value = \"\";\n        }\n\n        this.blur();\n    };\n};\n</script>\n\n<input id=\"save_file\" type=\"button\" value=\"Save state to file\">\nRestore from file: <input id=\"restore_file\" type=\"file\">\n<hr>\n\n<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/async_load.html",
    "content": "<!doctype html>\n<title>Asynchronous loading of disk images</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    // Async loading of the iso image\n    // Note how the emulation starts without downloading the 50MB image\n\n    // Support of the \"Range: bytes=...\" header is required on the server, CORS\n    // is required if the server is on a different host\n\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 64 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        cdrom: {\n            url: \"../images/dsl-4.11.rc2.iso\",\n            async: true,\n\n            // size can be determined automatically, but costs an extra request\n            // and might not work reliably\n            size: 52824064,\n        },\n        autostart: true,\n    });\n}\n</script>\n\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/basic.html",
    "content": "<!doctype html>\n<title>Basic Emulator</title><!-- not BASIC! -->\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = window.emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 32 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        cdrom: {\n            url: \"../images/linux.iso\",\n        },\n        autostart: true,\n    });\n}\n</script>\n\n<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/broadcast-network.html",
    "content": "<!doctype html>\n<title>Networking via Broadcast Channel API</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = window.emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 64 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        bzimage: {\n            url: \"../images/buildroot-bzimage68.bin\",\n        },\n        autostart: true,\n    });\n\n    var broadcast = new BroadcastChannel(\"v86-network\");\n\n    broadcast.addEventListener(\"message\", function(e) {\n        emulator.bus.send(\"net0-receive\", e.data);\n    });\n    emulator.add_listener(\"net0-send\", function(packet) {\n        broadcast.postMessage(packet);\n    });\n}\n</script>\n\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n\n<pre>\n# This example allows network across multiple browser tabs by using BroadcastChannels.\n\n# Configure a static IP\nifconfig eth0 up arp 10.5.0.x\n\n# Ping by IP\nping 10.5.0.x\n\n# Run a DNS server and send a query (10.5.0.x for server, 10.5.0.y for record)\necho \"anotherhost 10.5.0.y\" | dnsd -c - -v    - server\nnslookup -type=a anotherhost 10.5.0.x         - client\n\n# Telnet calculator\nsocat TCP-L:23,fork exec:bc\n\n# Simple HTTP server\nsocat TCP-L:80,crlf,fork system:'echo HTTP/1.1 200 OK;echo;lua /root/test.lua'\n</pre>\n"
  },
  {
    "path": "examples/destroy.html",
    "content": "<!doctype html>\n<title>Destroyable Emulator</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 32 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        cdrom: {\n            url: \"../images/linux.iso\",\n        },\n        autostart: true,\n    });\n\n    setTimeout(() => { emulator.destroy(); }, 1000);\n}\n</script>\n\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/lang.html",
    "content": "<!doctype html>\n<title>Basic Emulator</title><!-- not BASIC! -->\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var start = Date.now();\n\n    setInterval(function()\n    {\n        document.getElementById(\"time\").textContent = Math.round((Date.now() - start) / 1000);\n    }, 999);\n\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 512 * 1024 * 1024,\n        vga_memory_size: 8 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        initial_state: {\n            url: \"../images/arch_state-v2.bin.zst\",\n        },\n        filesystem: {\n            baseurl: \"../images/arch/\",\n        },\n        autostart: true,\n    });\n\n    document.getElementById(\"status\").textContent += \".\";\n\n    emulator.add_listener(\"emulator-ready\", async function()\n    {\n        document.getElementById(\"status\").textContent += \".\";\n\n        var code = \"console.log(3 * 7);\\n\";\n        var buffer = new Uint8Array(code.length);\n\n        buffer.set(code.split(\"\").map(function(chr) { return chr.charCodeAt(0); }));\n\n        await emulator.create_file(\"/root/code.js\", buffer);\n        emulator.serial0_send(\"node /root/code.js > /root/out.txt 2> /root/out.txt\\n\");\n    });\n\n    var serial_out = \"\";\n    emulator.add_listener(\"serial0-output-byte\", async function(byte)\n    {\n        var chr = String.fromCharCode(byte);\n        serial_out += chr;\n\n        if(serial_out.endsWith(\"root@nyu\"))\n        {\n            const data = await emulator.read_file(\"/root/out.txt\");\n            document.getElementById(\"output\").textContent += String.fromCharCode.apply(this, data);\n        }\n    });\n}\n</script>\n\n<pre><span id=time></span> <span id=status></span></pre>\n\n<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n\n<hr>\n\n<pre id=output></pre>\n"
  },
  {
    "path": "examples/lua.html",
    "content": "<!doctype html>\n<title>Lua interpreter</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 32 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n\n        // Uncomment to see what's going on\n        //screen_container: document.getElementById(\"screen_container\"),\n\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        bzimage: {\n            url: \"../images/buildroot-bzimage68.bin\",\n        },\n        autostart: true,\n        disable_keyboard: true,\n        disable_mouse: true,\n    });\n\n    var data = \"\";\n    var do_output = false;\n\n    emulator.add_listener(\"serial0-output-byte\", function(byte)\n    {\n        var char = String.fromCharCode(byte);\n        if(char !== \"\\r\")\n        {\n            data += char;\n        }\n\n        if(do_output)\n        {\n            document.getElementById(\"result\").textContent += char;\n        }\n\n        if(data.endsWith(\"~% \"))\n        {\n            console.log(\"Now ready\");\n            document.getElementById(\"status\").textContent = \"Ready.\\n\";\n            document.getElementById(\"run\").disabled = false;\n            do_output = false;\n        }\n    });\n\n    document.getElementById(\"source\").onkeydown = function(e)\n    {\n        if(e.which == 13 && e.ctrlKey)\n        {\n            document.getElementById(\"run\").onclick();\n        }\n    };\n\n    document.getElementById(\"run\").onclick = function()\n    {\n        var code = document.getElementById(\"source\").value;\n\n        emulator.serial0_send(\"lua -e \" + bashEscape(code) + \"\\n\");\n\n        document.getElementById(\"result\").textContent = \"\";\n        document.getElementById(\"status\").textContent = \"Running ...\\n\";\n        this.disabled = true;\n\n        do_output = true;\n    };\n};\n\n// https://gist.github.com/creationix/2502704\n// Implement bash string escaping.\nfunction bashEscape(arg)\n{\n    arg = arg.replace(/\\t+/g, \"\");\n    return \"'\" + arg.replace(/'+/g, function (val) {\n        return \"'\" + val.replace(/'/g, \"\\\\'\") + \"'\";\n    }) + \"'\";\n}\n</script>\n\n<textarea id=source rows=20 cols=80>\nk = 1\nx = 0\n\nwhile k &lt; 1000 do\n    x = x + 1 / (k * k)\n    k = k + 2\nend\n\nprint(math.sqrt(x*8))\n\nfunction factorial(n)\n    if n == 0 then\n        return 1\n    else\n    return n * factorial(n - 1)\n    end\nend\n\nprint(\"factorial(10):\", factorial(10))\n</textarea>\n<button disabled id=run>run (ctrl-enter)</button>\n<br>\n<hr>\n<pre id=status>Wait for boot ...</pre>\n<pre id=result></pre>\n\n<hr>\n\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/nodejs.js",
    "content": "#!/usr/bin/env node\n\nimport path from \"node:path\";\nimport fs from \"node:fs\";\nimport url from \"node:url\";\nimport { V86 } from \"../build/libv86.mjs\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nprocess.stdin.setRawMode(true);\nprocess.stdin.resume();\nprocess.stdin.setEncoding(\"utf8\");\n\nconsole.log(\"Now booting, please stand by ...\");\n\nvar emulator = new V86({\n    bios: { url: __dirname + \"/../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../images/linux4.iso\" },\n    autostart: true,\n    net_device: {\n        type: \"virtio\",\n        relay_url: \"fetch\",\n    },\n});\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr <= \"~\")\n    {\n        process.stdout.write(chr);\n    }\n});\n\nprocess.stdin.on(\"data\", function(c)\n{\n    if(c === \"\\u0003\")\n    {\n        // ctrl c\n        emulator.destroy();\n        process.stdin.pause();\n    }\n    else\n    {\n        emulator.serial0_send(c);\n    }\n});\n"
  },
  {
    "path": "examples/nodejs_state.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport url from \"node:url\";\nimport { V86 } from \"../build/libv86.mjs\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconsole.log(\"Use F2 to save the state and F3 to restore.\");\n\nprocess.stdin.setRawMode(true);\nprocess.stdin.resume();\nprocess.stdin.setEncoding(\"utf8\");\n\nconsole.log(\"Now booting, please stand by ...\");\n\nvar emulator = new V86({\n    bios: { url: __dirname + \"/../bios/seabios.bin\" },\n    cdrom: { url: __dirname + \"/../images/linux4.iso\" },\n    autostart: true,\n});\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr <= \"~\")\n    {\n        process.stdout.write(chr);\n    }\n});\n\nvar state;\n\nprocess.stdin.on(\"data\", async function(c)\n{\n    if(c === \"\\u0003\")\n    {\n        // ctrl c\n        emulator.destroy();\n        process.stdin.pause();\n    }\n    else if(c === \"\\x1b\\x4f\\x51\")\n    {\n        // f2\n        state = await emulator.save_state();\n        console.log(\"--- Saved ---\");\n    }\n    else if(c === \"\\x1b\\x4f\\x52\")\n    {\n        // f3\n        if(state)\n        {\n            console.log(\"--- Restored ---\");\n            await emulator.restore_state(state);\n        }\n    }\n    else\n    {\n        emulator.serial0_send(c);\n    }\n});\n"
  },
  {
    "path": "examples/save_restore.html",
    "content": "<!doctype html>\n<title>Save and restore</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 32 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        cdrom: {\n            url: \"../images/linux.iso\",\n        },\n        autostart: true,\n    });\n\n    var state;\n\n    document.getElementById(\"save_restore\").onclick = async function()\n    {\n        var button = this;\n\n        if(state)\n        {\n            button.value = \"Save state\";\n            await emulator.restore_state(state);\n            state = undefined;\n        }\n        else\n        {\n            const new_state = await emulator.save_state();\n            console.log(\"Saved state of \" + new_state.byteLength + \" bytes\");\n            button.value = \"Restore state\";\n            state = new_state;\n        }\n\n        button.blur();\n    };\n\n    document.getElementById(\"save_file\").onclick = async function()\n    {\n        const new_state = await emulator.save_state();\n        var a = document.createElement(\"a\");\n        a.download = \"v86state.bin\";\n        a.href = window.URL.createObjectURL(new Blob([new_state]));\n        a.dataset.downloadurl = \"application/octet-stream:\" + a.download + \":\" + a.href;\n        a.click();\n\n        this.blur();\n    };\n\n    document.getElementById(\"restore_file\").onchange = function()\n    {\n        if(this.files.length)\n        {\n            var filereader = new FileReader();\n            emulator.stop();\n\n            filereader.onload = async function(e)\n            {\n                await emulator.restore_state(e.target.result);\n                emulator.run();\n            };\n\n            filereader.readAsArrayBuffer(this.files[0]);\n\n            this.value = \"\";\n        }\n\n        this.blur();\n    };\n};\n</script>\n\n<input id=\"save_restore\" type=\"button\" value=\"Save state\">\n<input id=\"save_file\" type=\"button\" value=\"Save state to file\">\nRestore from file: <input id=\"restore_file\" type=\"file\">\n<hr>\n\n<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/sectorc.html",
    "content": "<!doctype html>\n<title>v86: sectorc</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    const libc = `int tmp1;\nint tmp2;\n\nvoid shutdown()\n{\n  /* Shutdown via APM: coded in asm machine code directly */\n\n  // Check for APM\n  // | mov ah,0x53; mov al,0x00; xor bx,bx; int 0x15; jc error\n  asm 180; asm  83; asm 176; asm   0; asm  49; asm 219;\n  asm 205; asm  21; asm 114; asm  55;\n\n  // Disconnect from any APM interface\n  // | mov ah,0x53; mov al,0x04; xor bx,bx; int 0x15\n  // | jc maybe_error; jmp no_error\n  asm 180; asm  83; asm 176; asm   4; asm  49; asm 219;\n  asm 205; asm  21; asm 114; asm   2; asm 235; asm   5;\n\n  // Label: maybe_error\n  // | cmp ah,0x03; jne error\n  asm 128; asm 252; asm   3; asm 117; asm  38;\n  // Label: no_error\n\n  // Connect to APM interface\n  // | mov ah,0x53; mov al,0x01; xor bx,bx; int 0x15; jc error\n  asm 180; asm  83; asm 176; asm   1; asm  49; asm 219;\n  asm 205; asm  21; asm 114; asm  28;\n\n  // Enable power management for all devices\n  // | mov ah,0x53; mov al,0x08; mov bx,0x0001; mov cx,0x0001\n  // | int 0x15; jc error\n  asm 180; asm  83; asm 176; asm   8;\n  asm 187; asm   1; asm   0; asm 185; asm   1; asm   0;\n  asm 205; asm  21; asm 114; asm  14;\n\n  // Set the power state for all devices\n  // | mov ah,0x53; mov al,0x7; mov bx,0x0001; mov cx,0x0003\n  // | int 0x15; jc error\n  asm 180; asm  83; asm 176; asm   7;\n  asm 187; asm   1; asm   0; asm 185; asm   3; asm   0;\n  asm 205; asm  21; asm 114; asm   0;\n\n  // Label: error\n  // | hlt; jmp error\n  asm 244; asm 235; asm 253;\n}\n\nint store_far_seg;\nint store_far_off;\nint store_far_val;\nvoid store_far()\n{\n  // mov es, store_far_seg\n  store_far_seg = store_far_seg;\n  asm 142; asm 192;\n\n  // mov si, store_far_off\n  store_far_off = store_far_off;\n  asm 137; asm 198;\n\n  // mov es:[si], store_far_val\n  store_far_val = store_far_val;\n  asm 38; asm 137; asm 4;\n}\n\nint div10_unsigned_n;\nint div10_unsigned_q;\nint div10_unsigned_r;\nvoid div10_unsigned()\n{\n  /* Taken from \"Hacker's Delight\", modified to \"fit your screen\" */\n\n  tmp1 = ( div10_unsigned_n >> 1 ) & 32767; // unsigned\n  tmp2 = ( div10_unsigned_n >> 2 ) & 16383; // unsigned\n  div10_unsigned_q = tmp1 + tmp2;\n\n  tmp1 = ( div10_unsigned_q >> 4 ) & 4095; // unsigned\n  div10_unsigned_q = div10_unsigned_q + tmp1;\n\n  tmp1 = ( div10_unsigned_q >> 8 ) & 255; // unsigned\n  div10_unsigned_q = div10_unsigned_q + tmp1;\n\n  div10_unsigned_q = ( div10_unsigned_q >> 3 ) & 8191; // unsigned\n\n  div10_unsigned_r = div10_unsigned_n\n    - ( ( div10_unsigned_q << 3 ) + ( div10_unsigned_q << 1 ) );\n\n  if( div10_unsigned_r > 9 ){\n    div10_unsigned_q = div10_unsigned_q + 1;\n    div10_unsigned_r = div10_unsigned_r - 10;\n  }\n}\n\nint print_ch;\nvoid print_char()\n{\n  /* Implement print char via serial port bios function accessed via int 0x14 */\n\n  print_ch = print_ch;     // mov ax,[&print_ch]\n  asm 180; asm 1;          // mov ah,1\n  asm 186; asm 0; asm 0 ;  // mov dx,0\n  asm 205; asm 20;         // int 0x14\n}\n\n// uses 'print_ch'\nvoid print_newline()\n{\n  print_ch = 10;\n  print_char();\n}\n\nint print_num; // input\nint print_u16_bufptr;\nint print_u16_cur;\nvoid print_u16()\n{\n  print_u16_bufptr = 30000; // buffer for ascii digits\n\n  if( print_num == 0 ){\n    print_ch = 48;\n    print_char();\n  }\n\n  print_u16_cur = print_num;\n  while( print_u16_cur != 0 ){\n    div10_unsigned_n = print_u16_cur;\n    div10_unsigned();\n\n    *(int*) print_u16_bufptr = div10_unsigned_r;\n    print_u16_bufptr = print_u16_bufptr + 1;\n\n    print_u16_cur = div10_unsigned_q;\n  }\n\n  while( print_u16_bufptr != 30000 ){ // emit them in reverse over\n    print_u16_bufptr = print_u16_bufptr - 1;\n    print_ch = ( *(int*) print_u16_bufptr & 255 ) + 48;\n    print_char();\n  }\n}\n\n// uses 'print_num' and 'print_ch'\nvoid print_i16()\n{\n  if( print_num < 0 ){\n    print_ch = 45; print_char(); // '-'\n    print_num = 0 - print_num;\n  }\n  print_u16();\n}\n\nvoid vga_init()\n{\n  // mov ah,0; mov al,0x13; int 0x10\n  asm 180; asm 0; asm 176; asm 19; asm 205; asm 16;\n}\n\nvoid vga_clear()\n{\n  // push di; xor di,di; mov bx,0xa000; mov es,bx;\n  // mov cx,0x7d00; xor ax,ax; rep stos; pop di\n  asm 87 ; asm 49 ; asm 255; asm 187; asm 0; asm 160;\n  asm 142; asm 195; asm 185; asm 0; asm 125; asm 49;\n  asm 192; asm 243; asm 171; asm 95;\n}\n\nint pixel_x;\nint pixel_y;\nvoid vga_set_pixel()\n{\n  // need to multiply pixel_y by 320 = 256 + 64\n  // use 'tmp1' for pixel index\n  tmp1 = ( ( pixel_y << 8 ) + ( pixel_y << 6 ) ) + pixel_x;\n\n  // store to 0xa000:pixel_idx\n  // mov bx,0xa000; mov es,bx; mov bx,ax; mov BYTE PTR es:[bx],0xf\n  tmp1 = tmp1;\n  asm 187; asm 0; asm 160; asm 142; asm 195;\n  asm 137; asm 195; asm 38; asm 198; asm 7; asm 15;\n}\n\nint port_num;\nint port_val;\nvoid port_inb()\n{\n  dx = port_num;\n  // mov dx,WORD PTR [0x464]; in al,dx\n  asm 139; asm 22; asm 160; asm 4; asm 236;\n\n  // mov WORD PTR [0x464],ax\n  asm 137; asm 6; asm 100; asm 4;\n  port_val = ax;\n}\nvoid port_inw()\n{\n  // mov dx,WORD PTR [0x464]; in ax,dx\n  dx = port_num;\n  asm 139; asm 22; asm 160; asm 4; asm 237;\n\n  // mov WORD PTR [0x464],ax\n  asm 137; asm 6; asm 100; asm 4;\n  port_val = ax;\n}\nvoid port_outb()\n{\n  dx = port_num;\n  ax = port_val;\n\n  // mov dx,WORD PTR [0x464]\n  asm 139; asm 22; asm 160; asm 4;\n\n  // mov ax,WORD PTR [0x464]\n  asm 139; asm 6; asm 100; asm 4;\n\n  // outb dx,al\n  asm 238;\n}\nvoid port_outw()\n{\n  dx = port_num;\n  ax = port_val;\n\n  // mov dx,WORD PTR [0x464]\n  asm 139; asm 22; asm 160; asm 4;\n\n  // mov ax,WORD PTR [0x464]\n  asm 139; asm 6; asm 100; asm 4;\n\n  // outb dx,al\n  asm 239;\n}\n\nvoid dump_code_segment_and_shutdown()\n{\n  /* NOTE: This code is in a different segment from data, and our compiled pointer accesses\n     do not leave the data segment, so we need a little machine code to grab data from the\n     code segment and stash it in a variable for C */\n\n  i = 0;\n  while( i < 8192 ){  /* Just assuming 8K is enough.. might not be true */\n\n    // (put \"i\" in ax); mov si,ax; mov ax,cs:[si]; mov [&a],ax\n    i = i; asm 137; asm 198; asm 46; asm 139; asm 4; asm 137; asm 133; asm 98; asm 0;\n\n    print_ch = a;\n    print_char();\n    i = i + 1;\n  }\n  shutdown();\n}\n`;\n\n    const start = `void _start()\n{\n  main();\n  shutdown();\n}\n`;\n\n    const hello = `int buf;\nint ptr;\nint len;\nvoid vga_write()\n{\n  /* Text vga is located at b800:0000 */\n  store_far_seg = 47104; // segment: 0xb800\n  store_far_off = idx << 1;\n  store_far_val = ( 15 << 8 ) | ( ch & 255 ); // white fg and black bg\n  store_far();\n}\n\nint x_off;\nint y_off;\nvoid vga_write_ch()\n{\n  if( ch != 10 ){\n    idx = y_off + x_off;\n    vga_write();\n    x_off = x_off + 1;\n  }\n  if( ( ch == 10 ) | ( x_off == 80 ) ){\n    y_off = y_off + 80;\n    x_off = 0;\n  }\n}\n\nint idx;\nvoid vga_clear()\n{\n  idx = 0;\n  while( idx < 2000 ){ // 80x25\n    ch = 32; // char: ' '\n    vga_write();\n    idx = idx + 1;\n  }\n  pos = 0;\n}\n\nvoid main()\n{\n  // dump_code_segment_and_shutdown();\n\n  vga_clear();\n\n  ch = 72;  vga_write_ch();\n  ch = 101; vga_write_ch();\n  ch = 108; vga_write_ch();\n  ch = 108; vga_write_ch();\n  ch = 111; vga_write_ch();\n  ch = 10;  vga_write_ch();\n  ch = 32;  vga_write_ch();\n  ch = 102; vga_write_ch();\n  ch = 114; vga_write_ch();\n  ch = 111; vga_write_ch();\n  ch = 109; vga_write_ch();\n  ch = 10;  vga_write_ch();\n  ch = 32;  vga_write_ch();\n  ch = 32;  vga_write_ch();\n  ch = 83;  vga_write_ch();\n  ch = 101; vga_write_ch();\n  ch = 99;  vga_write_ch();\n  ch = 116; vga_write_ch();\n  ch = 111; vga_write_ch();\n  ch = 114; vga_write_ch();\n  ch = 67;  vga_write_ch();\n  ch = 10;  vga_write_ch();\n  ch = 32;  vga_write_ch();\n  ch = 32;  vga_write_ch();\n  ch = 32;  vga_write_ch();\n\n  i = 0;\n  while( i < 10 ){\n    ch = 33; vga_write_ch();\n    i = i + 1;\n  }\n\n  while( 1 ){ }\n}\n`;\n\n    const sinwave = `/* A Sine-wave Animation\n\n   Math time:\n   ---------------------------\n   Along the range [0, pi] we can approximate sin(x) very crudely with a 2nd order quadratic\n   That is: y = a * x^2 + b * x + c\n\n   Three unknowns need three constraints, so picking the easy ones:\n     x = 0,    y = 0\n     x = pi/2, y = 1\n     x = pi,   y = 0\n\n   Solving the linear system:\n\n   | 0        0      1 |   | a |   | 0 |\n   | pi^2/4   pi/2   1 | * | b | = | 1 |\n   | pi^2     pi     1 |   | c |   | 0 |\n\n   We get:\n\n   a = -4 / pi^2\n   b = 4 / pi\n   c = 0\n\n   And:\n\n   y = 4x(pi - x)/(pi^2)\n\n   Engineering time:\n   ---------------------------\n   We are working with a 320x200 vga. We also don't have floating-point math. So, the\n   goal here is to do all the math in integer screen coordinates and accept some pixel\n   approximation error.\n\n   First, we want to center the wave in the middle, y = 100\n   We'll let y vary +-50 pixels to remain on the screen, so [50, 150]\n   We want to show an entire cycle (2pi) on the x-axis, so *50 gives us [0, ~314]\n   This implies that the \"x-origin\" is at x = 157\n\n   Substituting in everything, we get:\n\n   y ~= 100 + x*(157 - x)/125\n\n   The division by 125 is problematic as we don't have division. But luckily 128 is close enough.\n\n   Thus, we get:\n\n   y ~= 100 + (x*(157 - x)) >> 7\n\n   The rest is just adjusting for the [0, pi] range reduction by negating the approximation\n   along [pi, 2pi]\n\n   NOTE: the screen coordinate system is upside-down and I don't bother to correct for that.\n   it simply means that the animation starts at a +pi phase offset\n*/\n\nint y;\nint x;\nint x_0;\nvoid sin_positive_approx()\n{\n  y = ( x_0 * ( 157 - x_0 ) ) >> 7;\n}\nvoid sin()\n{\n  x_0 = x;\n  while( x_0 > 314 ){\n    x_0 = x_0 - 314;\n  }\n  if( x_0 <= 157 ){\n    sin_positive_approx();\n  }\n  if( x_0 > 157 ){\n    x_0 = x_0 - 157;\n    sin_positive_approx();\n    y = 0 - y;\n  }\n  y = 100 + y;\n}\n\n\nint offset;\nint x_end;\nvoid draw_sine_wave()\n{\n  x = offset;\n  x_end = x + 314;\n  while( x <= x_end ){\n    sin();\n    pixel_x = x - offset;\n    pixel_y = y;\n    vga_set_pixel();\n    x = x + 1;\n  }\n}\n\nint v_1;\nint v_2;\nvoid delay()\n{\n  v_1 = 0;\n  while( v_1 < 50 ){\n    v_2 = 0;\n    while( v_2 < 10000 ){\n      v_2 = v_2 + 1;\n    }\n    v_1 = v_1 + 1;\n  }\n}\n\nvoid main()\n{\n  vga_init();\n\n  offset = 0;\n  while( 1 ){\n    vga_clear();\n    draw_sine_wave();\n\n    delay();\n    offset = offset + 1;\n    if( offset >= 314 ){ // mod the value to avoid 2^16 integer overflow\n      offset = offset - 314;\n    }\n  }\n}\n`;\n\n    const twinkle = `/* References:\n     http://muruganad.com/8086/8086-assembly-language-program-to-play-sound-using-pc-speaker.html\n     https://en.wikipedia.org/wiki/Twinkle,_Twinkle,_Little_Star\n*/\n\nvoid delay_1()\n{\n  v_1 = 0;\n  while( v_1 < 4000 ){\n    v_2 = 0;\n    while( v_2 < 10000 ){\n      v_2 = v_2 + 1;\n    }\n    v_1 = v_1 + 1;\n  }\n}\n\nvoid delay_2()\n{\n  v_1 = 0;\n  while( v_1 < 300 ){\n    v_2 = 0;\n    while( v_2 < 10000 ){\n      v_2 = v_2 + 1;\n    }\n    v_1 = v_1 + 1;\n  }\n}\n\nvoid audio_init()\n{\n  // Configure PIC2 mode\n  port_num = 67;\n  port_val = 182;\n  port_outb();\n}\n\nvoid audio_enable()\n{\n  // Set bits 0 and 1 to enable\n  port_num = 97;\n  port_inb();\n  port_val = port_val | 3;\n  port_outb();\n}\n\nvoid audio_disable()\n{\n  // Clear bits 0 and 1 to enable\n  port_num = 97;\n  port_inb();\n  port_val = port_val & 65532;\n  port_outb();\n}\n\nint audio_freq;\nvoid audio_freq_set()\n{\n  // Set frequency\n  port_num = 66;\n  port_val = audio_freq & 255;\n  port_outb();\n  port_val = ( audio_freq >> 8 ) & 255;\n  port_outb();\n}\n\nint note;\nvoid play_quarter_note()\n{\n  audio_freq = note;\n  audio_freq_set();\n  audio_enable();\n  delay_1();\n  audio_disable();\n  delay_2();\n}\nvoid play_half_note()\n{\n  audio_freq = note;\n  audio_freq_set();\n  audio_enable();\n  delay_1();\n  delay_1();\n  audio_disable();\n  delay_2();\n}\n\nvoid play_section_1()\n{\n  note = C; play_quarter_note();\n  note = C; play_quarter_note();\n  note = G; play_quarter_note();\n  note = G; play_quarter_note();\n  note = A; play_quarter_note();\n  note = A; play_quarter_note();\n  note = G; play_half_note();\n\n  note = F; play_quarter_note();\n  note = F; play_quarter_note();\n  note = E; play_quarter_note();\n  note = E; play_quarter_note();\n  note = D; play_quarter_note();\n  note = D; play_quarter_note();\n  note = C; play_half_note();\n}\n\nvoid play_section_2()\n{\n  note = G; play_quarter_note();\n  note = G; play_quarter_note();\n  note = F; play_quarter_note();\n  note = F; play_quarter_note();\n  note = E; play_quarter_note();\n  note = E; play_quarter_note();\n  note = D; play_half_note();\n\n  note = G; play_quarter_note();\n  note = G; play_quarter_note();\n  note = F; play_quarter_note();\n  note = F; play_quarter_note();\n  note = E; play_quarter_note();\n  note = E; play_quarter_note();\n  note = D; play_half_note();\n}\n\nvoid main()\n{\n  audio_init();\n  audio_enable();\n\n  C = 4560;\n  D = 4063;\n  E = 3619;\n  F = 3416;\n  G = 3043;\n  A = 2711;\n\n  play_section_1();\n  play_section_2();\n  play_section_1();\n\n  audio_disable();\n}\n`;\n\n    document.getElementById(\"source\").onkeydown = function(e)\n    {\n        if(e.which == 13 && e.ctrlKey)\n        {\n            document.getElementById(\"run\").onclick();\n        }\n    };\n\n    document.getElementById(\"source\").textContent = sinwave;\n\n    document.getElementById(\"source\").onkeydown = function(e)\n    {\n        if(e.which == 13 && e.ctrlKey)\n        {\n            document.getElementById(\"run\").onclick();\n        }\n    };\n\n    document.getElementById(\"examples\").onchange = function()\n    {\n        document.getElementById(\"source\").textContent = { sinwave, twinkle, hello }[this.value];\n    };\n\n    let emulator;\n    document.getElementById(\"run\").onclick = run;\n\n    function run()\n    {\n        emulator && emulator.destroy();\n\n        emulator = window.emulator = new V86({\n            wasm_path: \"../build/v86.wasm\",\n            memory_size: 32 * 1024 * 1024,\n            vga_memory_size: 2 * 1024 * 1024,\n            screen_container: document.getElementById(\"screen_container\"),\n            bios: { url: \"../bios/seabios.bin\" },\n            vga_bios: { url: \"../bios/vgabios.bin\" },\n            fda: { url: \"../images/sectorc.bin\" },\n            autostart: true,\n        });\n\n        emulator.add_listener(\"emulator-ready\", () => {\n            const source = libc + document.getElementById(\"source\").value + start;\n            emulator.serial0_send(source);\n        });\n\n        document.getElementById(\"run\").onclick = stop;\n        document.getElementById(\"run\").textContent = \"stop\";\n    };\n\n    function stop()\n    {\n        emulator && emulator.destroy();\n        document.getElementById(\"run\").onclick = run;\n        document.getElementById(\"run\").textContent = \"run (ctrl-enter)\";\n    }\n}\n</script>\n\n<br>\n<textarea id=source rows=20 cols=80>\n</textarea>\n<br>\n\n<select id=examples>\n    <option value=sinwave>Sine wave</option>\n    <option value=hello>Hello</option>\n    <option value=twinkle>Twinkle (audio)</option>\n</select>\n<button id=run>run (ctrl-enter)</button>\n<br>\n<hr>\n\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n\n<hr>\n<a href=\"https://github.com/xorvoid/sectorc\">sectorc</a> on <a href=\"/v86/\">v86</a>\n"
  },
  {
    "path": "examples/serial.html",
    "content": "<!doctype html>\n<title>Serial example</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n\n        // Uncomment to see what's going on\n        //screen_container: document.getElementById(\"screen_container\"),\n\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        bzimage: {\n            url: \"../images/buildroot-bzimage68.bin\",\n            async: false,\n        },\n        filesystem: {},\n        cmdline: \"tsc=reliable mitigations=off random.trust_cpu=on\",\n        autostart: true,\n        disable_keyboard: true,\n    });\n\n    // In this example we wait for output from the serial terminal, which\n    // should be running busybox. We log in as soon as a prompt appears and then\n    // retrieve a directory listing of the root directory\n    var data = \"\";\n\n    var stages = [\n        {\n            test: \"~% \",\n            send: \"ls -1 --color=never /\\n\",\n        },\n        {\n            test: \"~% \",\n            send: \"lua -e 'print(3+4)'\\n\",\n        },\n    ];\n    var stage = 0;\n\n    emulator.add_listener(\"serial0-output-byte\", function(byte)\n    {\n        var char = String.fromCharCode(byte);\n        if(char === \"\\r\")\n        {\n            return;\n        }\n\n        data += char;\n        document.getElementById(\"terminal\").value += char;\n\n        var current = stages[stage];\n\n        if(!current)\n        {\n            return;\n        }\n\n        if(data.endsWith(current.test))\n        {\n            stage++;\n            emulator.serial0_send(current.send);\n\n            var log = \"Sending: \" + current.send.replace(/\\n/g, \"\\\\n\") + \"\\n\";\n            document.getElementById(\"log\").value += log;\n        }\n    });\n};\n</script>\n\n<textarea readonly rows=25 cols=60 id=\"log\">Waiting for boot ...\n</textarea>\n\n<textarea readonly rows=25 cols=60 id=\"terminal\"></textarea>\n\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/tcp_terminal.html",
    "content": "<!doctype html>\n<title>TCP Terminal</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = window.emulator = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        memory_size: 64 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        bzimage: {\n            url: \"../images/buildroot-bzimage68.bin\",\n        },\n        net_device: {\n            relay_url: \"fetch\",\n            type: \"virtio\"\n        },\n        autostart: true\n    });\n\n    var is_connected = false, connection = null;\n\n    function clear_log()\n    {\n        document.getElementById(\"log\").value = \"\";\n    }\n\n    function write_log(text)\n    {\n        document.getElementById(\"log\").value += text;\n    }\n\n    function on_connect()\n    {\n        document.getElementById(\"connect\").innerHTML = \"Disconnect\";\n        document.getElementById(\"send\").disabled = false;\n        is_connected = true; \n    }\n\n    function on_disconnect()\n    {\n        document.getElementById(\"connect\").innerHTML = \"Connect\";\n        document.getElementById(\"send\").disabled = true;\n        is_connected = false;\n    }\n\n    document.getElementById(\"connect\").onclick = async function() {\n        if(!is_connected) \n        {\n            clear_log();\n            let port = parseInt(document.getElementById(\"port\").value);\n            write_log(\"> Probing port \" + port + \"\\n\");\n\n            let open = await emulator.network_adapter.tcp_probe(port);\n            if(open)\n            {\n                connection = emulator.network_adapter.connect(port);\n\n                connection.on(\"connect\", function()\n                {\n                    write_log(\"> Connected\\n\\n\");\n                    on_connect();\n                });\n\n                connection.on(\"data\", function(data)\n                {\n                    write_log(new TextDecoder().decode(data));\n                });\n\n                connection.on(\"close\", function()\n                {\n                    write_log(\"\\n> Closed by listener\");\n                    on_disconnect();\n                });\n\n                connection.on(\"shutdown\", function()\n                {\n                    write_log(\"\\n> Shutdown by listener\");\n                    on_disconnect();\n                });\n            }\n            else\n            {\n                write_log(\"> Probing failed\");\n            }\n        }\n        else\n        {\n            connection.close();\n            write_log(\"\\n> Closed by client\");\n            on_disconnect();\n        }\n    }\n\n    document.getElementById(\"send\").onclick = function() \n    {\n        connection.write(new TextEncoder().encode(document.getElementById(\"prompt\").value + \"\\r\\n\"));\n        document.getElementById(\"prompt\").value = \"\";\n    }\n\n    document.getElementById(\"prompt\").onkeydown = function(e)\n    {\n        if(e.which == 13 && is_connected)\n        {\n            document.getElementById(\"send\").onclick();\n        }\n    };\n\n    clear_log();\n}\n</script>\n\n<div id=\"screen_container\" style=\"float: left\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n\n<div style=\"float: left; margin: 10px\">\n    <textarea readonly cols=\"60\" rows=\"15\" id=\"log\"></textarea><br>\n    Port: <input type=\"number\" min=\"1\" max=\"65535\" value=\"1234\" id=\"port\" />\n    <button id=\"connect\">Connect</button> <input type=\"text\" id=\"prompt\" />\n    <button id=\"send\" disabled=\"true\">Send (enter)</button>\n\n    <h3>Getting started</h3>\n    <ol>\n        <li>Run <code>udhcpc</code></li>\n        <li>Run any TCP server:\n        <ul>\n        <li>Echo: <code>socat -v PIPE TCP-L:1234,fork</code></li> \n        <li>(more examples on <a href=\"broadcast-network.html\">broadcast-network.html</a>)</li>\n        </ul>\n        <li>Type a port number and press \"Connect\"</li>\n    </ol>\n</div>\n"
  },
  {
    "path": "examples/two_instances.html",
    "content": "<!doctype html>\n<title>Two emulators</title>\n\n<script src=\"../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var container1 = document.getElementById(\"screen_container1\");\n    var container2 = document.getElementById(\"screen_container2\");\n\n    var emulator1 = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        screen_container: container1,\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        cdrom: {\n            url: \"../images/linux4.iso\",\n        },\n        autostart: true,\n    });\n\n    var emulator2 = new V86({\n        wasm_path: \"../build/v86.wasm\",\n        screen_container: container2,\n        bios: {\n            url: \"../bios/seabios.bin\",\n        },\n        vga_bios: {\n            url: \"../bios/vgabios.bin\",\n        },\n        cdrom: {\n            url: \"../images/linux4.iso\",\n        },\n        autostart: true,\n    });\n\n    emulator2.keyboard_set_status(false);\n\n    container1.addEventListener(\"mousedown\", function(e)\n    {\n        container1.style.borderColor = \"yellow\";\n        container2.style.borderColor = \"black\";\n\n        emulator1.keyboard_set_status(true);\n        emulator2.keyboard_set_status(false);\n    }, false);\n\n    container2.addEventListener(\"mousedown\", function(e)\n    {\n        container1.style.borderColor = \"black\";\n        container2.style.borderColor = \"yellow\";\n\n        emulator1.keyboard_set_status(false);\n        emulator2.keyboard_set_status(true);\n    }, false);\n\n    emulator1.add_listener(\"serial0-output-byte\", function(byte)\n    {\n        var char = String.fromCharCode(byte);\n        emulator2.serial0_send(char);\n    });\n    emulator1.add_listener(\"net0-send\", function(data)\n    {\n        emulator2.bus.send(\"net0-receive\", data);\n    });\n    emulator2.add_listener(\"net0-send\", function(data)\n    {\n        emulator1.bus.send(\"net0-receive\", data);\n    });\n};\n</script>\n\nTo set up networking:\n<pre>\n# first VM\nifconfig eth0 up arp 10.5.0.2\n\n# second VM\nifconfig eth0 up arp 10.5.0.3\nping 10.5.0.2\n</pre>\nClick on a screen to control it.\n<hr>\n\n<div id=\"screen_container1\" style=\"float: left; margin: 10px; border: 3px solid yellow;\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n\n<div id=\"screen_container2\" style=\"float: left; margin: 10px; border: 3px solid black;\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "examples/worker.html",
    "content": "<!doctype html>\n<title>Worker</title>\n\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var worker = new Worker(\"worker.js\");\n    var terminal = document.getElementById(\"terminal\");\n\n    worker.onmessage = function(e)\n    {\n        terminal.textContent += e.data;\n    }\n\n    terminal.onkeypress = function(e)\n    {\n        e.preventDefault();\n        worker.postMessage(String.fromCharCode(e.which));\n    };\n}\n\nvar start = Date.now();\nsetInterval(function()\n{\n    document.getElementById(\"time\").textContent = (Date.now() - start) / 1000 | 0;\n});\n</script>\n\n<span id=time></span>s\n<hr>\n<textarea rows=25 cols=80 id=terminal></textarea>\n\n"
  },
  {
    "path": "examples/worker.js",
    "content": "importScripts(\"../build/libv86.js\");\n\n/* global V86 */\n\nvar emulator = new V86({\n    wasm_path: \"../build/v86.wasm\",\n    memory_size: 32 * 1024 * 1024,\n    vga_memory_size: 2 * 1024 * 1024,\n    bios: {\n        url: \"../bios/seabios.bin\",\n    },\n    vga_bios: {\n        url: \"../bios/vgabios.bin\",\n    },\n    cdrom: {\n        url: \"../images/linux4.iso\",\n    },\n    autostart: true,\n});\n\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    this.postMessage(chr);\n}.bind(this));\n\nthis.onmessage = function(e)\n{\n    emulator.serial0_send(e.data);\n};\n"
  },
  {
    "path": "gen/generate_analyzer.js",
    "content": "#!/usr/bin/env node\n\n\nimport assert from \"node:assert/strict\";\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\n\nimport x86_table from \"./x86_table.js\";\nimport * as rust_ast from \"./rust_ast.js\";\nimport { hex, get_switch_value, get_switch_exist, finalize_table_rust } from \"./util.js\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nconst OUT_DIR = path.join(__dirname, \"..\", \"src/rust/gen/\");\n\nfs.mkdirSync(OUT_DIR, { recursive: true });\n\nconst table_arg = get_switch_value(\"--table\");\nconst gen_all = get_switch_exist(\"--all\");\nconst to_generate = {\n    analyzer: gen_all || table_arg === \"analyzer\",\n    analyzer0f: gen_all || table_arg === \"analyzer0f\",\n};\n\nassert(\n    Object.keys(to_generate).some(k => to_generate[k]),\n    \"Pass --table [analyzer|analyzer0f] or --all to pick which tables to generate\"\n);\n\ngen_table();\n\nfunction gen_read_imm_call(op, size_variant)\n{\n    let size = (op.os || op.opcode % 2 === 1) ? size_variant : 8;\n\n    if(op.imm8 || op.imm8s || op.imm16 || op.imm1632 || op.imm32 || op.immaddr)\n    {\n        if(op.imm8)\n        {\n            return \"cpu.read_imm8()\";\n        }\n        else if(op.imm8s)\n        {\n            return \"cpu.read_imm8s()\";\n        }\n        else\n        {\n            if(op.immaddr)\n            {\n                // immaddr: depends on address size\n                return \"cpu.read_moffs()\";\n            }\n            else\n            {\n                assert(op.imm1632 || op.imm16 || op.imm32);\n\n                if(op.imm1632 && size === 16 || op.imm16)\n                {\n                    return \"cpu.read_imm16()\";\n                }\n                else\n                {\n                    assert(op.imm1632 && size === 32 || op.imm32);\n                    return \"cpu.read_imm32()\";\n                }\n            }\n        }\n    }\n    else\n    {\n        return undefined;\n    }\n}\n\nfunction gen_call(name, args)\n{\n    args = args || [];\n    return `${name}(${args.join(\", \")});`;\n}\n\n/*\n * Current naming scheme:\n * instr(16|32|)_(66|F2|F3)?0F?[0-9a-f]{2}(_[0-7])?(_mem|_reg|)\n */\nfunction make_instruction_name(encoding, size)\n{\n    const suffix = encoding.os ? String(size) : \"\";\n    const opcode_hex = hex(encoding.opcode & 0xFF, 2);\n    const first_prefix = (encoding.opcode & 0xFF00) === 0 ? \"\" : hex(encoding.opcode >> 8 & 0xFF, 2);\n    const second_prefix = (encoding.opcode & 0xFF0000) === 0 ? \"\" : hex(encoding.opcode >> 16 & 0xFF, 2);\n    const fixed_g_suffix = encoding.fixed_g === undefined ? \"\" : `_${encoding.fixed_g}`;\n\n    assert(first_prefix === \"\" || first_prefix === \"0F\" || first_prefix === \"F2\" || first_prefix === \"F3\");\n    assert(second_prefix === \"\" || second_prefix === \"66\" || second_prefix === \"F2\" || second_prefix === \"F3\");\n\n    return `instr${suffix}_${second_prefix}${first_prefix}${opcode_hex}${fixed_g_suffix}`;\n}\n\nfunction gen_instruction_body(encodings, size)\n{\n    const encoding = encodings[0];\n\n    let has_66 = [];\n    let has_F2 = [];\n    let has_F3 = [];\n    let no_prefix = [];\n\n    for(let e of encodings)\n    {\n        if((e.opcode >>> 16) === 0x66) has_66.push(e);\n        else if((e.opcode >>> 8 & 0xFF) === 0xF2 || (e.opcode >>> 16) === 0xF2) has_F2.push(e);\n        else if((e.opcode >>> 8 & 0xFF) === 0xF3 || (e.opcode >>> 16) === 0xF3) has_F3.push(e);\n        else no_prefix.push(e);\n    }\n\n    if(has_F2.length || has_F3.length)\n    {\n        assert((encoding.opcode & 0xFF0000) === 0 || (encoding.opcode & 0xFF00) === 0x0F00);\n    }\n\n    if(has_66.length)\n    {\n        assert((encoding.opcode & 0xFF00) === 0x0F00);\n    }\n\n    const code = [];\n\n    if(encoding.e)\n    {\n        code.push(\"let modrm_byte = cpu.read_imm8();\");\n    }\n\n    if(has_66.length || has_F2.length || has_F3.length)\n    {\n        const if_blocks = [];\n\n        if(has_66.length) {\n            const body = gen_instruction_body_after_prefix(has_66, size);\n            if_blocks.push({ condition: \"cpu.prefixes & prefix::PREFIX_66 != 0\", body, });\n        }\n        if(has_F2.length) {\n            const body = gen_instruction_body_after_prefix(has_F2, size);\n            if_blocks.push({ condition: \"cpu.prefixes & prefix::PREFIX_F2 != 0\", body, });\n        }\n        if(has_F3.length) {\n            const body = gen_instruction_body_after_prefix(has_F3, size);\n            if_blocks.push({ condition: \"cpu.prefixes & prefix::PREFIX_F3 != 0\", body, });\n        }\n\n        const else_block = {\n            body: gen_instruction_body_after_prefix(no_prefix, size),\n        };\n\n        return [].concat(\n            code,\n            {\n                type: \"if-else\",\n                if_blocks,\n                else_block,\n            }\n        );\n    }\n    else {\n        return [].concat(\n            code,\n            gen_instruction_body_after_prefix(encodings, size)\n        );\n    }\n}\n\nfunction gen_instruction_body_after_prefix(encodings, size)\n{\n    const encoding = encodings[0];\n\n    if(encoding.fixed_g !== undefined)\n    {\n        assert(encoding.e);\n\n        // instruction with modrm byte where the middle 3 bits encode the instruction\n\n        // group by opcode without prefix plus middle bits of modrm byte\n        let cases = encodings.reduce((cases_by_opcode, case_) => {\n            assert(typeof case_.fixed_g === \"number\");\n            cases_by_opcode[case_.opcode & 0xFFFF | case_.fixed_g << 16] = case_;\n            return cases_by_opcode;\n        }, Object.create(null));\n        cases = Object.values(cases).sort((e1, e2) => e1.fixed_g - e2.fixed_g);\n\n        return [\n            {\n                type: \"switch\",\n                condition: \"modrm_byte >> 3 & 7\",\n                cases: cases.map(case_ => {\n                    const fixed_g = case_.fixed_g;\n                    const body = gen_instruction_body_after_fixed_g(case_, size);\n\n                    return {\n                        conditions: [fixed_g],\n                        body,\n                    };\n                }),\n\n                default_case: {\n                    body: [\n                        \"analysis.ty = analysis::AnalysisType::BlockBoundary;\",\n                        \"analysis.no_next_instruction = true;\",\n                    ],\n                }\n            },\n        ];\n    }\n    else {\n        assert(encodings.length === 1);\n        return gen_instruction_body_after_fixed_g(encodings[0], size);\n    }\n}\n\nfunction gen_instruction_body_after_fixed_g(encoding, size)\n{\n    const imm_read = gen_read_imm_call(encoding, size);\n    const instruction_postfix = [];\n\n    if(encoding.custom_sti) {\n        instruction_postfix.push(\"analysis.ty = analysis::AnalysisType::STI;\");\n    }\n    else if(\n        encoding.block_boundary &&\n        // jump_offset_imm: Is a block boundary, but gets a different type (Jump) below\n        !encoding.jump_offset_imm || (!encoding.custom && encoding.e))\n    {\n        instruction_postfix.push(\"analysis.ty = analysis::AnalysisType::BlockBoundary;\");\n    }\n\n    if(encoding.no_next_instruction)\n    {\n        instruction_postfix.push(\"analysis.no_next_instruction = true;\");\n    }\n    if(encoding.absolute_jump)\n    {\n        instruction_postfix.push(\"analysis.absolute_jump = true;\");\n    }\n\n    if(encoding.prefix)\n    {\n        const instruction_name = \"analysis::\" + make_instruction_name(encoding, size) + \"_analyze\";\n        const args = [\"cpu\", \"analysis\"];\n\n        assert(!imm_read);\n\n        return [].concat(\n            gen_call(instruction_name, args),\n            instruction_postfix\n        );\n    }\n    else if(encoding.e)\n    {\n        // instruction with modrm byte where the middle 3 bits encode a register\n\n        const reg_postfix = [];\n        const mem_postfix = [];\n\n        if(encoding.mem_ud)\n        {\n            mem_postfix.push(\n                \"analysis.ty = analysis::AnalysisType::BlockBoundary;\"\n            );\n        }\n\n        if(encoding.reg_ud)\n        {\n            reg_postfix.push(\n                \"analysis.ty = analysis::AnalysisType::BlockBoundary;\"\n            );\n        }\n\n        if(encoding.ignore_mod)\n        {\n            assert(!imm_read, \"Unexpected instruction (ignore mod with immediate value)\");\n\n            // Has modrm byte, but the 2 mod bits are ignored and both\n            // operands are always registers (0f20-0f24)\n\n            return instruction_postfix;\n        }\n        else\n        {\n            return [].concat(\n                {\n                    type: \"if-else\",\n                    if_blocks: [{\n                        condition: \"modrm_byte < 0xC0\",\n                        body: [].concat(\n                            gen_call(\"analysis::modrm_analyze\", [\"cpu\", \"modrm_byte\"]),\n                            mem_postfix,\n                        ),\n                    }],\n                    else_block: {\n                        body: reg_postfix,\n                    },\n                },\n                imm_read ? [imm_read + \";\"] : [],\n                instruction_postfix\n            );\n        }\n    }\n    else\n    {\n        // instruction without modrm byte or prefix\n\n        const body = [];\n\n        if(imm_read)\n        {\n            if(encoding.jump_offset_imm)\n            {\n                body.push(\"let jump_offset = \" + imm_read + \";\");\n\n                if(encoding.conditional_jump)\n                {\n                    assert(\n                        (encoding.opcode & ~0xF) === 0x70 ||\n                        (encoding.opcode & ~0xF) === 0x0F80 ||\n                        (encoding.opcode & ~0x3) === 0xE0\n                    );\n                    const condition_index = encoding.opcode & 0xFF;\n                    body.push(`analysis.ty = analysis::AnalysisType::Jump { offset: jump_offset as i32, condition: Some(0x${hex(condition_index, 2)}), is_32: cpu.osize_32() };`);\n                }\n                else\n                {\n                    body.push(`analysis.ty = analysis::AnalysisType::Jump { offset: jump_offset as i32, condition: None, is_32: cpu.osize_32() };`);\n                }\n            }\n            else\n            {\n                body.push(imm_read + \";\");\n            }\n        }\n\n        if(encoding.extra_imm16)\n        {\n            assert(imm_read);\n            body.push(gen_call(\"cpu.read_imm16\"));\n        }\n        else if(encoding.extra_imm8)\n        {\n            assert(imm_read);\n            body.push(gen_call(\"cpu.read_imm8\"));\n        }\n\n        return [].concat(\n            body,\n            instruction_postfix\n        );\n    }\n}\n\nfunction gen_table()\n{\n    let by_opcode = Object.create(null);\n    let by_opcode0f = Object.create(null);\n\n    for(let o of x86_table)\n    {\n        let opcode = o.opcode;\n\n        if((opcode & 0xFF00) === 0x0F00)\n        {\n            opcode &= 0xFF;\n            by_opcode0f[opcode] = by_opcode0f[opcode] || [];\n            by_opcode0f[opcode].push(o);\n        }\n        else\n        {\n            opcode &= 0xFF;\n            by_opcode[opcode] = by_opcode[opcode] || [];\n            by_opcode[opcode].push(o);\n        }\n    }\n\n    let cases = [];\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        let encoding = by_opcode[opcode];\n        assert(encoding && encoding.length);\n\n        let opcode_hex = hex(opcode, 2);\n        let opcode_high_hex = hex(opcode | 0x100, 2);\n\n        if(encoding[0].os)\n        {\n            cases.push({\n                conditions: [`0x${opcode_hex}`],\n                body: gen_instruction_body(encoding, 16),\n            });\n            cases.push({\n                conditions: [`0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, 32),\n            });\n        }\n        else\n        {\n            cases.push({\n                conditions: [`0x${opcode_hex}`, `0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, undefined),\n            });\n        }\n    }\n    const table = {\n        type: \"switch\",\n        condition: \"opcode\",\n        cases,\n        default_case: {\n            body: [\"dbg_assert!(false);\"]\n        },\n    };\n\n    if(to_generate.analyzer)\n    {\n        const code = [\n            \"#[cfg_attr(rustfmt, rustfmt_skip)]\",\n            \"use crate::analysis;\",\n            \"use crate::prefix;\",\n            \"use crate::cpu_context;\",\n            \"pub fn analyzer(opcode: u32, cpu: &mut cpu_context::CpuContext, analysis: &mut analysis::Analysis) {\",\n            table,\n            \"}\",\n        ];\n\n        finalize_table_rust(\n            OUT_DIR,\n            \"analyzer.rs\",\n            rust_ast.print_syntax_tree([].concat(code)).join(\"\\n\") + \"\\n\"\n        );\n    }\n\n    const cases0f = [];\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        let encoding = by_opcode0f[opcode];\n\n        assert(encoding && encoding.length);\n\n        let opcode_hex = hex(opcode, 2);\n        let opcode_high_hex = hex(opcode | 0x100, 2);\n\n        if(encoding[0].os)\n        {\n            cases0f.push({\n                conditions: [`0x${opcode_hex}`],\n                body: gen_instruction_body(encoding, 16),\n            });\n            cases0f.push({\n                conditions: [`0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, 32),\n            });\n        }\n        else\n        {\n            let block = {\n                conditions: [`0x${opcode_hex}`, `0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, undefined),\n            };\n            cases0f.push(block);\n        }\n    }\n\n    const table0f = {\n        type: \"switch\",\n        condition: \"opcode\",\n        cases: cases0f,\n        default_case: {\n            body: [\"dbg_assert!(false);\"]\n        },\n    };\n\n    if(to_generate.analyzer0f)\n    {\n        const code = [\n            \"#![allow(unused)]\",\n            \"#[cfg_attr(rustfmt, rustfmt_skip)]\",\n            \"use crate::analysis;\",\n            \"use crate::prefix;\",\n            \"use crate::cpu_context;\",\n            \"pub fn analyzer(opcode: u32, cpu: &mut cpu_context::CpuContext, analysis: &mut analysis::Analysis) {\",\n            table0f,\n            \"}\"\n        ];\n\n        finalize_table_rust(\n            OUT_DIR,\n            \"analyzer0f.rs\",\n            rust_ast.print_syntax_tree([].concat(code)).join(\"\\n\") + \"\\n\"\n        );\n    }\n}\n"
  },
  {
    "path": "gen/generate_interpreter.js",
    "content": "#!/usr/bin/env node\n\n\nimport assert from \"node:assert/strict\";\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\n\nimport x86_table from \"./x86_table.js\";\nimport * as rust_ast from \"./rust_ast.js\";\nimport { hex, get_switch_value, get_switch_exist, finalize_table_rust } from \"./util.js\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nconst OUT_DIR = path.join(__dirname, \"..\", \"src/rust/gen/\");\n\nfs.mkdirSync(OUT_DIR, { recursive: true });\n\nconst table_arg = get_switch_value(\"--table\");\nconst gen_all = get_switch_exist(\"--all\");\nconst to_generate = {\n    interpreter: gen_all || table_arg === \"interpreter\",\n    interpreter0f: gen_all || table_arg === \"interpreter0f\",\n};\n\nassert(\n    Object.keys(to_generate).some(k => to_generate[k]),\n    \"Pass --table [interpreter|interpreter0f] or --all to pick which tables to generate\"\n);\n\ngen_table();\n\nfunction wrap_imm_call(imm)\n{\n    return `match ${imm} { Ok(o) => o, Err(()) => return }`;\n}\n\nfunction gen_read_imm_call(op, size_variant)\n{\n    let size = (op.os || op.opcode % 2 === 1) ? size_variant : 8;\n\n    if(op.imm8 || op.imm8s || op.imm16 || op.imm1632 || op.imm32 || op.immaddr)\n    {\n        if(op.imm8)\n        {\n            return wrap_imm_call(\"read_imm8()\");\n        }\n        else if(op.imm8s)\n        {\n            return wrap_imm_call(\"read_imm8s()\");\n        }\n        else\n        {\n            if(op.immaddr)\n            {\n                // immaddr: depends on address size\n                return wrap_imm_call(\"read_moffs()\");\n            }\n            else\n            {\n                assert(op.imm1632 || op.imm16 || op.imm32);\n\n                if(op.imm1632 && size === 16 || op.imm16)\n                {\n                    return wrap_imm_call(\"read_imm16()\");\n                }\n                else\n                {\n                    assert(op.imm1632 && size === 32 || op.imm32);\n                    return wrap_imm_call(\"read_imm32s()\");\n                }\n            }\n        }\n    }\n    else\n    {\n        return undefined;\n    }\n}\n\nfunction gen_call(name, args)\n{\n    args = args || [];\n    return `${name}(${args.join(\", \")});`;\n}\n\n/*\n * Current naming scheme:\n * instr(16|32|)_(66|F2|F3)?0F?[0-9a-f]{2}(_[0-7])?(_mem|_reg|)\n */\nfunction make_instruction_name(encoding, size)\n{\n    const suffix = encoding.os ? String(size) : \"\";\n    const opcode_hex = hex(encoding.opcode & 0xFF, 2);\n    const first_prefix = (encoding.opcode & 0xFF00) === 0 ? \"\" : hex(encoding.opcode >> 8 & 0xFF, 2);\n    const second_prefix = (encoding.opcode & 0xFF0000) === 0 ? \"\" : hex(encoding.opcode >> 16 & 0xFF, 2);\n    const fixed_g_suffix = encoding.fixed_g === undefined ? \"\" : `_${encoding.fixed_g}`;\n    const module = first_prefix === \"0F\" || second_prefix === \"0F\" ? \"instructions_0f\" : \"instructions\";\n\n    assert(first_prefix === \"\" || first_prefix === \"0F\" || first_prefix === \"F2\" || first_prefix === \"F3\");\n    assert(second_prefix === \"\" || second_prefix === \"66\" || second_prefix === \"F2\" || second_prefix === \"F3\");\n\n    return `${module}::instr${suffix}_${second_prefix}${first_prefix}${opcode_hex}${fixed_g_suffix}`;\n}\n\nfunction gen_instruction_body(encodings, size)\n{\n    const encoding = encodings[0];\n\n    let has_66 = [];\n    let has_F2 = [];\n    let has_F3 = [];\n    let no_prefix = [];\n\n    for(let e of encodings)\n    {\n        if((e.opcode >>> 16) === 0x66) has_66.push(e);\n        else if((e.opcode >>> 8 & 0xFF) === 0xF2 || (e.opcode >>> 16) === 0xF2) has_F2.push(e);\n        else if((e.opcode >>> 8 & 0xFF) === 0xF3 || (e.opcode >>> 16) === 0xF3) has_F3.push(e);\n        else no_prefix.push(e);\n    }\n\n    if(has_F2.length || has_F3.length)\n    {\n        assert((encoding.opcode & 0xFF0000) === 0 || (encoding.opcode & 0xFF00) === 0x0F00);\n    }\n\n    if(has_66.length)\n    {\n        assert((encoding.opcode & 0xFF00) === 0x0F00);\n    }\n\n    const code = [];\n\n    if(encoding.e)\n    {\n        code.push(`let modrm_byte = ${wrap_imm_call(\"read_imm8()\")};`);\n    }\n\n    if(has_66.length || has_F2.length || has_F3.length)\n    {\n        const if_blocks = [];\n\n        if(has_66.length) {\n            const body = gen_instruction_body_after_prefix(has_66, size);\n            if_blocks.push({ condition: \"prefixes_ & prefix::PREFIX_66 != 0\", body, });\n        }\n        if(has_F2.length) {\n            const body = gen_instruction_body_after_prefix(has_F2, size);\n            if_blocks.push({ condition: \"prefixes_ & prefix::PREFIX_F2 != 0\", body, });\n        }\n        if(has_F3.length) {\n            const body = gen_instruction_body_after_prefix(has_F3, size);\n            if_blocks.push({ condition: \"prefixes_ & prefix::PREFIX_F3 != 0\", body, });\n        }\n\n        const check_prefixes = encoding.sse ? \"(prefix::PREFIX_66 | prefix::PREFIX_F2 | prefix::PREFIX_F3)\" : \"(prefix::PREFIX_F2 | prefix::PREFIX_F3)\";\n\n        const else_block = {\n            body: [].concat(\n                \"dbg_assert!((prefixes_ & \" + check_prefixes + \") == 0);\",\n                gen_instruction_body_after_prefix(no_prefix, size)\n            )\n        };\n\n        return [].concat(\n            \"let prefixes_ = *prefixes;\",\n            code,\n            {\n                type: \"if-else\",\n                if_blocks,\n                else_block,\n            }\n        );\n    }\n    else {\n        return [].concat(\n            code,\n            gen_instruction_body_after_prefix(encodings, size)\n        );\n    }\n}\n\nfunction gen_instruction_body_after_prefix(encodings, size)\n{\n    const encoding = encodings[0];\n\n    if(encoding.fixed_g !== undefined)\n    {\n        assert(encoding.e);\n\n        // instruction with modrm byte where the middle 3 bits encode the instruction\n\n        // group by opcode without prefix plus middle bits of modrm byte\n        let cases = encodings.reduce((cases_by_opcode, case_) => {\n            assert(typeof case_.fixed_g === \"number\");\n            cases_by_opcode[case_.opcode & 0xFFFF | case_.fixed_g << 16] = case_;\n            return cases_by_opcode;\n        }, Object.create(null));\n        cases = Object.values(cases).sort((e1, e2) => e1.fixed_g - e2.fixed_g);\n\n        return [\n            {\n                type: \"switch\",\n                condition: \"modrm_byte >> 3 & 7\",\n                cases: cases.map(case_ => {\n                    const fixed_g = case_.fixed_g;\n                    const body = gen_instruction_body_after_fixed_g(case_, size);\n\n                    return {\n                        conditions: [fixed_g],\n                        body,\n                    };\n                }),\n\n                default_case: {\n                    varname: \"x\",\n                    body: [\n                        `dbg_log!(\"#ud ${encoding.opcode.toString(16).toUpperCase()}/{} at {:x}\", x, *instruction_pointer);`,\n                        \"trigger_ud();\",\n                    ],\n                }\n            },\n        ];\n    }\n    else {\n        assert(encodings.length === 1);\n        return gen_instruction_body_after_fixed_g(encodings[0], size);\n    }\n}\n\nfunction gen_instruction_body_after_fixed_g(encoding, size)\n{\n    const instruction_prefix = [];\n    const instruction_postfix =\n        (encoding.block_boundary && !encoding.no_block_boundary_in_interpreted) ||\n        (!encoding.custom && encoding.e) ?\n        [\"after_block_boundary();\"] : [];\n\n    if(encoding.task_switch_test || encoding.sse)\n    {\n        instruction_prefix.push(\n            {\n                type: \"if-else\",\n                if_blocks: [\n                    {\n                        condition: encoding.sse ? \"!task_switch_test_mmx()\" : \"!task_switch_test()\",\n                        body: [\"return;\"],\n                    }\n                ],\n            });\n    }\n\n    const imm_read = gen_read_imm_call(encoding, size);\n    const instruction_name = make_instruction_name(encoding, size);\n\n    if(encoding.e)\n    {\n        // instruction with modrm byte\n\n        const imm_read = gen_read_imm_call(encoding, size);\n\n        if(encoding.ignore_mod)\n        {\n            assert(!imm_read, \"Unexpected instruction (ignore mod with immediate value)\");\n\n            // Has modrm byte, but the 2 mod bits are ignored and both\n            // operands are always registers (0f20-0f24)\n\n            return [].concat(\n                instruction_prefix,\n                gen_call(instruction_name, [\"modrm_byte & 7\", \"modrm_byte >> 3 & 7\"]),\n                instruction_postfix\n            );\n        }\n        else\n        {\n            let mem_args;\n\n            if(encoding.custom_modrm_resolve)\n            {\n                // requires special handling around modrm_resolve\n                mem_args = [\"modrm_byte\"];\n            }\n            else\n            {\n                mem_args = [\"match modrm_resolve(modrm_byte) { Ok(a) => a, Err(()) => return }\"];\n            }\n\n            const reg_args = [\"modrm_byte & 7\"];\n\n            if(encoding.fixed_g === undefined)\n            {\n                mem_args.push(\"modrm_byte >> 3 & 7\");\n                reg_args.push(\"modrm_byte >> 3 & 7\");\n            }\n\n            if(imm_read)\n            {\n                mem_args.push(imm_read);\n                reg_args.push(imm_read);\n            }\n\n            return [].concat(\n                instruction_prefix,\n                {\n                    type: \"if-else\",\n                    if_blocks: [\n                        {\n                            condition: \"modrm_byte < 0xC0\",\n                            body: [].concat(\n                                gen_call(`${instruction_name}_mem`, mem_args)\n                            ),\n                        }\n                    ],\n                    else_block: {\n                        body: [gen_call(`${instruction_name}_reg`, reg_args)],\n                    },\n                },\n                instruction_postfix\n            );\n        }\n    }\n    else\n    {\n        const args = [];\n\n        if(imm_read)\n        {\n            args.push(imm_read);\n        }\n\n        if(encoding.extra_imm16)\n        {\n            assert(imm_read);\n            args.push(wrap_imm_call(\"read_imm16()\"));\n        }\n        else if(encoding.extra_imm8)\n        {\n            assert(imm_read);\n            args.push(wrap_imm_call(\"read_imm8()\"));\n        }\n\n        return [].concat(\n            instruction_prefix,\n            gen_call(instruction_name, args),\n            instruction_postfix\n        );\n    }\n}\n\nfunction gen_table()\n{\n    let by_opcode = Object.create(null);\n    let by_opcode0f = Object.create(null);\n\n    for(let o of x86_table)\n    {\n        let opcode = o.opcode;\n\n        if((opcode & 0xFF00) === 0x0F00)\n        {\n            opcode &= 0xFF;\n            by_opcode0f[opcode] = by_opcode0f[opcode] || [];\n            by_opcode0f[opcode].push(o);\n        }\n        else\n        {\n            opcode &= 0xFF;\n            by_opcode[opcode] = by_opcode[opcode] || [];\n            by_opcode[opcode].push(o);\n        }\n    }\n\n    let cases = [];\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        let encoding = by_opcode[opcode];\n        assert(encoding && encoding.length);\n\n        let opcode_hex = hex(opcode, 2);\n        let opcode_high_hex = hex(opcode | 0x100, 2);\n\n        if(encoding[0].os)\n        {\n            cases.push({\n                conditions: [`0x${opcode_hex}`],\n                body: gen_instruction_body(encoding, 16),\n            });\n            cases.push({\n                conditions: [`0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, 32),\n            });\n        }\n        else\n        {\n            cases.push({\n                conditions: [`0x${opcode_hex}`, `0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, undefined),\n            });\n        }\n    }\n    const table = {\n        type: \"switch\",\n        condition: \"opcode\",\n        cases,\n        default_case: {\n            body: [\"assert!(false);\"]\n        },\n    };\n    if(to_generate.interpreter)\n    {\n        const code = [\n            \"#![cfg_attr(rustfmt, rustfmt_skip)]\",\n\n            \"use crate::cpu::cpu::{after_block_boundary, modrm_resolve};\",\n            \"use crate::cpu::cpu::{read_imm8, read_imm8s, read_imm16, read_imm32s, read_moffs};\",\n            \"use crate::cpu::cpu::{task_switch_test, trigger_ud};\",\n            \"use crate::cpu::instructions;\",\n            \"use crate::cpu::global_pointers::{instruction_pointer, prefixes};\",\n            \"use crate::prefix;\",\n\n            \"pub unsafe fn run(opcode: u32) {\",\n            table,\n            \"}\",\n        ];\n\n        finalize_table_rust(\n            OUT_DIR,\n            \"interpreter.rs\",\n            rust_ast.print_syntax_tree([].concat(code)).join(\"\\n\") + \"\\n\"\n        );\n    }\n\n    const cases0f = [];\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        let encoding = by_opcode0f[opcode];\n\n        assert(encoding && encoding.length);\n\n        let opcode_hex = hex(opcode, 2);\n        let opcode_high_hex = hex(opcode | 0x100, 2);\n\n        if(encoding[0].os)\n        {\n            cases0f.push({\n                conditions: [`0x${opcode_hex}`],\n                body: gen_instruction_body(encoding, 16),\n            });\n            cases0f.push({\n                conditions: [`0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, 32),\n            });\n        }\n        else\n        {\n            let block = {\n                conditions: [`0x${opcode_hex}`, `0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, undefined),\n            };\n            cases0f.push(block);\n        }\n    }\n\n    const table0f = {\n        type: \"switch\",\n        condition: \"opcode\",\n        cases: cases0f,\n        default_case: {\n            body: [\"assert!(false);\"]\n        },\n    };\n\n    if(to_generate.interpreter0f)\n    {\n        const code = [\n            \"#![cfg_attr(rustfmt, rustfmt_skip)]\",\n\n            \"use crate::cpu::cpu::{after_block_boundary, modrm_resolve};\",\n            \"use crate::cpu::cpu::{read_imm8, read_imm16, read_imm32s};\",\n            \"use crate::cpu::cpu::{task_switch_test, task_switch_test_mmx, trigger_ud};\",\n            \"use crate::cpu::instructions_0f;\",\n            \"use crate::cpu::global_pointers::{instruction_pointer, prefixes};\",\n            \"use crate::prefix;\",\n\n            \"pub unsafe fn run(opcode: u32) {\",\n            table0f,\n            \"}\",\n        ];\n\n        finalize_table_rust(\n            OUT_DIR,\n            \"interpreter0f.rs\",\n            rust_ast.print_syntax_tree([].concat(code)).join(\"\\n\") + \"\\n\"\n        );\n    }\n}\n"
  },
  {
    "path": "gen/generate_jit.js",
    "content": "#!/usr/bin/env node\n\n\nimport assert from \"node:assert/strict\";\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\n\nimport x86_table from \"./x86_table.js\";\nimport * as rust_ast from \"./rust_ast.js\";\nimport { hex, get_switch_value, get_switch_exist, finalize_table_rust } from \"./util.js\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nconst OUT_DIR = path.join(__dirname, \"..\", \"src/rust/gen/\");\n\nfs.mkdirSync(OUT_DIR, { recursive: true });\n\nconst table_arg = get_switch_value(\"--table\");\nconst gen_all = get_switch_exist(\"--all\");\nconst to_generate = {\n    jit: gen_all || table_arg === \"jit\",\n    jit0f: gen_all || table_arg === \"jit0f\",\n};\n\nassert(\n    Object.keys(to_generate).some(k => to_generate[k]),\n    \"Pass --table [jit|jit0f] or --all to pick which tables to generate\"\n);\n\ngen_table();\n\nfunction gen_read_imm_call(op, size_variant)\n{\n    let size = (op.os || op.opcode % 2 === 1) ? size_variant : 8;\n\n    if(op.imm8 || op.imm8s || op.imm16 || op.imm1632 || op.imm32 || op.immaddr)\n    {\n        if(op.imm8)\n        {\n            return \"ctx.cpu.read_imm8()\";\n        }\n        else if(op.imm8s)\n        {\n            return \"ctx.cpu.read_imm8s()\";\n        }\n        else\n        {\n            if(op.immaddr)\n            {\n                // immaddr: depends on address size\n                return \"ctx.cpu.read_moffs()\";\n            }\n            else\n            {\n                assert(op.imm1632 || op.imm16 || op.imm32);\n\n                if(op.imm1632 && size === 16 || op.imm16)\n                {\n                    return \"ctx.cpu.read_imm16()\";\n                }\n                else\n                {\n                    assert(op.imm1632 && size === 32 || op.imm32);\n                    return \"ctx.cpu.read_imm32()\";\n                }\n            }\n        }\n    }\n    else\n    {\n        return undefined;\n    }\n}\n\nfunction gen_call(name, args)\n{\n    args = args || [];\n    return `${name}(${args.join(\", \")});`;\n}\n\n/*\n * Current naming scheme:\n * instr(16|32|)_(66|F2|F3)?0F?[0-9a-f]{2}(_[0-7])?(_mem|_reg|)\n */\nfunction make_instruction_name(encoding, size)\n{\n    const suffix = encoding.os ? String(size) : \"\";\n    const opcode_hex = hex(encoding.opcode & 0xFF, 2);\n    const first_prefix = (encoding.opcode & 0xFF00) === 0 ? \"\" : hex(encoding.opcode >> 8 & 0xFF, 2);\n    const second_prefix = (encoding.opcode & 0xFF0000) === 0 ? \"\" : hex(encoding.opcode >> 16 & 0xFF, 2);\n    const fixed_g_suffix = encoding.fixed_g === undefined ? \"\" : `_${encoding.fixed_g}`;\n\n    assert(first_prefix === \"\" || first_prefix === \"0F\" || first_prefix === \"F2\" || first_prefix === \"F3\");\n    assert(second_prefix === \"\" || second_prefix === \"66\" || second_prefix === \"F2\" || second_prefix === \"F3\");\n\n    return `instr${suffix}_${second_prefix}${first_prefix}${opcode_hex}${fixed_g_suffix}`;\n}\n\nfunction gen_instruction_body(encodings, size)\n{\n    const encoding = encodings[0];\n\n    let has_66 = [];\n    let has_F2 = [];\n    let has_F3 = [];\n    let no_prefix = [];\n\n    for(let e of encodings)\n    {\n        if((e.opcode >>> 16) === 0x66) has_66.push(e);\n        else if((e.opcode >>> 8 & 0xFF) === 0xF2 || (e.opcode >>> 16) === 0xF2) has_F2.push(e);\n        else if((e.opcode >>> 8 & 0xFF) === 0xF3 || (e.opcode >>> 16) === 0xF3) has_F3.push(e);\n        else no_prefix.push(e);\n    }\n\n    if(has_F2.length || has_F3.length)\n    {\n        assert((encoding.opcode & 0xFF0000) === 0 || (encoding.opcode & 0xFF00) === 0x0F00);\n    }\n\n    if(has_66.length)\n    {\n        assert((encoding.opcode & 0xFF00) === 0x0F00);\n    }\n\n    const code = [];\n\n    if(encoding.e)\n    {\n        code.push(\"let modrm_byte = ctx.cpu.read_imm8();\");\n    }\n\n    if(has_66.length || has_F2.length || has_F3.length)\n    {\n        const if_blocks = [];\n\n        if(has_66.length) {\n            const body = gen_instruction_body_after_prefix(has_66, size);\n            if_blocks.push({ condition: \"ctx.cpu.prefixes & prefix::PREFIX_66 != 0\", body, });\n        }\n        if(has_F2.length) {\n            const body = gen_instruction_body_after_prefix(has_F2, size);\n            if_blocks.push({ condition: \"ctx.cpu.prefixes & prefix::PREFIX_F2 != 0\", body, });\n        }\n        if(has_F3.length) {\n            const body = gen_instruction_body_after_prefix(has_F3, size);\n            if_blocks.push({ condition: \"ctx.cpu.prefixes & prefix::PREFIX_F3 != 0\", body, });\n        }\n\n        const else_block = {\n            body: gen_instruction_body_after_prefix(no_prefix, size),\n        };\n\n        return [].concat(\n            code,\n            {\n                type: \"if-else\",\n                if_blocks,\n                else_block,\n            }\n        );\n    }\n    else {\n        return [].concat(\n            code,\n            gen_instruction_body_after_prefix(encodings, size)\n        );\n    }\n}\n\nfunction gen_instruction_body_after_prefix(encodings, size)\n{\n    const encoding = encodings[0];\n\n    if(encoding.fixed_g !== undefined)\n    {\n        assert(encoding.e);\n\n        // instruction with modrm byte where the middle 3 bits encode the instruction\n\n        // group by opcode without prefix plus middle bits of modrm byte\n        let cases = encodings.reduce((cases_by_opcode, case_) => {\n            assert(typeof case_.fixed_g === \"number\");\n            cases_by_opcode[case_.opcode & 0xFFFF | case_.fixed_g << 16] = case_;\n            return cases_by_opcode;\n        }, Object.create(null));\n        cases = Object.values(cases).sort((e1, e2) => e1.fixed_g - e2.fixed_g);\n\n        return [\n            {\n                type: \"switch\",\n                condition: \"modrm_byte >> 3 & 7\",\n                cases: cases.map(case_ => {\n                    const fixed_g = case_.fixed_g;\n                    const body = gen_instruction_body_after_fixed_g(case_, size);\n\n                    return {\n                        conditions: [fixed_g],\n                        body,\n                    };\n                }),\n\n                default_case: {\n                    body: [].concat(\n                        gen_call(`codegen::gen_trigger_ud`, [\"ctx\"]),\n                        \"*instr_flags |= jit::JIT_INSTR_BLOCK_BOUNDARY_FLAG;\"\n                    ),\n                }\n            },\n        ];\n    }\n    else {\n        assert(encodings.length === 1);\n        return gen_instruction_body_after_fixed_g(encodings[0], size);\n    }\n}\n\nfunction gen_instruction_body_after_fixed_g(encoding, size)\n{\n    const instruction_postfix = [];\n\n    if(encoding.block_boundary || (!encoding.custom && encoding.e))\n    {\n        instruction_postfix.push(\"*instr_flags |= jit::JIT_INSTR_BLOCK_BOUNDARY_FLAG;\");\n    }\n\n    const instruction_prefix = [];\n\n    if(encoding.task_switch_test || encoding.sse)\n    {\n        instruction_prefix.push(\n            gen_call(encoding.sse ? \"codegen::gen_task_switch_test_mmx\" : \"codegen::gen_task_switch_test\", [\"ctx\"])\n        );\n    }\n\n    const imm_read = gen_read_imm_call(encoding, size);\n    const imm_read_bindings = [];\n    if(imm_read)\n    {\n        imm_read_bindings.push(`let imm = ${imm_read} as u32;`);\n    }\n\n    const instruction_name = make_instruction_name(encoding, size);\n\n    if(!encoding.prefix)\n    {\n        if(encoding.custom)\n        {\n        }\n        else\n        {\n            instruction_prefix.push(\n                gen_call(\"codegen::gen_move_registers_from_locals_to_memory\", [\"ctx\"])\n            );\n            instruction_postfix.push(\n                gen_call(\"codegen::gen_move_registers_from_memory_to_locals\", [\"ctx\"])\n            );\n        }\n    }\n\n    if(encoding.e)\n    {\n        const reg_postfix = [];\n        const mem_postfix = [];\n\n        if(encoding.mem_ud)\n        {\n            mem_postfix.push(\n                \"*instr_flags |= jit::JIT_INSTR_BLOCK_BOUNDARY_FLAG;\"\n            );\n        }\n\n        if(encoding.reg_ud)\n        {\n            reg_postfix.push(\n                \"*instr_flags |= jit::JIT_INSTR_BLOCK_BOUNDARY_FLAG;\"\n            );\n        }\n\n        if(encoding.ignore_mod)\n        {\n            assert(!imm_read, \"Unexpected instruction (ignore mod with immediate value)\");\n\n            // Has modrm byte, but the 2 mod bits are ignored and both\n            // operands are always registers (0f20-0f24)\n            const args = [\"ctx.builder\", `\"${instruction_name}\"`, \"(modrm_byte & 7) as u32\", \"(modrm_byte >> 3 & 7) as u32\"];\n\n            return [].concat(\n                instruction_prefix,\n                gen_call(`codegen::gen_fn${args.length - 2}_const`, args),\n                reg_postfix,\n                instruction_postfix\n            );\n        }\n        else if(encoding.custom)\n        {\n            const mem_args = [\"ctx\", \"addr\"];\n            const reg_args = [\"ctx\", \"(modrm_byte & 7) as u32\"];\n\n            if(encoding.fixed_g === undefined)\n            {\n                mem_args.push(\"(modrm_byte >> 3 & 7) as u32\");\n                reg_args.push(\"(modrm_byte >> 3 & 7) as u32\");\n            }\n\n            if(imm_read)\n            {\n                mem_args.push(\"imm\");\n                reg_args.push(\"imm\");\n            }\n\n            return [].concat(\n                instruction_prefix,\n                {\n                    type: \"if-else\",\n                    if_blocks: [{\n                        condition: \"modrm_byte < 0xC0\",\n                        body: [].concat(\n                            \"let addr = modrm::decode(ctx.cpu, modrm_byte);\",\n                            imm_read_bindings,\n                            gen_call(`jit_instructions::${instruction_name}_mem_jit`, mem_args),\n                            mem_postfix\n                        ),\n                    }],\n                    else_block: {\n                        body: [].concat(\n                            imm_read_bindings,\n                            gen_call(`jit_instructions::${instruction_name}_reg_jit`, reg_args),\n                            reg_postfix\n                        ),\n                    },\n                },\n                instruction_postfix\n            );\n        }\n        else\n        {\n            const mem_args = [\"ctx.builder\", `\"${instruction_name}_mem\"`];\n            const reg_args = [\"ctx.builder\", `\"${instruction_name}_reg\"`, \"(modrm_byte & 7) as u32\"];\n\n            if(encoding.fixed_g === undefined)\n            {\n                mem_args.push(\"(modrm_byte >> 3 & 7) as u32\");\n                reg_args.push(\"(modrm_byte >> 3 & 7) as u32\");\n            }\n\n            if(imm_read)\n            {\n                mem_args.push(\"imm\");\n                reg_args.push(\"imm\");\n            }\n\n            return [].concat(\n                instruction_prefix,\n                {\n                    type: \"if-else\",\n                    if_blocks: [{\n                        condition: \"modrm_byte < 0xC0\",\n                        body: [].concat(\n                            \"let addr = modrm::decode(ctx.cpu, modrm_byte);\",\n                            gen_call(`codegen::gen_modrm_resolve`, [\"ctx\", \"addr\"]),\n                            imm_read_bindings,\n                            gen_call(`codegen::gen_modrm_fn${mem_args.length - 2}`, mem_args),\n                            mem_postfix\n                        ),\n                    }],\n                    else_block: {\n                        body: [].concat(\n                            imm_read_bindings,\n                            gen_call(`codegen::gen_fn${reg_args.length - 2}_const`, reg_args),\n                            reg_postfix\n                        ),\n                    },\n                },\n                instruction_postfix\n            );\n        }\n    }\n    else if(encoding.prefix || encoding.custom)\n    {\n        // custom, but not modrm\n\n        const args = [\"ctx\"];\n\n        if(imm_read)\n        {\n            args.push(\"imm\");\n        }\n\n        if(encoding.prefix)\n        {\n            args.push(\"instr_flags\");\n        }\n\n        return [].concat(\n            instruction_prefix,\n            imm_read_bindings,\n            gen_call(`jit_instructions::${instruction_name}_jit`, args),\n            instruction_postfix\n        );\n    }\n    else\n    {\n        // instruction without modrm byte or prefix\n\n        const args = [\"ctx.builder\", `\"${instruction_name}\"`];\n\n        if(imm_read)\n        {\n            args.push(\"imm\");\n        }\n\n        if(encoding.extra_imm16)\n        {\n            assert(imm_read);\n            imm_read_bindings.push(`let imm2 = ctx.cpu.read_imm16() as u32;`);\n            args.push(\"imm2\");\n        }\n        else if(encoding.extra_imm8)\n        {\n            assert(imm_read);\n            imm_read_bindings.push(`let imm2 = ctx.cpu.read_imm8() as u32;`);\n            args.push(\"imm2\");\n        }\n\n        return [].concat(\n            instruction_prefix,\n            imm_read_bindings,\n            gen_call(`codegen::gen_fn${args.length - 2}_const`, args),\n            instruction_postfix\n        );\n    }\n}\n\nfunction gen_table()\n{\n    let by_opcode = Object.create(null);\n    let by_opcode0f = Object.create(null);\n\n    for(let o of x86_table)\n    {\n        let opcode = o.opcode;\n\n        if((opcode & 0xFF00) === 0x0F00)\n        {\n            opcode &= 0xFF;\n            by_opcode0f[opcode] = by_opcode0f[opcode] || [];\n            by_opcode0f[opcode].push(o);\n        }\n        else\n        {\n            opcode &= 0xFF;\n            by_opcode[opcode] = by_opcode[opcode] || [];\n            by_opcode[opcode].push(o);\n        }\n    }\n\n    let cases = [];\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        let encoding = by_opcode[opcode];\n        assert(encoding && encoding.length);\n\n        let opcode_hex = hex(opcode, 2);\n        let opcode_high_hex = hex(opcode | 0x100, 2);\n\n        if(encoding[0].os)\n        {\n            cases.push({\n                conditions: [`0x${opcode_hex}`],\n                body: gen_instruction_body(encoding, 16),\n            });\n            cases.push({\n                conditions: [`0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, 32),\n            });\n        }\n        else\n        {\n            cases.push({\n                conditions: [`0x${opcode_hex}`, `0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, undefined),\n            });\n        }\n    }\n    const table = {\n        type: \"switch\",\n        condition: \"opcode\",\n        cases,\n        default_case: {\n            body: [\"assert!(false);\"]\n        },\n    };\n\n    if(to_generate.jit)\n    {\n        const code = [\n            \"#[cfg_attr(rustfmt, rustfmt_skip)]\",\n\n            \"use crate::prefix;\",\n            \"use crate::jit;\",\n            \"use crate::jit_instructions;\",\n            \"use crate::modrm;\",\n            \"use crate::codegen;\",\n\n            \"pub fn jit(opcode: u32, ctx: &mut jit::JitContext, instr_flags: &mut u32) {\",\n            table,\n            \"}\",\n        ];\n\n        finalize_table_rust(\n            OUT_DIR,\n            \"jit.rs\",\n            rust_ast.print_syntax_tree([].concat(code)).join(\"\\n\") + \"\\n\"\n        );\n    }\n\n    const cases0f = [];\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        let encoding = by_opcode0f[opcode];\n\n        assert(encoding && encoding.length);\n\n        let opcode_hex = hex(opcode, 2);\n        let opcode_high_hex = hex(opcode | 0x100, 2);\n\n        if(encoding[0].os)\n        {\n            cases0f.push({\n                conditions: [`0x${opcode_hex}`],\n                body: gen_instruction_body(encoding, 16),\n            });\n            cases0f.push({\n                conditions: [`0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, 32),\n            });\n        }\n        else\n        {\n            let block = {\n                conditions: [`0x${opcode_hex}`, `0x${opcode_high_hex}`],\n                body: gen_instruction_body(encoding, undefined),\n            };\n            cases0f.push(block);\n        }\n    }\n\n    const table0f = {\n        type: \"switch\",\n        condition: \"opcode\",\n        cases: cases0f,\n        default_case: {\n            body: [\"assert!(false);\"]\n        },\n    };\n\n    if(to_generate.jit0f)\n    {\n        const code = [\n            \"#[cfg_attr(rustfmt, rustfmt_skip)]\",\n\n            \"use crate::prefix;\",\n            \"use crate::jit;\",\n            \"use crate::jit_instructions;\",\n            \"use crate::modrm;\",\n            \"use crate::codegen;\",\n\n            \"pub fn jit(opcode: u32, ctx: &mut jit::JitContext, instr_flags: &mut u32) {\",\n            table0f,\n            \"}\",\n        ];\n\n        finalize_table_rust(\n            OUT_DIR,\n            \"jit0f.rs\",\n            rust_ast.print_syntax_tree([].concat(code)).join(\"\\n\") + \"\\n\"\n        );\n    }\n}\n"
  },
  {
    "path": "gen/rust_ast.js",
    "content": "import assert from \"node:assert/strict\";\n\nfunction indent(lines, how_much)\n{\n    return lines.map(line => \" \".repeat(how_much) + line);\n}\n\nexport function print_syntax_tree(statements)\n{\n    let code = [];\n\n    for(let statement of statements)\n    {\n        if(typeof statement === \"string\")\n        {\n            code.push(statement);\n        }\n        else if(statement.type === \"switch\")\n        {\n            assert(statement.condition);\n\n            const cases = [];\n\n            for(let case_ of statement.cases)\n            {\n                assert(case_.conditions.length >= 1);\n\n                cases.push(case_.conditions.join(\" | \") + \" => {\");\n                cases.push.apply(cases, indent(print_syntax_tree(case_.body), 4));\n                cases.push(`},`);\n            }\n\n            if(statement.default_case)\n            {\n                const varname = statement.default_case.varname || \"_\";\n                cases.push(`${varname} => {`);\n                cases.push.apply(cases, indent(print_syntax_tree(statement.default_case.body), 4));\n                cases.push(`}`);\n            }\n\n            code.push(`match ${statement.condition} {`);\n            code.push.apply(code, indent(cases, 4));\n            code.push(`}`);\n        }\n        else if(statement.type === \"if-else\")\n        {\n            assert(statement.if_blocks.length >= 1);\n\n            let first_if_block = statement.if_blocks[0];\n\n            code.push(`if ${first_if_block.condition} {`);\n            code.push.apply(code, indent(print_syntax_tree(first_if_block.body), 4));\n            code.push(`}`);\n\n            for(let i = 1; i < statement.if_blocks.length; i++)\n            {\n                let if_block = statement.if_blocks[i];\n\n                code.push(`else if ${if_block.condition} {`);\n                code.push.apply(code, indent(print_syntax_tree(if_block.body), 4));\n                code.push(`}`);\n            }\n\n            if(statement.else_block)\n            {\n                code.push(`else {`);\n                code.push.apply(code, indent(print_syntax_tree(statement.else_block.body), 4));\n                code.push(`}`);\n            }\n        }\n        else\n        {\n            assert(false, \"Unexpected type: \" + statement.type, \"In:\", statement);\n        }\n    }\n\n    return code;\n}\n"
  },
  {
    "path": "gen/util.js",
    "content": "import fs from \"node:fs\";\nimport path from \"node:path\";\nimport process from \"node:process\";\n\nconst CYAN_FMT = \"\\x1b[36m%s\\x1b[0m\";\n\nexport function hex(n, pad)\n{\n    pad = pad || 0;\n    let s = n.toString(16).toUpperCase();\n    while(s.length < pad) s = \"0\" + s;\n    return s;\n}\n\nexport function get_switch_value(arg_switch)\n{\n    const argv = process.argv;\n    const switch_i = argv.indexOf(arg_switch);\n    const val_i = switch_i + 1;\n    if(switch_i > -1 && val_i < argv.length)\n    {\n        return argv[switch_i + 1];\n    }\n    return null;\n}\n\nexport function get_switch_exist(arg_switch)\n{\n    return process.argv.includes(arg_switch);\n}\n\nexport function finalize_table_rust(out_dir, name, contents)\n{\n    const file_path = path.join(out_dir, name);\n    fs.writeFileSync(file_path, contents);\n    console.log(CYAN_FMT, `[+] Wrote table ${name}.`);\n}\n"
  },
  {
    "path": "gen/x86_table.js",
    "content": "// http://ref.x86asm.net/coder32.html\n\nconst zf = 1 << 6;\nconst of = 1 << 11;\nconst cf = 1 << 0;\nconst af = 1 << 4;\nconst pf = 1 << 2;\nconst sf = 1 << 7;\n\n// Test intel-specific behaviour\n// Setting this to true can make some tests fail\nconst TESTS_ASSUME_INTEL = false;\n\n// === Types of instructions\n//\n// create entry | check for compiled code | instruction\n// -------------+-------------------------+-----------------------------------------------------------\n//      1       |        optional         | pop ds (may change cpu state)\n//              |                         | trigger_ud, div (exception that doesn't generate conditional return from BB)\n//              |                         | port io, popf, sti (may call interrupt or continue at next instruction)\n//              |                         | hlt\n// -------------+-------------------------+-----------------------------------------------------------\n//      1       |            1            | call [eax], jmp [eax], int, iret, ret, jmpf, callf, sysenter, sysexit\n//              |                         | Special case: normal instruction with fallthough to next page\n//              |                         | Special case: after execution of compiled code\n//              |                         | -> may create redundant entry points depending on last instruction?\n// -------------+-------------------------+-----------------------------------------------------------\n//      1       |            0            | rep movs, rep lods, rep stos, rep cmps, rep scas\n//              |                         | -> Executed as follows:\n//              |                         |   - Upto including the first call in compiled mode\n//              |                         |   - Back to main loop and repeated in interpreted mode (as entry point is after instruction, not on)\n//              |                         |   - When finished entry pointer *after* instruction is hit and execution continues in compiled mode\n// -------------+-------------------------+-----------------------------------------------------------\n//      0       |        optional         | jmp foo, jnz foo\n//              |                         | (foo is in the same page as the instruction)\n// -------------+-------------------------+-----------------------------------------------------------\n//      1       |            1            | call foo\n//              |                         | (foo is in the same page as the instruction)\n//              |                         | -> The entry point is not created for jumps within\n//              |                         |    this page, but speculatively for calls from\n//              |                         |    other pages to the function in this page\n// -------------+-------------------------+-----------------------------------------------------------\n//      1       |            1            | call foo, jmp foo, jnz foo\n//              |                         | (foo is in a different page than the instruction)\n\n\n// e: a modrm byte follows the operand\n// os: the instruction behaves differently depending on the operand size\n// fixed_g: the reg field of the modrm byte selects an instruction\n// skip: skip automatically generated tests (nasmtests)\n// mask_flags: flags bits to mask in generated tests\n// prefix: is a prefix instruction\n// imm8, imm8s, imm16, imm1632, immaddr, extra_imm8, extra_imm16: one or two immediate bytes follows the instruction\n// custom: will callback jit to generate custom code\n// block_boundary: may change eip in a way not handled by the jit\n// no_next_instruction: jit will stop analysing after instruction (e.g., unconditional jump, ret)\nconst encodings = [\n    { opcode: 0x06, os: 1, custom: 1 },\n    { opcode: 0x07, os: 1, skip: 1, block_boundary: 1 }, // pop es: block_boundary since it uses non-raising cpu exceptions\n    { opcode: 0x0E, os: 1, custom: 1 },\n    { opcode: 0x0F, os: 1, prefix: 1 },\n    { opcode: 0x16, os: 1, custom: 1 },\n    { opcode: 0x17, block_boundary: 1, os: 1, skip: 1 }, // pop ss\n    { opcode: 0x1E, os: 1, custom: 1 },\n    { opcode: 0x1F, block_boundary: 1, os: 1, skip: 1 }, // pop ds\n    { opcode: 0x26, prefix: 1 },\n    { opcode: 0x27, mask_flags: of },\n    { opcode: 0x2E, prefix: 1 },\n    { opcode: 0x2F, mask_flags: of },\n    { opcode: 0x36, prefix: 1 },\n    { opcode: 0x37, mask_flags: of | sf | pf | zf },\n    { opcode: 0x3E, prefix: 1 },\n    { opcode: 0x3F, mask_flags: of | sf | pf | zf },\n\n    { opcode: 0x60, os: 1, block_boundary: 1 }, // pusha\n    { opcode: 0x61, os: 1, block_boundary: 1 }, // popa\n    { opcode: 0x62, e: 1, skip: 1 },\n    { opcode: 0x63, e: 1, block_boundary: 1 }, // arpl\n    { opcode: 0x64, prefix: 1 },\n    { opcode: 0x65, prefix: 1 },\n    { opcode: 0x66, prefix: 1 },\n    { opcode: 0x67, prefix: 1 },\n\n    { opcode: 0x68, custom: 1, os: 1, imm1632: 1 },\n    { opcode: 0x69, os: 1, e: 1, custom: 1, imm1632: 1, mask_flags: TESTS_ASSUME_INTEL ? af : sf | zf | af | pf },\n    { opcode: 0x6A, custom: 1, os: 1, imm8s: 1 },\n    { opcode: 0x6B, os: 1, e: 1, custom: 1, imm8s: 1, mask_flags: TESTS_ASSUME_INTEL ? af : sf | zf | af | pf },\n\n    { opcode: 0x6C, block_boundary: 1, custom: 1, is_string: 1, skip: 1 },          // ins\n    { opcode: 0xF26C, block_boundary: 1, custom: 1, is_string: 1, skip: 1 },\n    { opcode: 0xF36C, block_boundary: 1, custom: 1, is_string: 1, skip: 1 },\n    { opcode: 0x6D, block_boundary: 1, custom: 1, is_string: 1, os: 1, skip: 1 },\n    { opcode: 0xF26D, block_boundary: 1, custom: 1, is_string: 1, os: 1, skip: 1 },\n    { opcode: 0xF36D, block_boundary: 1, custom: 1, is_string: 1, os: 1, skip: 1 },\n\n    { opcode: 0x6E, block_boundary: 1, custom: 1, is_string: 1, skip: 1 },          // outs\n    { opcode: 0xF26E, block_boundary: 1, custom: 1, is_string: 1, skip: 1 },\n    { opcode: 0xF36E, block_boundary: 1, custom: 1, is_string: 1, skip: 1 },\n    { opcode: 0x6F, block_boundary: 1, custom: 1, is_string: 1, os: 1, skip: 1 },\n    { opcode: 0xF26F, block_boundary: 1, custom: 1, is_string: 1, os: 1, skip: 1 },\n    { opcode: 0xF36F, block_boundary: 1, custom: 1, is_string: 1, os: 1, skip: 1 },\n\n    { opcode: 0x84, custom: 1, e: 1 },\n    { opcode: 0x85, custom: 1, e: 1, os: 1 },\n    { opcode: 0x86, custom: 1, e: 1 },\n    { opcode: 0x87, custom: 1, os: 1, e: 1 },\n    { opcode: 0x88, custom: 1, e: 1 },\n    { opcode: 0x89, custom: 1, os: 1, e: 1 },\n    { opcode: 0x8A, custom: 1, e: 1 },\n    { opcode: 0x8B, custom: 1, os: 1, e: 1 },\n\n    { opcode: 0x8C, os: 1, e: 1, custom: 1, skip: 1 }, // mov reg, sreg\n    { opcode: 0x8D, reg_ud: 1, os: 1, e: 1, custom_modrm_resolve: 1, custom: 1 }, // lea\n    { opcode: 0x8E, block_boundary: 1, e: 1, skip: 1 }, // mov sreg\n    { opcode: 0x8F, os: 1, e: 1, fixed_g: 0, custom_modrm_resolve: 1, custom: 1, block_boundary: 1 }, // pop r/m\n\n    { opcode: 0x90, custom: 1 },\n    { opcode: 0x91, custom: 1, os: 1 },\n    { opcode: 0x92, custom: 1, os: 1 },\n    { opcode: 0x93, custom: 1, os: 1 },\n    { opcode: 0x94, custom: 1, os: 1 },\n    { opcode: 0x95, custom: 1, os: 1 },\n    { opcode: 0x96, custom: 1, os: 1 },\n    { opcode: 0x97, custom: 1, os: 1 },\n\n    { opcode: 0x98, os: 1, custom: 1 },\n    { opcode: 0x99, os: 1, custom: 1 },\n    { opcode: 0x9A, os: 1, imm1632: 1, extra_imm16: 1, skip: 1, block_boundary: 1 }, // callf\n    { opcode: 0x9B, block_boundary: 1, skip: 1 }, // fwait: block_boundary since it uses non-raising cpu exceptions\n    { opcode: 0x9C, os: 1, custom: 1, skip: 1 }, // pushf\n    { opcode: 0x9D, os: 1, custom: 1, skip: 1 }, // popf\n    { opcode: 0x9E, custom: 1 },\n    { opcode: 0x9F, custom: 1 },\n\n    { opcode: 0xA0, custom: 1, immaddr: 1 },\n    { opcode: 0xA1, custom: 1, os: 1, immaddr: 1 },\n    { opcode: 0xA2, custom: 1, immaddr: 1 },\n    { opcode: 0xA3, custom: 1, os: 1, immaddr: 1 },\n\n    // string instructions aren't jumps, but they modify eip due to how they're implemented\n    { opcode: 0xA4, block_boundary: 0, custom: 1, is_string: 1 },\n    { opcode: 0xF2A4, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xF3A4, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xA5, block_boundary: 0, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF2A5, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF3A5, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n\n    { opcode: 0xA6, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xF2A6, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xF3A6, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xA7, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF2A7, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF3A7, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n\n    { opcode: 0xA8, custom: 1, imm8: 1 },\n    { opcode: 0xA9, custom: 1, os: 1, imm1632: 1 },\n\n    { opcode: 0xAA, block_boundary: 0, custom: 1, is_string: 1 },\n    { opcode: 0xF2AA, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xF3AA, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xAB, block_boundary: 0, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF2AB, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF3AB, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n\n    { opcode: 0xAC, block_boundary: 0, custom: 1, is_string: 1 },\n    { opcode: 0xF2AC, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xF3AC, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xAD, block_boundary: 0, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF2AD, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF3AD, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n\n    { opcode: 0xAE, block_boundary: 0, custom: 1, is_string: 1 },\n    { opcode: 0xF2AE, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xF3AE, block_boundary: 1, custom: 1, is_string: 1 },\n    { opcode: 0xAF, block_boundary: 0, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF2AF, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n    { opcode: 0xF3AF, block_boundary: 1, custom: 1, is_string: 1, os: 1 },\n\n    { opcode: 0xC2, custom: 1, block_boundary: 1, no_next_instruction: 1, os: 1, absolute_jump: 1, imm16: 1, skip: 1 }, // ret\n    { opcode: 0xC3, custom: 1, block_boundary: 1, no_next_instruction: 1, os: 1, absolute_jump: 1, skip: 1 },\n\n    { opcode: 0xC4, block_boundary: 1, os: 1, e: 1, skip: 1 }, // les\n    { opcode: 0xC5, block_boundary: 1, os: 1, e: 1, skip: 1 }, // lds\n\n    { opcode: 0xC6, custom: 1, e: 1, fixed_g: 0, imm8: 1 },\n    { opcode: 0xC7, custom: 1, os: 1, e: 1, fixed_g: 0, imm1632: 1 },\n\n    // XXX: Temporary block boundary\n    { opcode: 0xC8, os: 1, imm16: 1, extra_imm8: 1, block_boundary: 1 }, // enter\n    { opcode: 0xC9, custom: 1, os: 1, skip: 1 }, // leave\n\n    { opcode: 0xCA, block_boundary: 1, no_next_instruction: 1, os: 1, imm16: 1, skip: 1 }, // retf\n    { opcode: 0xCB, block_boundary: 1, no_next_instruction: 1, os: 1, skip: 1 },\n    { opcode: 0xCC, block_boundary: 1, skip: 1 }, // int\n    { opcode: 0xCD, block_boundary: 1, skip: 1, imm8: 1 },\n    { opcode: 0xCE, block_boundary: 1, skip: 1 },\n    { opcode: 0xCF, block_boundary: 1, no_next_instruction: 1, os: 1, skip: 1 }, // iret\n\n    { opcode: 0xD4, imm8: 1, block_boundary: 1 }, // aam, may trigger #de\n    { opcode: 0xD5, imm8: 1, mask_flags: of | cf | af },\n    { opcode: 0xD6 },\n\n    { opcode: 0xD7, skip: 1, custom: 1 },\n\n    { opcode: 0xD8, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 4, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xD8, e: 1, fixed_g: 7, custom: 1, is_fpu: 1, task_switch_test: 1 },\n\n    { opcode: 0xD9, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xD9, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xD9, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xD9, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xD9, e: 1, fixed_g: 4, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1, skip_mem: 1 }, // fldenv (mem)\n    { opcode: 0xD9, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xD9, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1, skip: 1 }, // fstenv (mem), fprem (reg)\n    { opcode: 0xD9, e: 1, fixed_g: 7, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1, skip_reg: 1 }, // fprem, fyl2xp1 (precision issues)\n\n    { opcode: 0xDA, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 4, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDA, e: 1, fixed_g: 7, custom: 1, is_fpu: 1, task_switch_test: 1 },\n\n    { opcode: 0xDB, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDB, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1 }, // fisttp (sse3)\n    { opcode: 0xDB, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDB, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDB, e: 1, fixed_g: 4, custom: 0, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDB, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDB, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDB, e: 1, fixed_g: 7, custom: 0, is_fpu: 1, task_switch_test: 1 },\n\n    { opcode: 0xDC, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 4, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDC, e: 1, fixed_g: 7, custom: 1, is_fpu: 1, task_switch_test: 1 },\n\n    { opcode: 0xDD, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xDD, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 }, // fisttp (sse3)\n    { opcode: 0xDD, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xDD, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xDD, e: 1, fixed_g: 4, custom: 0, is_fpu: 1, task_switch_test: 1, os: 1, skip_mem: 1 }, // frstor\n    { opcode: 0xDD, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1, os: 1 },\n    { opcode: 0xDD, e: 1, fixed_g: 6, custom: 0, is_fpu: 1, task_switch_test: 1, os: 1, skip_mem: 1 }, // fsave\n    { opcode: 0xDD, e: 1, fixed_g: 7, custom: 0, is_fpu: 1, task_switch_test: 1, os: 1, skip_mem: 1 }, // fstsw (denormal flag)\n\n    { opcode: 0xDE, e: 1, fixed_g: 0, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 4, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDE, e: 1, fixed_g: 7, custom: 1, is_fpu: 1, task_switch_test: 1 },\n\n    { opcode: 0xDF, e: 1, fixed_g: 0, custom: 0, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDF, e: 1, fixed_g: 1, custom: 1, is_fpu: 1, task_switch_test: 1 }, // fisttp (sse3)\n    { opcode: 0xDF, e: 1, fixed_g: 2, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDF, e: 1, fixed_g: 3, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDF, e: 1, fixed_g: 4, custom: 1, is_fpu: 1, task_switch_test: 1, skip: 1 }, // unimplemented: Binary Coded Decimals / fsts (denormal flag)\n    { opcode: 0xDF, e: 1, fixed_g: 5, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDF, e: 1, fixed_g: 6, custom: 1, is_fpu: 1, task_switch_test: 1 },\n    { opcode: 0xDF, e: 1, fixed_g: 7, custom: 1, is_fpu: 1, task_switch_test: 1 },\n\n    // loop, jcxz, etc.\n    { opcode: 0xE0, os: 1, imm8s: 1, no_block_boundary_in_interpreted: 1, skip: 1, block_boundary: 1, jump_offset_imm: 1, custom: 1, conditional_jump: 1 },\n    { opcode: 0xE1, os: 1, imm8s: 1, no_block_boundary_in_interpreted: 1, skip: 1, block_boundary: 1, jump_offset_imm: 1, custom: 1, conditional_jump: 1 },\n    { opcode: 0xE2, os: 1, imm8s: 1, no_block_boundary_in_interpreted: 1, skip: 1, block_boundary: 1, jump_offset_imm: 1, custom: 1, conditional_jump: 1 },\n    { opcode: 0xE3, os: 1, imm8s: 1, no_block_boundary_in_interpreted: 1, skip: 1, block_boundary: 1, jump_offset_imm: 1, custom: 1, conditional_jump: 1 },\n\n    // port functions aren't jumps, but they may modify eip due to how they are implemented\n    { opcode: 0xE4, block_boundary: 1, imm8: 1, skip: 1 }, // in\n    { opcode: 0xE5, block_boundary: 1, os: 1, imm8: 1, skip: 1 },\n    { opcode: 0xE6, block_boundary: 1, imm8: 1, skip: 1 }, // out\n    { opcode: 0xE7, block_boundary: 1, os: 1, imm8: 1, skip: 1 },\n\n    { opcode: 0xE8, block_boundary: 1, jump_offset_imm: 1, os: 1, imm1632: 1, custom: 1, skip: 1 }, // call\n    { opcode: 0xE9, block_boundary: 1, no_block_boundary_in_interpreted: 1, jump_offset_imm: 1, no_next_instruction: 1, os: 1, imm1632: 1, custom: 1, skip: 1 },\n    { opcode: 0xEA, block_boundary: 1, no_next_instruction: 1, os: 1, imm1632: 1, extra_imm16: 1, skip: 1 }, // jmpf\n    { opcode: 0xEB, block_boundary: 1, no_block_boundary_in_interpreted: 1, jump_offset_imm: 1, no_next_instruction: 1, os: 1, imm8s: 1, custom: 1, skip: 1 },\n\n    { opcode: 0xEC, block_boundary: 1, skip: 1 }, // in\n    { opcode: 0xED, block_boundary: 1, os: 1, skip: 1 },\n    { opcode: 0xEE, block_boundary: 1, skip: 1 }, // out\n    { opcode: 0xEF, block_boundary: 1, os: 1, skip: 1 },\n\n    { opcode: 0xF0, prefix: 1 },\n    { opcode: 0xF1, skip: 1 },\n    { opcode: 0xF2, prefix: 1 },\n    { opcode: 0xF3, prefix: 1 },\n    { opcode: 0xF4, block_boundary: 1, no_next_instruction: 1, skip: 1 }, // hlt\n    { opcode: 0xF5 },\n\n    { opcode: 0xF6, e: 1, fixed_g: 0, imm8: 1, custom: 1 },\n    { opcode: 0xF6, e: 1, fixed_g: 1, imm8: 1, custom: 1 },\n    { opcode: 0xF6, e: 1, fixed_g: 2, custom: 1 },\n    { opcode: 0xF6, e: 1, fixed_g: 3, custom: 1 },\n    { opcode: 0xF6, e: 1, fixed_g: 4, mask_flags: TESTS_ASSUME_INTEL ? af | zf : sf | zf | af | pf },\n    { opcode: 0xF6, e: 1, fixed_g: 5, mask_flags: TESTS_ASSUME_INTEL ? af | zf : sf | zf | af | pf },\n    // div/idiv: Not a block boundary, but doesn't use control flow exceptions\n    { opcode: 0xF6, e: 1, fixed_g: 6, mask_flags: TESTS_ASSUME_INTEL ? 0 : sf | zf | af | pf, block_boundary: 1 },\n    { opcode: 0xF6, e: 1, fixed_g: 7, mask_flags: TESTS_ASSUME_INTEL ? 0 : sf | zf | af | pf, block_boundary: 1 },\n\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 0, imm1632: 1, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 1, imm1632: 1, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 2, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 3, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 4, mask_flags: TESTS_ASSUME_INTEL ? af | zf : sf | zf | af | pf, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 5, mask_flags: TESTS_ASSUME_INTEL ? af | zf : sf | zf | af | pf, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 6, mask_flags: TESTS_ASSUME_INTEL ? 0 : sf | zf | af | pf, custom: 1 },\n    { opcode: 0xF7, os: 1, e: 1, fixed_g: 7, mask_flags: TESTS_ASSUME_INTEL ? 0 : sf | zf | af | pf, custom: 1 },\n\n    { opcode: 0xF8, custom: 1 },\n    { opcode: 0xF9, custom: 1 },\n    { opcode: 0xFA, custom: 1, skip: 1 },\n    // STI: Note: Has special handling in jit in order to call handle_irqs safely\n    { opcode: 0xFB, custom: 1, custom_sti: 1, skip: 1 },\n    { opcode: 0xFC, custom: 1 },\n    { opcode: 0xFD, custom: 1 },\n\n    { opcode: 0xFE, e: 1, fixed_g: 0, custom: 1 },\n    { opcode: 0xFE, e: 1, fixed_g: 1, custom: 1 },\n    { opcode: 0xFF, os: 1, e: 1, fixed_g: 0, custom: 1 },\n    { opcode: 0xFF, os: 1, e: 1, fixed_g: 1, custom: 1 },\n    { opcode: 0xFF, os: 1, e: 1, fixed_g: 2, custom: 1, block_boundary: 1, absolute_jump: 1, skip: 1 },\n    { opcode: 0xFF, os: 1, e: 1, fixed_g: 3, block_boundary: 1, skip: 1 },\n    { opcode: 0xFF, os: 1, e: 1, fixed_g: 4, custom: 1, block_boundary: 1, absolute_jump: 1, no_next_instruction: 1, skip: 1 },\n    { opcode: 0xFF, os: 1, e: 1, fixed_g: 5, block_boundary: 1, no_next_instruction: 1, skip: 1 },\n    { opcode: 0xFF, custom: 1, os: 1, e: 1, fixed_g: 6 },\n\n    { opcode: 0x0F00, fixed_g: 0, e: 1, skip: 1, block_boundary: 1, os: 1 }, // sldt, ...\n    { opcode: 0x0F00, fixed_g: 1, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F00, fixed_g: 2, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F00, fixed_g: 3, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F00, fixed_g: 4, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F00, fixed_g: 5, e: 1, skip: 1, block_boundary: 1, os: 1 },\n\n    { opcode: 0x0F01, fixed_g: 0, e: 1, skip: 1, block_boundary: 1, os: 1 }, // sgdt, ...\n    { opcode: 0x0F01, fixed_g: 1, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F01, fixed_g: 2, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F01, fixed_g: 3, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F01, fixed_g: 4, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F01, fixed_g: 6, e: 1, skip: 1, block_boundary: 1, os: 1 },\n    { opcode: 0x0F01, fixed_g: 7, e: 1, skip: 1, block_boundary: 1, os: 1 },\n\n    { opcode: 0x0F02, os: 1, e: 1, skip: 1, block_boundary: 1 }, // lar\n    { opcode: 0x0F03, os: 1, e: 1, skip: 1, block_boundary: 1 }, // lsl\n    { opcode: 0x0F04, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F05, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F06, skip: 1, block_boundary: 1 }, // clts\n    { opcode: 0x0F07, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F08, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F09, skip: 1, block_boundary: 1 }, // wbinvd\n    { opcode: 0x0F0A, skip: 1, block_boundary: 1 },\n    // ud2\n    // Technically has a next instruction, but Linux uses this for assertions\n    // and embeds the assertion message after this instruction, which is likely\n    // the most common use case of ud2\n    { opcode: 0x0F0B, skip: 1, block_boundary: 1, custom: 1, no_next_instruction: 1 },\n    { opcode: 0x0F0C, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F0D, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F0E, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F0F, skip: 1, block_boundary: 1 },\n\n    { opcode: 0x0F18, e: 1, custom: 1 },\n    { opcode: 0x0F19, custom: 1, e: 1 },\n    { opcode: 0x0F1A, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F1B, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F1C, custom: 1, e: 1 },\n    { opcode: 0x0F1D, custom: 1, e: 1 },\n    { opcode: 0x0F1E, custom: 1, e: 1 },\n    { opcode: 0x0F1F, custom: 1, e: 1 },\n\n    { opcode: 0x0F20, ignore_mod: 1, e: 1, skip: 1, block_boundary: 1 }, // mov reg, creg\n    { opcode: 0x0F21, ignore_mod: 1, e: 1, skip: 1, block_boundary: 1 }, // mov reg, dreg\n    { opcode: 0x0F22, ignore_mod: 1, e: 1, skip: 1, block_boundary: 1 }, // mov creg, reg\n    { opcode: 0x0F23, ignore_mod: 1, e: 1, skip: 1, block_boundary: 1 }, // mov dreg, reg\n    { opcode: 0x0F24, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F25, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F26, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F27, skip: 1, block_boundary: 1 },\n\n    { opcode: 0x0F30, skip: 1, block_boundary: 1 }, // wrmsr\n    { opcode: 0x0F31, skip: 1, custom: 1 }, // rdtsc\n    { opcode: 0x0F32, skip: 1, block_boundary: 1 }, // rdmsr\n    { opcode: 0x0F33, skip: 1, block_boundary: 1 }, // rdpmc\n    { opcode: 0x0F34, skip: 1, block_boundary: 1, no_next_instruction: 1 }, // sysenter\n    { opcode: 0x0F35, skip: 1, block_boundary: 1, no_next_instruction: 1 }, // sysexit\n\n    { opcode: 0x0F36, skip: 1, block_boundary: 1 }, // ud\n    { opcode: 0x0F37, skip: 1, block_boundary: 1 }, // getsec\n\n    // ssse3+\n    { opcode: 0x0F38, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F39, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F3A, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F3B, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F3C, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F3D, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F3E, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F3F, skip: 1, block_boundary: 1 },\n\n    { opcode: 0x0FA0, os: 1, custom: 1 },\n    { opcode: 0x0FA1, os: 1, block_boundary: 1, skip: 1 }, // pop fs: block_boundary since it uses non-raising cpu exceptions\n\n    { opcode: 0x0FA2, skip: 1 },\n\n    { opcode: 0x0FA8, os: 1, custom: 1 },\n    { opcode: 0x0FA9, os: 1, block_boundary: 1, skip: 1 }, // pop gs\n\n    { opcode: 0x0FA3, os: 1, e: 1, custom: 1, skip_mem: 1 }, // bt (can also index memory, but not supported by test right now)\n    { opcode: 0x0FAB, os: 1, e: 1, custom: 1, skip_mem: 1 },\n    { opcode: 0x0FB3, os: 1, e: 1, custom: 1, skip_mem: 1 },\n    { opcode: 0x0FBB, os: 1, e: 1, custom: 1, skip_mem: 1 },\n\n    { opcode: 0x0FBA, os: 1, e: 1, fixed_g: 4, imm8: 1, custom: 1 }, // bt\n    { opcode: 0x0FBA, os: 1, e: 1, fixed_g: 5, imm8: 1, custom: 1 },\n    { opcode: 0x0FBA, os: 1, e: 1, fixed_g: 6, imm8: 1, custom: 1 },\n    { opcode: 0x0FBA, os: 1, e: 1, fixed_g: 7, imm8: 1, custom: 1 },\n\n    { opcode: 0x0FBC, os: 1, e: 1, mask_flags: of | sf | af | pf | cf, custom: 1 }, // bsf\n    { opcode: 0x0FBD, os: 1, e: 1, mask_flags: of | sf | af | pf | cf, custom: 1 },\n\n    // note: overflow flag only undefined if shift is > 1\n    { opcode: 0x0FA4, os: 1, e: 1, custom: 1, imm8: 1, mask_flags: af | of }, // shld\n    { opcode: 0x0FA5, os: 1, e: 1, custom: 1, mask_flags: af | of },\n    { opcode: 0x0FAC, os: 1, e: 1, custom: 1, imm8: 1, mask_flags: af | of },\n    { opcode: 0x0FAD, os: 1, e: 1, custom: 1, mask_flags: af | of },\n\n    { opcode: 0x0FA6, skip: 1, block_boundary: 1 }, // ud\n    { opcode: 0x0FA7, skip: 1, block_boundary: 1 }, // ud\n\n    { opcode: 0x0FAA, skip: 1 },\n\n    { opcode: 0x0FAE, e: 1, fixed_g: 0, reg_ud: 1, task_switch_test: 1, skip: 1, block_boundary: 1 }, // fxsave\n    { opcode: 0x0FAE, e: 1, fixed_g: 1, reg_ud: 1, task_switch_test: 1, skip: 1, block_boundary: 1 }, // fxrstor\n    { opcode: 0x0FAE, e: 1, fixed_g: 2, reg_ud: 1, sse: 1, skip: 1, block_boundary: 1 }, // ldmxcsr\n    { opcode: 0x0FAE, e: 1, fixed_g: 3, reg_ud: 1, sse: 1, skip: 1, block_boundary: 1 }, // stmxcsr\n\n    { opcode: 0x0FAE, e: 1, fixed_g: 4, reg_ud: 1, skip: 1, block_boundary: 1 }, // xsave (mem, not implemented)\n    { opcode: 0x0FAE, e: 1, fixed_g: 5, skip: 1, custom: 1 }, // lfence (reg, only 0), xrstor (mem, not implemented)\n    { opcode: 0x0FAE, e: 1, fixed_g: 6, skip: 1, block_boundary: 1 }, // mfence (reg, only 0), xsaveopt (mem, not implemented)\n    { opcode: 0x0FAE, e: 1, fixed_g: 7, skip: 1, block_boundary: 1 }, // sfence (reg, only 0), clflush (mem)\n\n    { opcode: 0x0FAF, os: 1, e: 1, mask_flags: TESTS_ASSUME_INTEL ? af | zf : sf | zf | af | pf, custom: 1 }, // imul\n\n    { opcode: 0x0FB0, e: 1 }, // cmxchg\n    { opcode: 0x0FB1, os: 1, e: 1, custom: 1 },\n    { opcode: 0x0FC7, e: 1, fixed_g: 1, os: 1, reg_ud: 1, custom: 1 }, // cmpxchg8b (memory)\n    { opcode: 0x0FC7, e: 1, fixed_g: 6, os: 1, mem_ud: 1, skip: 1 }, // rdrand\n\n    { opcode: 0x0FB2, block_boundary: 1, os: 1, e: 1, skip: 1 }, // lss\n    { opcode: 0x0FB4, block_boundary: 1, os: 1, e: 1, skip: 1 }, // lfs\n    { opcode: 0x0FB5, block_boundary: 1, os: 1, e: 1, skip: 1 }, // lgs\n\n    { opcode: 0x0FB6, os: 1, e: 1, custom: 1 }, // movzx\n    { opcode: 0x0FB7, os: 1, e: 1, custom: 1 },\n\n    { opcode: 0xF30FB8, os: 1, e: 1, custom: 1 }, // popcnt\n    { opcode: 0x0FB8, os: 1, e: 1, block_boundary: 1 }, // ud\n\n    { opcode: 0x0FB9, block_boundary: 1 }, // ud2\n\n    { opcode: 0x0FBE, os: 1, e: 1, custom: 1 }, // movsx\n    { opcode: 0x0FBF, os: 1, e: 1, custom: 1 },\n\n    { opcode: 0x0FC0, e: 1 }, // xadd\n    { opcode: 0x0FC1, os: 1, e: 1, custom: 1 },\n\n    { opcode: 0x0FC8, custom: 1 }, // bswap\n    { opcode: 0x0FC9, custom: 1 },\n    { opcode: 0x0FCA, custom: 1 },\n    { opcode: 0x0FCB, custom: 1 },\n    { opcode: 0x0FCC, custom: 1 },\n    { opcode: 0x0FCD, custom: 1 },\n    { opcode: 0x0FCE, custom: 1 },\n    { opcode: 0x0FCF, custom: 1 },\n\n\n    // mmx, sse\n\n    { sse: 1, opcode: 0x0F10, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F10, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F10, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F10, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F11, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F11, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F11, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F11, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F12, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F12, reg_ud: 1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F12, e: 1, custom: 1 }, // sse3\n    { sse: 1, opcode: 0xF30F12, e: 1, custom: 1 }, // sse3\n    { sse: 1, opcode: 0x0F13, reg_ud: 1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F13, reg_ud: 1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F14, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F14, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F15, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F15, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F16, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F16, reg_ud: 1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F16,  e: 1, custom: 1 }, // sse3\n    { sse: 1, opcode: 0x0F17, reg_ud: 1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F17, reg_ud: 1, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F28, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F28, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F29, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F29, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F2A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F2A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F2A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F2A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F2B, reg_ud: 1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F2B, reg_ud: 1, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F2C, e: 1 },\n    { sse: 1, opcode: 0x660F2C, e: 1 },\n    { sse: 1, opcode: 0xF20F2C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F2C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F2D, e: 1 },\n    { sse: 1, opcode: 0x660F2D, e: 1 },\n    { sse: 1, opcode: 0xF20F2D, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F2D, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F2E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F2E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F2F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F2F, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F50, mem_ud: 1, e: 1 },\n    { sse: 1, opcode: 0x660F50, mem_ud: 1, e: 1 },\n    { sse: 1, opcode: 0x0F51, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F51, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F51, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F51, e: 1, custom: 1 },\n\n    // approximation of 1/sqrt(x). Skipped because our approximation doesn't match intel's\n    { sse: 1, opcode: 0x0F52, e: 1, skip: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F52, e: 1, skip: 1, custom: 1 },\n\n    // reciprocal: approximation of 1/x. Skipped because our approximation doesn't match intel's\n    { sse: 1, opcode: 0x0F53, e: 1, skip: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F53, e: 1, skip: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F54, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F54, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F55, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F55, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F56, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F56, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F57, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F57, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F58, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F58, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F58, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F58, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F59, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F59, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F59, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F59, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F5A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F5A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F5A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F5A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F5B, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F5B, e: 1, custom: 1 },\n    // no F2 variant\n    { sse: 1, opcode: 0xF30F5B, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F5C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F5C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F5C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F5C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F5D, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F5D, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F5D, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F5D, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F5E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F5E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F5E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F5E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F5F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F5F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F5F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F5F, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x660F60, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F60, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F61, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F61, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F62, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F62, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F63, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F63, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F64, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F64, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F65, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F65, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F66, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F66, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F67, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F67, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x660F68, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F68, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F69, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F69, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F6A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F6A, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F6B, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F6B, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F6C, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F6C, e: 1, block_boundary: 1 }, // ud\n    { sse: 1, opcode: 0x660F6D, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F6D, e: 1, block_boundary: 1 }, // ud\n    { sse: 1, opcode: 0x660F6E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F6E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F6F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F6F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F6F, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F70, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F70, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20F70, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F70, e: 1, imm8: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F71, e: 1, fixed_g: 2, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F71, e: 1, fixed_g: 2, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F71, e: 1, fixed_g: 4, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F71, e: 1, fixed_g: 4, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F71, e: 1, fixed_g: 6, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F71, e: 1, fixed_g: 6, imm8: 1, mem_ud: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F72, e: 1, fixed_g: 2, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F72, e: 1, fixed_g: 2, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F72, e: 1, fixed_g: 4, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F72, e: 1, fixed_g: 4, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F72, e: 1, fixed_g: 6, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F72, e: 1, fixed_g: 6, imm8: 1, mem_ud: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F73, e: 1, fixed_g: 2, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F73, e: 1, fixed_g: 2, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F73, e: 1, fixed_g: 3, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F73, e: 1, fixed_g: 6, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F73, e: 1, fixed_g: 6, imm8: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F73, e: 1, fixed_g: 7, imm8: 1, mem_ud: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0F74, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F74, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F75, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F75, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F76, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F76, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F77, skip: 1 }, // emms (skip as it breaks gdb printing of float registers)\n\n    // vmx instructions\n    { opcode: 0x0F78, skip: 1, block_boundary: 1 },\n    { opcode: 0x0F79, skip: 1, block_boundary: 1 },\n\n    { opcode: 0x0F7A, skip: 1, block_boundary: 1 }, // ud\n    { opcode: 0x0F7B, skip: 1, block_boundary: 1 }, // ud\n\n    { sse: 1, opcode: 0x660F7C, e: 1, custom: 1 }, // sse3\n    { sse: 1, opcode: 0xF20F7C, e: 1, custom: 1 }, // sse3\n    { sse: 1, opcode: 0x660F7D, e: 1, custom: 1 }, // sse3\n    { sse: 1, opcode: 0xF20F7D, e: 1, custom: 1 }, // sse3\n\n    { opcode: 0x0F7C, skip: 1, block_boundary: 1 }, // ud\n    { opcode: 0x0F7D, skip: 1, block_boundary: 1 }, // ud\n\n    { sse: 1, opcode: 0x0F7E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F7E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F7E, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0F7F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660F7F, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30F7F, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FC2, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FC2, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20FC2, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30FC2, e: 1, imm8: 1, custom: 1 },\n\n    { opcode: 0x0FC3, e: 1, custom: 1, reg_ud: 1 }, // movnti: Uses normal registers, hence not marked as sse\n\n    { sse: 1, opcode: 0x0FC4, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FC4, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FC5, e: 1, mem_ud: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FC5, e: 1, mem_ud: 1, imm8: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FC6, e: 1, imm8: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FC6, e: 1, imm8: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FD0, skip: 1, block_boundary: 1 }, // sse3\n\n    { sse: 1, opcode: 0x0FD1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FD2, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD2, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FD3, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD3, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FD4, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD4, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FD5, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD5, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x660FD6, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20FD6, mem_ud: 1, e: 1 },\n    { sse: 1, opcode: 0xF30FD6, mem_ud: 1, e: 1 },\n    { sse: 1, opcode: 0x0FD6, e: 1, block_boundary: 1 }, // ud\n\n    { sse: 1, opcode: 0x0FD7, e: 1, mem_ud: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD7, e: 1, mem_ud: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FD8, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD8, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FD9, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FD9, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FDA, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FDA, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FDB, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FDB, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FDC, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FDC, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FDD, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FDD, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FDE, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FDE, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FDF, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FDF, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FE0, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE0, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE2, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE2, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE3, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE3, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE4, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE4, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE5, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE5, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x660FE6, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF20FE6, e: 1, custom: 1 },\n    { sse: 1, opcode: 0xF30FE6, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE6, e: 1, block_boundary: 1 }, // ud\n    { sse: 1, opcode: 0x0FE7, e: 1, reg_ud: 1 },\n    { sse: 1, opcode: 0x660FE7, e: 1, reg_ud: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FE8, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE8, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FE9, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FE9, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FEA, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FEA, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FEB, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FEB, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FEC, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FEC, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FED, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FED, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FEE, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FEE, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FEF, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FEF, e: 1, custom: 1 },\n\n    { sse: 1, opcode: 0x0FF0, skip: 1, block_boundary: 1 }, // sse3\n\n    { sse: 1, opcode: 0x0FF1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF1, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FF2, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF2, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FF3, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF3, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FF4, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF4, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FF5, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF5, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FF6, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF6, e: 1, custom: 1 },\n    // maskmovq (0FF7), maskmovdqu (660FF7) tested manually\n    // Generated tests don't setup EDI as required (yet)\n    { sse: 1, opcode: 0x0FF7, mem_ud: 1, e: 1, custom: 1, skip: 1 },\n    { sse: 1, opcode: 0x660FF7, mem_ud: 1, e: 1, custom: 1, skip: 1 },\n\n    { sse: 1, opcode: 0x0FF8, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF8, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FF9, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FF9, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FFA, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FFA, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FFB, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FFB, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FFC, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FFC, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FFD, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FFD, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x0FFE, e: 1, custom: 1 },\n    { sse: 1, opcode: 0x660FFE, e: 1, custom: 1 },\n\n    { opcode: 0x0FFF, block_boundary: 1 }, // ud\n];\n\nfor(let i = 0; i < 8; i++)\n{\n    encodings.push.apply(encodings, [\n        { opcode: 0x00 | i << 3, custom: 1, e: 1 },\n        { opcode: 0x01 | i << 3, custom: 1, os: 1, e: 1 },\n        { opcode: 0x02 | i << 3, custom: 1, e: 1 },\n        { opcode: 0x03 | i << 3, custom: 1, os: 1, e: 1 },\n        { opcode: 0x04 | i << 3, custom: 1, imm8: 1 },\n        { opcode: 0x05 | i << 3, custom: 1, os: 1, imm1632: 1 },\n\n        { opcode: 0x40 | i, os: 1, custom: 1 },\n        { opcode: 0x48 | i, os: 1, custom: 1 },\n\n        { opcode: 0x50 | i, custom: 1, os: 1 },\n        { opcode: 0x58 | i, custom: 1, os: 1 },\n\n        { opcode: 0x70 | i, block_boundary: 1, no_block_boundary_in_interpreted: 1, jump_offset_imm: 1, conditional_jump: 1, os: 1, imm8s: 1, custom: 1, skip: 1 },\n        { opcode: 0x78 | i, block_boundary: 1, no_block_boundary_in_interpreted: 1, jump_offset_imm: 1, conditional_jump: 1, os: 1, imm8s: 1, custom: 1, skip: 1 },\n\n        { opcode: 0x80, e: 1, fixed_g: i, imm8: 1, custom: 1 },\n        { opcode: 0x81, os: 1, e: 1, fixed_g: i, imm1632: 1, custom: 1 },\n        { opcode: 0x82, e: 1, fixed_g: i, imm8: 1, custom: 1 },\n        { opcode: 0x83, os: 1, e: 1, fixed_g: i, imm8s: 1, custom: 1 },\n\n        { opcode: 0xB0 | i, custom: 1, imm8: 1 },\n        { opcode: 0xB8 | i, custom: 1, os: 1, imm1632: 1 },\n\n        // note: overflow flag only undefined if shift is > 1\n        // note: the adjust flag is undefined for shifts > 0 and unaffected by rotates\n        { opcode: 0xC0, e: 1, fixed_g: i, imm8: 1, mask_flags: of | af, custom: 1 },\n        { opcode: 0xC1, os: 1, e: 1, fixed_g: i, imm8: 1, mask_flags: of | af, custom: 1 },\n        { opcode: 0xD0, e: 1, fixed_g: i, mask_flags: af, custom: 1 },\n        { opcode: 0xD1, os: 1, e: 1, fixed_g: i, mask_flags: af, custom: 1 },\n        { opcode: 0xD2, e: 1, fixed_g: i, mask_flags: of | af, custom: 1 },\n        { opcode: 0xD3, os: 1, e: 1, fixed_g: i, mask_flags: of | af, custom: 1 },\n\n        { opcode: 0x0F40 | i, e: 1, os: 1, custom: 1 },\n        { opcode: 0x0F48 | i, e: 1, os: 1, custom: 1 },\n\n        { opcode: 0x0F80 | i, block_boundary: 1, no_block_boundary_in_interpreted: 1, jump_offset_imm: 1, conditional_jump: 1, imm1632: 1, os: 1, custom: 1, skip: 1 },\n        { opcode: 0x0F88 | i, block_boundary: 1, no_block_boundary_in_interpreted: 1, jump_offset_imm: 1, conditional_jump: 1, imm1632: 1, os: 1, custom: 1, skip: 1 },\n\n        { opcode: 0x0F90 | i, e: 1, custom: 1 },\n        { opcode: 0x0F98 | i, e: 1, custom: 1 },\n    ]);\n}\n\nencodings.sort((e1, e2) => {\n    let o1 = (e1.opcode & 0xFF00) === 0x0F00 ? e1.opcode & 0xFFFF : e1.opcode & 0xFF;\n    let o2 = (e2.opcode & 0xFF00) === 0x0F00 ? e2.opcode & 0xFFFF : e2.opcode & 0xFF;\n    return o1 - o2 || e1.fixed_g - e2.fixed_g;\n});\n\nconst result = Object.freeze(encodings.map(entry => Object.freeze(entry)));\nexport default result;\n"
  },
  {
    "path": "index.html",
    "content": "<!doctype html>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n\n<title>v86</title>\n<meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,interactive-widget=resizes-content\">\n<meta name=\"description\" content=\"Run KolibriOS, Linux or Windows 98 in your browser\">\n\n<link rel=\"manifest\" href=\"manifest.json\">\n<link rel=\"apple-touch-icon\" href=\"192.png\">\n\n<script src=\"build/v86_all.js?98e7110c2\"></script>\n<link rel=\"stylesheet\" href=\"v86.css\">\n\n<div>\n    <div id=\"boot_options\">\n        <div id=\"filter\">\n            Family:\n            <label><input id=filter_linux type=checkbox> Linux</label>\n            <label><input id=filter_bsd type=checkbox> BSD</label>\n            <label title=\"Microsoft Windows\"><input id=filter_windows type=checkbox> Windows</label>\n            <label><input id=filter_unix type=checkbox> Unix-like</label>\n            <label><input id=filter_dos type=checkbox> DOS</label>\n            <label><input id=filter_custom type=checkbox> Custom</label>\n            <!--<label><input type=checkbox> Hobby OS</label>-->\n            UI:\n            <label title=\"Graphical desktop\"><input id=filter_graphical type=checkbox> Graphical</label>\n            <label title=\"Text-based or serial console only\"><input id=filter_text type=checkbox> Text</label>\n            Medium:\n            <label title=\"Boots from floppy disk\"><input id=filter_floppy type=checkbox> Floppy</label>\n            <label title=\"Boots from CD-ROM\"><input id=filter_cd type=checkbox> CD</label>\n            <label title=\"Boots from hard disk\"><input id=filter_hd type=checkbox> HD</label>\n            Size:\n            <label title=\"Fits it bootsector\"><input id=filter_bootsector type=checkbox> 512 B</label>\n            <label><input id=filter_lt5mb type=checkbox> &lt;5 MB</label>\n            <label><input id=filter_gt5mb type=checkbox> &gt;5 MB</label>\n            Status:\n            <label title=\"Most recent release is younger than 10 years\"><input id=filter_modern type=checkbox> Modern</label>\n            <label title=\"No release in 10 years\"><input id=filter_historic type=checkbox> Historic</label>\n            License:\n            <label><input id=filter_opensource type=checkbox> Open-Source</label>\n            <label><input id=filter_proprietary type=checkbox> Proprietary</label>\n            Arch:\n            <label><input id=filter_16bit type=checkbox> 16-bit</label>\n            <label><input id=filter_32bit type=checkbox> 32-bit</label>\n            <!--<label><input type=checkbox> Microkernel</label>-->\n            <!--<label title=\"Uses a saved memory dump to skip the boot process\"><input type=checkbox> skip boot process</label>-->\n            <!--<label title=\"Supports networking (in v86)\"><input type=checkbox> networking</label>\n            <label title=\"Supports audio output (in v86)\"><input type=checkbox> audio</label>\n            <label title=\"Supports the serial console (in v86)\"><input type=checkbox> serial</label>\n            <label title=\"Supports filesharing using virtio-9p (in v86)\"><input type=checkbox> 9pfs</label>-->\n            Lang:\n            <label title=\"Written in assembly language\"><input id=filter_asm type=checkbox> ASM</label>\n            <label title=\"Written in C\"><input id=filter_c type=checkbox> C</label>\n            <label title=\"Written in C++\"><input id=filter_cpp type=checkbox> C++</label>\n            <label title=\"Written in other languages\"><input id=filter_other_lang type=checkbox> Other</label>\n            <!--<label><input type=checkbox> contains: games</label>\n            <label><input type=checkbox> contains: demos</label>\n            <label><input type=checkbox> contains: compilers</label>\n            <label><input type=checkbox> contains: web browsers</label>-->\n            <div style=\"text-align: right; float: right\">\n                 <a id=\"reset_filters\">Reset filters</a> / <a href=\"#setup\">Skip to custom settings</a>\n            </div>\n            <br style=\"clear: both\">\n        </div>\n        <hr>\n\n        <div class=\"table\" id=\"oses\">\n            <div class=\"th\">\n                <div class=\"tr\">\n                    <span>Name</span>\n                    <span>Size</span>\n                    <span>UI</span>\n                    <span>Family</span>\n                    <span>Arch</span>\n                    <span>Status</span>\n                    <span>Source</span>\n                    <span>Lang</span>\n                    <span>Medium</span>\n                    <span>Notes</span>\n                </div>\n            </div>\n<a class=\"tr\" href=\"?profile=android\" id=\"start_android\"><span>Android</span> <span>54+ MB</span> <span><div class=gui_icon></div></span> <span>Linux</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>CD</span> <span>Android x86 1.6-r2</span></a>\n<a class=\"tr\" href=\"?profile=archlinux\" id=\"start_archlinux\"><span>Arch Linux</span> <span>15+ MB</span> <span><div class=tui_icon></div></span> <span>Linux</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>9pfs</span> <span>Various packages, including Xorg, Firefox and more</span></a>\n<a class=\"tr\" href=\"?profile=buildroot\" id=\"start_buildroot\"><span>Buildroot Linux</span> <span>4.9 MB</span> <span><div class=tui_icon></div></span> <span>Linux</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>bzImage</span> <span>Minimal Linux with Lua, ping, curl, telnet</span></a>\n<a class=\"tr\" href=\"?profile=dsl\" id=\"start_dsl\"><span>Damn Small Linux</span> <span>50 MB</span> <span><div class=gui_icon></div></span> <span>Linux</span> <span>32-bit</span> <span>Historic</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>4.11.rc2 with Firefox 2.0</span></a>\n<a class=\"tr\" href=\"?profile=elks\" id=\"start_elks\"><span>ELKS</span> <span>1.2 MB</span> <span><div class=tui_icon></div></span> <span>Linux-like</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>Linux for 8086</span></a>\n<a class=\"tr\" href=\"?profile=nodeos\" id=\"start_nodeos\"><span>NodeOS</span> <span>14 MB</span> <span><div class=tui_icon></div></span> <span>Linux</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>bzImage</span> <span>Linux with nodejs as /bin/init</span></a>\n<a class=\"tr\" href=\"?profile=tilck\" id=\"start_tilck\"><span>Tilck</span> <span>16 MB</span> <span><div class=tui_icon></div></span> <span>Linux-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>Tiny Linux-Compatible Kernel</span></a>\n<a class=\"tr\" href=\"?profile=freebsd\" id=\"start_freebsd\"><span>FreeBSD</span> <span>16+ MB</span> <span><div class=tui_icon></div></span> <span>BSD</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>FreeBSD 12.0</span></a>\n<a class=\"tr\" href=\"?profile=netbsd\" id=\"start_netbsd\"><span>NetBSD</span> <span>23+ MB</span> <span><div class=tui_icon></div></span> <span>BSD</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>NetBSD 4.0 with Xorg</span></a>\n<a class=\"tr\" href=\"?profile=openbsd\" id=\"start_openbsd\"><span>OpenBSD</span> <span>11+ MB</span> <span><div class=tui_icon></div></span> <span>BSD</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>OpenBSD 6.6</span></a>\n<a class=\"tr\" href=\"?profile=fiwix\" id=\"start_fiwix\"><span>FiwixOS</span> <span>4.2+ MB</span> <span><div class=tui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>With Doom</span></a>\n<a class=\"tr\" href=\"?profile=minix\" id=\"start_minix\"><span>Minix</span> <span>30+ MB</span> <span><div class=tui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Minix 3.3</span></a>\n<a class=\"tr\" href=\"?profile=redox\" id=\"start_redox\"><span>Redox</span> <span>31+ MB</span> <span><div class=gui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>Rust</span> <span>HD</span> <span>A Unix-like microkernel OS written in Rust</span></a>\n<a class=\"tr\" href=\"?profile=serenity\" id=\"start_serenity\"><span>SerenityOS</span> <span>16+ MB</span> <span><div class=gui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>HD</span> <span>Web browser, various games and demos</span></a>\n<a class=\"tr\" href=\"?profile=sortix\" id=\"start_sortix\"><span>Sortix</span> <span>67 MB</span> <span><div class=tui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>CD</span> <span>A small self-hosting Unix-like operating system</span></a>\n<a class=\"tr\" href=\"?profile=soso\" id=\"start_soso\"><span>Soso</span> <span>7.6 MB</span> <span><div class=gui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span> A Simple Unix-like operating system</span></a>\n<a class=\"tr\" href=\"?profile=syllable\" id=\"start_syllable\"><span>Syllable</span> <span>28+ MB</span> <span><div class=gui_icon></div></span> <span>Unix-like</span> <span>32-bit</span> <span>Historic</span> <span>Open-source</span> <span>C++</span> <span>HD</span> <span>A user friendly, POSIX compatible OS</span></a>\n<a class=\"tr\" href=\"?profile=unix-v7\" id=\"start_unix-v7\"><span>Unix V7</span> <span>0.5+ MB</span> <span><div class=tui_icon></div></span> <span>Unix</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C</span> <span>HD</span> <span>Unix V7 port for x86, including Amsterdam Compiler Kit</span></a>\n<a class=\"tr\" href=\"?profile=beos\" id=\"start_beos\"><span>BeOS 5</span> <span>34+ MB</span> <span><div class=gui_icon></div></span> <span>BeOS</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C++</span> <span>HD</span> <span>With Opera 3.62, NetPositive</span></a>\n<a class=\"tr\" href=\"?profile=haiku\" id=\"start_haiku\"><span>Haiku</span> <span>41+ MB</span> <span><div class=gui_icon></div></span> <span>BeOS</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>HD</span> <span>Networking (WebPositive), OCaml, 2048, NetHack</span></a>\n<a class=\"tr\" href=\"?profile=aros-broadway\" id=\"start_aros-broadway\"><span>AROS Broadway</span> <span>25+ MB</span> <span><div class=gui_icon></div></span> <span>AmigaOS</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>AmigaOS-like graphical OS</span></a>\n<a class=\"tr\" href=\"?profile=icaros\" id=\"start_icaros\"><span>Icaros Desktop</span> <span>60+ MB</span> <span><div class=gui_icon></div></span> <span>AmigaOS</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>AmigaOS-like graphical OS</span></a>\n<a class=\"tr\" href=\"?profile=tinyaros\" id=\"start_tinyaros\"><span>Tiny Aros</span> <span>17+ MB</span> <span><div class=gui_icon></div></span> <span>AmigaOS</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>AmigaOS-like graphical OS</span></a>\n<a class=\"tr\" href=\"?profile=9front\" id=\"start_9front\"><span>9front</span> <span>5.2+ MB</span> <span><div class=gui_icon></div></span> <span>Plan 9</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>An actively maintained fork of Plan 9</span></a>\n<a class=\"tr\" href=\"?profile=9legacy\" id=\"start_9legacy\"><span>9legacy</span> <span>13 MB</span> <span><div class=gui_icon></div></span> <span>Plan 9</span> <span>32-bit</span> <span>Historic</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>A set of patches based on the latest release of Plan 9</span></a>\n<a class=\"tr\" href=\"?profile=reactos\" id=\"start_reactos\"><span>ReactOS</span> <span>17+ MB</span> <span><div class=gui_icon></div></span> <span>Windows-like</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>HD</span> <span>QtWeb, LBreakout2, OpenTTD, Bochs, TCC</span></a>\n<a class=\"tr\" href=\"?profile=windows1\" id=\"start_windows1\"><span>Windows 1.01</span> <span>0.7 MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>Floppy</span> <span>The first release version of Windows</span></a>\n<a class=\"tr\" href=\"?profile=windows2\" id=\"start_windows2\"><span>Windows 2.03</span> <span>1.8 MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>HD</span> <span>Reversi, Paint</span></a>\n<a class=\"tr\" href=\"?profile=windows30\" id=\"start_windows30\"><span>Windows 3.0</span> <span>6.5 MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>CD</span> <span>CorelDRAW! 2.0, Reversi</span></a>\n<a class=\"tr\" href=\"?profile=windows31\" id=\"start_windows31\"><span>Windows 3.1</span> <span>17 MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>HD</span> <span>QBasic, Minesweeper, Solitaire</span></a>\n<a class=\"tr\" href=\"?profile=windows95\" id=\"start_windows95\"><span>Windows 95</span> <span>19+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>HD</span> <span>Age of Empires, FASM, POV-Ray, Hover!</span></a>\n<a class=\"tr\" href=\"?profile=windows98\" id=\"start_windows98\"><span>Windows 98</span> <span>13+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>HD</span> <span>FreeCell, Hearts, sheep.exe, IE 5</span></a>\n<a class=\"tr\" href=\"?profile=windows-me\" id=\"start_windows-me\"><span>Windows ME</span> <span>14+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>HD</span> <span>Visual Basic, Office 97</span></a>\n<a class=\"tr\" href=\"?profile=windowsnt3\" id=\"start_windowsnt3\"><span>Windows NT 3.1</span> <span>18+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C++</span> <span>HD</span> <span>The first retail version of Windows NT</span></a>\n<a class=\"tr\" href=\"?profile=windowsnt35\" id=\"start_windowsnt35\"><span>Windows NT 3.51</span> <span>28+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C++</span> <span>HD</span> <span>Internet Explorer 3 and Visual FoxPro 3.0</span></a>\n<a class=\"tr\" href=\"?profile=windowsnt4\" id=\"start_windowsnt4\"><span>Windows NT 4.0</span> <span>16+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C++</span> <span>HD</span> <span>Windows NT 4.0 Service Pack 1</span></a>\n<a class=\"tr\" href=\"?profile=windows2000\" id=\"start_windows2000\"><span>Windows 2000</span> <span>28+ MB</span> <span><div class=gui_icon></div></span> <span>Windows</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C++</span> <span>HD</span> <span>IE 6, K-Meleon, Winamp, Delphi, NetHack and more</span></a>\n<a class=\"tr\" href=\"?profile=86dos\" id=\"start_86dos\"><span>86-DOS</span> <span>0.1 MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM</span> <span>Floppy</span> <span>86-DOS version 1.0</span></a>\n<a class=\"tr\" href=\"?profile=ibm-exploring\" id=\"start_ibm-exploring\"><span>Exploring IBM</span> <span>0.1 MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM</span> <span>Floppy</span> <span>Learning program on how to use a computer</span></a>\n<a class=\"tr\" href=\"?profile=freedos\" id=\"start_freedos\"><span>FreeDOS</span> <span>0.6 MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM, C</span> <span>Floppy</span> <span>nasm, vim, debug.com, Rogue, various demos</span></a>\n<a class=\"tr\" href=\"?profile=freegem\" id=\"start_freegem\"><span>FreeGEM</span> <span>1.6+ MB</span> <span><div class=gui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Open-source</span> <span>ASM, C</span> <span>HD</span> <span>Graphical desktop for FreeDOS</span></a>\n<a class=\"tr\" href=\"?profile=xcom\" id=\"start_xcom\"><span>Xcom</span> <span>1.3 MB</span> <span><div class=gui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Open-source</span> <span>ASM, C</span> <span>Floppy</span> <span>Graphical desktop for FreeDOS</span></a>\n<a class=\"tr\" href=\"?profile=msdos4\" id=\"start_msdos4\"><span>MS-DOS 4</span> <span>0.5 MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM</span> <span>Floppy</span> <span>Contains EDLIN</span></a>\n<a class=\"tr\" href=\"?profile=msdos\" id=\"start_msdos\"><span>MS-DOS 6.22</span> <span>2.4+ MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM</span> <span>HD</span> <span>Doom, Sim City, OCaml 1.0, Turbo C and more</span></a>\n<a class=\"tr\" href=\"?profile=pcmos\" id=\"start_pcmos\"><span>PC-MOS/386</span> <span>0.7 MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>ASM, C</span> <span>Floppy</span> <span>Multi-user and multi-tasking OS</span></a>\n<a class=\"tr\" href=\"?profile=psychdos\" id=\"start_psychdos\"><span>PsychDOS</span> <span>4.6+ MB</span> <span><div class=gui_icon></div></span> <span>DOS</span> <span>16-bit</span> <span>Historic</span> <span>Open-source</span> <span>ASM</span> <span>HD</span> <span>ANSI-like graphical desktop for FreeDOS</span></a>\n<a class=\"tr\" href=\"?profile=leetos\" id=\"start_leetos\"><span>lEEt/OS</span> <span>0.5 MB</span> <span><div class=tui_icon></div></span> <span>DOS</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM, C</span> <span>Floppy</span> <span>Graphical desktop for ST-DOS</span></a>\n<a class=\"tr\" href=\"?profile=bleskos\" id=\"start_bleskos\"><span>BleskOS</span> <span>0.2 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Alternative OS for older PCs</span></a>\n<a class=\"tr\" href=\"?profile=bluejay\" id=\"start_bluejay\"><span>Blue Jay</span> <span>83 KB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Floppy</span> <span>Based on MikeOS</span></a>\n<a class=\"tr\" href=\"?profile=boneos\" id=\"start_boneos\"><span>BoneOS</span> <span>3.0 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Simple hobby OS</span></a>\n<a class=\"tr\" href=\"?profile=bootchess\" id=\"start_bootchess\"><span>BootChess</span> <span>4.0 KB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Floppy</span> <span>Chess in a bootsector</span></a>\n<a class=\"tr\" href=\"?profile=catk\" id=\"start_catk\"><span>CatK</span> <span>3.2 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Simple Unix-like kernel</span></a>\n<a class=\"tr\" href=\"?profile=crazierl\" id=\"start_crazierl\"><span>Crazierl</span> <span>11 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C, Erlang</span> <span>Multiboot</span> <span>An Erlang Operating System</span></a>\n<a class=\"tr\" href=\"?profile=duskos\" id=\"start_duskos\"><span>Dusk OS</span> <span>0.4 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>A 32-bit Forth</span></a>\n<a class=\"tr\" href=\"?profile=floppybird\" id=\"start_floppybird\"><span>Floppy Bird</span> <span>6.5 KB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>Floppy</span> <span>Flappy Bird game in a bootsector</span></a>\n<a class=\"tr\" href=\"?profile=helenos\" id=\"start_helenos\"><span>HelenOS</span> <span>7.9 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>A microkernel-based multiserver OS</span></a>\n<a class=\"tr\" href=\"?profile=hello-v86\" id=\"start_hello-v86\"><span>Hello v86</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>Small bootsector demo for v86</span></a>\n<a class=\"tr\" href=\"?profile=house\" id=\"start_house\"><span>House</span> <span>1.1 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>Haskell</span> <span>Floppy</span> <span>Haskell User's Operating System and Environment</span></a>\n<a class=\"tr\" href=\"?profile=jx\" id=\"start_jx\"><span>JX</span> <span>1.3 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>Java</span> <span>Floppy</span> <span>Java-based operating system</span></a>\n<a class=\"tr\" href=\"?profile=kolibrios\" id=\"start_kolibrios\"><span>KolibriOS</span> <span>1.3 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Floppy</span> <span>Various apps, games and demos</span></a>\n<a class=\"tr\" href=\"?profile=littlekernel\" id=\"start_littlekernel\"><span>Little Kernel</span> <span>0.4 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>Multiboot</span> <span>An embedded kernel designed for small systems</span></a>\n<a class=\"tr\" href=\"?profile=mcp\" id=\"start_mcp\"><span>M/CP</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>The Minimal Control Program</span></a>\n<a class=\"tr\" href=\"?profile=mikeos\" id=\"start_mikeos\"><span>MikeOS</span> <span>0.2 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>CD</span> <span>Contains a FORTH and BASIC interpreter and several games</span></a>\n<a class=\"tr\" href=\"?profile=mobius\" id=\"start_mobius\"><span>Mobius</span> <span>1.3 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>Floppy</span> <span>POSIX and Win32 compatible OS</span></a>\n<a class=\"tr\" href=\"?profile=mu\" id=\"start_mu\"><span>Mu</span> <span>0.2 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>Minimal-dependency hobbyist computing stack</span></a>\n<a class=\"tr\" href=\"?profile=nanoshell\" id=\"start_nanoshell\"><span>NanoShell</span> <span>3.5 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Multi-tasked 32-bit OS with Win9x-like GUI</span></a>\n<a class=\"tr\" href=\"?profile=newos\" id=\"start_newos\"><span>NewOS</span> <span>0.6 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>Floppy</span> <span>Cross-platform portable OS</span></a>\n<a class=\"tr\" href=\"?profile=nopeos\" id=\"start_nopeos\"><span>Nope OS</span> <span>92 KB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Simple OS with BASIC interpreter</span></a>\n<a class=\"tr\" href=\"?profile=oberon\" id=\"start_oberon\"><span>Oberon</span> <span>1.6 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Historic</span> <span>Open-source</span> <span>Oberon</span> <span>HD</span> <span>Native Oberon 2.3.6</span></a>\n<a class=\"tr\" href=\"?profile=qnx\" id=\"start_qnx\"><span>QNX 4.05</span> <span>1.4 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C</span> <span>Floppy</span> <span>1999 demo disk</span></a>\n<a class=\"tr\" href=\"?profile=sectorlisp\" id=\"start_sectorlisp\"><span>SectorLISP</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>Bootstrapping LISP in a Boot Sector</span></a>\n<a class=\"tr\" href=\"?profile=skift\" id=\"start_skift\"><span>Skift</span> <span>44 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C++</span> <span>CD</span> <span>A hobby OS built from scratch using C/C++</span></a>\n<a class=\"tr\" href=\"?profile=snowdrop\" id=\"start_snowdrop\"><span>Snowdrop</span> <span>0.4 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Floppy</span> <span>16-bit hobby OS with GUI</span></a>\n<a class=\"tr\" href=\"?profile=solos\" id=\"start_solos\"><span>Sol OS</span> <span>0.3 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Proprietary</span> <span>ASM</span> <span>Floppy</span> <span>Simple graphical OS</span></a>\n<a class=\"tr\" href=\"?profile=stillalive\" id=\"start_stillalive\"><span>Still Alive</span> <span>10 KB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>Floppy</span> <span>Bootable demo that plays \"Still Alive\" from Portal</span></a>\n<a class=\"tr\" href=\"?profile=t3xforth\" id=\"start_t3xforth\"><span>T3XFORTH</span> <span>59 KB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Historic</span> <span>Open-source</span> <span>ASM</span> <span>Floppy</span> <span>An old-school, plain vanilla FORTH system</span></a>\n<a class=\"tr\" href=\"?profile=tetros\" id=\"start_tetros\"><span>TetrOS</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>Tetris that fits into the boot sector</span></a>\n<a class=\"tr\" href=\"?profile=toaruos\" id=\"start_toaruos\"><span>ToaruOS</span> <span>6.3 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>A hobby OS written from scratch</span></a>\n<a class=\"tr\" href=\"?profile=bootbasic\" id=\"start_bootbasic\"><span>bootBASIC</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>A BASIC in 512 bytes of x86 machine code</span></a>\n<a class=\"tr\" href=\"?profile=pillman\" id=\"start_pillman\"><span>Pillman</span> <span>512 B</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>A yellow thing eats pills and is chased by monsters</span></a>\n<a class=\"tr\" href=\"?profile=bootlogo\" id=\"start_bootlogo\"><span>bootLogo</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>Logo language in 508 bytes</span></a>\n<a class=\"tr\" href=\"?profile=bootrogue\" id=\"start_bootrogue\"><span>bootRogue</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>A roguelike game that fits in a boot sector</span></a>\n<a class=\"tr\" href=\"?profile=dino\" id=\"start_dino\"><span>dino</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>Chrome's t-rex based bootsector game</span></a>\n<a class=\"tr\" href=\"?profile=invaders\" id=\"start_invaders\"><span>Invaders</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>Invaders in a bootsector</span></a>\n<a class=\"tr\" href=\"?profile=sanos\" id=\"start_sanos\"><span>Sanos</span> <span>0.5 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>Minimalistic 32-bit x86 OS</span></a>\n<a class=\"tr\" href=\"?profile=sectorforth\" id=\"start_sectorforth\"><span>sectorforth</span> <span>512 B</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>16-bit</span> <span>Modern</span> <span>Open-source</span> <span>ASM</span> <span>Bootsector</span> <span>16-bit x86 Forth in a bootsector</span></a>\n<a class=\"tr\" href=\"?profile=dancy\" id=\"start_dancy\"><span>Dancy</span> <span>1.3 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Dancy Operating System</span></a>\n<a class=\"tr\" href=\"?profile=curios\" id=\"start_curios\"><span>CuriOS</span> <span>6.6 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>HD</span> <span>Simple GUI based OS inspired by AmigaOS</span></a>\n<a class=\"tr\" href=\"?profile=os64\" id=\"start_os64\"><span>OS64</span> <span>2.2 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Commodore 64 emulator OS for x86 (slow)</span></a>\n<a class=\"tr\" href=\"?profile=netboot.xyz\" id=\"start_netboot.xyz\"><span>netboot.xyz</span> <span>1.0 MB</span> <span><div class=tui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C</span> <span>CD</span> <span>Netboot into various operating systems (slow)</span></a>\n<a class=\"tr\" href=\"?profile=squeaknos\" id=\"start_squeaknos\"><span>SqueakNOS</span> <span>20 MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Modern</span> <span>Open-source</span> <span>C, Smalltalk</span> <span>CD</span> <span>Smalltalk as a Standalone OS</span></a>\n<a class=\"tr\" href=\"?profile=chokanji4\" id=\"start_chokanji4\"><span>Chokanji 4</span> <span>13+ MB</span> <span><div class=gui_icon></div></span> <span>Custom</span> <span>32-bit</span> <span>Historic</span> <span>Proprietary</span> <span>C</span> <span>HD</span> <span>A Japanese OS based on the TRON project</span></a>\n        </div>\n\n        <hr>\n        <h4 id=\"setup\">Setup</h4>\n        <table>\n            <tr>\n                <td width=\"350\"><label for=\"cdrom_image\">CD image</label></td>\n                <td>\n                    <input type=\"file\" id=\"cdrom_image\">\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"floppy_image\">Floppy disk image</label></td>\n                <td><input type=\"file\" id=\"fda_image\"> or <a id=\"fda_toggle_empty_disk\">create empty floppy disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"floppy_image\">Second floppy disk image</label></td>\n                <td><input type=\"file\" id=\"fdb_image\"> or <a id=\"fdb_toggle_empty_disk\">create empty floppy disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"hda_image\">Hard disk image</label></td>\n                <td><input type=\"file\" id=\"hda_image\"> or <a id=\"hda_toggle_empty_disk\">create empty hard disk</a><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"hdb_image\">Second hard disk image</label></td>\n                <td><input type=\"file\" id=\"hdb_image\"> or <a id=\"hdb_toggle_empty_disk\">create empty hard disk</a><br></td>\n            </tr>\n\n            <!--\n            <tr>\n                <td><label for=\"multiboot_image\">Multiboot kernel image (experimental)</td>\n                <td><input type=\"file\" id=\"multiboot_image\"><br></td>\n            </tr>\n            -->\n\n            <tr>\n                <td><label for=\"bzimage\">Kernel image (bzimage)</label></td>\n                <td><input type=\"file\" id=\"bzimage\"><br></td>\n            </tr>\n            <tr>\n                <td><label for=\"initrd\">initrd</label></td>\n                <td><input type=\"file\" id=\"initrd\"><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"bios\">BIOS</label></td>\n                <td><input type=\"file\" id=\"bios\"><br></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"vga_bios\">VGA BIOS</label></td>\n                <td><input type=\"file\" id=\"vga_bios\"><br></td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><small>Disk images are not uploaded to the server</small><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"memory_size\">Memory size</label></td>\n                <td>\n                    <input id=\"memory_size\" type=\"number\" value=\"128\" min=\"16\" max=\"2048\" step=\"16\"> MB<br>\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"vga_memory_size\">Video Memory size</label></td>\n                <td>\n                    <input id=\"vga_memory_size\" type=\"number\" value=\"8\" min=\"1\" max=\"128\" step=\"1\"> MB<br>\n                </td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"relay_url\">Networking proxy</label><br>\n                    Presets: <a id=\"network_none\">none</a>, <a id=\"network_inbrowser\">inbrowser</a>, <a id=\"network_relay\">public relay</a>, <a id=\"network_wisp\">wisp</a>, <a id=\"network_fetch\">fetch</a></td>\n                <td>\n                    <input id=\"relay_url\" type=\"text\" value=\"wss://relay.widgetry.org/\">\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"net_device_type\">Network Device Type</label></td>\n                <td>\n                    <select id=\"net_device_type\">\n                        <option value=\"ne2k\">ne2k</option>\n                        <option value=\"virtio\">virtio</option>\n                    </select>\n                 </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"mtu\">Network MTU (only used for virtio)</label></td>\n                <td>\n                    <input id=\"mtu\" type=\"number\" value=\"1500\" min=\"68\" max=\"65535\" step=\"1\"> B<br>\n                 </td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"disable_audio\">Disable audio</label></td>\n                <td>\n                    <input id=\"disable_audio\" type=\"checkbox\"><br>\n                </td>\n            </tr>\n\n            <tr>\n                <td><label for=\"acpi\">Enable ACPI (experimental)</label></td>\n                <td>\n                    <input id=\"acpi\" type=\"checkbox\"><br>\n                </td>\n            </tr>\n\n            <tr>\n                <td colspan=\"2\"><hr></td>\n            </tr>\n\n            <tr>\n                <td><label for=\"boot_order\">Boot order</label></td>\n                <td>\n                    <select id=\"boot_order\">\n                        <option value=\"0\">Auto</option>\n                        <option value=\"213\">CD / Floppy / Hard Disk</option>\n                        <option value=\"123\">CD / Hard Disk / Floppy</option>\n                        <option value=\"231\">Floppy / CD / Hard Disk</option>\n                        <option value=\"321\">Floppy / Hard Disk / CD</option>\n                        <option value=\"312\">Hard Disk / Floppy / CD</option>\n                        <option value=\"132\">Hard Disk / CD / Floppy</option>\n                    </select>\n                 </td>\n            </tr>\n        </table>\n\n        <br>\n        <button id=\"start_emulation\">Start Emulation</button>\n    </div>\n\n    <div id=\"runtime_options\" style=\"display: none\">\n        <button id=\"run\">Pause</button>\n        <button id=\"reset\">Reset</button>\n        <button id=\"exit\">Exit</button>\n        <button id=\"ctrlaltdel\">Send Ctrl-Alt-Del</button>\n        <button id=\"alttab\">Send Alt-Tab</button>\n        <button id=\"get_fda_image\">Get floppy image</button>\n        <button id=\"get_fdb_image\">Get second floppy image</button>\n        <button id=\"get_hda_image\">Get hard disk image</button>\n        <button id=\"get_hdb_image\">Get second hard disk image</button>\n        <button id=\"get_cdrom_image\">Get CD-ROM image</button>\n        <button id=\"change_fda_image\">Insert floppy image</button>\n        <button id=\"change_fdb_image\">Insert second floppy image</button>\n        <button id=\"change_cdrom_image\" title=\"Either a single .iso file or multiple files of any type\">Insert CD image</button>\n        <button id=\"save_state\">Save State</button>\n        <button id=\"load_state\">Load State</button>\n        <button id=\"memory_dump\">Memory Dump</button>\n        <button id=\"capture_network_traffic\" title=\"In wireshark: file -> import from hex -> tick direction indication, timestamp %s.%f\">Capture network traffic</button>\n        <button id=\"toggle_mouse\">Disable mouse</button>\n        <button id=\"lock_mouse\">Lock mouse</button>\n        <button id=\"fullscreen\">Go fullscreen</button>\n        <button id=\"take_screenshot\">Take screenshot</button>\n        <button id=\"mute\">Mute</button>\n        <button id=\"toggle_theatre\">Enable theatre mode</button>\n        <button style=\"display: none\" id=\"toggle_zoom_to_fit\">Enable zoom to fit</button>\n\n        <label>\n            Scale:\n            <input type=\"number\" min=\"0.25\" step=\"0.25\" value=\"1.0\" id=\"scale\" style=\"width: 50px\">\n        </label>\n    </div>\n    <pre style=\"display: none\" id=\"loading\"></pre>\n</div>\n\n<div id=\"screen_container\" style=\"display: none\">\n    <div id=\"screen\"></div>\n    <canvas id=\"vga\"></canvas>\n    <div style=\"position: absolute; top: 0; z-index: 10\">\n        <textarea class=\"phone_keyboard\"></textarea>\n    </div>\n</div>\n\n\n<div id=\"runtime_infos\" style=\"display: none\">\n    Running: <span id=\"running_time\">0s</span> <br>\n    Speed: <span id=\"speed\">0</span> mIPS<br>\n    Avg speed: <span id=\"avg_speed\">0</span> mIPS<br>\n    <br>\n    <div id=\"info_storage\" style=\"display: none\">\n        <b>IDE device<span id=\"ide_type\"></span></b><br>\n        Sectors read: <span id=\"info_storage_sectors_read\">0</span><br>\n        Bytes read: <span id=\"info_storage_bytes_read\">0</span><br>\n        Sectors written: <span id=\"info_storage_sectors_written\">0</span><br>\n        Bytes written: <span id=\"info_storage_bytes_written\">0</span><br>\n        Status: <span id=\"info_storage_status\"></span><br>\n        <br>\n    </div>\n    <div id=\"info_filesystem\" style=\"display: none\">\n        <b>9p Filesystem</b><br>\n        Bytes read: <span id=\"info_filesystem_bytes_read\">0</span><br>\n        Bytes written: <span id=\"info_filesystem_bytes_written\">0</span><br>\n        <div style=\"white-space: nowrap; overflow-x: hidden\">Last file: <span id=\"info_filesystem_last_file\"></span></div>\n        Status: <span id=\"info_filesystem_status\"></span><br>\n        <br>\n    </div>\n    <div id=\"info_network\" style=\"display: none\">\n        <b>Network</b><br>\n        Bytes received: <span id=\"info_network_bytes_received\">0</span><br>\n        Bytes transmitted: <span id=\"info_network_bytes_transmitted\">0</span><br>\n        <br>\n    </div>\n    <b>VGA</b><br>\n    Mode: <span id=\"info_vga_mode\"></span><br>\n    Resolution: <span id=\"info_res\">-</span><br>\n    <br>\n    Mouse: <span id=\"info_mouse_enabled\">No</span><br>\n\n    <div id=\"description\" style=\"display: none\"><br></div>\n</div>\n\n<div id=\"filesystem_panel\" style=\"display: none\">\n    <label>\n        Send files to emulator<br>\n        <input type=\"file\" id=\"filesystem_send_file\" multiple>\n    </label>\n    <br><br>\n    <label>\n        Get file from emulator<br>\n        <input type=\"text\" id=\"filesystem_get_file\" placeholder=\"Absolute path\">\n    </label>\n</div>\n\n<br style=\"clear: both\"><br>\n\n<div id=\"terminal\"></div>\n\n<button id=\"toggle_ui\" style=\"display: none\">Hide UI</button>\n<div id=\"theatre_background\" style=\"display: none\"></div>\n\n<br style=\"clear: both\">\n<code>Version: <a id=\"version\" href=\"https://github.com/copy/v86/commits/98e7110c2\">98e7110c2</a> (Feb 16, 2021 12:02)</code>\n\n<hr>\n<a href=\"debug.html\">Enable debug</a>\n&mdash;\n<a href=\"https://github.com/copy/v86/blob/master/Readme.md\">Readme</a>\n&mdash;\n<a href=\"https://github.com/copy/v86\">Project on Github</a>\n&mdash;\n<a href=\"https://github.com/copy/v86#compatibility\">Compatibility</a>\n"
  },
  {
    "path": "lib/9p.js",
    "content": "// -------------------------------------------------\n// --------------------- 9P ------------------------\n// -------------------------------------------------\n// Implementation of the 9p filesystem device following the\n// 9P2000.L protocol ( https://code.google.com/p/diod/wiki/protocol )\n\nimport { LOG_9P } from \"./../src/const.js\";\nimport { VirtIO, VIRTIO_F_VERSION_1, VIRTIO_F_RING_EVENT_IDX, VIRTIO_F_RING_INDIRECT_DESC } from \"../src/virtio.js\";\nimport { S_IFREG, S_IFDIR, STATUS_UNLINKED } from \"./filesystem.js\";\nimport * as marshall from \"../lib/marshall.js\";\nimport { dbg_log, dbg_assert } from \"../src/log.js\";\nimport { h } from \"../src/lib.js\";\n\n// For Types Only\nimport { CPU } from \"../src/cpu.js\";\nimport { BusConnector } from \"../src/bus.js\";\nimport { FS } from \"./filesystem.js\";\n\n/**\n * @const\n * More accurate filenames in 9p debug messages at the cost of performance.\n */\nconst TRACK_FILENAMES = false;\n\n// Feature bit (bit position) for mount tag.\nconst VIRTIO_9P_F_MOUNT_TAG = 0;\n// Assumed max tag length in bytes.\nconst VIRTIO_9P_MAX_TAGLEN = 254;\n\nconst MAX_REPLYBUFFER_SIZE = 16 * 1024 * 1024;\n\n// TODO\n// flush\n\nexport const EPERM = 1;       /* Operation not permitted */\nexport const ENOENT = 2;      /* No such file or directory */\nexport const EEXIST = 17;      /* File exists */\nexport const EINVAL = 22;     /* Invalid argument */\nexport const EOPNOTSUPP = 95;  /* Operation is not supported */\nexport const ENOTEMPTY = 39;  /* Directory not empty */\nexport const EPROTO    = 71;  /* Protocol error */\n\nvar P9_SETATTR_MODE = 0x00000001;\nvar P9_SETATTR_UID = 0x00000002;\nvar P9_SETATTR_GID = 0x00000004;\nvar P9_SETATTR_SIZE = 0x00000008;\nvar P9_SETATTR_ATIME = 0x00000010;\nvar P9_SETATTR_MTIME = 0x00000020;\nvar P9_SETATTR_CTIME = 0x00000040;\nvar P9_SETATTR_ATIME_SET = 0x00000080;\nvar P9_SETATTR_MTIME_SET = 0x00000100;\n\nvar P9_STAT_MODE_DIR = 0x80000000;\nvar P9_STAT_MODE_APPEND = 0x40000000;\nvar P9_STAT_MODE_EXCL = 0x20000000;\nvar P9_STAT_MODE_MOUNT = 0x10000000;\nvar P9_STAT_MODE_AUTH = 0x08000000;\nvar P9_STAT_MODE_TMP = 0x04000000;\nvar P9_STAT_MODE_SYMLINK = 0x02000000;\nvar P9_STAT_MODE_LINK = 0x01000000;\nvar P9_STAT_MODE_DEVICE = 0x00800000;\nvar P9_STAT_MODE_NAMED_PIPE = 0x00200000;\nvar P9_STAT_MODE_SOCKET = 0x00100000;\nvar P9_STAT_MODE_SETUID = 0x00080000;\nvar P9_STAT_MODE_SETGID = 0x00040000;\nvar P9_STAT_MODE_SETVTX = 0x00010000;\n\nexport const P9_LOCK_TYPE_RDLCK = 0;\nexport const P9_LOCK_TYPE_WRLCK = 1;\nexport const P9_LOCK_TYPE_UNLCK = 2;\nconst P9_LOCK_TYPES = [\"shared\", \"exclusive\", \"unlock\"];\n\nconst P9_LOCK_FLAGS_BLOCK = 1;\nconst P9_LOCK_FLAGS_RECLAIM = 2;\n\nexport const P9_LOCK_SUCCESS = 0;\nexport const P9_LOCK_BLOCKED = 1;\nexport const P9_LOCK_ERROR = 2;\nexport const P9_LOCK_GRACE = 3;\n\nvar FID_NONE = -1;\nvar FID_INODE = 1;\nvar FID_XATTR = 2;\n\nfunction range(size)\n{\n    return Array.from(Array(size).keys());\n}\n\n/**\n * @param {CPU} cpu\n * @param {Function} receive\n */\nfunction init_virtio(cpu, configspace_taglen, configspace_tagname, receive)\n{\n    const virtio = new VirtIO(cpu,\n    {\n        name: \"virtio-9p\",\n        pci_id: 0x06 << 3,\n        device_id: 0x1049,\n        subsystem_device_id: 9,\n        common:\n        {\n            initial_port: 0xA800,\n            queues:\n            [\n                {\n                    size_supported: 32,\n                    notify_offset: 0,\n                },\n            ],\n            features:\n            [\n                VIRTIO_9P_F_MOUNT_TAG,\n                VIRTIO_F_VERSION_1,\n                VIRTIO_F_RING_EVENT_IDX,\n                VIRTIO_F_RING_INDIRECT_DESC,\n            ],\n            on_driver_ok: () => {},\n        },\n        notification:\n        {\n            initial_port: 0xA900,\n            single_handler: false,\n            handlers:\n            [\n                (queue_id) =>\n                {\n                    if(queue_id !== 0)\n                    {\n                        dbg_assert(false, \"Virtio9P Notified for non-existent queue: \" + queue_id +\n                            \" (expected queue_id of 0)\");\n                        return;\n                    }\n                    const virtqueue = virtio.queues[0];\n                    while(virtqueue.has_request())\n                    {\n                        const bufchain = virtqueue.pop_request();\n                        receive(bufchain);\n                    }\n                    virtqueue.notify_me_after(0);\n                    // Don't flush replies here: async replies are not completed yet.\n                },\n            ],\n        },\n        isr_status:\n        {\n            initial_port: 0xA700,\n        },\n        device_specific:\n        {\n            initial_port: 0xA600,\n            struct:\n            [\n                {\n                    bytes: 2,\n                    name: \"mount tag length\",\n                    read: () => configspace_taglen,\n                    write: data => { /* read only */ },\n                },\n            ].concat(range(VIRTIO_9P_MAX_TAGLEN).map(index =>\n                ({\n                    bytes: 1,\n                    name: \"mount tag name \" + index,\n                    // Note: configspace_tagname may have changed after set_state\n                    read: () => configspace_tagname[index] || 0,\n                    write: data => { /* read only */ },\n                })\n            )),\n        },\n    });\n    return virtio;\n}\n\n/**\n * @constructor\n *\n * @param {FS} filesystem\n * @param {CPU} cpu\n */\nexport function Virtio9p(filesystem, cpu, bus) {\n    /** @type {FS} */\n    this.fs = filesystem;\n\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n\n    this.configspace_tagname = [0x68, 0x6F, 0x73, 0x74, 0x39, 0x70]; // \"host9p\" string\n    this.configspace_taglen = this.configspace_tagname.length; // num bytes\n\n    this.virtio = init_virtio(cpu, this.configspace_taglen, this.configspace_tagname, this.ReceiveRequest.bind(this));\n    this.virtqueue = this.virtio.queues[0];\n\n    this.VERSION = \"9P2000.L\";\n    this.BLOCKSIZE = 8192; // Let's define one page.\n    this.msize = 8192; // maximum message size\n    this.replybuffer = new Uint8Array(this.msize*2); // Twice the msize to stay on the safe site\n    this.replybuffersize = 0;\n    this.fids = [];\n}\n\nVirtio9p.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.configspace_tagname;\n    state[1] = this.configspace_taglen;\n    state[2] = this.virtio;\n    state[3] = this.VERSION;\n    state[4] = this.BLOCKSIZE;\n    state[5] = this.msize;\n    state[6] = this.replybuffer;\n    state[7] = this.replybuffersize;\n    state[8] = this.fids.map(function(f) { return [f.inodeid, f.type, f.uid, f.dbg_name]; });\n    state[9] = this.fs;\n\n    return state;\n};\n\nVirtio9p.prototype.set_state = function(state)\n{\n    this.configspace_tagname = state[0];\n    this.configspace_taglen = state[1];\n    this.virtio.set_state(state[2]);\n    this.virtqueue = this.virtio.queues[0];\n    this.VERSION = state[3];\n    this.BLOCKSIZE = state[4];\n    this.msize = state[5];\n    this.replybuffer = state[6];\n    this.replybuffersize = state[7];\n    this.fids = state[8].map(function(f)\n    {\n        return { inodeid: f[0], type: f[1], uid: f[2], dbg_name: f[3] };\n    });\n    this.fs.set_state(state[9]);\n};\n\n// Note: dbg_name is only used for debugging messages and may not be the same as the filename,\n// since it is not synchronised with renames done outside of 9p. Hard-links, linking and unlinking\n// operations also mean that having a single filename no longer makes sense.\n// Set TRACK_FILENAMES = true to sync dbg_name during 9p renames.\nVirtio9p.prototype.Createfid = function(inodeid, type, uid, dbg_name) {\n    return {inodeid, type, uid, dbg_name};\n};\n\nVirtio9p.prototype.update_dbg_name = function(idx, newname)\n{\n    for(const fid of this.fids)\n    {\n        if(fid.inodeid === idx) fid.dbg_name = newname;\n    }\n};\n\nVirtio9p.prototype.reset = function()\n{\n    this.fids = [];\n    this.virtio.reset();\n};\n\nVirtio9p.prototype.BuildReply = function(id, tag, payloadsize) {\n    dbg_assert(payloadsize >= 0, \"9P: Negative payload size\");\n    marshall.Marshall([\"w\", \"b\", \"h\"], [payloadsize+7, id+1, tag], this.replybuffer, 0);\n    if((payloadsize+7) >= this.replybuffer.length) {\n        dbg_log(\"Error in 9p: payloadsize exceeds maximum length\", LOG_9P);\n    }\n    //for(var i=0; i<payload.length; i++)\n    //    this.replybuffer[7+i] = payload[i];\n    this.replybuffersize = payloadsize+7;\n};\n\nVirtio9p.prototype.SendError = function (tag, errormsg, errorcode) {\n    //var size = marshall.Marshall([\"s\", \"w\"], [errormsg, errorcode], this.replybuffer, 7);\n    var size = marshall.Marshall([\"w\"], [errorcode], this.replybuffer, 7);\n    this.BuildReply(6, tag, size);\n};\n\nVirtio9p.prototype.SendReply = function (bufchain) {\n    dbg_assert(this.replybuffersize >= 0, \"9P: Negative replybuffersize\");\n    bufchain.set_next_blob(this.replybuffer.subarray(0, this.replybuffersize));\n    this.virtqueue.push_reply(bufchain);\n    this.virtqueue.flush_replies();\n};\n\nVirtio9p.prototype.ReceiveRequest = async function (bufchain) {\n    // TODO: split into header + data blobs to avoid unnecessary copying.\n    const buffer = new Uint8Array(bufchain.length_readable);\n    bufchain.get_next_blob(buffer);\n\n    const state = { offset : 0 };\n    var header = marshall.Unmarshall([\"w\", \"b\", \"h\"], buffer, state);\n    var size = header[0];\n    var id = header[1];\n    var tag = header[2];\n    //dbg_log(\"size:\" + size + \" id:\" + id + \" tag:\" + tag, LOG_9P);\n\n    switch(id)\n    {\n        case 8: // statfs\n            size = this.fs.GetTotalSize(); // size used by all files\n            var space = this.fs.GetSpace();\n            var req = [];\n            req[0] = 0x01021997;\n            req[1] = this.BLOCKSIZE; // optimal transfer block size\n            req[2] = Math.floor(space/req[1]); // free blocks\n            req[3] = req[2] - Math.floor(size/req[1]); // free blocks in fs\n            req[4] = req[2] - Math.floor(size/req[1]); // free blocks avail to non-superuser\n            req[5] = this.fs.CountUsedInodes(); // total number of inodes\n            req[6] = this.fs.CountFreeInodes();\n            req[7] = 0; // file system id?\n            req[8] = 256; // maximum length of filenames\n\n            size = marshall.Marshall([\"w\", \"w\", \"d\", \"d\", \"d\", \"d\", \"d\", \"d\", \"w\"], req, this.replybuffer, 7);\n            this.BuildReply(id, tag, size);\n            this.SendReply(bufchain);\n            break;\n\n        case 112: // topen\n        case 12: // tlopen\n            var req = marshall.Unmarshall([\"w\", \"w\"], buffer, state);\n            var fid = req[0];\n            var mode = req[1];\n            dbg_log(\"[open] fid=\" + fid + \", mode=\" + mode, LOG_9P);\n            var idx = this.fids[fid].inodeid;\n            var inode = this.fs.GetInode(idx);\n            dbg_log(\"file open \" + this.fids[fid].dbg_name + \" tag:\"+tag, LOG_9P);\n            await this.fs.OpenInode(idx, mode);\n\n            req = [];\n            req[0] = inode.qid;\n            req[1] = this.msize - 24;\n            marshall.Marshall([\"Q\", \"w\"], req, this.replybuffer, 7);\n            this.BuildReply(id, tag, 13+4);\n            this.SendReply(bufchain);\n            break;\n\n        case 70: // link\n            var req = marshall.Unmarshall([\"w\", \"w\", \"s\"], buffer, state);\n            var dfid = req[0];\n            var fid = req[1];\n            var name = req[2];\n            dbg_log(\"[link] dfid=\" + dfid + \", name=\" + name, LOG_9P);\n\n            var ret = this.fs.Link(this.fids[dfid].inodeid, this.fids[fid].inodeid, name);\n\n            if(ret < 0)\n            {\n                let error_message = \"\";\n                if(ret === -EPERM) error_message = \"Operation not permitted\";\n                else\n                {\n                    error_message = \"Unknown error: \" + (-ret);\n                    dbg_assert(false, \"[link]: Unexpected error code: \" + (-ret));\n                }\n                this.SendError(tag, error_message, -ret);\n                this.SendReply(bufchain);\n                break;\n            }\n\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n        case 16: // symlink\n            var req = marshall.Unmarshall([\"w\", \"s\", \"s\", \"w\"], buffer, state);\n            var fid = req[0];\n            var name = req[1];\n            var symgt = req[2];\n            var gid = req[3];\n            dbg_log(\"[symlink] fid=\" + fid + \", name=\" + name + \", symgt=\" + symgt + \", gid=\" + gid, LOG_9P);\n            var idx = this.fs.CreateSymlink(name, this.fids[fid].inodeid, symgt);\n            var inode = this.fs.GetInode(idx);\n            inode.uid = this.fids[fid].uid;\n            inode.gid = gid;\n            marshall.Marshall([\"Q\"], [inode.qid], this.replybuffer, 7);\n            this.BuildReply(id, tag, 13);\n            this.SendReply(bufchain);\n            break;\n\n        case 18: // mknod\n            var req = marshall.Unmarshall([\"w\", \"s\", \"w\", \"w\", \"w\", \"w\"], buffer, state);\n            var fid = req[0];\n            var name = req[1];\n            var mode = req[2];\n            var major = req[3];\n            var minor = req[4];\n            var gid = req[5];\n            dbg_log(\"[mknod] fid=\" + fid + \", name=\" + name + \", major=\" + major + \", minor=\" + minor+ \"\", LOG_9P);\n            var idx = this.fs.CreateNode(name, this.fids[fid].inodeid, major, minor);\n            var inode = this.fs.GetInode(idx);\n            inode.mode = mode;\n            //inode.mode = mode | S_IFCHR; // XXX: fails \"Mknod - fifo\" test\n            inode.uid = this.fids[fid].uid;\n            inode.gid = gid;\n            marshall.Marshall([\"Q\"], [inode.qid], this.replybuffer, 7);\n            this.BuildReply(id, tag, 13);\n            this.SendReply(bufchain);\n            break;\n\n\n        case 22: // TREADLINK\n            var req = marshall.Unmarshall([\"w\"], buffer, state);\n            var fid = req[0];\n            var inode = this.fs.GetInode(this.fids[fid].inodeid);\n            dbg_log(\"[readlink] fid=\" + fid + \" name=\" + this.fids[fid].dbg_name + \" target=\" + inode.symlink, LOG_9P);\n            size = marshall.Marshall([\"s\"], [inode.symlink], this.replybuffer, 7);\n            this.BuildReply(id, tag, size);\n            this.SendReply(bufchain);\n            break;\n\n\n        case 72: // tmkdir\n            var req = marshall.Unmarshall([\"w\", \"s\", \"w\", \"w\"], buffer, state);\n            var fid = req[0];\n            var name = req[1];\n            var mode = req[2];\n            var gid = req[3];\n            dbg_log(\"[mkdir] fid=\" + fid + \", name=\" + name + \", mode=\" + mode + \", gid=\" + gid, LOG_9P);\n            var idx = this.fs.CreateDirectory(name, this.fids[fid].inodeid);\n            var inode = this.fs.GetInode(idx);\n            inode.mode = mode | S_IFDIR;\n            inode.uid = this.fids[fid].uid;\n            inode.gid = gid;\n            marshall.Marshall([\"Q\"], [inode.qid], this.replybuffer, 7);\n            this.BuildReply(id, tag, 13);\n            this.SendReply(bufchain);\n            break;\n\n        case 14: // tlcreate\n            var req = marshall.Unmarshall([\"w\", \"s\", \"w\", \"w\", \"w\"], buffer, state);\n            var fid = req[0];\n            var name = req[1];\n            var flags = req[2];\n            var mode = req[3];\n            var gid = req[4];\n            this.bus.send(\"9p-create\", [name, this.fids[fid].inodeid]);\n            dbg_log(\"[create] fid=\" + fid + \", name=\" + name + \", flags=\" + flags + \", mode=\" + mode + \", gid=\" + gid, LOG_9P);\n            var idx = this.fs.CreateFile(name, this.fids[fid].inodeid);\n            this.fids[fid].inodeid = idx;\n            this.fids[fid].type = FID_INODE;\n            this.fids[fid].dbg_name = name;\n            var inode = this.fs.GetInode(idx);\n            inode.uid = this.fids[fid].uid;\n            inode.gid = gid;\n            inode.mode = mode | S_IFREG;\n            marshall.Marshall([\"Q\", \"w\"], [inode.qid, this.msize - 24], this.replybuffer, 7);\n            this.BuildReply(id, tag, 13+4);\n            this.SendReply(bufchain);\n            break;\n\n        case 52: // lock\n            var req = marshall.Unmarshall([\"w\", \"b\", \"w\", \"d\", \"d\", \"w\", \"s\"], buffer, state);\n            var fid = req[0];\n            var flags = req[2];\n            var lock_length = req[4] === 0 ? Infinity : req[4];\n            var lock_request = this.fs.DescribeLock(req[1], req[3], lock_length, req[5], req[6]);\n            dbg_log(\"[lock] fid=\" + fid +\n                \", type=\" + P9_LOCK_TYPES[lock_request.type] + \", start=\" + lock_request.start +\n                \", length=\" + lock_request.length + \", proc_id=\" + lock_request.proc_id);\n\n            var ret = this.fs.Lock(this.fids[fid].inodeid, lock_request, flags);\n\n            marshall.Marshall([\"b\"], [ret], this.replybuffer, 7);\n            this.BuildReply(id, tag, 1);\n            this.SendReply(bufchain);\n            break;\n\n        case 54: // getlock\n            var req = marshall.Unmarshall([\"w\", \"b\", \"d\", \"d\", \"w\", \"s\"], buffer, state);\n            var fid = req[0];\n            var lock_length = req[3] === 0 ? Infinity : req[3];\n            var lock_request = this.fs.DescribeLock(req[1], req[2], lock_length, req[4], req[5]);\n            dbg_log(\"[getlock] fid=\" + fid +\n                \", type=\" + P9_LOCK_TYPES[lock_request.type] + \", start=\" + lock_request.start +\n                \", length=\" + lock_request.length + \", proc_id=\" + lock_request.proc_id);\n\n            var ret = this.fs.GetLock(this.fids[fid].inodeid, lock_request);\n\n            if(!ret)\n            {\n                ret = lock_request;\n                ret.type = P9_LOCK_TYPE_UNLCK;\n            }\n\n            var ret_length = ret.length === Infinity ? 0 : ret.length;\n\n            size = marshall.Marshall([\"b\", \"d\", \"d\", \"w\", \"s\"],\n                [ret.type, ret.start, ret_length, ret.proc_id, ret.client_id],\n                this.replybuffer, 7);\n\n            this.BuildReply(id, tag, size);\n            this.SendReply(bufchain);\n            break;\n\n        case 24: // getattr\n            var req = marshall.Unmarshall([\"w\", \"d\"], buffer, state);\n            var fid = req[0];\n            var inode = this.fs.GetInode(this.fids[fid].inodeid);\n            dbg_log(\"[getattr]: fid=\" + fid + \" name=\" + this.fids[fid].dbg_name + \" request mask=\" + req[1], LOG_9P);\n            if(!inode || inode.status === STATUS_UNLINKED)\n            {\n                dbg_log(\"getattr: unlinked\", LOG_9P);\n                this.SendError(tag, \"No such file or directory\", ENOENT);\n                this.SendReply(bufchain);\n                break;\n            }\n            req[0] = req[1]; // request mask\n            req[1] = inode.qid;\n\n            req[2] = inode.mode;\n            req[3] = inode.uid; // user id\n            req[4] = inode.gid; // group id\n\n            req[5] = inode.nlinks; // number of hard links\n            req[6] = (inode.major<<8) | (inode.minor); // device id low\n            req[7] = inode.size; // size low\n            req[8] = this.BLOCKSIZE;\n            req[9] = Math.floor(inode.size/512+1); // blk size low\n            req[10] = inode.atime; // atime\n            req[11] = 0x0;\n            req[12] = inode.mtime; // mtime\n            req[13] = 0x0;\n            req[14] = inode.ctime; // ctime\n            req[15] = 0x0;\n            req[16] = 0x0; // btime\n            req[17] = 0x0;\n            req[18] = 0x0; // st_gen\n            req[19] = 0x0; // data_version\n            marshall.Marshall([\n            \"d\", \"Q\",\n            \"w\",\n            \"w\", \"w\",\n            \"d\", \"d\",\n            \"d\", \"d\", \"d\",\n            \"d\", \"d\", // atime\n            \"d\", \"d\", // mtime\n            \"d\", \"d\", // ctime\n            \"d\", \"d\", // btime\n            \"d\", \"d\",\n            ], req, this.replybuffer, 7);\n            this.BuildReply(id, tag, 8 + 13 + 4 + 4+ 4 + 8*15);\n            this.SendReply(bufchain);\n            break;\n\n        case 26: // setattr\n            var req = marshall.Unmarshall([\"w\", \"w\",\n                \"w\", // mode\n                \"w\", \"w\", // uid, gid\n                \"d\", // size\n                \"d\", \"d\", // atime\n                \"d\", \"d\", // mtime\n            ], buffer, state);\n            var fid = req[0];\n            var inode = this.fs.GetInode(this.fids[fid].inodeid);\n            dbg_log(\"[setattr]: fid=\" + fid + \" request mask=\" + req[1] + \" name=\" + this.fids[fid].dbg_name, LOG_9P);\n            if(req[1] & P9_SETATTR_MODE) {\n                // XXX: check mode (S_IFREG or S_IFDIR or similar should be set)\n                inode.mode = req[2];\n            }\n            if(req[1] & P9_SETATTR_UID) {\n                inode.uid = req[3];\n            }\n            if(req[1] & P9_SETATTR_GID) {\n                inode.gid = req[4];\n            }\n            if(req[1] & P9_SETATTR_ATIME) {\n                inode.atime = Math.floor((new Date()).getTime()/1000);\n            }\n            if(req[1] & P9_SETATTR_MTIME) {\n                inode.mtime = Math.floor((new Date()).getTime()/1000);\n            }\n            if(req[1] & P9_SETATTR_CTIME) {\n                inode.ctime = Math.floor((new Date()).getTime()/1000);\n            }\n            if(req[1] & P9_SETATTR_ATIME_SET) {\n                inode.atime = req[6];\n            }\n            if(req[1] & P9_SETATTR_MTIME_SET) {\n                inode.mtime = req[8];\n            }\n            if(req[1] & P9_SETATTR_SIZE) {\n                await this.fs.ChangeSize(this.fids[fid].inodeid, req[5]);\n            }\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n        case 50: // fsync\n            var req = marshall.Unmarshall([\"w\", \"d\"], buffer, state);\n            var fid = req[0];\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n        case 40: // TREADDIR\n        case 116: // read\n            var req = marshall.Unmarshall([\"w\", \"d\", \"w\"], buffer, state);\n            var fid = req[0];\n            var offset = req[1];\n            var count = req[2];\n            var inode = this.fs.GetInode(this.fids[fid].inodeid);\n            if(id === 40) dbg_log(\"[treaddir]: fid=\" + fid + \" offset=\" + offset + \" count=\" + count, LOG_9P);\n            if(id === 116) dbg_log(\"[read]: fid=\" + fid + \" (\" + this.fids[fid].dbg_name + \") offset=\" + offset + \" count=\" + count + \" fidtype=\" + this.fids[fid].type, LOG_9P);\n            if(!inode || inode.status === STATUS_UNLINKED)\n            {\n                dbg_log(\"read/treaddir: unlinked\", LOG_9P);\n                this.SendError(tag, \"No such file or directory\", ENOENT);\n                this.SendReply(bufchain);\n                break;\n            }\n            if(this.fids[fid].type === FID_XATTR) {\n                if(inode.caps.length < offset+count) count = inode.caps.length - offset;\n                for(var i=0; i<count; i++)\n                    this.replybuffer[7+4+i] = inode.caps[offset+i];\n                marshall.Marshall([\"w\"], [count], this.replybuffer, 7);\n                this.BuildReply(id, tag, 4 + count);\n                this.SendReply(bufchain);\n            } else {\n                await this.fs.OpenInode(this.fids[fid].inodeid, undefined);\n                const inodeid = this.fids[fid].inodeid;\n\n                count = Math.min(count, this.replybuffer.length - (7 + 4));\n\n                if(inode.size < offset+count) count = inode.size - offset;\n                else if(id === 40)\n                {\n                    // for directories, return whole number of dir-entries.\n                    count = this.fs.RoundToDirentry(inodeid, offset + count) - offset;\n                }\n                if(offset > inode.size)\n                {\n                    // offset can be greater than available - should return count of zero.\n                    // See http://ericvh.github.io/9p-rfc/rfc9p2000.html#anchor30\n                    count = 0;\n                }\n\n                this.bus.send(\"9p-read-start\", [this.fids[fid].dbg_name]);\n\n                const data = await this.fs.Read(inodeid, offset, count);\n\n                this.bus.send(\"9p-read-end\", [this.fids[fid].dbg_name, count]);\n\n                if(data) {\n                    this.replybuffer.set(data, 7 + 4);\n                }\n                marshall.Marshall([\"w\"], [count], this.replybuffer, 7);\n                this.BuildReply(id, tag, 4 + count);\n                this.SendReply(bufchain);\n            }\n            break;\n\n        case 118: // write\n            var req = marshall.Unmarshall([\"w\", \"d\", \"w\"], buffer, state);\n            var fid = req[0];\n            var offset = req[1];\n            var count = req[2];\n\n            const filename = this.fids[fid].dbg_name;\n\n            dbg_log(\"[write]: fid=\" + fid + \" (\" + filename + \") offset=\" + offset + \" count=\" + count + \" fidtype=\" + this.fids[fid].type, LOG_9P);\n            if(this.fids[fid].type === FID_XATTR)\n            {\n                // XXX: xattr not supported yet. Ignore write.\n                this.SendError(tag, \"Setxattr not supported\", EOPNOTSUPP);\n                this.SendReply(bufchain);\n                break;\n            }\n            else\n            {\n                // XXX: Size of the subarray is unchecked\n                await this.fs.Write(this.fids[fid].inodeid, offset, count, buffer.subarray(state.offset));\n            }\n\n            this.bus.send(\"9p-write-end\", [filename, count]);\n\n            marshall.Marshall([\"w\"], [count], this.replybuffer, 7);\n            this.BuildReply(id, tag, 4);\n            this.SendReply(bufchain);\n            break;\n\n        case 74: // RENAMEAT\n            var req = marshall.Unmarshall([\"w\", \"s\", \"w\", \"s\"], buffer, state);\n            var olddirfid = req[0];\n            var oldname = req[1];\n            var newdirfid = req[2];\n            var newname = req[3];\n            dbg_log(\"[renameat]: oldname=\" + oldname + \" newname=\" + newname, LOG_9P);\n            var ret = await this.fs.Rename(this.fids[olddirfid].inodeid, oldname, this.fids[newdirfid].inodeid, newname);\n            if(ret < 0) {\n                let error_message = \"\";\n                if(ret === -ENOENT) error_message = \"No such file or directory\";\n                else if(ret === -EPERM) error_message = \"Operation not permitted\";\n                else if(ret === -ENOTEMPTY) error_message = \"Directory not empty\";\n                else\n                {\n                    error_message = \"Unknown error: \" + (-ret);\n                    dbg_assert(false, \"[renameat]: Unexpected error code: \" + (-ret));\n                }\n                this.SendError(tag, error_message, -ret);\n                this.SendReply(bufchain);\n                break;\n            }\n            if(TRACK_FILENAMES)\n            {\n                const newidx = this.fs.Search(this.fids[newdirfid].inodeid, newname);\n                this.update_dbg_name(newidx, newname);\n            }\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n        case 76: // TUNLINKAT\n            var req = marshall.Unmarshall([\"w\", \"s\", \"w\"], buffer, state);\n            var dirfd = req[0];\n            var name = req[1];\n            var flags = req[2];\n            dbg_log(\"[unlink]: dirfd=\" + dirfd + \" name=\" + name + \" flags=\" + flags, LOG_9P);\n            var fid = this.fs.Search(this.fids[dirfd].inodeid, name);\n            if(fid === -1) {\n                   this.SendError(tag, \"No such file or directory\", ENOENT);\n                   this.SendReply(bufchain);\n                   break;\n            }\n            var ret = this.fs.Unlink(this.fids[dirfd].inodeid, name);\n            if(ret < 0) {\n                let error_message = \"\";\n                if(ret === -ENOTEMPTY) error_message = \"Directory not empty\";\n                else if(ret === -EPERM) error_message = \"Operation not permitted\";\n                else\n                {\n                    error_message = \"Unknown error: \" + (-ret);\n                    dbg_assert(false, \"[unlink]: Unexpected error code: \" + (-ret));\n                }\n                this.SendError(tag, error_message, -ret);\n                this.SendReply(bufchain);\n                break;\n            }\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n        case 100: // version\n            var version = marshall.Unmarshall([\"w\", \"s\"], buffer, state);\n            dbg_log(\"[version]: msize=\" + version[0] + \" version=\" + version[1], LOG_9P);\n            if(this.msize !== version[0])\n            {\n                this.msize = version[0];\n                this.replybuffer = new Uint8Array(Math.min(MAX_REPLYBUFFER_SIZE, this.msize*2));\n            }\n            size = marshall.Marshall([\"w\", \"s\"], [this.msize, this.VERSION], this.replybuffer, 7);\n            this.BuildReply(id, tag, size);\n            this.SendReply(bufchain);\n            break;\n\n        case 104: // attach\n            // return root directorie's QID\n            var req = marshall.Unmarshall([\"w\", \"w\", \"s\", \"s\", \"w\"], buffer, state);\n            var fid = req[0];\n            var uid = req[4];\n            dbg_log(\"[attach]: fid=\" + fid + \" afid=\" + h(req[1]) + \" uname=\" + req[2] + \" aname=\" + req[3], LOG_9P);\n            this.fids[fid] = this.Createfid(0, FID_INODE, uid, \"\");\n            var inode = this.fs.GetInode(this.fids[fid].inodeid);\n            marshall.Marshall([\"Q\"], [inode.qid], this.replybuffer, 7);\n            this.BuildReply(id, tag, 13);\n            this.SendReply(bufchain);\n            this.bus.send(\"9p-attach\");\n            break;\n\n        case 108: // tflush\n            var req = marshall.Unmarshall([\"h\"], buffer, state);\n            var oldtag = req[0];\n            dbg_log(\"[flush] \" + tag, LOG_9P);\n            //marshall.Marshall([\"Q\"], [inode.qid], this.replybuffer, 7);\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n\n        case 110: // walk\n            var req = marshall.Unmarshall([\"w\", \"w\", \"h\"], buffer, state);\n            var fid = req[0];\n            var nwfid = req[1];\n            var nwname = req[2];\n            dbg_log(\"[walk]: fid=\" + req[0] + \" nwfid=\" + req[1] + \" nwname=\" + nwname, LOG_9P);\n            if(nwname === 0) {\n                this.fids[nwfid] = this.Createfid(this.fids[fid].inodeid, FID_INODE, this.fids[fid].uid, this.fids[fid].dbg_name);\n                //this.fids[nwfid].inodeid = this.fids[fid].inodeid;\n                marshall.Marshall([\"h\"], [0], this.replybuffer, 7);\n                this.BuildReply(id, tag, 2);\n                this.SendReply(bufchain);\n                break;\n            }\n            var wnames = [];\n            for(var i=0; i<nwname; i++) {\n                wnames.push(\"s\");\n            }\n            var walk = marshall.Unmarshall(wnames, buffer, state);\n            var idx = this.fids[fid].inodeid;\n            var offset = 7+2;\n            var nwidx = 0;\n            //console.log(idx, this.fs.GetInode(idx));\n            dbg_log(\"walk in dir \" + this.fids[fid].dbg_name  + \" to: \" + walk.toString(), LOG_9P);\n            for(var i=0; i<nwname; i++) {\n                idx = this.fs.Search(idx, walk[i]);\n\n                if(idx === -1) {\n                   dbg_log(\"Could not find: \" + walk[i], LOG_9P);\n                   break;\n                }\n                offset += marshall.Marshall([\"Q\"], [this.fs.GetInode(idx).qid], this.replybuffer, offset);\n                nwidx++;\n                //dbg_log(this.fids[nwfid].inodeid, LOG_9P);\n                //this.fids[nwfid].inodeid = idx;\n                //this.fids[nwfid].type = FID_INODE;\n                this.fids[nwfid] = this.Createfid(idx, FID_INODE, this.fids[fid].uid, walk[i]);\n            }\n            marshall.Marshall([\"h\"], [nwidx], this.replybuffer, 7);\n            this.BuildReply(id, tag, offset-7);\n            this.SendReply(bufchain);\n            break;\n\n        case 120: // clunk\n            var req = marshall.Unmarshall([\"w\"], buffer, state);\n            dbg_log(\"[clunk]: fid=\" + req[0], LOG_9P);\n            if(this.fids[req[0]] && this.fids[req[0]].inodeid >=  0) {\n                await this.fs.CloseInode(this.fids[req[0]].inodeid);\n                this.fids[req[0]].inodeid = -1;\n                this.fids[req[0]].type = FID_NONE;\n            }\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            break;\n\n        case 32: // txattrcreate\n            var req = marshall.Unmarshall([\"w\", \"s\", \"d\", \"w\"], buffer, state);\n            var fid = req[0];\n            var name = req[1];\n            var attr_size = req[2];\n            var flags = req[3];\n            dbg_log(\"[txattrcreate]: fid=\" + fid + \" name=\" + name + \" attr_size=\" + attr_size + \" flags=\" + flags, LOG_9P);\n\n            // XXX: xattr not supported yet. E.g. checks corresponding to the flags needed.\n            this.fids[fid].type = FID_XATTR;\n\n            this.BuildReply(id, tag, 0);\n            this.SendReply(bufchain);\n            //this.SendError(tag, \"Operation i not supported\",  EINVAL);\n            //this.SendReply(bufchain);\n            break;\n\n        case 30: // xattrwalk\n            var req = marshall.Unmarshall([\"w\", \"w\", \"s\"], buffer, state);\n            var fid = req[0];\n            var newfid = req[1];\n            var name = req[2];\n            dbg_log(\"[xattrwalk]: fid=\" + req[0] + \" newfid=\" + req[1] + \" name=\" + req[2], LOG_9P);\n\n            // Workaround for Linux restarts writes until full blocksize\n            this.SendError(tag, \"Setxattr not supported\", EOPNOTSUPP);\n            this.SendReply(bufchain);\n            /*\n            this.fids[newfid] = this.Createfid(this.fids[fid].inodeid, FID_NONE, this.fids[fid].uid, this.fids[fid].dbg_name);\n            //this.fids[newfid].inodeid = this.fids[fid].inodeid;\n            //this.fids[newfid].type = FID_NONE;\n            var length = 0;\n            if (name === \"security.capability\") {\n                length = this.fs.PrepareCAPs(this.fids[fid].inodeid);\n                this.fids[newfid].type = FID_XATTR;\n            }\n            marshall.Marshall([\"d\"], [length], this.replybuffer, 7);\n            this.BuildReply(id, tag, 8);\n            this.SendReply(bufchain);\n            */\n            break;\n\n        default:\n            dbg_log(\"Error in Virtio9p: Unknown id \" + id + \" received\", LOG_9P);\n            dbg_assert(false);\n            //this.SendError(tag, \"Operation i not supported\",  EOPNOTSUPP);\n            //this.SendReply(bufchain);\n            break;\n    }\n\n    //consistency checks if there are problems with the filesystem\n    //this.fs.Check();\n};\n\n/** @typedef {function(Uint8Array, function(Uint8Array):void):void} */\nlet P9Handler;\n\n/**\n * @constructor\n *\n * @param {P9Handler} handle_fn\n * @param {CPU} cpu\n */\nexport function Virtio9pHandler(handle_fn, cpu) {\n    /** @type {P9Handler} */\n    this.handle_fn = handle_fn;\n    this.tag_bufchain = new Map();\n\n    this.configspace_tagname = [0x68, 0x6F, 0x73, 0x74, 0x39, 0x70]; // \"host9p\" string\n    this.configspace_taglen = this.configspace_tagname.length; // num bytes\n\n    this.virtio = init_virtio(\n        cpu,\n        this.configspace_taglen,\n        this.configspace_tagname,\n        async (bufchain) => {\n            // TODO: split into header + data blobs to avoid unnecessary copying.\n            const reqbuf = new Uint8Array(bufchain.length_readable);\n            bufchain.get_next_blob(reqbuf);\n\n            var reqheader = marshall.Unmarshall([\"w\", \"b\", \"h\"], reqbuf, { offset : 0 });\n            var reqtag = reqheader[2];\n\n            this.tag_bufchain.set(reqtag, bufchain);\n            this.handle_fn(reqbuf, (replybuf) => {\n                var replyheader = marshall.Unmarshall([\"w\", \"b\", \"h\"], replybuf, { offset: 0 });\n                var replytag = replyheader[2];\n\n                const bufchain = this.tag_bufchain.get(replytag);\n                if(!bufchain)\n                {\n                    console.error(\"No bufchain found for tag: \" + replytag);\n                    return;\n                }\n\n                bufchain.set_next_blob(replybuf);\n                this.virtqueue.push_reply(bufchain);\n                this.virtqueue.flush_replies();\n\n                this.tag_bufchain.delete(replytag);\n            });\n        }\n    );\n    this.virtqueue = this.virtio.queues[0];\n}\n\nVirtio9pHandler.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.configspace_tagname;\n    state[1] = this.configspace_taglen;\n    state[2] = this.virtio;\n    state[3] = this.tag_bufchain;\n\n    return state;\n};\n\nVirtio9pHandler.prototype.set_state = function(state)\n{\n    this.configspace_tagname = state[0];\n    this.configspace_taglen = state[1];\n    this.virtio.set_state(state[2]);\n    this.virtqueue = this.virtio.queues[0];\n    this.tag_bufchain = state[3];\n};\n\n\nVirtio9pHandler.prototype.reset = function()\n{\n    this.virtio.reset();\n};\n\n\n/**\n * @constructor\n *\n * @param {string} url\n * @param {CPU} cpu\n */\nexport function Virtio9pProxy(url, cpu)\n{\n    this.socket = undefined;\n    this.cpu = cpu;\n\n    // TODO: circular buffer?\n    this.send_queue = [];\n    this.url = url;\n\n    this.reconnect_interval = 10000;\n    this.last_connect_attempt = Date.now() - this.reconnect_interval;\n    this.send_queue_limit = 64;\n    this.destroyed = false;\n\n    this.tag_bufchain = new Map();\n\n    this.configspace_tagname = [0x68, 0x6F, 0x73, 0x74, 0x39, 0x70]; // \"host9p\" string\n    this.configspace_taglen = this.configspace_tagname.length; // num bytes\n\n    this.virtio = init_virtio(\n        cpu,\n        this.configspace_taglen,\n        this.configspace_tagname,\n        async (bufchain) => {\n            // TODO: split into header + data blobs to avoid unnecessary copying.\n            const reqbuf = new Uint8Array(bufchain.length_readable);\n            bufchain.get_next_blob(reqbuf);\n\n            const reqheader = marshall.Unmarshall([\"w\", \"b\", \"h\"], reqbuf, { offset : 0 });\n            const reqtag = reqheader[2];\n\n            this.tag_bufchain.set(reqtag, bufchain);\n            this.send(reqbuf);\n        }\n    );\n    this.virtqueue = this.virtio.queues[0];\n}\n\nVirtio9pProxy.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.configspace_tagname;\n    state[1] = this.configspace_taglen;\n    state[2] = this.virtio;\n    state[3] = this.tag_bufchain;\n\n    return state;\n};\n\nVirtio9pProxy.prototype.set_state = function(state)\n{\n    this.configspace_tagname = state[0];\n    this.configspace_taglen = state[1];\n    this.virtio.set_state(state[2]);\n    this.virtqueue = this.virtio.queues[0];\n    this.tag_bufchain = state[3];\n};\n\nVirtio9pProxy.prototype.reset = function() {\n    this.virtio.reset();\n};\n\nVirtio9pProxy.prototype.handle_message = function(e)\n{\n    const replybuf = new Uint8Array(e.data);\n    const replyheader = marshall.Unmarshall([\"w\", \"b\", \"h\"], replybuf, { offset: 0 });\n    const replytag = replyheader[2];\n\n    const bufchain = this.tag_bufchain.get(replytag);\n    if(!bufchain)\n    {\n        console.error(\"Virtio9pProxy: No bufchain found for tag: \" + replytag);\n        return;\n    }\n\n    bufchain.set_next_blob(replybuf);\n    this.virtqueue.push_reply(bufchain);\n    this.virtqueue.flush_replies();\n\n    this.tag_bufchain.delete(replytag);\n};\n\nVirtio9pProxy.prototype.handle_close = function(e)\n{\n    //console.log(\"onclose\", e);\n\n    if(!this.destroyed)\n    {\n        this.connect();\n        setTimeout(this.connect.bind(this), this.reconnect_interval);\n    }\n};\n\nVirtio9pProxy.prototype.handle_open = function(e)\n{\n    //console.log(\"open\", e);\n\n    for(var i = 0; i < this.send_queue.length; i++)\n    {\n        this.send(this.send_queue[i]);\n    }\n\n    this.send_queue = [];\n};\n\nVirtio9pProxy.prototype.handle_error = function(e)\n{\n    //console.log(\"onerror\", e);\n};\n\nVirtio9pProxy.prototype.destroy = function()\n{\n    this.destroyed = true;\n    if(this.socket)\n    {\n        this.socket.close();\n    }\n};\n\nVirtio9pProxy.prototype.connect = function()\n{\n    if(typeof WebSocket === \"undefined\")\n    {\n        return;\n    }\n\n    if(this.socket)\n    {\n        var state = this.socket.readyState;\n\n        if(state === 0 || state === 1)\n        {\n            // already or almost there\n            return;\n        }\n    }\n\n    var now = Date.now();\n\n    if(this.last_connect_attempt + this.reconnect_interval > now)\n    {\n        return;\n    }\n\n    this.last_connect_attempt = Date.now();\n\n    try\n    {\n        this.socket = new WebSocket(this.url);\n    }\n    catch(e)\n    {\n        console.error(e);\n        return;\n    }\n\n    this.socket.binaryType = \"arraybuffer\";\n\n    this.socket.onopen = this.handle_open.bind(this);\n    this.socket.onmessage = this.handle_message.bind(this);\n    this.socket.onclose = this.handle_close.bind(this);\n    this.socket.onerror = this.handle_error.bind(this);\n};\n\nVirtio9pProxy.prototype.send = function(data)\n{\n    //console.log(\"send\", data);\n\n    if(!this.socket || this.socket.readyState !== 1)\n    {\n        this.send_queue.push(data);\n\n        if(this.send_queue.length > 2 * this.send_queue_limit)\n        {\n            this.send_queue = this.send_queue.slice(-this.send_queue_limit);\n        }\n\n        this.connect();\n    }\n    else\n    {\n        this.socket.send(data);\n    }\n};\n\nVirtio9pProxy.prototype.change_proxy = function(url)\n{\n    this.url = url;\n\n    if(this.socket)\n    {\n        this.socket.onclose = function() {};\n        this.socket.onerror = function() {};\n        this.socket.close();\n        this.socket = undefined;\n    }\n};\n"
  },
  {
    "path": "lib/filesystem.js",
    "content": "// -------------------------------------------------\n// ----------------- FILESYSTEM---------------------\n// -------------------------------------------------\n// Implementation of a unix filesystem in memory.\n\n\nimport { LOG_9P } from \"../src/const.js\";\nimport { h } from \"../src/lib.js\";\nimport { dbg_assert, dbg_log } from \"../src/log.js\";\nimport * as marshall from \"../lib/marshall.js\";\nimport { EEXIST, ENOTEMPTY, ENOENT, EPERM } from \"./9p.js\";\nimport { P9_LOCK_SUCCESS, P9_LOCK_BLOCKED, P9_LOCK_TYPE_UNLCK, P9_LOCK_TYPE_WRLCK, P9_LOCK_TYPE_RDLCK } from \"./9p.js\";\n\n// For Types Only\nimport { FileStorageInterface } from \"../src/browser/filestorage.js\";\n\nexport const S_IRWXUGO = 0x1FF;\nexport const S_IFMT = 0xF000;\nexport const S_IFSOCK = 0xC000;\nexport const S_IFLNK = 0xA000;\nexport const S_IFREG = 0x8000;\nexport const S_IFBLK = 0x6000;\nexport const S_IFDIR = 0x4000;\nexport const S_IFCHR = 0x2000;\n\n//var S_IFIFO  0010000\n//var S_ISUID  0004000\n//var S_ISGID  0002000\n//var S_ISVTX  0001000\n\nvar O_RDONLY = 0x0000; // open for reading only\nvar O_WRONLY = 0x0001; // open for writing only\nvar O_RDWR = 0x0002; // open for reading and writing\nvar O_ACCMODE = 0x0003; // mask for above modes\n\nexport const STATUS_INVALID = -0x1;\nexport const STATUS_OK = 0x0;\nexport const STATUS_ON_STORAGE = 0x2;\nexport const STATUS_UNLINKED = 0x4;\nexport const STATUS_FORWARDING = 0x5;\n\nconst texten = new TextEncoder();\n\n/** @const */ var JSONFS_VERSION = 3;\n\n\n/** @const */ var JSONFS_IDX_NAME = 0;\n/** @const */ var JSONFS_IDX_SIZE = 1;\n/** @const */ var JSONFS_IDX_MTIME = 2;\n/** @const */ var JSONFS_IDX_MODE = 3;\n/** @const */ var JSONFS_IDX_UID = 4;\n/** @const */ var JSONFS_IDX_GID = 5;\n/** @const */ var JSONFS_IDX_TARGET = 6;\n/** @const */ var JSONFS_IDX_SHA256 = 6;\n\n\n/**\n * @constructor\n * @param {!FileStorageInterface} storage\n * @param {{ last_qidnumber: number }=} qidcounter Another fs's qidcounter to synchronise with.\n */\nexport function FS(storage, qidcounter) {\n    /** @type {Array.<!Inode>} */\n    this.inodes = [];\n\n    this.storage = storage;\n\n    this.qidcounter = qidcounter || { last_qidnumber: 0 };\n\n    //this.tar = new TAR(this);\n\n    this.inodedata = {};\n\n    this.total_size = 256 * 1024 * 1024 * 1024;\n    this.used_size = 0;\n\n    /** @type {!Array<!FSMountInfo>} */\n    this.mounts = [];\n\n    //RegisterMessage(\"LoadFilesystem\", this.LoadFilesystem.bind(this) );\n    //RegisterMessage(\"MergeFile\", this.MergeFile.bind(this) );\n    //RegisterMessage(\"tar\",\n    //    function(data) {\n    //        SendToMaster(\"tar\", this.tar.Pack(data));\n    //    }.bind(this)\n    //);\n    //RegisterMessage(\"sync\",\n    //    function(data) {\n    //        SendToMaster(\"sync\", this.tar.Pack(data));\n    //    }.bind(this)\n    //);\n\n    // root entry\n    this.CreateDirectory(\"\", -1);\n}\n\nFS.prototype.get_state = function()\n{\n    let state = [];\n\n    state[0] = this.inodes;\n    state[1] = this.qidcounter.last_qidnumber;\n    state[2] = [];\n    for(const [id, data] of Object.entries(this.inodedata))\n    {\n        if((this.inodes[id].mode & S_IFDIR) === 0)\n        {\n            state[2].push([id, data]);\n        }\n    }\n    state[3] = this.total_size;\n    state[4] = this.used_size;\n    state = state.concat(this.mounts);\n\n    return state;\n};\n\nFS.prototype.set_state = function(state)\n{\n    this.inodes = state[0].map(state => { const inode = new Inode(0); inode.set_state(state); return inode; });\n    this.qidcounter.last_qidnumber = state[1];\n    this.inodedata = {};\n    for(let [key, value] of state[2])\n    {\n        if(value.buffer.byteLength !== value.byteLength)\n        {\n            // make a copy if we didn't get one\n            value = value.slice();\n        }\n\n        this.inodedata[key] = value;\n    }\n    this.total_size = state[3];\n    this.used_size = state[4];\n    this.mounts = state.slice(5);\n};\n\n\n// -----------------------------------------------------\n\nFS.prototype.load_from_json = function(fs)\n{\n    dbg_assert(fs, \"Invalid fs passed to load_from_json\");\n\n    if(fs[\"version\"] !== JSONFS_VERSION)\n    {\n        throw \"The filesystem JSON format has changed. Please recreate the filesystem JSON.\";\n    }\n\n    var fsroot = fs[\"fsroot\"];\n    this.used_size = fs[\"size\"];\n\n    for(var i = 0; i < fsroot.length; i++) {\n        this.LoadRecursive(fsroot[i], 0);\n    }\n\n    //if(DEBUG)\n    //{\n    //    this.Check();\n    //}\n};\n\nFS.prototype.LoadRecursive = function(data, parentid)\n{\n    var inode = this.CreateInode();\n\n    const name = data[JSONFS_IDX_NAME];\n    inode.size = data[JSONFS_IDX_SIZE];\n    inode.mtime = data[JSONFS_IDX_MTIME];\n    inode.ctime = inode.mtime;\n    inode.atime = inode.mtime;\n    inode.mode = data[JSONFS_IDX_MODE];\n    inode.uid = data[JSONFS_IDX_UID];\n    inode.gid = data[JSONFS_IDX_GID];\n\n    var ifmt = inode.mode & S_IFMT;\n\n    if(ifmt === S_IFDIR)\n    {\n        this.PushInode(inode, parentid, name);\n        this.LoadDir(this.inodes.length - 1, data[JSONFS_IDX_TARGET]);\n    }\n    else if(ifmt === S_IFREG)\n    {\n        inode.status = STATUS_ON_STORAGE;\n        inode.sha256sum = data[JSONFS_IDX_SHA256];\n        dbg_assert(inode.sha256sum);\n        this.PushInode(inode, parentid, name);\n    }\n    else if(ifmt === S_IFLNK)\n    {\n        inode.symlink = data[JSONFS_IDX_TARGET];\n        this.PushInode(inode, parentid, name);\n    }\n    else if(ifmt === S_IFSOCK)\n    {\n        // socket: ignore\n    }\n    else\n    {\n        dbg_log(\"Unexpected ifmt: \" + h(ifmt) + \" (\" + name + \")\", LOG_9P);\n    }\n};\n\nFS.prototype.LoadDir = function(parentid, children)\n{\n    for(var i = 0; i < children.length; i++) {\n        this.LoadRecursive(children[i], parentid);\n    }\n};\n\n\n// -----------------------------------------------------\n\n/**\n * @private\n * @param {Inode} inode\n * @return {boolean}\n */\nFS.prototype.should_be_linked = function(inode)\n{\n    // Note: Non-root forwarder inode could still have a non-forwarder parent, so don't use\n    // parent inode to check.\n    return !this.is_forwarder(inode) || inode.foreign_id === 0;\n};\n\n/**\n * @private\n * @param {number} parentid\n * @param {number} idx\n * @param {string} name\n */\nFS.prototype.link_under_dir = function(parentid, idx, name)\n{\n    const inode = this.inodes[idx];\n    const parent_inode = this.inodes[parentid];\n\n    dbg_assert(!this.is_forwarder(parent_inode),\n        \"Filesystem: Shouldn't link under fowarder parents\");\n    dbg_assert(this.IsDirectory(parentid),\n        \"Filesystem: Can't link under non-directories\");\n    dbg_assert(this.should_be_linked(inode),\n        \"Filesystem: Can't link across filesystems apart from their root\");\n    dbg_assert(inode.nlinks >= 0,\n        \"Filesystem: Found negative nlinks value of \" + inode.nlinks);\n    dbg_assert(!parent_inode.direntries.has(name),\n        \"Filesystem: Name '\" + name + \"' is already taken\");\n\n    parent_inode.direntries.set(name, idx);\n    inode.nlinks++;\n\n    if(this.IsDirectory(idx))\n    {\n        dbg_assert(!inode.direntries.has(\"..\"),\n            \"Filesystem: Cannot link a directory twice\");\n\n        if(!inode.direntries.has(\".\")) inode.nlinks++;\n        inode.direntries.set(\".\", idx);\n\n        inode.direntries.set(\"..\", parentid);\n        parent_inode.nlinks++;\n    }\n};\n\n/**\n * @private\n * @param {number} parentid\n * @param {string} name\n */\nFS.prototype.unlink_from_dir = function(parentid, name)\n{\n    const idx = this.Search(parentid, name);\n    const inode = this.inodes[idx];\n    const parent_inode = this.inodes[parentid];\n\n    dbg_assert(!this.is_forwarder(parent_inode), \"Filesystem: Can't unlink from forwarders\");\n    dbg_assert(this.IsDirectory(parentid), \"Filesystem: Can't unlink from non-directories\");\n\n    const exists = parent_inode.direntries.delete(name);\n    if(!exists)\n    {\n        dbg_assert(false, \"Filesystem: Can't unlink non-existent file: \" + name);\n        return;\n    }\n\n    inode.nlinks--;\n\n    if(this.IsDirectory(idx))\n    {\n        dbg_assert(inode.direntries.get(\"..\") === parentid,\n            \"Filesystem: Found directory with bad parent id\");\n\n        inode.direntries.delete(\"..\");\n        parent_inode.nlinks--;\n    }\n\n    dbg_assert(inode.nlinks >= 0,\n        \"Filesystem: Found negative nlinks value of \" + inode.nlinks);\n};\n\nFS.prototype.PushInode = function(inode, parentid, name) {\n    if(parentid !== -1) {\n        this.inodes.push(inode);\n        inode.fid = this.inodes.length - 1;\n        this.link_under_dir(parentid, inode.fid, name);\n        return;\n    } else {\n        if(this.inodes.length === 0) { // if root directory\n            this.inodes.push(inode);\n            inode.direntries.set(\".\", 0);\n            inode.direntries.set(\"..\", 0);\n            inode.nlinks = 2;\n            return;\n        }\n    }\n\n    dbg_assert(false, \"Error in Filesystem: Pushed inode with name = \"+ name + \" has no parent\");\n};\n\n/** @constructor */\nfunction Inode(qidnumber)\n{\n    this.direntries = new Map(); // maps filename to inode id\n    this.status = 0;\n    this.size = 0x0;\n    this.uid = 0x0;\n    this.gid = 0x0;\n    this.fid = 0;\n    this.ctime = 0;\n    this.atime = 0;\n    this.mtime = 0;\n    this.major = 0x0;\n    this.minor = 0x0;\n    this.symlink = \"\";\n    this.mode = 0x01ED;\n    this.qid = {\n        type: 0,\n        version: 0,\n        path: qidnumber,\n    };\n    this.caps = undefined;\n    this.nlinks = 0;\n    this.sha256sum = \"\";\n\n    /** @type{!Array<!FSLockRegion>} */\n    this.locks = []; // lock regions applied to the file, sorted by starting offset.\n\n    // For forwarders:\n    this.mount_id = -1; // which fs in this.mounts does this inode forward to?\n    this.foreign_id = -1; // which foreign inode id does it represent?\n\n    //this.qid_type = 0;\n    //this.qid_version = 0;\n    //this.qid_path = qidnumber;\n}\n\nInode.prototype.get_state = function()\n{\n    const state = [];\n    state[0] = this.mode;\n\n    if((this.mode & S_IFMT) === S_IFDIR)\n    {\n        state[1] = [...this.direntries];\n    }\n    else if((this.mode & S_IFMT) === S_IFREG)\n    {\n        state[1] = this.sha256sum;\n    }\n    else if((this.mode & S_IFMT) === S_IFLNK)\n    {\n        state[1] = this.symlink;\n    }\n    else if((this.mode & S_IFMT) === S_IFSOCK)\n    {\n        state[1] = [this.minor, this.major];\n    }\n    else\n    {\n        state[1] = null;\n    }\n\n    state[2] = this.locks;\n    state[3] = this.status;\n    state[4] = this.size;\n    state[5] = this.uid;\n    state[6] = this.gid;\n    state[7] = this.fid;\n    state[8] = this.ctime;\n    state[9] = this.atime;\n    state[10] = this.mtime;\n    state[11] = this.qid.version;\n    state[12] = this.qid.path;\n    state[13] = this.nlinks;\n\n    //state[23] = this.mount_id;\n    //state[24] = this.foreign_id;\n    //state[25] = this.caps; // currently not writable\n    return state;\n};\n\nInode.prototype.set_state = function(state)\n{\n    this.mode = state[0];\n\n    if((this.mode & S_IFMT) === S_IFDIR)\n    {\n        this.direntries = new Map();\n        for(const [name, entry] of state[1])\n        {\n            this.direntries.set(name, entry);\n        }\n    }\n    else if((this.mode & S_IFMT) === S_IFREG)\n    {\n        this.sha256sum = state[1];\n    }\n    else if((this.mode & S_IFMT) === S_IFLNK)\n    {\n        this.symlink = state[1];\n    }\n    else if((this.mode & S_IFMT) === S_IFSOCK)\n    {\n        [this.minor, this.major] = state[1];\n    }\n    else\n    {\n        // Nothing\n    }\n\n    this.locks = [];\n    for(const lock_state of state[2])\n    {\n        const lock = new FSLockRegion();\n        lock.set_state(lock_state);\n        this.locks.push(lock);\n    }\n    this.status = state[3];\n    this.size = state[4];\n    this.uid = state[5];\n    this.gid = state[6];\n    this.fid = state[7];\n    this.ctime = state[8];\n    this.atime = state[9];\n    this.mtime = state[10];\n    this.qid.type = (this.mode & S_IFMT) >> 8;\n    this.qid.version = state[11];\n    this.qid.path = state[12];\n    this.nlinks = state[13];\n\n    //this.mount_id = state[23];\n    //this.foreign_id = state[24];\n    //this.caps = state[20];\n};\n\n/**\n * Clones given inode to new idx, effectively diverting the inode to new idx value.\n * Hence, original idx value is now free to use without losing the original information.\n * @private\n * @param {number} parentid Parent of target to divert.\n * @param {string} filename Name of target to divert.\n * @return {number} New idx of diversion.\n */\nFS.prototype.divert = function(parentid, filename)\n{\n    const old_idx = this.Search(parentid, filename);\n    const old_inode = this.inodes[old_idx];\n    const new_inode = new Inode(-1);\n\n    dbg_assert(old_inode, \"Filesystem divert: name (\" + filename + \") not found\");\n    dbg_assert(this.IsDirectory(old_idx) || old_inode.nlinks <= 1,\n        \"Filesystem: can't divert hardlinked file '\" + filename + \"' with nlinks=\" +\n        old_inode.nlinks);\n\n    // Shallow copy is alright.\n    Object.assign(new_inode, old_inode);\n\n    const idx = this.inodes.length;\n    this.inodes.push(new_inode);\n    new_inode.fid = idx;\n\n    // Relink references\n    if(this.is_forwarder(old_inode))\n    {\n        this.mounts[old_inode.mount_id].backtrack.set(old_inode.foreign_id, idx);\n    }\n    if(this.should_be_linked(old_inode))\n    {\n        this.unlink_from_dir(parentid, filename);\n        this.link_under_dir(parentid, idx, filename);\n    }\n\n    // Update children\n    if(this.IsDirectory(old_idx) && !this.is_forwarder(old_inode))\n    {\n        for(const [name, child_id] of new_inode.direntries)\n        {\n            if(name === \".\" || name === \"..\") continue;\n            if(this.IsDirectory(child_id))\n            {\n                this.inodes[child_id].direntries.set(\"..\", idx);\n            }\n        }\n    }\n\n    // Relocate local data if any.\n    this.inodedata[idx] = this.inodedata[old_idx];\n    delete this.inodedata[old_idx];\n\n    // Retire old reference information.\n    old_inode.direntries = new Map();\n    old_inode.nlinks = 0;\n\n    return idx;\n};\n\n/**\n * Copy all non-redundant info.\n * References left untouched: local idx value and links\n * @private\n * @param {!Inode} src_inode\n * @param {!Inode} dest_inode\n */\nFS.prototype.copy_inode = function(src_inode, dest_inode)\n{\n    Object.assign(dest_inode, src_inode, {\n        fid: dest_inode.fid,\n        direntries: dest_inode.direntries,\n        nlinks: dest_inode.nlinks,\n    });\n};\n\nFS.prototype.CreateInode = function() {\n    //console.log(\"CreateInode\", Error().stack);\n    const now = Math.round(Date.now() / 1000);\n    const inode = new Inode(++this.qidcounter.last_qidnumber);\n    inode.atime = inode.ctime = inode.mtime = now;\n    return inode;\n};\n\n\n// Note: parentid = -1 for initial root directory.\nFS.prototype.CreateDirectory = function(name, parentid) {\n    const parent_inode = this.inodes[parentid];\n    if(parentid >= 0 && this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id = this.follow_fs(parent_inode).CreateDirectory(name, foreign_parentid);\n        return this.create_forwarder(parent_inode.mount_id, foreign_id);\n    }\n    var x = this.CreateInode();\n    x.mode = 0x01FF | S_IFDIR;\n    if(parentid >= 0) {\n        x.uid = this.inodes[parentid].uid;\n        x.gid = this.inodes[parentid].gid;\n        x.mode = (this.inodes[parentid].mode & 0x1FF) | S_IFDIR;\n    }\n    x.qid.type = S_IFDIR >> 8;\n    this.PushInode(x, parentid, name);\n    this.NotifyListeners(this.inodes.length-1, \"newdir\");\n    return this.inodes.length-1;\n};\n\nFS.prototype.CreateFile = function(filename, parentid) {\n    const parent_inode = this.inodes[parentid];\n    if(this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id = this.follow_fs(parent_inode).CreateFile(filename, foreign_parentid);\n        return this.create_forwarder(parent_inode.mount_id, foreign_id);\n    }\n    var x = this.CreateInode();\n    x.uid = this.inodes[parentid].uid;\n    x.gid = this.inodes[parentid].gid;\n    x.qid.type = S_IFREG >> 8;\n    x.mode = (this.inodes[parentid].mode & 0x1B6) | S_IFREG;\n    this.PushInode(x, parentid, filename);\n    this.NotifyListeners(this.inodes.length-1, \"newfile\");\n    return this.inodes.length-1;\n};\n\n\nFS.prototype.CreateNode = function(filename, parentid, major, minor) {\n    const parent_inode = this.inodes[parentid];\n    if(this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id =\n            this.follow_fs(parent_inode).CreateNode(filename, foreign_parentid, major, minor);\n        return this.create_forwarder(parent_inode.mount_id, foreign_id);\n    }\n    var x = this.CreateInode();\n    x.major = major;\n    x.minor = minor;\n    x.uid = this.inodes[parentid].uid;\n    x.gid = this.inodes[parentid].gid;\n    x.qid.type = S_IFSOCK >> 8;\n    x.mode = (this.inodes[parentid].mode & 0x1B6);\n    this.PushInode(x, parentid, filename);\n    return this.inodes.length-1;\n};\n\nFS.prototype.CreateSymlink = function(filename, parentid, symlink) {\n    const parent_inode = this.inodes[parentid];\n    if(this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id =\n            this.follow_fs(parent_inode).CreateSymlink(filename, foreign_parentid, symlink);\n        return this.create_forwarder(parent_inode.mount_id, foreign_id);\n    }\n    var x = this.CreateInode();\n    x.uid = this.inodes[parentid].uid;\n    x.gid = this.inodes[parentid].gid;\n    x.qid.type = S_IFLNK >> 8;\n    x.symlink = symlink;\n    x.mode = S_IFLNK;\n    this.PushInode(x, parentid, filename);\n    return this.inodes.length-1;\n};\n\nFS.prototype.CreateTextFile = async function(filename, parentid, str) {\n    const parent_inode = this.inodes[parentid];\n    if(this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id = await\n            this.follow_fs(parent_inode).CreateTextFile(filename, foreign_parentid, str);\n        return this.create_forwarder(parent_inode.mount_id, foreign_id);\n    }\n    var id = this.CreateFile(filename, parentid);\n    var x = this.inodes[id];\n    var data = new Uint8Array(str.length);\n    x.size = str.length;\n    for(var j = 0; j < str.length; j++) {\n        data[j] = str.charCodeAt(j);\n    }\n    await this.set_data(id, data);\n    return id;\n};\n\n/**\n * @param {Uint8Array} buffer\n */\nFS.prototype.CreateBinaryFile = async function(filename, parentid, buffer) {\n    const parent_inode = this.inodes[parentid];\n    if(this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id = await\n            this.follow_fs(parent_inode).CreateBinaryFile(filename, foreign_parentid, buffer);\n        return this.create_forwarder(parent_inode.mount_id, foreign_id);\n    }\n    var id = this.CreateFile(filename, parentid);\n    var x = this.inodes[id];\n    var data = new Uint8Array(buffer.length);\n    data.set(buffer);\n    await this.set_data(id, data);\n    x.size = buffer.length;\n    return id;\n};\n\n\nFS.prototype.OpenInode = async function(id, mode) {\n    var inode = this.inodes[id];\n    if(this.is_forwarder(inode))\n    {\n        return await this.follow_fs(inode).OpenInode(inode.foreign_id, mode);\n    }\n    if((inode.mode&S_IFMT) === S_IFDIR) {\n        this.FillDirectory(id);\n    }\n    /*\n    var type = \"\";\n    switch(inode.mode&S_IFMT) {\n        case S_IFREG: type = \"File\"; break;\n        case S_IFBLK: type = \"Block Device\"; break;\n        case S_IFDIR: type = \"Directory\"; break;\n        case S_IFCHR: type = \"Character Device\"; break;\n    }\n    */\n    //dbg_log(\"open:\" + this.GetFullPath(id) +  \" type: \" + inode.mode + \" status:\" + inode.status, LOG_9P);\n};\n\nFS.prototype.CloseInode = async function(id) {\n    //dbg_log(\"close: \" + this.GetFullPath(id), LOG_9P);\n    var inode = this.inodes[id];\n    if(this.is_forwarder(inode))\n    {\n        return await this.follow_fs(inode).CloseInode(inode.foreign_id);\n    }\n    if(inode.status === STATUS_ON_STORAGE)\n    {\n        this.storage.uncache(inode.sha256sum);\n    }\n    if(inode.status === STATUS_UNLINKED) {\n        //dbg_log(\"Filesystem: Delete unlinked file\", LOG_9P);\n        inode.status = STATUS_INVALID;\n        await this.DeleteData(id);\n    }\n};\n\n/**\n * @return {!Promise<number>} 0 if success, or -errno if failured.\n */\nFS.prototype.Rename = async function(olddirid, oldname, newdirid, newname) {\n    // dbg_log(\"Rename \" + oldname + \" to \" + newname, LOG_9P);\n    if((olddirid === newdirid) && (oldname === newname)) {\n        return 0;\n    }\n    var oldid = this.Search(olddirid, oldname);\n    if(oldid === -1)\n    {\n        return -ENOENT;\n    }\n\n    // For event notification near end of method.\n    var oldpath = this.GetFullPath(olddirid) + \"/\" + oldname;\n\n    var newid = this.Search(newdirid, newname);\n    if(newid !== -1) {\n        const ret = this.Unlink(newdirid, newname);\n        if(ret < 0) return ret;\n    }\n\n    var idx = oldid; // idx contains the id which we want to rename\n    var inode = this.inodes[idx];\n    const olddir = this.inodes[olddirid];\n    const newdir = this.inodes[newdirid];\n\n    if(!this.is_forwarder(olddir) && !this.is_forwarder(newdir))\n    {\n        // Move inode within current filesystem.\n\n        this.unlink_from_dir(olddirid, oldname);\n        this.link_under_dir(newdirid, idx, newname);\n\n        inode.qid.version++;\n    }\n    else if(this.is_forwarder(olddir) && olddir.mount_id === newdir.mount_id)\n    {\n        // Move inode within the same child filesystem.\n\n        const ret = await\n            this.follow_fs(olddir).Rename(olddir.foreign_id, oldname, newdir.foreign_id, newname);\n\n        if(ret < 0) return ret;\n    }\n    else if(this.is_a_root(idx))\n    {\n        // The actual inode is a root of some descendant filesystem.\n        // Moving mountpoint across fs not supported - needs to update all corresponding forwarders.\n        dbg_log(\"XXX: Attempted to move mountpoint (\" + oldname + \") - skipped\", LOG_9P);\n        return -EPERM;\n    }\n    else if(!this.IsDirectory(idx) && this.GetInode(idx).nlinks > 1)\n    {\n        // Move hardlinked inode vertically in mount tree.\n        dbg_log(\"XXX: Attempted to move hardlinked file (\" + oldname + \") \" +\n                \"across filesystems - skipped\", LOG_9P);\n        return -EPERM;\n    }\n    else\n    {\n        // Jump between filesystems.\n\n        // Can't work with both old and new inode information without first diverting the old\n        // information into a new idx value.\n        const diverted_old_idx = this.divert(olddirid, oldname);\n        const old_real_inode = this.GetInode(idx);\n\n        const data = await this.Read(diverted_old_idx, 0, old_real_inode.size);\n\n        if(this.is_forwarder(newdir))\n        {\n            // Create new inode.\n            const foreign_fs = this.follow_fs(newdir);\n            const foreign_id = this.IsDirectory(diverted_old_idx) ?\n                foreign_fs.CreateDirectory(newname, newdir.foreign_id) :\n                foreign_fs.CreateFile(newname, newdir.foreign_id);\n\n            const new_real_inode = foreign_fs.GetInode(foreign_id);\n            this.copy_inode(old_real_inode, new_real_inode);\n\n            // Point to this new location.\n            this.set_forwarder(idx, newdir.mount_id, foreign_id);\n        }\n        else\n        {\n            // Replace current forwarder with real inode.\n            this.delete_forwarder(inode);\n            this.copy_inode(old_real_inode, inode);\n\n            // Link into new location in this filesystem.\n            this.link_under_dir(newdirid, idx, newname);\n        }\n\n        // Rewrite data to newly created destination.\n        await this.ChangeSize(idx, old_real_inode.size);\n        if(data && data.length)\n        {\n            await this.Write(idx, 0, data.length, data);\n        }\n\n        // Move children to newly created destination.\n        if(this.IsDirectory(idx))\n        {\n            for(const child_filename of this.GetChildren(diverted_old_idx))\n            {\n                const ret = await this.Rename(diverted_old_idx, child_filename, idx, child_filename);\n                if(ret < 0) return ret;\n            }\n        }\n\n        // Perform destructive changes only after migration succeeded.\n        await this.DeleteData(diverted_old_idx);\n        const ret = this.Unlink(olddirid, oldname);\n        if(ret < 0) return ret;\n    }\n\n    this.NotifyListeners(idx, \"rename\", {oldpath: oldpath});\n\n    return 0;\n};\n\nFS.prototype.Write = async function(id, offset, count, buffer) {\n    this.NotifyListeners(id, \"write\");\n    var inode = this.inodes[id];\n\n    if(this.is_forwarder(inode))\n    {\n        const foreign_id = inode.foreign_id;\n        await this.follow_fs(inode).Write(foreign_id, offset, count, buffer);\n        return;\n    }\n\n    var data = await this.get_buffer(id);\n\n    if(!data || data.length < (offset+count)) {\n        await this.ChangeSize(id, Math.floor(((offset+count)*3)/2));\n        inode.size = offset + count;\n        data = await this.get_buffer(id);\n    } else\n    if(inode.size < (offset+count)) {\n        inode.size = offset + count;\n    }\n    if(buffer)\n    {\n        data.set(buffer.subarray(0, count), offset);\n    }\n    await this.set_data(id, data);\n};\n\nFS.prototype.Read = async function(inodeid, offset, count)\n{\n    const inode = this.inodes[inodeid];\n    if(this.is_forwarder(inode))\n    {\n        const foreign_id = inode.foreign_id;\n        return await this.follow_fs(inode).Read(foreign_id, offset, count);\n    }\n\n    return await this.get_data(inodeid, offset, count);\n};\n\nFS.prototype.Search = function(parentid, name) {\n    const parent_inode = this.inodes[parentid];\n\n    if(this.is_forwarder(parent_inode))\n    {\n        const foreign_parentid = parent_inode.foreign_id;\n        const foreign_id = this.follow_fs(parent_inode).Search(foreign_parentid, name);\n        if(foreign_id === -1) return -1;\n        return this.get_forwarder(parent_inode.mount_id, foreign_id);\n    }\n\n    const childid = parent_inode.direntries.get(name);\n    return childid === undefined ? -1 : childid;\n};\n\nFS.prototype.CountUsedInodes = function()\n{\n    let count = this.inodes.length;\n    for(const { fs, backtrack } of this.mounts)\n    {\n        count += fs.CountUsedInodes();\n\n        // Forwarder inodes don't count.\n        count -=  backtrack.size;\n    }\n    return count;\n};\n\nFS.prototype.CountFreeInodes = function()\n{\n    let count = 1024 * 1024;\n    for(const { fs } of this.mounts)\n    {\n        count += fs.CountFreeInodes();\n    }\n    return count;\n};\n\nFS.prototype.GetTotalSize = function() {\n    let size = this.used_size;\n    for(const { fs } of this.mounts)\n    {\n        size += fs.GetTotalSize();\n    }\n    return size;\n    //var size = 0;\n    //for(var i=0; i<this.inodes.length; i++) {\n    //    var d = this.inodes[i].data;\n    //    size += d ? d.length : 0;\n    //}\n    //return size;\n};\n\nFS.prototype.GetSpace = function() {\n    let size = this.total_size;\n    for(const { fs } of this.mounts)\n    {\n        size += fs.GetSpace();\n    }\n    return this.total_size;\n};\n\n/**\n * XXX: Not ideal.\n * @param {number} idx\n * @return {string}\n */\nFS.prototype.GetDirectoryName = function(idx)\n{\n    const parent_inode = this.inodes[this.GetParent(idx)];\n\n    if(this.is_forwarder(parent_inode))\n    {\n        return this.follow_fs(parent_inode).GetDirectoryName(this.inodes[idx].foreign_id);\n    }\n\n    // Root directory.\n    if(!parent_inode) return \"\";\n\n    for(const [name, childid] of parent_inode.direntries)\n    {\n        if(childid === idx) return name;\n    }\n\n    dbg_assert(false, \"Filesystem: Found directory inode whose parent doesn't link to it\");\n    return \"\";\n};\n\nFS.prototype.GetFullPath = function(idx) {\n    dbg_assert(this.IsDirectory(idx), \"Filesystem: Cannot get full path of non-directory inode\");\n\n    var path = \"\";\n\n    while(idx !== 0) {\n        path = \"/\" + this.GetDirectoryName(idx) + path;\n        idx = this.GetParent(idx);\n    }\n    return path.substring(1);\n};\n\n/**\n * @param {number} parentid\n * @param {number} targetid\n * @param {string} name\n * @return {number} 0 if success, or -errno if failured.\n */\nFS.prototype.Link = function(parentid, targetid, name)\n{\n    if(this.IsDirectory(targetid))\n    {\n        return -EPERM;\n    }\n\n    const parent_inode = this.inodes[parentid];\n    const inode = this.inodes[targetid];\n\n    if(this.is_forwarder(parent_inode))\n    {\n        if(!this.is_forwarder(inode) || inode.mount_id !== parent_inode.mount_id)\n        {\n            dbg_log(\"XXX: Attempted to hardlink a file into a child filesystem - skipped\", LOG_9P);\n            return -EPERM;\n        }\n        return this.follow_fs(parent_inode).Link(parent_inode.foreign_id, inode.foreign_id, name);\n    }\n\n    if(this.is_forwarder(inode))\n    {\n        dbg_log(\"XXX: Attempted to hardlink file across filesystems - skipped\", LOG_9P);\n        return -EPERM;\n    }\n\n    this.link_under_dir(parentid, targetid, name);\n    return 0;\n};\n\nFS.prototype.Unlink = function(parentid, name) {\n    if(name === \".\" || name === \"..\")\n    {\n        // Also guarantees that root cannot be deleted.\n        return -EPERM;\n    }\n    const idx = this.Search(parentid, name);\n    const inode = this.inodes[idx];\n    const parent_inode = this.inodes[parentid];\n    //dbg_log(\"Unlink \" + inode.name, LOG_9P);\n\n    // forward if necessary\n    if(this.is_forwarder(parent_inode))\n    {\n        dbg_assert(this.is_forwarder(inode), \"Children of forwarders should be forwarders\");\n\n        const foreign_parentid = parent_inode.foreign_id;\n        return this.follow_fs(parent_inode).Unlink(foreign_parentid, name);\n\n        // Keep the forwarder dangling - file is still accessible.\n    }\n\n    if(this.IsDirectory(idx) && !this.IsEmpty(idx))\n    {\n        return -ENOTEMPTY;\n    }\n\n    this.unlink_from_dir(parentid, name);\n\n    if(inode.nlinks === 0)\n    {\n        // don't delete the content. The file is still accessible\n        inode.status = STATUS_UNLINKED;\n        this.NotifyListeners(idx, \"delete\");\n    }\n    return 0;\n};\n\nFS.prototype.DeleteData = async function(idx)\n{\n    const inode = this.inodes[idx];\n    if(this.is_forwarder(inode))\n    {\n        await this.follow_fs(inode).DeleteData(inode.foreign_id);\n        return;\n    }\n    inode.size = 0;\n    delete this.inodedata[idx];\n};\n\n/**\n * @private\n * @param {number} idx\n * @return {!Promise<Uint8Array>} The buffer that contains the file contents, which may be larger\n *      than the data itself. To ensure that any modifications done to this buffer is reflected\n *      to the file, call set_data with the modified buffer.\n */\nFS.prototype.get_buffer = async function(idx)\n{\n    const inode = this.inodes[idx];\n    dbg_assert(inode, `Filesystem get_buffer: idx ${idx} does not point to an inode`);\n\n    if(this.inodedata[idx])\n    {\n        return this.inodedata[idx];\n    }\n    else if(inode.status === STATUS_ON_STORAGE)\n    {\n        dbg_assert(inode.sha256sum, \"Filesystem get_data: found inode on server without sha256sum\");\n        return await this.storage.read(inode.sha256sum, 0, inode.size, inode.size);\n    }\n    else\n    {\n        return null;\n    }\n};\n\n/**\n * @private\n * @param {number} idx\n * @param {number} offset\n * @param {number} count\n * @return {!Promise<Uint8Array>}\n */\nFS.prototype.get_data = async function(idx, offset, count)\n{\n    const inode = this.inodes[idx];\n    dbg_assert(inode, `Filesystem get_data: idx ${idx} does not point to an inode`);\n\n    if(this.inodedata[idx])\n    {\n        return this.inodedata[idx].subarray(offset, offset + count);\n    }\n    else if(inode.status === STATUS_ON_STORAGE)\n    {\n        dbg_assert(inode.sha256sum, \"Filesystem get_data: found inode on server without sha256sum\");\n        return await this.storage.read(inode.sha256sum, offset, count, inode.size);\n    }\n    else\n    {\n        return null;\n    }\n};\n\n/**\n * @private\n * @param {number} idx\n * @param {Uint8Array} buffer\n */\nFS.prototype.set_data = async function(idx, buffer)\n{\n    // Current scheme: Save all modified buffers into local inodedata.\n    this.inodedata[idx] = buffer;\n    if(this.inodes[idx].status === STATUS_ON_STORAGE)\n    {\n        this.inodes[idx].status = STATUS_OK;\n        this.storage.uncache(this.inodes[idx].sha256sum);\n    }\n};\n\n/**\n * @param {number} idx\n * @return {!Inode}\n */\nFS.prototype.GetInode = function(idx)\n{\n    dbg_assert(!isNaN(idx), \"Filesystem GetInode: NaN idx\");\n    dbg_assert(idx >= 0 && idx < this.inodes.length, \"Filesystem GetInode: out of range idx:\" + idx);\n\n    const inode = this.inodes[idx];\n    if(this.is_forwarder(inode))\n    {\n        return this.follow_fs(inode).GetInode(inode.foreign_id);\n    }\n\n    return inode;\n};\n\nFS.prototype.ChangeSize = async function(idx, newsize)\n{\n    var inode = this.GetInode(idx);\n    var temp = await this.get_data(idx, 0, inode.size);\n    //dbg_log(\"change size to: \" + newsize, LOG_9P);\n    if(newsize === inode.size) return;\n    var data = new Uint8Array(newsize);\n    inode.size = newsize;\n    if(temp)\n    {\n        var size = Math.min(temp.length, inode.size);\n        data.set(temp.subarray(0, size), 0);\n    }\n    await this.set_data(idx, data);\n};\n\nFS.prototype.SearchPath = function(path) {\n    //path = path.replace(/\\/\\//g, \"/\");\n    path = path.replace(\"//\", \"/\");\n    var walk = path.split(\"/\");\n    if(walk.length > 0 && walk[walk.length - 1].length === 0) walk.pop();\n    if(walk.length > 0 && walk[0].length === 0) walk.shift();\n    const n = walk.length;\n\n    var parentid = -1;\n    var id = 0;\n    let forward_path = null;\n    for(var i=0; i<n; i++) {\n        parentid = id;\n        id = this.Search(parentid, walk[i]);\n        if(!forward_path && this.is_forwarder(this.inodes[parentid]))\n        {\n            forward_path = \"/\" + walk.slice(i).join(\"/\");\n        }\n        if(id === -1) {\n            if(i < n-1) return {id: -1, parentid: -1, name: walk[i], forward_path }; // one name of the path cannot be found\n            return {id: -1, parentid: parentid, name: walk[i], forward_path}; // the last element in the path does not exist, but the parent\n        }\n    }\n    return {id: id, parentid: parentid, name: walk[i], forward_path};\n};\n// -----------------------------------------------------\n\n/**\n * @param {number} dirid\n * @param {Array<{parentid: number, name: string}>} list\n */\nFS.prototype.GetRecursiveList = function(dirid, list) {\n    if(this.is_forwarder(this.inodes[dirid]))\n    {\n        const foreign_fs = this.follow_fs(this.inodes[dirid]);\n        const foreign_dirid = this.inodes[dirid].foreign_id;\n        const mount_id = this.inodes[dirid].mount_id;\n\n        const foreign_start = list.length;\n        foreign_fs.GetRecursiveList(foreign_dirid, list);\n        for(let i = foreign_start; i < list.length; i++)\n        {\n            list[i].parentid = this.get_forwarder(mount_id, list[i].parentid);\n        }\n        return;\n    }\n    for(const [name, id] of this.inodes[dirid].direntries)\n    {\n        if(name !== \".\" && name !== \"..\")\n        {\n            list.push({ parentid: dirid, name });\n            if(this.IsDirectory(id))\n            {\n                this.GetRecursiveList(id, list);\n            }\n        }\n    }\n};\n\nFS.prototype.RecursiveDelete = function(path) {\n    var toDelete = [];\n    var ids = this.SearchPath(path);\n    if(ids.id === -1) return;\n\n    this.GetRecursiveList(ids.id, toDelete);\n\n    for(var i=toDelete.length-1; i>=0; i--)\n    {\n        const ret = this.Unlink(toDelete[i].parentid, toDelete[i].name);\n        dbg_assert(ret === 0, \"Filesystem RecursiveDelete failed at parent=\" + toDelete[i].parentid +\n            \", name='\" + toDelete[i].name + \"' with error code: \" + (-ret));\n    }\n};\n\nFS.prototype.DeleteNode = function(path) {\n    var ids = this.SearchPath(path);\n    if(ids.id === -1) return;\n\n    if((this.inodes[ids.id].mode&S_IFMT) === S_IFREG){\n        const ret = this.Unlink(ids.parentid, ids.name);\n        dbg_assert(ret === 0, \"Filesystem DeleteNode failed with error code: \" + (-ret));\n    }\n    else if((this.inodes[ids.id].mode&S_IFMT) === S_IFDIR){\n        this.RecursiveDelete(path);\n        const ret = this.Unlink(ids.parentid, ids.name);\n        dbg_assert(ret === 0, \"Filesystem DeleteNode failed with error code: \" + (-ret));\n    }\n};\n\n/** @param {*=} info */\nFS.prototype.NotifyListeners = function(id, action, info) {\n    //if(info==undefined)\n    //    info = {};\n\n    //var path = this.GetFullPath(id);\n    //if (this.watchFiles[path] === true && action=='write') {\n    //  message.Send(\"WatchFileEvent\", path);\n    //}\n    //for (var directory of this.watchDirectories) {\n    //    if (this.watchDirectories.hasOwnProperty(directory)) {\n    //        var indexOf = path.indexOf(directory)\n    //        if(indexOf === 0 || indexOf === 1)\n    //            message.Send(\"WatchDirectoryEvent\", {path: path, event: action, info: info});\n    //    }\n    //}\n};\n\n\nFS.prototype.Check = function() {\n    for(var i=1; i<this.inodes.length; i++)\n    {\n        if(this.inodes[i].status === STATUS_INVALID) continue;\n\n        var inode = this.GetInode(i);\n        if(inode.nlinks < 0) {\n            dbg_log(\"Error in filesystem: negative nlinks=\" + inode.nlinks + \" at id =\" + i, LOG_9P);\n        }\n\n        if(this.IsDirectory(i))\n        {\n            const inode = this.GetInode(i);\n            if(this.IsDirectory(i) && this.GetParent(i) < 0) {\n                dbg_log(\"Error in filesystem: negative parent id \" + i, LOG_9P);\n            }\n            for(const [name, id] of inode.direntries)\n            {\n                if(name.length === 0) {\n                    dbg_log(\"Error in filesystem: inode with no name and id \" + id, LOG_9P);\n                }\n\n                for(const c of name) {\n                    if(c < 32) {\n                        dbg_log(\"Error in filesystem: Unallowed char in filename\", LOG_9P);\n                    }\n                }\n            }\n        }\n    }\n\n};\n\n\nFS.prototype.FillDirectory = function(dirid) {\n    const inode = this.inodes[dirid];\n    if(this.is_forwarder(inode))\n    {\n        // XXX: The \"..\" of a mountpoint should point back to an inode in this fs.\n        // Otherwise, \"..\" gets the wrong qid and mode.\n        this.follow_fs(inode).FillDirectory(inode.foreign_id);\n        return;\n    }\n\n    let size = 0;\n    for(const name of inode.direntries.keys())\n    {\n        size += 13 + 8 + 1 + 2 + texten.encode(name).length;\n    }\n    const data = this.inodedata[dirid] = new Uint8Array(size);\n    inode.size = size;\n\n    let offset = 0x0;\n    for(const [name, id] of inode.direntries)\n    {\n        const child = this.GetInode(id);\n        offset += marshall.Marshall(\n            [\"Q\", \"d\", \"b\", \"s\"],\n            [child.qid,\n            offset+13+8+1+2+texten.encode(name).length,\n            child.mode >> 12,\n            name],\n            data, offset);\n    }\n};\n\nFS.prototype.RoundToDirentry = function(dirid, offset_target)\n{\n    const data = this.inodedata[dirid];\n    dbg_assert(data, `FS directory data for dirid=${dirid} should be generated`);\n    dbg_assert(data.length, \"FS directory should have at least an entry\");\n\n    if(offset_target >= data.length)\n    {\n        return data.length;\n    }\n\n    let offset = 0;\n    while(true)\n    {\n        const next_offset = marshall.Unmarshall([\"Q\", \"d\"], data, { offset })[1];\n        if(next_offset > offset_target) break;\n        offset = next_offset;\n    }\n\n    return offset;\n};\n\n/**\n * @param {number} idx\n * @return {boolean}\n */\nFS.prototype.IsDirectory = function(idx)\n{\n    const inode = this.inodes[idx];\n    if(this.is_forwarder(inode))\n    {\n        return this.follow_fs(inode).IsDirectory(inode.foreign_id);\n    }\n    return (inode.mode & S_IFMT) === S_IFDIR;\n};\n\n/**\n * @param {number} idx\n * @return {boolean}\n */\nFS.prototype.IsEmpty = function(idx)\n{\n    const inode = this.inodes[idx];\n    if(this.is_forwarder(inode))\n    {\n        return this.follow_fs(inode).IsDirectory(inode.foreign_id);\n    }\n    for(const name of inode.direntries.keys())\n    {\n        if(name !== \".\" && name !== \"..\") return false;\n    }\n    return true;\n};\n\n/**\n * @param {number} idx\n * @return {!Array<string>} List of children names\n */\nFS.prototype.GetChildren = function(idx)\n{\n    dbg_assert(this.IsDirectory(idx), \"Filesystem: cannot get children of non-directory inode\");\n    const inode = this.inodes[idx];\n    if(this.is_forwarder(inode))\n    {\n        return this.follow_fs(inode).GetChildren(inode.foreign_id);\n    }\n    const children = [];\n    for(const name of inode.direntries.keys())\n    {\n        if(name !== \".\" && name !== \"..\")\n        {\n            children.push(name);\n        }\n    }\n    return children;\n};\n\n/**\n * @param {number} idx\n * @return {number} Local idx of parent\n */\nFS.prototype.GetParent = function(idx)\n{\n    dbg_assert(this.IsDirectory(idx), \"Filesystem: cannot get parent of non-directory inode\");\n\n    const inode = this.inodes[idx];\n\n    if(this.should_be_linked(inode))\n    {\n        return inode.direntries.get(\"..\");\n    }\n    else\n    {\n        const foreign_dirid = this.follow_fs(inode).GetParent(inode.foreign_id);\n        dbg_assert(foreign_dirid !== -1, \"Filesystem: should not have invalid parent ids\");\n        return this.get_forwarder(inode.mount_id, foreign_dirid);\n    }\n};\n\n\n// -----------------------------------------------------\n\n// only support for security.capabilities\n// should return a  \"struct vfs_cap_data\" defined in\n// linux/capability for format\n// check also:\n//   sys/capability.h\n//   http://lxr.free-electrons.com/source/security/commoncap.c#L376\n//   http://man7.org/linux/man-pages/man7/capabilities.7.html\n//   http://man7.org/linux/man-pages/man8/getcap.8.html\n//   http://man7.org/linux/man-pages/man3/libcap.3.html\nFS.prototype.PrepareCAPs = function(id) {\n    var inode = this.GetInode(id);\n    if(inode.caps) return inode.caps.length;\n    inode.caps = new Uint8Array(20);\n    // format is little endian\n    // note: getxattr returns -EINVAL if using revision 1 format.\n    // note: getxattr presents revision 3 as revision 2 when revision 3 is not needed.\n    // magic_etc (revision=0x02: 20 bytes)\n    inode.caps[0]  = 0x00;\n    inode.caps[1]  = 0x00;\n    inode.caps[2]  = 0x00;\n    inode.caps[3]  = 0x02;\n\n    // lower\n    // permitted (first 32 capabilities)\n    inode.caps[4]  = 0xFF;\n    inode.caps[5]  = 0xFF;\n    inode.caps[6]  = 0xFF;\n    inode.caps[7]  = 0xFF;\n    // inheritable (first 32 capabilities)\n    inode.caps[8]  = 0xFF;\n    inode.caps[9]  = 0xFF;\n    inode.caps[10] = 0xFF;\n    inode.caps[11] = 0xFF;\n\n    // higher\n    // permitted (last 6 capabilities)\n    inode.caps[12] = 0x3F;\n    inode.caps[13] = 0x00;\n    inode.caps[14] = 0x00;\n    inode.caps[15] = 0x00;\n    // inheritable (last 6 capabilities)\n    inode.caps[16] = 0x3F;\n    inode.caps[17] = 0x00;\n    inode.caps[18] = 0x00;\n    inode.caps[19] = 0x00;\n\n    return inode.caps.length;\n};\n\n// -----------------------------------------------------\n\n/**\n * @constructor\n * @param {FS} filesystem\n */\nfunction FSMountInfo(filesystem)\n{\n    /** @type {FS}*/\n    this.fs = filesystem;\n\n    /**\n     * Maps foreign inode id back to local inode id.\n     * @type {!Map<number,number>}\n     */\n    this.backtrack = new Map();\n}\n\nFSMountInfo.prototype.get_state = function()\n{\n    const state = [];\n\n    state[0] = this.fs;\n    state[1] = [...this.backtrack];\n\n    return state;\n};\n\nFSMountInfo.prototype.set_state = function(state)\n{\n    this.fs = state[0];\n    this.backtrack = new Map(state[1]);\n};\n\n/**\n * @private\n * @param {number} idx Local idx of inode.\n * @param {number} mount_id Mount number of the destination fs.\n * @param {number} foreign_id Foreign idx of destination inode.\n */\nFS.prototype.set_forwarder = function(idx, mount_id, foreign_id)\n{\n    const inode = this.inodes[idx];\n\n    dbg_assert(inode.nlinks === 0,\n        \"Filesystem: attempted to convert an inode into forwarder before unlinking the inode\");\n\n    if(this.is_forwarder(inode))\n    {\n        this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id);\n    }\n\n    inode.status = STATUS_FORWARDING;\n    inode.mount_id = mount_id;\n    inode.foreign_id = foreign_id;\n\n    this.mounts[mount_id].backtrack.set(foreign_id, idx);\n};\n\n/**\n * @private\n * @param {number} mount_id Mount number of the destination fs.\n * @param {number} foreign_id Foreign idx of destination inode.\n * @return {number} Local idx of newly created forwarder.\n */\nFS.prototype.create_forwarder = function(mount_id, foreign_id)\n{\n    const inode = this.CreateInode();\n\n    const idx = this.inodes.length;\n    this.inodes.push(inode);\n    inode.fid = idx;\n\n    this.set_forwarder(idx, mount_id, foreign_id);\n    return idx;\n};\n\n/**\n * @private\n * @param {Inode} inode\n * @return {boolean}\n */\nFS.prototype.is_forwarder = function(inode)\n{\n    return inode.status === STATUS_FORWARDING;\n};\n\n/**\n * Whether the inode it points to is a root of some filesystem.\n * @private\n * @param {number} idx\n * @return {boolean}\n */\nFS.prototype.is_a_root = function(idx)\n{\n    return this.GetInode(idx).fid === 0;\n};\n\n/**\n * Ensures forwarder exists, and returns such forwarder, for the described foreign inode.\n * @private\n * @param {number} mount_id\n * @param {number} foreign_id\n * @return {number} Local idx of a forwarder to described inode.\n */\nFS.prototype.get_forwarder = function(mount_id, foreign_id)\n{\n    const mount = this.mounts[mount_id];\n\n    dbg_assert(foreign_id >= 0, \"Filesystem get_forwarder: invalid foreign_id: \" + foreign_id);\n    dbg_assert(mount, \"Filesystem get_forwarder: invalid mount number: \" + mount_id);\n\n    const result = mount.backtrack.get(foreign_id);\n\n    if(result === undefined)\n    {\n        // Create if not already exists.\n        return this.create_forwarder(mount_id, foreign_id);\n    }\n\n    return result;\n};\n\n/**\n * @private\n * @param {Inode} inode\n */\nFS.prototype.delete_forwarder = function(inode)\n{\n    dbg_assert(this.is_forwarder(inode), \"Filesystem delete_forwarder: expected forwarder\");\n\n    inode.status = STATUS_INVALID;\n    this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id);\n};\n\n/**\n * @private\n * @param {Inode} inode\n * @return {FS}\n */\nFS.prototype.follow_fs = function(inode)\n{\n    const mount = this.mounts[inode.mount_id];\n\n    dbg_assert(this.is_forwarder(inode),\n        \"Filesystem follow_fs: inode should be a forwarding inode\");\n    dbg_assert(mount, \"Filesystem follow_fs: inode<id=\" + inode.fid +\n        \"> should point to valid mounted FS\");\n\n    return mount.fs;\n};\n\n/**\n * Mount another filesystem to given path.\n * @param {string} path\n * @param {FS} fs\n * @return {number} inode id of mount point if successful, or -errno if mounting failed.\n */\nFS.prototype.Mount = function(path, fs)\n{\n    dbg_assert(fs.qidcounter === this.qidcounter,\n        \"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.\");\n\n    const path_infos = this.SearchPath(path);\n\n    if(path_infos.parentid === -1)\n    {\n        dbg_log(\"Mount failed: parent for path not found: \" + path, LOG_9P);\n        return -ENOENT;\n    }\n    if(path_infos.id !== -1)\n    {\n        dbg_log(\"Mount failed: file already exists at path: \" + path, LOG_9P);\n        return -EEXIST;\n    }\n    if(path_infos.forward_path)\n    {\n        const parent = this.inodes[path_infos.parentid];\n        const ret = this.follow_fs(parent).Mount(path_infos.forward_path, fs);\n        if(ret < 0) return ret;\n        return this.get_forwarder(parent.mount_id, ret);\n    }\n\n    const mount_id = this.mounts.length;\n    this.mounts.push(new FSMountInfo(fs));\n\n    const idx = this.create_forwarder(mount_id, 0);\n    this.link_under_dir(path_infos.parentid, idx, path_infos.name);\n\n    return idx;\n};\n\n/**\n * @constructor\n */\nfunction FSLockRegion()\n{\n    this.type = P9_LOCK_TYPE_UNLCK;\n    this.start = 0;\n    this.length = Infinity;\n    this.proc_id = -1;\n    this.client_id = \"\";\n}\n\nFSLockRegion.prototype.get_state = function()\n{\n    const state = [];\n\n    state[0] = this.type;\n    state[1] = this.start;\n    // Infinity is not JSON.stringify-able\n    state[2] = this.length === Infinity ? 0 : this.length;\n    state[3] = this.proc_id;\n    state[4] = this.client_id;\n\n    return state;\n};\n\nFSLockRegion.prototype.set_state = function(state)\n{\n    this.type = state[0];\n    this.start = state[1];\n    this.length = state[2] === 0 ? Infinity : state[2];\n    this.proc_id = state[3];\n    this.client_id = state[4];\n};\n\n/**\n * @return {FSLockRegion}\n */\nFSLockRegion.prototype.clone = function()\n{\n    const new_region = new FSLockRegion();\n    new_region.set_state(this.get_state());\n    return new_region;\n};\n\n/**\n * @param {FSLockRegion} region\n * @return {boolean}\n */\nFSLockRegion.prototype.conflicts_with = function(region)\n{\n    if(this.proc_id === region.proc_id && this.client_id === region.client_id) return false;\n    if(this.type === P9_LOCK_TYPE_UNLCK || region.type === P9_LOCK_TYPE_UNLCK) return false;\n    if(this.type !== P9_LOCK_TYPE_WRLCK && region.type !== P9_LOCK_TYPE_WRLCK) return false;\n    if(this.start + this.length <= region.start) return false;\n    if(region.start + region.length <= this.start) return false;\n    return true;\n};\n\n/**\n * @param {FSLockRegion} region\n * @return {boolean}\n */\nFSLockRegion.prototype.is_alike = function(region)\n{\n    return region.proc_id === this.proc_id &&\n        region.client_id === this.client_id &&\n        region.type === this.type;\n};\n\n/**\n * @param {FSLockRegion} region\n * @return {boolean}\n */\nFSLockRegion.prototype.may_merge_after = function(region)\n{\n    return this.is_alike(region) && region.start + region.length === this.start;\n};\n\n/**\n * @param {number} type\n * @param {number} start\n * @param {number} length\n * @param {number} proc_id\n * @param {string} client_id\n * @return {!FSLockRegion}\n */\nFS.prototype.DescribeLock = function(type, start, length, proc_id, client_id)\n{\n    dbg_assert(type === P9_LOCK_TYPE_RDLCK ||\n        type === P9_LOCK_TYPE_WRLCK ||\n        type === P9_LOCK_TYPE_UNLCK,\n        \"Filesystem: Invalid lock type: \" + type);\n    dbg_assert(start >= 0, \"Filesystem: Invalid negative lock starting offset: \" + start);\n    dbg_assert(length > 0, \"Filesystem: Invalid non-positive lock length: \" + length);\n\n    const lock = new FSLockRegion();\n    lock.type = type;\n    lock.start = start;\n    lock.length = length;\n    lock.proc_id = proc_id;\n    lock.client_id = client_id;\n\n    return lock;\n};\n\n/**\n * @param {number} id\n * @param {FSLockRegion} request\n * @return {FSLockRegion} The first conflicting lock found, or null if requested lock is possible.\n */\nFS.prototype.GetLock = function(id, request)\n{\n    const inode = this.inodes[id];\n\n    if(this.is_forwarder(inode))\n    {\n        const foreign_id = inode.foreign_id;\n        return this.follow_fs(inode).GetLock(foreign_id, request);\n    }\n\n    for(const region of inode.locks)\n    {\n        if(request.conflicts_with(region))\n        {\n            return region.clone();\n        }\n    }\n    return null;\n};\n\n/**\n * @param {number} id\n * @param {FSLockRegion} request\n * @param {number} flags\n * @return {number} One of P9_LOCK_SUCCESS / P9_LOCK_BLOCKED / P9_LOCK_ERROR / P9_LOCK_GRACE.\n */\nFS.prototype.Lock = function(id, request, flags)\n{\n    const inode = this.inodes[id];\n\n    if(this.is_forwarder(inode))\n    {\n        const foreign_id = inode.foreign_id;\n        return this.follow_fs(inode).Lock(foreign_id, request, flags);\n    }\n\n    request = request.clone();\n\n    // (1) Check whether lock is possible before any modification.\n    if(request.type !== P9_LOCK_TYPE_UNLCK && this.GetLock(id, request))\n    {\n        return P9_LOCK_BLOCKED;\n    }\n\n    // (2) Subtract requested region from locks of the same owner.\n    for(let i = 0; i < inode.locks.length; i++)\n    {\n        const region = inode.locks[i];\n\n        dbg_assert(region.length > 0,\n            \"Filesystem: Found non-positive lock region length: \" + region.length);\n        dbg_assert(region.type === P9_LOCK_TYPE_RDLCK || region.type === P9_LOCK_TYPE_WRLCK,\n            \"Filesystem: Found invalid lock type: \" + region.type);\n        dbg_assert(!inode.locks[i-1] || inode.locks[i-1].start <= region.start,\n            \"Filesystem: Locks should be sorted by starting offset\");\n\n        // Skip to requested region.\n        if(region.start + region.length <= request.start) continue;\n\n        // Check whether we've skipped past the requested region.\n        if(request.start + request.length <= region.start) break;\n\n        // Skip over locks of different owners.\n        if(region.proc_id !== request.proc_id || region.client_id !== request.client_id)\n        {\n            dbg_assert(!region.conflicts_with(request),\n                \"Filesytem: Found conflicting lock region, despite already checked for conflicts\");\n            continue;\n        }\n\n        // Pretend region would be split into parts 1 and 2.\n        const start1 = region.start;\n        const start2 = request.start + request.length;\n        const length1 = request.start - start1;\n        const length2 = region.start + region.length - start2;\n\n        if(length1 > 0 && length2 > 0 && region.type === request.type)\n        {\n            // Requested region is already locked with the required type.\n            // Return early - no need to modify anything.\n            return P9_LOCK_SUCCESS;\n        }\n\n        if(length1 > 0)\n        {\n            // Shrink from right / first half of the split.\n            region.length = length1;\n        }\n\n        if(length1 <= 0 && length2 > 0)\n        {\n            // Shrink from left.\n            region.start = start2;\n            region.length = length2;\n        }\n        else if(length2 > 0)\n        {\n            // Add second half of the split.\n\n            // Fast-forward to correct location.\n            while(i < inode.locks.length && inode.locks[i].start < start2) i++;\n\n            inode.locks.splice(i, 0,\n                this.DescribeLock(region.type, start2, length2, region.proc_id, region.client_id));\n        }\n        else if(length1 <= 0)\n        {\n            // Requested region completely covers this region. Delete.\n            inode.locks.splice(i, 1);\n            i--;\n        }\n    }\n\n    // (3) Insert requested lock region as a whole.\n    // No point in adding the requested lock region as fragmented bits in the above loop\n    // and having to merge them all back into one.\n    if(request.type !== P9_LOCK_TYPE_UNLCK)\n    {\n        let new_region = request;\n        let has_merged = false;\n        let i = 0;\n\n        // Fast-forward to requested position, and try merging with previous region.\n        for(; i < inode.locks.length; i++)\n        {\n            if(new_region.may_merge_after(inode.locks[i]))\n            {\n                inode.locks[i].length += request.length;\n                new_region = inode.locks[i];\n                has_merged = true;\n            }\n            if(request.start <= inode.locks[i].start) break;\n        }\n\n        if(!has_merged)\n        {\n            inode.locks.splice(i, 0, new_region);\n            i++;\n        }\n\n        // Try merging with the subsequent alike region.\n        for(; i < inode.locks.length; i++)\n        {\n            if(!inode.locks[i].is_alike(new_region)) continue;\n\n            if(inode.locks[i].may_merge_after(new_region))\n            {\n                new_region.length += inode.locks[i].length;\n                inode.locks.splice(i, 1);\n            }\n\n            // No more mergable regions after this.\n            break;\n        }\n    }\n\n    return P9_LOCK_SUCCESS;\n};\n\nFS.prototype.read_dir = function(path)\n{\n    const p = this.SearchPath(path);\n\n    if(p.id === -1)\n    {\n        return undefined;\n    }\n\n    const dir = this.GetInode(p.id);\n\n    return Array.from(dir.direntries.keys()).filter(path => path !== \".\" && path !== \"..\");\n};\n\nFS.prototype.read_file = function(file)\n{\n    const p = this.SearchPath(file);\n\n    if(p.id === -1)\n    {\n        return Promise.resolve(null);\n    }\n\n    const inode = this.GetInode(p.id);\n\n    return this.Read(p.id, 0, inode.size);\n};\n"
  },
  {
    "path": "lib/marshall.js",
    "content": "// -------------------------------------------------\n// ------------------ Marshall ---------------------\n// -------------------------------------------------\n// helper functions for virtio and 9p.\n\nimport { dbg_log } from \"./../src/log.js\";\n\nconst textde = new TextDecoder();\nconst texten = new TextEncoder();\n\n// Inserts data from an array to a byte aligned struct in memory\nexport function Marshall(typelist, input, struct, offset) {\n    var item;\n    var size = 0;\n    for(var i=0; i < typelist.length; i++) {\n        item = input[i];\n        switch(typelist[i]) {\n            case \"w\":\n                struct[offset++] = item & 0xFF;\n                struct[offset++] = (item >> 8) & 0xFF;\n                struct[offset++] = (item >> 16) & 0xFF;\n                struct[offset++] = (item >> 24) & 0xFF;\n                size += 4;\n                break;\n            case \"d\": // double word\n                struct[offset++] = item & 0xFF;\n                struct[offset++] = (item >> 8) & 0xFF;\n                struct[offset++] = (item >> 16) & 0xFF;\n                struct[offset++] = (item >> 24) & 0xFF;\n                struct[offset++] = 0x0;\n                struct[offset++] = 0x0;\n                struct[offset++] = 0x0;\n                struct[offset++] = 0x0;\n                size += 8;\n                break;\n            case \"h\":\n                struct[offset++] = item & 0xFF;\n                struct[offset++] = item >> 8;\n                size += 2;\n                break;\n            case \"b\":\n                struct[offset++] = item;\n                size += 1;\n                break;\n            case \"s\":\n                var lengthoffset = offset;\n                var length = 0;\n                struct[offset++] = 0; // set the length later\n                struct[offset++] = 0;\n                size += 2;\n\n                var stringBytes = texten.encode(item);\n                size += stringBytes.byteLength;\n                length += stringBytes.byteLength;\n                struct.set(stringBytes, offset);\n                offset += stringBytes.byteLength;\n\n                struct[lengthoffset+0] = length & 0xFF;\n                struct[lengthoffset+1] = (length >> 8) & 0xFF;\n                break;\n            case \"Q\":\n                Marshall([\"b\", \"w\", \"d\"], [item.type, item.version, item.path], struct, offset);\n                offset += 13;\n                size += 13;\n                break;\n            default:\n                dbg_log(\"Marshall: Unknown type=\" + typelist[i]);\n                break;\n        }\n    }\n    return size;\n}\n\n\n// Extracts data from a byte aligned struct in memory to an array\nexport function Unmarshall(typelist, struct, state) {\n    let offset = state.offset;\n    var output = [];\n    for(var i=0; i < typelist.length; i++) {\n        switch(typelist[i]) {\n            case \"w\":\n                var val = struct[offset++];\n                val += struct[offset++] << 8;\n                val += struct[offset++] << 16;\n                val += (struct[offset++] << 24) >>> 0;\n                output.push(val);\n                break;\n            case \"d\":\n                var val = struct[offset++];\n                val += struct[offset++] << 8;\n                val += struct[offset++] << 16;\n                val += (struct[offset++] << 24) >>> 0;\n                offset += 4;\n                output.push(val);\n                break;\n            case \"h\":\n                var val = struct[offset++];\n                output.push(val + (struct[offset++] << 8));\n                break;\n            case \"b\":\n                output.push(struct[offset++]);\n                break;\n            case \"s\":\n                var len = struct[offset++];\n                len += struct[offset++] << 8;\n\n                var stringBytes = struct.slice(offset, offset + len);\n                offset += len;\n                output.push(textde.decode(stringBytes));\n                break;\n            case \"Q\":\n                state.offset = offset;\n                const qid = Unmarshall([\"b\", \"w\", \"d\"], struct, state);\n                offset = state.offset;\n                output.push({\n                    type: qid[0],\n                    version: qid[1],\n                    path: qid[2],\n                });\n                break;\n            default:\n                dbg_log(\"Error in Unmarshall: Unknown type=\" + typelist[i]);\n                break;\n        }\n    }\n    state.offset = offset;\n    return output;\n}\n"
  },
  {
    "path": "lib/softfloat/softfloat.c",
    "content": "/**** start inlining ../../source/8086-SSE/softfloat_raiseFlags.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n/**** start inlining platform.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define LITTLEENDIAN 1\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#ifdef __GNUC_STDC_INLINE__\r\n#define INLINE inline\r\n#else\r\n#define INLINE extern inline\r\n#endif\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define SOFTFLOAT_BUILTIN_CLZ 1\r\n#define SOFTFLOAT_INTRINSIC_INT128 1\r\n/**** start inlining opts-GCC.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2017 The Regents of the University of California.  All rights\r\nreserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#ifndef opts_GCC_h\r\n#define opts_GCC_h 1\r\n\r\n#ifdef INLINE\r\n\r\n#include <stdint.h>\r\n/**** start inlining primitiveTypes.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#ifndef primitiveTypes_h\r\n#define primitiveTypes_h 1\r\n\r\n#include <stdint.h>\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\n#ifdef LITTLEENDIAN\r\nstruct uint128 { uint64_t v0, v64; };\r\nstruct uint64_extra { uint64_t extra, v; };\r\nstruct uint128_extra { uint64_t extra; struct uint128 v; };\r\n#else\r\nstruct uint128 { uint64_t v64, v0; };\r\nstruct uint64_extra { uint64_t v, extra; };\r\nstruct uint128_extra { struct uint128 v; uint64_t extra; };\r\n#endif\r\n\r\n#endif\r\n\r\n/*----------------------------------------------------------------------------\r\n| These macros are used to isolate the differences in word order between big-\r\n| endian and little-endian platforms.\r\n*----------------------------------------------------------------------------*/\r\n#ifdef LITTLEENDIAN\r\n#define wordIncr 1\r\n#define indexWord( total, n ) (n)\r\n#define indexWordHi( total ) ((total) - 1)\r\n#define indexWordLo( total ) 0\r\n#define indexMultiword( total, m, n ) (n)\r\n#define indexMultiwordHi( total, n ) ((total) - (n))\r\n#define indexMultiwordLo( total, n ) 0\r\n#define indexMultiwordHiBut( total, n ) (n)\r\n#define indexMultiwordLoBut( total, n ) 0\r\n#define INIT_UINTM4( v3, v2, v1, v0 ) { v0, v1, v2, v3 }\r\n#else\r\n#define wordIncr -1\r\n#define indexWord( total, n ) ((total) - 1 - (n))\r\n#define indexWordHi( total ) 0\r\n#define indexWordLo( total ) ((total) - 1)\r\n#define indexMultiword( total, m, n ) ((total) - 1 - (m))\r\n#define indexMultiwordHi( total, n ) 0\r\n#define indexMultiwordLo( total, n ) ((total) - (n))\r\n#define indexMultiwordHiBut( total, n ) 0\r\n#define indexMultiwordLoBut( total, n ) (n)\r\n#define INIT_UINTM4( v3, v2, v1, v0 ) { v3, v2, v1, v0 }\r\n#endif\r\n\r\n#endif\r\n\r\n/**** ended inlining primitiveTypes.h ****/\n\r\n#ifdef SOFTFLOAT_BUILTIN_CLZ\r\n\r\nINLINE uint_fast8_t softfloat_countLeadingZeros16( uint16_t a )\r\n    { return a ? __builtin_clz( a ) - 16 : 16; }\r\n#define softfloat_countLeadingZeros16 softfloat_countLeadingZeros16\r\n\r\nINLINE uint_fast8_t softfloat_countLeadingZeros32( uint32_t a )\r\n    { return a ? __builtin_clz( a ) : 32; }\r\n#define softfloat_countLeadingZeros32 softfloat_countLeadingZeros32\r\n\r\nINLINE uint_fast8_t softfloat_countLeadingZeros64( uint64_t a )\r\n    { return a ? __builtin_clzll( a ) : 64; }\r\n#define softfloat_countLeadingZeros64 softfloat_countLeadingZeros64\r\n\r\n#endif\r\n\r\n#ifdef SOFTFLOAT_INTRINSIC_INT128\r\n\r\nINLINE struct uint128 softfloat_mul64ByShifted32To128( uint64_t a, uint32_t b )\r\n{\r\n    union { unsigned __int128 ui; struct uint128 s; } uZ;\r\n    uZ.ui = (unsigned __int128) a * ((uint_fast64_t) b<<32);\r\n    return uZ.s;\r\n}\r\n#define softfloat_mul64ByShifted32To128 softfloat_mul64ByShifted32To128\r\n\r\nINLINE struct uint128 softfloat_mul64To128( uint64_t a, uint64_t b )\r\n{\r\n    union { unsigned __int128 ui; struct uint128 s; } uZ;\r\n    uZ.ui = (unsigned __int128) a * b;\r\n    return uZ.s;\r\n}\r\n#define softfloat_mul64To128 softfloat_mul64To128\r\n\r\nINLINE\r\nstruct uint128 softfloat_mul128By32( uint64_t a64, uint64_t a0, uint32_t b )\r\n{\r\n    union { unsigned __int128 ui; struct uint128 s; } uZ;\r\n    uZ.ui = ((unsigned __int128) a64<<64 | a0) * b;\r\n    return uZ.s;\r\n}\r\n#define softfloat_mul128By32 softfloat_mul128By32\r\n\r\nINLINE\r\nvoid\r\n softfloat_mul128To256M(\r\n     uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0, uint64_t *zPtr )\r\n{\r\n    unsigned __int128 z0, mid1, mid, z128;\r\n    z0 = (unsigned __int128) a0 * b0;\r\n    mid1 = (unsigned __int128) a64 * b0;\r\n    mid = mid1 + (unsigned __int128) a0 * b64;\r\n    z128 = (unsigned __int128) a64 * b64;\r\n    z128 += (unsigned __int128) (mid < mid1)<<64 | mid>>64;\r\n    mid <<= 64;\r\n    z0 += mid;\r\n    z128 += (z0 < mid);\r\n    zPtr[indexWord( 4, 0 )] = z0;\r\n    zPtr[indexWord( 4, 1 )] = z0>>64;\r\n    zPtr[indexWord( 4, 2 )] = z128;\r\n    zPtr[indexWord( 4, 3 )] = z128>>64;\r\n}\r\n#define softfloat_mul128To256M softfloat_mul128To256M\r\n\r\n#endif\r\n\r\n#endif\r\n\r\n#endif\r\n\r\n/**** ended inlining opts-GCC.h ****/\n\r\n/**** ended inlining platform.h ****/\n/**** start inlining softfloat.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n\r\n/*============================================================================\r\n| Note:  If SoftFloat is made available as a general library for programs to\r\n| use, it is strongly recommended that a platform-specific version of this\r\n| header, \"softfloat.h\", be created that folds in \"softfloat_types.h\" and that\r\n| eliminates all dependencies on compile-time macros.\r\n*============================================================================*/\r\n\r\n\r\n#ifndef softfloat_h\r\n#define softfloat_h 1\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** start inlining softfloat_types.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#ifndef softfloat_types_h\r\n#define softfloat_types_h 1\r\n\r\n#include <stdint.h>\r\n\r\n/*----------------------------------------------------------------------------\r\n| Types used to pass 16-bit, 32-bit, 64-bit, and 128-bit floating-point\r\n| arguments and results to/from functions.  These types must be exactly\r\n| 16 bits, 32 bits, 64 bits, and 128 bits in size, respectively.  Where a\r\n| platform has \"native\" support for IEEE-Standard floating-point formats,\r\n| the types below may, if desired, be defined as aliases for the native types\r\n| (typically 'float' and 'double', and possibly 'long double').\r\n*----------------------------------------------------------------------------*/\r\ntypedef struct { uint16_t v; } float16_t;\r\ntypedef struct { uint32_t v; } float32_t;\r\ntypedef struct { uint64_t v; } float64_t;\r\ntypedef struct { uint64_t v[2]; } float128_t;\r\n\r\n/*----------------------------------------------------------------------------\r\n| The format of an 80-bit extended floating-point number in memory.  This\r\n| structure must contain a 16-bit field named 'signExp' and a 64-bit field\r\n| named 'signif'.\r\n*----------------------------------------------------------------------------*/\r\n#ifdef LITTLEENDIAN\r\nstruct extFloat80M { uint64_t signif; uint16_t signExp; };\r\n#else\r\nstruct extFloat80M { uint16_t signExp; uint64_t signif; };\r\n#endif\r\n\r\n/*----------------------------------------------------------------------------\r\n| The type used to pass 80-bit extended floating-point arguments and\r\n| results to/from functions.  This type must have size identical to\r\n| 'struct extFloat80M'.  Type 'extFloat80_t' can be defined as an alias for\r\n| 'struct extFloat80M'.  Alternatively, if a platform has \"native\" support\r\n| for IEEE-Standard 80-bit extended floating-point, it may be possible,\r\n| if desired, to define 'extFloat80_t' as an alias for the native type\r\n| (presumably either 'long double' or a nonstandard compiler-intrinsic type).\r\n| In that case, the 'signif' and 'signExp' fields of 'struct extFloat80M'\r\n| must align exactly with the locations in memory of the sign, exponent, and\r\n| significand of the native type.\r\n*----------------------------------------------------------------------------*/\r\ntypedef struct extFloat80M extFloat80_t;\r\n\r\n#endif\r\n\r\n/**** ended inlining softfloat_types.h ****/\n\r\n#ifndef THREAD_LOCAL\r\n#define THREAD_LOCAL\r\n#endif\r\n\r\n/*----------------------------------------------------------------------------\r\n| Software floating-point underflow tininess-detection mode.\r\n*----------------------------------------------------------------------------*/\r\nextern THREAD_LOCAL uint_fast8_t softfloat_detectTininess;\r\nenum {\r\n    softfloat_tininess_beforeRounding = 0,\r\n    softfloat_tininess_afterRounding  = 1\r\n};\r\n\r\n/*----------------------------------------------------------------------------\r\n| Software floating-point rounding mode.  (Mode \"odd\" is supported only if\r\n| SoftFloat is compiled with macro 'SOFTFLOAT_ROUND_ODD' defined.)\r\n*----------------------------------------------------------------------------*/\r\nextern THREAD_LOCAL uint_fast8_t softfloat_roundingMode;\r\nenum {\r\n    softfloat_round_near_even   = 0,\r\n    softfloat_round_minMag      = 1,\r\n    softfloat_round_min         = 2,\r\n    softfloat_round_max         = 3,\r\n    softfloat_round_near_maxMag = 4,\r\n    softfloat_round_odd         = 6\r\n};\r\n\r\n/*----------------------------------------------------------------------------\r\n| Software floating-point exception flags.\r\n*----------------------------------------------------------------------------*/\r\nextern THREAD_LOCAL uint_fast8_t softfloat_exceptionFlags;\r\nenum {\r\n    softfloat_flag_inexact   =  1,\r\n    softfloat_flag_underflow =  2,\r\n    softfloat_flag_overflow  =  4,\r\n    softfloat_flag_infinite  =  8,\r\n    softfloat_flag_invalid   = 16\r\n};\r\n\r\n/*----------------------------------------------------------------------------\r\n| Routine to raise any or all of the software floating-point exception flags.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_raiseFlags( uint_fast8_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Integer-to-floating-point conversion routines.\r\n*----------------------------------------------------------------------------*/\r\nfloat16_t ui32_to_f16( uint32_t );\r\nfloat32_t ui32_to_f32( uint32_t );\r\nfloat64_t ui32_to_f64( uint32_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t ui32_to_extF80( uint32_t );\r\nfloat128_t ui32_to_f128( uint32_t );\r\n#endif\r\nvoid ui32_to_extF80M( uint32_t, extFloat80_t * );\r\nvoid ui32_to_f128M( uint32_t, float128_t * );\r\nfloat16_t ui64_to_f16( uint64_t );\r\nfloat32_t ui64_to_f32( uint64_t );\r\nfloat64_t ui64_to_f64( uint64_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t ui64_to_extF80( uint64_t );\r\nfloat128_t ui64_to_f128( uint64_t );\r\n#endif\r\nvoid ui64_to_extF80M( uint64_t, extFloat80_t * );\r\nvoid ui64_to_f128M( uint64_t, float128_t * );\r\nfloat16_t i32_to_f16( int32_t );\r\nfloat32_t i32_to_f32( int32_t );\r\nfloat64_t i32_to_f64( int32_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t i32_to_extF80( int32_t );\r\nfloat128_t i32_to_f128( int32_t );\r\n#endif\r\nvoid i32_to_extF80M( int32_t, extFloat80_t * );\r\nvoid i32_to_f128M( int32_t, float128_t * );\r\nfloat16_t i64_to_f16( int64_t );\r\nfloat32_t i64_to_f32( int64_t );\r\nfloat64_t i64_to_f64( int64_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t i64_to_extF80( int64_t );\r\nfloat128_t i64_to_f128( int64_t );\r\n#endif\r\nvoid i64_to_extF80M( int64_t, extFloat80_t * );\r\nvoid i64_to_f128M( int64_t, float128_t * );\r\n\r\n/*----------------------------------------------------------------------------\r\n| 16-bit (half-precision) floating-point operations.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t f16_to_ui32( float16_t, uint_fast8_t, bool );\r\nuint_fast64_t f16_to_ui64( float16_t, uint_fast8_t, bool );\r\nint_fast32_t f16_to_i32( float16_t, uint_fast8_t, bool );\r\nint_fast64_t f16_to_i64( float16_t, uint_fast8_t, bool );\r\nuint_fast32_t f16_to_ui32_r_minMag( float16_t, bool );\r\nuint_fast64_t f16_to_ui64_r_minMag( float16_t, bool );\r\nint_fast32_t f16_to_i32_r_minMag( float16_t, bool );\r\nint_fast64_t f16_to_i64_r_minMag( float16_t, bool );\r\nfloat32_t f16_to_f32( float16_t );\r\nfloat64_t f16_to_f64( float16_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t f16_to_extF80( float16_t );\r\nfloat128_t f16_to_f128( float16_t );\r\n#endif\r\nvoid f16_to_extF80M( float16_t, extFloat80_t * );\r\nvoid f16_to_f128M( float16_t, float128_t * );\r\nfloat16_t f16_roundToInt( float16_t, uint_fast8_t, bool );\r\nfloat16_t f16_add( float16_t, float16_t );\r\nfloat16_t f16_sub( float16_t, float16_t );\r\nfloat16_t f16_mul( float16_t, float16_t );\r\nfloat16_t f16_mulAdd( float16_t, float16_t, float16_t );\r\nfloat16_t f16_div( float16_t, float16_t );\r\nfloat16_t f16_rem( float16_t, float16_t );\r\nfloat16_t f16_sqrt( float16_t );\r\nbool f16_eq( float16_t, float16_t );\r\nbool f16_le( float16_t, float16_t );\r\nbool f16_lt( float16_t, float16_t );\r\nbool f16_eq_signaling( float16_t, float16_t );\r\nbool f16_le_quiet( float16_t, float16_t );\r\nbool f16_lt_quiet( float16_t, float16_t );\r\nbool f16_isSignalingNaN( float16_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n| 32-bit (single-precision) floating-point operations.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t f32_to_ui32( float32_t, uint_fast8_t, bool );\r\nuint_fast64_t f32_to_ui64( float32_t, uint_fast8_t, bool );\r\nint_fast32_t f32_to_i32( float32_t, uint_fast8_t, bool );\r\nint_fast64_t f32_to_i64( float32_t, uint_fast8_t, bool );\r\nuint_fast32_t f32_to_ui32_r_minMag( float32_t, bool );\r\nuint_fast64_t f32_to_ui64_r_minMag( float32_t, bool );\r\nint_fast32_t f32_to_i32_r_minMag( float32_t, bool );\r\nint_fast64_t f32_to_i64_r_minMag( float32_t, bool );\r\nfloat16_t f32_to_f16( float32_t );\r\nfloat64_t f32_to_f64( float32_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t f32_to_extF80( float32_t );\r\nfloat128_t f32_to_f128( float32_t );\r\n#endif\r\nvoid f32_to_extF80M( float32_t, extFloat80_t * );\r\nvoid f32_to_f128M( float32_t, float128_t * );\r\nfloat32_t f32_roundToInt( float32_t, uint_fast8_t, bool );\r\nfloat32_t f32_add( float32_t, float32_t );\r\nfloat32_t f32_sub( float32_t, float32_t );\r\nfloat32_t f32_mul( float32_t, float32_t );\r\nfloat32_t f32_mulAdd( float32_t, float32_t, float32_t );\r\nfloat32_t f32_div( float32_t, float32_t );\r\nfloat32_t f32_rem( float32_t, float32_t );\r\nfloat32_t f32_sqrt( float32_t );\r\nbool f32_eq( float32_t, float32_t );\r\nbool f32_le( float32_t, float32_t );\r\nbool f32_lt( float32_t, float32_t );\r\nbool f32_eq_signaling( float32_t, float32_t );\r\nbool f32_le_quiet( float32_t, float32_t );\r\nbool f32_lt_quiet( float32_t, float32_t );\r\nbool f32_isSignalingNaN( float32_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n| 64-bit (double-precision) floating-point operations.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t f64_to_ui32( float64_t, uint_fast8_t, bool );\r\nuint_fast64_t f64_to_ui64( float64_t, uint_fast8_t, bool );\r\nint_fast32_t f64_to_i32( float64_t, uint_fast8_t, bool );\r\nint_fast64_t f64_to_i64( float64_t, uint_fast8_t, bool );\r\nuint_fast32_t f64_to_ui32_r_minMag( float64_t, bool );\r\nuint_fast64_t f64_to_ui64_r_minMag( float64_t, bool );\r\nint_fast32_t f64_to_i32_r_minMag( float64_t, bool );\r\nint_fast64_t f64_to_i64_r_minMag( float64_t, bool );\r\nfloat16_t f64_to_f16( float64_t );\r\nfloat32_t f64_to_f32( float64_t );\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nextFloat80_t f64_to_extF80( float64_t );\r\nfloat128_t f64_to_f128( float64_t );\r\n#endif\r\nvoid f64_to_extF80M( float64_t, extFloat80_t * );\r\nvoid f64_to_f128M( float64_t, float128_t * );\r\nfloat64_t f64_roundToInt( float64_t, uint_fast8_t, bool );\r\nfloat64_t f64_add( float64_t, float64_t );\r\nfloat64_t f64_sub( float64_t, float64_t );\r\nfloat64_t f64_mul( float64_t, float64_t );\r\nfloat64_t f64_mulAdd( float64_t, float64_t, float64_t );\r\nfloat64_t f64_div( float64_t, float64_t );\r\nfloat64_t f64_rem( float64_t, float64_t );\r\nfloat64_t f64_sqrt( float64_t );\r\nbool f64_eq( float64_t, float64_t );\r\nbool f64_le( float64_t, float64_t );\r\nbool f64_lt( float64_t, float64_t );\r\nbool f64_eq_signaling( float64_t, float64_t );\r\nbool f64_le_quiet( float64_t, float64_t );\r\nbool f64_lt_quiet( float64_t, float64_t );\r\nbool f64_isSignalingNaN( float64_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Rounding precision for 80-bit extended double-precision floating-point.\r\n| Valid values are 32, 64, and 80.\r\n*----------------------------------------------------------------------------*/\r\nextern THREAD_LOCAL uint_fast8_t extF80_roundingPrecision;\r\n\r\n/*----------------------------------------------------------------------------\r\n| 80-bit extended double-precision floating-point operations.\r\n*----------------------------------------------------------------------------*/\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nuint_fast32_t extF80_to_ui32( extFloat80_t, uint_fast8_t, bool );\r\nuint_fast64_t extF80_to_ui64( extFloat80_t, uint_fast8_t, bool );\r\nint_fast32_t extF80_to_i32( extFloat80_t, uint_fast8_t, bool );\r\nint_fast64_t extF80_to_i64( extFloat80_t, uint_fast8_t, bool );\r\nuint_fast32_t extF80_to_ui32_r_minMag( extFloat80_t, bool );\r\nuint_fast64_t extF80_to_ui64_r_minMag( extFloat80_t, bool );\r\nint_fast32_t extF80_to_i32_r_minMag( extFloat80_t, bool );\r\nint_fast64_t extF80_to_i64_r_minMag( extFloat80_t, bool );\r\nfloat16_t extF80_to_f16( extFloat80_t );\r\nfloat32_t extF80_to_f32( extFloat80_t );\r\nfloat64_t extF80_to_f64( extFloat80_t );\r\nfloat128_t extF80_to_f128( extFloat80_t );\r\nextFloat80_t extF80_roundToInt( extFloat80_t, uint_fast8_t, bool );\r\nextFloat80_t extF80_add( extFloat80_t, extFloat80_t );\r\nextFloat80_t extF80_sub( extFloat80_t, extFloat80_t );\r\nextFloat80_t extF80_mul( extFloat80_t, extFloat80_t );\r\nextFloat80_t extF80_div( extFloat80_t, extFloat80_t );\r\nextFloat80_t extF80_rem( extFloat80_t, extFloat80_t );\r\nextFloat80_t extF80_sqrt( extFloat80_t );\r\nbool extF80_eq( extFloat80_t, extFloat80_t );\r\nbool extF80_le( extFloat80_t, extFloat80_t );\r\nbool extF80_lt( extFloat80_t, extFloat80_t );\r\nbool extF80_eq_signaling( extFloat80_t, extFloat80_t );\r\nbool extF80_le_quiet( extFloat80_t, extFloat80_t );\r\nbool extF80_lt_quiet( extFloat80_t, extFloat80_t );\r\nbool extF80_isSignalingNaN( extFloat80_t );\r\n#endif\r\nuint_fast32_t extF80M_to_ui32( const extFloat80_t *, uint_fast8_t, bool );\r\nuint_fast64_t extF80M_to_ui64( const extFloat80_t *, uint_fast8_t, bool );\r\nint_fast32_t extF80M_to_i32( const extFloat80_t *, uint_fast8_t, bool );\r\nint_fast64_t extF80M_to_i64( const extFloat80_t *, uint_fast8_t, bool );\r\nuint_fast32_t extF80M_to_ui32_r_minMag( const extFloat80_t *, bool );\r\nuint_fast64_t extF80M_to_ui64_r_minMag( const extFloat80_t *, bool );\r\nint_fast32_t extF80M_to_i32_r_minMag( const extFloat80_t *, bool );\r\nint_fast64_t extF80M_to_i64_r_minMag( const extFloat80_t *, bool );\r\nfloat16_t extF80M_to_f16( const extFloat80_t * );\r\nfloat32_t extF80M_to_f32( const extFloat80_t * );\r\nfloat64_t extF80M_to_f64( const extFloat80_t * );\r\nvoid extF80M_to_f128M( const extFloat80_t *, float128_t * );\r\nvoid\r\n extF80M_roundToInt(\r\n     const extFloat80_t *, uint_fast8_t, bool, extFloat80_t * );\r\nvoid extF80M_add( const extFloat80_t *, const extFloat80_t *, extFloat80_t * );\r\nvoid extF80M_sub( const extFloat80_t *, const extFloat80_t *, extFloat80_t * );\r\nvoid extF80M_mul( const extFloat80_t *, const extFloat80_t *, extFloat80_t * );\r\nvoid extF80M_div( const extFloat80_t *, const extFloat80_t *, extFloat80_t * );\r\nvoid extF80M_rem( const extFloat80_t *, const extFloat80_t *, extFloat80_t * );\r\nvoid extF80M_sqrt( const extFloat80_t *, extFloat80_t * );\r\nbool extF80M_eq( const extFloat80_t *, const extFloat80_t * );\r\nbool extF80M_le( const extFloat80_t *, const extFloat80_t * );\r\nbool extF80M_lt( const extFloat80_t *, const extFloat80_t * );\r\nbool extF80M_eq_signaling( const extFloat80_t *, const extFloat80_t * );\r\nbool extF80M_le_quiet( const extFloat80_t *, const extFloat80_t * );\r\nbool extF80M_lt_quiet( const extFloat80_t *, const extFloat80_t * );\r\nbool extF80M_isSignalingNaN( const extFloat80_t * );\r\n\r\n/*----------------------------------------------------------------------------\r\n| 128-bit (quadruple-precision) floating-point operations.\r\n*----------------------------------------------------------------------------*/\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nuint_fast32_t f128_to_ui32( float128_t, uint_fast8_t, bool );\r\nuint_fast64_t f128_to_ui64( float128_t, uint_fast8_t, bool );\r\nint_fast32_t f128_to_i32( float128_t, uint_fast8_t, bool );\r\nint_fast64_t f128_to_i64( float128_t, uint_fast8_t, bool );\r\nuint_fast32_t f128_to_ui32_r_minMag( float128_t, bool );\r\nuint_fast64_t f128_to_ui64_r_minMag( float128_t, bool );\r\nint_fast32_t f128_to_i32_r_minMag( float128_t, bool );\r\nint_fast64_t f128_to_i64_r_minMag( float128_t, bool );\r\nfloat16_t f128_to_f16( float128_t );\r\nfloat32_t f128_to_f32( float128_t );\r\nfloat64_t f128_to_f64( float128_t );\r\nextFloat80_t f128_to_extF80( float128_t );\r\nfloat128_t f128_roundToInt( float128_t, uint_fast8_t, bool );\r\nfloat128_t f128_add( float128_t, float128_t );\r\nfloat128_t f128_sub( float128_t, float128_t );\r\nfloat128_t f128_mul( float128_t, float128_t );\r\nfloat128_t f128_mulAdd( float128_t, float128_t, float128_t );\r\nfloat128_t f128_div( float128_t, float128_t );\r\nfloat128_t f128_rem( float128_t, float128_t );\r\nfloat128_t f128_sqrt( float128_t );\r\nbool f128_eq( float128_t, float128_t );\r\nbool f128_le( float128_t, float128_t );\r\nbool f128_lt( float128_t, float128_t );\r\nbool f128_eq_signaling( float128_t, float128_t );\r\nbool f128_le_quiet( float128_t, float128_t );\r\nbool f128_lt_quiet( float128_t, float128_t );\r\nbool f128_isSignalingNaN( float128_t );\r\n#endif\r\nuint_fast32_t f128M_to_ui32( const float128_t *, uint_fast8_t, bool );\r\nuint_fast64_t f128M_to_ui64( const float128_t *, uint_fast8_t, bool );\r\nint_fast32_t f128M_to_i32( const float128_t *, uint_fast8_t, bool );\r\nint_fast64_t f128M_to_i64( const float128_t *, uint_fast8_t, bool );\r\nuint_fast32_t f128M_to_ui32_r_minMag( const float128_t *, bool );\r\nuint_fast64_t f128M_to_ui64_r_minMag( const float128_t *, bool );\r\nint_fast32_t f128M_to_i32_r_minMag( const float128_t *, bool );\r\nint_fast64_t f128M_to_i64_r_minMag( const float128_t *, bool );\r\nfloat16_t f128M_to_f16( const float128_t * );\r\nfloat32_t f128M_to_f32( const float128_t * );\r\nfloat64_t f128M_to_f64( const float128_t * );\r\nvoid f128M_to_extF80M( const float128_t *, extFloat80_t * );\r\nvoid f128M_roundToInt( const float128_t *, uint_fast8_t, bool, float128_t * );\r\nvoid f128M_add( const float128_t *, const float128_t *, float128_t * );\r\nvoid f128M_sub( const float128_t *, const float128_t *, float128_t * );\r\nvoid f128M_mul( const float128_t *, const float128_t *, float128_t * );\r\nvoid\r\n f128M_mulAdd(\r\n     const float128_t *, const float128_t *, const float128_t *, float128_t *\r\n );\r\nvoid f128M_div( const float128_t *, const float128_t *, float128_t * );\r\nvoid f128M_rem( const float128_t *, const float128_t *, float128_t * );\r\nvoid f128M_sqrt( const float128_t *, float128_t * );\r\nbool f128M_eq( const float128_t *, const float128_t * );\r\nbool f128M_le( const float128_t *, const float128_t * );\r\nbool f128M_lt( const float128_t *, const float128_t * );\r\nbool f128M_eq_signaling( const float128_t *, const float128_t * );\r\nbool f128M_le_quiet( const float128_t *, const float128_t * );\r\nbool f128M_lt_quiet( const float128_t *, const float128_t * );\r\nbool f128M_isSignalingNaN( const float128_t * );\r\n\r\n#endif\r\n\r\n/**** ended inlining softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Raises the exceptions specified by `flags'.  Floating-point traps can be\r\n| defined here if desired.  It is currently not possible for such a trap\r\n| to substitute a result value.  If traps are not implemented, this routine\r\n| should be simply `softfloat_exceptionFlags |= flags;'.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_raiseFlags( uint_fast8_t flags )\r\n{\r\n\r\n    softfloat_exceptionFlags |= flags;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/softfloat_raiseFlags.c ****/\n/**** start inlining ../../source/8086-SSE/s_f16UIToCommonNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** start inlining specialize.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2018 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#ifndef specialize_h\r\n#define specialize_h 1\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: primitiveTypes.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Default value for 'softfloat_detectTininess'.\r\n*----------------------------------------------------------------------------*/\r\n#define init_detectTininess softfloat_tininess_afterRounding\r\n\r\n/*----------------------------------------------------------------------------\r\n| The values to return on conversions to 32-bit integer formats that raise an\r\n| invalid exception.\r\n*----------------------------------------------------------------------------*/\r\n#define ui32_fromPosOverflow 0xFFFFFFFF\r\n#define ui32_fromNegOverflow 0xFFFFFFFF\r\n#define ui32_fromNaN         0xFFFFFFFF\r\n#define i32_fromPosOverflow  (-0x7FFFFFFF - 1)\r\n#define i32_fromNegOverflow  (-0x7FFFFFFF - 1)\r\n#define i32_fromNaN          (-0x7FFFFFFF - 1)\r\n\r\n/*----------------------------------------------------------------------------\r\n| The values to return on conversions to 64-bit integer formats that raise an\r\n| invalid exception.\r\n*----------------------------------------------------------------------------*/\r\n#define ui64_fromPosOverflow UINT64_C( 0xFFFFFFFFFFFFFFFF )\r\n#define ui64_fromNegOverflow UINT64_C( 0xFFFFFFFFFFFFFFFF )\r\n#define ui64_fromNaN         UINT64_C( 0xFFFFFFFFFFFFFFFF )\r\n#define i64_fromPosOverflow  (-INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1)\r\n#define i64_fromNegOverflow  (-INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1)\r\n#define i64_fromNaN          (-INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1)\r\n\r\n/*----------------------------------------------------------------------------\r\n| \"Common NaN\" structure, used to transfer NaN representations from one format\r\n| to another.\r\n*----------------------------------------------------------------------------*/\r\nstruct commonNaN {\r\n    bool sign;\r\n#ifdef LITTLEENDIAN\r\n    uint64_t v0, v64;\r\n#else\r\n    uint64_t v64, v0;\r\n#endif\r\n};\r\n\r\n/*----------------------------------------------------------------------------\r\n| The bit pattern for a default generated 16-bit floating-point NaN.\r\n*----------------------------------------------------------------------------*/\r\n#define defaultNaNF16UI 0xFE00\r\n\r\n/*----------------------------------------------------------------------------\r\n| Returns true when 16-bit unsigned integer 'uiA' has the bit pattern of a\r\n| 16-bit floating-point signaling NaN.\r\n| Note:  This macro evaluates its argument more than once.\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_isSigNaNF16UI( uiA ) ((((uiA) & 0x7E00) == 0x7C00) && ((uiA) & 0x01FF))\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming 'uiA' has the bit pattern of a 16-bit floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by 'zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_f16UIToCommonNaN( uint_fast16_t uiA, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into a 16-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast16_t softfloat_commonNaNToF16UI( const struct commonNaN *aPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting 'uiA' and 'uiB' as the bit patterns of two 16-bit floating-\r\n| point values, at least one of which is a NaN, returns the bit pattern of\r\n| the combined NaN result.  If either 'uiA' or 'uiB' has the pattern of a\r\n| signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast16_t\r\n softfloat_propagateNaNF16UI( uint_fast16_t uiA, uint_fast16_t uiB );\r\n\r\n/*----------------------------------------------------------------------------\r\n| The bit pattern for a default generated 32-bit floating-point NaN.\r\n*----------------------------------------------------------------------------*/\r\n#define defaultNaNF32UI 0xFFC00000\r\n\r\n/*----------------------------------------------------------------------------\r\n| Returns true when 32-bit unsigned integer 'uiA' has the bit pattern of a\r\n| 32-bit floating-point signaling NaN.\r\n| Note:  This macro evaluates its argument more than once.\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_isSigNaNF32UI( uiA ) ((((uiA) & 0x7FC00000) == 0x7F800000) && ((uiA) & 0x003FFFFF))\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming 'uiA' has the bit pattern of a 32-bit floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by 'zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_f32UIToCommonNaN( uint_fast32_t uiA, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into a 32-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t softfloat_commonNaNToF32UI( const struct commonNaN *aPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting 'uiA' and 'uiB' as the bit patterns of two 32-bit floating-\r\n| point values, at least one of which is a NaN, returns the bit pattern of\r\n| the combined NaN result.  If either 'uiA' or 'uiB' has the pattern of a\r\n| signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t\r\n softfloat_propagateNaNF32UI( uint_fast32_t uiA, uint_fast32_t uiB );\r\n\r\n/*----------------------------------------------------------------------------\r\n| The bit pattern for a default generated 64-bit floating-point NaN.\r\n*----------------------------------------------------------------------------*/\r\n#define defaultNaNF64UI UINT64_C( 0xFFF8000000000000 )\r\n\r\n/*----------------------------------------------------------------------------\r\n| Returns true when 64-bit unsigned integer 'uiA' has the bit pattern of a\r\n| 64-bit floating-point signaling NaN.\r\n| Note:  This macro evaluates its argument more than once.\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_isSigNaNF64UI( uiA ) ((((uiA) & UINT64_C( 0x7FF8000000000000 )) == UINT64_C( 0x7FF0000000000000 )) && ((uiA) & UINT64_C( 0x0007FFFFFFFFFFFF )))\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming 'uiA' has the bit pattern of a 64-bit floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by 'zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_f64UIToCommonNaN( uint_fast64_t uiA, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into a 64-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast64_t softfloat_commonNaNToF64UI( const struct commonNaN *aPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting 'uiA' and 'uiB' as the bit patterns of two 64-bit floating-\r\n| point values, at least one of which is a NaN, returns the bit pattern of\r\n| the combined NaN result.  If either 'uiA' or 'uiB' has the pattern of a\r\n| signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast64_t\r\n softfloat_propagateNaNF64UI( uint_fast64_t uiA, uint_fast64_t uiB );\r\n\r\n/*----------------------------------------------------------------------------\r\n| The bit pattern for a default generated 80-bit extended floating-point NaN.\r\n*----------------------------------------------------------------------------*/\r\n#define defaultNaNExtF80UI64 0xFFFF\r\n#define defaultNaNExtF80UI0  UINT64_C( 0xC000000000000000 )\r\n\r\n/*----------------------------------------------------------------------------\r\n| Returns true when the 80-bit unsigned integer formed from concatenating\r\n| 16-bit 'uiA64' and 64-bit 'uiA0' has the bit pattern of an 80-bit extended\r\n| floating-point signaling NaN.\r\n| Note:  This macro evaluates its arguments more than once.\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_isSigNaNExtF80UI( uiA64, uiA0 ) ((((uiA64) & 0x7FFF) == 0x7FFF) && ! ((uiA0) & UINT64_C( 0x4000000000000000 )) && ((uiA0) & UINT64_C( 0x3FFFFFFFFFFFFFFF )))\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\n/*----------------------------------------------------------------------------\r\n| The following functions are needed only when 'SOFTFLOAT_FAST_INT64' is\r\n| defined.\r\n*----------------------------------------------------------------------------*/\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming the unsigned integer formed from concatenating 'uiA64' and 'uiA0'\r\n| has the bit pattern of an 80-bit extended floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by 'zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_extF80UIToCommonNaN(\r\n     uint_fast16_t uiA64, uint_fast64_t uiA0, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into an 80-bit extended\r\n| floating-point NaN, and returns the bit pattern of this value as an unsigned\r\n| integer.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128 softfloat_commonNaNToExtF80UI( const struct commonNaN *aPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting the unsigned integer formed from concatenating 'uiA64' and\r\n| 'uiA0' as an 80-bit extended floating-point value, and likewise interpreting\r\n| the unsigned integer formed from concatenating 'uiB64' and 'uiB0' as another\r\n| 80-bit extended floating-point value, and assuming at least on of these\r\n| floating-point values is a NaN, returns the bit pattern of the combined NaN\r\n| result.  If either original floating-point value is a signaling NaN, the\r\n| invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128\r\n softfloat_propagateNaNExtF80UI(\r\n     uint_fast16_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast16_t uiB64,\r\n     uint_fast64_t uiB0\r\n );\r\n\r\n/*----------------------------------------------------------------------------\r\n| The bit pattern for a default generated 128-bit floating-point NaN.\r\n*----------------------------------------------------------------------------*/\r\n#define defaultNaNF128UI64 UINT64_C( 0xFFFF800000000000 )\r\n#define defaultNaNF128UI0  UINT64_C( 0 )\r\n\r\n/*----------------------------------------------------------------------------\r\n| Returns true when the 128-bit unsigned integer formed from concatenating\r\n| 64-bit 'uiA64' and 64-bit 'uiA0' has the bit pattern of a 128-bit floating-\r\n| point signaling NaN.\r\n| Note:  This macro evaluates its arguments more than once.\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_isSigNaNF128UI( uiA64, uiA0 ) ((((uiA64) & UINT64_C( 0x7FFF800000000000 )) == UINT64_C( 0x7FFF000000000000 )) && ((uiA0) || ((uiA64) & UINT64_C( 0x00007FFFFFFFFFFF ))))\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming the unsigned integer formed from concatenating 'uiA64' and 'uiA0'\r\n| has the bit pattern of a 128-bit floating-point NaN, converts this NaN to\r\n| the common NaN form, and stores the resulting common NaN at the location\r\n| pointed to by 'zPtr'.  If the NaN is a signaling NaN, the invalid exception\r\n| is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_f128UIToCommonNaN(\r\n     uint_fast64_t uiA64, uint_fast64_t uiA0, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into a 128-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128 softfloat_commonNaNToF128UI( const struct commonNaN * );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting the unsigned integer formed from concatenating 'uiA64' and\r\n| 'uiA0' as a 128-bit floating-point value, and likewise interpreting the\r\n| unsigned integer formed from concatenating 'uiB64' and 'uiB0' as another\r\n| 128-bit floating-point value, and assuming at least on of these floating-\r\n| point values is a NaN, returns the bit pattern of the combined NaN result.\r\n| If either original floating-point value is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128\r\n softfloat_propagateNaNF128UI(\r\n     uint_fast64_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast64_t uiB64,\r\n     uint_fast64_t uiB0\r\n );\r\n\r\n#else\r\n\r\n/*----------------------------------------------------------------------------\r\n| The following functions are needed only when 'SOFTFLOAT_FAST_INT64' is not\r\n| defined.\r\n*----------------------------------------------------------------------------*/\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming the 80-bit extended floating-point value pointed to by 'aSPtr' is\r\n| a NaN, converts this NaN to the common NaN form, and stores the resulting\r\n| common NaN at the location pointed to by 'zPtr'.  If the NaN is a signaling\r\n| NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_extF80MToCommonNaN(\r\n     const struct extFloat80M *aSPtr, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into an 80-bit extended\r\n| floating-point NaN, and stores this NaN at the location pointed to by\r\n| 'zSPtr'.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_commonNaNToExtF80M(\r\n     const struct commonNaN *aPtr, struct extFloat80M *zSPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming at least one of the two 80-bit extended floating-point values\r\n| pointed to by 'aSPtr' and 'bSPtr' is a NaN, stores the combined NaN result\r\n| at the location pointed to by 'zSPtr'.  If either original floating-point\r\n| value is a signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_propagateNaNExtF80M(\r\n     const struct extFloat80M *aSPtr,\r\n     const struct extFloat80M *bSPtr,\r\n     struct extFloat80M *zSPtr\r\n );\r\n\r\n/*----------------------------------------------------------------------------\r\n| The bit pattern for a default generated 128-bit floating-point NaN.\r\n*----------------------------------------------------------------------------*/\r\n#define defaultNaNF128UI96 0xFFFF8000\r\n#define defaultNaNF128UI64 0\r\n#define defaultNaNF128UI32 0\r\n#define defaultNaNF128UI0  0\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming the 128-bit floating-point value pointed to by 'aWPtr' is a NaN,\r\n| converts this NaN to the common NaN form, and stores the resulting common\r\n| NaN at the location pointed to by 'zPtr'.  If the NaN is a signaling NaN,\r\n| the invalid exception is raised.  Argument 'aWPtr' points to an array of\r\n| four 32-bit elements that concatenate in the platform's normal endian order\r\n| to form a 128-bit floating-point value.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_f128MToCommonNaN( const uint32_t *aWPtr, struct commonNaN *zPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by 'aPtr' into a 128-bit floating-point\r\n| NaN, and stores this NaN at the location pointed to by 'zWPtr'.  Argument\r\n| 'zWPtr' points to an array of four 32-bit elements that concatenate in the\r\n| platform's normal endian order to form a 128-bit floating-point value.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_commonNaNToF128M( const struct commonNaN *aPtr, uint32_t *zWPtr );\r\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming at least one of the two 128-bit floating-point values pointed to by\r\n| 'aWPtr' and 'bWPtr' is a NaN, stores the combined NaN result at the location\r\n| pointed to by 'zWPtr'.  If either original floating-point value is a\r\n| signaling NaN, the invalid exception is raised.  Each of 'aWPtr', 'bWPtr',\r\n| and 'zWPtr' points to an array of four 32-bit elements that concatenate in\r\n| the platform's normal endian order to form a 128-bit floating-point value.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_propagateNaNF128M(\r\n     const uint32_t *aWPtr, const uint32_t *bWPtr, uint32_t *zWPtr );\r\n\r\n#endif\r\n\r\n#endif\r\n\r\n/**** ended inlining specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming `uiA' has the bit pattern of a 16-bit floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_f16UIToCommonNaN( uint_fast16_t uiA, struct commonNaN *zPtr )\r\n{\r\n\r\n    if ( softfloat_isSigNaNF16UI( uiA ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n    }\r\n    zPtr->sign = uiA>>15;\r\n    zPtr->v64  = (uint_fast64_t) uiA<<54;\r\n    zPtr->v0   = 0;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_f16UIToCommonNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_commonNaNToF16UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: specialize.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by `aPtr' into a 16-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast16_t softfloat_commonNaNToF16UI( const struct commonNaN *aPtr )\r\n{\r\n\r\n    return (uint_fast16_t) aPtr->sign<<15 | 0x7E00 | aPtr->v64>>54;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_commonNaNToF16UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_propagateNaNF16UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** start inlining internals.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#ifndef internals_h\r\n#define internals_h 1\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** start inlining primitives.h ****/\n\r\n/*============================================================================\r\n\r\nThis C header file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#ifndef primitives_h\r\n#define primitives_h 1\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shortShiftRightJam64\r\n#define softfloat_shortShiftRightJam64 softfloat_shortShiftRightJam64\r\n/*----------------------------------------------------------------------------\r\n| Shifts 'a' right by the number of bits given in 'dist', which must be in\r\n| the range 1 to 63.  If any nonzero bits are shifted off, they are \"jammed\"\r\n| into the least-significant bit of the shifted value by setting the least-\r\n| significant bit to 1.  This shifted-and-jammed value is returned.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nuint64_t softfloat_shortShiftRightJam64( uint64_t a, uint_fast8_t dist )\r\n    { return a>>dist | ((a & (((uint_fast64_t) 1<<dist) - 1)) != 0); }\r\n#else\r\nuint64_t softfloat_shortShiftRightJam64( uint64_t a, uint_fast8_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam32\r\n#define softfloat_shiftRightJam32 softfloat_shiftRightJam32\r\n/*----------------------------------------------------------------------------\r\n| Shifts 'a' right by the number of bits given in 'dist', which must not\r\n| be zero.  If any nonzero bits are shifted off, they are \"jammed\" into the\r\n| least-significant bit of the shifted value by setting the least-significant\r\n| bit to 1.  This shifted-and-jammed value is returned.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist' is\r\n| greater than 32, the result will be either 0 or 1, depending on whether 'a'\r\n| is zero or nonzero.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE uint32_t softfloat_shiftRightJam32( uint32_t a, uint_fast16_t dist )\r\n{\r\n    return\r\n        (dist < 31) ? a>>dist | ((uint32_t) (a<<(-dist & 31)) != 0) : (a != 0);\r\n}\r\n#else\r\nuint32_t softfloat_shiftRightJam32( uint32_t a, uint_fast16_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam64\r\n#define softfloat_shiftRightJam64 softfloat_shiftRightJam64\r\n/*----------------------------------------------------------------------------\r\n| Shifts 'a' right by the number of bits given in 'dist', which must not\r\n| be zero.  If any nonzero bits are shifted off, they are \"jammed\" into the\r\n| least-significant bit of the shifted value by setting the least-significant\r\n| bit to 1.  This shifted-and-jammed value is returned.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist' is\r\n| greater than 64, the result will be either 0 or 1, depending on whether 'a'\r\n| is zero or nonzero.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (3 <= INLINE_LEVEL)\r\nINLINE uint64_t softfloat_shiftRightJam64( uint64_t a, uint_fast32_t dist )\r\n{\r\n    return\r\n        (dist < 63) ? a>>dist | ((uint64_t) (a<<(-dist & 63)) != 0) : (a != 0);\r\n}\r\n#else\r\nuint64_t softfloat_shiftRightJam64( uint64_t a, uint_fast32_t dist );\r\n#endif\r\n#endif\r\n\r\n/*----------------------------------------------------------------------------\r\n| A constant table that translates an 8-bit unsigned integer (the array index)\r\n| into the number of leading 0 bits before the most-significant 1 of that\r\n| integer.  For integer zero (index 0), the corresponding table element is 8.\r\n*----------------------------------------------------------------------------*/\r\nextern const uint_least8_t softfloat_countLeadingZeros8[256];\r\n\r\n#ifndef softfloat_countLeadingZeros16\r\n#define softfloat_countLeadingZeros16 softfloat_countLeadingZeros16\r\n/*----------------------------------------------------------------------------\r\n| Returns the number of leading 0 bits before the most-significant 1 bit of\r\n| 'a'.  If 'a' is zero, 16 is returned.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE uint_fast8_t softfloat_countLeadingZeros16( uint16_t a )\r\n{\r\n    uint_fast8_t count = 8;\r\n    if ( 0x100 <= a ) {\r\n        count = 0;\r\n        a >>= 8;\r\n    }\r\n    count += softfloat_countLeadingZeros8[a];\r\n    return count;\r\n}\r\n#else\r\nuint_fast8_t softfloat_countLeadingZeros16( uint16_t a );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_countLeadingZeros32\r\n#define softfloat_countLeadingZeros32 softfloat_countLeadingZeros32\r\n/*----------------------------------------------------------------------------\r\n| Returns the number of leading 0 bits before the most-significant 1 bit of\r\n| 'a'.  If 'a' is zero, 32 is returned.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (3 <= INLINE_LEVEL)\r\nINLINE uint_fast8_t softfloat_countLeadingZeros32( uint32_t a )\r\n{\r\n    uint_fast8_t count = 0;\r\n    if ( a < 0x10000 ) {\r\n        count = 16;\r\n        a <<= 16;\r\n    }\r\n    if ( a < 0x1000000 ) {\r\n        count += 8;\r\n        a <<= 8;\r\n    }\r\n    count += softfloat_countLeadingZeros8[a>>24];\r\n    return count;\r\n}\r\n#else\r\nuint_fast8_t softfloat_countLeadingZeros32( uint32_t a );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_countLeadingZeros64\r\n#define softfloat_countLeadingZeros64 softfloat_countLeadingZeros64\r\n/*----------------------------------------------------------------------------\r\n| Returns the number of leading 0 bits before the most-significant 1 bit of\r\n| 'a'.  If 'a' is zero, 64 is returned.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast8_t softfloat_countLeadingZeros64( uint64_t a );\r\n#endif\r\n\r\nextern const uint16_t softfloat_approxRecip_1k0s[16];\r\nextern const uint16_t softfloat_approxRecip_1k1s[16];\r\n\r\n#ifndef softfloat_approxRecip32_1\r\n/*----------------------------------------------------------------------------\r\n| Returns an approximation to the reciprocal of the number represented by 'a',\r\n| where 'a' is interpreted as an unsigned fixed-point number with one integer\r\n| bit and 31 fraction bits.  The 'a' input must be \"normalized\", meaning that\r\n| its most-significant bit (bit 31) must be 1.  Thus, if A is the value of\r\n| the fixed-point interpretation of 'a', then 1 <= A < 2.  The returned value\r\n| is interpreted as a pure unsigned fraction, having no integer bits and 32\r\n| fraction bits.  The approximation returned is never greater than the true\r\n| reciprocal 1/A, and it differs from the true reciprocal by at most 2.006 ulp\r\n| (units in the last place).\r\n*----------------------------------------------------------------------------*/\r\n#ifdef SOFTFLOAT_FAST_DIV64TO32\r\n#define softfloat_approxRecip32_1( a ) ((uint32_t) (UINT64_C( 0x7FFFFFFFFFFFFFFF ) / (uint32_t) (a)))\r\n#else\r\nuint32_t softfloat_approxRecip32_1( uint32_t a );\r\n#endif\r\n#endif\r\n\r\nextern const uint16_t softfloat_approxRecipSqrt_1k0s[16];\r\nextern const uint16_t softfloat_approxRecipSqrt_1k1s[16];\r\n\r\n#ifndef softfloat_approxRecipSqrt32_1\r\n/*----------------------------------------------------------------------------\r\n| Returns an approximation to the reciprocal of the square root of the number\r\n| represented by 'a', where 'a' is interpreted as an unsigned fixed-point\r\n| number either with one integer bit and 31 fraction bits or with two integer\r\n| bits and 30 fraction bits.  The format of 'a' is determined by 'oddExpA',\r\n| which must be either 0 or 1.  If 'oddExpA' is 1, 'a' is interpreted as\r\n| having one integer bit, and if 'oddExpA' is 0, 'a' is interpreted as having\r\n| two integer bits.  The 'a' input must be \"normalized\", meaning that its\r\n| most-significant bit (bit 31) must be 1.  Thus, if A is the value of the\r\n| fixed-point interpretation of 'a', it follows that 1 <= A < 2 when 'oddExpA'\r\n| is 1, and 2 <= A < 4 when 'oddExpA' is 0.\r\n|   The returned value is interpreted as a pure unsigned fraction, having\r\n| no integer bits and 32 fraction bits.  The approximation returned is never\r\n| greater than the true reciprocal 1/sqrt(A), and it differs from the true\r\n| reciprocal by at most 2.06 ulp (units in the last place).  The approximation\r\n| returned is also always within the range 0.5 to 1; thus, the most-\r\n| significant bit of the result is always set.\r\n*----------------------------------------------------------------------------*/\r\nuint32_t softfloat_approxRecipSqrt32_1( unsigned int oddExpA, uint32_t a );\r\n#endif\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\n/*----------------------------------------------------------------------------\r\n| The following functions are needed only when 'SOFTFLOAT_FAST_INT64' is\r\n| defined.\r\n*----------------------------------------------------------------------------*/\r\n\r\n#ifndef softfloat_eq128\r\n#define softfloat_eq128 softfloat_eq128\r\n/*----------------------------------------------------------------------------\r\n| Returns true if the 128-bit unsigned integer formed by concatenating 'a64'\r\n| and 'a0' is equal to the 128-bit unsigned integer formed by concatenating\r\n| 'b64' and 'b0'.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (1 <= INLINE_LEVEL)\r\nINLINE\r\nbool softfloat_eq128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n    { return (a64 == b64) && (a0 == b0); }\r\n#else\r\nbool softfloat_eq128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_le128\r\n#define softfloat_le128 softfloat_le128\r\n/*----------------------------------------------------------------------------\r\n| Returns true if the 128-bit unsigned integer formed by concatenating 'a64'\r\n| and 'a0' is less than or equal to the 128-bit unsigned integer formed by\r\n| concatenating 'b64' and 'b0'.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nbool softfloat_le128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n    { return (a64 < b64) || ((a64 == b64) && (a0 <= b0)); }\r\n#else\r\nbool softfloat_le128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_lt128\r\n#define softfloat_lt128 softfloat_lt128\r\n/*----------------------------------------------------------------------------\r\n| Returns true if the 128-bit unsigned integer formed by concatenating 'a64'\r\n| and 'a0' is less than the 128-bit unsigned integer formed by concatenating\r\n| 'b64' and 'b0'.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nbool softfloat_lt128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n    { return (a64 < b64) || ((a64 == b64) && (a0 < b0)); }\r\n#else\r\nbool softfloat_lt128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftLeft128\r\n#define softfloat_shortShiftLeft128 softfloat_shortShiftLeft128\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 128 bits formed by concatenating 'a64' and 'a0' left by the\r\n| number of bits given in 'dist', which must be in the range 1 to 63.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128\r\n softfloat_shortShiftLeft128( uint64_t a64, uint64_t a0, uint_fast8_t dist )\r\n{\r\n    struct uint128 z;\r\n    z.v64 = a64<<dist | a0>>(-dist & 63);\r\n    z.v0 = a0<<dist;\r\n    return z;\r\n}\r\n#else\r\nstruct uint128\r\n softfloat_shortShiftLeft128( uint64_t a64, uint64_t a0, uint_fast8_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRight128\r\n#define softfloat_shortShiftRight128 softfloat_shortShiftRight128\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 128 bits formed by concatenating 'a64' and 'a0' right by the\r\n| number of bits given in 'dist', which must be in the range 1 to 63.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128\r\n softfloat_shortShiftRight128( uint64_t a64, uint64_t a0, uint_fast8_t dist )\r\n{\r\n    struct uint128 z;\r\n    z.v64 = a64>>dist;\r\n    z.v0 = a64<<(-dist & 63) | a0>>dist;\r\n    return z;\r\n}\r\n#else\r\nstruct uint128\r\n softfloat_shortShiftRight128( uint64_t a64, uint64_t a0, uint_fast8_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRightJam64Extra\r\n#define softfloat_shortShiftRightJam64Extra softfloat_shortShiftRightJam64Extra\r\n/*----------------------------------------------------------------------------\r\n| This function is the same as 'softfloat_shiftRightJam64Extra' (below),\r\n| except that 'dist' must be in the range 1 to 63.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint64_extra\r\n softfloat_shortShiftRightJam64Extra(\r\n     uint64_t a, uint64_t extra, uint_fast8_t dist )\r\n{\r\n    struct uint64_extra z;\r\n    z.v = a>>dist;\r\n    z.extra = a<<(-dist & 63) | (extra != 0);\r\n    return z;\r\n}\r\n#else\r\nstruct uint64_extra\r\n softfloat_shortShiftRightJam64Extra(\r\n     uint64_t a, uint64_t extra, uint_fast8_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRightJam128\r\n#define softfloat_shortShiftRightJam128 softfloat_shortShiftRightJam128\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 128 bits formed by concatenating 'a64' and 'a0' right by the\r\n| number of bits given in 'dist', which must be in the range 1 to 63.  If any\r\n| nonzero bits are shifted off, they are \"jammed\" into the least-significant\r\n| bit of the shifted value by setting the least-significant bit to 1.  This\r\n| shifted-and-jammed value is returned.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (3 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128\r\n softfloat_shortShiftRightJam128(\r\n     uint64_t a64, uint64_t a0, uint_fast8_t dist )\r\n{\r\n    uint_fast8_t negDist = -dist;\r\n    struct uint128 z;\r\n    z.v64 = a64>>dist;\r\n    z.v0 =\r\n        a64<<(negDist & 63) | a0>>dist\r\n            | ((uint64_t) (a0<<(negDist & 63)) != 0);\r\n    return z;\r\n}\r\n#else\r\nstruct uint128\r\n softfloat_shortShiftRightJam128(\r\n     uint64_t a64, uint64_t a0, uint_fast8_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRightJam128Extra\r\n#define softfloat_shortShiftRightJam128Extra softfloat_shortShiftRightJam128Extra\r\n/*----------------------------------------------------------------------------\r\n| This function is the same as 'softfloat_shiftRightJam128Extra' (below),\r\n| except that 'dist' must be in the range 1 to 63.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (3 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128_extra\r\n softfloat_shortShiftRightJam128Extra(\r\n     uint64_t a64, uint64_t a0, uint64_t extra, uint_fast8_t dist )\r\n{\r\n    uint_fast8_t negDist = -dist;\r\n    struct uint128_extra z;\r\n    z.v.v64 = a64>>dist;\r\n    z.v.v0 = a64<<(negDist & 63) | a0>>dist;\r\n    z.extra = a0<<(negDist & 63) | (extra != 0);\r\n    return z;\r\n}\r\n#else\r\nstruct uint128_extra\r\n softfloat_shortShiftRightJam128Extra(\r\n     uint64_t a64, uint64_t a0, uint64_t extra, uint_fast8_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam64Extra\r\n#define softfloat_shiftRightJam64Extra softfloat_shiftRightJam64Extra\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 128 bits formed by concatenating 'a' and 'extra' right by 64\r\n| _plus_ the number of bits given in 'dist', which must not be zero.  This\r\n| shifted value is at most 64 nonzero bits and is returned in the 'v' field\r\n| of the 'struct uint64_extra' result.  The 64-bit 'extra' field of the result\r\n| contains a value formed as follows from the bits that were shifted off:  The\r\n| _last_ bit shifted off is the most-significant bit of the 'extra' field, and\r\n| the other 63 bits of the 'extra' field are all zero if and only if _all_but_\r\n| _the_last_ bits shifted off were all zero.\r\n|   (This function makes more sense if 'a' and 'extra' are considered to form\r\n| an unsigned fixed-point number with binary point between 'a' and 'extra'.\r\n| This fixed-point value is shifted right by the number of bits given in\r\n| 'dist', and the integer part of this shifted value is returned in the 'v'\r\n| field of the result.  The fractional part of the shifted value is modified\r\n| as described above and returned in the 'extra' field of the result.)\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (4 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint64_extra\r\n softfloat_shiftRightJam64Extra(\r\n     uint64_t a, uint64_t extra, uint_fast32_t dist )\r\n{\r\n    struct uint64_extra z;\r\n    if ( dist < 64 ) {\r\n        z.v = a>>dist;\r\n        z.extra = a<<(-dist & 63);\r\n    } else {\r\n        z.v = 0;\r\n        z.extra = (dist == 64) ? a : (a != 0);\r\n    }\r\n    z.extra |= (extra != 0);\r\n    return z;\r\n}\r\n#else\r\nstruct uint64_extra\r\n softfloat_shiftRightJam64Extra(\r\n     uint64_t a, uint64_t extra, uint_fast32_t dist );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam128\r\n//#define softfloat_shiftRightJam128 softfloat_shiftRightJam128\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 128 bits formed by concatenating 'a64' and 'a0' right by the\r\n| number of bits given in 'dist', which must not be zero.  If any nonzero bits\r\n| are shifted off, they are \"jammed\" into the least-significant bit of the\r\n| shifted value by setting the least-significant bit to 1.  This shifted-and-\r\n| jammed value is returned.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist' is\r\n| greater than 128, the result will be either 0 or 1, depending on whether the\r\n| original 128 bits are all zeros.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128\r\n softfloat_shiftRightJam128( uint64_t a64, uint64_t a0, uint_fast32_t dist );\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam128Extra\r\n#define softfloat_shiftRightJam128Extra softfloat_shiftRightJam128Extra\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 192 bits formed by concatenating 'a64', 'a0', and 'extra' right\r\n| by 64 _plus_ the number of bits given in 'dist', which must not be zero.\r\n| This shifted value is at most 128 nonzero bits and is returned in the 'v'\r\n| field of the 'struct uint128_extra' result.  The 64-bit 'extra' field of the\r\n| result contains a value formed as follows from the bits that were shifted\r\n| off:  The _last_ bit shifted off is the most-significant bit of the 'extra'\r\n| field, and the other 63 bits of the 'extra' field are all zero if and only\r\n| if _all_but_the_last_ bits shifted off were all zero.\r\n|   (This function makes more sense if 'a64', 'a0', and 'extra' are considered\r\n| to form an unsigned fixed-point number with binary point between 'a0' and\r\n| 'extra'.  This fixed-point value is shifted right by the number of bits\r\n| given in 'dist', and the integer part of this shifted value is returned\r\n| in the 'v' field of the result.  The fractional part of the shifted value\r\n| is modified as described above and returned in the 'extra' field of the\r\n| result.)\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128_extra\r\n softfloat_shiftRightJam128Extra(\r\n     uint64_t a64, uint64_t a0, uint64_t extra, uint_fast32_t dist );\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam256M\r\n#define softfloat_shiftRightJam256M softfloat_shiftRightJam256M\r\n/*----------------------------------------------------------------------------\r\n| Shifts the 256-bit unsigned integer pointed to by 'aPtr' right by the number\r\n| of bits given in 'dist', which must not be zero.  If any nonzero bits are\r\n| shifted off, they are \"jammed\" into the least-significant bit of the shifted\r\n| value by setting the least-significant bit to 1.  This shifted-and-jammed\r\n| value is stored at the location pointed to by 'zPtr'.  Each of 'aPtr' and\r\n| 'zPtr' points to an array of four 64-bit elements that concatenate in the\r\n| platform's normal endian order to form a 256-bit integer.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist'\r\n| is greater than 256, the stored result will be either 0 or 1, depending on\r\n| whether the original 256 bits are all zeros.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shiftRightJam256M(\r\n     const uint64_t *aPtr, uint_fast32_t dist, uint64_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_add128\r\n#define softfloat_add128 softfloat_add128\r\n/*----------------------------------------------------------------------------\r\n| Returns the sum of the 128-bit integer formed by concatenating 'a64' and\r\n| 'a0' and the 128-bit integer formed by concatenating 'b64' and 'b0'.  The\r\n| addition is modulo 2^128, so any carry out is lost.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128\r\n softfloat_add128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n    struct uint128 z;\r\n    z.v0 = a0 + b0;\r\n    z.v64 = a64 + b64 + (z.v0 < a0);\r\n    return z;\r\n}\r\n#else\r\nstruct uint128\r\n softfloat_add128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_add256M\r\n/*----------------------------------------------------------------------------\r\n| Adds the two 256-bit integers pointed to by 'aPtr' and 'bPtr'.  The addition\r\n| is modulo 2^256, so any carry out is lost.  The sum is stored at the\r\n| location pointed to by 'zPtr'.  Each of 'aPtr', 'bPtr', and 'zPtr' points to\r\n| an array of four 64-bit elements that concatenate in the platform's normal\r\n| endian order to form a 256-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_add256M(\r\n     const uint64_t *aPtr, const uint64_t *bPtr, uint64_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_sub128\r\n#define softfloat_sub128 softfloat_sub128\r\n/*----------------------------------------------------------------------------\r\n| Returns the difference of the 128-bit integer formed by concatenating 'a64'\r\n| and 'a0' and the 128-bit integer formed by concatenating 'b64' and 'b0'.\r\n| The subtraction is modulo 2^128, so any borrow out (carry out) is lost.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128\r\n softfloat_sub128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n    struct uint128 z;\r\n    z.v0 = a0 - b0;\r\n    z.v64 = a64 - b64;\r\n    z.v64 -= (a0 < b0);\r\n    return z;\r\n}\r\n#else\r\nstruct uint128\r\n softfloat_sub128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_sub256M\r\n/*----------------------------------------------------------------------------\r\n| Subtracts the 256-bit integer pointed to by 'bPtr' from the 256-bit integer\r\n| pointed to by 'aPtr'.  The addition is modulo 2^256, so any borrow out\r\n| (carry out) is lost.  The difference is stored at the location pointed to\r\n| by 'zPtr'.  Each of 'aPtr', 'bPtr', and 'zPtr' points to an array of four\r\n| 64-bit elements that concatenate in the platform's normal endian order to\r\n| form a 256-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_sub256M(\r\n     const uint64_t *aPtr, const uint64_t *bPtr, uint64_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_mul64ByShifted32To128\r\n/*----------------------------------------------------------------------------\r\n| Returns the 128-bit product of 'a', 'b', and 2^32.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (3 <= INLINE_LEVEL)\r\nINLINE struct uint128 softfloat_mul64ByShifted32To128( uint64_t a, uint32_t b )\r\n{\r\n    uint_fast64_t mid;\r\n    struct uint128 z;\r\n    mid = (uint_fast64_t) (uint32_t) a * b;\r\n    z.v0 = mid<<32;\r\n    z.v64 = (uint_fast64_t) (uint32_t) (a>>32) * b + (mid>>32);\r\n    return z;\r\n}\r\n#else\r\nstruct uint128 softfloat_mul64ByShifted32To128( uint64_t a, uint32_t b );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_mul64To128\r\n/*----------------------------------------------------------------------------\r\n| Returns the 128-bit product of 'a' and 'b'.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128 softfloat_mul64To128( uint64_t a, uint64_t b );\r\n#endif\r\n\r\n#ifndef softfloat_mul128By32\r\n/*----------------------------------------------------------------------------\r\n| Returns the product of the 128-bit integer formed by concatenating 'a64' and\r\n| 'a0', multiplied by 'b'.  The multiplication is modulo 2^128; any overflow\r\n| bits are discarded.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (4 <= INLINE_LEVEL)\r\nINLINE\r\nstruct uint128 softfloat_mul128By32( uint64_t a64, uint64_t a0, uint32_t b )\r\n{\r\n    struct uint128 z;\r\n    uint_fast64_t mid;\r\n    uint_fast32_t carry;\r\n    z.v0 = a0 * b;\r\n    mid = (uint_fast64_t) (uint32_t) (a0>>32) * b;\r\n    carry = (uint32_t) ((uint_fast32_t) (z.v0>>32) - (uint_fast32_t) mid);\r\n    z.v64 = a64 * b + (uint_fast32_t) ((mid + carry)>>32);\r\n    return z;\r\n}\r\n#else\r\nstruct uint128 softfloat_mul128By32( uint64_t a64, uint64_t a0, uint32_t b );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_mul128To256M\r\n/*----------------------------------------------------------------------------\r\n| Multiplies the 128-bit unsigned integer formed by concatenating 'a64' and\r\n| 'a0' by the 128-bit unsigned integer formed by concatenating 'b64' and\r\n| 'b0'.  The 256-bit product is stored at the location pointed to by 'zPtr'.\r\n| Argument 'zPtr' points to an array of four 64-bit elements that concatenate\r\n| in the platform's normal endian order to form a 256-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_mul128To256M(\r\n     uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0, uint64_t *zPtr );\r\n#endif\r\n\r\n#else\r\n\r\n/*----------------------------------------------------------------------------\r\n| The following functions are needed only when 'SOFTFLOAT_FAST_INT64' is not\r\n| defined.\r\n*----------------------------------------------------------------------------*/\r\n\r\n#ifndef softfloat_compare96M\r\n/*----------------------------------------------------------------------------\r\n| Compares the two 96-bit unsigned integers pointed to by 'aPtr' and 'bPtr'.\r\n| Returns -1 if the first integer (A) is less than the second (B); returns 0\r\n| if the two integers are equal; and returns +1 if the first integer (A)\r\n| is greater than the second (B).  (The result is thus the signum of A - B.)\r\n| Each of 'aPtr' and 'bPtr' points to an array of three 32-bit elements that\r\n| concatenate in the platform's normal endian order to form a 96-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nint_fast8_t softfloat_compare96M( const uint32_t *aPtr, const uint32_t *bPtr );\r\n#endif\r\n\r\n#ifndef softfloat_compare128M\r\n/*----------------------------------------------------------------------------\r\n| Compares the two 128-bit unsigned integers pointed to by 'aPtr' and 'bPtr'.\r\n| Returns -1 if the first integer (A) is less than the second (B); returns 0\r\n| if the two integers are equal; and returns +1 if the first integer (A)\r\n| is greater than the second (B).  (The result is thus the signum of A - B.)\r\n| Each of 'aPtr' and 'bPtr' points to an array of four 32-bit elements that\r\n| concatenate in the platform's normal endian order to form a 128-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nint_fast8_t\r\n softfloat_compare128M( const uint32_t *aPtr, const uint32_t *bPtr );\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftLeft64To96M\r\n/*----------------------------------------------------------------------------\r\n| Extends 'a' to 96 bits and shifts the value left by the number of bits given\r\n| in 'dist', which must be in the range 1 to 31.  The result is stored at the\r\n| location pointed to by 'zPtr'.  Argument 'zPtr' points to an array of three\r\n| 32-bit elements that concatenate in the platform's normal endian order to\r\n| form a 96-bit integer.\r\n*----------------------------------------------------------------------------*/\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\nINLINE\r\nvoid\r\n softfloat_shortShiftLeft64To96M(\r\n     uint64_t a, uint_fast8_t dist, uint32_t *zPtr )\r\n{\r\n    zPtr[indexWord( 3, 0 )] = (uint32_t) a<<dist;\r\n    a >>= 32 - dist;\r\n    zPtr[indexWord( 3, 2 )] = a>>32;\r\n    zPtr[indexWord( 3, 1 )] = a;\r\n}\r\n#else\r\nvoid\r\n softfloat_shortShiftLeft64To96M(\r\n     uint64_t a, uint_fast8_t dist, uint32_t *zPtr );\r\n#endif\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftLeftM\r\n/*----------------------------------------------------------------------------\r\n| Shifts the N-bit unsigned integer pointed to by 'aPtr' left by the number\r\n| of bits given in 'dist', where N = 'size_words' * 32.  The value of 'dist'\r\n| must be in the range 1 to 31.  Any nonzero bits shifted off are lost.  The\r\n| shifted N-bit result is stored at the location pointed to by 'zPtr'.  Each\r\n| of 'aPtr' and 'zPtr' points to a 'size_words'-long array of 32-bit elements\r\n| that concatenate in the platform's normal endian order to form an N-bit\r\n| integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shortShiftLeftM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     uint_fast8_t dist,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftLeft96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shortShiftLeftM' with\r\n| 'size_words' = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shortShiftLeft96M( aPtr, dist, zPtr ) softfloat_shortShiftLeftM( 3, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftLeft128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shortShiftLeftM' with\r\n| 'size_words' = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shortShiftLeft128M( aPtr, dist, zPtr ) softfloat_shortShiftLeftM( 4, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftLeft160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shortShiftLeftM' with\r\n| 'size_words' = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shortShiftLeft160M( aPtr, dist, zPtr ) softfloat_shortShiftLeftM( 5, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftLeftM\r\n/*----------------------------------------------------------------------------\r\n| Shifts the N-bit unsigned integer pointed to by 'aPtr' left by the number\r\n| of bits given in 'dist', where N = 'size_words' * 32.  The value of 'dist'\r\n| must not be zero.  Any nonzero bits shifted off are lost.  The shifted\r\n| N-bit result is stored at the location pointed to by 'zPtr'.  Each of 'aPtr'\r\n| and 'zPtr' points to a 'size_words'-long array of 32-bit elements that\r\n| concatenate in the platform's normal endian order to form an N-bit integer.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist' is\r\n| greater than N, the stored result will be 0.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shiftLeftM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     uint32_t dist,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_shiftLeft96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftLeftM' with\r\n| 'size_words' = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftLeft96M( aPtr, dist, zPtr ) softfloat_shiftLeftM( 3, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftLeft128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftLeftM' with\r\n| 'size_words' = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftLeft128M( aPtr, dist, zPtr ) softfloat_shiftLeftM( 4, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftLeft160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftLeftM' with\r\n| 'size_words' = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftLeft160M( aPtr, dist, zPtr ) softfloat_shiftLeftM( 5, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRightM\r\n/*----------------------------------------------------------------------------\r\n| Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number\r\n| of bits given in 'dist', where N = 'size_words' * 32.  The value of 'dist'\r\n| must be in the range 1 to 31.  Any nonzero bits shifted off are lost.  The\r\n| shifted N-bit result is stored at the location pointed to by 'zPtr'.  Each\r\n| of 'aPtr' and 'zPtr' points to a 'size_words'-long array of 32-bit elements\r\n| that concatenate in the platform's normal endian order to form an N-bit\r\n| integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shortShiftRightM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     uint_fast8_t dist,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRight128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shortShiftRightM' with\r\n| 'size_words' = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shortShiftRight128M( aPtr, dist, zPtr ) softfloat_shortShiftRightM( 4, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRight160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shortShiftRightM' with\r\n| 'size_words' = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shortShiftRight160M( aPtr, dist, zPtr ) softfloat_shortShiftRightM( 5, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRightJamM\r\n/*----------------------------------------------------------------------------\r\n| Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number\r\n| of bits given in 'dist', where N = 'size_words' * 32.  The value of 'dist'\r\n| must be in the range 1 to 31.  If any nonzero bits are shifted off, they are\r\n| \"jammed\" into the least-significant bit of the shifted value by setting the\r\n| least-significant bit to 1.  This shifted-and-jammed N-bit result is stored\r\n| at the location pointed to by 'zPtr'.  Each of 'aPtr' and 'zPtr' points\r\n| to a 'size_words'-long array of 32-bit elements that concatenate in the\r\n| platform's normal endian order to form an N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shortShiftRightJamM(\r\n     uint_fast8_t, const uint32_t *, uint_fast8_t, uint32_t * );\r\n#endif\r\n\r\n#ifndef softfloat_shortShiftRightJam160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shortShiftRightJamM' with\r\n| 'size_words' = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shortShiftRightJam160M( aPtr, dist, zPtr ) softfloat_shortShiftRightJamM( 5, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightM\r\n/*----------------------------------------------------------------------------\r\n| Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number\r\n| of bits given in 'dist', where N = 'size_words' * 32.  The value of 'dist'\r\n| must not be zero.  Any nonzero bits shifted off are lost.  The shifted\r\n| N-bit result is stored at the location pointed to by 'zPtr'.  Each of 'aPtr'\r\n| and 'zPtr' points to a 'size_words'-long array of 32-bit elements that\r\n| concatenate in the platform's normal endian order to form an N-bit integer.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist' is\r\n| greater than N, the stored result will be 0.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shiftRightM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     uint32_t dist,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_shiftRight96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftRightM' with\r\n| 'size_words' = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftRight96M( aPtr, dist, zPtr ) softfloat_shiftRightM( 3, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJamM\r\n/*----------------------------------------------------------------------------\r\n| Shifts the N-bit unsigned integer pointed to by 'aPtr' right by the number\r\n| of bits given in 'dist', where N = 'size_words' * 32.  The value of 'dist'\r\n| must not be zero.  If any nonzero bits are shifted off, they are \"jammed\"\r\n| into the least-significant bit of the shifted value by setting the least-\r\n| significant bit to 1.  This shifted-and-jammed N-bit result is stored\r\n| at the location pointed to by 'zPtr'.  Each of 'aPtr' and 'zPtr' points\r\n| to a 'size_words'-long array of 32-bit elements that concatenate in the\r\n| platform's normal endian order to form an N-bit integer.\r\n|   The value of 'dist' can be arbitrarily large.  In particular, if 'dist'\r\n| is greater than N, the stored result will be either 0 or 1, depending on\r\n| whether the original N bits are all zeros.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_shiftRightJamM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     uint32_t dist,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftRightJamM' with\r\n| 'size_words' = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftRightJam96M( aPtr, dist, zPtr ) softfloat_shiftRightJamM( 3, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftRightJamM' with\r\n| 'size_words' = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftRightJam128M( aPtr, dist, zPtr ) softfloat_shiftRightJamM( 4, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_shiftRightJam160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_shiftRightJamM' with\r\n| 'size_words' = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_shiftRightJam160M( aPtr, dist, zPtr ) softfloat_shiftRightJamM( 5, aPtr, dist, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_addM\r\n/*----------------------------------------------------------------------------\r\n| Adds the two N-bit integers pointed to by 'aPtr' and 'bPtr', where N =\r\n| 'size_words' * 32.  The addition is modulo 2^N, so any carry out is lost.\r\n| The N-bit sum is stored at the location pointed to by 'zPtr'.  Each of\r\n| 'aPtr', 'bPtr', and 'zPtr' points to a 'size_words'-long array of 32-bit\r\n| elements that concatenate in the platform's normal endian order to form an\r\n| N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_addM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     const uint32_t *bPtr,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_add96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_addM' with 'size_words'\r\n| = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_add96M( aPtr, bPtr, zPtr ) softfloat_addM( 3, aPtr, bPtr, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_add128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_addM' with 'size_words'\r\n| = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_add128M( aPtr, bPtr, zPtr ) softfloat_addM( 4, aPtr, bPtr, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_add160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_addM' with 'size_words'\r\n| = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_add160M( aPtr, bPtr, zPtr ) softfloat_addM( 5, aPtr, bPtr, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_addCarryM\r\n/*----------------------------------------------------------------------------\r\n| Adds the two N-bit unsigned integers pointed to by 'aPtr' and 'bPtr', where\r\n| N = 'size_words' * 32, plus 'carry', which must be either 0 or 1.  The N-bit\r\n| sum (modulo 2^N) is stored at the location pointed to by 'zPtr', and any\r\n| carry out is returned as the result.  Each of 'aPtr', 'bPtr', and 'zPtr'\r\n| points to a 'size_words'-long array of 32-bit elements that concatenate in\r\n| the platform's normal endian order to form an N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast8_t\r\n softfloat_addCarryM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     const uint32_t *bPtr,\r\n     uint_fast8_t carry,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_addComplCarryM\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_addCarryM', except that\r\n| the value of the unsigned integer pointed to by 'bPtr' is bit-wise completed\r\n| before the addition.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast8_t\r\n softfloat_addComplCarryM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     const uint32_t *bPtr,\r\n     uint_fast8_t carry,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_addComplCarry96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_addComplCarryM' with\r\n| 'size_words' = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_addComplCarry96M( aPtr, bPtr, carry, zPtr ) softfloat_addComplCarryM( 3, aPtr, bPtr, carry, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_negXM\r\n/*----------------------------------------------------------------------------\r\n| Replaces the N-bit unsigned integer pointed to by 'zPtr' by the\r\n| 2s-complement of itself, where N = 'size_words' * 32.  Argument 'zPtr'\r\n| points to a 'size_words'-long array of 32-bit elements that concatenate in\r\n| the platform's normal endian order to form an N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_negXM( uint_fast8_t size_words, uint32_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_negX96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_negXM' with 'size_words'\r\n| = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_negX96M( zPtr ) softfloat_negXM( 3, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_negX128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_negXM' with 'size_words'\r\n| = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_negX128M( zPtr ) softfloat_negXM( 4, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_negX160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_negXM' with 'size_words'\r\n| = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_negX160M( zPtr ) softfloat_negXM( 5, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_negX256M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_negXM' with 'size_words'\r\n| = 8 (N = 256).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_negX256M( zPtr ) softfloat_negXM( 8, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_sub1XM\r\n/*----------------------------------------------------------------------------\r\n| Subtracts 1 from the N-bit integer pointed to by 'zPtr', where N =\r\n| 'size_words' * 32.  The subtraction is modulo 2^N, so any borrow out (carry\r\n| out) is lost.  Argument 'zPtr' points to a 'size_words'-long array of 32-bit\r\n| elements that concatenate in the platform's normal endian order to form an\r\n| N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_sub1XM( uint_fast8_t size_words, uint32_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_sub1X96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_sub1XM' with 'size_words'\r\n| = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_sub1X96M( zPtr ) softfloat_sub1XM( 3, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_sub1X160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_sub1XM' with 'size_words'\r\n| = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_sub1X160M( zPtr ) softfloat_sub1XM( 5, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_subM\r\n/*----------------------------------------------------------------------------\r\n| Subtracts the two N-bit integers pointed to by 'aPtr' and 'bPtr', where N =\r\n| 'size_words' * 32.  The subtraction is modulo 2^N, so any borrow out (carry\r\n| out) is lost.  The N-bit difference is stored at the location pointed to by\r\n| 'zPtr'.  Each of 'aPtr', 'bPtr', and 'zPtr' points to a 'size_words'-long\r\n| array of 32-bit elements that concatenate in the platform's normal endian\r\n| order to form an N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_subM(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *aPtr,\r\n     const uint32_t *bPtr,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_sub96M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_subM' with 'size_words'\r\n| = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_sub96M( aPtr, bPtr, zPtr ) softfloat_subM( 3, aPtr, bPtr, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_sub128M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_subM' with 'size_words'\r\n| = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_sub128M( aPtr, bPtr, zPtr ) softfloat_subM( 4, aPtr, bPtr, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_sub160M\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_subM' with 'size_words'\r\n| = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_sub160M( aPtr, bPtr, zPtr ) softfloat_subM( 5, aPtr, bPtr, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_mul64To128M\r\n/*----------------------------------------------------------------------------\r\n| Multiplies 'a' and 'b' and stores the 128-bit product at the location\r\n| pointed to by 'zPtr'.  Argument 'zPtr' points to an array of four 32-bit\r\n| elements that concatenate in the platform's normal endian order to form a\r\n| 128-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_mul64To128M( uint64_t a, uint64_t b, uint32_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_mul128MTo256M\r\n/*----------------------------------------------------------------------------\r\n| Multiplies the two 128-bit unsigned integers pointed to by 'aPtr' and\r\n| 'bPtr', and stores the 256-bit product at the location pointed to by 'zPtr'.\r\n| Each of 'aPtr' and 'bPtr' points to an array of four 32-bit elements that\r\n| concatenate in the platform's normal endian order to form a 128-bit integer.\r\n| Argument 'zPtr' points to an array of eight 32-bit elements that concatenate\r\n| to form a 256-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_mul128MTo256M(\r\n     const uint32_t *aPtr, const uint32_t *bPtr, uint32_t *zPtr );\r\n#endif\r\n\r\n#ifndef softfloat_remStepMBy32\r\n/*----------------------------------------------------------------------------\r\n| Performs a \"remainder reduction step\" as follows:  Arguments 'remPtr' and\r\n| 'bPtr' both point to N-bit unsigned integers, where N = 'size_words' * 32.\r\n| Defining R and B as the values of those integers, the expression (R<<'dist')\r\n| - B * q is computed modulo 2^N, and the N-bit result is stored at the\r\n| location pointed to by 'zPtr'.  Each of 'remPtr', 'bPtr', and 'zPtr' points\r\n| to a 'size_words'-long array of 32-bit elements that concatenate in the\r\n| platform's normal endian order to form an N-bit integer.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_remStepMBy32(\r\n     uint_fast8_t size_words,\r\n     const uint32_t *remPtr,\r\n     uint_fast8_t dist,\r\n     const uint32_t *bPtr,\r\n     uint32_t q,\r\n     uint32_t *zPtr\r\n );\r\n#endif\r\n\r\n#ifndef softfloat_remStep96MBy32\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_remStepMBy32' with\r\n| 'size_words' = 3 (N = 96).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_remStep96MBy32( remPtr, dist, bPtr, q, zPtr ) softfloat_remStepMBy32( 3, remPtr, dist, bPtr, q, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_remStep128MBy32\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_remStepMBy32' with\r\n| 'size_words' = 4 (N = 128).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_remStep128MBy32( remPtr, dist, bPtr, q, zPtr ) softfloat_remStepMBy32( 4, remPtr, dist, bPtr, q, zPtr )\r\n#endif\r\n\r\n#ifndef softfloat_remStep160MBy32\r\n/*----------------------------------------------------------------------------\r\n| This function or macro is the same as 'softfloat_remStepMBy32' with\r\n| 'size_words' = 5 (N = 160).\r\n*----------------------------------------------------------------------------*/\r\n#define softfloat_remStep160MBy32( remPtr, dist, bPtr, q, zPtr ) softfloat_remStepMBy32( 5, remPtr, dist, bPtr, q, zPtr )\r\n#endif\r\n\r\n#endif\r\n\r\n#endif\r\n\r\n/**** ended inlining primitives.h ****/\n/**** skipping file: softfloat_types.h ****/\n\r\nunion ui16_f16 { uint16_t ui; float16_t f; };\r\nunion ui32_f32 { uint32_t ui; float32_t f; };\r\nunion ui64_f64 { uint64_t ui; float64_t f; };\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nunion extF80M_extF80 { struct extFloat80M fM; extFloat80_t f; };\r\nunion ui128_f128 { struct uint128 ui; float128_t f; };\r\n#endif\r\n\r\nenum {\r\n    softfloat_mulAdd_subC    = 1,\r\n    softfloat_mulAdd_subProd = 2\r\n};\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t softfloat_roundToUI32( bool, uint_fast64_t, uint_fast8_t, bool );\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nuint_fast64_t\r\n softfloat_roundToUI64(\r\n     bool, uint_fast64_t, uint_fast64_t, uint_fast8_t, bool );\r\n#else\r\nuint_fast64_t softfloat_roundMToUI64( bool, uint32_t *, uint_fast8_t, bool );\r\n#endif\r\n\r\nint_fast32_t softfloat_roundToI32( bool, uint_fast64_t, uint_fast8_t, bool );\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\nint_fast64_t\r\n softfloat_roundToI64(\r\n     bool, uint_fast64_t, uint_fast64_t, uint_fast8_t, bool );\r\n#else\r\nint_fast64_t softfloat_roundMToI64( bool, uint32_t *, uint_fast8_t, bool );\r\n#endif\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define signF16UI( a ) ((bool) ((uint16_t) (a)>>15))\r\n#define expF16UI( a ) ((int_fast8_t) ((a)>>10) & 0x1F)\r\n#define fracF16UI( a ) ((a) & 0x03FF)\r\n#define packToF16UI( sign, exp, sig ) (((uint16_t) (sign)<<15) + ((uint16_t) (exp)<<10) + (sig))\r\n\r\n#define isNaNF16UI( a ) (((~(a) & 0x7C00) == 0) && ((a) & 0x03FF))\r\n\r\nstruct exp8_sig16 { int_fast8_t exp; uint_fast16_t sig; };\r\nstruct exp8_sig16 softfloat_normSubnormalF16Sig( uint_fast16_t );\r\n\r\nfloat16_t softfloat_roundPackToF16( bool, int_fast16_t, uint_fast16_t );\r\nfloat16_t softfloat_normRoundPackToF16( bool, int_fast16_t, uint_fast16_t );\r\n\r\nfloat16_t softfloat_addMagsF16( uint_fast16_t, uint_fast16_t );\r\nfloat16_t softfloat_subMagsF16( uint_fast16_t, uint_fast16_t );\r\nfloat16_t\r\n softfloat_mulAddF16(\r\n     uint_fast16_t, uint_fast16_t, uint_fast16_t, uint_fast8_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define signF32UI( a ) ((bool) ((uint32_t) (a)>>31))\r\n#define expF32UI( a ) ((int_fast16_t) ((a)>>23) & 0xFF)\r\n#define fracF32UI( a ) ((a) & 0x007FFFFF)\r\n#define packToF32UI( sign, exp, sig ) (((uint32_t) (sign)<<31) + ((uint32_t) (exp)<<23) + (sig))\r\n\r\n#define isNaNF32UI( a ) (((~(a) & 0x7F800000) == 0) && ((a) & 0x007FFFFF))\r\n\r\nstruct exp16_sig32 { int_fast16_t exp; uint_fast32_t sig; };\r\nstruct exp16_sig32 softfloat_normSubnormalF32Sig( uint_fast32_t );\r\n\r\nfloat32_t softfloat_roundPackToF32( bool, int_fast16_t, uint_fast32_t );\r\nfloat32_t softfloat_normRoundPackToF32( bool, int_fast16_t, uint_fast32_t );\r\n\r\nfloat32_t softfloat_addMagsF32( uint_fast32_t, uint_fast32_t );\r\nfloat32_t softfloat_subMagsF32( uint_fast32_t, uint_fast32_t );\r\nfloat32_t\r\n softfloat_mulAddF32(\r\n     uint_fast32_t, uint_fast32_t, uint_fast32_t, uint_fast8_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define signF64UI( a ) ((bool) ((uint64_t) (a)>>63))\r\n#define expF64UI( a ) ((int_fast16_t) ((a)>>52) & 0x7FF)\r\n#define fracF64UI( a ) ((a) & UINT64_C( 0x000FFFFFFFFFFFFF ))\r\n#define packToF64UI( sign, exp, sig ) ((uint64_t) (((uint_fast64_t) (sign)<<63) + ((uint_fast64_t) (exp)<<52) + (sig)))\r\n\r\n#define isNaNF64UI( a ) (((~(a) & UINT64_C( 0x7FF0000000000000 )) == 0) && ((a) & UINT64_C( 0x000FFFFFFFFFFFFF )))\r\n\r\nstruct exp16_sig64 { int_fast16_t exp; uint_fast64_t sig; };\r\nstruct exp16_sig64 softfloat_normSubnormalF64Sig( uint_fast64_t );\r\n\r\nfloat64_t softfloat_roundPackToF64( bool, int_fast16_t, uint_fast64_t );\r\nfloat64_t softfloat_normRoundPackToF64( bool, int_fast16_t, uint_fast64_t );\r\n\r\nfloat64_t softfloat_addMagsF64( uint_fast64_t, uint_fast64_t, bool );\r\nfloat64_t softfloat_subMagsF64( uint_fast64_t, uint_fast64_t, bool );\r\nfloat64_t\r\n softfloat_mulAddF64(\r\n     uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast8_t );\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define signExtF80UI64( a64 ) ((bool) ((uint16_t) (a64)>>15))\r\n#define expExtF80UI64( a64 ) ((a64) & 0x7FFF)\r\n#define packToExtF80UI64( sign, exp ) ((uint_fast16_t) (sign)<<15 | (exp))\r\n\r\n#define isNaNExtF80UI( a64, a0 ) ((((a64) & 0x7FFF) == 0x7FFF) && ((a0) & UINT64_C( 0x7FFFFFFFFFFFFFFF )))\r\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n\r\nstruct exp32_sig64 { int_fast32_t exp; uint64_t sig; };\r\nstruct exp32_sig64 softfloat_normSubnormalExtF80Sig( uint_fast64_t );\r\n\r\nextFloat80_t\r\n softfloat_roundPackToExtF80(\r\n     bool, int_fast32_t, uint_fast64_t, uint_fast64_t, uint_fast8_t );\r\nextFloat80_t\r\n softfloat_normRoundPackToExtF80(\r\n     bool, int_fast32_t, uint_fast64_t, uint_fast64_t, uint_fast8_t );\r\n\r\nextFloat80_t\r\n softfloat_addMagsExtF80(\r\n     uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );\r\nextFloat80_t\r\n softfloat_subMagsExtF80(\r\n     uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define signF128UI64( a64 ) ((bool) ((uint64_t) (a64)>>63))\r\n#define expF128UI64( a64 ) ((int_fast32_t) ((a64)>>48) & 0x7FFF)\r\n#define fracF128UI64( a64 ) ((a64) & UINT64_C( 0x0000FFFFFFFFFFFF ))\r\n#define packToF128UI64( sign, exp, sig64 ) (((uint_fast64_t) (sign)<<63) + ((uint_fast64_t) (exp)<<48) + (sig64))\r\n\r\n#define isNaNF128UI( a64, a0 ) (((~(a64) & UINT64_C( 0x7FFF000000000000 )) == 0) && (a0 || ((a64) & UINT64_C( 0x0000FFFFFFFFFFFF ))))\r\n\r\nstruct exp32_sig128 { int_fast32_t exp; struct uint128 sig; };\r\nstruct exp32_sig128\r\n softfloat_normSubnormalF128Sig( uint_fast64_t, uint_fast64_t );\r\n\r\nfloat128_t\r\n softfloat_roundPackToF128(\r\n     bool, int_fast32_t, uint_fast64_t, uint_fast64_t, uint_fast64_t );\r\nfloat128_t\r\n softfloat_normRoundPackToF128(\r\n     bool, int_fast32_t, uint_fast64_t, uint_fast64_t );\r\n\r\nfloat128_t\r\n softfloat_addMagsF128(\r\n     uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );\r\nfloat128_t\r\n softfloat_subMagsF128(\r\n     uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );\r\nfloat128_t\r\n softfloat_mulAddF128(\r\n     uint_fast64_t,\r\n     uint_fast64_t,\r\n     uint_fast64_t,\r\n     uint_fast64_t,\r\n     uint_fast64_t,\r\n     uint_fast64_t,\r\n     uint_fast8_t\r\n );\r\n\r\n#else\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n\r\nbool\r\n softfloat_tryPropagateNaNExtF80M(\r\n     const struct extFloat80M *,\r\n     const struct extFloat80M *,\r\n     struct extFloat80M *\r\n );\r\nvoid softfloat_invalidExtF80M( struct extFloat80M * );\r\n\r\nint softfloat_normExtF80SigM( uint64_t * );\r\n\r\nvoid\r\n softfloat_roundPackMToExtF80M(\r\n     bool, int32_t, uint32_t *, uint_fast8_t, struct extFloat80M * );\r\nvoid\r\n softfloat_normRoundPackMToExtF80M(\r\n     bool, int32_t, uint32_t *, uint_fast8_t, struct extFloat80M * );\r\n\r\nvoid\r\n softfloat_addExtF80M(\r\n     const struct extFloat80M *,\r\n     const struct extFloat80M *,\r\n     struct extFloat80M *,\r\n     bool\r\n );\r\n\r\nint\r\n softfloat_compareNonnormExtF80M(\r\n     const struct extFloat80M *, const struct extFloat80M * );\r\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\n#define signF128UI96( a96 ) ((bool) ((uint32_t) (a96)>>31))\r\n#define expF128UI96( a96 ) ((int32_t) ((a96)>>16) & 0x7FFF)\r\n#define fracF128UI96( a96 ) ((a96) & 0x0000FFFF)\r\n#define packToF128UI96( sign, exp, sig96 ) (((uint32_t) (sign)<<31) + ((uint32_t) (exp)<<16) + (sig96))\r\n\r\nbool softfloat_isNaNF128M( const uint32_t * );\r\n\r\nbool\r\n softfloat_tryPropagateNaNF128M(\r\n     const uint32_t *, const uint32_t *, uint32_t * );\r\nvoid softfloat_invalidF128M( uint32_t * );\r\n\r\nint softfloat_shiftNormSigF128M( const uint32_t *, uint_fast8_t, uint32_t * );\r\n\r\nvoid softfloat_roundPackMToF128M( bool, int32_t, uint32_t *, uint32_t * );\r\nvoid softfloat_normRoundPackMToF128M( bool, int32_t, uint32_t *, uint32_t * );\r\n\r\nvoid\r\n softfloat_addF128M( const uint32_t *, const uint32_t *, uint32_t *, bool );\r\nvoid\r\n softfloat_mulAddF128M(\r\n     const uint32_t *,\r\n     const uint32_t *,\r\n     const uint32_t *,\r\n     uint32_t *,\r\n     uint_fast8_t\r\n );\r\n\r\n#endif\r\n\r\n#endif\r\n\r\n/**** ended inlining internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting `uiA' and `uiB' as the bit patterns of two 16-bit floating-\r\n| point values, at least one of which is a NaN, returns the bit pattern of\r\n| the combined NaN result.  If either `uiA' or `uiB' has the pattern of a\r\n| signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast16_t\r\n softfloat_propagateNaNF16UI( uint_fast16_t uiA, uint_fast16_t uiB )\r\n{\r\n    bool isSigNaNA;\r\n\r\n    isSigNaNA = softfloat_isSigNaNF16UI( uiA );\r\n    if ( isSigNaNA || softfloat_isSigNaNF16UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        if ( isSigNaNA ) return uiA | 0x0200;\r\n    }\r\n    return (isNaNF16UI( uiA ) ? uiA : uiB) | 0x0200;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_propagateNaNF16UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_f32UIToCommonNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming `uiA' has the bit pattern of a 32-bit floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_f32UIToCommonNaN( uint_fast32_t uiA, struct commonNaN *zPtr )\r\n{\r\n\r\n    if ( softfloat_isSigNaNF32UI( uiA ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n    }\r\n    zPtr->sign = uiA>>31;\r\n    zPtr->v64  = (uint_fast64_t) uiA<<41;\r\n    zPtr->v0   = 0;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_f32UIToCommonNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_commonNaNToF32UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: specialize.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by `aPtr' into a 32-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t softfloat_commonNaNToF32UI( const struct commonNaN *aPtr )\r\n{\r\n\r\n    return (uint_fast32_t) aPtr->sign<<31 | 0x7FC00000 | aPtr->v64>>41;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_commonNaNToF32UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_propagateNaNF32UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting `uiA' and `uiB' as the bit patterns of two 32-bit floating-\r\n| point values, at least one of which is a NaN, returns the bit pattern of\r\n| the combined NaN result.  If either `uiA' or `uiB' has the pattern of a\r\n| signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast32_t\r\n softfloat_propagateNaNF32UI( uint_fast32_t uiA, uint_fast32_t uiB )\r\n{\r\n    bool isSigNaNA;\r\n\r\n    isSigNaNA = softfloat_isSigNaNF32UI( uiA );\r\n    if ( isSigNaNA || softfloat_isSigNaNF32UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        if ( isSigNaNA ) return uiA | 0x00400000;\r\n    }\r\n    return (isNaNF32UI( uiA ) ? uiA : uiB) | 0x00400000;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_propagateNaNF32UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_f64UIToCommonNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming `uiA' has the bit pattern of a 64-bit floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid softfloat_f64UIToCommonNaN( uint_fast64_t uiA, struct commonNaN *zPtr )\r\n{\r\n\r\n    if ( softfloat_isSigNaNF64UI( uiA ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n    }\r\n    zPtr->sign = uiA>>63;\r\n    zPtr->v64  = uiA<<12;\r\n    zPtr->v0   = 0;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_f64UIToCommonNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_commonNaNToF64UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: specialize.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by `aPtr' into a 64-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast64_t softfloat_commonNaNToF64UI( const struct commonNaN *aPtr )\r\n{\r\n\r\n    return\r\n        (uint_fast64_t) aPtr->sign<<63 | UINT64_C( 0x7FF8000000000000 )\r\n            | aPtr->v64>>12;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_commonNaNToF64UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_propagateNaNF64UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting `uiA' and `uiB' as the bit patterns of two 64-bit floating-\r\n| point values, at least one of which is a NaN, returns the bit pattern of\r\n| the combined NaN result.  If either `uiA' or `uiB' has the pattern of a\r\n| signaling NaN, the invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nuint_fast64_t\r\n softfloat_propagateNaNF64UI( uint_fast64_t uiA, uint_fast64_t uiB )\r\n{\r\n    bool isSigNaNA;\r\n\r\n    isSigNaNA = softfloat_isSigNaNF64UI( uiA );\r\n    if ( isSigNaNA || softfloat_isSigNaNF64UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        if ( isSigNaNA ) return uiA | UINT64_C( 0x0008000000000000 );\r\n    }\r\n    return (isNaNF64UI( uiA ) ? uiA : uiB) | UINT64_C( 0x0008000000000000 );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_propagateNaNF64UI.c ****/\n/**** start inlining ../../source/8086-SSE/extF80M_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\nbool extF80M_isSignalingNaN( const extFloat80_t *aPtr )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint64_t uiA0;\r\n\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    if ( (aSPtr->signExp & 0x7FFF) != 0x7FFF ) return false;\r\n    uiA0 = aSPtr->signif;\r\n    return\r\n        ! (uiA0 & UINT64_C( 0x4000000000000000 ))\r\n            && (uiA0 & UINT64_C( 0x3FFFFFFFFFFFFFFF));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/extF80M_isSignalingNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_extF80UIToCommonNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming the unsigned integer formed from concatenating `uiA64' and `uiA0'\r\n| has the bit pattern of an 80-bit extended floating-point NaN, converts\r\n| this NaN to the common NaN form, and stores the resulting common NaN at the\r\n| location pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_extF80UIToCommonNaN(\r\n     uint_fast16_t uiA64, uint_fast64_t uiA0, struct commonNaN *zPtr )\r\n{\r\n\r\n    if ( softfloat_isSigNaNExtF80UI( uiA64, uiA0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n    }\r\n    zPtr->sign = uiA64>>15;\r\n    zPtr->v64  = uiA0<<1;\r\n    zPtr->v0   = 0;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_extF80UIToCommonNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_commonNaNToExtF80UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n/**** skipping file: specialize.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by `aPtr' into an 80-bit extended\r\n| floating-point NaN, and returns the bit pattern of this value as an unsigned\r\n| integer.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128 softfloat_commonNaNToExtF80UI( const struct commonNaN *aPtr )\r\n{\r\n    struct uint128 uiZ;\r\n\r\n    uiZ.v64 = (uint_fast16_t) aPtr->sign<<15 | 0x7FFF;\r\n    uiZ.v0 = UINT64_C( 0xC000000000000000 ) | aPtr->v64>>1;\r\n    return uiZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_commonNaNToExtF80UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_propagateNaNExtF80UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2018 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting the unsigned integer formed from concatenating 'uiA64' and\r\n| 'uiA0' as an 80-bit extended floating-point value, and likewise interpreting\r\n| the unsigned integer formed from concatenating 'uiB64' and 'uiB0' as another\r\n| 80-bit extended floating-point value, and assuming at least on of these\r\n| floating-point values is a NaN, returns the bit pattern of the combined NaN\r\n| result.  If either original floating-point value is a signaling NaN, the\r\n| invalid exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128\r\n softfloat_propagateNaNExtF80UI(\r\n     uint_fast16_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast16_t uiB64,\r\n     uint_fast64_t uiB0\r\n )\r\n{\r\n    bool isSigNaNA, isSigNaNB;\r\n    uint_fast64_t uiNonsigA0, uiNonsigB0;\r\n    uint_fast16_t uiMagA64, uiMagB64;\r\n    struct uint128 uiZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    isSigNaNA = softfloat_isSigNaNExtF80UI( uiA64, uiA0 );\r\n    isSigNaNB = softfloat_isSigNaNExtF80UI( uiB64, uiB0 );\r\n    /*------------------------------------------------------------------------\r\n    | Make NaNs non-signaling.\r\n    *------------------------------------------------------------------------*/\r\n    uiNonsigA0 = uiA0 | UINT64_C( 0xC000000000000000 );\r\n    uiNonsigB0 = uiB0 | UINT64_C( 0xC000000000000000 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isSigNaNA | isSigNaNB ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        if ( isSigNaNA ) {\r\n            if ( isSigNaNB ) goto returnLargerMag;\r\n            if ( isNaNExtF80UI( uiB64, uiB0 ) ) goto returnB;\r\n            goto returnA;\r\n        } else {\r\n            if ( isNaNExtF80UI( uiA64, uiA0 ) ) goto returnA;\r\n            goto returnB;\r\n        }\r\n    }\r\n returnLargerMag:\r\n    uiMagA64 = uiA64 & 0x7FFF;\r\n    uiMagB64 = uiB64 & 0x7FFF;\r\n    if ( uiMagA64 < uiMagB64 ) goto returnB;\r\n    if ( uiMagB64 < uiMagA64 ) goto returnA;\r\n    if ( uiA0 < uiB0 ) goto returnB;\r\n    if ( uiB0 < uiA0 ) goto returnA;\r\n    if ( uiA64 < uiB64 ) goto returnA;\r\n returnB:\r\n    uiZ.v64 = uiB64;\r\n    uiZ.v0  = uiNonsigB0;\r\n    return uiZ;\r\n returnA:\r\n    uiZ.v64 = uiA64;\r\n    uiZ.v0  = uiNonsigA0;\r\n    return uiZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_propagateNaNExtF80UI.c ****/\n/**** start inlining ../../source/8086-SSE/f128M_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n*----------------------------------------------------------------------------*/\r\nbool f128M_isSignalingNaN( const float128_t *aPtr )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    if ( (uiA96 & 0x7FFF8000) != 0x7FFF0000 ) return false;\r\n    return\r\n        ((uiA96 & 0x00007FFF) != 0)\r\n            || ((aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                     | aWPtr[indexWord( 4, 0 )])\r\n                    != 0);\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/f128M_isSignalingNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_f128UIToCommonNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Assuming the unsigned integer formed from concatenating `uiA64' and `uiA0'\r\n| has the bit pattern of a 128-bit floating-point NaN, converts this NaN to\r\n| the common NaN form, and stores the resulting common NaN at the location\r\n| pointed to by `zPtr'.  If the NaN is a signaling NaN, the invalid exception\r\n| is raised.\r\n*----------------------------------------------------------------------------*/\r\nvoid\r\n softfloat_f128UIToCommonNaN(\r\n     uint_fast64_t uiA64, uint_fast64_t uiA0, struct commonNaN *zPtr )\r\n{\r\n    struct uint128 NaNSig;\r\n\r\n    if ( softfloat_isSigNaNF128UI( uiA64, uiA0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n    }\r\n    NaNSig = softfloat_shortShiftLeft128( uiA64, uiA0, 16 );\r\n    zPtr->sign = uiA64>>63;\r\n    zPtr->v64  = NaNSig.v64;\r\n    zPtr->v0   = NaNSig.v0;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_f128UIToCommonNaN.c ****/\n/**** start inlining ../../source/8086-SSE/s_commonNaNToF128UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n/**** skipping file: specialize.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Converts the common NaN pointed to by `aPtr' into a 128-bit floating-point\r\n| NaN, and returns the bit pattern of this value as an unsigned integer.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128 softfloat_commonNaNToF128UI( const struct commonNaN *aPtr )\r\n{\r\n    struct uint128 uiZ;\r\n\r\n    uiZ = softfloat_shortShiftRight128( aPtr->v64, aPtr->v0, 16 );\r\n    uiZ.v64 |= (uint_fast64_t) aPtr->sign<<63 | UINT64_C( 0x7FFF800000000000 );\r\n    return uiZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_commonNaNToF128UI.c ****/\n/**** start inlining ../../source/8086-SSE/s_propagateNaNF128UI.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n/*----------------------------------------------------------------------------\r\n| Interpreting the unsigned integer formed from concatenating `uiA64' and\r\n| `uiA0' as a 128-bit floating-point value, and likewise interpreting the\r\n| unsigned integer formed from concatenating `uiB64' and `uiB0' as another\r\n| 128-bit floating-point value, and assuming at least on of these floating-\r\n| point values is a NaN, returns the bit pattern of the combined NaN result.\r\n| If either original floating-point value is a signaling NaN, the invalid\r\n| exception is raised.\r\n*----------------------------------------------------------------------------*/\r\nstruct uint128\r\n softfloat_propagateNaNF128UI(\r\n     uint_fast64_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast64_t uiB64,\r\n     uint_fast64_t uiB0\r\n )\r\n{\r\n    bool isSigNaNA;\r\n    struct uint128 uiZ;\r\n\r\n    isSigNaNA = softfloat_isSigNaNF128UI( uiA64, uiA0 );\r\n    if ( isSigNaNA || softfloat_isSigNaNF128UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        if ( isSigNaNA ) goto returnNonsigA;\r\n    }\r\n    if ( isNaNF128UI( uiA64, uiA0 ) ) {\r\n returnNonsigA:\r\n        uiZ.v64 = uiA64;\r\n        uiZ.v0  = uiA0;\r\n    } else {\r\n        uiZ.v64 = uiB64;\r\n        uiZ.v0  = uiB0;\r\n    }\r\n    uiZ.v64 |= UINT64_C( 0x0000800000000000 );\r\n    return uiZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/8086-SSE/s_propagateNaNF128UI.c ****/\n\n/**** start inlining ../../source/s_eq128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_eq128\r\n#define softfloat_eq128 softfloat_eq128\r\n\r\nbool softfloat_eq128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n\r\n    return (a64 == b64) && (a0 == b0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_eq128.c ****/\n/**** start inlining ../../source/s_le128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_le128\r\n#define softfloat_le128 softfloat_le128\r\n\r\nbool softfloat_le128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n\r\n    return (a64 < b64) || ((a64 == b64) && (a0 <= b0));\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_le128.c ****/\n/**** start inlining ../../source/s_lt128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_lt128\r\n#define softfloat_lt128 softfloat_lt128\r\n\r\nbool softfloat_lt128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n\r\n    return (a64 < b64) || ((a64 == b64) && (a0 < b0));\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_lt128.c ****/\n/**** start inlining ../../source/s_shortShiftLeft128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shortShiftLeft128\r\n#define softfloat_shortShiftLeft128 softfloat_shortShiftLeft128\r\n\r\nstruct uint128\r\n softfloat_shortShiftLeft128( uint64_t a64, uint64_t a0, uint_fast8_t dist )\r\n{\r\n    struct uint128 z;\r\n\r\n    z.v64 = a64<<dist | a0>>(-dist & 63);\r\n    z.v0 = a0<<dist;\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shortShiftLeft128.c ****/\n/**** start inlining ../../source/s_shortShiftRight128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shortShiftRight128\r\n#define softfloat_shortShiftRight128 softfloat_shortShiftRight128\r\n\r\nstruct uint128\r\n softfloat_shortShiftRight128( uint64_t a64, uint64_t a0, uint_fast8_t dist )\r\n{\r\n    struct uint128 z;\r\n\r\n    z.v64 = a64>>dist;\r\n    z.v0 = a64<<(-dist & 63) | a0>>dist;\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shortShiftRight128.c ****/\n/**** start inlining ../../source/s_shortShiftRightJam64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_shortShiftRightJam64\r\n#define softfloat_shortShiftRightJam64 softfloat_shortShiftRightJam64\r\n\r\nuint64_t softfloat_shortShiftRightJam64( uint64_t a, uint_fast8_t dist )\r\n{\r\n\r\n    return a>>dist | ((a & (((uint_fast64_t) 1<<dist) - 1)) != 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shortShiftRightJam64.c ****/\n/**** start inlining ../../source/s_shortShiftRightJam64Extra.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shortShiftRightJam64Extra\r\n#define softfloat_shortShiftRightJam64Extra softfloat_shortShiftRightJam64Extra\r\n\r\nstruct uint64_extra\r\n softfloat_shortShiftRightJam64Extra(\r\n     uint64_t a, uint64_t extra, uint_fast8_t dist )\r\n{\r\n    struct uint64_extra z;\r\n\r\n    z.v = a>>dist;\r\n    z.extra = a<<(-dist & 63) | (extra != 0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shortShiftRightJam64Extra.c ****/\n/**** start inlining ../../source/s_shortShiftRightJam128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shortShiftRightJam128\r\n#define softfloat_shortShiftRightJam128 softfloat_shortShiftRightJam128\r\n\r\nstruct uint128\r\n softfloat_shortShiftRightJam128(\r\n     uint64_t a64, uint64_t a0, uint_fast8_t dist )\r\n{\r\n    uint_fast8_t uNegDist;\r\n    struct uint128 z;\r\n\r\n    uNegDist = -dist;\r\n    z.v64 = a64>>dist;\r\n    z.v0 =\r\n        a64<<(uNegDist & 63) | a0>>dist\r\n            | ((uint64_t) (a0<<(uNegDist & 63)) != 0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shortShiftRightJam128.c ****/\n/**** start inlining ../../source/s_shortShiftRightJam128Extra.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shortShiftRightJam128Extra\r\n#define softfloat_shortShiftRightJam128Extra softfloat_shortShiftRightJam128Extra\r\n\r\nstruct uint128_extra\r\n softfloat_shortShiftRightJam128Extra(\r\n     uint64_t a64, uint64_t a0, uint64_t extra, uint_fast8_t dist )\r\n{\r\n    uint_fast8_t uNegDist;\r\n    struct uint128_extra z;\r\n\r\n    uNegDist = -dist;\r\n    z.v.v64 = a64>>dist;\r\n    z.v.v0 = a64<<(uNegDist & 63) | a0>>dist;\r\n    z.extra = a0<<(uNegDist & 63) | (extra != 0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shortShiftRightJam128Extra.c ****/\n/**** start inlining ../../source/s_shiftRightJam32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_shiftRightJam32\r\n#define softfloat_shiftRightJam32 softfloat_shiftRightJam32\r\n\r\nuint32_t softfloat_shiftRightJam32( uint32_t a, uint_fast16_t dist )\r\n{\r\n\r\n    return\r\n        (dist < 31) ? a>>dist | ((uint32_t) (a<<(-dist & 31)) != 0) : (a != 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shiftRightJam32.c ****/\n/**** start inlining ../../source/s_shiftRightJam64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_shiftRightJam64\r\n#define softfloat_shiftRightJam64 softfloat_shiftRightJam64\r\n\r\nuint64_t softfloat_shiftRightJam64( uint64_t a, uint_fast32_t dist )\r\n{\r\n\r\n    return\r\n        (dist < 63) ? a>>dist | ((uint64_t) (a<<(-dist & 63)) != 0) : (a != 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shiftRightJam64.c ****/\n/**** start inlining ../../source/s_shiftRightJam64Extra.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shiftRightJam64Extra\r\n#define softfloat_shiftRightJam64Extra softfloat_shiftRightJam64Extra\r\n\r\nstruct uint64_extra\r\n softfloat_shiftRightJam64Extra(\r\n     uint64_t a, uint64_t extra, uint_fast32_t dist )\r\n{\r\n    struct uint64_extra z;\r\n\r\n    if ( dist < 64 ) {\r\n        z.v = a>>dist;\r\n        z.extra = a<<(-dist & 63);\r\n    } else {\r\n        z.v = 0;\r\n        z.extra = (dist == 64) ? a : (a != 0);\r\n    }\r\n    z.extra |= (extra != 0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shiftRightJam64Extra.c ****/\n/**** start inlining ../../source/s_shiftRightJam128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shiftRightJam128\r\n\r\nstruct uint128\r\n softfloat_shiftRightJam128( uint64_t a64, uint64_t a0, uint_fast32_t dist )\r\n{\r\n    uint_fast8_t u8NegDist;\r\n    struct uint128 z;\r\n\r\n    if ( dist < 64 ) {\r\n        u8NegDist = -dist;\r\n        z.v64 = a64>>dist;\r\n        z.v0 =\r\n            a64<<(u8NegDist & 63) | a0>>dist\r\n                | ((uint64_t) (a0<<(u8NegDist & 63)) != 0);\r\n    } else {\r\n        z.v64 = 0;\r\n        z.v0 =\r\n            (dist < 127)\r\n                ? a64>>(dist & 63)\r\n                      | (((a64 & (((uint_fast64_t) 1<<(dist & 63)) - 1)) | a0)\r\n                             != 0)\r\n                : ((a64 | a0) != 0);\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shiftRightJam128.c ****/\n/**** start inlining ../../source/s_shiftRightJam128Extra.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shiftRightJam128Extra\r\n\r\nstruct uint128_extra\r\n softfloat_shiftRightJam128Extra(\r\n     uint64_t a64, uint64_t a0, uint64_t extra, uint_fast32_t dist )\r\n{\r\n    uint_fast8_t u8NegDist;\r\n    struct uint128_extra z;\r\n\r\n    u8NegDist = -dist;\r\n    if ( dist < 64 ) {\r\n        z.v.v64 = a64>>dist;\r\n        z.v.v0 = a64<<(u8NegDist & 63) | a0>>dist;\r\n        z.extra = a0<<(u8NegDist & 63);\r\n    } else {\r\n        z.v.v64 = 0;\r\n        if ( dist == 64 ) {\r\n            z.v.v0 = a64;\r\n            z.extra = a0;\r\n        } else {\r\n            extra |= a0;\r\n            if ( dist < 128 ) {\r\n                z.v.v0 = a64>>(dist & 63);\r\n                z.extra = a64<<(u8NegDist & 63);\r\n            } else {\r\n                z.v.v0 = 0;\r\n                z.extra = (dist == 128) ? a64 : (a64 != 0);\r\n            }\r\n        }\r\n    }\r\n    z.extra |= (extra != 0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shiftRightJam128Extra.c ****/\n/**** start inlining ../../source/s_shiftRightJam256M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_shiftRightJam256M\r\n\r\nstatic\r\n void\r\n  softfloat_shortShiftRightJamM(\r\n      uint_fast8_t size_words,\r\n      const uint64_t *aPtr,\r\n      uint_fast8_t dist,\r\n      uint64_t *zPtr\r\n  )\r\n{\r\n    uint_fast8_t uNegDist;\r\n    unsigned int index, lastIndex;\r\n    uint64_t partWordZ, wordA;\r\n\r\n    uNegDist = -dist;\r\n    index = indexWordLo( size_words );\r\n    lastIndex = indexWordHi( size_words );\r\n    wordA = aPtr[index];\r\n    partWordZ = wordA>>dist;\r\n    if ( partWordZ<<dist != wordA ) partWordZ |= 1;\r\n    while ( index != lastIndex ) {\r\n        wordA = aPtr[index + wordIncr];\r\n        zPtr[index] = wordA<<(uNegDist & 63) | partWordZ;\r\n        index += wordIncr;\r\n        partWordZ = wordA>>dist;\r\n    }\r\n    zPtr[index] = partWordZ;\r\n\r\n}\r\n\r\nvoid\r\n softfloat_shiftRightJam256M(\r\n     const uint64_t *aPtr, uint_fast32_t dist, uint64_t *zPtr )\r\n{\r\n    uint64_t wordJam;\r\n    uint_fast32_t wordDist;\r\n    uint64_t *ptr;\r\n    uint_fast8_t i, innerDist;\r\n\r\n    wordJam = 0;\r\n    wordDist = dist>>6;\r\n    if ( wordDist ) {\r\n        if ( 4 < wordDist ) wordDist = 4;\r\n        ptr = (uint64_t *) (aPtr + indexMultiwordLo( 4, wordDist ));\r\n        i = wordDist;\r\n        do {\r\n            wordJam = *ptr++;\r\n            if ( wordJam ) break;\r\n            --i;\r\n        } while ( i );\r\n        ptr = zPtr;\r\n    }\r\n    if ( wordDist < 4 ) {\r\n        aPtr += indexMultiwordHiBut( 4, wordDist );\r\n        innerDist = dist & 63;\r\n        if ( innerDist ) {\r\n            softfloat_shortShiftRightJamM(\r\n                4 - wordDist,\r\n                aPtr,\r\n                innerDist,\r\n                zPtr + indexMultiwordLoBut( 4, wordDist )\r\n            );\r\n            if ( ! wordDist ) goto wordJam;\r\n        } else {\r\n            aPtr += indexWordLo( 4 - wordDist );\r\n            ptr = zPtr + indexWordLo( 4 );\r\n            for ( i = 4 - wordDist; i; --i ) {\r\n                *ptr = *aPtr;\r\n                aPtr += wordIncr;\r\n                ptr += wordIncr;\r\n            }\r\n        }\r\n        ptr = zPtr + indexMultiwordHi( 4, wordDist );\r\n    }\r\n    do {\r\n        *ptr++ = 0;\r\n        --wordDist;\r\n    } while ( wordDist );\r\n wordJam:\r\n    if ( wordJam ) zPtr[indexWordLo( 4 )] |= 1;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_shiftRightJam256M.c ****/\n/**** start inlining ../../source/s_countLeadingZeros8.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n\r\nconst uint_least8_t softfloat_countLeadingZeros8[256] = {\r\n    8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,\r\n    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,\r\n    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\r\n    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\r\n    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r\n    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r\n    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r\n    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r\n    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\r\n};\r\n\r\n/**** ended inlining ../../source/s_countLeadingZeros8.c ****/\n/**** start inlining ../../source/s_countLeadingZeros16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_countLeadingZeros16\r\n\r\n#define softfloat_countLeadingZeros16 softfloat_countLeadingZeros16\r\n/**** skipping file: primitives.h ****/\n\r\nuint_fast8_t softfloat_countLeadingZeros16( uint16_t a )\r\n{\r\n    uint_fast8_t count;\r\n\r\n    count = 8;\r\n    if ( 0x100 <= a ) {\r\n        count = 0;\r\n        a >>= 8;\r\n    }\r\n    count += softfloat_countLeadingZeros8[a];\r\n    return count;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_countLeadingZeros16.c ****/\n/**** start inlining ../../source/s_countLeadingZeros32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_countLeadingZeros32\r\n\r\n#define softfloat_countLeadingZeros32 softfloat_countLeadingZeros32\r\n/**** skipping file: primitives.h ****/\n\r\nuint_fast8_t softfloat_countLeadingZeros32( uint32_t a )\r\n{\r\n    uint_fast8_t count;\r\n\r\n    count = 0;\r\n    if ( a < 0x10000 ) {\r\n        count = 16;\r\n        a <<= 16;\r\n    }\r\n    if ( a < 0x1000000 ) {\r\n        count += 8;\r\n        a <<= 8;\r\n    }\r\n    count += softfloat_countLeadingZeros8[a>>24];\r\n    return count;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_countLeadingZeros32.c ****/\n/**** start inlining ../../source/s_countLeadingZeros64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_countLeadingZeros64\r\n\r\n#define softfloat_countLeadingZeros64 softfloat_countLeadingZeros64\r\n/**** skipping file: primitives.h ****/\n\r\nuint_fast8_t softfloat_countLeadingZeros64( uint64_t a )\r\n{\r\n    uint_fast8_t count;\r\n    uint32_t a32;\r\n\r\n    count = 0;\r\n    a32 = a>>32;\r\n    if ( ! a32 ) {\r\n        count = 32;\r\n        a32 = a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    | From here, result is current count + count leading zeros of `a32'.\r\n    *------------------------------------------------------------------------*/\r\n    if ( a32 < 0x10000 ) {\r\n        count += 16;\r\n        a32 <<= 16;\r\n    }\r\n    if ( a32 < 0x1000000 ) {\r\n        count += 8;\r\n        a32 <<= 8;\r\n    }\r\n    count += softfloat_countLeadingZeros8[a32>>24];\r\n    return count;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_countLeadingZeros64.c ****/\n/**** start inlining ../../source/s_add128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_add128\r\n\r\nstruct uint128\r\n softfloat_add128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n    struct uint128 z;\r\n\r\n    z.v0 = a0 + b0;\r\n    z.v64 = a64 + b64 + (z.v0 < a0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_add128.c ****/\n/**** start inlining ../../source/s_add256M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_add256M\r\n\r\nvoid\r\n softfloat_add256M(\r\n     const uint64_t *aPtr, const uint64_t *bPtr, uint64_t *zPtr )\r\n{\r\n    unsigned int index;\r\n    uint_fast8_t carry;\r\n    uint64_t wordA, wordZ;\r\n\r\n    index = indexWordLo( 4 );\r\n    carry = 0;\r\n    for (;;) {\r\n        wordA = aPtr[index];\r\n        wordZ = wordA + bPtr[index] + carry;\r\n        zPtr[index] = wordZ;\r\n        if ( index == indexWordHi( 4 ) ) break;\r\n        if ( wordZ != wordA ) carry = (wordZ < wordA);\r\n        index += wordIncr;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_add256M.c ****/\n/**** start inlining ../../source/s_sub128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_sub128\r\n#define softfloat_sub128 softfloat_sub128\r\n\r\nstruct uint128\r\n softfloat_sub128( uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0 )\r\n{\r\n    struct uint128 z;\r\n\r\n    z.v0 = a0 - b0;\r\n    z.v64 = a64 - b64 - (a0 < b0);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_sub128.c ****/\n/**** start inlining ../../source/s_sub256M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_sub256M\r\n\r\nvoid\r\n softfloat_sub256M(\r\n     const uint64_t *aPtr, const uint64_t *bPtr, uint64_t *zPtr )\r\n{\r\n    unsigned int index;\r\n    uint_fast8_t borrow;\r\n    uint64_t wordA, wordB;\r\n\r\n    index = indexWordLo( 4 );\r\n    borrow = 0;\r\n    for (;;) {\r\n        wordA = aPtr[index];\r\n        wordB = bPtr[index];\r\n        zPtr[index] = wordA - wordB - borrow;\r\n        if ( index == indexWordHi( 4 ) ) break;\r\n        borrow = borrow ? (wordA <= wordB) : (wordA < wordB);\r\n        index += wordIncr;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_sub256M.c ****/\n/**** start inlining ../../source/s_mul64ByShifted32To128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_mul64ByShifted32To128\r\n\r\nstruct uint128 softfloat_mul64ByShifted32To128( uint64_t a, uint32_t b )\r\n{\r\n    uint_fast64_t mid;\r\n    struct uint128 z;\r\n\r\n    mid = (uint_fast64_t) (uint32_t) a * b;\r\n    z.v0 = mid<<32;\r\n    z.v64 = (uint_fast64_t) (uint32_t) (a>>32) * b + (mid>>32);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_mul64ByShifted32To128.c ****/\n/**** start inlining ../../source/s_mul64To128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_mul64To128\r\n\r\nstruct uint128 softfloat_mul64To128( uint64_t a, uint64_t b )\r\n{\r\n    uint32_t a32, a0, b32, b0;\r\n    struct uint128 z;\r\n    uint64_t mid1, mid;\r\n\r\n    a32 = a>>32;\r\n    a0 = a;\r\n    b32 = b>>32;\r\n    b0 = b;\r\n    z.v0 = (uint_fast64_t) a0 * b0;\r\n    mid1 = (uint_fast64_t) a32 * b0;\r\n    mid = mid1 + (uint_fast64_t) a0 * b32;\r\n    z.v64 = (uint_fast64_t) a32 * b32;\r\n    z.v64 += (uint_fast64_t) (mid < mid1)<<32 | mid>>32;\r\n    mid <<= 32;\r\n    z.v0 += mid;\r\n    z.v64 += (z.v0 < mid);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_mul64To128.c ****/\n/**** start inlining ../../source/s_mul128By32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitiveTypes.h ****/\n\r\n#ifndef softfloat_mul128By32\r\n\r\nstruct uint128 softfloat_mul128By32( uint64_t a64, uint64_t a0, uint32_t b )\r\n{\r\n    struct uint128 z;\r\n    uint_fast64_t mid;\r\n    uint_fast32_t carry;\r\n\r\n    z.v0 = a0 * b;\r\n    mid = (uint_fast64_t) (uint32_t) (a0>>32) * b;\r\n    carry = (uint32_t) ((uint_fast32_t) (z.v0>>32) - (uint_fast32_t) mid);\r\n    z.v64 = a64 * b + (uint_fast32_t) ((mid + carry)>>32);\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_mul128By32.c ****/\n/**** start inlining ../../source/s_mul128To256M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_mul128To256M\r\n\r\n#define softfloat_mul128To256M softfloat_mul128To256M\r\n/**** skipping file: primitives.h ****/\n\r\nvoid\r\n softfloat_mul128To256M(\r\n     uint64_t a64, uint64_t a0, uint64_t b64, uint64_t b0, uint64_t *zPtr )\r\n{\r\n    struct uint128 p0, p64, p128;\r\n    uint_fast64_t z64, z128, z192;\r\n\r\n    p0 = softfloat_mul64To128( a0, b0 );\r\n    zPtr[indexWord( 4, 0 )] = p0.v0;\r\n    p64 = softfloat_mul64To128( a64, b0 );\r\n    z64 = p64.v0 + p0.v64;\r\n    z128 = p64.v64 + (z64 < p64.v0);\r\n    p128 = softfloat_mul64To128( a64, b64 );\r\n    z128 += p128.v0;\r\n    z192 = p128.v64 + (z128 < p128.v0);\r\n    p64 = softfloat_mul64To128( a0, b64 );\r\n    z64 += p64.v0;\r\n    zPtr[indexWord( 4, 1 )] = z64;\r\n    p64.v64 += (z64 < p64.v0);\r\n    z128 += p64.v64;\r\n    zPtr[indexWord( 4, 2 )] = z128;\r\n    zPtr[indexWord( 4, 3 )] = z192 + (z128 < p64.v64);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_mul128To256M.c ****/\n/**** start inlining ../../source/s_approxRecip_1Ks.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n\r\nconst uint16_t softfloat_approxRecip_1k0s[16] = {\r\n    0xFFC4, 0xF0BE, 0xE363, 0xD76F, 0xCCAD, 0xC2F0, 0xBA16, 0xB201,\r\n    0xAA97, 0xA3C6, 0x9D7A, 0x97A6, 0x923C, 0x8D32, 0x887E, 0x8417\r\n};\r\nconst uint16_t softfloat_approxRecip_1k1s[16] = {\r\n    0xF0F1, 0xD62C, 0xBFA1, 0xAC77, 0x9C0A, 0x8DDB, 0x8185, 0x76BA,\r\n    0x6D3B, 0x64D4, 0x5D5C, 0x56B1, 0x50B6, 0x4B55, 0x4679, 0x4211\r\n};\r\n\r\n/**** ended inlining ../../source/s_approxRecip_1Ks.c ****/\n/**** start inlining ../../source/s_approxRecip32_1.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_approxRecip32_1\r\n\r\nextern const uint16_t softfloat_approxRecip_1k0s[16];\r\nextern const uint16_t softfloat_approxRecip_1k1s[16];\r\n\r\nuint32_t softfloat_approxRecip32_1( uint32_t a )\r\n{\r\n    int index;\r\n    uint16_t eps, r0;\r\n    uint32_t sigma0;\r\n    uint_fast32_t r;\r\n    uint32_t sqrSigma0;\r\n\r\n    index = a>>27 & 0xF;\r\n    eps = (uint16_t) (a>>11);\r\n    r0 = softfloat_approxRecip_1k0s[index]\r\n             - ((softfloat_approxRecip_1k1s[index] * (uint_fast32_t) eps)>>20);\r\n    sigma0 = ~(uint_fast32_t) ((r0 * (uint_fast64_t) a)>>7);\r\n    r = ((uint_fast32_t) r0<<16) + ((r0 * (uint_fast64_t) sigma0)>>24);\r\n    sqrSigma0 = ((uint_fast64_t) sigma0 * sigma0)>>32;\r\n    r += ((uint32_t) r * (uint_fast64_t) sqrSigma0)>>48;\r\n    return r;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_approxRecip32_1.c ****/\n/**** start inlining ../../source/s_approxRecipSqrt_1Ks.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: primitives.h ****/\n\r\nconst uint16_t softfloat_approxRecipSqrt_1k0s[16] = {\r\n    0xB4C9, 0xFFAB, 0xAA7D, 0xF11C, 0xA1C5, 0xE4C7, 0x9A43, 0xDA29,\r\n    0x93B5, 0xD0E5, 0x8DED, 0xC8B7, 0x88C6, 0xC16D, 0x8424, 0xBAE1\r\n};\r\nconst uint16_t softfloat_approxRecipSqrt_1k1s[16] = {\r\n    0xA5A5, 0xEA42, 0x8C21, 0xC62D, 0x788F, 0xAA7F, 0x6928, 0x94B6,\r\n    0x5CC7, 0x8335, 0x52A6, 0x74E2, 0x4A3E, 0x68FE, 0x432B, 0x5EFD\r\n};\r\n\r\n/**** ended inlining ../../source/s_approxRecipSqrt_1Ks.c ****/\n/**** start inlining ../../source/s_approxRecipSqrt32_1.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n\r\n#ifndef softfloat_approxRecipSqrt32_1\r\n\r\nextern const uint16_t softfloat_approxRecipSqrt_1k0s[];\r\nextern const uint16_t softfloat_approxRecipSqrt_1k1s[];\r\n\r\nuint32_t softfloat_approxRecipSqrt32_1( unsigned int oddExpA, uint32_t a )\r\n{\r\n    int index;\r\n    uint16_t eps, r0;\r\n    uint_fast32_t ESqrR0;\r\n    uint32_t sigma0;\r\n    uint_fast32_t r;\r\n    uint32_t sqrSigma0;\r\n\r\n    index = (a>>27 & 0xE) + oddExpA;\r\n    eps = (uint16_t) (a>>12);\r\n    r0 = softfloat_approxRecipSqrt_1k0s[index]\r\n             - ((softfloat_approxRecipSqrt_1k1s[index] * (uint_fast32_t) eps)\r\n                    >>20);\r\n    ESqrR0 = (uint_fast32_t) r0 * r0;\r\n    if ( ! oddExpA ) ESqrR0 <<= 1;\r\n    sigma0 = ~(uint_fast32_t) (((uint32_t) ESqrR0 * (uint_fast64_t) a)>>23);\r\n    r = ((uint_fast32_t) r0<<16) + ((r0 * (uint_fast64_t) sigma0)>>25);\r\n    sqrSigma0 = ((uint_fast64_t) sigma0 * sigma0)>>32;\r\n    r += ((uint32_t) ((r>>1) + (r>>3) - ((uint_fast32_t) r0<<14))\r\n              * (uint_fast64_t) sqrSigma0)\r\n             >>48;\r\n    if ( ! (r & 0x80000000) ) r = 0x80000000;\r\n    return r;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_approxRecipSqrt32_1.c ****/\n/**** start inlining ../../source/s_roundToUI32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t\r\n softfloat_roundToUI32(\r\n     bool sign, uint_fast64_t sig, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    uint_fast16_t roundIncrement, roundBits;\r\n    uint_fast32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundIncrement = 0x800;\r\n    if ( \r\n        (roundingMode != softfloat_round_near_maxMag) \r\n            && (roundingMode != softfloat_round_near_even)\r\n    ) {\r\n        roundIncrement = 0;\r\n        if ( sign ) {\r\n            if ( !sig ) return 0;\r\n            if ( roundingMode == softfloat_round_min ) goto invalid;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n            if ( roundingMode == softfloat_round_odd ) goto invalid;\r\n#endif\r\n        } else {\r\n            if ( roundingMode == softfloat_round_max ) roundIncrement = 0xFFF;\r\n        }\r\n    }\r\n    roundBits = sig & 0xFFF;\r\n    sig += roundIncrement;\r\n    if ( sig & UINT64_C( 0xFFFFF00000000000 ) ) goto invalid;\r\n    z = sig>>12;\r\n    if ( \r\n        (roundBits == 0x800) && (roundingMode == softfloat_round_near_even)\r\n    ) {\r\n        z &= ~(uint_fast32_t) 1;\r\n    }\r\n    if ( sign && z ) goto invalid;\r\n    if ( roundBits ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) z |= 1;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundToUI32.c ****/\n/**** start inlining ../../source/s_roundToUI64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t\r\n softfloat_roundToUI64(\r\n     bool sign,\r\n     uint_fast64_t sig,\r\n     uint_fast64_t sigExtra,\r\n     uint_fast8_t roundingMode,\r\n     bool exact\r\n )\r\n{\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if (\r\n        (roundingMode == softfloat_round_near_maxMag)\r\n            || (roundingMode == softfloat_round_near_even)\r\n    ) {\r\n        if ( UINT64_C( 0x8000000000000000 ) <= sigExtra ) goto increment;\r\n    } else {\r\n        if ( sign ) {\r\n            if ( !(sig | sigExtra) ) return 0;\r\n            if ( roundingMode == softfloat_round_min ) goto invalid;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n            if ( roundingMode == softfloat_round_odd ) goto invalid;\r\n#endif\r\n        } else {\r\n            if ( (roundingMode == softfloat_round_max) && sigExtra ) {\r\n increment:\r\n                ++sig;\r\n                if ( !sig ) goto invalid;\r\n                if ( \r\n                    (sigExtra == UINT64_C( 0x8000000000000000 ))\r\n                        && (roundingMode == softfloat_round_near_even)\r\n                ) {\r\n                    sig &= ~(uint_fast64_t) 1;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    if ( sign && sig ) goto invalid;\r\n    if ( sigExtra ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) sig |= 1;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sig;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundToUI64.c ****/\n/**** start inlining ../../source/s_roundToI32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t\r\n softfloat_roundToI32(\r\n     bool sign, uint_fast64_t sig, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    uint_fast16_t roundIncrement, roundBits;\r\n    uint_fast32_t sig32;\r\n    union { uint32_t ui; int32_t i; } uZ;\r\n    int_fast32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundIncrement = 0x800;\r\n    if (\r\n        (roundingMode != softfloat_round_near_maxMag)\r\n            && (roundingMode != softfloat_round_near_even)\r\n    ) {\r\n        roundIncrement = 0;\r\n        if ( \r\n            sign\r\n                ? (roundingMode == softfloat_round_min)\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n                      || (roundingMode == softfloat_round_odd)\r\n#endif\r\n                : (roundingMode == softfloat_round_max)\r\n        ) {\r\n            roundIncrement = 0xFFF;\r\n        }\r\n    }\r\n    roundBits = sig & 0xFFF;\r\n    sig += roundIncrement;\r\n    if ( sig & UINT64_C( 0xFFFFF00000000000 ) ) goto invalid;\r\n    sig32 = sig>>12;\r\n    if (\r\n        (roundBits == 0x800) && (roundingMode == softfloat_round_near_even)\r\n    ) {\r\n        sig32 &= ~(uint_fast32_t) 1;\r\n    }\r\n    uZ.ui = sign ? -sig32 : sig32;\r\n    z = uZ.i;\r\n    if ( z && ((z < 0) ^ sign) ) goto invalid;\r\n    if ( roundBits ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) z |= 1;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundToI32.c ****/\n/**** start inlining ../../source/s_roundToI64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t\r\n softfloat_roundToI64(\r\n     bool sign,\r\n     uint_fast64_t sig,\r\n     uint_fast64_t sigExtra,\r\n     uint_fast8_t roundingMode,\r\n     bool exact\r\n )\r\n{\r\n    union { uint64_t ui; int64_t i; } uZ;\r\n    int_fast64_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if (\r\n        (roundingMode == softfloat_round_near_maxMag)\r\n            || (roundingMode == softfloat_round_near_even)\r\n    ) {\r\n        if ( UINT64_C( 0x8000000000000000 ) <= sigExtra ) goto increment;\r\n    } else {\r\n        if (\r\n            sigExtra\r\n                && (sign\r\n                        ? (roundingMode == softfloat_round_min)\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n                              || (roundingMode == softfloat_round_odd)\r\n#endif\r\n                        : (roundingMode == softfloat_round_max))\r\n        ) {\r\n increment:\r\n            ++sig;\r\n            if ( !sig ) goto invalid;\r\n            if (\r\n                (sigExtra == UINT64_C( 0x8000000000000000 ))\r\n                    && (roundingMode == softfloat_round_near_even)\r\n            ) {\r\n                sig &= ~(uint_fast64_t) 1;\r\n            }\r\n        }\r\n    }\r\n    uZ.ui = sign ? -sig : sig;\r\n    z = uZ.i;\r\n    if ( z && ((z < 0) ^ sign) ) goto invalid;\r\n    if ( sigExtra ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) z |= 1;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundToI64.c ****/\n/**** start inlining ../../source/s_normSubnormalF16Sig.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nstruct exp8_sig16 softfloat_normSubnormalF16Sig( uint_fast16_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct exp8_sig16 z;\r\n\r\n    shiftDist = softfloat_countLeadingZeros16( sig ) - 5;\r\n    z.exp = 1 - shiftDist;\r\n    z.sig = sig<<shiftDist;\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normSubnormalF16Sig.c ****/\n/**** start inlining ../../source/s_roundPackToF16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t\r\n softfloat_roundPackToF16( bool sign, int_fast16_t exp, uint_fast16_t sig )\r\n{\r\n    uint_fast8_t roundingMode;\r\n    bool roundNearEven;\r\n    uint_fast8_t roundIncrement, roundBits;\r\n    bool isTiny;\r\n    uint_fast16_t uiZ;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundingMode = softfloat_roundingMode;\r\n    roundNearEven = (roundingMode == softfloat_round_near_even);\r\n    roundIncrement = 0x8;\r\n    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {\r\n        roundIncrement =\r\n            (roundingMode\r\n                 == (sign ? softfloat_round_min : softfloat_round_max))\r\n                ? 0xF\r\n                : 0;\r\n    }\r\n    roundBits = sig & 0xF;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x1D <= (unsigned int) exp ) {\r\n        if ( exp < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            isTiny =\r\n                (softfloat_detectTininess == softfloat_tininess_beforeRounding)\r\n                    || (exp < -1) || (sig + roundIncrement < 0x8000);\r\n            sig = softfloat_shiftRightJam32( sig, -exp );\r\n            exp = 0;\r\n            roundBits = sig & 0xF;\r\n            if ( isTiny && roundBits ) {\r\n                softfloat_raiseFlags( softfloat_flag_underflow );\r\n            }\r\n        } else if ( (0x1D < exp) || (0x8000 <= sig + roundIncrement) ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            softfloat_raiseFlags(\r\n                softfloat_flag_overflow | softfloat_flag_inexact );\r\n            uiZ = packToF16UI( sign, 0x1F, 0 ) - ! roundIncrement;\r\n            goto uiZ;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = (sig + roundIncrement)>>4;\r\n    if ( roundBits ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            sig |= 1;\r\n            goto packReturn;\r\n        }\r\n#endif\r\n    }\r\n    sig &= ~(uint_fast16_t) (! (roundBits ^ 8) & roundNearEven);\r\n    if ( ! sig ) exp = 0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n packReturn:\r\n    uiZ = packToF16UI( sign, exp, sig );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundPackToF16.c ****/\n/**** start inlining ../../source/s_normRoundPackToF16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nfloat16_t\r\n softfloat_normRoundPackToF16( bool sign, int_fast16_t exp, uint_fast16_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    union ui16_f16 uZ;\r\n\r\n    shiftDist = softfloat_countLeadingZeros16( sig ) - 1;\r\n    exp -= shiftDist;\r\n    if ( (4 <= shiftDist) && ((unsigned int) exp < 0x1D) ) {\r\n        uZ.ui = packToF16UI( sign, sig ? exp : 0, sig<<(shiftDist - 4) );\r\n        return uZ.f;\r\n    } else {\r\n        return softfloat_roundPackToF16( sign, exp, sig<<shiftDist );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normRoundPackToF16.c ****/\n/**** start inlining ../../source/s_addMagsF16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t softfloat_addMagsF16( uint_fast16_t uiA, uint_fast16_t uiB )\r\n{\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA;\r\n    int_fast8_t expB;\r\n    uint_fast16_t sigB;\r\n    int_fast8_t expDiff;\r\n    uint_fast16_t uiZ;\r\n    bool signZ;\r\n    int_fast8_t expZ;\r\n    uint_fast16_t sigZ;\r\n    uint_fast16_t sigX, sigY;\r\n    int_fast8_t shiftDist;\r\n    uint_fast32_t sig32Z;\r\n    int_fast8_t roundingMode;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expF16UI( uiA );\r\n    sigA = fracF16UI( uiA );\r\n    expB = expF16UI( uiB );\r\n    sigB = fracF16UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! expA ) {\r\n            uiZ = uiA + sigB;\r\n            goto uiZ;\r\n        }\r\n        if ( expA == 0x1F ) {\r\n            if ( sigA | sigB ) goto propagateNaN;\r\n            uiZ = uiA;\r\n            goto uiZ;\r\n        }\r\n        signZ = signF16UI( uiA );\r\n        expZ = expA;\r\n        sigZ = 0x0800 + sigA + sigB;\r\n        if ( ! (sigZ & 1) && (expZ < 0x1E) ) {\r\n            sigZ >>= 1;\r\n            goto pack;\r\n        }\r\n        sigZ <<= 3;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        signZ = signF16UI( uiA );\r\n        if ( expDiff < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            if ( expB == 0x1F ) {\r\n                if ( sigB ) goto propagateNaN;\r\n                uiZ = packToF16UI( signZ, 0x1F, 0 );\r\n                goto uiZ;\r\n            }\r\n            if ( expDiff <= -13 ) {\r\n                uiZ = packToF16UI( signZ, expB, sigB );\r\n                if ( expA | sigA ) goto addEpsilon;\r\n                goto uiZ;\r\n            }\r\n            expZ = expB;\r\n            sigX = sigB | 0x0400;\r\n            sigY = sigA + (expA ? 0x0400 : sigA);\r\n            shiftDist = 19 + expDiff;\r\n        } else {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            uiZ = uiA;\r\n            if ( expA == 0x1F ) {\r\n                if ( sigA ) goto propagateNaN;\r\n                goto uiZ;\r\n            }\r\n            if ( 13 <= expDiff ) {\r\n                if ( expB | sigB ) goto addEpsilon;\r\n                goto uiZ;\r\n            }\r\n            expZ = expA;\r\n            sigX = sigA | 0x0400;\r\n            sigY = sigB + (expB ? 0x0400 : sigB);\r\n            shiftDist = 19 - expDiff;\r\n        }\r\n        sig32Z =\r\n            ((uint_fast32_t) sigX<<19) + ((uint_fast32_t) sigY<<shiftDist);\r\n        if ( sig32Z < 0x40000000 ) {\r\n            --expZ;\r\n            sig32Z <<= 1;\r\n        }\r\n        sigZ = sig32Z>>16;\r\n        if ( sig32Z & 0xFFFF ) {\r\n            sigZ |= 1;\r\n        } else {\r\n            if ( ! (sigZ & 0xF) && (expZ < 0x1E) ) {\r\n                sigZ >>= 4;\r\n                goto pack;\r\n            }\r\n        }\r\n    }\r\n    return softfloat_roundPackToF16( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n addEpsilon:\r\n    roundingMode = softfloat_roundingMode;\r\n    if ( roundingMode != softfloat_round_near_even ) {\r\n        if (\r\n            roundingMode\r\n                == (signF16UI( uiZ ) ? softfloat_round_min\r\n                        : softfloat_round_max)\r\n        ) {\r\n            ++uiZ;\r\n            if ( (uint16_t) (uiZ<<1) == 0xF800 ) {\r\n                softfloat_raiseFlags(\r\n                    softfloat_flag_overflow | softfloat_flag_inexact );\r\n            }\r\n        }\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        else if ( roundingMode == softfloat_round_odd ) {\r\n            uiZ |= 1;\r\n        }\r\n#endif\r\n    }\r\n    softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n pack:\r\n    uiZ = packToF16UI( signZ, expZ, sigZ );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_addMagsF16.c ****/\n/**** start inlining ../../source/s_subMagsF16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t softfloat_subMagsF16( uint_fast16_t uiA, uint_fast16_t uiB )\r\n{\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA;\r\n    int_fast8_t expB;\r\n    uint_fast16_t sigB;\r\n    int_fast8_t expDiff;\r\n    uint_fast16_t uiZ;\r\n    int_fast16_t sigDiff;\r\n    bool signZ;\r\n    int_fast8_t shiftDist, expZ;\r\n    uint_fast16_t sigZ, sigX, sigY;\r\n    uint_fast32_t sig32Z;\r\n    int_fast8_t roundingMode;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expF16UI( uiA );\r\n    sigA = fracF16UI( uiA );\r\n    expB = expF16UI( uiB );\r\n    sigB = fracF16UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expA == 0x1F ) {\r\n            if ( sigA | sigB ) goto propagateNaN;\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            uiZ = defaultNaNF16UI;\r\n            goto uiZ;\r\n        }\r\n        sigDiff = sigA - sigB;\r\n        if ( ! sigDiff ) {\r\n            uiZ =\r\n                packToF16UI(\r\n                    (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( expA ) --expA;\r\n        signZ = signF16UI( uiA );\r\n        if ( sigDiff < 0 ) {\r\n            signZ = ! signZ;\r\n            sigDiff = -sigDiff;\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros16( sigDiff ) - 5;\r\n        expZ = expA - shiftDist;\r\n        if ( expZ < 0 ) {\r\n            shiftDist = expA;\r\n            expZ = 0;\r\n        }\r\n        sigZ = sigDiff<<shiftDist;\r\n        goto pack;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        signZ = signF16UI( uiA );\r\n        if ( expDiff < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            signZ = ! signZ;\r\n            if ( expB == 0x1F ) {\r\n                if ( sigB ) goto propagateNaN;\r\n                uiZ = packToF16UI( signZ, 0x1F, 0 );\r\n                goto uiZ;\r\n            }\r\n            if ( expDiff <= -13 ) {\r\n                uiZ = packToF16UI( signZ, expB, sigB );\r\n                if ( expA | sigA ) goto subEpsilon;\r\n                goto uiZ;\r\n            }\r\n            expZ = expA + 19;\r\n            sigX = sigB | 0x0400;\r\n            sigY = sigA + (expA ? 0x0400 : sigA);\r\n            expDiff = -expDiff;\r\n        } else {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            uiZ = uiA;\r\n            if ( expA == 0x1F ) {\r\n                if ( sigA ) goto propagateNaN;\r\n                goto uiZ;\r\n            }\r\n            if ( 13 <= expDiff ) {\r\n                if ( expB | sigB ) goto subEpsilon;\r\n                goto uiZ;\r\n            }\r\n            expZ = expB + 19;\r\n            sigX = sigA | 0x0400;\r\n            sigY = sigB + (expB ? 0x0400 : sigB);\r\n        }\r\n        sig32Z = ((uint_fast32_t) sigX<<expDiff) - sigY;\r\n        shiftDist = softfloat_countLeadingZeros32( sig32Z ) - 1;\r\n        sig32Z <<= shiftDist;\r\n        expZ -= shiftDist;\r\n        sigZ = sig32Z>>16;\r\n        if ( sig32Z & 0xFFFF ) {\r\n            sigZ |= 1;\r\n        } else {\r\n            if ( ! (sigZ & 0xF) && ((unsigned int) expZ < 0x1E) ) {\r\n                sigZ >>= 4;\r\n                goto pack;\r\n            }\r\n        }\r\n        return softfloat_roundPackToF16( signZ, expZ, sigZ );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n subEpsilon:\r\n    roundingMode = softfloat_roundingMode;\r\n    if ( roundingMode != softfloat_round_near_even ) {\r\n        if (\r\n            (roundingMode == softfloat_round_minMag)\r\n                || (roundingMode\r\n                        == (signF16UI( uiZ ) ? softfloat_round_max\r\n                                : softfloat_round_min))\r\n        ) {\r\n            --uiZ;\r\n        }\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        else if ( roundingMode == softfloat_round_odd ) {\r\n            uiZ = (uiZ - 1) | 1;\r\n        }\r\n#endif\r\n    }\r\n    softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n pack:\r\n    uiZ = packToF16UI( signZ, expZ, sigZ );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_subMagsF16.c ****/\n/**** start inlining ../../source/s_mulAddF16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t\r\n softfloat_mulAddF16(\r\n     uint_fast16_t uiA, uint_fast16_t uiB, uint_fast16_t uiC, uint_fast8_t op )\r\n{\r\n    bool signA;\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA;\r\n    bool signB;\r\n    int_fast8_t expB;\r\n    uint_fast16_t sigB;\r\n    bool signC;\r\n    int_fast8_t expC;\r\n    uint_fast16_t sigC;\r\n    bool signProd;\r\n    uint_fast16_t magBits, uiZ;\r\n    struct exp8_sig16 normExpSig;\r\n    int_fast8_t expProd;\r\n    uint_fast32_t sigProd;\r\n    bool signZ;\r\n    int_fast8_t expZ;\r\n    uint_fast16_t sigZ;\r\n    int_fast8_t expDiff;\r\n    uint_fast32_t sig32Z, sig32C;\r\n    int_fast8_t shiftDist;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signF16UI( uiA );\r\n    expA  = expF16UI( uiA );\r\n    sigA  = fracF16UI( uiA );\r\n    signB = signF16UI( uiB );\r\n    expB  = expF16UI( uiB );\r\n    sigB  = fracF16UI( uiB );\r\n    signC = signF16UI( uiC ) ^ (op == softfloat_mulAdd_subC);\r\n    expC  = expF16UI( uiC );\r\n    sigC  = fracF16UI( uiC );\r\n    signProd = signA ^ signB ^ (op == softfloat_mulAdd_subProd);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x1F ) {\r\n        if ( sigA || ((expB == 0x1F) && sigB) ) goto propagateNaN_ABC;\r\n        magBits = expB | sigB;\r\n        goto infProdArg;\r\n    }\r\n    if ( expB == 0x1F ) {\r\n        if ( sigB ) goto propagateNaN_ABC;\r\n        magBits = expA | sigA;\r\n        goto infProdArg;\r\n    }\r\n    if ( expC == 0x1F ) {\r\n        if ( sigC ) {\r\n            uiZ = 0;\r\n            goto propagateNaN_ZC;\r\n        }\r\n        uiZ = uiC;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expProd = expA + expB - 0xE;\r\n    sigA = (sigA | 0x0400)<<4;\r\n    sigB = (sigB | 0x0400)<<4;\r\n    sigProd = (uint_fast32_t) sigA * sigB;\r\n    if ( sigProd < 0x20000000 ) {\r\n        --expProd;\r\n        sigProd <<= 1;\r\n    }\r\n    signZ = signProd;\r\n    if ( ! expC ) {\r\n        if ( ! sigC ) {\r\n            expZ = expProd - 1;\r\n            sigZ = sigProd>>15 | ((sigProd & 0x7FFF) != 0);\r\n            goto roundPack;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigC );\r\n        expC = normExpSig.exp;\r\n        sigC = normExpSig.sig;\r\n    }\r\n    sigC = (sigC | 0x0400)<<3;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expProd - expC;\r\n    if ( signProd == signC ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff <= 0 ) {\r\n            expZ = expC;\r\n            sigZ = sigC + softfloat_shiftRightJam32( sigProd, 16 - expDiff );\r\n        } else {\r\n            expZ = expProd;\r\n            sig32Z =\r\n                sigProd\r\n                    + softfloat_shiftRightJam32(\r\n                          (uint_fast32_t) sigC<<16, expDiff );\r\n            sigZ = sig32Z>>16 | ((sig32Z & 0xFFFF) != 0 );\r\n        }\r\n        if ( sigZ < 0x4000 ) {\r\n            --expZ;\r\n            sigZ <<= 1;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig32C = (uint_fast32_t) sigC<<16;\r\n        if ( expDiff < 0 ) {\r\n            signZ = signC;\r\n            expZ = expC;\r\n            sig32Z = sig32C - softfloat_shiftRightJam32( sigProd, -expDiff );\r\n        } else if ( ! expDiff ) {\r\n            expZ = expProd;\r\n            sig32Z = sigProd - sig32C;\r\n            if ( ! sig32Z ) goto completeCancellation;\r\n            if ( sig32Z & 0x80000000 ) {\r\n                signZ = ! signZ;\r\n                sig32Z = -sig32Z;\r\n            }\r\n        } else {\r\n            expZ = expProd;\r\n            sig32Z = sigProd - softfloat_shiftRightJam32( sig32C, expDiff );\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros32( sig32Z ) - 1;\r\n        expZ -= shiftDist;\r\n        shiftDist -= 16;\r\n        if ( shiftDist < 0 ) {\r\n            sigZ =\r\n                sig32Z>>(-shiftDist)\r\n                    | ((uint32_t) (sig32Z<<(shiftDist & 31)) != 0);\r\n        } else {\r\n            sigZ = (uint_fast16_t) sig32Z<<shiftDist;\r\n        }\r\n    }\r\n roundPack:\r\n    return softfloat_roundPackToF16( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN_ABC:\r\n    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );\r\n    goto propagateNaN_ZC;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infProdArg:\r\n    if ( magBits ) {\r\n        uiZ = packToF16UI( signProd, 0x1F, 0 );\r\n        if ( expC != 0x1F ) goto uiZ;\r\n        if ( sigC ) goto propagateNaN_ZC;\r\n        if ( signProd == signC ) goto uiZ;\r\n    }\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF16UI;\r\n propagateNaN_ZC:\r\n    uiZ = softfloat_propagateNaNF16UI( uiZ, uiC );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zeroProd:\r\n    uiZ = uiC;\r\n    if ( ! (expC | sigC) && (signProd != signC) ) {\r\n completeCancellation:\r\n        uiZ =\r\n            packToF16UI(\r\n                (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_mulAddF16.c ****/\n/**** start inlining ../../source/s_normSubnormalF32Sig.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nstruct exp16_sig32 softfloat_normSubnormalF32Sig( uint_fast32_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct exp16_sig32 z;\r\n\r\n    shiftDist = softfloat_countLeadingZeros32( sig ) - 8;\r\n    z.exp = 1 - shiftDist;\r\n    z.sig = sig<<shiftDist;\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normSubnormalF32Sig.c ****/\n/**** start inlining ../../source/s_roundPackToF32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t\r\n softfloat_roundPackToF32( bool sign, int_fast16_t exp, uint_fast32_t sig )\r\n{\r\n    uint_fast8_t roundingMode;\r\n    bool roundNearEven;\r\n    uint_fast8_t roundIncrement, roundBits;\r\n    bool isTiny;\r\n    uint_fast32_t uiZ;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundingMode = softfloat_roundingMode;\r\n    roundNearEven = (roundingMode == softfloat_round_near_even);\r\n    roundIncrement = 0x40;\r\n    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {\r\n        roundIncrement =\r\n            (roundingMode\r\n                 == (sign ? softfloat_round_min : softfloat_round_max))\r\n                ? 0x7F\r\n                : 0;\r\n    }\r\n    roundBits = sig & 0x7F;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0xFD <= (unsigned int) exp ) {\r\n        if ( exp < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            isTiny =\r\n                (softfloat_detectTininess == softfloat_tininess_beforeRounding)\r\n                    || (exp < -1) || (sig + roundIncrement < 0x80000000);\r\n            sig = softfloat_shiftRightJam32( sig, -exp );\r\n            exp = 0;\r\n            roundBits = sig & 0x7F;\r\n            if ( isTiny && roundBits ) {\r\n                softfloat_raiseFlags( softfloat_flag_underflow );\r\n            }\r\n        } else if ( (0xFD < exp) || (0x80000000 <= sig + roundIncrement) ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            softfloat_raiseFlags(\r\n                softfloat_flag_overflow | softfloat_flag_inexact );\r\n            uiZ = packToF32UI( sign, 0xFF, 0 ) - ! roundIncrement;\r\n            goto uiZ;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = (sig + roundIncrement)>>7;\r\n    if ( roundBits ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            sig |= 1;\r\n            goto packReturn;\r\n        }\r\n#endif\r\n    }\r\n    sig &= ~(uint_fast32_t) (! (roundBits ^ 0x40) & roundNearEven);\r\n    if ( ! sig ) exp = 0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n packReturn:\r\n    uiZ = packToF32UI( sign, exp, sig );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundPackToF32.c ****/\n/**** start inlining ../../source/s_normRoundPackToF32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nfloat32_t\r\n softfloat_normRoundPackToF32( bool sign, int_fast16_t exp, uint_fast32_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    union ui32_f32 uZ;\r\n\r\n    shiftDist = softfloat_countLeadingZeros32( sig ) - 1;\r\n    exp -= shiftDist;\r\n    if ( (7 <= shiftDist) && ((unsigned int) exp < 0xFD) ) {\r\n        uZ.ui = packToF32UI( sign, sig ? exp : 0, sig<<(shiftDist - 7) );\r\n        return uZ.f;\r\n    } else {\r\n        return softfloat_roundPackToF32( sign, exp, sig<<shiftDist );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normRoundPackToF32.c ****/\n/**** start inlining ../../source/s_addMagsF32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n\r\nfloat32_t softfloat_addMagsF32( uint_fast32_t uiA, uint_fast32_t uiB )\r\n{\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA;\r\n    int_fast16_t expB;\r\n    uint_fast32_t sigB;\r\n    int_fast16_t expDiff;\r\n    uint_fast32_t uiZ;\r\n    bool signZ;\r\n    int_fast16_t expZ;\r\n    uint_fast32_t sigZ;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expF32UI( uiA );\r\n    sigA = fracF32UI( uiA );\r\n    expB = expF32UI( uiB );\r\n    sigB = fracF32UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! expA ) {\r\n            uiZ = uiA + sigB;\r\n            goto uiZ;\r\n        }\r\n        if ( expA == 0xFF ) {\r\n            if ( sigA | sigB ) goto propagateNaN;\r\n            uiZ = uiA;\r\n            goto uiZ;\r\n        }\r\n        signZ = signF32UI( uiA );\r\n        expZ = expA;\r\n        sigZ = 0x01000000 + sigA + sigB;\r\n        if ( ! (sigZ & 1) && (expZ < 0xFE) ) {\r\n            uiZ = packToF32UI( signZ, expZ, sigZ>>1 );\r\n            goto uiZ;\r\n        }\r\n        sigZ <<= 6;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        signZ = signF32UI( uiA );\r\n        sigA <<= 6;\r\n        sigB <<= 6;\r\n        if ( expDiff < 0 ) {\r\n            if ( expB == 0xFF ) {\r\n                if ( sigB ) goto propagateNaN;\r\n                uiZ = packToF32UI( signZ, 0xFF, 0 );\r\n                goto uiZ;\r\n            }\r\n            expZ = expB;\r\n            sigA += expA ? 0x20000000 : sigA;\r\n            sigA = softfloat_shiftRightJam32( sigA, -expDiff );\r\n        } else {\r\n            if ( expA == 0xFF ) {\r\n                if ( sigA ) goto propagateNaN;\r\n                uiZ = uiA;\r\n                goto uiZ;\r\n            }\r\n            expZ = expA;\r\n            sigB += expB ? 0x20000000 : sigB;\r\n            sigB = softfloat_shiftRightJam32( sigB, expDiff );\r\n        }\r\n        sigZ = 0x20000000 + sigA + sigB;\r\n        if ( sigZ < 0x40000000 ) {\r\n            --expZ;\r\n            sigZ <<= 1;\r\n        }\r\n    }\r\n    return softfloat_roundPackToF32( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF32UI( uiA, uiB );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_addMagsF32.c ****/\n/**** start inlining ../../source/s_subMagsF32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t softfloat_subMagsF32( uint_fast32_t uiA, uint_fast32_t uiB )\r\n{\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA;\r\n    int_fast16_t expB;\r\n    uint_fast32_t sigB;\r\n    int_fast16_t expDiff;\r\n    uint_fast32_t uiZ;\r\n    int_fast32_t sigDiff;\r\n    bool signZ;\r\n    int_fast8_t shiftDist;\r\n    int_fast16_t expZ;\r\n    uint_fast32_t sigX, sigY;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expF32UI( uiA );\r\n    sigA = fracF32UI( uiA );\r\n    expB = expF32UI( uiB );\r\n    sigB = fracF32UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expA == 0xFF ) {\r\n            if ( sigA | sigB ) goto propagateNaN;\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            uiZ = defaultNaNF32UI;\r\n            goto uiZ;\r\n        }\r\n        sigDiff = sigA - sigB;\r\n        if ( ! sigDiff ) {\r\n            uiZ =\r\n                packToF32UI(\r\n                    (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( expA ) --expA;\r\n        signZ = signF32UI( uiA );\r\n        if ( sigDiff < 0 ) {\r\n            signZ = ! signZ;\r\n            sigDiff = -sigDiff;\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros32( sigDiff ) - 8;\r\n        expZ = expA - shiftDist;\r\n        if ( expZ < 0 ) {\r\n            shiftDist = expA;\r\n            expZ = 0;\r\n        }\r\n        uiZ = packToF32UI( signZ, expZ, sigDiff<<shiftDist );\r\n        goto uiZ;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        signZ = signF32UI( uiA );\r\n        sigA <<= 7;\r\n        sigB <<= 7;\r\n        if ( expDiff < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            signZ = ! signZ;\r\n            if ( expB == 0xFF ) {\r\n                if ( sigB ) goto propagateNaN;\r\n                uiZ = packToF32UI( signZ, 0xFF, 0 );\r\n                goto uiZ;\r\n            }\r\n            expZ = expB - 1;\r\n            sigX = sigB | 0x40000000;\r\n            sigY = sigA + (expA ? 0x40000000 : sigA);\r\n            expDiff = -expDiff;\r\n        } else {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            if ( expA == 0xFF ) {\r\n                if ( sigA ) goto propagateNaN;\r\n                uiZ = uiA;\r\n                goto uiZ;\r\n            }\r\n            expZ = expA - 1;\r\n            sigX = sigA | 0x40000000;\r\n            sigY = sigB + (expB ? 0x40000000 : sigB);\r\n        }\r\n        return\r\n            softfloat_normRoundPackToF32(\r\n                signZ, expZ, sigX - softfloat_shiftRightJam32( sigY, expDiff )\r\n            );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF32UI( uiA, uiB );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_subMagsF32.c ****/\n/**** start inlining ../../source/s_mulAddF32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t\r\n softfloat_mulAddF32(\r\n     uint_fast32_t uiA, uint_fast32_t uiB, uint_fast32_t uiC, uint_fast8_t op )\r\n{\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint_fast32_t sigB;\r\n    bool signC;\r\n    int_fast16_t expC;\r\n    uint_fast32_t sigC;\r\n    bool signProd;\r\n    uint_fast32_t magBits, uiZ;\r\n    struct exp16_sig32 normExpSig;\r\n    int_fast16_t expProd;\r\n    uint_fast64_t sigProd;\r\n    bool signZ;\r\n    int_fast16_t expZ;\r\n    uint_fast32_t sigZ;\r\n    int_fast16_t expDiff;\r\n    uint_fast64_t sig64Z, sig64C;\r\n    int_fast8_t shiftDist;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signF32UI( uiA );\r\n    expA  = expF32UI( uiA );\r\n    sigA  = fracF32UI( uiA );\r\n    signB = signF32UI( uiB );\r\n    expB  = expF32UI( uiB );\r\n    sigB  = fracF32UI( uiB );\r\n    signC = signF32UI( uiC ) ^ (op == softfloat_mulAdd_subC);\r\n    expC  = expF32UI( uiC );\r\n    sigC  = fracF32UI( uiC );\r\n    signProd = signA ^ signB ^ (op == softfloat_mulAdd_subProd);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0xFF ) {\r\n        if ( sigA || ((expB == 0xFF) && sigB) ) goto propagateNaN_ABC;\r\n        magBits = expB | sigB;\r\n        goto infProdArg;\r\n    }\r\n    if ( expB == 0xFF ) {\r\n        if ( sigB ) goto propagateNaN_ABC;\r\n        magBits = expA | sigA;\r\n        goto infProdArg;\r\n    }\r\n    if ( expC == 0xFF ) {\r\n        if ( sigC ) {\r\n            uiZ = 0;\r\n            goto propagateNaN_ZC;\r\n        }\r\n        uiZ = uiC;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expProd = expA + expB - 0x7E;\r\n    sigA = (sigA | 0x00800000)<<7;\r\n    sigB = (sigB | 0x00800000)<<7;\r\n    sigProd = (uint_fast64_t) sigA * sigB;\r\n    if ( sigProd < UINT64_C( 0x2000000000000000 ) ) {\r\n        --expProd;\r\n        sigProd <<= 1;\r\n    }\r\n    signZ = signProd;\r\n    if ( ! expC ) {\r\n        if ( ! sigC ) {\r\n            expZ = expProd - 1;\r\n            sigZ = softfloat_shortShiftRightJam64( sigProd, 31 );\r\n            goto roundPack;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigC );\r\n        expC = normExpSig.exp;\r\n        sigC = normExpSig.sig;\r\n    }\r\n    sigC = (sigC | 0x00800000)<<6;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expProd - expC;\r\n    if ( signProd == signC ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff <= 0 ) {\r\n            expZ = expC;\r\n            sigZ = sigC + softfloat_shiftRightJam64( sigProd, 32 - expDiff );\r\n        } else {\r\n            expZ = expProd;\r\n            sig64Z =\r\n                sigProd\r\n                    + softfloat_shiftRightJam64(\r\n                          (uint_fast64_t) sigC<<32, expDiff );\r\n            sigZ = softfloat_shortShiftRightJam64( sig64Z, 32 );\r\n        }\r\n        if ( sigZ < 0x40000000 ) {\r\n            --expZ;\r\n            sigZ <<= 1;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig64C = (uint_fast64_t) sigC<<32;\r\n        if ( expDiff < 0 ) {\r\n            signZ = signC;\r\n            expZ = expC;\r\n            sig64Z = sig64C - softfloat_shiftRightJam64( sigProd, -expDiff );\r\n        } else if ( ! expDiff ) {\r\n            expZ = expProd;\r\n            sig64Z = sigProd - sig64C;\r\n            if ( ! sig64Z ) goto completeCancellation;\r\n            if ( sig64Z & UINT64_C( 0x8000000000000000 ) ) {\r\n                signZ = ! signZ;\r\n                sig64Z = -sig64Z;\r\n            }\r\n        } else {\r\n            expZ = expProd;\r\n            sig64Z = sigProd - softfloat_shiftRightJam64( sig64C, expDiff );\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros64( sig64Z ) - 1;\r\n        expZ -= shiftDist;\r\n        shiftDist -= 32;\r\n        if ( shiftDist < 0 ) {\r\n            sigZ = softfloat_shortShiftRightJam64( sig64Z, -shiftDist );\r\n        } else {\r\n            sigZ = (uint_fast32_t) sig64Z<<shiftDist;\r\n        }\r\n    }\r\n roundPack:\r\n    return softfloat_roundPackToF32( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN_ABC:\r\n    uiZ = softfloat_propagateNaNF32UI( uiA, uiB );\r\n    goto propagateNaN_ZC;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infProdArg:\r\n    if ( magBits ) {\r\n        uiZ = packToF32UI( signProd, 0xFF, 0 );\r\n        if ( expC != 0xFF ) goto uiZ;\r\n        if ( sigC ) goto propagateNaN_ZC;\r\n        if ( signProd == signC ) goto uiZ;\r\n    }\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF32UI;\r\n propagateNaN_ZC:\r\n    uiZ = softfloat_propagateNaNF32UI( uiZ, uiC );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zeroProd:\r\n    uiZ = uiC;\r\n    if ( ! (expC | sigC) && (signProd != signC) ) {\r\n completeCancellation:\r\n        uiZ =\r\n            packToF32UI(\r\n                (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_mulAddF32.c ****/\n/**** start inlining ../../source/s_normSubnormalF64Sig.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nstruct exp16_sig64 softfloat_normSubnormalF64Sig( uint_fast64_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct exp16_sig64 z;\r\n\r\n    shiftDist = softfloat_countLeadingZeros64( sig ) - 11;\r\n    z.exp = 1 - shiftDist;\r\n    z.sig = sig<<shiftDist;\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normSubnormalF64Sig.c ****/\n/**** start inlining ../../source/s_roundPackToF64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t\r\n softfloat_roundPackToF64( bool sign, int_fast16_t exp, uint_fast64_t sig )\r\n{\r\n    uint_fast8_t roundingMode;\r\n    bool roundNearEven;\r\n    uint_fast16_t roundIncrement, roundBits;\r\n    bool isTiny;\r\n    uint_fast64_t uiZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundingMode = softfloat_roundingMode;\r\n    roundNearEven = (roundingMode == softfloat_round_near_even);\r\n    roundIncrement = 0x200;\r\n    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {\r\n        roundIncrement =\r\n            (roundingMode\r\n                 == (sign ? softfloat_round_min : softfloat_round_max))\r\n                ? 0x3FF\r\n                : 0;\r\n    }\r\n    roundBits = sig & 0x3FF;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x7FD <= (uint16_t) exp ) {\r\n        if ( exp < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            isTiny =\r\n                (softfloat_detectTininess == softfloat_tininess_beforeRounding)\r\n                    || (exp < -1)\r\n                    || (sig + roundIncrement < UINT64_C( 0x8000000000000000 ));\r\n            sig = softfloat_shiftRightJam64( sig, -exp );\r\n            exp = 0;\r\n            roundBits = sig & 0x3FF;\r\n            if ( isTiny && roundBits ) {\r\n                softfloat_raiseFlags( softfloat_flag_underflow );\r\n            }\r\n        } else if (\r\n            (0x7FD < exp)\r\n                || (UINT64_C( 0x8000000000000000 ) <= sig + roundIncrement)\r\n        ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            softfloat_raiseFlags(\r\n                softfloat_flag_overflow | softfloat_flag_inexact );\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 ) - ! roundIncrement;\r\n            goto uiZ;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = (sig + roundIncrement)>>10;\r\n    if ( roundBits ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            sig |= 1;\r\n            goto packReturn;\r\n        }\r\n#endif\r\n    }\r\n    sig &= ~(uint_fast64_t) (! (roundBits ^ 0x200) & roundNearEven);\r\n    if ( ! sig ) exp = 0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n packReturn:\r\n    uiZ = packToF64UI( sign, exp, sig );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundPackToF64.c ****/\n/**** start inlining ../../source/s_normRoundPackToF64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nfloat64_t\r\n softfloat_normRoundPackToF64( bool sign, int_fast16_t exp, uint_fast64_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    union ui64_f64 uZ;\r\n\r\n    shiftDist = softfloat_countLeadingZeros64( sig ) - 1;\r\n    exp -= shiftDist;\r\n    if ( (10 <= shiftDist) && ((unsigned int) exp < 0x7FD) ) {\r\n        uZ.ui = packToF64UI( sign, sig ? exp : 0, sig<<(shiftDist - 10) );\r\n        return uZ.f;\r\n    } else {\r\n        return softfloat_roundPackToF64( sign, exp, sig<<shiftDist );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normRoundPackToF64.c ****/\n/**** start inlining ../../source/s_addMagsF64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n\r\nfloat64_t\r\n softfloat_addMagsF64( uint_fast64_t uiA, uint_fast64_t uiB, bool signZ )\r\n{\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA;\r\n    int_fast16_t expB;\r\n    uint_fast64_t sigB;\r\n    int_fast16_t expDiff;\r\n    uint_fast64_t uiZ;\r\n    int_fast16_t expZ;\r\n    uint_fast64_t sigZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expF64UI( uiA );\r\n    sigA = fracF64UI( uiA );\r\n    expB = expF64UI( uiB );\r\n    sigB = fracF64UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! expA ) {\r\n            uiZ = uiA + sigB;\r\n            goto uiZ;\r\n        }\r\n        if ( expA == 0x7FF ) {\r\n            if ( sigA | sigB ) goto propagateNaN;\r\n            uiZ = uiA;\r\n            goto uiZ;\r\n        }\r\n        expZ = expA;\r\n        sigZ = UINT64_C( 0x0020000000000000 ) + sigA + sigB;\r\n        sigZ <<= 9;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sigA <<= 9;\r\n        sigB <<= 9;\r\n        if ( expDiff < 0 ) {\r\n            if ( expB == 0x7FF ) {\r\n                if ( sigB ) goto propagateNaN;\r\n                uiZ = packToF64UI( signZ, 0x7FF, 0 );\r\n                goto uiZ;\r\n            }\r\n            expZ = expB;\r\n            if ( expA ) {\r\n                sigA += UINT64_C( 0x2000000000000000 );\r\n            } else {\r\n                sigA <<= 1;\r\n            }\r\n            sigA = softfloat_shiftRightJam64( sigA, -expDiff );\r\n        } else {\r\n            if ( expA == 0x7FF ) {\r\n                if ( sigA ) goto propagateNaN;\r\n                uiZ = uiA;\r\n                goto uiZ;\r\n            }\r\n            expZ = expA;\r\n            if ( expB ) {\r\n                sigB += UINT64_C( 0x2000000000000000 );\r\n            } else {\r\n                sigB <<= 1;\r\n            }\r\n            sigB = softfloat_shiftRightJam64( sigB, expDiff );\r\n        }\r\n        sigZ = UINT64_C( 0x2000000000000000 ) + sigA + sigB;\r\n        if ( sigZ < UINT64_C( 0x4000000000000000 ) ) {\r\n            --expZ;\r\n            sigZ <<= 1;\r\n        }\r\n    }\r\n    return softfloat_roundPackToF64( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_addMagsF64.c ****/\n/**** start inlining ../../source/s_subMagsF64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t\r\n softfloat_subMagsF64( uint_fast64_t uiA, uint_fast64_t uiB, bool signZ )\r\n{\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA;\r\n    int_fast16_t expB;\r\n    uint_fast64_t sigB;\r\n    int_fast16_t expDiff;\r\n    uint_fast64_t uiZ;\r\n    int_fast64_t sigDiff;\r\n    int_fast8_t shiftDist;\r\n    int_fast16_t expZ;\r\n    uint_fast64_t sigZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expF64UI( uiA );\r\n    sigA = fracF64UI( uiA );\r\n    expB = expF64UI( uiB );\r\n    sigB = fracF64UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expA == 0x7FF ) {\r\n            if ( sigA | sigB ) goto propagateNaN;\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            uiZ = defaultNaNF64UI;\r\n            goto uiZ;\r\n        }\r\n        sigDiff = sigA - sigB;\r\n        if ( ! sigDiff ) {\r\n            uiZ =\r\n                packToF64UI(\r\n                    (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( expA ) --expA;\r\n        if ( sigDiff < 0 ) {\r\n            signZ = ! signZ;\r\n            sigDiff = -sigDiff;\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros64( sigDiff ) - 11;\r\n        expZ = expA - shiftDist;\r\n        if ( expZ < 0 ) {\r\n            shiftDist = expA;\r\n            expZ = 0;\r\n        }\r\n        uiZ = packToF64UI( signZ, expZ, sigDiff<<shiftDist );\r\n        goto uiZ;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sigA <<= 10;\r\n        sigB <<= 10;\r\n        if ( expDiff < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            signZ = ! signZ;\r\n            if ( expB == 0x7FF ) {\r\n                if ( sigB ) goto propagateNaN;\r\n                uiZ = packToF64UI( signZ, 0x7FF, 0 );\r\n                goto uiZ;\r\n            }\r\n            sigA += expA ? UINT64_C( 0x4000000000000000 ) : sigA;\r\n            sigA = softfloat_shiftRightJam64( sigA, -expDiff );\r\n            sigB |= UINT64_C( 0x4000000000000000 );\r\n            expZ = expB;\r\n            sigZ = sigB - sigA;\r\n        } else {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            if ( expA == 0x7FF ) {\r\n                if ( sigA ) goto propagateNaN;\r\n                uiZ = uiA;\r\n                goto uiZ;\r\n            }\r\n            sigB += expB ? UINT64_C( 0x4000000000000000 ) : sigB;\r\n            sigB = softfloat_shiftRightJam64( sigB, expDiff );\r\n            sigA |= UINT64_C( 0x4000000000000000 );\r\n            expZ = expA;\r\n            sigZ = sigA - sigB;\r\n        }\r\n        return softfloat_normRoundPackToF64( signZ, expZ - 1, sigZ );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_subMagsF64.c ****/\n/**** start inlining ../../source/s_mulAddF64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat64_t\r\n softfloat_mulAddF64(\r\n     uint_fast64_t uiA, uint_fast64_t uiB, uint_fast64_t uiC, uint_fast8_t op )\r\n{\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint_fast64_t sigB;\r\n    bool signC;\r\n    int_fast16_t expC;\r\n    uint_fast64_t sigC;\r\n    bool signZ;\r\n    uint_fast64_t magBits, uiZ;\r\n    struct exp16_sig64 normExpSig;\r\n    int_fast16_t expZ;\r\n    struct uint128 sig128Z;\r\n    uint_fast64_t sigZ;\r\n    int_fast16_t expDiff;\r\n    struct uint128 sig128C;\r\n    int_fast8_t shiftDist;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signF64UI( uiA );\r\n    expA  = expF64UI( uiA );\r\n    sigA  = fracF64UI( uiA );\r\n    signB = signF64UI( uiB );\r\n    expB  = expF64UI( uiB );\r\n    sigB  = fracF64UI( uiB );\r\n    signC = signF64UI( uiC ) ^ (op == softfloat_mulAdd_subC);\r\n    expC  = expF64UI( uiC );\r\n    sigC  = fracF64UI( uiC );\r\n    signZ = signA ^ signB ^ (op == softfloat_mulAdd_subProd);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FF ) {\r\n        if ( sigA || ((expB == 0x7FF) && sigB) ) goto propagateNaN_ABC;\r\n        magBits = expB | sigB;\r\n        goto infProdArg;\r\n    }\r\n    if ( expB == 0x7FF ) {\r\n        if ( sigB ) goto propagateNaN_ABC;\r\n        magBits = expA | sigA;\r\n        goto infProdArg;\r\n    }\r\n    if ( expC == 0x7FF ) {\r\n        if ( sigC ) {\r\n            uiZ = 0;\r\n            goto propagateNaN_ZC;\r\n        }\r\n        uiZ = uiC;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x3FE;\r\n    sigA = (sigA | UINT64_C( 0x0010000000000000 ))<<10;\r\n    sigB = (sigB | UINT64_C( 0x0010000000000000 ))<<10;\r\n    sig128Z = softfloat_mul64To128( sigA, sigB );\r\n    if ( sig128Z.v64 < UINT64_C( 0x2000000000000000 ) ) {\r\n        --expZ;\r\n        sig128Z =\r\n            softfloat_add128(\r\n                sig128Z.v64, sig128Z.v0, sig128Z.v64, sig128Z.v0 );\r\n    }\r\n    if ( ! expC ) {\r\n        if ( ! sigC ) {\r\n            --expZ;\r\n            sigZ = sig128Z.v64<<1 | (sig128Z.v0 != 0);\r\n            goto roundPack;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigC );\r\n        expC = normExpSig.exp;\r\n        sigC = normExpSig.sig;\r\n    }\r\n    sigC = (sigC | UINT64_C( 0x0010000000000000 ))<<9;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expZ - expC;\r\n    if ( expDiff < 0 ) {\r\n        expZ = expC;\r\n        if ( (signZ == signC) || (expDiff < -1) ) {\r\n            sig128Z.v64 = softfloat_shiftRightJam64( sig128Z.v64, -expDiff );\r\n        } else {\r\n            sig128Z =\r\n                softfloat_shortShiftRightJam128( sig128Z.v64, sig128Z.v0, 1 );\r\n        }\r\n    } else if ( expDiff ) {\r\n        sig128C = softfloat_shiftRightJam128( sigC, 0, expDiff );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signZ == signC ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff <= 0 ) {\r\n            sigZ = (sigC + sig128Z.v64) | (sig128Z.v0 != 0);\r\n        } else {\r\n            sig128Z =\r\n                softfloat_add128(\r\n                    sig128Z.v64, sig128Z.v0, sig128C.v64, sig128C.v0 );\r\n            sigZ = sig128Z.v64 | (sig128Z.v0 != 0);\r\n        }\r\n        if ( sigZ < UINT64_C( 0x4000000000000000 ) ) {\r\n            --expZ;\r\n            sigZ <<= 1;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff < 0 ) {\r\n            signZ = signC;\r\n            sig128Z = softfloat_sub128( sigC, 0, sig128Z.v64, sig128Z.v0 );\r\n        } else if ( ! expDiff ) {\r\n            sig128Z.v64 = sig128Z.v64 - sigC;\r\n            if ( ! (sig128Z.v64 | sig128Z.v0) ) goto completeCancellation;\r\n            if ( sig128Z.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n                signZ = ! signZ;\r\n                sig128Z = softfloat_sub128( 0, 0, sig128Z.v64, sig128Z.v0 );\r\n            }\r\n        } else {\r\n            sig128Z =\r\n                softfloat_sub128(\r\n                    sig128Z.v64, sig128Z.v0, sig128C.v64, sig128C.v0 );\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! sig128Z.v64 ) {\r\n            expZ -= 64;\r\n            sig128Z.v64 = sig128Z.v0;\r\n            sig128Z.v0 = 0;\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros64( sig128Z.v64 ) - 1;\r\n        expZ -= shiftDist;\r\n        if ( shiftDist < 0 ) {\r\n            sigZ = softfloat_shortShiftRightJam64( sig128Z.v64, -shiftDist );\r\n        } else {\r\n            sig128Z =\r\n                softfloat_shortShiftLeft128(\r\n                    sig128Z.v64, sig128Z.v0, shiftDist );\r\n            sigZ = sig128Z.v64;\r\n        }\r\n        sigZ |= (sig128Z.v0 != 0);\r\n    }\r\n roundPack:\r\n    return softfloat_roundPackToF64( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN_ABC:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n    goto propagateNaN_ZC;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infProdArg:\r\n    if ( magBits ) {\r\n        uiZ = packToF64UI( signZ, 0x7FF, 0 );\r\n        if ( expC != 0x7FF ) goto uiZ;\r\n        if ( sigC ) goto propagateNaN_ZC;\r\n        if ( signZ == signC ) goto uiZ;\r\n    }\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF64UI;\r\n propagateNaN_ZC:\r\n    uiZ = softfloat_propagateNaNF64UI( uiZ, uiC );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zeroProd:\r\n    uiZ = uiC;\r\n    if ( ! (expC | sigC) && (signZ != signC) ) {\r\n completeCancellation:\r\n        uiZ =\r\n            packToF64UI(\r\n                (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat64_t\r\n softfloat_mulAddF64(\r\n     uint_fast64_t uiA, uint_fast64_t uiB, uint_fast64_t uiC, uint_fast8_t op )\r\n{\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint64_t sigA;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint64_t sigB;\r\n    bool signC;\r\n    int_fast16_t expC;\r\n    uint64_t sigC;\r\n    bool signZ;\r\n    uint64_t magBits, uiZ;\r\n    struct exp16_sig64 normExpSig;\r\n    int_fast16_t expZ;\r\n    uint32_t sig128Z[4];\r\n    uint64_t sigZ;\r\n    int_fast16_t shiftDist, expDiff;\r\n    uint32_t sig128C[4];\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signF64UI( uiA );\r\n    expA  = expF64UI( uiA );\r\n    sigA  = fracF64UI( uiA );\r\n    signB = signF64UI( uiB );\r\n    expB  = expF64UI( uiB );\r\n    sigB  = fracF64UI( uiB );\r\n    signC = signF64UI( uiC ) ^ (op == softfloat_mulAdd_subC);\r\n    expC  = expF64UI( uiC );\r\n    sigC  = fracF64UI( uiC );\r\n    signZ = signA ^ signB ^ (op == softfloat_mulAdd_subProd);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FF ) {\r\n        if ( sigA || ((expB == 0x7FF) && sigB) ) goto propagateNaN_ABC;\r\n        magBits = expB | sigB;\r\n        goto infProdArg;\r\n    }\r\n    if ( expB == 0x7FF ) {\r\n        if ( sigB ) goto propagateNaN_ABC;\r\n        magBits = expA | sigA;\r\n        goto infProdArg;\r\n    }\r\n    if ( expC == 0x7FF ) {\r\n        if ( sigC ) {\r\n            uiZ = 0;\r\n            goto propagateNaN_ZC;\r\n        }\r\n        uiZ = uiC;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x3FE;\r\n    sigA = (sigA | UINT64_C( 0x0010000000000000 ))<<10;\r\n    sigB = (sigB | UINT64_C( 0x0010000000000000 ))<<11;\r\n    softfloat_mul64To128M( sigA, sigB, sig128Z );\r\n    sigZ =\r\n        (uint64_t) sig128Z[indexWord( 4, 3 )]<<32 | sig128Z[indexWord( 4, 2 )];\r\n    shiftDist = 0;\r\n    if ( ! (sigZ & UINT64_C( 0x4000000000000000 )) ) {\r\n        --expZ;\r\n        shiftDist = -1;\r\n    }\r\n    if ( ! expC ) {\r\n        if ( ! sigC ) {\r\n            if ( shiftDist ) sigZ <<= 1;\r\n            goto sigZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigC );\r\n        expC = normExpSig.exp;\r\n        sigC = normExpSig.sig;\r\n    }\r\n    sigC = (sigC | UINT64_C( 0x0010000000000000 ))<<10;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expZ - expC;\r\n    if ( expDiff < 0 ) {\r\n        expZ = expC;\r\n        if ( (signZ == signC) || (expDiff < -1) ) {\r\n            shiftDist -= expDiff;\r\n            if ( shiftDist) {\r\n                sigZ = softfloat_shiftRightJam64( sigZ, shiftDist );\r\n            }\r\n        } else {\r\n            if ( ! shiftDist ) {\r\n                softfloat_shortShiftRight128M( sig128Z, 1, sig128Z );\r\n            }\r\n        }\r\n    } else {\r\n        if ( shiftDist ) softfloat_add128M( sig128Z, sig128Z, sig128Z );\r\n        if ( ! expDiff ) {\r\n            sigZ =\r\n                (uint64_t) sig128Z[indexWord( 4, 3 )]<<32\r\n                    | sig128Z[indexWord( 4, 2 )];\r\n        } else {\r\n            sig128C[indexWord( 4, 3 )] = sigC>>32;\r\n            sig128C[indexWord( 4, 2 )] = sigC;\r\n            sig128C[indexWord( 4, 1 )] = 0;\r\n            sig128C[indexWord( 4, 0 )] = 0;\r\n            softfloat_shiftRightJam128M( sig128C, expDiff, sig128C );\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signZ == signC ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff <= 0 ) {\r\n            sigZ += sigC;\r\n        } else {\r\n            softfloat_add128M( sig128Z, sig128C, sig128Z );\r\n            sigZ =\r\n                (uint64_t) sig128Z[indexWord( 4, 3 )]<<32\r\n                    | sig128Z[indexWord( 4, 2 )];\r\n        }\r\n        if ( sigZ & UINT64_C( 0x8000000000000000 ) ) {\r\n            ++expZ;\r\n            sigZ = softfloat_shortShiftRightJam64( sigZ, 1 );\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff < 0 ) {\r\n            signZ = signC;\r\n            if ( expDiff < -1 ) {\r\n                sigZ = sigC - sigZ;\r\n                if (\r\n                    sig128Z[indexWord( 4, 1 )] || sig128Z[indexWord( 4, 0 )]\r\n                ) {\r\n                    sigZ = (sigZ - 1) | 1;\r\n                }\r\n                if ( ! (sigZ & UINT64_C( 0x4000000000000000 )) ) {\r\n                    --expZ;\r\n                    sigZ <<= 1;\r\n                }\r\n                goto roundPack;\r\n            } else {\r\n                sig128C[indexWord( 4, 3 )] = sigC>>32;\r\n                sig128C[indexWord( 4, 2 )] = sigC;\r\n                sig128C[indexWord( 4, 1 )] = 0;\r\n                sig128C[indexWord( 4, 0 )] = 0;\r\n                softfloat_sub128M( sig128C, sig128Z, sig128Z );\r\n            }\r\n        } else if ( ! expDiff ) {\r\n            sigZ -= sigC;\r\n            if (\r\n                ! sigZ && ! sig128Z[indexWord( 4, 1 )]\r\n                    && ! sig128Z[indexWord( 4, 0 )]\r\n            ) {\r\n                goto completeCancellation;\r\n            }\r\n            sig128Z[indexWord( 4, 3 )] = sigZ>>32;\r\n            sig128Z[indexWord( 4, 2 )] = sigZ;\r\n            if ( sigZ & UINT64_C( 0x8000000000000000 ) ) {\r\n                signZ = ! signZ;\r\n                softfloat_negX128M( sig128Z );\r\n            }\r\n        } else {\r\n            softfloat_sub128M( sig128Z, sig128C, sig128Z );\r\n            if ( 1 < expDiff ) {\r\n                sigZ =\r\n                    (uint64_t) sig128Z[indexWord( 4, 3 )]<<32\r\n                        | sig128Z[indexWord( 4, 2 )];\r\n                if ( ! (sigZ & UINT64_C( 0x4000000000000000 )) ) {\r\n                    --expZ;\r\n                    sigZ <<= 1;\r\n                }\r\n                goto sigZ;\r\n            }\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        shiftDist = 0;\r\n        sigZ =\r\n            (uint64_t) sig128Z[indexWord( 4, 3 )]<<32\r\n                | sig128Z[indexWord( 4, 2 )];\r\n        if ( ! sigZ ) {\r\n            shiftDist = 64;\r\n            sigZ =\r\n                (uint64_t) sig128Z[indexWord( 4, 1 )]<<32\r\n                    | sig128Z[indexWord( 4, 0 )];\r\n        }\r\n        shiftDist += softfloat_countLeadingZeros64( sigZ ) - 1;\r\n        if ( shiftDist ) {\r\n            expZ -= shiftDist;\r\n            softfloat_shiftLeft128M( sig128Z, shiftDist, sig128Z );\r\n            sigZ =\r\n                (uint64_t) sig128Z[indexWord( 4, 3 )]<<32\r\n                    | sig128Z[indexWord( 4, 2 )];\r\n        }\r\n    }\r\n sigZ:\r\n    if ( sig128Z[indexWord( 4, 1 )] || sig128Z[indexWord( 4, 0 )] ) sigZ |= 1;\r\n roundPack:\r\n    return softfloat_roundPackToF64( signZ, expZ - 1, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN_ABC:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n    goto propagateNaN_ZC;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infProdArg:\r\n    if ( magBits ) {\r\n        uiZ = packToF64UI( signZ, 0x7FF, 0 );\r\n        if ( expC != 0x7FF ) goto uiZ;\r\n        if ( sigC ) goto propagateNaN_ZC;\r\n        if ( signZ == signC ) goto uiZ;\r\n    }\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF64UI;\r\n propagateNaN_ZC:\r\n    uiZ = softfloat_propagateNaNF64UI( uiZ, uiC );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zeroProd:\r\n    uiZ = uiC;\r\n    if ( ! (expC | sigC) && (signZ != signC) ) {\r\n completeCancellation:\r\n        uiZ =\r\n            packToF64UI(\r\n                (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/s_mulAddF64.c ****/\n/**** start inlining ../../source/s_normSubnormalExtF80Sig.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nstruct exp32_sig64 softfloat_normSubnormalExtF80Sig( uint_fast64_t sig )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct exp32_sig64 z;\r\n\r\n    shiftDist = softfloat_countLeadingZeros64( sig );\r\n    z.exp = -shiftDist;\r\n    z.sig = sig<<shiftDist;\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normSubnormalExtF80Sig.c ****/\n/**** start inlining ../../source/s_roundPackToExtF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t\r\n softfloat_roundPackToExtF80(\r\n     bool sign,\r\n     int_fast32_t exp,\r\n     uint_fast64_t sig,\r\n     uint_fast64_t sigExtra,\r\n     uint_fast8_t roundingPrecision\r\n )\r\n{\r\n    uint_fast8_t roundingMode;\r\n    bool roundNearEven;\r\n    uint_fast64_t roundIncrement, roundMask, roundBits;\r\n    bool isTiny, doIncrement;\r\n    struct uint64_extra sig64Extra;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundingMode = softfloat_roundingMode;\r\n    roundNearEven = (roundingMode == softfloat_round_near_even);\r\n    if ( roundingPrecision == 80 ) goto precision80;\r\n    if ( roundingPrecision == 64 ) {\r\n        roundIncrement = UINT64_C( 0x0000000000000400 );\r\n        roundMask = UINT64_C( 0x00000000000007FF );\r\n    } else if ( roundingPrecision == 32 ) {\r\n        roundIncrement = UINT64_C( 0x0000008000000000 );\r\n        roundMask = UINT64_C( 0x000000FFFFFFFFFF );\r\n    } else {\r\n        goto precision80;\r\n    }\r\n    sig |= (sigExtra != 0);\r\n    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {\r\n        roundIncrement =\r\n            (roundingMode\r\n                 == (sign ? softfloat_round_min : softfloat_round_max))\r\n                ? roundMask\r\n                : 0;\r\n    }\r\n    roundBits = sig & roundMask;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x7FFD <= (uint32_t) (exp - 1) ) {\r\n        if ( exp <= 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            isTiny =\r\n                   (softfloat_detectTininess\r\n                        == softfloat_tininess_beforeRounding)\r\n                || (exp < 0)\r\n                || (sig <= (uint64_t) (sig + roundIncrement));\r\n            sig = softfloat_shiftRightJam64( sig, 1 - exp );\r\n            roundBits = sig & roundMask;\r\n            if ( roundBits ) {\r\n                if ( isTiny ) softfloat_raiseFlags( softfloat_flag_underflow );\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n                if ( roundingMode == softfloat_round_odd ) {\r\n                    sig |= roundMask + 1;\r\n                }\r\n#endif\r\n            }\r\n            sig += roundIncrement;\r\n            exp = ((sig & UINT64_C( 0x8000000000000000 )) != 0);\r\n            roundIncrement = roundMask + 1;\r\n            if ( roundNearEven && (roundBits<<1 == roundIncrement) ) {\r\n                roundMask |= roundIncrement;\r\n            }\r\n            sig &= ~roundMask;\r\n            goto packReturn;\r\n        }\r\n        if (\r\n               (0x7FFE < exp)\r\n            || ((exp == 0x7FFE) && ((uint64_t) (sig + roundIncrement) < sig))\r\n        ) {\r\n            goto overflow;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( roundBits ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            sig = (sig & ~roundMask) | (roundMask + 1);\r\n            goto packReturn;\r\n        }\r\n#endif\r\n    }\r\n    sig = (uint64_t) (sig + roundIncrement);\r\n    if ( sig < roundIncrement ) {\r\n        ++exp;\r\n        sig = UINT64_C( 0x8000000000000000 );\r\n    }\r\n    roundIncrement = roundMask + 1;\r\n    if ( roundNearEven && (roundBits<<1 == roundIncrement) ) {\r\n        roundMask |= roundIncrement;\r\n    }\r\n    sig &= ~roundMask;\r\n    goto packReturn;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n precision80:\r\n    doIncrement = (UINT64_C( 0x8000000000000000 ) <= sigExtra);\r\n    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {\r\n        doIncrement =\r\n            (roundingMode\r\n                 == (sign ? softfloat_round_min : softfloat_round_max))\r\n                && sigExtra;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x7FFD <= (uint32_t) (exp - 1) ) {\r\n        if ( exp <= 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            isTiny =\r\n                   (softfloat_detectTininess\r\n                        == softfloat_tininess_beforeRounding)\r\n                || (exp < 0)\r\n                || ! doIncrement\r\n                || (sig < UINT64_C( 0xFFFFFFFFFFFFFFFF ));\r\n            sig64Extra =\r\n                softfloat_shiftRightJam64Extra( sig, sigExtra, 1 - exp );\r\n            exp = 0;\r\n            sig = sig64Extra.v;\r\n            sigExtra = sig64Extra.extra;\r\n            if ( sigExtra ) {\r\n                if ( isTiny ) softfloat_raiseFlags( softfloat_flag_underflow );\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n                if ( roundingMode == softfloat_round_odd ) {\r\n                    sig |= 1;\r\n                    goto packReturn;\r\n                }\r\n#endif\r\n            }\r\n            doIncrement = (UINT64_C( 0x8000000000000000 ) <= sigExtra);\r\n            if (\r\n                ! roundNearEven\r\n                    && (roundingMode != softfloat_round_near_maxMag)\r\n            ) {\r\n                doIncrement =\r\n                    (roundingMode\r\n                         == (sign ? softfloat_round_min : softfloat_round_max))\r\n                        && sigExtra;\r\n            }\r\n            if ( doIncrement ) {\r\n                ++sig;\r\n                sig &=\r\n                    ~(uint_fast64_t)\r\n                         (! (sigExtra & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                              & roundNearEven);\r\n                exp = ((sig & UINT64_C( 0x8000000000000000 )) != 0);\r\n            }\r\n            goto packReturn;\r\n        }\r\n        if (\r\n               (0x7FFE < exp)\r\n            || ((exp == 0x7FFE) && (sig == UINT64_C( 0xFFFFFFFFFFFFFFFF ))\r\n                    && doIncrement)\r\n        ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            roundMask = 0;\r\n overflow:\r\n            softfloat_raiseFlags(\r\n                softfloat_flag_overflow | softfloat_flag_inexact );\r\n            if (\r\n                   roundNearEven\r\n                || (roundingMode == softfloat_round_near_maxMag)\r\n                || (roundingMode\r\n                        == (sign ? softfloat_round_min : softfloat_round_max))\r\n            ) {\r\n                exp = 0x7FFF;\r\n                sig = UINT64_C( 0x8000000000000000 );\r\n            } else {\r\n                exp = 0x7FFE;\r\n                sig = ~roundMask;\r\n            }\r\n            goto packReturn;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( sigExtra ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            sig |= 1;\r\n            goto packReturn;\r\n        }\r\n#endif\r\n    }\r\n    if ( doIncrement ) {\r\n        ++sig;\r\n        if ( ! sig ) {\r\n            ++exp;\r\n            sig = UINT64_C( 0x8000000000000000 );\r\n        } else {\r\n            sig &=\r\n                ~(uint_fast64_t)\r\n                     (! (sigExtra & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                          & roundNearEven);\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n packReturn:\r\n    uZ.s.signExp = packToExtF80UI64( sign, exp );\r\n    uZ.s.signif = sig;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundPackToExtF80.c ****/\n/**** start inlining ../../source/s_normRoundPackToExtF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nextFloat80_t\r\n softfloat_normRoundPackToExtF80(\r\n     bool sign,\r\n     int_fast32_t exp,\r\n     uint_fast64_t sig,\r\n     uint_fast64_t sigExtra,\r\n     uint_fast8_t roundingPrecision\r\n )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct uint128 sig128;\r\n\r\n    if ( ! sig ) {\r\n        exp -= 64;\r\n        sig = sigExtra;\r\n        sigExtra = 0;\r\n    }\r\n    shiftDist = softfloat_countLeadingZeros64( sig );\r\n    exp -= shiftDist;\r\n    if ( shiftDist ) {\r\n        sig128 = softfloat_shortShiftLeft128( sig, sigExtra, shiftDist );\r\n        sig = sig128.v64;\r\n        sigExtra = sig128.v0;\r\n    }\r\n    return\r\n        softfloat_roundPackToExtF80(\r\n            sign, exp, sig, sigExtra, roundingPrecision );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normRoundPackToExtF80.c ****/\n/**** start inlining ../../source/s_addMagsExtF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t\r\n softfloat_addMagsExtF80(\r\n     uint_fast16_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast16_t uiB64,\r\n     uint_fast64_t uiB0,\r\n     bool signZ\r\n )\r\n{\r\n    int_fast32_t expA;\r\n    uint_fast64_t sigA;\r\n    int_fast32_t expB;\r\n    uint_fast64_t sigB;\r\n    int_fast32_t expDiff;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0, sigZ, sigZExtra;\r\n    struct exp32_sig64 normExpSig;\r\n    int_fast32_t expZ;\r\n    struct uint64_extra sig64Extra;\r\n    struct uint128 uiZ;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expExtF80UI64( uiA64 );\r\n    sigA = uiA0;\r\n    expB = expExtF80UI64( uiB64 );\r\n    sigB = uiB0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        if ( expA == 0x7FFF ) {\r\n            if ( (sigA | sigB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n                goto propagateNaN;\r\n            }\r\n            uiZ64 = uiA64;\r\n            uiZ0  = uiA0;\r\n            goto uiZ;\r\n        }\r\n        sigZ = sigA + sigB;\r\n        sigZExtra = 0;\r\n        if ( ! expA ) {\r\n            normExpSig = softfloat_normSubnormalExtF80Sig( sigZ );\r\n            expZ = normExpSig.exp + 1;\r\n            sigZ = normExpSig.sig;\r\n            goto roundAndPack;\r\n        }\r\n        expZ = expA;\r\n        goto shiftRight1;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expDiff < 0 ) {\r\n        if ( expB == 0x7FFF ) {\r\n            if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n            uiZ64 = packToExtF80UI64( signZ, 0x7FFF );\r\n            uiZ0  = uiB0;\r\n            goto uiZ;\r\n        }\r\n        expZ = expB;\r\n        if ( ! expA ) {\r\n            ++expDiff;\r\n            sigZExtra = 0;\r\n            if ( ! expDiff ) goto newlyAligned;\r\n        }\r\n        sig64Extra = softfloat_shiftRightJam64Extra( sigA, 0, -expDiff );\r\n        sigA = sig64Extra.v;\r\n        sigZExtra = sig64Extra.extra;\r\n    } else {\r\n        if ( expA == 0x7FFF ) {\r\n            if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n            uiZ64 = uiA64;\r\n            uiZ0  = uiA0;\r\n            goto uiZ;\r\n        }\r\n        expZ = expA;\r\n        if ( ! expB ) {\r\n            --expDiff;\r\n            sigZExtra = 0;\r\n            if ( ! expDiff ) goto newlyAligned;\r\n        }\r\n        sig64Extra = softfloat_shiftRightJam64Extra( sigB, 0, expDiff );\r\n        sigB = sig64Extra.v;\r\n        sigZExtra = sig64Extra.extra;\r\n    }\r\n newlyAligned:\r\n    sigZ = sigA + sigB;\r\n    if ( sigZ & UINT64_C( 0x8000000000000000 ) ) goto roundAndPack;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n shiftRight1:\r\n    sig64Extra = softfloat_shortShiftRightJam64Extra( sigZ, sigZExtra, 1 );\r\n    sigZ = sig64Extra.v | UINT64_C( 0x8000000000000000 );\r\n    sigZExtra = sig64Extra.extra;\r\n    ++expZ;\r\n roundAndPack:\r\n    return\r\n        softfloat_roundPackToExtF80(\r\n            signZ, expZ, sigZ, sigZExtra, extF80_roundingPrecision );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );\r\n    uiZ64 = uiZ.v64;\r\n    uiZ0  = uiZ.v0;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_addMagsExtF80.c ****/\n/**** start inlining ../../source/s_subMagsExtF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t\r\n softfloat_subMagsExtF80(\r\n     uint_fast16_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast16_t uiB64,\r\n     uint_fast64_t uiB0,\r\n     bool signZ\r\n )\r\n{\r\n    int_fast32_t expA;\r\n    uint_fast64_t sigA;\r\n    int_fast32_t expB;\r\n    uint_fast64_t sigB;\r\n    int_fast32_t expDiff;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    int_fast32_t expZ;\r\n    uint_fast64_t sigExtra;\r\n    struct uint128 sig128, uiZ;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = expExtF80UI64( uiA64 );\r\n    sigA = uiA0;\r\n    expB = expExtF80UI64( uiB64 );\r\n    sigB = uiB0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( 0 < expDiff ) goto expABigger;\r\n    if ( expDiff < 0 ) goto expBBigger;\r\n    if ( expA == 0x7FFF ) {\r\n        if ( (sigA | sigB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            goto propagateNaN;\r\n        }\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ64 = defaultNaNExtF80UI64;\r\n        uiZ0  = defaultNaNExtF80UI0;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA;\r\n    if ( ! expZ ) expZ = 1;\r\n    sigExtra = 0;\r\n    if ( sigB < sigA ) goto aBigger;\r\n    if ( sigA < sigB ) goto bBigger;\r\n    uiZ64 =\r\n        packToExtF80UI64( (softfloat_roundingMode == softfloat_round_min), 0 );\r\n    uiZ0 = 0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n expBBigger:\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n        uiZ64 = packToExtF80UI64( signZ ^ 1, 0x7FFF );\r\n        uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        goto uiZ;\r\n    }\r\n    if ( ! expA ) {\r\n        ++expDiff;\r\n        sigExtra = 0;\r\n        if ( ! expDiff ) goto newlyAlignedBBigger;\r\n    }\r\n    sig128 = softfloat_shiftRightJam128( sigA, 0, -expDiff );\r\n    sigA = sig128.v64;\r\n    sigExtra = sig128.v0;\r\n newlyAlignedBBigger:\r\n    expZ = expB;\r\n bBigger:\r\n    signZ = ! signZ;\r\n    sig128 = softfloat_sub128( sigB, 0, sigA, sigExtra );\r\n    goto normRoundPack;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n expABigger:\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n        uiZ64 = uiA64;\r\n        uiZ0  = uiA0;\r\n        goto uiZ;\r\n    }\r\n    if ( ! expB ) {\r\n        --expDiff;\r\n        sigExtra = 0;\r\n        if ( ! expDiff ) goto newlyAlignedABigger;\r\n    }\r\n    sig128 = softfloat_shiftRightJam128( sigB, 0, expDiff );\r\n    sigB = sig128.v64;\r\n    sigExtra = sig128.v0;\r\n newlyAlignedABigger:\r\n    expZ = expA;\r\n aBigger:\r\n    sig128 = softfloat_sub128( sigA, 0, sigB, sigExtra );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n normRoundPack:\r\n    return\r\n        softfloat_normRoundPackToExtF80(\r\n            signZ, expZ, sig128.v64, sig128.v0, extF80_roundingPrecision );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );\r\n    uiZ64 = uiZ.v64;\r\n    uiZ0  = uiZ.v0;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_subMagsExtF80.c ****/\n/**** start inlining ../../source/s_normSubnormalF128Sig.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nstruct exp32_sig128\r\n softfloat_normSubnormalF128Sig( uint_fast64_t sig64, uint_fast64_t sig0 )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct exp32_sig128 z;\r\n\r\n    if ( ! sig64 ) {\r\n        shiftDist = softfloat_countLeadingZeros64( sig0 ) - 15;\r\n        z.exp = -63 - shiftDist;\r\n        if ( shiftDist < 0 ) {\r\n            z.sig.v64 = sig0>>-shiftDist;\r\n            z.sig.v0  = sig0<<(shiftDist & 63);\r\n        } else {\r\n            z.sig.v64 = sig0<<shiftDist;\r\n            z.sig.v0  = 0;\r\n        }\r\n    } else {\r\n        shiftDist = softfloat_countLeadingZeros64( sig64 ) - 15;\r\n        z.exp = 1 - shiftDist;\r\n        z.sig = softfloat_shortShiftLeft128( sig64, sig0, shiftDist );\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normSubnormalF128Sig.c ****/\n/**** start inlining ../../source/s_roundPackToF128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t\r\n softfloat_roundPackToF128(\r\n     bool sign,\r\n     int_fast32_t exp,\r\n     uint_fast64_t sig64,\r\n     uint_fast64_t sig0,\r\n     uint_fast64_t sigExtra\r\n )\r\n{\r\n    uint_fast8_t roundingMode;\r\n    bool roundNearEven, doIncrement, isTiny;\r\n    struct uint128_extra sig128Extra;\r\n    uint_fast64_t uiZ64, uiZ0;\r\n    struct uint128 sig128;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    roundingMode = softfloat_roundingMode;\r\n    roundNearEven = (roundingMode == softfloat_round_near_even);\r\n    doIncrement = (UINT64_C( 0x8000000000000000 ) <= sigExtra);\r\n    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {\r\n        doIncrement =\r\n            (roundingMode\r\n                 == (sign ? softfloat_round_min : softfloat_round_max))\r\n                && sigExtra;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x7FFD <= (uint32_t) exp ) {\r\n        if ( exp < 0 ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            isTiny =\r\n                   (softfloat_detectTininess\r\n                        == softfloat_tininess_beforeRounding)\r\n                || (exp < -1)\r\n                || ! doIncrement\r\n                || softfloat_lt128(\r\n                       sig64,\r\n                       sig0,\r\n                       UINT64_C( 0x0001FFFFFFFFFFFF ),\r\n                       UINT64_C( 0xFFFFFFFFFFFFFFFF )\r\n                   );\r\n            sig128Extra =\r\n                softfloat_shiftRightJam128Extra( sig64, sig0, sigExtra, -exp );\r\n            sig64 = sig128Extra.v.v64;\r\n            sig0  = sig128Extra.v.v0;\r\n            sigExtra = sig128Extra.extra;\r\n            exp = 0;\r\n            if ( isTiny && sigExtra ) {\r\n                softfloat_raiseFlags( softfloat_flag_underflow );\r\n            }\r\n            doIncrement = (UINT64_C( 0x8000000000000000 ) <= sigExtra);\r\n            if (\r\n                   ! roundNearEven\r\n                && (roundingMode != softfloat_round_near_maxMag)\r\n            ) {\r\n                doIncrement =\r\n                    (roundingMode\r\n                         == (sign ? softfloat_round_min : softfloat_round_max))\r\n                        && sigExtra;\r\n            }\r\n        } else if (\r\n               (0x7FFD < exp)\r\n            || ((exp == 0x7FFD)\r\n                    && softfloat_eq128( \r\n                           sig64,\r\n                           sig0,\r\n                           UINT64_C( 0x0001FFFFFFFFFFFF ),\r\n                           UINT64_C( 0xFFFFFFFFFFFFFFFF )\r\n                       )\r\n                    && doIncrement)\r\n        ) {\r\n            /*----------------------------------------------------------------\r\n            *----------------------------------------------------------------*/\r\n            softfloat_raiseFlags(\r\n                softfloat_flag_overflow | softfloat_flag_inexact );\r\n            if (\r\n                   roundNearEven\r\n                || (roundingMode == softfloat_round_near_maxMag)\r\n                || (roundingMode\r\n                        == (sign ? softfloat_round_min : softfloat_round_max))\r\n            ) {\r\n                uiZ64 = packToF128UI64( sign, 0x7FFF, 0 );\r\n                uiZ0  = 0;\r\n            } else {\r\n                uiZ64 =\r\n                    packToF128UI64(\r\n                        sign, 0x7FFE, UINT64_C( 0x0000FFFFFFFFFFFF ) );\r\n                uiZ0 = UINT64_C( 0xFFFFFFFFFFFFFFFF );\r\n            }\r\n            goto uiZ;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( sigExtra ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            sig0 |= 1;\r\n            goto packReturn;\r\n        }\r\n#endif\r\n    }\r\n    if ( doIncrement ) {\r\n        sig128 = softfloat_add128( sig64, sig0, 0, 1 );\r\n        sig64 = sig128.v64;\r\n        sig0 =\r\n            sig128.v0\r\n                & ~(uint64_t)\r\n                       (! (sigExtra & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                            & roundNearEven);\r\n    } else {\r\n        if ( ! (sig64 | sig0) ) exp = 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n packReturn:\r\n    uiZ64 = packToF128UI64( sign, exp, sig64 );\r\n    uiZ0  = sig0;\r\n uiZ:\r\n    uZ.ui.v64 = uiZ64;\r\n    uZ.ui.v0  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_roundPackToF128.c ****/\n/**** start inlining ../../source/s_normRoundPackToF128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n\r\nfloat128_t\r\n softfloat_normRoundPackToF128(\r\n     bool sign, int_fast32_t exp, uint_fast64_t sig64, uint_fast64_t sig0 )\r\n{\r\n    int_fast8_t shiftDist;\r\n    struct uint128 sig128;\r\n    union ui128_f128 uZ;\r\n    uint_fast64_t sigExtra;\r\n    struct uint128_extra sig128Extra;\r\n\r\n    if ( ! sig64 ) {\r\n        exp -= 64;\r\n        sig64 = sig0;\r\n        sig0 = 0;\r\n    }\r\n    shiftDist = softfloat_countLeadingZeros64( sig64 ) - 15;\r\n    exp -= shiftDist;\r\n    if ( 0 <= shiftDist ) {\r\n        if ( shiftDist ) {\r\n            sig128 = softfloat_shortShiftLeft128( sig64, sig0, shiftDist );\r\n            sig64 = sig128.v64;\r\n            sig0  = sig128.v0;\r\n        }\r\n        if ( (uint32_t) exp < 0x7FFD ) {\r\n            uZ.ui.v64 = packToF128UI64( sign, sig64 | sig0 ? exp : 0, sig64 );\r\n            uZ.ui.v0  = sig0;\r\n            return uZ.f;\r\n        }\r\n        sigExtra = 0;\r\n    } else {\r\n        sig128Extra =\r\n            softfloat_shortShiftRightJam128Extra( sig64, sig0, 0, -shiftDist );\r\n        sig64 = sig128Extra.v.v64;\r\n        sig0  = sig128Extra.v.v0;\r\n        sigExtra = sig128Extra.extra;\r\n    }\r\n    return softfloat_roundPackToF128( sign, exp, sig64, sig0, sigExtra );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_normRoundPackToF128.c ****/\n/**** start inlining ../../source/s_addMagsF128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n\r\nfloat128_t\r\n softfloat_addMagsF128(\r\n     uint_fast64_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast64_t uiB64,\r\n     uint_fast64_t uiB0,\r\n     bool signZ\r\n )\r\n{\r\n    int_fast32_t expA;\r\n    struct uint128 sigA;\r\n    int_fast32_t expB;\r\n    struct uint128 sigB;\r\n    int_fast32_t expDiff;\r\n    struct uint128 uiZ, sigZ;\r\n    int_fast32_t expZ;\r\n    uint_fast64_t sigZExtra;\r\n    struct uint128_extra sig128Extra;\r\n    union ui128_f128 uZ;\r\n\r\n    expA = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    expB = expF128UI64( uiB64 );\r\n    sigB.v64 = fracF128UI64( uiB64 );\r\n    sigB.v0  = uiB0;\r\n    expDiff = expA - expB;\r\n    if ( ! expDiff ) {\r\n        if ( expA == 0x7FFF ) {\r\n            if ( sigA.v64 | sigA.v0 | sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n            uiZ.v64 = uiA64;\r\n            uiZ.v0  = uiA0;\r\n            goto uiZ;\r\n        }\r\n        sigZ = softfloat_add128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 );\r\n        if ( ! expA ) {\r\n            uiZ.v64 = packToF128UI64( signZ, 0, sigZ.v64 );\r\n            uiZ.v0  = sigZ.v0;\r\n            goto uiZ;\r\n        }\r\n        expZ = expA;\r\n        sigZ.v64 |= UINT64_C( 0x0002000000000000 );\r\n        sigZExtra = 0;\r\n        goto shiftRight1;\r\n    }\r\n    if ( expDiff < 0 ) {\r\n        if ( expB == 0x7FFF ) {\r\n            if ( sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n            uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );\r\n            uiZ.v0  = 0;\r\n            goto uiZ;\r\n        }\r\n        expZ = expB;\r\n        if ( expA ) {\r\n            sigA.v64 |= UINT64_C( 0x0001000000000000 );\r\n        } else {\r\n            ++expDiff;\r\n            sigZExtra = 0;\r\n            if ( ! expDiff ) goto newlyAligned;\r\n        }\r\n        sig128Extra =\r\n            softfloat_shiftRightJam128Extra( sigA.v64, sigA.v0, 0, -expDiff );\r\n        sigA = sig128Extra.v;\r\n        sigZExtra = sig128Extra.extra;\r\n    } else {\r\n        if ( expA == 0x7FFF ) {\r\n            if ( sigA.v64 | sigA.v0 ) goto propagateNaN;\r\n            uiZ.v64 = uiA64;\r\n            uiZ.v0  = uiA0;\r\n            goto uiZ;\r\n        }\r\n        expZ = expA;\r\n        if ( expB ) {\r\n            sigB.v64 |= UINT64_C( 0x0001000000000000 );\r\n        } else {\r\n            --expDiff;\r\n            sigZExtra = 0;\r\n            if ( ! expDiff ) goto newlyAligned;\r\n        }\r\n        sig128Extra =\r\n            softfloat_shiftRightJam128Extra( sigB.v64, sigB.v0, 0, expDiff );\r\n        sigB = sig128Extra.v;\r\n        sigZExtra = sig128Extra.extra;\r\n    }\r\n newlyAligned:\r\n    sigZ =\r\n        softfloat_add128(\r\n            sigA.v64 | UINT64_C( 0x0001000000000000 ),\r\n            sigA.v0,\r\n            sigB.v64,\r\n            sigB.v0\r\n        );\r\n    --expZ;\r\n    if ( sigZ.v64 < UINT64_C( 0x0002000000000000 ) ) goto roundAndPack;\r\n    ++expZ;\r\n shiftRight1:\r\n    sig128Extra =\r\n        softfloat_shortShiftRightJam128Extra(\r\n            sigZ.v64, sigZ.v0, sigZExtra, 1 );\r\n    sigZ = sig128Extra.v;\r\n    sigZExtra = sig128Extra.extra;\r\n roundAndPack:\r\n    return\r\n        softfloat_roundPackToF128( signZ, expZ, sigZ.v64, sigZ.v0, sigZExtra );\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_addMagsF128.c ****/\n/**** start inlining ../../source/s_subMagsF128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t\r\n softfloat_subMagsF128(\r\n     uint_fast64_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast64_t uiB64,\r\n     uint_fast64_t uiB0,\r\n     bool signZ\r\n )\r\n{\r\n    int_fast32_t expA;\r\n    struct uint128 sigA;\r\n    int_fast32_t expB;\r\n    struct uint128 sigB, sigZ;\r\n    int_fast32_t expDiff, expZ;\r\n    struct uint128 uiZ;\r\n    union ui128_f128 uZ;\r\n\r\n    expA = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    expB = expF128UI64( uiB64 );\r\n    sigB.v64 = fracF128UI64( uiB64 );\r\n    sigB.v0  = uiB0;\r\n    sigA = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 4 );\r\n    sigB = softfloat_shortShiftLeft128( sigB.v64, sigB.v0, 4 );\r\n    expDiff = expA - expB;\r\n    if ( 0 < expDiff ) goto expABigger;\r\n    if ( expDiff < 0 ) goto expBBigger;\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA.v64 | sigA.v0 | sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ.v64 = defaultNaNF128UI64;\r\n        uiZ.v0  = defaultNaNF128UI0;\r\n        goto uiZ;\r\n    }\r\n    expZ = expA;\r\n    if ( ! expZ ) expZ = 1;\r\n    if ( sigB.v64 < sigA.v64 ) goto aBigger;\r\n    if ( sigA.v64 < sigB.v64 ) goto bBigger;\r\n    if ( sigB.v0 < sigA.v0 ) goto aBigger;\r\n    if ( sigA.v0 < sigB.v0 ) goto bBigger;\r\n    uiZ.v64 =\r\n        packToF128UI64(\r\n            (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n    uiZ.v0 = 0;\r\n    goto uiZ;\r\n expBBigger:\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n        uiZ.v64 = packToF128UI64( signZ ^ 1, 0x7FFF, 0 );\r\n        uiZ.v0  = 0;\r\n        goto uiZ;\r\n    }\r\n    if ( expA ) {\r\n        sigA.v64 |= UINT64_C( 0x0010000000000000 );\r\n    } else {\r\n        ++expDiff;\r\n        if ( ! expDiff ) goto newlyAlignedBBigger;\r\n    }\r\n    sigA = softfloat_shiftRightJam128( sigA.v64, sigA.v0, -expDiff );\r\n newlyAlignedBBigger:\r\n    expZ = expB;\r\n    sigB.v64 |= UINT64_C( 0x0010000000000000 );\r\n bBigger:\r\n    signZ = ! signZ;\r\n    sigZ = softfloat_sub128( sigB.v64, sigB.v0, sigA.v64, sigA.v0 );\r\n    goto normRoundPack;\r\n expABigger:\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA.v64 | sigA.v0 ) goto propagateNaN;\r\n        uiZ.v64 = uiA64;\r\n        uiZ.v0  = uiA0;\r\n        goto uiZ;\r\n    }\r\n    if ( expB ) {\r\n        sigB.v64 |= UINT64_C( 0x0010000000000000 );\r\n    } else {\r\n        --expDiff;\r\n        if ( ! expDiff ) goto newlyAlignedABigger;\r\n    }\r\n    sigB = softfloat_shiftRightJam128( sigB.v64, sigB.v0, expDiff );\r\n newlyAlignedABigger:\r\n    expZ = expA;\r\n    sigA.v64 |= UINT64_C( 0x0010000000000000 );\r\n aBigger:\r\n    sigZ = softfloat_sub128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 );\r\n normRoundPack:\r\n    return softfloat_normRoundPackToF128( signZ, expZ - 5, sigZ.v64, sigZ.v0 );\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_subMagsF128.c ****/\n/**** start inlining ../../source/s_mulAddF128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t\r\n softfloat_mulAddF128(\r\n     uint_fast64_t uiA64,\r\n     uint_fast64_t uiA0,\r\n     uint_fast64_t uiB64,\r\n     uint_fast64_t uiB0,\r\n     uint_fast64_t uiC64,\r\n     uint_fast64_t uiC0,\r\n     uint_fast8_t op\r\n )\r\n{\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    struct uint128 sigA;\r\n    bool signB;\r\n    int_fast32_t expB;\r\n    struct uint128 sigB;\r\n    bool signC;\r\n    int_fast32_t expC;\r\n    struct uint128 sigC;\r\n    bool signZ;\r\n    uint_fast64_t magBits;\r\n    struct uint128 uiZ;\r\n    struct exp32_sig128 normExpSig;\r\n    int_fast32_t expZ;\r\n    uint64_t sig256Z[4];\r\n    struct uint128 sigZ;\r\n    int_fast32_t shiftDist, expDiff;\r\n    struct uint128 x128;\r\n    uint64_t sig256C[4];\r\n    static uint64_t zero256[4] = INIT_UINTM4( 0, 0, 0, 0 );\r\n    uint_fast64_t sigZExtra, sig256Z0;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signF128UI64( uiA64 );\r\n    expA  = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    signB = signF128UI64( uiB64 );\r\n    expB  = expF128UI64( uiB64 );\r\n    sigB.v64 = fracF128UI64( uiB64 );\r\n    sigB.v0  = uiB0;\r\n    signC = signF128UI64( uiC64 ) ^ (op == softfloat_mulAdd_subC);\r\n    expC  = expF128UI64( uiC64 );\r\n    sigC.v64 = fracF128UI64( uiC64 );\r\n    sigC.v0  = uiC0;\r\n    signZ = signA ^ signB ^ (op == softfloat_mulAdd_subProd);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if (\r\n            (sigA.v64 | sigA.v0) || ((expB == 0x7FFF) && (sigB.v64 | sigB.v0))\r\n        ) {\r\n            goto propagateNaN_ABC;\r\n        }\r\n        magBits = expB | sigB.v64 | sigB.v0;\r\n        goto infProdArg;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB.v64 | sigB.v0 ) goto propagateNaN_ABC;\r\n        magBits = expA | sigA.v64 | sigA.v0;\r\n        goto infProdArg;\r\n    }\r\n    if ( expC == 0x7FFF ) {\r\n        if ( sigC.v64 | sigC.v0 ) {\r\n            uiZ.v64 = 0;\r\n            uiZ.v0  = 0;\r\n            goto propagateNaN_ZC;\r\n        }\r\n        uiZ.v64 = uiC64;\r\n        uiZ.v0  = uiC0;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! (sigA.v64 | sigA.v0) ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! (sigB.v64 | sigB.v0) ) goto zeroProd;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigB.v64, sigB.v0 );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x3FFE;\r\n    sigA.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sigB.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sigA = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 8 );\r\n    sigB = softfloat_shortShiftLeft128( sigB.v64, sigB.v0, 15 );\r\n    softfloat_mul128To256M( sigA.v64, sigA.v0, sigB.v64, sigB.v0, sig256Z );\r\n    sigZ.v64 = sig256Z[indexWord( 4, 3 )];\r\n    sigZ.v0  = sig256Z[indexWord( 4, 2 )];\r\n    shiftDist = 0;\r\n    if ( ! (sigZ.v64 & UINT64_C( 0x0100000000000000 )) ) {\r\n        --expZ;\r\n        shiftDist = -1;\r\n    }\r\n    if ( ! expC ) {\r\n        if ( ! (sigC.v64 | sigC.v0) ) {\r\n            shiftDist += 8;\r\n            goto sigZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigC.v64, sigC.v0 );\r\n        expC = normExpSig.exp;\r\n        sigC = normExpSig.sig;\r\n    }\r\n    sigC.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sigC = softfloat_shortShiftLeft128( sigC.v64, sigC.v0, 8 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expZ - expC;\r\n    if ( expDiff < 0 ) {\r\n        expZ = expC;\r\n        if ( (signZ == signC) || (expDiff < -1) ) {\r\n            shiftDist -= expDiff;\r\n            if ( shiftDist ) {\r\n                sigZ =\r\n                    softfloat_shiftRightJam128( sigZ.v64, sigZ.v0, shiftDist );\r\n            }\r\n        } else {\r\n            if ( ! shiftDist ) {\r\n                x128 =\r\n                    softfloat_shortShiftRight128(\r\n                        sig256Z[indexWord( 4, 1 )], sig256Z[indexWord( 4, 0 )],\r\n                        1\r\n                    );\r\n                sig256Z[indexWord( 4, 1 )] = (sigZ.v0<<63) | x128.v64;\r\n                sig256Z[indexWord( 4, 0 )] = x128.v0;\r\n                sigZ = softfloat_shortShiftRight128( sigZ.v64, sigZ.v0, 1 );\r\n                sig256Z[indexWord( 4, 3 )] = sigZ.v64;\r\n                sig256Z[indexWord( 4, 2 )] = sigZ.v0;\r\n            }\r\n        }\r\n    } else {\r\n        if ( shiftDist ) softfloat_add256M( sig256Z, sig256Z, sig256Z );\r\n        if ( ! expDiff ) {\r\n            sigZ.v64 = sig256Z[indexWord( 4, 3 )];\r\n            sigZ.v0  = sig256Z[indexWord( 4, 2 )];\r\n        } else {\r\n            sig256C[indexWord( 4, 3 )] = sigC.v64;\r\n            sig256C[indexWord( 4, 2 )] = sigC.v0;\r\n            sig256C[indexWord( 4, 1 )] = 0;\r\n            sig256C[indexWord( 4, 0 )] = 0;\r\n            softfloat_shiftRightJam256M( sig256C, expDiff, sig256C );\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 8;\r\n    if ( signZ == signC ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff <= 0 ) {\r\n            sigZ = softfloat_add128( sigC.v64, sigC.v0, sigZ.v64, sigZ.v0 );\r\n        } else {\r\n            softfloat_add256M( sig256Z, sig256C, sig256Z );\r\n            sigZ.v64 = sig256Z[indexWord( 4, 3 )];\r\n            sigZ.v0  = sig256Z[indexWord( 4, 2 )];\r\n        }\r\n        if ( sigZ.v64 & UINT64_C( 0x0200000000000000 ) ) {\r\n            ++expZ;\r\n            shiftDist = 9;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( expDiff < 0 ) {\r\n            signZ = signC;\r\n            if ( expDiff < -1 ) {\r\n                sigZ =\r\n                    softfloat_sub128( sigC.v64, sigC.v0, sigZ.v64, sigZ.v0 );\r\n                sigZExtra =\r\n                    sig256Z[indexWord( 4, 1 )] | sig256Z[indexWord( 4, 0 )];\r\n                if ( sigZExtra ) {\r\n                    sigZ = softfloat_sub128( sigZ.v64, sigZ.v0, 0, 1 );\r\n                }\r\n                if ( ! (sigZ.v64 & UINT64_C( 0x0100000000000000 )) ) {\r\n                    --expZ;\r\n                    shiftDist = 7;\r\n                }\r\n                goto shiftRightRoundPack;\r\n            } else {\r\n                sig256C[indexWord( 4, 3 )] = sigC.v64;\r\n                sig256C[indexWord( 4, 2 )] = sigC.v0;\r\n                sig256C[indexWord( 4, 1 )] = 0;\r\n                sig256C[indexWord( 4, 0 )] = 0;\r\n                softfloat_sub256M( sig256C, sig256Z, sig256Z );\r\n            }\r\n        } else if ( ! expDiff ) {\r\n            sigZ = softfloat_sub128( sigZ.v64, sigZ.v0, sigC.v64, sigC.v0 );\r\n            if (\r\n                ! (sigZ.v64 | sigZ.v0) && ! sig256Z[indexWord( 4, 1 )]\r\n                    && ! sig256Z[indexWord( 4, 0 )]\r\n            ) {\r\n                goto completeCancellation;\r\n            }\r\n            sig256Z[indexWord( 4, 3 )] = sigZ.v64;\r\n            sig256Z[indexWord( 4, 2 )] = sigZ.v0;\r\n            if ( sigZ.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n                signZ = ! signZ;\r\n                softfloat_sub256M( zero256, sig256Z, sig256Z );\r\n            }\r\n        } else {\r\n            softfloat_sub256M( sig256Z, sig256C, sig256Z );\r\n            if ( 1 < expDiff ) {\r\n                sigZ.v64 = sig256Z[indexWord( 4, 3 )];\r\n                sigZ.v0  = sig256Z[indexWord( 4, 2 )];\r\n                if ( ! (sigZ.v64 & UINT64_C( 0x0100000000000000 )) ) {\r\n                    --expZ;\r\n                    shiftDist = 7;\r\n                }\r\n                goto sigZ;\r\n            }\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sigZ.v64  = sig256Z[indexWord( 4, 3 )];\r\n        sigZ.v0   = sig256Z[indexWord( 4, 2 )];\r\n        sigZExtra = sig256Z[indexWord( 4, 1 )];\r\n        sig256Z0  = sig256Z[indexWord( 4, 0 )];\r\n        if ( sigZ.v64 ) {\r\n            if ( sig256Z0 ) sigZExtra |= 1;\r\n        } else {\r\n            expZ -= 64;\r\n            sigZ.v64  = sigZ.v0;\r\n            sigZ.v0   = sigZExtra;\r\n            sigZExtra = sig256Z0;\r\n            if ( ! sigZ.v64 ) {\r\n                expZ -= 64;\r\n                sigZ.v64  = sigZ.v0;\r\n                sigZ.v0   = sigZExtra;\r\n                sigZExtra = 0;\r\n                if ( ! sigZ.v64 ) {\r\n                    expZ -= 64;\r\n                    sigZ.v64 = sigZ.v0;\r\n                    sigZ.v0  = 0;\r\n                }\r\n            }\r\n        }\r\n        shiftDist = softfloat_countLeadingZeros64( sigZ.v64 );\r\n        expZ += 7 - shiftDist;\r\n        shiftDist = 15 - shiftDist;\r\n        if ( 0 < shiftDist ) goto shiftRightRoundPack;\r\n        if ( shiftDist ) {\r\n            shiftDist = -shiftDist;\r\n            sigZ = softfloat_shortShiftLeft128( sigZ.v64, sigZ.v0, shiftDist );\r\n            x128 = softfloat_shortShiftLeft128( 0, sigZExtra, shiftDist );\r\n            sigZ.v0 |= x128.v64;\r\n            sigZExtra = x128.v0;\r\n        }\r\n        goto roundPack;\r\n    }\r\n sigZ:\r\n    sigZExtra = sig256Z[indexWord( 4, 1 )] | sig256Z[indexWord( 4, 0 )];\r\n shiftRightRoundPack:\r\n    sigZExtra = (uint64_t) (sigZ.v0<<(64 - shiftDist)) | (sigZExtra != 0);\r\n    sigZ = softfloat_shortShiftRight128( sigZ.v64, sigZ.v0, shiftDist );\r\n roundPack:\r\n    return\r\n        softfloat_roundPackToF128(\r\n            signZ, expZ - 1, sigZ.v64, sigZ.v0, sigZExtra );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN_ABC:\r\n    uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );\r\n    goto propagateNaN_ZC;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infProdArg:\r\n    if ( magBits ) {\r\n        uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );\r\n        uiZ.v0 = 0;\r\n        if ( expC != 0x7FFF ) goto uiZ;\r\n        if ( sigC.v64 | sigC.v0 ) goto propagateNaN_ZC;\r\n        if ( signZ == signC ) goto uiZ;\r\n    }\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ.v64 = defaultNaNF128UI64;\r\n    uiZ.v0  = defaultNaNF128UI0;\r\n propagateNaN_ZC:\r\n    uiZ = softfloat_propagateNaNF128UI( uiZ.v64, uiZ.v0, uiC64, uiC0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zeroProd:\r\n    uiZ.v64 = uiC64;\r\n    uiZ.v0  = uiC0;\r\n    if ( ! (expC | sigC.v64 | sigC.v0) && (signZ != signC) ) {\r\n completeCancellation:\r\n        uiZ.v64 =\r\n            packToF128UI64(\r\n                (softfloat_roundingMode == softfloat_round_min), 0, 0 );\r\n        uiZ.v0 = 0;\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/s_mulAddF128.c ****/\n/**** start inlining ../../source/softfloat_state.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifndef THREAD_LOCAL\r\n#define THREAD_LOCAL\r\n#endif\r\n\r\nTHREAD_LOCAL uint_fast8_t softfloat_roundingMode = softfloat_round_near_even;\r\nTHREAD_LOCAL uint_fast8_t softfloat_detectTininess = init_detectTininess;\r\nTHREAD_LOCAL uint_fast8_t softfloat_exceptionFlags = 0;\r\n\r\nTHREAD_LOCAL uint_fast8_t extF80_roundingPrecision = 80;\r\n\r\n/**** ended inlining ../../source/softfloat_state.c ****/\n/**** start inlining ../../source/ui32_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t ui32_to_f16( uint32_t a )\r\n{\r\n    int_fast8_t shiftDist;\r\n    union ui16_f16 u;\r\n    uint_fast16_t sig;\r\n\r\n    shiftDist = softfloat_countLeadingZeros32( a ) - 21;\r\n    if ( 0 <= shiftDist ) {\r\n        u.ui =\r\n            a ? packToF16UI(\r\n                    0, 0x18 - shiftDist, (uint_fast16_t) a<<shiftDist )\r\n                : 0;\r\n        return u.f;\r\n    } else {\r\n        shiftDist += 4;\r\n        sig =\r\n            (shiftDist < 0)\r\n                ? a>>(-shiftDist) | ((uint32_t) (a<<(shiftDist & 31)) != 0)\r\n                : (uint_fast16_t) a<<shiftDist;\r\n        return softfloat_roundPackToF16( 0, 0x1C - shiftDist, sig );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui32_to_f16.c ****/\n/**** start inlining ../../source/ui32_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t ui32_to_f32( uint32_t a )\r\n{\r\n    union ui32_f32 uZ;\r\n\r\n    if ( ! a ) {\r\n        uZ.ui = 0;\r\n        return uZ.f;\r\n    }\r\n    if ( a & 0x80000000 ) {\r\n        return softfloat_roundPackToF32( 0, 0x9D, a>>1 | (a & 1) );\r\n    } else {\r\n        return softfloat_normRoundPackToF32( 0, 0x9C, a );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui32_to_f32.c ****/\n/**** start inlining ../../source/ui32_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t ui32_to_f64( uint32_t a )\r\n{\r\n    uint_fast64_t uiZ;\r\n    int_fast8_t shiftDist;\r\n    union ui64_f64 uZ;\r\n\r\n    if ( ! a ) {\r\n        uiZ = 0;\r\n    } else {\r\n        shiftDist = softfloat_countLeadingZeros32( a ) + 21;\r\n        uiZ =\r\n            packToF64UI( 0, 0x432 - shiftDist, (uint_fast64_t) a<<shiftDist );\r\n    }\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui32_to_f64.c ****/\n/**** start inlining ../../source/ui32_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t ui32_to_extF80( uint32_t a )\r\n{\r\n    uint_fast16_t uiZ64;\r\n    int_fast8_t shiftDist;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    uiZ64 = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros32( a );\r\n        uiZ64 = 0x401E - shiftDist;\r\n        a <<= shiftDist;\r\n    }\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif = (uint_fast64_t) a<<32;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui32_to_extF80.c ****/\n/**** start inlining ../../source/ui32_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid ui32_to_extF80M( uint32_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = ui32_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid ui32_to_extF80M( uint32_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiZ64;\r\n    uint64_t sigZ;\r\n    int_fast8_t shiftDist;\r\n\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uiZ64 = 0;\r\n    sigZ = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros32( a );\r\n        uiZ64 = packToExtF80UI64( 0, 0x401E - shiftDist );\r\n        sigZ = (uint64_t) (a<<shiftDist)<<32;\r\n    }\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = sigZ;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/ui32_to_extF80M.c ****/\n/**** start inlining ../../source/ui32_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t ui32_to_f128( uint32_t a )\r\n{\r\n    uint_fast64_t uiZ64;\r\n    int_fast8_t shiftDist;\r\n    union ui128_f128 uZ;\r\n\r\n    uiZ64 = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros32( a ) + 17;\r\n        uiZ64 =\r\n            packToF128UI64(\r\n                0, 0x402E - shiftDist, (uint_fast64_t) a<<shiftDist );\r\n    }\r\n    uZ.ui.v64 = uiZ64;\r\n    uZ.ui.v0  = 0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui32_to_f128.c ****/\n/**** start inlining ../../source/ui32_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid ui32_to_f128M( uint32_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = ui32_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid ui32_to_f128M( uint32_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr, uiZ96, uiZ64;\r\n    int_fast8_t shiftDist;\r\n    uint64_t normA;\r\n\r\n    zWPtr = (uint32_t *) zPtr;\r\n    uiZ96 = 0;\r\n    uiZ64 = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros32( a ) + 17;\r\n        normA = (uint64_t) a<<shiftDist;\r\n        uiZ96 = packToF128UI96( 0, 0x402E - shiftDist, normA>>32 );\r\n        uiZ64 = normA;\r\n    }\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = uiZ64;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/ui32_to_f128M.c ****/\n/**** start inlining ../../source/ui64_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t ui64_to_f16( uint64_t a )\r\n{\r\n    int_fast8_t shiftDist;\r\n    union ui16_f16 u;\r\n    uint_fast16_t sig;\r\n\r\n    shiftDist = softfloat_countLeadingZeros64( a ) - 53;\r\n    if ( 0 <= shiftDist ) {\r\n        u.ui =\r\n            a ? packToF16UI(\r\n                    0, 0x18 - shiftDist, (uint_fast16_t) a<<shiftDist )\r\n                : 0;\r\n        return u.f;\r\n    } else {\r\n        shiftDist += 4;\r\n        sig =\r\n            (shiftDist < 0) ? softfloat_shortShiftRightJam64( a, -shiftDist )\r\n                : (uint_fast16_t) a<<shiftDist;\r\n        return softfloat_roundPackToF16( 0, 0x1C - shiftDist, sig );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui64_to_f16.c ****/\n/**** start inlining ../../source/ui64_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t ui64_to_f32( uint64_t a )\r\n{\r\n    int_fast8_t shiftDist;\r\n    union ui32_f32 u;\r\n    uint_fast32_t sig;\r\n\r\n    shiftDist = softfloat_countLeadingZeros64( a ) - 40;\r\n    if ( 0 <= shiftDist ) {\r\n        u.ui =\r\n            a ? packToF32UI(\r\n                    0, 0x95 - shiftDist, (uint_fast32_t) a<<shiftDist )\r\n                : 0;\r\n        return u.f;\r\n    } else {\r\n        shiftDist += 7;\r\n        sig =\r\n            (shiftDist < 0) ? softfloat_shortShiftRightJam64( a, -shiftDist )\r\n                : (uint_fast32_t) a<<shiftDist;\r\n        return softfloat_roundPackToF32( 0, 0x9C - shiftDist, sig );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui64_to_f32.c ****/\n/**** start inlining ../../source/ui64_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t ui64_to_f64( uint64_t a )\r\n{\r\n    union ui64_f64 uZ;\r\n\r\n    if ( ! a ) {\r\n        uZ.ui = 0;\r\n        return uZ.f;\r\n    }\r\n    if ( a & UINT64_C( 0x8000000000000000 ) ) {\r\n        return\r\n            softfloat_roundPackToF64(\r\n                0, 0x43D, softfloat_shortShiftRightJam64( a, 1 ) );\r\n    } else {\r\n        return softfloat_normRoundPackToF64( 0, 0x43C, a );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui64_to_f64.c ****/\n/**** start inlining ../../source/ui64_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t ui64_to_extF80( uint64_t a )\r\n{\r\n    uint_fast16_t uiZ64;\r\n    int_fast8_t shiftDist;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    uiZ64 = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros64( a );\r\n        uiZ64 = 0x403E - shiftDist;\r\n        a <<= shiftDist;\r\n    }\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = a;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui64_to_extF80.c ****/\n/**** start inlining ../../source/ui64_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid ui64_to_extF80M( uint64_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = ui64_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid ui64_to_extF80M( uint64_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiZ64;\r\n    uint64_t sigZ;\r\n    int_fast8_t shiftDist;\r\n\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uiZ64 = 0;\r\n    sigZ = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros64( a );\r\n        uiZ64 = packToExtF80UI64( 0, 0x403E - shiftDist );\r\n        sigZ = a<<shiftDist;\r\n    }\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = sigZ;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/ui64_to_extF80M.c ****/\n/**** start inlining ../../source/ui64_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t ui64_to_f128( uint64_t a )\r\n{\r\n    uint_fast64_t uiZ64, uiZ0;\r\n    int_fast8_t shiftDist;\r\n    struct uint128 zSig;\r\n    union ui128_f128 uZ;\r\n\r\n    if ( ! a ) {\r\n        uiZ64 = 0;\r\n        uiZ0  = 0;\r\n    } else {\r\n        shiftDist = softfloat_countLeadingZeros64( a ) + 49;\r\n        if ( 64 <= shiftDist ) {\r\n            zSig.v64 = a<<(shiftDist - 64);\r\n            zSig.v0  = 0;\r\n        } else {\r\n            zSig = softfloat_shortShiftLeft128( 0, a, shiftDist );\r\n        }\r\n        uiZ64 = packToF128UI64( 0, 0x406E - shiftDist, zSig.v64 );\r\n        uiZ0  = zSig.v0;\r\n    }\r\n    uZ.ui.v64 = uiZ64;\r\n    uZ.ui.v0  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/ui64_to_f128.c ****/\n/**** start inlining ../../source/ui64_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All Rights Reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid ui64_to_f128M( uint64_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = ui64_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid ui64_to_f128M( uint64_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr, uiZ96, uiZ64;\r\n    uint_fast8_t shiftDist;\r\n    uint32_t *ptr;\r\n\r\n    zWPtr = (uint32_t *) zPtr;\r\n    uiZ96 = 0;\r\n    uiZ64 = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n    if ( a ) {\r\n        shiftDist = softfloat_countLeadingZeros64( a ) + 17;\r\n        if ( shiftDist < 32 ) {\r\n            ptr = zWPtr + indexMultiwordHi( 4, 3 );\r\n            ptr[indexWord( 3, 2 )] = 0;\r\n            ptr[indexWord( 3, 1 )] = a>>32;\r\n            ptr[indexWord( 3, 0 )] = a;\r\n            softfloat_shortShiftLeft96M( ptr, shiftDist, ptr );\r\n            ptr[indexWordHi( 3 )] =\r\n                packToF128UI96( 0, 0x404E - shiftDist, ptr[indexWordHi( 3 )] );\r\n            return;\r\n        }\r\n        a <<= shiftDist - 32;\r\n        uiZ96 = packToF128UI96( 0, 0x404E - shiftDist, a>>32 );\r\n        uiZ64 = a;\r\n    }\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = uiZ64;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/ui64_to_f128M.c ****/\n/**** start inlining ../../source/i32_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t i32_to_f16( int32_t a )\r\n{\r\n    bool sign;\r\n    uint_fast32_t absA;\r\n    int_fast8_t shiftDist;\r\n    union ui16_f16 u;\r\n    uint_fast16_t sig;\r\n\r\n    sign = (a < 0);\r\n    absA = sign ? -(uint_fast32_t) a : (uint_fast32_t) a;\r\n    shiftDist = softfloat_countLeadingZeros32( absA ) - 21;\r\n    if ( 0 <= shiftDist ) {\r\n        u.ui =\r\n            a ? packToF16UI(\r\n                    sign, 0x18 - shiftDist, (uint_fast16_t) absA<<shiftDist )\r\n                : 0;\r\n        return u.f;\r\n    } else {\r\n        shiftDist += 4;\r\n        sig =\r\n            (shiftDist < 0)\r\n                ? absA>>(-shiftDist)\r\n                      | ((uint32_t) (absA<<(shiftDist & 31)) != 0)\r\n                : (uint_fast16_t) absA<<shiftDist;\r\n        return softfloat_roundPackToF16( sign, 0x1C - shiftDist, sig );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i32_to_f16.c ****/\n/**** start inlining ../../source/i32_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t i32_to_f32( int32_t a )\r\n{\r\n    bool sign;\r\n    union ui32_f32 uZ;\r\n    uint_fast32_t absA;\r\n\r\n    sign = (a < 0);\r\n    if ( ! (a & 0x7FFFFFFF) ) {\r\n        uZ.ui = sign ? packToF32UI( 1, 0x9E, 0 ) : 0;\r\n        return uZ.f;\r\n    }\r\n    absA = sign ? -(uint_fast32_t) a : (uint_fast32_t) a;\r\n    return softfloat_normRoundPackToF32( sign, 0x9C, absA );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i32_to_f32.c ****/\n/**** start inlining ../../source/i32_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t i32_to_f64( int32_t a )\r\n{\r\n    uint_fast64_t uiZ;\r\n    bool sign;\r\n    uint_fast32_t absA;\r\n    int_fast8_t shiftDist;\r\n    union ui64_f64 uZ;\r\n\r\n    if ( ! a ) {\r\n        uiZ = 0;\r\n    } else {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint_fast32_t) a : (uint_fast32_t) a;\r\n        shiftDist = softfloat_countLeadingZeros32( absA ) + 21;\r\n        uiZ =\r\n            packToF64UI(\r\n                sign, 0x432 - shiftDist, (uint_fast64_t) absA<<shiftDist );\r\n    }\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i32_to_f64.c ****/\n/**** start inlining ../../source/i32_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t i32_to_extF80( int32_t a )\r\n{\r\n    uint_fast16_t uiZ64;\r\n    uint_fast32_t absA;\r\n    bool sign;\r\n    int_fast8_t shiftDist;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    uiZ64 = 0;\r\n    absA = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint_fast32_t) a : (uint_fast32_t) a;\r\n        shiftDist = softfloat_countLeadingZeros32( absA );\r\n        uiZ64 = packToExtF80UI64( sign, 0x401E - shiftDist );\r\n        absA <<= shiftDist;\r\n    }\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif = (uint_fast64_t) absA<<32;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i32_to_extF80.c ****/\n/**** start inlining ../../source/i32_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid i32_to_extF80M( int32_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = i32_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid i32_to_extF80M( int32_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiZ64;\r\n    uint64_t sigZ;\r\n    bool sign;\r\n    uint32_t absA;\r\n    int_fast8_t shiftDist;\r\n\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uiZ64 = 0;\r\n    sigZ = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint32_t) a : (uint32_t) a;\r\n        shiftDist = softfloat_countLeadingZeros32( absA );\r\n        uiZ64 = packToExtF80UI64( sign, 0x401E - shiftDist );\r\n        sigZ = (uint64_t) (absA<<shiftDist)<<32;\r\n    }\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = sigZ;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/i32_to_extF80M.c ****/\n/**** start inlining ../../source/i32_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t i32_to_f128( int32_t a )\r\n{\r\n    uint_fast64_t uiZ64;\r\n    bool sign;\r\n    uint_fast32_t absA;\r\n    int_fast8_t shiftDist;\r\n    union ui128_f128 uZ;\r\n\r\n    uiZ64 = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint_fast32_t) a : (uint_fast32_t) a;\r\n        shiftDist = softfloat_countLeadingZeros32( absA ) + 17;\r\n        uiZ64 =\r\n            packToF128UI64(\r\n                sign, 0x402E - shiftDist, (uint_fast64_t) absA<<shiftDist );\r\n    }\r\n    uZ.ui.v64 = uiZ64;\r\n    uZ.ui.v0  = 0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i32_to_f128.c ****/\n/**** start inlining ../../source/i32_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid i32_to_f128M( int32_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = i32_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid i32_to_f128M( int32_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr;\r\n    uint32_t uiZ96, uiZ64;\r\n    bool sign;\r\n    uint32_t absA;\r\n    int_fast8_t shiftDist;\r\n    uint64_t normAbsA;\r\n\r\n    zWPtr = (uint32_t *) zPtr;\r\n    uiZ96 = 0;\r\n    uiZ64 = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint32_t) a : (uint32_t) a;\r\n        shiftDist = softfloat_countLeadingZeros32( absA ) + 17;\r\n        normAbsA = (uint64_t) absA<<shiftDist;\r\n        uiZ96 = packToF128UI96( sign, 0x402E - shiftDist, normAbsA>>32 );\r\n        uiZ64 = normAbsA;\r\n    }\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = uiZ64;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/i32_to_f128M.c ****/\n/**** start inlining ../../source/i64_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t i64_to_f16( int64_t a )\r\n{\r\n    bool sign;\r\n    uint_fast64_t absA;\r\n    int_fast8_t shiftDist;\r\n    union ui16_f16 u;\r\n    uint_fast16_t sig;\r\n\r\n    sign = (a < 0);\r\n    absA = sign ? -(uint_fast64_t) a : (uint_fast64_t) a;\r\n    shiftDist = softfloat_countLeadingZeros64( absA ) - 53;\r\n    if ( 0 <= shiftDist ) {\r\n        u.ui =\r\n            a ? packToF16UI(\r\n                    sign, 0x18 - shiftDist, (uint_fast16_t) absA<<shiftDist )\r\n                : 0;\r\n        return u.f;\r\n    } else {\r\n        shiftDist += 4;\r\n        sig =\r\n            (shiftDist < 0)\r\n                ? softfloat_shortShiftRightJam64( absA, -shiftDist )\r\n                : (uint_fast16_t) absA<<shiftDist;\r\n        return softfloat_roundPackToF16( sign, 0x1C - shiftDist, sig );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i64_to_f16.c ****/\n/**** start inlining ../../source/i64_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t i64_to_f32( int64_t a )\r\n{\r\n    bool sign;\r\n    uint_fast64_t absA;\r\n    int_fast8_t shiftDist;\r\n    union ui32_f32 u;\r\n    uint_fast32_t sig;\r\n\r\n    sign = (a < 0);\r\n    absA = sign ? -(uint_fast64_t) a : (uint_fast64_t) a;\r\n    shiftDist = softfloat_countLeadingZeros64( absA ) - 40;\r\n    if ( 0 <= shiftDist ) {\r\n        u.ui =\r\n            a ? packToF32UI(\r\n                    sign, 0x95 - shiftDist, (uint_fast32_t) absA<<shiftDist )\r\n                : 0;\r\n        return u.f;\r\n    } else {\r\n        shiftDist += 7;\r\n        sig =\r\n            (shiftDist < 0)\r\n                ? softfloat_shortShiftRightJam64( absA, -shiftDist )\r\n                : (uint_fast32_t) absA<<shiftDist;\r\n        return softfloat_roundPackToF32( sign, 0x9C - shiftDist, sig );\r\n    }\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i64_to_f32.c ****/\n/**** start inlining ../../source/i64_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t i64_to_f64( int64_t a )\r\n{\r\n    bool sign;\r\n    union ui64_f64 uZ;\r\n    uint_fast64_t absA;\r\n\r\n    sign = (a < 0);\r\n    if ( ! (a & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) {\r\n        uZ.ui = sign ? packToF64UI( 1, 0x43E, 0 ) : 0;\r\n        return uZ.f;\r\n    }\r\n    absA = sign ? -(uint_fast64_t) a : (uint_fast64_t) a;\r\n    return softfloat_normRoundPackToF64( sign, 0x43C, absA );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i64_to_f64.c ****/\n/**** start inlining ../../source/i64_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t i64_to_extF80( int64_t a )\r\n{\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t absA;\r\n    bool sign;\r\n    int_fast8_t shiftDist;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    uiZ64 = 0;\r\n    absA = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint_fast64_t) a : (uint_fast64_t) a;\r\n        shiftDist = softfloat_countLeadingZeros64( absA );\r\n        uiZ64 = packToExtF80UI64( sign, 0x403E - shiftDist );\r\n        absA <<= shiftDist;\r\n    }\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = absA;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i64_to_extF80.c ****/\n/**** start inlining ../../source/i64_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid i64_to_extF80M( int64_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = i64_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid i64_to_extF80M( int64_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiZ64;\r\n    uint64_t sigZ;\r\n    bool sign;\r\n    uint64_t absA;\r\n    int_fast8_t shiftDist;\r\n\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uiZ64 = 0;\r\n    sigZ = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint64_t) a : (uint64_t) a;\r\n        shiftDist = softfloat_countLeadingZeros64( absA );\r\n        uiZ64 = packToExtF80UI64( sign, 0x403E - shiftDist );\r\n        sigZ = absA<<shiftDist;\r\n    }\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = sigZ;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/i64_to_extF80M.c ****/\n/**** start inlining ../../source/i64_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t i64_to_f128( int64_t a )\r\n{\r\n    uint_fast64_t uiZ64, uiZ0;\r\n    bool sign;\r\n    uint_fast64_t absA;\r\n    int_fast8_t shiftDist;\r\n    struct uint128 zSig;\r\n    union ui128_f128 uZ;\r\n\r\n    if ( ! a ) {\r\n        uiZ64 = 0;\r\n        uiZ0  = 0;\r\n    } else {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint_fast64_t) a : (uint_fast64_t) a;\r\n        shiftDist = softfloat_countLeadingZeros64( absA ) + 49;\r\n        if ( 64 <= shiftDist ) {\r\n            zSig.v64 = absA<<(shiftDist - 64);\r\n            zSig.v0  = 0;\r\n        } else {\r\n            zSig = softfloat_shortShiftLeft128( 0, absA, shiftDist );\r\n        }\r\n        uiZ64 = packToF128UI64( sign, 0x406E - shiftDist, zSig.v64 );\r\n        uiZ0  = zSig.v0;\r\n    }\r\n    uZ.ui.v64 = uiZ64;\r\n    uZ.ui.v0  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/i64_to_f128.c ****/\n/**** start inlining ../../source/i64_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid i64_to_f128M( int64_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = i64_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid i64_to_f128M( int64_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr;\r\n    uint32_t uiZ96, uiZ64;\r\n    bool sign;\r\n    uint64_t absA;\r\n    uint_fast8_t shiftDist;\r\n    uint32_t *ptr;\r\n\r\n    zWPtr = (uint32_t *) zPtr;\r\n    uiZ96 = 0;\r\n    uiZ64 = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n    if ( a ) {\r\n        sign = (a < 0);\r\n        absA = sign ? -(uint64_t) a : (uint64_t) a;\r\n        shiftDist = softfloat_countLeadingZeros64( absA ) + 17;\r\n        if ( shiftDist < 32 ) {\r\n            ptr = zWPtr + indexMultiwordHi( 4, 3 );\r\n            ptr[indexWord( 3, 2 )] = 0;\r\n            ptr[indexWord( 3, 1 )] = absA>>32;\r\n            ptr[indexWord( 3, 0 )] = absA;\r\n            softfloat_shortShiftLeft96M( ptr, shiftDist, ptr );\r\n            ptr[indexWordHi( 3 )] =\r\n                packToF128UI96(\r\n                    sign, 0x404E - shiftDist, ptr[indexWordHi( 3 )] );\r\n            return;\r\n        }\r\n        absA <<= shiftDist - 32;\r\n        uiZ96 = packToF128UI96( sign, 0x404E - shiftDist, absA>>32 );\r\n        uiZ64 = absA;\r\n    }\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = uiZ64;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/i64_to_f128M.c ****/\n/**** start inlining ../../source/f16_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f16_to_ui32( float16_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    uint_fast32_t sig32;\r\n    int_fast8_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            frac ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig32 = frac;\r\n    if ( exp ) {\r\n        sig32 |= 0x0400;\r\n        shiftDist = exp - 0x19;\r\n        if ( (0 <= shiftDist) && ! sign ) {\r\n            return sig32<<shiftDist;\r\n        }\r\n        shiftDist = exp - 0x0D;\r\n        if ( 0 < shiftDist ) sig32 <<= shiftDist;\r\n    }\r\n    return softfloat_roundToUI32( sign, sig32, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_ui32.c ****/\n/**** start inlining ../../source/f16_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f16_to_ui64( float16_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    uint_fast32_t sig32;\r\n    int_fast8_t shiftDist;\r\n#ifndef SOFTFLOAT_FAST_INT64\r\n    uint32_t extSig[3];\r\n#endif\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            frac ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig32 = frac;\r\n    if ( exp ) {\r\n        sig32 |= 0x0400;\r\n        shiftDist = exp - 0x19;\r\n        if ( (0 <= shiftDist) && ! sign ) {\r\n            return sig32<<shiftDist;\r\n        }\r\n        shiftDist = exp - 0x0D;\r\n        if ( 0 < shiftDist ) sig32 <<= shiftDist;\r\n    }\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    return\r\n        softfloat_roundToUI64(\r\n            sign, sig32>>12, (uint_fast64_t) sig32<<52, roundingMode, exact );\r\n#else\r\n    extSig[indexWord( 3, 2 )] = 0;\r\n    extSig[indexWord( 3, 1 )] = sig32>>12;\r\n    extSig[indexWord( 3, 0 )] = sig32<<20;\r\n    return softfloat_roundMToUI64( sign, extSig, roundingMode, exact );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_ui64.c ****/\n/**** start inlining ../../source/f16_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f16_to_i32( float16_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    int_fast32_t sig32;\r\n    int_fast8_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            frac ? i32_fromNaN\r\n                : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig32 = frac;\r\n    if ( exp ) {\r\n        sig32 |= 0x0400;\r\n        shiftDist = exp - 0x19;\r\n        if ( 0 <= shiftDist ) {\r\n            sig32 <<= shiftDist;\r\n            return sign ? -sig32 : sig32;\r\n        }\r\n        shiftDist = exp - 0x0D;\r\n        if ( 0 < shiftDist ) sig32 <<= shiftDist;\r\n    }\r\n    return\r\n        softfloat_roundToI32(\r\n            sign, (uint_fast32_t) sig32, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_i32.c ****/\n/**** start inlining ../../source/f16_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f16_to_i64( float16_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    int_fast32_t sig32;\r\n    int_fast8_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            frac ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig32 = frac;\r\n    if ( exp ) {\r\n        sig32 |= 0x0400;\r\n        shiftDist = exp - 0x19;\r\n        if ( 0 <= shiftDist ) {\r\n            sig32 <<= shiftDist;\r\n            return sign ? -sig32 : sig32;\r\n        }\r\n        shiftDist = exp - 0x0D;\r\n        if ( 0 < shiftDist ) sig32 <<= shiftDist;\r\n    }\r\n    return\r\n        softfloat_roundToI32(\r\n            sign, (uint_fast32_t) sig32, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_i64.c ****/\n/**** start inlining ../../source/f16_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f16_to_ui32_r_minMag( float16_t a, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    int_fast8_t shiftDist;\r\n    bool sign;\r\n    uint_fast32_t alignedSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = exp - 0x0F;\r\n    if ( shiftDist < 0 ) {\r\n        if ( exact && (exp | frac) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF16UI( uiA );\r\n    if ( sign || (exp == 0x1F) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x1F) && frac ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    alignedSig = (uint_fast32_t) (frac | 0x0400)<<shiftDist;\r\n    if ( exact && (alignedSig & 0x3FF) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return alignedSig>>10;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/f16_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f16_to_ui64_r_minMag( float16_t a, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    int_fast8_t shiftDist;\r\n    bool sign;\r\n    uint_fast32_t alignedSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = exp - 0x0F;\r\n    if ( shiftDist < 0 ) {\r\n        if ( exact && (exp | frac) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF16UI( uiA );\r\n    if ( sign || (exp == 0x1F) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x1F) && frac ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    alignedSig = (uint_fast32_t) (frac | 0x0400)<<shiftDist;\r\n    if ( exact && (alignedSig & 0x3FF) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return alignedSig>>10;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/f16_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f16_to_i32_r_minMag( float16_t a, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    int_fast8_t shiftDist;\r\n    bool sign;\r\n    int_fast32_t alignedSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = exp - 0x0F;\r\n    if ( shiftDist < 0 ) {\r\n        if ( exact && (exp | frac) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF16UI( uiA );\r\n    if ( exp == 0x1F ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x1F) && frac ? i32_fromNaN\r\n                : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    alignedSig = (int_fast32_t) (frac | 0x0400)<<shiftDist;\r\n    if ( exact && (alignedSig & 0x3FF) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    alignedSig >>= 10;\r\n    return sign ? -alignedSig : alignedSig;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/f16_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f16_to_i64_r_minMag( float16_t a, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    int_fast8_t shiftDist;\r\n    bool sign;\r\n    int_fast32_t alignedSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = exp - 0x0F;\r\n    if ( shiftDist < 0 ) {\r\n        if ( exact && (exp | frac) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF16UI( uiA );\r\n    if ( exp == 0x1F ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x1F) && frac ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    alignedSig = (int_fast32_t) (frac | 0x0400)<<shiftDist;\r\n    if ( exact && (alignedSig & 0x3FF) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    alignedSig >>= 10;\r\n    return sign ? -alignedSig : alignedSig;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/f16_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f16_to_f32( float16_t a )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast32_t uiZ;\r\n    struct exp8_sig16 normExpSig;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        if ( frac ) {\r\n            softfloat_f16UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF32UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF32UI( sign, 0xFF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ = packToF32UI( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ = packToF32UI( sign, exp + 0x70, (uint_fast32_t) frac<<13 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_f32.c ****/\n/**** start inlining ../../source/f16_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f16_to_f64( float16_t a )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast64_t uiZ;\r\n    struct exp8_sig16 normExpSig;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        if ( frac ) {\r\n            softfloat_f16UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF64UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ = packToF64UI( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ = packToF64UI( sign, exp + 0x3F0, (uint_fast64_t) frac<<42 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_f64.c ****/\n/**** start inlining ../../source/f16_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t f16_to_extF80( float16_t a )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    struct exp8_sig16 normExpSig;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        if ( frac ) {\r\n            softfloat_f16UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToExtF80UI( &commonNaN );\r\n            uiZ64 = uiZ.v64;\r\n            uiZ0  = uiZ.v0;\r\n        } else {\r\n            uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n            uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( frac );\r\n        exp = normExpSig.exp;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = packToExtF80UI64( sign, exp + 0x3FF0 );\r\n    uiZ0  = (uint_fast64_t) (frac | 0x0400)<<53;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_extF80.c ****/\n/**** start inlining ../../source/f16_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f16_to_extF80M( float16_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = f16_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f16_to_extF80M( float16_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    union ui16_f16 uA;\r\n    uint16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint16_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ64;\r\n    uint32_t uiZ32;\r\n    struct exp8_sig16 normExpSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        if ( frac ) {\r\n            softfloat_f16UIToCommonNaN( uiA, &commonNaN );\r\n            softfloat_commonNaNToExtF80M( &commonNaN, zSPtr );\r\n            return;\r\n        }\r\n        uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n        uiZ32 = 0x80000000;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ32 = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( frac );\r\n        exp = normExpSig.exp;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = packToExtF80UI64( sign, exp + 0x3FF0 );\r\n    uiZ32 = 0x80000000 | (uint32_t) frac<<21;\r\n uiZ:\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = (uint64_t) uiZ32<<32;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f16_to_extF80M.c ****/\n/**** start inlining ../../source/f16_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f16_to_f128( float16_t a )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint_fast16_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    struct exp8_sig16 normExpSig;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        if ( frac ) {\r\n            softfloat_f16UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF128UI( &commonNaN );\r\n        } else {\r\n            uiZ.v64 = packToF128UI64( sign, 0x7FFF, 0 );\r\n            uiZ.v0  = 0;\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ.v64 = packToF128UI64( sign, 0, 0 );\r\n            uiZ.v0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ.v64 = packToF128UI64( sign, exp + 0x3FF0, (uint_fast64_t) frac<<38 );\r\n    uiZ.v0  = 0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_to_f128.c ****/\n/**** start inlining ../../source/f16_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f16_to_f128M( float16_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f16_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f16_to_f128M( float16_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr;\r\n    union ui16_f16 uA;\r\n    uint16_t uiA;\r\n    bool sign;\r\n    int_fast8_t exp;\r\n    uint16_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint32_t uiZ96;\r\n    struct exp8_sig16 normExpSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF16UI( uiA );\r\n    exp  = expF16UI( uiA );\r\n    frac = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x1F ) {\r\n        if ( frac ) {\r\n            softfloat_f16UIToCommonNaN( uiA, &commonNaN );\r\n            softfloat_commonNaNToF128M( &commonNaN, zWPtr );\r\n            return;\r\n        }\r\n        uiZ96 = packToF128UI96( sign, 0x7FFF, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ96 = packToF128UI96( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ96 = packToF128UI96( sign, exp + 0x3FF0, (uint32_t) frac<<6 );\r\n uiZ:\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f16_to_f128M.c ****/\n/**** start inlining ../../source/f16_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f16_roundToInt( float16_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    int_fast8_t exp;\r\n    uint_fast16_t uiZ, lastBitMask, roundBitsMask;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp <= 0xE ) {\r\n        if ( !(uint16_t) (uiA<<1) ) return a;\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        uiZ = uiA & packToF16UI( 1, 0, 0 );\r\n        switch ( roundingMode ) {\r\n         case softfloat_round_near_even:\r\n            if ( !fracF16UI( uiA ) ) break;\r\n         case softfloat_round_near_maxMag:\r\n            if ( exp == 0xE ) uiZ |= packToF16UI( 0, 0xF, 0 );\r\n            break;\r\n         case softfloat_round_min:\r\n            if ( uiZ ) uiZ = packToF16UI( 1, 0xF, 0 );\r\n            break;\r\n         case softfloat_round_max:\r\n            if ( !uiZ ) uiZ = packToF16UI( 0, 0xF, 0 );\r\n            break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n         case softfloat_round_odd:\r\n            uiZ |= packToF16UI( 0, 0xF, 0 );\r\n            break;\r\n#endif\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x19 <= exp ) {\r\n        if ( (exp == 0x1F) && fracF16UI( uiA ) ) {\r\n            uiZ = softfloat_propagateNaNF16UI( uiA, 0 );\r\n            goto uiZ;\r\n        }\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ = uiA;\r\n    lastBitMask = (uint_fast16_t) 1<<(0x19 - exp);\r\n    roundBitsMask = lastBitMask - 1;\r\n    if ( roundingMode == softfloat_round_near_maxMag ) {\r\n        uiZ += lastBitMask>>1;\r\n    } else if ( roundingMode == softfloat_round_near_even ) {\r\n        uiZ += lastBitMask>>1;\r\n        if ( !(uiZ & roundBitsMask) ) uiZ &= ~lastBitMask;\r\n    } else if (\r\n        roundingMode\r\n            == (signF16UI( uiZ ) ? softfloat_round_min : softfloat_round_max)\r\n    ) {\r\n        uiZ += roundBitsMask;\r\n    }\r\n    uiZ &= ~roundBitsMask;\r\n    if ( uiZ != uiA ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) uiZ |= lastBitMask;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_roundToInt.c ****/\n/**** start inlining ../../source/f16_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f16_add( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 1)\r\n    float16_t (*magsFuncPtr)( uint_fast16_t, uint_fast16_t );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n#if defined INLINE_LEVEL && (1 <= INLINE_LEVEL)\r\n    if ( signF16UI( uiA ^ uiB ) ) {\r\n        return softfloat_subMagsF16( uiA, uiB );\r\n    } else {\r\n        return softfloat_addMagsF16( uiA, uiB );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        signF16UI( uiA ^ uiB ) ? softfloat_subMagsF16 : softfloat_addMagsF16;\r\n    return (*magsFuncPtr)( uiA, uiB );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_add.c ****/\n/**** start inlining ../../source/f16_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f16_sub( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 1)\r\n    float16_t (*magsFuncPtr)( uint_fast16_t, uint_fast16_t );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n#if defined INLINE_LEVEL && (1 <= INLINE_LEVEL)\r\n    if ( signF16UI( uiA ^ uiB ) ) {\r\n        return softfloat_addMagsF16( uiA, uiB );\r\n    } else {\r\n        return softfloat_subMagsF16( uiA, uiB );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        signF16UI( uiA ^ uiB ) ? softfloat_addMagsF16 : softfloat_subMagsF16;\r\n    return (*magsFuncPtr)( uiA, uiB );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_sub.c ****/\n/**** start inlining ../../source/f16_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f16_mul( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool signA;\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    bool signB;\r\n    int_fast8_t expB;\r\n    uint_fast16_t sigB;\r\n    bool signZ;\r\n    uint_fast16_t magBits;\r\n    struct exp8_sig16 normExpSig;\r\n    int_fast8_t expZ;\r\n    uint_fast32_t sig32Z;\r\n    uint_fast16_t sigZ, uiZ;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF16UI( uiA );\r\n    expA  = expF16UI( uiA );\r\n    sigA  = fracF16UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF16UI( uiB );\r\n    expB  = expF16UI( uiB );\r\n    sigB  = fracF16UI( uiB );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x1F ) {\r\n        if ( sigA || ((expB == 0x1F) && sigB) ) goto propagateNaN;\r\n        magBits = expB | sigB;\r\n        goto infArg;\r\n    }\r\n    if ( expB == 0x1F ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        magBits = expA | sigA;\r\n        goto infArg;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0xF;\r\n    sigA = (sigA | 0x0400)<<4;\r\n    sigB = (sigB | 0x0400)<<5;\r\n    sig32Z = (uint_fast32_t) sigA * sigB;\r\n    sigZ = sig32Z>>16;\r\n    if ( sig32Z & 0xFFFF ) sigZ |= 1;\r\n    if ( sigZ < 0x4000 ) {\r\n        --expZ;\r\n        sigZ <<= 1;\r\n    }\r\n    return softfloat_roundPackToF16( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infArg:\r\n    if ( ! magBits ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ = defaultNaNF16UI;\r\n    } else {\r\n        uiZ = packToF16UI( signZ, 0x1F, 0 );\r\n    }\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ = packToF16UI( signZ, 0, 0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_mul.c ****/\n/**** start inlining ../../source/f16_mulAdd.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f16_mulAdd( float16_t a, float16_t b, float16_t c )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    union ui16_f16 uC;\r\n    uint_fast16_t uiC;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    uC.f = c;\r\n    uiC = uC.ui;\r\n    return softfloat_mulAddF16( uiA, uiB, uiC, 0 );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_mulAdd.c ****/\n/**** start inlining ../../source/f16_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextern const uint16_t softfloat_approxRecip_1k0s[];\r\nextern const uint16_t softfloat_approxRecip_1k1s[];\r\n\r\nfloat16_t f16_div( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool signA;\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    bool signB;\r\n    int_fast8_t expB;\r\n    uint_fast16_t sigB;\r\n    bool signZ;\r\n    struct exp8_sig16 normExpSig;\r\n    int_fast8_t expZ;\r\n#ifdef SOFTFLOAT_FAST_DIV32TO16\r\n    uint_fast32_t sig32A;\r\n    uint_fast16_t sigZ;\r\n#else\r\n    int index;\r\n    uint16_t r0;\r\n    uint_fast16_t sigZ, rem;\r\n#endif\r\n    uint_fast16_t uiZ;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF16UI( uiA );\r\n    expA  = expF16UI( uiA );\r\n    sigA  = fracF16UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF16UI( uiB );\r\n    expB  = expF16UI( uiB );\r\n    sigB  = fracF16UI( uiB );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x1F ) {\r\n        if ( sigA ) goto propagateNaN;\r\n        if ( expB == 0x1F ) {\r\n            if ( sigB ) goto propagateNaN;\r\n            goto invalid;\r\n        }\r\n        goto infinity;\r\n    }\r\n    if ( expB == 0x1F ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) {\r\n            if ( ! (expA | sigA) ) goto invalid;\r\n            softfloat_raiseFlags( softfloat_flag_infinite );\r\n            goto infinity;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0xE;\r\n    sigA |= 0x0400;\r\n    sigB |= 0x0400;\r\n#ifdef SOFTFLOAT_FAST_DIV32TO16\r\n    if ( sigA < sigB ) {\r\n        --expZ;\r\n        sig32A = (uint_fast32_t) sigA<<15;\r\n    } else {\r\n        sig32A = (uint_fast32_t) sigA<<14;\r\n    }\r\n    sigZ = sig32A / sigB;\r\n    if ( ! (sigZ & 7) ) sigZ |= ((uint_fast32_t) sigB * sigZ != sig32A);\r\n#else\r\n    if ( sigA < sigB ) {\r\n        --expZ;\r\n        sigA <<= 5;\r\n    } else {\r\n        sigA <<= 4;\r\n    }\r\n    index = sigB>>6 & 0xF;\r\n    r0 = softfloat_approxRecip_1k0s[index]\r\n             - (((uint_fast32_t) softfloat_approxRecip_1k1s[index]\r\n                     * (sigB & 0x3F))\r\n                    >>10);\r\n    sigZ = ((uint_fast32_t) sigA * r0)>>16;\r\n    rem = (sigA<<10) - sigZ * sigB;\r\n    sigZ += (rem * (uint_fast32_t) r0)>>26;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    ++sigZ;\r\n    if ( ! (sigZ & 7) ) {\r\n        sigZ &= ~1;\r\n        rem = (sigA<<10) - sigZ * sigB;\r\n        if ( rem & 0x8000 ) {\r\n            sigZ -= 2;\r\n        } else {\r\n            if ( rem ) sigZ |= 1;\r\n        }\r\n    }\r\n#endif\r\n    return softfloat_roundPackToF16( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF16UI;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ = packToF16UI( signZ, 0x1F, 0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ = packToF16UI( signZ, 0, 0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_div.c ****/\n/**** start inlining ../../source/f16_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f16_rem( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool signA;\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    int_fast8_t expB;\r\n    uint_fast16_t sigB;\r\n    struct exp8_sig16 normExpSig;\r\n    uint16_t rem;\r\n    int_fast8_t expDiff;\r\n    uint_fast16_t q;\r\n    uint32_t recip32, q32;\r\n    uint16_t altRem, meanRem;\r\n    bool signRem;\r\n    uint_fast16_t uiZ;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF16UI( uiA );\r\n    expA  = expF16UI( uiA );\r\n    sigA  = fracF16UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    expB = expF16UI( uiB );\r\n    sigB = fracF16UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x1F ) {\r\n        if ( sigA || ((expB == 0x1F) && sigB) ) goto propagateNaN;\r\n        goto invalid;\r\n    }\r\n    if ( expB == 0x1F ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto invalid;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) return a;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    rem = sigA | 0x0400;\r\n    sigB |= 0x0400;\r\n    expDiff = expA - expB;\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff < -1 ) return a;\r\n        sigB <<= 3;\r\n        if ( expDiff ) {\r\n            rem <<= 2;\r\n            q = 0;\r\n        } else {\r\n            rem <<= 3;\r\n            q = (sigB <= rem);\r\n            if ( q ) rem -= sigB;\r\n        }\r\n    } else {\r\n        recip32 = softfloat_approxRecip32_1( (uint_fast32_t) sigB<<21 );\r\n        /*--------------------------------------------------------------------\r\n        | Changing the shift of `rem' here requires also changing the initial\r\n        | subtraction from `expDiff'.\r\n        *--------------------------------------------------------------------*/\r\n        rem <<= 4;\r\n        expDiff -= 31;\r\n        /*--------------------------------------------------------------------\r\n        | The scale of `sigB' affects how many bits are obtained during each\r\n        | cycle of the loop.  Currently this is 29 bits per loop iteration,\r\n        | which is believed to be the maximum possible.\r\n        *--------------------------------------------------------------------*/\r\n        sigB <<= 3;\r\n        for (;;) {\r\n            q32 = (rem * (uint_fast64_t) recip32)>>16;\r\n            if ( expDiff < 0 ) break;\r\n            rem = -((uint_fast16_t) q32 * sigB);\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -30 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q32 >>= ~expDiff & 31;\r\n        q = q32;\r\n        rem = (rem<<(expDiff + 30)) - q * sigB;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    do {\r\n        altRem = rem;\r\n        ++q;\r\n        rem -= sigB;\r\n    } while ( ! (rem & 0x8000) );\r\n    meanRem = rem + altRem;\r\n    if ( (meanRem & 0x8000) || (! meanRem && (q & 1)) ) rem = altRem;\r\n    signRem = signA;\r\n    if ( 0x8000 <= rem ) {\r\n        signRem = ! signRem;\r\n        rem = -rem;\r\n    }\r\n    return softfloat_normRoundPackToF16( signRem, expB, rem );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );\r\n    goto uiZ;\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF16UI;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_rem.c ****/\n/**** start inlining ../../source/f16_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextern const uint16_t softfloat_approxRecipSqrt_1k0s[];\r\nextern const uint16_t softfloat_approxRecipSqrt_1k1s[];\r\n\r\nfloat16_t f16_sqrt( float16_t a )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    bool signA;\r\n    int_fast8_t expA;\r\n    uint_fast16_t sigA, uiZ;\r\n    struct exp8_sig16 normExpSig;\r\n    int_fast8_t expZ;\r\n    int index;\r\n    uint_fast16_t r0;\r\n    uint_fast32_t ESqrR0;\r\n    uint16_t sigma0;\r\n    uint_fast16_t recipSqrt16, sigZ, shiftedSigZ;\r\n    uint16_t negRem;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF16UI( uiA );\r\n    expA  = expF16UI( uiA );\r\n    sigA  = fracF16UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x1F ) {\r\n        if ( sigA ) {\r\n            uiZ = softfloat_propagateNaNF16UI( uiA, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( ! signA ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signA ) {\r\n        if ( ! (expA | sigA) ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) return a;\r\n        normExpSig = softfloat_normSubnormalF16Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0xF)>>1) + 0xE;\r\n    expA &= 1;\r\n    sigA |= 0x0400;\r\n    index = (sigA>>6 & 0xE) + expA;\r\n    r0 = softfloat_approxRecipSqrt_1k0s[index]\r\n             - (((uint_fast32_t) softfloat_approxRecipSqrt_1k1s[index]\r\n                     * (sigA & 0x7F))\r\n                    >>11);\r\n    ESqrR0 = ((uint_fast32_t) r0 * r0)>>1;\r\n    if ( expA ) ESqrR0 >>= 1;\r\n    sigma0 = ~(uint_fast16_t) ((ESqrR0 * sigA)>>16);\r\n    recipSqrt16 = r0 + (((uint_fast32_t) r0 * sigma0)>>25);\r\n    if ( ! (recipSqrt16 & 0x8000) ) recipSqrt16 = 0x8000;\r\n    sigZ = ((uint_fast32_t) (sigA<<5) * recipSqrt16)>>16;\r\n    if ( expA ) sigZ >>= 1;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    ++sigZ;\r\n    if ( ! (sigZ & 7) ) {\r\n        shiftedSigZ = sigZ>>1;\r\n        negRem = shiftedSigZ * shiftedSigZ;\r\n        sigZ &= ~1;\r\n        if ( negRem & 0x8000 ) {\r\n            sigZ |= 1;\r\n        } else {\r\n            if ( negRem ) --sigZ;\r\n        }\r\n    }\r\n    return softfloat_roundPackToF16( 0, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF16UI;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_sqrt.c ****/\n/**** start inlining ../../source/f16_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_eq( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF16UI( uiA ) || isNaNF16UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF16UI( uiA ) || softfloat_isSigNaNF16UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    return (uiA == uiB) || ! (uint16_t) ((uiA | uiB)<<1);\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_eq.c ****/\n/**** start inlining ../../source/f16_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_le( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF16UI( uiA ) || isNaNF16UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF16UI( uiA );\r\n    signB = signF16UI( uiB );\r\n    return\r\n        (signA != signB) ? signA || ! (uint16_t) ((uiA | uiB)<<1)\r\n            : (uiA == uiB) || (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_le.c ****/\n/**** start inlining ../../source/f16_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_lt( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF16UI( uiA ) || isNaNF16UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF16UI( uiA );\r\n    signB = signF16UI( uiB );\r\n    return\r\n        (signA != signB) ? signA && ((uint16_t) ((uiA | uiB)<<1) != 0)\r\n            : (uiA != uiB) && (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_lt.c ****/\n/**** start inlining ../../source/f16_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_eq_signaling( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF16UI( uiA ) || isNaNF16UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    return (uiA == uiB) || ! (uint16_t) ((uiA | uiB)<<1);\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_eq_signaling.c ****/\n/**** start inlining ../../source/f16_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_le_quiet( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF16UI( uiA ) || isNaNF16UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF16UI( uiA ) || softfloat_isSigNaNF16UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF16UI( uiA );\r\n    signB = signF16UI( uiB );\r\n    return\r\n        (signA != signB) ? signA || ! (uint16_t) ((uiA | uiB)<<1)\r\n            : (uiA == uiB) || (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_le_quiet.c ****/\n/**** start inlining ../../source/f16_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_lt_quiet( float16_t a, float16_t b )\r\n{\r\n    union ui16_f16 uA;\r\n    uint_fast16_t uiA;\r\n    union ui16_f16 uB;\r\n    uint_fast16_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF16UI( uiA ) || isNaNF16UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF16UI( uiA ) || softfloat_isSigNaNF16UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF16UI( uiA );\r\n    signB = signF16UI( uiB );\r\n    return\r\n        (signA != signB) ? signA && ((uint16_t) ((uiA | uiB)<<1) != 0)\r\n            : (uiA != uiB) && (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_lt_quiet.c ****/\n/**** start inlining ../../source/f16_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f16_isSignalingNaN( float16_t a )\r\n{\r\n    union ui16_f16 uA;\r\n\r\n    uA.f = a;\r\n    return softfloat_isSigNaNF16UI( uA.ui );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f16_isSignalingNaN.c ****/\n/**** start inlining ../../source/f32_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f32_to_ui32( float32_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    uint_fast64_t sig64;\r\n    int_fast16_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    sig  = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (ui32_fromNaN != ui32_fromPosOverflow) || (ui32_fromNaN != ui32_fromNegOverflow)\r\n    if ( (exp == 0xFF) && sig ) {\r\n#if (ui32_fromNaN == ui32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (ui32_fromNaN == ui32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return ui32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= 0x00800000;\r\n    sig64 = (uint_fast64_t) sig<<32;\r\n    shiftDist = 0xAA - exp;\r\n    if ( 0 < shiftDist ) sig64 = softfloat_shiftRightJam64( sig64, shiftDist );\r\n    return softfloat_roundToUI32( sign, sig64, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_ui32.c ****/\n/**** start inlining ../../source/f32_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f32_to_ui64( float32_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    int_fast16_t shiftDist;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    uint_fast64_t sig64, extra;\r\n    struct uint64_extra sig64Extra;\r\n#else\r\n    uint32_t extSig[3];\r\n#endif\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    sig  = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0xBE - exp;\r\n    if ( shiftDist < 0 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0xFF) && sig ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= 0x00800000;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    sig64 = (uint_fast64_t) sig<<40;\r\n    extra = 0;\r\n    if ( shiftDist ) {\r\n        sig64Extra = softfloat_shiftRightJam64Extra( sig64, 0, shiftDist );\r\n        sig64 = sig64Extra.v;\r\n        extra = sig64Extra.extra;\r\n    }\r\n    return softfloat_roundToUI64( sign, sig64, extra, roundingMode, exact );\r\n#else\r\n    extSig[indexWord( 3, 2 )] = sig<<8;\r\n    extSig[indexWord( 3, 1 )] = 0;\r\n    extSig[indexWord( 3, 0 )] = 0;\r\n    if ( shiftDist ) softfloat_shiftRightJam96M( extSig, shiftDist, extSig );\r\n    return softfloat_roundMToUI64( sign, extSig, roundingMode, exact );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_ui64.c ****/\n/**** start inlining ../../source/f32_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f32_to_i32( float32_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    uint_fast64_t sig64;\r\n    int_fast16_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    sig  = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (i32_fromNaN != i32_fromPosOverflow) || (i32_fromNaN != i32_fromNegOverflow)\r\n    if ( (exp == 0xFF) && sig ) {\r\n#if (i32_fromNaN == i32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (i32_fromNaN == i32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return i32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= 0x00800000;\r\n    sig64 = (uint_fast64_t) sig<<32;\r\n    shiftDist = 0xAA - exp;\r\n    if ( 0 < shiftDist ) sig64 = softfloat_shiftRightJam64( sig64, shiftDist );\r\n    return softfloat_roundToI32( sign, sig64, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_i32.c ****/\n/**** start inlining ../../source/f32_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f32_to_i64( float32_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    int_fast16_t shiftDist;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    uint_fast64_t sig64, extra;\r\n    struct uint64_extra sig64Extra;\r\n#else\r\n    uint32_t extSig[3];\r\n#endif\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    sig  = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0xBE - exp;\r\n    if ( shiftDist < 0 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0xFF) && sig ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= 0x00800000;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    sig64 = (uint_fast64_t) sig<<40;\r\n    extra = 0;\r\n    if ( shiftDist ) {\r\n        sig64Extra = softfloat_shiftRightJam64Extra( sig64, 0, shiftDist );\r\n        sig64 = sig64Extra.v;\r\n        extra = sig64Extra.extra;\r\n    }\r\n    return softfloat_roundToI64( sign, sig64, extra, roundingMode, exact );\r\n#else\r\n    extSig[indexWord( 3, 2 )] = sig<<8;\r\n    extSig[indexWord( 3, 1 )] = 0;\r\n    extSig[indexWord( 3, 0 )] = 0;\r\n    if ( shiftDist ) softfloat_shiftRightJam96M( extSig, shiftDist, extSig );\r\n    return softfloat_roundMToI64( sign, extSig, roundingMode, exact );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_i64.c ****/\n/**** start inlining ../../source/f32_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f32_to_ui32_r_minMag( float32_t a, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    uint_fast32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF32UI( uiA );\r\n    sig = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x9E - exp;\r\n    if ( 32 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF32UI( uiA );\r\n    if ( sign || (shiftDist < 0) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0xFF) && sig ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = (sig | 0x00800000)<<8;\r\n    z = sig>>shiftDist;\r\n    if ( exact && (z<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/f32_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f32_to_ui64_r_minMag( float32_t a, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    uint_fast64_t sig64, z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF32UI( uiA );\r\n    sig = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0xBE - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF32UI( uiA );\r\n    if ( sign || (shiftDist < 0) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0xFF) && sig ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig |= 0x00800000;\r\n    sig64 = (uint_fast64_t) sig<<40;\r\n    z = sig64>>shiftDist;\r\n    shiftDist = 40 - shiftDist;\r\n    if ( exact && (shiftDist < 0) && (uint32_t) (sig<<(shiftDist & 31)) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/f32_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f32_to_i32_r_minMag( float32_t a, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    int_fast32_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF32UI( uiA );\r\n    sig = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x9E - exp;\r\n    if ( 32 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF32UI( uiA );\r\n    if ( shiftDist <= 0 ) {\r\n        if ( uiA == packToF32UI( 1, 0x9E, 0 ) ) return -0x7FFFFFFF - 1;\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0xFF) && sig ? i32_fromNaN\r\n                : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = (sig | 0x00800000)<<8;\r\n    absZ = sig>>shiftDist;\r\n    if ( exact && ((uint_fast32_t) absZ<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/f32_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f32_to_i64_r_minMag( float32_t a, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast32_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    uint_fast64_t sig64;\r\n    int_fast64_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF32UI( uiA );\r\n    sig = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0xBE - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF32UI( uiA );\r\n    if ( shiftDist <= 0 ) {\r\n        if ( uiA == packToF32UI( 1, 0xBE, 0 ) ) {\r\n            return -INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1;\r\n        }\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0xFF) && sig ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig |= 0x00800000;\r\n    sig64 = (uint_fast64_t) sig<<40;\r\n    absZ = sig64>>shiftDist;\r\n    shiftDist = 40 - shiftDist;\r\n    if ( exact && (shiftDist < 0) && (uint32_t) (sig<<(shiftDist & 31)) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/f32_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f32_to_f16( float32_t a )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ, frac16;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    frac = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0xFF ) {\r\n        if ( frac ) {\r\n            softfloat_f32UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF16UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF16UI( sign, 0x1F, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac16 = frac>>9 | ((frac & 0x1FF) != 0);\r\n    if ( ! (exp | frac16) ) {\r\n        uiZ = packToF16UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    return softfloat_roundPackToF16( sign, exp - 0x71, frac16 | 0x4000 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_f16.c ****/\n/**** start inlining ../../source/f32_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f32_to_f64( float32_t a )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast64_t uiZ;\r\n    struct exp16_sig32 normExpSig;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    frac = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0xFF ) {\r\n        if ( frac ) {\r\n            softfloat_f32UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF64UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ = packToF64UI( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ = packToF64UI( sign, exp + 0x380, (uint_fast64_t) frac<<29 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_f64.c ****/\n/**** start inlining ../../source/f32_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t f32_to_extF80( float32_t a )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    struct exp16_sig32 normExpSig;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    frac = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0xFF ) {\r\n        if ( frac ) {\r\n            softfloat_f32UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToExtF80UI( &commonNaN );\r\n            uiZ64 = uiZ.v64;\r\n            uiZ0  = uiZ.v0;\r\n        } else {\r\n            uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n            uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( frac );\r\n        exp = normExpSig.exp;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = packToExtF80UI64( sign, exp + 0x3F80 );\r\n    uiZ0  = (uint_fast64_t) (frac | 0x00800000)<<40;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_extF80.c ****/\n/**** start inlining ../../source/f32_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f32_to_extF80M( float32_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = f32_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f32_to_extF80M( float32_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    union ui32_f32 uA;\r\n    uint32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint32_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ64;\r\n    uint32_t uiZ32;\r\n    struct exp16_sig32 normExpSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    frac = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0xFF ) {\r\n        if ( frac ) {\r\n            softfloat_f32UIToCommonNaN( uiA, &commonNaN );\r\n            softfloat_commonNaNToExtF80M( &commonNaN, zSPtr );\r\n            return;\r\n        }\r\n        uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n        uiZ32 = 0x80000000;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ32 = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( frac );\r\n        exp = normExpSig.exp;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = packToExtF80UI64( sign, exp + 0x3F80 );\r\n    uiZ32 = 0x80000000 | (uint32_t) frac<<8;\r\n uiZ:\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = (uint64_t) uiZ32<<32;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f32_to_extF80M.c ****/\n/**** start inlining ../../source/f32_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f32_to_f128( float32_t a )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast32_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    struct exp16_sig32 normExpSig;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    frac = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0xFF ) {\r\n        if ( frac ) {\r\n            softfloat_f32UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF128UI( &commonNaN );\r\n        } else {\r\n            uiZ.v64 = packToF128UI64( sign, 0x7FFF, 0 );\r\n            uiZ.v0  = 0;\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ.v64 = packToF128UI64( sign, 0, 0 );\r\n            uiZ.v0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ.v64 = packToF128UI64( sign, exp + 0x3F80, (uint_fast64_t) frac<<25 );\r\n    uiZ.v0  = 0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_to_f128.c ****/\n/**** start inlining ../../source/f32_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f32_to_f128M( float32_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f32_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f32_to_f128M( float32_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr;\r\n    union ui32_f32 uA;\r\n    uint32_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint32_t frac, uiZ64;\r\n    struct commonNaN commonNaN;\r\n    uint32_t uiZ96;\r\n    struct exp16_sig32 normExpSig;\r\n    uint64_t frac64;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF32UI( uiA );\r\n    exp  = expF32UI( uiA );\r\n    frac = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = 0;\r\n    if ( exp == 0xFF ) {\r\n        if ( frac ) {\r\n            softfloat_f32UIToCommonNaN( uiA, &commonNaN );\r\n            softfloat_commonNaNToF128M( &commonNaN, zWPtr );\r\n            return;\r\n        }\r\n        uiZ96 = packToF128UI96( sign, 0x7FFF, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ96 = packToF128UI96( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac64 = (uint64_t) frac<<25;\r\n    uiZ96 = packToF128UI96( sign, exp + 0x3F80, frac64>>32 );\r\n    uiZ64 = frac64;\r\n uiZ:\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = uiZ64;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f32_to_f128M.c ****/\n/**** start inlining ../../source/f32_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_roundToInt( float32_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast32_t uiZ, lastBitMask, roundBitsMask;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp <= 0x7E ) {\r\n        if ( !(uint32_t) (uiA<<1) ) return a;\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        uiZ = uiA & packToF32UI( 1, 0, 0 );\r\n        switch ( roundingMode ) {\r\n         case softfloat_round_near_even:\r\n            if ( !fracF32UI( uiA ) ) break;\r\n         case softfloat_round_near_maxMag:\r\n            if ( exp == 0x7E ) uiZ |= packToF32UI( 0, 0x7F, 0 );\r\n            break;\r\n         case softfloat_round_min:\r\n            if ( uiZ ) uiZ = packToF32UI( 1, 0x7F, 0 );\r\n            break;\r\n         case softfloat_round_max:\r\n            if ( !uiZ ) uiZ = packToF32UI( 0, 0x7F, 0 );\r\n            break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n         case softfloat_round_odd:\r\n            uiZ |= packToF32UI( 0, 0x7F, 0 );\r\n            break;\r\n#endif\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x96 <= exp ) {\r\n        if ( (exp == 0xFF) && fracF32UI( uiA ) ) {\r\n            uiZ = softfloat_propagateNaNF32UI( uiA, 0 );\r\n            goto uiZ;\r\n        }\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ = uiA;\r\n    lastBitMask = (uint_fast32_t) 1<<(0x96 - exp);\r\n    roundBitsMask = lastBitMask - 1;\r\n    if ( roundingMode == softfloat_round_near_maxMag ) {\r\n        uiZ += lastBitMask>>1;\r\n    } else if ( roundingMode == softfloat_round_near_even ) {\r\n        uiZ += lastBitMask>>1;\r\n        if ( !(uiZ & roundBitsMask) ) uiZ &= ~lastBitMask;\r\n    } else if (\r\n        roundingMode\r\n            == (signF32UI( uiZ ) ? softfloat_round_min : softfloat_round_max)\r\n    ) {\r\n        uiZ += roundBitsMask;\r\n    }\r\n    uiZ &= ~roundBitsMask;\r\n    if ( uiZ != uiA ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) uiZ |= lastBitMask;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_roundToInt.c ****/\n/**** start inlining ../../source/f32_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_add( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 1)\r\n    float32_t (*magsFuncPtr)( uint_fast32_t, uint_fast32_t );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n#if defined INLINE_LEVEL && (1 <= INLINE_LEVEL)\r\n    if ( signF32UI( uiA ^ uiB ) ) {\r\n        return softfloat_subMagsF32( uiA, uiB );\r\n    } else {\r\n        return softfloat_addMagsF32( uiA, uiB );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        signF32UI( uiA ^ uiB ) ? softfloat_subMagsF32 : softfloat_addMagsF32;\r\n    return (*magsFuncPtr)( uiA, uiB );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_add.c ****/\n/**** start inlining ../../source/f32_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_sub( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 1)\r\n    float32_t (*magsFuncPtr)( uint_fast32_t, uint_fast32_t );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n#if defined INLINE_LEVEL && (1 <= INLINE_LEVEL)\r\n    if ( signF32UI( uiA ^ uiB ) ) {\r\n        return softfloat_addMagsF32( uiA, uiB );\r\n    } else {\r\n        return softfloat_subMagsF32( uiA, uiB );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        signF32UI( uiA ^ uiB ) ? softfloat_addMagsF32 : softfloat_subMagsF32;\r\n    return (*magsFuncPtr)( uiA, uiB );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_sub.c ****/\n/**** start inlining ../../source/f32_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_mul( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint_fast32_t sigB;\r\n    bool signZ;\r\n    uint_fast32_t magBits;\r\n    struct exp16_sig32 normExpSig;\r\n    int_fast16_t expZ;\r\n    uint_fast32_t sigZ, uiZ;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF32UI( uiA );\r\n    expA  = expF32UI( uiA );\r\n    sigA  = fracF32UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF32UI( uiB );\r\n    expB  = expF32UI( uiB );\r\n    sigB  = fracF32UI( uiB );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0xFF ) {\r\n        if ( sigA || ((expB == 0xFF) && sigB) ) goto propagateNaN;\r\n        magBits = expB | sigB;\r\n        goto infArg;\r\n    }\r\n    if ( expB == 0xFF ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        magBits = expA | sigA;\r\n        goto infArg;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x7F;\r\n    sigA = (sigA | 0x00800000)<<7;\r\n    sigB = (sigB | 0x00800000)<<8;\r\n    sigZ = softfloat_shortShiftRightJam64( (uint_fast64_t) sigA * sigB, 32 );\r\n    if ( sigZ < 0x40000000 ) {\r\n        --expZ;\r\n        sigZ <<= 1;\r\n    }\r\n    return softfloat_roundPackToF32( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF32UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infArg:\r\n    if ( ! magBits ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ = defaultNaNF32UI;\r\n    } else {\r\n        uiZ = packToF32UI( signZ, 0xFF, 0 );\r\n    }\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ = packToF32UI( signZ, 0, 0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_mul.c ****/\n/**** start inlining ../../source/f32_mulAdd.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_mulAdd( float32_t a, float32_t b, float32_t c )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    union ui32_f32 uC;\r\n    uint_fast32_t uiC;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    uC.f = c;\r\n    uiC = uC.ui;\r\n    return softfloat_mulAddF32( uiA, uiB, uiC, 0 );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_mulAdd.c ****/\n/**** start inlining ../../source/f32_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_div( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint_fast32_t sigB;\r\n    bool signZ;\r\n    struct exp16_sig32 normExpSig;\r\n    int_fast16_t expZ;\r\n#ifdef SOFTFLOAT_FAST_DIV64TO32\r\n    uint_fast64_t sig64A;\r\n    uint_fast32_t sigZ;\r\n#else\r\n    uint_fast32_t sigZ;\r\n    uint_fast64_t rem;\r\n#endif\r\n    uint_fast32_t uiZ;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF32UI( uiA );\r\n    expA  = expF32UI( uiA );\r\n    sigA  = fracF32UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF32UI( uiB );\r\n    expB  = expF32UI( uiB );\r\n    sigB  = fracF32UI( uiB );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0xFF ) {\r\n        if ( sigA ) goto propagateNaN;\r\n        if ( expB == 0xFF ) {\r\n            if ( sigB ) goto propagateNaN;\r\n            goto invalid;\r\n        }\r\n        goto infinity;\r\n    }\r\n    if ( expB == 0xFF ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) {\r\n            if ( ! (expA | sigA) ) goto invalid;\r\n            softfloat_raiseFlags( softfloat_flag_infinite );\r\n            goto infinity;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0x7E;\r\n    sigA |= 0x00800000;\r\n    sigB |= 0x00800000;\r\n#ifdef SOFTFLOAT_FAST_DIV64TO32\r\n    if ( sigA < sigB ) {\r\n        --expZ;\r\n        sig64A = (uint_fast64_t) sigA<<31;\r\n    } else {\r\n        sig64A = (uint_fast64_t) sigA<<30;\r\n    }\r\n    sigZ = sig64A / sigB;\r\n    if ( ! (sigZ & 0x3F) ) sigZ |= ((uint_fast64_t) sigB * sigZ != sig64A);\r\n#else\r\n    if ( sigA < sigB ) {\r\n        --expZ;\r\n        sigA <<= 8;\r\n    } else {\r\n        sigA <<= 7;\r\n    }\r\n    sigB <<= 8;\r\n    sigZ = ((uint_fast64_t) sigA * softfloat_approxRecip32_1( sigB ))>>32;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigZ += 2;\r\n    if ( (sigZ & 0x3F) < 2 ) {\r\n        sigZ &= ~3;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n        rem = ((uint_fast64_t) sigA<<31) - (uint_fast64_t) sigZ * sigB;\r\n#else\r\n        rem = ((uint_fast64_t) sigA<<32) - (uint_fast64_t) (sigZ<<1) * sigB;\r\n#endif\r\n        if ( rem & UINT64_C( 0x8000000000000000 ) ) {\r\n            sigZ -= 4;\r\n        } else {\r\n            if ( rem ) sigZ |= 1;\r\n        }\r\n    }\r\n#endif\r\n    return softfloat_roundPackToF32( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF32UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF32UI;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ = packToF32UI( signZ, 0xFF, 0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ = packToF32UI( signZ, 0, 0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_div.c ****/\n/**** start inlining ../../source/f32_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_rem( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    int_fast16_t expB;\r\n    uint_fast32_t sigB;\r\n    struct exp16_sig32 normExpSig;\r\n    uint32_t rem;\r\n    int_fast16_t expDiff;\r\n    uint32_t q, recip32, altRem, meanRem;\r\n    bool signRem;\r\n    uint_fast32_t uiZ;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF32UI( uiA );\r\n    expA  = expF32UI( uiA );\r\n    sigA  = fracF32UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    expB = expF32UI( uiB );\r\n    sigB = fracF32UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0xFF ) {\r\n        if ( sigA || ((expB == 0xFF) && sigB) ) goto propagateNaN;\r\n        goto invalid;\r\n    }\r\n    if ( expB == 0xFF ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto invalid;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) return a;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    rem = sigA | 0x00800000;\r\n    sigB |= 0x00800000;\r\n    expDiff = expA - expB;\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff < -1 ) return a;\r\n        sigB <<= 6;\r\n        if ( expDiff ) {\r\n            rem <<= 5;\r\n            q = 0;\r\n        } else {\r\n            rem <<= 6;\r\n            q = (sigB <= rem);\r\n            if ( q ) rem -= sigB;\r\n        }\r\n    } else {\r\n        recip32 = softfloat_approxRecip32_1( sigB<<8 );\r\n        /*--------------------------------------------------------------------\r\n        | Changing the shift of `rem' here requires also changing the initial\r\n        | subtraction from `expDiff'.\r\n        *--------------------------------------------------------------------*/\r\n        rem <<= 7;\r\n        expDiff -= 31;\r\n        /*--------------------------------------------------------------------\r\n        | The scale of `sigB' affects how many bits are obtained during each\r\n        | cycle of the loop.  Currently this is 29 bits per loop iteration,\r\n        | which is believed to be the maximum possible.\r\n        *--------------------------------------------------------------------*/\r\n        sigB <<= 6;\r\n        for (;;) {\r\n            q = (rem * (uint_fast64_t) recip32)>>32;\r\n            if ( expDiff < 0 ) break;\r\n            rem = -(q * (uint32_t) sigB);\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -30 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q >>= ~expDiff & 31;\r\n        rem = (rem<<(expDiff + 30)) - q * (uint32_t) sigB;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    do {\r\n        altRem = rem;\r\n        ++q;\r\n        rem -= sigB;\r\n    } while ( ! (rem & 0x80000000) );\r\n    meanRem = rem + altRem;\r\n    if ( (meanRem & 0x80000000) || (! meanRem && (q & 1)) ) rem = altRem;\r\n    signRem = signA;\r\n    if ( 0x80000000 <= rem ) {\r\n        signRem = ! signRem;\r\n        rem = -rem;\r\n    }\r\n    return softfloat_normRoundPackToF32( signRem, expB, rem );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF32UI( uiA, uiB );\r\n    goto uiZ;\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF32UI;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_rem.c ****/\n/**** start inlining ../../source/f32_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f32_sqrt( float32_t a )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast32_t sigA, uiZ;\r\n    struct exp16_sig32 normExpSig;\r\n    int_fast16_t expZ;\r\n    uint_fast32_t sigZ, shiftedSigZ;\r\n    uint32_t negRem;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF32UI( uiA );\r\n    expA  = expF32UI( uiA );\r\n    sigA  = fracF32UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0xFF ) {\r\n        if ( sigA ) {\r\n            uiZ = softfloat_propagateNaNF32UI( uiA, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( ! signA ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signA ) {\r\n        if ( ! (expA | sigA) ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) return a;\r\n        normExpSig = softfloat_normSubnormalF32Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0x7F)>>1) + 0x7E;\r\n    expA &= 1;\r\n    sigA = (sigA | 0x00800000)<<8;\r\n    sigZ =\r\n        ((uint_fast64_t) sigA * softfloat_approxRecipSqrt32_1( expA, sigA ))\r\n            >>32;\r\n    if ( expA ) sigZ >>= 1;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigZ += 2;\r\n    if ( (sigZ & 0x3F) < 2 ) {\r\n        shiftedSigZ = sigZ>>2;\r\n        negRem = shiftedSigZ * shiftedSigZ;\r\n        sigZ &= ~3;\r\n        if ( negRem & 0x80000000 ) {\r\n            sigZ |= 1;\r\n        } else {\r\n            if ( negRem ) --sigZ;\r\n        }\r\n    }\r\n    return softfloat_roundPackToF32( 0, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF32UI;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_sqrt.c ****/\n/**** start inlining ../../source/f32_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_eq( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF32UI( uiA ) || isNaNF32UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF32UI( uiA ) || softfloat_isSigNaNF32UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    return (uiA == uiB) || ! (uint32_t) ((uiA | uiB)<<1);\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_eq.c ****/\n/**** start inlining ../../source/f32_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_le( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF32UI( uiA ) || isNaNF32UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF32UI( uiA );\r\n    signB = signF32UI( uiB );\r\n    return\r\n        (signA != signB) ? signA || ! (uint32_t) ((uiA | uiB)<<1)\r\n            : (uiA == uiB) || (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_le.c ****/\n/**** start inlining ../../source/f32_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_lt( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF32UI( uiA ) || isNaNF32UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF32UI( uiA );\r\n    signB = signF32UI( uiB );\r\n    return\r\n        (signA != signB) ? signA && ((uint32_t) ((uiA | uiB)<<1) != 0)\r\n            : (uiA != uiB) && (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_lt.c ****/\n/**** start inlining ../../source/f32_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_eq_signaling( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF32UI( uiA ) || isNaNF32UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    return (uiA == uiB) || ! (uint32_t) ((uiA | uiB)<<1);\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_eq_signaling.c ****/\n/**** start inlining ../../source/f32_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_le_quiet( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF32UI( uiA ) || isNaNF32UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF32UI( uiA ) || softfloat_isSigNaNF32UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF32UI( uiA );\r\n    signB = signF32UI( uiB );\r\n    return\r\n        (signA != signB) ? signA || ! (uint32_t) ((uiA | uiB)<<1)\r\n            : (uiA == uiB) || (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_le_quiet.c ****/\n/**** start inlining ../../source/f32_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_lt_quiet( float32_t a, float32_t b )\r\n{\r\n    union ui32_f32 uA;\r\n    uint_fast32_t uiA;\r\n    union ui32_f32 uB;\r\n    uint_fast32_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF32UI( uiA ) || isNaNF32UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF32UI( uiA ) || softfloat_isSigNaNF32UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF32UI( uiA );\r\n    signB = signF32UI( uiB );\r\n    return\r\n        (signA != signB) ? signA && ((uint32_t) ((uiA | uiB)<<1) != 0)\r\n            : (uiA != uiB) && (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_lt_quiet.c ****/\n/**** start inlining ../../source/f32_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f32_isSignalingNaN( float32_t a )\r\n{\r\n    union ui32_f32 uA;\r\n\r\n    uA.f = a;\r\n    return softfloat_isSigNaNF32UI( uA.ui );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f32_isSignalingNaN.c ****/\n/**** start inlining ../../source/f64_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f64_to_ui32( float64_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    sig  = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (ui32_fromNaN != ui32_fromPosOverflow) || (ui32_fromNaN != ui32_fromNegOverflow)\r\n    if ( (exp == 0x7FF) && sig ) {\r\n#if (ui32_fromNaN == ui32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (ui32_fromNaN == ui32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return ui32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= UINT64_C( 0x0010000000000000 );\r\n    shiftDist = 0x427 - exp;\r\n    if ( 0 < shiftDist ) sig = softfloat_shiftRightJam64( sig, shiftDist );\r\n    return softfloat_roundToUI32( sign, sig, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_ui32.c ****/\n/**** start inlining ../../source/f64_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f64_to_ui64( float64_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    struct uint64_extra sigExtra;\r\n#else\r\n    uint32_t extSig[3];\r\n#endif\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    sig  = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= UINT64_C( 0x0010000000000000 );\r\n    shiftDist = 0x433 - exp;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    if ( shiftDist <= 0 ) {\r\n        if ( shiftDist < -11 ) goto invalid;\r\n        sigExtra.v = sig<<-shiftDist;\r\n        sigExtra.extra = 0;\r\n    } else {\r\n        sigExtra = softfloat_shiftRightJam64Extra( sig, 0, shiftDist );\r\n    }\r\n    return\r\n        softfloat_roundToUI64(\r\n            sign, sigExtra.v, sigExtra.extra, roundingMode, exact );\r\n#else\r\n    extSig[indexWord( 3, 0 )] = 0;\r\n    if ( shiftDist <= 0 ) {\r\n        if ( shiftDist < -11 ) goto invalid;\r\n        sig <<= -shiftDist;\r\n        extSig[indexWord( 3, 2 )] = sig>>32;\r\n        extSig[indexWord( 3, 1 )] = sig;\r\n    } else {\r\n        extSig[indexWord( 3, 2 )] = sig>>32;\r\n        extSig[indexWord( 3, 1 )] = sig;\r\n        softfloat_shiftRightJam96M( extSig, shiftDist, extSig );\r\n    }\r\n    return softfloat_roundMToUI64( sign, extSig, roundingMode, exact );\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FF) && fracF64UI( uiA ) ? ui64_fromNaN\r\n            : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_ui64.c ****/\n/**** start inlining ../../source/f64_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f64_to_i32( float64_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    sig  = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (i32_fromNaN != i32_fromPosOverflow) || (i32_fromNaN != i32_fromNegOverflow)\r\n    if ( (exp == 0x7FF) && sig ) {\r\n#if (i32_fromNaN == i32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (i32_fromNaN == i32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return i32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= UINT64_C( 0x0010000000000000 );\r\n    shiftDist = 0x427 - exp;\r\n    if ( 0 < shiftDist ) sig = softfloat_shiftRightJam64( sig, shiftDist );\r\n    return softfloat_roundToI32( sign, sig, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_i32.c ****/\n/**** start inlining ../../source/f64_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f64_to_i64( float64_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    struct uint64_extra sigExtra;\r\n#else\r\n    uint32_t extSig[3];\r\n#endif\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    sig  = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig |= UINT64_C( 0x0010000000000000 );\r\n    shiftDist = 0x433 - exp;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    if ( shiftDist <= 0 ) {\r\n        if ( shiftDist < -11 ) goto invalid;\r\n        sigExtra.v = sig<<-shiftDist;\r\n        sigExtra.extra = 0;\r\n    } else {\r\n        sigExtra = softfloat_shiftRightJam64Extra( sig, 0, shiftDist );\r\n    }\r\n    return\r\n        softfloat_roundToI64(\r\n            sign, sigExtra.v, sigExtra.extra, roundingMode, exact );\r\n#else\r\n    extSig[indexWord( 3, 0 )] = 0;\r\n    if ( shiftDist <= 0 ) {\r\n        if ( shiftDist < -11 ) goto invalid;\r\n        sig <<= -shiftDist;\r\n        extSig[indexWord( 3, 2 )] = sig>>32;\r\n        extSig[indexWord( 3, 1 )] = sig;\r\n    } else {\r\n        extSig[indexWord( 3, 2 )] = sig>>32;\r\n        extSig[indexWord( 3, 1 )] = sig;\r\n        softfloat_shiftRightJam96M( extSig, shiftDist, extSig );\r\n    }\r\n    return softfloat_roundMToI64( sign, extSig, roundingMode, exact );\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FF) && fracF64UI( uiA ) ? i64_fromNaN\r\n            : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_i64.c ****/\n/**** start inlining ../../source/f64_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f64_to_ui32_r_minMag( float64_t a, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    uint_fast32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF64UI( uiA );\r\n    sig = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x433 - exp;\r\n    if ( 53 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF64UI( uiA );\r\n    if ( sign || (shiftDist < 21) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FF) && sig ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig |= UINT64_C( 0x0010000000000000 );\r\n    z = sig>>shiftDist;\r\n    if ( exact && ((uint_fast64_t) z<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/f64_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f64_to_ui64_r_minMag( float64_t a, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    uint_fast64_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF64UI( uiA );\r\n    sig = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x433 - exp;\r\n    if ( 53 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF64UI( uiA );\r\n    if ( sign ) goto invalid;\r\n    if ( shiftDist <= 0 ) {\r\n        if ( shiftDist < -11 ) goto invalid;\r\n        z = (sig | UINT64_C( 0x0010000000000000 ))<<-shiftDist;\r\n    } else {\r\n        sig |= UINT64_C( 0x0010000000000000 );\r\n        z = sig>>shiftDist;\r\n        if ( exact && (uint64_t) (sig<<(-shiftDist & 63)) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FF) && sig ? ui64_fromNaN\r\n            : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/f64_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f64_to_i32_r_minMag( float64_t a, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n    bool sign;\r\n    int_fast32_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF64UI( uiA );\r\n    sig = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x433 - exp;\r\n    if ( 53 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF64UI( uiA );\r\n    if ( shiftDist < 22 ) {\r\n        if (\r\n            sign && (exp == 0x41E) && (sig < UINT64_C( 0x0000000000200000 ))\r\n        ) {\r\n            if ( exact && sig ) {\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            }\r\n            return -0x7FFFFFFF - 1;\r\n        }\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FF) && sig ? i32_fromNaN\r\n                : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig |= UINT64_C( 0x0010000000000000 );\r\n    absZ = sig>>shiftDist;\r\n    if ( exact && ((uint_fast64_t) (uint_fast32_t) absZ<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/f64_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f64_to_i64_r_minMag( float64_t a, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast16_t shiftDist;\r\n    int_fast64_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    sig  = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x433 - exp;\r\n    if ( shiftDist <= 0 ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( shiftDist < -10 ) {\r\n            if ( uiA == packToF64UI( 1, 0x43E, 0 ) ) {\r\n                return -INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1;\r\n            }\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            return\r\n                (exp == 0x7FF) && sig ? i64_fromNaN\r\n                    : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig |= UINT64_C( 0x0010000000000000 );\r\n        absZ = sig<<-shiftDist;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( 53 <= shiftDist ) {\r\n            if ( exact && (exp | sig) ) {\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            }\r\n            return 0;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig |= UINT64_C( 0x0010000000000000 );\r\n        absZ = sig>>shiftDist;\r\n        if ( exact && (absZ<<shiftDist != sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/f64_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f64_to_f16( float64_t a )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ, frac16;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    frac = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FF ) {\r\n        if ( frac ) {\r\n            softfloat_f64UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF16UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF16UI( sign, 0x1F, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac16 = softfloat_shortShiftRightJam64( frac, 38 );\r\n    if ( ! (exp | frac16) ) {\r\n        uiZ = packToF16UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    return softfloat_roundPackToF16( sign, exp - 0x3F1, frac16 | 0x4000 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_f16.c ****/\n/**** start inlining ../../source/f64_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f64_to_f32( float64_t a )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast32_t uiZ, frac32;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    frac = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FF ) {\r\n        if ( frac ) {\r\n            softfloat_f64UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF32UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF32UI( sign, 0xFF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac32 = softfloat_shortShiftRightJam64( frac, 22 );\r\n    if ( ! (exp | frac32) ) {\r\n        uiZ = packToF32UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    return softfloat_roundPackToF32( sign, exp - 0x381, frac32 | 0x40000000 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_f32.c ****/\n/**** start inlining ../../source/f64_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t f64_to_extF80( float64_t a )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    struct exp16_sig64 normExpSig;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    frac = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FF ) {\r\n        if ( frac ) {\r\n            softfloat_f64UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToExtF80UI( &commonNaN );\r\n            uiZ64 = uiZ.v64;\r\n            uiZ0  = uiZ.v0;\r\n        } else {\r\n            uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n            uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( frac );\r\n        exp = normExpSig.exp;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = packToExtF80UI64( sign, exp + 0x3C00 );\r\n    uiZ0  = (frac | UINT64_C( 0x0010000000000000 ))<<11;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_extF80.c ****/\n/**** start inlining ../../source/f64_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f64_to_extF80M( float64_t a, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = f64_to_extF80( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f64_to_extF80M( float64_t a, extFloat80_t *zPtr )\r\n{\r\n    struct extFloat80M *zSPtr;\r\n    union ui64_f64 uA;\r\n    uint64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint64_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ64;\r\n    uint64_t uiZ0;\r\n    struct exp16_sig64 normExpSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    frac = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FF ) {\r\n        if ( frac ) {\r\n            softfloat_f64UIToCommonNaN( uiA, &commonNaN );\r\n            softfloat_commonNaNToExtF80M( &commonNaN, zSPtr );\r\n            return;\r\n        }\r\n        uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n        uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( frac );\r\n        exp = normExpSig.exp;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = packToExtF80UI64( sign, exp + 0x3C00 );\r\n    uiZ0  = UINT64_C( 0x8000000000000000 ) | frac<<11;\r\n uiZ:\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif  = uiZ0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f64_to_extF80M.c ****/\n/**** start inlining ../../source/f64_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f64_to_f128( float64_t a )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint_fast64_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    struct exp16_sig64 normExpSig;\r\n    struct uint128 frac128;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    frac = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FF ) {\r\n        if ( frac ) {\r\n            softfloat_f64UIToCommonNaN( uiA, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF128UI( &commonNaN );\r\n        } else {\r\n            uiZ.v64 = packToF128UI64( sign, 0x7FFF, 0 );\r\n            uiZ.v0  = 0;\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ.v64 = packToF128UI64( sign, 0, 0 );\r\n            uiZ.v0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac128 = softfloat_shortShiftLeft128( 0, frac, 60 );\r\n    uiZ.v64 = packToF128UI64( sign, exp + 0x3C00, frac128.v64 );\r\n    uiZ.v0  = frac128.v0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_to_f128.c ****/\n/**** start inlining ../../source/f64_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f64_to_f128M( float64_t a, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f64_to_f128( a );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f64_to_f128M( float64_t a, float128_t *zPtr )\r\n{\r\n    uint32_t *zWPtr;\r\n    union ui64_f64 uA;\r\n    uint64_t uiA;\r\n    bool sign;\r\n    int_fast16_t exp;\r\n    uint64_t frac;\r\n    struct commonNaN commonNaN;\r\n    uint32_t uiZ96;\r\n    struct exp16_sig64 normExpSig;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    sign = signF64UI( uiA );\r\n    exp  = expF64UI( uiA );\r\n    frac = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n    if ( exp == 0x7FF ) {\r\n        if ( frac ) {\r\n            softfloat_f64UIToCommonNaN( uiA, &commonNaN );\r\n            softfloat_commonNaNToF128M( &commonNaN, zWPtr );\r\n            return;\r\n        }\r\n        uiZ96 = packToF128UI96( sign, 0x7FFF, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! frac ) {\r\n            uiZ96 = packToF128UI96( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( frac );\r\n        exp = normExpSig.exp - 1;\r\n        frac = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr[indexWord( 4, 1 )] = (uint32_t) frac<<28;\r\n    frac >>= 4;\r\n    zWPtr[indexWordHi( 4 )] = packToF128UI96( sign, exp + 0x3C00, frac>>32 );\r\n    zWPtr[indexWord( 4, 2 )] = frac;\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f64_to_f128M.c ****/\n/**** start inlining ../../source/f64_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_roundToInt( float64_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    int_fast16_t exp;\r\n    uint_fast64_t uiZ, lastBitMask, roundBitsMask;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    exp = expF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp <= 0x3FE ) {\r\n        if ( !(uiA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) return a;\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        uiZ = uiA & packToF64UI( 1, 0, 0 );\r\n        switch ( roundingMode ) {\r\n         case softfloat_round_near_even:\r\n            if ( !fracF64UI( uiA ) ) break;\r\n         case softfloat_round_near_maxMag:\r\n            if ( exp == 0x3FE ) uiZ |= packToF64UI( 0, 0x3FF, 0 );\r\n            break;\r\n         case softfloat_round_min:\r\n            if ( uiZ ) uiZ = packToF64UI( 1, 0x3FF, 0 );\r\n            break;\r\n         case softfloat_round_max:\r\n            if ( !uiZ ) uiZ = packToF64UI( 0, 0x3FF, 0 );\r\n            break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n         case softfloat_round_odd:\r\n            uiZ |= packToF64UI( 0, 0x3FF, 0 );\r\n            break;\r\n#endif\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x433 <= exp ) {\r\n        if ( (exp == 0x7FF) && fracF64UI( uiA ) ) {\r\n            uiZ = softfloat_propagateNaNF64UI( uiA, 0 );\r\n            goto uiZ;\r\n        }\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ = uiA;\r\n    lastBitMask = (uint_fast64_t) 1<<(0x433 - exp);\r\n    roundBitsMask = lastBitMask - 1;\r\n    if ( roundingMode == softfloat_round_near_maxMag ) {\r\n        uiZ += lastBitMask>>1;\r\n    } else if ( roundingMode == softfloat_round_near_even ) {\r\n        uiZ += lastBitMask>>1;\r\n        if ( !(uiZ & roundBitsMask) ) uiZ &= ~lastBitMask;\r\n    } else if (\r\n        roundingMode\r\n            == (signF64UI( uiZ ) ? softfloat_round_min : softfloat_round_max)\r\n    ) {\r\n        uiZ += roundBitsMask;\r\n    }\r\n    uiZ &= ~roundBitsMask;\r\n    if ( uiZ != uiA ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) uiZ |= lastBitMask;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_roundToInt.c ****/\n/**** start inlining ../../source/f64_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_add( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool signA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    float64_t (*magsFuncPtr)( uint_fast64_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF64UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF64UI( uiB );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        return softfloat_addMagsF64( uiA, uiB, signA );\r\n    } else {\r\n        return softfloat_subMagsF64( uiA, uiB, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_addMagsF64 : softfloat_subMagsF64;\r\n    return (*magsFuncPtr)( uiA, uiB, signA );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_add.c ****/\n/**** start inlining ../../source/f64_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_sub( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool signA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    float64_t (*magsFuncPtr)( uint_fast64_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF64UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF64UI( uiB );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        return softfloat_subMagsF64( uiA, uiB, signA );\r\n    } else {\r\n        return softfloat_addMagsF64( uiA, uiB, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_subMagsF64 : softfloat_addMagsF64;\r\n    return (*magsFuncPtr)( uiA, uiB, signA );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_sub.c ****/\n/**** start inlining ../../source/f64_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_mul( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint_fast64_t sigB;\r\n    bool signZ;\r\n    uint_fast64_t magBits;\r\n    struct exp16_sig64 normExpSig;\r\n    int_fast16_t expZ;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    struct uint128 sig128Z;\r\n#else\r\n    uint32_t sig128Z[4];\r\n#endif\r\n    uint_fast64_t sigZ, uiZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF64UI( uiA );\r\n    expA  = expF64UI( uiA );\r\n    sigA  = fracF64UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF64UI( uiB );\r\n    expB  = expF64UI( uiB );\r\n    sigB  = fracF64UI( uiB );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FF ) {\r\n        if ( sigA || ((expB == 0x7FF) && sigB) ) goto propagateNaN;\r\n        magBits = expB | sigB;\r\n        goto infArg;\r\n    }\r\n    if ( expB == 0x7FF ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        magBits = expA | sigA;\r\n        goto infArg;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x3FF;\r\n    sigA = (sigA | UINT64_C( 0x0010000000000000 ))<<10;\r\n    sigB = (sigB | UINT64_C( 0x0010000000000000 ))<<11;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n    sig128Z = softfloat_mul64To128( sigA, sigB );\r\n    sigZ = sig128Z.v64 | (sig128Z.v0 != 0);\r\n#else\r\n    softfloat_mul64To128M( sigA, sigB, sig128Z );\r\n    sigZ =\r\n        (uint64_t) sig128Z[indexWord( 4, 3 )]<<32 | sig128Z[indexWord( 4, 2 )];\r\n    if ( sig128Z[indexWord( 4, 1 )] || sig128Z[indexWord( 4, 0 )] ) sigZ |= 1;\r\n#endif\r\n    if ( sigZ < UINT64_C( 0x4000000000000000 ) ) {\r\n        --expZ;\r\n        sigZ <<= 1;\r\n    }\r\n    return softfloat_roundPackToF64( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infArg:\r\n    if ( ! magBits ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ = defaultNaNF64UI;\r\n    } else {\r\n        uiZ = packToF64UI( signZ, 0x7FF, 0 );\r\n    }\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ = packToF64UI( signZ, 0, 0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_mul.c ****/\n/**** start inlining ../../source/f64_mulAdd.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_mulAdd( float64_t a, float64_t b, float64_t c )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    union ui64_f64 uC;\r\n    uint_fast64_t uiC;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    uC.f = c;\r\n    uiC = uC.ui;\r\n    return softfloat_mulAddF64( uiA, uiB, uiC, 0 );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_mulAdd.c ****/\n/**** start inlining ../../source/f64_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_div( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signB;\r\n    int_fast16_t expB;\r\n    uint_fast64_t sigB;\r\n    bool signZ;\r\n    struct exp16_sig64 normExpSig;\r\n    int_fast16_t expZ;\r\n    uint32_t recip32, sig32Z, doubleTerm;\r\n    uint_fast64_t rem;\r\n    uint32_t q;\r\n    uint_fast64_t sigZ;\r\n    uint_fast64_t uiZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF64UI( uiA );\r\n    expA  = expF64UI( uiA );\r\n    sigA  = fracF64UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    signB = signF64UI( uiB );\r\n    expB  = expF64UI( uiB );\r\n    sigB  = fracF64UI( uiB );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FF ) {\r\n        if ( sigA ) goto propagateNaN;\r\n        if ( expB == 0x7FF ) {\r\n            if ( sigB ) goto propagateNaN;\r\n            goto invalid;\r\n        }\r\n        goto infinity;\r\n    }\r\n    if ( expB == 0x7FF ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) {\r\n            if ( ! (expA | sigA) ) goto invalid;\r\n            softfloat_raiseFlags( softfloat_flag_infinite );\r\n            goto infinity;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0x3FE;\r\n    sigA |= UINT64_C( 0x0010000000000000 );\r\n    sigB |= UINT64_C( 0x0010000000000000 );\r\n    if ( sigA < sigB ) {\r\n        --expZ;\r\n        sigA <<= 11;\r\n    } else {\r\n        sigA <<= 10;\r\n    }\r\n    sigB <<= 11;\r\n    recip32 = softfloat_approxRecip32_1( sigB>>32 ) - 2;\r\n    sig32Z = ((uint32_t) (sigA>>32) * (uint_fast64_t) recip32)>>32;\r\n    doubleTerm = sig32Z<<1;\r\n    rem =\r\n        ((sigA - (uint_fast64_t) doubleTerm * (uint32_t) (sigB>>32))<<28)\r\n            - (uint_fast64_t) doubleTerm * ((uint32_t) sigB>>4);\r\n    q = (((uint32_t) (rem>>32) * (uint_fast64_t) recip32)>>32) + 4;\r\n    sigZ = ((uint_fast64_t) sig32Z<<32) + ((uint_fast64_t) q<<4);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (sigZ & 0x1FF) < 4<<4 ) {\r\n        q &= ~7;\r\n        sigZ &= ~(uint_fast64_t) 0x7F;\r\n        doubleTerm = q<<1;\r\n        rem =\r\n            ((rem - (uint_fast64_t) doubleTerm * (uint32_t) (sigB>>32))<<28)\r\n                - (uint_fast64_t) doubleTerm * ((uint32_t) sigB>>4);\r\n        if ( rem & UINT64_C( 0x8000000000000000 ) ) {\r\n            sigZ -= 1<<7;\r\n        } else {\r\n            if ( rem ) sigZ |= 1;\r\n        }\r\n    }\r\n    return softfloat_roundPackToF64( signZ, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF64UI;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ = packToF64UI( signZ, 0x7FF, 0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ = packToF64UI( signZ, 0, 0 );\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_div.c ****/\n/**** start inlining ../../source/f64_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_rem( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    int_fast16_t expB;\r\n    uint_fast64_t sigB;\r\n    struct exp16_sig64 normExpSig;\r\n    uint64_t rem;\r\n    int_fast16_t expDiff;\r\n    uint32_t q, recip32;\r\n    uint_fast64_t q64;\r\n    uint64_t altRem, meanRem;\r\n    bool signRem;\r\n    uint_fast64_t uiZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF64UI( uiA );\r\n    expA  = expF64UI( uiA );\r\n    sigA  = fracF64UI( uiA );\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    expB = expF64UI( uiB );\r\n    sigB = fracF64UI( uiB );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FF ) {\r\n        if ( sigA || ((expB == 0x7FF) && sigB) ) goto propagateNaN;\r\n        goto invalid;\r\n    }\r\n    if ( expB == 0x7FF ) {\r\n        if ( sigB ) goto propagateNaN;\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA < expB - 1 ) return a;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! sigB ) goto invalid;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigB );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) return a;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    rem = sigA | UINT64_C( 0x0010000000000000 );\r\n    sigB |= UINT64_C( 0x0010000000000000 );\r\n    expDiff = expA - expB;\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff < -1 ) return a;\r\n        sigB <<= 9;\r\n        if ( expDiff ) {\r\n            rem <<= 8;\r\n            q = 0;\r\n        } else {\r\n            rem <<= 9;\r\n            q = (sigB <= rem);\r\n            if ( q ) rem -= sigB;\r\n        }\r\n    } else {\r\n        recip32 = softfloat_approxRecip32_1( sigB>>21 );\r\n        /*--------------------------------------------------------------------\r\n        | Changing the shift of `rem' here requires also changing the initial\r\n        | subtraction from `expDiff'.\r\n        *--------------------------------------------------------------------*/\r\n        rem <<= 9;\r\n        expDiff -= 30;\r\n        /*--------------------------------------------------------------------\r\n        | The scale of `sigB' affects how many bits are obtained during each\r\n        | cycle of the loop.  Currently this is 29 bits per loop iteration,\r\n        | the maximum possible.\r\n        *--------------------------------------------------------------------*/\r\n        sigB <<= 9;\r\n        for (;;) {\r\n            q64 = (uint32_t) (rem>>32) * (uint_fast64_t) recip32;\r\n            if ( expDiff < 0 ) break;\r\n            q = (q64 + 0x80000000)>>32;\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n            rem <<= 29;\r\n#else\r\n            rem = (uint_fast64_t) (uint32_t) (rem>>3)<<32;\r\n#endif\r\n            rem -= q * (uint64_t) sigB;\r\n            if ( rem & UINT64_C( 0x8000000000000000 ) ) rem += sigB;\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -29 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q = (uint32_t) (q64>>32)>>(~expDiff & 31);\r\n        rem = (rem<<(expDiff + 30)) - q * (uint64_t) sigB;\r\n        if ( rem & UINT64_C( 0x8000000000000000 ) ) {\r\n            altRem = rem + sigB;\r\n            goto selectRem;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    do {\r\n        altRem = rem;\r\n        ++q;\r\n        rem -= sigB;\r\n    } while ( ! (rem & UINT64_C( 0x8000000000000000 )) );\r\n selectRem:\r\n    meanRem = rem + altRem;\r\n    if (\r\n        (meanRem & UINT64_C( 0x8000000000000000 )) || (! meanRem && (q & 1))\r\n    ) {\r\n        rem = altRem;\r\n    }\r\n    signRem = signA;\r\n    if ( rem & UINT64_C( 0x8000000000000000 ) ) {\r\n        signRem = ! signRem;\r\n        rem = -rem;\r\n    }\r\n    return softfloat_normRoundPackToF64( signRem, expB, rem );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF64UI( uiA, uiB );\r\n    goto uiZ;\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF64UI;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_rem.c ****/\n/**** start inlining ../../source/f64_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f64_sqrt( float64_t a )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    bool signA;\r\n    int_fast16_t expA;\r\n    uint_fast64_t sigA, uiZ;\r\n    struct exp16_sig64 normExpSig;\r\n    int_fast16_t expZ;\r\n    uint32_t sig32A, recipSqrt32, sig32Z;\r\n    uint_fast64_t rem;\r\n    uint32_t q;\r\n    uint_fast64_t sigZ, shiftedSigZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    signA = signF64UI( uiA );\r\n    expA  = expF64UI( uiA );\r\n    sigA  = fracF64UI( uiA );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FF ) {\r\n        if ( sigA ) {\r\n            uiZ = softfloat_propagateNaNF64UI( uiA, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( ! signA ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signA ) {\r\n        if ( ! (expA | sigA) ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! sigA ) return a;\r\n        normExpSig = softfloat_normSubnormalF64Sig( sigA );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    | (`sig32Z' is guaranteed to be a lower bound on the square root of\r\n    | `sig32A', which makes `sig32Z' also a lower bound on the square root of\r\n    | `sigA'.)\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0x3FF)>>1) + 0x3FE;\r\n    expA &= 1;\r\n    sigA |= UINT64_C( 0x0010000000000000 );\r\n    sig32A = sigA>>21;\r\n    recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );\r\n    sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32;\r\n    if ( expA ) {\r\n        sigA <<= 8;\r\n        sig32Z >>= 1;\r\n    } else {\r\n        sigA <<= 9;\r\n    }\r\n    rem = sigA - (uint_fast64_t) sig32Z * sig32Z;\r\n    q = ((uint32_t) (rem>>2) * (uint_fast64_t) recipSqrt32)>>32;\r\n    sigZ = ((uint_fast64_t) sig32Z<<32 | 1<<5) + ((uint_fast64_t) q<<3);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (sigZ & 0x1FF) < 0x22 ) {\r\n        sigZ &= ~(uint_fast64_t) 0x3F;\r\n        shiftedSigZ = sigZ>>6;\r\n        rem = (sigA<<52) - shiftedSigZ * shiftedSigZ;\r\n        if ( rem & UINT64_C( 0x8000000000000000 ) ) {\r\n            --sigZ;\r\n        } else {\r\n            if ( rem ) sigZ |= 1;\r\n        }\r\n    }\r\n    return softfloat_roundPackToF64( 0, expZ, sigZ );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ = defaultNaNF64UI;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_sqrt.c ****/\n/**** start inlining ../../source/f64_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_eq( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF64UI( uiA ) || isNaNF64UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF64UI( uiA ) || softfloat_isSigNaNF64UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    return (uiA == uiB) || ! ((uiA | uiB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_eq.c ****/\n/**** start inlining ../../source/f64_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_le( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF64UI( uiA ) || isNaNF64UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF64UI( uiA );\r\n    signB = signF64UI( uiB );\r\n    return\r\n        (signA != signB)\r\n            ? signA || ! ((uiA | uiB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            : (uiA == uiB) || (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_le.c ****/\n/**** start inlining ../../source/f64_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_lt( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF64UI( uiA ) || isNaNF64UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF64UI( uiA );\r\n    signB = signF64UI( uiB );\r\n    return\r\n        (signA != signB)\r\n            ? signA && ((uiA | uiB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            : (uiA != uiB) && (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_lt.c ****/\n/**** start inlining ../../source/f64_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_eq_signaling( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF64UI( uiA ) || isNaNF64UI( uiB ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    return (uiA == uiB) || ! ((uiA | uiB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_eq_signaling.c ****/\n/**** start inlining ../../source/f64_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_le_quiet( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF64UI( uiA ) || isNaNF64UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF64UI( uiA ) || softfloat_isSigNaNF64UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF64UI( uiA );\r\n    signB = signF64UI( uiB );\r\n    return\r\n        (signA != signB)\r\n            ? signA || ! ((uiA | uiB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            : (uiA == uiB) || (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_le_quiet.c ****/\n/**** start inlining ../../source/f64_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_lt_quiet( float64_t a, float64_t b )\r\n{\r\n    union ui64_f64 uA;\r\n    uint_fast64_t uiA;\r\n    union ui64_f64 uB;\r\n    uint_fast64_t uiB;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA = uA.ui;\r\n    uB.f = b;\r\n    uiB = uB.ui;\r\n    if ( isNaNF64UI( uiA ) || isNaNF64UI( uiB ) ) {\r\n        if (\r\n            softfloat_isSigNaNF64UI( uiA ) || softfloat_isSigNaNF64UI( uiB )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF64UI( uiA );\r\n    signB = signF64UI( uiB );\r\n    return\r\n        (signA != signB)\r\n            ? signA && ((uiA | uiB) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            : (uiA != uiB) && (signA ^ (uiA < uiB));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_lt_quiet.c ****/\n/**** start inlining ../../source/f64_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f64_isSignalingNaN( float64_t a )\r\n{\r\n    union ui64_f64 uA;\r\n\r\n    uA.f = a;\r\n    return softfloat_isSigNaNF64UI( uA.ui );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f64_isSignalingNaN.c ****/\n/**** start inlining ../../source/extF80_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t\r\n extF80_to_ui32( extFloat80_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (ui32_fromNaN != ui32_fromPosOverflow) || (ui32_fromNaN != ui32_fromNegOverflow)\r\n    if ( (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) {\r\n#if (ui32_fromNaN == ui32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (ui32_fromNaN == ui32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return ui32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x4032 - exp;\r\n    if ( shiftDist <= 0 ) shiftDist = 1;\r\n    sig = softfloat_shiftRightJam64( sig, shiftDist );\r\n    return softfloat_roundToUI32( sign, sig, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_ui32.c ****/\n/**** start inlining ../../source/extF80_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t\r\n extF80_to_ui64( extFloat80_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n    uint_fast64_t sigExtra;\r\n    struct uint64_extra sig64Extra;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( shiftDist < 0 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigExtra = 0;\r\n    if ( shiftDist ) {\r\n        sig64Extra = softfloat_shiftRightJam64Extra( sig, 0, shiftDist );\r\n        sig = sig64Extra.v;\r\n        sigExtra = sig64Extra.extra;\r\n    }\r\n    return softfloat_roundToUI64( sign, sig, sigExtra, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_ui64.c ****/\n/**** start inlining ../../source/extF80_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t\r\n extF80_to_i32( extFloat80_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (i32_fromNaN != i32_fromPosOverflow) || (i32_fromNaN != i32_fromNegOverflow)\r\n    if ( (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) {\r\n#if (i32_fromNaN == i32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (i32_fromNaN == i32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return i32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x4032 - exp;\r\n    if ( shiftDist <= 0 ) shiftDist = 1;\r\n    sig = softfloat_shiftRightJam64( sig, shiftDist );\r\n    return softfloat_roundToI32( sign, sig, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_i32.c ****/\n/**** start inlining ../../source/extF80_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t\r\n extF80_to_i64( extFloat80_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n    uint_fast64_t sigExtra;\r\n    struct uint64_extra sig64Extra;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( shiftDist <= 0 ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( shiftDist ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            return\r\n                (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                    ? i64_fromNaN\r\n                    : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sigExtra = 0;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig64Extra = softfloat_shiftRightJam64Extra( sig, 0, shiftDist );\r\n        sig = sig64Extra.v;\r\n        sigExtra = sig64Extra.extra;\r\n    }\r\n    return softfloat_roundToI64( sign, sig, sigExtra, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_i64.c ****/\n/**** start inlining ../../source/extF80_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t extF80_to_ui32_r_minMag( extFloat80_t a, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n    bool sign;\r\n    uint_fast32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signExtF80UI64( uiA64 );\r\n    if ( sign || (shiftDist < 32) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    z = sig>>shiftDist;\r\n    if ( exact && ((uint_fast64_t) z<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/extF80_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t extF80_to_ui64_r_minMag( extFloat80_t a, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n    bool sign;\r\n    uint_fast64_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signExtF80UI64( uiA64 );\r\n    if ( sign || (shiftDist < 0) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    z = sig>>shiftDist;\r\n    if ( exact && (z<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/extF80_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t extF80_to_i32_r_minMag( extFloat80_t a, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n    bool sign;\r\n    int_fast32_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signExtF80UI64( uiA64 );\r\n    if ( shiftDist < 33 ) {\r\n        if (\r\n            (uiA64 == packToExtF80UI64( 1, 0x401E ))\r\n                && (sig < UINT64_C( 0x8000000100000000 ))\r\n        ) {\r\n            if ( exact && (sig & UINT64_C( 0x00000000FFFFFFFF )) ) {\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            }\r\n            return -0x7FFFFFFF - 1;\r\n        }\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? i32_fromNaN\r\n                : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    absZ = sig>>shiftDist;\r\n    if ( exact && ((uint_fast64_t) (uint_fast32_t) absZ<<shiftDist != sig) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/extF80_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t extF80_to_i64_r_minMag( extFloat80_t a, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    int_fast32_t shiftDist;\r\n    bool sign;\r\n    int_fast64_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact && (exp | sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signExtF80UI64( uiA64 );\r\n    if ( shiftDist <= 0 ) {\r\n        if (\r\n            (uiA64 == packToExtF80UI64( 1, 0x403E ))\r\n                && (sig == UINT64_C( 0x8000000000000000 ))\r\n        ) {\r\n            return -INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1;\r\n        }\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    absZ = sig>>shiftDist;\r\n    if ( exact && (uint64_t) (sig<<(-shiftDist & 63)) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/extF80_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t extF80_to_f16( extFloat80_t a )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ, sig16;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF16UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF16UI( sign, 0x1F, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig16 = softfloat_shortShiftRightJam64( sig, 49 );\r\n    if ( ! (exp | sig16) ) {\r\n        uiZ = packToF16UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3FF1;\r\n    if ( sizeof (int_fast16_t) < sizeof (int_fast32_t) ) {\r\n        if ( exp < -0x40 ) exp = -0x40;\r\n    }\r\n    return softfloat_roundPackToF16( sign, exp, sig16 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_f16.c ****/\n/**** start inlining ../../source/extF80_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t extF80_to_f32( extFloat80_t a )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint_fast32_t uiZ, sig32;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF32UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF32UI( sign, 0xFF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig32 = softfloat_shortShiftRightJam64( sig, 33 );\r\n    if ( ! (exp | sig32) ) {\r\n        uiZ = packToF32UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3F81;\r\n    if ( sizeof (int_fast16_t) < sizeof (int_fast32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return softfloat_roundPackToF32( sign, exp, sig32 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_f32.c ****/\n/**** start inlining ../../source/extF80_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t extF80_to_f64( extFloat80_t a )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint_fast64_t uiZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! (exp | sig) ) {\r\n        uiZ = packToF64UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF64UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = softfloat_shortShiftRightJam64( sig, 1 );\r\n    exp -= 0x3C01;\r\n    if ( sizeof (int_fast16_t) < sizeof (int_fast32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return softfloat_roundPackToF64( sign, exp, sig );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_f64.c ****/\n/**** start inlining ../../source/extF80_to_f128.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t extF80_to_f128( extFloat80_t a )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    uint_fast16_t exp;\r\n    uint_fast64_t frac;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    bool sign;\r\n    struct uint128 frac128;\r\n    union ui128_f128 uZ;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    exp = expExtF80UI64( uiA64 );\r\n    frac = uiA0 & UINT64_C( 0x7FFFFFFFFFFFFFFF );\r\n    if ( (exp == 0x7FFF) && frac ) {\r\n        softfloat_extF80UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n        uiZ = softfloat_commonNaNToF128UI( &commonNaN );\r\n    } else {\r\n        sign = signExtF80UI64( uiA64 );\r\n        frac128 = softfloat_shortShiftLeft128( 0, frac, 49 );\r\n        uiZ.v64 = packToF128UI64( sign, exp, frac128.v64 );\r\n        uiZ.v0  = frac128.v0;\r\n    }\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_to_f128.c ****/\n/**** start inlining ../../source/extF80_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t\r\n extF80_roundToInt( extFloat80_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64, signUI64;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sigA;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t sigZ;\r\n    struct exp32_sig64 normExpSig;\r\n    struct uint128 uiZ;\r\n    uint_fast64_t lastBitMask, roundBitsMask;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    signUI64 = uiA64 & packToExtF80UI64( 1, 0 );\r\n    exp = expExtF80UI64( uiA64 );\r\n    sigA = uA.s.signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( !(sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) {\r\n        if ( !sigA ) {\r\n            uiZ64 = signUI64;\r\n            sigZ = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigA );\r\n        exp += normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x403E <= exp ) {\r\n        if ( exp == 0x7FFF ) {\r\n            if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n                uiZ = softfloat_propagateNaNExtF80UI( uiA64, sigA, 0, 0 );\r\n                uiZ64 = uiZ.v64;\r\n                sigZ  = uiZ.v0;\r\n                goto uiZ;\r\n            }\r\n            sigZ = UINT64_C( 0x8000000000000000 );\r\n        } else {\r\n            sigZ = sigA;\r\n        }\r\n        uiZ64 = signUI64 | exp;\r\n        goto uiZ;\r\n    }\r\n    if ( exp <= 0x3FFE ) {\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        switch ( roundingMode ) {\r\n         case softfloat_round_near_even:\r\n            if ( !(sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;\r\n         case softfloat_round_near_maxMag:\r\n            if ( exp == 0x3FFE ) goto mag1;\r\n            break;\r\n         case softfloat_round_min:\r\n            if ( signUI64 ) goto mag1;\r\n            break;\r\n         case softfloat_round_max:\r\n            if ( !signUI64 ) goto mag1;\r\n            break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n         case softfloat_round_odd:\r\n            goto mag1;\r\n#endif\r\n        }\r\n        uiZ64 = signUI64;\r\n        sigZ  = 0;\r\n        goto uiZ;\r\n     mag1:\r\n        uiZ64 = signUI64 | 0x3FFF;\r\n        sigZ  = UINT64_C( 0x8000000000000000 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = signUI64 | exp;\r\n    lastBitMask = (uint_fast64_t) 1<<(0x403E - exp);\r\n    roundBitsMask = lastBitMask - 1;\r\n    sigZ = sigA;\r\n    if ( roundingMode == softfloat_round_near_maxMag ) {\r\n        sigZ += lastBitMask>>1;\r\n    } else if ( roundingMode == softfloat_round_near_even ) {\r\n        sigZ += lastBitMask>>1;\r\n        if ( !(sigZ & roundBitsMask) ) sigZ &= ~lastBitMask;\r\n    } else if (\r\n        roundingMode == (signUI64 ? softfloat_round_min : softfloat_round_max)\r\n    ) {\r\n        sigZ += roundBitsMask;\r\n    }\r\n    sigZ &= ~roundBitsMask;\r\n    if ( !sigZ ) {\r\n        ++uiZ64;\r\n        sigZ = UINT64_C( 0x8000000000000000 );\r\n    }\r\n    if ( sigZ != sigA ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) sigZ |= lastBitMask;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif = sigZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_roundToInt.c ****/\n/**** start inlining ../../source/extF80_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t extF80_add( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    extFloat80_t\r\n        (*magsFuncPtr)(\r\n            uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    signB = signExtF80UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        return softfloat_addMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        return softfloat_subMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_addMagsExtF80 : softfloat_subMagsExtF80;\r\n    return (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_add.c ****/\n/**** start inlining ../../source/extF80_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t extF80_sub( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    extFloat80_t\r\n        (*magsFuncPtr)(\r\n            uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    signB = signExtF80UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        return softfloat_subMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        return softfloat_addMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_subMagsExtF80 : softfloat_addMagsExtF80;\r\n    return (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_sub.c ****/\n/**** start inlining ../../source/extF80_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t extF80_mul( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    uint_fast64_t sigA;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signB;\r\n    int_fast32_t expB;\r\n    uint_fast64_t sigB;\r\n    bool signZ;\r\n    uint_fast64_t magBits;\r\n    struct exp32_sig64 normExpSig;\r\n    int_fast32_t expZ;\r\n    struct uint128 sig128Z, uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    expA  = expExtF80UI64( uiA64 );\r\n    sigA  = uiA0;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    signB = signExtF80UI64( uiB64 );\r\n    expB  = expExtF80UI64( uiB64 );\r\n    sigB  = uiB0;\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if (\r\n               (sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            || ((expB == 0x7FFF) && (sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF )))\r\n        ) {\r\n            goto propagateNaN;\r\n        }\r\n        magBits = expB | sigB;\r\n        goto infArg;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n        magBits = expA | sigA;\r\n        goto infArg;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) expA = 1;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigA );\r\n        expA += normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) expB = 1;\r\n    if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigB ) goto zero;\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigB );\r\n        expB += normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x3FFE;\r\n    sig128Z = softfloat_mul64To128( sigA, sigB );\r\n    if ( sig128Z.v64 < UINT64_C( 0x8000000000000000 ) ) {\r\n        --expZ;\r\n        sig128Z =\r\n            softfloat_add128(\r\n                sig128Z.v64, sig128Z.v0, sig128Z.v64, sig128Z.v0 );\r\n    }\r\n    return\r\n        softfloat_roundPackToExtF80(\r\n            signZ, expZ, sig128Z.v64, sig128Z.v0, extF80_roundingPrecision );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );\r\n    uiZ64 = uiZ.v64;\r\n    uiZ0  = uiZ.v0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infArg:\r\n    if ( ! magBits ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ64 = defaultNaNExtF80UI64;\r\n        uiZ0  = defaultNaNExtF80UI0;\r\n    } else {\r\n        uiZ64 = packToExtF80UI64( signZ, 0x7FFF );\r\n        uiZ0  = UINT64_C( 0x8000000000000000 );\r\n    }\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ64 = packToExtF80UI64( signZ, 0 );\r\n    uiZ0  = 0;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_mul.c ****/\n/**** start inlining ../../source/extF80_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t extF80_div( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    uint_fast64_t sigA;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signB;\r\n    int_fast32_t expB;\r\n    uint_fast64_t sigB;\r\n    bool signZ;\r\n    struct exp32_sig64 normExpSig;\r\n    int_fast32_t expZ;\r\n    struct uint128 rem;\r\n    uint_fast32_t recip32;\r\n    uint_fast64_t sigZ;\r\n    int ix;\r\n    uint_fast64_t q64;\r\n    uint_fast32_t q;\r\n    struct uint128 term;\r\n    uint_fast64_t sigZExtra;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    expA  = expExtF80UI64( uiA64 );\r\n    sigA  = uiA0;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    signB = signExtF80UI64( uiB64 );\r\n    expB  = expExtF80UI64( uiB64 );\r\n    sigB  = uiB0;\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n        if ( expB == 0x7FFF ) {\r\n            if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n            goto invalid;\r\n        }\r\n        goto infinity;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) expB = 1;\r\n    if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigB ) {\r\n            if ( ! sigA ) goto invalid;\r\n            softfloat_raiseFlags( softfloat_flag_infinite );\r\n            goto infinity;\r\n        }\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigB );\r\n        expB += normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) expA = 1;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigA );\r\n        expA += normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0x3FFF;\r\n    if ( sigA < sigB ) {\r\n        --expZ;\r\n        rem = softfloat_shortShiftLeft128( 0, sigA, 32 );\r\n    } else {\r\n        rem = softfloat_shortShiftLeft128( 0, sigA, 31 );\r\n    }\r\n    recip32 = softfloat_approxRecip32_1( sigB>>32 );\r\n    sigZ = 0;\r\n    ix = 2;\r\n    for (;;) {\r\n        q64 = (uint_fast64_t) (uint32_t) (rem.v64>>2) * recip32;\r\n        q = (q64 + 0x80000000)>>32;\r\n        --ix;\r\n        if ( ix < 0 ) break;\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n        term = softfloat_mul64ByShifted32To128( sigB, q );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            --q;\r\n            rem = softfloat_add128( rem.v64, rem.v0, sigB>>32, sigB<<32 );\r\n        }\r\n        sigZ = (sigZ<<29) + q;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ((q + 1) & 0x3FFFFF) < 2 ) {\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n        term = softfloat_mul64ByShifted32To128( sigB, q );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        term = softfloat_shortShiftLeft128( 0, sigB, 32 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            --q;\r\n            rem = softfloat_add128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        } else if ( softfloat_le128( term.v64, term.v0, rem.v64, rem.v0 ) ) {\r\n            ++q;\r\n            rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        }\r\n        if ( rem.v64 | rem.v0 ) q |= 1;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigZ = (sigZ<<6) + (q>>23);\r\n    sigZExtra = (uint64_t) ((uint_fast64_t) q<<41);\r\n    return\r\n        softfloat_roundPackToExtF80(\r\n            signZ, expZ, sigZ, sigZExtra, extF80_roundingPrecision );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );\r\n    uiZ64 = uiZ.v64;\r\n    uiZ0  = uiZ.v0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ64 = defaultNaNExtF80UI64;\r\n    uiZ0  = defaultNaNExtF80UI0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ64 = packToExtF80UI64( signZ, 0x7FFF );\r\n    uiZ0  = UINT64_C( 0x8000000000000000 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ64 = packToExtF80UI64( signZ, 0 );\r\n    uiZ0  = 0;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_div.c ****/\n/**** start inlining ../../source/extF80_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t extF80_rem( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    uint_fast64_t sigA;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    int_fast32_t expB;\r\n    uint_fast64_t sigB;\r\n    struct exp32_sig64 normExpSig;\r\n    int_fast32_t expDiff;\r\n    struct uint128 rem, shiftedSigB;\r\n    uint_fast32_t q, recip32;\r\n    uint_fast64_t q64;\r\n    struct uint128 term, altRem, meanRem;\r\n    bool signRem;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    expA  = expExtF80UI64( uiA64 );\r\n    sigA  = uiA0;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    expB  = expExtF80UI64( uiB64 );\r\n    sigB  = uiB0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if (\r\n               (sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            || ((expB == 0x7FFF) && (sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF )))\r\n        ) {\r\n            goto propagateNaN;\r\n        }\r\n        goto invalid;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;\r\n        /*--------------------------------------------------------------------\r\n        | Argument b is an infinity.  Doubling `expB' is an easy way to ensure\r\n        | that `expDiff' later is less than -1, which will result in returning\r\n        | a canonicalized version of argument a.\r\n        *--------------------------------------------------------------------*/\r\n        expB += expB;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) expB = 1;\r\n    if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigB ) goto invalid;\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigB );\r\n        expB += normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) expA = 1;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) {\r\n            expA = 0;\r\n            goto copyA;\r\n        }\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigA );\r\n        expA += normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( expDiff < -1 ) goto copyA;\r\n    rem = softfloat_shortShiftLeft128( 0, sigA, 32 );\r\n    shiftedSigB = softfloat_shortShiftLeft128( 0, sigB, 32 );\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff ) {\r\n            --expB;\r\n            shiftedSigB = softfloat_shortShiftLeft128( 0, sigB, 33 );\r\n            q = 0;\r\n        } else {\r\n            q = (sigB <= sigA);\r\n            if ( q ) {\r\n                rem =\r\n                    softfloat_sub128(\r\n                        rem.v64, rem.v0, shiftedSigB.v64, shiftedSigB.v0 );\r\n            }\r\n        }\r\n    } else {\r\n        recip32 = softfloat_approxRecip32_1( sigB>>32 );\r\n        expDiff -= 30;\r\n        for (;;) {\r\n            q64 = (uint_fast64_t) (uint32_t) (rem.v64>>2) * recip32;\r\n            if ( expDiff < 0 ) break;\r\n            q = (q64 + 0x80000000)>>32;\r\n            rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n            term = softfloat_mul64ByShifted32To128( sigB, q );\r\n            rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n            if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n                rem =\r\n                    softfloat_add128(\r\n                        rem.v64, rem.v0, shiftedSigB.v64, shiftedSigB.v0 );\r\n            }\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -29 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q = (uint32_t) (q64>>32)>>(~expDiff & 31);\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, expDiff + 30 );\r\n        term = softfloat_mul64ByShifted32To128( sigB, q );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            altRem =\r\n                softfloat_add128(\r\n                    rem.v64, rem.v0, shiftedSigB.v64, shiftedSigB.v0 );\r\n            goto selectRem;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    do {\r\n        altRem = rem;\r\n        ++q;\r\n        rem =\r\n            softfloat_sub128(\r\n                rem.v64, rem.v0, shiftedSigB.v64, shiftedSigB.v0 );\r\n    } while ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) );\r\n selectRem:\r\n    meanRem = softfloat_add128( rem.v64, rem.v0, altRem.v64, altRem.v0 );\r\n    if (\r\n        (meanRem.v64 & UINT64_C( 0x8000000000000000 ))\r\n            || (! (meanRem.v64 | meanRem.v0) && (q & 1))\r\n    ) {\r\n        rem = altRem;\r\n    }\r\n    signRem = signA;\r\n    if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n        signRem = ! signRem;\r\n        rem = softfloat_sub128( 0, 0, rem.v64, rem.v0 );\r\n    }\r\n    return\r\n        softfloat_normRoundPackToExtF80(\r\n            signRem, rem.v64 | rem.v0 ? expB + 32 : 0, rem.v64, rem.v0, 80 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );\r\n    uiZ64 = uiZ.v64;\r\n    uiZ0  = uiZ.v0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ64 = defaultNaNExtF80UI64;\r\n    uiZ0  = defaultNaNExtF80UI0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n copyA:\r\n    if ( expA < 1 ) {\r\n        sigA >>= 1 - expA;\r\n        expA = 0;\r\n    }\r\n    uiZ64 = packToExtF80UI64( signA, expA );\r\n    uiZ0  = sigA;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_rem.c ****/\n/**** start inlining ../../source/extF80_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t extF80_sqrt( extFloat80_t a )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    uint_fast64_t sigA;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    struct exp32_sig64 normExpSig;\r\n    int_fast32_t expZ;\r\n    uint_fast32_t sig32A, recipSqrt32, sig32Z;\r\n    struct uint128 rem;\r\n    uint_fast64_t q, x64, sigZ;\r\n    struct uint128 y, term;\r\n    uint_fast64_t sigZExtra;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    expA  = expExtF80UI64( uiA64 );\r\n    sigA  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, 0, 0 );\r\n            uiZ64 = uiZ.v64;\r\n            uiZ0  = uiZ.v0;\r\n            goto uiZ;\r\n        }\r\n        if ( ! signA ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signA ) {\r\n        if ( ! sigA ) goto zero;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) expA = 1;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) goto zero;\r\n        normExpSig = softfloat_normSubnormalExtF80Sig( sigA );\r\n        expA += normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    | (`sig32Z' is guaranteed to be a lower bound on the square root of\r\n    | `sig32A', which makes `sig32Z' also a lower bound on the square root of\r\n    | `sigA'.)\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0x3FFF)>>1) + 0x3FFF;\r\n    expA &= 1;\r\n    sig32A = sigA>>32;\r\n    recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );\r\n    sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32;\r\n    if ( expA ) {\r\n        sig32Z >>= 1;\r\n        rem = softfloat_shortShiftLeft128( 0, sigA, 61 );\r\n    } else {\r\n        rem = softfloat_shortShiftLeft128( 0, sigA, 62 );\r\n    }\r\n    rem.v64 -= (uint_fast64_t) sig32Z * sig32Z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = ((uint32_t) (rem.v64>>2) * (uint_fast64_t) recipSqrt32)>>32;\r\n    x64 = (uint_fast64_t) sig32Z<<32;\r\n    sigZ = x64 + (q<<3);\r\n    y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n    /*------------------------------------------------------------------------\r\n    | (Repeating this loop is a rare occurrence.)\r\n    *------------------------------------------------------------------------*/\r\n    for (;;) {\r\n        term = softfloat_mul64ByShifted32To128( x64 + sigZ, q );\r\n        rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 );\r\n        if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break;\r\n        --q;\r\n        sigZ -= 1<<3;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = (((rem.v64>>2) * recipSqrt32)>>32) + 2;\r\n    x64 = sigZ;\r\n    sigZ = (sigZ<<1) + (q>>25);\r\n    sigZExtra = (uint64_t) (q<<39);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (q & 0xFFFFFF) <= 2 ) {\r\n        q &= ~(uint_fast64_t) 0xFFFF;\r\n        sigZExtra = (uint64_t) (q<<39);\r\n        term = softfloat_mul64ByShifted32To128( x64 + (q>>27), q );\r\n        x64 = (uint32_t) (q<<5) * (uint_fast64_t) (uint32_t) q;\r\n        term = softfloat_add128( term.v64, term.v0, 0, x64 );\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 28 );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            if ( ! sigZExtra ) --sigZ;\r\n            --sigZExtra;\r\n        } else {\r\n            if ( rem.v64 | rem.v0 ) sigZExtra |= 1;\r\n        }\r\n    }\r\n    return\r\n        softfloat_roundPackToExtF80(\r\n            0, expZ, sigZ, sigZExtra, extF80_roundingPrecision );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ64 = defaultNaNExtF80UI64;\r\n    uiZ0  = defaultNaNExtF80UI0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ64 = packToExtF80UI64( signA, 0 );\r\n    uiZ0  = 0;\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_sqrt.c ****/\n/**** start inlining ../../source/extF80_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_eq( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNExtF80UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNExtF80UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    return\r\n           (uiA0 == uiB0)\r\n        && ((uiA64 == uiB64) || (! uiA0 && ! ((uiA64 | uiB64) & 0x7FFF)));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_eq.c ****/\n/**** start inlining ../../source/extF80_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_le( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signExtF80UI64( uiA64 );\r\n    signB = signExtF80UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA || ! (((uiA64 | uiB64) & 0x7FFF) | uiA0 | uiB0)\r\n            : ((uiA64 == uiB64) && (uiA0 == uiB0))\r\n                  || (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_le.c ****/\n/**** start inlining ../../source/extF80_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_lt( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signExtF80UI64( uiA64 );\r\n    signB = signExtF80UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA && (((uiA64 | uiB64) & 0x7FFF) | uiA0 | uiB0)\r\n            : ((uiA64 != uiB64) || (uiA0 != uiB0))\r\n                  && (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_lt.c ****/\n/**** start inlining ../../source/extF80_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_eq_signaling( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    return\r\n           (uiA0 == uiB0)\r\n        && ((uiA64 == uiB64) || (! uiA0 && ! ((uiA64 | uiB64) & 0x7FFF)));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_eq_signaling.c ****/\n/**** start inlining ../../source/extF80_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_le_quiet( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNExtF80UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNExtF80UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signExtF80UI64( uiA64 );\r\n    signB = signExtF80UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA || ! (((uiA64 | uiB64) & 0x7FFF) | uiA0 | uiB0)\r\n            : ((uiA64 == uiB64) && (uiA0 == uiB0))\r\n                  || (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_le_quiet.c ****/\n/**** start inlining ../../source/extF80_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_lt_quiet( extFloat80_t a, extFloat80_t b )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    union { struct extFloat80M s; extFloat80_t f; } uB;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.s.signExp;\r\n    uiA0  = uA.s.signif;\r\n    uB.f = b;\r\n    uiB64 = uB.s.signExp;\r\n    uiB0  = uB.s.signif;\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNExtF80UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNExtF80UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signExtF80UI64( uiA64 );\r\n    signB = signExtF80UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA && (((uiA64 | uiB64) & 0x7FFF) | uiA0 | uiB0)\r\n            : ((uiA64 != uiB64) || (uiA0 != uiB0))\r\n                  && (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_lt_quiet.c ****/\n/**** start inlining ../../source/extF80_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool extF80_isSignalingNaN( extFloat80_t a )\r\n{\r\n    union { struct extFloat80M s; extFloat80_t f; } uA;\r\n\r\n    uA.f = a;\r\n    return softfloat_isSigNaNExtF80UI( uA.s.signExp, uA.s.signif );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/extF80_isSignalingNaN.c ****/\n/**** start inlining ../../source/extF80M_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast32_t\r\n extF80M_to_ui32(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return extF80_to_ui32( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast32_t\r\n extF80M_to_ui32(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x4032 - exp;\r\n    if ( shiftDist <= 0 ) {\r\n        if ( sig>>32 ) goto invalid;\r\n        if ( -32 < shiftDist ) {\r\n            sig <<= -shiftDist;\r\n        } else {\r\n            if ( (uint32_t) sig ) goto invalid;\r\n        }\r\n    } else {\r\n        sig = softfloat_shiftRightJam64( sig, shiftDist );\r\n    }\r\n    return softfloat_roundToUI32( sign, sig, roundingMode, exact );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            ? ui32_fromNaN\r\n            : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_ui32.c ****/\n/**** start inlining ../../source/extF80M_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast64_t\r\n extF80M_to_ui64(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return extF80_to_ui64( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast64_t\r\n extF80M_to_ui64(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n    uint32_t extSig[3];\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( shiftDist < 0 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    extSig[indexWord( 3, 2 )] = sig>>32;\r\n    extSig[indexWord( 3, 1 )] = sig;\r\n    extSig[indexWord( 3, 0 )] = 0;\r\n    if ( shiftDist ) softfloat_shiftRightJam96M( extSig, shiftDist, extSig );\r\n    return softfloat_roundMToUI64( sign, extSig, roundingMode, exact );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_ui64.c ****/\n/**** start inlining ../../source/extF80M_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast32_t\r\n extF80M_to_i32(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return extF80_to_i32( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast32_t\r\n extF80M_to_i32(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x4032 - exp;\r\n    if ( shiftDist <= 0 ) {\r\n        if ( sig>>32 ) goto invalid;\r\n        if ( -32 < shiftDist ) {\r\n            sig <<= -shiftDist;\r\n        } else {\r\n            if ( (uint32_t) sig ) goto invalid;\r\n        }\r\n    } else {\r\n        sig = softfloat_shiftRightJam64( sig, shiftDist );\r\n    }\r\n    return softfloat_roundToI32( sign, sig, roundingMode, exact );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ? i32_fromNaN\r\n            : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_i32.c ****/\n/**** start inlining ../../source/extF80M_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast64_t\r\n extF80M_to_i64(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return extF80_to_i64( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast64_t\r\n extF80M_to_i64(\r\n     const extFloat80_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n    uint32_t extSig[3];\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( shiftDist < 0 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    extSig[indexWord( 3, 2 )] = sig>>32;\r\n    extSig[indexWord( 3, 1 )] = sig;\r\n    extSig[indexWord( 3, 0 )] = 0;\r\n    if ( shiftDist ) softfloat_shiftRightJam96M( extSig, shiftDist, extSig );\r\n    return softfloat_roundMToI64( sign, extSig, roundingMode, exact );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_i64.c ****/\n/**** start inlining ../../source/extF80M_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast32_t extF80M_to_ui32_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n\r\n    return extF80_to_ui32_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast32_t extF80M_to_ui32_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n    bool sign;\r\n    uint64_t shiftedSig;\r\n    uint32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! sig && (exp != 0x7FFF) ) return 0;\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signExtF80UI64( uiA64 );\r\n    if ( shiftDist < 0 ) {\r\n        if ( sign || sig>>32 || (shiftDist <= -31) ) goto invalid;\r\n        shiftedSig = (uint64_t) (uint32_t) sig<<-shiftDist;\r\n        if ( shiftedSig>>32 ) goto invalid;\r\n        z = shiftedSig;\r\n    } else {\r\n        shiftedSig = sig;\r\n        if ( shiftDist ) shiftedSig >>= shiftDist;\r\n        if ( shiftedSig>>32 ) goto invalid;\r\n        z = shiftedSig;\r\n        if ( sign && z ) goto invalid;\r\n        if ( exact && shiftDist && ((uint64_t) z<<shiftDist != sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            ? ui32_fromNaN\r\n            : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/extF80M_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast64_t extF80M_to_ui64_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n\r\n    return extF80_to_ui64_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast64_t extF80M_to_ui64_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n    bool sign;\r\n    uint64_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! sig && (exp != 0x7FFF) ) return 0;\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signExtF80UI64( uiA64 );\r\n    if ( shiftDist < 0 ) {\r\n        if ( sign || (shiftDist <= -63) ) goto invalid;\r\n        shiftDist = -shiftDist;\r\n        z = sig<<shiftDist;\r\n        if ( z>>shiftDist != sig ) goto invalid;\r\n    } else {\r\n        z = sig;\r\n        if ( shiftDist ) z >>= shiftDist;\r\n        if ( sign && z ) goto invalid;\r\n        if ( exact && shiftDist && (z<<shiftDist != sig) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n            ? ui64_fromNaN\r\n            : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/extF80M_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast32_t extF80M_to_i32_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n\r\n    return extF80_to_i32_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast32_t extF80M_to_i32_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n    bool sign, raiseInexact;\r\n    int32_t z;\r\n    uint64_t shiftedSig;\r\n    uint32_t absZ;\r\n    union { uint32_t ui; int32_t i; } u;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! sig && (exp != 0x7FFF) ) return 0;\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        raiseInexact = exact;\r\n        z = 0;\r\n    } else {\r\n        sign = signExtF80UI64( uiA64 );\r\n        raiseInexact = false;\r\n        if ( shiftDist < 0 ) {\r\n            if ( sig>>32 || (shiftDist <= -31) ) goto invalid;\r\n            shiftedSig = (uint64_t) (uint32_t) sig<<-shiftDist;\r\n            if ( shiftedSig>>32 ) goto invalid;\r\n            absZ = shiftedSig;\r\n        } else {\r\n            shiftedSig = sig;\r\n            if ( shiftDist ) shiftedSig >>= shiftDist;\r\n            if ( shiftedSig>>32 ) goto invalid;\r\n            absZ = shiftedSig;\r\n            if ( exact && shiftDist ) {\r\n                raiseInexact = ((uint64_t) absZ<<shiftDist != sig);\r\n            }\r\n        }\r\n        if ( sign ) {\r\n            if ( 0x80000000 < absZ ) goto invalid;\r\n            u.ui = -absZ;\r\n            z = u.i;\r\n        } else {\r\n            if ( 0x80000000 <= absZ ) goto invalid;\r\n            z = absZ;\r\n        }\r\n    }\r\n    if ( raiseInexact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ? i32_fromNaN\r\n            : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/extF80M_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast64_t extF80M_to_i64_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n\r\n    return extF80_to_i64_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast64_t extF80M_to_i64_r_minMag( const extFloat80_t *aPtr, bool exact )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    int32_t shiftDist;\r\n    bool sign, raiseInexact;\r\n    int64_t z;\r\n    uint64_t absZ;\r\n    union { uint64_t ui; int64_t i; } u;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    exp = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! sig && (exp != 0x7FFF) ) return 0;\r\n    shiftDist = 0x403E - exp;\r\n    if ( 64 <= shiftDist ) {\r\n        raiseInexact = exact;\r\n        z = 0;\r\n    } else {\r\n        sign = signExtF80UI64( uiA64 );\r\n        raiseInexact = false;\r\n        if ( shiftDist < 0 ) {\r\n            if ( shiftDist <= -63 ) goto invalid;\r\n            shiftDist = -shiftDist;\r\n            absZ = sig<<shiftDist;\r\n            if ( absZ>>shiftDist != sig ) goto invalid;\r\n        } else {\r\n            absZ = sig;\r\n            if ( shiftDist ) absZ >>= shiftDist;\r\n            if ( exact && shiftDist ) raiseInexact = (absZ<<shiftDist != sig);\r\n        }\r\n        if ( sign ) {\r\n            if ( UINT64_C( 0x8000000000000000 ) < absZ ) goto invalid;\r\n            u.ui = -absZ;\r\n            z = u.i;\r\n        } else {\r\n            if ( UINT64_C( 0x8000000000000000 ) <= absZ ) goto invalid;\r\n            z = absZ;\r\n        }\r\n    }\r\n    if ( raiseInexact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ? i64_fromNaN\r\n            : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/extF80M_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat16_t extF80M_to_f16( const extFloat80_t *aPtr )\r\n{\r\n\r\n    return extF80_to_f16( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat16_t extF80M_to_f16( const extFloat80_t *aPtr )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint16_t uiZ, sig16;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80MToCommonNaN( aSPtr, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF16UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF16UI( sign, 0x1F, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! (sig & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sig ) {\r\n            uiZ = packToF16UI( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        exp += softfloat_normExtF80SigM( &sig );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig16 = softfloat_shortShiftRightJam64( sig, 49 );\r\n    exp -= 0x3FF1;\r\n    if ( sizeof (int_fast16_t) < sizeof (int32_t) ) {\r\n        if ( exp < -0x40 ) exp = -0x40;\r\n    }\r\n    return softfloat_roundPackToF16( sign, exp, sig16 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_f16.c ****/\n/**** start inlining ../../source/extF80M_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat32_t extF80M_to_f32( const extFloat80_t *aPtr )\r\n{\r\n\r\n    return extF80_to_f32( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat32_t extF80M_to_f32( const extFloat80_t *aPtr )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint32_t uiZ, sig32;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80MToCommonNaN( aSPtr, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF32UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF32UI( sign, 0xFF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! (sig & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sig ) {\r\n            uiZ = packToF32UI( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        exp += softfloat_normExtF80SigM( &sig );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig32 = softfloat_shortShiftRightJam64( sig, 33 );\r\n    exp -= 0x3F81;\r\n    if ( sizeof (int_fast16_t) < sizeof (int32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return softfloat_roundPackToF32( sign, exp, sig32 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_f32.c ****/\n/**** start inlining ../../source/extF80M_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat64_t extF80M_to_f64( const extFloat80_t *aPtr )\r\n{\r\n\r\n    return extF80_to_f64( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat64_t extF80M_to_f64( const extFloat80_t *aPtr )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint64_t uiZ;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80MToCommonNaN( aSPtr, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF64UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! (sig & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sig ) {\r\n            uiZ = packToF64UI( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        exp += softfloat_normExtF80SigM( &sig );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig = softfloat_shortShiftRightJam64( sig, 1 );\r\n    exp -= 0x3C01;\r\n    if ( sizeof (int_fast16_t) < sizeof (int32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return softfloat_roundPackToF64( sign, exp, sig );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_f64.c ****/\n/**** start inlining ../../source/extF80M_to_f128M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid extF80M_to_f128M( const extFloat80_t *aPtr, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = extF80_to_f128( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid extF80M_to_f128M( const extFloat80_t *aPtr, float128_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    uint32_t *zWPtr;\r\n    uint_fast16_t uiA64;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig;\r\n    struct commonNaN commonNaN;\r\n    uint32_t uiZ96;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    sign = signExtF80UI64( uiA64 );\r\n    exp  = expExtF80UI64( uiA64 );\r\n    sig = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n    if ( exp == 0x7FFF ) {\r\n        if ( sig & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_extF80MToCommonNaN( aSPtr, &commonNaN );\r\n            softfloat_commonNaNToF128M( &commonNaN, zWPtr );\r\n            return;\r\n        }\r\n        uiZ96 = packToF128UI96( sign, 0x7FFF, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) --exp;\r\n    if ( ! (sig & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sig ) {\r\n            uiZ96 = packToF128UI96( sign, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        exp += softfloat_normExtF80SigM( &sig );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    zWPtr[indexWord( 4, 1 )] = (uint32_t) sig<<17;\r\n    sig >>= 15;\r\n    zWPtr[indexWord( 4, 2 )] = sig;\r\n    if ( exp < 0 ) {\r\n        zWPtr[indexWordHi( 4 )] = sig>>32;\r\n        softfloat_shiftRight96M(\r\n            &zWPtr[indexMultiwordHi( 4, 3 )],\r\n            -exp,\r\n            &zWPtr[indexMultiwordHi( 4, 3 )]\r\n        );\r\n        exp = 0;\r\n        sig = (uint64_t) zWPtr[indexWordHi( 4 )]<<32;\r\n    }\r\n    zWPtr[indexWordHi( 4 )] = packToF128UI96( sign, exp, sig>>32 );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    zWPtr[indexWord( 4, 3 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_to_f128M.c ****/\n/**** start inlining ../../source/extF80M_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n extF80M_roundToInt(\r\n     const extFloat80_t *aPtr,\r\n     uint_fast8_t roundingMode,\r\n     bool exact,\r\n     extFloat80_t *zPtr\r\n )\r\n{\r\n\r\n    *zPtr = extF80_roundToInt( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n extF80M_roundToInt(\r\n     const extFloat80_t *aPtr,\r\n     uint_fast8_t roundingMode,\r\n     bool exact,\r\n     extFloat80_t *zPtr\r\n )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiA64, signUI64;\r\n    int32_t exp;\r\n    uint64_t sigA;\r\n    uint_fast16_t uiZ64;\r\n    uint64_t sigZ, lastBitMask, roundBitsMask;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    signUI64 = uiA64 & packToExtF80UI64( 1, 0 );\r\n    exp = expExtF80UI64( uiA64 );\r\n    sigA = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( !(sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) {\r\n        if ( !sigA ) {\r\n            uiZ64 = signUI64;\r\n            sigZ = 0;\r\n            goto uiZ;\r\n        }\r\n        exp += softfloat_normExtF80SigM( &sigA );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp <= 0x3FFE ) {\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        switch ( roundingMode ) {\r\n         case softfloat_round_near_even:\r\n            if ( !(sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;\r\n         case softfloat_round_near_maxMag:\r\n            if ( exp == 0x3FFE ) goto mag1;\r\n            break;\r\n         case softfloat_round_min:\r\n            if ( signUI64 ) goto mag1;\r\n            break;\r\n         case softfloat_round_max:\r\n            if ( !signUI64 ) goto mag1;\r\n            break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n         case softfloat_round_odd:\r\n            goto mag1;\r\n#endif\r\n        }\r\n        uiZ64 = signUI64;\r\n        sigZ = 0;\r\n        goto uiZ;\r\n     mag1:\r\n        uiZ64 = signUI64 | 0x3FFF;\r\n        sigZ = UINT64_C( 0x8000000000000000 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x403E <= exp ) {\r\n        if ( exp == 0x7FFF ) {\r\n            if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n                softfloat_propagateNaNExtF80M( aSPtr, 0, zSPtr );\r\n                return;\r\n            }\r\n            sigZ = UINT64_C( 0x8000000000000000 );\r\n        } else {\r\n            sigZ = sigA;\r\n        }\r\n        uiZ64 = signUI64 | exp;\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiZ64 = signUI64 | exp;\r\n    lastBitMask = (uint64_t) 1<<(0x403E - exp);\r\n    roundBitsMask = lastBitMask - 1;\r\n    sigZ = sigA;\r\n    if ( roundingMode == softfloat_round_near_maxMag ) {\r\n        sigZ += lastBitMask>>1;\r\n    } else if ( roundingMode == softfloat_round_near_even ) {\r\n        sigZ += lastBitMask>>1;\r\n        if ( !(sigZ & roundBitsMask) ) sigZ &= ~lastBitMask;\r\n    } else if (\r\n        roundingMode == (signUI64 ? softfloat_round_min : softfloat_round_max)\r\n    ) {\r\n        sigZ += roundBitsMask;\r\n    }\r\n    sigZ &= ~roundBitsMask;\r\n    if ( !sigZ ) {\r\n        ++uiZ64;\r\n        sigZ = UINT64_C( 0x8000000000000000 );\r\n    }\r\n    if ( sigZ != sigA ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) sigZ |= lastBitMask;\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n uiZ:\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif = sigZ;\r\n    return;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_roundToInt.c ****/\n/**** start inlining ../../source/extF80M_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n extF80M_add(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    extFloat80_t\r\n        (*magsFuncPtr)(\r\n            uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    signB = signExtF80UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        *zPtr = softfloat_addMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        *zPtr = softfloat_subMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_addMagsExtF80 : softfloat_subMagsExtF80;\r\n    *zPtr = (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n extF80M_add(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    softfloat_addExtF80M(\r\n        (const struct extFloat80M *) aPtr,\r\n        (const struct extFloat80M *) bPtr,\r\n        (struct extFloat80M *) zPtr,\r\n        false\r\n    );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_add.c ****/\n/**** start inlining ../../source/extF80M_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n extF80M_sub(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint_fast64_t uiA0;\r\n    bool signA;\r\n    uint_fast16_t uiB64;\r\n    uint_fast64_t uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    extFloat80_t\r\n        (*magsFuncPtr)(\r\n            uint_fast16_t, uint_fast64_t, uint_fast16_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    signA = signExtF80UI64( uiA64 );\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    signB = signExtF80UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        *zPtr = softfloat_subMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        *zPtr = softfloat_addMagsExtF80( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_subMagsExtF80 : softfloat_addMagsExtF80;\r\n    *zPtr = (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n extF80M_sub(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    softfloat_addExtF80M(\r\n        (const struct extFloat80M *) aPtr,\r\n        (const struct extFloat80M *) bPtr,\r\n        (struct extFloat80M *) zPtr,\r\n        true\r\n    );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_sub.c ****/\n/**** start inlining ../../source/extF80M_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n extF80M_mul(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = extF80_mul( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n extF80M_mul(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t expA;\r\n    uint_fast16_t uiB64;\r\n    int32_t expB;\r\n    bool signZ;\r\n    uint_fast16_t exp, uiZ64;\r\n    uint64_t uiZ0, sigA, sigB;\r\n    int32_t expZ;\r\n    uint32_t sigProd[4], *extSigZPtr;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    expA = expExtF80UI64( uiA64 );\r\n    uiB64 = bSPtr->signExp;\r\n    expB = expExtF80UI64( uiB64 );\r\n    signZ = signExtF80UI64( uiA64 ) ^ signExtF80UI64( uiB64 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {\r\n        if ( softfloat_tryPropagateNaNExtF80M( aSPtr, bSPtr, zSPtr ) ) return;\r\n        if (\r\n               (! aSPtr->signif && (expA != 0x7FFF))\r\n            || (! bSPtr->signif && (expB != 0x7FFF))\r\n        ) {\r\n            softfloat_invalidExtF80M( zSPtr );\r\n            return;\r\n        }\r\n        uiZ64 = packToExtF80UI64( signZ, 0x7FFF );\r\n        uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) expA = 1;\r\n    sigA = aSPtr->signif;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) goto zero;\r\n        expA += softfloat_normExtF80SigM( &sigA );\r\n    }\r\n    if ( ! expB ) expB = 1;\r\n    sigB = bSPtr->signif;\r\n    if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigB ) goto zero;\r\n        expB += softfloat_normExtF80SigM( &sigB );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x3FFE;\r\n    softfloat_mul64To128M( sigA, sigB, sigProd );\r\n    if ( sigProd[indexWordLo( 4 )] ) sigProd[indexWord( 4, 1 )] |= 1;\r\n    extSigZPtr = &sigProd[indexMultiwordHi( 4, 3 )];\r\n    if ( sigProd[indexWordHi( 4 )] < 0x80000000 ) {\r\n        --expZ;\r\n        softfloat_add96M( extSigZPtr, extSigZPtr, extSigZPtr );\r\n    }\r\n    softfloat_roundPackMToExtF80M(\r\n        signZ, expZ, extSigZPtr, extF80_roundingPrecision, zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ64 = packToExtF80UI64( signZ, 0 );\r\n    uiZ0  = 0;\r\n uiZ:\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif  = uiZ0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_mul.c ****/\n/**** start inlining ../../source/extF80M_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n extF80M_div(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = extF80_div( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n extF80M_div(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t expA;\r\n    uint_fast16_t uiB64;\r\n    int32_t expB;\r\n    bool signZ;\r\n    uint64_t sigA, x64;\r\n    int32_t expZ;\r\n    int shiftDist;\r\n    uint32_t y[3], recip32, sigB[3];\r\n    int ix;\r\n    uint32_t q, qs[2];\r\n    uint_fast16_t uiZ64;\r\n    uint64_t uiZ0;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    expA = expExtF80UI64( uiA64 );\r\n    uiB64 = bSPtr->signExp;\r\n    expB = expExtF80UI64( uiB64 );\r\n    signZ = signExtF80UI64( uiA64 ) ^ signExtF80UI64( uiB64 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {\r\n        if ( softfloat_tryPropagateNaNExtF80M( aSPtr, bSPtr, zSPtr ) ) return;\r\n        if ( expA == 0x7FFF ) {\r\n            if ( expB == 0x7FFF ) goto invalid;\r\n            goto infinity;\r\n        }\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigA = aSPtr->signif;\r\n    x64 = bSPtr->signif;\r\n    if ( ! expB ) expB = 1;\r\n    if ( ! (x64 & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! x64 ) {\r\n            if ( ! sigA ) goto invalid;\r\n            softfloat_raiseFlags( softfloat_flag_infinite );\r\n            goto infinity;\r\n        }\r\n        expB += softfloat_normExtF80SigM( &x64 );\r\n    }\r\n    if ( ! expA ) expA = 1;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) goto zero;\r\n        expA += softfloat_normExtF80SigM( &sigA );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0x3FFF;\r\n    shiftDist = 29;\r\n    if ( sigA < x64 ) {\r\n        --expZ;\r\n        shiftDist = 30;\r\n    }\r\n    softfloat_shortShiftLeft64To96M( sigA, shiftDist, y );\r\n    recip32 = softfloat_approxRecip32_1( x64>>32 );\r\n    sigB[indexWord( 3, 0 )] = (uint32_t) x64<<30;\r\n    x64 >>= 2;\r\n    sigB[indexWord( 3, 2 )] = x64>>32;\r\n    sigB[indexWord( 3, 1 )] = x64;\r\n    ix = 2;\r\n    for (;;) {\r\n        x64 = (uint64_t) y[indexWordHi( 3 )] * recip32;\r\n        q = (x64 + 0x80000000)>>32;\r\n        --ix;\r\n        if ( ix < 0 ) break;\r\n        softfloat_remStep96MBy32( y, 29, sigB, q, y );\r\n        if ( y[indexWordHi( 3 )] & 0x80000000 ) {\r\n            --q;\r\n            softfloat_add96M( y, sigB, y );\r\n        }\r\n        qs[ix] = q;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ((q + 1) & 0x3FFFFF) < 2 ) {\r\n        softfloat_remStep96MBy32( y, 29, sigB, q, y );\r\n        if ( y[indexWordHi( 3 )] & 0x80000000 ) {\r\n            --q;\r\n            softfloat_add96M( y, sigB, y );\r\n        } else if ( softfloat_compare96M( sigB, y ) <= 0 ) {\r\n            ++q;\r\n            softfloat_sub96M( y, sigB, y );\r\n        }\r\n        if (\r\n            y[indexWordLo( 3 )] || y[indexWord( 3, 1 )] || y[indexWord( 3, 2 )]\r\n        ) {\r\n            q |= 1;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    x64 = (uint64_t) q<<9;\r\n    y[indexWord( 3, 0 )] = x64;\r\n    x64 = ((uint64_t) qs[0]<<6) + (x64>>32);\r\n    y[indexWord( 3, 1 )] = x64;\r\n    y[indexWord( 3, 2 )] = (qs[1]<<3) + (x64>>32);\r\n    softfloat_roundPackMToExtF80M(\r\n        signZ, expZ, y, extF80_roundingPrecision, zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_invalidExtF80M( zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ64 = packToExtF80UI64( signZ, 0x7FFF );\r\n    uiZ0  = UINT64_C( 0x8000000000000000 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ64 = packToExtF80UI64( signZ, 0 );\r\n    uiZ0  = 0;\r\n uiZ:\r\n    zSPtr->signExp = uiZ64;\r\n    zSPtr->signif  = uiZ0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_div.c ****/\n/**** start inlining ../../source/extF80M_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n extF80M_rem(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = extF80_rem( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n extF80M_rem(\r\n     const extFloat80_t *aPtr, const extFloat80_t *bPtr, extFloat80_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiA64;\r\n    int32_t expA, expB;\r\n    uint64_t x64;\r\n    bool signRem;\r\n    uint64_t sigA;\r\n    int32_t expDiff;\r\n    uint32_t rem[3], x[3], sig32B, q, recip32, rem2[3], *remPtr, *altRemPtr;\r\n    uint32_t *newRemPtr, wordMeanRem;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    expA = expExtF80UI64( uiA64 );\r\n    expB = expExtF80UI64( bSPtr->signExp );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {\r\n        if ( softfloat_tryPropagateNaNExtF80M( aSPtr, bSPtr, zSPtr ) ) return;\r\n        if ( expA == 0x7FFF ) goto invalid;\r\n        /*--------------------------------------------------------------------\r\n        | If we get here, then argument b is an infinity and `expB' is 0x7FFF;\r\n        | Doubling `expB' is an easy way to ensure that `expDiff' later is\r\n        | less than -1, which will result in returning a canonicalized version\r\n        | of argument a.\r\n        *--------------------------------------------------------------------*/\r\n        expB += expB;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) expB = 1;\r\n    x64 = bSPtr->signif;\r\n    if ( ! (x64 & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! x64 ) goto invalid;\r\n        expB += softfloat_normExtF80SigM( &x64 );\r\n    }\r\n    signRem = signExtF80UI64( uiA64 );\r\n    if ( ! expA ) expA = 1;\r\n    sigA = aSPtr->signif;\r\n    if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! sigA ) {\r\n            expA = 0;\r\n            goto copyA;\r\n        }\r\n        expA += softfloat_normExtF80SigM( &sigA );\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( expDiff < -1 ) goto copyA;\r\n    rem[indexWord( 3, 2 )] = sigA>>34;\r\n    rem[indexWord( 3, 1 )] = sigA>>2;\r\n    rem[indexWord( 3, 0 )] = (uint32_t) sigA<<30;\r\n    x[indexWord( 3, 0 )] = (uint32_t) x64<<30;\r\n    sig32B = x64>>32;\r\n    x64 >>= 2;\r\n    x[indexWord( 3, 2 )] = x64>>32;\r\n    x[indexWord( 3, 1 )] = x64;\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff ) {\r\n            --expB;\r\n            softfloat_add96M( x, x, x );\r\n            q = 0;\r\n        } else {\r\n            q = (softfloat_compare96M( x, rem ) <= 0);\r\n            if ( q ) softfloat_sub96M( rem, x, rem );\r\n        }\r\n    } else {\r\n        recip32 = softfloat_approxRecip32_1( sig32B );\r\n        expDiff -= 30;\r\n        for (;;) {\r\n            x64 = (uint64_t) rem[indexWordHi( 3 )] * recip32;\r\n            if ( expDiff < 0 ) break;\r\n            q = (x64 + 0x80000000)>>32;\r\n            softfloat_remStep96MBy32( rem, 29, x, q, rem );\r\n            if ( rem[indexWordHi( 3 )] & 0x80000000 ) {\r\n                softfloat_add96M( rem, x, rem );\r\n            }\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -29 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q = (uint32_t) (x64>>32)>>(~expDiff & 31);\r\n        softfloat_remStep96MBy32( rem, expDiff + 30, x, q, rem );\r\n        if ( rem[indexWordHi( 3 )] & 0x80000000 ) {\r\n            remPtr = rem;\r\n            altRemPtr = rem2;\r\n            softfloat_add96M( remPtr, x, altRemPtr );\r\n            goto selectRem;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    remPtr = rem;\r\n    altRemPtr = rem2;\r\n    do {\r\n        ++q;\r\n        newRemPtr = altRemPtr;\r\n        softfloat_sub96M( remPtr, x, newRemPtr );\r\n        altRemPtr = remPtr;\r\n        remPtr = newRemPtr;\r\n    } while ( ! (remPtr[indexWordHi( 3 )] & 0x80000000) );\r\n selectRem:\r\n    softfloat_add96M( remPtr, altRemPtr, x );\r\n    wordMeanRem = x[indexWordHi( 3 )];\r\n    if (\r\n        (wordMeanRem & 0x80000000)\r\n            || (! wordMeanRem && (q & 1) && ! x[indexWord( 3, 0 )]\r\n                    && ! x[indexWord( 3, 1 )])\r\n    ) {\r\n        remPtr = altRemPtr;\r\n    }\r\n    if ( remPtr[indexWordHi( 3 )] & 0x80000000 ) {\r\n        signRem = ! signRem;\r\n        softfloat_negX96M( remPtr );\r\n    }\r\n    softfloat_normRoundPackMToExtF80M( signRem, expB + 2, remPtr, 80, zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_invalidExtF80M( zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n copyA:\r\n    if ( expA < 1 ) {\r\n        sigA >>= 1 - expA;\r\n        expA = 0;\r\n    }\r\n    zSPtr->signExp = packToExtF80UI64( signRem, expA );\r\n    zSPtr->signif = sigA;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_rem.c ****/\n/**** start inlining ../../source/extF80M_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid extF80M_sqrt( const extFloat80_t *aPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = extF80_sqrt( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid extF80M_sqrt( const extFloat80_t *aPtr, extFloat80_t *zPtr )\r\n{\r\n    const struct extFloat80M *aSPtr;\r\n    struct extFloat80M *zSPtr;\r\n    uint_fast16_t uiA64, signUI64;\r\n    int32_t expA;\r\n    uint64_t rem64;\r\n    int32_t expZ;\r\n    uint32_t rem96[3], sig32A, recipSqrt32, sig32Z, q;\r\n    uint64_t sig64Z, x64;\r\n    uint32_t rem32, term[4], rem[4], extSigZ[3];\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    signUI64 = uiA64 & packToExtF80UI64( 1, 0 );\r\n    expA = expExtF80UI64( uiA64 );\r\n    rem64 = aSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if ( rem64 & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {\r\n            softfloat_propagateNaNExtF80M( aSPtr, 0, zSPtr );\r\n            return;\r\n        }\r\n        if ( signUI64 ) goto invalid;\r\n        rem64 = UINT64_C( 0x8000000000000000 );\r\n        goto copyA;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) expA = 1;\r\n    if ( ! (rem64 & UINT64_C( 0x8000000000000000 )) ) {\r\n        if ( ! rem64 ) {\r\n            uiA64 = signUI64;\r\n            goto copyA;\r\n        }\r\n        expA += softfloat_normExtF80SigM( &rem64 );\r\n    }\r\n    if ( signUI64 ) goto invalid;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0x3FFF)>>1) + 0x3FFF;\r\n    expA &= 1;\r\n    softfloat_shortShiftLeft64To96M( rem64, 30 - expA, rem96 );\r\n    sig32A = rem64>>32;\r\n    recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );\r\n    sig32Z = ((uint64_t) sig32A * recipSqrt32)>>32;\r\n    if ( expA ) sig32Z >>= 1;\r\n    rem64 =\r\n        ((uint64_t) rem96[indexWord( 3, 2 )]<<32 | rem96[indexWord( 3, 1 )])\r\n            - (uint64_t) sig32Z * sig32Z;\r\n    rem96[indexWord( 3, 2 )] = rem64>>32;\r\n    rem96[indexWord( 3, 1 )] = rem64;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = ((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32;\r\n    sig64Z = ((uint64_t) sig32Z<<32) + ((uint64_t) q<<3);\r\n    term[indexWord( 3, 2 )] = 0;\r\n    /*------------------------------------------------------------------------\r\n    | (Repeating this loop is a rare occurrence.)\r\n    *------------------------------------------------------------------------*/\r\n    for (;;) {\r\n        x64 = ((uint64_t) sig32Z<<32) + sig64Z;\r\n        term[indexWord( 3, 1 )] = x64>>32;\r\n        term[indexWord( 3, 0 )] = x64;\r\n        softfloat_remStep96MBy32(\r\n            rem96, 29, term, q, &rem[indexMultiwordHi( 4, 3 )] );\r\n        rem32 = rem[indexWord( 4, 3 )];\r\n        if ( ! (rem32 & 0x80000000) ) break;\r\n        --q;\r\n        sig64Z -= 1<<3;\r\n    }\r\n    rem64 = (uint64_t) rem32<<32 | rem[indexWord( 4, 2 )];\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = (((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32) + 2;\r\n    if ( rem64>>34 ) q += recipSqrt32;\r\n    x64 = (uint64_t) q<<7;\r\n    extSigZ[indexWord( 3, 0 )] = x64;\r\n    x64 = (sig64Z<<1) + (x64>>32);\r\n    extSigZ[indexWord( 3, 2 )] = x64>>32;\r\n    extSigZ[indexWord( 3, 1 )] = x64;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (q & 0xFFFFFF) <= 2 ) {\r\n        q &= ~(uint32_t) 0xFFFF;\r\n        extSigZ[indexWordLo( 3 )] = q<<7;\r\n        x64 = sig64Z + (q>>27);\r\n        term[indexWord( 4, 3 )] = 0;\r\n        term[indexWord( 4, 2 )] = x64>>32;\r\n        term[indexWord( 4, 1 )] = x64;\r\n        term[indexWord( 4, 0 )] = q<<5;\r\n        rem[indexWord( 4, 0 )] = 0;\r\n        softfloat_remStep128MBy32( rem, 28, term, q, rem );\r\n        q = rem[indexWordHi( 4 )];\r\n        if ( q & 0x80000000 ) {\r\n            softfloat_sub1X96M( extSigZ );\r\n        } else {\r\n            if ( q || rem[indexWord( 4, 1 )] || rem[indexWord( 4, 2 )] ) {\r\n                extSigZ[indexWordLo( 3 )] |= 1;\r\n            }\r\n        }\r\n    }\r\n    softfloat_roundPackMToExtF80M(\r\n        0, expZ, extSigZ, extF80_roundingPrecision, zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_invalidExtF80M( zSPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n copyA:\r\n    zSPtr->signExp = uiA64;\r\n    zSPtr->signif  = rem64;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_sqrt.c ****/\n/**** start inlining ../../source/extF80M_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool extF80M_eq( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n\r\n    return extF80_eq( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool extF80M_eq( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint64_t uiA0;\r\n    uint_fast16_t uiB64;\r\n    uint64_t uiB0;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNExtF80UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNExtF80UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( uiA0 == uiB0 ) {\r\n        return (uiA64 == uiB64) || ! uiA0;\r\n    } else {\r\n        if ( ! ((uiA0 & uiB0) & UINT64_C( 0x8000000000000000 )) ) {\r\n            return ! softfloat_compareNonnormExtF80M( aSPtr, bSPtr );\r\n        }\r\n        return false;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_eq.c ****/\n/**** start inlining ../../source/extF80M_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool extF80M_le( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n\r\n    return extF80_le( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool extF80M_le( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint64_t uiA0;\r\n    uint_fast16_t uiB64;\r\n    uint64_t uiB0;\r\n    bool signA, ltMags;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signExtF80UI64( uiA64 );\r\n    if ( (uiA64 ^ uiB64) & 0x8000 ) {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are different.\r\n        *--------------------------------------------------------------------*/\r\n        return signA || ! (uiA0 | uiB0);\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are the same.\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! ((uiA0 & uiB0) & UINT64_C( 0x8000000000000000 )) ) {\r\n            return (softfloat_compareNonnormExtF80M( aSPtr, bSPtr ) <= 0);\r\n        }\r\n        if ( uiA64 == uiB64 ) {\r\n            if ( uiA0 == uiB0 ) return true;\r\n            ltMags = (uiA0 < uiB0);\r\n        } else {\r\n            ltMags = (uiA64 < uiB64);\r\n        }\r\n        return signA ^ ltMags;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_le.c ****/\n/**** start inlining ../../source/extF80M_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool extF80M_lt( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n\r\n    return extF80_lt( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool extF80M_lt( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint64_t uiA0;\r\n    uint_fast16_t uiB64;\r\n    uint64_t uiB0;\r\n    bool signA, ltMags;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signExtF80UI64( uiA64 );\r\n    if ( (uiA64 ^ uiB64) & 0x8000 ) {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are different.\r\n        *--------------------------------------------------------------------*/\r\n        return signA && ((uiA0 | uiB0) != 0);\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are the same.\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! ((uiA0 & uiB0) & UINT64_C( 0x8000000000000000 )) ) {\r\n            return (softfloat_compareNonnormExtF80M( aSPtr, bSPtr ) < 0);\r\n        }\r\n        if ( uiA64 == uiB64 ) {\r\n            if ( uiA0 == uiB0 ) return false;\r\n            ltMags = (uiA0 < uiB0);\r\n        } else {\r\n            ltMags = (uiA64 < uiB64);\r\n        }\r\n        return signA ^ ltMags;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_lt.c ****/\n/**** start inlining ../../source/extF80M_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool extF80M_eq_signaling( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n\r\n    return extF80_eq_signaling( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool extF80M_eq_signaling( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint64_t uiA0;\r\n    uint_fast16_t uiB64;\r\n    uint64_t uiB0;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( uiA0 == uiB0 ) {\r\n        return (uiA64 == uiB64) || ! uiA0;\r\n    } else {\r\n        if ( ! ((uiA0 & uiB0) & UINT64_C( 0x8000000000000000 )) ) {\r\n            return ! softfloat_compareNonnormExtF80M( aSPtr, bSPtr );\r\n        }\r\n        return false;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_eq_signaling.c ****/\n/**** start inlining ../../source/extF80M_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool extF80M_le_quiet( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n\r\n    return extF80_le_quiet( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool extF80M_le_quiet( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint64_t uiA0;\r\n    uint_fast16_t uiB64;\r\n    uint64_t uiB0;\r\n    bool signA, ltMags;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNExtF80UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNExtF80UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signExtF80UI64( uiA64 );\r\n    if ( (uiA64 ^ uiB64) & 0x8000 ) {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are different.\r\n        *--------------------------------------------------------------------*/\r\n        return signA || ! (uiA0 | uiB0);\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are the same.\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! ((uiA0 & uiB0) & UINT64_C( 0x8000000000000000 )) ) {\r\n            return (softfloat_compareNonnormExtF80M( aSPtr, bSPtr ) <= 0);\r\n        }\r\n        if ( uiA64 == uiB64 ) {\r\n            if ( uiA0 == uiB0 ) return true;\r\n            ltMags = (uiA0 < uiB0);\r\n        } else {\r\n            ltMags = (uiA64 < uiB64);\r\n        }\r\n        return signA ^ ltMags;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_le_quiet.c ****/\n/**** start inlining ../../source/extF80M_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool extF80M_lt_quiet( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n\r\n    return extF80_lt_quiet( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool extF80M_lt_quiet( const extFloat80_t *aPtr, const extFloat80_t *bPtr )\r\n{\r\n    const struct extFloat80M *aSPtr, *bSPtr;\r\n    uint_fast16_t uiA64;\r\n    uint64_t uiA0;\r\n    uint_fast16_t uiB64;\r\n    uint64_t uiB0;\r\n    bool signA, ltMags;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aSPtr = (const struct extFloat80M *) aPtr;\r\n    bSPtr = (const struct extFloat80M *) bPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA64 = aSPtr->signExp;\r\n    uiA0  = aSPtr->signif;\r\n    uiB64 = bSPtr->signExp;\r\n    uiB0  = bSPtr->signif;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( isNaNExtF80UI( uiA64, uiA0 ) || isNaNExtF80UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNExtF80UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNExtF80UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    signA = signExtF80UI64( uiA64 );\r\n    if ( (uiA64 ^ uiB64) & 0x8000 ) {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are different.\r\n        *--------------------------------------------------------------------*/\r\n        return signA && ((uiA0 | uiB0) != 0);\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        | Signs are the same.\r\n        *--------------------------------------------------------------------*/\r\n        if ( ! ((uiA0 & uiB0) & UINT64_C( 0x8000000000000000 )) ) {\r\n            return (softfloat_compareNonnormExtF80M( aSPtr, bSPtr ) < 0);\r\n        }\r\n        if ( uiA64 == uiB64 ) {\r\n            if ( uiA0 == uiB0 ) return false;\r\n            ltMags = (uiA0 < uiB0);\r\n        } else {\r\n            ltMags = (uiA64 < uiB64);\r\n        }\r\n        return signA ^ ltMags;\r\n    }\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/extF80M_lt_quiet.c ****/\n/**** start inlining ../../source/f128_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t\r\n f128_to_ui32( float128_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64;\r\n    int_fast32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 ) | (uiA0 != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (ui32_fromNaN != ui32_fromPosOverflow) || (ui32_fromNaN != ui32_fromNegOverflow)\r\n    if ( (exp == 0x7FFF) && sig64 ) {\r\n#if (ui32_fromNaN == ui32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (ui32_fromNaN == ui32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return ui32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig64 |= UINT64_C( 0x0001000000000000 );\r\n    shiftDist = 0x4023 - exp;\r\n    if ( 0 < shiftDist ) {\r\n        sig64 = softfloat_shiftRightJam64( sig64, shiftDist );\r\n    }\r\n    return softfloat_roundToUI32( sign, sig64, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_ui32.c ****/\n/**** start inlining ../../source/f128_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t\r\n f128_to_ui64( float128_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64, sig0;\r\n    int_fast32_t shiftDist;\r\n    struct uint128 sig128;\r\n    struct uint64_extra sigExtra;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 );\r\n    sig0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( shiftDist <= 0 ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( shiftDist < -15 ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            return\r\n                (exp == 0x7FFF) && (sig64 | sig0) ? ui64_fromNaN\r\n                    : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig64 |= UINT64_C( 0x0001000000000000 );\r\n        if ( shiftDist ) {\r\n            sig128 = softfloat_shortShiftLeft128( sig64, sig0, -shiftDist );\r\n            sig64 = sig128.v64;\r\n            sig0  = sig128.v0;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( exp ) sig64 |= UINT64_C( 0x0001000000000000 );\r\n        sigExtra = softfloat_shiftRightJam64Extra( sig64, sig0, shiftDist );\r\n        sig64 = sigExtra.v;\r\n        sig0  = sigExtra.extra;\r\n    }\r\n    return softfloat_roundToUI64( sign, sig64, sig0, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_ui64.c ****/\n/**** start inlining ../../source/f128_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f128_to_i32( float128_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64, sig0;\r\n    int_fast32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 );\r\n    sig0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (i32_fromNaN != i32_fromPosOverflow) || (i32_fromNaN != i32_fromNegOverflow)\r\n    if ( (exp == 0x7FFF) && (sig64 | sig0) ) {\r\n#if (i32_fromNaN == i32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (i32_fromNaN == i32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return i32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig64 |= UINT64_C( 0x0001000000000000 );\r\n    sig64 |= (sig0 != 0);\r\n    shiftDist = 0x4023 - exp;\r\n    if ( 0 < shiftDist ) sig64 = softfloat_shiftRightJam64( sig64, shiftDist );\r\n    return softfloat_roundToI32( sign, sig64, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_i32.c ****/\n/**** start inlining ../../source/f128_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f128_to_i64( float128_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64, sig0;\r\n    int_fast32_t shiftDist;\r\n    struct uint128 sig128;\r\n    struct uint64_extra sigExtra;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 );\r\n    sig0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( shiftDist <= 0 ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( shiftDist < -15 ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            return\r\n                (exp == 0x7FFF) && (sig64 | sig0) ? i64_fromNaN\r\n                    : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig64 |= UINT64_C( 0x0001000000000000 );\r\n        if ( shiftDist ) {\r\n            sig128 = softfloat_shortShiftLeft128( sig64, sig0, -shiftDist );\r\n            sig64 = sig128.v64;\r\n            sig0  = sig128.v0;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( exp ) sig64 |= UINT64_C( 0x0001000000000000 );\r\n        sigExtra = softfloat_shiftRightJam64Extra( sig64, sig0, shiftDist );\r\n        sig64 = sigExtra.v;\r\n        sig0  = sigExtra.extra;\r\n    }\r\n    return softfloat_roundToI64( sign, sig64, sig0, roundingMode, exact );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_i64.c ****/\n/**** start inlining ../../source/f128_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast32_t f128_to_ui32_r_minMag( float128_t a, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64;\r\n    int_fast32_t shiftDist;\r\n    bool sign;\r\n    uint_fast32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 ) | (uiA0 != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( 49 <= shiftDist ) {\r\n        if ( exact && (exp | sig64) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF128UI64( uiA64 );\r\n    if ( sign || (shiftDist < 17) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && sig64 ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig64 |= UINT64_C( 0x0001000000000000 );\r\n    z = sig64>>shiftDist;\r\n    if ( exact && ((uint_fast64_t) z<<shiftDist != sig64) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/f128_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nuint_fast64_t f128_to_ui64_r_minMag( float128_t a, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64, sig0;\r\n    int_fast32_t shiftDist;\r\n    int_fast8_t negShiftDist;\r\n    uint_fast64_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 );\r\n    sig0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( shiftDist < 0 ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( sign || (shiftDist < -15) ) goto invalid;\r\n        sig64 |= UINT64_C( 0x0001000000000000 );\r\n        negShiftDist = -shiftDist;\r\n        z = sig64<<negShiftDist | sig0>>(shiftDist & 63);\r\n        if ( exact && (uint64_t) (sig0<<negShiftDist) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( 49 <= shiftDist ) {\r\n            if ( exact && (exp | sig64 | sig0) ) {\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            }\r\n            return 0;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( sign ) goto invalid;\r\n        sig64 |= UINT64_C( 0x0001000000000000 );\r\n        z = sig64>>shiftDist;\r\n        if ( exact && (sig0 || (z<<shiftDist != sig64)) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && (sig64 | sig0) ? ui64_fromNaN\r\n            : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/f128_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast32_t f128_to_i32_r_minMag( float128_t a, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64;\r\n    int_fast32_t shiftDist;\r\n    bool sign;\r\n    int_fast32_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 ) | (uiA0 != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( 49 <= shiftDist ) {\r\n        if ( exact && (exp | sig64) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF128UI64( uiA64 );\r\n    if ( shiftDist < 18 ) {\r\n        if (\r\n            sign && (shiftDist == 17)\r\n                && (sig64 < UINT64_C( 0x0000000000020000 ))\r\n        ) {\r\n            if ( exact && sig64 ) {\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            }\r\n            return -0x7FFFFFFF - 1;\r\n        }\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && sig64 ? i32_fromNaN\r\n                : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig64 |= UINT64_C( 0x0001000000000000 );\r\n    absZ = sig64>>shiftDist;\r\n    if (\r\n        exact && ((uint_fast64_t) (uint_fast32_t) absZ<<shiftDist != sig64)\r\n    ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/f128_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nint_fast64_t f128_to_i64_r_minMag( float128_t a, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t sig64, sig0;\r\n    int_fast32_t shiftDist;\r\n    int_fast8_t negShiftDist;\r\n    int_fast64_t absZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    sig64 = fracF128UI64( uiA64 );\r\n    sig0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( shiftDist < 0 ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( shiftDist < -14 ) {\r\n            if (\r\n                   (uiA64 == UINT64_C( 0xC03E000000000000 ))\r\n                && (sig0 < UINT64_C( 0x0002000000000000 ))\r\n            ) {\r\n                if ( exact && sig0 ) {\r\n                    softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n                }\r\n                return -INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1;\r\n            }\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n            return\r\n                (exp == 0x7FFF) && (sig64 | sig0) ? i64_fromNaN\r\n                    : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig64 |= UINT64_C( 0x0001000000000000 );\r\n        negShiftDist = -shiftDist;\r\n        absZ = sig64<<negShiftDist | sig0>>(shiftDist & 63);\r\n        if ( exact && (uint64_t) (sig0<<negShiftDist) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( 49 <= shiftDist ) {\r\n            if ( exact && (exp | sig64 | sig0) ) {\r\n                softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            }\r\n            return 0;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        sig64 |= UINT64_C( 0x0001000000000000 );\r\n        absZ = sig64>>shiftDist;\r\n        if ( exact && (sig0 || (absZ<<shiftDist != sig64)) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    }\r\n    return sign ? -absZ : absZ;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/f128_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat16_t f128_to_f16( float128_t a )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t frac64;\r\n    struct commonNaN commonNaN;\r\n    uint_fast16_t uiZ, frac16;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    frac64 = fracF128UI64( uiA64 ) | (uiA0 != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac64 ) {\r\n            softfloat_f128UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF16UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF16UI( sign, 0x1F, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac16 = softfloat_shortShiftRightJam64( frac64, 34 );\r\n    if ( ! (exp | frac16) ) {\r\n        uiZ = packToF16UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3FF1;\r\n    if ( sizeof (int_fast16_t) < sizeof (int_fast32_t) ) {\r\n        if ( exp < -0x40 ) exp = -0x40;\r\n    }\r\n    return softfloat_roundPackToF16( sign, exp, frac16 | 0x4000 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_f16.c ****/\n/**** start inlining ../../source/f128_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat32_t f128_to_f32( float128_t a )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t frac64;\r\n    struct commonNaN commonNaN;\r\n    uint_fast32_t uiZ, frac32;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    frac64 = fracF128UI64( uiA64 ) | (uiA0 != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac64 ) {\r\n            softfloat_f128UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF32UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF32UI( sign, 0xFF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac32 = softfloat_shortShiftRightJam64( frac64, 18 );\r\n    if ( ! (exp | frac32) ) {\r\n        uiZ = packToF32UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3F81;\r\n    if ( sizeof (int_fast16_t) < sizeof (int_fast32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return softfloat_roundPackToF32( sign, exp, frac32 | 0x40000000 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_f32.c ****/\n/**** start inlining ../../source/f128_to_extF80.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nextFloat80_t f128_to_extF80( float128_t a )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t frac64, frac0;\r\n    struct commonNaN commonNaN;\r\n    struct uint128 uiZ;\r\n    uint_fast16_t uiZ64;\r\n    uint_fast64_t uiZ0;\r\n    struct exp32_sig128 normExpSig;\r\n    struct uint128 sig128;\r\n    union { struct extFloat80M s; extFloat80_t f; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign   = signF128UI64( uiA64 );\r\n    exp    = expF128UI64( uiA64 );\r\n    frac64 = fracF128UI64( uiA64 );\r\n    frac0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac64 | frac0 ) {\r\n            softfloat_f128UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToExtF80UI( &commonNaN );\r\n            uiZ64 = uiZ.v64;\r\n            uiZ0  = uiZ.v0;\r\n        } else {\r\n            uiZ64 = packToExtF80UI64( sign, 0x7FFF );\r\n            uiZ0  = UINT64_C( 0x8000000000000000 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! exp ) {\r\n        if ( ! (frac64 | frac0) ) {\r\n            uiZ64 = packToExtF80UI64( sign, 0 );\r\n            uiZ0  = 0;\r\n            goto uiZ;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF128Sig( frac64, frac0 );\r\n        exp   = normExpSig.exp;\r\n        frac64 = normExpSig.sig.v64;\r\n        frac0  = normExpSig.sig.v0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig128 =\r\n        softfloat_shortShiftLeft128(\r\n            frac64 | UINT64_C( 0x0001000000000000 ), frac0, 15 );\r\n    return softfloat_roundPackToExtF80( sign, exp, sig128.v64, sig128.v0, 80 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.s.signExp = uiZ64;\r\n    uZ.s.signif  = uiZ0;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_extF80.c ****/\n/**** start inlining ../../source/f128_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat64_t f128_to_f64( float128_t a )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool sign;\r\n    int_fast32_t exp;\r\n    uint_fast64_t frac64, frac0;\r\n    struct commonNaN commonNaN;\r\n    uint_fast64_t uiZ;\r\n    struct uint128 frac128;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    sign  = signF128UI64( uiA64 );\r\n    exp   = expF128UI64( uiA64 );\r\n    frac64 = fracF128UI64( uiA64 );\r\n    frac0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac64 | frac0 ) {\r\n            softfloat_f128UIToCommonNaN( uiA64, uiA0, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF64UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac128 = softfloat_shortShiftLeft128( frac64, frac0, 14 );\r\n    frac64 = frac128.v64 | (frac128.v0 != 0);\r\n    if ( ! (exp | frac64) ) {\r\n        uiZ = packToF64UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3C01;\r\n    if ( sizeof (int_fast16_t) < sizeof (int_fast32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return\r\n        softfloat_roundPackToF64(\r\n            sign, exp, frac64 | UINT64_C( 0x4000000000000000 ) );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_to_f64.c ****/\n/**** start inlining ../../source/f128_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t\r\n f128_roundToInt( float128_t a, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    int_fast32_t exp;\r\n    struct uint128 uiZ;\r\n    uint_fast64_t lastBitMask0, roundBitsMask;\r\n    bool roundNearEven;\r\n    uint_fast64_t lastBitMask64;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    exp = expF128UI64( uiA64 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x402F <= exp ) {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( 0x406F <= exp ) {\r\n            if ( (exp == 0x7FFF) && (fracF128UI64( uiA64 ) | uiA0) ) {\r\n                uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, 0, 0 );\r\n                goto uiZ;\r\n            }\r\n            return a;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        lastBitMask0 = (uint_fast64_t) 2<<(0x406E - exp);\r\n        roundBitsMask = lastBitMask0 - 1;\r\n        uiZ.v64 = uiA64;\r\n        uiZ.v0  = uiA0;\r\n        roundNearEven = (roundingMode == softfloat_round_near_even);\r\n        if ( roundNearEven || (roundingMode == softfloat_round_near_maxMag) ) {\r\n            if ( exp == 0x402F ) {\r\n                if ( UINT64_C( 0x8000000000000000 ) <= uiZ.v0 ) {\r\n                    ++uiZ.v64;\r\n                    if (\r\n                        roundNearEven\r\n                            && (uiZ.v0 == UINT64_C( 0x8000000000000000 ))\r\n                    ) {\r\n                        uiZ.v64 &= ~1;\r\n                    }\r\n                }\r\n            } else {\r\n                uiZ = softfloat_add128( uiZ.v64, uiZ.v0, 0, lastBitMask0>>1 );\r\n                if ( roundNearEven && !(uiZ.v0 & roundBitsMask) ) {\r\n                    uiZ.v0 &= ~lastBitMask0;\r\n                }\r\n            }\r\n        } else if (\r\n            roundingMode\r\n                == (signF128UI64( uiZ.v64 ) ? softfloat_round_min\r\n                        : softfloat_round_max)\r\n        ) {\r\n            uiZ = softfloat_add128( uiZ.v64, uiZ.v0, 0, roundBitsMask );\r\n        }\r\n        uiZ.v0 &= ~roundBitsMask;\r\n        lastBitMask64 = !lastBitMask0;\r\n    } else {\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        if ( exp < 0x3FFF ) {\r\n            if ( !((uiA64 & UINT64_C( 0x7FFFFFFFFFFFFFFF )) | uiA0) ) return a;\r\n            if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            uiZ.v64 = uiA64 & packToF128UI64( 1, 0, 0 );\r\n            uiZ.v0  = 0;\r\n            switch ( roundingMode ) {\r\n             case softfloat_round_near_even:\r\n                if ( !(fracF128UI64( uiA64 ) | uiA0) ) break;\r\n             case softfloat_round_near_maxMag:\r\n                if ( exp == 0x3FFE ) uiZ.v64 |= packToF128UI64( 0, 0x3FFF, 0 );\r\n                break;\r\n             case softfloat_round_min:\r\n                if ( uiZ.v64 ) uiZ.v64 = packToF128UI64( 1, 0x3FFF, 0 );\r\n                break;\r\n             case softfloat_round_max:\r\n                if ( !uiZ.v64 ) uiZ.v64 = packToF128UI64( 0, 0x3FFF, 0 );\r\n                break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n             case softfloat_round_odd:\r\n                uiZ.v64 |= packToF128UI64( 0, 0x3FFF, 0 );\r\n                break;\r\n#endif\r\n            }\r\n            goto uiZ;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        *--------------------------------------------------------------------*/\r\n        uiZ.v64 = uiA64;\r\n        uiZ.v0  = 0;\r\n        lastBitMask64 = (uint_fast64_t) 1<<(0x402F - exp);\r\n        roundBitsMask = lastBitMask64 - 1;\r\n        if ( roundingMode == softfloat_round_near_maxMag ) {\r\n            uiZ.v64 += lastBitMask64>>1;\r\n        } else if ( roundingMode == softfloat_round_near_even ) {\r\n            uiZ.v64 += lastBitMask64>>1;\r\n            if ( !((uiZ.v64 & roundBitsMask) | uiA0) ) {\r\n                uiZ.v64 &= ~lastBitMask64;\r\n            }\r\n        } else if (\r\n            roundingMode\r\n                == (signF128UI64( uiZ.v64 ) ? softfloat_round_min\r\n                        : softfloat_round_max)\r\n        ) {\r\n            uiZ.v64 = (uiZ.v64 | (uiA0 != 0)) + roundBitsMask;\r\n        }\r\n        uiZ.v64 &= ~roundBitsMask;\r\n        lastBitMask0 = 0;\r\n    }\r\n    if ( (uiZ.v64 != uiA64) || (uiZ.v0 != uiA0) ) {\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n        if ( roundingMode == softfloat_round_odd ) {\r\n            uiZ.v64 |= lastBitMask64;\r\n            uiZ.v0  |= lastBitMask0;\r\n        }\r\n#endif\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_roundToInt.c ****/\n/**** start inlining ../../source/f128_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_add( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    float128_t\r\n        (*magsFuncPtr)(\r\n            uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    signA = signF128UI64( uiA64 );\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    signB = signF128UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        return softfloat_addMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        return softfloat_subMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_addMagsF128 : softfloat_subMagsF128;\r\n    return (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_add.c ****/\n/**** start inlining ../../source/f128_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_sub( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    float128_t\r\n        (*magsFuncPtr)(\r\n            uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    signA = signF128UI64( uiA64 );\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    signB = signF128UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        return softfloat_subMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        return softfloat_addMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_subMagsF128 : softfloat_addMagsF128;\r\n    return (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_sub.c ****/\n/**** start inlining ../../source/f128_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_mul( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    struct uint128 sigA;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signB;\r\n    int_fast32_t expB;\r\n    struct uint128 sigB;\r\n    bool signZ;\r\n    uint_fast64_t magBits;\r\n    struct exp32_sig128 normExpSig;\r\n    int_fast32_t expZ;\r\n    uint64_t sig256Z[4];\r\n    uint_fast64_t sigZExtra;\r\n    struct uint128 sigZ;\r\n    struct uint128_extra sig128Extra;\r\n    struct uint128 uiZ;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    signA = signF128UI64( uiA64 );\r\n    expA  = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    signB = signF128UI64( uiB64 );\r\n    expB  = expF128UI64( uiB64 );\r\n    sigB.v64 = fracF128UI64( uiB64 );\r\n    sigB.v0  = uiB0;\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if (\r\n            (sigA.v64 | sigA.v0) || ((expB == 0x7FFF) && (sigB.v64 | sigB.v0))\r\n        ) {\r\n            goto propagateNaN;\r\n        }\r\n        magBits = expB | sigB.v64 | sigB.v0;\r\n        goto infArg;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n        magBits = expA | sigA.v64 | sigA.v0;\r\n        goto infArg;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! (sigA.v64 | sigA.v0) ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    if ( ! expB ) {\r\n        if ( ! (sigB.v64 | sigB.v0) ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigB.v64, sigB.v0 );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x4000;\r\n    sigA.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sigB = softfloat_shortShiftLeft128( sigB.v64, sigB.v0, 16 );\r\n    softfloat_mul128To256M( sigA.v64, sigA.v0, sigB.v64, sigB.v0, sig256Z );\r\n    sigZExtra = sig256Z[indexWord( 4, 1 )] | (sig256Z[indexWord( 4, 0 )] != 0);\r\n    sigZ =\r\n        softfloat_add128(\r\n            sig256Z[indexWord( 4, 3 )], sig256Z[indexWord( 4, 2 )],\r\n            sigA.v64, sigA.v0\r\n        );\r\n    if ( UINT64_C( 0x0002000000000000 ) <= sigZ.v64 ) {\r\n        ++expZ;\r\n        sig128Extra =\r\n            softfloat_shortShiftRightJam128Extra(\r\n                sigZ.v64, sigZ.v0, sigZExtra, 1 );\r\n        sigZ = sig128Extra.v;\r\n        sigZExtra = sig128Extra.extra;\r\n    }\r\n    return\r\n        softfloat_roundPackToF128( signZ, expZ, sigZ.v64, sigZ.v0, sigZExtra );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infArg:\r\n    if ( ! magBits ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        uiZ.v64 = defaultNaNF128UI64;\r\n        uiZ.v0  = defaultNaNF128UI0;\r\n        goto uiZ;\r\n    }\r\n    uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );\r\n    goto uiZ0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ.v64 = packToF128UI64( signZ, 0, 0 );\r\n uiZ0:\r\n    uiZ.v0 = 0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_mul.c ****/\n/**** start inlining ../../source/f128_mulAdd.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_mulAdd( float128_t a, float128_t b, float128_t c )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    union ui128_f128 uC;\r\n    uint_fast64_t uiC64, uiC0;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    uC.f = c;\r\n    uiC64 = uC.ui.v64;\r\n    uiC0  = uC.ui.v0;\r\n    return softfloat_mulAddF128( uiA64, uiA0, uiB64, uiB0, uiC64, uiC0, 0 );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_mulAdd.c ****/\n/**** start inlining ../../source/f128_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_div( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    struct uint128 sigA;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signB;\r\n    int_fast32_t expB;\r\n    struct uint128 sigB;\r\n    bool signZ;\r\n    struct exp32_sig128 normExpSig;\r\n    int_fast32_t expZ;\r\n    struct uint128 rem;\r\n    uint_fast32_t recip32;\r\n    int ix;\r\n    uint_fast64_t q64;\r\n    uint_fast32_t q;\r\n    struct uint128 term;\r\n    uint_fast32_t qs[3];\r\n    uint_fast64_t sigZExtra;\r\n    struct uint128 sigZ, uiZ;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    signA = signF128UI64( uiA64 );\r\n    expA  = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    signB = signF128UI64( uiB64 );\r\n    expB  = expF128UI64( uiB64 );\r\n    sigB.v64 = fracF128UI64( uiB64 );\r\n    sigB.v0  = uiB0;\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA.v64 | sigA.v0 ) goto propagateNaN;\r\n        if ( expB == 0x7FFF ) {\r\n            if ( sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n            goto invalid;\r\n        }\r\n        goto infinity;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! (sigB.v64 | sigB.v0) ) {\r\n            if ( ! (expA | sigA.v64 | sigA.v0) ) goto invalid;\r\n            softfloat_raiseFlags( softfloat_flag_infinite );\r\n            goto infinity;\r\n        }\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigB.v64, sigB.v0 );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! (sigA.v64 | sigA.v0) ) goto zero;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0x3FFE;\r\n    sigA.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sigB.v64 |= UINT64_C( 0x0001000000000000 );\r\n    rem = sigA;\r\n    if ( softfloat_lt128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 ) ) {\r\n        --expZ;\r\n        rem = softfloat_add128( sigA.v64, sigA.v0, sigA.v64, sigA.v0 );\r\n    }\r\n    recip32 = softfloat_approxRecip32_1( sigB.v64>>17 );\r\n    ix = 3;\r\n    for (;;) {\r\n        q64 = (uint_fast64_t) (uint32_t) (rem.v64>>19) * recip32;\r\n        q = (q64 + 0x80000000)>>32;\r\n        --ix;\r\n        if ( ix < 0 ) break;\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n        term = softfloat_mul128By32( sigB.v64, sigB.v0, q );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            --q;\r\n            rem = softfloat_add128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n        }\r\n        qs[ix] = q;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ((q + 1) & 7) < 2 ) {\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n        term = softfloat_mul128By32( sigB.v64, sigB.v0, q );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            --q;\r\n            rem = softfloat_add128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n        } else if ( softfloat_le128( sigB.v64, sigB.v0, rem.v64, rem.v0 ) ) {\r\n            ++q;\r\n            rem = softfloat_sub128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n        }\r\n        if ( rem.v64 | rem.v0 ) q |= 1;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigZExtra = (uint64_t) ((uint_fast64_t) q<<60);\r\n    term = softfloat_shortShiftLeft128( 0, qs[1], 54 );\r\n    sigZ =\r\n        softfloat_add128(\r\n            (uint_fast64_t) qs[2]<<19, ((uint_fast64_t) qs[0]<<25) + (q>>4),\r\n            term.v64, term.v0\r\n        );\r\n    return\r\n        softfloat_roundPackToF128( signZ, expZ, sigZ.v64, sigZ.v0, sigZExtra );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ.v64 = defaultNaNF128UI64;\r\n    uiZ.v0  = defaultNaNF128UI0;\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );\r\n    goto uiZ0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ.v64 = packToF128UI64( signZ, 0, 0 );\r\n uiZ0:\r\n    uiZ.v0 = 0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_div.c ****/\n/**** start inlining ../../source/f128_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_rem( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    struct uint128 sigA;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    int_fast32_t expB;\r\n    struct uint128 sigB;\r\n    struct exp32_sig128 normExpSig;\r\n    struct uint128 rem;\r\n    int_fast32_t expDiff;\r\n    uint_fast32_t q, recip32;\r\n    uint_fast64_t q64;\r\n    struct uint128 term, altRem, meanRem;\r\n    bool signRem;\r\n    struct uint128 uiZ;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    signA = signF128UI64( uiA64 );\r\n    expA  = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    expB  = expF128UI64( uiB64 );\r\n    sigB.v64 = fracF128UI64( uiB64 );\r\n    sigB.v0  = uiB0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if (\r\n            (sigA.v64 | sigA.v0) || ((expB == 0x7FFF) && (sigB.v64 | sigB.v0))\r\n        ) {\r\n            goto propagateNaN;\r\n        }\r\n        goto invalid;\r\n    }\r\n    if ( expB == 0x7FFF ) {\r\n        if ( sigB.v64 | sigB.v0 ) goto propagateNaN;\r\n        return a;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expB ) {\r\n        if ( ! (sigB.v64 | sigB.v0) ) goto invalid;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigB.v64, sigB.v0 );\r\n        expB = normExpSig.exp;\r\n        sigB = normExpSig.sig;\r\n    }\r\n    if ( ! expA ) {\r\n        if ( ! (sigA.v64 | sigA.v0) ) return a;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sigA.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sigB.v64 |= UINT64_C( 0x0001000000000000 );\r\n    rem = sigA;\r\n    expDiff = expA - expB;\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff < -1 ) return a;\r\n        if ( expDiff ) {\r\n            --expB;\r\n            sigB = softfloat_add128( sigB.v64, sigB.v0, sigB.v64, sigB.v0 );\r\n            q = 0;\r\n        } else {\r\n            q = softfloat_le128( sigB.v64, sigB.v0, rem.v64, rem.v0 );\r\n            if ( q ) {\r\n                rem = softfloat_sub128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n            }\r\n        }\r\n    } else {\r\n        recip32 = softfloat_approxRecip32_1( sigB.v64>>17 );\r\n        expDiff -= 30;\r\n        for (;;) {\r\n            q64 = (uint_fast64_t) (uint32_t) (rem.v64>>19) * recip32;\r\n            if ( expDiff < 0 ) break;\r\n            q = (q64 + 0x80000000)>>32;\r\n            rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n            term = softfloat_mul128By32( sigB.v64, sigB.v0, q );\r\n            rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n            if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n                rem = softfloat_add128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n            }\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -29 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q = (uint32_t) (q64>>32)>>(~expDiff & 31);\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, expDiff + 30 );\r\n        term = softfloat_mul128By32( sigB.v64, sigB.v0, q );\r\n        rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );\r\n        if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            altRem = softfloat_add128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n            goto selectRem;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    do {\r\n        altRem = rem;\r\n        ++q;\r\n        rem = softfloat_sub128( rem.v64, rem.v0, sigB.v64, sigB.v0 );\r\n    } while ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) );\r\n selectRem:\r\n    meanRem = softfloat_add128( rem.v64, rem.v0, altRem.v64, altRem.v0 );\r\n    if (\r\n        (meanRem.v64 & UINT64_C( 0x8000000000000000 ))\r\n            || (! (meanRem.v64 | meanRem.v0) && (q & 1))\r\n    ) {\r\n        rem = altRem;\r\n    }\r\n    signRem = signA;\r\n    if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n        signRem = ! signRem;\r\n        rem = softfloat_sub128( 0, 0, rem.v64, rem.v0 );\r\n    }\r\n    return softfloat_normRoundPackToF128( signRem, expB - 1, rem.v64, rem.v0 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n propagateNaN:\r\n    uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );\r\n    goto uiZ;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ.v64 = defaultNaNF128UI64;\r\n    uiZ.v0  = defaultNaNF128UI0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_rem.c ****/\n/**** start inlining ../../source/f128_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nfloat128_t f128_sqrt( float128_t a )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    int_fast32_t expA;\r\n    struct uint128 sigA, uiZ;\r\n    struct exp32_sig128 normExpSig;\r\n    int_fast32_t expZ;\r\n    uint_fast32_t sig32A, recipSqrt32, sig32Z;\r\n    struct uint128 rem;\r\n    uint32_t qs[3];\r\n    uint_fast32_t q;\r\n    uint_fast64_t x64, sig64Z;\r\n    struct uint128 y, term;\r\n    uint_fast64_t sigZExtra;\r\n    struct uint128 sigZ;\r\n    union ui128_f128 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    signA = signF128UI64( uiA64 );\r\n    expA  = expF128UI64( uiA64 );\r\n    sigA.v64 = fracF128UI64( uiA64 );\r\n    sigA.v0  = uiA0;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA == 0x7FFF ) {\r\n        if ( sigA.v64 | sigA.v0 ) {\r\n            uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, 0, 0 );\r\n            goto uiZ;\r\n        }\r\n        if ( ! signA ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( signA ) {\r\n        if ( ! (expA | sigA.v64 | sigA.v0) ) return a;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ! expA ) {\r\n        if ( ! (sigA.v64 | sigA.v0) ) return a;\r\n        normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );\r\n        expA = normExpSig.exp;\r\n        sigA = normExpSig.sig;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    | (`sig32Z' is guaranteed to be a lower bound on the square root of\r\n    | `sig32A', which makes `sig32Z' also a lower bound on the square root of\r\n    | `sigA'.)\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0x3FFF)>>1) + 0x3FFE;\r\n    expA &= 1;\r\n    sigA.v64 |= UINT64_C( 0x0001000000000000 );\r\n    sig32A = sigA.v64>>17;\r\n    recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );\r\n    sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32;\r\n    if ( expA ) {\r\n        sig32Z >>= 1;\r\n        rem = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 12 );\r\n    } else {\r\n        rem = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 13 );\r\n    }\r\n    qs[2] = sig32Z;\r\n    rem.v64 -= (uint_fast64_t) sig32Z * sig32Z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = ((uint32_t) (rem.v64>>2) * (uint_fast64_t) recipSqrt32)>>32;\r\n    x64 = (uint_fast64_t) sig32Z<<32;\r\n    sig64Z = x64 + ((uint_fast64_t) q<<3);\r\n    y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n    /*------------------------------------------------------------------------\r\n    | (Repeating this loop is a rare occurrence.)\r\n    *------------------------------------------------------------------------*/\r\n    for (;;) {\r\n        term = softfloat_mul64ByShifted32To128( x64 + sig64Z, q );\r\n        rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 );\r\n        if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break;\r\n        --q;\r\n        sig64Z -= 1<<3;\r\n    }\r\n    qs[1] = q;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = ((rem.v64>>2) * recipSqrt32)>>32;\r\n    y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );\r\n    sig64Z <<= 1;\r\n    /*------------------------------------------------------------------------\r\n    | (Repeating this loop is a rare occurrence.)\r\n    *------------------------------------------------------------------------*/\r\n    for (;;) {\r\n        term = softfloat_shortShiftLeft128( 0, sig64Z, 32 );\r\n        term = softfloat_add128( term.v64, term.v0, 0, (uint_fast64_t) q<<6 );\r\n        term = softfloat_mul128By32( term.v64, term.v0, q );\r\n        rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 );\r\n        if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break;\r\n        --q;\r\n    }\r\n    qs[0] = q;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = (((rem.v64>>2) * recipSqrt32)>>32) + 2;\r\n    sigZExtra = (uint64_t) ((uint_fast64_t) q<<59);\r\n    term = softfloat_shortShiftLeft128( 0, qs[1], 53 );\r\n    sigZ =\r\n        softfloat_add128(\r\n            (uint_fast64_t) qs[2]<<18, ((uint_fast64_t) qs[0]<<24) + (q>>5),\r\n            term.v64, term.v0\r\n        );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (q & 0xF) <= 2 ) {\r\n        q &= ~3;\r\n        sigZExtra = (uint64_t) ((uint_fast64_t) q<<59);\r\n        y = softfloat_shortShiftLeft128( sigZ.v64, sigZ.v0, 6 );\r\n        y.v0 |= sigZExtra>>58;\r\n        term = softfloat_sub128( y.v64, y.v0, 0, q );\r\n        y    = softfloat_mul64ByShifted32To128( term.v0,  q );\r\n        term = softfloat_mul64ByShifted32To128( term.v64, q );\r\n        term = softfloat_add128( term.v64, term.v0, 0, y.v64 );\r\n        rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 20 );\r\n        term = softfloat_sub128( term.v64, term.v0, rem.v64, rem.v0 );\r\n        /*--------------------------------------------------------------------\r\n        | The concatenation of `term' and `y.v0' is now the negative remainder\r\n        | (3 words altogether).\r\n        *--------------------------------------------------------------------*/\r\n        if ( term.v64 & UINT64_C( 0x8000000000000000 ) ) {\r\n            sigZExtra |= 1;\r\n        } else {\r\n            if ( term.v64 | term.v0 | y.v0 ) {\r\n                if ( sigZExtra ) {\r\n                    --sigZExtra;\r\n                } else {\r\n                    sigZ = softfloat_sub128( sigZ.v64, sigZ.v0, 0, 1 );\r\n                    sigZExtra = ~0;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    return softfloat_roundPackToF128( 0, expZ, sigZ.v64, sigZ.v0, sigZExtra );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    uiZ.v64 = defaultNaNF128UI64;\r\n    uiZ.v0  = defaultNaNF128UI0;\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_sqrt.c ****/\n/**** start inlining ../../source/f128_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_eq( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    if ( isNaNF128UI( uiA64, uiA0 ) || isNaNF128UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNF128UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNF128UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    return\r\n           (uiA0 == uiB0)\r\n        && (   (uiA64 == uiB64)\r\n            || (! uiA0 && ! ((uiA64 | uiB64) & UINT64_C( 0x7FFFFFFFFFFFFFFF )))\r\n           );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_eq.c ****/\n/**** start inlining ../../source/f128_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_le( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    if ( isNaNF128UI( uiA64, uiA0 ) || isNaNF128UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF128UI64( uiA64 );\r\n    signB = signF128UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA\r\n                  || ! (((uiA64 | uiB64) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                            | uiA0 | uiB0)\r\n            : ((uiA64 == uiB64) && (uiA0 == uiB0))\r\n                  || (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_le.c ****/\n/**** start inlining ../../source/f128_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_lt( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    if ( isNaNF128UI( uiA64, uiA0 ) || isNaNF128UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    signA = signF128UI64( uiA64 );\r\n    signB = signF128UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA\r\n                  && (((uiA64 | uiB64) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                          | uiA0 | uiB0)\r\n            : ((uiA64 != uiB64) || (uiA0 != uiB0))\r\n                  && (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_lt.c ****/\n/**** start inlining ../../source/f128_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_eq_signaling( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    if ( isNaNF128UI( uiA64, uiA0 ) || isNaNF128UI( uiB64, uiB0 ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    return\r\n           (uiA0 == uiB0)\r\n        && (   (uiA64 == uiB64)\r\n            || (! uiA0 && ! ((uiA64 | uiB64) & UINT64_C( 0x7FFFFFFFFFFFFFFF )))\r\n           );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_eq_signaling.c ****/\n/**** start inlining ../../source/f128_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_le_quiet( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    if ( isNaNF128UI( uiA64, uiA0 ) || isNaNF128UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNF128UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNF128UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF128UI64( uiA64 );\r\n    signB = signF128UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA\r\n                  || ! (((uiA64 | uiB64) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                            | uiA0 | uiB0)\r\n            : ((uiA64 == uiB64) && (uiA0 == uiB0))\r\n                  || (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_le_quiet.c ****/\n/**** start inlining ../../source/f128_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_lt_quiet( float128_t a, float128_t b )\r\n{\r\n    union ui128_f128 uA;\r\n    uint_fast64_t uiA64, uiA0;\r\n    union ui128_f128 uB;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signA, signB;\r\n\r\n    uA.f = a;\r\n    uiA64 = uA.ui.v64;\r\n    uiA0  = uA.ui.v0;\r\n    uB.f = b;\r\n    uiB64 = uB.ui.v64;\r\n    uiB0  = uB.ui.v0;\r\n    if ( isNaNF128UI( uiA64, uiA0 ) || isNaNF128UI( uiB64, uiB0 ) ) {\r\n        if (\r\n               softfloat_isSigNaNF128UI( uiA64, uiA0 )\r\n            || softfloat_isSigNaNF128UI( uiB64, uiB0 )\r\n        ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    signA = signF128UI64( uiA64 );\r\n    signB = signF128UI64( uiB64 );\r\n    return\r\n        (signA != signB)\r\n            ? signA\r\n                  && (((uiA64 | uiB64) & UINT64_C( 0x7FFFFFFFFFFFFFFF ))\r\n                          | uiA0 | uiB0)\r\n            : ((uiA64 != uiB64) || (uiA0 != uiB0))\r\n                  && (signA ^ softfloat_lt128( uiA64, uiA0, uiB64, uiB0 ));\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_lt_quiet.c ****/\n/**** start inlining ../../source/f128_isSignalingNaN.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\nbool f128_isSignalingNaN( float128_t a )\r\n{\r\n    union ui128_f128 uA;\r\n\r\n    uA.f = a;\r\n    return softfloat_isSigNaNF128UI( uA.ui.v64, uA.ui.v0 );\r\n\r\n}\r\n\r\n/**** ended inlining ../../source/f128_isSignalingNaN.c ****/\n/**** start inlining ../../source/f128M_to_ui32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast32_t\r\n f128M_to_ui32( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return f128_to_ui32( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast32_t\r\n f128M_to_ui32( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig64;\r\n    int32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    sig64 = (uint64_t) fracF128UI96( uiA96 )<<32 | aWPtr[indexWord( 4, 2 )];\r\n    if ( aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )] ) sig64 |= 1;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (ui32_fromNaN != ui32_fromPosOverflow) || (ui32_fromNaN != ui32_fromNegOverflow)\r\n    if ( (exp == 0x7FFF) && sig64 ) {\r\n#if (ui32_fromNaN == ui32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (ui32_fromNaN == ui32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return ui32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig64 |= UINT64_C( 0x0001000000000000 );\r\n    shiftDist = 0x4023 - exp;\r\n    if ( 0 < shiftDist ) sig64 = softfloat_shiftRightJam64( sig64, shiftDist );\r\n    return softfloat_roundToUI32( sign, sig64, roundingMode, exact );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_ui32.c ****/\n/**** start inlining ../../source/f128M_to_ui64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast64_t\r\n f128M_to_ui64( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return f128_to_ui64( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast64_t\r\n f128M_to_ui64( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint32_t sig96;\r\n    int32_t shiftDist;\r\n    uint32_t sig[4];\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign  = signF128UI96( uiA96 );\r\n    exp   = expF128UI96( uiA96 );\r\n    sig96 = fracF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x404F - exp;\r\n    if ( shiftDist < 17 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF)\r\n                && (sig96\r\n                        || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                                | aWPtr[indexWord( 4, 0 )]))\r\n                ? ui64_fromNaN\r\n                : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig96 |= 0x00010000;\r\n    sig[indexWord( 4, 3 )] = sig96;\r\n    sig[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n    sig[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n    sig[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n    softfloat_shiftRightJam128M( sig, shiftDist, sig );\r\n    return\r\n        softfloat_roundMToUI64(\r\n            sign, sig + indexMultiwordLo( 4, 3 ), roundingMode, exact );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_ui64.c ****/\n/**** start inlining ../../source/f128M_to_i32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast32_t\r\n f128M_to_i32( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return f128_to_i32( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast32_t\r\n f128M_to_i32( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig64;\r\n    int32_t shiftDist;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    sig64 = (uint64_t) fracF128UI96( uiA96 )<<32 | aWPtr[indexWord( 4, 2 )];\r\n    if ( aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )] ) sig64 |= 1;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n#if (i32_fromNaN != i32_fromPosOverflow) || (i32_fromNaN != i32_fromNegOverflow)\r\n    if ( (exp == 0x7FFF) && sig64 ) {\r\n#if (i32_fromNaN == i32_fromPosOverflow)\r\n        sign = 0;\r\n#elif (i32_fromNaN == i32_fromNegOverflow)\r\n        sign = 1;\r\n#else\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return i32_fromNaN;\r\n#endif\r\n    }\r\n#endif\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig64 |= UINT64_C( 0x0001000000000000 );\r\n    shiftDist = 0x4023 - exp;\r\n    if ( 0 < shiftDist ) sig64 = softfloat_shiftRightJam64( sig64, shiftDist );\r\n    return softfloat_roundToI32( sign, sig64, roundingMode, exact );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_i32.c ****/\n/**** start inlining ../../source/f128M_to_i64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the\r\nUniversity of California.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast64_t\r\n f128M_to_i64( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n\r\n    return f128_to_i64( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast64_t\r\n f128M_to_i64( const float128_t *aPtr, uint_fast8_t roundingMode, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint32_t sig96;\r\n    int32_t shiftDist;\r\n    uint32_t sig[4];\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign  = signF128UI96( uiA96 );\r\n    exp   = expF128UI96( uiA96 );\r\n    sig96 = fracF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x404F - exp;\r\n    if ( shiftDist < 17 ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF)\r\n                && (sig96\r\n                        || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                                | aWPtr[indexWord( 4, 0 )]))\r\n                ? i64_fromNaN\r\n                : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp ) sig96 |= 0x00010000;\r\n    sig[indexWord( 4, 3 )] = sig96;\r\n    sig[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n    sig[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n    sig[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n    softfloat_shiftRightJam128M( sig, shiftDist, sig );\r\n    return\r\n        softfloat_roundMToI64(\r\n            sign, sig + indexMultiwordLo( 4, 3 ), roundingMode, exact );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_i64.c ****/\n/**** start inlining ../../source/f128M_to_ui32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast32_t f128M_to_ui32_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n\r\n    return f128_to_ui32_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast32_t f128M_to_ui32_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    int32_t exp;\r\n    uint64_t sig64;\r\n    int32_t shiftDist;\r\n    bool sign;\r\n    uint32_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    exp = expF128UI96( uiA96 );\r\n    sig64 = (uint64_t) fracF128UI96( uiA96 )<<32 | aWPtr[indexWord( 4, 2 )];\r\n    if ( aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )] ) sig64 |= 1;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x402F - exp;\r\n    if ( 49 <= shiftDist ) {\r\n        if ( exact && (exp | sig64) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sign = signF128UI96( uiA96 );\r\n    if ( sign || (shiftDist < 17) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return\r\n            (exp == 0x7FFF) && sig64 ? ui32_fromNaN\r\n                : sign ? ui32_fromNegOverflow : ui32_fromPosOverflow;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    sig64 |= UINT64_C( 0x0001000000000000 );\r\n    z = sig64>>shiftDist;\r\n    if ( exact && ((uint64_t) z<<shiftDist != sig64) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    return z;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_ui32_r_minMag.c ****/\n/**** start inlining ../../source/f128M_to_ui64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nuint_fast64_t f128M_to_ui64_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n\r\n    return f128_to_ui64_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nuint_fast64_t f128M_to_ui64_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint32_t sig96;\r\n    int32_t shiftDist;\r\n    uint32_t sig[4];\r\n    uint64_t z;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign  = signF128UI96( uiA96 );\r\n    exp   = expF128UI96( uiA96 );\r\n    sig96 = fracF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( shiftDist < 0 ) goto invalid;\r\n    if ( exact ) {\r\n        if ( exp ) sig96 |= 0x00010000;\r\n        sig[indexWord( 4, 3 )] = sig96;\r\n        sig[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n        sig[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n        sig[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n        softfloat_shiftRightJam128M( sig, shiftDist + 17, sig );\r\n        z = (uint64_t) sig[indexWord( 4, 2 )]<<32 | sig[indexWord( 4, 1 )];\r\n        if ( sign && z ) goto invalid;\r\n        if ( sig[indexWordLo( 4 )] ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    } else {\r\n        if ( 64 <= shiftDist ) return 0;\r\n        if ( sign ) goto invalid;\r\n        z =   UINT64_C( 0x8000000000000000 )\r\n            | (uint64_t) sig96<<47\r\n            | (uint64_t) aWPtr[indexWord( 4, 2 )]<<15\r\n            | aWPtr[indexWord( 4, 1 )]>>17;\r\n        z >>= shiftDist;\r\n    }\r\n    return z;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF)\r\n            && (sig96\r\n                    || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                            | aWPtr[indexWord( 4, 0 )]))\r\n            ? ui64_fromNaN\r\n            : sign ? ui64_fromNegOverflow : ui64_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_ui64_r_minMag.c ****/\n/**** start inlining ../../source/f128M_to_i32_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast32_t f128M_to_i32_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n\r\n    return f128_to_i32_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast32_t f128M_to_i32_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t sig64;\r\n    int32_t shiftDist;\r\n    uint32_t absZ, uiZ;\r\n    union { uint32_t ui; int32_t i; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    sig64 = (uint64_t) fracF128UI96( uiA96 )<<32 | aWPtr[indexWord( 4, 2 )];\r\n    if ( aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )] ) sig64 |= 1;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp < 0x3FFF ) {\r\n        if ( exact && (exp | sig64) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        return 0;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x401F <= exp ) goto invalid;\r\n    shiftDist = 0x402F - exp;\r\n    sig64 |= UINT64_C( 0x0001000000000000 );\r\n    absZ = sig64>>shiftDist;\r\n    uiZ = sign ? -absZ : absZ;\r\n    if ( uiZ>>31 != sign ) goto invalid;\r\n    if ( exact && ((uint64_t) absZ<<shiftDist != sig64) ) {\r\n        softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n    }\r\n    uZ.ui = uiZ;\r\n    return uZ.i;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF) && sig64 ? i32_fromNaN\r\n            : sign ? i32_fromNegOverflow : i32_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_i32_r_minMag.c ****/\n/**** start inlining ../../source/f128M_to_i64_r_minMag.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nint_fast64_t f128M_to_i64_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n\r\n    return f128_to_i64_r_minMag( *aPtr, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nint_fast64_t f128M_to_i64_r_minMag( const float128_t *aPtr, bool exact )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint32_t sig96;\r\n    int32_t shiftDist;\r\n    uint32_t sig[4];\r\n    uint64_t uiZ;\r\n    union { uint64_t ui; int64_t i; } uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign  = signF128UI96( uiA96 );\r\n    exp   = expF128UI96( uiA96 );\r\n    sig96 = fracF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    shiftDist = 0x403E - exp;\r\n    if ( shiftDist < 0 ) goto invalid;\r\n    if ( exact ) {\r\n        if ( exp ) sig96 |= 0x00010000;\r\n        sig[indexWord( 4, 3 )] = sig96;\r\n        sig[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n        sig[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n        sig[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n        softfloat_shiftRightJam128M( sig, shiftDist + 17, sig );\r\n        uiZ = (uint64_t) sig[indexWord( 4, 2 )]<<32 | sig[indexWord( 4, 1 )];\r\n        if ( uiZ>>63 && (! sign || (uiZ != UINT64_C( 0x8000000000000000 ))) ) {\r\n            goto invalid;\r\n        }\r\n        if ( sig[indexWordLo( 4 )] ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n    } else {\r\n        if ( 64 <= shiftDist ) return 0;\r\n        uiZ =\r\n              (uint64_t) sig96<<47\r\n            | (uint64_t) aWPtr[indexWord( 4, 2 )]<<15\r\n            | aWPtr[indexWord( 4, 1 )]>>17;\r\n        if ( shiftDist ) {\r\n            uiZ |= UINT64_C( 0x8000000000000000 );\r\n            uiZ >>= shiftDist;\r\n        } else {\r\n            if ( uiZ || ! sign ) goto invalid;\r\n            uiZ |= UINT64_C( 0x8000000000000000 );\r\n        }\r\n    }\r\n    if ( sign ) uiZ = -uiZ;\r\n    uZ.ui = uiZ;\r\n    return uZ.i;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_raiseFlags( softfloat_flag_invalid );\r\n    return\r\n        (exp == 0x7FFF)\r\n            && (sig96\r\n                    || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                            | aWPtr[indexWord( 4, 0 )]))\r\n            ? i64_fromNaN\r\n            : sign ? i64_fromNegOverflow : i64_fromPosOverflow;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_i64_r_minMag.c ****/\n/**** start inlining ../../source/f128M_to_f16.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat16_t f128M_to_f16( const float128_t *aPtr )\r\n{\r\n\r\n    return f128_to_f16( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat16_t f128M_to_f16( const float128_t *aPtr )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint32_t frac32;\r\n    struct commonNaN commonNaN;\r\n    uint16_t uiZ, frac16;\r\n    union ui16_f16 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    frac32 =\r\n        fracF128UI96( uiA96 )\r\n            | ((aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                    | aWPtr[indexWord( 4, 0 )])\r\n                   != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac32 ) {\r\n            softfloat_f128MToCommonNaN( aWPtr, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF16UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF16UI( sign, 0x1F, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac16 = frac32>>2 | (frac32 & 3);\r\n    if ( ! (exp | frac16) ) {\r\n        uiZ = packToF16UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3FF1;\r\n    if ( sizeof (int_fast16_t) < sizeof (int32_t) ) {\r\n        if ( exp < -0x40 ) exp = -0x40;\r\n    }\r\n    return softfloat_roundPackToF16( sign, exp, frac16 | 0x4000 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_f16.c ****/\n/**** start inlining ../../source/f128M_to_f32.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat32_t f128M_to_f32( const float128_t *aPtr )\r\n{\r\n\r\n    return f128_to_f32( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat32_t f128M_to_f32( const float128_t *aPtr )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t frac64;\r\n    struct commonNaN commonNaN;\r\n    uint32_t uiZ, frac32;\r\n    union ui32_f32 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    frac64 =\r\n        (uint64_t) fracF128UI96( uiA96 )<<32 | aWPtr[indexWord( 4, 2 )]\r\n            | ((aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )]) != 0);\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac64 ) {\r\n            softfloat_f128MToCommonNaN( aWPtr, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF32UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF32UI( sign, 0xFF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac32 = softfloat_shortShiftRightJam64( frac64, 18 );\r\n    if ( ! (exp | frac32) ) {\r\n        uiZ = packToF32UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3F81;\r\n    if ( sizeof (int_fast16_t) < sizeof (int32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return softfloat_roundPackToF32( sign, exp, frac32 | 0x40000000 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_f32.c ****/\n/**** start inlining ../../source/f128M_to_extF80M.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f128M_to_extF80M( const float128_t *aPtr, extFloat80_t *zPtr )\r\n{\r\n\r\n    *zPtr = f128_to_extF80( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f128M_to_extF80M( const float128_t *aPtr, extFloat80_t *zPtr )\r\n{\r\n    const uint32_t *aWPtr;\r\n    struct extFloat80M *zSPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    struct commonNaN commonNaN;\r\n    uint32_t sig[4];\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    zSPtr = (struct extFloat80M *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( softfloat_isNaNF128M( aWPtr ) ) {\r\n            softfloat_f128MToCommonNaN( aWPtr, &commonNaN );\r\n            softfloat_commonNaNToExtF80M( &commonNaN, zSPtr );\r\n            return;\r\n        }\r\n        zSPtr->signExp = packToExtF80UI64( sign, 0x7FFF );\r\n        zSPtr->signif = UINT64_C( 0x8000000000000000 );\r\n        return;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp = softfloat_shiftNormSigF128M( aWPtr, 15, sig );\r\n    if ( exp == -128 ) {\r\n        zSPtr->signExp = packToExtF80UI64( sign, 0 );\r\n        zSPtr->signif = 0;\r\n        return;\r\n    }\r\n    if ( sig[indexWord( 4, 0 )] ) sig[indexWord( 4, 1 )] |= 1;\r\n    softfloat_roundPackMToExtF80M(\r\n        sign, exp, &sig[indexMultiwordHi( 4, 3 )], 80, zSPtr );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_extF80M.c ****/\n/**** start inlining ../../source/f128M_to_f64.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nfloat64_t f128M_to_f64( const float128_t *aPtr )\r\n{\r\n\r\n    return f128_to_f64( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nfloat64_t f128M_to_f64( const float128_t *aPtr )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t uiA96;\r\n    bool sign;\r\n    int32_t exp;\r\n    uint64_t frac64;\r\n    struct commonNaN commonNaN;\r\n    uint64_t uiZ;\r\n    uint32_t frac32;\r\n    union ui64_f64 uZ;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    sign = signF128UI96( uiA96 );\r\n    exp  = expF128UI96( uiA96 );\r\n    frac64 = (uint64_t) fracF128UI96( uiA96 )<<32 | aWPtr[indexWord( 4, 2 )];\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp == 0x7FFF ) {\r\n        if ( frac64 || aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )] ) {\r\n            softfloat_f128MToCommonNaN( aWPtr, &commonNaN );\r\n            uiZ = softfloat_commonNaNToF64UI( &commonNaN );\r\n        } else {\r\n            uiZ = packToF64UI( sign, 0x7FF, 0 );\r\n        }\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    frac32 = aWPtr[indexWord( 4, 1 )];\r\n    frac64 = frac64<<14 | frac32>>18;\r\n    if ( (frac32 & 0x0003FFFF) || aWPtr[indexWord( 4, 0 )] ) frac64 |= 1;\r\n    if ( ! (exp | frac64) ) {\r\n        uiZ = packToF64UI( sign, 0, 0 );\r\n        goto uiZ;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    exp -= 0x3C01;\r\n    if ( sizeof (int_fast16_t) < sizeof (int32_t) ) {\r\n        if ( exp < -0x1000 ) exp = -0x1000;\r\n    }\r\n    return\r\n        softfloat_roundPackToF64(\r\n            sign, exp, frac64 | UINT64_C( 0x4000000000000000 ) );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n uiZ:\r\n    uZ.ui = uiZ;\r\n    return uZ.f;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_to_f64.c ****/\n/**** start inlining ../../source/f128M_roundToInt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_roundToInt(\r\n     const float128_t *aPtr,\r\n     uint_fast8_t roundingMode,\r\n     bool exact,\r\n     float128_t *zPtr\r\n )\r\n{\r\n\r\n    *zPtr = f128_roundToInt( *aPtr, roundingMode, exact );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_roundToInt(\r\n     const float128_t *aPtr,\r\n     uint_fast8_t roundingMode,\r\n     bool exact,\r\n     float128_t *zPtr\r\n )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t *zWPtr;\r\n    uint32_t ui96;\r\n    int32_t exp;\r\n    uint32_t sigExtra;\r\n    bool sign;\r\n    uint_fast8_t bitPos;\r\n    bool roundNear;\r\n    unsigned int index, lastIndex;\r\n    bool extra;\r\n    uint32_t wordA, bit, wordZ;\r\n    uint_fast8_t carry;\r\n    uint32_t extrasMask;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    ui96 = aWPtr[indexWordHi( 4 )];\r\n    exp = expF128UI96( ui96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( exp < 0x3FFF ) {\r\n        zWPtr[indexWord( 4, 2 )] = 0;\r\n        zWPtr[indexWord( 4, 1 )] = 0;\r\n        zWPtr[indexWord( 4, 0 )] = 0;\r\n        sigExtra = aWPtr[indexWord( 4, 2 )];\r\n        if ( !sigExtra ) {\r\n            sigExtra = aWPtr[indexWord( 4, 1 )] | aWPtr[indexWord( 4, 0 )];\r\n        }\r\n        if ( !sigExtra && !(ui96 & 0x7FFFFFFF) ) goto ui96;\r\n        if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        sign = signF128UI96( ui96 );\r\n        switch ( roundingMode ) {\r\n         case softfloat_round_near_even:\r\n            if ( !fracF128UI96( ui96 ) && !sigExtra ) break;\r\n         case softfloat_round_near_maxMag:\r\n            if ( exp == 0x3FFE ) goto mag1;\r\n            break;\r\n         case softfloat_round_min:\r\n            if ( sign ) goto mag1;\r\n            break;\r\n         case softfloat_round_max:\r\n            if ( !sign ) goto mag1;\r\n            break;\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n         case softfloat_round_odd:\r\n            goto mag1;\r\n#endif\r\n        }\r\n        ui96 = packToF128UI96( sign, 0, 0 );\r\n        goto ui96;\r\n     mag1:\r\n        ui96 = packToF128UI96( sign, 0x3FFF, 0 );\r\n        goto ui96;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( 0x406F <= exp ) {\r\n        if (\r\n            (exp == 0x7FFF)\r\n                && (fracF128UI96( ui96 )\r\n                        || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                                | aWPtr[indexWord( 4, 0 )]))\r\n        ) {\r\n            softfloat_propagateNaNF128M( aWPtr, 0, zWPtr );\r\n            return;\r\n        }\r\n        zWPtr[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n        zWPtr[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n        zWPtr[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n        goto ui96;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    bitPos = 0x406F - exp;\r\n    roundNear =\r\n           (roundingMode == softfloat_round_near_maxMag)\r\n        || (roundingMode == softfloat_round_near_even);\r\n    bitPos -= roundNear;\r\n    index = indexWordLo( 4 );\r\n    lastIndex = indexWordHi( 4 );\r\n    extra = 0;\r\n    for (;;) {\r\n        wordA = aWPtr[index];\r\n        if ( bitPos < 32 ) break;\r\n        if ( wordA ) extra = 1;\r\n        zWPtr[index] = 0;\r\n        index += wordIncr;\r\n        bitPos -= 32;\r\n    }\r\n    bit = (uint32_t) 1<<bitPos;\r\n    if ( roundNear ) {\r\n        wordZ = wordA + bit;\r\n        carry = (wordZ < wordA);\r\n        bit <<= 1;\r\n        extrasMask = bit - 1;\r\n        if ( exact && (extra || (wordA & extrasMask)) ) {\r\n            softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n        }\r\n        if (\r\n            (roundingMode == softfloat_round_near_even)\r\n                && !extra && !(wordZ & extrasMask)\r\n        ) {\r\n            if ( !bit ) {\r\n                zWPtr[index] = wordZ;\r\n                index += wordIncr;\r\n                wordZ = aWPtr[index] + carry;\r\n                carry &= !wordZ;\r\n                zWPtr[index] = wordZ & ~1;\r\n                goto propagateCarry;\r\n            }\r\n            wordZ &= ~bit;\r\n        }\r\n    } else {\r\n        wordZ = wordA;\r\n        carry = 0;\r\n        extrasMask = bit - 1;\r\n        if ( extra || (wordA & extrasMask) ) {\r\n            if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;\r\n            if (\r\n                roundingMode\r\n                    == (signF128UI96( ui96 ) ? softfloat_round_min\r\n                            : softfloat_round_max)\r\n            ) {\r\n                wordZ += bit;\r\n                carry = (wordZ < wordA);\r\n#ifdef SOFTFLOAT_ROUND_ODD\r\n            } else if ( roundingMode == softfloat_round_odd ) {\r\n                wordZ |= bit;\r\n#endif\r\n            }\r\n        }\r\n    }\r\n    wordZ &= ~extrasMask;\r\n    zWPtr[index] = wordZ;\r\n propagateCarry:\r\n    while ( index != lastIndex ) {\r\n        index += wordIncr;\r\n        wordZ = aWPtr[index] + carry;\r\n        zWPtr[index] = wordZ;\r\n        carry &= !wordZ;\r\n    }\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n ui96:\r\n    zWPtr[indexWordHi( 4 )] = ui96;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_roundToInt.c ****/\n/**** start inlining ../../source/f128M_add.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_add( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n    const uint64_t *aWPtr, *bWPtr;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    float128_t\r\n        (*magsFuncPtr)(\r\n            uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    aWPtr = (const uint64_t *) aPtr;\r\n    bWPtr = (const uint64_t *) bPtr;\r\n    uiA64 = aWPtr[indexWord( 2, 1 )];\r\n    uiA0  = aWPtr[indexWord( 2, 0 )];\r\n    signA = signF128UI64( uiA64 );\r\n    uiB64 = bWPtr[indexWord( 2, 1 )];\r\n    uiB0  = bWPtr[indexWord( 2, 0 )];\r\n    signB = signF128UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        *zPtr = softfloat_addMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        *zPtr = softfloat_subMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_addMagsF128 : softfloat_subMagsF128;\r\n    *zPtr = (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_add( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n\r\n    softfloat_addF128M(\r\n        (const uint32_t *) aPtr,\r\n        (const uint32_t *) bPtr,\r\n        (uint32_t *) zPtr,\r\n        false\r\n    );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_add.c ****/\n/**** start inlining ../../source/f128M_sub.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_sub( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n    const uint64_t *aWPtr, *bWPtr;\r\n    uint_fast64_t uiA64, uiA0;\r\n    bool signA;\r\n    uint_fast64_t uiB64, uiB0;\r\n    bool signB;\r\n#if ! defined INLINE_LEVEL || (INLINE_LEVEL < 2)\r\n    float128_t\r\n        (*magsFuncPtr)(\r\n            uint_fast64_t, uint_fast64_t, uint_fast64_t, uint_fast64_t, bool );\r\n#endif\r\n\r\n    aWPtr = (const uint64_t *) aPtr;\r\n    bWPtr = (const uint64_t *) bPtr;\r\n    uiA64 = aWPtr[indexWord( 2, 1 )];\r\n    uiA0  = aWPtr[indexWord( 2, 0 )];\r\n    signA = signF128UI64( uiA64 );\r\n    uiB64 = bWPtr[indexWord( 2, 1 )];\r\n    uiB0  = bWPtr[indexWord( 2, 0 )];\r\n    signB = signF128UI64( uiB64 );\r\n#if defined INLINE_LEVEL && (2 <= INLINE_LEVEL)\r\n    if ( signA == signB ) {\r\n        *zPtr = softfloat_subMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    } else {\r\n        *zPtr = softfloat_addMagsF128( uiA64, uiA0, uiB64, uiB0, signA );\r\n    }\r\n#else\r\n    magsFuncPtr =\r\n        (signA == signB) ? softfloat_subMagsF128 : softfloat_addMagsF128;\r\n    *zPtr = (*magsFuncPtr)( uiA64, uiA0, uiB64, uiB0, signA );\r\n#endif\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_sub( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n\r\n    softfloat_addF128M(\r\n        (const uint32_t *) aPtr,\r\n        (const uint32_t *) bPtr,\r\n        (uint32_t *) zPtr,\r\n        true\r\n    );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_sub.c ****/\n/**** start inlining ../../source/f128M_mul.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_mul( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f128_mul( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_mul( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t *zWPtr;\r\n    uint32_t uiA96;\r\n    int32_t expA;\r\n    uint32_t uiB96;\r\n    int32_t expB;\r\n    bool signZ;\r\n    const uint32_t *ptr;\r\n    uint32_t uiZ96, sigA[4];\r\n    uint_fast8_t shiftDist;\r\n    uint32_t sigB[4];\r\n    int32_t expZ;\r\n    uint32_t sigProd[8], *extSigZPtr;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    expA = expF128UI96( uiA96 );\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    expB = expF128UI96( uiB96 );\r\n    signZ = signF128UI96( uiA96 ) ^ signF128UI96( uiB96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {\r\n        if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;\r\n        ptr = aWPtr;\r\n        if ( ! expA ) goto possiblyInvalid;\r\n        if ( ! expB ) {\r\n            ptr = bWPtr;\r\n     possiblyInvalid:\r\n            if (\r\n                ! fracF128UI96( ptr[indexWordHi( 4 )] )\r\n                    && ! (ptr[indexWord( 4, 2 )] | ptr[indexWord( 4, 1 )]\r\n                              | ptr[indexWord( 4, 0 )])\r\n            ) {\r\n                softfloat_invalidF128M( zWPtr );\r\n                return;\r\n            }\r\n        }\r\n        uiZ96 = packToF128UI96( signZ, 0x7FFF, 0 );\r\n        goto uiZ96;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA ) {\r\n        sigA[indexWordHi( 4 )] = fracF128UI96( uiA96 ) | 0x00010000;\r\n        sigA[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n        sigA[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n        sigA[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n    } else {\r\n        expA = softfloat_shiftNormSigF128M( aWPtr, 0, sigA );\r\n        if ( expA == -128 ) goto zero;\r\n    }\r\n    if ( expB ) {\r\n        sigB[indexWordHi( 4 )] = fracF128UI96( uiB96 ) | 0x00010000;\r\n        sigB[indexWord( 4, 2 )] = bWPtr[indexWord( 4, 2 )];\r\n        sigB[indexWord( 4, 1 )] = bWPtr[indexWord( 4, 1 )];\r\n        sigB[indexWord( 4, 0 )] = bWPtr[indexWord( 4, 0 )];\r\n    } else {\r\n        expB = softfloat_shiftNormSigF128M( bWPtr, 0, sigB );\r\n        if ( expB == -128 ) goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA + expB - 0x4000;\r\n    softfloat_mul128MTo256M( sigA, sigB, sigProd );\r\n    if (\r\n        sigProd[indexWord( 8, 2 )]\r\n            || (sigProd[indexWord( 8, 1 )] | sigProd[indexWord( 8, 0 )])\r\n    ) {\r\n        sigProd[indexWord( 8, 3 )] |= 1;\r\n    }\r\n    extSigZPtr = &sigProd[indexMultiwordHi( 8, 5 )];\r\n    shiftDist = 16;\r\n    if ( extSigZPtr[indexWordHi( 5 )] & 2 ) {\r\n        ++expZ;\r\n        shiftDist = 15;\r\n    }\r\n    softfloat_shortShiftLeft160M( extSigZPtr, shiftDist, extSigZPtr );\r\n    softfloat_roundPackMToF128M( signZ, expZ, extSigZPtr, zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n zero:\r\n    uiZ96 = packToF128UI96( signZ, 0, 0 );\r\n uiZ96:\r\n    zWPtr[indexWordHi( 4 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_mul.c ****/\n/**** start inlining ../../source/f128M_mulAdd.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_mulAdd(\r\n     const float128_t *aPtr,\r\n     const float128_t *bPtr,\r\n     const float128_t *cPtr,\r\n     float128_t *zPtr\r\n )\r\n{\r\n    const uint64_t *aWPtr, *bWPtr, *cWPtr;\r\n    uint_fast64_t uiA64, uiA0;\r\n    uint_fast64_t uiB64, uiB0;\r\n    uint_fast64_t uiC64, uiC0;\r\n\r\n    aWPtr = (const uint64_t *) aPtr;\r\n    bWPtr = (const uint64_t *) bPtr;\r\n    cWPtr = (const uint64_t *) cPtr;\r\n    uiA64 = aWPtr[indexWord( 2, 1 )];\r\n    uiA0  = aWPtr[indexWord( 2, 0 )];\r\n    uiB64 = bWPtr[indexWord( 2, 1 )];\r\n    uiB0  = bWPtr[indexWord( 2, 0 )];\r\n    uiC64 = cWPtr[indexWord( 2, 1 )];\r\n    uiC0  = cWPtr[indexWord( 2, 0 )];\r\n    *zPtr = softfloat_mulAddF128( uiA64, uiA0, uiB64, uiB0, uiC64, uiC0, 0 );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_mulAdd(\r\n     const float128_t *aPtr,\r\n     const float128_t *bPtr,\r\n     const float128_t *cPtr,\r\n     float128_t *zPtr\r\n )\r\n{\r\n\r\n    softfloat_mulAddF128M(\r\n        (const uint32_t *) aPtr,\r\n        (const uint32_t *) bPtr,\r\n        (const uint32_t *) cPtr,\r\n        (uint32_t *) zPtr,\r\n        0\r\n    );\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_mulAdd.c ****/\n/**** start inlining ../../source/f128M_div.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_div( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f128_div( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_div( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t *zWPtr, uiA96;\r\n    bool signA;\r\n    int32_t expA;\r\n    uint32_t uiB96;\r\n    bool signB;\r\n    int32_t expB;\r\n    bool signZ;\r\n    uint32_t y[5], sigB[4];\r\n    int32_t expZ;\r\n    uint32_t recip32;\r\n    int ix;\r\n    uint64_t q64;\r\n    uint32_t q, qs[3], uiZ96;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    signA = signF128UI96( uiA96 );\r\n    expA  = expF128UI96( uiA96 );\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    signB = signF128UI96( uiB96 );\r\n    expB  = expF128UI96( uiB96 );\r\n    signZ = signA ^ signB;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {\r\n        if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;\r\n        if ( expA == 0x7FFF ) {\r\n            if ( expB == 0x7FFF ) goto invalid;\r\n            goto infinity;\r\n        }\r\n        goto zero;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = softfloat_shiftNormSigF128M( aWPtr, 13, y );\r\n    expB = softfloat_shiftNormSigF128M( bWPtr, 13, sigB );\r\n    if ( expA == -128 ) {\r\n        if ( expB == -128 ) goto invalid;\r\n        goto zero;\r\n    }\r\n    if ( expB == -128 ) {\r\n        softfloat_raiseFlags( softfloat_flag_infinite );\r\n        goto infinity;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expZ = expA - expB + 0x3FFE;\r\n    if ( softfloat_compare128M( y, sigB ) < 0 ) {\r\n        --expZ;\r\n        softfloat_add128M( y, y, y );\r\n    }\r\n    recip32 =\r\n        softfloat_approxRecip32_1(\r\n            ((uint64_t) sigB[indexWord( 4, 3 )]<<32 | sigB[indexWord( 4, 2 )])\r\n                >>30\r\n        );\r\n    ix = 3;\r\n    for (;;) {\r\n        q64 = (uint64_t) y[indexWordHi( 4 )] * recip32;\r\n        q = (q64 + 0x80000000)>>32;\r\n        --ix;\r\n        if ( ix < 0 ) break;\r\n        softfloat_remStep128MBy32( y, 29, sigB, q, y );\r\n        if ( y[indexWordHi( 4 )] & 0x80000000 ) {\r\n            --q;\r\n            softfloat_add128M( y, sigB, y );\r\n        }\r\n        qs[ix] = q;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( ((q + 1) & 7) < 2 ) {\r\n        softfloat_remStep128MBy32( y, 29, sigB, q, y );\r\n        if ( y[indexWordHi( 4 )] & 0x80000000 ) {\r\n            --q;\r\n            softfloat_add128M( y, sigB, y );\r\n        } else if ( softfloat_compare128M( sigB, y ) <= 0 ) {\r\n            ++q;\r\n            softfloat_sub128M( y, sigB, y );\r\n        }\r\n        if (\r\n            y[indexWordLo( 4 )] || y[indexWord( 4, 1 )]\r\n                || (y[indexWord( 4, 2 )] | y[indexWord( 4, 3 )])\r\n        ) {\r\n            q |= 1;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q64 = (uint64_t) q<<28;\r\n    y[indexWord( 5, 0 )] = q64;\r\n    q64 = ((uint64_t) qs[0]<<25) + (q64>>32);\r\n    y[indexWord( 5, 1 )] = q64;\r\n    q64 = ((uint64_t) qs[1]<<22) + (q64>>32);\r\n    y[indexWord( 5, 2 )] = q64;\r\n    q64 = ((uint64_t) qs[2]<<19) + (q64>>32);\r\n    y[indexWord( 5, 3 )] = q64;\r\n    y[indexWord( 5, 4 )] = q64>>32;\r\n    softfloat_roundPackMToF128M( signZ, expZ, y, zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_invalidF128M( zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n infinity:\r\n    uiZ96 = packToF128UI96( signZ, 0x7FFF, 0 );\r\n    goto uiZ96;\r\n zero:\r\n    uiZ96 = packToF128UI96( signZ, 0, 0 );\r\n uiZ96:\r\n    zWPtr[indexWordHi( 4 )] = uiZ96;\r\n    zWPtr[indexWord( 4, 2 )] = 0;\r\n    zWPtr[indexWord( 4, 1 )] = 0;\r\n    zWPtr[indexWord( 4, 0 )] = 0;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_div.c ****/\n/**** start inlining ../../source/f128M_rem.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid\r\n f128M_rem( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f128_rem( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid\r\n f128M_rem( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t *zWPtr, uiA96;\r\n    int32_t expA, expB;\r\n    uint32_t x[4], rem1[5], *remPtr;\r\n    bool signRem;\r\n    int32_t expDiff;\r\n    uint32_t q, recip32;\r\n    uint64_t q64;\r\n    uint32_t rem2[5], *altRemPtr, *newRemPtr, wordMeanRem;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    expA = expF128UI96( uiA96 );\r\n    expB = expF128UI96( bWPtr[indexWordHi( 4 )] );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {\r\n        if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;\r\n        if ( expA == 0x7FFF ) goto invalid;\r\n        goto copyA;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( expA < expB - 1 ) goto copyA;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expB = softfloat_shiftNormSigF128M( bWPtr, 13, x );\r\n    if ( expB == -128 ) goto invalid;\r\n    remPtr = &rem1[indexMultiwordLo( 5, 4 )];\r\n    expA = softfloat_shiftNormSigF128M( aWPtr, 13, remPtr );\r\n    if ( expA == -128 ) goto copyA;\r\n    signRem = signF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expDiff = expA - expB;\r\n    if ( expDiff < 1 ) {\r\n        if ( expDiff < -1 ) goto copyA;\r\n        if ( expDiff ) {\r\n            --expB;\r\n            softfloat_add128M( x, x, x );\r\n            q = 0;\r\n        } else {\r\n            q = (softfloat_compare128M( x, remPtr ) <= 0);\r\n            if ( q ) softfloat_sub128M( remPtr, x, remPtr );\r\n        }\r\n    } else {\r\n        recip32 =\r\n            softfloat_approxRecip32_1(\r\n                ((uint64_t) x[indexWord( 4, 3 )]<<32 | x[indexWord( 4, 2 )])\r\n                    >>30\r\n            );\r\n        expDiff -= 30;\r\n        for (;;) {\r\n            q64 = (uint64_t) remPtr[indexWordHi( 4 )] * recip32;\r\n            if ( expDiff < 0 ) break;\r\n            q = (q64 + 0x80000000)>>32;\r\n            softfloat_remStep128MBy32( remPtr, 29, x, q, remPtr );\r\n            if ( remPtr[indexWordHi( 4 )] & 0x80000000 ) {\r\n                softfloat_add128M( remPtr, x, remPtr );\r\n            }\r\n            expDiff -= 29;\r\n        }\r\n        /*--------------------------------------------------------------------\r\n        | (`expDiff' cannot be less than -29 here.)\r\n        *--------------------------------------------------------------------*/\r\n        q = (uint32_t) (q64>>32)>>(~expDiff & 31);\r\n        softfloat_remStep128MBy32( remPtr, expDiff + 30, x, q, remPtr );\r\n        if ( remPtr[indexWordHi( 4 )] & 0x80000000 ) {\r\n            altRemPtr = &rem2[indexMultiwordLo( 5, 4 )];\r\n            softfloat_add128M( remPtr, x, altRemPtr );\r\n            goto selectRem;\r\n        }\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    altRemPtr = &rem2[indexMultiwordLo( 5, 4 )];\r\n    do {\r\n        ++q;\r\n        newRemPtr = altRemPtr;\r\n        softfloat_sub128M( remPtr, x, newRemPtr );\r\n        altRemPtr = remPtr;\r\n        remPtr = newRemPtr;\r\n    } while ( ! (remPtr[indexWordHi( 4 )] & 0x80000000) );\r\n selectRem:\r\n    softfloat_add128M( remPtr, altRemPtr, x );\r\n    wordMeanRem = x[indexWordHi( 4 )];\r\n    if (\r\n        (wordMeanRem & 0x80000000)\r\n            || (! wordMeanRem && (q & 1) && ! x[indexWord( 4, 0 )]\r\n                    && ! (x[indexWord( 4, 2 )] | x[indexWord( 4, 1 )]))\r\n    ) {\r\n        remPtr = altRemPtr;\r\n    }\r\n    if ( remPtr[indexWordHi( 4 )] & 0x80000000 ) {\r\n        signRem = ! signRem;\r\n        softfloat_negX128M( remPtr );\r\n    }\r\n    remPtr -= indexMultiwordLo( 5, 4 );\r\n    remPtr[indexWordHi( 5 )] = 0;\r\n    softfloat_normRoundPackMToF128M( signRem, expB + 18, remPtr, zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_invalidF128M( zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n copyA:\r\n    zWPtr[indexWordHi( 4 )] = uiA96;\r\n    zWPtr[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n    zWPtr[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n    zWPtr[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_rem.c ****/\n/**** start inlining ../../source/f128M_sqrt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014, 2017 The Regents of the University of\r\nCalifornia.  All rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nvoid f128M_sqrt( const float128_t *aPtr, float128_t *zPtr )\r\n{\r\n\r\n    *zPtr = f128_sqrt( *aPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nvoid f128M_sqrt( const float128_t *aPtr, float128_t *zPtr )\r\n{\r\n    const uint32_t *aWPtr;\r\n    uint32_t *zWPtr;\r\n    uint32_t uiA96;\r\n    bool signA;\r\n    int32_t rawExpA;\r\n    uint32_t rem[6];\r\n    int32_t expA, expZ;\r\n    uint64_t rem64;\r\n    uint32_t sig32A, recipSqrt32, sig32Z, qs[3], q;\r\n    uint64_t sig64Z;\r\n    uint32_t term[5];\r\n    uint64_t x64;\r\n    uint32_t y[5], rem32;\r\n\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    zWPtr = (uint32_t *) zPtr;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    signA = signF128UI96( uiA96 );\r\n    rawExpA  = expF128UI96( uiA96 );\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( rawExpA == 0x7FFF ) {\r\n        if (\r\n            fracF128UI96( uiA96 )\r\n                || (aWPtr[indexWord( 4, 2 )] | aWPtr[indexWord( 4, 1 )]\r\n                        | aWPtr[indexWord( 4, 0 )])\r\n        ) {\r\n            softfloat_propagateNaNF128M( aWPtr, 0, zWPtr );\r\n            return;\r\n        }\r\n        if ( ! signA ) goto copyA;\r\n        goto invalid;\r\n    }\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    expA = softfloat_shiftNormSigF128M( aWPtr, 13 - (rawExpA & 1), rem );\r\n    if ( expA == -128 ) goto copyA;\r\n    if ( signA ) goto invalid;\r\n    /*------------------------------------------------------------------------\r\n    | (`sig32Z' is guaranteed to be a lower bound on the square root of\r\n    | `sig32A', which makes `sig32Z' also a lower bound on the square root of\r\n    | `sigA'.)\r\n    *------------------------------------------------------------------------*/\r\n    expZ = ((expA - 0x3FFF)>>1) + 0x3FFE;\r\n    expA &= 1;\r\n    rem64 = (uint64_t) rem[indexWord( 4, 3 )]<<32 | rem[indexWord( 4, 2 )];\r\n    if ( expA ) {\r\n        if ( ! rawExpA ) {\r\n            softfloat_shortShiftRight128M( rem, 1, rem );\r\n            rem64 >>= 1;\r\n        }\r\n        sig32A = rem64>>29;\r\n    } else {\r\n        sig32A = rem64>>30;\r\n    }\r\n    recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );\r\n    sig32Z = ((uint64_t) sig32A * recipSqrt32)>>32;\r\n    if ( expA ) sig32Z >>= 1;\r\n    qs[2] = sig32Z;\r\n    rem64 -= (uint64_t) sig32Z * sig32Z;\r\n    rem[indexWord( 4, 3 )] = rem64>>32;\r\n    rem[indexWord( 4, 2 )] = rem64;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = ((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32;\r\n    sig64Z = ((uint64_t) sig32Z<<32) + ((uint64_t) q<<3);\r\n    term[indexWord( 4, 3 )] = 0;\r\n    term[indexWord( 4, 0 )] = 0;\r\n    /*------------------------------------------------------------------------\r\n    | (Repeating this loop is a rare occurrence.)\r\n    *------------------------------------------------------------------------*/\r\n    for (;;) {\r\n        x64 = ((uint64_t) sig32Z<<32) + sig64Z;\r\n        term[indexWord( 4, 2 )] = x64>>32;\r\n        term[indexWord( 4, 1 )] = x64;\r\n        softfloat_remStep128MBy32( rem, 29, term, q, y );\r\n        rem32 = y[indexWord( 4, 3 )];\r\n        if ( ! (rem32 & 0x80000000) ) break;\r\n        --q;\r\n        sig64Z -= 1<<3;\r\n    }\r\n    qs[1] = q;\r\n    rem64 = (uint64_t) rem32<<32 | y[indexWord( 4, 2 )];\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = ((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32;\r\n    if ( rem64>>34 ) q += recipSqrt32;\r\n    sig64Z <<= 1;\r\n    /*------------------------------------------------------------------------\r\n    | (Repeating this loop is a rare occurrence.)\r\n    *------------------------------------------------------------------------*/\r\n    for (;;) {\r\n        x64 = sig64Z + (q>>26);\r\n        term[indexWord( 4, 2 )] = x64>>32;\r\n        term[indexWord( 4, 1 )] = x64;\r\n        term[indexWord( 4, 0 )] = q<<6;\r\n        softfloat_remStep128MBy32(\r\n            y, 29, term, q, &rem[indexMultiwordHi( 6, 4 )] );\r\n        rem32 = rem[indexWordHi( 6 )];\r\n        if ( ! (rem32 & 0x80000000) ) break;\r\n        --q;\r\n    }\r\n    qs[0] = q;\r\n    rem64 = (uint64_t) rem32<<32 | rem[indexWord( 6, 4 )];\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    q = (((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32) + 2;\r\n    if ( rem64>>34 ) q += recipSqrt32;\r\n    x64 = (uint64_t) q<<27;\r\n    y[indexWord( 5, 0 )] = x64;\r\n    x64 = ((uint64_t) qs[0]<<24) + (x64>>32);\r\n    y[indexWord( 5, 1 )] = x64;\r\n    x64 = ((uint64_t) qs[1]<<21) + (x64>>32);\r\n    y[indexWord( 5, 2 )] = x64;\r\n    x64 = ((uint64_t) qs[2]<<18) + (x64>>32);\r\n    y[indexWord( 5, 3 )] = x64;\r\n    y[indexWord( 5, 4 )] = x64>>32;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n    if ( (q & 0xF) <= 2 ) {\r\n        q &= ~3;\r\n        y[indexWordLo( 5 )] = q<<27;\r\n        term[indexWord( 5, 4 )] = 0;\r\n        term[indexWord( 5, 3 )] = 0;\r\n        term[indexWord( 5, 2 )] = 0;\r\n        term[indexWord( 5, 1 )] = q>>6;\r\n        term[indexWord( 5, 0 )] = q<<26;\r\n        softfloat_sub160M( y, term, term );\r\n        rem[indexWord( 6, 1 )] = 0;\r\n        rem[indexWord( 6, 0 )] = 0;\r\n        softfloat_remStep160MBy32(\r\n            &rem[indexMultiwordLo( 6, 5 )],\r\n            14,\r\n            term,\r\n            q,\r\n            &rem[indexMultiwordLo( 6, 5 )]\r\n        );\r\n        rem32 = rem[indexWord( 6, 4 )];\r\n        if ( rem32 & 0x80000000 ) {\r\n            softfloat_sub1X160M( y );\r\n        } else {\r\n            if (\r\n                rem32 || rem[indexWord( 6, 0 )] || rem[indexWord( 6, 1 )]\r\n                    || (rem[indexWord( 6, 3 )] | rem[indexWord( 6, 2 )])\r\n            ) {\r\n                y[indexWordLo( 5 )] |= 1;\r\n            }\r\n        }\r\n    }\r\n    softfloat_roundPackMToF128M( 0, expZ, y, zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n invalid:\r\n    softfloat_invalidF128M( zWPtr );\r\n    return;\r\n    /*------------------------------------------------------------------------\r\n    *------------------------------------------------------------------------*/\r\n copyA:\r\n    zWPtr[indexWordHi( 4 )] = uiA96;\r\n    zWPtr[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];\r\n    zWPtr[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];\r\n    zWPtr[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_sqrt.c ****/\n/**** start inlining ../../source/f128M_eq.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool f128M_eq( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n\r\n    return f128_eq( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool f128M_eq( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t wordA, wordB, uiA96, uiB96;\r\n    bool possibleOppositeZeros;\r\n    uint32_t mashWord;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    wordA = aWPtr[indexWord( 4, 2 )];\r\n    wordB = bWPtr[indexWord( 4, 2 )];\r\n    if ( wordA != wordB ) goto false_checkSigNaNs;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    possibleOppositeZeros = false;\r\n    if ( uiA96 != uiB96 ) {\r\n        possibleOppositeZeros = (((uiA96 | uiB96) & 0x7FFFFFFF) == 0);\r\n        if ( ! possibleOppositeZeros ) goto false_checkSigNaNs;\r\n    }\r\n    mashWord = wordA | wordB;\r\n    wordA = aWPtr[indexWord( 4, 1 )];\r\n    wordB = bWPtr[indexWord( 4, 1 )];\r\n    if ( wordA != wordB ) goto false_checkSigNaNs;\r\n    mashWord |= wordA | wordB;\r\n    wordA = aWPtr[indexWord( 4, 0 )];\r\n    wordB = bWPtr[indexWord( 4, 0 )];\r\n    if ( wordA != wordB ) goto false_checkSigNaNs;\r\n    if ( possibleOppositeZeros && ((mashWord | wordA | wordB) != 0) ) {\r\n        goto false_checkSigNaNs;\r\n    }\r\n    if ( ! softfloat_isNaNF128M( aWPtr ) && ! softfloat_isNaNF128M( bWPtr ) ) {\r\n        return true;\r\n    }\r\n false_checkSigNaNs:\r\n    if (\r\n           f128M_isSignalingNaN( (const float128_t *) aWPtr )\r\n        || f128M_isSignalingNaN( (const float128_t *) bWPtr )\r\n    ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n    }\r\n    return false;\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_eq.c ****/\n/**** start inlining ../../source/f128M_le.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool f128M_le( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n\r\n    return f128_le( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool f128M_le( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t uiA96, uiB96;\r\n    bool signA, signB;\r\n    uint32_t wordA, wordB;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    if ( softfloat_isNaNF128M( aWPtr ) || softfloat_isNaNF128M( bWPtr ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    signA = signF128UI96( uiA96 );\r\n    signB = signF128UI96( uiB96 );\r\n    if ( signA != signB ) {\r\n        if ( signA ) return true;\r\n        if ( (uiA96 | uiB96) & 0x7FFFFFFF ) return false;\r\n        wordA = aWPtr[indexWord( 4, 2 )];\r\n        wordB = bWPtr[indexWord( 4, 2 )];\r\n        if ( wordA | wordB ) return false;\r\n        wordA = aWPtr[indexWord( 4, 1 )];\r\n        wordB = bWPtr[indexWord( 4, 1 )];\r\n        if ( wordA | wordB ) return false;\r\n        wordA = aWPtr[indexWord( 4, 0 )];\r\n        wordB = bWPtr[indexWord( 4, 0 )];\r\n        return ((wordA | wordB) == 0);\r\n    }\r\n    if ( signA ) {\r\n        aWPtr = (const uint32_t *) bPtr;\r\n        bWPtr = (const uint32_t *) aPtr;\r\n    }\r\n    return (softfloat_compare128M( aWPtr, bWPtr ) <= 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_le.c ****/\n/**** start inlining ../../source/f128M_lt.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool f128M_lt( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n\r\n    return f128_lt( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool f128M_lt( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t uiA96, uiB96;\r\n    bool signA, signB;\r\n    uint32_t wordA, wordB;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    if ( softfloat_isNaNF128M( aWPtr ) || softfloat_isNaNF128M( bWPtr ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    signA = signF128UI96( uiA96 );\r\n    signB = signF128UI96( uiB96 );\r\n    if ( signA != signB ) {\r\n        if ( signB ) return false;\r\n        if ( (uiA96 | uiB96) & 0x7FFFFFFF ) return true;\r\n        wordA = aWPtr[indexWord( 4, 2 )];\r\n        wordB = bWPtr[indexWord( 4, 2 )];\r\n        if ( wordA | wordB ) return true;\r\n        wordA = aWPtr[indexWord( 4, 1 )];\r\n        wordB = bWPtr[indexWord( 4, 1 )];\r\n        if ( wordA | wordB ) return true;\r\n        wordA = aWPtr[indexWord( 4, 0 )];\r\n        wordB = bWPtr[indexWord( 4, 0 )];\r\n        return ((wordA | wordB) != 0);\r\n    }\r\n    if ( signA ) {\r\n        aWPtr = (const uint32_t *) bPtr;\r\n        bWPtr = (const uint32_t *) aPtr;\r\n    }\r\n    return (softfloat_compare128M( aWPtr, bWPtr ) < 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_lt.c ****/\n/**** start inlining ../../source/f128M_eq_signaling.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool f128M_eq_signaling( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n\r\n    return f128_eq_signaling( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool f128M_eq_signaling( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t wordA, wordB, uiA96, uiB96;\r\n    bool possibleOppositeZeros;\r\n    uint32_t mashWord;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    if ( softfloat_isNaNF128M( aWPtr ) || softfloat_isNaNF128M( bWPtr ) ) {\r\n        softfloat_raiseFlags( softfloat_flag_invalid );\r\n        return false;\r\n    }\r\n    wordA = aWPtr[indexWord( 4, 2 )];\r\n    wordB = bWPtr[indexWord( 4, 2 )];\r\n    if ( wordA != wordB ) return false;\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    possibleOppositeZeros = false;\r\n    if ( uiA96 != uiB96 ) {\r\n        possibleOppositeZeros = (((uiA96 | uiB96) & 0x7FFFFFFF) == 0);\r\n        if ( ! possibleOppositeZeros ) return false;\r\n    }\r\n    mashWord = wordA | wordB;\r\n    wordA = aWPtr[indexWord( 4, 1 )];\r\n    wordB = bWPtr[indexWord( 4, 1 )];\r\n    if ( wordA != wordB ) return false;\r\n    mashWord |= wordA | wordB;\r\n    wordA = aWPtr[indexWord( 4, 0 )];\r\n    wordB = bWPtr[indexWord( 4, 0 )];\r\n    return\r\n        (wordA == wordB)\r\n            && (! possibleOppositeZeros || ((mashWord | wordA | wordB) == 0));\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_eq_signaling.c ****/\n/**** start inlining ../../source/f128M_le_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool f128M_le_quiet( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n\r\n    return f128_le_quiet( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool f128M_le_quiet( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t uiA96, uiB96;\r\n    bool signA, signB;\r\n    uint32_t wordA, wordB;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    if ( softfloat_isNaNF128M( aWPtr ) || softfloat_isNaNF128M( bWPtr ) ) {\r\n        if ( f128M_isSignalingNaN( aPtr ) || f128M_isSignalingNaN( bPtr ) ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    signA = signF128UI96( uiA96 );\r\n    signB = signF128UI96( uiB96 );\r\n    if ( signA != signB ) {\r\n        if ( signA ) return true;\r\n        if ( (uiA96 | uiB96) & 0x7FFFFFFF ) return false;\r\n        wordA = aWPtr[indexWord( 4, 2 )];\r\n        wordB = bWPtr[indexWord( 4, 2 )];\r\n        if ( wordA | wordB ) return false;\r\n        wordA = aWPtr[indexWord( 4, 1 )];\r\n        wordB = bWPtr[indexWord( 4, 1 )];\r\n        if ( wordA | wordB ) return false;\r\n        wordA = aWPtr[indexWord( 4, 0 )];\r\n        wordB = bWPtr[indexWord( 4, 0 )];\r\n        return ((wordA | wordB) == 0);\r\n    }\r\n    if ( signA ) {\r\n        aWPtr = (const uint32_t *) bPtr;\r\n        bWPtr = (const uint32_t *) aPtr;\r\n    }\r\n    return (softfloat_compare128M( aWPtr, bWPtr ) <= 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_le_quiet.c ****/\n/**** start inlining ../../source/f128M_lt_quiet.c ****/\n\r\n/*============================================================================\r\n\r\nThis C source file is part of the SoftFloat IEEE Floating-Point Arithmetic\r\nPackage, Release 3e, by John R. Hauser.\r\n\r\nCopyright 2011, 2012, 2013, 2014 The Regents of the University of California.\r\nAll rights reserved.\r\n\r\nRedistribution and use in source and binary forms, with or without\r\nmodification, are permitted provided that the following conditions are met:\r\n\r\n 1. Redistributions of source code must retain the above copyright notice,\r\n    this list of conditions, and the following disclaimer.\r\n\r\n 2. Redistributions in binary form must reproduce the above copyright notice,\r\n    this list of conditions, and the following disclaimer in the documentation\r\n    and/or other materials provided with the distribution.\r\n\r\n 3. Neither the name of the University nor the names of its contributors may\r\n    be used to endorse or promote products derived from this software without\r\n    specific prior written permission.\r\n\r\nTHIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS \"AS IS\", AND ANY\r\nEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE\r\nDISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r\nDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n\r\n=============================================================================*/\r\n\r\n#include <stdbool.h>\r\n#include <stdint.h>\r\n/**** skipping file: platform.h ****/\n/**** skipping file: internals.h ****/\n/**** skipping file: specialize.h ****/\n/**** skipping file: softfloat.h ****/\n\r\n#ifdef SOFTFLOAT_FAST_INT64\r\n\r\nbool f128M_lt_quiet( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n\r\n    return f128_lt_quiet( *aPtr, *bPtr );\r\n\r\n}\r\n\r\n#else\r\n\r\nbool f128M_lt_quiet( const float128_t *aPtr, const float128_t *bPtr )\r\n{\r\n    const uint32_t *aWPtr, *bWPtr;\r\n    uint32_t uiA96, uiB96;\r\n    bool signA, signB;\r\n    uint32_t wordA, wordB;\r\n\r\n    aWPtr = (const uint32_t *) aPtr;\r\n    bWPtr = (const uint32_t *) bPtr;\r\n    if ( softfloat_isNaNF128M( aWPtr ) || softfloat_isNaNF128M( bWPtr ) ) {\r\n        if ( f128M_isSignalingNaN( aPtr ) || f128M_isSignalingNaN( bPtr ) ) {\r\n            softfloat_raiseFlags( softfloat_flag_invalid );\r\n        }\r\n        return false;\r\n    }\r\n    uiA96 = aWPtr[indexWordHi( 4 )];\r\n    uiB96 = bWPtr[indexWordHi( 4 )];\r\n    signA = signF128UI96( uiA96 );\r\n    signB = signF128UI96( uiB96 );\r\n    if ( signA != signB ) {\r\n        if ( signB ) return false;\r\n        if ( (uiA96 | uiB96) & 0x7FFFFFFF ) return true;\r\n        wordA = aWPtr[indexWord( 4, 2 )];\r\n        wordB = bWPtr[indexWord( 4, 2 )];\r\n        if ( wordA | wordB ) return true;\r\n        wordA = aWPtr[indexWord( 4, 1 )];\r\n        wordB = bWPtr[indexWord( 4, 1 )];\r\n        if ( wordA | wordB ) return true;\r\n        wordA = aWPtr[indexWord( 4, 0 )];\r\n        wordB = bWPtr[indexWord( 4, 0 )];\r\n        return ((wordA | wordB) != 0);\r\n    }\r\n    if ( signA ) {\r\n        aWPtr = (const uint32_t *) bPtr;\r\n        bWPtr = (const uint32_t *) aPtr;\r\n    }\r\n    return (softfloat_compare128M( aWPtr, bWPtr ) < 0);\r\n\r\n}\r\n\r\n#endif\r\n\r\n/**** ended inlining ../../source/f128M_lt_quiet.c ****/\n"
  },
  {
    "path": "lib/zstd/zstddeclib.c",
    "content": "#define malloc v86_malloc\n#define free v86_free\n#include <stddef.h>\nvoid *calloc(size_t nmemb, size_t size);\nvoid *memset(void *s, int c, size_t n);\nvoid *memcpy(void *dest, const void *src, size_t n);\nvoid *memmove(void *dest, const void *src, size_t n);\nvoid *malloc(size_t size);\nvoid free(void *ptr);\n/**\n * \\file zstddeclib.c\n * Single-file Zstandard decompressor.\n *\n * Generate using:\n * \\code\n *\tcombine.sh -r ../../lib -o zstddeclib.c zstddeclib-in.c\n * \\endcode\n */\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n/*\n * Settings to bake for the standalone decompressor.\n *\n * Note: It's important that none of these affects 'zstd.h' (only the\n * implementation files we're amalgamating).\n *\n * Note: MEM_MODULE stops xxhash redefining BYTE, U16, etc., which are also\n * defined in mem.h (breaking C99 compatibility).\n *\n * Note: the undefs for xxHash allow Zstd's implementation to coinside with with\n * standalone xxHash usage (with global defines).\n */\n#define DEBUGLEVEL 0\n#define MEM_MODULE\n#undef  XXH_NAMESPACE\n#define XXH_NAMESPACE ZSTD_\n#undef  XXH_PRIVATE_API\n#define XXH_PRIVATE_API\n#undef  XXH_INLINE_ALL\n#define XXH_INLINE_ALL\n#define ZSTD_LEGACY_SUPPORT 0\n#define ZSTD_LIB_COMPRESSION 0\n#define ZSTD_LIB_DEPRECATED 0\n#define ZSTD_NOBENCH\n#define ZSTD_STRIP_ERROR_STRINGS\n\n/**** start inlining common/debug.c ****/\n/* ******************************************************************\n * debug\n * Part of FSE library\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n * You can contact the author at :\n * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n\n/*\n * This module only hosts one global variable\n * which can be used to dynamically influence the verbosity of traces,\n * such as DEBUGLOG and RAWLOG\n */\n\n/**** start inlining debug.h ****/\n/* ******************************************************************\n * debug\n * Part of FSE library\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n * You can contact the author at :\n * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n\n/*\n * The purpose of this header is to enable debug functions.\n * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time,\n * and DEBUG_STATIC_ASSERT() for compile-time.\n *\n * By default, DEBUGLEVEL==0, which means run-time debug is disabled.\n *\n * Level 1 enables assert() only.\n * Starting level 2, traces can be generated and pushed to stderr.\n * The higher the level, the more verbose the traces.\n *\n * It's possible to dynamically adjust level using variable g_debug_level,\n * which is only declared if DEBUGLEVEL>=2,\n * and is a global variable, not multi-thread protected (use with care)\n */\n\n#ifndef DEBUG_H_12987983217\n#define DEBUG_H_12987983217\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n\n/* static assert is triggered at compile time, leaving no runtime artefact.\n * static assert only works with compile-time constants.\n * Also, this variant can only be used inside a function. */\n#define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])\n\n\n/* DEBUGLEVEL is expected to be defined externally,\n * typically through compiler command line.\n * Value must be a number. */\n#ifndef DEBUGLEVEL\n#  define DEBUGLEVEL 0\n#endif\n\n\n/* DEBUGFILE can be defined externally,\n * typically through compiler command line.\n * note : currently useless.\n * Value must be stderr or stdout */\n#ifndef DEBUGFILE\n#  define DEBUGFILE stderr\n#endif\n\n\n/* recommended values for DEBUGLEVEL :\n * 0 : release mode, no debug, all run-time checks disabled\n * 1 : enables assert() only, no display\n * 2 : reserved, for currently active debug path\n * 3 : events once per object lifetime (CCtx, CDict, etc.)\n * 4 : events once per frame\n * 5 : events once per block\n * 6 : events once per sequence (verbose)\n * 7+: events at every position (*very* verbose)\n *\n * It's generally inconvenient to output traces > 5.\n * In which case, it's possible to selectively trigger high verbosity levels\n * by modifying g_debug_level.\n */\n\n#if (DEBUGLEVEL>=1)\n#  include <assert.h>\n#else\n#  ifndef assert   /* assert may be already defined, due to prior #include <assert.h> */\n#    define assert(condition) ((void)0)   /* disable assert (default) */\n#  endif\n#endif\n\n#if (DEBUGLEVEL>=2)\n#  include <stdio.h>\nextern int g_debuglevel; /* the variable is only declared,\n                            it actually lives in debug.c,\n                            and is shared by the whole process.\n                            It's not thread-safe.\n                            It's useful when enabling very verbose levels\n                            on selective conditions (such as position in src) */\n\n#  define RAWLOG(l, ...) {                                      \\\n                if (l<=g_debuglevel) {                          \\\n                    fprintf(stderr, __VA_ARGS__);               \\\n            }   }\n#  define DEBUGLOG(l, ...) {                                    \\\n                if (l<=g_debuglevel) {                          \\\n                    fprintf(stderr, __FILE__ \": \" __VA_ARGS__); \\\n                    fprintf(stderr, \" \\n\");                     \\\n            }   }\n#else\n#  define RAWLOG(l, ...)      {}    /* disabled */\n#  define DEBUGLOG(l, ...)    {}    /* disabled */\n#endif\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* DEBUG_H_12987983217 */\n/**** ended inlining debug.h ****/\n\nint g_debuglevel = DEBUGLEVEL;\n/**** ended inlining common/debug.c ****/\n/**** start inlining common/entropy_common.c ****/\n/* ******************************************************************\n * Common functions of New Generation Entropy library\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n *\n *  You can contact the author at :\n *  - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *  - Public forum : https://groups.google.com/forum/#!forum/lz4c\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n/* *************************************\n*  Dependencies\n***************************************/\n/**** start inlining mem.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef MEM_H_MODULE\n#define MEM_H_MODULE\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/*-****************************************\n*  Dependencies\n******************************************/\n#include <stddef.h>     /* size_t, ptrdiff_t */\n\n\n/*-****************************************\n*  Compiler specifics\n******************************************/\n#if defined(_MSC_VER)   /* Visual Studio */\n#   include <intrin.h>  /* _byteswap_* */\n#endif\n#if defined(__GNUC__)\n#  define MEM_STATIC static __inline __attribute__((unused))\n#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)\n#  define MEM_STATIC static inline\n#elif defined(_MSC_VER)\n#  define MEM_STATIC static __inline\n#else\n#  define MEM_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */\n#endif\n\n#ifndef __has_builtin\n#  define __has_builtin(x) 0  /* compat. with non-clang compilers */\n#endif\n\n/* code only tested on 32 and 64 bits systems */\n#define MEM_STATIC_ASSERT(c)   { enum { MEM_static_assert = 1/(int)(!!(c)) }; }\nMEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }\n\n/* detects whether we are being compiled under msan */\n#if defined (__has_feature)\n#  if __has_feature(memory_sanitizer)\n#    define MEMORY_SANITIZER 1\n#  endif\n#endif\n\n#if defined (MEMORY_SANITIZER)\n/* Not all platforms that support msan provide sanitizers/msan_interface.h.\n * We therefore declare the functions we need ourselves, rather than trying to\n * include the header file... */\n\n#include <stdint.h> /* intptr_t */\n\n/* Make memory region fully initialized (without changing its contents). */\nvoid __msan_unpoison(const volatile void *a, size_t size);\n\n/* Make memory region fully uninitialized (without changing its contents).\n   This is a legacy interface that does not update origin information. Use\n   __msan_allocated_memory() instead. */\nvoid __msan_poison(const volatile void *a, size_t size);\n\n/* Returns the offset of the first (at least partially) poisoned byte in the\n   memory range, or -1 if the whole range is good. */\nintptr_t __msan_test_shadow(const volatile void *x, size_t size);\n#endif\n\n/* detects whether we are being compiled under asan */\n#if defined (__has_feature)\n#  if __has_feature(address_sanitizer)\n#    define ADDRESS_SANITIZER 1\n#  endif\n#elif defined(__SANITIZE_ADDRESS__)\n#  define ADDRESS_SANITIZER 1\n#endif\n\n#if defined (ADDRESS_SANITIZER)\n/* Not all platforms that support asan provide sanitizers/asan_interface.h.\n * We therefore declare the functions we need ourselves, rather than trying to\n * include the header file... */\n\n/**\n * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.\n *\n * This memory must be previously allocated by your program. Instrumented\n * code is forbidden from accessing addresses in this region until it is\n * unpoisoned. This function is not guaranteed to poison the entire region -\n * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan\n * alignment restrictions.\n *\n * \\note This function is not thread-safe because no two threads can poison or\n * unpoison memory in the same memory region simultaneously.\n *\n * \\param addr Start of memory region.\n * \\param size Size of memory region. */\nvoid __asan_poison_memory_region(void const volatile *addr, size_t size);\n\n/**\n * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.\n *\n * This memory must be previously allocated by your program. Accessing\n * addresses in this region is allowed until this region is poisoned again.\n * This function could unpoison a super-region of <c>[addr, addr+size)</c> due\n * to ASan alignment restrictions.\n *\n * \\note This function is not thread-safe because no two threads can\n * poison or unpoison memory in the same memory region simultaneously.\n *\n * \\param addr Start of memory region.\n * \\param size Size of memory region. */\nvoid __asan_unpoison_memory_region(void const volatile *addr, size_t size);\n#endif\n\n\n/*-**************************************************************\n*  Basic Types\n*****************************************************************/\n#if  !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )\n# include <stdint.h>\n  typedef   uint8_t BYTE;\n  typedef  uint16_t U16;\n  typedef   int16_t S16;\n  typedef  uint32_t U32;\n  typedef   int32_t S32;\n  typedef  uint64_t U64;\n  typedef   int64_t S64;\n#else\n# include <limits.h>\n#if CHAR_BIT != 8\n#  error \"this implementation requires char to be exactly 8-bit type\"\n#endif\n  typedef unsigned char      BYTE;\n#if USHRT_MAX != 65535\n#  error \"this implementation requires short to be exactly 16-bit type\"\n#endif\n  typedef unsigned short      U16;\n  typedef   signed short      S16;\n#if UINT_MAX != 4294967295\n#  error \"this implementation requires int to be exactly 32-bit type\"\n#endif\n  typedef unsigned int        U32;\n  typedef   signed int        S32;\n/* note : there are no limits defined for long long type in C90.\n * limits exist in C99, however, in such case, <stdint.h> is preferred */\n  typedef unsigned long long  U64;\n  typedef   signed long long  S64;\n#endif\n\n\n/*-**************************************************************\n*  Memory I/O\n*****************************************************************/\n/* MEM_FORCE_MEMORY_ACCESS :\n * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.\n * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.\n * The below switch allow to select different access method for improved performance.\n * Method 0 (default) : use `memcpy()`. Safe and portable.\n * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).\n *            This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.\n * Method 2 : direct access. This method is portable but violate C standard.\n *            It can generate buggy code on targets depending on alignment.\n *            In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)\n * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.\n * Prefer these methods in priority order (0 > 1 > 2)\n */\n#ifndef MEM_FORCE_MEMORY_ACCESS   /* can be defined externally, on command line for example */\n#  if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )\n#    define MEM_FORCE_MEMORY_ACCESS 2\n#  elif defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)\n#    define MEM_FORCE_MEMORY_ACCESS 1\n#  endif\n#endif\n\nMEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }\nMEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }\n\nMEM_STATIC unsigned MEM_isLittleEndian(void)\n{\n    const union { U32 u; BYTE c[4]; } one = { 1 };   /* don't use static : performance detrimental  */\n    return one.c[0];\n}\n\n#if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)\n\n/* violates C standard, by lying on structure alignment.\nOnly use if no other choice to achieve best performance on target platform */\nMEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }\nMEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }\nMEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }\nMEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }\n\nMEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }\nMEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }\nMEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }\n\n#elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)\n\n/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */\n/* currently only defined for gcc and icc */\n#if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))\n    __pragma( pack(push, 1) )\n    typedef struct { U16 v; } unalign16;\n    typedef struct { U32 v; } unalign32;\n    typedef struct { U64 v; } unalign64;\n    typedef struct { size_t v; } unalignArch;\n    __pragma( pack(pop) )\n#else\n    typedef struct { U16 v; } __attribute__((packed)) unalign16;\n    typedef struct { U32 v; } __attribute__((packed)) unalign32;\n    typedef struct { U64 v; } __attribute__((packed)) unalign64;\n    typedef struct { size_t v; } __attribute__((packed)) unalignArch;\n#endif\n\nMEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign16*)ptr)->v; }\nMEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign32*)ptr)->v; }\nMEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign64*)ptr)->v; }\nMEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalignArch*)ptr)->v; }\n\nMEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign16*)memPtr)->v = value; }\nMEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign32*)memPtr)->v = value; }\nMEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v = value; }\n\n#else\n\n/* default method, safe and standard.\n   can sometimes prove slower */\n\nMEM_STATIC U16 MEM_read16(const void* memPtr)\n{\n    U16 val; memcpy(&val, memPtr, sizeof(val)); return val;\n}\n\nMEM_STATIC U32 MEM_read32(const void* memPtr)\n{\n    U32 val; memcpy(&val, memPtr, sizeof(val)); return val;\n}\n\nMEM_STATIC U64 MEM_read64(const void* memPtr)\n{\n    U64 val; memcpy(&val, memPtr, sizeof(val)); return val;\n}\n\nMEM_STATIC size_t MEM_readST(const void* memPtr)\n{\n    size_t val; memcpy(&val, memPtr, sizeof(val)); return val;\n}\n\nMEM_STATIC void MEM_write16(void* memPtr, U16 value)\n{\n    memcpy(memPtr, &value, sizeof(value));\n}\n\nMEM_STATIC void MEM_write32(void* memPtr, U32 value)\n{\n    memcpy(memPtr, &value, sizeof(value));\n}\n\nMEM_STATIC void MEM_write64(void* memPtr, U64 value)\n{\n    memcpy(memPtr, &value, sizeof(value));\n}\n\n#endif /* MEM_FORCE_MEMORY_ACCESS */\n\nMEM_STATIC U32 MEM_swap32(U32 in)\n{\n#if defined(_MSC_VER)     /* Visual Studio */\n    return _byteswap_ulong(in);\n#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \\\n  || (defined(__clang__) && __has_builtin(__builtin_bswap32))\n    return __builtin_bswap32(in);\n#else\n    return  ((in << 24) & 0xff000000 ) |\n            ((in <<  8) & 0x00ff0000 ) |\n            ((in >>  8) & 0x0000ff00 ) |\n            ((in >> 24) & 0x000000ff );\n#endif\n}\n\nMEM_STATIC U64 MEM_swap64(U64 in)\n{\n#if defined(_MSC_VER)     /* Visual Studio */\n    return _byteswap_uint64(in);\n#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \\\n  || (defined(__clang__) && __has_builtin(__builtin_bswap64))\n    return __builtin_bswap64(in);\n#else\n    return  ((in << 56) & 0xff00000000000000ULL) |\n            ((in << 40) & 0x00ff000000000000ULL) |\n            ((in << 24) & 0x0000ff0000000000ULL) |\n            ((in << 8)  & 0x000000ff00000000ULL) |\n            ((in >> 8)  & 0x00000000ff000000ULL) |\n            ((in >> 24) & 0x0000000000ff0000ULL) |\n            ((in >> 40) & 0x000000000000ff00ULL) |\n            ((in >> 56) & 0x00000000000000ffULL);\n#endif\n}\n\nMEM_STATIC size_t MEM_swapST(size_t in)\n{\n    if (MEM_32bits())\n        return (size_t)MEM_swap32((U32)in);\n    else\n        return (size_t)MEM_swap64((U64)in);\n}\n\n/*=== Little endian r/w ===*/\n\nMEM_STATIC U16 MEM_readLE16(const void* memPtr)\n{\n    if (MEM_isLittleEndian())\n        return MEM_read16(memPtr);\n    else {\n        const BYTE* p = (const BYTE*)memPtr;\n        return (U16)(p[0] + (p[1]<<8));\n    }\n}\n\nMEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)\n{\n    if (MEM_isLittleEndian()) {\n        MEM_write16(memPtr, val);\n    } else {\n        BYTE* p = (BYTE*)memPtr;\n        p[0] = (BYTE)val;\n        p[1] = (BYTE)(val>>8);\n    }\n}\n\nMEM_STATIC U32 MEM_readLE24(const void* memPtr)\n{\n    return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);\n}\n\nMEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)\n{\n    MEM_writeLE16(memPtr, (U16)val);\n    ((BYTE*)memPtr)[2] = (BYTE)(val>>16);\n}\n\nMEM_STATIC U32 MEM_readLE32(const void* memPtr)\n{\n    if (MEM_isLittleEndian())\n        return MEM_read32(memPtr);\n    else\n        return MEM_swap32(MEM_read32(memPtr));\n}\n\nMEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)\n{\n    if (MEM_isLittleEndian())\n        MEM_write32(memPtr, val32);\n    else\n        MEM_write32(memPtr, MEM_swap32(val32));\n}\n\nMEM_STATIC U64 MEM_readLE64(const void* memPtr)\n{\n    if (MEM_isLittleEndian())\n        return MEM_read64(memPtr);\n    else\n        return MEM_swap64(MEM_read64(memPtr));\n}\n\nMEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)\n{\n    if (MEM_isLittleEndian())\n        MEM_write64(memPtr, val64);\n    else\n        MEM_write64(memPtr, MEM_swap64(val64));\n}\n\nMEM_STATIC size_t MEM_readLEST(const void* memPtr)\n{\n    if (MEM_32bits())\n        return (size_t)MEM_readLE32(memPtr);\n    else\n        return (size_t)MEM_readLE64(memPtr);\n}\n\nMEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)\n{\n    if (MEM_32bits())\n        MEM_writeLE32(memPtr, (U32)val);\n    else\n        MEM_writeLE64(memPtr, (U64)val);\n}\n\n/*=== Big endian r/w ===*/\n\nMEM_STATIC U32 MEM_readBE32(const void* memPtr)\n{\n    if (MEM_isLittleEndian())\n        return MEM_swap32(MEM_read32(memPtr));\n    else\n        return MEM_read32(memPtr);\n}\n\nMEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)\n{\n    if (MEM_isLittleEndian())\n        MEM_write32(memPtr, MEM_swap32(val32));\n    else\n        MEM_write32(memPtr, val32);\n}\n\nMEM_STATIC U64 MEM_readBE64(const void* memPtr)\n{\n    if (MEM_isLittleEndian())\n        return MEM_swap64(MEM_read64(memPtr));\n    else\n        return MEM_read64(memPtr);\n}\n\nMEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)\n{\n    if (MEM_isLittleEndian())\n        MEM_write64(memPtr, MEM_swap64(val64));\n    else\n        MEM_write64(memPtr, val64);\n}\n\nMEM_STATIC size_t MEM_readBEST(const void* memPtr)\n{\n    if (MEM_32bits())\n        return (size_t)MEM_readBE32(memPtr);\n    else\n        return (size_t)MEM_readBE64(memPtr);\n}\n\nMEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)\n{\n    if (MEM_32bits())\n        MEM_writeBE32(memPtr, (U32)val);\n    else\n        MEM_writeBE64(memPtr, (U64)val);\n}\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* MEM_H_MODULE */\n/**** ended inlining mem.h ****/\n/**** start inlining error_private.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n/* Note : this module is expected to remain private, do not expose it */\n\n#ifndef ERROR_H_MODULE\n#define ERROR_H_MODULE\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n\n/* ****************************************\n*  Dependencies\n******************************************/\n#include <stddef.h>        /* size_t */\n/**** start inlining zstd_errors.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_ERRORS_H_398273423\n#define ZSTD_ERRORS_H_398273423\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/*===== dependency =====*/\n#include <stddef.h>   /* size_t */\n\n\n/* =====   ZSTDERRORLIB_API : control library symbols visibility   ===== */\n#ifndef ZSTDERRORLIB_VISIBILITY\n#  if defined(__GNUC__) && (__GNUC__ >= 4)\n#    define ZSTDERRORLIB_VISIBILITY __attribute__ ((visibility (\"default\")))\n#  else\n#    define ZSTDERRORLIB_VISIBILITY\n#  endif\n#endif\n#if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)\n#  define ZSTDERRORLIB_API __declspec(dllexport) ZSTDERRORLIB_VISIBILITY\n#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)\n#  define ZSTDERRORLIB_API __declspec(dllimport) ZSTDERRORLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/\n#else\n#  define ZSTDERRORLIB_API ZSTDERRORLIB_VISIBILITY\n#endif\n\n/*-*********************************************\n *  Error codes list\n *-*********************************************\n *  Error codes _values_ are pinned down since v1.3.1 only.\n *  Therefore, don't rely on values if you may link to any version < v1.3.1.\n *\n *  Only values < 100 are considered stable.\n *\n *  note 1 : this API shall be used with static linking only.\n *           dynamic linking is not yet officially supported.\n *  note 2 : Prefer relying on the enum than on its value whenever possible\n *           This is the only supported way to use the error list < v1.3.1\n *  note 3 : ZSTD_isError() is always correct, whatever the library version.\n **********************************************/\ntypedef enum {\n  ZSTD_error_no_error = 0,\n  ZSTD_error_GENERIC  = 1,\n  ZSTD_error_prefix_unknown                = 10,\n  ZSTD_error_version_unsupported           = 12,\n  ZSTD_error_frameParameter_unsupported    = 14,\n  ZSTD_error_frameParameter_windowTooLarge = 16,\n  ZSTD_error_corruption_detected = 20,\n  ZSTD_error_checksum_wrong      = 22,\n  ZSTD_error_dictionary_corrupted      = 30,\n  ZSTD_error_dictionary_wrong          = 32,\n  ZSTD_error_dictionaryCreation_failed = 34,\n  ZSTD_error_parameter_unsupported   = 40,\n  ZSTD_error_parameter_outOfBound    = 42,\n  ZSTD_error_tableLog_tooLarge       = 44,\n  ZSTD_error_maxSymbolValue_tooLarge = 46,\n  ZSTD_error_maxSymbolValue_tooSmall = 48,\n  ZSTD_error_stage_wrong       = 60,\n  ZSTD_error_init_missing      = 62,\n  ZSTD_error_memory_allocation = 64,\n  ZSTD_error_workSpace_tooSmall= 66,\n  ZSTD_error_dstSize_tooSmall = 70,\n  ZSTD_error_srcSize_wrong    = 72,\n  ZSTD_error_dstBuffer_null   = 74,\n  /* following error codes are __NOT STABLE__, they can be removed or changed in future versions */\n  ZSTD_error_frameIndex_tooLarge = 100,\n  ZSTD_error_seekableIO          = 102,\n  ZSTD_error_dstBuffer_wrong     = 104,\n  ZSTD_error_maxCode = 120  /* never EVER use this value directly, it can change in future versions! Use ZSTD_isError() instead */\n} ZSTD_ErrorCode;\n\n/*! ZSTD_getErrorCode() :\n    convert a `size_t` function result into a `ZSTD_ErrorCode` enum type,\n    which can be used to compare with enum list published above */\nZSTDERRORLIB_API ZSTD_ErrorCode ZSTD_getErrorCode(size_t functionResult);\nZSTDERRORLIB_API const char* ZSTD_getErrorString(ZSTD_ErrorCode code);   /**< Same as ZSTD_getErrorName, but using a `ZSTD_ErrorCode` enum argument */\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* ZSTD_ERRORS_H_398273423 */\n/**** ended inlining zstd_errors.h ****/\n\n\n/* ****************************************\n*  Compiler-specific\n******************************************/\n#if defined(__GNUC__)\n#  define ERR_STATIC static __attribute__((unused))\n#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)\n#  define ERR_STATIC static inline\n#elif defined(_MSC_VER)\n#  define ERR_STATIC static __inline\n#else\n#  define ERR_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */\n#endif\n\n\n/*-****************************************\n*  Customization (error_public.h)\n******************************************/\ntypedef ZSTD_ErrorCode ERR_enum;\n#define PREFIX(name) ZSTD_error_##name\n\n\n/*-****************************************\n*  Error codes handling\n******************************************/\n#undef ERROR   /* already defined on Visual Studio */\n#define ERROR(name) ZSTD_ERROR(name)\n#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))\n\nERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }\n\nERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }\n\n/* check and forward error code */\n#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e\n#define CHECK_F(f)   { CHECK_V_F(_var_err__, f); }\n\n\n/*-****************************************\n*  Error Strings\n******************************************/\n\nconst char* ERR_getErrorString(ERR_enum code);   /* error_private.c */\n\nERR_STATIC const char* ERR_getErrorName(size_t code)\n{\n    return ERR_getErrorString(ERR_getErrorCode(code));\n}\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* ERROR_H_MODULE */\n/**** ended inlining error_private.h ****/\n#define FSE_STATIC_LINKING_ONLY  /* FSE_MIN_TABLELOG */\n/**** start inlining fse.h ****/\n/* ******************************************************************\n * FSE : Finite State Entropy codec\n * Public Prototypes declaration\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n * You can contact the author at :\n * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n#ifndef FSE_H\n#define FSE_H\n\n\n/*-*****************************************\n*  Dependencies\n******************************************/\n#include <stddef.h>    /* size_t, ptrdiff_t */\n\n\n/*-*****************************************\n*  FSE_PUBLIC_API : control library symbols visibility\n******************************************/\n#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)\n#  define FSE_PUBLIC_API __attribute__ ((visibility (\"default\")))\n#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1)   /* Visual expected */\n#  define FSE_PUBLIC_API __declspec(dllexport)\n#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)\n#  define FSE_PUBLIC_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/\n#else\n#  define FSE_PUBLIC_API\n#endif\n\n/*------   Version   ------*/\n#define FSE_VERSION_MAJOR    0\n#define FSE_VERSION_MINOR    9\n#define FSE_VERSION_RELEASE  0\n\n#define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE\n#define FSE_QUOTE(str) #str\n#define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str)\n#define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION)\n\n#define FSE_VERSION_NUMBER  (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE)\nFSE_PUBLIC_API unsigned FSE_versionNumber(void);   /**< library version number; to be used when checking dll version */\n\n\n/*-****************************************\n*  FSE simple functions\n******************************************/\n/*! FSE_compress() :\n    Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'.\n    'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= FSE_compressBound(srcSize).\n    @return : size of compressed data (<= dstCapacity).\n    Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!\n                     if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead.\n                     if FSE_isError(return), compression failed (more details using FSE_getErrorName())\n*/\nFSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity,\n                             const void* src, size_t srcSize);\n\n/*! FSE_decompress():\n    Decompress FSE data from buffer 'cSrc', of size 'cSrcSize',\n    into already allocated destination buffer 'dst', of size 'dstCapacity'.\n    @return : size of regenerated data (<= maxDstSize),\n              or an error code, which can be tested using FSE_isError() .\n\n    ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!!\n    Why ? : making this distinction requires a header.\n    Header management is intentionally delegated to the user layer, which can better manage special cases.\n*/\nFSE_PUBLIC_API size_t FSE_decompress(void* dst,  size_t dstCapacity,\n                               const void* cSrc, size_t cSrcSize);\n\n\n/*-*****************************************\n*  Tool functions\n******************************************/\nFSE_PUBLIC_API size_t FSE_compressBound(size_t size);       /* maximum compressed size */\n\n/* Error Management */\nFSE_PUBLIC_API unsigned    FSE_isError(size_t code);        /* tells if a return value is an error code */\nFSE_PUBLIC_API const char* FSE_getErrorName(size_t code);   /* provides error code string (useful for debugging) */\n\n\n/*-*****************************************\n*  FSE advanced functions\n******************************************/\n/*! FSE_compress2() :\n    Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog'\n    Both parameters can be defined as '0' to mean : use default value\n    @return : size of compressed data\n    Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!!\n                     if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression.\n                     if FSE_isError(return), it's an error code.\n*/\nFSE_PUBLIC_API size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);\n\n\n/*-*****************************************\n*  FSE detailed API\n******************************************/\n/*!\nFSE_compress() does the following:\n1. count symbol occurrence from source[] into table count[] (see hist.h)\n2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog)\n3. save normalized counters to memory buffer using writeNCount()\n4. build encoding table 'CTable' from normalized counters\n5. encode the data stream using encoding table 'CTable'\n\nFSE_decompress() does the following:\n1. read normalized counters with readNCount()\n2. build decoding table 'DTable' from normalized counters\n3. decode the data stream using decoding table 'DTable'\n\nThe following API allows targeting specific sub-functions for advanced tasks.\nFor example, it's possible to compress several blocks using the same 'CTable',\nor to save and provide normalized distribution using external method.\n*/\n\n/* *** COMPRESSION *** */\n\n/*! FSE_optimalTableLog():\n    dynamically downsize 'tableLog' when conditions are met.\n    It saves CPU time, by using smaller tables, while preserving or even improving compression ratio.\n    @return : recommended tableLog (necessarily <= 'maxTableLog') */\nFSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);\n\n/*! FSE_normalizeCount():\n    normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)\n    'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).\n    @return : tableLog,\n              or an errorCode, which can be tested using FSE_isError() */\nFSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog,\n                    const unsigned* count, size_t srcSize, unsigned maxSymbolValue);\n\n/*! FSE_NCountWriteBound():\n    Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.\n    Typically useful for allocation purpose. */\nFSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);\n\n/*! FSE_writeNCount():\n    Compactly save 'normalizedCounter' into 'buffer'.\n    @return : size of the compressed table,\n              or an errorCode, which can be tested using FSE_isError(). */\nFSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize,\n                                 const short* normalizedCounter,\n                                 unsigned maxSymbolValue, unsigned tableLog);\n\n/*! Constructor and Destructor of FSE_CTable.\n    Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */\ntypedef unsigned FSE_CTable;   /* don't allocate that. It's only meant to be more restrictive than void* */\nFSE_PUBLIC_API FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog);\nFSE_PUBLIC_API void        FSE_freeCTable (FSE_CTable* ct);\n\n/*! FSE_buildCTable():\n    Builds `ct`, which must be already allocated, using FSE_createCTable().\n    @return : 0, or an errorCode, which can be tested using FSE_isError() */\nFSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);\n\n/*! FSE_compress_usingCTable():\n    Compress `src` using `ct` into `dst` which must be already allocated.\n    @return : size of compressed data (<= `dstCapacity`),\n              or 0 if compressed data could not fit into `dst`,\n              or an errorCode, which can be tested using FSE_isError() */\nFSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);\n\n/*!\nTutorial :\n----------\nThe first step is to count all symbols. FSE_count() does this job very fast.\nResult will be saved into 'count', a table of unsigned int, which must be already allocated, and have 'maxSymbolValuePtr[0]+1' cells.\n'src' is a table of bytes of size 'srcSize'. All values within 'src' MUST be <= maxSymbolValuePtr[0]\nmaxSymbolValuePtr[0] will be updated, with its real value (necessarily <= original value)\nFSE_count() will return the number of occurrence of the most frequent symbol.\nThis can be used to know if there is a single symbol within 'src', and to quickly evaluate its compressibility.\nIf there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).\n\nThe next step is to normalize the frequencies.\nFSE_normalizeCount() will ensure that sum of frequencies is == 2 ^'tableLog'.\nIt also guarantees a minimum of 1 to any Symbol with frequency >= 1.\nYou can use 'tableLog'==0 to mean \"use default tableLog value\".\nIf you are unsure of which tableLog value to use, you can ask FSE_optimalTableLog(),\nwhich will provide the optimal valid tableLog given sourceSize, maxSymbolValue, and a user-defined maximum (0 means \"default\").\n\nThe result of FSE_normalizeCount() will be saved into a table,\ncalled 'normalizedCounter', which is a table of signed short.\n'normalizedCounter' must be already allocated, and have at least 'maxSymbolValue+1' cells.\nThe return value is tableLog if everything proceeded as expected.\nIt is 0 if there is a single symbol within distribution.\nIf there is an error (ex: invalid tableLog value), the function will return an ErrorCode (which can be tested using FSE_isError()).\n\n'normalizedCounter' can be saved in a compact manner to a memory area using FSE_writeNCount().\n'buffer' must be already allocated.\nFor guaranteed success, buffer size must be at least FSE_headerBound().\nThe result of the function is the number of bytes written into 'buffer'.\nIf there is an error, the function will return an ErrorCode (which can be tested using FSE_isError(); ex : buffer size too small).\n\n'normalizedCounter' can then be used to create the compression table 'CTable'.\nThe space required by 'CTable' must be already allocated, using FSE_createCTable().\nYou can then use FSE_buildCTable() to fill 'CTable'.\nIf there is an error, both functions will return an ErrorCode (which can be tested using FSE_isError()).\n\n'CTable' can then be used to compress 'src', with FSE_compress_usingCTable().\nSimilar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize'\nThe function returns the size of compressed data (without header), necessarily <= `dstCapacity`.\nIf it returns '0', compressed data could not fit into 'dst'.\nIf there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).\n*/\n\n\n/* *** DECOMPRESSION *** */\n\n/*! FSE_readNCount():\n    Read compactly saved 'normalizedCounter' from 'rBuffer'.\n    @return : size read from 'rBuffer',\n              or an errorCode, which can be tested using FSE_isError().\n              maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */\nFSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter,\n                           unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,\n                           const void* rBuffer, size_t rBuffSize);\n\n/*! Constructor and Destructor of FSE_DTable.\n    Note that its size depends on 'tableLog' */\ntypedef unsigned FSE_DTable;   /* don't allocate that. It's just a way to be more restrictive than void* */\nFSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog);\nFSE_PUBLIC_API void        FSE_freeDTable(FSE_DTable* dt);\n\n/*! FSE_buildDTable():\n    Builds 'dt', which must be already allocated, using FSE_createDTable().\n    return : 0, or an errorCode, which can be tested using FSE_isError() */\nFSE_PUBLIC_API size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);\n\n/*! FSE_decompress_usingDTable():\n    Decompress compressed source `cSrc` of size `cSrcSize` using `dt`\n    into `dst` which must be already allocated.\n    @return : size of regenerated data (necessarily <= `dstCapacity`),\n              or an errorCode, which can be tested using FSE_isError() */\nFSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);\n\n/*!\nTutorial :\n----------\n(Note : these functions only decompress FSE-compressed blocks.\n If block is uncompressed, use memcpy() instead\n If block is a single repeated byte, use memset() instead )\n\nThe first step is to obtain the normalized frequencies of symbols.\nThis can be performed by FSE_readNCount() if it was saved using FSE_writeNCount().\n'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short.\nIn practice, that means it's necessary to know 'maxSymbolValue' beforehand,\nor size the table to handle worst case situations (typically 256).\nFSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'.\nThe result of FSE_readNCount() is the number of bytes read from 'rBuffer'.\nNote that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that.\nIf there is an error, the function will return an error code, which can be tested using FSE_isError().\n\nThe next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'.\nThis is performed by the function FSE_buildDTable().\nThe space required by 'FSE_DTable' must be already allocated using FSE_createDTable().\nIf there is an error, the function will return an error code, which can be tested using FSE_isError().\n\n`FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable().\n`cSrcSize` must be strictly correct, otherwise decompression will fail.\nFSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`).\nIf there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small)\n*/\n\n#endif  /* FSE_H */\n\n#if defined(FSE_STATIC_LINKING_ONLY) && !defined(FSE_H_FSE_STATIC_LINKING_ONLY)\n#define FSE_H_FSE_STATIC_LINKING_ONLY\n\n/* *** Dependency *** */\n/**** start inlining bitstream.h ****/\n/* ******************************************************************\n * bitstream\n * Part of FSE library\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n * You can contact the author at :\n * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n#ifndef BITSTREAM_H_MODULE\n#define BITSTREAM_H_MODULE\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/*\n*  This API consists of small unitary functions, which must be inlined for best performance.\n*  Since link-time-optimization is not available for all compilers,\n*  these functions are defined into a .h to be included.\n*/\n\n/*-****************************************\n*  Dependencies\n******************************************/\n/**** skipping file: mem.h ****/\n/**** start inlining compiler.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_COMPILER_H\n#define ZSTD_COMPILER_H\n\n/*-*******************************************************\n*  Compiler specifics\n*********************************************************/\n/* force inlining */\n\n#if !defined(ZSTD_NO_INLINE)\n#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */\n#  define INLINE_KEYWORD inline\n#else\n#  define INLINE_KEYWORD\n#endif\n\n#if defined(__GNUC__) || defined(__ICCARM__)\n#  define FORCE_INLINE_ATTR __attribute__((always_inline))\n#elif defined(_MSC_VER)\n#  define FORCE_INLINE_ATTR __forceinline\n#else\n#  define FORCE_INLINE_ATTR\n#endif\n\n#else\n\n#define INLINE_KEYWORD\n#define FORCE_INLINE_ATTR\n\n#endif\n\n/**\n  On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). \n  This explictly marks such functions as __cdecl so that the code will still compile \n  if a CC other than __cdecl has been made the default.\n*/\n#if  defined(_MSC_VER)\n#  define WIN_CDECL __cdecl\n#else\n#  define WIN_CDECL \n#endif\n\n/**\n * FORCE_INLINE_TEMPLATE is used to define C \"templates\", which take constant\n * parameters. They must be inlined for the compiler to eliminate the constant\n * branches.\n */\n#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR\n/**\n * HINT_INLINE is used to help the compiler generate better code. It is *not*\n * used for \"templates\", so it can be tweaked based on the compilers\n * performance.\n *\n * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the\n * always_inline attribute.\n *\n * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline\n * attribute.\n */\n#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5\n#  define HINT_INLINE static INLINE_KEYWORD\n#else\n#  define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR\n#endif\n\n/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */\n#if defined(__GNUC__)\n#  define UNUSED_ATTR __attribute__((unused))\n#else\n#  define UNUSED_ATTR\n#endif\n\n/* force no inlining */\n#ifdef _MSC_VER\n#  define FORCE_NOINLINE static __declspec(noinline)\n#else\n#  if defined(__GNUC__) || defined(__ICCARM__)\n#    define FORCE_NOINLINE static __attribute__((__noinline__))\n#  else\n#    define FORCE_NOINLINE static\n#  endif\n#endif\n\n/* target attribute */\n#ifndef __has_attribute\n  #define __has_attribute(x) 0  /* Compatibility with non-clang compilers. */\n#endif\n#if defined(__GNUC__) || defined(__ICCARM__)\n#  define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))\n#else\n#  define TARGET_ATTRIBUTE(target)\n#endif\n\n/* Enable runtime BMI2 dispatch based on the CPU.\n * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.\n */\n#ifndef DYNAMIC_BMI2\n  #if ((defined(__clang__) && __has_attribute(__target__)) \\\n      || (defined(__GNUC__) \\\n          && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \\\n      && (defined(__x86_64__) || defined(_M_X86)) \\\n      && !defined(__BMI2__)\n  #  define DYNAMIC_BMI2 1\n  #else\n  #  define DYNAMIC_BMI2 0\n  #endif\n#endif\n\n/* prefetch\n * can be disabled, by declaring NO_PREFETCH build macro */\n#if defined(NO_PREFETCH)\n#  define PREFETCH_L1(ptr)  (void)(ptr)  /* disabled */\n#  define PREFETCH_L2(ptr)  (void)(ptr)  /* disabled */\n#else\n#  if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))  /* _mm_prefetch() is not defined outside of x86/x64 */\n#    include <mmintrin.h>   /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */\n#    define PREFETCH_L1(ptr)  _mm_prefetch((const char*)(ptr), _MM_HINT_T0)\n#    define PREFETCH_L2(ptr)  _mm_prefetch((const char*)(ptr), _MM_HINT_T1)\n#    elif defined(__aarch64__)\n#     define PREFETCH_L1(ptr)  __asm__ __volatile__(\"prfm pldl1keep, %0\" ::\"Q\"(*(ptr)))\n#     define PREFETCH_L2(ptr)  __asm__ __volatile__(\"prfm pldl2keep, %0\" ::\"Q\"(*(ptr)))\n#  elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )\n#    define PREFETCH_L1(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)\n#    define PREFETCH_L2(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)\n#  else\n#    define PREFETCH_L1(ptr) (void)(ptr)  /* disabled */\n#    define PREFETCH_L2(ptr) (void)(ptr)  /* disabled */\n#  endif\n#endif  /* NO_PREFETCH */\n\n#define CACHELINE_SIZE 64\n\n#define PREFETCH_AREA(p, s)  {            \\\n    const char* const _ptr = (const char*)(p);  \\\n    size_t const _size = (size_t)(s);     \\\n    size_t _pos;                          \\\n    for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) {  \\\n        PREFETCH_L2(_ptr + _pos);         \\\n    }                                     \\\n}\n\n/* vectorization\n * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */\n#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)\n#  if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)\n#    define DONT_VECTORIZE __attribute__((optimize(\"no-tree-vectorize\")))\n#  else\n#    define DONT_VECTORIZE _Pragma(\"GCC optimize(\\\"no-tree-vectorize\\\")\")\n#  endif\n#else\n#  define DONT_VECTORIZE\n#endif\n\n/* Tell the compiler that a branch is likely or unlikely.\n * Only use these macros if it causes the compiler to generate better code.\n * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc\n * and clang, please do.\n */\n#if defined(__GNUC__)\n#define LIKELY(x) (__builtin_expect((x), 1))\n#define UNLIKELY(x) (__builtin_expect((x), 0))\n#else\n#define LIKELY(x) (x)\n#define UNLIKELY(x) (x)\n#endif\n\n/* disable warnings */\n#ifdef _MSC_VER    /* Visual Studio */\n#  include <intrin.h>                    /* For Visual 2005 */\n#  pragma warning(disable : 4100)        /* disable: C4100: unreferenced formal parameter */\n#  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */\n#  pragma warning(disable : 4204)        /* disable: C4204: non-constant aggregate initializer */\n#  pragma warning(disable : 4214)        /* disable: C4214: non-int bitfields */\n#  pragma warning(disable : 4324)        /* disable: C4324: padded structure */\n#endif\n\n#endif /* ZSTD_COMPILER_H */\n/**** ended inlining compiler.h ****/\n/**** skipping file: debug.h ****/\n/**** skipping file: error_private.h ****/\n\n\n/*=========================================\n*  Target specific\n=========================================*/\n#if defined(__BMI__) && defined(__GNUC__)\n#  include <immintrin.h>   /* support for bextr (experimental) */\n#elif defined(__ICCARM__)\n#  include <intrinsics.h>\n#endif\n\n#define STREAM_ACCUMULATOR_MIN_32  25\n#define STREAM_ACCUMULATOR_MIN_64  57\n#define STREAM_ACCUMULATOR_MIN    ((U32)(MEM_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64))\n\n\n/*-******************************************\n*  bitStream encoding API (write forward)\n********************************************/\n/* bitStream can mix input from multiple sources.\n * A critical property of these streams is that they encode and decode in **reverse** direction.\n * So the first bit sequence you add will be the last to be read, like a LIFO stack.\n */\ntypedef struct {\n    size_t bitContainer;\n    unsigned bitPos;\n    char*  startPtr;\n    char*  ptr;\n    char*  endPtr;\n} BIT_CStream_t;\n\nMEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);\nMEM_STATIC void   BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);\nMEM_STATIC void   BIT_flushBits(BIT_CStream_t* bitC);\nMEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);\n\n/* Start with initCStream, providing the size of buffer to write into.\n*  bitStream will never write outside of this buffer.\n*  `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code.\n*\n*  bits are first added to a local register.\n*  Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.\n*  Writing data into memory is an explicit operation, performed by the flushBits function.\n*  Hence keep track how many bits are potentially stored into local register to avoid register overflow.\n*  After a flushBits, a maximum of 7 bits might still be stored into local register.\n*\n*  Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.\n*\n*  Last operation is to close the bitStream.\n*  The function returns the final size of CStream in bytes.\n*  If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable)\n*/\n\n\n/*-********************************************\n*  bitStream decoding API (read backward)\n**********************************************/\ntypedef struct {\n    size_t   bitContainer;\n    unsigned bitsConsumed;\n    const char* ptr;\n    const char* start;\n    const char* limitPtr;\n} BIT_DStream_t;\n\ntypedef enum { BIT_DStream_unfinished = 0,\n               BIT_DStream_endOfBuffer = 1,\n               BIT_DStream_completed = 2,\n               BIT_DStream_overflow = 3 } BIT_DStream_status;  /* result of BIT_reloadDStream() */\n               /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */\n\nMEM_STATIC size_t   BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);\nMEM_STATIC size_t   BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);\nMEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);\nMEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);\n\n\n/* Start by invoking BIT_initDStream().\n*  A chunk of the bitStream is then stored into a local register.\n*  Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).\n*  You can then retrieve bitFields stored into the local register, **in reverse order**.\n*  Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.\n*  A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.\n*  Otherwise, it can be less than that, so proceed accordingly.\n*  Checking if DStream has reached its end can be performed with BIT_endOfDStream().\n*/\n\n\n/*-****************************************\n*  unsafe API\n******************************************/\nMEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);\n/* faster, but works only if value is \"clean\", meaning all high bits above nbBits are 0 */\n\nMEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);\n/* unsafe version; does not check buffer overflow */\n\nMEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);\n/* faster, but works only if nbBits >= 1 */\n\n\n\n/*-**************************************************************\n*  Internal functions\n****************************************************************/\nMEM_STATIC unsigned BIT_highbit32 (U32 val)\n{\n    assert(val != 0);\n    {\n#   if defined(_MSC_VER)   /* Visual */\n        unsigned long r=0;\n        return _BitScanReverse ( &r, val ) ? (unsigned)r : 0;\n#   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* Use GCC Intrinsic */\n        return __builtin_clz (val) ^ 31;\n#   elif defined(__ICCARM__)    /* IAR Intrinsic */\n        return 31 - __CLZ(val);\n#   else   /* Software version */\n        static const unsigned DeBruijnClz[32] = { 0,  9,  1, 10, 13, 21,  2, 29,\n                                                 11, 14, 16, 18, 22, 25,  3, 30,\n                                                  8, 12, 20, 28, 15, 17, 24,  7,\n                                                 19, 27, 23,  6, 26,  5,  4, 31 };\n        U32 v = val;\n        v |= v >> 1;\n        v |= v >> 2;\n        v |= v >> 4;\n        v |= v >> 8;\n        v |= v >> 16;\n        return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];\n#   endif\n    }\n}\n\n/*=====    Local Constants   =====*/\nstatic const unsigned BIT_mask[] = {\n    0,          1,         3,         7,         0xF,       0x1F,\n    0x3F,       0x7F,      0xFF,      0x1FF,     0x3FF,     0x7FF,\n    0xFFF,      0x1FFF,    0x3FFF,    0x7FFF,    0xFFFF,    0x1FFFF,\n    0x3FFFF,    0x7FFFF,   0xFFFFF,   0x1FFFFF,  0x3FFFFF,  0x7FFFFF,\n    0xFFFFFF,   0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF,\n    0x3FFFFFFF, 0x7FFFFFFF}; /* up to 31 bits */\n#define BIT_MASK_SIZE (sizeof(BIT_mask) / sizeof(BIT_mask[0]))\n\n/*-**************************************************************\n*  bitStream encoding\n****************************************************************/\n/*! BIT_initCStream() :\n *  `dstCapacity` must be > sizeof(size_t)\n *  @return : 0 if success,\n *            otherwise an error code (can be tested using ERR_isError()) */\nMEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,\n                                  void* startPtr, size_t dstCapacity)\n{\n    bitC->bitContainer = 0;\n    bitC->bitPos = 0;\n    bitC->startPtr = (char*)startPtr;\n    bitC->ptr = bitC->startPtr;\n    bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->bitContainer);\n    if (dstCapacity <= sizeof(bitC->bitContainer)) return ERROR(dstSize_tooSmall);\n    return 0;\n}\n\n/*! BIT_addBits() :\n *  can add up to 31 bits into `bitC`.\n *  Note : does not check for register overflow ! */\nMEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,\n                            size_t value, unsigned nbBits)\n{\n    MEM_STATIC_ASSERT(BIT_MASK_SIZE == 32);\n    assert(nbBits < BIT_MASK_SIZE);\n    assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);\n    bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;\n    bitC->bitPos += nbBits;\n}\n\n/*! BIT_addBitsFast() :\n *  works only if `value` is _clean_,\n *  meaning all high bits above nbBits are 0 */\nMEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,\n                                size_t value, unsigned nbBits)\n{\n    assert((value>>nbBits) == 0);\n    assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);\n    bitC->bitContainer |= value << bitC->bitPos;\n    bitC->bitPos += nbBits;\n}\n\n/*! BIT_flushBitsFast() :\n *  assumption : bitContainer has not overflowed\n *  unsafe version; does not check buffer overflow */\nMEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)\n{\n    size_t const nbBytes = bitC->bitPos >> 3;\n    assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);\n    assert(bitC->ptr <= bitC->endPtr);\n    MEM_writeLEST(bitC->ptr, bitC->bitContainer);\n    bitC->ptr += nbBytes;\n    bitC->bitPos &= 7;\n    bitC->bitContainer >>= nbBytes*8;\n}\n\n/*! BIT_flushBits() :\n *  assumption : bitContainer has not overflowed\n *  safe version; check for buffer overflow, and prevents it.\n *  note : does not signal buffer overflow.\n *  overflow will be revealed later on using BIT_closeCStream() */\nMEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)\n{\n    size_t const nbBytes = bitC->bitPos >> 3;\n    assert(bitC->bitPos < sizeof(bitC->bitContainer) * 8);\n    assert(bitC->ptr <= bitC->endPtr);\n    MEM_writeLEST(bitC->ptr, bitC->bitContainer);\n    bitC->ptr += nbBytes;\n    if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;\n    bitC->bitPos &= 7;\n    bitC->bitContainer >>= nbBytes*8;\n}\n\n/*! BIT_closeCStream() :\n *  @return : size of CStream, in bytes,\n *            or 0 if it could not fit into dstBuffer */\nMEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)\n{\n    BIT_addBitsFast(bitC, 1, 1);   /* endMark */\n    BIT_flushBits(bitC);\n    if (bitC->ptr >= bitC->endPtr) return 0; /* overflow detected */\n    return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);\n}\n\n\n/*-********************************************************\n*  bitStream decoding\n**********************************************************/\n/*! BIT_initDStream() :\n *  Initialize a BIT_DStream_t.\n * `bitD` : a pointer to an already allocated BIT_DStream_t structure.\n * `srcSize` must be the *exact* size of the bitStream, in bytes.\n * @return : size of stream (== srcSize), or an errorCode if a problem is detected\n */\nMEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)\n{\n    if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }\n\n    bitD->start = (const char*)srcBuffer;\n    bitD->limitPtr = bitD->start + sizeof(bitD->bitContainer);\n\n    if (srcSize >=  sizeof(bitD->bitContainer)) {  /* normal case */\n        bitD->ptr   = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);\n        bitD->bitContainer = MEM_readLEST(bitD->ptr);\n        { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];\n          bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;  /* ensures bitsConsumed is always set */\n          if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }\n    } else {\n        bitD->ptr   = bitD->start;\n        bitD->bitContainer = *(const BYTE*)(bitD->start);\n        switch(srcSize)\n        {\n        case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);\n                /* fall-through */\n\n        case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);\n                /* fall-through */\n\n        case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);\n                /* fall-through */\n\n        case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;\n                /* fall-through */\n\n        case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;\n                /* fall-through */\n\n        case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) <<  8;\n                /* fall-through */\n\n        default: break;\n        }\n        {   BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];\n            bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;\n            if (lastByte == 0) return ERROR(corruption_detected);  /* endMark not present */\n        }\n        bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;\n    }\n\n    return srcSize;\n}\n\nMEM_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start)\n{\n    return bitContainer >> start;\n}\n\nMEM_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)\n{\n    U32 const regMask = sizeof(bitContainer)*8 - 1;\n    /* if start > regMask, bitstream is corrupted, and result is undefined */\n    assert(nbBits < BIT_MASK_SIZE);\n    return (bitContainer >> (start & regMask)) & BIT_mask[nbBits];\n}\n\nMEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)\n{\n    assert(nbBits < BIT_MASK_SIZE);\n    return bitContainer & BIT_mask[nbBits];\n}\n\n/*! BIT_lookBits() :\n *  Provides next n bits from local register.\n *  local register is not modified.\n *  On 32-bits, maxNbBits==24.\n *  On 64-bits, maxNbBits==56.\n * @return : value extracted */\nMEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)\n{\n    /* arbitrate between double-shift and shift+mask */\n#if 1\n    /* if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8,\n     * bitstream is likely corrupted, and result is undefined */\n    return BIT_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);\n#else\n    /* this code path is slower on my os-x laptop */\n    U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;\n    return ((bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> 1) >> ((regMask-nbBits) & regMask);\n#endif\n}\n\n/*! BIT_lookBitsFast() :\n *  unsafe version; only works if nbBits >= 1 */\nMEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)\n{\n    U32 const regMask = sizeof(bitD->bitContainer)*8 - 1;\n    assert(nbBits >= 1);\n    return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);\n}\n\nMEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)\n{\n    bitD->bitsConsumed += nbBits;\n}\n\n/*! BIT_readBits() :\n *  Read (consume) next n bits from local register and update.\n *  Pay attention to not read more than nbBits contained into local register.\n * @return : extracted value. */\nMEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)\n{\n    size_t const value = BIT_lookBits(bitD, nbBits);\n    BIT_skipBits(bitD, nbBits);\n    return value;\n}\n\n/*! BIT_readBitsFast() :\n *  unsafe version; only works only if nbBits >= 1 */\nMEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)\n{\n    size_t const value = BIT_lookBitsFast(bitD, nbBits);\n    assert(nbBits >= 1);\n    BIT_skipBits(bitD, nbBits);\n    return value;\n}\n\n/*! BIT_reloadDStreamFast() :\n *  Similar to BIT_reloadDStream(), but with two differences:\n *  1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!\n *  2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this\n *     point you must use BIT_reloadDStream() to reload.\n */\nMEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)\n{\n    if (UNLIKELY(bitD->ptr < bitD->limitPtr))\n        return BIT_DStream_overflow;\n    assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);\n    bitD->ptr -= bitD->bitsConsumed >> 3;\n    bitD->bitsConsumed &= 7;\n    bitD->bitContainer = MEM_readLEST(bitD->ptr);\n    return BIT_DStream_unfinished;\n}\n\n/*! BIT_reloadDStream() :\n *  Refill `bitD` from buffer previously set in BIT_initDStream() .\n *  This function is safe, it guarantees it will not read beyond src buffer.\n * @return : status of `BIT_DStream_t` internal register.\n *           when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */\nMEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)\n{\n    if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8))  /* overflow detected, like end of stream */\n        return BIT_DStream_overflow;\n\n    if (bitD->ptr >= bitD->limitPtr) {\n        return BIT_reloadDStreamFast(bitD);\n    }\n    if (bitD->ptr == bitD->start) {\n        if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;\n        return BIT_DStream_completed;\n    }\n    /* start < ptr < limitPtr */\n    {   U32 nbBytes = bitD->bitsConsumed >> 3;\n        BIT_DStream_status result = BIT_DStream_unfinished;\n        if (bitD->ptr - nbBytes < bitD->start) {\n            nbBytes = (U32)(bitD->ptr - bitD->start);  /* ptr > start */\n            result = BIT_DStream_endOfBuffer;\n        }\n        bitD->ptr -= nbBytes;\n        bitD->bitsConsumed -= nbBytes*8;\n        bitD->bitContainer = MEM_readLEST(bitD->ptr);   /* reminder : srcSize > sizeof(bitD->bitContainer), otherwise bitD->ptr == bitD->start */\n        return result;\n    }\n}\n\n/*! BIT_endOfDStream() :\n * @return : 1 if DStream has _exactly_ reached its end (all bits consumed).\n */\nMEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)\n{\n    return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));\n}\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* BITSTREAM_H_MODULE */\n/**** ended inlining bitstream.h ****/\n\n\n/* *****************************************\n*  Static allocation\n*******************************************/\n/* FSE buffer bounds */\n#define FSE_NCOUNTBOUND 512\n#define FSE_BLOCKBOUND(size) (size + (size>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)\n#define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size))   /* Macro version, useful for static allocation */\n\n/* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */\n#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue)   (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))\n#define FSE_DTABLE_SIZE_U32(maxTableLog)                   (1 + (1<<maxTableLog))\n\n/* or use the size to malloc() space directly. Pay attention to alignment restrictions though */\n#define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue)   (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable))\n#define FSE_DTABLE_SIZE(maxTableLog)                   (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable))\n\n\n/* *****************************************\n *  FSE advanced API\n ***************************************** */\n\nunsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus);\n/**< same as FSE_optimalTableLog(), which used `minus==2` */\n\n/* FSE_compress_wksp() :\n * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).\n * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.\n */\n#define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue)   ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )\nsize_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);\n\nsize_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits);\n/**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */\n\nsize_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);\n/**< build a fake FSE_CTable, designed to compress always the same symbolValue */\n\n/* FSE_buildCTable_wksp() :\n * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).\n * `wkspSize` must be >= `(1<<tableLog)`.\n */\nsize_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);\n\nsize_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);\n/**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */\n\nsize_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);\n/**< build a fake FSE_DTable, designed to always generate the same symbolValue */\n\nsize_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog);\n/**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */\n\ntypedef enum {\n   FSE_repeat_none,  /**< Cannot use the previous table */\n   FSE_repeat_check, /**< Can use the previous table but it must be checked */\n   FSE_repeat_valid  /**< Can use the previous table and it is assumed to be valid */\n } FSE_repeat;\n\n/* *****************************************\n*  FSE symbol compression API\n*******************************************/\n/*!\n   This API consists of small unitary functions, which highly benefit from being inlined.\n   Hence their body are included in next section.\n*/\ntypedef struct {\n    ptrdiff_t   value;\n    const void* stateTable;\n    const void* symbolTT;\n    unsigned    stateLog;\n} FSE_CState_t;\n\nstatic void FSE_initCState(FSE_CState_t* CStatePtr, const FSE_CTable* ct);\n\nstatic void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* CStatePtr, unsigned symbol);\n\nstatic void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* CStatePtr);\n\n/**<\nThese functions are inner components of FSE_compress_usingCTable().\nThey allow the creation of custom streams, mixing multiple tables and bit sources.\n\nA key property to keep in mind is that encoding and decoding are done **in reverse direction**.\nSo the first symbol you will encode is the last you will decode, like a LIFO stack.\n\nYou will need a few variables to track your CStream. They are :\n\nFSE_CTable    ct;         // Provided by FSE_buildCTable()\nBIT_CStream_t bitStream;  // bitStream tracking structure\nFSE_CState_t  state;      // State tracking structure (can have several)\n\n\nThe first thing to do is to init bitStream and state.\n    size_t errorCode = BIT_initCStream(&bitStream, dstBuffer, maxDstSize);\n    FSE_initCState(&state, ct);\n\nNote that BIT_initCStream() can produce an error code, so its result should be tested, using FSE_isError();\nYou can then encode your input data, byte after byte.\nFSE_encodeSymbol() outputs a maximum of 'tableLog' bits at a time.\nRemember decoding will be done in reverse direction.\n    FSE_encodeByte(&bitStream, &state, symbol);\n\nAt any time, you can also add any bit sequence.\nNote : maximum allowed nbBits is 25, for compatibility with 32-bits decoders\n    BIT_addBits(&bitStream, bitField, nbBits);\n\nThe above methods don't commit data to memory, they just store it into local register, for speed.\nLocal register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).\nWriting data to memory is a manual operation, performed by the flushBits function.\n    BIT_flushBits(&bitStream);\n\nYour last FSE encoding operation shall be to flush your last state value(s).\n    FSE_flushState(&bitStream, &state);\n\nFinally, you must close the bitStream.\nThe function returns the size of CStream in bytes.\nIf data couldn't fit into dstBuffer, it will return a 0 ( == not compressible)\nIf there is an error, it returns an errorCode (which can be tested using FSE_isError()).\n    size_t size = BIT_closeCStream(&bitStream);\n*/\n\n\n/* *****************************************\n*  FSE symbol decompression API\n*******************************************/\ntypedef struct {\n    size_t      state;\n    const void* table;   /* precise table may vary, depending on U16 */\n} FSE_DState_t;\n\n\nstatic void     FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt);\n\nstatic unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);\n\nstatic unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);\n\n/**<\nLet's now decompose FSE_decompress_usingDTable() into its unitary components.\nYou will decode FSE-encoded symbols from the bitStream,\nand also any other bitFields you put in, **in reverse order**.\n\nYou will need a few variables to track your bitStream. They are :\n\nBIT_DStream_t DStream;    // Stream context\nFSE_DState_t  DState;     // State context. Multiple ones are possible\nFSE_DTable*   DTablePtr;  // Decoding table, provided by FSE_buildDTable()\n\nThe first thing to do is to init the bitStream.\n    errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);\n\nYou should then retrieve your initial state(s)\n(in reverse flushing order if you have several ones) :\n    errorCode = FSE_initDState(&DState, &DStream, DTablePtr);\n\nYou can then decode your data, symbol after symbol.\nFor information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.\nKeep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).\n    unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);\n\nYou can retrieve any bitfield you eventually stored into the bitStream (in reverse order)\nNote : maximum allowed nbBits is 25, for 32-bits compatibility\n    size_t bitField = BIT_readBits(&DStream, nbBits);\n\nAll above operations only read from local register (which size depends on size_t).\nRefueling the register from memory is manually performed by the reload method.\n    endSignal = FSE_reloadDStream(&DStream);\n\nBIT_reloadDStream() result tells if there is still some more data to read from DStream.\nBIT_DStream_unfinished : there is still some data left into the DStream.\nBIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.\nBIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.\nBIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.\n\nWhen reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,\nto properly detect the exact end of stream.\nAfter each decoded symbol, check if DStream is fully consumed using this simple test :\n    BIT_reloadDStream(&DStream) >= BIT_DStream_completed\n\nWhen it's done, verify decompression is fully completed, by checking both DStream and the relevant states.\nChecking if DStream has reached its end is performed by :\n    BIT_endOfDStream(&DStream);\nCheck also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.\n    FSE_endOfDState(&DState);\n*/\n\n\n/* *****************************************\n*  FSE unsafe API\n*******************************************/\nstatic unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD);\n/* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */\n\n\n/* *****************************************\n*  Implementation of inlined functions\n*******************************************/\ntypedef struct {\n    int deltaFindState;\n    U32 deltaNbBits;\n} FSE_symbolCompressionTransform; /* total 8 bytes */\n\nMEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct)\n{\n    const void* ptr = ct;\n    const U16* u16ptr = (const U16*) ptr;\n    const U32 tableLog = MEM_read16(ptr);\n    statePtr->value = (ptrdiff_t)1<<tableLog;\n    statePtr->stateTable = u16ptr+2;\n    statePtr->symbolTT = ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1);\n    statePtr->stateLog = tableLog;\n}\n\n\n/*! FSE_initCState2() :\n*   Same as FSE_initCState(), but the first symbol to include (which will be the last to be read)\n*   uses the smallest state value possible, saving the cost of this symbol */\nMEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U32 symbol)\n{\n    FSE_initCState(statePtr, ct);\n    {   const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];\n        const U16* stateTable = (const U16*)(statePtr->stateTable);\n        U32 nbBitsOut  = (U32)((symbolTT.deltaNbBits + (1<<15)) >> 16);\n        statePtr->value = (nbBitsOut << 16) - symbolTT.deltaNbBits;\n        statePtr->value = stateTable[(statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];\n    }\n}\n\nMEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, unsigned symbol)\n{\n    FSE_symbolCompressionTransform const symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];\n    const U16* const stateTable = (const U16*)(statePtr->stateTable);\n    U32 const nbBitsOut  = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);\n    BIT_addBits(bitC, statePtr->value, nbBitsOut);\n    statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];\n}\n\nMEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePtr)\n{\n    BIT_addBits(bitC, statePtr->value, statePtr->stateLog);\n    BIT_flushBits(bitC);\n}\n\n\n/* FSE_getMaxNbBits() :\n * Approximate maximum cost of a symbol, in bits.\n * Fractional get rounded up (i.e : a symbol with a normalized frequency of 3 gives the same result as a frequency of 2)\n * note 1 : assume symbolValue is valid (<= maxSymbolValue)\n * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */\nMEM_STATIC U32 FSE_getMaxNbBits(const void* symbolTTPtr, U32 symbolValue)\n{\n    const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr;\n    return (symbolTT[symbolValue].deltaNbBits + ((1<<16)-1)) >> 16;\n}\n\n/* FSE_bitCost() :\n * Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional bits)\n * note 1 : assume symbolValue is valid (<= maxSymbolValue)\n * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */\nMEM_STATIC U32 FSE_bitCost(const void* symbolTTPtr, U32 tableLog, U32 symbolValue, U32 accuracyLog)\n{\n    const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr;\n    U32 const minNbBits = symbolTT[symbolValue].deltaNbBits >> 16;\n    U32 const threshold = (minNbBits+1) << 16;\n    assert(tableLog < 16);\n    assert(accuracyLog < 31-tableLog);  /* ensure enough room for renormalization double shift */\n    {   U32 const tableSize = 1 << tableLog;\n        U32 const deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize);\n        U32 const normalizedDeltaFromThreshold = (deltaFromThreshold << accuracyLog) >> tableLog;   /* linear interpolation (very approximate) */\n        U32 const bitMultiplier = 1 << accuracyLog;\n        assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold);\n        assert(normalizedDeltaFromThreshold <= bitMultiplier);\n        return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold;\n    }\n}\n\n\n/* ======    Decompression    ====== */\n\ntypedef struct {\n    U16 tableLog;\n    U16 fastMode;\n} FSE_DTableHeader;   /* sizeof U32 */\n\ntypedef struct\n{\n    unsigned short newState;\n    unsigned char  symbol;\n    unsigned char  nbBits;\n} FSE_decode_t;   /* size == U32 */\n\nMEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt)\n{\n    const void* ptr = dt;\n    const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr;\n    DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);\n    BIT_reloadDStream(bitD);\n    DStatePtr->table = dt + 1;\n}\n\nMEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr)\n{\n    FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];\n    return DInfo.symbol;\n}\n\nMEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)\n{\n    FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];\n    U32 const nbBits = DInfo.nbBits;\n    size_t const lowBits = BIT_readBits(bitD, nbBits);\n    DStatePtr->state = DInfo.newState + lowBits;\n}\n\nMEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)\n{\n    FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];\n    U32 const nbBits = DInfo.nbBits;\n    BYTE const symbol = DInfo.symbol;\n    size_t const lowBits = BIT_readBits(bitD, nbBits);\n\n    DStatePtr->state = DInfo.newState + lowBits;\n    return symbol;\n}\n\n/*! FSE_decodeSymbolFast() :\n    unsafe, only works if no symbol has a probability > 50% */\nMEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)\n{\n    FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];\n    U32 const nbBits = DInfo.nbBits;\n    BYTE const symbol = DInfo.symbol;\n    size_t const lowBits = BIT_readBitsFast(bitD, nbBits);\n\n    DStatePtr->state = DInfo.newState + lowBits;\n    return symbol;\n}\n\nMEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)\n{\n    return DStatePtr->state == 0;\n}\n\n\n\n#ifndef FSE_COMMONDEFS_ONLY\n\n/* **************************************************************\n*  Tuning parameters\n****************************************************************/\n/*!MEMORY_USAGE :\n*  Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)\n*  Increasing memory usage improves compression ratio\n*  Reduced memory usage can improve speed, due to cache effect\n*  Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */\n#ifndef FSE_MAX_MEMORY_USAGE\n#  define FSE_MAX_MEMORY_USAGE 14\n#endif\n#ifndef FSE_DEFAULT_MEMORY_USAGE\n#  define FSE_DEFAULT_MEMORY_USAGE 13\n#endif\n\n/*!FSE_MAX_SYMBOL_VALUE :\n*  Maximum symbol value authorized.\n*  Required for proper stack allocation */\n#ifndef FSE_MAX_SYMBOL_VALUE\n#  define FSE_MAX_SYMBOL_VALUE 255\n#endif\n\n/* **************************************************************\n*  template functions type & suffix\n****************************************************************/\n#define FSE_FUNCTION_TYPE BYTE\n#define FSE_FUNCTION_EXTENSION\n#define FSE_DECODE_TYPE FSE_decode_t\n\n\n#endif   /* !FSE_COMMONDEFS_ONLY */\n\n\n/* ***************************************************************\n*  Constants\n*****************************************************************/\n#define FSE_MAX_TABLELOG  (FSE_MAX_MEMORY_USAGE-2)\n#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)\n#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)\n#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)\n#define FSE_MIN_TABLELOG 5\n\n#define FSE_TABLELOG_ABSOLUTE_MAX 15\n#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX\n#  error \"FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported\"\n#endif\n\n#define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3)\n\n\n#endif /* FSE_STATIC_LINKING_ONLY */\n\n\n#if defined (__cplusplus)\n}\n#endif\n/**** ended inlining fse.h ****/\n#define HUF_STATIC_LINKING_ONLY  /* HUF_TABLELOG_ABSOLUTEMAX */\n/**** start inlining huf.h ****/\n/* ******************************************************************\n * huff0 huffman codec,\n * part of Finite State Entropy library\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n * You can contact the author at :\n * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n#ifndef HUF_H_298734234\n#define HUF_H_298734234\n\n/* *** Dependencies *** */\n#include <stddef.h>    /* size_t */\n\n\n/* *** library symbols visibility *** */\n/* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,\n *        HUF symbols remain \"private\" (internal symbols for library only).\n *        Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */\n#if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)\n#  define HUF_PUBLIC_API __attribute__ ((visibility (\"default\")))\n#elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1)   /* Visual expected */\n#  define HUF_PUBLIC_API __declspec(dllexport)\n#elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)\n#  define HUF_PUBLIC_API __declspec(dllimport)  /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */\n#else\n#  define HUF_PUBLIC_API\n#endif\n\n\n/* ========================== */\n/* ***  simple functions  *** */\n/* ========================== */\n\n/** HUF_compress() :\n *  Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.\n * 'dst' buffer must be already allocated.\n *  Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).\n * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.\n * @return : size of compressed data (<= `dstCapacity`).\n *  Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!\n *                   if HUF_isError(return), compression failed (more details using HUF_getErrorName())\n */\nHUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,\n                             const void* src, size_t srcSize);\n\n/** HUF_decompress() :\n *  Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',\n *  into already allocated buffer 'dst', of minimum size 'dstSize'.\n * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.\n *  Note : in contrast with FSE, HUF_decompress can regenerate\n *         RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,\n *         because it knows size to regenerate (originalSize).\n * @return : size of regenerated data (== originalSize),\n *           or an error code, which can be tested using HUF_isError()\n */\nHUF_PUBLIC_API size_t HUF_decompress(void* dst,  size_t originalSize,\n                               const void* cSrc, size_t cSrcSize);\n\n\n/* ***   Tool functions *** */\n#define HUF_BLOCKSIZE_MAX (128 * 1024)                  /**< maximum input size for a single block compressed with HUF_compress */\nHUF_PUBLIC_API size_t HUF_compressBound(size_t size);   /**< maximum compressed size (worst case) */\n\n/* Error Management */\nHUF_PUBLIC_API unsigned    HUF_isError(size_t code);       /**< tells if a return value is an error code */\nHUF_PUBLIC_API const char* HUF_getErrorName(size_t code);  /**< provides error code string (useful for debugging) */\n\n\n/* ***   Advanced function   *** */\n\n/** HUF_compress2() :\n *  Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.\n * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .\n * `tableLog` must be `<= HUF_TABLELOG_MAX` . */\nHUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,\n                               const void* src, size_t srcSize,\n                               unsigned maxSymbolValue, unsigned tableLog);\n\n/** HUF_compress4X_wksp() :\n *  Same as HUF_compress2(), but uses externally allocated `workSpace`.\n * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */\n#define HUF_WORKSPACE_SIZE ((6 << 10) + 256)\n#define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))\nHUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,\n                                     const void* src, size_t srcSize,\n                                     unsigned maxSymbolValue, unsigned tableLog,\n                                     void* workSpace, size_t wkspSize);\n\n#endif   /* HUF_H_298734234 */\n\n/* ******************************************************************\n *  WARNING !!\n *  The following section contains advanced and experimental definitions\n *  which shall never be used in the context of a dynamic library,\n *  because they are not guaranteed to remain stable in the future.\n *  Only consider them in association with static linking.\n * *****************************************************************/\n#if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)\n#define HUF_H_HUF_STATIC_LINKING_ONLY\n\n/* *** Dependencies *** */\n/**** skipping file: mem.h ****/\n\n\n/* *** Constants *** */\n#define HUF_TABLELOG_MAX      12      /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */\n#define HUF_TABLELOG_DEFAULT  11      /* default tableLog value when none specified */\n#define HUF_SYMBOLVALUE_MAX  255\n\n#define HUF_TABLELOG_ABSOLUTEMAX  15  /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */\n#if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)\n#  error \"HUF_TABLELOG_MAX is too large !\"\n#endif\n\n\n/* ****************************************\n*  Static allocation\n******************************************/\n/* HUF buffer bounds */\n#define HUF_CTABLEBOUND 129\n#define HUF_BLOCKBOUND(size) (size + (size>>8) + 8)   /* only true when incompressible is pre-filtered with fast heuristic */\n#define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size))   /* Macro version, useful for static allocation */\n\n/* static allocation of HUF's Compression Table */\n#define HUF_CTABLE_SIZE_U32(maxSymbolValue)   ((maxSymbolValue)+1)   /* Use tables of U32, for proper alignment */\n#define HUF_CTABLE_SIZE(maxSymbolValue)       (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))\n#define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \\\n    U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \\\n    void* name##hv = &(name##hb); \\\n    HUF_CElt* name = (HUF_CElt*)(name##hv)   /* no final ; */\n\n/* static allocation of HUF's DTable */\ntypedef U32 HUF_DTable;\n#define HUF_DTABLE_SIZE(maxTableLog)   (1 + (1<<(maxTableLog)))\n#define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \\\n        HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }\n#define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \\\n        HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }\n\n\n/* ****************************************\n*  Advanced decompression functions\n******************************************/\nsize_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< single-symbol decoder */\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< double-symbols decoder */\n#endif\n\nsize_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< decodes RLE and uncompressed */\nsize_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */\nsize_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */\nsize_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< single-symbol decoder */\nsize_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< single-symbol decoder */\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< double-symbols decoder */\nsize_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< double-symbols decoder */\n#endif\n\n\n/* ****************************************\n *  HUF detailed API\n * ****************************************/\n\n/*! HUF_compress() does the following:\n *  1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within \"fse.h\")\n *  2. (optional) refine tableLog using HUF_optimalTableLog()\n *  3. build Huffman table from count using HUF_buildCTable()\n *  4. save Huffman table to memory buffer using HUF_writeCTable()\n *  5. encode the data stream using HUF_compress4X_usingCTable()\n *\n *  The following API allows targeting specific sub-functions for advanced tasks.\n *  For example, it's possible to compress several blocks using the same 'CTable',\n *  or to save and regenerate 'CTable' using external methods.\n */\nunsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);\ntypedef struct HUF_CElt_s HUF_CElt;   /* incomplete type */\nsize_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits);   /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */\nsize_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);\nsize_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);\nsize_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);\nint HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);\n\ntypedef enum {\n   HUF_repeat_none,  /**< Cannot use the previous table */\n   HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */\n   HUF_repeat_valid  /**< Can use the previous table and it is assumed to be valid */\n } HUF_repeat;\n/** HUF_compress4X_repeat() :\n *  Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.\n *  If it uses hufTable it does not modify hufTable or repeat.\n *  If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.\n *  If preferRepeat then the old table will always be used if valid. */\nsize_t HUF_compress4X_repeat(void* dst, size_t dstSize,\n                       const void* src, size_t srcSize,\n                       unsigned maxSymbolValue, unsigned tableLog,\n                       void* workSpace, size_t wkspSize,    /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */\n                       HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);\n\n/** HUF_buildCTable_wksp() :\n *  Same as HUF_buildCTable(), but using externally allocated scratch buffer.\n * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.\n */\n#define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)\n#define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))\nsize_t HUF_buildCTable_wksp (HUF_CElt* tree,\n                       const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,\n                             void* workSpace, size_t wkspSize);\n\n/*! HUF_readStats() :\n *  Read compact Huffman tree, saved by HUF_writeCTable().\n * `huffWeight` is destination buffer.\n * @return : size read from `src` , or an error Code .\n *  Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */\nsize_t HUF_readStats(BYTE* huffWeight, size_t hwSize,\n                     U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,\n                     const void* src, size_t srcSize);\n\n/** HUF_readCTable() :\n *  Loading a CTable saved with HUF_writeCTable() */\nsize_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);\n\n/** HUF_getNbBits() :\n *  Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX\n *  Note 1 : is not inlined, as HUF_CElt definition is private\n *  Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */\nU32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);\n\n/*\n * HUF_decompress() does the following:\n * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics\n * 2. build Huffman table from save, using HUF_readDTableX?()\n * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()\n */\n\n/** HUF_selectDecoder() :\n *  Tells which decoder is likely to decode faster,\n *  based on a set of pre-computed metrics.\n * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .\n *  Assumption : 0 < dstSize <= 128 KB */\nU32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);\n\n/**\n *  The minimum workspace size for the `workSpace` used in\n *  HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().\n *\n *  The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when\n *  HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.\n *  Buffer overflow errors may potentially occur if code modifications result in\n *  a required workspace size greater than that specified in the following\n *  macro.\n */\n#define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)\n#define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))\n\n#ifndef HUF_FORCE_DECOMPRESS_X2\nsize_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);\nsize_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);\n#endif\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);\nsize_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);\n#endif\n\nsize_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);\n#ifndef HUF_FORCE_DECOMPRESS_X2\nsize_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);\n#endif\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);\n#endif\n\n\n/* ====================== */\n/* single stream variants */\n/* ====================== */\n\nsize_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);\nsize_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);  /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */\nsize_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);\n/** HUF_compress1X_repeat() :\n *  Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.\n *  If it uses hufTable it does not modify hufTable or repeat.\n *  If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.\n *  If preferRepeat then the old table will always be used if valid. */\nsize_t HUF_compress1X_repeat(void* dst, size_t dstSize,\n                       const void* src, size_t srcSize,\n                       unsigned maxSymbolValue, unsigned tableLog,\n                       void* workSpace, size_t wkspSize,   /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */\n                       HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);\n\nsize_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* single-symbol decoder */\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /* double-symbol decoder */\n#endif\n\nsize_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);\nsize_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);\n#ifndef HUF_FORCE_DECOMPRESS_X2\nsize_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< single-symbol decoder */\nsize_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< single-symbol decoder */\n#endif\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);   /**< double-symbols decoder */\nsize_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);   /**< double-symbols decoder */\n#endif\n\nsize_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);   /**< automatic selection of sing or double symbol decoder, based on DTable */\n#ifndef HUF_FORCE_DECOMPRESS_X2\nsize_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);\n#endif\n#ifndef HUF_FORCE_DECOMPRESS_X1\nsize_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);\n#endif\n\n/* BMI2 variants.\n * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.\n */\nsize_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);\n#ifndef HUF_FORCE_DECOMPRESS_X2\nsize_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);\n#endif\nsize_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);\nsize_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);\n\n#endif /* HUF_STATIC_LINKING_ONLY */\n\n#if defined (__cplusplus)\n}\n#endif\n/**** ended inlining huf.h ****/\n\n\n/*===   Version   ===*/\nunsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }\n\n\n/*===   Error Management   ===*/\nunsigned FSE_isError(size_t code) { return ERR_isError(code); }\nconst char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }\n\nunsigned HUF_isError(size_t code) { return ERR_isError(code); }\nconst char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }\n\n\n/*-**************************************************************\n*  FSE NCount encoding-decoding\n****************************************************************/\nsize_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,\n                 const void* headerBuffer, size_t hbSize)\n{\n    const BYTE* const istart = (const BYTE*) headerBuffer;\n    const BYTE* const iend = istart + hbSize;\n    const BYTE* ip = istart;\n    int nbBits;\n    int remaining;\n    int threshold;\n    U32 bitStream;\n    int bitCount;\n    unsigned charnum = 0;\n    int previous0 = 0;\n\n    if (hbSize < 4) {\n        /* This function only works when hbSize >= 4 */\n        char buffer[4];\n        memset(buffer, 0, sizeof(buffer));\n        memcpy(buffer, headerBuffer, hbSize);\n        {   size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,\n                                                    buffer, sizeof(buffer));\n            if (FSE_isError(countSize)) return countSize;\n            if (countSize > hbSize) return ERROR(corruption_detected);\n            return countSize;\n    }   }\n    assert(hbSize >= 4);\n\n    /* init */\n    memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0]));   /* all symbols not present in NCount have a frequency of 0 */\n    bitStream = MEM_readLE32(ip);\n    nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG;   /* extract tableLog */\n    if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);\n    bitStream >>= 4;\n    bitCount = 4;\n    *tableLogPtr = nbBits;\n    remaining = (1<<nbBits)+1;\n    threshold = 1<<nbBits;\n    nbBits++;\n\n    while ((remaining>1) & (charnum<=*maxSVPtr)) {\n        if (previous0) {\n            unsigned n0 = charnum;\n            while ((bitStream & 0xFFFF) == 0xFFFF) {\n                n0 += 24;\n                if (ip < iend-5) {\n                    ip += 2;\n                    bitStream = MEM_readLE32(ip) >> bitCount;\n                } else {\n                    bitStream >>= 16;\n                    bitCount   += 16;\n            }   }\n            while ((bitStream & 3) == 3) {\n                n0 += 3;\n                bitStream >>= 2;\n                bitCount += 2;\n            }\n            n0 += bitStream & 3;\n            bitCount += 2;\n            if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);\n            while (charnum < n0) normalizedCounter[charnum++] = 0;\n            if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {\n                assert((bitCount >> 3) <= 3); /* For first condition to work */\n                ip += bitCount>>3;\n                bitCount &= 7;\n                bitStream = MEM_readLE32(ip) >> bitCount;\n            } else {\n                bitStream >>= 2;\n        }   }\n        {   int const max = (2*threshold-1) - remaining;\n            int count;\n\n            if ((bitStream & (threshold-1)) < (U32)max) {\n                count = bitStream & (threshold-1);\n                bitCount += nbBits-1;\n            } else {\n                count = bitStream & (2*threshold-1);\n                if (count >= threshold) count -= max;\n                bitCount += nbBits;\n            }\n\n            count--;   /* extra accuracy */\n            remaining -= count < 0 ? -count : count;   /* -1 means +1 */\n            normalizedCounter[charnum++] = (short)count;\n            previous0 = !count;\n            while (remaining < threshold) {\n                nbBits--;\n                threshold >>= 1;\n            }\n\n            if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {\n                ip += bitCount>>3;\n                bitCount &= 7;\n            } else {\n                bitCount -= (int)(8 * (iend - 4 - ip));\n                ip = iend - 4;\n            }\n            bitStream = MEM_readLE32(ip) >> (bitCount & 31);\n    }   }   /* while ((remaining>1) & (charnum<=*maxSVPtr)) */\n    if (remaining != 1) return ERROR(corruption_detected);\n    if (bitCount > 32) return ERROR(corruption_detected);\n    *maxSVPtr = charnum-1;\n\n    ip += (bitCount+7)>>3;\n    return ip-istart;\n}\n\n\n/*! HUF_readStats() :\n    Read compact Huffman tree, saved by HUF_writeCTable().\n    `huffWeight` is destination buffer.\n    `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.\n    @return : size read from `src` , or an error Code .\n    Note : Needed by HUF_readCTable() and HUF_readDTableX?() .\n*/\nsize_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,\n                     U32* nbSymbolsPtr, U32* tableLogPtr,\n                     const void* src, size_t srcSize)\n{\n    U32 weightTotal;\n    const BYTE* ip = (const BYTE*) src;\n    size_t iSize;\n    size_t oSize;\n\n    if (!srcSize) return ERROR(srcSize_wrong);\n    iSize = ip[0];\n    /* memset(huffWeight, 0, hwSize);   *//* is not necessary, even though some analyzer complain ... */\n\n    if (iSize >= 128) {  /* special header */\n        oSize = iSize - 127;\n        iSize = ((oSize+1)/2);\n        if (iSize+1 > srcSize) return ERROR(srcSize_wrong);\n        if (oSize >= hwSize) return ERROR(corruption_detected);\n        ip += 1;\n        {   U32 n;\n            for (n=0; n<oSize; n+=2) {\n                huffWeight[n]   = ip[n/2] >> 4;\n                huffWeight[n+1] = ip[n/2] & 15;\n    }   }   }\n    else  {   /* header compressed with FSE (normal case) */\n        FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)];  /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */\n        if (iSize+1 > srcSize) return ERROR(srcSize_wrong);\n        oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6);   /* max (hwSize-1) values decoded, as last one is implied */\n        if (FSE_isError(oSize)) return oSize;\n    }\n\n    /* collect weight stats */\n    memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));\n    weightTotal = 0;\n    {   U32 n; for (n=0; n<oSize; n++) {\n            if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);\n            rankStats[huffWeight[n]]++;\n            weightTotal += (1 << huffWeight[n]) >> 1;\n    }   }\n    if (weightTotal == 0) return ERROR(corruption_detected);\n\n    /* get last non-null symbol weight (implied, total must be 2^n) */\n    {   U32 const tableLog = BIT_highbit32(weightTotal) + 1;\n        if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);\n        *tableLogPtr = tableLog;\n        /* determine last weight */\n        {   U32 const total = 1 << tableLog;\n            U32 const rest = total - weightTotal;\n            U32 const verif = 1 << BIT_highbit32(rest);\n            U32 const lastWeight = BIT_highbit32(rest) + 1;\n            if (verif != rest) return ERROR(corruption_detected);    /* last value must be a clean power of 2 */\n            huffWeight[oSize] = (BYTE)lastWeight;\n            rankStats[lastWeight]++;\n    }   }\n\n    /* check tree construction validity */\n    if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected);   /* by construction : at least 2 elts of rank 1, must be even */\n\n    /* results */\n    *nbSymbolsPtr = (U32)(oSize+1);\n    return iSize+1;\n}\n/**** ended inlining common/entropy_common.c ****/\n/**** start inlining common/error_private.c ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n/* The purpose of this file is to have a single list of error strings embedded in binary */\n\n/**** skipping file: error_private.h ****/\n\nconst char* ERR_getErrorString(ERR_enum code)\n{\n#ifdef ZSTD_STRIP_ERROR_STRINGS\n    (void)code;\n    return \"Error strings stripped\";\n#else\n    static const char* const notErrorCode = \"Unspecified error code\";\n    switch( code )\n    {\n    case PREFIX(no_error): return \"No error detected\";\n    case PREFIX(GENERIC):  return \"Error (generic)\";\n    case PREFIX(prefix_unknown): return \"Unknown frame descriptor\";\n    case PREFIX(version_unsupported): return \"Version not supported\";\n    case PREFIX(frameParameter_unsupported): return \"Unsupported frame parameter\";\n    case PREFIX(frameParameter_windowTooLarge): return \"Frame requires too much memory for decoding\";\n    case PREFIX(corruption_detected): return \"Corrupted block detected\";\n    case PREFIX(checksum_wrong): return \"Restored data doesn't match checksum\";\n    case PREFIX(parameter_unsupported): return \"Unsupported parameter\";\n    case PREFIX(parameter_outOfBound): return \"Parameter is out of bound\";\n    case PREFIX(init_missing): return \"Context should be init first\";\n    case PREFIX(memory_allocation): return \"Allocation error : not enough memory\";\n    case PREFIX(workSpace_tooSmall): return \"workSpace buffer is not large enough\";\n    case PREFIX(stage_wrong): return \"Operation not authorized at current processing stage\";\n    case PREFIX(tableLog_tooLarge): return \"tableLog requires too much memory : unsupported\";\n    case PREFIX(maxSymbolValue_tooLarge): return \"Unsupported max Symbol Value : too large\";\n    case PREFIX(maxSymbolValue_tooSmall): return \"Specified maxSymbolValue is too small\";\n    case PREFIX(dictionary_corrupted): return \"Dictionary is corrupted\";\n    case PREFIX(dictionary_wrong): return \"Dictionary mismatch\";\n    case PREFIX(dictionaryCreation_failed): return \"Cannot create Dictionary from provided samples\";\n    case PREFIX(dstSize_tooSmall): return \"Destination buffer is too small\";\n    case PREFIX(srcSize_wrong): return \"Src size is incorrect\";\n    case PREFIX(dstBuffer_null): return \"Operation on NULL destination buffer\";\n        /* following error codes are not stable and may be removed or changed in a future version */\n    case PREFIX(frameIndex_tooLarge): return \"Frame index is too large\";\n    case PREFIX(seekableIO): return \"An I/O error occurred when reading/seeking\";\n    case PREFIX(dstBuffer_wrong): return \"Destination buffer is wrong\";\n    case PREFIX(maxCode):\n    default: return notErrorCode;\n    }\n#endif\n}\n/**** ended inlining common/error_private.c ****/\n/**** start inlining common/fse_decompress.c ****/\n/* ******************************************************************\n * FSE : Finite State Entropy decoder\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n *  You can contact the author at :\n *  - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *  - Public forum : https://groups.google.com/forum/#!forum/lz4c\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n\n/* **************************************************************\n*  Includes\n****************************************************************/\n/**** skipping file: bitstream.h ****/\n/**** skipping file: compiler.h ****/\n#define FSE_STATIC_LINKING_ONLY\n/**** skipping file: fse.h ****/\n/**** skipping file: error_private.h ****/\n\n\n/* **************************************************************\n*  Error Management\n****************************************************************/\n#define FSE_isError ERR_isError\n#define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)   /* use only *after* variable declarations */\n\n\n/* **************************************************************\n*  Templates\n****************************************************************/\n/*\n  designed to be included\n  for type-specific functions (template emulation in C)\n  Objective is to write these functions only once, for improved maintenance\n*/\n\n/* safety checks */\n#ifndef FSE_FUNCTION_EXTENSION\n#  error \"FSE_FUNCTION_EXTENSION must be defined\"\n#endif\n#ifndef FSE_FUNCTION_TYPE\n#  error \"FSE_FUNCTION_TYPE must be defined\"\n#endif\n\n/* Function names */\n#define FSE_CAT(X,Y) X##Y\n#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)\n#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)\n\n\n/* Function templates */\nFSE_DTable* FSE_createDTable (unsigned tableLog)\n{\n    if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;\n    return (FSE_DTable*)malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) );\n}\n\nvoid FSE_freeDTable (FSE_DTable* dt)\n{\n    free(dt);\n}\n\nsize_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)\n{\n    void* const tdPtr = dt+1;   /* because *dt is unsigned, 32-bits aligned on 32-bits */\n    FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);\n    U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];\n\n    U32 const maxSV1 = maxSymbolValue + 1;\n    U32 const tableSize = 1 << tableLog;\n    U32 highThreshold = tableSize-1;\n\n    /* Sanity Checks */\n    if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);\n    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);\n\n    /* Init, lay down lowprob symbols */\n    {   FSE_DTableHeader DTableH;\n        DTableH.tableLog = (U16)tableLog;\n        DTableH.fastMode = 1;\n        {   S16 const largeLimit= (S16)(1 << (tableLog-1));\n            U32 s;\n            for (s=0; s<maxSV1; s++) {\n                if (normalizedCounter[s]==-1) {\n                    tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;\n                    symbolNext[s] = 1;\n                } else {\n                    if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;\n                    symbolNext[s] = normalizedCounter[s];\n        }   }   }\n        memcpy(dt, &DTableH, sizeof(DTableH));\n    }\n\n    /* Spread symbols */\n    {   U32 const tableMask = tableSize-1;\n        U32 const step = FSE_TABLESTEP(tableSize);\n        U32 s, position = 0;\n        for (s=0; s<maxSV1; s++) {\n            int i;\n            for (i=0; i<normalizedCounter[s]; i++) {\n                tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;\n                position = (position + step) & tableMask;\n                while (position > highThreshold) position = (position + step) & tableMask;   /* lowprob area */\n        }   }\n        if (position!=0) return ERROR(GENERIC);   /* position must reach all cells once, otherwise normalizedCounter is incorrect */\n    }\n\n    /* Build Decoding table */\n    {   U32 u;\n        for (u=0; u<tableSize; u++) {\n            FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);\n            U32 const nextState = symbolNext[symbol]++;\n            tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );\n            tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);\n    }   }\n\n    return 0;\n}\n\n\n#ifndef FSE_COMMONDEFS_ONLY\n\n/*-*******************************************************\n*  Decompression (Byte symbols)\n*********************************************************/\nsize_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)\n{\n    void* ptr = dt;\n    FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;\n    void* dPtr = dt + 1;\n    FSE_decode_t* const cell = (FSE_decode_t*)dPtr;\n\n    DTableH->tableLog = 0;\n    DTableH->fastMode = 0;\n\n    cell->newState = 0;\n    cell->symbol = symbolValue;\n    cell->nbBits = 0;\n\n    return 0;\n}\n\n\nsize_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)\n{\n    void* ptr = dt;\n    FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;\n    void* dPtr = dt + 1;\n    FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;\n    const unsigned tableSize = 1 << nbBits;\n    const unsigned tableMask = tableSize - 1;\n    const unsigned maxSV1 = tableMask+1;\n    unsigned s;\n\n    /* Sanity checks */\n    if (nbBits < 1) return ERROR(GENERIC);         /* min size */\n\n    /* Build Decoding Table */\n    DTableH->tableLog = (U16)nbBits;\n    DTableH->fastMode = 1;\n    for (s=0; s<maxSV1; s++) {\n        dinfo[s].newState = 0;\n        dinfo[s].symbol = (BYTE)s;\n        dinfo[s].nbBits = (BYTE)nbBits;\n    }\n\n    return 0;\n}\n\nFORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(\n          void* dst, size_t maxDstSize,\n    const void* cSrc, size_t cSrcSize,\n    const FSE_DTable* dt, const unsigned fast)\n{\n    BYTE* const ostart = (BYTE*) dst;\n    BYTE* op = ostart;\n    BYTE* const omax = op + maxDstSize;\n    BYTE* const olimit = omax-3;\n\n    BIT_DStream_t bitD;\n    FSE_DState_t state1;\n    FSE_DState_t state2;\n\n    /* Init */\n    CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));\n\n    FSE_initDState(&state1, &bitD, dt);\n    FSE_initDState(&state2, &bitD, dt);\n\n#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)\n\n    /* 4 symbols per loop */\n    for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {\n        op[0] = FSE_GETSYMBOL(&state1);\n\n        if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8)    /* This test must be static */\n            BIT_reloadDStream(&bitD);\n\n        op[1] = FSE_GETSYMBOL(&state2);\n\n        if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8)    /* This test must be static */\n            { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }\n\n        op[2] = FSE_GETSYMBOL(&state1);\n\n        if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8)    /* This test must be static */\n            BIT_reloadDStream(&bitD);\n\n        op[3] = FSE_GETSYMBOL(&state2);\n    }\n\n    /* tail */\n    /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */\n    while (1) {\n        if (op>(omax-2)) return ERROR(dstSize_tooSmall);\n        *op++ = FSE_GETSYMBOL(&state1);\n        if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {\n            *op++ = FSE_GETSYMBOL(&state2);\n            break;\n        }\n\n        if (op>(omax-2)) return ERROR(dstSize_tooSmall);\n        *op++ = FSE_GETSYMBOL(&state2);\n        if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {\n            *op++ = FSE_GETSYMBOL(&state1);\n            break;\n    }   }\n\n    return op-ostart;\n}\n\n\nsize_t FSE_decompress_usingDTable(void* dst, size_t originalSize,\n                            const void* cSrc, size_t cSrcSize,\n                            const FSE_DTable* dt)\n{\n    const void* ptr = dt;\n    const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;\n    const U32 fastMode = DTableH->fastMode;\n\n    /* select fast mode (static) */\n    if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);\n    return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);\n}\n\n\nsize_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)\n{\n    const BYTE* const istart = (const BYTE*)cSrc;\n    const BYTE* ip = istart;\n    short counting[FSE_MAX_SYMBOL_VALUE+1];\n    unsigned tableLog;\n    unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;\n\n    /* normal FSE decoding mode */\n    size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);\n    if (FSE_isError(NCountLength)) return NCountLength;\n    /* if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); */  /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */\n    if (tableLog > maxLog) return ERROR(tableLog_tooLarge);\n    ip += NCountLength;\n    cSrcSize -= NCountLength;\n\n    CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );\n\n    return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace);   /* always return, even if it is an error code */\n}\n\n\ntypedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];\n\nsize_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)\n{\n    DTable_max_t dt;   /* Static analyzer seems unable to understand this table will be properly initialized later */\n    return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);\n}\n\n\n\n#endif   /* FSE_COMMONDEFS_ONLY */\n/**** ended inlining common/fse_decompress.c ****/\n/**** start inlining common/zstd_common.c ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n\n\n/*-*************************************\n*  Dependencies\n***************************************/\n/**** skipping file: error_private.h ****/\n/**** start inlining zstd_internal.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_CCOMMON_H_MODULE\n#define ZSTD_CCOMMON_H_MODULE\n\n/* this module contains definitions which must be identical\n * across compression, decompression and dictBuilder.\n * It also contains a few functions useful to at least 2 of them\n * and which benefit from being inlined */\n\n/*-*************************************\n*  Dependencies\n***************************************/\n#ifdef __aarch64__\n#include <arm_neon.h>\n#endif\n/**** skipping file: compiler.h ****/\n/**** skipping file: mem.h ****/\n/**** skipping file: debug.h ****/\n/**** skipping file: error_private.h ****/\n#define ZSTD_STATIC_LINKING_ONLY\n/**** start inlining ../zstd.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n#ifndef ZSTD_H_235446\n#define ZSTD_H_235446\n\n/* ======   Dependency   ======*/\n#include <limits.h>   /* INT_MAX */\n#include <stddef.h>   /* size_t */\n\n\n/* =====   ZSTDLIB_API : control library symbols visibility   ===== */\n#ifndef ZSTDLIB_VISIBILITY\n#  if defined(__GNUC__) && (__GNUC__ >= 4)\n#    define ZSTDLIB_VISIBILITY __attribute__ ((visibility (\"default\")))\n#  else\n#    define ZSTDLIB_VISIBILITY\n#  endif\n#endif\n#if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)\n#  define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY\n#elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)\n#  define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/\n#else\n#  define ZSTDLIB_API ZSTDLIB_VISIBILITY\n#endif\n\n\n/*******************************************************************************\n  Introduction\n\n  zstd, short for Zstandard, is a fast lossless compression algorithm, targeting\n  real-time compression scenarios at zlib-level and better compression ratios.\n  The zstd compression library provides in-memory compression and decompression\n  functions.\n\n  The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),\n  which is currently 22. Levels >= 20, labeled `--ultra`, should be used with\n  caution, as they require more memory. The library also offers negative\n  compression levels, which extend the range of speed vs. ratio preferences.\n  The lower the level, the faster the speed (at the cost of compression).\n\n  Compression can be done in:\n    - a single step (described as Simple API)\n    - a single step, reusing a context (described as Explicit context)\n    - unbounded multiple steps (described as Streaming compression)\n\n  The compression ratio achievable on small data can be highly improved using\n  a dictionary. Dictionary compression can be performed in:\n    - a single step (described as Simple dictionary API)\n    - a single step, reusing a dictionary (described as Bulk-processing\n      dictionary API)\n\n  Advanced experimental functions can be accessed using\n  `#define ZSTD_STATIC_LINKING_ONLY` before including zstd.h.\n\n  Advanced experimental APIs should never be used with a dynamically-linked\n  library. They are not \"stable\"; their definitions or signatures may change in\n  the future. Only static linking is allowed.\n*******************************************************************************/\n\n/*------   Version   ------*/\n#define ZSTD_VERSION_MAJOR    1\n#define ZSTD_VERSION_MINOR    4\n#define ZSTD_VERSION_RELEASE  5\n\n#define ZSTD_VERSION_NUMBER  (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)\nZSTDLIB_API unsigned ZSTD_versionNumber(void);   /**< to check runtime library version */\n\n#define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE\n#define ZSTD_QUOTE(str) #str\n#define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)\n#define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)\nZSTDLIB_API const char* ZSTD_versionString(void);   /* requires v1.3.0+ */\n\n/* *************************************\n *  Default constant\n ***************************************/\n#ifndef ZSTD_CLEVEL_DEFAULT\n#  define ZSTD_CLEVEL_DEFAULT 3\n#endif\n\n/* *************************************\n *  Constants\n ***************************************/\n\n/* All magic numbers are supposed read/written to/from files/memory using little-endian convention */\n#define ZSTD_MAGICNUMBER            0xFD2FB528    /* valid since v0.8.0 */\n#define ZSTD_MAGIC_DICTIONARY       0xEC30A437    /* valid since v0.7.0 */\n#define ZSTD_MAGIC_SKIPPABLE_START  0x184D2A50    /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */\n#define ZSTD_MAGIC_SKIPPABLE_MASK   0xFFFFFFF0\n\n#define ZSTD_BLOCKSIZELOG_MAX  17\n#define ZSTD_BLOCKSIZE_MAX     (1<<ZSTD_BLOCKSIZELOG_MAX)\n\n\n\n/***************************************\n*  Simple API\n***************************************/\n/*! ZSTD_compress() :\n *  Compresses `src` content as a single zstd compressed frame into already allocated `dst`.\n *  Hint : compression runs faster if `dstCapacity` >=  `ZSTD_compressBound(srcSize)`.\n *  @return : compressed size written into `dst` (<= `dstCapacity),\n *            or an error code if it fails (which can be tested using ZSTD_isError()). */\nZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,\n                            const void* src, size_t srcSize,\n                                  int compressionLevel);\n\n/*! ZSTD_decompress() :\n *  `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames.\n *  `dstCapacity` is an upper bound of originalSize to regenerate.\n *  If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.\n *  @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),\n *            or an errorCode if it fails (which can be tested using ZSTD_isError()). */\nZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,\n                              const void* src, size_t compressedSize);\n\n/*! ZSTD_getFrameContentSize() : requires v1.3.0+\n *  `src` should point to the start of a ZSTD encoded frame.\n *  `srcSize` must be at least as large as the frame header.\n *            hint : any size >= `ZSTD_frameHeaderSize_max` is large enough.\n *  @return : - decompressed size of `src` frame content, if known\n *            - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined\n *            - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)\n *   note 1 : a 0 return value means the frame is valid but \"empty\".\n *   note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode.\n *            When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.\n *            In which case, it's necessary to use streaming mode to decompress data.\n *            Optionally, application can rely on some implicit limit,\n *            as ZSTD_decompress() only needs an upper bound of decompressed size.\n *            (For example, data could be necessarily cut into blocks <= 16 KB).\n *   note 3 : decompressed size is always present when compression is completed using single-pass functions,\n *            such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict().\n *   note 4 : decompressed size can be very large (64-bits value),\n *            potentially larger than what local system can handle as a single memory segment.\n *            In which case, it's necessary to use streaming mode to decompress data.\n *   note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified.\n *            Always ensure return value fits within application's authorized limits.\n *            Each application can set its own limits.\n *   note 6 : This function replaces ZSTD_getDecompressedSize() */\n#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)\n#define ZSTD_CONTENTSIZE_ERROR   (0ULL - 2)\nZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);\n\n/*! ZSTD_getDecompressedSize() :\n *  NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize().\n *  Both functions work the same way, but ZSTD_getDecompressedSize() blends\n *  \"empty\", \"unknown\" and \"error\" results to the same return value (0),\n *  while ZSTD_getFrameContentSize() gives them separate return values.\n * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */\nZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);\n\n/*! ZSTD_findFrameCompressedSize() :\n * `src` should point to the start of a ZSTD frame or skippable frame.\n * `srcSize` must be >= first frame size\n * @return : the compressed size of the first frame starting at `src`,\n *           suitable to pass as `srcSize` to `ZSTD_decompress` or similar,\n *        or an error code if input is invalid */\nZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);\n\n\n/*======  Helper functions  ======*/\n#define ZSTD_COMPRESSBOUND(srcSize)   ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0))  /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */\nZSTDLIB_API size_t      ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */\nZSTDLIB_API unsigned    ZSTD_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */\nZSTDLIB_API const char* ZSTD_getErrorName(size_t code);     /*!< provides readable string from an error code */\nZSTDLIB_API int         ZSTD_minCLevel(void);               /*!< minimum negative compression level allowed */\nZSTDLIB_API int         ZSTD_maxCLevel(void);               /*!< maximum compression level available */\n\n\n/***************************************\n*  Explicit context\n***************************************/\n/*= Compression context\n *  When compressing many times,\n *  it is recommended to allocate a context just once,\n *  and re-use it for each successive compression operation.\n *  This will make workload friendlier for system's memory.\n *  Note : re-using context is just a speed / resource optimization.\n *         It doesn't change the compression ratio, which remains identical.\n *  Note 2 : In multi-threaded environments,\n *         use one different context per thread for parallel execution.\n */\ntypedef struct ZSTD_CCtx_s ZSTD_CCtx;\nZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);\nZSTDLIB_API size_t     ZSTD_freeCCtx(ZSTD_CCtx* cctx);\n\n/*! ZSTD_compressCCtx() :\n *  Same as ZSTD_compress(), using an explicit ZSTD_CCtx.\n *  Important : in order to behave similarly to `ZSTD_compress()`,\n *  this function compresses at requested compression level,\n *  __ignoring any other parameter__ .\n *  If any advanced parameter was set using the advanced API,\n *  they will all be reset. Only `compressionLevel` remains.\n */\nZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,\n                                     void* dst, size_t dstCapacity,\n                               const void* src, size_t srcSize,\n                                     int compressionLevel);\n\n/*= Decompression context\n *  When decompressing many times,\n *  it is recommended to allocate a context only once,\n *  and re-use it for each successive compression operation.\n *  This will make workload friendlier for system's memory.\n *  Use one context per thread for parallel execution. */\ntypedef struct ZSTD_DCtx_s ZSTD_DCtx;\nZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);\nZSTDLIB_API size_t     ZSTD_freeDCtx(ZSTD_DCtx* dctx);\n\n/*! ZSTD_decompressDCtx() :\n *  Same as ZSTD_decompress(),\n *  requires an allocated ZSTD_DCtx.\n *  Compatible with sticky parameters.\n */\nZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,\n                                       void* dst, size_t dstCapacity,\n                                 const void* src, size_t srcSize);\n\n\n/***************************************\n*  Advanced compression API\n***************************************/\n\n/* API design :\n *   Parameters are pushed one by one into an existing context,\n *   using ZSTD_CCtx_set*() functions.\n *   Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame.\n *   \"sticky\" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` !\n *   __They do not apply to \"simple\" one-shot variants such as ZSTD_compressCCtx()__ .\n *\n *   It's possible to reset all parameters to \"default\" using ZSTD_CCtx_reset().\n *\n *   This API supercedes all other \"advanced\" API entry points in the experimental section.\n *   In the future, we expect to remove from experimental API entry points which are redundant with this API.\n */\n\n\n/* Compression strategies, listed from fastest to strongest */\ntypedef enum { ZSTD_fast=1,\n               ZSTD_dfast=2,\n               ZSTD_greedy=3,\n               ZSTD_lazy=4,\n               ZSTD_lazy2=5,\n               ZSTD_btlazy2=6,\n               ZSTD_btopt=7,\n               ZSTD_btultra=8,\n               ZSTD_btultra2=9\n               /* note : new strategies _might_ be added in the future.\n                         Only the order (from fast to strong) is guaranteed */\n} ZSTD_strategy;\n\n\ntypedef enum {\n\n    /* compression parameters\n     * Note: When compressing with a ZSTD_CDict these parameters are superseded\n     * by the parameters used to construct the ZSTD_CDict.\n     * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */\n    ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table.\n                              * Note that exact compression parameters are dynamically determined,\n                              * depending on both compression level and srcSize (when known).\n                              * Default level is ZSTD_CLEVEL_DEFAULT==3.\n                              * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.\n                              * Note 1 : it's possible to pass a negative compression level.\n                              * Note 2 : setting a level does not automatically set all other compression parameters\n                              *   to default. Setting this will however eventually dynamically impact the compression\n                              *   parameters which have not been manually set. The manually set\n                              *   ones will 'stick'. */\n    /* Advanced compression parameters :\n     * It's possible to pin down compression parameters to some specific values.\n     * In which case, these values are no longer dynamically selected by the compressor */\n    ZSTD_c_windowLog=101,    /* Maximum allowed back-reference distance, expressed as power of 2.\n                              * This will set a memory budget for streaming decompression,\n                              * with larger values requiring more memory\n                              * and typically compressing more.\n                              * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.\n                              * Special: value 0 means \"use default windowLog\".\n                              * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT\n                              *       requires explicitly allowing such size at streaming decompression stage. */\n    ZSTD_c_hashLog=102,      /* Size of the initial probe table, as a power of 2.\n                              * Resulting memory usage is (1 << (hashLog+2)).\n                              * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.\n                              * Larger tables improve compression ratio of strategies <= dFast,\n                              * and improve speed of strategies > dFast.\n                              * Special: value 0 means \"use default hashLog\". */\n    ZSTD_c_chainLog=103,     /* Size of the multi-probe search table, as a power of 2.\n                              * Resulting memory usage is (1 << (chainLog+2)).\n                              * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.\n                              * Larger tables result in better and slower compression.\n                              * This parameter is useless for \"fast\" strategy.\n                              * It's still useful when using \"dfast\" strategy,\n                              * in which case it defines a secondary probe table.\n                              * Special: value 0 means \"use default chainLog\". */\n    ZSTD_c_searchLog=104,    /* Number of search attempts, as a power of 2.\n                              * More attempts result in better and slower compression.\n                              * This parameter is useless for \"fast\" and \"dFast\" strategies.\n                              * Special: value 0 means \"use default searchLog\". */\n    ZSTD_c_minMatch=105,     /* Minimum size of searched matches.\n                              * Note that Zstandard can still find matches of smaller size,\n                              * it just tweaks its search algorithm to look for this size and larger.\n                              * Larger values increase compression and decompression speed, but decrease ratio.\n                              * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX.\n                              * Note that currently, for all strategies < btopt, effective minimum is 4.\n                              *                    , for all strategies > fast, effective maximum is 6.\n                              * Special: value 0 means \"use default minMatchLength\". */\n    ZSTD_c_targetLength=106, /* Impact of this field depends on strategy.\n                              * For strategies btopt, btultra & btultra2:\n                              *     Length of Match considered \"good enough\" to stop search.\n                              *     Larger values make compression stronger, and slower.\n                              * For strategy fast:\n                              *     Distance between match sampling.\n                              *     Larger values make compression faster, and weaker.\n                              * Special: value 0 means \"use default targetLength\". */\n    ZSTD_c_strategy=107,     /* See ZSTD_strategy enum definition.\n                              * The higher the value of selected strategy, the more complex it is,\n                              * resulting in stronger and slower compression.\n                              * Special: value 0 means \"use default strategy\". */\n\n    /* LDM mode parameters */\n    ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching.\n                                     * This parameter is designed to improve compression ratio\n                                     * for large inputs, by finding large matches at long distance.\n                                     * It increases memory usage and window size.\n                                     * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB\n                                     * except when expressly set to a different value. */\n    ZSTD_c_ldmHashLog=161,   /* Size of the table for long distance matching, as a power of 2.\n                              * Larger values increase memory usage and compression ratio,\n                              * but decrease compression speed.\n                              * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX\n                              * default: windowlog - 7.\n                              * Special: value 0 means \"automatically determine hashlog\". */\n    ZSTD_c_ldmMinMatch=162,  /* Minimum match size for long distance matcher.\n                              * Larger/too small values usually decrease compression ratio.\n                              * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.\n                              * Special: value 0 means \"use default value\" (default: 64). */\n    ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution.\n                              * Larger values improve collision resolution but decrease compression speed.\n                              * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX.\n                              * Special: value 0 means \"use default value\" (default: 3). */\n    ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table.\n                              * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).\n                              * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.\n                              * Larger values improve compression speed.\n                              * Deviating far from default value will likely result in a compression ratio decrease.\n                              * Special: value 0 means \"automatically determine hashRateLog\". */\n\n    /* frame parameters */\n    ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)\n                              * Content size must be known at the beginning of compression.\n                              * This is automatically the case when using ZSTD_compress2(),\n                              * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */\n    ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */\n    ZSTD_c_dictIDFlag=202,   /* When applicable, dictionary's ID is written into frame header (default:1) */\n\n    /* multi-threading parameters */\n    /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD).\n     * They return an error otherwise. */\n    ZSTD_c_nbWorkers=400,    /* Select how many threads will be spawned to compress in parallel.\n                              * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() :\n                              * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller,\n                              * while compression work is performed in parallel, within worker threads.\n                              * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end :\n                              *  in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call).\n                              * More workers improve speed, but also increase memory usage.\n                              * Default value is `0`, aka \"single-threaded mode\" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */\n    ZSTD_c_jobSize=401,      /* Size of a compression job. This value is enforced only when nbWorkers >= 1.\n                              * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads.\n                              * 0 means default, which is dynamically determined based on compression parameters.\n                              * Job size must be a minimum of overlap size, or 1 MB, whichever is largest.\n                              * The minimum size is automatically and transparently enforced. */\n    ZSTD_c_overlapLog=402,   /* Control the overlap size, as a fraction of window size.\n                              * The overlap size is an amount of data reloaded from previous job at the beginning of a new job.\n                              * It helps preserve compression ratio, while each job is compressed in parallel.\n                              * This value is enforced only when nbWorkers >= 1.\n                              * Larger values increase compression ratio, but decrease speed.\n                              * Possible values range from 0 to 9 :\n                              * - 0 means \"default\" : value will be determined by the library, depending on strategy\n                              * - 1 means \"no overlap\"\n                              * - 9 means \"full overlap\", using a full window size.\n                              * Each intermediate rank increases/decreases load size by a factor 2 :\n                              * 9: full window;  8: w/2;  7: w/4;  6: w/8;  5:w/16;  4: w/32;  3:w/64;  2:w/128;  1:no overlap;  0:default\n                              * default value varies between 6 and 9, depending on strategy */\n\n    /* note : additional experimental parameters are also available\n     * within the experimental section of the API.\n     * At the time of this writing, they include :\n     * ZSTD_c_rsyncable\n     * ZSTD_c_format\n     * ZSTD_c_forceMaxWindow\n     * ZSTD_c_forceAttachDict\n     * ZSTD_c_literalCompressionMode\n     * ZSTD_c_targetCBlockSize\n     * ZSTD_c_srcSizeHint\n     * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.\n     * note : never ever use experimentalParam? names directly;\n     *        also, the enums values themselves are unstable and can still change.\n     */\n     ZSTD_c_experimentalParam1=500,\n     ZSTD_c_experimentalParam2=10,\n     ZSTD_c_experimentalParam3=1000,\n     ZSTD_c_experimentalParam4=1001,\n     ZSTD_c_experimentalParam5=1002,\n     ZSTD_c_experimentalParam6=1003,\n     ZSTD_c_experimentalParam7=1004\n} ZSTD_cParameter;\n\ntypedef struct {\n    size_t error;\n    int lowerBound;\n    int upperBound;\n} ZSTD_bounds;\n\n/*! ZSTD_cParam_getBounds() :\n *  All parameters must belong to an interval with lower and upper bounds,\n *  otherwise they will either trigger an error or be automatically clamped.\n * @return : a structure, ZSTD_bounds, which contains\n *         - an error status field, which must be tested using ZSTD_isError()\n *         - lower and upper bounds, both inclusive\n */\nZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam);\n\n/*! ZSTD_CCtx_setParameter() :\n *  Set one compression parameter, selected by enum ZSTD_cParameter.\n *  All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds().\n *  Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).\n *  Setting a parameter is generally only possible during frame initialization (before starting compression).\n *  Exception : when using multi-threading mode (nbWorkers >= 1),\n *              the following parameters can be updated _during_ compression (within same frame):\n *              => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.\n *              new parameters will be active for next job only (after a flush()).\n * @return : an error code (which can be tested using ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value);\n\n/*! ZSTD_CCtx_setPledgedSrcSize() :\n *  Total input data size to be compressed as a single frame.\n *  Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag.\n *  This value will also be controlled at end of frame, and trigger an error if not respected.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame.\n *           In order to mean \"unknown content size\", pass constant ZSTD_CONTENTSIZE_UNKNOWN.\n *           ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame.\n *  Note 2 : pledgedSrcSize is only valid once, for the next frame.\n *           It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.\n *  Note 3 : Whenever all input data is provided and consumed in a single round,\n *           for example with ZSTD_compress2(),\n *           or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),\n *           this value is automatically overridden by srcSize instead.\n */\nZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);\n\ntypedef enum {\n    ZSTD_reset_session_only = 1,\n    ZSTD_reset_parameters = 2,\n    ZSTD_reset_session_and_parameters = 3\n} ZSTD_ResetDirective;\n\n/*! ZSTD_CCtx_reset() :\n *  There are 2 different things that can be reset, independently or jointly :\n *  - The session : will stop compressing current frame, and make CCtx ready to start a new one.\n *                  Useful after an error, or to interrupt any ongoing compression.\n *                  Any internal data not yet flushed is cancelled.\n *                  Compression parameters and dictionary remain unchanged.\n *                  They will be used to compress next frame.\n *                  Resetting session never fails.\n *  - The parameters : changes all parameters back to \"default\".\n *                  This removes any reference to any dictionary too.\n *                  Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)\n *                  otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())\n *  - Both : similar to resetting the session, followed by resetting parameters.\n */\nZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);\n\n/*! ZSTD_compress2() :\n *  Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.\n *  ZSTD_compress2() always starts a new frame.\n *  Should cctx hold data from a previously unfinished frame, everything about it is forgotten.\n *  - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()\n *  - The function is always blocking, returns when compression is completed.\n *  Hint : compression runs faster if `dstCapacity` >=  `ZSTD_compressBound(srcSize)`.\n * @return : compressed size written into `dst` (<= `dstCapacity),\n *           or an error code if it fails (which can be tested using ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,\n                                   void* dst, size_t dstCapacity,\n                             const void* src, size_t srcSize);\n\n\n/***************************************\n*  Advanced decompression API\n***************************************/\n\n/* The advanced API pushes parameters one by one into an existing DCtx context.\n * Parameters are sticky, and remain valid for all following frames\n * using the same DCtx context.\n * It's possible to reset parameters to default values using ZSTD_DCtx_reset().\n * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().\n *        Therefore, no new decompression function is necessary.\n */\n\ntypedef enum {\n\n    ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which\n                              * the streaming API will refuse to allocate memory buffer\n                              * in order to protect the host from unreasonable memory requirements.\n                              * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.\n                              * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT).\n                              * Special: value 0 means \"use default maximum windowLog\". */\n\n    /* note : additional experimental parameters are also available\n     * within the experimental section of the API.\n     * At the time of this writing, they include :\n     * ZSTD_d_format\n     * ZSTD_d_stableOutBuffer\n     * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.\n     * note : never ever use experimentalParam? names directly\n     */\n     ZSTD_d_experimentalParam1=1000,\n     ZSTD_d_experimentalParam2=1001\n\n} ZSTD_dParameter;\n\n/*! ZSTD_dParam_getBounds() :\n *  All parameters must belong to an interval with lower and upper bounds,\n *  otherwise they will either trigger an error or be automatically clamped.\n * @return : a structure, ZSTD_bounds, which contains\n *         - an error status field, which must be tested using ZSTD_isError()\n *         - both lower and upper bounds, inclusive\n */\nZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);\n\n/*! ZSTD_DCtx_setParameter() :\n *  Set one compression parameter, selected by enum ZSTD_dParameter.\n *  All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().\n *  Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).\n *  Setting a parameter is only possible during frame initialization (before starting decompression).\n * @return : 0, or an error code (which can be tested using ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);\n\n/*! ZSTD_DCtx_reset() :\n *  Return a DCtx to clean state.\n *  Session and parameters can be reset jointly or separately.\n *  Parameters can only be reset when no active frame is being decompressed.\n * @return : 0, or an error code, which can be tested with ZSTD_isError()\n */\nZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);\n\n\n/****************************\n*  Streaming\n****************************/\n\ntypedef struct ZSTD_inBuffer_s {\n  const void* src;    /**< start of input buffer */\n  size_t size;        /**< size of input buffer */\n  size_t pos;         /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */\n} ZSTD_inBuffer;\n\ntypedef struct ZSTD_outBuffer_s {\n  void*  dst;         /**< start of output buffer */\n  size_t size;        /**< size of output buffer */\n  size_t pos;         /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */\n} ZSTD_outBuffer;\n\n\n\n/*-***********************************************************************\n*  Streaming compression - HowTo\n*\n*  A ZSTD_CStream object is required to track streaming operation.\n*  Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.\n*  ZSTD_CStream objects can be reused multiple times on consecutive compression operations.\n*  It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory.\n*\n*  For parallel execution, use one separate ZSTD_CStream per thread.\n*\n*  note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.\n*\n*  Parameters are sticky : when starting a new compression on the same context,\n*  it will re-use the same sticky parameters as previous compression session.\n*  When in doubt, it's recommended to fully initialize the context before usage.\n*  Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(),\n*  ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to\n*  set more specific parameters, the pledged source size, or load a dictionary.\n*\n*  Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to\n*  consume input stream. The function will automatically update both `pos`\n*  fields within `input` and `output`.\n*  Note that the function may not consume the entire input, for example, because\n*  the output buffer is already full, in which case `input.pos < input.size`.\n*  The caller must check if input has been entirely consumed.\n*  If not, the caller must make some room to receive more compressed data,\n*  and then present again remaining input data.\n*  note: ZSTD_e_continue is guaranteed to make some forward progress when called,\n*        but doesn't guarantee maximal forward progress. This is especially relevant\n*        when compressing with multiple threads. The call won't block if it can\n*        consume some input, but if it can't it will wait for some, but not all,\n*        output to be flushed.\n* @return : provides a minimum amount of data remaining to be flushed from internal buffers\n*           or an error code, which can be tested using ZSTD_isError().\n*\n*  At any moment, it's possible to flush whatever data might remain stuck within internal buffer,\n*  using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated.\n*  Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0).\n*  In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush.\n*  You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the\n*  operation.\n*  note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will\n*        block until the flush is complete or the output buffer is full.\n*  @return : 0 if internal buffers are entirely flushed,\n*            >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),\n*            or an error code, which can be tested using ZSTD_isError().\n*\n*  Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame.\n*  It will perform a flush and write frame epilogue.\n*  The epilogue is required for decoders to consider a frame completed.\n*  flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush.\n*  You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to\n*  start a new frame.\n*  note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will\n*        block until the flush is complete or the output buffer is full.\n*  @return : 0 if frame fully completed and fully flushed,\n*            >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),\n*            or an error code, which can be tested using ZSTD_isError().\n*\n* *******************************************************************/\n\ntypedef ZSTD_CCtx ZSTD_CStream;  /**< CCtx and CStream are now effectively same object (>= v1.3.0) */\n                                 /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */\n/*===== ZSTD_CStream management functions =====*/\nZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);\nZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);\n\n/*===== Streaming compression functions =====*/\ntypedef enum {\n    ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */\n    ZSTD_e_flush=1,    /* flush any data provided so far,\n                        * it creates (at least) one new block, that can be decoded immediately on reception;\n                        * frame will continue: any future data can still reference previously compressed data, improving compression.\n                        * note : multithreaded compression will block to flush as much output as possible. */\n    ZSTD_e_end=2       /* flush any remaining data _and_ close current frame.\n                        * note that frame is only closed after compressed data is fully flushed (return value == 0).\n                        * After that point, any additional data starts a new frame.\n                        * note : each frame is independent (does not reference any content from previous frame).\n                        : note : multithreaded compression will block to flush as much output as possible. */\n} ZSTD_EndDirective;\n\n/*! ZSTD_compressStream2() :\n *  Behaves about the same as ZSTD_compressStream, with additional control on end directive.\n *  - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()\n *  - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)\n *  - output->pos must be <= dstCapacity, input->pos must be <= srcSize\n *  - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.\n *  - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.\n *  - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,\n *                                                  and then immediately returns, just indicating that there is some data remaining to be flushed.\n *                                                  The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.\n *  - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking.\n *  - @return provides a minimum amount of data remaining to be flushed from internal buffers\n *            or an error code, which can be tested using ZSTD_isError().\n *            if @return != 0, flush is not fully completed, there is still some data left within internal buffers.\n *            This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.\n *            For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.\n *  - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),\n *            only ZSTD_e_end or ZSTD_e_flush operations are allowed.\n *            Before starting a new compression job, or changing compression parameters,\n *            it is required to fully flush internal buffers.\n */\nZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,\n                                         ZSTD_outBuffer* output,\n                                         ZSTD_inBuffer* input,\n                                         ZSTD_EndDirective endOp);\n\n\n/* These buffer sizes are softly recommended.\n * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output.\n * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(),\n * reducing the amount of memory shuffling and buffering, resulting in minor performance savings.\n *\n * However, note that these recommendations are from the perspective of a C caller program.\n * If the streaming interface is invoked from some other language,\n * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo,\n * a major performance rule is to reduce crossing such interface to an absolute minimum.\n * It's not rare that performance ends being spent more into the interface, rather than compression itself.\n * In which cases, prefer using large buffers, as large as practical,\n * for both input and output, to reduce the nb of roundtrips.\n */\nZSTDLIB_API size_t ZSTD_CStreamInSize(void);    /**< recommended size for input buffer */\nZSTDLIB_API size_t ZSTD_CStreamOutSize(void);   /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */\n\n\n/* *****************************************************************************\n * This following is a legacy streaming API.\n * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2().\n * It is redundant, but remains fully supported.\n * Advanced parameters and dictionary compression can only be used through the\n * new API.\n ******************************************************************************/\n\n/*!\n * Equivalent to:\n *\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)\n *     ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);\n */\nZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);\n/*!\n * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue).\n * NOTE: The return value is different. ZSTD_compressStream() returns a hint for\n * the next read size (if non-zero and not an error). ZSTD_compressStream2()\n * returns the minimum nb of bytes left to flush (if non-zero and not an error).\n */\nZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);\n/*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */\nZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);\n/*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */\nZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);\n\n\n/*-***************************************************************************\n*  Streaming decompression - HowTo\n*\n*  A ZSTD_DStream object is required to track streaming operations.\n*  Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.\n*  ZSTD_DStream objects can be re-used multiple times.\n*\n*  Use ZSTD_initDStream() to start a new decompression operation.\n* @return : recommended first input size\n*  Alternatively, use advanced API to set specific properties.\n*\n*  Use ZSTD_decompressStream() repetitively to consume your input.\n*  The function will update both `pos` fields.\n*  If `input.pos < input.size`, some input has not been consumed.\n*  It's up to the caller to present again remaining data.\n*  The function tries to flush all data decoded immediately, respecting output buffer size.\n*  If `output.pos < output.size`, decoder has flushed everything it could.\n*  But if `output.pos == output.size`, there might be some data left within internal buffers.,\n*  In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.\n*  Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.\n* @return : 0 when a frame is completely decoded and fully flushed,\n*        or an error code, which can be tested using ZSTD_isError(),\n*        or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :\n*                                the return value is a suggested next input size (just a hint for better latency)\n*                                that will never request more than the remaining frame size.\n* *******************************************************************************/\n\ntypedef ZSTD_DCtx ZSTD_DStream;  /**< DCtx and DStream are now effectively same object (>= v1.3.0) */\n                                 /* For compatibility with versions <= v1.2.0, prefer differentiating them. */\n/*===== ZSTD_DStream management functions =====*/\nZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);\nZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);\n\n/*===== Streaming decompression functions =====*/\n\n/* This function is redundant with the advanced API and equivalent to:\n *\n *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);\n *     ZSTD_DCtx_refDDict(zds, NULL);\n */\nZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);\n\nZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);\n\nZSTDLIB_API size_t ZSTD_DStreamInSize(void);    /*!< recommended size for input buffer */\nZSTDLIB_API size_t ZSTD_DStreamOutSize(void);   /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */\n\n\n/**************************\n*  Simple dictionary API\n***************************/\n/*! ZSTD_compress_usingDict() :\n *  Compression at an explicit compression level using a Dictionary.\n *  A dictionary can be any arbitrary data segment (also called a prefix),\n *  or a buffer with specified information (see dictBuilder/zdict.h).\n *  Note : This function loads the dictionary, resulting in significant startup delay.\n *         It's intended for a dictionary used only once.\n *  Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */\nZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,\n                                           void* dst, size_t dstCapacity,\n                                     const void* src, size_t srcSize,\n                                     const void* dict,size_t dictSize,\n                                           int compressionLevel);\n\n/*! ZSTD_decompress_usingDict() :\n *  Decompression using a known Dictionary.\n *  Dictionary must be identical to the one used during compression.\n *  Note : This function loads the dictionary, resulting in significant startup delay.\n *         It's intended for a dictionary used only once.\n *  Note : When `dict == NULL || dictSize < 8` no dictionary is used. */\nZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,\n                                             void* dst, size_t dstCapacity,\n                                       const void* src, size_t srcSize,\n                                       const void* dict,size_t dictSize);\n\n\n/***********************************\n *  Bulk processing dictionary API\n **********************************/\ntypedef struct ZSTD_CDict_s ZSTD_CDict;\n\n/*! ZSTD_createCDict() :\n *  When compressing multiple messages or blocks using the same dictionary,\n *  it's recommended to digest the dictionary only once, since it's a costly operation.\n *  ZSTD_createCDict() will create a state from digesting a dictionary.\n *  The resulting state can be used for future compression operations with very limited startup cost.\n *  ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.\n * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict.\n *  Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content.\n *  Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer,\n *      in which case the only thing that it transports is the @compressionLevel.\n *      This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively,\n *      expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */\nZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,\n                                         int compressionLevel);\n\n/*! ZSTD_freeCDict() :\n *  Function frees memory allocated by ZSTD_createCDict(). */\nZSTDLIB_API size_t      ZSTD_freeCDict(ZSTD_CDict* CDict);\n\n/*! ZSTD_compress_usingCDict() :\n *  Compression using a digested Dictionary.\n *  Recommended when same dictionary is used multiple times.\n *  Note : compression level is _decided at dictionary creation time_,\n *     and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */\nZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,\n                                            void* dst, size_t dstCapacity,\n                                      const void* src, size_t srcSize,\n                                      const ZSTD_CDict* cdict);\n\n\ntypedef struct ZSTD_DDict_s ZSTD_DDict;\n\n/*! ZSTD_createDDict() :\n *  Create a digested dictionary, ready to start decompression operation without startup delay.\n *  dictBuffer can be released after DDict creation, as its content is copied inside DDict. */\nZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);\n\n/*! ZSTD_freeDDict() :\n *  Function frees memory allocated with ZSTD_createDDict() */\nZSTDLIB_API size_t      ZSTD_freeDDict(ZSTD_DDict* ddict);\n\n/*! ZSTD_decompress_usingDDict() :\n *  Decompression using a digested Dictionary.\n *  Recommended when same dictionary is used multiple times. */\nZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,\n                                              void* dst, size_t dstCapacity,\n                                        const void* src, size_t srcSize,\n                                        const ZSTD_DDict* ddict);\n\n\n/********************************\n *  Dictionary helper functions\n *******************************/\n\n/*! ZSTD_getDictID_fromDict() :\n *  Provides the dictID stored within dictionary.\n *  if @return == 0, the dictionary is not conformant with Zstandard specification.\n *  It can still be loaded, but as a content-only dictionary. */\nZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);\n\n/*! ZSTD_getDictID_fromDDict() :\n *  Provides the dictID of the dictionary loaded into `ddict`.\n *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.\n *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */\nZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);\n\n/*! ZSTD_getDictID_fromFrame() :\n *  Provides the dictID required to decompressed the frame stored within `src`.\n *  If @return == 0, the dictID could not be decoded.\n *  This could for one of the following reasons :\n *  - The frame does not require a dictionary to be decoded (most common case).\n *  - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.\n *    Note : this use case also happens when using a non-conformant dictionary.\n *  - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).\n *  - This is not a Zstandard frame.\n *  When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */\nZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);\n\n\n/*******************************************************************************\n * Advanced dictionary and prefix API\n *\n * This API allows dictionaries to be used with ZSTD_compress2(),\n * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and\n * only reset with the context is reset with ZSTD_reset_parameters or\n * ZSTD_reset_session_and_parameters. Prefixes are single-use.\n ******************************************************************************/\n\n\n/*! ZSTD_CCtx_loadDictionary() :\n *  Create an internal CDict from `dict` buffer.\n *  Decompression will have to use same dictionary.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary,\n *           meaning \"return to no-dictionary mode\".\n *  Note 1 : Dictionary is sticky, it will be used for all future compressed frames.\n *           To return to \"no-dictionary\" situation, load a NULL dictionary (or reset parameters).\n *  Note 2 : Loading a dictionary involves building tables.\n *           It's also a CPU consuming operation, with non-negligible impact on latency.\n *           Tables are dependent on compression parameters, and for this reason,\n *           compression parameters can no longer be changed after loading a dictionary.\n *  Note 3 :`dict` content will be copied internally.\n *           Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead.\n *           In such a case, dictionary buffer must outlive its users.\n *  Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()\n *           to precisely select how dictionary content must be interpreted. */\nZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);\n\n/*! ZSTD_CCtx_refCDict() :\n *  Reference a prepared dictionary, to be used for all next compressed frames.\n *  Note that compression parameters are enforced from within CDict,\n *  and supersede any compression parameter previously set within CCtx.\n *  The parameters ignored are labled as \"superseded-by-cdict\" in the ZSTD_cParameter enum docs.\n *  The ignored parameters will be used again if the CCtx is returned to no-dictionary mode.\n *  The dictionary will remain valid for future compressed frames using same CCtx.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Special : Referencing a NULL CDict means \"return to no-dictionary mode\".\n *  Note 1 : Currently, only one dictionary can be managed.\n *           Referencing a new dictionary effectively \"discards\" any previous one.\n *  Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */\nZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);\n\n/*! ZSTD_CCtx_refPrefix() :\n *  Reference a prefix (single-usage dictionary) for next compressed frame.\n *  A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end).\n *  Decompression will need same prefix to properly regenerate data.\n *  Compressing with a prefix is similar in outcome as performing a diff and compressing it,\n *  but performs much faster, especially during decompression (compression speed is tunable with compression level).\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary\n *  Note 1 : Prefix buffer is referenced. It **must** outlive compression.\n *           Its content must remain unmodified during compression.\n *  Note 2 : If the intention is to diff some large src data blob with some prior version of itself,\n *           ensure that the window size is large enough to contain the entire source.\n *           See ZSTD_c_windowLog.\n *  Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.\n *           It's a CPU consuming operation, with non-negligible impact on latency.\n *           If there is a need to use the same prefix multiple times, consider loadDictionary instead.\n *  Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent).\n *           Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */\nZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,\n                                 const void* prefix, size_t prefixSize);\n\n/*! ZSTD_DCtx_loadDictionary() :\n *  Create an internal DDict from dict buffer,\n *  to be used to decompress next frames.\n *  The dictionary remains valid for all future frames, until explicitly invalidated.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,\n *            meaning \"return to no-dictionary mode\".\n *  Note 1 : Loading a dictionary involves building tables,\n *           which has a non-negligible impact on CPU usage and latency.\n *           It's recommended to \"load once, use many times\", to amortize the cost\n *  Note 2 :`dict` content will be copied internally, so `dict` can be released after loading.\n *           Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead.\n *  Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of\n *           how dictionary content is loaded and interpreted.\n */\nZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);\n\n/*! ZSTD_DCtx_refDDict() :\n *  Reference a prepared dictionary, to be used to decompress next frames.\n *  The dictionary remains active for decompression of future frames using same DCtx.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Note 1 : Currently, only one dictionary can be managed.\n *           Referencing a new dictionary effectively \"discards\" any previous one.\n *  Special: referencing a NULL DDict means \"return to no-dictionary mode\".\n *  Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.\n */\nZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);\n\n/*! ZSTD_DCtx_refPrefix() :\n *  Reference a prefix (single-usage dictionary) to decompress next frame.\n *  This is the reverse operation of ZSTD_CCtx_refPrefix(),\n *  and must use the same prefix as the one used during compression.\n *  Prefix is **only used once**. Reference is discarded at end of frame.\n *  End of frame is reached when ZSTD_decompressStream() returns 0.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n *  Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary\n *  Note 2 : Prefix buffer is referenced. It **must** outlive decompression.\n *           Prefix buffer must remain unmodified up to the end of frame,\n *           reached when ZSTD_decompressStream() returns 0.\n *  Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent).\n *           Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section)\n *  Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.\n *           A full dictionary is more costly, as it requires building tables.\n */\nZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,\n                                 const void* prefix, size_t prefixSize);\n\n/* ===   Memory management   === */\n\n/*! ZSTD_sizeof_*() :\n *  These functions give the _current_ memory usage of selected object.\n *  Note that object memory usage can evolve (increase or decrease) over time. */\nZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);\nZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);\nZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);\nZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);\nZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);\nZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);\n\n#endif  /* ZSTD_H_235446 */\n\n\n/* **************************************************************************************\n *   ADVANCED AND EXPERIMENTAL FUNCTIONS\n ****************************************************************************************\n * The definitions in the following section are considered experimental.\n * They are provided for advanced scenarios.\n * They should never be used with a dynamic library, as prototypes may change in the future.\n * Use them only in association with static linking.\n * ***************************************************************************************/\n\n#if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)\n#define ZSTD_H_ZSTD_STATIC_LINKING_ONLY\n\n/****************************************************************************************\n *   experimental API (static linking only)\n ****************************************************************************************\n * The following symbols and constants\n * are not planned to join \"stable API\" status in the near future.\n * They can still change in future versions.\n * Some of them are planned to remain in the static_only section indefinitely.\n * Some of them might be removed in the future (especially when redundant with existing stable functions)\n * ***************************************************************************************/\n\n#define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1)   /* minimum input size required to query frame header size */\n#define ZSTD_FRAMEHEADERSIZE_MIN(format)    ((format) == ZSTD_f_zstd1 ? 6 : 2)\n#define ZSTD_FRAMEHEADERSIZE_MAX   18   /* can be useful for static allocation */\n#define ZSTD_SKIPPABLEHEADERSIZE    8\n\n/* compression parameter bounds */\n#define ZSTD_WINDOWLOG_MAX_32    30\n#define ZSTD_WINDOWLOG_MAX_64    31\n#define ZSTD_WINDOWLOG_MAX     ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))\n#define ZSTD_WINDOWLOG_MIN       10\n#define ZSTD_HASHLOG_MAX       ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)\n#define ZSTD_HASHLOG_MIN          6\n#define ZSTD_CHAINLOG_MAX_32     29\n#define ZSTD_CHAINLOG_MAX_64     30\n#define ZSTD_CHAINLOG_MAX      ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))\n#define ZSTD_CHAINLOG_MIN        ZSTD_HASHLOG_MIN\n#define ZSTD_SEARCHLOG_MAX      (ZSTD_WINDOWLOG_MAX-1)\n#define ZSTD_SEARCHLOG_MIN        1\n#define ZSTD_MINMATCH_MAX         7   /* only for ZSTD_fast, other strategies are limited to 6 */\n#define ZSTD_MINMATCH_MIN         3   /* only for ZSTD_btopt+, faster strategies are limited to 4 */\n#define ZSTD_TARGETLENGTH_MAX    ZSTD_BLOCKSIZE_MAX\n#define ZSTD_TARGETLENGTH_MIN     0   /* note : comparing this constant to an unsigned results in a tautological test */\n#define ZSTD_STRATEGY_MIN        ZSTD_fast\n#define ZSTD_STRATEGY_MAX        ZSTD_btultra2\n\n\n#define ZSTD_OVERLAPLOG_MIN       0\n#define ZSTD_OVERLAPLOG_MAX       9\n\n#define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27   /* by default, the streaming decoder will refuse any frame\n                                           * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,\n                                           * to preserve host's memory from unreasonable requirements.\n                                           * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).\n                                           * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */\n\n\n/* LDM parameter bounds */\n#define ZSTD_LDM_HASHLOG_MIN      ZSTD_HASHLOG_MIN\n#define ZSTD_LDM_HASHLOG_MAX      ZSTD_HASHLOG_MAX\n#define ZSTD_LDM_MINMATCH_MIN        4\n#define ZSTD_LDM_MINMATCH_MAX     4096\n#define ZSTD_LDM_BUCKETSIZELOG_MIN   1\n#define ZSTD_LDM_BUCKETSIZELOG_MAX   8\n#define ZSTD_LDM_HASHRATELOG_MIN     0\n#define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)\n\n/* Advanced parameter bounds */\n#define ZSTD_TARGETCBLOCKSIZE_MIN   64\n#define ZSTD_TARGETCBLOCKSIZE_MAX   ZSTD_BLOCKSIZE_MAX\n#define ZSTD_SRCSIZEHINT_MIN        0\n#define ZSTD_SRCSIZEHINT_MAX        INT_MAX\n\n/* internal */\n#define ZSTD_HASHLOG3_MAX           17\n\n\n/* ---  Advanced types  --- */\n\ntypedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;\n\ntypedef struct {\n    unsigned int matchPos; /* Match pos in dst */\n    /* If seqDef.offset > 3, then this is seqDef.offset - 3\n     * If seqDef.offset < 3, then this is the corresponding repeat offset\n     * But if seqDef.offset < 3 and litLength == 0, this is the\n     *   repeat offset before the corresponding repeat offset\n     * And if seqDef.offset == 3 and litLength == 0, this is the\n     *   most recent repeat offset - 1\n     */\n    unsigned int offset;\n    unsigned int litLength; /* Literal length */\n    unsigned int matchLength; /* Match length */\n    /* 0 when seq not rep and seqDef.offset otherwise\n     * when litLength == 0 this will be <= 4, otherwise <= 3 like normal\n     */\n    unsigned int rep;\n} ZSTD_Sequence;\n\ntypedef struct {\n    unsigned windowLog;       /**< largest match distance : larger == more compression, more memory needed during decompression */\n    unsigned chainLog;        /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */\n    unsigned hashLog;         /**< dispatch table : larger == faster, more memory */\n    unsigned searchLog;       /**< nb of searches : larger == more compression, slower */\n    unsigned minMatch;        /**< match length searched : larger == faster decompression, sometimes less compression */\n    unsigned targetLength;    /**< acceptable match size for optimal parser (only) : larger == more compression, slower */\n    ZSTD_strategy strategy;   /**< see ZSTD_strategy definition above */\n} ZSTD_compressionParameters;\n\ntypedef struct {\n    int contentSizeFlag; /**< 1: content size will be in frame header (when known) */\n    int checksumFlag;    /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */\n    int noDictIDFlag;    /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */\n} ZSTD_frameParameters;\n\ntypedef struct {\n    ZSTD_compressionParameters cParams;\n    ZSTD_frameParameters fParams;\n} ZSTD_parameters;\n\ntypedef enum {\n    ZSTD_dct_auto = 0,       /* dictionary is \"full\" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is \"rawContent\" */\n    ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */\n    ZSTD_dct_fullDict = 2    /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */\n} ZSTD_dictContentType_e;\n\ntypedef enum {\n    ZSTD_dlm_byCopy = 0,  /**< Copy dictionary content internally */\n    ZSTD_dlm_byRef = 1    /**< Reference dictionary content -- the dictionary buffer must outlive its users. */\n} ZSTD_dictLoadMethod_e;\n\ntypedef enum {\n    ZSTD_f_zstd1 = 0,           /* zstd frame format, specified in zstd_compression_format.md (default) */\n    ZSTD_f_zstd1_magicless = 1  /* Variant of zstd frame format, without initial 4-bytes magic number.\n                                 * Useful to save 4 bytes per generated frame.\n                                 * Decoder cannot recognise automatically this format, requiring this instruction. */\n} ZSTD_format_e;\n\ntypedef enum {\n    /* Note: this enum and the behavior it controls are effectively internal\n     * implementation details of the compressor. They are expected to continue\n     * to evolve and should be considered only in the context of extremely\n     * advanced performance tuning.\n     *\n     * Zstd currently supports the use of a CDict in three ways:\n     *\n     * - The contents of the CDict can be copied into the working context. This\n     *   means that the compression can search both the dictionary and input\n     *   while operating on a single set of internal tables. This makes\n     *   the compression faster per-byte of input. However, the initial copy of\n     *   the CDict's tables incurs a fixed cost at the beginning of the\n     *   compression. For small compressions (< 8 KB), that copy can dominate\n     *   the cost of the compression.\n     *\n     * - The CDict's tables can be used in-place. In this model, compression is\n     *   slower per input byte, because the compressor has to search two sets of\n     *   tables. However, this model incurs no start-up cost (as long as the\n     *   working context's tables can be reused). For small inputs, this can be\n     *   faster than copying the CDict's tables.\n     *\n     * - The CDict's tables are not used at all, and instead we use the working\n     *   context alone to reload the dictionary and use params based on the source\n     *   size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict().\n     *   This method is effective when the dictionary sizes are very small relative\n     *   to the input size, and the input size is fairly large to begin with.\n     *\n     * Zstd has a simple internal heuristic that selects which strategy to use\n     * at the beginning of a compression. However, if experimentation shows that\n     * Zstd is making poor choices, it is possible to override that choice with\n     * this enum.\n     */\n    ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */\n    ZSTD_dictForceAttach   = 1, /* Never copy the dictionary. */\n    ZSTD_dictForceCopy     = 2, /* Always copy the dictionary. */\n    ZSTD_dictForceLoad     = 3  /* Always reload the dictionary */\n} ZSTD_dictAttachPref_e;\n\ntypedef enum {\n  ZSTD_lcm_auto = 0,          /**< Automatically determine the compression mode based on the compression level.\n                               *   Negative compression levels will be uncompressed, and positive compression\n                               *   levels will be compressed. */\n  ZSTD_lcm_huffman = 1,       /**< Always attempt Huffman compression. Uncompressed literals will still be\n                               *   emitted if Huffman compression is not profitable. */\n  ZSTD_lcm_uncompressed = 2   /**< Always emit uncompressed literals. */\n} ZSTD_literalCompressionMode_e;\n\n\n/***************************************\n*  Frame size functions\n***************************************/\n\n/*! ZSTD_findDecompressedSize() :\n *  `src` should point to the start of a series of ZSTD encoded and/or skippable frames\n *  `srcSize` must be the _exact_ size of this series\n *       (i.e. there should be a frame boundary at `src + srcSize`)\n *  @return : - decompressed size of all data in all successive frames\n *            - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN\n *            - if an error occurred: ZSTD_CONTENTSIZE_ERROR\n *\n *   note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.\n *            When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.\n *            In which case, it's necessary to use streaming mode to decompress data.\n *   note 2 : decompressed size is always present when compression is done with ZSTD_compress()\n *   note 3 : decompressed size can be very large (64-bits value),\n *            potentially larger than what local system can handle as a single memory segment.\n *            In which case, it's necessary to use streaming mode to decompress data.\n *   note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.\n *            Always ensure result fits within application's authorized limits.\n *            Each application can set its own limits.\n *   note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to\n *            read each contained frame header.  This is fast as most of the data is skipped,\n *            however it does mean that all frame data must be present and valid. */\nZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);\n\n/*! ZSTD_decompressBound() :\n *  `src` should point to the start of a series of ZSTD encoded and/or skippable frames\n *  `srcSize` must be the _exact_ size of this series\n *       (i.e. there should be a frame boundary at `src + srcSize`)\n *  @return : - upper-bound for the decompressed size of all data in all successive frames\n *            - if an error occured: ZSTD_CONTENTSIZE_ERROR\n *\n *  note 1  : an error can occur if `src` contains an invalid or incorrectly formatted frame.\n *  note 2  : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`.\n *            in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value.\n *  note 3  : when the decompressed size field isn't available, the upper-bound for that frame is calculated by:\n *              upper-bound = # blocks * min(128 KB, Window_Size)\n */\nZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize);\n\n/*! ZSTD_frameHeaderSize() :\n *  srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.\n * @return : size of the Frame Header,\n *           or an error code (if srcSize is too small) */\nZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);\n\n/*! ZSTD_getSequences() :\n * Extract sequences from the sequence store\n * zc can be used to insert custom compression params.\n * This function invokes ZSTD_compress2\n * @return : number of sequences extracted\n */\nZSTDLIB_API size_t ZSTD_getSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs,\n    size_t outSeqsSize, const void* src, size_t srcSize);\n\n\n/***************************************\n*  Memory management\n***************************************/\n\n/*! ZSTD_estimate*() :\n *  These functions make it possible to estimate memory usage\n *  of a future {D,C}Ctx, before its creation.\n *\n *  ZSTD_estimateCCtxSize() will provide a memory budget large enough\n *  for any compression level up to selected one.\n *  Note : Unlike ZSTD_estimateCStreamSize*(), this estimate\n *         does not include space for a window buffer.\n *         Therefore, the estimation is only guaranteed for single-shot compressions, not streaming.\n *  The estimate will assume the input may be arbitrarily large,\n *  which is the worst case.\n *\n *  When srcSize can be bound by a known and rather \"small\" value,\n *  this fact can be used to provide a tighter estimation\n *  because the CCtx compression context will need less memory.\n *  This tighter estimation can be provided by more advanced functions\n *  ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(),\n *  and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter().\n *  Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits.\n *\n *  Note 2 : only single-threaded compression is supported.\n *  ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1.\n */\nZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);\nZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);\nZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);\nZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);\n\n/*! ZSTD_estimateCStreamSize() :\n *  ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.\n *  It will also consider src size to be arbitrarily \"large\", which is worst case.\n *  If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.\n *  ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.\n *  ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.\n *  Note : CStream size estimation is only correct for single-threaded compression.\n *  ZSTD_DStream memory budget depends on window Size.\n *  This information can be passed manually, using ZSTD_estimateDStreamSize,\n *  or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();\n *  Note : if streaming is init with function ZSTD_init?Stream_usingDict(),\n *         an internal ?Dict will be created, which additional size is not estimated here.\n *         In this case, get total size by adding ZSTD_estimate?DictSize */\nZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);\nZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);\nZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);\nZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);\nZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);\n\n/*! ZSTD_estimate?DictSize() :\n *  ZSTD_estimateCDictSize() will bet that src size is relatively \"small\", and content is copied, like ZSTD_createCDict().\n *  ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().\n *  Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.\n */\nZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);\nZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);\nZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);\n\n/*! ZSTD_initStatic*() :\n *  Initialize an object using a pre-allocated fixed-size buffer.\n *  workspace: The memory area to emplace the object into.\n *             Provided pointer *must be 8-bytes aligned*.\n *             Buffer must outlive object.\n *  workspaceSize: Use ZSTD_estimate*Size() to determine\n *                 how large workspace must be to support target scenario.\n * @return : pointer to object (same address as workspace, just different type),\n *           or NULL if error (size too small, incorrect alignment, etc.)\n *  Note : zstd will never resize nor malloc() when using a static buffer.\n *         If the object requires more memory than available,\n *         zstd will just error out (typically ZSTD_error_memory_allocation).\n *  Note 2 : there is no corresponding \"free\" function.\n *           Since workspace is allocated externally, it must be freed externally too.\n *  Note 3 : cParams : use ZSTD_getCParams() to convert a compression level\n *           into its associated cParams.\n *  Limitation 1 : currently not compatible with internal dictionary creation, triggered by\n *                 ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().\n *  Limitation 2 : static cctx currently not compatible with multi-threading.\n *  Limitation 3 : static dctx is incompatible with legacy support.\n */\nZSTDLIB_API ZSTD_CCtx*    ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);\nZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize);    /**< same as ZSTD_initStaticCCtx() */\n\nZSTDLIB_API ZSTD_DCtx*    ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);\nZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize);    /**< same as ZSTD_initStaticDCtx() */\n\nZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(\n                                        void* workspace, size_t workspaceSize,\n                                        const void* dict, size_t dictSize,\n                                        ZSTD_dictLoadMethod_e dictLoadMethod,\n                                        ZSTD_dictContentType_e dictContentType,\n                                        ZSTD_compressionParameters cParams);\n\nZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(\n                                        void* workspace, size_t workspaceSize,\n                                        const void* dict, size_t dictSize,\n                                        ZSTD_dictLoadMethod_e dictLoadMethod,\n                                        ZSTD_dictContentType_e dictContentType);\n\n\n/*! Custom memory allocation :\n *  These prototypes make it possible to pass your own allocation/free functions.\n *  ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.\n *  All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.\n */\ntypedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);\ntypedef void  (*ZSTD_freeFunction) (void* opaque, void* address);\ntypedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;\nstatic ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL };  /**< this constant defers to stdlib's functions */\n\nZSTDLIB_API ZSTD_CCtx*    ZSTD_createCCtx_advanced(ZSTD_customMem customMem);\nZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);\nZSTDLIB_API ZSTD_DCtx*    ZSTD_createDCtx_advanced(ZSTD_customMem customMem);\nZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);\n\nZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,\n                                                  ZSTD_dictLoadMethod_e dictLoadMethod,\n                                                  ZSTD_dictContentType_e dictContentType,\n                                                  ZSTD_compressionParameters cParams,\n                                                  ZSTD_customMem customMem);\n\nZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,\n                                                  ZSTD_dictLoadMethod_e dictLoadMethod,\n                                                  ZSTD_dictContentType_e dictContentType,\n                                                  ZSTD_customMem customMem);\n\n\n\n/***************************************\n*  Advanced compression functions\n***************************************/\n\n/*! ZSTD_createCDict_byReference() :\n *  Create a digested dictionary for compression\n *  Dictionary content is just referenced, not duplicated.\n *  As a consequence, `dictBuffer` **must** outlive CDict,\n *  and its content must remain unmodified throughout the lifetime of CDict.\n *  note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */\nZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);\n\n/*! ZSTD_getCParams() :\n * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.\n * `estimatedSrcSize` value is optional, select 0 if not known */\nZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);\n\n/*! ZSTD_getParams() :\n *  same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.\n *  All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */\nZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);\n\n/*! ZSTD_checkCParams() :\n *  Ensure param values remain within authorized range.\n * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */\nZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);\n\n/*! ZSTD_adjustCParams() :\n *  optimize params for a given `srcSize` and `dictSize`.\n * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN.\n * `dictSize` must be `0` when there is no dictionary.\n *  cPar can be invalid : all parameters will be clamped within valid range in the @return struct.\n *  This function never fails (wide contract) */\nZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);\n\n/*! ZSTD_compress_advanced() :\n *  Note : this function is now DEPRECATED.\n *         It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters.\n *  This prototype will be marked as deprecated and generate compilation warning on reaching v1.5.x */\nZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,\n                                          void* dst, size_t dstCapacity,\n                                    const void* src, size_t srcSize,\n                                    const void* dict,size_t dictSize,\n                                          ZSTD_parameters params);\n\n/*! ZSTD_compress_usingCDict_advanced() :\n *  Note : this function is now REDUNDANT.\n *         It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters.\n *  This prototype will be marked as deprecated and generate compilation warning in some future version */\nZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,\n                                              void* dst, size_t dstCapacity,\n                                        const void* src, size_t srcSize,\n                                        const ZSTD_CDict* cdict,\n                                              ZSTD_frameParameters fParams);\n\n\n/*! ZSTD_CCtx_loadDictionary_byReference() :\n *  Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx.\n *  It saves some memory, but also requires that `dict` outlives its usage within `cctx` */\nZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);\n\n/*! ZSTD_CCtx_loadDictionary_advanced() :\n *  Same as ZSTD_CCtx_loadDictionary(), but gives finer control over\n *  how to load the dictionary (by copy ? by reference ?)\n *  and how to interpret it (automatic ? force raw mode ? full mode only ?) */\nZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);\n\n/*! ZSTD_CCtx_refPrefix_advanced() :\n *  Same as ZSTD_CCtx_refPrefix(), but gives finer control over\n *  how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */\nZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);\n\n/* ===   experimental parameters   === */\n/* these parameters can be used with ZSTD_setParameter()\n * they are not guaranteed to remain supported in the future */\n\n /* Enables rsyncable mode,\n  * which makes compressed files more rsync friendly\n  * by adding periodic synchronization points to the compressed data.\n  * The target average block size is ZSTD_c_jobSize / 2.\n  * It's possible to modify the job size to increase or decrease\n  * the granularity of the synchronization point.\n  * Once the jobSize is smaller than the window size,\n  * it will result in compression ratio degradation.\n  * NOTE 1: rsyncable mode only works when multithreading is enabled.\n  * NOTE 2: rsyncable performs poorly in combination with long range mode,\n  * since it will decrease the effectiveness of synchronization points,\n  * though mileage may vary.\n  * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s.\n  * If the selected compression level is already running significantly slower,\n  * the overall speed won't be significantly impacted.\n  */\n #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1\n\n/* Select a compression format.\n * The value must be of type ZSTD_format_e.\n * See ZSTD_format_e enum definition for details */\n#define ZSTD_c_format ZSTD_c_experimentalParam2\n\n/* Force back-reference distances to remain < windowSize,\n * even when referencing into Dictionary content (default:0) */\n#define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3\n\n/* Controls whether the contents of a CDict\n * are used in place, or copied into the working context.\n * Accepts values from the ZSTD_dictAttachPref_e enum.\n * See the comments on that enum for an explanation of the feature. */\n#define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4\n\n/* Controls how the literals are compressed (default is auto).\n * The value must be of type ZSTD_literalCompressionMode_e.\n * See ZSTD_literalCompressionMode_t enum definition for details.\n */\n#define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5\n\n/* Tries to fit compressed block size to be around targetCBlockSize.\n * No target when targetCBlockSize == 0.\n * There is no guarantee on compressed block size (default:0) */\n#define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6\n\n/* User's best guess of source size.\n * Hint is not valid when srcSizeHint == 0.\n * There is no guarantee that hint is close to actual source size,\n * but compression ratio may regress significantly if guess considerably underestimates */\n#define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7\n\n/*! ZSTD_CCtx_getParameter() :\n *  Get the requested compression parameter value, selected by enum ZSTD_cParameter,\n *  and store it into int* value.\n * @return : 0, or an error code (which can be tested with ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value);\n\n\n/*! ZSTD_CCtx_params :\n *  Quick howto :\n *  - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure\n *  - ZSTD_CCtxParams_setParameter() : Push parameters one by one into\n *                                     an existing ZSTD_CCtx_params structure.\n *                                     This is similar to\n *                                     ZSTD_CCtx_setParameter().\n *  - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to\n *                                    an existing CCtx.\n *                                    These parameters will be applied to\n *                                    all subsequent frames.\n *  - ZSTD_compressStream2() : Do compression using the CCtx.\n *  - ZSTD_freeCCtxParams() : Free the memory.\n *\n *  This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()\n *  for static allocation of CCtx for single-threaded compression.\n */\nZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);\nZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);\n\n/*! ZSTD_CCtxParams_reset() :\n *  Reset params to default values.\n */\nZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);\n\n/*! ZSTD_CCtxParams_init() :\n *  Initializes the compression parameters of cctxParams according to\n *  compression level. All other parameters are reset to their default values.\n */\nZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);\n\n/*! ZSTD_CCtxParams_init_advanced() :\n *  Initializes the compression and frame parameters of cctxParams according to\n *  params. All other parameters are reset to their default values.\n */\nZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);\n\n/*! ZSTD_CCtxParams_setParameter() :\n *  Similar to ZSTD_CCtx_setParameter.\n *  Set one compression parameter, selected by enum ZSTD_cParameter.\n *  Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);\n\n/*! ZSTD_CCtxParams_getParameter() :\n * Similar to ZSTD_CCtx_getParameter.\n * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.\n * @result : 0, or an error code (which can be tested with ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);\n\n/*! ZSTD_CCtx_setParametersUsingCCtxParams() :\n *  Apply a set of ZSTD_CCtx_params to the compression context.\n *  This can be done even after compression is started,\n *    if nbWorkers==0, this will have no impact until a new compression is started.\n *    if nbWorkers>=1, new parameters will be picked up at next job,\n *       with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).\n */\nZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(\n        ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);\n\n/*! ZSTD_compressStream2_simpleArgs() :\n *  Same as ZSTD_compressStream2(),\n *  but using only integral types as arguments.\n *  This variant might be helpful for binders from dynamic languages\n *  which have troubles handling structures containing memory pointers.\n */\nZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs (\n                            ZSTD_CCtx* cctx,\n                            void* dst, size_t dstCapacity, size_t* dstPos,\n                      const void* src, size_t srcSize, size_t* srcPos,\n                            ZSTD_EndDirective endOp);\n\n\n/***************************************\n*  Advanced decompression functions\n***************************************/\n\n/*! ZSTD_isFrame() :\n *  Tells if the content of `buffer` starts with a valid Frame Identifier.\n *  Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.\n *  Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.\n *  Note 3 : Skippable Frame Identifiers are considered valid. */\nZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);\n\n/*! ZSTD_createDDict_byReference() :\n *  Create a digested dictionary, ready to start decompression operation without startup delay.\n *  Dictionary content is referenced, and therefore stays in dictBuffer.\n *  It is important that dictBuffer outlives DDict,\n *  it must remain read accessible throughout the lifetime of DDict */\nZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);\n\n/*! ZSTD_DCtx_loadDictionary_byReference() :\n *  Same as ZSTD_DCtx_loadDictionary(),\n *  but references `dict` content instead of copying it into `dctx`.\n *  This saves memory if `dict` remains around.,\n *  However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */\nZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);\n\n/*! ZSTD_DCtx_loadDictionary_advanced() :\n *  Same as ZSTD_DCtx_loadDictionary(),\n *  but gives direct control over\n *  how to load the dictionary (by copy ? by reference ?)\n *  and how to interpret it (automatic ? force raw mode ? full mode only ?). */\nZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);\n\n/*! ZSTD_DCtx_refPrefix_advanced() :\n *  Same as ZSTD_DCtx_refPrefix(), but gives finer control over\n *  how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */\nZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);\n\n/*! ZSTD_DCtx_setMaxWindowSize() :\n *  Refuses allocating internal buffers for frames requiring a window size larger than provided limit.\n *  This protects a decoder context from reserving too much memory for itself (potential attack scenario).\n *  This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.\n *  By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT)\n * @return : 0, or an error code (which can be tested using ZSTD_isError()).\n */\nZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);\n\n/* ZSTD_d_format\n * experimental parameter,\n * allowing selection between ZSTD_format_e input compression formats\n */\n#define ZSTD_d_format ZSTD_d_experimentalParam1\n/* ZSTD_d_stableOutBuffer\n * Experimental parameter.\n * Default is 0 == disabled. Set to 1 to enable.\n *\n * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same\n * between calls, except for the modifications that zstd makes to pos (the\n * caller must not modify pos). This is checked by the decompressor, and\n * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer\n * MUST be large enough to fit the entire decompressed frame. This will be\n * checked when the frame content size is known. The data in the ZSTD_outBuffer\n * in the range [dst, dst + pos) MUST not be modified during decompression\n * or you will get data corruption.\n *\n * When this flags is enabled zstd won't allocate an output buffer, because\n * it can write directly to the ZSTD_outBuffer, but it will still allocate\n * an input buffer large enough to fit any compressed block. This will also\n * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer.\n * If you need to avoid the input buffer allocation use the buffer-less\n * streaming API.\n *\n * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using\n * this flag is ALWAYS memory safe, and will never access out-of-bounds\n * memory. However, decompression WILL fail if you violate the preconditions.\n *\n * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST\n * not be modified during decompression or you will get data corruption. This\n * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate\n * matches. Normally zstd maintains its own buffer for this purpose, but passing\n * this flag tells zstd to use the user provided buffer.\n */\n#define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2\n\n/*! ZSTD_DCtx_setFormat() :\n *  Instruct the decoder context about what kind of data to decode next.\n *  This instruction is mandatory to decode data without a fully-formed header,\n *  such ZSTD_f_zstd1_magicless for example.\n * @return : 0, or an error code (which can be tested using ZSTD_isError()). */\nZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);\n\n/*! ZSTD_decompressStream_simpleArgs() :\n *  Same as ZSTD_decompressStream(),\n *  but using only integral types as arguments.\n *  This can be helpful for binders from dynamic languages\n *  which have troubles handling structures containing memory pointers.\n */\nZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs (\n                            ZSTD_DCtx* dctx,\n                            void* dst, size_t dstCapacity, size_t* dstPos,\n                      const void* src, size_t srcSize, size_t* srcPos);\n\n\n/********************************************************************\n*  Advanced streaming functions\n*  Warning : most of these functions are now redundant with the Advanced API.\n*  Once Advanced API reaches \"stable\" status,\n*  redundant functions will be deprecated, and then at some point removed.\n********************************************************************/\n\n/*=====   Advanced Streaming compression functions  =====*/\n/**! ZSTD_initCStream_srcSize() :\n * This function is deprecated, and equivalent to:\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)\n *     ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);\n *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);\n *\n * pledgedSrcSize must be correct. If it is not known at init time, use\n * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs,\n * \"0\" also disables frame content size field. It may be enabled in the future.\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t\nZSTD_initCStream_srcSize(ZSTD_CStream* zcs,\n                         int compressionLevel,\n                         unsigned long long pledgedSrcSize);\n\n/**! ZSTD_initCStream_usingDict() :\n * This function is deprecated, and is equivalent to:\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);\n *     ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);\n *\n * Creates of an internal CDict (incompatible with static CCtx), except if\n * dict == NULL or dictSize < 8, in which case no dict is used.\n * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if\n * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t\nZSTD_initCStream_usingDict(ZSTD_CStream* zcs,\n                     const void* dict, size_t dictSize,\n                           int compressionLevel);\n\n/**! ZSTD_initCStream_advanced() :\n * This function is deprecated, and is approximately equivalent to:\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     // Pseudocode: Set each zstd parameter and leave the rest as-is.\n *     for ((param, value) : params) {\n *         ZSTD_CCtx_setParameter(zcs, param, value);\n *     }\n *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);\n *     ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);\n *\n * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy.\n * pledgedSrcSize must be correct.\n * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN.\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t\nZSTD_initCStream_advanced(ZSTD_CStream* zcs,\n                    const void* dict, size_t dictSize,\n                          ZSTD_parameters params,\n                          unsigned long long pledgedSrcSize);\n\n/**! ZSTD_initCStream_usingCDict() :\n * This function is deprecated, and equivalent to:\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     ZSTD_CCtx_refCDict(zcs, cdict);\n *\n * note : cdict will just be referenced, and must outlive compression session\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict);\n\n/**! ZSTD_initCStream_usingCDict_advanced() :\n *   This function is DEPRECATED, and is approximately equivalent to:\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     // Pseudocode: Set each zstd frame parameter and leave the rest as-is.\n *     for ((fParam, value) : fParams) {\n *         ZSTD_CCtx_setParameter(zcs, fParam, value);\n *     }\n *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);\n *     ZSTD_CCtx_refCDict(zcs, cdict);\n *\n * same as ZSTD_initCStream_usingCDict(), with control over frame parameters.\n * pledgedSrcSize must be correct. If srcSize is not known at init time, use\n * value ZSTD_CONTENTSIZE_UNKNOWN.\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t\nZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs,\n                               const ZSTD_CDict* cdict,\n                                     ZSTD_frameParameters fParams,\n                                     unsigned long long pledgedSrcSize);\n\n/*! ZSTD_resetCStream() :\n * This function is deprecated, and is equivalent to:\n *     ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);\n *     ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);\n *\n *  start a new frame, using same parameters from previous frame.\n *  This is typically useful to skip dictionary loading stage, since it will re-use it in-place.\n *  Note that zcs must be init at least once before using ZSTD_resetCStream().\n *  If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.\n *  If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.\n *  For the time being, pledgedSrcSize==0 is interpreted as \"srcSize unknown\" for compatibility with older programs,\n *  but it will change to mean \"empty\" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.\n * @return : 0, or an error code (which can be tested using ZSTD_isError())\n *  Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);\n\n\ntypedef struct {\n    unsigned long long ingested;   /* nb input bytes read and buffered */\n    unsigned long long consumed;   /* nb input bytes actually compressed */\n    unsigned long long produced;   /* nb of compressed bytes generated and buffered */\n    unsigned long long flushed;    /* nb of compressed bytes flushed : not provided; can be tracked from caller side */\n    unsigned currentJobID;         /* MT only : latest started job nb */\n    unsigned nbActiveWorkers;      /* MT only : nb of workers actively compressing at probe time */\n} ZSTD_frameProgression;\n\n/* ZSTD_getFrameProgression() :\n * tells how much data has been ingested (read from input)\n * consumed (input actually compressed) and produced (output) for current frame.\n * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.\n * Aggregates progression inside active worker threads.\n */\nZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);\n\n/*! ZSTD_toFlushNow() :\n *  Tell how many bytes are ready to be flushed immediately.\n *  Useful for multithreading scenarios (nbWorkers >= 1).\n *  Probe the oldest active job, defined as oldest job not yet entirely flushed,\n *  and check its output buffer.\n * @return : amount of data stored in oldest job and ready to be flushed immediately.\n *  if @return == 0, it means either :\n *  + there is no active job (could be checked with ZSTD_frameProgression()), or\n *  + oldest job is still actively compressing data,\n *    but everything it has produced has also been flushed so far,\n *    therefore flush speed is limited by production speed of oldest job\n *    irrespective of the speed of concurrent (and newer) jobs.\n */\nZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);\n\n\n/*=====   Advanced Streaming decompression functions  =====*/\n/**\n * This function is deprecated, and is equivalent to:\n *\n *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);\n *     ZSTD_DCtx_loadDictionary(zds, dict, dictSize);\n *\n * note: no dictionary will be used if dict == NULL or dictSize < 8\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);\n\n/**\n * This function is deprecated, and is equivalent to:\n *\n *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);\n *     ZSTD_DCtx_refDDict(zds, ddict);\n *\n * note : ddict is referenced, it must outlive decompression session\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict);\n\n/**\n * This function is deprecated, and is equivalent to:\n *\n *     ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);\n *\n * re-use decompression parameters from previous init; saves dictionary loading\n * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x\n */\nZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);\n\n\n/*********************************************************************\n*  Buffer-less and synchronous inner streaming functions\n*\n*  This is an advanced API, giving full control over buffer management, for users which need direct control over memory.\n*  But it's also a complex one, with several restrictions, documented below.\n*  Prefer normal streaming API for an easier experience.\n********************************************************************* */\n\n/**\n  Buffer-less streaming compression (synchronous mode)\n\n  A ZSTD_CCtx object is required to track streaming operations.\n  Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.\n  ZSTD_CCtx object can be re-used multiple times within successive compression operations.\n\n  Start by initializing a context.\n  Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,\n  or ZSTD_compressBegin_advanced(), for finer parameter control.\n  It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()\n\n  Then, consume your input using ZSTD_compressContinue().\n  There are some important considerations to keep in mind when using this advanced function :\n  - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.\n  - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.\n  - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.\n    Worst case evaluation is provided by ZSTD_compressBound().\n    ZSTD_compressContinue() doesn't guarantee recover after a failed compression.\n  - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).\n    It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)\n  - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.\n    In which case, it will \"discard\" the relevant memory section from its history.\n\n  Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.\n  It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.\n  Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.\n\n  `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.\n*/\n\n/*=====   Buffer-less streaming compression functions  =====*/\nZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);\nZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);\nZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */\nZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */\nZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize);   /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */\nZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**<  note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */\n\nZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\nZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n\n/*-\n  Buffer-less streaming decompression (synchronous mode)\n\n  A ZSTD_DCtx object is required to track streaming operations.\n  Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.\n  A ZSTD_DCtx object can be re-used multiple times.\n\n  First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().\n  Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.\n  Data fragment must be large enough to ensure successful decoding.\n `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.\n  @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.\n           >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.\n           errorCode, which can be tested using ZSTD_isError().\n\n  It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,\n  such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).\n  Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.\n  As a consequence, check that values remain within valid application range.\n  For example, do not allocate memory blindly, check that `windowSize` is within expectation.\n  Each application can set its own limits, depending on local restrictions.\n  For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.\n\n  ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.\n  ZSTD_decompressContinue() is very sensitive to contiguity,\n  if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,\n  or that previous contiguous segment is large enough to properly handle maximum back-reference distance.\n  There are multiple ways to guarantee this condition.\n\n  The most memory efficient way is to use a round buffer of sufficient size.\n  Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),\n  which can @return an error code if required value is too large for current system (in 32-bits mode).\n  In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,\n  up to the moment there is not enough room left in the buffer to guarantee decoding another full block,\n  which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.\n  At which point, decoding can resume from the beginning of the buffer.\n  Note that already decoded data stored in the buffer should be flushed before being overwritten.\n\n  There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.\n\n  Finally, if you control the compression process, you can also ignore all buffer size rules,\n  as long as the encoder and decoder progress in \"lock-step\",\n  aka use exactly the same buffer sizes, break contiguity at the same place, etc.\n\n  Once buffers are setup, start decompression, with ZSTD_decompressBegin().\n  If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().\n\n  Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.\n  ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().\n  ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.\n\n @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).\n  It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.\n  It can also be an error code, which can be tested with ZSTD_isError().\n\n  A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.\n  Context can then be reset to start a new decompression.\n\n  Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().\n  This information is not required to properly decode a frame.\n\n  == Special case : skippable frames ==\n\n  Skippable frames allow integration of user-defined data into a flow of concatenated frames.\n  Skippable frames will be ignored (skipped) by decompressor.\n  The format of skippable frames is as follows :\n  a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F\n  b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits\n  c) Frame Content - any content (User Data) of length equal to Frame Size\n  For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.\n  For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.\n*/\n\n/*=====   Buffer-less streaming decompression functions  =====*/\ntypedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;\ntypedef struct {\n    unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means \"empty\" */\n    unsigned long long windowSize;       /* can be very large, up to <= frameContentSize */\n    unsigned blockSizeMax;\n    ZSTD_frameType_e frameType;          /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */\n    unsigned headerSize;\n    unsigned dictID;\n    unsigned checksumFlag;\n} ZSTD_frameHeader;\n\n/*! ZSTD_getFrameHeader() :\n *  decode Frame Header, or requires larger `srcSize`.\n * @return : 0, `zfhPtr` is correctly filled,\n *          >0, `srcSize` is too small, value is wanted `srcSize` amount,\n *           or an error code, which can be tested using ZSTD_isError() */\nZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize);   /**< doesn't consume input */\n/*! ZSTD_getFrameHeader_advanced() :\n *  same as ZSTD_getFrameHeader(),\n *  with added capability to select a format (like ZSTD_f_zstd1_magicless) */\nZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format);\nZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize);  /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */\n\nZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);\nZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);\nZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);\n\nZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);\nZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n/* misc */\nZSTDLIB_API void   ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);\ntypedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;\nZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);\n\n\n\n\n/* ============================ */\n/**       Block level API       */\n/* ============================ */\n\n/*!\n    Block functions produce and decode raw zstd blocks, without frame metadata.\n    Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes).\n    But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes.\n\n    A few rules to respect :\n    - Compressing and decompressing require a context structure\n      + Use ZSTD_createCCtx() and ZSTD_createDCtx()\n    - It is necessary to init context before starting\n      + compression : any ZSTD_compressBegin*() variant, including with dictionary\n      + decompression : any ZSTD_decompressBegin*() variant, including with dictionary\n      + copyCCtx() and copyDCtx() can be used too\n    - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB\n      + If input is larger than a block size, it's necessary to split input data into multiple blocks\n      + For inputs larger than a single block, consider using regular ZSTD_compress() instead.\n        Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block.\n    - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) !\n      ===> In which case, nothing is produced into `dst` !\n      + User __must__ test for such outcome and deal directly with uncompressed data\n      + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0.\n        Doing so would mess up with statistics history, leading to potential data corruption.\n      + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !!\n      + In case of multiple successive blocks, should some of them be uncompressed,\n        decoder must be informed of their existence in order to follow proper history.\n        Use ZSTD_insertBlock() for such a case.\n*/\n\n/*=====   Raw zstd block functions  =====*/\nZSTDLIB_API size_t ZSTD_getBlockSize   (const ZSTD_CCtx* cctx);\nZSTDLIB_API size_t ZSTD_compressBlock  (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\nZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\nZSTDLIB_API size_t ZSTD_insertBlock    (ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize);  /**< insert uncompressed block into `dctx` history. Useful for multi-blocks decompression. */\n\n\n#endif   /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */\n\n#if defined (__cplusplus)\n}\n#endif\n/**** ended inlining ../zstd.h ****/\n#define FSE_STATIC_LINKING_ONLY\n/**** skipping file: fse.h ****/\n#define HUF_STATIC_LINKING_ONLY\n/**** skipping file: huf.h ****/\n#ifndef XXH_STATIC_LINKING_ONLY\n#  define XXH_STATIC_LINKING_ONLY  /* XXH64_state_t */\n#endif\n/**** start inlining xxhash.h ****/\n/*\n * xxHash - Extremely Fast Hash algorithm\n * Header File\n * Copyright (c) 2012-2020, Yann Collet, Facebook, Inc.\n *\n * You can contact the author at :\n * - xxHash source repository : https://github.com/Cyan4973/xxHash\n * \n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n*/\n\n/* Notice extracted from xxHash homepage :\n\nxxHash is an extremely fast Hash algorithm, running at RAM speed limits.\nIt also successfully passes all tests from the SMHasher suite.\n\nComparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)\n\nName            Speed       Q.Score   Author\nxxHash          5.4 GB/s     10\nCrapWow         3.2 GB/s      2       Andrew\nMumurHash 3a    2.7 GB/s     10       Austin Appleby\nSpookyHash      2.0 GB/s     10       Bob Jenkins\nSBox            1.4 GB/s      9       Bret Mulvey\nLookup3         1.2 GB/s      9       Bob Jenkins\nSuperFastHash   1.2 GB/s      1       Paul Hsieh\nCityHash64      1.05 GB/s    10       Pike & Alakuijala\nFNV             0.55 GB/s     5       Fowler, Noll, Vo\nCRC32           0.43 GB/s     9\nMD5-32          0.33 GB/s    10       Ronald L. Rivest\nSHA1-32         0.28 GB/s    10\n\nQ.Score is a measure of quality of the hash function.\nIt depends on successfully passing SMHasher test set.\n10 is a perfect score.\n\nA 64-bits version, named XXH64, is available since r35.\nIt offers much better speed, but for 64-bits applications only.\nName     Speed on 64 bits    Speed on 32 bits\nXXH64       13.8 GB/s            1.9 GB/s\nXXH32        6.8 GB/s            6.0 GB/s\n*/\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n#ifndef XXHASH_H_5627135585666179\n#define XXHASH_H_5627135585666179 1\n\n\n/* ****************************\n*  Definitions\n******************************/\n#include <stddef.h>   /* size_t */\ntypedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;\n\n\n/* ****************************\n*  API modifier\n******************************/\n/** XXH_PRIVATE_API\n*   This is useful if you want to include xxhash functions in `static` mode\n*   in order to inline them, and remove their symbol from the public list.\n*   Methodology :\n*     #define XXH_PRIVATE_API\n*     #include \"xxhash.h\"\n*   `xxhash.c` is automatically included.\n*   It's not useful to compile and link it as a separate module anymore.\n*/\n#ifdef XXH_PRIVATE_API\n#  ifndef XXH_STATIC_LINKING_ONLY\n#    define XXH_STATIC_LINKING_ONLY\n#  endif\n#  if defined(__GNUC__)\n#    define XXH_PUBLIC_API static __inline __attribute__((unused))\n#  elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)\n#    define XXH_PUBLIC_API static inline\n#  elif defined(_MSC_VER)\n#    define XXH_PUBLIC_API static __inline\n#  else\n#    define XXH_PUBLIC_API static   /* this version may generate warnings for unused static functions; disable the relevant warning */\n#  endif\n#else\n#  define XXH_PUBLIC_API   /* do nothing */\n#endif /* XXH_PRIVATE_API */\n\n/*!XXH_NAMESPACE, aka Namespace Emulation :\n\nIf you want to include _and expose_ xxHash functions from within your own library,\nbut also want to avoid symbol collisions with another library which also includes xxHash,\n\nyou can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library\nwith the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).\n\nNote that no change is required within the calling program as long as it includes `xxhash.h` :\nregular symbol name will be automatically translated by this header.\n*/\n#ifdef XXH_NAMESPACE\n#  define XXH_CAT(A,B) A##B\n#  define XXH_NAME2(A,B) XXH_CAT(A,B)\n#  define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)\n#  define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)\n#  define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)\n#  define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)\n#  define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)\n#  define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)\n#  define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)\n#  define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)\n#  define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)\n#  define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)\n#  define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)\n#  define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)\n#  define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)\n#  define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)\n#  define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)\n#  define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)\n#  define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)\n#  define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)\n#  define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)\n#endif\n\n\n/* *************************************\n*  Version\n***************************************/\n#define XXH_VERSION_MAJOR    0\n#define XXH_VERSION_MINOR    6\n#define XXH_VERSION_RELEASE  2\n#define XXH_VERSION_NUMBER  (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)\nXXH_PUBLIC_API unsigned XXH_versionNumber (void);\n\n\n/* ****************************\n*  Simple Hash Functions\n******************************/\ntypedef unsigned int       XXH32_hash_t;\ntypedef unsigned long long XXH64_hash_t;\n\nXXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);\nXXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);\n\n/*!\nXXH32() :\n    Calculate the 32-bits hash of sequence \"length\" bytes stored at memory address \"input\".\n    The memory between input & input+length must be valid (allocated and read-accessible).\n    \"seed\" can be used to alter the result predictably.\n    Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s\nXXH64() :\n    Calculate the 64-bits hash of sequence of length \"len\" stored at memory address \"input\".\n    \"seed\" can be used to alter the result predictably.\n    This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark).\n*/\n\n\n/* ****************************\n*  Streaming Hash Functions\n******************************/\ntypedef struct XXH32_state_s XXH32_state_t;   /* incomplete type */\ntypedef struct XXH64_state_s XXH64_state_t;   /* incomplete type */\n\n/*! State allocation, compatible with dynamic libraries */\n\nXXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);\nXXH_PUBLIC_API XXH_errorcode  XXH32_freeState(XXH32_state_t* statePtr);\n\nXXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);\nXXH_PUBLIC_API XXH_errorcode  XXH64_freeState(XXH64_state_t* statePtr);\n\n\n/* hash streaming */\n\nXXH_PUBLIC_API XXH_errorcode XXH32_reset  (XXH32_state_t* statePtr, unsigned int seed);\nXXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);\nXXH_PUBLIC_API XXH32_hash_t  XXH32_digest (const XXH32_state_t* statePtr);\n\nXXH_PUBLIC_API XXH_errorcode XXH64_reset  (XXH64_state_t* statePtr, unsigned long long seed);\nXXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);\nXXH_PUBLIC_API XXH64_hash_t  XXH64_digest (const XXH64_state_t* statePtr);\n\n/*\nThese functions generate the xxHash of an input provided in multiple segments.\nNote that, for small input, they are slower than single-call functions, due to state management.\nFor small input, prefer `XXH32()` and `XXH64()` .\n\nXXH state must first be allocated, using XXH*_createState() .\n\nStart a new hash by initializing state with a seed, using XXH*_reset().\n\nThen, feed the hash state by calling XXH*_update() as many times as necessary.\nObviously, input must be allocated and read accessible.\nThe function returns an error code, with 0 meaning OK, and any other value meaning there is an error.\n\nFinally, a hash value can be produced anytime, by using XXH*_digest().\nThis function returns the nn-bits hash as an int or long long.\n\nIt's still possible to continue inserting input into the hash state after a digest,\nand generate some new hashes later on, by calling again XXH*_digest().\n\nWhen done, free XXH state space if it was allocated dynamically.\n*/\n\n\n/* **************************\n*  Utils\n****************************/\n#if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))   /* ! C99 */\n#  define restrict   /* disable restrict */\n#endif\n\nXXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state);\nXXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state);\n\n\n/* **************************\n*  Canonical representation\n****************************/\n/* Default result type for XXH functions are primitive unsigned 32 and 64 bits.\n*  The canonical representation uses human-readable write convention, aka big-endian (large digits first).\n*  These functions allow transformation of hash result into and from its canonical format.\n*  This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.\n*/\ntypedef struct { unsigned char digest[4]; } XXH32_canonical_t;\ntypedef struct { unsigned char digest[8]; } XXH64_canonical_t;\n\nXXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);\nXXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);\n\nXXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);\nXXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);\n\n#endif /* XXHASH_H_5627135585666179 */\n\n\n\n/* ================================================================================================\n   This section contains definitions which are not guaranteed to remain stable.\n   They may change in future versions, becoming incompatible with a different version of the library.\n   They shall only be used with static linking.\n   Never use these definitions in association with dynamic linking !\n=================================================================================================== */\n#if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXH_STATIC_H_3543687687345)\n#define XXH_STATIC_H_3543687687345\n\n/* These definitions are only meant to allow allocation of XXH state\n   statically, on stack, or in a struct for example.\n   Do not use members directly. */\n\n   struct XXH32_state_s {\n       unsigned total_len_32;\n       unsigned large_len;\n       unsigned v1;\n       unsigned v2;\n       unsigned v3;\n       unsigned v4;\n       unsigned mem32[4];   /* buffer defined as U32 for alignment */\n       unsigned memsize;\n       unsigned reserved;   /* never read nor write, will be removed in a future version */\n   };   /* typedef'd to XXH32_state_t */\n\n   struct XXH64_state_s {\n       unsigned long long total_len;\n       unsigned long long v1;\n       unsigned long long v2;\n       unsigned long long v3;\n       unsigned long long v4;\n       unsigned long long mem64[4];   /* buffer defined as U64 for alignment */\n       unsigned memsize;\n       unsigned reserved[2];          /* never read nor write, will be removed in a future version */\n   };   /* typedef'd to XXH64_state_t */\n\n\n#  ifdef XXH_PRIVATE_API\n/**** start inlining xxhash.c ****/\n/*\n *  xxHash - Fast Hash algorithm\n *  Copyright (c) 2012-2020, Yann Collet, Facebook, Inc.\n *\n *  You can contact the author at :\n *  - xxHash homepage: http://www.xxhash.com\n *  - xxHash source repository : https://github.com/Cyan4973/xxHash\n * \n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n*/\n\n\n/* *************************************\n*  Tuning parameters\n***************************************/\n/*!XXH_FORCE_MEMORY_ACCESS :\n * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.\n * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.\n * The below switch allow to select different access method for improved performance.\n * Method 0 (default) : use `memcpy()`. Safe and portable.\n * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).\n *            This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.\n * Method 2 : direct access. This method doesn't depend on compiler but violate C standard.\n *            It can generate buggy code on targets which do not support unaligned memory accesses.\n *            But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)\n * See http://stackoverflow.com/a/32095106/646947 for details.\n * Prefer these methods in priority order (0 > 1 > 2)\n */\n#ifndef XXH_FORCE_MEMORY_ACCESS   /* can be defined externally, on command line for example */\n#  if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )\n#    define XXH_FORCE_MEMORY_ACCESS 2\n#  elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \\\n  (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) )) || \\\n  defined(__ICCARM__)\n#    define XXH_FORCE_MEMORY_ACCESS 1\n#  endif\n#endif\n\n/*!XXH_ACCEPT_NULL_INPUT_POINTER :\n * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.\n * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.\n * By default, this option is disabled. To enable it, uncomment below define :\n */\n/* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */\n\n/*!XXH_FORCE_NATIVE_FORMAT :\n * By default, xxHash library provides endian-independent Hash values, based on little-endian convention.\n * Results are therefore identical for little-endian and big-endian CPU.\n * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.\n * Should endian-independence be of no importance for your application, you may set the #define below to 1,\n * to improve speed for Big-endian CPU.\n * This option has no impact on Little_Endian CPU.\n */\n#ifndef XXH_FORCE_NATIVE_FORMAT   /* can be defined externally */\n#  define XXH_FORCE_NATIVE_FORMAT 0\n#endif\n\n/*!XXH_FORCE_ALIGN_CHECK :\n * This is a minor performance trick, only useful with lots of very small keys.\n * It means : check for aligned/unaligned input.\n * The check costs one initial branch per hash; set to 0 when the input data\n * is guaranteed to be aligned.\n */\n#ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */\n#  if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)\n#    define XXH_FORCE_ALIGN_CHECK 0\n#  else\n#    define XXH_FORCE_ALIGN_CHECK 1\n#  endif\n#endif\n\n\n/* *************************************\n*  Includes & Memory related functions\n***************************************/\n/* Modify the local functions below should you wish to use some other memory routines */\n/* for malloc(), free() */\n#include <stddef.h>     /* size_t */\nstatic void* XXH_malloc(size_t s) { return malloc(s); }\nstatic void  XXH_free  (void* p)  { free(p); }\n/* for memcpy() */\nstatic void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }\n\n#ifndef XXH_STATIC_LINKING_ONLY\n#  define XXH_STATIC_LINKING_ONLY\n#endif\n/**** skipping file: xxhash.h ****/\n\n\n/* *************************************\n*  Compiler Specific Options\n***************************************/\n#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */\n#  define INLINE_KEYWORD inline\n#else\n#  define INLINE_KEYWORD\n#endif\n\n#if defined(__GNUC__) || defined(__ICCARM__)\n#  define FORCE_INLINE_ATTR __attribute__((always_inline))\n#elif defined(_MSC_VER)\n#  define FORCE_INLINE_ATTR __forceinline\n#else\n#  define FORCE_INLINE_ATTR\n#endif\n\n#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR\n\n\n#ifdef _MSC_VER\n#  pragma warning(disable : 4127)      /* disable: C4127: conditional expression is constant */\n#endif\n\n\n/* *************************************\n*  Basic Types\n***************************************/\n#ifndef MEM_MODULE\n# define MEM_MODULE\n# if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )\n#   include <stdint.h>\n    typedef uint8_t  BYTE;\n    typedef uint16_t U16;\n    typedef uint32_t U32;\n    typedef  int32_t S32;\n    typedef uint64_t U64;\n#  else\n    typedef unsigned char      BYTE;\n    typedef unsigned short     U16;\n    typedef unsigned int       U32;\n    typedef   signed int       S32;\n    typedef unsigned long long U64;   /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */\n#  endif\n#endif\n\n\n#if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))\n\n/* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */\nstatic U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }\nstatic U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }\n\n#elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))\n\n/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */\n/* currently only defined for gcc and icc */\ntypedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign;\n\nstatic U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }\nstatic U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }\n\n#else\n\n/* portable and safe solution. Generally efficient.\n * see : http://stackoverflow.com/a/32095106/646947\n */\n\nstatic U32 XXH_read32(const void* memPtr)\n{\n    U32 val;\n    memcpy(&val, memPtr, sizeof(val));\n    return val;\n}\n\nstatic U64 XXH_read64(const void* memPtr)\n{\n    U64 val;\n    memcpy(&val, memPtr, sizeof(val));\n    return val;\n}\n\n#endif   /* XXH_FORCE_DIRECT_MEMORY_ACCESS */\n\n\n/* ****************************************\n*  Compiler-specific Functions and Macros\n******************************************/\n#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)\n\n/* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */\n#if defined(_MSC_VER)\n#  define XXH_rotl32(x,r) _rotl(x,r)\n#  define XXH_rotl64(x,r) _rotl64(x,r)\n#else\n#if defined(__ICCARM__)\n#  include <intrinsics.h>\n#  define XXH_rotl32(x,r) __ROR(x,(32 - r))\n#else\n#  define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))\n#endif\n#  define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))\n#endif\n\n#if defined(_MSC_VER)     /* Visual Studio */\n#  define XXH_swap32 _byteswap_ulong\n#  define XXH_swap64 _byteswap_uint64\n#elif GCC_VERSION >= 403\n#  define XXH_swap32 __builtin_bswap32\n#  define XXH_swap64 __builtin_bswap64\n#else\nstatic U32 XXH_swap32 (U32 x)\n{\n    return  ((x << 24) & 0xff000000 ) |\n            ((x <<  8) & 0x00ff0000 ) |\n            ((x >>  8) & 0x0000ff00 ) |\n            ((x >> 24) & 0x000000ff );\n}\nstatic U64 XXH_swap64 (U64 x)\n{\n    return  ((x << 56) & 0xff00000000000000ULL) |\n            ((x << 40) & 0x00ff000000000000ULL) |\n            ((x << 24) & 0x0000ff0000000000ULL) |\n            ((x << 8)  & 0x000000ff00000000ULL) |\n            ((x >> 8)  & 0x00000000ff000000ULL) |\n            ((x >> 24) & 0x0000000000ff0000ULL) |\n            ((x >> 40) & 0x000000000000ff00ULL) |\n            ((x >> 56) & 0x00000000000000ffULL);\n}\n#endif\n\n\n/* *************************************\n*  Architecture Macros\n***************************************/\ntypedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;\n\n/* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */\n#ifndef XXH_CPU_LITTLE_ENDIAN\n    static const int g_one = 1;\n#   define XXH_CPU_LITTLE_ENDIAN   (*(const char*)(&g_one))\n#endif\n\n\n/* ***************************\n*  Memory reads\n*****************************/\ntypedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;\n\nFORCE_INLINE_TEMPLATE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)\n{\n    if (align==XXH_unaligned)\n        return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));\n    else\n        return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);\n}\n\nFORCE_INLINE_TEMPLATE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)\n{\n    return XXH_readLE32_align(ptr, endian, XXH_unaligned);\n}\n\nstatic U32 XXH_readBE32(const void* ptr)\n{\n    return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);\n}\n\nFORCE_INLINE_TEMPLATE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)\n{\n    if (align==XXH_unaligned)\n        return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));\n    else\n        return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);\n}\n\nFORCE_INLINE_TEMPLATE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)\n{\n    return XXH_readLE64_align(ptr, endian, XXH_unaligned);\n}\n\nstatic U64 XXH_readBE64(const void* ptr)\n{\n    return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);\n}\n\n\n/* *************************************\n*  Macros\n***************************************/\n#define XXH_STATIC_ASSERT(c)   { enum { XXH_static_assert = 1/(int)(!!(c)) }; }    /* use only *after* variable declarations */\n\n\n/* *************************************\n*  Constants\n***************************************/\nstatic const U32 PRIME32_1 = 2654435761U;\nstatic const U32 PRIME32_2 = 2246822519U;\nstatic const U32 PRIME32_3 = 3266489917U;\nstatic const U32 PRIME32_4 =  668265263U;\nstatic const U32 PRIME32_5 =  374761393U;\n\nstatic const U64 PRIME64_1 = 11400714785074694791ULL;\nstatic const U64 PRIME64_2 = 14029467366897019727ULL;\nstatic const U64 PRIME64_3 =  1609587929392839161ULL;\nstatic const U64 PRIME64_4 =  9650029242287828579ULL;\nstatic const U64 PRIME64_5 =  2870177450012600261ULL;\n\nXXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }\n\n\n/* **************************\n*  Utils\n****************************/\nXXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState)\n{\n    memcpy(dstState, srcState, sizeof(*dstState));\n}\n\nXXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)\n{\n    memcpy(dstState, srcState, sizeof(*dstState));\n}\n\n\n/* ***************************\n*  Simple Hash Functions\n*****************************/\n\nstatic U32 XXH32_round(U32 seed, U32 input)\n{\n    seed += input * PRIME32_2;\n    seed  = XXH_rotl32(seed, 13);\n    seed *= PRIME32_1;\n    return seed;\n}\n\nFORCE_INLINE_TEMPLATE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)\n{\n    const BYTE* p = (const BYTE*)input;\n    const BYTE* bEnd = p + len;\n    U32 h32;\n#define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)\n\n#ifdef XXH_ACCEPT_NULL_INPUT_POINTER\n    if (p==NULL) {\n        len=0;\n        bEnd=p=(const BYTE*)(size_t)16;\n    }\n#endif\n\n    if (len>=16) {\n        const BYTE* const limit = bEnd - 16;\n        U32 v1 = seed + PRIME32_1 + PRIME32_2;\n        U32 v2 = seed + PRIME32_2;\n        U32 v3 = seed + 0;\n        U32 v4 = seed - PRIME32_1;\n\n        do {\n            v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;\n            v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;\n            v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;\n            v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;\n        } while (p<=limit);\n\n        h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);\n    } else {\n        h32  = seed + PRIME32_5;\n    }\n\n    h32 += (U32) len;\n\n    while (p+4<=bEnd) {\n        h32 += XXH_get32bits(p) * PRIME32_3;\n        h32  = XXH_rotl32(h32, 17) * PRIME32_4 ;\n        p+=4;\n    }\n\n    while (p<bEnd) {\n        h32 += (*p) * PRIME32_5;\n        h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;\n        p++;\n    }\n\n    h32 ^= h32 >> 15;\n    h32 *= PRIME32_2;\n    h32 ^= h32 >> 13;\n    h32 *= PRIME32_3;\n    h32 ^= h32 >> 16;\n\n    return h32;\n}\n\n\nXXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed)\n{\n#if 0\n    /* Simple version, good for code maintenance, but unfortunately slow for small inputs */\n    XXH32_CREATESTATE_STATIC(state);\n    XXH32_reset(state, seed);\n    XXH32_update(state, input, len);\n    return XXH32_digest(state);\n#else\n    XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;\n\n    if (XXH_FORCE_ALIGN_CHECK) {\n        if ((((size_t)input) & 3) == 0) {   /* Input is 4-bytes aligned, leverage the speed benefit */\n            if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n                return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);\n            else\n                return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);\n    }   }\n\n    if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n        return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);\n    else\n        return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);\n#endif\n}\n\n\nstatic U64 XXH64_round(U64 acc, U64 input)\n{\n    acc += input * PRIME64_2;\n    acc  = XXH_rotl64(acc, 31);\n    acc *= PRIME64_1;\n    return acc;\n}\n\nstatic U64 XXH64_mergeRound(U64 acc, U64 val)\n{\n    val  = XXH64_round(0, val);\n    acc ^= val;\n    acc  = acc * PRIME64_1 + PRIME64_4;\n    return acc;\n}\n\nFORCE_INLINE_TEMPLATE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)\n{\n    const BYTE* p = (const BYTE*)input;\n    const BYTE* const bEnd = p + len;\n    U64 h64;\n#define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)\n\n#ifdef XXH_ACCEPT_NULL_INPUT_POINTER\n    if (p==NULL) {\n        len=0;\n        bEnd=p=(const BYTE*)(size_t)32;\n    }\n#endif\n\n    if (len>=32) {\n        const BYTE* const limit = bEnd - 32;\n        U64 v1 = seed + PRIME64_1 + PRIME64_2;\n        U64 v2 = seed + PRIME64_2;\n        U64 v3 = seed + 0;\n        U64 v4 = seed - PRIME64_1;\n\n        do {\n            v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;\n            v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;\n            v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;\n            v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;\n        } while (p<=limit);\n\n        h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);\n        h64 = XXH64_mergeRound(h64, v1);\n        h64 = XXH64_mergeRound(h64, v2);\n        h64 = XXH64_mergeRound(h64, v3);\n        h64 = XXH64_mergeRound(h64, v4);\n\n    } else {\n        h64  = seed + PRIME64_5;\n    }\n\n    h64 += (U64) len;\n\n    while (p+8<=bEnd) {\n        U64 const k1 = XXH64_round(0, XXH_get64bits(p));\n        h64 ^= k1;\n        h64  = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;\n        p+=8;\n    }\n\n    if (p+4<=bEnd) {\n        h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;\n        h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;\n        p+=4;\n    }\n\n    while (p<bEnd) {\n        h64 ^= (*p) * PRIME64_5;\n        h64 = XXH_rotl64(h64, 11) * PRIME64_1;\n        p++;\n    }\n\n    h64 ^= h64 >> 33;\n    h64 *= PRIME64_2;\n    h64 ^= h64 >> 29;\n    h64 *= PRIME64_3;\n    h64 ^= h64 >> 32;\n\n    return h64;\n}\n\n\nXXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)\n{\n#if 0\n    /* Simple version, good for code maintenance, but unfortunately slow for small inputs */\n    XXH64_CREATESTATE_STATIC(state);\n    XXH64_reset(state, seed);\n    XXH64_update(state, input, len);\n    return XXH64_digest(state);\n#else\n    XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;\n\n    if (XXH_FORCE_ALIGN_CHECK) {\n        if ((((size_t)input) & 7)==0) {  /* Input is aligned, let's leverage the speed advantage */\n            if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n                return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);\n            else\n                return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);\n    }   }\n\n    if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n        return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);\n    else\n        return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);\n#endif\n}\n\n\n/* **************************************************\n*  Advanced Hash Functions\n****************************************************/\n\nXXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)\n{\n    return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));\n}\nXXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)\n{\n    XXH_free(statePtr);\n    return XXH_OK;\n}\n\nXXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)\n{\n    return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));\n}\nXXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)\n{\n    XXH_free(statePtr);\n    return XXH_OK;\n}\n\n\n/*** Hash feed ***/\n\nXXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)\n{\n    XXH32_state_t state;   /* using a local state to memcpy() in order to avoid strict-aliasing warnings */\n    memset(&state, 0, sizeof(state)-4);   /* do not write into reserved, for future removal */\n    state.v1 = seed + PRIME32_1 + PRIME32_2;\n    state.v2 = seed + PRIME32_2;\n    state.v3 = seed + 0;\n    state.v4 = seed - PRIME32_1;\n    memcpy(statePtr, &state, sizeof(state));\n    return XXH_OK;\n}\n\n\nXXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)\n{\n    XXH64_state_t state;   /* using a local state to memcpy() in order to avoid strict-aliasing warnings */\n    memset(&state, 0, sizeof(state)-8);   /* do not write into reserved, for future removal */\n    state.v1 = seed + PRIME64_1 + PRIME64_2;\n    state.v2 = seed + PRIME64_2;\n    state.v3 = seed + 0;\n    state.v4 = seed - PRIME64_1;\n    memcpy(statePtr, &state, sizeof(state));\n    return XXH_OK;\n}\n\n\nFORCE_INLINE_TEMPLATE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian)\n{\n    const BYTE* p = (const BYTE*)input;\n    const BYTE* const bEnd = p + len;\n\n#ifdef XXH_ACCEPT_NULL_INPUT_POINTER\n    if (input==NULL) return XXH_ERROR;\n#endif\n\n    state->total_len_32 += (unsigned)len;\n    state->large_len |= (len>=16) | (state->total_len_32>=16);\n\n    if (state->memsize + len < 16)  {   /* fill in tmp buffer */\n        XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);\n        state->memsize += (unsigned)len;\n        return XXH_OK;\n    }\n\n    if (state->memsize) {   /* some data left from previous update */\n        XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);\n        {   const U32* p32 = state->mem32;\n            state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;\n            state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;\n            state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;\n            state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++;\n        }\n        p += 16-state->memsize;\n        state->memsize = 0;\n    }\n\n    if (p <= bEnd-16) {\n        const BYTE* const limit = bEnd - 16;\n        U32 v1 = state->v1;\n        U32 v2 = state->v2;\n        U32 v3 = state->v3;\n        U32 v4 = state->v4;\n\n        do {\n            v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;\n            v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;\n            v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;\n            v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;\n        } while (p<=limit);\n\n        state->v1 = v1;\n        state->v2 = v2;\n        state->v3 = v3;\n        state->v4 = v4;\n    }\n\n    if (p < bEnd) {\n        XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));\n        state->memsize = (unsigned)(bEnd-p);\n    }\n\n    return XXH_OK;\n}\n\nXXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)\n{\n    XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;\n\n    if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n        return XXH32_update_endian(state_in, input, len, XXH_littleEndian);\n    else\n        return XXH32_update_endian(state_in, input, len, XXH_bigEndian);\n}\n\n\n\nFORCE_INLINE_TEMPLATE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian)\n{\n    const BYTE * p = (const BYTE*)state->mem32;\n    const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize;\n    U32 h32;\n\n    if (state->large_len) {\n        h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);\n    } else {\n        h32 = state->v3 /* == seed */ + PRIME32_5;\n    }\n\n    h32 += state->total_len_32;\n\n    while (p+4<=bEnd) {\n        h32 += XXH_readLE32(p, endian) * PRIME32_3;\n        h32  = XXH_rotl32(h32, 17) * PRIME32_4;\n        p+=4;\n    }\n\n    while (p<bEnd) {\n        h32 += (*p) * PRIME32_5;\n        h32  = XXH_rotl32(h32, 11) * PRIME32_1;\n        p++;\n    }\n\n    h32 ^= h32 >> 15;\n    h32 *= PRIME32_2;\n    h32 ^= h32 >> 13;\n    h32 *= PRIME32_3;\n    h32 ^= h32 >> 16;\n\n    return h32;\n}\n\n\nXXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)\n{\n    XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;\n\n    if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n        return XXH32_digest_endian(state_in, XXH_littleEndian);\n    else\n        return XXH32_digest_endian(state_in, XXH_bigEndian);\n}\n\n\n\n/* **** XXH64 **** */\n\nFORCE_INLINE_TEMPLATE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)\n{\n    const BYTE* p = (const BYTE*)input;\n    const BYTE* const bEnd = p + len;\n\n#ifdef XXH_ACCEPT_NULL_INPUT_POINTER\n    if (input==NULL) return XXH_ERROR;\n#endif\n\n    state->total_len += len;\n\n    if (state->memsize + len < 32) {  /* fill in tmp buffer */\n        if (input != NULL) {\n            XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);\n        }\n        state->memsize += (U32)len;\n        return XXH_OK;\n    }\n\n    if (state->memsize) {   /* tmp buffer is full */\n        XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);\n        state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));\n        state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));\n        state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));\n        state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));\n        p += 32-state->memsize;\n        state->memsize = 0;\n    }\n\n    if (p+32 <= bEnd) {\n        const BYTE* const limit = bEnd - 32;\n        U64 v1 = state->v1;\n        U64 v2 = state->v2;\n        U64 v3 = state->v3;\n        U64 v4 = state->v4;\n\n        do {\n            v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;\n            v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;\n            v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;\n            v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;\n        } while (p<=limit);\n\n        state->v1 = v1;\n        state->v2 = v2;\n        state->v3 = v3;\n        state->v4 = v4;\n    }\n\n    if (p < bEnd) {\n        XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));\n        state->memsize = (unsigned)(bEnd-p);\n    }\n\n    return XXH_OK;\n}\n\nXXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)\n{\n    XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;\n\n    if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n        return XXH64_update_endian(state_in, input, len, XXH_littleEndian);\n    else\n        return XXH64_update_endian(state_in, input, len, XXH_bigEndian);\n}\n\n\n\nFORCE_INLINE_TEMPLATE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)\n{\n    const BYTE * p = (const BYTE*)state->mem64;\n    const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;\n    U64 h64;\n\n    if (state->total_len >= 32) {\n        U64 const v1 = state->v1;\n        U64 const v2 = state->v2;\n        U64 const v3 = state->v3;\n        U64 const v4 = state->v4;\n\n        h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);\n        h64 = XXH64_mergeRound(h64, v1);\n        h64 = XXH64_mergeRound(h64, v2);\n        h64 = XXH64_mergeRound(h64, v3);\n        h64 = XXH64_mergeRound(h64, v4);\n    } else {\n        h64  = state->v3 + PRIME64_5;\n    }\n\n    h64 += (U64) state->total_len;\n\n    while (p+8<=bEnd) {\n        U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));\n        h64 ^= k1;\n        h64  = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;\n        p+=8;\n    }\n\n    if (p+4<=bEnd) {\n        h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;\n        h64  = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;\n        p+=4;\n    }\n\n    while (p<bEnd) {\n        h64 ^= (*p) * PRIME64_5;\n        h64  = XXH_rotl64(h64, 11) * PRIME64_1;\n        p++;\n    }\n\n    h64 ^= h64 >> 33;\n    h64 *= PRIME64_2;\n    h64 ^= h64 >> 29;\n    h64 *= PRIME64_3;\n    h64 ^= h64 >> 32;\n\n    return h64;\n}\n\n\nXXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)\n{\n    XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;\n\n    if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)\n        return XXH64_digest_endian(state_in, XXH_littleEndian);\n    else\n        return XXH64_digest_endian(state_in, XXH_bigEndian);\n}\n\n\n/* **************************\n*  Canonical representation\n****************************/\n\n/*! Default XXH result types are basic unsigned 32 and 64 bits.\n*   The canonical representation follows human-readable write convention, aka big-endian (large digits first).\n*   These functions allow transformation of hash result into and from its canonical format.\n*   This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs.\n*/\n\nXXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)\n{\n    XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));\n    if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);\n    memcpy(dst, &hash, sizeof(*dst));\n}\n\nXXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)\n{\n    XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));\n    if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);\n    memcpy(dst, &hash, sizeof(*dst));\n}\n\nXXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)\n{\n    return XXH_readBE32(src);\n}\n\nXXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)\n{\n    return XXH_readBE64(src);\n}\n/**** ended inlining xxhash.c ****/\n#  endif\n\n#endif /* XXH_STATIC_LINKING_ONLY && XXH_STATIC_H_3543687687345 */\n\n\n#if defined (__cplusplus)\n}\n#endif\n/**** ended inlining xxhash.h ****/\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/* ---- static assert (debug) --- */\n#define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c)\n#define ZSTD_isError ERR_isError   /* for inlining */\n#define FSE_isError  ERR_isError\n#define HUF_isError  ERR_isError\n\n\n/*-*************************************\n*  shared macros\n***************************************/\n#undef MIN\n#undef MAX\n#define MIN(a,b) ((a)<(b) ? (a) : (b))\n#define MAX(a,b) ((a)>(b) ? (a) : (b))\n\n/**\n * Ignore: this is an internal helper.\n *\n * This is a helper function to help force C99-correctness during compilation.\n * Under strict compilation modes, variadic macro arguments can't be empty.\n * However, variadic function arguments can be. Using a function therefore lets\n * us statically check that at least one (string) argument was passed,\n * independent of the compilation flags.\n */\nstatic INLINE_KEYWORD UNUSED_ATTR\nvoid _force_has_format_string(const char *format, ...) {\n  (void)format;\n}\n\n/**\n * Ignore: this is an internal helper.\n *\n * We want to force this function invocation to be syntactically correct, but\n * we don't want to force runtime evaluation of its arguments.\n */\n#define _FORCE_HAS_FORMAT_STRING(...) \\\n  if (0) { \\\n    _force_has_format_string(__VA_ARGS__); \\\n  }\n\n/**\n * Return the specified error if the condition evaluates to true.\n *\n * In debug modes, prints additional information.\n * In order to do that (particularly, printing the conditional that failed),\n * this can't just wrap RETURN_ERROR().\n */\n#define RETURN_ERROR_IF(cond, err, ...) \\\n  if (cond) { \\\n    RAWLOG(3, \"%s:%d: ERROR!: check %s failed, returning %s\", \\\n           __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \\\n    _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \\\n    RAWLOG(3, \": \" __VA_ARGS__); \\\n    RAWLOG(3, \"\\n\"); \\\n    return ERROR(err); \\\n  }\n\n/**\n * Unconditionally return the specified error.\n *\n * In debug modes, prints additional information.\n */\n#define RETURN_ERROR(err, ...) \\\n  do { \\\n    RAWLOG(3, \"%s:%d: ERROR!: unconditional check failed, returning %s\", \\\n           __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \\\n    _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \\\n    RAWLOG(3, \": \" __VA_ARGS__); \\\n    RAWLOG(3, \"\\n\"); \\\n    return ERROR(err); \\\n  } while(0);\n\n/**\n * If the provided expression evaluates to an error code, returns that error code.\n *\n * In debug modes, prints additional information.\n */\n#define FORWARD_IF_ERROR(err, ...) \\\n  do { \\\n    size_t const err_code = (err); \\\n    if (ERR_isError(err_code)) { \\\n      RAWLOG(3, \"%s:%d: ERROR!: forwarding error in %s: %s\", \\\n             __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \\\n      _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \\\n      RAWLOG(3, \": \" __VA_ARGS__); \\\n      RAWLOG(3, \"\\n\"); \\\n      return err_code; \\\n    } \\\n  } while(0);\n\n\n/*-*************************************\n*  Common constants\n***************************************/\n#define ZSTD_OPT_NUM    (1<<12)\n\n#define ZSTD_REP_NUM      3                 /* number of repcodes */\n#define ZSTD_REP_MOVE     (ZSTD_REP_NUM-1)\nstatic const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };\n\n#define KB *(1 <<10)\n#define MB *(1 <<20)\n#define GB *(1U<<30)\n\n#define BIT7 128\n#define BIT6  64\n#define BIT5  32\n#define BIT4  16\n#define BIT1   2\n#define BIT0   1\n\n#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10\nstatic const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };\nstatic const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };\n\n#define ZSTD_FRAMEIDSIZE 4   /* magic number size */\n\n#define ZSTD_BLOCKHEADERSIZE 3   /* C standard doesn't allow `static const` variable to be init using another `static const` variable */\nstatic const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;\ntypedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;\n\n#define ZSTD_FRAMECHECKSUMSIZE 4\n\n#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */\n#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */)   /* for a non-null block */\n\n#define HufLog 12\ntypedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;\n\n#define LONGNBSEQ 0x7F00\n\n#define MINMATCH 3\n\n#define Litbits  8\n#define MaxLit ((1<<Litbits) - 1)\n#define MaxML   52\n#define MaxLL   35\n#define DefaultMaxOff 28\n#define MaxOff  31\n#define MaxSeq MAX(MaxLL, MaxML)   /* Assumption : MaxOff < MaxLL,MaxML */\n#define MLFSELog    9\n#define LLFSELog    9\n#define OffFSELog   8\n#define MaxFSELog  MAX(MAX(MLFSELog, LLFSELog), OffFSELog)\n\nstatic const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,\n                                      0, 0, 0, 0, 0, 0, 0, 0,\n                                      1, 1, 1, 1, 2, 2, 3, 3,\n                                      4, 6, 7, 8, 9,10,11,12,\n                                     13,14,15,16 };\nstatic const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,\n                                             2, 2, 2, 2, 2, 1, 1, 1,\n                                             2, 2, 2, 2, 2, 2, 2, 2,\n                                             2, 3, 2, 1, 1, 1, 1, 1,\n                                            -1,-1,-1,-1 };\n#define LL_DEFAULTNORMLOG 6  /* for static allocation */\nstatic const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;\n\nstatic const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,\n                                      0, 0, 0, 0, 0, 0, 0, 0,\n                                      0, 0, 0, 0, 0, 0, 0, 0,\n                                      0, 0, 0, 0, 0, 0, 0, 0,\n                                      1, 1, 1, 1, 2, 2, 3, 3,\n                                      4, 4, 5, 7, 8, 9,10,11,\n                                     12,13,14,15,16 };\nstatic const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,\n                                             2, 1, 1, 1, 1, 1, 1, 1,\n                                             1, 1, 1, 1, 1, 1, 1, 1,\n                                             1, 1, 1, 1, 1, 1, 1, 1,\n                                             1, 1, 1, 1, 1, 1, 1, 1,\n                                             1, 1, 1, 1, 1, 1,-1,-1,\n                                            -1,-1,-1,-1,-1 };\n#define ML_DEFAULTNORMLOG 6  /* for static allocation */\nstatic const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;\n\nstatic const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,\n                                                     2, 1, 1, 1, 1, 1, 1, 1,\n                                                     1, 1, 1, 1, 1, 1, 1, 1,\n                                                    -1,-1,-1,-1,-1 };\n#define OF_DEFAULTNORMLOG 5  /* for static allocation */\nstatic const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;\n\n\n/*-*******************************************\n*  Shared functions to include for inlining\n*********************************************/\nstatic void ZSTD_copy8(void* dst, const void* src) {\n#ifdef __aarch64__\n    vst1_u8((uint8_t*)dst, vld1_u8((const uint8_t*)src));\n#else\n    memcpy(dst, src, 8);\n#endif\n}\n\n#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }\nstatic void ZSTD_copy16(void* dst, const void* src) {\n#ifdef __aarch64__\n    vst1q_u8((uint8_t*)dst, vld1q_u8((const uint8_t*)src));\n#else\n    memcpy(dst, src, 16);\n#endif\n}\n#define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; }\n\n#define WILDCOPY_OVERLENGTH 32\n#define WILDCOPY_VECLEN 16\n\ntypedef enum {\n    ZSTD_no_overlap,\n    ZSTD_overlap_src_before_dst\n    /*  ZSTD_overlap_dst_before_src, */\n} ZSTD_overlap_e;\n\n/*! ZSTD_wildcopy() :\n *  Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0)\n *  @param ovtype controls the overlap detection\n *         - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.\n *         - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart.\n *           The src buffer must be before the dst buffer.\n */\nMEM_STATIC FORCE_INLINE_ATTR \nvoid ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype)\n{\n    ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src;\n    const BYTE* ip = (const BYTE*)src;\n    BYTE* op = (BYTE*)dst;\n    BYTE* const oend = op + length;\n\n    assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN));\n\n    if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) {\n        /* Handle short offset copies. */\n        do {\n            COPY8(op, ip)\n        } while (op < oend);\n    } else {\n        assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN);\n        /* Separate out the first COPY16() call because the copy length is\n         * almost certain to be short, so the branches have different\n         * probabilities. Since it is almost certain to be short, only do\n         * one COPY16() in the first call. Then, do two calls per loop since\n         * at that point it is more likely to have a high trip count.\n         */\n#ifndef __aarch64__\n        do {\n            COPY16(op, ip);\n        }\n        while (op < oend);\n#else\n        COPY16(op, ip);\n        if (op >= oend) return;\n        do {\n            COPY16(op, ip);\n            COPY16(op, ip);\n        }\n        while (op < oend);\n#endif\n    }\n}\n\nMEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)\n{\n    size_t const length = MIN(dstCapacity, srcSize);\n    if (length > 0) {\n        memcpy(dst, src, length);\n    }\n    return length;\n}\n\n/* define \"workspace is too large\" as this number of times larger than needed */\n#define ZSTD_WORKSPACETOOLARGE_FACTOR 3\n\n/* when workspace is continuously too large\n * during at least this number of times,\n * context's memory usage is considered wasteful,\n * because it's sized to handle a worst case scenario which rarely happens.\n * In which case, resize it down to free some memory */\n#define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128\n\n\n/*-*******************************************\n*  Private declarations\n*********************************************/\ntypedef struct seqDef_s {\n    U32 offset;\n    U16 litLength;\n    U16 matchLength;\n} seqDef;\n\ntypedef struct {\n    seqDef* sequencesStart;\n    seqDef* sequences;\n    BYTE* litStart;\n    BYTE* lit;\n    BYTE* llCode;\n    BYTE* mlCode;\n    BYTE* ofCode;\n    size_t maxNbSeq;\n    size_t maxNbLit;\n    U32   longLengthID;   /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */\n    U32   longLengthPos;\n} seqStore_t;\n\ntypedef struct {\n    U32 litLength;\n    U32 matchLength;\n} ZSTD_sequenceLength;\n\n/**\n * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences\n * indicated by longLengthPos and longLengthID, and adds MINMATCH back to matchLength.\n */\nMEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq)\n{\n    ZSTD_sequenceLength seqLen;\n    seqLen.litLength = seq->litLength;\n    seqLen.matchLength = seq->matchLength + MINMATCH;\n    if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) {\n        if (seqStore->longLengthID == 1) {\n            seqLen.litLength += 0xFFFF;\n        }\n        if (seqStore->longLengthID == 2) {\n            seqLen.matchLength += 0xFFFF;\n        }\n    }\n    return seqLen;\n}\n\n/**\n * Contains the compressed frame size and an upper-bound for the decompressed frame size.\n * Note: before using `compressedSize`, check for errors using ZSTD_isError().\n *       similarly, before using `decompressedBound`, check for errors using:\n *          `decompressedBound != ZSTD_CONTENTSIZE_ERROR`\n */\ntypedef struct {\n    size_t compressedSize;\n    unsigned long long decompressedBound;\n} ZSTD_frameSizeInfo;   /* decompress & legacy */\n\nconst seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);   /* compress & dictBuilder */\nvoid ZSTD_seqToCodes(const seqStore_t* seqStorePtr);   /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */\n\n/* custom memory allocation functions */\nvoid* ZSTD_malloc(size_t size, ZSTD_customMem customMem);\nvoid* ZSTD_calloc(size_t size, ZSTD_customMem customMem);\nvoid ZSTD_free(void* ptr, ZSTD_customMem customMem);\n\n\nMEM_STATIC U32 ZSTD_highbit32(U32 val)   /* compress, dictBuilder, decodeCorpus */\n{\n    assert(val != 0);\n    {\n#   if defined(_MSC_VER)   /* Visual */\n        unsigned long r=0;\n        return _BitScanReverse(&r, val) ? (unsigned)r : 0;\n#   elif defined(__GNUC__) && (__GNUC__ >= 3)   /* GCC Intrinsic */\n        return __builtin_clz (val) ^ 31;\n#   elif defined(__ICCARM__)    /* IAR Intrinsic */\n        return 31 - __CLZ(val);\n#   else   /* Software version */\n        static const U32 DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };\n        U32 v = val;\n        v |= v >> 1;\n        v |= v >> 2;\n        v |= v >> 4;\n        v |= v >> 8;\n        v |= v >> 16;\n        return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];\n#   endif\n    }\n}\n\n\n/* ZSTD_invalidateRepCodes() :\n * ensures next compression will not use repcodes from previous block.\n * Note : only works with regular variant;\n *        do not use with extDict variant ! */\nvoid ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);   /* zstdmt, adaptive_compression (shouldn't get this definition from here) */\n\n\ntypedef struct {\n    blockType_e blockType;\n    U32 lastBlock;\n    U32 origSize;\n} blockProperties_t;   /* declared here for decompress and fullbench */\n\n/*! ZSTD_getcBlockSize() :\n *  Provides the size of compressed block from block header `src` */\n/* Used by: decompress, fullbench (does not get its definition from here) */\nsize_t ZSTD_getcBlockSize(const void* src, size_t srcSize,\n                          blockProperties_t* bpPtr);\n\n/*! ZSTD_decodeSeqHeaders() :\n *  decode sequence header from src */\n/* Used by: decompress, fullbench (does not get its definition from here) */\nsize_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,\n                       const void* src, size_t srcSize);\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif   /* ZSTD_CCOMMON_H_MODULE */\n/**** ended inlining zstd_internal.h ****/\n\n\n/*-****************************************\n*  Version\n******************************************/\nunsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }\n\nconst char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }\n\n\n/*-****************************************\n*  ZSTD Error Management\n******************************************/\n#undef ZSTD_isError   /* defined within zstd_internal.h */\n/*! ZSTD_isError() :\n *  tells if a return value is an error code\n *  symbol is required for external callers */\nunsigned ZSTD_isError(size_t code) { return ERR_isError(code); }\n\n/*! ZSTD_getErrorName() :\n *  provides error code string from function result (useful for debugging) */\nconst char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }\n\n/*! ZSTD_getError() :\n *  convert a `size_t` function result into a proper ZSTD_errorCode enum */\nZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }\n\n/*! ZSTD_getErrorString() :\n *  provides error code string from enum */\nconst char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }\n\n\n\n/*=**************************************************************\n*  Custom allocator\n****************************************************************/\nvoid* ZSTD_malloc(size_t size, ZSTD_customMem customMem)\n{\n    if (customMem.customAlloc)\n        return customMem.customAlloc(customMem.opaque, size);\n    return malloc(size);\n}\n\nvoid* ZSTD_calloc(size_t size, ZSTD_customMem customMem)\n{\n    if (customMem.customAlloc) {\n        /* calloc implemented as malloc+memset;\n         * not as efficient as calloc, but next best guess for custom malloc */\n        void* const ptr = customMem.customAlloc(customMem.opaque, size);\n        memset(ptr, 0, size);\n        return ptr;\n    }\n    return calloc(1, size);\n}\n\nvoid ZSTD_free(void* ptr, ZSTD_customMem customMem)\n{\n    if (ptr!=NULL) {\n        if (customMem.customFree)\n            customMem.customFree(customMem.opaque, ptr);\n        else\n            free(ptr);\n    }\n}\n/**** ended inlining common/zstd_common.c ****/\n\n/**** start inlining decompress/huf_decompress.c ****/\n/* ******************************************************************\n * huff0 huffman decoder,\n * part of Finite State Entropy library\n * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.\n *\n *  You can contact the author at :\n *  - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n****************************************************************** */\n\n/* **************************************************************\n*  Dependencies\n****************************************************************/\n/**** skipping file: ../common/compiler.h ****/\n/**** skipping file: ../common/bitstream.h ****/\n/**** skipping file: ../common/fse.h ****/\n#define HUF_STATIC_LINKING_ONLY\n/**** skipping file: ../common/huf.h ****/\n/**** skipping file: ../common/error_private.h ****/\n\n/* **************************************************************\n*  Macros\n****************************************************************/\n\n/* These two optional macros force the use one way or another of the two\n * Huffman decompression implementations. You can't force in both directions\n * at the same time.\n */\n#if defined(HUF_FORCE_DECOMPRESS_X1) && \\\n    defined(HUF_FORCE_DECOMPRESS_X2)\n#error \"Cannot force the use of the X1 and X2 decoders at the same time!\"\n#endif\n\n\n/* **************************************************************\n*  Error Management\n****************************************************************/\n#define HUF_isError ERR_isError\n\n\n/* **************************************************************\n*  Byte alignment for workSpace management\n****************************************************************/\n#define HUF_ALIGN(x, a)         HUF_ALIGN_MASK((x), (a) - 1)\n#define HUF_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))\n\n\n/* **************************************************************\n*  BMI2 Variant Wrappers\n****************************************************************/\n#if DYNAMIC_BMI2\n\n#define HUF_DGEN(fn)                                                        \\\n                                                                            \\\n    static size_t fn##_default(                                             \\\n                  void* dst,  size_t dstSize,                               \\\n            const void* cSrc, size_t cSrcSize,                              \\\n            const HUF_DTable* DTable)                                       \\\n    {                                                                       \\\n        return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable);             \\\n    }                                                                       \\\n                                                                            \\\n    static TARGET_ATTRIBUTE(\"bmi2\") size_t fn##_bmi2(                       \\\n                  void* dst,  size_t dstSize,                               \\\n            const void* cSrc, size_t cSrcSize,                              \\\n            const HUF_DTable* DTable)                                       \\\n    {                                                                       \\\n        return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable);             \\\n    }                                                                       \\\n                                                                            \\\n    static size_t fn(void* dst, size_t dstSize, void const* cSrc,           \\\n                     size_t cSrcSize, HUF_DTable const* DTable, int bmi2)   \\\n    {                                                                       \\\n        if (bmi2) {                                                         \\\n            return fn##_bmi2(dst, dstSize, cSrc, cSrcSize, DTable);         \\\n        }                                                                   \\\n        return fn##_default(dst, dstSize, cSrc, cSrcSize, DTable);          \\\n    }\n\n#else\n\n#define HUF_DGEN(fn)                                                        \\\n    static size_t fn(void* dst, size_t dstSize, void const* cSrc,           \\\n                     size_t cSrcSize, HUF_DTable const* DTable, int bmi2)   \\\n    {                                                                       \\\n        (void)bmi2;                                                         \\\n        return fn##_body(dst, dstSize, cSrc, cSrcSize, DTable);             \\\n    }\n\n#endif\n\n\n/*-***************************/\n/*  generic DTableDesc       */\n/*-***************************/\ntypedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;\n\nstatic DTableDesc HUF_getDTableDesc(const HUF_DTable* table)\n{\n    DTableDesc dtd;\n    memcpy(&dtd, table, sizeof(dtd));\n    return dtd;\n}\n\n\n#ifndef HUF_FORCE_DECOMPRESS_X2\n\n/*-***************************/\n/*  single-symbol decoding   */\n/*-***************************/\ntypedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX1;   /* single-symbol decoding */\n\nsize_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize)\n{\n    U32 tableLog = 0;\n    U32 nbSymbols = 0;\n    size_t iSize;\n    void* const dtPtr = DTable + 1;\n    HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr;\n\n    U32* rankVal;\n    BYTE* huffWeight;\n    size_t spaceUsed32 = 0;\n\n    rankVal = (U32 *)workSpace + spaceUsed32;\n    spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;\n    huffWeight = (BYTE *)((U32 *)workSpace + spaceUsed32);\n    spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;\n\n    if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);\n\n    DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));\n    /* memset(huffWeight, 0, sizeof(huffWeight)); */   /* is not necessary, even though some analyzer complain ... */\n\n    iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);\n    if (HUF_isError(iSize)) return iSize;\n\n    /* Table header */\n    {   DTableDesc dtd = HUF_getDTableDesc(DTable);\n        if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge);   /* DTable too small, Huffman tree cannot fit in */\n        dtd.tableType = 0;\n        dtd.tableLog = (BYTE)tableLog;\n        memcpy(DTable, &dtd, sizeof(dtd));\n    }\n\n    /* Calculate starting value for each rank */\n    {   U32 n, nextRankStart = 0;\n        for (n=1; n<tableLog+1; n++) {\n            U32 const current = nextRankStart;\n            nextRankStart += (rankVal[n] << (n-1));\n            rankVal[n] = current;\n    }   }\n\n    /* fill DTable */\n    {   U32 n;\n        size_t const nEnd = nbSymbols;\n        for (n=0; n<nEnd; n++) {\n            size_t const w = huffWeight[n];\n            size_t const length = (1 << w) >> 1;\n            size_t const uStart = rankVal[w];\n            size_t const uEnd = uStart + length;\n            size_t u;\n            HUF_DEltX1 D;\n            D.byte = (BYTE)n;\n            D.nbBits = (BYTE)(tableLog + 1 - w);\n            rankVal[w] = (U32)uEnd;\n            if (length < 4) {\n                /* Use length in the loop bound so the compiler knows it is short. */\n                for (u = 0; u < length; ++u)\n                    dt[uStart + u] = D;\n            } else {\n                /* Unroll the loop 4 times, we know it is a power of 2. */\n                for (u = uStart; u < uEnd; u += 4) {\n                    dt[u + 0] = D;\n                    dt[u + 1] = D;\n                    dt[u + 2] = D;\n                    dt[u + 3] = D;\n    }   }   }   }\n    return iSize;\n}\n\nsize_t HUF_readDTableX1(HUF_DTable* DTable, const void* src, size_t srcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_readDTableX1_wksp(DTable, src, srcSize,\n                                 workSpace, sizeof(workSpace));\n}\n\nFORCE_INLINE_TEMPLATE BYTE\nHUF_decodeSymbolX1(BIT_DStream_t* Dstream, const HUF_DEltX1* dt, const U32 dtLog)\n{\n    size_t const val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */\n    BYTE const c = dt[val].byte;\n    BIT_skipBits(Dstream, dt[val].nbBits);\n    return c;\n}\n\n#define HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr) \\\n    *ptr++ = HUF_decodeSymbolX1(DStreamPtr, dt, dtLog)\n\n#define HUF_DECODE_SYMBOLX1_1(ptr, DStreamPtr)  \\\n    if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \\\n        HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr)\n\n#define HUF_DECODE_SYMBOLX1_2(ptr, DStreamPtr) \\\n    if (MEM_64bits()) \\\n        HUF_DECODE_SYMBOLX1_0(ptr, DStreamPtr)\n\nHINT_INLINE size_t\nHUF_decodeStreamX1(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX1* const dt, const U32 dtLog)\n{\n    BYTE* const pStart = p;\n\n    /* up to 4 symbols at a time */\n    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-3)) {\n        HUF_DECODE_SYMBOLX1_2(p, bitDPtr);\n        HUF_DECODE_SYMBOLX1_1(p, bitDPtr);\n        HUF_DECODE_SYMBOLX1_2(p, bitDPtr);\n        HUF_DECODE_SYMBOLX1_0(p, bitDPtr);\n    }\n\n    /* [0-3] symbols remaining */\n    if (MEM_32bits())\n        while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd))\n            HUF_DECODE_SYMBOLX1_0(p, bitDPtr);\n\n    /* no more data to retrieve from bitstream, no need to reload */\n    while (p < pEnd)\n        HUF_DECODE_SYMBOLX1_0(p, bitDPtr);\n\n    return pEnd-pStart;\n}\n\nFORCE_INLINE_TEMPLATE size_t\nHUF_decompress1X1_usingDTable_internal_body(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    BYTE* op = (BYTE*)dst;\n    BYTE* const oend = op + dstSize;\n    const void* dtPtr = DTable + 1;\n    const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;\n    BIT_DStream_t bitD;\n    DTableDesc const dtd = HUF_getDTableDesc(DTable);\n    U32 const dtLog = dtd.tableLog;\n\n    CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );\n\n    HUF_decodeStreamX1(op, &bitD, oend, dt, dtLog);\n\n    if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);\n\n    return dstSize;\n}\n\nFORCE_INLINE_TEMPLATE size_t\nHUF_decompress4X1_usingDTable_internal_body(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    /* Check */\n    if (cSrcSize < 10) return ERROR(corruption_detected);  /* strict minimum : jump table + 1 byte per stream */\n\n    {   const BYTE* const istart = (const BYTE*) cSrc;\n        BYTE* const ostart = (BYTE*) dst;\n        BYTE* const oend = ostart + dstSize;\n        BYTE* const olimit = oend - 3;\n        const void* const dtPtr = DTable + 1;\n        const HUF_DEltX1* const dt = (const HUF_DEltX1*)dtPtr;\n\n        /* Init */\n        BIT_DStream_t bitD1;\n        BIT_DStream_t bitD2;\n        BIT_DStream_t bitD3;\n        BIT_DStream_t bitD4;\n        size_t const length1 = MEM_readLE16(istart);\n        size_t const length2 = MEM_readLE16(istart+2);\n        size_t const length3 = MEM_readLE16(istart+4);\n        size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);\n        const BYTE* const istart1 = istart + 6;  /* jumpTable */\n        const BYTE* const istart2 = istart1 + length1;\n        const BYTE* const istart3 = istart2 + length2;\n        const BYTE* const istart4 = istart3 + length3;\n        const size_t segmentSize = (dstSize+3) / 4;\n        BYTE* const opStart2 = ostart + segmentSize;\n        BYTE* const opStart3 = opStart2 + segmentSize;\n        BYTE* const opStart4 = opStart3 + segmentSize;\n        BYTE* op1 = ostart;\n        BYTE* op2 = opStart2;\n        BYTE* op3 = opStart3;\n        BYTE* op4 = opStart4;\n        DTableDesc const dtd = HUF_getDTableDesc(DTable);\n        U32 const dtLog = dtd.tableLog;\n        U32 endSignal = 1;\n\n        if (length4 > cSrcSize) return ERROR(corruption_detected);   /* overflow */\n        CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );\n        CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );\n        CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );\n        CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );\n\n        /* up to 16 symbols per loop (4 symbols per stream) in 64-bit mode */\n        for ( ; (endSignal) & (op4 < olimit) ; ) {\n            HUF_DECODE_SYMBOLX1_2(op1, &bitD1);\n            HUF_DECODE_SYMBOLX1_2(op2, &bitD2);\n            HUF_DECODE_SYMBOLX1_2(op3, &bitD3);\n            HUF_DECODE_SYMBOLX1_2(op4, &bitD4);\n            HUF_DECODE_SYMBOLX1_1(op1, &bitD1);\n            HUF_DECODE_SYMBOLX1_1(op2, &bitD2);\n            HUF_DECODE_SYMBOLX1_1(op3, &bitD3);\n            HUF_DECODE_SYMBOLX1_1(op4, &bitD4);\n            HUF_DECODE_SYMBOLX1_2(op1, &bitD1);\n            HUF_DECODE_SYMBOLX1_2(op2, &bitD2);\n            HUF_DECODE_SYMBOLX1_2(op3, &bitD3);\n            HUF_DECODE_SYMBOLX1_2(op4, &bitD4);\n            HUF_DECODE_SYMBOLX1_0(op1, &bitD1);\n            HUF_DECODE_SYMBOLX1_0(op2, &bitD2);\n            HUF_DECODE_SYMBOLX1_0(op3, &bitD3);\n            HUF_DECODE_SYMBOLX1_0(op4, &bitD4);\n            endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;\n            endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;\n            endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;\n            endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;\n        }\n\n        /* check corruption */\n        /* note : should not be necessary : op# advance in lock step, and we control op4.\n         *        but curiously, binary generated by gcc 7.2 & 7.3 with -mbmi2 runs faster when >=1 test is present */\n        if (op1 > opStart2) return ERROR(corruption_detected);\n        if (op2 > opStart3) return ERROR(corruption_detected);\n        if (op3 > opStart4) return ERROR(corruption_detected);\n        /* note : op4 supposed already verified within main loop */\n\n        /* finish bitStreams one by one */\n        HUF_decodeStreamX1(op1, &bitD1, opStart2, dt, dtLog);\n        HUF_decodeStreamX1(op2, &bitD2, opStart3, dt, dtLog);\n        HUF_decodeStreamX1(op3, &bitD3, opStart4, dt, dtLog);\n        HUF_decodeStreamX1(op4, &bitD4, oend,     dt, dtLog);\n\n        /* check */\n        { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);\n          if (!endCheck) return ERROR(corruption_detected); }\n\n        /* decoded size */\n        return dstSize;\n    }\n}\n\n\ntypedef size_t (*HUF_decompress_usingDTable_t)(void *dst, size_t dstSize,\n                                               const void *cSrc,\n                                               size_t cSrcSize,\n                                               const HUF_DTable *DTable);\n\nHUF_DGEN(HUF_decompress1X1_usingDTable_internal)\nHUF_DGEN(HUF_decompress4X1_usingDTable_internal)\n\n\n\nsize_t HUF_decompress1X1_usingDTable(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    DTableDesc dtd = HUF_getDTableDesc(DTable);\n    if (dtd.tableType != 0) return ERROR(GENERIC);\n    return HUF_decompress1X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n}\n\nsize_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,\n                                   const void* cSrc, size_t cSrcSize,\n                                   void* workSpace, size_t wkspSize)\n{\n    const BYTE* ip = (const BYTE*) cSrc;\n\n    size_t const hSize = HUF_readDTableX1_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize);\n    if (HUF_isError(hSize)) return hSize;\n    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);\n    ip += hSize; cSrcSize -= hSize;\n\n    return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0);\n}\n\n\nsize_t HUF_decompress1X1_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,\n                              const void* cSrc, size_t cSrcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_decompress1X1_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,\n                                       workSpace, sizeof(workSpace));\n}\n\nsize_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);\n    return HUF_decompress1X1_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);\n}\n\nsize_t HUF_decompress4X1_usingDTable(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    DTableDesc dtd = HUF_getDTableDesc(DTable);\n    if (dtd.tableType != 0) return ERROR(GENERIC);\n    return HUF_decompress4X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n}\n\nstatic size_t HUF_decompress4X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize,\n                                   const void* cSrc, size_t cSrcSize,\n                                   void* workSpace, size_t wkspSize, int bmi2)\n{\n    const BYTE* ip = (const BYTE*) cSrc;\n\n    size_t const hSize = HUF_readDTableX1_wksp (dctx, cSrc, cSrcSize,\n                                                workSpace, wkspSize);\n    if (HUF_isError(hSize)) return hSize;\n    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);\n    ip += hSize; cSrcSize -= hSize;\n\n    return HUF_decompress4X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);\n}\n\nsize_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,\n                                   const void* cSrc, size_t cSrcSize,\n                                   void* workSpace, size_t wkspSize)\n{\n    return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, 0);\n}\n\n\nsize_t HUF_decompress4X1_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,\n                                       workSpace, sizeof(workSpace));\n}\nsize_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);\n    return HUF_decompress4X1_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);\n}\n\n#endif /* HUF_FORCE_DECOMPRESS_X2 */\n\n\n#ifndef HUF_FORCE_DECOMPRESS_X1\n\n/* *************************/\n/* double-symbols decoding */\n/* *************************/\n\ntypedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX2;  /* double-symbols decoding */\ntypedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;\ntypedef U32 rankValCol_t[HUF_TABLELOG_MAX + 1];\ntypedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];\n\n\n/* HUF_fillDTableX2Level2() :\n * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */\nstatic void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 sizeLog, const U32 consumed,\n                           const U32* rankValOrigin, const int minWeight,\n                           const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,\n                           U32 nbBitsBaseline, U16 baseSeq)\n{\n    HUF_DEltX2 DElt;\n    U32 rankVal[HUF_TABLELOG_MAX + 1];\n\n    /* get pre-calculated rankVal */\n    memcpy(rankVal, rankValOrigin, sizeof(rankVal));\n\n    /* fill skipped values */\n    if (minWeight>1) {\n        U32 i, skipSize = rankVal[minWeight];\n        MEM_writeLE16(&(DElt.sequence), baseSeq);\n        DElt.nbBits   = (BYTE)(consumed);\n        DElt.length   = 1;\n        for (i = 0; i < skipSize; i++)\n            DTable[i] = DElt;\n    }\n\n    /* fill DTable */\n    {   U32 s; for (s=0; s<sortedListSize; s++) {   /* note : sortedSymbols already skipped */\n            const U32 symbol = sortedSymbols[s].symbol;\n            const U32 weight = sortedSymbols[s].weight;\n            const U32 nbBits = nbBitsBaseline - weight;\n            const U32 length = 1 << (sizeLog-nbBits);\n            const U32 start = rankVal[weight];\n            U32 i = start;\n            const U32 end = start + length;\n\n            MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));\n            DElt.nbBits = (BYTE)(nbBits + consumed);\n            DElt.length = 2;\n            do { DTable[i++] = DElt; } while (i<end);   /* since length >= 1 */\n\n            rankVal[weight] += length;\n    }   }\n}\n\n\nstatic void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,\n                           const sortedSymbol_t* sortedList, const U32 sortedListSize,\n                           const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,\n                           const U32 nbBitsBaseline)\n{\n    U32 rankVal[HUF_TABLELOG_MAX + 1];\n    const int scaleLog = nbBitsBaseline - targetLog;   /* note : targetLog >= srcLog, hence scaleLog <= 1 */\n    const U32 minBits  = nbBitsBaseline - maxWeight;\n    U32 s;\n\n    memcpy(rankVal, rankValOrigin, sizeof(rankVal));\n\n    /* fill DTable */\n    for (s=0; s<sortedListSize; s++) {\n        const U16 symbol = sortedList[s].symbol;\n        const U32 weight = sortedList[s].weight;\n        const U32 nbBits = nbBitsBaseline - weight;\n        const U32 start = rankVal[weight];\n        const U32 length = 1 << (targetLog-nbBits);\n\n        if (targetLog-nbBits >= minBits) {   /* enough room for a second symbol */\n            U32 sortedRank;\n            int minWeight = nbBits + scaleLog;\n            if (minWeight < 1) minWeight = 1;\n            sortedRank = rankStart[minWeight];\n            HUF_fillDTableX2Level2(DTable+start, targetLog-nbBits, nbBits,\n                           rankValOrigin[nbBits], minWeight,\n                           sortedList+sortedRank, sortedListSize-sortedRank,\n                           nbBitsBaseline, symbol);\n        } else {\n            HUF_DEltX2 DElt;\n            MEM_writeLE16(&(DElt.sequence), symbol);\n            DElt.nbBits = (BYTE)(nbBits);\n            DElt.length = 1;\n            {   U32 const end = start + length;\n                U32 u;\n                for (u = start; u < end; u++) DTable[u] = DElt;\n        }   }\n        rankVal[weight] += length;\n    }\n}\n\nsize_t HUF_readDTableX2_wksp(HUF_DTable* DTable,\n                       const void* src, size_t srcSize,\n                             void* workSpace, size_t wkspSize)\n{\n    U32 tableLog, maxW, sizeOfSort, nbSymbols;\n    DTableDesc dtd = HUF_getDTableDesc(DTable);\n    U32 const maxTableLog = dtd.maxTableLog;\n    size_t iSize;\n    void* dtPtr = DTable+1;   /* force compiler to avoid strict-aliasing */\n    HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;\n    U32 *rankStart;\n\n    rankValCol_t* rankVal;\n    U32* rankStats;\n    U32* rankStart0;\n    sortedSymbol_t* sortedSymbol;\n    BYTE* weightList;\n    size_t spaceUsed32 = 0;\n\n    rankVal = (rankValCol_t *)((U32 *)workSpace + spaceUsed32);\n    spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;\n    rankStats = (U32 *)workSpace + spaceUsed32;\n    spaceUsed32 += HUF_TABLELOG_MAX + 1;\n    rankStart0 = (U32 *)workSpace + spaceUsed32;\n    spaceUsed32 += HUF_TABLELOG_MAX + 2;\n    sortedSymbol = (sortedSymbol_t *)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t);\n    spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;\n    weightList = (BYTE *)((U32 *)workSpace + spaceUsed32);\n    spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;\n\n    if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);\n\n    rankStart = rankStart0 + 1;\n    memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));\n\n    DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(HUF_DTable));   /* if compiler fails here, assertion is wrong */\n    if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);\n    /* memset(weightList, 0, sizeof(weightList)); */  /* is not necessary, even though some analyzer complain ... */\n\n    iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);\n    if (HUF_isError(iSize)) return iSize;\n\n    /* check result */\n    if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge);   /* DTable can't fit code depth */\n\n    /* find maxWeight */\n    for (maxW = tableLog; rankStats[maxW]==0; maxW--) {}  /* necessarily finds a solution before 0 */\n\n    /* Get start index of each weight */\n    {   U32 w, nextRankStart = 0;\n        for (w=1; w<maxW+1; w++) {\n            U32 current = nextRankStart;\n            nextRankStart += rankStats[w];\n            rankStart[w] = current;\n        }\n        rankStart[0] = nextRankStart;   /* put all 0w symbols at the end of sorted list*/\n        sizeOfSort = nextRankStart;\n    }\n\n    /* sort symbols by weight */\n    {   U32 s;\n        for (s=0; s<nbSymbols; s++) {\n            U32 const w = weightList[s];\n            U32 const r = rankStart[w]++;\n            sortedSymbol[r].symbol = (BYTE)s;\n            sortedSymbol[r].weight = (BYTE)w;\n        }\n        rankStart[0] = 0;   /* forget 0w symbols; this is beginning of weight(1) */\n    }\n\n    /* Build rankVal */\n    {   U32* const rankVal0 = rankVal[0];\n        {   int const rescale = (maxTableLog-tableLog) - 1;   /* tableLog <= maxTableLog */\n            U32 nextRankVal = 0;\n            U32 w;\n            for (w=1; w<maxW+1; w++) {\n                U32 current = nextRankVal;\n                nextRankVal += rankStats[w] << (w+rescale);\n                rankVal0[w] = current;\n        }   }\n        {   U32 const minBits = tableLog+1 - maxW;\n            U32 consumed;\n            for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {\n                U32* const rankValPtr = rankVal[consumed];\n                U32 w;\n                for (w = 1; w < maxW+1; w++) {\n                    rankValPtr[w] = rankVal0[w] >> consumed;\n    }   }   }   }\n\n    HUF_fillDTableX2(dt, maxTableLog,\n                   sortedSymbol, sizeOfSort,\n                   rankStart0, rankVal, maxW,\n                   tableLog+1);\n\n    dtd.tableLog = (BYTE)maxTableLog;\n    dtd.tableType = 1;\n    memcpy(DTable, &dtd, sizeof(dtd));\n    return iSize;\n}\n\nsize_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize)\n{\n  U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n  return HUF_readDTableX2_wksp(DTable, src, srcSize,\n                               workSpace, sizeof(workSpace));\n}\n\n\nFORCE_INLINE_TEMPLATE U32\nHUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)\n{\n    size_t const val = BIT_lookBitsFast(DStream, dtLog);   /* note : dtLog >= 1 */\n    memcpy(op, dt+val, 2);\n    BIT_skipBits(DStream, dt[val].nbBits);\n    return dt[val].length;\n}\n\nFORCE_INLINE_TEMPLATE U32\nHUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)\n{\n    size_t const val = BIT_lookBitsFast(DStream, dtLog);   /* note : dtLog >= 1 */\n    memcpy(op, dt+val, 1);\n    if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);\n    else {\n        if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {\n            BIT_skipBits(DStream, dt[val].nbBits);\n            if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))\n                /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */\n                DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8);\n    }   }\n    return 1;\n}\n\n#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \\\n    ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)\n\n#define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \\\n    if (MEM_64bits() || (HUF_TABLELOG_MAX<=12)) \\\n        ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)\n\n#define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \\\n    if (MEM_64bits()) \\\n        ptr += HUF_decodeSymbolX2(ptr, DStreamPtr, dt, dtLog)\n\nHINT_INLINE size_t\nHUF_decodeStreamX2(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd,\n                const HUF_DEltX2* const dt, const U32 dtLog)\n{\n    BYTE* const pStart = p;\n\n    /* up to 8 symbols at a time */\n    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p < pEnd-(sizeof(bitDPtr->bitContainer)-1))) {\n        HUF_DECODE_SYMBOLX2_2(p, bitDPtr);\n        HUF_DECODE_SYMBOLX2_1(p, bitDPtr);\n        HUF_DECODE_SYMBOLX2_2(p, bitDPtr);\n        HUF_DECODE_SYMBOLX2_0(p, bitDPtr);\n    }\n\n    /* closer to end : up to 2 symbols at a time */\n    while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) & (p <= pEnd-2))\n        HUF_DECODE_SYMBOLX2_0(p, bitDPtr);\n\n    while (p <= pEnd-2)\n        HUF_DECODE_SYMBOLX2_0(p, bitDPtr);   /* no need to reload : reached the end of DStream */\n\n    if (p < pEnd)\n        p += HUF_decodeLastSymbolX2(p, bitDPtr, dt, dtLog);\n\n    return p-pStart;\n}\n\nFORCE_INLINE_TEMPLATE size_t\nHUF_decompress1X2_usingDTable_internal_body(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    BIT_DStream_t bitD;\n\n    /* Init */\n    CHECK_F( BIT_initDStream(&bitD, cSrc, cSrcSize) );\n\n    /* decode */\n    {   BYTE* const ostart = (BYTE*) dst;\n        BYTE* const oend = ostart + dstSize;\n        const void* const dtPtr = DTable+1;   /* force compiler to not use strict-aliasing */\n        const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;\n        DTableDesc const dtd = HUF_getDTableDesc(DTable);\n        HUF_decodeStreamX2(ostart, &bitD, oend, dt, dtd.tableLog);\n    }\n\n    /* check */\n    if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);\n\n    /* decoded size */\n    return dstSize;\n}\n\nFORCE_INLINE_TEMPLATE size_t\nHUF_decompress4X2_usingDTable_internal_body(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    if (cSrcSize < 10) return ERROR(corruption_detected);   /* strict minimum : jump table + 1 byte per stream */\n\n    {   const BYTE* const istart = (const BYTE*) cSrc;\n        BYTE* const ostart = (BYTE*) dst;\n        BYTE* const oend = ostart + dstSize;\n        BYTE* const olimit = oend - (sizeof(size_t)-1);\n        const void* const dtPtr = DTable+1;\n        const HUF_DEltX2* const dt = (const HUF_DEltX2*)dtPtr;\n\n        /* Init */\n        BIT_DStream_t bitD1;\n        BIT_DStream_t bitD2;\n        BIT_DStream_t bitD3;\n        BIT_DStream_t bitD4;\n        size_t const length1 = MEM_readLE16(istart);\n        size_t const length2 = MEM_readLE16(istart+2);\n        size_t const length3 = MEM_readLE16(istart+4);\n        size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);\n        const BYTE* const istart1 = istart + 6;  /* jumpTable */\n        const BYTE* const istart2 = istart1 + length1;\n        const BYTE* const istart3 = istart2 + length2;\n        const BYTE* const istart4 = istart3 + length3;\n        size_t const segmentSize = (dstSize+3) / 4;\n        BYTE* const opStart2 = ostart + segmentSize;\n        BYTE* const opStart3 = opStart2 + segmentSize;\n        BYTE* const opStart4 = opStart3 + segmentSize;\n        BYTE* op1 = ostart;\n        BYTE* op2 = opStart2;\n        BYTE* op3 = opStart3;\n        BYTE* op4 = opStart4;\n        U32 endSignal = 1;\n        DTableDesc const dtd = HUF_getDTableDesc(DTable);\n        U32 const dtLog = dtd.tableLog;\n\n        if (length4 > cSrcSize) return ERROR(corruption_detected);   /* overflow */\n        CHECK_F( BIT_initDStream(&bitD1, istart1, length1) );\n        CHECK_F( BIT_initDStream(&bitD2, istart2, length2) );\n        CHECK_F( BIT_initDStream(&bitD3, istart3, length3) );\n        CHECK_F( BIT_initDStream(&bitD4, istart4, length4) );\n\n        /* 16-32 symbols per loop (4-8 symbols per stream) */\n        for ( ; (endSignal) & (op4 < olimit); ) {\n#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))\n            HUF_DECODE_SYMBOLX2_2(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_1(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_2(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_0(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_2(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_1(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_2(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_0(op2, &bitD2);\n            endSignal &= BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished;\n            endSignal &= BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished;\n            HUF_DECODE_SYMBOLX2_2(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_1(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_2(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_0(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_2(op4, &bitD4);\n            HUF_DECODE_SYMBOLX2_1(op4, &bitD4);\n            HUF_DECODE_SYMBOLX2_2(op4, &bitD4);\n            HUF_DECODE_SYMBOLX2_0(op4, &bitD4);\n            endSignal &= BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished;\n            endSignal &= BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished;\n#else\n            HUF_DECODE_SYMBOLX2_2(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_2(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_2(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_2(op4, &bitD4);\n            HUF_DECODE_SYMBOLX2_1(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_1(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_1(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_1(op4, &bitD4);\n            HUF_DECODE_SYMBOLX2_2(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_2(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_2(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_2(op4, &bitD4);\n            HUF_DECODE_SYMBOLX2_0(op1, &bitD1);\n            HUF_DECODE_SYMBOLX2_0(op2, &bitD2);\n            HUF_DECODE_SYMBOLX2_0(op3, &bitD3);\n            HUF_DECODE_SYMBOLX2_0(op4, &bitD4);\n            endSignal = (U32)LIKELY(\n                        (BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished)\n                      & (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished)\n                      & (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished)\n                      & (BIT_reloadDStreamFast(&bitD4) == BIT_DStream_unfinished));\n#endif\n        }\n\n        /* check corruption */\n        if (op1 > opStart2) return ERROR(corruption_detected);\n        if (op2 > opStart3) return ERROR(corruption_detected);\n        if (op3 > opStart4) return ERROR(corruption_detected);\n        /* note : op4 already verified within main loop */\n\n        /* finish bitStreams one by one */\n        HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);\n        HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);\n        HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);\n        HUF_decodeStreamX2(op4, &bitD4, oend,     dt, dtLog);\n\n        /* check */\n        { U32 const endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);\n          if (!endCheck) return ERROR(corruption_detected); }\n\n        /* decoded size */\n        return dstSize;\n    }\n}\n\nHUF_DGEN(HUF_decompress1X2_usingDTable_internal)\nHUF_DGEN(HUF_decompress4X2_usingDTable_internal)\n\nsize_t HUF_decompress1X2_usingDTable(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    DTableDesc dtd = HUF_getDTableDesc(DTable);\n    if (dtd.tableType != 1) return ERROR(GENERIC);\n    return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n}\n\nsize_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,\n                                   const void* cSrc, size_t cSrcSize,\n                                   void* workSpace, size_t wkspSize)\n{\n    const BYTE* ip = (const BYTE*) cSrc;\n\n    size_t const hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize,\n                                               workSpace, wkspSize);\n    if (HUF_isError(hSize)) return hSize;\n    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);\n    ip += hSize; cSrcSize -= hSize;\n\n    return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, /* bmi2 */ 0);\n}\n\n\nsize_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,\n                              const void* cSrc, size_t cSrcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,\n                                       workSpace, sizeof(workSpace));\n}\n\nsize_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);\n    return HUF_decompress1X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);\n}\n\nsize_t HUF_decompress4X2_usingDTable(\n          void* dst,  size_t dstSize,\n    const void* cSrc, size_t cSrcSize,\n    const HUF_DTable* DTable)\n{\n    DTableDesc dtd = HUF_getDTableDesc(DTable);\n    if (dtd.tableType != 1) return ERROR(GENERIC);\n    return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n}\n\nstatic size_t HUF_decompress4X2_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize,\n                                   const void* cSrc, size_t cSrcSize,\n                                   void* workSpace, size_t wkspSize, int bmi2)\n{\n    const BYTE* ip = (const BYTE*) cSrc;\n\n    size_t hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize,\n                                         workSpace, wkspSize);\n    if (HUF_isError(hSize)) return hSize;\n    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);\n    ip += hSize; cSrcSize -= hSize;\n\n    return HUF_decompress4X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);\n}\n\nsize_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,\n                                   const void* cSrc, size_t cSrcSize,\n                                   void* workSpace, size_t wkspSize)\n{\n    return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, /* bmi2 */ 0);\n}\n\n\nsize_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,\n                              const void* cSrc, size_t cSrcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,\n                                       workSpace, sizeof(workSpace));\n}\n\nsize_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);\n    return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);\n}\n\n#endif /* HUF_FORCE_DECOMPRESS_X1 */\n\n\n/* ***********************************/\n/* Universal decompression selectors */\n/* ***********************************/\n\nsize_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize,\n                                    const void* cSrc, size_t cSrcSize,\n                                    const HUF_DTable* DTable)\n{\n    DTableDesc const dtd = HUF_getDTableDesc(DTable);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n    (void)dtd;\n    assert(dtd.tableType == 0);\n    return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n    (void)dtd;\n    assert(dtd.tableType == 1);\n    return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n#else\n    return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0) :\n                           HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n#endif\n}\n\nsize_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize,\n                                    const void* cSrc, size_t cSrcSize,\n                                    const HUF_DTable* DTable)\n{\n    DTableDesc const dtd = HUF_getDTableDesc(DTable);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n    (void)dtd;\n    assert(dtd.tableType == 0);\n    return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n    (void)dtd;\n    assert(dtd.tableType == 1);\n    return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n#else\n    return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0) :\n                           HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, /* bmi2 */ 0);\n#endif\n}\n\n\n#if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)\ntypedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;\nstatic const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =\n{\n    /* single, double, quad */\n    {{0,0}, {1,1}, {2,2}},  /* Q==0 : impossible */\n    {{0,0}, {1,1}, {2,2}},  /* Q==1 : impossible */\n    {{  38,130}, {1313, 74}, {2151, 38}},   /* Q == 2 : 12-18% */\n    {{ 448,128}, {1353, 74}, {2238, 41}},   /* Q == 3 : 18-25% */\n    {{ 556,128}, {1353, 74}, {2238, 47}},   /* Q == 4 : 25-32% */\n    {{ 714,128}, {1418, 74}, {2436, 53}},   /* Q == 5 : 32-38% */\n    {{ 883,128}, {1437, 74}, {2464, 61}},   /* Q == 6 : 38-44% */\n    {{ 897,128}, {1515, 75}, {2622, 68}},   /* Q == 7 : 44-50% */\n    {{ 926,128}, {1613, 75}, {2730, 75}},   /* Q == 8 : 50-56% */\n    {{ 947,128}, {1729, 77}, {3359, 77}},   /* Q == 9 : 56-62% */\n    {{1107,128}, {2083, 81}, {4006, 84}},   /* Q ==10 : 62-69% */\n    {{1177,128}, {2379, 87}, {4785, 88}},   /* Q ==11 : 69-75% */\n    {{1242,128}, {2415, 93}, {5155, 84}},   /* Q ==12 : 75-81% */\n    {{1349,128}, {2644,106}, {5260,106}},   /* Q ==13 : 81-87% */\n    {{1455,128}, {2422,124}, {4174,124}},   /* Q ==14 : 87-93% */\n    {{ 722,128}, {1891,145}, {1936,146}},   /* Q ==15 : 93-99% */\n};\n#endif\n\n/** HUF_selectDecoder() :\n *  Tells which decoder is likely to decode faster,\n *  based on a set of pre-computed metrics.\n * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .\n *  Assumption : 0 < dstSize <= 128 KB */\nU32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)\n{\n    assert(dstSize > 0);\n    assert(dstSize <= 128*1024);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n    (void)dstSize;\n    (void)cSrcSize;\n    return 0;\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n    (void)dstSize;\n    (void)cSrcSize;\n    return 1;\n#else\n    /* decoder timing evaluation */\n    {   U32 const Q = (cSrcSize >= dstSize) ? 15 : (U32)(cSrcSize * 16 / dstSize);   /* Q < 16 */\n        U32 const D256 = (U32)(dstSize >> 8);\n        U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);\n        U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);\n        DTime1 += DTime1 >> 3;  /* advantage to algorithm using less memory, to reduce cache eviction */\n        return DTime1 < DTime0;\n    }\n#endif\n}\n\n\ntypedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);\n\nsize_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n#if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)\n    static const decompressionAlgo decompress[2] = { HUF_decompress4X1, HUF_decompress4X2 };\n#endif\n\n    /* validation checks */\n    if (dstSize == 0) return ERROR(dstSize_tooSmall);\n    if (cSrcSize > dstSize) return ERROR(corruption_detected);   /* invalid */\n    if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; }   /* not compressed */\n    if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; }   /* RLE */\n\n    {   U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n        (void)algoNb;\n        assert(algoNb == 0);\n        return HUF_decompress4X1(dst, dstSize, cSrc, cSrcSize);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n        (void)algoNb;\n        assert(algoNb == 1);\n        return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize);\n#else\n        return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);\n#endif\n    }\n}\n\nsize_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    /* validation checks */\n    if (dstSize == 0) return ERROR(dstSize_tooSmall);\n    if (cSrcSize > dstSize) return ERROR(corruption_detected);   /* invalid */\n    if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; }   /* not compressed */\n    if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; }   /* RLE */\n\n    {   U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n        (void)algoNb;\n        assert(algoNb == 0);\n        return HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n        (void)algoNb;\n        assert(algoNb == 1);\n        return HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);\n#else\n        return algoNb ? HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :\n                        HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;\n#endif\n    }\n}\n\nsize_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize,\n                                         workSpace, sizeof(workSpace));\n}\n\n\nsize_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst,\n                                     size_t dstSize, const void* cSrc,\n                                     size_t cSrcSize, void* workSpace,\n                                     size_t wkspSize)\n{\n    /* validation checks */\n    if (dstSize == 0) return ERROR(dstSize_tooSmall);\n    if (cSrcSize == 0) return ERROR(corruption_detected);\n\n    {   U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n        (void)algoNb;\n        assert(algoNb == 0);\n        return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n        (void)algoNb;\n        assert(algoNb == 1);\n        return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);\n#else\n        return algoNb ? HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc,\n                            cSrcSize, workSpace, wkspSize):\n                        HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize);\n#endif\n    }\n}\n\nsize_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,\n                                  const void* cSrc, size_t cSrcSize,\n                                  void* workSpace, size_t wkspSize)\n{\n    /* validation checks */\n    if (dstSize == 0) return ERROR(dstSize_tooSmall);\n    if (cSrcSize > dstSize) return ERROR(corruption_detected);   /* invalid */\n    if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; }   /* not compressed */\n    if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; }   /* RLE */\n\n    {   U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n        (void)algoNb;\n        assert(algoNb == 0);\n        return HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,\n                                cSrcSize, workSpace, wkspSize);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n        (void)algoNb;\n        assert(algoNb == 1);\n        return HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,\n                                cSrcSize, workSpace, wkspSize);\n#else\n        return algoNb ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc,\n                                cSrcSize, workSpace, wkspSize):\n                        HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc,\n                                cSrcSize, workSpace, wkspSize);\n#endif\n    }\n}\n\nsize_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,\n                             const void* cSrc, size_t cSrcSize)\n{\n    U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];\n    return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,\n                                      workSpace, sizeof(workSpace));\n}\n\n\nsize_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)\n{\n    DTableDesc const dtd = HUF_getDTableDesc(DTable);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n    (void)dtd;\n    assert(dtd.tableType == 0);\n    return HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n    (void)dtd;\n    assert(dtd.tableType == 1);\n    return HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);\n#else\n    return dtd.tableType ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) :\n                           HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);\n#endif\n}\n\n#ifndef HUF_FORCE_DECOMPRESS_X2\nsize_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2)\n{\n    const BYTE* ip = (const BYTE*) cSrc;\n\n    size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize);\n    if (HUF_isError(hSize)) return hSize;\n    if (hSize >= cSrcSize) return ERROR(srcSize_wrong);\n    ip += hSize; cSrcSize -= hSize;\n\n    return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, bmi2);\n}\n#endif\n\nsize_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)\n{\n    DTableDesc const dtd = HUF_getDTableDesc(DTable);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n    (void)dtd;\n    assert(dtd.tableType == 0);\n    return HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n    (void)dtd;\n    assert(dtd.tableType == 1);\n    return HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);\n#else\n    return dtd.tableType ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) :\n                           HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2);\n#endif\n}\n\nsize_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2)\n{\n    /* validation checks */\n    if (dstSize == 0) return ERROR(dstSize_tooSmall);\n    if (cSrcSize == 0) return ERROR(corruption_detected);\n\n    {   U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);\n#if defined(HUF_FORCE_DECOMPRESS_X1)\n        (void)algoNb;\n        assert(algoNb == 0);\n        return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);\n#elif defined(HUF_FORCE_DECOMPRESS_X2)\n        (void)algoNb;\n        assert(algoNb == 1);\n        return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);\n#else\n        return algoNb ? HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2) :\n                        HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2);\n#endif\n    }\n}\n/**** ended inlining decompress/huf_decompress.c ****/\n/**** start inlining decompress/zstd_ddict.c ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n/* zstd_ddict.c :\n * concentrates all logic that needs to know the internals of ZSTD_DDict object */\n\n/*-*******************************************************\n*  Dependencies\n*********************************************************/\n/**** start inlining ../common/cpu.h ****/\n/*\n * Copyright (c) 2018-2020, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_COMMON_CPU_H\n#define ZSTD_COMMON_CPU_H\n\n/**\n * Implementation taken from folly/CpuId.h\n * https://github.com/facebook/folly/blob/master/folly/CpuId.h\n */\n\n\n/**** skipping file: mem.h ****/\n\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\n\ntypedef struct {\n    U32 f1c;\n    U32 f1d;\n    U32 f7b;\n    U32 f7c;\n} ZSTD_cpuid_t;\n\nMEM_STATIC ZSTD_cpuid_t ZSTD_cpuid(void) {\n    U32 f1c = 0;\n    U32 f1d = 0;\n    U32 f7b = 0;\n    U32 f7c = 0;\n#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))\n    int reg[4];\n    __cpuid((int*)reg, 0);\n    {\n        int const n = reg[0];\n        if (n >= 1) {\n            __cpuid((int*)reg, 1);\n            f1c = (U32)reg[2];\n            f1d = (U32)reg[3];\n        }\n        if (n >= 7) {\n            __cpuidex((int*)reg, 7, 0);\n            f7b = (U32)reg[1];\n            f7c = (U32)reg[2];\n        }\n    }\n#elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__)\n    /* The following block like the normal cpuid branch below, but gcc\n     * reserves ebx for use of its pic register so we must specially\n     * handle the save and restore to avoid clobbering the register\n     */\n    U32 n;\n    __asm__(\n        \"pushl %%ebx\\n\\t\"\n        \"cpuid\\n\\t\"\n        \"popl %%ebx\\n\\t\"\n        : \"=a\"(n)\n        : \"a\"(0)\n        : \"ecx\", \"edx\");\n    if (n >= 1) {\n      U32 f1a;\n      __asm__(\n          \"pushl %%ebx\\n\\t\"\n          \"cpuid\\n\\t\"\n          \"popl %%ebx\\n\\t\"\n          : \"=a\"(f1a), \"=c\"(f1c), \"=d\"(f1d)\n          : \"a\"(1));\n    }\n    if (n >= 7) {\n      __asm__(\n          \"pushl %%ebx\\n\\t\"\n          \"cpuid\\n\\t\"\n          \"movl %%ebx, %%eax\\n\\t\"\n          \"popl %%ebx\"\n          : \"=a\"(f7b), \"=c\"(f7c)\n          : \"a\"(7), \"c\"(0)\n          : \"edx\");\n    }\n#elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__)\n    U32 n;\n    __asm__(\"cpuid\" : \"=a\"(n) : \"a\"(0) : \"ebx\", \"ecx\", \"edx\");\n    if (n >= 1) {\n      U32 f1a;\n      __asm__(\"cpuid\" : \"=a\"(f1a), \"=c\"(f1c), \"=d\"(f1d) : \"a\"(1) : \"ebx\");\n    }\n    if (n >= 7) {\n      U32 f7a;\n      __asm__(\"cpuid\"\n              : \"=a\"(f7a), \"=b\"(f7b), \"=c\"(f7c)\n              : \"a\"(7), \"c\"(0)\n              : \"edx\");\n    }\n#endif\n    {\n        ZSTD_cpuid_t cpuid;\n        cpuid.f1c = f1c;\n        cpuid.f1d = f1d;\n        cpuid.f7b = f7b;\n        cpuid.f7c = f7c;\n        return cpuid;\n    }\n}\n\n#define X(name, r, bit)                                                        \\\n  MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) {                 \\\n    return ((cpuid.r) & (1U << bit)) != 0;                                     \\\n  }\n\n/* cpuid(1): Processor Info and Feature Bits. */\n#define C(name, bit) X(name, f1c, bit)\n  C(sse3, 0)\n  C(pclmuldq, 1)\n  C(dtes64, 2)\n  C(monitor, 3)\n  C(dscpl, 4)\n  C(vmx, 5)\n  C(smx, 6)\n  C(eist, 7)\n  C(tm2, 8)\n  C(ssse3, 9)\n  C(cnxtid, 10)\n  C(fma, 12)\n  C(cx16, 13)\n  C(xtpr, 14)\n  C(pdcm, 15)\n  C(pcid, 17)\n  C(dca, 18)\n  C(sse41, 19)\n  C(sse42, 20)\n  C(x2apic, 21)\n  C(movbe, 22)\n  C(popcnt, 23)\n  C(tscdeadline, 24)\n  C(aes, 25)\n  C(xsave, 26)\n  C(osxsave, 27)\n  C(avx, 28)\n  C(f16c, 29)\n  C(rdrand, 30)\n#undef C\n#define D(name, bit) X(name, f1d, bit)\n  D(fpu, 0)\n  D(vme, 1)\n  D(de, 2)\n  D(pse, 3)\n  D(tsc, 4)\n  D(msr, 5)\n  D(pae, 6)\n  D(mce, 7)\n  D(cx8, 8)\n  D(apic, 9)\n  D(sep, 11)\n  D(mtrr, 12)\n  D(pge, 13)\n  D(mca, 14)\n  D(cmov, 15)\n  D(pat, 16)\n  D(pse36, 17)\n  D(psn, 18)\n  D(clfsh, 19)\n  D(ds, 21)\n  D(acpi, 22)\n  D(mmx, 23)\n  D(fxsr, 24)\n  D(sse, 25)\n  D(sse2, 26)\n  D(ss, 27)\n  D(htt, 28)\n  D(tm, 29)\n  D(pbe, 31)\n#undef D\n\n/* cpuid(7): Extended Features. */\n#define B(name, bit) X(name, f7b, bit)\n  B(bmi1, 3)\n  B(hle, 4)\n  B(avx2, 5)\n  B(smep, 7)\n  B(bmi2, 8)\n  B(erms, 9)\n  B(invpcid, 10)\n  B(rtm, 11)\n  B(mpx, 14)\n  B(avx512f, 16)\n  B(avx512dq, 17)\n  B(rdseed, 18)\n  B(adx, 19)\n  B(smap, 20)\n  B(avx512ifma, 21)\n  B(pcommit, 22)\n  B(clflushopt, 23)\n  B(clwb, 24)\n  B(avx512pf, 26)\n  B(avx512er, 27)\n  B(avx512cd, 28)\n  B(sha, 29)\n  B(avx512bw, 30)\n  B(avx512vl, 31)\n#undef B\n#define C(name, bit) X(name, f7c, bit)\n  C(prefetchwt1, 0)\n  C(avx512vbmi, 1)\n#undef C\n\n#undef X\n\n#endif /* ZSTD_COMMON_CPU_H */\n/**** ended inlining ../common/cpu.h ****/\n/**** skipping file: ../common/mem.h ****/\n#define FSE_STATIC_LINKING_ONLY\n/**** skipping file: ../common/fse.h ****/\n#define HUF_STATIC_LINKING_ONLY\n/**** skipping file: ../common/huf.h ****/\n/**** start inlining zstd_decompress_internal.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n\n/* zstd_decompress_internal:\n * objects and definitions shared within lib/decompress modules */\n\n #ifndef ZSTD_DECOMPRESS_INTERNAL_H\n #define ZSTD_DECOMPRESS_INTERNAL_H\n\n\n/*-*******************************************************\n *  Dependencies\n *********************************************************/\n/**** skipping file: ../common/mem.h ****/\n/**** skipping file: ../common/zstd_internal.h ****/\n\n\n\n/*-*******************************************************\n *  Constants\n *********************************************************/\nstatic const U32 LL_base[MaxLL+1] = {\n                 0,    1,    2,     3,     4,     5,     6,      7,\n                 8,    9,   10,    11,    12,    13,    14,     15,\n                16,   18,   20,    22,    24,    28,    32,     40,\n                48,   64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,\n                0x2000, 0x4000, 0x8000, 0x10000 };\n\nstatic const U32 OF_base[MaxOff+1] = {\n                 0,        1,       1,       5,     0xD,     0x1D,     0x3D,     0x7D,\n                 0xFD,   0x1FD,   0x3FD,   0x7FD,   0xFFD,   0x1FFD,   0x3FFD,   0x7FFD,\n                 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,\n                 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD, 0x1FFFFFFD, 0x3FFFFFFD, 0x7FFFFFFD };\n\nstatic const U32 OF_bits[MaxOff+1] = {\n                     0,  1,  2,  3,  4,  5,  6,  7,\n                     8,  9, 10, 11, 12, 13, 14, 15,\n                    16, 17, 18, 19, 20, 21, 22, 23,\n                    24, 25, 26, 27, 28, 29, 30, 31 };\n\nstatic const U32 ML_base[MaxML+1] = {\n                     3,  4,  5,    6,     7,     8,     9,    10,\n                    11, 12, 13,   14,    15,    16,    17,    18,\n                    19, 20, 21,   22,    23,    24,    25,    26,\n                    27, 28, 29,   30,    31,    32,    33,    34,\n                    35, 37, 39,   41,    43,    47,    51,    59,\n                    67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,\n                    0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };\n\n\n/*-*******************************************************\n *  Decompression types\n *********************************************************/\n typedef struct {\n     U32 fastMode;\n     U32 tableLog;\n } ZSTD_seqSymbol_header;\n\n typedef struct {\n     U16  nextState;\n     BYTE nbAdditionalBits;\n     BYTE nbBits;\n     U32  baseValue;\n } ZSTD_seqSymbol;\n\n #define SEQSYMBOL_TABLE_SIZE(log)   (1 + (1 << (log)))\n\ntypedef struct {\n    ZSTD_seqSymbol LLTable[SEQSYMBOL_TABLE_SIZE(LLFSELog)];    /* Note : Space reserved for FSE Tables */\n    ZSTD_seqSymbol OFTable[SEQSYMBOL_TABLE_SIZE(OffFSELog)];   /* is also used as temporary workspace while building hufTable during DDict creation */\n    ZSTD_seqSymbol MLTable[SEQSYMBOL_TABLE_SIZE(MLFSELog)];    /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */\n    HUF_DTable hufTable[HUF_DTABLE_SIZE(HufLog)];  /* can accommodate HUF_decompress4X */\n    U32 rep[ZSTD_REP_NUM];\n} ZSTD_entropyDTables_t;\n\ntypedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,\n               ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,\n               ZSTDds_decompressLastBlock, ZSTDds_checkChecksum,\n               ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTD_dStage;\n\ntypedef enum { zdss_init=0, zdss_loadHeader,\n               zdss_read, zdss_load, zdss_flush } ZSTD_dStreamStage;\n\ntypedef enum {\n    ZSTD_use_indefinitely = -1,  /* Use the dictionary indefinitely */\n    ZSTD_dont_use = 0,           /* Do not use the dictionary (if one exists free it) */\n    ZSTD_use_once = 1            /* Use the dictionary once and set to ZSTD_dont_use */\n} ZSTD_dictUses_e;\n\ntypedef enum {\n    ZSTD_obm_buffered = 0,  /* Buffer the output */\n    ZSTD_obm_stable = 1     /* ZSTD_outBuffer is stable */\n} ZSTD_outBufferMode_e;\n\nstruct ZSTD_DCtx_s\n{\n    const ZSTD_seqSymbol* LLTptr;\n    const ZSTD_seqSymbol* MLTptr;\n    const ZSTD_seqSymbol* OFTptr;\n    const HUF_DTable* HUFptr;\n    ZSTD_entropyDTables_t entropy;\n    U32 workspace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];   /* space needed when building huffman tables */\n    const void* previousDstEnd;   /* detect continuity */\n    const void* prefixStart;      /* start of current segment */\n    const void* virtualStart;     /* virtual start of previous segment if it was just before current one */\n    const void* dictEnd;          /* end of previous segment */\n    size_t expected;\n    ZSTD_frameHeader fParams;\n    U64 decodedSize;\n    blockType_e bType;            /* used in ZSTD_decompressContinue(), store blockType between block header decoding and block decompression stages */\n    ZSTD_dStage stage;\n    U32 litEntropy;\n    U32 fseEntropy;\n    XXH64_state_t xxhState;\n    size_t headerSize;\n    ZSTD_format_e format;\n    const BYTE* litPtr;\n    ZSTD_customMem customMem;\n    size_t litSize;\n    size_t rleSize;\n    size_t staticSize;\n    int bmi2;                     /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */\n\n    /* dictionary */\n    ZSTD_DDict* ddictLocal;\n    const ZSTD_DDict* ddict;     /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */\n    U32 dictID;\n    int ddictIsCold;             /* if == 1 : dictionary is \"new\" for working context, and presumed \"cold\" (not in cpu cache) */\n    ZSTD_dictUses_e dictUses;\n\n    /* streaming */\n    ZSTD_dStreamStage streamStage;\n    char*  inBuff;\n    size_t inBuffSize;\n    size_t inPos;\n    size_t maxWindowSize;\n    char*  outBuff;\n    size_t outBuffSize;\n    size_t outStart;\n    size_t outEnd;\n    size_t lhSize;\n    void* legacyContext;\n    U32 previousLegacyVersion;\n    U32 legacyVersion;\n    U32 hostageByte;\n    int noForwardProgress;\n    ZSTD_outBufferMode_e outBufferMode;\n    ZSTD_outBuffer expectedOutBuffer;\n\n    /* workspace */\n    BYTE litBuffer[ZSTD_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];\n    BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX];\n\n    size_t oversizedDuration;\n\n#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION\n    void const* dictContentBeginForFuzzing;\n    void const* dictContentEndForFuzzing;\n#endif\n};  /* typedef'd to ZSTD_DCtx within \"zstd.h\" */\n\n\n/*-*******************************************************\n *  Shared internal functions\n *********************************************************/\n\n/*! ZSTD_loadDEntropy() :\n *  dict : must point at beginning of a valid zstd dictionary.\n * @return : size of dictionary header (size of magic number + dict ID + entropy tables) */\nsize_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,\n                   const void* const dict, size_t const dictSize);\n\n/*! ZSTD_checkContinuity() :\n *  check if next `dst` follows previous position, where decompression ended.\n *  If yes, do nothing (continue on current segment).\n *  If not, classify previous segment as \"external dictionary\", and start a new segment.\n *  This function cannot fail. */\nvoid ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst);\n\n\n#endif /* ZSTD_DECOMPRESS_INTERNAL_H */\n/**** ended inlining zstd_decompress_internal.h ****/\n/**** start inlining zstd_ddict.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n\n#ifndef ZSTD_DDICT_H\n#define ZSTD_DDICT_H\n\n/*-*******************************************************\n *  Dependencies\n *********************************************************/\n#include <stddef.h>   /* size_t */\n/**** skipping file: ../zstd.h ****/\n\n\n/*-*******************************************************\n *  Interface\n *********************************************************/\n\n/* note: several prototypes are already published in `zstd.h` :\n * ZSTD_createDDict()\n * ZSTD_createDDict_byReference()\n * ZSTD_createDDict_advanced()\n * ZSTD_freeDDict()\n * ZSTD_initStaticDDict()\n * ZSTD_sizeof_DDict()\n * ZSTD_estimateDDictSize()\n * ZSTD_getDictID_fromDict()\n */\n\nconst void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict);\nsize_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict);\n\nvoid ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);\n\n\n\n#endif /* ZSTD_DDICT_H */\n/**** ended inlining zstd_ddict.h ****/\n\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)\n/**** start inlining ../legacy/zstd_legacy.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_LEGACY_H\n#define ZSTD_LEGACY_H\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/* *************************************\n*  Includes\n***************************************/\n/**** skipping file: ../common/mem.h ****/\n/**** skipping file: ../common/error_private.h ****/\n/**** skipping file: ../common/zstd_internal.h ****/\n\n#if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)\n#  undef ZSTD_LEGACY_SUPPORT\n#  define ZSTD_LEGACY_SUPPORT 8\n#endif\n\n#if (ZSTD_LEGACY_SUPPORT <= 1)\n/**** start inlining zstd_v01.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_V01_H_28739879432\n#define ZSTD_V01_H_28739879432\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/* *************************************\n*  Includes\n***************************************/\n#include <stddef.h>   /* size_t */\n\n\n/* *************************************\n*  Simple one-step function\n***************************************/\n/**\nZSTDv01_decompress() : decompress ZSTD frames compliant with v0.1.x format\n    compressedSize : is the exact source size\n    maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.\n                      It must be equal or larger than originalSize, otherwise decompression will fail.\n    return : the number of bytes decompressed into destination buffer (originalSize)\n             or an errorCode if it fails (which can be tested using ZSTDv01_isError())\n*/\nsize_t ZSTDv01_decompress( void* dst, size_t maxOriginalSize,\n                     const void* src, size_t compressedSize);\n\n /**\n ZSTDv01_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.1.x format\n     srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n     cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                 or an error code if it fails (which can be tested using ZSTDv01_isError())\n     dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                 or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n     note : assumes `cSize` and `dBound` are _not_ NULL.\n */\nvoid ZSTDv01_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                     size_t* cSize, unsigned long long* dBound);\n\n/**\nZSTDv01_isError() : tells if the result of ZSTDv01_decompress() is an error\n*/\nunsigned ZSTDv01_isError(size_t code);\n\n\n/* *************************************\n*  Advanced functions\n***************************************/\ntypedef struct ZSTDv01_Dctx_s ZSTDv01_Dctx;\nZSTDv01_Dctx* ZSTDv01_createDCtx(void);\nsize_t ZSTDv01_freeDCtx(ZSTDv01_Dctx* dctx);\n\nsize_t ZSTDv01_decompressDCtx(void* ctx,\n                              void* dst, size_t maxOriginalSize,\n                        const void* src, size_t compressedSize);\n\n/* *************************************\n*  Streaming functions\n***************************************/\nsize_t ZSTDv01_resetDCtx(ZSTDv01_Dctx* dctx);\n\nsize_t ZSTDv01_nextSrcSizeToDecompress(ZSTDv01_Dctx* dctx);\nsize_t ZSTDv01_decompressContinue(ZSTDv01_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);\n/**\n  Use above functions alternatively.\n  ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().\n  ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.\n  Result is the number of bytes regenerated within 'dst'.\n  It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.\n*/\n\n/* *************************************\n*  Prefix - version detection\n***************************************/\n#define ZSTDv01_magicNumber   0xFD2FB51E   /* Big Endian version */\n#define ZSTDv01_magicNumberLE 0x1EB52FFD   /* Little Endian version */\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* ZSTD_V01_H_28739879432 */\n/**** ended inlining zstd_v01.h ****/\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 2)\n/**** start inlining zstd_v02.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_V02_H_4174539423\n#define ZSTD_V02_H_4174539423\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/* *************************************\n*  Includes\n***************************************/\n#include <stddef.h>   /* size_t */\n\n\n/* *************************************\n*  Simple one-step function\n***************************************/\n/**\nZSTDv02_decompress() : decompress ZSTD frames compliant with v0.2.x format\n    compressedSize : is the exact source size\n    maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.\n                      It must be equal or larger than originalSize, otherwise decompression will fail.\n    return : the number of bytes decompressed into destination buffer (originalSize)\n             or an errorCode if it fails (which can be tested using ZSTDv01_isError())\n*/\nsize_t ZSTDv02_decompress( void* dst, size_t maxOriginalSize,\n                     const void* src, size_t compressedSize);\n\n /**\n ZSTDv02_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.2.x format\n     srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n     cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                 or an error code if it fails (which can be tested using ZSTDv01_isError())\n     dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                 or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n    note : assumes `cSize` and `dBound` are _not_ NULL.\n */\nvoid ZSTDv02_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                     size_t* cSize, unsigned long long* dBound);\n\n/**\nZSTDv02_isError() : tells if the result of ZSTDv02_decompress() is an error\n*/\nunsigned ZSTDv02_isError(size_t code);\n\n\n/* *************************************\n*  Advanced functions\n***************************************/\ntypedef struct ZSTDv02_Dctx_s ZSTDv02_Dctx;\nZSTDv02_Dctx* ZSTDv02_createDCtx(void);\nsize_t ZSTDv02_freeDCtx(ZSTDv02_Dctx* dctx);\n\nsize_t ZSTDv02_decompressDCtx(void* ctx,\n                              void* dst, size_t maxOriginalSize,\n                        const void* src, size_t compressedSize);\n\n/* *************************************\n*  Streaming functions\n***************************************/\nsize_t ZSTDv02_resetDCtx(ZSTDv02_Dctx* dctx);\n\nsize_t ZSTDv02_nextSrcSizeToDecompress(ZSTDv02_Dctx* dctx);\nsize_t ZSTDv02_decompressContinue(ZSTDv02_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);\n/**\n  Use above functions alternatively.\n  ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().\n  ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.\n  Result is the number of bytes regenerated within 'dst'.\n  It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.\n*/\n\n/* *************************************\n*  Prefix - version detection\n***************************************/\n#define ZSTDv02_magicNumber 0xFD2FB522   /* v0.2 */\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* ZSTD_V02_H_4174539423 */\n/**** ended inlining zstd_v02.h ****/\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 3)\n/**** start inlining zstd_v03.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_V03_H_298734209782\n#define ZSTD_V03_H_298734209782\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/* *************************************\n*  Includes\n***************************************/\n#include <stddef.h>   /* size_t */\n\n\n/* *************************************\n*  Simple one-step function\n***************************************/\n/**\nZSTDv03_decompress() : decompress ZSTD frames compliant with v0.3.x format\n    compressedSize : is the exact source size\n    maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.\n                      It must be equal or larger than originalSize, otherwise decompression will fail.\n    return : the number of bytes decompressed into destination buffer (originalSize)\n             or an errorCode if it fails (which can be tested using ZSTDv01_isError())\n*/\nsize_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,\n                     const void* src, size_t compressedSize);\n\n /**\n ZSTDv03_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.3.x format\n     srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n     cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                 or an error code if it fails (which can be tested using ZSTDv01_isError())\n     dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                 or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n    note : assumes `cSize` and `dBound` are _not_ NULL.\n */\n void ZSTDv03_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                      size_t* cSize, unsigned long long* dBound);\n\n    /**\nZSTDv03_isError() : tells if the result of ZSTDv03_decompress() is an error\n*/\nunsigned ZSTDv03_isError(size_t code);\n\n\n/* *************************************\n*  Advanced functions\n***************************************/\ntypedef struct ZSTDv03_Dctx_s ZSTDv03_Dctx;\nZSTDv03_Dctx* ZSTDv03_createDCtx(void);\nsize_t ZSTDv03_freeDCtx(ZSTDv03_Dctx* dctx);\n\nsize_t ZSTDv03_decompressDCtx(void* ctx,\n                              void* dst, size_t maxOriginalSize,\n                        const void* src, size_t compressedSize);\n\n/* *************************************\n*  Streaming functions\n***************************************/\nsize_t ZSTDv03_resetDCtx(ZSTDv03_Dctx* dctx);\n\nsize_t ZSTDv03_nextSrcSizeToDecompress(ZSTDv03_Dctx* dctx);\nsize_t ZSTDv03_decompressContinue(ZSTDv03_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);\n/**\n  Use above functions alternatively.\n  ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().\n  ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.\n  Result is the number of bytes regenerated within 'dst'.\n  It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.\n*/\n\n/* *************************************\n*  Prefix - version detection\n***************************************/\n#define ZSTDv03_magicNumber 0xFD2FB523   /* v0.3 */\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* ZSTD_V03_H_298734209782 */\n/**** ended inlining zstd_v03.h ****/\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n/**** start inlining zstd_v04.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTD_V04_H_91868324769238\n#define ZSTD_V04_H_91868324769238\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/* *************************************\n*  Includes\n***************************************/\n#include <stddef.h>   /* size_t */\n\n\n/* *************************************\n*  Simple one-step function\n***************************************/\n/**\nZSTDv04_decompress() : decompress ZSTD frames compliant with v0.4.x format\n    compressedSize : is the exact source size\n    maxOriginalSize : is the size of the 'dst' buffer, which must be already allocated.\n                      It must be equal or larger than originalSize, otherwise decompression will fail.\n    return : the number of bytes decompressed into destination buffer (originalSize)\n             or an errorCode if it fails (which can be tested using ZSTDv01_isError())\n*/\nsize_t ZSTDv04_decompress( void* dst, size_t maxOriginalSize,\n                     const void* src, size_t compressedSize);\n\n /**\n ZSTDv04_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.4.x format\n     srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n     cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                 or an error code if it fails (which can be tested using ZSTDv01_isError())\n     dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                 or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n    note : assumes `cSize` and `dBound` are _not_ NULL.\n */\n void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                      size_t* cSize, unsigned long long* dBound);\n\n/**\nZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error\n*/\nunsigned ZSTDv04_isError(size_t code);\n\n\n/* *************************************\n*  Advanced functions\n***************************************/\ntypedef struct ZSTDv04_Dctx_s ZSTDv04_Dctx;\nZSTDv04_Dctx* ZSTDv04_createDCtx(void);\nsize_t ZSTDv04_freeDCtx(ZSTDv04_Dctx* dctx);\n\nsize_t ZSTDv04_decompressDCtx(ZSTDv04_Dctx* dctx,\n                              void* dst, size_t maxOriginalSize,\n                        const void* src, size_t compressedSize);\n\n\n/* *************************************\n*  Direct Streaming\n***************************************/\nsize_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx);\n\nsize_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx);\nsize_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize);\n/**\n  Use above functions alternatively.\n  ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().\n  ZSTD_decompressContinue() will use previous data blocks to improve compression if they are located prior to current block.\n  Result is the number of bytes regenerated within 'dst'.\n  It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.\n*/\n\n\n/* *************************************\n*  Buffered Streaming\n***************************************/\ntypedef struct ZBUFFv04_DCtx_s ZBUFFv04_DCtx;\nZBUFFv04_DCtx* ZBUFFv04_createDCtx(void);\nsize_t         ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx);\n\nsize_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx);\nsize_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* dict, size_t dictSize);\n\nsize_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr);\n\n/** ************************************************\n*  Streaming decompression\n*\n*  A ZBUFF_DCtx object is required to track streaming operation.\n*  Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.\n*  Use ZBUFF_decompressInit() to start a new decompression operation.\n*  ZBUFF_DCtx objects can be reused multiple times.\n*\n*  Optionally, a reference to a static dictionary can be set, using ZBUFF_decompressWithDictionary()\n*  It must be the same content as the one set during compression phase.\n*  Dictionary content must remain accessible during the decompression process.\n*\n*  Use ZBUFF_decompressContinue() repetitively to consume your input.\n*  *srcSizePtr and *maxDstSizePtr can be any size.\n*  The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.\n*  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.\n*  The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst.\n*  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)\n*            or 0 when a frame is completely decoded\n*            or an error code, which can be tested using ZBUFF_isError().\n*\n*  Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize / ZBUFF_recommendedDOutSize\n*  output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when it's decoded.\n*  input : ZBUFF_recommendedDInSize==128Kb+3; just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .\n* **************************************************/\nunsigned ZBUFFv04_isError(size_t errorCode);\nconst char* ZBUFFv04_getErrorName(size_t errorCode);\n\n\n/** The below functions provide recommended buffer sizes for Compression or Decompression operations.\n*   These sizes are not compulsory, they just tend to offer better latency */\nsize_t ZBUFFv04_recommendedDInSize(void);\nsize_t ZBUFFv04_recommendedDOutSize(void);\n\n\n/* *************************************\n*  Prefix - version detection\n***************************************/\n#define ZSTDv04_magicNumber 0xFD2FB524   /* v0.4 */\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif /* ZSTD_V04_H_91868324769238 */\n/**** ended inlining zstd_v04.h ****/\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n/**** start inlining zstd_v05.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTDv05_H\n#define ZSTDv05_H\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/*-*************************************\n*  Dependencies\n***************************************/\n#include <stddef.h>   /* size_t */\n/**** skipping file: ../common/mem.h ****/\n\n\n/* *************************************\n*  Simple functions\n***************************************/\n/*! ZSTDv05_decompress() :\n    `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.\n    `dstCapacity` must be large enough, equal or larger than originalSize.\n    @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),\n              or an errorCode if it fails (which can be tested using ZSTDv05_isError()) */\nsize_t ZSTDv05_decompress( void* dst, size_t dstCapacity,\n                     const void* src, size_t compressedSize);\n\n /**\n ZSTDv05_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.5.x format\n     srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n     cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                 or an error code if it fails (which can be tested using ZSTDv01_isError())\n     dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                 or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n    note : assumes `cSize` and `dBound` are _not_ NULL.\n */\nvoid ZSTDv05_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                     size_t* cSize, unsigned long long* dBound);\n\n/* *************************************\n*  Helper functions\n***************************************/\n/* Error Management */\nunsigned    ZSTDv05_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */\nconst char* ZSTDv05_getErrorName(size_t code);     /*!< provides readable string for an error code */\n\n\n/* *************************************\n*  Explicit memory management\n***************************************/\n/** Decompression context */\ntypedef struct ZSTDv05_DCtx_s ZSTDv05_DCtx;\nZSTDv05_DCtx* ZSTDv05_createDCtx(void);\nsize_t ZSTDv05_freeDCtx(ZSTDv05_DCtx* dctx);      /*!< @return : errorCode */\n\n/** ZSTDv05_decompressDCtx() :\n*   Same as ZSTDv05_decompress(), but requires an already allocated ZSTDv05_DCtx (see ZSTDv05_createDCtx()) */\nsize_t ZSTDv05_decompressDCtx(ZSTDv05_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n\n/*-***********************\n*  Simple Dictionary API\n*************************/\n/*! ZSTDv05_decompress_usingDict() :\n*   Decompression using a pre-defined Dictionary content (see dictBuilder).\n*   Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.\n*   Note : dict can be NULL, in which case, it's equivalent to ZSTDv05_decompressDCtx() */\nsize_t ZSTDv05_decompress_usingDict(ZSTDv05_DCtx* dctx,\n                                            void* dst, size_t dstCapacity,\n                                      const void* src, size_t srcSize,\n                                      const void* dict,size_t dictSize);\n\n/*-************************\n*  Advanced Streaming API\n***************************/\ntypedef enum { ZSTDv05_fast, ZSTDv05_greedy, ZSTDv05_lazy, ZSTDv05_lazy2, ZSTDv05_btlazy2, ZSTDv05_opt, ZSTDv05_btopt } ZSTDv05_strategy;\ntypedef struct {\n    U64 srcSize;\n    U32 windowLog;     /* the only useful information to retrieve */\n    U32 contentLog; U32 hashLog; U32 searchLog; U32 searchLength; U32 targetLength; ZSTDv05_strategy strategy;\n} ZSTDv05_parameters;\nsize_t ZSTDv05_getFrameParams(ZSTDv05_parameters* params, const void* src, size_t srcSize);\n\nsize_t ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize);\nvoid   ZSTDv05_copyDCtx(ZSTDv05_DCtx* dstDCtx, const ZSTDv05_DCtx* srcDCtx);\nsize_t ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx* dctx);\nsize_t ZSTDv05_decompressContinue(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n\n/*-***********************\n*  ZBUFF API\n*************************/\ntypedef struct ZBUFFv05_DCtx_s ZBUFFv05_DCtx;\nZBUFFv05_DCtx* ZBUFFv05_createDCtx(void);\nsize_t         ZBUFFv05_freeDCtx(ZBUFFv05_DCtx* dctx);\n\nsize_t ZBUFFv05_decompressInit(ZBUFFv05_DCtx* dctx);\nsize_t ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx* dctx, const void* dict, size_t dictSize);\n\nsize_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* dctx,\n                                            void* dst, size_t* dstCapacityPtr,\n                                      const void* src, size_t* srcSizePtr);\n\n/*-***************************************************************************\n*  Streaming decompression\n*\n*  A ZBUFFv05_DCtx object is required to track streaming operations.\n*  Use ZBUFFv05_createDCtx() and ZBUFFv05_freeDCtx() to create/release resources.\n*  Use ZBUFFv05_decompressInit() to start a new decompression operation,\n*   or ZBUFFv05_decompressInitDictionary() if decompression requires a dictionary.\n*  Note that ZBUFFv05_DCtx objects can be reused multiple times.\n*\n*  Use ZBUFFv05_decompressContinue() repetitively to consume your input.\n*  *srcSizePtr and *dstCapacityPtr can be any size.\n*  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.\n*  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.\n*  The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change @dst.\n*  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency)\n*            or 0 when a frame is completely decoded\n*            or an error code, which can be tested using ZBUFFv05_isError().\n*\n*  Hint : recommended buffer sizes (not compulsory) : ZBUFFv05_recommendedDInSize() / ZBUFFv05_recommendedDOutSize()\n*  output : ZBUFFv05_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.\n*  input  : ZBUFFv05_recommendedDInSize==128Kb+3; just follow indications from ZBUFFv05_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .\n* *******************************************************************************/\n\n\n/* *************************************\n*  Tool functions\n***************************************/\nunsigned ZBUFFv05_isError(size_t errorCode);\nconst char* ZBUFFv05_getErrorName(size_t errorCode);\n\n/** Functions below provide recommended buffer sizes for Compression or Decompression operations.\n*   These sizes are just hints, and tend to offer better latency */\nsize_t ZBUFFv05_recommendedDInSize(void);\nsize_t ZBUFFv05_recommendedDOutSize(void);\n\n\n\n/*-*************************************\n*  Constants\n***************************************/\n#define ZSTDv05_MAGICNUMBER 0xFD2FB525   /* v0.5 */\n\n\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif  /* ZSTDv0505_H */\n/**** ended inlining zstd_v05.h ****/\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n/**** start inlining zstd_v06.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTDv06_H\n#define ZSTDv06_H\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/*======  Dependency  ======*/\n#include <stddef.h>   /* size_t */\n\n\n/*======  Export for Windows  ======*/\n/*!\n*  ZSTDv06_DLL_EXPORT :\n*  Enable exporting of functions when building a Windows DLL\n*/\n#if defined(_WIN32) && defined(ZSTDv06_DLL_EXPORT) && (ZSTDv06_DLL_EXPORT==1)\n#  define ZSTDLIBv06_API __declspec(dllexport)\n#else\n#  define ZSTDLIBv06_API\n#endif\n\n\n/* *************************************\n*  Simple functions\n***************************************/\n/*! ZSTDv06_decompress() :\n    `compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.\n    `dstCapacity` must be large enough, equal or larger than originalSize.\n    @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),\n              or an errorCode if it fails (which can be tested using ZSTDv06_isError()) */\nZSTDLIBv06_API size_t ZSTDv06_decompress( void* dst, size_t dstCapacity,\n                                    const void* src, size_t compressedSize);\n\n/**\nZSTDv06_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.6.x format\n    srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n    cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                or an error code if it fails (which can be tested using ZSTDv01_isError())\n    dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n    note : assumes `cSize` and `dBound` are _not_ NULL.\n*/\nvoid ZSTDv06_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                     size_t* cSize, unsigned long long* dBound);\n\n/* *************************************\n*  Helper functions\n***************************************/\nZSTDLIBv06_API size_t      ZSTDv06_compressBound(size_t srcSize); /*!< maximum compressed size (worst case scenario) */\n\n/* Error Management */\nZSTDLIBv06_API unsigned    ZSTDv06_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */\nZSTDLIBv06_API const char* ZSTDv06_getErrorName(size_t code);     /*!< provides readable string for an error code */\n\n\n/* *************************************\n*  Explicit memory management\n***************************************/\n/** Decompression context */\ntypedef struct ZSTDv06_DCtx_s ZSTDv06_DCtx;\nZSTDLIBv06_API ZSTDv06_DCtx* ZSTDv06_createDCtx(void);\nZSTDLIBv06_API size_t     ZSTDv06_freeDCtx(ZSTDv06_DCtx* dctx);      /*!< @return : errorCode */\n\n/** ZSTDv06_decompressDCtx() :\n*   Same as ZSTDv06_decompress(), but requires an already allocated ZSTDv06_DCtx (see ZSTDv06_createDCtx()) */\nZSTDLIBv06_API size_t ZSTDv06_decompressDCtx(ZSTDv06_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n\n/*-***********************\n*  Dictionary API\n*************************/\n/*! ZSTDv06_decompress_usingDict() :\n*   Decompression using a pre-defined Dictionary content (see dictBuilder).\n*   Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.\n*   Note : dict can be NULL, in which case, it's equivalent to ZSTDv06_decompressDCtx() */\nZSTDLIBv06_API size_t ZSTDv06_decompress_usingDict(ZSTDv06_DCtx* dctx,\n                                                   void* dst, size_t dstCapacity,\n                                             const void* src, size_t srcSize,\n                                             const void* dict,size_t dictSize);\n\n\n/*-************************\n*  Advanced Streaming API\n***************************/\nstruct ZSTDv06_frameParams_s { unsigned long long frameContentSize; unsigned windowLog; };\ntypedef struct ZSTDv06_frameParams_s ZSTDv06_frameParams;\n\nZSTDLIBv06_API size_t ZSTDv06_getFrameParams(ZSTDv06_frameParams* fparamsPtr, const void* src, size_t srcSize);   /**< doesn't consume input */\nZSTDLIBv06_API size_t ZSTDv06_decompressBegin_usingDict(ZSTDv06_DCtx* dctx, const void* dict, size_t dictSize);\nZSTDLIBv06_API void   ZSTDv06_copyDCtx(ZSTDv06_DCtx* dctx, const ZSTDv06_DCtx* preparedDCtx);\n\nZSTDLIBv06_API size_t ZSTDv06_nextSrcSizeToDecompress(ZSTDv06_DCtx* dctx);\nZSTDLIBv06_API size_t ZSTDv06_decompressContinue(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n\n\n/* *************************************\n*  ZBUFF API\n***************************************/\n\ntypedef struct ZBUFFv06_DCtx_s ZBUFFv06_DCtx;\nZSTDLIBv06_API ZBUFFv06_DCtx* ZBUFFv06_createDCtx(void);\nZSTDLIBv06_API size_t         ZBUFFv06_freeDCtx(ZBUFFv06_DCtx* dctx);\n\nZSTDLIBv06_API size_t ZBUFFv06_decompressInit(ZBUFFv06_DCtx* dctx);\nZSTDLIBv06_API size_t ZBUFFv06_decompressInitDictionary(ZBUFFv06_DCtx* dctx, const void* dict, size_t dictSize);\n\nZSTDLIBv06_API size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* dctx,\n                                                  void* dst, size_t* dstCapacityPtr,\n                                            const void* src, size_t* srcSizePtr);\n\n/*-***************************************************************************\n*  Streaming decompression howto\n*\n*  A ZBUFFv06_DCtx object is required to track streaming operations.\n*  Use ZBUFFv06_createDCtx() and ZBUFFv06_freeDCtx() to create/release resources.\n*  Use ZBUFFv06_decompressInit() to start a new decompression operation,\n*   or ZBUFFv06_decompressInitDictionary() if decompression requires a dictionary.\n*  Note that ZBUFFv06_DCtx objects can be re-init multiple times.\n*\n*  Use ZBUFFv06_decompressContinue() repetitively to consume your input.\n*  *srcSizePtr and *dstCapacityPtr can be any size.\n*  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.\n*  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.\n*  The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.\n*  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),\n*            or 0 when a frame is completely decoded,\n*            or an error code, which can be tested using ZBUFFv06_isError().\n*\n*  Hint : recommended buffer sizes (not compulsory) : ZBUFFv06_recommendedDInSize() and ZBUFFv06_recommendedDOutSize()\n*  output : ZBUFFv06_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.\n*  input  : ZBUFFv06_recommendedDInSize == 128KB + 3;\n*           just follow indications from ZBUFFv06_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .\n* *******************************************************************************/\n\n\n/* *************************************\n*  Tool functions\n***************************************/\nZSTDLIBv06_API unsigned ZBUFFv06_isError(size_t errorCode);\nZSTDLIBv06_API const char* ZBUFFv06_getErrorName(size_t errorCode);\n\n/** Functions below provide recommended buffer sizes for Compression or Decompression operations.\n*   These sizes are just hints, they tend to offer better latency */\nZSTDLIBv06_API size_t ZBUFFv06_recommendedDInSize(void);\nZSTDLIBv06_API size_t ZBUFFv06_recommendedDOutSize(void);\n\n\n/*-*************************************\n*  Constants\n***************************************/\n#define ZSTDv06_MAGICNUMBER 0xFD2FB526   /* v0.6 */\n\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif  /* ZSTDv06_BUFFERED_H */\n/**** ended inlining zstd_v06.h ****/\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n/**** start inlining zstd_v07.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n#ifndef ZSTDv07_H_235446\n#define ZSTDv07_H_235446\n\n#if defined (__cplusplus)\nextern \"C\" {\n#endif\n\n/*======  Dependency  ======*/\n#include <stddef.h>   /* size_t */\n\n\n/*======  Export for Windows  ======*/\n/*!\n*  ZSTDv07_DLL_EXPORT :\n*  Enable exporting of functions when building a Windows DLL\n*/\n#if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1)\n#  define ZSTDLIBv07_API __declspec(dllexport)\n#else\n#  define ZSTDLIBv07_API\n#endif\n\n\n/* *************************************\n*  Simple API\n***************************************/\n/*! ZSTDv07_getDecompressedSize() :\n*   @return : decompressed size if known, 0 otherwise.\n       note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause.\n       note 2 : decompressed size could be wrong or intentionally modified !\n                always ensure results fit within application's authorized limits */\nunsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize);\n\n/*! ZSTDv07_decompress() :\n    `compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail.\n    `dstCapacity` must be equal or larger than originalSize.\n    @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),\n              or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */\nZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity,\n                                    const void* src, size_t compressedSize);\n\n/**\nZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format\n    srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'\n    cSize (output parameter)  : the number of bytes that would be read to decompress this frame\n                                or an error code if it fails (which can be tested using ZSTDv01_isError())\n    dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame\n                                or ZSTD_CONTENTSIZE_ERROR if an error occurs\n\n    note : assumes `cSize` and `dBound` are _not_ NULL.\n*/\nvoid ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize,\n                                     size_t* cSize, unsigned long long* dBound);\n\n/*======  Helper functions  ======*/\nZSTDLIBv07_API unsigned    ZSTDv07_isError(size_t code);          /*!< tells if a `size_t` function result is an error code */\nZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code);     /*!< provides readable string from an error code */\n\n\n/*-*************************************\n*  Explicit memory management\n***************************************/\n/** Decompression context */\ntypedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx;\nZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void);\nZSTDLIBv07_API size_t     ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx);      /*!< @return : errorCode */\n\n/** ZSTDv07_decompressDCtx() :\n*   Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */\nZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);\n\n\n/*-************************\n*  Simple dictionary API\n***************************/\n/*! ZSTDv07_decompress_usingDict() :\n*   Decompression using a pre-defined Dictionary content (see dictBuilder).\n*   Dictionary must be identical to the one used during compression.\n*   Note : This function load the dictionary, resulting in a significant startup time */\nZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,\n                                                   void* dst, size_t dstCapacity,\n                                             const void* src, size_t srcSize,\n                                             const void* dict,size_t dictSize);\n\n\n/*-**************************\n*  Advanced Dictionary API\n****************************/\n/*! ZSTDv07_createDDict() :\n*   Create a digested dictionary, ready to start decompression operation without startup delay.\n*   `dict` can be released after creation */\ntypedef struct ZSTDv07_DDict_s ZSTDv07_DDict;\nZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize);\nZSTDLIBv07_API size_t      ZSTDv07_freeDDict(ZSTDv07_DDict* ddict);\n\n/*! ZSTDv07_decompress_usingDDict() :\n*   Decompression using a pre-digested Dictionary\n*   Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */\nZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,\n                                                    void* dst, size_t dstCapacity,\n                                              const void* src, size_t srcSize,\n                                              const ZSTDv07_DDict* ddict);\n\ntypedef struct {\n    unsigned long long frameContentSize;\n    unsigned windowSize;\n    unsigned dictID;\n    unsigned checksumFlag;\n} ZSTDv07_frameParams;\n\nZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize);   /**< doesn't consume input */\n\n\n\n\n/* *************************************\n*  Streaming functions\n***************************************/\ntypedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx;\nZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void);\nZSTDLIBv07_API size_t      ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx);\n\nZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx);\nZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize);\n\nZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx,\n                                            void* dst, size_t* dstCapacityPtr,\n                                      const void* src, size_t* srcSizePtr);\n\n/*-***************************************************************************\n*  Streaming decompression howto\n*\n*  A ZBUFFv07_DCtx object is required to track streaming operations.\n*  Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.\n*  Use ZBUFFv07_decompressInit() to start a new decompression operation,\n*   or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.\n*  Note that ZBUFFv07_DCtx objects can be re-init multiple times.\n*\n*  Use ZBUFFv07_decompressContinue() repetitively to consume your input.\n*  *srcSizePtr and *dstCapacityPtr can be any size.\n*  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.\n*  Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.\n*  The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.\n*  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),\n*            or 0 when a frame is completely decoded,\n*            or an error code, which can be tested using ZBUFFv07_isError().\n*\n*  Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()\n*  output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.\n*  input  : ZBUFFv07_recommendedDInSize == 128KB + 3;\n*           just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .\n* *******************************************************************************/\n\n\n/* *************************************\n*  Tool functions\n***************************************/\nZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode);\nZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode);\n\n/** Functions below provide recommended buffer sizes for Compression or Decompression operations.\n*   These sizes are just hints, they tend to offer better latency */\nZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void);\nZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void);\n\n\n/*-*************************************\n*  Constants\n***************************************/\n#define ZSTDv07_MAGICNUMBER            0xFD2FB527   /* v0.7 */\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif  /* ZSTDv07_H_235446 */\n/**** ended inlining zstd_v07.h ****/\n#endif\n\n/** ZSTD_isLegacy() :\n    @return : > 0 if supported by legacy decoder. 0 otherwise.\n              return value is the version.\n*/\nMEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)\n{\n    U32 magicNumberLE;\n    if (srcSize<4) return 0;\n    magicNumberLE = MEM_readLE32(src);\n    switch(magicNumberLE)\n    {\n#if (ZSTD_LEGACY_SUPPORT <= 1)\n        case ZSTDv01_magicNumberLE:return 1;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 2)\n        case ZSTDv02_magicNumber : return 2;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 3)\n        case ZSTDv03_magicNumber : return 3;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n        case ZSTDv04_magicNumber : return 4;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n        case ZSTDv05_MAGICNUMBER : return 5;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n        case ZSTDv06_MAGICNUMBER : return 6;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n        case ZSTDv07_MAGICNUMBER : return 7;\n#endif\n        default : return 0;\n    }\n}\n\n\nMEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)\n{\n    U32 const version = ZSTD_isLegacy(src, srcSize);\n    if (version < 5) return 0;  /* no decompressed size in frame header, or not a legacy format */\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n    if (version==5) {\n        ZSTDv05_parameters fParams;\n        size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize);\n        if (frResult != 0) return 0;\n        return fParams.srcSize;\n    }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n    if (version==6) {\n        ZSTDv06_frameParams fParams;\n        size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize);\n        if (frResult != 0) return 0;\n        return fParams.frameContentSize;\n    }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n    if (version==7) {\n        ZSTDv07_frameParams fParams;\n        size_t const frResult = ZSTDv07_getFrameParams(&fParams, src, srcSize);\n        if (frResult != 0) return 0;\n        return fParams.frameContentSize;\n    }\n#endif\n    return 0;   /* should not be possible */\n}\n\n\nMEM_STATIC size_t ZSTD_decompressLegacy(\n                     void* dst, size_t dstCapacity,\n               const void* src, size_t compressedSize,\n               const void* dict,size_t dictSize)\n{\n    U32 const version = ZSTD_isLegacy(src, compressedSize);\n    (void)dst; (void)dstCapacity; (void)dict; (void)dictSize;  /* unused when ZSTD_LEGACY_SUPPORT >= 8 */\n    switch(version)\n    {\n#if (ZSTD_LEGACY_SUPPORT <= 1)\n        case 1 :\n            return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 2)\n        case 2 :\n            return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 3)\n        case 3 :\n            return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n        case 4 :\n            return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n        case 5 :\n            {   size_t result;\n                ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();\n                if (zd==NULL) return ERROR(memory_allocation);\n                result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);\n                ZSTDv05_freeDCtx(zd);\n                return result;\n            }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n        case 6 :\n            {   size_t result;\n                ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();\n                if (zd==NULL) return ERROR(memory_allocation);\n                result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);\n                ZSTDv06_freeDCtx(zd);\n                return result;\n            }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n        case 7 :\n            {   size_t result;\n                ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx();\n                if (zd==NULL) return ERROR(memory_allocation);\n                result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);\n                ZSTDv07_freeDCtx(zd);\n                return result;\n            }\n#endif\n        default :\n            return ERROR(prefix_unknown);\n    }\n}\n\nMEM_STATIC ZSTD_frameSizeInfo ZSTD_findFrameSizeInfoLegacy(const void *src, size_t srcSize)\n{\n    ZSTD_frameSizeInfo frameSizeInfo;\n    U32 const version = ZSTD_isLegacy(src, srcSize);\n    switch(version)\n    {\n#if (ZSTD_LEGACY_SUPPORT <= 1)\n        case 1 :\n            ZSTDv01_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 2)\n        case 2 :\n            ZSTDv02_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 3)\n        case 3 :\n            ZSTDv03_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n        case 4 :\n            ZSTDv04_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n        case 5 :\n            ZSTDv05_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n        case 6 :\n            ZSTDv06_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n        case 7 :\n            ZSTDv07_findFrameSizeInfoLegacy(src, srcSize,\n                &frameSizeInfo.compressedSize,\n                &frameSizeInfo.decompressedBound);\n            break;\n#endif\n        default :\n            frameSizeInfo.compressedSize = ERROR(prefix_unknown);\n            frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;\n            break;\n    }\n    if (!ZSTD_isError(frameSizeInfo.compressedSize) && frameSizeInfo.compressedSize > srcSize) {\n        frameSizeInfo.compressedSize = ERROR(srcSize_wrong);\n        frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;\n    }\n    return frameSizeInfo;\n}\n\nMEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src, size_t srcSize)\n{\n    ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfoLegacy(src, srcSize);\n    return frameSizeInfo.compressedSize;\n}\n\nMEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)\n{\n    switch(version)\n    {\n        default :\n        case 1 :\n        case 2 :\n        case 3 :\n            (void)legacyContext;\n            return ERROR(version_unsupported);\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n        case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n        case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n        case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n        case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);\n#endif\n    }\n}\n\n\nMEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,\n                                        const void* dict, size_t dictSize)\n{\n    DEBUGLOG(5, \"ZSTD_initLegacyStream for v0.%u\", newVersion);\n    if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);\n    switch(newVersion)\n    {\n        default :\n        case 1 :\n        case 2 :\n        case 3 :\n            (void)dict; (void)dictSize;\n            return 0;\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n        case 4 :\n        {\n            ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;\n            if (dctx==NULL) return ERROR(memory_allocation);\n            ZBUFFv04_decompressInit(dctx);\n            ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);\n            *legacyContext = dctx;\n            return 0;\n        }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n        case 5 :\n        {\n            ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;\n            if (dctx==NULL) return ERROR(memory_allocation);\n            ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);\n            *legacyContext = dctx;\n            return 0;\n        }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n        case 6 :\n        {\n            ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;\n            if (dctx==NULL) return ERROR(memory_allocation);\n            ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);\n            *legacyContext = dctx;\n            return 0;\n        }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n        case 7 :\n        {\n            ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;\n            if (dctx==NULL) return ERROR(memory_allocation);\n            ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);\n            *legacyContext = dctx;\n            return 0;\n        }\n#endif\n    }\n}\n\n\n\nMEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,\n                                              ZSTD_outBuffer* output, ZSTD_inBuffer* input)\n{\n    DEBUGLOG(5, \"ZSTD_decompressLegacyStream for v0.%u\", version);\n    switch(version)\n    {\n        default :\n        case 1 :\n        case 2 :\n        case 3 :\n            (void)legacyContext; (void)output; (void)input;\n            return ERROR(version_unsupported);\n#if (ZSTD_LEGACY_SUPPORT <= 4)\n        case 4 :\n            {\n                ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;\n                const void* src = (const char*)input->src + input->pos;\n                size_t readSize = input->size - input->pos;\n                void* dst = (char*)output->dst + output->pos;\n                size_t decodedSize = output->size - output->pos;\n                size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);\n                output->pos += decodedSize;\n                input->pos += readSize;\n                return hintSize;\n            }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 5)\n        case 5 :\n            {\n                ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;\n                const void* src = (const char*)input->src + input->pos;\n                size_t readSize = input->size - input->pos;\n                void* dst = (char*)output->dst + output->pos;\n                size_t decodedSize = output->size - output->pos;\n                size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);\n                output->pos += decodedSize;\n                input->pos += readSize;\n                return hintSize;\n            }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 6)\n        case 6 :\n            {\n                ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;\n                const void* src = (const char*)input->src + input->pos;\n                size_t readSize = input->size - input->pos;\n                void* dst = (char*)output->dst + output->pos;\n                size_t decodedSize = output->size - output->pos;\n                size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);\n                output->pos += decodedSize;\n                input->pos += readSize;\n                return hintSize;\n            }\n#endif\n#if (ZSTD_LEGACY_SUPPORT <= 7)\n        case 7 :\n            {\n                ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;\n                const void* src = (const char*)input->src + input->pos;\n                size_t readSize = input->size - input->pos;\n                void* dst = (char*)output->dst + output->pos;\n                size_t decodedSize = output->size - output->pos;\n                size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);\n                output->pos += decodedSize;\n                input->pos += readSize;\n                return hintSize;\n            }\n#endif\n    }\n}\n\n\n#if defined (__cplusplus)\n}\n#endif\n\n#endif   /* ZSTD_LEGACY_H */\n/**** ended inlining ../legacy/zstd_legacy.h ****/\n#endif\n\n\n\n/*-*******************************************************\n*  Types\n*********************************************************/\nstruct ZSTD_DDict_s {\n    void* dictBuffer;\n    const void* dictContent;\n    size_t dictSize;\n    ZSTD_entropyDTables_t entropy;\n    U32 dictID;\n    U32 entropyPresent;\n    ZSTD_customMem cMem;\n};  /* typedef'd to ZSTD_DDict within \"zstd.h\" */\n\nconst void* ZSTD_DDict_dictContent(const ZSTD_DDict* ddict)\n{\n    assert(ddict != NULL);\n    return ddict->dictContent;\n}\n\nsize_t ZSTD_DDict_dictSize(const ZSTD_DDict* ddict)\n{\n    assert(ddict != NULL);\n    return ddict->dictSize;\n}\n\nvoid ZSTD_copyDDictParameters(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)\n{\n    DEBUGLOG(4, \"ZSTD_copyDDictParameters\");\n    assert(dctx != NULL);\n    assert(ddict != NULL);\n    dctx->dictID = ddict->dictID;\n    dctx->prefixStart = ddict->dictContent;\n    dctx->virtualStart = ddict->dictContent;\n    dctx->dictEnd = (const BYTE*)ddict->dictContent + ddict->dictSize;\n    dctx->previousDstEnd = dctx->dictEnd;\n#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION\n    dctx->dictContentBeginForFuzzing = dctx->prefixStart;\n    dctx->dictContentEndForFuzzing = dctx->previousDstEnd;\n#endif\n    if (ddict->entropyPresent) {\n        dctx->litEntropy = 1;\n        dctx->fseEntropy = 1;\n        dctx->LLTptr = ddict->entropy.LLTable;\n        dctx->MLTptr = ddict->entropy.MLTable;\n        dctx->OFTptr = ddict->entropy.OFTable;\n        dctx->HUFptr = ddict->entropy.hufTable;\n        dctx->entropy.rep[0] = ddict->entropy.rep[0];\n        dctx->entropy.rep[1] = ddict->entropy.rep[1];\n        dctx->entropy.rep[2] = ddict->entropy.rep[2];\n    } else {\n        dctx->litEntropy = 0;\n        dctx->fseEntropy = 0;\n    }\n}\n\n\nstatic size_t\nZSTD_loadEntropy_intoDDict(ZSTD_DDict* ddict,\n                           ZSTD_dictContentType_e dictContentType)\n{\n    ddict->dictID = 0;\n    ddict->entropyPresent = 0;\n    if (dictContentType == ZSTD_dct_rawContent) return 0;\n\n    if (ddict->dictSize < 8) {\n        if (dictContentType == ZSTD_dct_fullDict)\n            return ERROR(dictionary_corrupted);   /* only accept specified dictionaries */\n        return 0;   /* pure content mode */\n    }\n    {   U32 const magic = MEM_readLE32(ddict->dictContent);\n        if (magic != ZSTD_MAGIC_DICTIONARY) {\n            if (dictContentType == ZSTD_dct_fullDict)\n                return ERROR(dictionary_corrupted);   /* only accept specified dictionaries */\n            return 0;   /* pure content mode */\n        }\n    }\n    ddict->dictID = MEM_readLE32((const char*)ddict->dictContent + ZSTD_FRAMEIDSIZE);\n\n    /* load entropy tables */\n    RETURN_ERROR_IF(ZSTD_isError(ZSTD_loadDEntropy(\n            &ddict->entropy, ddict->dictContent, ddict->dictSize)),\n        dictionary_corrupted, \"\");\n    ddict->entropyPresent = 1;\n    return 0;\n}\n\n\nstatic size_t ZSTD_initDDict_internal(ZSTD_DDict* ddict,\n                                      const void* dict, size_t dictSize,\n                                      ZSTD_dictLoadMethod_e dictLoadMethod,\n                                      ZSTD_dictContentType_e dictContentType)\n{\n    if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dict) || (!dictSize)) {\n        ddict->dictBuffer = NULL;\n        ddict->dictContent = dict;\n        if (!dict) dictSize = 0;\n    } else {\n        void* const internalBuffer = ZSTD_malloc(dictSize, ddict->cMem);\n        ddict->dictBuffer = internalBuffer;\n        ddict->dictContent = internalBuffer;\n        if (!internalBuffer) return ERROR(memory_allocation);\n        memcpy(internalBuffer, dict, dictSize);\n    }\n    ddict->dictSize = dictSize;\n    ddict->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001);  /* cover both little and big endian */\n\n    /* parse dictionary content */\n    FORWARD_IF_ERROR( ZSTD_loadEntropy_intoDDict(ddict, dictContentType) , \"\");\n\n    return 0;\n}\n\nZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,\n                                      ZSTD_dictLoadMethod_e dictLoadMethod,\n                                      ZSTD_dictContentType_e dictContentType,\n                                      ZSTD_customMem customMem)\n{\n    if (!customMem.customAlloc ^ !customMem.customFree) return NULL;\n\n    {   ZSTD_DDict* const ddict = (ZSTD_DDict*) ZSTD_malloc(sizeof(ZSTD_DDict), customMem);\n        if (ddict == NULL) return NULL;\n        ddict->cMem = customMem;\n        {   size_t const initResult = ZSTD_initDDict_internal(ddict,\n                                            dict, dictSize,\n                                            dictLoadMethod, dictContentType);\n            if (ZSTD_isError(initResult)) {\n                ZSTD_freeDDict(ddict);\n                return NULL;\n        }   }\n        return ddict;\n    }\n}\n\n/*! ZSTD_createDDict() :\n*   Create a digested dictionary, to start decompression without startup delay.\n*   `dict` content is copied inside DDict.\n*   Consequently, `dict` can be released after `ZSTD_DDict` creation */\nZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize)\n{\n    ZSTD_customMem const allocator = { NULL, NULL, NULL };\n    return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto, allocator);\n}\n\n/*! ZSTD_createDDict_byReference() :\n *  Create a digested dictionary, to start decompression without startup delay.\n *  Dictionary content is simply referenced, it will be accessed during decompression.\n *  Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */\nZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize)\n{\n    ZSTD_customMem const allocator = { NULL, NULL, NULL };\n    return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto, allocator);\n}\n\n\nconst ZSTD_DDict* ZSTD_initStaticDDict(\n                                void* sBuffer, size_t sBufferSize,\n                                const void* dict, size_t dictSize,\n                                ZSTD_dictLoadMethod_e dictLoadMethod,\n                                ZSTD_dictContentType_e dictContentType)\n{\n    size_t const neededSpace = sizeof(ZSTD_DDict)\n                             + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);\n    ZSTD_DDict* const ddict = (ZSTD_DDict*)sBuffer;\n    assert(sBuffer != NULL);\n    assert(dict != NULL);\n    if ((size_t)sBuffer & 7) return NULL;   /* 8-aligned */\n    if (sBufferSize < neededSpace) return NULL;\n    if (dictLoadMethod == ZSTD_dlm_byCopy) {\n        memcpy(ddict+1, dict, dictSize);  /* local copy */\n        dict = ddict+1;\n    }\n    if (ZSTD_isError( ZSTD_initDDict_internal(ddict,\n                                              dict, dictSize,\n                                              ZSTD_dlm_byRef, dictContentType) ))\n        return NULL;\n    return ddict;\n}\n\n\nsize_t ZSTD_freeDDict(ZSTD_DDict* ddict)\n{\n    if (ddict==NULL) return 0;   /* support free on NULL */\n    {   ZSTD_customMem const cMem = ddict->cMem;\n        ZSTD_free(ddict->dictBuffer, cMem);\n        ZSTD_free(ddict, cMem);\n        return 0;\n    }\n}\n\n/*! ZSTD_estimateDDictSize() :\n *  Estimate amount of memory that will be needed to create a dictionary for decompression.\n *  Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */\nsize_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod)\n{\n    return sizeof(ZSTD_DDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize);\n}\n\nsize_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict)\n{\n    if (ddict==NULL) return 0;   /* support sizeof on NULL */\n    return sizeof(*ddict) + (ddict->dictBuffer ? ddict->dictSize : 0) ;\n}\n\n/*! ZSTD_getDictID_fromDDict() :\n *  Provides the dictID of the dictionary loaded into `ddict`.\n *  If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.\n *  Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */\nunsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict)\n{\n    if (ddict==NULL) return 0;\n    return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize);\n}\n/**** ended inlining decompress/zstd_ddict.c ****/\n/**** start inlining decompress/zstd_decompress.c ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n\n/* ***************************************************************\n*  Tuning parameters\n*****************************************************************/\n/*!\n * HEAPMODE :\n * Select how default decompression function ZSTD_decompress() allocates its context,\n * on stack (0), or into heap (1, default; requires malloc()).\n * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected.\n */\n#ifndef ZSTD_HEAPMODE\n#  define ZSTD_HEAPMODE 1\n#endif\n\n/*!\n*  LEGACY_SUPPORT :\n*  if set to 1+, ZSTD_decompress() can decode older formats (v0.1+)\n*/\n#ifndef ZSTD_LEGACY_SUPPORT\n#  define ZSTD_LEGACY_SUPPORT 0\n#endif\n\n/*!\n *  MAXWINDOWSIZE_DEFAULT :\n *  maximum window size accepted by DStream __by default__.\n *  Frames requiring more memory will be rejected.\n *  It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize().\n */\n#ifndef ZSTD_MAXWINDOWSIZE_DEFAULT\n#  define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1)\n#endif\n\n/*!\n *  NO_FORWARD_PROGRESS_MAX :\n *  maximum allowed nb of calls to ZSTD_decompressStream()\n *  without any forward progress\n *  (defined as: no byte read from input, and no byte flushed to output)\n *  before triggering an error.\n */\n#ifndef ZSTD_NO_FORWARD_PROGRESS_MAX\n#  define ZSTD_NO_FORWARD_PROGRESS_MAX 16\n#endif\n\n\n/*-*******************************************************\n*  Dependencies\n*********************************************************/\n/**** skipping file: ../common/cpu.h ****/\n/**** skipping file: ../common/mem.h ****/\n#define FSE_STATIC_LINKING_ONLY\n/**** skipping file: ../common/fse.h ****/\n#define HUF_STATIC_LINKING_ONLY\n/**** skipping file: ../common/huf.h ****/\n/**** skipping file: ../common/zstd_internal.h ****/\n/**** skipping file: zstd_decompress_internal.h ****/\n/**** skipping file: zstd_ddict.h ****/\n/**** start inlining zstd_decompress_block.h ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n\n#ifndef ZSTD_DEC_BLOCK_H\n#define ZSTD_DEC_BLOCK_H\n\n/*-*******************************************************\n *  Dependencies\n *********************************************************/\n#include <stddef.h>   /* size_t */\n/**** skipping file: ../zstd.h ****/\n/**** skipping file: ../common/zstd_internal.h ****/\n/**** skipping file: zstd_decompress_internal.h ****/\n\n\n/* ===   Prototypes   === */\n\n/* note: prototypes already published within `zstd.h` :\n * ZSTD_decompressBlock()\n */\n\n/* note: prototypes already published within `zstd_internal.h` :\n * ZSTD_getcBlockSize()\n * ZSTD_decodeSeqHeaders()\n */\n\n\n/* ZSTD_decompressBlock_internal() :\n * decompress block, starting at `src`,\n * into destination buffer `dst`.\n * @return : decompressed block size,\n *           or an error code (which can be tested using ZSTD_isError())\n */\nsize_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,\n                               void* dst, size_t dstCapacity,\n                         const void* src, size_t srcSize, const int frame);\n\n/* ZSTD_buildFSETable() :\n * generate FSE decoding table for one symbol (ll, ml or off)\n * this function must be called with valid parameters only\n * (dt is large enough, normalizedCounter distribution total is a power of 2, max is within range, etc.)\n * in which case it cannot fail.\n * Internal use only.\n */\nvoid ZSTD_buildFSETable(ZSTD_seqSymbol* dt,\n             const short* normalizedCounter, unsigned maxSymbolValue,\n             const U32* baseValue, const U32* nbAdditionalBits,\n                   unsigned tableLog);\n\n\n#endif /* ZSTD_DEC_BLOCK_H */\n/**** ended inlining zstd_decompress_block.h ****/\n\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)\n/**** skipping file: ../legacy/zstd_legacy.h ****/\n#endif\n\n\n/*-*************************************************************\n*   Context management\n***************************************************************/\nsize_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx)\n{\n    if (dctx==NULL) return 0;   /* support sizeof NULL */\n    return sizeof(*dctx)\n           + ZSTD_sizeof_DDict(dctx->ddictLocal)\n           + dctx->inBuffSize + dctx->outBuffSize;\n}\n\nsize_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); }\n\n\nstatic size_t ZSTD_startingInputLength(ZSTD_format_e format)\n{\n    size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format);\n    /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */\n    assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) );\n    return startingInputLength;\n}\n\nstatic void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)\n{\n    dctx->format = ZSTD_f_zstd1;  /* ZSTD_decompressBegin() invokes ZSTD_startingInputLength() with argument dctx->format */\n    dctx->staticSize  = 0;\n    dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;\n    dctx->ddict       = NULL;\n    dctx->ddictLocal  = NULL;\n    dctx->dictEnd     = NULL;\n    dctx->ddictIsCold = 0;\n    dctx->dictUses = ZSTD_dont_use;\n    dctx->inBuff      = NULL;\n    dctx->inBuffSize  = 0;\n    dctx->outBuffSize = 0;\n    dctx->streamStage = zdss_init;\n    dctx->legacyContext = NULL;\n    dctx->previousLegacyVersion = 0;\n    dctx->noForwardProgress = 0;\n    dctx->oversizedDuration = 0;\n    dctx->bmi2 = ZSTD_cpuid_bmi2(ZSTD_cpuid());\n    dctx->outBufferMode = ZSTD_obm_buffered;\n#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION\n    dctx->dictContentEndForFuzzing = NULL;\n#endif\n}\n\nZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)\n{\n    ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace;\n\n    if ((size_t)workspace & 7) return NULL;  /* 8-aligned */\n    if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL;  /* minimum size */\n\n    ZSTD_initDCtx_internal(dctx);\n    dctx->staticSize = workspaceSize;\n    dctx->inBuff = (char*)(dctx+1);\n    return dctx;\n}\n\nZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)\n{\n    if (!customMem.customAlloc ^ !customMem.customFree) return NULL;\n\n    {   ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);\n        if (!dctx) return NULL;\n        dctx->customMem = customMem;\n        ZSTD_initDCtx_internal(dctx);\n        return dctx;\n    }\n}\n\nZSTD_DCtx* ZSTD_createDCtx(void)\n{\n    DEBUGLOG(3, \"ZSTD_createDCtx\");\n    return ZSTD_createDCtx_advanced(ZSTD_defaultCMem);\n}\n\nstatic void ZSTD_clearDict(ZSTD_DCtx* dctx)\n{\n    ZSTD_freeDDict(dctx->ddictLocal);\n    dctx->ddictLocal = NULL;\n    dctx->ddict = NULL;\n    dctx->dictUses = ZSTD_dont_use;\n}\n\nsize_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)\n{\n    if (dctx==NULL) return 0;   /* support free on NULL */\n    RETURN_ERROR_IF(dctx->staticSize, memory_allocation, \"not compatible with static DCtx\");\n    {   ZSTD_customMem const cMem = dctx->customMem;\n        ZSTD_clearDict(dctx);\n        ZSTD_free(dctx->inBuff, cMem);\n        dctx->inBuff = NULL;\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)\n        if (dctx->legacyContext)\n            ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);\n#endif\n        ZSTD_free(dctx, cMem);\n        return 0;\n    }\n}\n\n/* no longer useful */\nvoid ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)\n{\n    size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);\n    memcpy(dstDCtx, srcDCtx, toCopy);  /* no need to copy workspace */\n}\n\n\n/*-*************************************************************\n *   Frame header decoding\n ***************************************************************/\n\n/*! ZSTD_isFrame() :\n *  Tells if the content of `buffer` starts with a valid Frame Identifier.\n *  Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.\n *  Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.\n *  Note 3 : Skippable Frame Identifiers are considered valid. */\nunsigned ZSTD_isFrame(const void* buffer, size_t size)\n{\n    if (size < ZSTD_FRAMEIDSIZE) return 0;\n    {   U32 const magic = MEM_readLE32(buffer);\n        if (magic == ZSTD_MAGICNUMBER) return 1;\n        if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1;\n    }\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)\n    if (ZSTD_isLegacy(buffer, size)) return 1;\n#endif\n    return 0;\n}\n\n/** ZSTD_frameHeaderSize_internal() :\n *  srcSize must be large enough to reach header size fields.\n *  note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless.\n * @return : size of the Frame Header\n *           or an error code, which can be tested with ZSTD_isError() */\nstatic size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format)\n{\n    size_t const minInputSize = ZSTD_startingInputLength(format);\n    RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, \"\");\n\n    {   BYTE const fhd = ((const BYTE*)src)[minInputSize-1];\n        U32 const dictID= fhd & 3;\n        U32 const singleSegment = (fhd >> 5) & 1;\n        U32 const fcsId = fhd >> 6;\n        return minInputSize + !singleSegment\n             + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId]\n             + (singleSegment && !fcsId);\n    }\n}\n\n/** ZSTD_frameHeaderSize() :\n *  srcSize must be >= ZSTD_frameHeaderSize_prefix.\n * @return : size of the Frame Header,\n *           or an error code (if srcSize is too small) */\nsize_t ZSTD_frameHeaderSize(const void* src, size_t srcSize)\n{\n    return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1);\n}\n\n\n/** ZSTD_getFrameHeader_advanced() :\n *  decode Frame Header, or require larger `srcSize`.\n *  note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless\n * @return : 0, `zfhPtr` is correctly filled,\n *          >0, `srcSize` is too small, value is wanted `srcSize` amount,\n *           or an error code, which can be tested using ZSTD_isError() */\nsize_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format)\n{\n    const BYTE* ip = (const BYTE*)src;\n    size_t const minInputSize = ZSTD_startingInputLength(format);\n\n    memset(zfhPtr, 0, sizeof(*zfhPtr));   /* not strictly necessary, but static analyzer do not understand that zfhPtr is only going to be read only if return value is zero, since they are 2 different signals */\n    if (srcSize < minInputSize) return minInputSize;\n    RETURN_ERROR_IF(src==NULL, GENERIC, \"invalid parameter\");\n\n    if ( (format != ZSTD_f_zstd1_magicless)\n      && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) {\n        if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {\n            /* skippable frame */\n            if (srcSize < ZSTD_SKIPPABLEHEADERSIZE)\n                return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */\n            memset(zfhPtr, 0, sizeof(*zfhPtr));\n            zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE);\n            zfhPtr->frameType = ZSTD_skippableFrame;\n            return 0;\n        }\n        RETURN_ERROR(prefix_unknown, \"\");\n    }\n\n    /* ensure there is enough `srcSize` to fully read/decode frame header */\n    {   size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format);\n        if (srcSize < fhsize) return fhsize;\n        zfhPtr->headerSize = (U32)fhsize;\n    }\n\n    {   BYTE const fhdByte = ip[minInputSize-1];\n        size_t pos = minInputSize;\n        U32 const dictIDSizeCode = fhdByte&3;\n        U32 const checksumFlag = (fhdByte>>2)&1;\n        U32 const singleSegment = (fhdByte>>5)&1;\n        U32 const fcsID = fhdByte>>6;\n        U64 windowSize = 0;\n        U32 dictID = 0;\n        U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN;\n        RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported,\n                        \"reserved bits, must be zero\");\n\n        if (!singleSegment) {\n            BYTE const wlByte = ip[pos++];\n            U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN;\n            RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, \"\");\n            windowSize = (1ULL << windowLog);\n            windowSize += (windowSize >> 3) * (wlByte&7);\n        }\n        switch(dictIDSizeCode)\n        {\n            default: assert(0);  /* impossible */\n            case 0 : break;\n            case 1 : dictID = ip[pos]; pos++; break;\n            case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;\n            case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;\n        }\n        switch(fcsID)\n        {\n            default: assert(0);  /* impossible */\n            case 0 : if (singleSegment) frameContentSize = ip[pos]; break;\n            case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;\n            case 2 : frameContentSize = MEM_readLE32(ip+pos); break;\n            case 3 : frameContentSize = MEM_readLE64(ip+pos); break;\n        }\n        if (singleSegment) windowSize = frameContentSize;\n\n        zfhPtr->frameType = ZSTD_frame;\n        zfhPtr->frameContentSize = frameContentSize;\n        zfhPtr->windowSize = windowSize;\n        zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);\n        zfhPtr->dictID = dictID;\n        zfhPtr->checksumFlag = checksumFlag;\n    }\n    return 0;\n}\n\n/** ZSTD_getFrameHeader() :\n *  decode Frame Header, or require larger `srcSize`.\n *  note : this function does not consume input, it only reads it.\n * @return : 0, `zfhPtr` is correctly filled,\n *          >0, `srcSize` is too small, value is wanted `srcSize` amount,\n *           or an error code, which can be tested using ZSTD_isError() */\nsize_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize)\n{\n    return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1);\n}\n\n\n/** ZSTD_getFrameContentSize() :\n *  compatible with legacy mode\n * @return : decompressed size of the single frame pointed to be `src` if known, otherwise\n *         - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined\n *         - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */\nunsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize)\n{\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)\n    if (ZSTD_isLegacy(src, srcSize)) {\n        unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize);\n        return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret;\n    }\n#endif\n    {   ZSTD_frameHeader zfh;\n        if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0)\n            return ZSTD_CONTENTSIZE_ERROR;\n        if (zfh.frameType == ZSTD_skippableFrame) {\n            return 0;\n        } else {\n            return zfh.frameContentSize;\n    }   }\n}\n\nstatic size_t readSkippableFrameSize(void const* src, size_t srcSize)\n{\n    size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE;\n    U32 sizeU32;\n\n    RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, \"\");\n\n    sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE);\n    RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32,\n                    frameParameter_unsupported, \"\");\n    {\n        size_t const skippableSize = skippableHeaderSize + sizeU32;\n        RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, \"\");\n        return skippableSize;\n    }\n}\n\n/** ZSTD_findDecompressedSize() :\n *  compatible with legacy mode\n *  `srcSize` must be the exact length of some number of ZSTD compressed and/or\n *      skippable frames\n *  @return : decompressed size of the frames contained */\nunsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize)\n{\n    unsigned long long totalDstSize = 0;\n\n    while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) {\n        U32 const magicNumber = MEM_readLE32(src);\n\n        if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {\n            size_t const skippableSize = readSkippableFrameSize(src, srcSize);\n            if (ZSTD_isError(skippableSize)) {\n                return ZSTD_CONTENTSIZE_ERROR;\n            }\n            assert(skippableSize <= srcSize);\n\n            src = (const BYTE *)src + skippableSize;\n            srcSize -= skippableSize;\n            continue;\n        }\n\n        {   unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);\n            if (ret >= ZSTD_CONTENTSIZE_ERROR) return ret;\n\n            /* check for overflow */\n            if (totalDstSize + ret < totalDstSize) return ZSTD_CONTENTSIZE_ERROR;\n            totalDstSize += ret;\n        }\n        {   size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize);\n            if (ZSTD_isError(frameSrcSize)) {\n                return ZSTD_CONTENTSIZE_ERROR;\n            }\n\n            src = (const BYTE *)src + frameSrcSize;\n            srcSize -= frameSrcSize;\n        }\n    }  /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */\n\n    if (srcSize) return ZSTD_CONTENTSIZE_ERROR;\n\n    return totalDstSize;\n}\n\n/** ZSTD_getDecompressedSize() :\n *  compatible with legacy mode\n * @return : decompressed size if known, 0 otherwise\n             note : 0 can mean any of the following :\n                   - frame content is empty\n                   - decompressed size field is not present in frame header\n                   - frame header unknown / not supported\n                   - frame header not complete (`srcSize` too small) */\nunsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)\n{\n    unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize);\n    ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN);\n    return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret;\n}\n\n\n/** ZSTD_decodeFrameHeader() :\n * `headerSize` must be the size provided by ZSTD_frameHeaderSize().\n * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */\nstatic size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)\n{\n    size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format);\n    if (ZSTD_isError(result)) return result;    /* invalid header */\n    RETURN_ERROR_IF(result>0, srcSize_wrong, \"headerSize too small\");\n#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION\n    /* Skip the dictID check in fuzzing mode, because it makes the search\n     * harder.\n     */\n    RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID),\n                    dictionary_wrong, \"\");\n#endif\n    if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);\n    return 0;\n}\n\nstatic ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)\n{\n    ZSTD_frameSizeInfo frameSizeInfo;\n    frameSizeInfo.compressedSize = ret;\n    frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;\n    return frameSizeInfo;\n}\n\nstatic ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize)\n{\n    ZSTD_frameSizeInfo frameSizeInfo;\n    memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo));\n\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)\n    if (ZSTD_isLegacy(src, srcSize))\n        return ZSTD_findFrameSizeInfoLegacy(src, srcSize);\n#endif\n\n    if ((srcSize >= ZSTD_SKIPPABLEHEADERSIZE)\n        && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {\n        frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize);\n        assert(ZSTD_isError(frameSizeInfo.compressedSize) ||\n               frameSizeInfo.compressedSize <= srcSize);\n        return frameSizeInfo;\n    } else {\n        const BYTE* ip = (const BYTE*)src;\n        const BYTE* const ipstart = ip;\n        size_t remainingSize = srcSize;\n        size_t nbBlocks = 0;\n        ZSTD_frameHeader zfh;\n\n        /* Extract Frame Header */\n        {   size_t const ret = ZSTD_getFrameHeader(&zfh, src, srcSize);\n            if (ZSTD_isError(ret))\n                return ZSTD_errorFrameSizeInfo(ret);\n            if (ret > 0)\n                return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));\n        }\n\n        ip += zfh.headerSize;\n        remainingSize -= zfh.headerSize;\n\n        /* Iterate over each block */\n        while (1) {\n            blockProperties_t blockProperties;\n            size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);\n            if (ZSTD_isError(cBlockSize))\n                return ZSTD_errorFrameSizeInfo(cBlockSize);\n\n            if (ZSTD_blockHeaderSize + cBlockSize > remainingSize)\n                return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));\n\n            ip += ZSTD_blockHeaderSize + cBlockSize;\n            remainingSize -= ZSTD_blockHeaderSize + cBlockSize;\n            nbBlocks++;\n\n            if (blockProperties.lastBlock) break;\n        }\n\n        /* Final frame content checksum */\n        if (zfh.checksumFlag) {\n            if (remainingSize < 4)\n                return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong));\n            ip += 4;\n        }\n\n        frameSizeInfo.compressedSize = ip - ipstart;\n        frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN)\n                                        ? zfh.frameContentSize\n                                        : nbBlocks * zfh.blockSizeMax;\n        return frameSizeInfo;\n    }\n}\n\n/** ZSTD_findFrameCompressedSize() :\n *  compatible with legacy mode\n *  `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame\n *  `srcSize` must be at least as large as the frame contained\n *  @return : the compressed size of the frame starting at `src` */\nsize_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)\n{\n    ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);\n    return frameSizeInfo.compressedSize;\n}\n\n/** ZSTD_decompressBound() :\n *  compatible with legacy mode\n *  `src` must point to the start of a ZSTD frame or a skippeable frame\n *  `srcSize` must be at least as large as the frame contained\n *  @return : the maximum decompressed size of the compressed source\n */\nunsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)\n{\n    unsigned long long bound = 0;\n    /* Iterate over each frame */\n    while (srcSize > 0) {\n        ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);\n        size_t const compressedSize = frameSizeInfo.compressedSize;\n        unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;\n        if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)\n            return ZSTD_CONTENTSIZE_ERROR;\n        assert(srcSize >= compressedSize);\n        src = (const BYTE*)src + compressedSize;\n        srcSize -= compressedSize;\n        bound += decompressedBound;\n    }\n    return bound;\n}\n\n\n/*-*************************************************************\n *   Frame decoding\n ***************************************************************/\n\n/** ZSTD_insertBlock() :\n *  insert `src` block into `dctx` history. Useful to track uncompressed blocks. */\nsize_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize)\n{\n    DEBUGLOG(5, \"ZSTD_insertBlock: %u bytes\", (unsigned)blockSize);\n    ZSTD_checkContinuity(dctx, blockStart);\n    dctx->previousDstEnd = (const char*)blockStart + blockSize;\n    return blockSize;\n}\n\n\nstatic size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity,\n                          const void* src, size_t srcSize)\n{\n    DEBUGLOG(5, \"ZSTD_copyRawBlock\");\n    if (dst == NULL) {\n        if (srcSize == 0) return 0;\n        RETURN_ERROR(dstBuffer_null, \"\");\n    }\n    RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, \"\");\n    memcpy(dst, src, srcSize);\n    return srcSize;\n}\n\nstatic size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity,\n                               BYTE b,\n                               size_t regenSize)\n{\n    if (dst == NULL) {\n        if (regenSize == 0) return 0;\n        RETURN_ERROR(dstBuffer_null, \"\");\n    }\n    RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, \"\");\n    memset(dst, b, regenSize);\n    return regenSize;\n}\n\n\n/*! ZSTD_decompressFrame() :\n * @dctx must be properly initialized\n *  will update *srcPtr and *srcSizePtr,\n *  to make *srcPtr progress by one frame. */\nstatic size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,\n                                   void* dst, size_t dstCapacity,\n                             const void** srcPtr, size_t *srcSizePtr)\n{\n    const BYTE* ip = (const BYTE*)(*srcPtr);\n    BYTE* const ostart = (BYTE* const)dst;\n    BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart;\n    BYTE* op = ostart;\n    size_t remainingSrcSize = *srcSizePtr;\n\n    DEBUGLOG(4, \"ZSTD_decompressFrame (srcSize:%i)\", (int)*srcSizePtr);\n\n    /* check */\n    RETURN_ERROR_IF(\n        remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize,\n        srcSize_wrong, \"\");\n\n    /* Frame Header */\n    {   size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal(\n                ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format);\n        if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize;\n        RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize,\n                        srcSize_wrong, \"\");\n        FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , \"\");\n        ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize;\n    }\n\n    /* Loop on each block */\n    while (1) {\n        size_t decodedSize;\n        blockProperties_t blockProperties;\n        size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties);\n        if (ZSTD_isError(cBlockSize)) return cBlockSize;\n\n        ip += ZSTD_blockHeaderSize;\n        remainingSrcSize -= ZSTD_blockHeaderSize;\n        RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, \"\");\n\n        switch(blockProperties.blockType)\n        {\n        case bt_compressed:\n            decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize, /* frame */ 1);\n            break;\n        case bt_raw :\n            decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize);\n            break;\n        case bt_rle :\n            decodedSize = ZSTD_setRleBlock(op, oend-op, *ip, blockProperties.origSize);\n            break;\n        case bt_reserved :\n        default:\n            RETURN_ERROR(corruption_detected, \"invalid block type\");\n        }\n\n        if (ZSTD_isError(decodedSize)) return decodedSize;\n        if (dctx->fParams.checksumFlag)\n            XXH64_update(&dctx->xxhState, op, decodedSize);\n        if (decodedSize != 0)\n            op += decodedSize;\n        assert(ip != NULL);\n        ip += cBlockSize;\n        remainingSrcSize -= cBlockSize;\n        if (blockProperties.lastBlock) break;\n    }\n\n    if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) {\n        RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize,\n                        corruption_detected, \"\");\n    }\n    if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */\n        U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState);\n        U32 checkRead;\n        RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, \"\");\n        checkRead = MEM_readLE32(ip);\n        RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, \"\");\n        ip += 4;\n        remainingSrcSize -= 4;\n    }\n\n    /* Allow caller to get size read */\n    *srcPtr = ip;\n    *srcSizePtr = remainingSrcSize;\n    return op-ostart;\n}\n\nstatic size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,\n                                        void* dst, size_t dstCapacity,\n                                  const void* src, size_t srcSize,\n                                  const void* dict, size_t dictSize,\n                                  const ZSTD_DDict* ddict)\n{\n    void* const dststart = dst;\n    int moreThan1Frame = 0;\n\n    DEBUGLOG(5, \"ZSTD_decompressMultiFrame\");\n    assert(dict==NULL || ddict==NULL);  /* either dict or ddict set, not both */\n\n    if (ddict) {\n        dict = ZSTD_DDict_dictContent(ddict);\n        dictSize = ZSTD_DDict_dictSize(ddict);\n    }\n\n    while (srcSize >= ZSTD_startingInputLength(dctx->format)) {\n\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)\n        if (ZSTD_isLegacy(src, srcSize)) {\n            size_t decodedSize;\n            size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);\n            if (ZSTD_isError(frameSize)) return frameSize;\n            RETURN_ERROR_IF(dctx->staticSize, memory_allocation,\n                \"legacy support is not compatible with static dctx\");\n\n            decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);\n            if (ZSTD_isError(decodedSize)) return decodedSize;\n\n            assert(decodedSize <=- dstCapacity);\n            dst = (BYTE*)dst + decodedSize;\n            dstCapacity -= decodedSize;\n\n            src = (const BYTE*)src + frameSize;\n            srcSize -= frameSize;\n\n            continue;\n        }\n#endif\n\n        {   U32 const magicNumber = MEM_readLE32(src);\n            DEBUGLOG(4, \"reading magic number %08X (expecting %08X)\",\n                        (unsigned)magicNumber, ZSTD_MAGICNUMBER);\n            if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {\n                size_t const skippableSize = readSkippableFrameSize(src, srcSize);\n                FORWARD_IF_ERROR(skippableSize, \"readSkippableFrameSize failed\");\n                assert(skippableSize <= srcSize);\n\n                src = (const BYTE *)src + skippableSize;\n                srcSize -= skippableSize;\n                continue;\n        }   }\n\n        if (ddict) {\n            /* we were called from ZSTD_decompress_usingDDict */\n            FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), \"\");\n        } else {\n            /* this will initialize correctly with no dict if dict == NULL, so\n             * use this in all cases but ddict */\n            FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), \"\");\n        }\n        ZSTD_checkContinuity(dctx, dst);\n\n        {   const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity,\n                                                    &src, &srcSize);\n            RETURN_ERROR_IF(\n                (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown)\n             && (moreThan1Frame==1),\n                srcSize_wrong,\n                \"at least one frame successfully completed, but following \"\n                \"bytes are garbage: it's more likely to be a srcSize error, \"\n                \"specifying more bytes than compressed size of frame(s). This \"\n                \"error message replaces ERROR(prefix_unknown), which would be \"\n                \"confusing, as the first header is actually correct. Note that \"\n                \"one could be unlucky, it might be a corruption error instead, \"\n                \"happening right at the place where we expect zstd magic \"\n                \"bytes. But this is _much_ less likely than a srcSize field \"\n                \"error.\");\n            if (ZSTD_isError(res)) return res;\n            assert(res <= dstCapacity);\n            if (res != 0)\n                dst = (BYTE*)dst + res;\n            dstCapacity -= res;\n        }\n        moreThan1Frame = 1;\n    }  /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */\n\n    RETURN_ERROR_IF(srcSize, srcSize_wrong, \"input not entirely consumed\");\n\n    return (BYTE*)dst - (BYTE*)dststart;\n}\n\nsize_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,\n                                 void* dst, size_t dstCapacity,\n                           const void* src, size_t srcSize,\n                           const void* dict, size_t dictSize)\n{\n    return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);\n}\n\n\nstatic ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)\n{\n    switch (dctx->dictUses) {\n    default:\n        assert(0 /* Impossible */);\n        /* fall-through */\n    case ZSTD_dont_use:\n        ZSTD_clearDict(dctx);\n        return NULL;\n    case ZSTD_use_indefinitely:\n        return dctx->ddict;\n    case ZSTD_use_once:\n        dctx->dictUses = ZSTD_dont_use;\n        return dctx->ddict;\n    }\n}\n\nsize_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)\n{\n    return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));\n}\n\n\nsize_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)\n{\n#if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1)\n    size_t regenSize;\n    ZSTD_DCtx* const dctx = ZSTD_createDCtx();\n    RETURN_ERROR_IF(dctx==NULL, memory_allocation, \"NULL pointer!\");\n    regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);\n    ZSTD_freeDCtx(dctx);\n    return regenSize;\n#else   /* stack mode */\n    ZSTD_DCtx dctx;\n    ZSTD_initDCtx_internal(&dctx);\n    return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);\n#endif\n}\n\n\n/*-**************************************\n*   Advanced Streaming Decompression API\n*   Bufferless and synchronous\n****************************************/\nsize_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; }\n\n/**\n * Similar to ZSTD_nextSrcSizeToDecompress(), but when when a block input can be streamed,\n * we allow taking a partial block as the input. Currently only raw uncompressed blocks can\n * be streamed.\n *\n * For blocks that can be streamed, this allows us to reduce the latency until we produce\n * output, and avoid copying the input.\n *\n * @param inputSize - The total amount of input that the caller currently has.\n */\nstatic size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) {\n    if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock))\n        return dctx->expected;\n    if (dctx->bType != bt_raw)\n        return dctx->expected;\n    return MIN(MAX(inputSize, 1), dctx->expected);\n}\n\nZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {\n    switch(dctx->stage)\n    {\n    default:   /* should not happen */\n        assert(0);\n    case ZSTDds_getFrameHeaderSize:\n    case ZSTDds_decodeFrameHeader:\n        return ZSTDnit_frameHeader;\n    case ZSTDds_decodeBlockHeader:\n        return ZSTDnit_blockHeader;\n    case ZSTDds_decompressBlock:\n        return ZSTDnit_block;\n    case ZSTDds_decompressLastBlock:\n        return ZSTDnit_lastBlock;\n    case ZSTDds_checkChecksum:\n        return ZSTDnit_checksum;\n    case ZSTDds_decodeSkippableHeader:\n    case ZSTDds_skipFrame:\n        return ZSTDnit_skippableFrame;\n    }\n}\n\nstatic int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }\n\n/** ZSTD_decompressContinue() :\n *  srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())\n *  @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)\n *            or an error code, which can be tested using ZSTD_isError() */\nsize_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)\n{\n    DEBUGLOG(5, \"ZSTD_decompressContinue (srcSize:%u)\", (unsigned)srcSize);\n    /* Sanity check */\n    RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, \"not allowed\");\n    if (dstCapacity) ZSTD_checkContinuity(dctx, dst);\n\n    switch (dctx->stage)\n    {\n    case ZSTDds_getFrameHeaderSize :\n        assert(src != NULL);\n        if (dctx->format == ZSTD_f_zstd1) {  /* allows header */\n            assert(srcSize >= ZSTD_FRAMEIDSIZE);  /* to read skippable magic number */\n            if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {        /* skippable frame */\n                memcpy(dctx->headerBuffer, src, srcSize);\n                dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize;  /* remaining to load to get full skippable frame header */\n                dctx->stage = ZSTDds_decodeSkippableHeader;\n                return 0;\n        }   }\n        dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format);\n        if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize;\n        memcpy(dctx->headerBuffer, src, srcSize);\n        dctx->expected = dctx->headerSize - srcSize;\n        dctx->stage = ZSTDds_decodeFrameHeader;\n        return 0;\n\n    case ZSTDds_decodeFrameHeader:\n        assert(src != NULL);\n        memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize);\n        FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), \"\");\n        dctx->expected = ZSTD_blockHeaderSize;\n        dctx->stage = ZSTDds_decodeBlockHeader;\n        return 0;\n\n    case ZSTDds_decodeBlockHeader:\n        {   blockProperties_t bp;\n            size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);\n            if (ZSTD_isError(cBlockSize)) return cBlockSize;\n            RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, \"Block Size Exceeds Maximum\");\n            dctx->expected = cBlockSize;\n            dctx->bType = bp.blockType;\n            dctx->rleSize = bp.origSize;\n            if (cBlockSize) {\n                dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;\n                return 0;\n            }\n            /* empty block */\n            if (bp.lastBlock) {\n                if (dctx->fParams.checksumFlag) {\n                    dctx->expected = 4;\n                    dctx->stage = ZSTDds_checkChecksum;\n                } else {\n                    dctx->expected = 0; /* end of frame */\n                    dctx->stage = ZSTDds_getFrameHeaderSize;\n                }\n            } else {\n                dctx->expected = ZSTD_blockHeaderSize;  /* jump to next header */\n                dctx->stage = ZSTDds_decodeBlockHeader;\n            }\n            return 0;\n        }\n\n    case ZSTDds_decompressLastBlock:\n    case ZSTDds_decompressBlock:\n        DEBUGLOG(5, \"ZSTD_decompressContinue: case ZSTDds_decompressBlock\");\n        {   size_t rSize;\n            switch(dctx->bType)\n            {\n            case bt_compressed:\n                DEBUGLOG(5, \"ZSTD_decompressContinue: case bt_compressed\");\n                rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 1);\n                dctx->expected = 0;  /* Streaming not supported */\n                break;\n            case bt_raw :\n                assert(srcSize <= dctx->expected);\n                rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize);\n                FORWARD_IF_ERROR(rSize, \"ZSTD_copyRawBlock failed\");\n                assert(rSize == srcSize);\n                dctx->expected -= rSize;\n                break;\n            case bt_rle :\n                rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize);\n                dctx->expected = 0;  /* Streaming not supported */\n                break;\n            case bt_reserved :   /* should never happen */\n            default:\n                RETURN_ERROR(corruption_detected, \"invalid block type\");\n            }\n            FORWARD_IF_ERROR(rSize, \"\");\n            RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, \"Decompressed Block Size Exceeds Maximum\");\n            DEBUGLOG(5, \"ZSTD_decompressContinue: decoded size from block : %u\", (unsigned)rSize);\n            dctx->decodedSize += rSize;\n            if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);\n            dctx->previousDstEnd = (char*)dst + rSize;\n\n            /* Stay on the same stage until we are finished streaming the block. */\n            if (dctx->expected > 0) {\n                return rSize;\n            }\n\n            if (dctx->stage == ZSTDds_decompressLastBlock) {   /* end of frame */\n                DEBUGLOG(4, \"ZSTD_decompressContinue: decoded size from frame : %u\", (unsigned)dctx->decodedSize);\n                RETURN_ERROR_IF(\n                    dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN\n                 && dctx->decodedSize != dctx->fParams.frameContentSize,\n                    corruption_detected, \"\");\n                if (dctx->fParams.checksumFlag) {  /* another round for frame checksum */\n                    dctx->expected = 4;\n                    dctx->stage = ZSTDds_checkChecksum;\n                } else {\n                    dctx->expected = 0;   /* ends here */\n                    dctx->stage = ZSTDds_getFrameHeaderSize;\n                }\n            } else {\n                dctx->stage = ZSTDds_decodeBlockHeader;\n                dctx->expected = ZSTD_blockHeaderSize;\n            }\n            return rSize;\n        }\n\n    case ZSTDds_checkChecksum:\n        assert(srcSize == 4);  /* guaranteed by dctx->expected */\n        {   U32 const h32 = (U32)XXH64_digest(&dctx->xxhState);\n            U32 const check32 = MEM_readLE32(src);\n            DEBUGLOG(4, \"ZSTD_decompressContinue: checksum : calculated %08X :: %08X read\", (unsigned)h32, (unsigned)check32);\n            RETURN_ERROR_IF(check32 != h32, checksum_wrong, \"\");\n            dctx->expected = 0;\n            dctx->stage = ZSTDds_getFrameHeaderSize;\n            return 0;\n        }\n\n    case ZSTDds_decodeSkippableHeader:\n        assert(src != NULL);\n        assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE);\n        memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize);   /* complete skippable header */\n        dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE);   /* note : dctx->expected can grow seriously large, beyond local buffer size */\n        dctx->stage = ZSTDds_skipFrame;\n        return 0;\n\n    case ZSTDds_skipFrame:\n        dctx->expected = 0;\n        dctx->stage = ZSTDds_getFrameHeaderSize;\n        return 0;\n\n    default:\n        assert(0);   /* impossible */\n        RETURN_ERROR(GENERIC, \"impossible to reach\");   /* some compiler require default to do something */\n    }\n}\n\n\nstatic size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)\n{\n    dctx->dictEnd = dctx->previousDstEnd;\n    dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));\n    dctx->prefixStart = dict;\n    dctx->previousDstEnd = (const char*)dict + dictSize;\n#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION\n    dctx->dictContentBeginForFuzzing = dctx->prefixStart;\n    dctx->dictContentEndForFuzzing = dctx->previousDstEnd;\n#endif\n    return 0;\n}\n\n/*! ZSTD_loadDEntropy() :\n *  dict : must point at beginning of a valid zstd dictionary.\n * @return : size of entropy tables read */\nsize_t\nZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,\n                  const void* const dict, size_t const dictSize)\n{\n    const BYTE* dictPtr = (const BYTE*)dict;\n    const BYTE* const dictEnd = dictPtr + dictSize;\n\n    RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, \"dict is too small\");\n    assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY);   /* dict must be valid */\n    dictPtr += 8;   /* skip header = magic + dictID */\n\n    ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable));\n    ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable));\n    ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE);\n    {   void* const workspace = &entropy->LLTable;   /* use fse tables as temporary workspace; implies fse tables are grouped together */\n        size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable);\n#ifdef HUF_FORCE_DECOMPRESS_X1\n        /* in minimal huffman, we always use X1 variants */\n        size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable,\n                                                dictPtr, dictEnd - dictPtr,\n                                                workspace, workspaceSize);\n#else\n        size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable,\n                                                dictPtr, dictEnd - dictPtr,\n                                                workspace, workspaceSize);\n#endif\n        RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, \"\");\n        dictPtr += hSize;\n    }\n\n    {   short offcodeNCount[MaxOff+1];\n        unsigned offcodeMaxValue = MaxOff, offcodeLog;\n        size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);\n        RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, \"\");\n        RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, \"\");\n        RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, \"\");\n        ZSTD_buildFSETable( entropy->OFTable,\n                            offcodeNCount, offcodeMaxValue,\n                            OF_base, OF_bits,\n                            offcodeLog);\n        dictPtr += offcodeHeaderSize;\n    }\n\n    {   short matchlengthNCount[MaxML+1];\n        unsigned matchlengthMaxValue = MaxML, matchlengthLog;\n        size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);\n        RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, \"\");\n        RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, \"\");\n        RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, \"\");\n        ZSTD_buildFSETable( entropy->MLTable,\n                            matchlengthNCount, matchlengthMaxValue,\n                            ML_base, ML_bits,\n                            matchlengthLog);\n        dictPtr += matchlengthHeaderSize;\n    }\n\n    {   short litlengthNCount[MaxLL+1];\n        unsigned litlengthMaxValue = MaxLL, litlengthLog;\n        size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);\n        RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, \"\");\n        RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, \"\");\n        RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, \"\");\n        ZSTD_buildFSETable( entropy->LLTable,\n                            litlengthNCount, litlengthMaxValue,\n                            LL_base, LL_bits,\n                            litlengthLog);\n        dictPtr += litlengthHeaderSize;\n    }\n\n    RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, \"\");\n    {   int i;\n        size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12));\n        for (i=0; i<3; i++) {\n            U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4;\n            RETURN_ERROR_IF(rep==0 || rep > dictContentSize,\n                            dictionary_corrupted, \"\");\n            entropy->rep[i] = rep;\n    }   }\n\n    return dictPtr - (const BYTE*)dict;\n}\n\nstatic size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)\n{\n    if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize);\n    {   U32 const magic = MEM_readLE32(dict);\n        if (magic != ZSTD_MAGIC_DICTIONARY) {\n            return ZSTD_refDictContent(dctx, dict, dictSize);   /* pure content mode */\n    }   }\n    dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);\n\n    /* load entropy tables */\n    {   size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize);\n        RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, \"\");\n        dict = (const char*)dict + eSize;\n        dictSize -= eSize;\n    }\n    dctx->litEntropy = dctx->fseEntropy = 1;\n\n    /* reference dictionary content */\n    return ZSTD_refDictContent(dctx, dict, dictSize);\n}\n\nsize_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)\n{\n    assert(dctx != NULL);\n    dctx->expected = ZSTD_startingInputLength(dctx->format);  /* dctx->format must be properly set */\n    dctx->stage = ZSTDds_getFrameHeaderSize;\n    dctx->decodedSize = 0;\n    dctx->previousDstEnd = NULL;\n    dctx->prefixStart = NULL;\n    dctx->virtualStart = NULL;\n    dctx->dictEnd = NULL;\n    dctx->entropy.hufTable[0] = (HUF_DTable)((HufLog)*0x1000001);  /* cover both little and big endian */\n    dctx->litEntropy = dctx->fseEntropy = 0;\n    dctx->dictID = 0;\n    dctx->bType = bt_reserved;\n    ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue));\n    memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue));  /* initial repcodes */\n    dctx->LLTptr = dctx->entropy.LLTable;\n    dctx->MLTptr = dctx->entropy.MLTable;\n    dctx->OFTptr = dctx->entropy.OFTable;\n    dctx->HUFptr = dctx->entropy.hufTable;\n    return 0;\n}\n\nsize_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)\n{\n    FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , \"\");\n    if (dict && dictSize)\n        RETURN_ERROR_IF(\n            ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),\n            dictionary_corrupted, \"\");\n    return 0;\n}\n\n\n/* ======   ZSTD_DDict   ====== */\n\nsize_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)\n{\n    DEBUGLOG(4, \"ZSTD_decompressBegin_usingDDict\");\n    assert(dctx != NULL);\n    if (ddict) {\n        const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);\n        size_t const dictSize = ZSTD_DDict_dictSize(ddict);\n        const void* const dictEnd = dictStart + dictSize;\n        dctx->ddictIsCold = (dctx->dictEnd != dictEnd);\n        DEBUGLOG(4, \"DDict is %s\",\n                    dctx->ddictIsCold ? \"~cold~\" : \"hot!\");\n    }\n    FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , \"\");\n    if (ddict) {   /* NULL ddict is equivalent to no dictionary */\n        ZSTD_copyDDictParameters(dctx, ddict);\n    }\n    return 0;\n}\n\n/*! ZSTD_getDictID_fromDict() :\n *  Provides the dictID stored within dictionary.\n *  if @return == 0, the dictionary is not conformant with Zstandard specification.\n *  It can still be loaded, but as a content-only dictionary. */\nunsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize)\n{\n    if (dictSize < 8) return 0;\n    if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0;\n    return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE);\n}\n\n/*! ZSTD_getDictID_fromFrame() :\n *  Provides the dictID required to decompress frame stored within `src`.\n *  If @return == 0, the dictID could not be decoded.\n *  This could for one of the following reasons :\n *  - The frame does not require a dictionary (most common case).\n *  - The frame was built with dictID intentionally removed.\n *    Needed dictionary is a hidden information.\n *    Note : this use case also happens when using a non-conformant dictionary.\n *  - `srcSize` is too small, and as a result, frame header could not be decoded.\n *    Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`.\n *  - This is not a Zstandard frame.\n *  When identifying the exact failure cause, it's possible to use\n *  ZSTD_getFrameHeader(), which will provide a more precise error code. */\nunsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize)\n{\n    ZSTD_frameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0 };\n    size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize);\n    if (ZSTD_isError(hError)) return 0;\n    return zfp.dictID;\n}\n\n\n/*! ZSTD_decompress_usingDDict() :\n*   Decompression using a pre-digested Dictionary\n*   Use dictionary without significant overhead. */\nsize_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,\n                                  void* dst, size_t dstCapacity,\n                            const void* src, size_t srcSize,\n                            const ZSTD_DDict* ddict)\n{\n    /* pass content and size in case legacy frames are encountered */\n    return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,\n                                     NULL, 0,\n                                     ddict);\n}\n\n\n/*=====================================\n*   Streaming decompression\n*====================================*/\n\nZSTD_DStream* ZSTD_createDStream(void)\n{\n    DEBUGLOG(3, \"ZSTD_createDStream\");\n    return ZSTD_createDStream_advanced(ZSTD_defaultCMem);\n}\n\nZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize)\n{\n    return ZSTD_initStaticDCtx(workspace, workspaceSize);\n}\n\nZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem)\n{\n    return ZSTD_createDCtx_advanced(customMem);\n}\n\nsize_t ZSTD_freeDStream(ZSTD_DStream* zds)\n{\n    return ZSTD_freeDCtx(zds);\n}\n\n\n/* ***  Initialization  *** */\n\nsize_t ZSTD_DStreamInSize(void)  { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; }\nsize_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }\n\nsize_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,\n                                   const void* dict, size_t dictSize,\n                                         ZSTD_dictLoadMethod_e dictLoadMethod,\n                                         ZSTD_dictContentType_e dictContentType)\n{\n    RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, \"\");\n    ZSTD_clearDict(dctx);\n    if (dict && dictSize != 0) {\n        dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);\n        RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, \"NULL pointer!\");\n        dctx->ddict = dctx->ddictLocal;\n        dctx->dictUses = ZSTD_use_indefinitely;\n    }\n    return 0;\n}\n\nsize_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)\n{\n    return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto);\n}\n\nsize_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize)\n{\n    return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto);\n}\n\nsize_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType)\n{\n    FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), \"\");\n    dctx->dictUses = ZSTD_use_once;\n    return 0;\n}\n\nsize_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize)\n{\n    return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent);\n}\n\n\n/* ZSTD_initDStream_usingDict() :\n * return : expected size, aka ZSTD_startingInputLength().\n * this function cannot fail */\nsize_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize)\n{\n    DEBUGLOG(4, \"ZSTD_initDStream_usingDict\");\n    FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , \"\");\n    FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , \"\");\n    return ZSTD_startingInputLength(zds->format);\n}\n\n/* note : this variant can't fail */\nsize_t ZSTD_initDStream(ZSTD_DStream* zds)\n{\n    DEBUGLOG(4, \"ZSTD_initDStream\");\n    return ZSTD_initDStream_usingDDict(zds, NULL);\n}\n\n/* ZSTD_initDStream_usingDDict() :\n * ddict will just be referenced, and must outlive decompression session\n * this function cannot fail */\nsize_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict)\n{\n    FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , \"\");\n    FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , \"\");\n    return ZSTD_startingInputLength(dctx->format);\n}\n\n/* ZSTD_resetDStream() :\n * return : expected size, aka ZSTD_startingInputLength().\n * this function cannot fail */\nsize_t ZSTD_resetDStream(ZSTD_DStream* dctx)\n{\n    FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), \"\");\n    return ZSTD_startingInputLength(dctx->format);\n}\n\n\nsize_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)\n{\n    RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, \"\");\n    ZSTD_clearDict(dctx);\n    if (ddict) {\n        dctx->ddict = ddict;\n        dctx->dictUses = ZSTD_use_indefinitely;\n    }\n    return 0;\n}\n\n/* ZSTD_DCtx_setMaxWindowSize() :\n * note : no direct equivalence in ZSTD_DCtx_setParameter,\n * since this version sets windowSize, and the other sets windowLog */\nsize_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize)\n{\n    ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax);\n    size_t const min = (size_t)1 << bounds.lowerBound;\n    size_t const max = (size_t)1 << bounds.upperBound;\n    RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, \"\");\n    RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, \"\");\n    RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, \"\");\n    dctx->maxWindowSize = maxWindowSize;\n    return 0;\n}\n\nsize_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format)\n{\n    return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, format);\n}\n\nZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam)\n{\n    ZSTD_bounds bounds = { 0, 0, 0 };\n    switch(dParam) {\n        case ZSTD_d_windowLogMax:\n            bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN;\n            bounds.upperBound = ZSTD_WINDOWLOG_MAX;\n            return bounds;\n        case ZSTD_d_format:\n            bounds.lowerBound = (int)ZSTD_f_zstd1;\n            bounds.upperBound = (int)ZSTD_f_zstd1_magicless;\n            ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless);\n            return bounds;\n        case ZSTD_d_stableOutBuffer:\n            bounds.lowerBound = (int)ZSTD_obm_buffered;\n            bounds.upperBound = (int)ZSTD_obm_stable;\n            return bounds;\n        default:;\n    }\n    bounds.error = ERROR(parameter_unsupported);\n    return bounds;\n}\n\n/* ZSTD_dParam_withinBounds:\n * @return 1 if value is within dParam bounds,\n * 0 otherwise */\nstatic int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value)\n{\n    ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam);\n    if (ZSTD_isError(bounds.error)) return 0;\n    if (value < bounds.lowerBound) return 0;\n    if (value > bounds.upperBound) return 0;\n    return 1;\n}\n\n#define CHECK_DBOUNDS(p,v) {                \\\n    RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, \"\"); \\\n}\n\nsize_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value)\n{\n    RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, \"\");\n    switch(dParam) {\n        case ZSTD_d_windowLogMax:\n            if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT;\n            CHECK_DBOUNDS(ZSTD_d_windowLogMax, value);\n            dctx->maxWindowSize = ((size_t)1) << value;\n            return 0;\n        case ZSTD_d_format:\n            CHECK_DBOUNDS(ZSTD_d_format, value);\n            dctx->format = (ZSTD_format_e)value;\n            return 0;\n        case ZSTD_d_stableOutBuffer:\n            CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value);\n            dctx->outBufferMode = (ZSTD_outBufferMode_e)value;\n            return 0;\n        default:;\n    }\n    RETURN_ERROR(parameter_unsupported, \"\");\n}\n\nsize_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)\n{\n    if ( (reset == ZSTD_reset_session_only)\n      || (reset == ZSTD_reset_session_and_parameters) ) {\n        dctx->streamStage = zdss_init;\n        dctx->noForwardProgress = 0;\n    }\n    if ( (reset == ZSTD_reset_parameters)\n      || (reset == ZSTD_reset_session_and_parameters) ) {\n        RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, \"\");\n        ZSTD_clearDict(dctx);\n        dctx->format = ZSTD_f_zstd1;\n        dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT;\n    }\n    return 0;\n}\n\n\nsize_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx)\n{\n    return ZSTD_sizeof_DCtx(dctx);\n}\n\nsize_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize)\n{\n    size_t const blockSize = (size_t) MIN(windowSize, ZSTD_BLOCKSIZE_MAX);\n    unsigned long long const neededRBSize = windowSize + blockSize + (WILDCOPY_OVERLENGTH * 2);\n    unsigned long long const neededSize = MIN(frameContentSize, neededRBSize);\n    size_t const minRBSize = (size_t) neededSize;\n    RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize,\n                    frameParameter_windowTooLarge, \"\");\n    return minRBSize;\n}\n\nsize_t ZSTD_estimateDStreamSize(size_t windowSize)\n{\n    size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX);\n    size_t const inBuffSize = blockSize;  /* no block can be larger */\n    size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN);\n    return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize;\n}\n\nsize_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize)\n{\n    U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX;   /* note : should be user-selectable, but requires an additional parameter (or a dctx) */\n    ZSTD_frameHeader zfh;\n    size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize);\n    if (ZSTD_isError(err)) return err;\n    RETURN_ERROR_IF(err>0, srcSize_wrong, \"\");\n    RETURN_ERROR_IF(zfh.windowSize > windowSizeMax,\n                    frameParameter_windowTooLarge, \"\");\n    return ZSTD_estimateDStreamSize((size_t)zfh.windowSize);\n}\n\n\n/* *****   Decompression   ***** */\n\nstatic int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)\n{\n    return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR;\n}\n\nstatic void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize)\n{\n    if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize))\n        zds->oversizedDuration++;\n    else \n        zds->oversizedDuration = 0;\n}\n\nstatic int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds)\n{\n    return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION;\n}\n\n/* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */\nstatic size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output)\n{\n    ZSTD_outBuffer const expect = zds->expectedOutBuffer;\n    /* No requirement when ZSTD_obm_stable is not enabled. */\n    if (zds->outBufferMode != ZSTD_obm_stable)\n        return 0;\n    /* Any buffer is allowed in zdss_init, this must be the same for every other call until\n     * the context is reset.\n     */\n    if (zds->streamStage == zdss_init)\n        return 0;\n    /* The buffer must match our expectation exactly. */\n    if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size)\n        return 0;\n    RETURN_ERROR(dstBuffer_wrong, \"ZSTD_obm_stable enabled but output differs!\");\n}\n\n/* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream()\n * and updates the stage and the output buffer state. This call is extracted so it can be\n * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode.\n * NOTE: You must break after calling this function since the streamStage is modified.\n */\nstatic size_t ZSTD_decompressContinueStream(\n            ZSTD_DStream* zds, char** op, char* oend,\n            void const* src, size_t srcSize) {\n    int const isSkipFrame = ZSTD_isSkipFrame(zds);\n    if (zds->outBufferMode == ZSTD_obm_buffered) {\n        size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart;\n        size_t const decodedSize = ZSTD_decompressContinue(zds,\n                zds->outBuff + zds->outStart, dstSize, src, srcSize);\n        FORWARD_IF_ERROR(decodedSize, \"\");\n        if (!decodedSize && !isSkipFrame) {\n            zds->streamStage = zdss_read;\n        } else {\n            zds->outEnd = zds->outStart + decodedSize;\n            zds->streamStage = zdss_flush;\n        }\n    } else {\n        /* Write directly into the output buffer */\n        size_t const dstSize = isSkipFrame ? 0 : oend - *op;\n        size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);\n        FORWARD_IF_ERROR(decodedSize, \"\");\n        *op += decodedSize;\n        /* Flushing is not needed. */\n        zds->streamStage = zdss_read;\n        assert(*op <= oend);\n        assert(zds->outBufferMode == ZSTD_obm_stable);\n    }\n    return 0;\n}\n\nsize_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)\n{\n    const char* const src = (const char*)input->src;\n    const char* const istart = input->pos != 0 ? src + input->pos : src;\n    const char* const iend = input->size != 0 ? src + input->size : src;\n    const char* ip = istart;\n    char* const dst = (char*)output->dst;\n    char* const ostart = output->pos != 0 ? dst + output->pos : dst;\n    char* const oend = output->size != 0 ? dst + output->size : dst;\n    char* op = ostart;\n    U32 someMoreWork = 1;\n\n    DEBUGLOG(5, \"ZSTD_decompressStream\");\n    RETURN_ERROR_IF(\n        input->pos > input->size,\n        srcSize_wrong,\n        \"forbidden. in: pos: %u   vs size: %u\",\n        (U32)input->pos, (U32)input->size);\n    RETURN_ERROR_IF(\n        output->pos > output->size,\n        dstSize_tooSmall,\n        \"forbidden. out: pos: %u   vs size: %u\",\n        (U32)output->pos, (U32)output->size);\n    DEBUGLOG(5, \"input size : %u\", (U32)(input->size - input->pos));\n    FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), \"\");\n\n    while (someMoreWork) {\n        switch(zds->streamStage)\n        {\n        case zdss_init :\n            DEBUGLOG(5, \"stage zdss_init => transparent reset \");\n            zds->streamStage = zdss_loadHeader;\n            zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0;\n            zds->legacyVersion = 0;\n            zds->hostageByte = 0;\n            zds->expectedOutBuffer = *output;\n            /* fall-through */\n\n        case zdss_loadHeader :\n            DEBUGLOG(5, \"stage zdss_loadHeader (srcSize : %u)\", (U32)(iend - ip));\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)\n            if (zds->legacyVersion) {\n                RETURN_ERROR_IF(zds->staticSize, memory_allocation,\n                    \"legacy support is incompatible with static dctx\");\n                {   size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input);\n                    if (hint==0) zds->streamStage = zdss_init;\n                    return hint;\n            }   }\n#endif\n            {   size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);\n                DEBUGLOG(5, \"header size : %u\", (U32)hSize);\n                if (ZSTD_isError(hSize)) {\n#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)\n                    U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);\n                    if (legacyVersion) {\n                        ZSTD_DDict const* const ddict = ZSTD_getDDict(zds);\n                        const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL;\n                        size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0;\n                        DEBUGLOG(5, \"ZSTD_decompressStream: detected legacy version v0.%u\", legacyVersion);\n                        RETURN_ERROR_IF(zds->staticSize, memory_allocation,\n                            \"legacy support is incompatible with static dctx\");\n                        FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext,\n                                    zds->previousLegacyVersion, legacyVersion,\n                                    dict, dictSize), \"\");\n                        zds->legacyVersion = zds->previousLegacyVersion = legacyVersion;\n                        {   size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input);\n                            if (hint==0) zds->streamStage = zdss_init;   /* or stay in stage zdss_loadHeader */\n                            return hint;\n                    }   }\n#endif\n                    return hSize;   /* error */\n                }\n                if (hSize != 0) {   /* need more input */\n                    size_t const toLoad = hSize - zds->lhSize;   /* if hSize!=0, hSize > zds->lhSize */\n                    size_t const remainingInput = (size_t)(iend-ip);\n                    assert(iend >= ip);\n                    if (toLoad > remainingInput) {   /* not enough input to load full header */\n                        if (remainingInput > 0) {\n                            memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput);\n                            zds->lhSize += remainingInput;\n                        }\n                        input->pos = input->size;\n                        return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize;   /* remaining header bytes + next block header */\n                    }\n                    assert(ip != NULL);\n                    memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad;\n                    break;\n            }   }\n\n            /* check for single-pass mode opportunity */\n            if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN\n                && zds->fParams.frameType != ZSTD_skippableFrame\n                && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {\n                size_t const cSize = ZSTD_findFrameCompressedSize(istart, iend-istart);\n                if (cSize <= (size_t)(iend-istart)) {\n                    /* shortcut : using single-pass mode */\n                    size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, oend-op, istart, cSize, ZSTD_getDDict(zds));\n                    if (ZSTD_isError(decompressedSize)) return decompressedSize;\n                    DEBUGLOG(4, \"shortcut to single-pass ZSTD_decompress_usingDDict()\")\n                    ip = istart + cSize;\n                    op += decompressedSize;\n                    zds->expected = 0;\n                    zds->streamStage = zdss_init;\n                    someMoreWork = 0;\n                    break;\n            }   }\n\n            /* Check output buffer is large enough for ZSTD_odm_stable. */\n            if (zds->outBufferMode == ZSTD_obm_stable\n                && zds->fParams.frameType != ZSTD_skippableFrame\n                && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN\n                && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) {\n                RETURN_ERROR(dstSize_tooSmall, \"ZSTD_obm_stable passed but ZSTD_outBuffer is too small\");\n            }\n\n            /* Consume header (see ZSTDds_decodeFrameHeader) */\n            DEBUGLOG(4, \"Consume header\");\n            FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), \"\");\n\n            if ((MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) {  /* skippable frame */\n                zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE);\n                zds->stage = ZSTDds_skipFrame;\n            } else {\n                FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), \"\");\n                zds->expected = ZSTD_blockHeaderSize;\n                zds->stage = ZSTDds_decodeBlockHeader;\n            }\n\n            /* control buffer memory usage */\n            DEBUGLOG(4, \"Control max memory usage (%u KB <= max %u KB)\",\n                        (U32)(zds->fParams.windowSize >>10),\n                        (U32)(zds->maxWindowSize >> 10) );\n            zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN);\n            RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize,\n                            frameParameter_windowTooLarge, \"\");\n\n            /* Adapt buffer sizes to frame header instructions */\n            {   size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */);\n                size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_obm_buffered\n                        ? ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize)\n                        : 0;\n\n                ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize);\n\n                {   int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize);\n                    int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds);\n                    \n                    if (tooSmall || tooLarge) {\n                        size_t const bufferSize = neededInBuffSize + neededOutBuffSize;\n                        DEBUGLOG(4, \"inBuff  : from %u to %u\",\n                                    (U32)zds->inBuffSize, (U32)neededInBuffSize);\n                        DEBUGLOG(4, \"outBuff : from %u to %u\",\n                                    (U32)zds->outBuffSize, (U32)neededOutBuffSize);\n                        if (zds->staticSize) {  /* static DCtx */\n                            DEBUGLOG(4, \"staticSize : %u\", (U32)zds->staticSize);\n                            assert(zds->staticSize >= sizeof(ZSTD_DCtx));  /* controlled at init */\n                            RETURN_ERROR_IF(\n                                bufferSize > zds->staticSize - sizeof(ZSTD_DCtx),\n                                memory_allocation, \"\");\n                        } else {\n                            ZSTD_free(zds->inBuff, zds->customMem);\n                            zds->inBuffSize = 0;\n                            zds->outBuffSize = 0;\n                            zds->inBuff = (char*)ZSTD_malloc(bufferSize, zds->customMem);\n                            RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, \"\");\n                        }\n                        zds->inBuffSize = neededInBuffSize;\n                        zds->outBuff = zds->inBuff + zds->inBuffSize;\n                        zds->outBuffSize = neededOutBuffSize;\n            }   }   }\n            zds->streamStage = zdss_read;\n            /* fall-through */\n\n        case zdss_read:\n            DEBUGLOG(5, \"stage zdss_read\");\n            {   size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, iend - ip);\n                DEBUGLOG(5, \"neededInSize = %u\", (U32)neededInSize);\n                if (neededInSize==0) {  /* end of frame */\n                    zds->streamStage = zdss_init;\n                    someMoreWork = 0;\n                    break;\n                }\n                if ((size_t)(iend-ip) >= neededInSize) {  /* decode directly from src */\n                    FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), \"\");\n                    ip += neededInSize;\n                    /* Function modifies the stage so we must break */\n                    break;\n            }   }\n            if (ip==iend) { someMoreWork = 0; break; }   /* no more input */\n            zds->streamStage = zdss_load;\n            /* fall-through */\n\n        case zdss_load:\n            {   size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds);\n                size_t const toLoad = neededInSize - zds->inPos;\n                int const isSkipFrame = ZSTD_isSkipFrame(zds);\n                size_t loadedSize;\n                /* At this point we shouldn't be decompressing a block that we can stream. */\n                assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, iend - ip));\n                if (isSkipFrame) {\n                    loadedSize = MIN(toLoad, (size_t)(iend-ip));\n                } else {\n                    RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos,\n                                    corruption_detected,\n                                    \"should never happen\");\n                    loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, iend-ip);\n                }\n                ip += loadedSize;\n                zds->inPos += loadedSize;\n                if (loadedSize < toLoad) { someMoreWork = 0; break; }   /* not enough input, wait for more */\n\n                /* decode loaded input */\n                zds->inPos = 0;   /* input is consumed */\n                FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), \"\");\n                /* Function modifies the stage so we must break */\n                break;\n            }\n        case zdss_flush:\n            {   size_t const toFlushSize = zds->outEnd - zds->outStart;\n                size_t const flushedSize = ZSTD_limitCopy(op, oend-op, zds->outBuff + zds->outStart, toFlushSize);\n                op += flushedSize;\n                zds->outStart += flushedSize;\n                if (flushedSize == toFlushSize) {  /* flush completed */\n                    zds->streamStage = zdss_read;\n                    if ( (zds->outBuffSize < zds->fParams.frameContentSize)\n                      && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) {\n                        DEBUGLOG(5, \"restart filling outBuff from beginning (left:%i, needed:%u)\",\n                                (int)(zds->outBuffSize - zds->outStart),\n                                (U32)zds->fParams.blockSizeMax);\n                        zds->outStart = zds->outEnd = 0;\n                    }\n                    break;\n            }   }\n            /* cannot complete flush */\n            someMoreWork = 0;\n            break;\n\n        default:\n            assert(0);    /* impossible */\n            RETURN_ERROR(GENERIC, \"impossible to reach\");   /* some compiler require default to do something */\n    }   }\n\n    /* result */\n    input->pos = (size_t)(ip - (const char*)(input->src));\n    output->pos = (size_t)(op - (char*)(output->dst));\n\n    /* Update the expected output buffer for ZSTD_obm_stable. */\n    zds->expectedOutBuffer = *output;\n\n    if ((ip==istart) && (op==ostart)) {  /* no forward progress */\n        zds->noForwardProgress ++;\n        if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) {\n            RETURN_ERROR_IF(op==oend, dstSize_tooSmall, \"\");\n            RETURN_ERROR_IF(ip==iend, srcSize_wrong, \"\");\n            assert(0);\n        }\n    } else {\n        zds->noForwardProgress = 0;\n    }\n    {   size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds);\n        if (!nextSrcSizeHint) {   /* frame fully decoded */\n            if (zds->outEnd == zds->outStart) {  /* output fully flushed */\n                if (zds->hostageByte) {\n                    if (input->pos >= input->size) {\n                        /* can't release hostage (not present) */\n                        zds->streamStage = zdss_read;\n                        return 1;\n                    }\n                    input->pos++;  /* release hostage */\n                }   /* zds->hostageByte */\n                return 0;\n            }  /* zds->outEnd == zds->outStart */\n            if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */\n                input->pos--;   /* note : pos > 0, otherwise, impossible to finish reading last block */\n                zds->hostageByte=1;\n            }\n            return 1;\n        }  /* nextSrcSizeHint==0 */\n        nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block);   /* preload header of next block */\n        assert(zds->inPos <= nextSrcSizeHint);\n        nextSrcSizeHint -= zds->inPos;   /* part already loaded*/\n        return nextSrcSizeHint;\n    }\n}\n\nsize_t ZSTD_decompressStream_simpleArgs (\n                            ZSTD_DCtx* dctx,\n                            void* dst, size_t dstCapacity, size_t* dstPos,\n                      const void* src, size_t srcSize, size_t* srcPos)\n{\n    ZSTD_outBuffer output = { dst, dstCapacity, *dstPos };\n    ZSTD_inBuffer  input  = { src, srcSize, *srcPos };\n    /* ZSTD_compress_generic() will check validity of dstPos and srcPos */\n    size_t const cErr = ZSTD_decompressStream(dctx, &output, &input);\n    *dstPos = output.pos;\n    *srcPos = input.pos;\n    return cErr;\n}\n/**** ended inlining decompress/zstd_decompress.c ****/\n/**** start inlining decompress/zstd_decompress_block.c ****/\n/*\n * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.\n * All rights reserved.\n *\n * This source code is licensed under both the BSD-style license (found in the\n * LICENSE file in the root directory of this source tree) and the GPLv2 (found\n * in the COPYING file in the root directory of this source tree).\n * You may select, at your option, one of the above-listed licenses.\n */\n\n/* zstd_decompress_block :\n * this module takes care of decompressing _compressed_ block */\n\n/*-*******************************************************\n*  Dependencies\n*********************************************************/\n/**** skipping file: ../common/compiler.h ****/\n/**** skipping file: ../common/cpu.h ****/\n/**** skipping file: ../common/mem.h ****/\n#define FSE_STATIC_LINKING_ONLY\n/**** skipping file: ../common/fse.h ****/\n#define HUF_STATIC_LINKING_ONLY\n/**** skipping file: ../common/huf.h ****/\n/**** skipping file: ../common/zstd_internal.h ****/\n/**** skipping file: zstd_decompress_internal.h ****/\n/**** skipping file: zstd_ddict.h ****/\n/**** skipping file: zstd_decompress_block.h ****/\n\n/*_*******************************************************\n*  Macros\n**********************************************************/\n\n/* These two optional macros force the use one way or another of the two\n * ZSTD_decompressSequences implementations. You can't force in both directions\n * at the same time.\n */\n#if defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \\\n    defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)\n#error \"Cannot force the use of the short and the long ZSTD_decompressSequences variants!\"\n#endif\n\n\n/*_*******************************************************\n*  Memory operations\n**********************************************************/\nstatic void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }\n\n\n/*-*************************************************************\n *   Block decoding\n ***************************************************************/\n\n/*! ZSTD_getcBlockSize() :\n *  Provides the size of compressed block from block header `src` */\nsize_t ZSTD_getcBlockSize(const void* src, size_t srcSize,\n                          blockProperties_t* bpPtr)\n{\n    RETURN_ERROR_IF(srcSize < ZSTD_blockHeaderSize, srcSize_wrong, \"\");\n\n    {   U32 const cBlockHeader = MEM_readLE24(src);\n        U32 const cSize = cBlockHeader >> 3;\n        bpPtr->lastBlock = cBlockHeader & 1;\n        bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3);\n        bpPtr->origSize = cSize;   /* only useful for RLE */\n        if (bpPtr->blockType == bt_rle) return 1;\n        RETURN_ERROR_IF(bpPtr->blockType == bt_reserved, corruption_detected, \"\");\n        return cSize;\n    }\n}\n\n\n/* Hidden declaration for fullbench */\nsize_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,\n                          const void* src, size_t srcSize);\n/*! ZSTD_decodeLiteralsBlock() :\n * @return : nb of bytes read from src (< srcSize )\n *  note : symbol not declared but exposed for fullbench */\nsize_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,\n                          const void* src, size_t srcSize)   /* note : srcSize < BLOCKSIZE */\n{\n    DEBUGLOG(5, \"ZSTD_decodeLiteralsBlock\");\n    RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected, \"\");\n\n    {   const BYTE* const istart = (const BYTE*) src;\n        symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);\n\n        switch(litEncType)\n        {\n        case set_repeat:\n            DEBUGLOG(5, \"set_repeat flag : re-using stats from previous compressed literals block\");\n            RETURN_ERROR_IF(dctx->litEntropy==0, dictionary_corrupted, \"\");\n            /* fall-through */\n\n        case set_compressed:\n            RETURN_ERROR_IF(srcSize < 5, corruption_detected, \"srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3\");\n            {   size_t lhSize, litSize, litCSize;\n                U32 singleStream=0;\n                U32 const lhlCode = (istart[0] >> 2) & 3;\n                U32 const lhc = MEM_readLE32(istart);\n                size_t hufSuccess;\n                switch(lhlCode)\n                {\n                case 0: case 1: default:   /* note : default is impossible, since lhlCode into [0..3] */\n                    /* 2 - 2 - 10 - 10 */\n                    singleStream = !lhlCode;\n                    lhSize = 3;\n                    litSize  = (lhc >> 4) & 0x3FF;\n                    litCSize = (lhc >> 14) & 0x3FF;\n                    break;\n                case 2:\n                    /* 2 - 2 - 14 - 14 */\n                    lhSize = 4;\n                    litSize  = (lhc >> 4) & 0x3FFF;\n                    litCSize = lhc >> 18;\n                    break;\n                case 3:\n                    /* 2 - 2 - 18 - 18 */\n                    lhSize = 5;\n                    litSize  = (lhc >> 4) & 0x3FFFF;\n                    litCSize = (lhc >> 22) + ((size_t)istart[4] << 10);\n                    break;\n                }\n                RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, \"\");\n                RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected, \"\");\n\n                /* prefetch huffman table if cold */\n                if (dctx->ddictIsCold && (litSize > 768 /* heuristic */)) {\n                    PREFETCH_AREA(dctx->HUFptr, sizeof(dctx->entropy.hufTable));\n                }\n\n                if (litEncType==set_repeat) {\n                    if (singleStream) {\n                        hufSuccess = HUF_decompress1X_usingDTable_bmi2(\n                            dctx->litBuffer, litSize, istart+lhSize, litCSize,\n                            dctx->HUFptr, dctx->bmi2);\n                    } else {\n                        hufSuccess = HUF_decompress4X_usingDTable_bmi2(\n                            dctx->litBuffer, litSize, istart+lhSize, litCSize,\n                            dctx->HUFptr, dctx->bmi2);\n                    }\n                } else {\n                    if (singleStream) {\n#if defined(HUF_FORCE_DECOMPRESS_X2)\n                        hufSuccess = HUF_decompress1X_DCtx_wksp(\n                            dctx->entropy.hufTable, dctx->litBuffer, litSize,\n                            istart+lhSize, litCSize, dctx->workspace,\n                            sizeof(dctx->workspace));\n#else\n                        hufSuccess = HUF_decompress1X1_DCtx_wksp_bmi2(\n                            dctx->entropy.hufTable, dctx->litBuffer, litSize,\n                            istart+lhSize, litCSize, dctx->workspace,\n                            sizeof(dctx->workspace), dctx->bmi2);\n#endif\n                    } else {\n                        hufSuccess = HUF_decompress4X_hufOnly_wksp_bmi2(\n                            dctx->entropy.hufTable, dctx->litBuffer, litSize,\n                            istart+lhSize, litCSize, dctx->workspace,\n                            sizeof(dctx->workspace), dctx->bmi2);\n                    }\n                }\n\n                RETURN_ERROR_IF(HUF_isError(hufSuccess), corruption_detected, \"\");\n\n                dctx->litPtr = dctx->litBuffer;\n                dctx->litSize = litSize;\n                dctx->litEntropy = 1;\n                if (litEncType==set_compressed) dctx->HUFptr = dctx->entropy.hufTable;\n                memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);\n                return litCSize + lhSize;\n            }\n\n        case set_basic:\n            {   size_t litSize, lhSize;\n                U32 const lhlCode = ((istart[0]) >> 2) & 3;\n                switch(lhlCode)\n                {\n                case 0: case 2: default:   /* note : default is impossible, since lhlCode into [0..3] */\n                    lhSize = 1;\n                    litSize = istart[0] >> 3;\n                    break;\n                case 1:\n                    lhSize = 2;\n                    litSize = MEM_readLE16(istart) >> 4;\n                    break;\n                case 3:\n                    lhSize = 3;\n                    litSize = MEM_readLE24(istart) >> 4;\n                    break;\n                }\n\n                if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) {  /* risk reading beyond src buffer with wildcopy */\n                    RETURN_ERROR_IF(litSize+lhSize > srcSize, corruption_detected, \"\");\n                    memcpy(dctx->litBuffer, istart+lhSize, litSize);\n                    dctx->litPtr = dctx->litBuffer;\n                    dctx->litSize = litSize;\n                    memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);\n                    return lhSize+litSize;\n                }\n                /* direct reference into compressed stream */\n                dctx->litPtr = istart+lhSize;\n                dctx->litSize = litSize;\n                return lhSize+litSize;\n            }\n\n        case set_rle:\n            {   U32 const lhlCode = ((istart[0]) >> 2) & 3;\n                size_t litSize, lhSize;\n                switch(lhlCode)\n                {\n                case 0: case 2: default:   /* note : default is impossible, since lhlCode into [0..3] */\n                    lhSize = 1;\n                    litSize = istart[0] >> 3;\n                    break;\n                case 1:\n                    lhSize = 2;\n                    litSize = MEM_readLE16(istart) >> 4;\n                    break;\n                case 3:\n                    lhSize = 3;\n                    litSize = MEM_readLE24(istart) >> 4;\n                    RETURN_ERROR_IF(srcSize<4, corruption_detected, \"srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4\");\n                    break;\n                }\n                RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, \"\");\n                memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);\n                dctx->litPtr = dctx->litBuffer;\n                dctx->litSize = litSize;\n                return lhSize+1;\n            }\n        default:\n            RETURN_ERROR(corruption_detected, \"impossible\");\n        }\n    }\n}\n\n/* Default FSE distribution tables.\n * These are pre-calculated FSE decoding tables using default distributions as defined in specification :\n * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#default-distributions\n * They were generated programmatically with following method :\n * - start from default distributions, present in /lib/common/zstd_internal.h\n * - generate tables normally, using ZSTD_buildFSETable()\n * - printout the content of tables\n * - pretify output, report below, test with fuzzer to ensure it's correct */\n\n/* Default FSE distribution table for Literal Lengths */\nstatic const ZSTD_seqSymbol LL_defaultDTable[(1<<LL_DEFAULTNORMLOG)+1] = {\n     {  1,  1,  1, LL_DEFAULTNORMLOG},  /* header : fastMode, tableLog */\n     /* nextState, nbAddBits, nbBits, baseVal */\n     {  0,  0,  4,    0},  { 16,  0,  4,    0},\n     { 32,  0,  5,    1},  {  0,  0,  5,    3},\n     {  0,  0,  5,    4},  {  0,  0,  5,    6},\n     {  0,  0,  5,    7},  {  0,  0,  5,    9},\n     {  0,  0,  5,   10},  {  0,  0,  5,   12},\n     {  0,  0,  6,   14},  {  0,  1,  5,   16},\n     {  0,  1,  5,   20},  {  0,  1,  5,   22},\n     {  0,  2,  5,   28},  {  0,  3,  5,   32},\n     {  0,  4,  5,   48},  { 32,  6,  5,   64},\n     {  0,  7,  5,  128},  {  0,  8,  6,  256},\n     {  0, 10,  6, 1024},  {  0, 12,  6, 4096},\n     { 32,  0,  4,    0},  {  0,  0,  4,    1},\n     {  0,  0,  5,    2},  { 32,  0,  5,    4},\n     {  0,  0,  5,    5},  { 32,  0,  5,    7},\n     {  0,  0,  5,    8},  { 32,  0,  5,   10},\n     {  0,  0,  5,   11},  {  0,  0,  6,   13},\n     { 32,  1,  5,   16},  {  0,  1,  5,   18},\n     { 32,  1,  5,   22},  {  0,  2,  5,   24},\n     { 32,  3,  5,   32},  {  0,  3,  5,   40},\n     {  0,  6,  4,   64},  { 16,  6,  4,   64},\n     { 32,  7,  5,  128},  {  0,  9,  6,  512},\n     {  0, 11,  6, 2048},  { 48,  0,  4,    0},\n     { 16,  0,  4,    1},  { 32,  0,  5,    2},\n     { 32,  0,  5,    3},  { 32,  0,  5,    5},\n     { 32,  0,  5,    6},  { 32,  0,  5,    8},\n     { 32,  0,  5,    9},  { 32,  0,  5,   11},\n     { 32,  0,  5,   12},  {  0,  0,  6,   15},\n     { 32,  1,  5,   18},  { 32,  1,  5,   20},\n     { 32,  2,  5,   24},  { 32,  2,  5,   28},\n     { 32,  3,  5,   40},  { 32,  4,  5,   48},\n     {  0, 16,  6,65536},  {  0, 15,  6,32768},\n     {  0, 14,  6,16384},  {  0, 13,  6, 8192},\n};   /* LL_defaultDTable */\n\n/* Default FSE distribution table for Offset Codes */\nstatic const ZSTD_seqSymbol OF_defaultDTable[(1<<OF_DEFAULTNORMLOG)+1] = {\n    {  1,  1,  1, OF_DEFAULTNORMLOG},  /* header : fastMode, tableLog */\n    /* nextState, nbAddBits, nbBits, baseVal */\n    {  0,  0,  5,    0},     {  0,  6,  4,   61},\n    {  0,  9,  5,  509},     {  0, 15,  5,32765},\n    {  0, 21,  5,2097149},   {  0,  3,  5,    5},\n    {  0,  7,  4,  125},     {  0, 12,  5, 4093},\n    {  0, 18,  5,262141},    {  0, 23,  5,8388605},\n    {  0,  5,  5,   29},     {  0,  8,  4,  253},\n    {  0, 14,  5,16381},     {  0, 20,  5,1048573},\n    {  0,  2,  5,    1},     { 16,  7,  4,  125},\n    {  0, 11,  5, 2045},     {  0, 17,  5,131069},\n    {  0, 22,  5,4194301},   {  0,  4,  5,   13},\n    { 16,  8,  4,  253},     {  0, 13,  5, 8189},\n    {  0, 19,  5,524285},    {  0,  1,  5,    1},\n    { 16,  6,  4,   61},     {  0, 10,  5, 1021},\n    {  0, 16,  5,65533},     {  0, 28,  5,268435453},\n    {  0, 27,  5,134217725}, {  0, 26,  5,67108861},\n    {  0, 25,  5,33554429},  {  0, 24,  5,16777213},\n};   /* OF_defaultDTable */\n\n\n/* Default FSE distribution table for Match Lengths */\nstatic const ZSTD_seqSymbol ML_defaultDTable[(1<<ML_DEFAULTNORMLOG)+1] = {\n    {  1,  1,  1, ML_DEFAULTNORMLOG},  /* header : fastMode, tableLog */\n    /* nextState, nbAddBits, nbBits, baseVal */\n    {  0,  0,  6,    3},  {  0,  0,  4,    4},\n    { 32,  0,  5,    5},  {  0,  0,  5,    6},\n    {  0,  0,  5,    8},  {  0,  0,  5,    9},\n    {  0,  0,  5,   11},  {  0,  0,  6,   13},\n    {  0,  0,  6,   16},  {  0,  0,  6,   19},\n    {  0,  0,  6,   22},  {  0,  0,  6,   25},\n    {  0,  0,  6,   28},  {  0,  0,  6,   31},\n    {  0,  0,  6,   34},  {  0,  1,  6,   37},\n    {  0,  1,  6,   41},  {  0,  2,  6,   47},\n    {  0,  3,  6,   59},  {  0,  4,  6,   83},\n    {  0,  7,  6,  131},  {  0,  9,  6,  515},\n    { 16,  0,  4,    4},  {  0,  0,  4,    5},\n    { 32,  0,  5,    6},  {  0,  0,  5,    7},\n    { 32,  0,  5,    9},  {  0,  0,  5,   10},\n    {  0,  0,  6,   12},  {  0,  0,  6,   15},\n    {  0,  0,  6,   18},  {  0,  0,  6,   21},\n    {  0,  0,  6,   24},  {  0,  0,  6,   27},\n    {  0,  0,  6,   30},  {  0,  0,  6,   33},\n    {  0,  1,  6,   35},  {  0,  1,  6,   39},\n    {  0,  2,  6,   43},  {  0,  3,  6,   51},\n    {  0,  4,  6,   67},  {  0,  5,  6,   99},\n    {  0,  8,  6,  259},  { 32,  0,  4,    4},\n    { 48,  0,  4,    4},  { 16,  0,  4,    5},\n    { 32,  0,  5,    7},  { 32,  0,  5,    8},\n    { 32,  0,  5,   10},  { 32,  0,  5,   11},\n    {  0,  0,  6,   14},  {  0,  0,  6,   17},\n    {  0,  0,  6,   20},  {  0,  0,  6,   23},\n    {  0,  0,  6,   26},  {  0,  0,  6,   29},\n    {  0,  0,  6,   32},  {  0, 16,  6,65539},\n    {  0, 15,  6,32771},  {  0, 14,  6,16387},\n    {  0, 13,  6, 8195},  {  0, 12,  6, 4099},\n    {  0, 11,  6, 2051},  {  0, 10,  6, 1027},\n};   /* ML_defaultDTable */\n\n\nstatic void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, U32 baseValue, U32 nbAddBits)\n{\n    void* ptr = dt;\n    ZSTD_seqSymbol_header* const DTableH = (ZSTD_seqSymbol_header*)ptr;\n    ZSTD_seqSymbol* const cell = dt + 1;\n\n    DTableH->tableLog = 0;\n    DTableH->fastMode = 0;\n\n    cell->nbBits = 0;\n    cell->nextState = 0;\n    assert(nbAddBits < 255);\n    cell->nbAdditionalBits = (BYTE)nbAddBits;\n    cell->baseValue = baseValue;\n}\n\n\n/* ZSTD_buildFSETable() :\n * generate FSE decoding table for one symbol (ll, ml or off)\n * cannot fail if input is valid =>\n * all inputs are presumed validated at this stage */\nvoid\nZSTD_buildFSETable(ZSTD_seqSymbol* dt,\n            const short* normalizedCounter, unsigned maxSymbolValue,\n            const U32* baseValue, const U32* nbAdditionalBits,\n            unsigned tableLog)\n{\n    ZSTD_seqSymbol* const tableDecode = dt+1;\n    U16 symbolNext[MaxSeq+1];\n\n    U32 const maxSV1 = maxSymbolValue + 1;\n    U32 const tableSize = 1 << tableLog;\n    U32 highThreshold = tableSize-1;\n\n    /* Sanity Checks */\n    assert(maxSymbolValue <= MaxSeq);\n    assert(tableLog <= MaxFSELog);\n\n    /* Init, lay down lowprob symbols */\n    {   ZSTD_seqSymbol_header DTableH;\n        DTableH.tableLog = tableLog;\n        DTableH.fastMode = 1;\n        {   S16 const largeLimit= (S16)(1 << (tableLog-1));\n            U32 s;\n            for (s=0; s<maxSV1; s++) {\n                if (normalizedCounter[s]==-1) {\n                    tableDecode[highThreshold--].baseValue = s;\n                    symbolNext[s] = 1;\n                } else {\n                    if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;\n                    assert(normalizedCounter[s]>=0);\n                    symbolNext[s] = (U16)normalizedCounter[s];\n        }   }   }\n        memcpy(dt, &DTableH, sizeof(DTableH));\n    }\n\n    /* Spread symbols */\n    {   U32 const tableMask = tableSize-1;\n        U32 const step = FSE_TABLESTEP(tableSize);\n        U32 s, position = 0;\n        for (s=0; s<maxSV1; s++) {\n            int i;\n            for (i=0; i<normalizedCounter[s]; i++) {\n                tableDecode[position].baseValue = s;\n                position = (position + step) & tableMask;\n                while (position > highThreshold) position = (position + step) & tableMask;   /* lowprob area */\n        }   }\n        assert(position == 0); /* position must reach all cells once, otherwise normalizedCounter is incorrect */\n    }\n\n    /* Build Decoding table */\n    {   U32 u;\n        for (u=0; u<tableSize; u++) {\n            U32 const symbol = tableDecode[u].baseValue;\n            U32 const nextState = symbolNext[symbol]++;\n            tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );\n            tableDecode[u].nextState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);\n            assert(nbAdditionalBits[symbol] < 255);\n            tableDecode[u].nbAdditionalBits = (BYTE)nbAdditionalBits[symbol];\n            tableDecode[u].baseValue = baseValue[symbol];\n    }   }\n}\n\n\n/*! ZSTD_buildSeqTable() :\n * @return : nb bytes read from src,\n *           or an error code if it fails */\nstatic size_t ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, const ZSTD_seqSymbol** DTablePtr,\n                                 symbolEncodingType_e type, unsigned max, U32 maxLog,\n                                 const void* src, size_t srcSize,\n                                 const U32* baseValue, const U32* nbAdditionalBits,\n                                 const ZSTD_seqSymbol* defaultTable, U32 flagRepeatTable,\n                                 int ddictIsCold, int nbSeq)\n{\n    switch(type)\n    {\n    case set_rle :\n        RETURN_ERROR_IF(!srcSize, srcSize_wrong, \"\");\n        RETURN_ERROR_IF((*(const BYTE*)src) > max, corruption_detected, \"\");\n        {   U32 const symbol = *(const BYTE*)src;\n            U32 const baseline = baseValue[symbol];\n            U32 const nbBits = nbAdditionalBits[symbol];\n            ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits);\n        }\n        *DTablePtr = DTableSpace;\n        return 1;\n    case set_basic :\n        *DTablePtr = defaultTable;\n        return 0;\n    case set_repeat:\n        RETURN_ERROR_IF(!flagRepeatTable, corruption_detected, \"\");\n        /* prefetch FSE table if used */\n        if (ddictIsCold && (nbSeq > 24 /* heuristic */)) {\n            const void* const pStart = *DTablePtr;\n            size_t const pSize = sizeof(ZSTD_seqSymbol) * (SEQSYMBOL_TABLE_SIZE(maxLog));\n            PREFETCH_AREA(pStart, pSize);\n        }\n        return 0;\n    case set_compressed :\n        {   unsigned tableLog;\n            S16 norm[MaxSeq+1];\n            size_t const headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize);\n            RETURN_ERROR_IF(FSE_isError(headerSize), corruption_detected, \"\");\n            RETURN_ERROR_IF(tableLog > maxLog, corruption_detected, \"\");\n            ZSTD_buildFSETable(DTableSpace, norm, max, baseValue, nbAdditionalBits, tableLog);\n            *DTablePtr = DTableSpace;\n            return headerSize;\n        }\n    default :\n        assert(0);\n        RETURN_ERROR(GENERIC, \"impossible\");\n    }\n}\n\nsize_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr,\n                             const void* src, size_t srcSize)\n{\n    const BYTE* const istart = (const BYTE* const)src;\n    const BYTE* const iend = istart + srcSize;\n    const BYTE* ip = istart;\n    int nbSeq;\n    DEBUGLOG(5, \"ZSTD_decodeSeqHeaders\");\n\n    /* check */\n    RETURN_ERROR_IF(srcSize < MIN_SEQUENCES_SIZE, srcSize_wrong, \"\");\n\n    /* SeqHead */\n    nbSeq = *ip++;\n    if (!nbSeq) {\n        *nbSeqPtr=0;\n        RETURN_ERROR_IF(srcSize != 1, srcSize_wrong, \"\");\n        return 1;\n    }\n    if (nbSeq > 0x7F) {\n        if (nbSeq == 0xFF) {\n            RETURN_ERROR_IF(ip+2 > iend, srcSize_wrong, \"\");\n            nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;\n        } else {\n            RETURN_ERROR_IF(ip >= iend, srcSize_wrong, \"\");\n            nbSeq = ((nbSeq-0x80)<<8) + *ip++;\n        }\n    }\n    *nbSeqPtr = nbSeq;\n\n    /* FSE table descriptors */\n    RETURN_ERROR_IF(ip+1 > iend, srcSize_wrong, \"\"); /* minimum possible size: 1 byte for symbol encoding types */\n    {   symbolEncodingType_e const LLtype = (symbolEncodingType_e)(*ip >> 6);\n        symbolEncodingType_e const OFtype = (symbolEncodingType_e)((*ip >> 4) & 3);\n        symbolEncodingType_e const MLtype = (symbolEncodingType_e)((*ip >> 2) & 3);\n        ip++;\n\n        /* Build DTables */\n        {   size_t const llhSize = ZSTD_buildSeqTable(dctx->entropy.LLTable, &dctx->LLTptr,\n                                                      LLtype, MaxLL, LLFSELog,\n                                                      ip, iend-ip,\n                                                      LL_base, LL_bits,\n                                                      LL_defaultDTable, dctx->fseEntropy,\n                                                      dctx->ddictIsCold, nbSeq);\n            RETURN_ERROR_IF(ZSTD_isError(llhSize), corruption_detected, \"ZSTD_buildSeqTable failed\");\n            ip += llhSize;\n        }\n\n        {   size_t const ofhSize = ZSTD_buildSeqTable(dctx->entropy.OFTable, &dctx->OFTptr,\n                                                      OFtype, MaxOff, OffFSELog,\n                                                      ip, iend-ip,\n                                                      OF_base, OF_bits,\n                                                      OF_defaultDTable, dctx->fseEntropy,\n                                                      dctx->ddictIsCold, nbSeq);\n            RETURN_ERROR_IF(ZSTD_isError(ofhSize), corruption_detected, \"ZSTD_buildSeqTable failed\");\n            ip += ofhSize;\n        }\n\n        {   size_t const mlhSize = ZSTD_buildSeqTable(dctx->entropy.MLTable, &dctx->MLTptr,\n                                                      MLtype, MaxML, MLFSELog,\n                                                      ip, iend-ip,\n                                                      ML_base, ML_bits,\n                                                      ML_defaultDTable, dctx->fseEntropy,\n                                                      dctx->ddictIsCold, nbSeq);\n            RETURN_ERROR_IF(ZSTD_isError(mlhSize), corruption_detected, \"ZSTD_buildSeqTable failed\");\n            ip += mlhSize;\n        }\n    }\n\n    return ip-istart;\n}\n\n\ntypedef struct {\n    size_t litLength;\n    size_t matchLength;\n    size_t offset;\n    const BYTE* match;\n} seq_t;\n\ntypedef struct {\n    size_t state;\n    const ZSTD_seqSymbol* table;\n} ZSTD_fseState;\n\ntypedef struct {\n    BIT_DStream_t DStream;\n    ZSTD_fseState stateLL;\n    ZSTD_fseState stateOffb;\n    ZSTD_fseState stateML;\n    size_t prevOffset[ZSTD_REP_NUM];\n    const BYTE* prefixStart;\n    const BYTE* dictEnd;\n    size_t pos;\n} seqState_t;\n\n/*! ZSTD_overlapCopy8() :\n *  Copies 8 bytes from ip to op and updates op and ip where ip <= op.\n *  If the offset is < 8 then the offset is spread to at least 8 bytes.\n *\n *  Precondition: *ip <= *op\n *  Postcondition: *op - *op >= 8\n */\nHINT_INLINE void ZSTD_overlapCopy8(BYTE** op, BYTE const** ip, size_t offset) {\n    assert(*ip <= *op);\n    if (offset < 8) {\n        /* close range match, overlap */\n        static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 };   /* added */\n        static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 };   /* subtracted */\n        int const sub2 = dec64table[offset];\n        (*op)[0] = (*ip)[0];\n        (*op)[1] = (*ip)[1];\n        (*op)[2] = (*ip)[2];\n        (*op)[3] = (*ip)[3];\n        *ip += dec32table[offset];\n        ZSTD_copy4(*op+4, *ip);\n        *ip -= sub2;\n    } else {\n        ZSTD_copy8(*op, *ip);\n    }\n    *ip += 8;\n    *op += 8;\n    assert(*op - *ip >= 8);\n}\n\n/*! ZSTD_safecopy() :\n *  Specialized version of memcpy() that is allowed to READ up to WILDCOPY_OVERLENGTH past the input buffer\n *  and write up to 16 bytes past oend_w (op >= oend_w is allowed).\n *  This function is only called in the uncommon case where the sequence is near the end of the block. It\n *  should be fast for a single long sequence, but can be slow for several short sequences.\n *\n *  @param ovtype controls the overlap detection\n *         - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart.\n *         - ZSTD_overlap_src_before_dst: The src and dst may overlap and may be any distance apart.\n *           The src buffer must be before the dst buffer.\n */\nstatic void ZSTD_safecopy(BYTE* op, BYTE* const oend_w, BYTE const* ip, ptrdiff_t length, ZSTD_overlap_e ovtype) {\n    ptrdiff_t const diff = op - ip;\n    BYTE* const oend = op + length;\n\n    assert((ovtype == ZSTD_no_overlap && (diff <= -8 || diff >= 8 || op >= oend_w)) ||\n           (ovtype == ZSTD_overlap_src_before_dst && diff >= 0));\n\n    if (length < 8) {\n        /* Handle short lengths. */\n        while (op < oend) *op++ = *ip++;\n        return;\n    }\n    if (ovtype == ZSTD_overlap_src_before_dst) {\n        /* Copy 8 bytes and ensure the offset >= 8 when there can be overlap. */\n        assert(length >= 8);\n        ZSTD_overlapCopy8(&op, &ip, diff);\n        assert(op - ip >= 8);\n        assert(op <= oend);\n    }\n\n    if (oend <= oend_w) {\n        /* No risk of overwrite. */\n        ZSTD_wildcopy(op, ip, length, ovtype);\n        return;\n    }\n    if (op <= oend_w) {\n        /* Wildcopy until we get close to the end. */\n        assert(oend > oend_w);\n        ZSTD_wildcopy(op, ip, oend_w - op, ovtype);\n        ip += oend_w - op;\n        op = oend_w;\n    }\n    /* Handle the leftovers. */\n    while (op < oend) *op++ = *ip++;\n}\n\n/* ZSTD_execSequenceEnd():\n * This version handles cases that are near the end of the output buffer. It requires\n * more careful checks to make sure there is no overflow. By separating out these hard\n * and unlikely cases, we can speed up the common cases.\n *\n * NOTE: This function needs to be fast for a single long sequence, but doesn't need\n * to be optimized for many small sequences, since those fall into ZSTD_execSequence().\n */\nFORCE_NOINLINE\nsize_t ZSTD_execSequenceEnd(BYTE* op,\n                            BYTE* const oend, seq_t sequence,\n                            const BYTE** litPtr, const BYTE* const litLimit,\n                            const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)\n{\n    BYTE* const oLitEnd = op + sequence.litLength;\n    size_t const sequenceLength = sequence.litLength + sequence.matchLength;\n    const BYTE* const iLitEnd = *litPtr + sequence.litLength;\n    const BYTE* match = oLitEnd - sequence.offset;\n    BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;\n\n    /* bounds checks : careful of address space overflow in 32-bit mode */\n    RETURN_ERROR_IF(sequenceLength > (size_t)(oend - op), dstSize_tooSmall, \"last match must fit within dstBuffer\");\n    RETURN_ERROR_IF(sequence.litLength > (size_t)(litLimit - *litPtr), corruption_detected, \"try to read beyond literal buffer\");\n    assert(op < op + sequenceLength);\n    assert(oLitEnd < op + sequenceLength);\n\n    /* copy literals */\n    ZSTD_safecopy(op, oend_w, *litPtr, sequence.litLength, ZSTD_no_overlap);\n    op = oLitEnd;\n    *litPtr = iLitEnd;\n\n    /* copy Match */\n    if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {\n        /* offset beyond prefix */\n        RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected, \"\");\n        match = dictEnd - (prefixStart-match);\n        if (match + sequence.matchLength <= dictEnd) {\n            memmove(oLitEnd, match, sequence.matchLength);\n            return sequenceLength;\n        }\n        /* span extDict & currentPrefixSegment */\n        {   size_t const length1 = dictEnd - match;\n            memmove(oLitEnd, match, length1);\n            op = oLitEnd + length1;\n            sequence.matchLength -= length1;\n            match = prefixStart;\n    }   }\n    ZSTD_safecopy(op, oend_w, match, sequence.matchLength, ZSTD_overlap_src_before_dst);\n    return sequenceLength;\n}\n\nHINT_INLINE\nsize_t ZSTD_execSequence(BYTE* op,\n                         BYTE* const oend, seq_t sequence,\n                         const BYTE** litPtr, const BYTE* const litLimit,\n                         const BYTE* const prefixStart, const BYTE* const virtualStart, const BYTE* const dictEnd)\n{\n    BYTE* const oLitEnd = op + sequence.litLength;\n    size_t const sequenceLength = sequence.litLength + sequence.matchLength;\n    BYTE* const oMatchEnd = op + sequenceLength;   /* risk : address space overflow (32-bits) */\n    BYTE* const oend_w = oend - WILDCOPY_OVERLENGTH;   /* risk : address space underflow on oend=NULL */\n    const BYTE* const iLitEnd = *litPtr + sequence.litLength;\n    const BYTE* match = oLitEnd - sequence.offset;\n\n    assert(op != NULL /* Precondition */);\n    assert(oend_w < oend /* No underflow */);\n    /* Handle edge cases in a slow path:\n     *   - Read beyond end of literals\n     *   - Match end is within WILDCOPY_OVERLIMIT of oend\n     *   - 32-bit mode and the match length overflows\n     */\n    if (UNLIKELY(\n            iLitEnd > litLimit ||\n            oMatchEnd > oend_w ||\n            (MEM_32bits() && (size_t)(oend - op) < sequenceLength + WILDCOPY_OVERLENGTH)))\n        return ZSTD_execSequenceEnd(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd);\n\n    /* Assumptions (everything else goes into ZSTD_execSequenceEnd()) */\n    assert(op <= oLitEnd /* No overflow */);\n    assert(oLitEnd < oMatchEnd /* Non-zero match & no overflow */);\n    assert(oMatchEnd <= oend /* No underflow */);\n    assert(iLitEnd <= litLimit /* Literal length is in bounds */);\n    assert(oLitEnd <= oend_w /* Can wildcopy literals */);\n    assert(oMatchEnd <= oend_w /* Can wildcopy matches */);\n\n    /* Copy Literals:\n     * Split out litLength <= 16 since it is nearly always true. +1.6% on gcc-9.\n     * We likely don't need the full 32-byte wildcopy.\n     */\n    assert(WILDCOPY_OVERLENGTH >= 16);\n    ZSTD_copy16(op, (*litPtr));\n    if (UNLIKELY(sequence.litLength > 16)) {\n        ZSTD_wildcopy(op+16, (*litPtr)+16, sequence.litLength-16, ZSTD_no_overlap);\n    }\n    op = oLitEnd;\n    *litPtr = iLitEnd;   /* update for next sequence */\n\n    /* Copy Match */\n    if (sequence.offset > (size_t)(oLitEnd - prefixStart)) {\n        /* offset beyond prefix -> go into extDict */\n        RETURN_ERROR_IF(UNLIKELY(sequence.offset > (size_t)(oLitEnd - virtualStart)), corruption_detected, \"\");\n        match = dictEnd + (match - prefixStart);\n        if (match + sequence.matchLength <= dictEnd) {\n            memmove(oLitEnd, match, sequence.matchLength);\n            return sequenceLength;\n        }\n        /* span extDict & currentPrefixSegment */\n        {   size_t const length1 = dictEnd - match;\n            memmove(oLitEnd, match, length1);\n            op = oLitEnd + length1;\n            sequence.matchLength -= length1;\n            match = prefixStart;\n    }   }\n    /* Match within prefix of 1 or more bytes */\n    assert(op <= oMatchEnd);\n    assert(oMatchEnd <= oend_w);\n    assert(match >= prefixStart);\n    assert(sequence.matchLength >= 1);\n\n    /* Nearly all offsets are >= WILDCOPY_VECLEN bytes, which means we can use wildcopy\n     * without overlap checking.\n     */\n    if (LIKELY(sequence.offset >= WILDCOPY_VECLEN)) {\n        /* We bet on a full wildcopy for matches, since we expect matches to be\n         * longer than literals (in general). In silesia, ~10% of matches are longer\n         * than 16 bytes.\n         */\n        ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength, ZSTD_no_overlap);\n        return sequenceLength;\n    }\n    assert(sequence.offset < WILDCOPY_VECLEN);\n\n    /* Copy 8 bytes and spread the offset to be >= 8. */\n    ZSTD_overlapCopy8(&op, &match, sequence.offset);\n\n    /* If the match length is > 8 bytes, then continue with the wildcopy. */\n    if (sequence.matchLength > 8) {\n        assert(op < oMatchEnd);\n        ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8, ZSTD_overlap_src_before_dst);\n    }\n    return sequenceLength;\n}\n\nstatic void\nZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, const ZSTD_seqSymbol* dt)\n{\n    const void* ptr = dt;\n    const ZSTD_seqSymbol_header* const DTableH = (const ZSTD_seqSymbol_header*)ptr;\n    DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog);\n    DEBUGLOG(6, \"ZSTD_initFseState : val=%u using %u bits\",\n                (U32)DStatePtr->state, DTableH->tableLog);\n    BIT_reloadDStream(bitD);\n    DStatePtr->table = dt + 1;\n}\n\nFORCE_INLINE_TEMPLATE void\nZSTD_updateFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD)\n{\n    ZSTD_seqSymbol const DInfo = DStatePtr->table[DStatePtr->state];\n    U32 const nbBits = DInfo.nbBits;\n    size_t const lowBits = BIT_readBits(bitD, nbBits);\n    DStatePtr->state = DInfo.nextState + lowBits;\n}\n\nFORCE_INLINE_TEMPLATE void\nZSTD_updateFseStateWithDInfo(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, ZSTD_seqSymbol const DInfo)\n{\n    U32 const nbBits = DInfo.nbBits;\n    size_t const lowBits = BIT_readBits(bitD, nbBits);\n    DStatePtr->state = DInfo.nextState + lowBits;\n}\n\n/* We need to add at most (ZSTD_WINDOWLOG_MAX_32 - 1) bits to read the maximum\n * offset bits. But we can only read at most (STREAM_ACCUMULATOR_MIN_32 - 1)\n * bits before reloading. This value is the maximum number of bytes we read\n * after reloading when we are decoding long offsets.\n */\n#define LONG_OFFSETS_MAX_EXTRA_BITS_32                       \\\n    (ZSTD_WINDOWLOG_MAX_32 > STREAM_ACCUMULATOR_MIN_32       \\\n        ? ZSTD_WINDOWLOG_MAX_32 - STREAM_ACCUMULATOR_MIN_32  \\\n        : 0)\n\ntypedef enum { ZSTD_lo_isRegularOffset, ZSTD_lo_isLongOffset=1 } ZSTD_longOffset_e;\ntypedef enum { ZSTD_p_noPrefetch=0, ZSTD_p_prefetch=1 } ZSTD_prefetch_e;\n\nFORCE_INLINE_TEMPLATE seq_t\nZSTD_decodeSequence(seqState_t* seqState, const ZSTD_longOffset_e longOffsets, const ZSTD_prefetch_e prefetch)\n{\n    seq_t seq;\n    ZSTD_seqSymbol const llDInfo = seqState->stateLL.table[seqState->stateLL.state];\n    ZSTD_seqSymbol const mlDInfo = seqState->stateML.table[seqState->stateML.state];\n    ZSTD_seqSymbol const ofDInfo = seqState->stateOffb.table[seqState->stateOffb.state];\n    U32 const llBase = llDInfo.baseValue;\n    U32 const mlBase = mlDInfo.baseValue;\n    U32 const ofBase = ofDInfo.baseValue;\n    BYTE const llBits = llDInfo.nbAdditionalBits;\n    BYTE const mlBits = mlDInfo.nbAdditionalBits;\n    BYTE const ofBits = ofDInfo.nbAdditionalBits;\n    BYTE const totalBits = llBits+mlBits+ofBits;\n\n    /* sequence */\n    {   size_t offset;\n        if (ofBits > 1) {\n            ZSTD_STATIC_ASSERT(ZSTD_lo_isLongOffset == 1);\n            ZSTD_STATIC_ASSERT(LONG_OFFSETS_MAX_EXTRA_BITS_32 == 5);\n            assert(ofBits <= MaxOff);\n            if (MEM_32bits() && longOffsets && (ofBits >= STREAM_ACCUMULATOR_MIN_32)) {\n                U32 const extraBits = ofBits - MIN(ofBits, 32 - seqState->DStream.bitsConsumed);\n                offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << extraBits);\n                BIT_reloadDStream(&seqState->DStream);\n                if (extraBits) offset += BIT_readBitsFast(&seqState->DStream, extraBits);\n                assert(extraBits <= LONG_OFFSETS_MAX_EXTRA_BITS_32);   /* to avoid another reload */\n            } else {\n                offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits/*>0*/);   /* <=  (ZSTD_WINDOWLOG_MAX-1) bits */\n                if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);\n            }\n            seqState->prevOffset[2] = seqState->prevOffset[1];\n            seqState->prevOffset[1] = seqState->prevOffset[0];\n            seqState->prevOffset[0] = offset;\n        } else {\n            U32 const ll0 = (llBase == 0);\n            if (LIKELY((ofBits == 0))) {\n                if (LIKELY(!ll0))\n                    offset = seqState->prevOffset[0];\n                else {\n                    offset = seqState->prevOffset[1];\n                    seqState->prevOffset[1] = seqState->prevOffset[0];\n                    seqState->prevOffset[0] = offset;\n                }\n            } else {\n                offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1);\n                {   size_t temp = (offset==3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset];\n                    temp += !temp;   /* 0 is not valid; input is corrupted; force offset to 1 */\n                    if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];\n                    seqState->prevOffset[1] = seqState->prevOffset[0];\n                    seqState->prevOffset[0] = offset = temp;\n        }   }   }\n        seq.offset = offset;\n    }\n\n    seq.matchLength = mlBase;\n    if (mlBits > 0)\n        seq.matchLength += BIT_readBitsFast(&seqState->DStream, mlBits/*>0*/);\n\n    if (MEM_32bits() && (mlBits+llBits >= STREAM_ACCUMULATOR_MIN_32-LONG_OFFSETS_MAX_EXTRA_BITS_32))\n        BIT_reloadDStream(&seqState->DStream);\n    if (MEM_64bits() && UNLIKELY(totalBits >= STREAM_ACCUMULATOR_MIN_64-(LLFSELog+MLFSELog+OffFSELog)))\n        BIT_reloadDStream(&seqState->DStream);\n    /* Ensure there are enough bits to read the rest of data in 64-bit mode. */\n    ZSTD_STATIC_ASSERT(16+LLFSELog+MLFSELog+OffFSELog < STREAM_ACCUMULATOR_MIN_64);\n\n    seq.litLength = llBase;\n    if (llBits > 0)\n        seq.litLength += BIT_readBitsFast(&seqState->DStream, llBits/*>0*/);\n\n    if (MEM_32bits())\n        BIT_reloadDStream(&seqState->DStream);\n\n    DEBUGLOG(6, \"seq: litL=%u, matchL=%u, offset=%u\",\n                (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);\n\n    if (prefetch == ZSTD_p_prefetch) {\n        size_t const pos = seqState->pos + seq.litLength;\n        const BYTE* const matchBase = (seq.offset > pos) ? seqState->dictEnd : seqState->prefixStart;\n        seq.match = matchBase + pos - seq.offset;  /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted.\n                                                    * No consequence though : no memory access will occur, offset is only used for prefetching */\n        seqState->pos = pos + seq.matchLength;\n    }\n\n    /* ANS state update\n     * gcc-9.0.0 does 2.5% worse with ZSTD_updateFseStateWithDInfo().\n     * clang-9.2.0 does 7% worse with ZSTD_updateFseState().\n     * Naturally it seems like ZSTD_updateFseStateWithDInfo() should be the\n     * better option, so it is the default for other compilers. But, if you\n     * measure that it is worse, please put up a pull request.\n     */\n    {\n#if defined(__GNUC__) && !defined(__clang__)\n        const int kUseUpdateFseState = 1;\n#else\n        const int kUseUpdateFseState = 0;\n#endif\n        if (kUseUpdateFseState) {\n            ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream);    /* <=  9 bits */\n            ZSTD_updateFseState(&seqState->stateML, &seqState->DStream);    /* <=  9 bits */\n            if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);    /* <= 18 bits */\n            ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream);  /* <=  8 bits */\n        } else {\n            ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llDInfo);    /* <=  9 bits */\n            ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlDInfo);    /* <=  9 bits */\n            if (MEM_32bits()) BIT_reloadDStream(&seqState->DStream);    /* <= 18 bits */\n            ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofDInfo);  /* <=  8 bits */\n        }\n    }\n\n    return seq;\n}\n\n#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION\nMEM_STATIC int ZSTD_dictionaryIsActive(ZSTD_DCtx const* dctx, BYTE const* prefixStart, BYTE const* oLitEnd)\n{\n    size_t const windowSize = dctx->fParams.windowSize;\n    /* No dictionary used. */\n    if (dctx->dictContentEndForFuzzing == NULL) return 0;\n    /* Dictionary is our prefix. */\n    if (prefixStart == dctx->dictContentBeginForFuzzing) return 1;\n    /* Dictionary is not our ext-dict. */\n    if (dctx->dictEnd != dctx->dictContentEndForFuzzing) return 0;\n    /* Dictionary is not within our window size. */\n    if ((size_t)(oLitEnd - prefixStart) >= windowSize) return 0;\n    /* Dictionary is active. */\n    return 1;\n}\n\nMEM_STATIC void ZSTD_assertValidSequence(\n        ZSTD_DCtx const* dctx,\n        BYTE const* op, BYTE const* oend,\n        seq_t const seq,\n        BYTE const* prefixStart, BYTE const* virtualStart)\n{\n#if DEBUGLEVEL >= 1\n    size_t const windowSize = dctx->fParams.windowSize;\n    size_t const sequenceSize = seq.litLength + seq.matchLength;\n    BYTE const* const oLitEnd = op + seq.litLength;\n    DEBUGLOG(6, \"Checking sequence: litL=%u matchL=%u offset=%u\",\n            (U32)seq.litLength, (U32)seq.matchLength, (U32)seq.offset);\n    assert(op <= oend);\n    assert((size_t)(oend - op) >= sequenceSize);\n    assert(sequenceSize <= ZSTD_BLOCKSIZE_MAX);\n    if (ZSTD_dictionaryIsActive(dctx, prefixStart, oLitEnd)) {\n        size_t const dictSize = (size_t)((char const*)dctx->dictContentEndForFuzzing - (char const*)dctx->dictContentBeginForFuzzing);\n        /* Offset must be within the dictionary. */\n        assert(seq.offset <= (size_t)(oLitEnd - virtualStart));\n        assert(seq.offset <= windowSize + dictSize);\n    } else {\n        /* Offset must be within our window. */\n        assert(seq.offset <= windowSize);\n    }\n#else\n    (void)dctx, (void)op, (void)oend, (void)seq, (void)prefixStart, (void)virtualStart;\n#endif\n}\n#endif\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG\nFORCE_INLINE_TEMPLATE size_t\nDONT_VECTORIZE\nZSTD_decompressSequences_body( ZSTD_DCtx* dctx,\n                               void* dst, size_t maxDstSize,\n                         const void* seqStart, size_t seqSize, int nbSeq,\n                         const ZSTD_longOffset_e isLongOffset,\n                         const int frame)\n{\n    const BYTE* ip = (const BYTE*)seqStart;\n    const BYTE* const iend = ip + seqSize;\n    BYTE* const ostart = (BYTE* const)dst;\n    BYTE* const oend = ostart + maxDstSize;\n    BYTE* op = ostart;\n    const BYTE* litPtr = dctx->litPtr;\n    const BYTE* const litEnd = litPtr + dctx->litSize;\n    const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);\n    const BYTE* const vBase = (const BYTE*) (dctx->virtualStart);\n    const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);\n    DEBUGLOG(5, \"ZSTD_decompressSequences_body\");\n    (void)frame;\n\n    /* Regen sequences */\n    if (nbSeq) {\n        seqState_t seqState;\n        size_t error = 0;\n        dctx->fseEntropy = 1;\n        { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }\n        RETURN_ERROR_IF(\n            ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),\n            corruption_detected, \"\");\n        ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);\n        ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);\n        ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);\n        assert(dst != NULL);\n\n        ZSTD_STATIC_ASSERT(\n                BIT_DStream_unfinished < BIT_DStream_completed &&\n                BIT_DStream_endOfBuffer < BIT_DStream_completed &&\n                BIT_DStream_completed < BIT_DStream_overflow);\n\n#if defined(__GNUC__) && defined(__x86_64__)\n        /* Align the decompression loop to 32 + 16 bytes.\n         *\n         * zstd compiled with gcc-9 on an Intel i9-9900k shows 10% decompression\n         * speed swings based on the alignment of the decompression loop. This\n         * performance swing is caused by parts of the decompression loop falling\n         * out of the DSB. The entire decompression loop should fit in the DSB,\n         * when it can't we get much worse performance. You can measure if you've\n         * hit the good case or the bad case with this perf command for some\n         * compressed file test.zst:\n         *\n         *   perf stat -e cycles -e instructions -e idq.all_dsb_cycles_any_uops \\\n         *             -e idq.all_mite_cycles_any_uops -- ./zstd -tq test.zst\n         *\n         * If you see most cycles served out of the MITE you've hit the bad case.\n         * If you see most cycles served out of the DSB you've hit the good case.\n         * If it is pretty even then you may be in an okay case.\n         *\n         * I've been able to reproduce this issue on the following CPUs:\n         *   - Kabylake: Macbook Pro (15-inch, 2019) 2.4 GHz Intel Core i9\n         *               Use Instruments->Counters to get DSB/MITE cycles.\n         *               I never got performance swings, but I was able to\n         *               go from the good case of mostly DSB to half of the\n         *               cycles served from MITE.\n         *   - Coffeelake: Intel i9-9900k\n         *\n         * I haven't been able to reproduce the instability or DSB misses on any\n         * of the following CPUS:\n         *   - Haswell\n         *   - Broadwell: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GH\n         *   - Skylake\n         *\n         * If you are seeing performance stability this script can help test.\n         * It tests on 4 commits in zstd where I saw performance change.\n         *\n         *   https://gist.github.com/terrelln/9889fc06a423fd5ca6e99351564473f4\n         */\n        __asm__(\".p2align 5\");\n        __asm__(\"nop\");\n        __asm__(\".p2align 4\");\n#endif\n        for ( ; ; ) {\n            seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_noPrefetch);\n            size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd);\n#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)\n            assert(!ZSTD_isError(oneSeqSize));\n            if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequence, prefixStart, vBase);\n#endif\n            DEBUGLOG(6, \"regenerated sequence size : %u\", (U32)oneSeqSize);\n            BIT_reloadDStream(&(seqState.DStream));\n            /* gcc and clang both don't like early returns in this loop.\n             * gcc doesn't like early breaks either.\n             * Instead save an error and report it at the end.\n             * When there is an error, don't increment op, so we don't\n             * overwrite.\n             */\n            if (UNLIKELY(ZSTD_isError(oneSeqSize))) error = oneSeqSize;\n            else op += oneSeqSize;\n            if (UNLIKELY(!--nbSeq)) break;\n        }\n\n        /* check if reached exact end */\n        DEBUGLOG(5, \"ZSTD_decompressSequences_body: after decode loop, remaining nbSeq : %i\", nbSeq);\n        if (ZSTD_isError(error)) return error;\n        RETURN_ERROR_IF(nbSeq, corruption_detected, \"\");\n        RETURN_ERROR_IF(BIT_reloadDStream(&seqState.DStream) < BIT_DStream_completed, corruption_detected, \"\");\n        /* save reps for next block */\n        { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }\n    }\n\n    /* last literal segment */\n    {   size_t const lastLLSize = litEnd - litPtr;\n        RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, \"\");\n        if (op != NULL) {\n            memcpy(op, litPtr, lastLLSize);\n            op += lastLLSize;\n        }\n    }\n\n    return op-ostart;\n}\n\nstatic size_t\nZSTD_decompressSequences_default(ZSTD_DCtx* dctx,\n                                 void* dst, size_t maxDstSize,\n                           const void* seqStart, size_t seqSize, int nbSeq,\n                           const ZSTD_longOffset_e isLongOffset,\n                           const int frame)\n{\n    return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n}\n#endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT\nFORCE_INLINE_TEMPLATE size_t\nZSTD_decompressSequencesLong_body(\n                               ZSTD_DCtx* dctx,\n                               void* dst, size_t maxDstSize,\n                         const void* seqStart, size_t seqSize, int nbSeq,\n                         const ZSTD_longOffset_e isLongOffset,\n                         const int frame)\n{\n    const BYTE* ip = (const BYTE*)seqStart;\n    const BYTE* const iend = ip + seqSize;\n    BYTE* const ostart = (BYTE* const)dst;\n    BYTE* const oend = ostart + maxDstSize;\n    BYTE* op = ostart;\n    const BYTE* litPtr = dctx->litPtr;\n    const BYTE* const litEnd = litPtr + dctx->litSize;\n    const BYTE* const prefixStart = (const BYTE*) (dctx->prefixStart);\n    const BYTE* const dictStart = (const BYTE*) (dctx->virtualStart);\n    const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);\n    (void)frame;\n\n    /* Regen sequences */\n    if (nbSeq) {\n#define STORED_SEQS 4\n#define STORED_SEQS_MASK (STORED_SEQS-1)\n#define ADVANCED_SEQS 4\n        seq_t sequences[STORED_SEQS];\n        int const seqAdvance = MIN(nbSeq, ADVANCED_SEQS);\n        seqState_t seqState;\n        int seqNb;\n        dctx->fseEntropy = 1;\n        { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqState.prevOffset[i] = dctx->entropy.rep[i]; }\n        seqState.prefixStart = prefixStart;\n        seqState.pos = (size_t)(op-prefixStart);\n        seqState.dictEnd = dictEnd;\n        assert(dst != NULL);\n        assert(iend >= ip);\n        RETURN_ERROR_IF(\n            ERR_isError(BIT_initDStream(&seqState.DStream, ip, iend-ip)),\n            corruption_detected, \"\");\n        ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr);\n        ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr);\n        ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr);\n\n        /* prepare in advance */\n        for (seqNb=0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_completed) && (seqNb<seqAdvance); seqNb++) {\n            sequences[seqNb] = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_prefetch);\n            PREFETCH_L1(sequences[seqNb].match); PREFETCH_L1(sequences[seqNb].match + sequences[seqNb].matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */\n        }\n        RETURN_ERROR_IF(seqNb<seqAdvance, corruption_detected, \"\");\n\n        /* decode and decompress */\n        for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && (seqNb<nbSeq) ; seqNb++) {\n            seq_t const sequence = ZSTD_decodeSequence(&seqState, isLongOffset, ZSTD_p_prefetch);\n            size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);\n#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)\n            assert(!ZSTD_isError(oneSeqSize));\n            if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequences[(seqNb-ADVANCED_SEQS) & STORED_SEQS_MASK], prefixStart, dictStart);\n#endif\n            if (ZSTD_isError(oneSeqSize)) return oneSeqSize;\n            PREFETCH_L1(sequence.match); PREFETCH_L1(sequence.match + sequence.matchLength - 1); /* note : it's safe to invoke PREFETCH() on any memory address, including invalid ones */\n            sequences[seqNb & STORED_SEQS_MASK] = sequence;\n            op += oneSeqSize;\n        }\n        RETURN_ERROR_IF(seqNb<nbSeq, corruption_detected, \"\");\n\n        /* finish queue */\n        seqNb -= seqAdvance;\n        for ( ; seqNb<nbSeq ; seqNb++) {\n            size_t const oneSeqSize = ZSTD_execSequence(op, oend, sequences[seqNb&STORED_SEQS_MASK], &litPtr, litEnd, prefixStart, dictStart, dictEnd);\n#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && defined(FUZZING_ASSERT_VALID_SEQUENCE)\n            assert(!ZSTD_isError(oneSeqSize));\n            if (frame) ZSTD_assertValidSequence(dctx, op, oend, sequences[seqNb&STORED_SEQS_MASK], prefixStart, dictStart);\n#endif\n            if (ZSTD_isError(oneSeqSize)) return oneSeqSize;\n            op += oneSeqSize;\n        }\n\n        /* save reps for next block */\n        { U32 i; for (i=0; i<ZSTD_REP_NUM; i++) dctx->entropy.rep[i] = (U32)(seqState.prevOffset[i]); }\n    }\n\n    /* last literal segment */\n    {   size_t const lastLLSize = litEnd - litPtr;\n        RETURN_ERROR_IF(lastLLSize > (size_t)(oend-op), dstSize_tooSmall, \"\");\n        if (op != NULL) {\n            memcpy(op, litPtr, lastLLSize);\n            op += lastLLSize;\n        }\n    }\n\n    return op-ostart;\n}\n\nstatic size_t\nZSTD_decompressSequencesLong_default(ZSTD_DCtx* dctx,\n                                 void* dst, size_t maxDstSize,\n                           const void* seqStart, size_t seqSize, int nbSeq,\n                           const ZSTD_longOffset_e isLongOffset,\n                           const int frame)\n{\n    return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n}\n#endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */\n\n\n\n#if DYNAMIC_BMI2\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG\nstatic TARGET_ATTRIBUTE(\"bmi2\") size_t\nDONT_VECTORIZE\nZSTD_decompressSequences_bmi2(ZSTD_DCtx* dctx,\n                                 void* dst, size_t maxDstSize,\n                           const void* seqStart, size_t seqSize, int nbSeq,\n                           const ZSTD_longOffset_e isLongOffset,\n                           const int frame)\n{\n    return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n}\n#endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT\nstatic TARGET_ATTRIBUTE(\"bmi2\") size_t\nZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx* dctx,\n                                 void* dst, size_t maxDstSize,\n                           const void* seqStart, size_t seqSize, int nbSeq,\n                           const ZSTD_longOffset_e isLongOffset,\n                           const int frame)\n{\n    return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n}\n#endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */\n\n#endif /* DYNAMIC_BMI2 */\n\ntypedef size_t (*ZSTD_decompressSequences_t)(\n                            ZSTD_DCtx* dctx,\n                            void* dst, size_t maxDstSize,\n                            const void* seqStart, size_t seqSize, int nbSeq,\n                            const ZSTD_longOffset_e isLongOffset,\n                            const int frame);\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG\nstatic size_t\nZSTD_decompressSequences(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize,\n                   const void* seqStart, size_t seqSize, int nbSeq,\n                   const ZSTD_longOffset_e isLongOffset,\n                   const int frame)\n{\n    DEBUGLOG(5, \"ZSTD_decompressSequences\");\n#if DYNAMIC_BMI2\n    if (dctx->bmi2) {\n        return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n    }\n#endif\n  return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n}\n#endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG */\n\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT\n/* ZSTD_decompressSequencesLong() :\n * decompression function triggered when a minimum share of offsets is considered \"long\",\n * aka out of cache.\n * note : \"long\" definition seems overloaded here, sometimes meaning \"wider than bitstream register\", and sometimes meaning \"farther than memory cache distance\".\n * This function will try to mitigate main memory latency through the use of prefetching */\nstatic size_t\nZSTD_decompressSequencesLong(ZSTD_DCtx* dctx,\n                             void* dst, size_t maxDstSize,\n                             const void* seqStart, size_t seqSize, int nbSeq,\n                             const ZSTD_longOffset_e isLongOffset,\n                             const int frame)\n{\n    DEBUGLOG(5, \"ZSTD_decompressSequencesLong\");\n#if DYNAMIC_BMI2\n    if (dctx->bmi2) {\n        return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n    }\n#endif\n  return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame);\n}\n#endif /* ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT */\n\n\n\n#if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \\\n    !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)\n/* ZSTD_getLongOffsetsShare() :\n * condition : offTable must be valid\n * @return : \"share\" of long offsets (arbitrarily defined as > (1<<23))\n *           compared to maximum possible of (1<<OffFSELog) */\nstatic unsigned\nZSTD_getLongOffsetsShare(const ZSTD_seqSymbol* offTable)\n{\n    const void* ptr = offTable;\n    U32 const tableLog = ((const ZSTD_seqSymbol_header*)ptr)[0].tableLog;\n    const ZSTD_seqSymbol* table = offTable + 1;\n    U32 const max = 1 << tableLog;\n    U32 u, total = 0;\n    DEBUGLOG(5, \"ZSTD_getLongOffsetsShare: (tableLog=%u)\", tableLog);\n\n    assert(max <= (1 << OffFSELog));  /* max not too large */\n    for (u=0; u<max; u++) {\n        if (table[u].nbAdditionalBits > 22) total += 1;\n    }\n\n    assert(tableLog <= OffFSELog);\n    total <<= (OffFSELog - tableLog);  /* scale to OffFSELog */\n\n    return total;\n}\n#endif\n\nsize_t\nZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,\n                              void* dst, size_t dstCapacity,\n                        const void* src, size_t srcSize, const int frame)\n{   /* blockType == blockCompressed */\n    const BYTE* ip = (const BYTE*)src;\n    /* isLongOffset must be true if there are long offsets.\n     * Offsets are long if they are larger than 2^STREAM_ACCUMULATOR_MIN.\n     * We don't expect that to be the case in 64-bit mode.\n     * In block mode, window size is not known, so we have to be conservative.\n     * (note: but it could be evaluated from current-lowLimit)\n     */\n    ZSTD_longOffset_e const isLongOffset = (ZSTD_longOffset_e)(MEM_32bits() && (!frame || (dctx->fParams.windowSize > (1ULL << STREAM_ACCUMULATOR_MIN))));\n    DEBUGLOG(5, \"ZSTD_decompressBlock_internal (size : %u)\", (U32)srcSize);\n\n    RETURN_ERROR_IF(srcSize >= ZSTD_BLOCKSIZE_MAX, srcSize_wrong, \"\");\n\n    /* Decode literals section */\n    {   size_t const litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);\n        DEBUGLOG(5, \"ZSTD_decodeLiteralsBlock : %u\", (U32)litCSize);\n        if (ZSTD_isError(litCSize)) return litCSize;\n        ip += litCSize;\n        srcSize -= litCSize;\n    }\n\n    /* Build Decoding Tables */\n    {\n        /* These macros control at build-time which decompressor implementation\n         * we use. If neither is defined, we do some inspection and dispatch at\n         * runtime.\n         */\n#if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \\\n    !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)\n        int usePrefetchDecoder = dctx->ddictIsCold;\n#endif\n        int nbSeq;\n        size_t const seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize);\n        if (ZSTD_isError(seqHSize)) return seqHSize;\n        ip += seqHSize;\n        srcSize -= seqHSize;\n\n        RETURN_ERROR_IF(dst == NULL && nbSeq > 0, dstSize_tooSmall, \"NULL not handled\");\n\n#if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \\\n    !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)\n        if ( !usePrefetchDecoder\n          && (!frame || (dctx->fParams.windowSize > (1<<24)))\n          && (nbSeq>ADVANCED_SEQS) ) {  /* could probably use a larger nbSeq limit */\n            U32 const shareLongOffsets = ZSTD_getLongOffsetsShare(dctx->OFTptr);\n            U32 const minShare = MEM_64bits() ? 7 : 20; /* heuristic values, correspond to 2.73% and 7.81% */\n            usePrefetchDecoder = (shareLongOffsets >= minShare);\n        }\n#endif\n\n        dctx->ddictIsCold = 0;\n\n#if !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT) && \\\n    !defined(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG)\n        if (usePrefetchDecoder)\n#endif\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT\n            return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset, frame);\n#endif\n\n#ifndef ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG\n        /* else */\n        return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset, frame);\n#endif\n    }\n}\n\n\nvoid ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst)\n{\n    if (dst != dctx->previousDstEnd) {   /* not contiguous */\n        dctx->dictEnd = dctx->previousDstEnd;\n        dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));\n        dctx->prefixStart = dst;\n        dctx->previousDstEnd = dst;\n    }\n}\n\n\nsize_t ZSTD_decompressBlock(ZSTD_DCtx* dctx,\n                            void* dst, size_t dstCapacity,\n                      const void* src, size_t srcSize)\n{\n    size_t dSize;\n    ZSTD_checkContinuity(dctx, dst);\n    dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, /* frame */ 0);\n    dctx->previousDstEnd = (char*)dst + dSize;\n    return dSize;\n}\n/**** ended inlining decompress/zstd_decompress_block.c ****/\n"
  },
  {
    "path": "manifest.json",
    "content": "{\n    \"name\": \"v86\",\n    \"short_name\": \"v86\",\n    \"start_url\": \"/v86/\",\n    \"display\": \"standalone\",\n    \"icons\": [\n        {\n            \"src\": \"192.png\",\n            \"sizes\": \"192x192\",\n            \"type\": \"image/png\"\n        }\n    ]\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"v86\",\n  \"version\": \"0.5\",\n  \"license\": \"BSD-2-Clause\",\n  \"description\": \"x86 PC emulator and x86-to-wasm JIT, running in the browser\",\n  \"homepage\": \"https://copy.sh/v86/\",\n  \"files\": [\n    \"Readme.md\",\n    \"LICENSE\",\n    \"v86.d.ts\",\n    \"build/libv86*.js\",\n    \"build/libv86*.js.map\",\n    \"build/*.mjs\",\n    \"build/v86*.wasm\"\n  ],\n  \"main\": \"build/libv86.mjs\",\n  \"types\": \"v86.d.ts\",\n  \"repository\": \"github:copy/v86\",\n  \"type\": \"module\"\n}\n"
  },
  {
    "path": "src/acpi.js",
    "content": "// http://www.uefi.org/sites/default/files/resources/ACPI_6_1.pdf\n\nimport { v86 } from \"./main.js\";\nimport { LOG_ACPI } from \"../src/const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_log, dbg_assert } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\n\nconst PMTIMER_FREQ_SECONDS = 3579545;\n\n/**\n * @constructor\n * @param {CPU} cpu\n */\nexport function ACPI(cpu)\n{\n    /** @type {CPU} */\n    this.cpu = cpu;\n\n    var io = cpu.io;\n\n    var acpi = {\n        pci_id: 0x07 << 3,\n        pci_space: [\n            0x86, 0x80, 0x13, 0x71, 0x07, 0x00, 0x80, 0x02, 0x08, 0x00, 0x80, 0x06, 0x00, 0x00, 0x80, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x01, 0x00, 0x00,\n        ],\n        pci_bars: [],\n        name: \"acpi\",\n    };\n\n    // 00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)\n    cpu.devices.pci.register_device(acpi);\n\n    this.timer_last_value = 0;\n    this.timer_imprecision_offset = 0;\n\n    this.status = 1;\n    this.pm1_status = 0;\n    this.pm1_enable = 0;\n    this.last_timer = this.get_timer(v86.microtick());\n\n    this.gpe = new Uint8Array(4);\n\n    io.register_read(0xB000, this, undefined, function()\n    {\n        dbg_log(\"ACPI pm1_status read\", LOG_ACPI);\n        return this.pm1_status;\n    });\n    io.register_write(0xB000, this, undefined, function(value)\n    {\n        dbg_log(\"ACPI pm1_status write: \" + h(value, 4), LOG_ACPI);\n        this.pm1_status &= ~value;\n    });\n\n    io.register_read(0xB002, this, undefined, function()\n    {\n        dbg_log(\"ACPI pm1_enable read\", LOG_ACPI);\n        return this.pm1_enable;\n    });\n    io.register_write(0xB002, this, undefined, function(value)\n    {\n        dbg_log(\"ACPI pm1_enable write: \" + h(value), LOG_ACPI);\n        this.pm1_enable = value;\n    });\n\n    // ACPI status\n    io.register_read(0xB004, this, function()\n    {\n        dbg_log(\"ACPI status read8\", LOG_ACPI);\n        return this.status & 0xFF;\n    }, function()\n    {\n        dbg_log(\"ACPI status read\", LOG_ACPI);\n        return this.status;\n    });\n    io.register_write(0xB004, this, undefined, function(value)\n    {\n        dbg_log(\"ACPI status write: \" + h(value), LOG_ACPI);\n        this.status = value;\n    });\n\n    // ACPI, pmtimer\n    io.register_read(0xB008, this, undefined, undefined, function()\n    {\n        var value = this.get_timer(v86.microtick()) & 0xFFFFFF;\n        //dbg_log(\"pmtimer read: \" + h(value >>> 0), LOG_ACPI);\n        return value;\n    });\n\n    // ACPI, gpe\n    io.register_read(0xAFE0, this, function()\n    {\n        dbg_log(\"Read gpe#0\", LOG_ACPI);\n        return this.gpe[0];\n    });\n    io.register_read(0xAFE1, this, function()\n    {\n        dbg_log(\"Read gpe#1\", LOG_ACPI);\n        return this.gpe[1];\n    });\n    io.register_read(0xAFE2, this, function()\n    {\n        dbg_log(\"Read gpe#2\", LOG_ACPI);\n        return this.gpe[2];\n    });\n    io.register_read(0xAFE3, this, function()\n    {\n        dbg_log(\"Read gpe#3\", LOG_ACPI);\n        return this.gpe[3];\n    });\n\n    io.register_write(0xAFE0, this, function(value)\n    {\n        dbg_log(\"Write gpe#0: \" + h(value), LOG_ACPI);\n        this.gpe[0] = value;\n    });\n    io.register_write(0xAFE1, this, function(value)\n    {\n        dbg_log(\"Write gpe#1: \" + h(value), LOG_ACPI);\n        this.gpe[1] = value;\n    });\n    io.register_write(0xAFE2, this, function(value)\n    {\n        dbg_log(\"Write gpe#2: \" + h(value), LOG_ACPI);\n        this.gpe[2] = value;\n    });\n    io.register_write(0xAFE3, this, function(value)\n    {\n        dbg_log(\"Write gpe#3: \" + h(value), LOG_ACPI);\n        this.gpe[3] = value;\n    });\n}\n\nACPI.prototype.timer = function(now)\n{\n    var timer = this.get_timer(now);\n    var highest_bit_changed = ((timer ^ this.last_timer) & (1 << 23)) !== 0;\n\n    if((this.pm1_enable & 1) && highest_bit_changed)\n    {\n        dbg_log(\"ACPI raise irq\", LOG_ACPI);\n        this.pm1_status |= 1;\n        this.cpu.device_raise_irq(9);\n    }\n    else\n    {\n        this.cpu.device_lower_irq(9);\n    }\n\n    this.last_timer = timer;\n    return 100; // TODO\n};\n\nACPI.prototype.get_timer = function(now)\n{\n    const t = Math.round(now * (PMTIMER_FREQ_SECONDS / 1000));\n\n    // Due to the low precision of JavaScript's time functions we increment the\n    // returned timer value every time it is read\n\n    if(t === this.timer_last_value)\n    {\n        // don't go past 1ms\n\n        if(this.timer_imprecision_offset < PMTIMER_FREQ_SECONDS / 1000)\n        {\n            this.timer_imprecision_offset++;\n        }\n    }\n    else\n    {\n        dbg_assert(t > this.timer_last_value);\n\n        const previous_timer = this.timer_last_value + this.timer_imprecision_offset;\n\n        // don't go back in time\n\n        if(previous_timer <= t)\n        {\n            this.timer_imprecision_offset = 0;\n            this.timer_last_value = t;\n        }\n        else\n        {\n            dbg_log(\"Warning: Overshot pmtimer, waiting;\" +\n                    \" current=\" + t +\n                    \" last=\" + this.timer_last_value +\n                    \" offset=\" + this.timer_imprecision_offset, LOG_ACPI);\n        }\n    }\n\n    return this.timer_last_value + this.timer_imprecision_offset;\n};\n\nACPI.prototype.get_state = function()\n{\n    var state = [];\n    state[0] = this.status;\n    state[1] = this.pm1_status;\n    state[2] = this.pm1_enable;\n    state[3] = this.gpe;\n    return state;\n};\n\nACPI.prototype.set_state = function(state)\n{\n    this.status = state[0];\n    this.pm1_status = state[1];\n    this.pm1_enable = state[2];\n    this.gpe = state[3];\n};\n"
  },
  {
    "path": "src/browser/dummy_screen.js",
    "content": "import { dbg_assert } from \"../log.js\";\nimport { get_charmap } from \"../lib.js\";\n\n/**\n * @constructor\n * @param {Object=} options\n */\nexport function DummyScreenAdapter(options)\n{\n    var\n        graphic_image_data,\n\n        /** @type {number} */\n        cursor_row = 0,\n\n        /** @type {number} */\n        cursor_col = 0,\n\n        graphical_mode_width = 0,\n        graphical_mode_height = 0,\n\n        // are we in graphical mode now?\n        is_graphical = false,\n\n        // Index 0: ASCII code\n        // Index 1: Blinking\n        // Index 2: Background color\n        // Index 3: Foreground color\n        text_mode_data,\n\n        // number of columns\n        text_mode_width = 0,\n\n        // number of rows\n        text_mode_height = 0,\n\n        // 8-bit-text to Unicode character map\n        charmap = get_charmap(options?.encoding);\n\n    this.put_char = function(row, col, chr, blinking, bg_color, fg_color)\n    {\n        dbg_assert(row >= 0 && row < text_mode_height);\n        dbg_assert(col >= 0 && col < text_mode_width);\n        text_mode_data[row * text_mode_width + col] = chr;\n    };\n\n    this.destroy = function() {};\n    this.pause = function() {};\n    this.continue = function() {};\n\n    this.set_mode = function(graphical)\n    {\n        is_graphical = graphical;\n    };\n\n    this.set_font_bitmap = function(height, width_9px, width_dbl, copy_8th_col, bitmap, bitmap_changed)\n    {\n    };\n\n    this.set_font_page = function(page_a, page_b)\n    {\n    };\n\n    this.clear_screen = function()\n    {\n    };\n\n    /**\n     * @param {number} cols\n     * @param {number} rows\n     */\n    this.set_size_text = function(cols, rows)\n    {\n        if(cols === text_mode_width && rows === text_mode_height)\n        {\n            return;\n        }\n\n        text_mode_data = new Uint8Array(cols * rows);\n        text_mode_width = cols;\n        text_mode_height = rows;\n    };\n\n    this.set_size_graphical = function(width, height)\n    {\n        graphical_mode_width = width;\n        graphical_mode_height = height;\n    };\n\n    this.set_scale = function(s_x, s_y)\n    {\n    };\n\n    this.update_cursor_scanline = function(start, end, max)\n    {\n    };\n\n    this.update_cursor = function(row, col)\n    {\n        cursor_row = row;\n        cursor_col = col;\n    };\n\n    this.update_buffer = function(layers)\n    {\n    };\n\n    this.get_text_screen = function()\n    {\n        var screen = [];\n\n        for(var i = 0; i < text_mode_height; i++)\n        {\n            screen.push(this.get_text_row(i));\n        }\n\n        return screen;\n    };\n\n    this.get_text_row = function(y)\n    {\n        const begin = y * text_mode_width;\n        const end = begin + text_mode_width;\n        return Array.from(text_mode_data.subarray(begin, end), chr => charmap[chr]).join(\"\");\n    };\n\n    this.set_size_text(80, 25);\n}\n"
  },
  {
    "path": "src/browser/fake_network.js",
    "content": "import { LOG_FETCH } from \"../const.js\";\nimport { h } from \"../lib.js\";\nimport { dbg_assert, dbg_log } from \"../log.js\";\n\n// https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml\nconst ETHERTYPE_IPV4 = 0x0800;\nconst ETHERTYPE_ARP = 0x0806;\nconst ETHERTYPE_IPV6 = 0x86DD;\n\nconst IPV4_PROTO_ICMP = 1;\nconst IPV4_PROTO_TCP = 6;\nconst IPV4_PROTO_UDP = 17;\n\nconst UNIX_EPOCH = new Date(\"1970-01-01T00:00:00Z\").getTime();\nconst NTP_EPOCH = new Date(\"1900-01-01T00:00:00Z\").getTime();\nconst NTP_EPOC_DIFF = UNIX_EPOCH - NTP_EPOCH;\nconst TWO_TO_32 = Math.pow(2, 32);\n\nconst DHCP_MAGIC_COOKIE = 0x63825363;\nconst V86_ASCII = [118, 56, 54];\n\n/* For the complete TCP state diagram see:\n *\n *   https://en.wikipedia.org/wiki/File:Tcp_state_diagram_fixed_new.svg\n *\n * State TIME_WAIT is not needed, we can skip it and transition directly to CLOSED instead.\n */\nexport const TCP_STATE_CLOSED = \"closed\";\nexport const TCP_STATE_SYN_RECEIVED = \"syn-received\";\nexport const TCP_STATE_SYN_SENT = \"syn-sent\";\nexport const TCP_STATE_SYN_PROBE = \"syn-probe\";\n//const TCP_STATE_LISTEN = \"listen\";\nexport const TCP_STATE_ESTABLISHED = \"established\";\nexport const TCP_STATE_FIN_WAIT_1 = \"fin-wait-1\";\nexport const TCP_STATE_CLOSE_WAIT = \"close-wait\";\nexport const TCP_STATE_FIN_WAIT_2 = \"fin-wait-2\";\nexport const TCP_STATE_LAST_ACK = \"last-ack\";\nexport const TCP_STATE_CLOSING = \"closing\";\n//const TCP_STATE_TIME_WAIT = \"time-wait\";\n\n// source: RFC6335, 6. Port Number Ranges\nconst TCP_DYNAMIC_PORT_START = 49152;\nconst TCP_DYNAMIC_PORT_END   = 65535;\nconst TCP_DYNAMIC_PORT_RANGE = TCP_DYNAMIC_PORT_END - TCP_DYNAMIC_PORT_START;\n\nconst ETH_HEADER_SIZE     = 14;\nconst ETH_PAYLOAD_OFFSET  = ETH_HEADER_SIZE;\nconst MTU_DEFAULT         = 1500;\nconst ETH_TRAILER_SIZE    = 4;\nconst IPV4_HEADER_SIZE    = 20;\nconst IPV4_PAYLOAD_OFFSET = ETH_PAYLOAD_OFFSET + IPV4_HEADER_SIZE;\nconst UDP_HEADER_SIZE     = 8;\nconst UDP_PAYLOAD_OFFSET  = IPV4_PAYLOAD_OFFSET + UDP_HEADER_SIZE;\nconst TCP_HEADER_SIZE     = 20;\nconst TCP_PAYLOAD_OFFSET  = IPV4_PAYLOAD_OFFSET + TCP_HEADER_SIZE;\nconst ICMP_HEADER_SIZE    = 4;\n\nconst DEFAULT_DOH_SERVER = \"cloudflare-dns.com\";\n\nfunction a2ethaddr(bytes) {\n    return [0,1,2,3,4,5].map((i) => bytes[i].toString(16)).map(x => x.length === 1 ? \"0\" + x : x).join(\":\");\n}\n\nfunction iptolong(parts) {\n    return parts[0] << 24 | parts[1] << 16 | parts[2] << 8 | parts[3];\n}\n\nclass GrowableRingbuffer\n{\n    /**\n     * @param {number} initial_capacity\n     * @param {number} maximum_capacity\n     */\n    constructor(initial_capacity, maximum_capacity)\n    {\n        initial_capacity = Math.min(initial_capacity, 16);\n        this.maximum_capacity = maximum_capacity ? Math.max(maximum_capacity, initial_capacity) : 0;\n        this.tail = 0;\n        this.head = 0;\n        this.length = 0;\n        this.buffer = new Uint8Array(initial_capacity);\n    }\n\n    /**\n     * @param {Uint8Array} src_array\n     */\n    write(src_array)\n    {\n        const src_length = src_array.length;\n        const total_length = this.length + src_length;\n        let capacity = this.buffer.length;\n        if(capacity < total_length) {\n            dbg_assert(capacity > 0);\n            while(capacity < total_length) {\n                capacity *= 2;\n            }\n            if(this.maximum_capacity && capacity > this.maximum_capacity) {\n                throw new Error(\"stream capacity overflow in GrowableRingbuffer.write(), package dropped\");\n            }\n            const new_buffer = new Uint8Array(capacity);\n            this.peek(new_buffer);\n            this.tail = 0;\n            this.head = this.length;\n            this.buffer = new_buffer;\n        }\n        const buffer = this.buffer;\n\n        const new_head = this.head + src_length;\n        if(new_head > capacity) {\n            const i_split = capacity - this.head;\n            buffer.set(src_array.subarray(0, i_split), this.head);\n            buffer.set(src_array.subarray(i_split));\n        }\n        else {\n            buffer.set(src_array, this.head);\n        }\n        this.head = new_head % capacity;\n        this.length += src_length;\n    }\n\n    /**\n     * @param {Uint8Array} dst_array\n     */\n    peek(dst_array)\n    {\n        const length = Math.min(this.length, dst_array.length);\n        if(length) {\n            const buffer = this.buffer;\n            const capacity = buffer.length;\n            const new_tail = this.tail + length;\n            if(new_tail > capacity) {\n                const buf_len_left = new_tail % capacity;\n                const buf_len_right = capacity - this.tail;\n                dst_array.set(buffer.subarray(this.tail));\n                dst_array.set(buffer.subarray(0, buf_len_left), buf_len_right);\n            }\n            else {\n                dst_array.set(buffer.subarray(this.tail, new_tail));\n            }\n        }\n        return length;\n    }\n\n    /**\n     * @param {number} length\n     */\n    remove(length)\n    {\n        if(length > this.length) {\n            length = this.length;\n        }\n        if(length) {\n            this.tail = (this.tail + length) % this.buffer.length;\n            this.length -= length;\n        }\n        return length;\n    }\n}\n\nexport function create_eth_encoder_buf(mtu = MTU_DEFAULT)\n{\n    const ETH_FRAME_SIZE = ETH_HEADER_SIZE + mtu + ETH_TRAILER_SIZE;\n    const IPV4_PAYLOAD_SIZE = mtu - IPV4_HEADER_SIZE;\n    const UDP_PAYLOAD_SIZE = IPV4_PAYLOAD_SIZE - UDP_HEADER_SIZE;\n\n    const eth_frame = new Uint8Array(ETH_FRAME_SIZE);\n    const buffer = eth_frame.buffer;\n    const offset = eth_frame.byteOffset;\n    return {\n        eth_frame: eth_frame,\n        eth_frame_view: new DataView(buffer),\n        eth_payload_view: new DataView(buffer, offset + ETH_PAYLOAD_OFFSET, mtu),\n        ipv4_payload_view: new DataView(buffer, offset + IPV4_PAYLOAD_OFFSET, IPV4_PAYLOAD_SIZE),\n        udp_payload_view: new DataView(buffer, offset + UDP_PAYLOAD_OFFSET, UDP_PAYLOAD_SIZE),\n        text_encoder: new TextEncoder()\n    };\n}\n\n/**\n * Copy given data array into view starting at offset, return number of bytes written.\n *\n * @param {number} offset\n * @param {ArrayBuffer|ArrayBufferView} data\n * @param {DataView} view\n * @param {Object} out\n */\nfunction view_set_array(offset, data, view, out)\n{\n    out.eth_frame.set(data, view.byteOffset + offset);\n    return data.length;\n}\n\n/**\n * Write zeros into the view starting at offset\n * @param {number} offset\n * @param {number} length\n * @param {DataView} view\n */\nfunction view_set_zeros(offset, length, view, out)\n{\n    out.eth_frame.fill(0, view.byteOffset + offset, view.byteOffset + offset + length);\n}\n\n/**\n * UTF8-encode given string into view starting at offset, return number of bytes written.\n *\n * @param {number} offset\n * @param {string} str\n * @param {DataView} view\n * @param {Object} out\n */\nfunction view_set_string(offset, str, view, out)\n{\n    return out.text_encoder.encodeInto(str, out.eth_frame.subarray(view.byteOffset + offset)).written;\n}\n\n/**\n * Calculate internet checksum for view[0 : length] and return the 16-bit result.\n * Source: RFC768 and RFC1071 (chapter 4.1).\n *\n * @param {number} length\n * @param {number} checksum\n * @param {DataView} view\n * @param {Object} out\n */\nfunction calc_inet_checksum(length, checksum, view, out)\n{\n    const uint16_end = view.byteOffset + (length & ~1);\n    const eth_frame = out.eth_frame;\n    for(let i = view.byteOffset; i < uint16_end; i += 2) {\n        checksum += eth_frame[i] << 8 | eth_frame[i+1];\n    }\n    if(length & 1) {\n        checksum += eth_frame[uint16_end] << 8;\n    }\n    while(checksum >>> 16) {\n        checksum = (checksum & 0xffff) + (checksum >>> 16);\n    }\n    return ~checksum & 0xffff;\n}\n\n/**\n * @param {Object} out\n * @param {Object} spec\n */\nfunction make_packet(out, spec)\n{\n    dbg_assert(spec.eth);\n    out.eth_frame.fill(0);\n    return out.eth_frame.subarray(0, write_eth(spec, out));\n}\n\nfunction handle_fake_tcp(packet, adapter)\n{\n    const tuple = `${packet.ipv4.src.join(\".\")}:${packet.tcp.sport}:${packet.ipv4.dest.join(\".\")}:${packet.tcp.dport}`;\n\n    if(packet.tcp.syn && !packet.tcp.ack) {\n        if(adapter.tcp_conn[tuple]) {\n            dbg_log(\"SYN to already opened port\", LOG_FETCH);\n            delete adapter.tcp_conn[tuple];\n        }\n\n        let conn = new TCPConnection(adapter);\n        conn.state = TCP_STATE_SYN_RECEIVED;\n        conn.tuple = tuple;\n        conn.last = packet;\n\n        conn.hsrc = packet.eth.dest;\n        conn.psrc = packet.ipv4.dest;\n        conn.sport = packet.tcp.dport;\n        conn.hdest = packet.eth.src;\n        conn.dport = packet.tcp.sport;\n        conn.pdest = packet.ipv4.src;\n\n        adapter.bus.pair.send(\"tcp-connection\", conn);\n\n        if(adapter.on_tcp_connection) {\n            adapter.on_tcp_connection(conn, packet);\n        }\n        if(adapter.tcp_conn[tuple]) return;\n    }\n\n    if(!adapter.tcp_conn[tuple]) {\n        dbg_log(`I dont know about ${tuple}, so resetting`, LOG_FETCH);\n        let bop = packet.tcp.ackn;\n        if(packet.tcp.fin || packet.tcp.syn) bop += 1;\n        let reply = {};\n        reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };\n        reply.ipv4 = {\n            proto: IPV4_PROTO_TCP,\n            src: packet.ipv4.dest,\n            dest: packet.ipv4.src\n        };\n        reply.tcp = {\n            sport: packet.tcp.dport,\n            dport: packet.tcp.sport,\n            seq: bop,\n            ackn: packet.tcp.seq + (packet.tcp.syn ? 1: 0),\n            winsize: packet.tcp.winsize,\n            rst: true,\n            ack: packet.tcp.syn\n        };\n        adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n        return true;\n    }\n\n    adapter.tcp_conn[tuple].process(packet);\n}\n\nfunction handle_fake_dns_static(packet, adapter)\n{\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };\n    reply.ipv4 = {\n        proto: IPV4_PROTO_UDP,\n        src: adapter.router_ip,\n        dest: packet.ipv4.src,\n    };\n    reply.udp = { sport: 53, dport: packet.udp.sport };\n\n    let answers = [];\n    let flags = 0x8000; //Response,\n    flags |= 0x0180; // Recursion\n    // flags |= 0x0400; Authoritative\n\n    for(let i = 0; i < packet.dns.questions.length; ++i) {\n        let q = packet.dns.questions[i];\n\n        switch(q.type){\n            case 1: // A record\n                answers.push({\n                    name: q.name,\n                    type: q.type,\n                    class: q.class,\n                    ttl: 600,\n                    data: [192, 168, 87, 1]\n                });\n                break;\n            default:\n        }\n    }\n\n    reply.dns = {\n        id: packet.dns.id,\n        flags: flags,\n        questions: packet.dns.questions,\n        answers: answers\n    };\n    adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n    return true;\n}\n\nfunction handle_fake_dns_doh(packet, adapter)\n{\n    const fetch_url = `https://${adapter.doh_server || DEFAULT_DOH_SERVER}/dns-query`;\n    const fetch_opts = {\n        method: \"POST\",\n        headers: [[\"content-type\", \"application/dns-message\"]],\n        body: packet.udp.data\n    };\n    fetch(fetch_url, fetch_opts).then(async (resp) => {\n        const reply = {\n            eth: {\n                ethertype: ETHERTYPE_IPV4,\n                src: adapter.router_mac,\n                dest: packet.eth.src\n            },\n            ipv4: {\n                proto: IPV4_PROTO_UDP,\n                src: adapter.router_ip,\n                dest: packet.ipv4.src\n            },\n            udp: {\n                sport: 53,\n                dport: packet.udp.sport,\n                data: new Uint8Array(await resp.arrayBuffer())\n            }\n        };\n        adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n    });\n    return true;\n}\n\nfunction handle_fake_dns(packet, adapter)\n{\n    if(adapter.dns_method === \"static\") {\n        return handle_fake_dns_static(packet, adapter);\n    }\n    else {\n        return handle_fake_dns_doh(packet, adapter);\n    }\n}\n\nfunction handle_fake_ntp(packet, adapter) {\n    let now = Date.now(); // - 1000 * 60 * 60 * 24 * 7;\n    let now_n = now + NTP_EPOC_DIFF;\n    let now_n_f = TWO_TO_32 * ((now_n % 1000) / 1000);\n\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };\n    reply.ipv4 = {\n        proto: IPV4_PROTO_UDP,\n        src: packet.ipv4.dest,\n        dest: packet.ipv4.src,\n    };\n    reply.udp = { sport: 123, dport: packet.udp.sport };\n    let flags = (0 << 6) | (4 << 3) | 4;\n    reply.ntp = Object.assign({}, packet.ntp);\n    reply.ntp.flags = flags;\n    reply.ntp.poll = 10;\n    reply.ntp.ori_ts_i = packet.ntp.trans_ts_i;\n    reply.ntp.ori_ts_f = packet.ntp.trans_ts_f;\n\n    reply.ntp.rec_ts_i = now_n / 1000;\n    reply.ntp.rec_ts_f = now_n_f;\n\n    reply.ntp.trans_ts_i = now_n / 1000;\n    reply.ntp.trans_ts_f = now_n_f;\n\n    reply.ntp.stratum = 2;\n    adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n    return true;\n}\n\nfunction handle_fake_dhcp(packet, adapter) {\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };\n    reply.ipv4 = {\n        proto: IPV4_PROTO_UDP,\n        src: adapter.router_ip,\n        dest: adapter.vm_ip,\n    };\n    reply.udp = { sport: 67, dport: 68, };\n    reply.dhcp = {\n        htype: 1,\n        hlen: 6,\n        hops: 0,\n        xid: packet.dhcp.xid,\n        secs: 0,\n        flags: 0,\n        ciaddr: 0,\n        yiaddr: iptolong(adapter.vm_ip),\n        siaddr: iptolong(adapter.router_ip),\n        giaddr: iptolong(adapter.router_ip),\n        chaddr: packet.dhcp.chaddr,\n    };\n\n    let options = [];\n\n    // idk, it seems like op should be 3, but udhcpc sends 1\n    let fix = packet.dhcp.options.find(function(x) { return x[0] === 53; });\n    if( fix && fix[2] === 3 ) packet.dhcp.op = 3;\n\n    if(packet.dhcp.op === 1) {\n        reply.dhcp.op = 2;\n        options.push(new Uint8Array([53, 1, 2]));\n    }\n\n    if(packet.dhcp.op === 3) {\n        reply.dhcp.op = 2;\n        options.push(new Uint8Array([53, 1, 5]));\n        options.push(new Uint8Array([51, 4, 8, 0, 0, 0]));  // Lease Time\n    }\n\n    let router_ip = [adapter.router_ip[0], adapter.router_ip[1], adapter.router_ip[2], adapter.router_ip[3]];\n    options.push(new Uint8Array([1, 4, 255, 255, 255, 0])); // Netmask\n    if(adapter.masquerade) {\n        options.push(new Uint8Array([3, 4].concat(router_ip))); // Router\n        options.push(new Uint8Array([6, 4].concat(router_ip))); // DNS\n    }\n    options.push(new Uint8Array([54, 4].concat(router_ip))); // DHCP Server\n    options.push(new Uint8Array([60, 3].concat(V86_ASCII))); // Vendor\n    options.push(new Uint8Array([255, 0]));\n\n    reply.dhcp.options = options;\n    adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n}\n\nexport function handle_fake_networking(data, adapter) {\n    let packet = {};\n    parse_eth(data, packet);\n\n    if(packet.ipv4) {\n        if(packet.tcp) {\n            handle_fake_tcp(packet, adapter);\n        }\n        else if(packet.udp) {\n            if(packet.dns) {\n                handle_fake_dns(packet, adapter);\n            }\n            else if(packet.dhcp) {\n                handle_fake_dhcp(packet, adapter);\n            }\n            else if(packet.ntp) {\n                handle_fake_ntp(packet, adapter);\n            }\n            else if(packet.udp.dport === 8) {\n                handle_udp_echo(packet, adapter);\n            }\n        }\n        else if(packet.icmp && packet.icmp.type === 8) {\n            handle_fake_ping(packet, adapter);\n        }\n    }\n    else if(packet.arp && packet.arp.oper === 1 && packet.arp.ptype === ETHERTYPE_IPV4) {\n        arp_whohas(packet, adapter);\n    }\n}\n\nfunction parse_eth(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\n    let ethertype = view.getUint16(12);\n    let eth = {\n        ethertype: ethertype,\n        dest: data.subarray(0, 6),\n        dest_s: a2ethaddr(data.subarray(0, 6)),\n        src: data.subarray(6, 12),\n        src_s: a2ethaddr(data.subarray(6, 12)),\n    };\n\n    o.eth = eth;\n\n    // TODO: Remove CRC from the end of the packet maybe?\n    let payload = data.subarray(ETH_HEADER_SIZE, data.length);\n\n    if(ethertype === ETHERTYPE_IPV4) {\n        parse_ipv4(payload, o);\n    }\n    else if(ethertype === ETHERTYPE_ARP) {\n        parse_arp(payload, o);\n    }\n    else if(ethertype === ETHERTYPE_IPV6) {\n        dbg_log(\"Unimplemented: ipv6\");\n    }\n    else {\n        dbg_log(\"Unknown ethertype: \" + h(ethertype), LOG_FETCH);\n    }\n}\n\nfunction write_eth(spec, out) {\n    const view = out.eth_frame_view;\n    view_set_array(0, spec.eth.dest, view, out);\n    view_set_array(6, spec.eth.src, view, out);\n    view.setUint16(12, spec.eth.ethertype);\n    let len = ETH_HEADER_SIZE;\n    if(spec.arp) {\n        len += write_arp(spec, out);\n    }\n    else if(spec.ipv4) {\n        len += write_ipv4(spec, out);\n    }\n    return len;\n}\n\nfunction parse_arp(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n    let hlen = data[4];\n    let plen = data[5];\n\n    let arp = {\n        htype: view.getUint16(0),\n        ptype: view.getUint16(2),\n        oper: view.getUint16(6),\n        sha: data.subarray(8, 14),\n        spa: data.subarray(14, 18),\n        tha: data.subarray(18, 24),\n        tpa: data.subarray(24, 28),\n    };\n    o.arp = arp;\n}\n\nfunction write_arp(spec, out) {\n    const view = out.eth_payload_view;\n    view.setUint16(0, spec.arp.htype);\n    view.setUint16(2, spec.arp.ptype);\n    view.setUint8(4, spec.arp.sha.length);\n    view.setUint8(5, spec.arp.spa.length);\n    view.setUint16(6, spec.arp.oper);\n    view_set_array(8, spec.arp.sha, view, out);\n    view_set_array(14, spec.arp.spa, view, out);\n    view_set_array(18, spec.arp.tha, view, out);\n    view_set_array(24, spec.arp.tpa, view, out);\n    return 28;\n}\n\nfunction parse_ipv4(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\n    let version = (data[0] >> 4) & 0x0F;\n    let ihl = data[0] & 0x0F;\n\n    let tos = view.getUint8(1);\n    let len = view.getUint16(2);\n\n    let ttl = view.getUint8(8);\n    let proto = view.getUint8(9);\n    let ip_checksum = view.getUint16(10);\n\n    let ipv4 = {\n        version,\n        ihl,\n        tos,\n        len,\n        ttl,\n        proto,\n        ip_checksum,\n        src: data.subarray(12, 12+4),\n        dest: data.subarray(16, 16+4),\n    };\n\n    // Ethernet minmum packet size.\n    if(Math.max(len, 46) !== data.length) {\n        dbg_log(`ipv4 Length mismatch: ${len} != ${data.length}`, LOG_FETCH);\n    }\n\n    o.ipv4 = ipv4;\n    let ipdata = data.subarray(ihl * 4, len);\n    if(proto === IPV4_PROTO_ICMP) {\n        parse_icmp(ipdata, o);\n    }\n    else if(proto === IPV4_PROTO_TCP) {\n        parse_tcp(ipdata, o);\n    }\n    else if(proto === IPV4_PROTO_UDP) {\n        parse_udp(ipdata, o);\n    }\n}\n\nfunction write_ipv4(spec, out) {\n    const view = out.eth_payload_view;\n    const ihl = IPV4_HEADER_SIZE >> 2; // header length in 32-bit words\n    const version = 4;\n\n    let len = IPV4_HEADER_SIZE;\n    if(spec.icmp) {\n        len += write_icmp(spec, out);\n    }\n    else if(spec.udp) {\n        len += write_udp(spec, out);\n    }\n    else if(spec.tcp) {\n        len += write_tcp(spec, out);\n    }\n\n    view.setUint8(0, version << 4 | (ihl & 0x0F));\n    view.setUint8(1, spec.ipv4.tos || 0);\n    view.setUint16(2, len);\n    view.setUint16(4, spec.ipv4.id || 0);\n    view.setUint8(6, 2 << 5); // DF Flag\n    view.setUint8(8, spec.ipv4.ttl || 32);\n    view.setUint8(9, spec.ipv4.proto);\n    view.setUint16(10, 0); // checksum initially zero before calculation\n    view_set_array(12, spec.ipv4.src, view, out);\n    view_set_array(16, spec.ipv4.dest, view, out);\n    view.setUint16(10, calc_inet_checksum(IPV4_HEADER_SIZE, 0, view, out));\n    return len;\n}\n\nfunction parse_icmp(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n    let icmp = {\n        type: view.getUint8(0),\n        code: view.getUint8(1),\n        checksum: view.getUint16(2),\n        data: data.subarray(4)\n    };\n    o.icmp = icmp;\n}\n\nfunction write_icmp(spec, out) {\n    const view = out.ipv4_payload_view;\n    view.setUint8(0, spec.icmp.type);\n    view.setUint8(1, spec.icmp.code);\n    view.setUint16(2, 0); // checksum initially zero before calculation\n    const data_length = view_set_array(ICMP_HEADER_SIZE, spec.icmp.data, view, out);\n    const total_length = ICMP_HEADER_SIZE + data_length;\n    view.setUint16(2, calc_inet_checksum(total_length, 0, view, out));\n    return total_length;\n}\n\nfunction parse_udp(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n    let udp = {\n        sport: view.getUint16(0),\n        dport: view.getUint16(2),\n        len: view.getUint16(4),\n        checksum: view.getUint16(6),\n        data: data.subarray(8),\n        data_s: new TextDecoder().decode(data.subarray(8))\n    };\n\n    //dbg_assert(udp.data.length + 8 == udp.len);\n    if(udp.dport === 67 || udp.sport === 67) { //DHCP\n        parse_dhcp(data.subarray(8), o);\n    }\n    else if(udp.dport === 53 || udp.sport === 53) {\n        parse_dns(data.subarray(8), o);\n    }\n    else if(udp.dport === 123) {\n        parse_ntp(data.subarray(8), o);\n    }\n    o.udp = udp;\n}\n\nfunction write_udp(spec, out) {\n    const view = out.ipv4_payload_view;\n    let total_length = UDP_HEADER_SIZE;\n    if(spec.dhcp) {\n        total_length += write_dhcp(spec, out);\n    }\n    else if(spec.dns) {\n        total_length += write_dns(spec, out);\n    }\n    else if(spec.ntp) {\n        total_length += write_ntp(spec, out);\n    }\n    else {\n        total_length += view_set_array(0, spec.udp.data, out.udp_payload_view, out);\n    }\n\n    view.setUint16(0, spec.udp.sport);\n    view.setUint16(2, spec.udp.dport);\n    view.setUint16(4, total_length);\n    view.setUint16(6, 0); // checksum initially zero before calculation\n\n    const pseudo_header =\n        (spec.ipv4.src[0] << 8 | spec.ipv4.src[1]) +\n        (spec.ipv4.src[2] << 8 | spec.ipv4.src[3]) +\n        (spec.ipv4.dest[0] << 8 | spec.ipv4.dest[1]) +\n        (spec.ipv4.dest[2] << 8 | spec.ipv4.dest[3]) +\n        IPV4_PROTO_UDP +\n        total_length;\n    view.setUint16(6, calc_inet_checksum(total_length, pseudo_header, view, out));\n    return total_length;\n}\n\nfunction parse_dns(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n    let dns = {\n        id: view.getUint16(0),\n        flags: view.getUint16(2),\n        questions: [],\n        answers: []\n    };\n\n    let qdcount = view.getUint16(4);\n    let ancount = view.getUint16(6);\n    let nscount = view.getUint16(8);\n    let arcount = view.getUint16(10);\n\n    let offset = 12;\n    function read_dstr() {\n        let o = [];\n        let len;\n        do {\n            len = view.getUint8(offset);\n            o.push(new TextDecoder().decode(data.subarray(offset+1, offset+1+len)));\n            offset += len + 1;\n        } while(len > 0);\n        return o;\n    }\n\n    for(let i = 0; i < qdcount; i++) {\n        dns.questions.push({\n            name: read_dstr(),\n            type: view.getInt16(offset),\n            class: view.getInt16(offset + 2)\n        });\n        offset += 4;\n    }\n    for(let i = 0; i < ancount; i++) {\n        let ans = {\n            name: read_dstr(),\n            type: view.getInt16(offset),\n            class: view.getUint16(offset + 2),\n            ttl: view.getUint32(offset + 4)\n        };\n        offset += 8;\n        let rdlen = view.getUint16(offset);\n        offset += 2;\n        ans.data = data.subarray(offset, offset+rdlen);\n        offset += rdlen;\n        dns.answers.push(ans);\n    }\n    o.dns = dns;\n}\n\nfunction write_dns(spec, out) {\n    const view = out.udp_payload_view;\n    view.setUint16(0, spec.dns.id);\n    view.setUint16(2, spec.dns.flags);\n    view.setUint16(4, spec.dns.questions.length);\n    view.setUint16(6, spec.dns.answers.length);\n\n    let offset = 12;\n    for(let i = 0; i < spec.dns.questions.length; ++i) {\n        let q = spec.dns.questions[i];\n        for(let s of q.name) {\n            const n_written = view_set_string(offset + 1, s, view, out);\n            view.setUint8(offset, n_written);\n            offset += 1 + n_written;\n        }\n        view.setUint16(offset, q.type);\n        offset += 2;\n        view.setUint16(offset, q.class);\n        offset += 2;\n    }\n\n    function write_reply(a) {\n        for(let s of a.name) {\n            const n_written = view_set_string(offset + 1, s, view, out);\n            view.setUint8(offset, n_written);\n            offset += 1 + n_written;\n        }\n        view.setUint16(offset, a.type);\n        offset += 2;\n        view.setUint16(offset, a.class);\n        offset += 2;\n        view.setUint32(offset, a.ttl);\n        offset += 4;\n        view.setUint16(offset, a.data.length);\n        offset += 2;\n        offset += view_set_array(offset, a.data, view, out);\n    }\n\n    for(let i = 0; i < spec.dns.answers.length; ++i) {\n        let a = spec.dns.answers[i];\n        write_reply(a);\n    }\n\n    return offset;\n}\n\nfunction parse_dhcp(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n    let bootpo  = data.subarray(44,44+192);\n    let dhcp = {\n        op:  view.getUint8(0),\n        htype: view.getUint8(1),\n        hlen: view.getUint8(2),\n        hops: view.getUint8(3),\n        xid: view.getUint32(4),\n        secs: view.getUint16(8),\n        flags: view.getUint16(10),\n        ciaddr: view.getUint32(12),\n        yiaddr: view.getUint32(16),\n        siaddr: view.getUint32(20),\n        giaddr: view.getUint32(24),\n        chaddr: data.subarray(28,28+16),\n        magic: view.getUint32(236),\n        options: [],\n    };\n\n    let options = data.subarray(240);\n    for(let i = 0; i < options.length; ++i) {\n        let start = i;\n        let op = options[i];\n        if(op === 0) continue;\n        ++i;\n        let len = options[i];\n        i += len;\n        dhcp.options.push(options.subarray(start, start + len + 2));\n    }\n\n    o.dhcp = dhcp;\n    o.dhcp_options = dhcp.options;\n}\n\nfunction write_dhcp(spec, out) {\n    const view = out.udp_payload_view;\n    view.setUint8(0, spec.dhcp.op);\n    view.setUint8(1, spec.dhcp.htype);\n    view.setUint8(2, spec.dhcp.hlen);\n    view.setUint8(3, spec.dhcp.hops);\n    view.setUint32(4, spec.dhcp.xid);\n    view.setUint16(8, spec.dhcp.secs);\n    view.setUint16(10, spec.dhcp.flags);\n    view.setUint32(12, spec.dhcp.ciaddr);\n    view.setUint32(16, spec.dhcp.yiaddr);\n    view.setUint32(20, spec.dhcp.siaddr);\n    view.setUint32(24, spec.dhcp.giaddr);\n    view_set_array(28, spec.dhcp.chaddr, view, out);\n\n    view.setUint32(236, DHCP_MAGIC_COOKIE);\n\n    let offset = 240;\n    for(let o of spec.dhcp.options) {\n        offset += view_set_array(offset, o, view, out);\n    }\n    return offset;\n}\n\nfunction parse_ntp(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n    o.ntp = {\n        flags: view.getUint8(0),\n        stratum: view.getUint8(1),\n        poll: view.getUint8(2),\n        precision: view.getUint8(3),\n        root_delay: view.getUint32(4),\n        root_disp: view.getUint32(8),\n        ref_id: view.getUint32(12),\n        ref_ts_i: view.getUint32(16),\n        ref_ts_f: view.getUint32(20),\n        ori_ts_i: view.getUint32(24),\n        ori_ts_f: view.getUint32(28),\n        rec_ts_i: view.getUint32(32),\n        rec_ts_f: view.getUint32(36),\n        trans_ts_i: view.getUint32(40),\n        trans_ts_f: view.getUint32(44),\n    };\n}\n\nfunction write_ntp(spec, out) {\n    const view = out.udp_payload_view;\n    view.setUint8(0, spec.ntp.flags);\n    view.setUint8(1, spec.ntp.stratum);\n    view.setUint8(2, spec.ntp.poll);\n    view.setUint8(3, spec.ntp.precision);\n    view.setUint32(4, spec.ntp.root_delay);\n    view.setUint32(8, spec.ntp.root_disp);\n    view.setUint32(12, spec.ntp.ref_id);\n    view.setUint32(16, spec.ntp.ref_ts_i);\n    view.setUint32(20, spec.ntp.ref_ts_f);\n    view.setUint32(24, spec.ntp.ori_ts_i);\n    view.setUint32(28, spec.ntp.ori_ts_f);\n    view.setUint32(32, spec.ntp.rec_ts_i);\n    view.setUint32(36, spec.ntp.rec_ts_f);\n    view.setUint32(40, spec.ntp.trans_ts_i);\n    view.setUint32(44, spec.ntp.trans_ts_f);\n    return 48;\n}\n\nfunction parse_tcp(data, o) {\n    let view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\n    let tcp = {\n        sport: view.getUint16(0),\n        dport: view.getUint16(2),\n        seq: view.getUint32(4),\n        ackn: view.getUint32(8),\n        doff: view.getUint8(12) >> 4,\n        winsize: view.getUint16(14),\n        checksum: view.getUint16(16),\n        urgent: view.getUint16(18),\n    };\n\n    let flags = view.getUint8(13);\n\n    tcp.fin = !!(flags & 0x01);\n    tcp.syn = !!(flags & 0x02);\n    tcp.rst = !!(flags & 0x04);\n    tcp.psh = !!(flags & 0x08);\n    tcp.ack = !!(flags & 0x10);\n    tcp.urg = !!(flags & 0x20);\n    tcp.ece = !!(flags & 0x40);\n    tcp.cwr = !!(flags & 0x80);\n\n    o.tcp = tcp;\n\n    let offset = tcp.doff * 4;\n    o.tcp_data = data.subarray(offset);\n}\n\nfunction write_tcp(spec, out) {\n    const view = out.ipv4_payload_view;\n    let flags = 0;\n    let tcp = spec.tcp;\n\n    if(tcp.fin) flags |= 0x01;\n    if(tcp.syn) flags |= 0x02;\n    if(tcp.rst) flags |= 0x04;\n    if(tcp.psh) flags |= 0x08;\n    if(tcp.ack) flags |= 0x10;\n    if(tcp.urg) flags |= 0x20;\n    if(tcp.ece) flags |= 0x40;\n    if(tcp.cwr) flags |= 0x80;\n\n    let doff = TCP_HEADER_SIZE;\n    if(tcp.options) {\n        if(tcp.options.mss) {\n            view.setUint8(doff, 0x02); //mss option type\n            view.setUint8(doff + 1, 0x04); //option length\n            view.setUint16(doff + 2, tcp.options.mss);\n            doff += 4;\n        }\n    }\n\n    let total_length = Math.ceil(doff / 4) * 4; // needs to a multiple of 4 bytes\n    if(tcp.options && total_length - doff > 0) {\n        view_set_zeros(doff, total_length - doff, view, out); //write zeros into remaining space for options\n    }\n\n    view.setUint16(0, tcp.sport);\n    view.setUint16(2, tcp.dport);\n    view.setUint32(4, tcp.seq);\n    view.setUint32(8, tcp.ackn);\n    view.setUint8(12, (total_length >> 2) << 4); // header length in 32-bit words\n    view.setUint8(13, flags);\n    view.setUint16(14, tcp.winsize);\n    view.setUint16(16, 0); // checksum initially zero before calculation\n    view.setUint16(18, tcp.urgent || 0);\n\n    if(spec.tcp_data) {\n        total_length += view_set_array(TCP_HEADER_SIZE, spec.tcp_data, view, out);\n    }\n\n    const pseudo_header =\n        (spec.ipv4.src[0] << 8 | spec.ipv4.src[1]) +\n        (spec.ipv4.src[2] << 8 | spec.ipv4.src[3]) +\n        (spec.ipv4.dest[0] << 8 | spec.ipv4.dest[1]) +\n        (spec.ipv4.dest[2] << 8 | spec.ipv4.dest[3]) +\n        IPV4_PROTO_TCP +\n        total_length;\n    view.setUint16(16, calc_inet_checksum(total_length, pseudo_header, view, out));\n    return total_length;\n}\n\nexport function fake_tcp_connect(dport, adapter)\n{\n    const vm_ip_str = adapter.vm_ip.join(\".\");\n    const router_ip_str = adapter.router_ip.join(\".\");\n    const sport_0 = (Math.random() * TCP_DYNAMIC_PORT_RANGE) | 0;\n    let sport, tuple, sport_i = 0;\n    do {\n        sport = TCP_DYNAMIC_PORT_START + ((sport_0 + sport_i) % TCP_DYNAMIC_PORT_RANGE);\n        tuple = `${vm_ip_str}:${dport}:${router_ip_str}:${sport}`;\n    } while(++sport_i < TCP_DYNAMIC_PORT_RANGE && adapter.tcp_conn[tuple]);\n    if(adapter.tcp_conn[tuple]) {\n        throw new Error(\"pool of dynamic TCP port numbers exhausted, connection aborted\");\n    }\n\n    let conn = new TCPConnection(adapter);\n\n    conn.tuple = tuple;\n    conn.hsrc = adapter.router_mac;\n    conn.psrc = adapter.router_ip;\n    conn.sport = sport;\n    conn.hdest = adapter.vm_mac;\n    conn.dport = dport;\n    conn.pdest = adapter.vm_ip;\n    adapter.tcp_conn[tuple] = conn;\n    conn.connect();\n    return conn;\n}\n\nexport function fake_tcp_probe(dport, adapter) {\n    return new Promise((res, rej) => {\n        let handle = fake_tcp_connect(dport, adapter);\n        handle.state = TCP_STATE_SYN_PROBE;\n        handle.on(\"probe\", res);\n    });\n}\n\n/**\n * @constructor\n */\nexport function TCPConnection(adapter)\n{\n    this.mtu = (adapter.mtu || MTU_DEFAULT);\n    const IPV4_PAYLOAD_SIZE = this.mtu - IPV4_HEADER_SIZE;\n    const TCP_PAYLOAD_SIZE = IPV4_PAYLOAD_SIZE - TCP_HEADER_SIZE;\n\n    this.state = TCP_STATE_CLOSED;\n    this.net = adapter; // The adapter is stored here\n    this.send_buffer = new GrowableRingbuffer(2048, 0);\n    this.send_chunk_buf = new Uint8Array(TCP_PAYLOAD_SIZE);\n    this.in_active_close = false;\n    this.delayed_send_fin = false;\n    this.delayed_state = undefined;\n    this.events_handlers = {};\n}\n\nTCPConnection.prototype.on = function(event, handler) {\n    this.events_handlers[event] = handler;\n};\n\nTCPConnection.prototype.emit = function(event, ...args) {\n    if(!this.events_handlers[event]) return;\n    this.events_handlers[event].apply(this, args);\n};\n\n\nTCPConnection.prototype.ipv4_reply = function() {\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_IPV4, src: this.hsrc, dest: this.hdest };\n    reply.ipv4 = {\n        proto: IPV4_PROTO_TCP,\n        src: this.psrc,\n        dest: this.pdest\n    };\n    reply.tcp = {\n        sport: this.sport,\n        dport: this.dport,\n        winsize: this.winsize,\n        ackn: this.ack,\n        seq: this.seq,\n        ack: true\n    };\n    return reply;\n};\n\nTCPConnection.prototype.packet_reply = function(packet, tcp_options) {\n    const reply_tcp = {\n        sport: packet.tcp.dport,\n        dport: packet.tcp.sport,\n        winsize: packet.tcp.winsize,\n        ackn: this.ack,\n        seq: this.seq\n    };\n    if(tcp_options) {\n        for(const opt in tcp_options) {\n            reply_tcp[opt] = tcp_options[opt];\n        }\n    }\n    const reply = this.ipv4_reply();\n    reply.tcp = reply_tcp;\n    return reply;\n};\n\n\nTCPConnection.prototype.connect = function() {\n    // dbg_log(`TCP[${this.tuple}]: connect(): sending SYN+ACK in state \"${this.state}\", next \"${TCP_STATE_SYN_SENT}\"`, LOG_FETCH);\n    this.seq = 1338;\n    this.ack = 1;\n    this.start_seq = 0;\n    this.winsize = 64240;\n    if(this.state !== TCP_STATE_SYN_PROBE) {\n        this.state = TCP_STATE_SYN_SENT;\n    }\n\n    let reply = this.ipv4_reply();\n    reply.ipv4.id = 2345;\n    reply.tcp = {\n        sport: this.sport,\n        dport: this.dport,\n        seq: 1337,\n        ackn: 0,\n        winsize: 0,\n        syn: true,\n    };\n    this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n};\n\n\nTCPConnection.prototype.accept = function(packet=undefined) {\n    packet = packet || this.last;\n    this.net.tcp_conn[this.tuple] = this;\n    this.seq = 1338;\n    this.ack = packet.tcp.seq + 1;\n    this.start_seq = packet.tcp.seq;\n    this.winsize = packet.tcp.winsize;\n\n    let reply = this.ipv4_reply();\n    reply.tcp = {\n        sport: this.sport,\n        dport: this.dport,\n        seq: 1337,\n        ackn: this.ack,\n        winsize: packet.tcp.winsize,\n        syn: true,\n        ack: true,\n        options: {\n            mss: (this.mtu - TCP_HEADER_SIZE - IPV4_HEADER_SIZE)\n        }\n    };\n    // dbg_log(`TCP[${this.tuple}]: accept(): sending SYN+ACK in state \"${this.state}\", next \"${TCP_STATE_ESTABLISHED}\"`, LOG_FETCH);\n    this.state = TCP_STATE_ESTABLISHED;\n    this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n};\n\nTCPConnection.prototype.process = function(packet) {\n    this.last = packet;\n    if(this.state === TCP_STATE_CLOSED) {\n        // dbg_log(`TCP[${this.tuple}]: WARNING: connection already closed, packet dropped`, LOG_FETCH);\n        const reply = this.packet_reply(packet, {rst: true});\n        this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n        return;\n    }\n    else if(packet.tcp.rst) {\n        if(this.state === TCP_STATE_SYN_PROBE) {\n            this.emit(\"probe\", false);\n            this.release();\n            return;\n        }\n        // dbg_log(`TCP[${this.tuple}]: received RST in state \"${this.state}\"`, LOG_FETCH);\n        this.on_close();\n        this.release();\n        return;\n    }\n    else if(packet.tcp.syn) {\n        if(this.state === TCP_STATE_SYN_SENT && packet.tcp.ack) {\n            this.ack = packet.tcp.seq + 1;\n            this.start_seq = packet.tcp.seq;\n            this.last_received_ackn = packet.tcp.ackn;\n\n            const reply = this.ipv4_reply();\n            this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n            // dbg_log(`TCP[${this.tuple}]: received SYN+ACK in state \"${this.state}\", next \"${TCP_STATE_ESTABLISHED}\"`, LOG_FETCH);\n            this.state = TCP_STATE_ESTABLISHED;\n            this.emit(\"connect\");\n        }\n        else if(this.state === TCP_STATE_SYN_PROBE && packet.tcp.ack) {\n            this.emit(\"probe\", true);\n            const reply = this.packet_reply(packet, {rst: true});\n            this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n            this.release();\n        }\n        else {\n            dbg_log(`TCP[${this.tuple}]: WARNING: unexpected SYN packet dropped`, LOG_FETCH);\n        }\n        if(packet.tcp_data.length) {\n            dbg_log(`TCP[${this.tuple}]: WARNING: ${packet.tcp_data.length} bytes of unexpected SYN packet payload dropped`, LOG_FETCH);\n        }\n        return;\n    }\n\n    if(packet.tcp.ack) {\n        if(this.state === TCP_STATE_SYN_RECEIVED) {\n            // dbg_log(`TCP[${this.tuple}]: received ACK in state \"${this.state}\", next \"${TCP_STATE_ESTABLISHED}\"`, LOG_FETCH);\n            this.state = TCP_STATE_ESTABLISHED;\n        }\n        else if(this.state === TCP_STATE_FIN_WAIT_1) {\n            if(!packet.tcp.fin) {   // handle FIN+ACK in FIN_WAIT_1 separately further down below\n                // dbg_log(`TCP[${this.tuple}]: received ACK in state \"${this.state}\", next \"${TCP_STATE_FIN_WAIT_2}\"`, LOG_FETCH);\n                this.state = TCP_STATE_FIN_WAIT_2;\n            }\n        }\n        else if(this.state === TCP_STATE_CLOSING || this.state === TCP_STATE_LAST_ACK) {\n            // dbg_log(`TCP[${this.tuple}]: received ACK in state \"${this.state}\"`, LOG_FETCH);\n            this.release();\n            return;\n        }\n    }\n\n    if(this.last_received_ackn === undefined) {\n        this.last_received_ackn = packet.tcp.ackn;\n    }\n    else {\n        const n_ack = packet.tcp.ackn - this.last_received_ackn;\n        //console.log(\"Read \", n_ack, \"(\", this.last_received_ackn, \") \", packet.tcp.ackn, packet.tcp.winsize)\n        if(n_ack > 0) {\n            this.last_received_ackn = packet.tcp.ackn;\n            this.send_buffer.remove(n_ack);\n            this.seq += n_ack;\n            this.pending = false;\n\n            if(this.delayed_send_fin && !this.send_buffer.length) {\n                // dbg_log(`TCP[${this.tuple}]: sending delayed FIN from active close in state \"${this.state}\", next \"${this.delayed_state}\"`, LOG_FETCH);\n                this.delayed_send_fin = false;\n                this.state = this.delayed_state;\n                const reply = this.ipv4_reply();\n                reply.tcp.fin = true;\n                this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n                return;\n            }\n        }\n        else if(n_ack < 0) {    // TODO: could this just be a 32-bit sequence number overflow?\n            dbg_log(`TCP[${this.tuple}]: ERROR: ack underflow (pkt=${packet.tcp.ackn} last=${this.last_received_ackn}), resetting`, LOG_FETCH);\n            const reply = this.packet_reply(packet, {rst: true});\n            this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n            this.on_close();\n            this.release();\n            return;\n        }\n    }\n\n    if(packet.tcp.fin) {\n        if(this.ack !== packet.tcp.seq) {\n            dbg_log(`TCP[${this.tuple}]: WARNING: closing connection in state \"${this.state}\" with invalid seq (${this.ack} != ${packet.tcp.seq})`, LOG_FETCH);\n        }\n        ++this.ack; // FIN increases seqnr\n        const reply = this.packet_reply(packet, {});\n        if(this.state === TCP_STATE_ESTABLISHED) {\n            // dbg_log(`TCP[${this.tuple}]: received FIN in state \"${this.state}, next \"${TCP_STATE_CLOSE_WAIT}\"\"`, LOG_FETCH);\n            reply.tcp.ack = true;\n            this.state = TCP_STATE_CLOSE_WAIT;\n            this.on_shutdown();\n        }\n        else if(this.state === TCP_STATE_FIN_WAIT_1) {\n            if(packet.tcp.ack) {\n                // dbg_log(`TCP[${this.tuple}]: received ACK+FIN in state \"${this.state}\"`, LOG_FETCH);\n                this.release();\n            }\n            else {\n                // dbg_log(`TCP[${this.tuple}]: received ACK in state \"${this.state}\", next \"${TCP_STATE_CLOSING}\"`, LOG_FETCH);\n                this.state = TCP_STATE_CLOSING;\n            }\n            reply.tcp.ack = true;\n        }\n        else if(this.state === TCP_STATE_FIN_WAIT_2) {\n            // dbg_log(`TCP[${this.tuple}]: received FIN in state \"${this.state}\"`, LOG_FETCH);\n            this.release();\n            reply.tcp.ack = true;\n        }\n        else {\n            // dbg_log(`TCP[${this.tuple}]: ERROR: received FIN in unexpected TCP state \"${this.state}\", resetting`, LOG_FETCH);\n            this.release();\n            this.on_close();\n            reply.tcp.rst = true;\n        }\n        this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n    }\n    else if(this.ack !== packet.tcp.seq) {\n        // Handle TCP Keep-Alives silently.\n        // Excerpt from RFC 9293, 3.8.4. TCP Keep-Alives:\n        //   To confirm that an idle connection is still active, these\n        //   implementations send a probe segment designed to elicit a response\n        //   from the TCP peer.  Such a segment generally contains SEG.SEQ =\n        //   SND.NXT-1 and may or may not contain one garbage octet of data.\n        if(this.ack !== packet.tcp.seq + 1) {\n            dbg_log(`Packet seq was wrong ex: ${this.ack} ~${this.ack - this.start_seq} ` +\n                `pk: ${packet.tcp.seq} ~${this.start_seq - packet.tcp.seq} ` +\n                `(${this.ack - packet.tcp.seq}) = ${this.name}`, LOG_FETCH);\n        }\n        const reply = this.packet_reply(packet, {ack: true});\n        this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n    }\n    else if(packet.tcp.ack && packet.tcp_data.length > 0) {\n        this.ack += packet.tcp_data.length;\n        const reply = this.ipv4_reply();\n        this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n        this.emit(\"data\", packet.tcp_data);\n    }\n\n    this.pump();\n};\n\n/**\n * @param {Uint8Array} data\n */\nTCPConnection.prototype.write = function(data) {\n    if(!this.in_active_close) {\n        this.send_buffer.write(data);\n    }\n    this.pump();\n};\n\n/**\n * @param {!Array<Uint8Array>} data_array\n */\nTCPConnection.prototype.writev = function(data_array) {\n    if(!this.in_active_close) {\n        for(const data of data_array) {\n            this.send_buffer.write(data);\n        }\n    }\n    this.pump();\n};\n\nTCPConnection.prototype.close = function() {\n    if(!this.in_active_close) {\n        this.in_active_close = true;\n        let next_state;\n        if(this.state === TCP_STATE_ESTABLISHED || this.state === TCP_STATE_SYN_RECEIVED) {\n            next_state = TCP_STATE_FIN_WAIT_1;\n        }\n        else if(this.state === TCP_STATE_CLOSE_WAIT) {\n            next_state = TCP_STATE_LAST_ACK;\n        }\n        else {\n            if(this.state !== TCP_STATE_SYN_SENT) {\n                dbg_log(`TCP[${this.tuple}]: active close in unexpected state \"${this.state}\"`, LOG_FETCH);\n            }\n            this.release();\n            return;\n        }\n\n        if(this.send_buffer.length || this.pending) {\n            // dbg_log(`TCP[${this.tuple}]: active close, delaying FIN in state \"${this.state}\", delayed next \"${next_state}\"`, LOG_FETCH);\n            this.delayed_send_fin = true;\n            this.delayed_state = next_state;\n        }\n        else {\n            // dbg_log(`TCP[${this.tuple}]: active close, sending FIN in state \"${this.state}\", next \"${next_state}\"`, LOG_FETCH);\n            this.state = next_state;\n            const reply = this.ipv4_reply();\n            reply.tcp.fin = true;\n            this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n        }\n    }\n    this.pump();\n};\n\nTCPConnection.prototype.on_shutdown = function() {\n    this.emit(\"shutdown\");\n    // forward FIN event from guest device to network adapter\n};\n\nTCPConnection.prototype.on_close = function() {\n    this.emit(\"close\");\n    // forward RST event from guest device to network adapter\n};\n\nTCPConnection.prototype.release = function() {\n    if(this.net.tcp_conn[this.tuple]) {\n        // dbg_log(`TCP[${this.tuple}]: connection closed in state \"${this.state}\"`, LOG_FETCH);\n        this.state = TCP_STATE_CLOSED;\n        delete this.net.tcp_conn[this.tuple];\n    }\n};\n\nTCPConnection.prototype.pump = function() {\n    if(this.send_buffer.length && !this.pending) {\n        const data = this.send_chunk_buf;\n        const n_ready = this.send_buffer.peek(data);\n        const reply = this.ipv4_reply();\n        reply.tcp.psh = true;\n        reply.tcp_data = data.subarray(0, n_ready);\n        this.net.receive(make_packet(this.net.eth_encoder_buf, reply));\n        this.pending = true;\n    }\n};\n\n\nfunction arp_whohas(packet, adapter) {\n    let packet_subnet = iptolong(packet.arp.tpa) & 0xFFFFFF00;\n    let router_subnet = iptolong(adapter.router_ip) & 0xFFFFFF00;\n\n    if(!adapter.masquerade) {\n        if(packet_subnet !== router_subnet) {\n            return;\n        }\n    }\n\n    if(packet_subnet === router_subnet) {\n        // Ignore the DHCP client area\n        if(packet.arp.tpa[3] > 99) return;\n    }\n\n    // Reply to ARP Whohas\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_ARP, src: adapter.router_mac, dest: packet.eth.src };\n    reply.arp = {\n        htype: 1,\n        ptype: ETHERTYPE_IPV4,\n        oper: 2,\n        sha: adapter.router_mac,\n        spa: packet.arp.tpa,\n        tha: packet.eth.src,\n        tpa: packet.arp.spa\n    };\n    adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n}\n\nfunction handle_fake_ping(packet, adapter) {\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };\n    reply.ipv4 = {\n        proto: IPV4_PROTO_ICMP,\n        src: packet.ipv4.dest,\n        dest: packet.ipv4.src,\n    };\n    reply.icmp = {\n        type: 0,\n        code: packet.icmp.code,\n        data: packet.icmp.data\n    };\n    adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n}\n\nfunction handle_udp_echo(packet, adapter) {\n    // UDP Echo Server\n    let reply = {};\n    reply.eth = { ethertype: ETHERTYPE_IPV4, src: adapter.router_mac, dest: packet.eth.src };\n    reply.ipv4 = {\n        proto: IPV4_PROTO_UDP,\n        src: packet.ipv4.dest,\n        dest: packet.ipv4.src,\n    };\n    reply.udp = {\n        sport: packet.udp.dport,\n        dport: packet.udp.sport,\n        data: new TextEncoder().encode(packet.udp.data_s)\n    };\n    adapter.receive(make_packet(adapter.eth_encoder_buf, reply));\n}\n"
  },
  {
    "path": "src/browser/fetch_network.js",
    "content": "import { LOG_FETCH } from \"../const.js\";\nimport { dbg_log } from \"../log.js\";\n\nimport {\n    create_eth_encoder_buf,\n    handle_fake_networking,\n    TCPConnection,\n    TCP_STATE_SYN_RECEIVED,\n    fake_tcp_connect,\n    fake_tcp_probe\n} from \"./fake_network.js\";\n\n// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/**\n * @constructor\n *\n * @param {BusConnector} bus\n * @param {*=} config\n */\nexport function FetchNetworkAdapter(bus, config)\n{\n    config = config || {};\n    this.bus = bus;\n    this.id = config.id || 0;\n    this.router_mac = new Uint8Array((config.router_mac || \"52:54:0:1:2:3\").split(\":\").map(function(x) { return parseInt(x, 16); }));\n    this.router_ip = new Uint8Array((config.router_ip || \"192.168.86.1\").split(\".\").map(function(x) { return parseInt(x, 10); }));\n    this.vm_ip = new Uint8Array((config.vm_ip || \"192.168.86.100\").split(\".\").map(function(x) { return parseInt(x, 10); }));\n    this.masquerade = config.masquerade === undefined || !!config.masquerade;\n    this.vm_mac = new Uint8Array(6);\n    this.dns_method = config.dns_method || \"static\";\n    this.doh_server = config.doh_server;\n    this.tcp_conn = {};\n    this.mtu = config.mtu;\n    this.eth_encoder_buf = create_eth_encoder_buf(this.mtu);\n    this.fetch = (...args) => fetch(...args);\n\n    // Ex: 'https://corsproxy.io/?'\n    this.cors_proxy = config.cors_proxy;\n\n    this.bus.register(\"net\" + this.id + \"-mac\", function(mac) {\n        this.vm_mac = new Uint8Array(mac.split(\":\").map(function(x) { return parseInt(x, 16); }));\n    }, this);\n    this.bus.register(\"net\" + this.id + \"-send\", function(data)\n    {\n        this.send(data);\n    }, this);\n    this.bus.register(\"tcp-connection\", (conn) => {\n        if(conn.sport === 80) {\n            conn.on(\"data\", on_data_http);\n            conn.accept();\n        }\n    }, this);\n}\n\nFetchNetworkAdapter.prototype.destroy = function()\n{\n};\n\nFetchNetworkAdapter.prototype.connect = function(port)\n{\n    return fake_tcp_connect(port, this);\n};\n\nFetchNetworkAdapter.prototype.tcp_probe = function(port)\n{\n    return fake_tcp_probe(port, this);\n};\n\n/**\n * @this {TCPConnection}\n * @param {!ArrayBuffer} data\n */\nasync function on_data_http(data)\n{\n    this.read = this.read || \"\";\n    this.read += new TextDecoder().decode(data);\n    if(this.read && this.read.indexOf(\"\\r\\n\\r\\n\") !== -1) {\n        let offset = this.read.indexOf(\"\\r\\n\\r\\n\");\n        let headers = this.read.substring(0, offset).split(/\\r\\n/);\n        let data = this.read.substring(offset + 4);\n        this.read = \"\";\n\n        let first_line = headers[0].split(\" \");\n        let target;\n        if(/^https?:/.test(first_line[1])) {\n            // HTTP proxy\n            target = new URL(first_line[1]);\n        }\n        else {\n            target = new URL(\"http://host\" + first_line[1]);\n        }\n        if(typeof window !== \"undefined\" && target.protocol === \"http:\" && window.location.protocol === \"https:\") {\n            // fix \"Mixed Content\" errors\n            target.protocol = \"https:\";\n        }\n\n        let req_headers = new Headers();\n        for(let i = 1; i < headers.length; ++i) {\n            const header = this.net.parse_http_header(headers[i]);\n            if(!header) {\n                console.warn('The request contains an invalid header: \"%s\"', headers[i]);\n                this.net.respond_text_and_close(this, 400, \"Bad Request\", `Invalid header in request: ${headers[i]}`);\n                return;\n            }\n            if( header.key.toLowerCase() === \"host\" ) target.host = header.value;\n            else req_headers.append(header.key, header.value);\n        }\n\n        if(!this.net.cors_proxy && /^\\d+\\.external$/.test(target.hostname)) {\n            dbg_log(\"Request to localhost: \" + target.href, LOG_FETCH);\n            const localport = parseInt(target.hostname.split(\".\")[0], 10);\n            if(!isNaN(localport) && localport > 0 && localport < 65536) {\n                target.protocol = \"http:\";\n                target.hostname = \"localhost\";\n                target.port = localport.toString(10);\n            } else {\n                console.warn('Unknown port for localhost: \"%s\"', target.href);\n                this.net.respond_text_and_close(this, 400, \"Bad Request\", `Unknown port for localhost: ${target.href}`);\n                return;\n            }\n        }\n\n        dbg_log(\"HTTP Dispatch: \" + target.href, LOG_FETCH);\n        this.name = target.href;\n        let opts = {\n            method: first_line[0],\n            headers: req_headers,\n        };\n        if([\"put\", \"post\"].indexOf(opts.method.toLowerCase()) !== -1) {\n            opts.body = data;\n        }\n\n        const fetch_url = this.net.cors_proxy ? this.net.cors_proxy + encodeURIComponent(target.href) : target.href;\n        const encoder = new TextEncoder();\n        let response_started = false;\n        let handler = (resp) => {\n            let resp_headers = new Headers(resp.headers);\n            resp_headers.delete(\"content-encoding\");\n            resp_headers.delete(\"keep-alive\");\n            resp_headers.delete(\"content-length\");\n            resp_headers.delete(\"transfer-encoding\");\n            resp_headers.set(\"x-was-fetch-redirected\", `${!!resp.redirected}`);\n            resp_headers.set(\"x-fetch-resp-url\", resp.url);\n            resp_headers.set(\"connection\", \"close\");\n\n            this.write(this.net.form_response_head(resp.status, resp.statusText, resp_headers));\n            response_started = true;\n\n            if(resp.body && resp.body.getReader) {\n                const resp_reader = resp.body.getReader();\n                const pump = ({ value, done }) => {\n                    if(value) {\n                        this.write(value);\n                    }\n                    if(done) {\n                        this.close();\n                    }\n                    else {\n                        return resp_reader.read().then(pump);\n                    }\n                };\n                resp_reader.read().then(pump);\n            } else {\n                resp.arrayBuffer().then(buffer => {\n                    this.write(new Uint8Array(buffer));\n                    this.close();\n                });\n            }\n        };\n\n        this.net.fetch(fetch_url, opts).then(handler)\n        .catch((e) => {\n            console.warn(\"Fetch Failed: \" + fetch_url + \"\\n\" + e);\n            if(!response_started) {\n                this.net.respond_text_and_close(this, 502, \"Fetch Error\", `Fetch ${fetch_url} failed:\\n\\n${e.stack || e.message}`);\n            }\n            this.close();\n        });\n    }\n}\n\nFetchNetworkAdapter.prototype.fetch = async function(url, options)\n{\n    if(this.cors_proxy) url = this.cors_proxy + encodeURIComponent(url);\n\n    try\n    {\n        const resp = await fetch(url, options);\n        const ab = await resp.arrayBuffer();\n        return [resp, ab];\n    }\n    catch(e)\n    {\n        console.warn(\"Fetch Failed: \" + url + \"\\n\" + e);\n        return [\n            {\n                status: 502,\n                statusText: \"Fetch Error\",\n                headers: new Headers({ \"Content-Type\": \"text/plain\" }),\n            },\n            new TextEncoder().encode(`Fetch ${url} failed:\\n\\n${e.stack}`).buffer\n        ];\n    }\n};\n\nFetchNetworkAdapter.prototype.form_response_head = function(status_code, status_text, headers)\n{\n    let lines = [\n        `HTTP/1.1 ${status_code} ${status_text}`\n    ];\n\n    for(const [key, value] of headers.entries()) {\n        lines.push(`${key}: ${value}`);\n    }\n\n    return new TextEncoder().encode(lines.join(\"\\r\\n\") + \"\\r\\n\\r\\n\");\n};\n\nFetchNetworkAdapter.prototype.respond_text_and_close = function(conn, status_code, status_text, body)\n{\n    const headers = new Headers({\n        \"content-type\": \"text/plain\",\n        \"content-length\": body.length.toString(10),\n        \"connection\": \"close\"\n    });\n    conn.writev([this.form_response_head(status_code, status_text, headers), new TextEncoder().encode(body)]);\n    conn.close();\n};\n\nFetchNetworkAdapter.prototype.parse_http_header = function(header)\n{\n    const parts = header.match(/^([^:]*):(.*)$/);\n    if(!parts) {\n        dbg_log(\"Unable to parse HTTP header\", LOG_FETCH);\n        return;\n    }\n\n    const key = parts[1];\n    const value = parts[2].trim();\n\n    if(key.length === 0)\n    {\n        dbg_log(\"Header key is empty, raw header\", LOG_FETCH);\n        return;\n    }\n    if(value.length === 0)\n    {\n        dbg_log(\"Header value is empty\", LOG_FETCH);\n        return;\n    }\n    if(!/^[\\w-]+$/.test(key))\n    {\n        dbg_log(\"Header key contains forbidden characters\", LOG_FETCH);\n        return;\n    }\n    if(!/^[\\x20-\\x7E]+$/.test(value))\n    {\n        dbg_log(\"Header value contains forbidden characters\", LOG_FETCH);\n        return;\n    }\n\n    return { key, value };\n};\n\n/**\n * @param {Uint8Array} data\n */\nFetchNetworkAdapter.prototype.send = function(data)\n{\n    handle_fake_networking(data, this);\n};\n\n/**\n * @param {Uint8Array} data\n */\nFetchNetworkAdapter.prototype.receive = function(data)\n{\n    this.bus.send(\"net\" + this.id + \"-receive\", new Uint8Array(data));\n};\n"
  },
  {
    "path": "src/browser/filestorage.js",
    "content": "import { dbg_assert } from \"../log.js\";\nimport { load_file } from \"../lib.js\";\n\n/** @interface */\nexport function FileStorageInterface() {}\n\n/**\n * Read a portion of a file.\n * @param {string} sha256sum\n * @param {number} offset\n * @param {number} count\n * @param {number} file_size\n * @return {!Promise<Uint8Array>} null if file does not exist.\n */\nFileStorageInterface.prototype.read = function(sha256sum, offset, count, file_size) {};\n\n/**\n * Add a read-only file to the filestorage.\n * @param {string} sha256sum\n * @param {!Uint8Array} data\n * @return {!Promise}\n */\nFileStorageInterface.prototype.cache = function(sha256sum, data) {};\n\n/**\n * Call this when the file won't be used soon, e.g. when a file closes or when this immutable\n * version is already out of date. It is used to help prevent accumulation of unused files in\n * memory in the long run for some FileStorage mediums.\n */\nFileStorageInterface.prototype.uncache = function(sha256sum) {};\n\n/**\n * @constructor\n * @implements {FileStorageInterface}\n */\nexport function MemoryFileStorage()\n{\n    /**\n     * From sha256sum to file data.\n     * @type {Map<string,Uint8Array>}\n     */\n    this.filedata = new Map();\n}\n\n/**\n * @param {string} sha256sum\n * @param {number} offset\n * @param {number} count\n * @return {!Promise<Uint8Array>} null if file does not exist.\n */\nMemoryFileStorage.prototype.read = async function(sha256sum, offset, count)\n{\n    dbg_assert(sha256sum, \"MemoryFileStorage read: sha256sum should be a non-empty string\");\n    const data = this.filedata.get(sha256sum);\n\n    if(!data)\n    {\n        return null;\n    }\n\n    return data.subarray(offset, offset + count);\n};\n\n/**\n * @param {string} sha256sum\n * @param {!Uint8Array} data\n */\nMemoryFileStorage.prototype.cache = async function(sha256sum, data)\n{\n    dbg_assert(sha256sum, \"MemoryFileStorage cache: sha256sum should be a non-empty string\");\n    this.filedata.set(sha256sum, data);\n};\n\n/**\n * @param {string} sha256sum\n */\nMemoryFileStorage.prototype.uncache = function(sha256sum)\n{\n    this.filedata.delete(sha256sum);\n};\n\n/**\n * @constructor\n * @implements {FileStorageInterface}\n * @param {FileStorageInterface} file_storage\n * @param {string} baseurl\n * @param {function(number,Uint8Array):ArrayBuffer} zstd_decompress\n */\nexport function ServerFileStorageWrapper(file_storage, baseurl, zstd_decompress)\n{\n    dbg_assert(baseurl, \"ServerMemoryFileStorage: baseurl should not be empty\");\n\n    if(!baseurl.endsWith(\"/\"))\n    {\n        baseurl += \"/\";\n    }\n\n    this.storage = file_storage;\n    this.baseurl = baseurl;\n    this.zstd_decompress = zstd_decompress;\n}\n\n/**\n * @param {string} sha256sum\n * @param {number} file_size\n * @return {!Promise<Uint8Array>}\n */\nServerFileStorageWrapper.prototype.load_from_server = function(sha256sum, file_size)\n{\n    return new Promise((resolve, reject) =>\n    {\n        load_file(this.baseurl + sha256sum, { done: async buffer =>\n        {\n            let data = new Uint8Array(buffer);\n            if(sha256sum.endsWith(\".zst\"))\n            {\n                data = new Uint8Array(\n                    this.zstd_decompress(file_size, data)\n                );\n            }\n            await this.cache(sha256sum, data);\n            resolve(data);\n        }});\n    });\n};\n\n/**\n * @param {string} sha256sum\n * @param {number} offset\n * @param {number} count\n * @param {number} file_size\n * @return {!Promise<Uint8Array>}\n */\nServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count, file_size)\n{\n    const data = await this.storage.read(sha256sum, offset, count, file_size);\n    if(!data)\n    {\n        const full_file = await this.load_from_server(sha256sum, file_size);\n        return full_file.subarray(offset, offset + count);\n    }\n    return data;\n};\n\n/**\n * @param {string} sha256sum\n * @param {!Uint8Array} data\n */\nServerFileStorageWrapper.prototype.cache = async function(sha256sum, data)\n{\n    return await this.storage.cache(sha256sum, data);\n};\n\n/**\n * @param {string} sha256sum\n */\nServerFileStorageWrapper.prototype.uncache = function(sha256sum)\n{\n    this.storage.uncache(sha256sum);\n};\n"
  },
  {
    "path": "src/browser/inbrowser_network.js",
    "content": "// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/**\n * Network adapter \"inbrowser\" which connects the emulated NIC\n * to a shared in-browser BroadcastChannel.\n *\n * NOTE: BroadcastChannel.postMessage() sends the given message to\n *       *other* BroadcastChannel objects set up for the same channel.\n *       Since we use the same BroadcastChannel instance for both\n *       sending and receiving we do not receive copies of our\n *       own sent messages.\n *       Source: https://html.spec.whatwg.org/multipage/web-messaging.html#broadcasting-to-other-browsing-contexts\n *\n * @constructor\n *\n * @param {BusConnector} bus\n * @param {*=} config\n */\nexport function InBrowserNetworkAdapter(bus, config)\n{\n    const id = config.id || 0;\n\n    this.bus = bus;\n    this.bus_send_msgid = `net${id}-send`;\n    this.bus_recv_msgid = `net${id}-receive`;\n    this.channel = new BroadcastChannel(`v86-inbrowser-${id}`);\n    this.is_open = true;\n\n    // forward ethernet frames from emulated NIC to hub\n    this.nic_to_hub_fn = (eth_frame) => {\n        this.channel.postMessage(eth_frame);\n    };\n    this.bus.register(this.bus_send_msgid, this.nic_to_hub_fn, this);\n\n    // forward ethernet frames from hub to emulated NIC\n    this.hub_to_nic_fn = (ev) => {\n        this.bus.send(this.bus_recv_msgid, ev.data);\n    };\n    this.channel.addEventListener(\"message\", this.hub_to_nic_fn);\n}\n\nInBrowserNetworkAdapter.prototype.destroy = function()\n{\n    if(this.is_open) {\n        this.bus.unregister(this.bus_send_msgid, this.nic_to_hub_fn);\n        this.channel.removeEventListener(\"message\", this.hub_to_nic_fn);\n        this.channel.close();\n        this.is_open = false;\n    }\n};\n"
  },
  {
    "path": "src/browser/keyboard.js",
    "content": "// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\nconst SHIFT_SCAN_CODE = 0x2A;\nconst SCAN_CODE_RELEASE = 0x80;\n\nconst PLATFOM_WINDOWS = typeof window !== \"undefined\" && window.navigator.platform.toString().toLowerCase().search(\"win\") >= 0;\n\n/**\n * @constructor\n *\n * @param {BusConnector} bus\n */\nexport function KeyboardAdapter(bus)\n{\n    var\n        /**\n         * @type {!Object.<boolean>}\n         */\n        keys_pressed = {},\n\n        /**\n         * Deferred KeyboardEvent or null (Windows AltGr-Filter)\n         * @type {KeyboardEvent|Object|null}\n         */\n        deferred_event = null,\n\n        /**\n         * Deferred keydown state (Windows AltGr-Filter)\n         * @type {boolean}\n         */\n        deferred_keydown = false,\n\n        /**\n         * Timeout-ID returned by setTimeout() or 0 (Windows AltGr-Filter)\n         * @type {number}\n         */\n        deferred_timeout_id = 0,\n\n        keyboard = this;\n\n    /**\n     * Set by emulator\n     * @type {boolean}\n     */\n    this.emu_enabled = true;\n\n    // Format:\n    // Javascript event.keyCode -> make code\n    const charmap = new Uint16Array([\n        0, 0, 0, 0,  0, 0, 0, 0,\n        // 0x08: backspace, tab, enter\n        0x0E, 0x0F, 0, 0,  0, 0x1C, 0, 0,\n\n        // 0x10: shift, ctrl, alt, pause, caps lock\n        0x2A, 0x1D, 0x38, 0,  0x3A, 0, 0, 0,\n\n        // 0x18: escape\n        0, 0, 0, 0x01,  0, 0, 0, 0,\n\n        // 0x20: spacebar, page down/up, end, home, arrow keys, ins, del\n        0x39, 0xE049, 0xE051, 0xE04F,  0xE047, 0xE04B, 0xE048, 0xE04D,\n        0x50, 0, 0, 0,  0, 0x52, 0x53, 0,\n\n        // 0x30: numbers\n        0x0B, 0x02, 0x03, 0x04,  0x05, 0x06, 0x07, 0x08,\n        0x09, 0x0A,\n\n        // 0x3B: ;= (firefox only)\n        0, 0x27, 0, 0x0D, 0, 0,\n\n        // 0x40\n        0,\n\n        // 0x41: letters\n        0x1E, 0x30, 0x2E, 0x20, 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, 0x32,\n        0x31, 0x18, 0x19, 0x10, 0x13, 0x1F, 0x14, 0x16, 0x2F, 0x11, 0x2D, 0x15, 0x2C,\n\n        // 0x5B: Left Win, Right Win, Menu\n        0xE05B, 0xE05C, 0xE05D, 0, 0,\n\n        // 0x60: keypad\n        0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47,\n        0x48, 0x49, 0, 0, 0, 0, 0, 0,\n\n        // 0x70: F1 to F12\n        0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x57, 0x58,\n\n        0, 0, 0, 0,\n\n        // 0x80\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0, 0, 0,\n\n        // 0x90: Numlock\n        0x45, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,     0, 0, 0, 0,\n\n        // 0xA0: - (firefox only)\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0x0C, 0, 0,\n\n        // 0xB0\n        // ,\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0x27, 0x0D,  0x33, 0x0C, 0x34, 0x35,\n\n        // 0xC0\n        // `\n        0x29, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,     0, 0, 0, 0,\n\n        // 0xD0\n        // [']\\\n        0, 0, 0, 0,     0, 0, 0, 0,\n        0, 0, 0, 0x1A,  0x2B, 0x1B, 0x28, 0,\n\n        // 0xE0\n        // Apple key on Gecko, Right alt\n        0xE05B, 0xE038, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,            0, 0, 0, 0,\n    ]);\n\n\n    // ascii -> javascript event code (US layout)\n    const asciimap = {8: 8, 10: 13, 32: 32, 39: 222, 44: 188, 45: 189, 46: 190, 47: 191, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 59: 186, 61: 187, 91: 219, 92: 220, 93: 221, 96: 192, 97: 65, 98: 66, 99: 67, 100: 68, 101: 69, 102: 70, 103: 71, 104: 72, 105: 73, 106: 74, 107: 75, 108: 76, 109: 77, 110: 78, 111: 79, 112: 80, 113: 81, 114: 82, 115: 83, 116: 84, 117: 85, 118: 86, 119: 87, 120: 88, 121: 89, 122: 90};\n    const asciimap_shift = {33: 49, 34: 222, 35: 51, 36: 52, 37: 53, 38: 55, 40: 57, 41: 48, 42: 56, 43: 187, 58: 186, 60: 188, 62: 190, 63: 191, 64: 50, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 94: 54, 95: 189, 123: 219, 124: 220, 125: 221, 126: 192};\n\n    // From:\n    // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code#Code_values_on_Linux_%28X11%29_%28When_scancode_is_available%29\n    // http://stanislavs.org/helppc/make_codes.html\n    // http://www.computer-engineering.org/ps2keyboard/scancodes1.html\n    //\n    // Mapping from event.code to scancode\n    var codemap = {\n        \"Escape\": 0x0001,\n        \"Digit1\": 0x0002,\n        \"Digit2\": 0x0003,\n        \"Digit3\": 0x0004,\n        \"Digit4\": 0x0005,\n        \"Digit5\": 0x0006,\n        \"Digit6\": 0x0007,\n        \"Digit7\": 0x0008,\n        \"Digit8\": 0x0009,\n        \"Digit9\": 0x000a,\n        \"Digit0\": 0x000b,\n        \"Minus\": 0x000c,\n        \"Equal\": 0x000d,\n        \"Backspace\": 0x000e,\n        \"Tab\": 0x000f,\n        \"KeyQ\": 0x0010,\n        \"KeyW\": 0x0011,\n        \"KeyE\": 0x0012,\n        \"KeyR\": 0x0013,\n        \"KeyT\": 0x0014,\n        \"KeyY\": 0x0015,\n        \"KeyU\": 0x0016,\n        \"KeyI\": 0x0017,\n        \"KeyO\": 0x0018,\n        \"KeyP\": 0x0019,\n        \"BracketLeft\": 0x001a,\n        \"BracketRight\": 0x001b,\n        \"Enter\": 0x001c,\n        \"ControlLeft\": 0x001d,\n        \"KeyA\": 0x001e,\n        \"KeyS\": 0x001f,\n        \"KeyD\": 0x0020,\n        \"KeyF\": 0x0021,\n        \"KeyG\": 0x0022,\n        \"KeyH\": 0x0023,\n        \"KeyJ\": 0x0024,\n        \"KeyK\": 0x0025,\n        \"KeyL\": 0x0026,\n        \"Semicolon\": 0x0027,\n        \"Quote\": 0x0028,\n        \"Backquote\": 0x0029,\n        \"ShiftLeft\": 0x002a,\n        \"Backslash\": 0x002b,\n        \"KeyZ\": 0x002c,\n        \"KeyX\": 0x002d,\n        \"KeyC\": 0x002e,\n        \"KeyV\": 0x002f,\n        \"KeyB\": 0x0030,\n        \"KeyN\": 0x0031,\n        \"KeyM\": 0x0032,\n        \"Comma\": 0x0033,\n        \"Period\": 0x0034,\n        \"Slash\": 0x0035,\n        \"IntlRo\": 0x0035,\n        \"ShiftRight\": 0x0036,\n        \"NumpadMultiply\": 0x0037,\n        \"AltLeft\": 0x0038,\n        \"Space\": 0x0039,\n        \"CapsLock\": 0x003a,\n        \"F1\": 0x003b,\n        \"F2\": 0x003c,\n        \"F3\": 0x003d,\n        \"F4\": 0x003e,\n        \"F5\": 0x003f,\n        \"F6\": 0x0040,\n        \"F7\": 0x0041,\n        \"F8\": 0x0042,\n        \"F9\": 0x0043,\n        \"F10\": 0x0044,\n        \"NumLock\": 0x0045,\n        \"ScrollLock\": 0x0046,\n        \"Numpad7\": 0x0047,\n        \"Numpad8\": 0x0048,\n        \"Numpad9\": 0x0049,\n        \"NumpadSubtract\": 0x004a,\n        \"Numpad4\": 0x004b,\n        \"Numpad5\": 0x004c,\n        \"Numpad6\": 0x004d,\n        \"NumpadAdd\": 0x004e,\n        \"Numpad1\": 0x004f,\n        \"Numpad2\": 0x0050,\n        \"Numpad3\": 0x0051,\n        \"Numpad0\": 0x0052,\n        \"NumpadDecimal\": 0x0053,\n        \"IntlBackslash\": 0x0056,\n        \"F11\": 0x0057,\n        \"F12\": 0x0058,\n\n        \"NumpadEnter\": 0xe01c,\n        \"ControlRight\": 0xe01d,\n        \"NumpadDivide\": 0xe035,\n        //\"PrintScreen\": 0x0063,\n        \"AltRight\": 0xe038,\n        \"Home\": 0xe047,\n        \"ArrowUp\": 0xe048,\n        \"PageUp\": 0xe049,\n        \"ArrowLeft\": 0xe04b,\n        \"ArrowRight\": 0xe04d,\n        \"End\": 0xe04f,\n        \"ArrowDown\": 0xe050,\n        \"PageDown\": 0xe051,\n        \"Insert\": 0xe052,\n        \"Delete\": 0xe053,\n\n        \"MetaLeft\": 0xe05b,\n        \"OSLeft\": 0xe05b,\n        \"MetaRight\": 0xe05c,\n        \"OSRight\": 0xe05c,\n        \"ContextMenu\": 0xe05d,\n    };\n\n    this.bus = bus;\n\n    this.destroy = function()\n    {\n        if(typeof window !== \"undefined\")\n        {\n            window.removeEventListener(\"keyup\", keyup_handler, false);\n            window.removeEventListener(\"keydown\", keydown_handler, false);\n            window.removeEventListener(\"blur\", blur_handler, false);\n            window.removeEventListener(\"input\", input_handler, false);\n        }\n    };\n\n    this.init = function()\n    {\n        if(typeof window === \"undefined\")\n        {\n            return;\n        }\n        this.destroy();\n\n        window.addEventListener(\"keyup\", keyup_handler, false);\n        window.addEventListener(\"keydown\", keydown_handler, false);\n        window.addEventListener(\"blur\", blur_handler, false);\n        window.addEventListener(\"input\", input_handler, false);\n    };\n    this.init();\n\n    this.simulate_press = function(code)\n    {\n        var ev = { keyCode: code };\n        handler(ev, true);\n        handler(ev, false);\n    };\n\n    this.simulate_char = function(chr)\n    {\n        var code = chr.charCodeAt(0);\n\n        if(code in asciimap)\n        {\n            this.simulate_press(asciimap[code]);\n        }\n        else if(code in asciimap_shift)\n        {\n            send_to_controller(SHIFT_SCAN_CODE);\n            this.simulate_press(asciimap_shift[code]);\n            send_to_controller(SHIFT_SCAN_CODE | SCAN_CODE_RELEASE);\n        }\n        else\n        {\n            console.log(\"ascii -> keyCode not found: \", code, chr);\n        }\n    };\n\n    function may_handle(e)\n    {\n        if(e.shiftKey && e.ctrlKey && (e.keyCode === 73 || e.keyCode === 74 || e.keyCode === 75))\n        {\n              // don't prevent opening chromium dev tools\n              // maybe add other important combinations here, too\n              return false;\n        }\n\n        if(!keyboard.emu_enabled)\n        {\n            return false;\n        }\n\n        if(e.target)\n        {\n            // className shouldn't be hardcoded here\n            return e.target.classList.contains(\"phone_keyboard\") ||\n                (e.target.nodeName !== \"INPUT\" && e.target.nodeName !== \"TEXTAREA\");\n        }\n        else\n        {\n            return true;\n        }\n    }\n\n    function translate(e)\n    {\n        if(e.code !== undefined)\n        {\n            var code = codemap[e.code];\n\n            if(code !== undefined)\n            {\n                return code;\n            }\n        }\n\n        return charmap[e.keyCode];\n    }\n\n    function keyup_handler(e)\n    {\n        if(!e.altKey && keys_pressed[0x38])\n        {\n            // trigger ALT keyup manually - some browsers don't\n            // see issue #165\n            handle_code(0x38, false);\n        }\n        return handler(e, false);\n    }\n\n    function keydown_handler(e)\n    {\n        if(!e.altKey && keys_pressed[0x38])\n        {\n            // trigger ALT keyup manually - some browsers don't\n            // see issue #165\n            handle_code(0x38, false);\n        }\n        return handler(e, true);\n    }\n\n    function blur_handler(e)\n    {\n        // trigger keyup for all pressed keys\n        var keys = Object.keys(keys_pressed),\n            key;\n\n        for(var i = 0; i < keys.length; i++)\n        {\n            key = +keys[i];\n\n            if(keys_pressed[key])\n            {\n                handle_code(key, false);\n            }\n        }\n\n        keys_pressed = {};\n    }\n\n    function input_handler(e)\n    {\n        if(!keyboard.bus)\n        {\n            return;\n        }\n\n        if(!may_handle(e))\n        {\n            return;\n        }\n\n        switch(e.inputType)\n        {\n            case \"insertText\":\n                for(var i = 0; i < e.data.length; i++)\n                {\n                    keyboard.simulate_char(e.data[i]);\n                }\n                break;\n\n            case \"insertLineBreak\":\n                keyboard.simulate_press(13); // enter\n                break;\n\n            case \"deleteContentBackward\":\n                keyboard.simulate_press(8); // backspace\n                break;\n        }\n    }\n\n    /**\n     * @param {KeyboardEvent|Object} e\n     * @param {boolean} keydown\n     */\n    function handler(e, keydown)\n    {\n        if(!keyboard.bus)\n        {\n            return;\n        }\n\n        if(!may_handle(e))\n        {\n            return;\n        }\n\n        if(e.code === \"\" || e.key === \"Process\" || e.key === \"Unidentified\" || e.keyCode === 229)\n        {\n            // Handling mobile browsers and virtual keyboards\n            return;\n        }\n\n        e.preventDefault && e.preventDefault();\n\n        if(PLATFOM_WINDOWS)\n        {\n            // Remove ControlLeft from key sequence [ControlLeft, AltRight] when\n            // AltGraph-key is pressed or released.\n            //\n            // NOTE: AltGraph is false for the 1st key (ControlLeft-Down), becomes\n            // true with the 2nd (AltRight-Down) and stays true until key AltGraph\n            // is released (AltRight-Up).\n            if(deferred_event)\n            {\n                clearTimeout(deferred_timeout_id);\n                if(!(e.getModifierState && e.getModifierState(\"AltGraph\") &&\n                        deferred_keydown === keydown &&\n                        deferred_event.code === \"ControlLeft\" && e.code === \"AltRight\"))\n                {\n                    handle_event(deferred_event, deferred_keydown);\n                }\n                deferred_event = null;\n            }\n\n            if(e.code === \"ControlLeft\")\n            {\n                // defer ControlLeft-Down/-Up until the next invocation of this method or 10ms have passed, whichever comes first\n                deferred_event = e;\n                deferred_keydown = keydown;\n                deferred_timeout_id = setTimeout(() => {\n                    handle_event(deferred_event, deferred_keydown);\n                    deferred_event = null;\n                }, 10);\n                return false;\n            }\n        }\n\n        handle_event(e, keydown);\n        return false;\n    }\n\n    /**\n     * @param {KeyboardEvent|Object} e\n     * @param {boolean} keydown\n     */\n    function handle_event(e, keydown)\n    {\n        var code = translate(e);\n\n        if(!code)\n        {\n            console.log(\"Missing char in map: keyCode=\" + (e.keyCode || -1).toString(16) + \" code=\" + e.code);\n            return;\n        }\n\n        handle_code(code, keydown, e.repeat);\n    }\n\n    /**\n     * @param {number} code\n     * @param {boolean} keydown\n     * @param {boolean=} is_repeat\n     */\n    function handle_code(code, keydown, is_repeat)\n    {\n        if(keydown)\n        {\n            if(keys_pressed[code] && !is_repeat)\n            {\n                handle_code(code, false);\n            }\n        }\n        else\n        {\n            if(!keys_pressed[code])\n            {\n                // stray keyup\n                return;\n            }\n        }\n\n        keys_pressed[code] = keydown;\n\n        if(!keydown)\n        {\n            code |= 0x80;\n        }\n        //console.log(\"Key: \" + code.toString(16) + \" from \" + chr.toString(16) + \" down=\" + keydown);\n\n        if(code > 0xFF)\n        {\n            // prefix\n            send_to_controller(code >> 8);\n            send_to_controller(code & 0xFF);\n        }\n        else\n        {\n            send_to_controller(code);\n        }\n    }\n\n    function send_to_controller(code)\n    {\n        keyboard.bus.send(\"keyboard-code\", code);\n    }\n}\n"
  },
  {
    "path": "src/browser/main.js",
    "content": "import { V86 } from \"./starter.js\";\nimport { LOG_NAMES } from \"../const.js\";\nimport { SyncBuffer, SyncFileBuffer } from \"../buffer.js\";\nimport { h, pad0, pads, hex_dump, dump_file, download, round_up_to_next_power_of_2 } from \"../lib.js\";\nimport { log_data, LOG_LEVEL, set_log_level } from \"../log.js\";\nimport * as iso9660 from \"../iso9660.js\";\n\n\nconst ON_LOCALHOST = !location.hostname.endsWith(\"copy.sh\");\n\nconst DEFAULT_NETWORKING_PROXIES = [\"wss://relay.widgetry.org/\", \"ws://localhost:8080/\"];\nconst DEFAULT_MEMORY_SIZE = 128;\nconst DEFAULT_VGA_MEMORY_SIZE = 8;\nconst DEFAULT_BOOT_ORDER = 0;\nconst DEFAULT_MTU = 1500;\nconst DEFAULT_NIC_TYPE = \"ne2k\";\n\nconst MAX_ARRAY_BUFFER_SIZE_MB = 2000;\n\nfunction query_append()\n{\n    const version = $(\"version\");\n    return version ? \"?\" + version.textContent : \"\";\n}\n\nfunction set_title(text)\n{\n    document.title = text + \" - v86\" +  (DEBUG ? \" - debug\" : \"\");\n    const description = document.querySelector(\"meta[name=description]\");\n    description && (description.content = \"Running \" + text);\n}\n\nfunction bool_arg(x)\n{\n    return !!x && x !== \"0\";\n}\n\nfunction format_timestamp(time)\n{\n    if(time < 60)\n    {\n        return time + \"s\";\n    }\n    else if(time < 3600)\n    {\n        return (time / 60 | 0) + \"m \" + pad0(time % 60, 2) + \"s\";\n    }\n    else\n    {\n        return (time / 3600 | 0) + \"h \" +\n            pad0((time / 60 | 0) % 60, 2) + \"m \" +\n            pad0(time % 60, 2) + \"s\";\n    }\n}\n\nfunction read_file(file)\n{\n    return new Promise((resolve, reject) => {\n        const fr = new FileReader();\n        fr.onload = () => resolve(fr.result);\n        fr.onerror = e => reject(e);\n        fr.readAsArrayBuffer(file);\n    });\n}\n\nlet progress_ticks = 0;\n\nfunction show_progress(e)\n{\n    const el = $(\"loading\");\n    el.style.display = \"block\";\n\n    const file_name = e.file_name.split(\"?\", 1)[0];\n\n    if(file_name.endsWith(\".wasm\"))\n    {\n        const parts = file_name.split(\"/\");\n        el.textContent = \"Fetching \" + parts[parts.length - 1] + \" ...\";\n        return;\n    }\n\n    if(e.file_index === e.file_count - 1 && e.loaded >= e.total - 2048)\n    {\n        // last file is (almost) loaded\n        el.textContent = \"Done downloading. Starting now ...\";\n        return;\n    }\n\n    let line = \"Downloading images \";\n\n    if(typeof e.file_index === \"number\" && e.file_count)\n    {\n        line += \"[\" + (e.file_index + 1) + \"/\" + e.file_count + \"] \";\n    }\n\n    if(e.total && typeof e.loaded === \"number\")\n    {\n        var per100 = Math.floor(e.loaded / e.total * 100);\n        per100 = Math.min(100, Math.max(0, per100));\n\n        var per50 = Math.floor(per100 / 2);\n\n        line += per100 + \"% [\";\n        line += \"#\".repeat(per50);\n        line += \" \".repeat(50 - per50) + \"]\";\n    }\n    else\n    {\n        line += \".\".repeat(progress_ticks++ % 50);\n    }\n\n    el.textContent = line;\n}\n\nfunction $(id)\n{\n    return document.getElementById(id);\n}\n\n// These values were previously stored in localStorage\nconst elements_to_restore = [\n    \"memory_size\",\n    \"video_memory_size\",\n    \"networking_proxy\",\n    \"disable_audio\",\n    \"enable_acpi\",\n    \"boot_order\",\n];\nfor(const item of elements_to_restore)\n{\n    try\n    {\n        window.localStorage.removeItem(item);\n    }\n    catch(e) {}\n}\n\nfunction onload()\n{\n    if(!window.WebAssembly)\n    {\n        alert(\"Your browser is not supported because it doesn't support WebAssembly\");\n        return;\n    }\n\n    $(\"start_emulation\").onclick = function(e)\n    {\n        start_emulation(null, null);\n        $(\"start_emulation\").blur();\n        e.preventDefault();\n    };\n\n    if(DEBUG)\n    {\n        debug_onload();\n    }\n\n    if(DEBUG && ON_LOCALHOST)\n    {\n        // don't use online relay in debug mode\n        $(\"relay_url\").value = \"ws://localhost:8080/\";\n    }\n\n    const query_args = new URLSearchParams(location.search);\n    const host = query_args.get(\"cdn\") || (ON_LOCALHOST ? \"images/\" : \"//i.copy.sh/\");\n\n    // Abandonware OS images are from https://winworldpc.com/library/operating-systems\n    const oses = [\n        {\n            id: \"archlinux\",\n            name: \"Arch Linux\",\n            memory_size: 512 * 1024 * 1024,\n            vga_memory_size: 8 * 1024 * 1024,\n            state: { url: host + \"arch_state-v3.bin.zst\" },\n            filesystem: {\n                baseurl: host + \"arch/\",\n            },\n            net_device_type: \"virtio\",\n        },\n        {\n            id: \"archlinux-boot\",\n            name: \"Arch Linux\",\n            memory_size: 512 * 1024 * 1024,\n            vga_memory_size: 8 * 1024 * 1024,\n            filesystem: {\n                baseurl: host + \"arch/\",\n                basefs: { url: host + \"fs.json\" },\n            },\n            cmdline: [\n                \"rw apm=off vga=0x344 video=vesafb:ypan,vremap:8\",\n                \"root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose\",\n                \"mitigations=off audit=0\",\n                \"init_on_free=on\",\n                \"tsc=reliable\",\n                \"random.trust_cpu=on\",\n                \"nowatchdog\",\n                \"init=/usr/bin/init-openrc net.ifnames=0 biosdevname=0\",\n            ].join(\" \"),\n            bzimage_initrd_from_filesystem: true,\n            net_device_type: \"virtio\",\n        },\n        {\n            id: \"copy/skiffos\",\n            name: \"SkiffOS\",\n            cdrom: {\n                url: host + \"skiffos/.iso\",\n                size: 124672000,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 512 * 1024 * 1024,\n        },\n        {\n            id: \"serenity\",\n            name: \"SerenityOS\",\n            hda: {\n                url: host + \"serenity-v3/.img.zst\",\n                size: 734003200,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 512 * 1024 * 1024,\n            state: { url: host + \"serenity_state-v4.bin.zst\" },\n            homepage: \"https://serenityos.org/\",\n            mac_address_translation: true,\n        },\n        {\n            id: \"serenity-boot\",\n            name: \"SerenityOS\",\n            hda: {\n                url: host + \"serenity-v3/.img.zst\",\n                size: 734003200,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 512 * 1024 * 1024,\n            homepage: \"https://serenityos.org/\",\n        },\n        {\n            id: \"redox\",\n            name: \"Redox\",\n            hda: {\n                url: host + \"redox_demo_i686_2024-09-07_1225_harddrive/.img\",\n                size: 671088640,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 1024 * 1024 * 1024,\n            state: { url: host + \"redox_state-v2.bin.zst\" },\n            homepage: \"https://www.redox-os.org/\",\n            acpi: true,\n        },\n        {\n            id: \"redox-boot\",\n            name: \"Redox\",\n            hda: {\n                url: host + \"redox_demo_i686_2024-09-07_1225_harddrive/.img\",\n                size: 671088640,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 1024 * 1024 * 1024,\n            homepage: \"https://www.redox-os.org/\",\n            acpi: true,\n        },\n        {\n            id: \"helenos\",\n            memory_size: 256 * 1024 * 1024,\n            cdrom: {\n                //url: host + \"HelenOS-0.11.2-ia32.iso\",\n                //size: 25765888,\n                url: host + \"HelenOS-0.14.1-ia32.iso\",\n                size: 25792512,\n                async: false,\n            },\n            name: \"HelenOS\",\n            homepage: \"http://www.helenos.org/\",\n        },\n        {\n            id: \"fiwix\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"FiwixOS-3.4-i386/.img\",\n                size: 1024 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"FiwixOS\",\n            homepage: \"https://www.fiwix.org/\",\n        },\n        {\n            id: \"haiku\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"haiku-v5/.img\",\n                size: 1342177280,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"haiku_state-v5.bin.zst\" },\n            name: \"Haiku\",\n            homepage: \"https://www.haiku-os.org/\",\n            acpi: true,\n        },\n        {\n            id: \"haiku-boot\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"haiku-v5/.img\",\n                size: 1342177280,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"Haiku\",\n            homepage: \"https://www.haiku-os.org/\",\n            acpi: true,\n        },\n        {\n            id: \"beos\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"beos5/.img\",\n                size: 536870912,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"BeOS 5\",\n            // NOTE: segfaults if 256k bios is used\n        },\n        {\n            id: \"msdos\",\n            hda: {\n                url: host + \"msdos622/.img\",\n                size: 64 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"MS-DOS 6.22\",\n        },\n        {\n            id: \"msdos4\",\n            fda: {\n                url: host + \"msdos4.img\",\n                size: 1474560,\n            },\n            name: \"MS-DOS 4\",\n        },\n        {\n            id: \"freedos\",\n            fda: {\n                url: host + \"freedos722.img\",\n                size: 737280,\n            },\n            name: \"FreeDOS\",\n        },\n        {\n            id: \"doof\",\n            fda: {\n                url: host + \"doof-1440.img\",\n                size: 1474560,\n            },\n            name: \"Doom On One Floppy\",\n            homepage: \"https://github.com/fragglet/squashware\",\n        },\n        {\n            id: \"quantixos\",\n            cdrom: {\n                url: host + \"quantixos.iso\",\n                size: 11784192,\n                async: false,\n            },\n            name: \"QuantixOS\",\n            homepage: \"https://github.com/MrGilli/QuantixOS\",\n        },\n        {\n            id: \"chip4504\",\n            fda: {\n                url: host + \"chip4504.img\",\n                size: 1474560,\n            },\n            name: \"Chip4504\",\n            homepage: \"https://github.com/RelativisticMechanic/chip4504\",\n        },\n        {\n            id: \"forthos\",\n            hda: {\n                url: host + \"forthos20.img.zst\",\n                size: 95420416,\n                async: false,\n            },\n            memory_size: 128 * 1024 * 1024,\n            name: \"ForthOS\",\n            homepage: \"http://sources.vsta.org/forthos/\",\n        },\n        {\n            id: \"chimaeraos\",\n            hda: {\n                url: host + \"chimaeraos.img\",\n                size: 34120704,\n                async: false,\n            },\n            name: \"Chimaera OS\",\n            homepage: \"https://chimaeraos.org/\",\n        },\n        {\n            id: \"freegem\",\n            hda: {\n                url: host + \"freegem/.bin\",\n                size: 209715200,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Freedos with FreeGEM\",\n        },\n        {\n            id: \"xcom\",\n            fda: {\n                url: host + \"xcom144.img\",\n                size: 1440 * 1024,\n            },\n            name: \"Freedos with Xcom\",\n            homepage: \"http://xcom.infora.hu/index.html\",\n        },\n        {\n            id: \"psychdos\",\n            hda: {\n                url: host + \"psychdos/.img\",\n                size: 549453824,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"PsychDOS\",\n            homepage: \"https://psychoslinux.gitlab.io/DOS/INDEX.HTM\",\n        },\n        {\n            id: \"86dos\",\n            fda: {\n                url: host + \"pc86dos.img\",\n                size: 163840,\n            },\n            name: \"86-DOS\",\n            homepage: \"https://www.os2museum.com/wp/pc-86-dos/\",\n        },\n        {\n            id: \"oberon\",\n            hda: {\n                url: host + \"oberon.img\",\n                size: 24 * 1024 * 1024,\n                async: false,\n            },\n            name: \"Oberon\",\n        },\n        {\n            id: \"windows1\",\n            fda: {\n                url: host + \"windows101.img\",\n                size: 1474560,\n            },\n            name: \"Windows 1.01\",\n        },\n        {\n            id: \"windows2\",\n            hda: {\n                url: host + \"windows2.img\",\n                size: 4177920,\n                async: false,\n            },\n            name: \"Windows 2.03\",\n        },\n        {\n            id: \"linux26\",\n            cdrom: {\n                url: host + \"linux.iso\",\n                size: 6547456,\n                async: false,\n            },\n            name: \"Linux\",\n        },\n        {\n            id: \"linux3\",\n            cdrom: {\n                url: host + \"linux3.iso\",\n                size: 8638464,\n                async: false,\n            },\n            name: \"Linux\",\n        },\n        {\n            id: \"linux4\",\n            cdrom: {\n                url: host + \"linux4.iso\",\n                size: 7731200,\n                async: false,\n            },\n            name: \"Linux\",\n            filesystem: {},\n        },\n        {\n            id: \"buildroot\",\n            bzimage: {\n                url: host + \"buildroot-bzimage.bin\",\n                size: 5166352,\n                async: false,\n            },\n            name: \"Buildroot Linux\",\n            filesystem: {},\n            cmdline: \"tsc=reliable mitigations=off random.trust_cpu=on\",\n            mouse_disabled_default: true,\n        },\n        {\n            id: \"buildroot6\",\n            bzimage: {\n                url: host + \"buildroot-bzimage68.bin\",\n                size: 10068480,\n                async: false,\n            },\n            name: \"Buildroot Linux 6.8\",\n            filesystem: {},\n            cmdline: \"tsc=reliable mitigations=off random.trust_cpu=on\",\n        },\n        {\n            id: \"basiclinux\",\n            hda: {\n                url: host + \"bl3-5.img\",\n                size: 104857600,\n                async: false,\n            },\n            name: \"BasicLinux\",\n        },\n        {\n            id: \"xpud\",\n            cdrom: {\n                url: host + \"xpud-0.9.2.iso\",\n                size: 67108864,\n                async: false,\n            },\n            name: \"xPUD\",\n            memory_size: 256 * 1024 * 1024,\n        },\n        {\n            id: \"elks\",\n            hda: {\n                url: host + \"elks-hd32-fat.img\",\n                size: 32514048,\n                async: false,\n            },\n            name: \"ELKS\",\n            homepage: \"https://github.com/ghaerr/elks\",\n        },\n        {\n            id: \"nodeos\",\n            bzimage: {\n                url: host + \"nodeos-kernel.bin\",\n                size: 14452000,\n                async: false,\n            },\n            name: \"NodeOS\",\n            cmdline: \"tsc=reliable mitigations=off random.trust_cpu=on\",\n        },\n        {\n            id: \"dsl\",\n            memory_size: 256 * 1024 * 1024,\n            cdrom: {\n                url: host + \"dsl-4.11.rc2.iso\",\n                size: 52824064,\n                async: false,\n            },\n            name: \"Damn Small Linux\",\n            homepage: \"http://www.damnsmalllinux.org/\",\n        },\n        {\n            id: \"xwoaf\",\n            memory_size: 256 * 1024 * 1024,\n            cdrom: {\n                url: host + \"xwoaf_rebuild4.iso\",\n                size: 2205696,\n                async: false,\n            },\n            name: \"xwoaf\",\n            homepage: \"https://pupngo.dk/xwinflpy/xwoaf_rebuild.html\",\n        },\n        {\n            id: \"minix\",\n            name: \"Minix\",\n            memory_size: 256 * 1024 * 1024,\n            cdrom: {\n                url: host + \"minix-3.3.0/.iso\",\n                size: 605581312,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            homepage: \"https://www.minix3.org/\",\n        },\n        {\n            id: \"unix-v7\",\n            name: \"Unix V7\",\n            hda: {\n                url: host + \"unix-v7x86-0.8a/.img\",\n                size: 152764416,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n        },\n        {\n            id: \"kolibrios\",\n            fda: {\n                url: ON_LOCALHOST ?\n                        host + \"kolibri.img\" :\n                        \"//builds.kolibrios.org/en_US/data/data/kolibri.img\",\n                size: 1474560,\n            },\n            name: \"KolibriOS\",\n            homepage: \"https://kolibrios.org/en/\",\n        },\n        {\n            id: \"kolibrios-fallback\",\n            fda: {\n                url: host + \"kolibri.img\",\n                size: 1474560,\n            },\n            name: \"KolibriOS\",\n        },\n        {\n            id: \"mu\",\n            hda: {\n                url: host + \"mu-shell.img\",\n                size: 10321920,\n                async: false,\n            },\n            memory_size: 256 * 1024 * 1024,\n            name: \"Mu\",\n            homepage: \"https://github.com/akkartik/mu\",\n            mouse_disabled_default: true, // https://github.com/akkartik/mu/issues/52\n        },\n        {\n            id: \"openbsd\",\n            hda: {\n                url: host + \"openbsd/.img\",\n                size: 1073741824,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"openbsd_state-v2.bin.zst\" },\n            memory_size: 256 * 1024 * 1024,\n            name: \"OpenBSD\",\n        },\n        {\n            id: \"sortix\",\n            cdrom: {\n                url: host + \"sortix-1.0-i686.iso\",\n                size: 71075840,\n                async: false,\n            },\n            memory_size: 512 * 1024 * 1024,\n            name: \"Sortix\",\n        },\n        {\n            id: \"openbsd-boot\",\n            hda: {\n                url: host + \"openbsd/.img\",\n                size: 1073741824,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 256 * 1024 * 1024,\n            name: \"OpenBSD\",\n            //acpi: true, // doesn't seem to work\n        },\n        {\n            id: \"netbsd\",\n            hda: {\n                url: host + \"netbsd/.img\",\n                size: 511000064,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 256 * 1024 * 1024,\n            name: \"NetBSD\",\n        },\n        {\n            id: \"crazierl\",\n            multiboot: {\n                url: host + \"crazierl-elf.img\",\n                size: 896592,\n                async: false,\n            },\n            initrd: {\n                url: host + \"crazierl-initrd.img\",\n                size: 18448316,\n                async: false,\n            },\n            acpi: true,\n            cmdline: \"kernel /libexec/ld-elf32.so.1\",\n            memory_size: 128 * 1024 * 1024,\n            name: \"Crazierl\",\n        },\n        {\n            id: \"solos\",\n            fda: {\n                url: host + \"os8.img\",\n                size: 1474560,\n            },\n            name: \"Sol OS\",\n            homepage: \"http://oby.ro/os/\",\n        },\n        {\n            id: \"bootchess\",\n            fda: {\n                url: host + \"bootchess.img\",\n                size: 1474560,\n            },\n            name: \"BootChess\",\n            homepage: \"http://www.pouet.net/prod.php?which=64962\",\n        },\n        {\n            id: \"bootbasic\",\n            fda: {\n                url: host + \"bootbasic.img\",\n                size: 512,\n            },\n            name: \"bootBASIC\",\n            homepage: \"https://github.com/nanochess/bootBASIC\",\n        },\n        {\n            id: \"bootlogo\",\n            fda: {\n                url: host + \"bootlogo.img\",\n                size: 512,\n            },\n            name: \"bootLogo\",\n            homepage: \"https://github.com/nanochess/bootLogo\",\n        },\n        {\n            id: \"pillman\",\n            fda: {\n                url: host + \"pillman.img\",\n                size: 512,\n            },\n            name: \"Pillman\",\n            homepage: \"https://github.com/nanochess/Pillman\",\n        },\n        {\n            id: \"invaders\",\n            fda: {\n                url: host + \"invaders.img\",\n                size: 512,\n            },\n            name: \"Invaders\",\n            homepage: \"https://github.com/nanochess/Invaders\",\n        },\n        {\n            id: \"bootos\",\n            fda: {\n                url: host + \"bootos-all.img\",\n                size: 368640,\n            },\n            name: \"bootOS\",\n            homepage: \"https://github.com/nanochess/bootOS\",\n        },\n        {\n            id: \"sectorlisp\",\n            fda: {\n                url: host + \"sectorlisp-friendly.bin\",\n                size: 512,\n            },\n            name: \"SectorLISP\",\n            homepage: \"https://justine.lol/sectorlisp2/\",\n        },\n        {\n            id: \"sectorforth\",\n            fda: {\n                url: host + \"sectorforth.img\",\n                size: 512,\n            },\n            name: \"sectorforth\",\n            homepage: \"https://github.com/cesarblum/sectorforth\",\n        },\n        {\n            id: \"floppybird\",\n            fda: {\n                url: host + \"floppybird.img\",\n                size: 1474560,\n            },\n            name: \"Floppy Bird\",\n            homepage: \"http://mihail.co/floppybird\",\n        },\n        {\n            id: \"stillalive\",\n            fda: {\n                url: host + \"stillalive-os.img\",\n                size: 368640,\n            },\n            name: \"Still Alive\",\n            homepage: \"https://github.com/maniekx86/stillalive-os\",\n        },\n        {\n            id: \"hello-v86\",\n            fda: {\n                url: host + \"hello-v86.img\",\n                size: 512,\n            },\n            name: \"Hello v86\",\n        },\n        {\n            id: \"tetros\",\n            fda: {\n                url: host + \"tetros.img\",\n                size: 512,\n            },\n            name: \"TetrOS\",\n            homepage: \"https://github.com/daniel-e/tetros\",\n        },\n        {\n            id: \"dino\",\n            fda: {\n                url: host + \"bootdino.img\",\n                size: 512,\n            },\n            name: \"dino\",\n            homepage: \"https://github.com/franeklubi/dino\",\n        },\n        {\n            id: \"bootrogue\",\n            fda: {\n                url: host + \"bootrogue.img\",\n                size: 512,\n            },\n            name: \"bootRogue\",\n            homepage: \"https://github.com/nanochess/bootRogue\",\n        },\n        {\n            id: \"duskos\",\n            hda: {\n                url: host + \"duskos.img\",\n                async: false,\n                size: 8388608,\n            },\n            name: \"Dusk OS\",\n            homepage: \"http://duskos.org/\",\n        },\n        {\n            id: \"windows2000\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"windows2k-v2/.img\",\n                size: 2 * 1024 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows 2000\",\n            state: { url: host + \"windows2k_state-v4.bin.zst\" },\n            mac_address_translation: true,\n        },\n        {\n            id: \"windows2000-boot\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"windows2k-v2/.img\",\n                size: 2 * 1024 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows 2000\",\n        },\n        {\n            id: \"windows-me\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"windowsme-v3/.img\",\n                size: 1073741824,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"windows-me_state-v3.bin.zst\" },\n            name: \"Windows ME\",\n        },\n        {\n            id: \"windowsnt4\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"winnt4_noacpi/.img\",\n                size: 523837440,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows NT 4.0\",\n            cpuid_level: 2,\n        },\n        {\n            id: \"windowsnt35\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"windowsnt351-v2/.img\",\n                size: 163577856,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows NT 3.51\",\n        },\n        {\n            id: \"windowsnt3\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"winnt31/.img\",\n                size: 87 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows NT 3.1\",\n        },\n        {\n            id: \"windows98\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"windows98/.img\",\n                size: 300 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows 98\",\n            state: { url: host + \"windows98_state-v2.bin.zst\" },\n            mac_address_translation: true,\n        },\n        {\n            id: \"windows98-boot\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"windows98/.img\",\n                size: 300 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows 98\",\n        },\n        {\n            id: \"windows95\",\n            memory_size: 64 * 1024 * 1024,\n            // old image:\n            //memory_size: 32 * 1024 * 1024,\n            //hda: {\n            //    url: host + \"w95/.img\",\n            //    size: 242049024,\n            //    async: true,\n            //    fixed_chunk_size: 256 * 1024,\n            //    use_parts: true,\n            //},\n            //state: { url: host + \"windows95_state.bin.zst\" },\n            hda: {\n                url: host + \"windows95-v2/.img\",\n                size: 471859200,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows 95\",\n        },\n        {\n            id: \"windows95-boot\",\n            memory_size: 64 * 1024 * 1024,\n            hda: {\n                url: host + \"windows95-v2/.img\",\n                size: 471859200,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            name: \"Windows 95\",\n        },\n        {\n            id: \"windows30-old\",\n            memory_size: 64 * 1024 * 1024,\n            cdrom: {\n                url: host + \"Win30.iso\",\n                size: 7774208,\n                async: false,\n            },\n            name: \"Windows 3.0\",\n        },\n        {\n            id: \"windows30\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"windows30.img\",\n                size: 25165824,\n                async: false,\n            },\n            name: \"Windows 3.0\",\n        },\n        {\n            id: \"windows31\",\n            memory_size: 64 * 1024 * 1024,\n            hda: {\n                url: host + \"win31.img\",\n                async: false,\n                size: 34463744,\n            },\n            name: \"Windows 3.1\",\n        },\n        {\n            id: \"tilck\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"tilck.img\",\n                async: false,\n                size: 37748736,\n            },\n            name: \"Tilck\",\n            homepage: \"https://github.com/vvaltchev/tilck\",\n        },\n        {\n            id: \"littlekernel\",\n            multiboot: {\n                url: host + \"littlekernel-multiboot.img\",\n                async: false,\n                size: 969580,\n            },\n            name: \"Little Kernel\",\n            homepage: \"https://github.com/littlekernel/lk\",\n        },\n        {\n            id: \"sanos\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"sanos-flp.img\",\n                async: false,\n                size: 1474560,\n            },\n            name: \"Sanos\",\n            homepage: \"http://www.jbox.dk/sanos/\",\n        },\n        {\n            id: \"freebsd\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"freebsd/.img\",\n                size: 2147483648,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"freebsd_state-v2.bin.zst\" },\n            name: \"FreeBSD\",\n        },\n        {\n            id: \"freebsd-boot\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"freebsd/.img\",\n                size: 2147483648,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"FreeBSD\",\n        },\n        {\n            id: \"reactos\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"reactos-v3/.img\",\n                size: 734003200,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"reactos_state-v3.bin.zst\" },\n            mac_address_translation: true,\n            name: \"ReactOS\",\n            acpi: true,\n            net_device_type: \"virtio\",\n            homepage: \"https://reactos.org/\",\n        },\n        {\n            id: \"reactos-boot\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"reactos-v2/.img\",\n                size: 681574400,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"ReactOS\",\n            acpi: true,\n            homepage: \"https://reactos.org/\",\n        },\n        {\n            id: \"skift\",\n            cdrom: {\n                url: host + \"skift-20200910.iso\",\n                size: 64452608,\n                async: false,\n            },\n            name: \"Skift\",\n            homepage: \"https://skiftos.org/\",\n        },\n        {\n            id: \"snowdrop\",\n            fda: {\n                url: host + \"snowdrop.img\",\n                size: 1440 * 1024,\n            },\n            name: \"Snowdrop\",\n            homepage: \"http://www.sebastianmihai.com/snowdrop/\",\n        },\n        {\n            id: \"openwrt\",\n            hda: {\n                url: host + \"openwrt-18.06.1-x86-legacy-combined-squashfs.img\",\n                size: 19846474,\n                async: false,\n            },\n            name: \"OpenWrt\",\n        },\n        {\n            id: \"qnx\",\n            fda: {\n                url: host + \"qnx-demo-network-4.05.img\",\n                size: 1474560,\n            },\n            name: \"QNX 4.05\",\n        },\n        {\n            id: \"9front\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"9front-10931.386/.iso\",\n                size: 489453568,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"9front_state-v3.bin.zst\" },\n            acpi: true,\n            name: \"9front\",\n            homepage: \"https://9front.org/\",\n        },\n        {\n            id: \"9front-boot\",\n            memory_size: 128 * 1024 * 1024,\n            hda: {\n                url: host + \"9front-10931.386/.iso\",\n                size: 489453568,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            acpi: true,\n            name: \"9front\",\n            homepage: \"https://9front.org/\",\n        },\n        {\n            id: \"9legacy\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"9legacy.img\",\n                async: false,\n                size: 16000000,\n            },\n            name: \"9legacy\",\n            homepage: \"http://www.9legacy.org/\",\n            //net_device_type: \"none\",\n        },\n        {\n            id: \"mobius\",\n            fda: {\n                url: host + \"mobius-fd-release5.img\",\n                size: 1474560,\n            },\n            name: \"Mobius\",\n        },\n        {\n            id: \"android\",\n            memory_size: 512 * 1024 * 1024,\n            cdrom: {\n                url: host + \"android-x86-1.6-r2/.iso\",\n                size: 54661120,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"Android\",\n        },\n        {\n            id: \"android4\",\n            memory_size: 512 * 1024 * 1024,\n            cdrom: {\n                url: host + \"android_x86_nonsse3_4.4r1_20140904/.iso\",\n                size: 247463936,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            name: \"Android 4\",\n        },\n        {\n            id: \"tinycore\",\n            memory_size: 256 * 1024 * 1024,\n            hda: {\n                url: host + \"TinyCore-11.0.iso\",\n                size: 19922944,\n                async: false,\n            },\n            name: \"Tinycore\",\n            homepage: \"http://www.tinycorelinux.net/\",\n        },\n        {\n            id: \"slitaz\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"slitaz-rolling-2024.iso\",\n                size: 56573952,\n                async: false,\n            },\n            name: \"SliTaz\",\n            homepage: \"https://slitaz.org/\",\n        },\n        {\n            id: \"freenos\",\n            memory_size: 256 * 1024 * 1024,\n            cdrom: {\n                url: host + \"FreeNOS-1.0.3.iso\",\n                async: false,\n                size: 11014144,\n            },\n            name: \"FreeNOS\",\n            acpi: true,\n            homepage: \"http://www.freenos.org/\",\n        },\n        {\n            id: \"syllable\",\n            memory_size: 512 * 1024 * 1024,\n            hda: {\n                url: host + \"syllable-destop-0.6.7/.img\",\n                async: true,\n                size: 500 * 1024 * 1024,\n                fixed_chunk_size: 512 * 1024,\n                use_parts: true,\n            },\n            name: \"Syllable\",\n            homepage: \"http://syllable.metaproject.frl/\",\n        },\n        {\n            id: \"toaruos\",\n            memory_size: 512 * 1024 * 1024,\n            cdrom: {\n                url: host + \"toaruos-1.6.1-core.iso\",\n                size: 67567616,\n                async: false,\n            },\n            name: \"ToaruOS\",\n            acpi: true,\n            homepage: \"https://toaruos.org/\",\n        },\n        {\n            id: \"nopeos\",\n            cdrom: {\n                url: host + \"nopeos-0.1.iso\",\n                size: 532480,\n                async: false,\n            },\n            name: \"Nope OS\",\n            homepage: \"https://github.com/d99kris/nopeos\",\n        },\n        {\n            id: \"soso\",\n            cdrom: {\n                url: host + \"soso.iso\",\n                size: 22546432,\n                async: false,\n            },\n            name: \"Soso\",\n            homepage: \"https://github.com/ozkl/soso\",\n        },\n        {\n            id: \"pcmos\",\n            fda: {\n                url: host + \"PCMOS386-9-user-patched.img\",\n                size: 1440 * 1024,\n            },\n            name: \"PC-MOS/386\",\n            homepage: \"https://github.com/roelandjansen/pcmos386v501\",\n        },\n        {\n            id: \"jx\",\n            fda: {\n                url: host + \"jx-demo.img\",\n                size: 1440 * 1024,\n            },\n            name: \"JX\",\n            homepage: \"https://www4.cs.fau.de/Projects/JX/index.html\",\n        },\n        {\n            id: \"house\",\n            fda: {\n                url: host + \"hOp-0.8.img\",\n                size: 1440 * 1024,\n            },\n            name: \"House\",\n            homepage: \"https://programatica.cs.pdx.edu/House/\",\n        },\n        {\n            id: \"bleskos\",\n            name: \"BleskOS\",\n            cdrom: {\n                url: host + \"bleskos_2024u32.iso\",\n                size: 1835008,\n                async: false,\n            },\n            homepage: \"https://github.com/VendelinSlezak/BleskOS\",\n        },\n        {\n            id: \"boneos\",\n            name: \"BoneOS\",\n            cdrom: {\n                url: host + \"BoneOS.iso\",\n                size: 11429888,\n                async: false,\n            },\n            homepage: \"https://amanuel.io/projects/BoneOS/\",\n        },\n        {\n            id: \"mikeos\",\n            name: \"MikeOS\",\n            cdrom: {\n                url: host + \"mikeos.iso\",\n                size: 3311616,\n                async: false,\n            },\n            homepage: \"https://mikeos.sourceforge.net/\",\n        },\n        {\n            id: \"bluejay\",\n            name: \"Blue Jay\",\n            fda: {\n                url: host + \"bj050.img\",\n                size: 1474560,\n            },\n            homepage: \"https://archiveos.org/blue-jay/\",\n        },\n        {\n            id: \"t3xforth\",\n            name: \"T3XFORTH\",\n            fda: {\n                url: host + \"t3xforth.img\",\n                size: 1474560,\n            },\n            homepage: \"https://t3x.org/t3xforth/\",\n        },\n        {\n            id: \"nanoshell\",\n            name: \"NanoShell\",\n            cdrom: {\n                url: host + \"nanoshell.iso\",\n                size: 6785024,\n                async: false,\n            },\n            homepage: \"https://github.com/iProgramMC/NanoShellOS\",\n        },\n        {\n            id: \"catk\",\n            name: \"CatK\",\n            cdrom: {\n                url: host + \"catkernel.iso\",\n                size: 11968512,\n                async: false,\n            },\n            homepage: \"https://catk.neocities.org/\",\n        },\n        {\n            id: \"mcp\",\n            name: \"M/CP\",\n            fda: {\n                url: host + \"mcp2.img\",\n                size: 512,\n            },\n            homepage: \"https://github.com/ybuzoku/MCP\",\n        },\n        {\n            id: \"ibm-exploring\",\n            name: \"Exploring The IBM Personal Computer\",\n            fda: {\n                url: host + \"ibm-exploring.img\",\n                size: 368640,\n            },\n        },\n        {\n            id: \"leetos\",\n            name: \"lEEt/OS\",\n            fda: {\n                url: host + \"leetos.img\",\n                size: 1474560,\n            },\n            homepage: \"http://sininenankka.dy.fi/leetos/index.php\",\n        },\n        {\n            id: \"newos\",\n            name: \"NewOS\",\n            fda: {\n                url: host + \"newos-flp.img\",\n                size: 1474560,\n                async: false,\n            },\n            homepage: \"https://newos.org/\",\n        },\n        {\n            id: \"newos-notion\",\n            hda: {\n                url: host + \"newos-notion.img\",\n                size: 4128768,\n                async: false,\n            },\n            memory_size: 128 * 1024 * 1024,\n            name: \"NewOS Notion\",\n            homepage: \"http://notion.muelln-kommune.net/newos.html\",\n        },\n        {\n            id: \"aros-broadway\",\n            name: \"AROS Broadway\",\n            memory_size: 512 * 1024 * 1024,\n            cdrom: {\n                url: host + \"broadway10/.iso\",\n                size: 742051840,\n                async: true,\n                fixed_chunk_size: 512 * 1024,\n                use_parts: true,\n            },\n            homepage: \"https://web.archive.org/web/20231109224346/http://www.aros-broadway.de/\",\n        },\n        {\n            id: \"icaros\",\n            name: \"Icaros Desktop\",\n            memory_size: 512 * 1024 * 1024,\n            cdrom: {\n                url: host + \"icaros-pc-i386-2.3/.iso\",\n                size: 726511616,\n                async: true,\n                // NOTE: needs 136MB/287 requests to boot, maybe state image or zst parts?\n                fixed_chunk_size: 512 * 1024,\n                use_parts: true,\n            },\n            homepage: \"http://vmwaros.blogspot.com/\",\n        },\n        {\n            id: \"tinyaros\",\n            name: \"Tiny Aros\",\n            memory_size: 512 * 1024 * 1024,\n            cdrom: {\n                url: host + \"tinyaros-pc-i386/.iso\",\n                size: 111175680,\n                async: true,\n                fixed_chunk_size: 512 * 1024,\n                use_parts: true,\n            },\n            homepage: \"https://www.tinyaros.it/\",\n        },\n        {\n            id: \"dancy\",\n            name: \"Dancy\",\n            cdrom: {\n                url: host + \"dancy.iso\",\n                size: 10485760,\n                async: false,\n            },\n            homepage: \"https://github.com/Tiihala/Dancy\",\n        },\n        {\n            id: \"curios\",\n            name: \"CuriOS\",\n            hda: {\n                url: host + \"curios.img\",\n                size: 83886080,\n                async: false,\n            },\n            homepage: \"https://github.com/h5n1xp/CuriOS\",\n        },\n        {\n            id: \"os64\",\n            name: \"OS64\",\n            cdrom: {\n                url: host + \"os64boot.iso\",\n                size: 5580800,\n                async: false,\n            },\n            homepage: \"https://os64.blogspot.com/\",\n        },\n        {\n            id: \"ipxe\",\n            name: \"iPXE\",\n            cdrom: {\n                url: host + \"ipxe.iso\",\n                size: 4194304,\n                async: false,\n            },\n            homepage: \"https://ipxe.org/\",\n        },\n        {\n            id: \"netboot.xyz\",\n            name: \"netboot.xyz\",\n            cdrom: {\n                url: host + \"netboot.xyz.iso\",\n                size: 2398208,\n                async: false,\n            },\n            homepage: \"https://netboot.xyz/\",\n            net_device_type: \"virtio\",\n        },\n        {\n            id: \"squeaknos\",\n            name: \"SqueakNOS\",\n            cdrom: {\n                url: host + \"SqueakNOS.iso\",\n                size: 61171712,\n                async: false,\n            },\n            memory_size: 512 * 1024 * 1024,\n            homepage: \"https://squeaknos.blogspot.com/\"\n        },\n        {\n            id: \"chokanji4\",\n            name: \"Chokanji 4\",\n            hda: {\n                url: host + \"chokanji4/.img.zst\",\n                size: 10737418240,\n                async: true,\n                fixed_chunk_size: 256 * 1024,\n                use_parts: true,\n            },\n            memory_size: 512 * 1024 * 1024,\n            homepage: \"https://archive.org/details/brightv4000\"\n        },\n        {\n            id: \"archhurd\",\n            name: \"Arch Hurd\",\n            hda: {\n                url: host + \"archhurd-2018.09.28/.img.zst\",\n                size: 4294967296,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            memory_size: 512 * 1024 * 1024,\n            homepage: \"https://archhurd.org/\",\n        },\n        {\n            id: \"prettyos\",\n            name: \"PrettyOS\",\n            fda: {\n                url: host + \"prettyos.img\",\n                size: 1474560,\n                async: false,\n            },\n            homepage: \"https://www.prettyos.de/Image.html\",\n        },\n        {\n            id: \"vanadium\",\n            name: \"Vanadium OS\",\n            cdrom: {\n                url: host + \"vanadiumos.iso\",\n                size: 8388608,\n                async: false,\n            },\n            homepage: \"https://www.durlej.net/software.html\",\n        },\n        {\n            id: \"xenus\",\n            name: \"XENUS\",\n            hda: {\n                url: host + \"xenushdd.img\",\n                size: 52428800,\n                async: false,\n            },\n            homepage: \"https://www.durlej.net/xenus/\",\n        },\n        {\n            id: \"mojo\",\n            name: \"Mojo OS\",\n            cdrom: {\n                url: host + \"mojo-0.2.2.iso\",\n                size: 4048896,\n                async: false,\n            },\n            homepage: \"https://archiveos.org/mojoos/\",\n        },\n        {\n            id: \"bsdos\",\n            memory_size: 128 * 1024 * 1024,\n            name: \"BSD/OS\",\n            hda: {\n                url: host + \"bsdos43/.img.zst\",\n                size: 1024 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            state: { url: host + \"bsdos43_state.bin\" },\n            homepage: \"https://en.wikipedia.org/wiki/BSD/OS\",\n        },\n        {\n            id: \"bsdos-boot\",\n            memory_size: 128 * 1024 * 1024,\n            name: \"BSD/OS\",\n            hda: {\n                url: host + \"bsdos43/.img.zst\",\n                size: 1024 * 1024 * 1024,\n                async: true,\n                fixed_chunk_size: 1024 * 1024,\n                use_parts: true,\n            },\n            homepage: \"https://en.wikipedia.org/wiki/BSD/OS\",\n        },\n        {\n            id: \"asuro\",\n            name: \"Asuro\",\n            cdrom: {\n                url: host + \"asuro.iso\",\n                size: 5361664,\n                async: false,\n            },\n            homepage: \"https://asuro.xyz/\",\n        },\n    ];\n\n    if(DEBUG)\n    {\n        // see tests/kvm-unit-tests/x86/\n        const tests = [\n            \"realmode\",\n            // All tests below require an APIC\n            \"cmpxchg8b\",\n            \"port80\",\n            \"setjmp\",\n            \"sieve\",\n            \"hypercall\", // crashes\n            \"init\", // stops execution\n            \"msr\", // TODO: Expects 64 bit msrs\n            \"smap\", // test stops, SMAP not enabled\n            \"tsc_adjust\", // TODO: IA32_TSC_ADJUST\n            \"tsc\", // TODO: rdtscp\n            \"rmap_chain\", // crashes\n            \"memory\", // missing mfence (uninteresting)\n            \"taskswitch\", // TODO: Jump\n            \"taskswitch2\", // TODO: Call TSS\n            \"eventinj\", // Missing #nt\n            \"ioapic\",\n            \"apic\",\n        ];\n\n        for(const test of tests)\n        {\n            oses.push({\n                name: \"Test case: \" + test,\n                id: \"test-\" + test,\n                memory_size: 128 * 1024 * 1024,\n                multiboot: { url: \"tests/kvm-unit-tests/x86/\" + test + \".flat\" }\n            });\n        }\n    }\n\n    const profile = query_args.get(\"profile\");\n\n    if(!profile && !DEBUG)\n    {\n        const link = document.createElement(\"link\");\n        link.rel = \"prefetch\";\n        link.href = \"build/v86.wasm\" + query_append();\n        document.head.appendChild(link);\n    }\n\n    const link = document.createElement(\"link\");\n    link.rel = \"prefetch\";\n    link.href = \"build/xterm.js\";\n    document.head.appendChild(link);\n\n    for(const os of oses)\n    {\n        if(profile === os.id)\n        {\n            start_emulation(os, query_args);\n            return;\n        }\n\n        const element = $(\"start_\" + os.id);\n\n        if(element)\n        {\n            element.onclick = e =>\n            {\n                if(!e.ctrlKey)\n                {\n                    e.preventDefault();\n                    element.blur();\n                    start_emulation(os, null);\n                }\n            };\n        }\n    }\n\n    if(profile === \"custom\")\n    {\n        // TODO: if one of the file form fields has a value (firefox), start here?\n\n        if(query_args.has(\"hda.url\") || query_args.has(\"cdrom.url\") || query_args.has(\"fda.url\"))\n        {\n            start_emulation(null, query_args);\n            return;\n        }\n    }\n    else if(/^[a-zA-Z0-9\\-_]+\\/[a-zA-Z0-9\\-_]+$/g.test(profile))\n    {\n        // experimental: server that allows user-uploaded images\n\n        const base = \"https://v86-user-images.b-cdn.net/\" + profile;\n\n        fetch(base + \"/profile.json\")\n            .catch(e => alert(\"Profile not found: \" + profile))\n            .then(response => response.json())\n            .then(p => {\n                function handle_image(o)\n                {\n                    return o && { url: base + \"/\" + o[\"url\"], async: o[\"async\"], size: o[\"size\"] };\n                }\n\n                const profile = {\n                    id: p[\"id\"],\n                    name: p[\"name\"],\n                    memory_size: p[\"memory_size\"],\n                    vga_memory_size: p[\"vga_memory_size\"],\n                    acpi: p[\"acpi\"],\n                    boot_order: p[\"boot_order\"],\n                    hda: handle_image(p[\"hda\"]),\n                    cdrom: handle_image(p[\"cdrom\"]),\n                    fda: handle_image(p[\"fda\"]),\n                    multiboot: handle_image(p[\"multiboot\"]),\n                    bzimage: handle_image(p[\"bzimage\"]),\n                    initrd: handle_image(p[\"initrd\"]),\n                };\n\n                start_emulation(profile, query_args);\n            });\n    }\n\n    if(query_args.has(\"m\")) $(\"memory_size\").value = query_args.get(\"m\");\n    if(query_args.has(\"vram\")) $(\"vga_memory_size\").value = query_args.get(\"vram\");\n    if(query_args.has(\"relay_url\")) $(\"relay_url\").value = query_args.get(\"relay_url\");\n    if(query_args.has(\"mute\")) $(\"disable_audio\").checked = bool_arg(query_args.get(\"mute\"));\n    if(query_args.has(\"acpi\")) $(\"acpi\").checked = bool_arg(query_args.get(\"acpi\"));\n    if(query_args.has(\"boot_order\")) $(\"boot_order\").value = query_args.get(\"boot_order\");\n    if(query_args.has(\"net_device_type\")) $(\"net_device_type\").value = query_args.get(\"net_device_type\");\n    if(query_args.has(\"mtu\")) $(\"mtu\").value = query_args.get(\"mtu\");\n\n    for(const dev of [\"fda\", \"fdb\"])\n    {\n        const toggle = $(dev + \"_toggle_empty_disk\");\n        if(!toggle) continue;\n\n        toggle.onclick = function(e)\n        {\n            e.preventDefault();\n            const select = document.createElement(\"select\");\n            select.id = dev + \"_empty_size\";\n            for(const n_sect of [320, 360, 400, 640, 720, 800, 1440, 2400, 2880, 3444, 5760, 7680])\n            {\n                const n_bytes = n_sect * 512, kB = 1024, MB = kB * 1000;\n                const option = document.createElement(\"option\");\n                if(n_bytes < MB)\n                {\n                    option.textContent = (n_bytes / kB) + \" kB\";\n                }\n                else\n                {\n                    option.textContent = (n_bytes / MB).toFixed(2) + \" MB\";\n                }\n                if(n_sect === 2880)\n                {\n                    option.selected = true;\n                }\n                option.value = n_bytes;\n                select.appendChild(option);\n            }\n            // TODO (when closure compiler supports it): parent.parentNode.replaceChildren(...);\n            const parent = toggle.parentNode;\n            parent.innerHTML = \"\";\n            parent.append(\"Empty disk of \", select);\n        };\n    }\n\n    for(const dev of [\"hda\", \"hdb\"])\n    {\n        const toggle = $(dev + \"_toggle_empty_disk\");\n        if(!toggle) continue;\n\n        toggle.onclick = function(e)\n        {\n            e.preventDefault();\n            const input = document.createElement(\"input\");\n            input.id = dev + \"_empty_size\";\n            input.type = \"number\";\n            input.min = \"0\";\n            input.max = String(MAX_ARRAY_BUFFER_SIZE_MB);\n            input.step = \"100\";\n            input.value = \"100\";\n            // TODO (when closure compiler supports it): parent.parentNode.replaceChildren(...);\n            const parent = toggle.parentNode;\n            parent.innerHTML = \"\";\n            parent.append(\"Empty disk of \", input, \" MB\");\n        };\n    }\n\n    const os_info = Array.from(document.querySelectorAll(\"#oses a.tr\")).map(element =>\n    {\n        const [_, size_raw, unit] = element.children[1].textContent.match(/([\\d\\.]+)\\+? (\\w+)/);\n        let size = +size_raw;\n        if(unit === \"MB\") size *= 1024 * 1024;\n        else if(unit === \"KB\") size *= 1024;\n        return {\n            element,\n            size,\n            graphical: element.children[2].firstChild.className === \"gui_icon\",\n            family: element.children[3].textContent.replace(/-like/, \"\"),\n            arch: element.children[4].textContent,\n            status: element.children[5].textContent,\n            source: element.children[6].textContent,\n            languages: new Set(element.children[7].textContent.split(\", \")),\n            medium: element.children[8].textContent,\n        };\n    });\n\n    const known_filter = [\n        [   // Family:\n            { id: \"linux\", condition: os => os.family === \"Linux\" },\n            { id: \"bsd\", condition: os => os.family === \"BSD\" },\n            { id: \"windows\", condition: os => os.family === \"Windows\" },\n            { id: \"unix\", condition: os => os.family === \"Unix\" },\n            { id: \"dos\", condition: os => os.family === \"DOS\" },\n            { id: \"custom\", condition: os => os.family === \"Custom\" },\n        ],\n        [   // UI:\n            { id: \"graphical\", condition: os => os.graphical },\n            { id: \"text\", condition: os => !os.graphical },\n        ],\n        [   // Medium:\n            { id: \"floppy\", condition: os => os.medium === \"Floppy\" },\n            { id: \"cd\", condition: os => os.medium === \"CD\" },\n            { id: \"hd\", condition: os => os.medium === \"HD\" },\n        ],\n        [   // Size:\n            { id: \"bootsector\", condition: os => os.size <= 512 },\n            { id: \"lt5mb\", condition: os => os.size <= 5 * 1024 * 1024 },\n            { id: \"gt5mb\", condition: os => os.size > 5 * 1024 * 1024 },\n        ],\n        [   // Status:\n            { id: \"modern\", condition: os => os.status === \"Modern\" },\n            { id: \"historic\", condition: os => os.status === \"Historic\" },\n        ],\n        [   // License:\n            { id: \"opensource\", condition: os => os.source === \"Open-source\" },\n            { id: \"proprietary\", condition: os => os.source === \"Proprietary\" },\n        ],\n        [   // Arch:\n            { id: \"16bit\", condition: os => os.arch === \"16-bit\" },\n            { id: \"32bit\", condition: os => os.arch === \"32-bit\" },\n        ],\n        [   // Lang:\n            { id: \"asm\", condition: os => os.languages.has(\"ASM\") },\n            { id: \"c\", condition: os => os.languages.has(\"C\") },\n            { id: \"cpp\", condition: os => os.languages.has(\"C++\") },\n            { id: \"other_lang\", condition: os => [\"ASM\", \"C\", \"C++\"].every(lang => !os.languages.has(lang)) },\n        ],\n    ];\n\n    const defined_filter = [];\n    for(const known_category of known_filter)\n    {\n        const category = known_category.filter(filter => {\n            const element = document.getElementById(`filter_${filter.id}`);\n            if(element)\n            {\n                element.onchange = update_filters;\n                filter.element = element;\n            }\n            return element;\n        });\n        if(category.length)\n        {\n            defined_filter.push(category);\n        }\n    }\n\n    function update_filters()\n    {\n        const conjunction = [];\n        for(const category of defined_filter)\n        {\n            const disjunction = category.filter(filter => filter.element.checked);\n            if(disjunction.length)\n            {\n                conjunction.push(disjunction);\n            }\n        }\n        for(const os of os_info)\n        {\n            os.element.style.display = conjunction.every(disjunction => disjunction.some(filter => filter.condition(os))) ? \"\" : \"none\";\n        }\n    }\n\n    if($(\"reset_filters\"))\n    {\n        $(\"reset_filters\").onclick = function()\n        {\n            for(const element of document.querySelectorAll(\"#filter input[type=checkbox]\"))\n            {\n                element.checked = false;\n            }\n            update_filters();\n        };\n    }\n\n    function set_proxy_value(id, value)\n    {\n        const elem = $(id);\n        if(elem)\n        {\n            elem.onclick = () => $(\"relay_url\").value = value;\n        }\n    }\n    set_proxy_value(\"network_none\", \"\");\n    set_proxy_value(\"network_inbrowser\", \"inbrowser\");\n    set_proxy_value(\"network_fetch\", \"fetch\");\n    set_proxy_value(\"network_relay\", \"wss://relay.widgetry.org/\");\n    set_proxy_value(\"network_wisp\", \"wisps://wisp.mercurywork.shop/v86/\");\n}\n\nfunction debug_onload()\n{\n    // called on window.onload, in debug mode\n\n    const log_levels = $(\"log_levels\");\n\n    if(!log_levels)\n    {\n        return;\n    }\n\n    for(let i = 0; i < LOG_NAMES.length; i++)\n    {\n        const mask = LOG_NAMES[i][0];\n\n        if(mask === 1)\n            continue;\n\n        const name = LOG_NAMES[i][1].toLowerCase();\n        const input = document.createElement(\"input\");\n        const label = document.createElement(\"label\");\n\n        input.type = \"checkbox\";\n\n        label.htmlFor = input.id = \"log_\" + name;\n\n        if(LOG_LEVEL & mask)\n        {\n            input.checked = true;\n        }\n        input.mask = mask;\n\n        label.append(input, pads(name, 4) + \" \");\n        log_levels.appendChild(label);\n\n        if(i === Math.floor(LOG_NAMES.length / 2))\n        {\n            log_levels.append(\"\\n\");\n        }\n    }\n\n    log_levels.onchange = function(e)\n    {\n        const target = e.target;\n        const mask = target.mask;\n\n        if(target.checked)\n        {\n            set_log_level(LOG_LEVEL | mask);\n        }\n        else\n        {\n            set_log_level(LOG_LEVEL & ~mask);\n        }\n\n        target.blur();\n    };\n}\n\nwindow.addEventListener(\"load\", onload, false);\n\n// old webkit fires popstate on every load, fuck webkit\n// https://code.google.com/p/chromium/issues/detail?id=63040\nwindow.addEventListener(\"load\", function()\n{\n    setTimeout(function()\n    {\n        window.addEventListener(\"popstate\", onpopstate);\n    }, 0);\n});\n\n// works in firefox and chromium\nif(document.readyState === \"complete\")\n{\n    onload();\n}\n\n// we can get here in various ways:\n// - the user clicked on the \"start emulation\" button\n// - the user clicked on a profile\n// - the ?profile= query parameter specified a valid profile\n// - the ?profile= query parameter was set to \"custom\" and at least one disk image was given\nfunction start_emulation(profile, query_args)\n{\n    $(\"boot_options\").style.display = \"none\";\n\n    const new_query_args = new Map();\n    new_query_args.set(\"profile\", profile?.id || \"custom\");\n\n    const settings = {};\n\n    if(profile)\n    {\n        if(profile.state)\n        {\n            $(\"reset\").style.display = \"none\";\n        }\n\n        set_title(profile.name);\n\n        settings.initial_state = profile.state;\n        settings.filesystem = profile.filesystem;\n        settings.fda = profile.fda;\n        settings.fdb = profile.fdb;\n        settings.cdrom = profile.cdrom;\n        settings.hda = profile.hda;\n        settings.hdb = profile.hdb;\n        settings.multiboot = profile.multiboot;\n        settings.bzimage = profile.bzimage;\n        settings.initrd = profile.initrd;\n        settings.cmdline = profile.cmdline;\n        settings.bzimage_initrd_from_filesystem = profile.bzimage_initrd_from_filesystem;\n        settings.mac_address_translation = profile.mac_address_translation;\n        settings.cpuid_level = profile.cpuid_level;\n        settings.acpi = profile.acpi;\n        settings.memory_size = profile.memory_size;\n        settings.vga_memory_size = profile.vga_memory_size;\n        settings.boot_order = profile.boot_order;\n        settings.net_device_type = profile.net_device_type;\n\n        if(!DEBUG && profile.homepage)\n        {\n            $(\"description\").style.display = \"block\";\n            const link = document.createElement(\"a\");\n            link.href = profile.homepage;\n            link.textContent = profile.name;\n            link.target = \"_blank\";\n            $(\"description\").append(document.createTextNode(\"Running \"), link);\n        }\n    }\n\n    if(query_args)\n    {\n        // ignore certain settings when using a state image\n        if(!settings.initial_state)\n        {\n            let chunk_size = parseInt(query_args.get(\"chunk_size\"), 10);\n            if(chunk_size >= 0)\n            {\n                chunk_size = Math.min(4 * 1024 * 1024, Math.max(512, chunk_size));\n                chunk_size = round_up_to_next_power_of_2(chunk_size);\n            }\n            else\n            {\n                chunk_size = 256 * 1024;\n            }\n\n            if(query_args.has(\"hda.url\"))\n            {\n                settings.hda = {\n                    size: parseInt(query_args.get(\"hda.size\"), 10) || undefined,\n                    // TODO: synchronous if small?\n                    url: query_args.get(\"hda.url\"),\n                    fixed_chunk_size: chunk_size,\n                    async: true,\n                };\n            }\n            else if(query_args.has(\"hda.empty\"))\n            {\n                const empty_size = parseInt(query_args.get(\"hda.empty\"), 10);\n                if(empty_size > 0)\n                {\n                    settings.hda = { buffer: new ArrayBuffer(empty_size) };\n                }\n            }\n\n            if(query_args.has(\"hdb.url\"))\n            {\n                settings.hdb = {\n                    size: parseInt(query_args.get(\"hdb.size\"), 10) || undefined,\n                    // TODO: synchronous if small?\n                    url: query_args.get(\"hdb.url\"),\n                    fixed_chunk_size: chunk_size,\n                    async: true,\n                };\n            }\n            else if(query_args.has(\"hdb.empty\"))\n            {\n                const empty_size = parseInt(query_args.get(\"hdb.empty\"), 10);\n                if(empty_size > 0)\n                {\n                    settings.hdb = { buffer: new ArrayBuffer(empty_size) };\n                }\n            }\n\n            if(query_args.has(\"cdrom.url\"))\n            {\n                settings.cdrom = {\n                    size: parseInt(query_args.get(\"cdrom.size\"), 10) || undefined,\n                    url: query_args.get(\"cdrom.url\"),\n                    fixed_chunk_size: chunk_size,\n                    async: true,\n                };\n            }\n\n            if(query_args.has(\"fda.url\"))\n            {\n                settings.fda = {\n                    size: parseInt(query_args.get(\"fda.size\"), 10) || undefined,\n                    url: query_args.get(\"fda.url\"),\n                    async: false,\n                };\n            }\n\n            const m = parseInt(query_args.get(\"m\"), 10);\n            if(m > 0)\n            {\n                settings.memory_size = Math.max(16, m) * 1024 * 1024;\n            }\n\n            const vram = parseInt(query_args.get(\"vram\"), 10);\n            if(vram > 0)\n            {\n                settings.vga_memory_size = vram * 1024 * 1024;\n            }\n\n            settings.acpi = query_args.has(\"acpi\") ? bool_arg(query_args.get(\"acpi\")) : settings.acpi;\n            settings.use_bochs_bios = query_args.get(\"bios\") === \"bochs\";\n            settings.net_device_type = query_args.get(\"net_device_type\") || settings.net_device_type;\n            settings.mtu = parseInt(query_args.get(\"mtu\"), 10) || undefined;\n        }\n\n        settings.relay_url = query_args.get(\"relay_url\");\n        settings.disable_jit = bool_arg(query_args.get(\"disable_jit\"));\n        settings.disable_audio = bool_arg(query_args.get(\"mute\"));\n    }\n\n    if(!settings.relay_url)\n    {\n        settings.relay_url = $(\"relay_url\").value;\n        if(!DEFAULT_NETWORKING_PROXIES.includes(settings.relay_url)) new_query_args.set(\"relay_url\", settings.relay_url);\n    }\n    if(settings.relay_url.startsWith(\"fetch:\"))\n    {\n        settings.cors_proxy = settings.relay_url.slice(6);\n        settings.relay_url = \"fetch\";\n    }\n    settings.disable_audio = $(\"disable_audio\").checked || settings.disable_audio;\n    if(settings.disable_audio) new_query_args.set(\"mute\", \"1\");\n\n    // some settings cannot be overridden when a state image is used\n    if(!settings.initial_state)\n    {\n        const bios = $(\"bios\").files[0];\n        if(bios)\n        {\n            settings.bios = { buffer: bios };\n        }\n        const vga_bios = $(\"vga_bios\").files[0];\n        if(vga_bios)\n        {\n            settings.vga_bios = { buffer: vga_bios };\n        }\n        const fda = $(\"fda_image\")?.files[0];\n        if(fda)\n        {\n            settings.fda = { buffer: fda };\n        }\n        const fda_empty_size = +$(\"fda_empty_size\")?.value;\n        if(fda_empty_size)\n        {\n            settings.fda = { buffer: new ArrayBuffer(fda_empty_size) };\n        }\n        const fdb = $(\"fdb_image\")?.files[0];\n        if(fdb)\n        {\n            settings.fdb = { buffer: fdb };\n        }\n        const fdb_empty_size = +$(\"fdb_empty_size\")?.value;\n        if(fdb_empty_size)\n        {\n            settings.fdb = { buffer: new ArrayBuffer(fdb_empty_size) };\n        }\n        const cdrom = $(\"cdrom_image\").files[0];\n        if(cdrom)\n        {\n            settings.cdrom = { buffer: cdrom };\n        }\n        const hda = $(\"hda_image\")?.files[0];\n        if(hda)\n        {\n            settings.hda = { buffer: hda };\n        }\n        const hda_empty_size = +$(\"hda_empty_size\")?.value;\n        if(hda_empty_size)\n        {\n            const size = Math.max(1, Math.min(MAX_ARRAY_BUFFER_SIZE_MB, hda_empty_size)) * 1024 * 1024;\n            settings.hda = { buffer: new ArrayBuffer(size) };\n            new_query_args.set(\"hda.empty\", String(size));\n        }\n        const hdb = $(\"hdb_image\")?.files[0];\n        if(hdb)\n        {\n            settings.hdb = { buffer: hdb };\n        }\n        const hdb_empty_size = +$(\"hdb_empty_size\")?.value;\n        if(hdb_empty_size)\n        {\n            const size = Math.max(1, Math.min(MAX_ARRAY_BUFFER_SIZE_MB, hdb_empty_size)) * 1024 * 1024;\n            settings.hdb = { buffer: new ArrayBuffer(size) };\n            new_query_args.set(\"hdb.empty\", String(size));\n        }\n        const multiboot = $(\"multiboot_image\")?.files[0];\n        if(multiboot)\n        {\n            settings.multiboot = { buffer: multiboot };\n        }\n        const bzimage = $(\"bzimage\").files[0];\n        if(bzimage)\n        {\n            settings.bzimage = { buffer: bzimage };\n        }\n        const initrd = $(\"initrd\").files[0];\n        if(initrd)\n        {\n            settings.initrd = { buffer: initrd };\n        }\n\n        const title = multiboot?.name || hda?.name || cdrom?.name || hdb?.name || fda?.name || bios?.name;\n        if(title)\n        {\n            set_title(title);\n        }\n\n        const MB = 1024 * 1024;\n\n        const memory_size = parseInt($(\"memory_size\").value, 10) || DEFAULT_MEMORY_SIZE;\n        if(!settings.memory_size || memory_size !== DEFAULT_MEMORY_SIZE)\n        {\n            settings.memory_size = memory_size * MB;\n        }\n        if(memory_size !== DEFAULT_MEMORY_SIZE) new_query_args.set(\"m\", String(memory_size));\n\n        const vga_memory_size = parseInt($(\"vga_memory_size\").value, 10) || DEFAULT_VGA_MEMORY_SIZE;\n        if(!settings.vga_memory_size || vga_memory_size !== DEFAULT_VGA_MEMORY_SIZE)\n        {\n            settings.vga_memory_size = vga_memory_size * MB;\n        }\n        if(vga_memory_size !== DEFAULT_VGA_MEMORY_SIZE) new_query_args.set(\"vram\", String(vga_memory_size));\n\n        const boot_order = parseInt($(\"boot_order\").value, 16) || DEFAULT_BOOT_ORDER;\n        if(!settings.boot_order || boot_order !== DEFAULT_BOOT_ORDER)\n        {\n            settings.boot_order = boot_order;\n        }\n        if(settings.boot_order !== DEFAULT_BOOT_ORDER) new_query_args.set(\"boot_order\", settings.boot_order.toString(16));\n\n        if(settings.acpi === undefined)\n        {\n            settings.acpi = $(\"acpi\").checked;\n            if(settings.acpi) new_query_args.set(\"acpi\", \"1\");\n        }\n\n        const BIOSPATH = \"bios/\";\n\n        if(!settings.bios)\n        {\n            settings.bios = { url: BIOSPATH + (DEBUG ? \"seabios-debug.bin\" : \"seabios.bin\") };\n        }\n        if(!settings.vga_bios)\n        {\n            settings.vga_bios = { url: BIOSPATH + (DEBUG ? \"vgabios-debug.bin\" : \"vgabios.bin\") };\n        }\n        if(settings.use_bochs_bios)\n        {\n            settings.bios = { url: BIOSPATH + \"bochs-bios.bin\" };\n            settings.vga_bios = { url: BIOSPATH + \"bochs-vgabios.bin\" };\n        }\n\n        const nic_type = $(\"net_device_type\").value || DEFAULT_NIC_TYPE;\n        if(!settings.net_device_type || nic_type !== DEFAULT_NIC_TYPE)\n        {\n            settings.net_device_type = nic_type;\n        }\n        if(settings.net_device_type !== DEFAULT_NIC_TYPE) new_query_args.set(\"net_device_type\", settings.net_device_type);\n\n        const mtu = parseInt($(\"mtu\").value, 10) || DEFAULT_MTU;\n        if(!settings.mtu || mtu !== DEFAULT_MTU)\n        {\n            settings.mtu = mtu;\n        }\n        if(settings.mtu !== DEFAULT_MTU) new_query_args.set(\"mtu\", settings.mtu.toString());\n\n    }\n\n    if(!query_args)\n    {\n        push_state(new_query_args);\n    }\n\n    const emulator = new V86({\n        wasm_path: \"build/\" + (DEBUG ? \"v86-debug.wasm\" : \"v86.wasm\") + query_append(),\n        screen: {\n            container: $(\"screen_container\"),\n            use_graphical_text: false,\n        },\n        net_device: {\n            type: settings.net_device_type || DEFAULT_NIC_TYPE,\n            relay_url: settings.relay_url,\n            cors_proxy: settings.cors_proxy,\n            mtu: settings.mtu\n        },\n        autostart: true,\n\n        memory_size: settings.memory_size,\n        vga_memory_size: settings.vga_memory_size,\n        boot_order: settings.boot_order,\n\n        bios: settings.bios,\n        vga_bios: settings.vga_bios,\n        fda: settings.fda,\n        fdb: settings.fdb,\n        hda: settings.hda,\n        hdb: settings.hdb,\n        cdrom: settings.cdrom,\n        multiboot: settings.multiboot,\n        bzimage: settings.bzimage,\n        initrd: settings.initrd,\n\n        cmdline: settings.cmdline,\n        bzimage_initrd_from_filesystem: settings.bzimage_initrd_from_filesystem,\n        acpi: settings.acpi,\n        disable_jit: settings.disable_jit,\n        initial_state: settings.initial_state,\n        filesystem: settings.filesystem || {},\n        disable_speaker: settings.disable_audio,\n        mac_address_translation: settings.mac_address_translation,\n        cpuid_level: settings.cpuid_level,\n    });\n\n    if(DEBUG) window.emulator = emulator;\n\n    emulator.add_listener(\"emulator-ready\", function()\n    {\n        if(DEBUG)\n        {\n            debug_start(emulator);\n        }\n\n        if(emulator.v86.cpu.wm.exports[\"profiler_is_enabled\"]())\n        {\n            const CLEAR_STATS = false;\n\n            const panel = document.createElement(\"pre\");\n            document.body.appendChild(panel);\n\n            setInterval(function()\n                {\n                    if(!emulator.is_running())\n                    {\n                        return;\n                    }\n\n                    panel.textContent = emulator.get_instruction_stats();\n\n                    CLEAR_STATS && emulator.v86.cpu.clear_opstats();\n                }, CLEAR_STATS ? 5000 : 1000);\n        }\n\n        if([\"dsl\", \"helenos\", \"android\", \"android4\", \"redox\", \"beos\", \"9legacy\"].includes(profile?.id))\n        {\n            setTimeout(() => {\n                // hack: Start automatically\n                emulator.keyboard_send_text(profile.id === \"9legacy\" ? \"1\\n\" : \"\\n\");\n            }, 3000);\n        }\n\n        init_ui(profile, settings, emulator);\n\n        if(query_args?.has(\"c\"))\n        {\n            setTimeout(function()\n            {\n                emulator.keyboard_send_text(query_args.get(\"c\") + \"\\n\");\n            }, 25);\n        }\n\n        if(query_args?.has(\"s\"))\n        {\n            setTimeout(function()\n            {\n                emulator.serial0_send(query_args.get(\"s\") + \"\\n\");\n            }, 25);\n        }\n\n        if(query_args?.has(\"theatre\") && bool_arg(query_args?.get(\"theatre\")))\n        {\n            $(\"toggle_theatre\").click();\n        }\n    });\n\n    emulator.add_listener(\"emulator-loaded\", function()\n    {\n        if(!emulator.v86.cpu.devices.cdrom)\n        {\n            $(\"change_cdrom_image\").style.display = \"none\";\n        }\n    });\n\n    emulator.add_listener(\"download-progress\", function(e)\n    {\n        show_progress(e);\n    });\n\n    emulator.add_listener(\"download-error\", function(e)\n    {\n        const el = $(\"loading\");\n        el.style.display = \"block\";\n        el.textContent = `Loading ${e.file_name} failed. Check your connection and reload the page to try again.`;\n    });\n}\n\n/**\n * @param {Object} settings\n * @param {V86} emulator\n */\nfunction init_ui(profile, settings, emulator)\n{\n    $(\"loading\").style.display = \"none\";\n    $(\"runtime_options\").style.display = \"block\";\n    $(\"runtime_infos\").style.display = \"block\";\n    $(\"screen_container\").style.display = \"block\";\n\n    var filesystem_is_enabled = false;\n\n    if(settings.filesystem)\n    {\n        filesystem_is_enabled = true;\n        init_filesystem_panel(emulator);\n    }\n    else\n    {\n        emulator.add_listener(\"9p-attach\", function()\n        {\n            filesystem_is_enabled = true;\n            init_filesystem_panel(emulator);\n        });\n    }\n\n    $(\"run\").onclick = function()\n    {\n        if(emulator.is_running())\n        {\n            $(\"run\").textContent = \"Run\";\n            emulator.stop();\n        }\n        else\n        {\n            $(\"run\").textContent = \"Pause\";\n            emulator.run();\n        }\n\n        $(\"run\").blur();\n    };\n\n    $(\"exit\").onclick = function()\n    {\n        emulator.destroy();\n        const url = new URL(location.href);\n        url.searchParams.delete(\"profile\");\n        location.href = url.pathname + url.search;\n    };\n\n    $(\"lock_mouse\").onclick = function()\n    {\n        if(!mouse_is_enabled)\n        {\n            $(\"toggle_mouse\").onclick();\n        }\n\n        emulator.lock_mouse();\n        $(\"lock_mouse\").blur();\n    };\n\n    var mouse_is_enabled = true;\n\n    $(\"toggle_mouse\").onclick = function()\n    {\n        mouse_is_enabled = !mouse_is_enabled;\n\n        emulator.mouse_set_enabled(mouse_is_enabled);\n        $(\"toggle_mouse\").textContent = (mouse_is_enabled ? \"Dis\" : \"En\") + \"able mouse\";\n        $(\"toggle_mouse\").blur();\n    };\n\n    if(profile?.mouse_disabled_default)\n    {\n        $(\"toggle_mouse\").onclick();\n    }\n\n    var theatre_mode = false;\n    var theatre_ui = true;\n    var theatre_zoom_to_fit = false;\n\n    function zoom_to_fit()\n    {\n        // reset size\n        emulator.screen_set_scale(1, 1);\n\n        const emulator_screen = $(\"screen_container\").getBoundingClientRect();\n        const emulator_screen_width = emulator_screen.width;\n        const emulator_screen_height = emulator_screen.height;\n\n        const viewport_screen_width = window.innerWidth;\n        const viewport_screen_height = window.innerHeight;\n\n        const n = Math.min(viewport_screen_width / emulator_screen_width, viewport_screen_height / emulator_screen_height);\n        emulator.screen_set_scale(n, n);\n    }\n\n    /**\n     * @param {boolean} enabled\n     */\n    function enable_theatre_ui(enabled)\n    {\n        theatre_ui = enabled;\n\n        $(\"runtime_options\").style.display = theatre_ui ? \"block\" : \"none\";\n        $(\"runtime_infos\").style.display = theatre_ui ? \"block\" : \"none\";\n        $(\"filesystem_panel\").style.display = (filesystem_is_enabled && theatre_ui) ? \"block\" : \"none\";\n\n        $(\"toggle_ui\").textContent = (theatre_ui ? \"Hide\" : \"Show\") + \" UI\";\n    }\n\n    /**\n     * @param {boolean} enabled\n     */\n    function enable_zoom_to_fit(enabled)\n    {\n        theatre_zoom_to_fit = enabled;\n        $(\"scale\").disabled = theatre_zoom_to_fit;\n\n        if(theatre_zoom_to_fit)\n        {\n            window.addEventListener(\"resize\", zoom_to_fit, true);\n            emulator.add_listener(\"screen-set-size\", zoom_to_fit);\n\n            zoom_to_fit();\n        }\n        else\n        {\n            window.removeEventListener(\"resize\", zoom_to_fit, true);\n            emulator.remove_listener(\"screen-set-size\", zoom_to_fit);\n\n            const n = parseFloat($(\"scale\").value) || 1;\n            emulator.screen_set_scale(n, n);\n        }\n\n        $(\"toggle_zoom_to_fit\").textContent = (theatre_zoom_to_fit ? \"Dis\" : \"En\") + \"able zoom to fit\";\n    }\n\n    /**\n     * @param {boolean} enabled\n     */\n    function enable_theatre_mode(enabled)\n    {\n        theatre_mode = enabled;\n\n        if(!theatre_ui)\n        {\n            enable_theatre_ui(true);\n        }\n\n        if(!theatre_mode && theatre_zoom_to_fit)\n        {\n            enable_zoom_to_fit(false);\n        }\n\n        for(const el of [\"screen_container\", \"runtime_options\", \"runtime_infos\", \"filesystem_panel\"])\n        {\n            $(el).classList.toggle(\"theatre_\" + el);\n        }\n\n        $(\"theatre_background\").style.display = theatre_mode ? \"block\" : \"none\";\n        $(\"toggle_zoom_to_fit\").style.display = theatre_mode ? \"inline\" : \"none\";\n        $(\"toggle_ui\").style.display = theatre_mode ? \"block\" : \"none\";\n\n        // hide scrolling\n        document.body.style.overflow = theatre_mode ? \"hidden\" : \"visible\";\n\n        $(\"toggle_theatre\").textContent = (theatre_mode ? \"Dis\" : \"En\") + \"able theatre mode\";\n    }\n\n    $(\"toggle_ui\").onclick = function()\n    {\n        enable_theatre_ui(!theatre_ui);\n        $(\"toggle_ui\").blur();\n    };\n\n    $(\"toggle_theatre\").onclick = function()\n    {\n        enable_theatre_mode(!theatre_mode);\n        $(\"toggle_theatre\").blur();\n    };\n\n    $(\"toggle_zoom_to_fit\").onclick = function()\n    {\n        enable_zoom_to_fit(!theatre_zoom_to_fit);\n        $(\"toggle_zoom_to_fit\").blur();\n    };\n\n    var last_tick = 0;\n    var running_time = 0;\n    var last_instr_counter = 0;\n    var interval = null;\n    var os_uses_mouse = false;\n    var total_instructions = 0;\n\n    function update_info()\n    {\n        var now = Date.now();\n\n        var instruction_counter = emulator.get_instruction_counter();\n\n        if(instruction_counter < last_instr_counter)\n        {\n            // 32-bit wrap-around\n            last_instr_counter -= 0x100000000;\n        }\n\n        var last_ips = instruction_counter - last_instr_counter;\n        last_instr_counter = instruction_counter;\n        total_instructions += last_ips;\n\n        var delta_time = now - last_tick;\n\n        if(delta_time)\n        {\n            running_time += delta_time;\n            last_tick = now;\n\n            $(\"speed\").textContent = (last_ips / 1000 / delta_time).toFixed(1);\n            $(\"avg_speed\").textContent = (total_instructions / 1000 / running_time).toFixed(1);\n            $(\"running_time\").textContent = format_timestamp(running_time / 1000 | 0);\n        }\n    }\n\n    emulator.add_listener(\"emulator-started\", function()\n    {\n        last_tick = Date.now();\n        interval = setInterval(update_info, 1000);\n    });\n\n    emulator.add_listener(\"emulator-stopped\", function()\n    {\n        update_info();\n        if(interval !== null)\n        {\n            clearInterval(interval);\n        }\n    });\n\n    var stats_9p = {\n        read: 0,\n        write: 0,\n        files: [],\n    };\n\n    emulator.add_listener(\"9p-read-start\", function(args)\n    {\n        const file = args[0];\n        stats_9p.files.push(file);\n        $(\"info_filesystem\").style.display = \"block\";\n        $(\"info_filesystem_status\").textContent = \"Loading ...\";\n        $(\"info_filesystem_last_file\").textContent = file;\n    });\n    emulator.add_listener(\"9p-read-end\", function(args)\n    {\n        stats_9p.read += args[1];\n        $(\"info_filesystem_bytes_read\").textContent = stats_9p.read;\n\n        const file = args[0];\n        stats_9p.files = stats_9p.files.filter(f => f !== file);\n\n        if(stats_9p.files[0])\n        {\n            $(\"info_filesystem_last_file\").textContent = stats_9p.files[0];\n        }\n        else\n        {\n            $(\"info_filesystem_status\").textContent = \"Idle\";\n        }\n    });\n    emulator.add_listener(\"9p-write-end\", function(args)\n    {\n        stats_9p.write += args[1];\n        $(\"info_filesystem_bytes_written\").textContent = stats_9p.write;\n\n        if(!stats_9p.files[0])\n        {\n            $(\"info_filesystem_last_file\").textContent = args[0];\n        }\n    });\n\n    var stats_storage = {\n        read: 0,\n        read_sectors: 0,\n        write: 0,\n        write_sectors: 0,\n    };\n\n    $(\"ide_type\").textContent = settings.cdrom ? \" (CD-ROM)\" : \" (hard disk)\";\n\n    emulator.add_listener(\"ide-read-start\", function()\n    {\n        $(\"info_storage\").style.display = \"block\";\n        $(\"info_storage_status\").textContent = \"Loading ...\";\n    });\n    emulator.add_listener(\"ide-read-end\", function(args)\n    {\n        stats_storage.read += args[1];\n        stats_storage.read_sectors += args[2];\n\n        $(\"info_storage_status\").textContent = \"Idle\";\n        $(\"info_storage_bytes_read\").textContent = stats_storage.read;\n        $(\"info_storage_sectors_read\").textContent = stats_storage.read_sectors;\n    });\n    emulator.add_listener(\"ide-write-end\", function(args)\n    {\n        stats_storage.write += args[1];\n        stats_storage.write_sectors += args[2];\n\n        $(\"info_storage_bytes_written\").textContent = stats_storage.write;\n        $(\"info_storage_sectors_written\").textContent = stats_storage.write_sectors;\n    });\n\n    var stats_net = {\n        bytes_transmitted: 0,\n        bytes_received: 0,\n    };\n\n    emulator.add_listener(\"eth-receive-end\", function(args)\n    {\n        stats_net.bytes_received += args[0];\n\n        $(\"info_network\").style.display = \"block\";\n        $(\"info_network_bytes_received\").textContent = stats_net.bytes_received;\n    });\n    emulator.add_listener(\"eth-transmit-end\", function(args)\n    {\n        stats_net.bytes_transmitted += args[0];\n\n        $(\"info_network\").style.display = \"block\";\n        $(\"info_network_bytes_transmitted\").textContent = stats_net.bytes_transmitted;\n    });\n\n\n    emulator.add_listener(\"mouse-enable\", function(is_enabled)\n    {\n        os_uses_mouse = is_enabled;\n        $(\"info_mouse_enabled\").textContent = is_enabled ? \"Yes\" : \"No\";\n    });\n\n    emulator.add_listener(\"screen-set-size\", function(args)\n    {\n        const [w, h, bpp] = args;\n        $(\"info_res\").textContent = w + \"x\" + h + (bpp ? \"x\" + bpp : \"\");\n        $(\"info_vga_mode\").textContent = bpp ? \"Graphical\" : \"Text\";\n    });\n\n\n    $(\"reset\").onclick = function()\n    {\n        emulator.restart();\n        $(\"reset\").blur();\n    };\n\n    add_image_download_button(settings.hda, () => emulator.v86.cpu.devices.ide.primary.master.buffer, \"hda\");\n    add_image_download_button(settings.hdb, () => emulator.v86.cpu.devices.ide.primary.slave.buffer, \"hdb\");\n    add_image_download_button(settings.fda, () => emulator.v86.cpu.devices.fdc.drives[0].buffer, \"fda\");\n    add_image_download_button(settings.fdb, () => emulator.v86.cpu.devices.fdc.drives[1].buffer, \"fdb\");\n    add_image_download_button(settings.cdrom, () => emulator.v86.cpu.devices.cdrom.buffer, \"cdrom\");\n\n    function add_image_download_button(obj, get_buffer, type)\n    {\n        var elem = $(\"get_\" + type + \"_image\");\n\n        if(!obj || obj.async)\n        {\n            elem.style.display = \"none\";\n        }\n\n        elem.onclick = function(e)\n        {\n            const buffer = get_buffer();\n            const filename = buffer.file && buffer.file.name || ((profile?.id || \"v86\") + \"-\" + type + (type === \"cdrom\" ? \".iso\" : \".img\"));\n\n            if(buffer.get_as_file)\n            {\n                var file = buffer.get_as_file(filename);\n                download(file, filename);\n            }\n            else\n            {\n                buffer.get_buffer(function(b)\n                {\n                    if(b)\n                    {\n                        dump_file(b, filename);\n                    }\n                    else\n                    {\n                        alert(\"The file could not be loaded. Maybe it's too big?\");\n                    }\n                });\n            }\n\n            elem.blur();\n        };\n    }\n\n    function pick_file(multiple)\n    {\n        return new Promise(resolve => {\n            const file_input = document.createElement(\"input\");\n            file_input.type = \"file\";\n            file_input.multiple = multiple;\n            file_input.onchange = function()\n            {\n                resolve(file_input.files);\n            };\n            file_input.oncancel = function()\n            {\n                resolve([]);\n            };\n            file_input.click();\n        });\n    }\n\n    $(\"change_fda_image\").textContent = settings.fda ? \"Eject floppy image\" : \"Insert floppy image\";\n    $(\"change_fda_image\").ondragover = function(e)\n    {\n        e.preventDefault();\n    };\n    async function insert_fda(files)\n    {\n        const file = files[0];\n        if(file)\n        {\n            await emulator.set_fda({ buffer: file });\n            $(\"change_fda_image\").textContent = \"Eject floppy image\";\n            $(\"get_fda_image\").style.display = \"block\";\n        }\n    }\n    $(\"change_fda_image\").ondrop = function(e)\n    {\n        e.preventDefault();\n        if(emulator.get_disk_fda())\n        {\n            emulator.eject_fda();\n        }\n        insert_fda(e.dataTransfer.files);\n    };\n    $(\"change_fda_image\").onclick = async function()\n    {\n        if(emulator.get_disk_fda())\n        {\n            emulator.eject_fda();\n            $(\"change_fda_image\").textContent = \"Insert floppy image\";\n            $(\"get_fda_image\").style.display = \"none\";\n        }\n        else\n        {\n            const files = await pick_file(false);\n            insert_fda(files);\n        }\n        $(\"change_fda_image\").blur();\n    };\n\n    $(\"change_fdb_image\").textContent = settings.fdb ? \"Eject second floppy image\" : \"Insert second floppy image\";\n    $(\"change_fdb_image\").ondragover = function(e)\n    {\n        e.preventDefault();\n    };\n    async function insert_fdb(files)\n    {\n        const file = files[0];\n        if(file)\n        {\n            await emulator.set_fdb({ buffer: file });\n            $(\"change_fdb_image\").textContent = \"Eject second floppy image\";\n            $(\"get_fdb_image\").style.display = \"block\";\n        }\n    }\n    $(\"change_fdb_image\").ondrop = function(e)\n    {\n        e.preventDefault();\n        if(emulator.get_disk_fdb())\n        {\n            emulator.eject_fdb();\n        }\n        insert_fdb(e.dataTransfer.files);\n    };\n    $(\"change_fdb_image\").onclick = async function()\n    {\n        if(emulator.get_disk_fdb())\n        {\n            emulator.eject_fdb();\n            $(\"change_fdb_image\").textContent = \"Insert second floppy image\";\n            $(\"get_fdb_image\").style.display = \"none\";\n        }\n        else\n        {\n            const files = await pick_file(false);\n            insert_fdb(files);\n        }\n        $(\"change_fdb_image\").blur();\n    };\n\n    $(\"change_cdrom_image\").textContent = settings.cdrom ? \"Eject CD image\" : \"Insert CD image\";\n    $(\"change_cdrom_image\").ondragover = function(e)\n    {\n        e.preventDefault();\n    };\n    async function insert_cdrom(files)\n    {\n        let buffer;\n\n        if(files.length === 1 && /\\.(iso(9660|img)?|cdr)$/i.test(files[0].name))\n        {\n            buffer = files[0];\n        }\n        else if(files.length)\n        {\n            const files2 = [];\n            for(const file of files)\n            {\n                files2.push({\n                    name: file.name,\n                    contents: new Uint8Array(await read_file(file)),\n                });\n\n            }\n            buffer = iso9660.generate(files2).buffer;\n        }\n\n        if(buffer)\n        {\n            await emulator.set_cdrom({ buffer });\n            $(\"change_cdrom_image\").textContent = \"Eject CD image\";\n            $(\"get_cdrom_image\").style.display = \"block\";\n        }\n    }\n    $(\"change_cdrom_image\").ondrop = function(e)\n    {\n        e.preventDefault();\n        if(emulator.v86.cpu.devices.cdrom.has_disk())\n        {\n            emulator.eject_cdrom();\n        }\n        insert_cdrom(e.dataTransfer.files);\n    };\n    $(\"change_cdrom_image\").onclick = async function()\n    {\n        if(emulator.v86.cpu.devices.cdrom.has_disk())\n        {\n            emulator.eject_cdrom();\n            $(\"change_cdrom_image\").textContent = \"Insert CD image\";\n            $(\"get_cdrom_image\").style.display = \"none\";\n        }\n        else\n        {\n            const files = await pick_file(true);\n            insert_cdrom(files);\n        }\n        $(\"change_cdrom_image\").blur();\n    };\n\n    $(\"memory_dump\").onclick = function()\n    {\n        const mem8 = emulator.v86.cpu.mem8;\n        dump_file(new Uint8Array(mem8.buffer, mem8.byteOffset, mem8.length), \"v86memory.bin\");\n        $(\"memory_dump\").blur();\n    };\n\n    //$(\"memory_dump_dmp\").onclick = function()\n    //{\n    //    var memory = emulator.v86.cpu.mem8;\n    //    var memory_size = memory.length;\n    //    var page_size = 4096;\n    //    var header = new Uint8Array(4096);\n    //    var header32 = new Int32Array(header.buffer);\n\n    //    header32[0] = 0x45474150; // 'PAGE'\n    //    header32[1] = 0x504D5544; // 'DUMP'\n\n    //    header32[0x10 >> 2] = emulator.v86.cpu.cr[3]; // DirectoryTableBase\n    //    header32[0x24 >> 2] = 1; // NumberProcessors\n    //    header32[0xf88 >> 2] = 1; // DumpType: full dump\n    //    header32[0xfa0 >> 2] = header.length + memory_size; // RequiredDumpSpace\n\n    //    header32[0x064 + 0 >> 2] = 1; // NumberOfRuns\n    //    header32[0x064 + 4 >> 2] = memory_size / page_size; // NumberOfPages\n    //    header32[0x064 + 8 >> 2] = 0; // BasePage\n    //    header32[0x064 + 12 >> 2] = memory_size / page_size; // PageCount\n\n    //    dump_file([header, memory], \"v86memory.dmp\");\n\n    //    $(\"memory_dump_dmp\").blur();\n    //};\n\n    /**\n     * @this HTMLElement\n     */\n    $(\"capture_network_traffic\").onclick = function()\n    {\n        this.textContent = \"0 packets\";\n\n        let capture = [];\n\n        function do_capture(direction, data)\n        {\n            capture.push({ direction, time: performance.now() / 1000, hex_dump: hex_dump(data) });\n            $(\"capture_network_traffic\").textContent = capture.length + \" packets\";\n        }\n\n        emulator.emulator_bus.register(\"net0-receive\", do_capture.bind(this, \"I\"));\n        emulator.add_listener(\"net0-send\", do_capture.bind(this, \"O\"));\n\n        this.onclick = function()\n        {\n            const capture_raw = capture.map(({ direction, time, hex_dump }) => {\n                // https://www.wireshark.org/docs/wsug_html_chunked/ChIOImportSection.html\n                // In wireshark: file -> import from hex -> tick direction indication, timestamp %s.%f\n                return direction + \" \" + time.toFixed(6) + hex_dump + \"\\n\";\n            }).join(\"\");\n            dump_file(capture_raw, \"traffic.hex\");\n            capture = [];\n            this.textContent = \"0 packets\";\n        };\n    };\n\n\n    $(\"save_state\").onclick = async function()\n    {\n        const result = await emulator.save_state();\n        dump_file(result, \"v86state.bin\");\n\n        $(\"save_state\").blur();\n    };\n\n    $(\"load_state\").onclick = async function()\n    {\n        $(\"load_state\").blur();\n\n        const files = await pick_file(false);\n        const file = files[0];\n\n        if(!file)\n        {\n            return;\n        }\n\n        const was_running = emulator.is_running();\n\n        if(was_running)\n        {\n            await emulator.stop();\n        }\n\n        const filereader = new FileReader();\n        filereader.onload = async function(e)\n        {\n            try\n            {\n                await emulator.restore_state(e.target.result);\n            }\n            catch(err)\n            {\n                alert(\"Something bad happened while restoring the state:\\n\" + err + \"\\n\\n\" +\n                      \"Note that the current configuration must be the same as the original\");\n                throw err;\n            }\n\n            if(was_running)\n            {\n                emulator.run();\n            }\n        };\n        filereader.readAsArrayBuffer(file);\n    };\n\n    $(\"ctrlaltdel\").onclick = function()\n    {\n        emulator.keyboard_send_scancodes([\n            0x1D, // ctrl\n            0x38, // alt\n            0x53, // delete\n\n            // break codes\n            0x1D | 0x80,\n            0x38 | 0x80,\n            0x53 | 0x80,\n        ]);\n\n        $(\"ctrlaltdel\").blur();\n    };\n\n    $(\"alttab\").onclick = function()\n    {\n        emulator.keyboard_send_scancodes([\n            0x38, // alt\n            0x0F, // tab\n        ]);\n\n        setTimeout(function()\n        {\n            emulator.keyboard_send_scancodes([\n                0x38 | 0x80,\n                0x0F | 0x80,\n            ]);\n        }, 100);\n\n        $(\"alttab\").blur();\n    };\n\n    /**\n     * @this HTMLElement\n     */\n    $(\"scale\").onchange = function()\n    {\n        var n = parseFloat(this.value);\n\n        if(n || n > 0)\n        {\n            emulator.screen_set_scale(n, n);\n        }\n    };\n\n    $(\"fullscreen\").onclick = function()\n    {\n        emulator.screen_go_fullscreen();\n    };\n\n    $(\"screen_container\").onclick = function(e)\n    {\n        if(emulator.is_running() && emulator.speaker_adapter?.audio_context?.state === \"suspended\")\n        {\n            emulator.speaker_adapter.audio_context.resume();\n        }\n\n        if(mouse_is_enabled && os_uses_mouse)\n        {\n            emulator.lock_mouse();\n        }\n\n        // allow text selection\n        if(window.getSelection().isCollapsed)\n        {\n            const phone_keyboard = document.getElementsByClassName(\"phone_keyboard\")[0];\n\n            phone_keyboard.style.top = window.scrollY + e.clientY + 20 + \"px\";\n            phone_keyboard.style.left = window.scrollX + e.clientX + \"px\";\n\n            // clean after previous input\n            phone_keyboard.value = \"\";\n            phone_keyboard.focus();\n        }\n    };\n\n    const phone_keyboard = document.getElementsByClassName(\"phone_keyboard\")[0];\n\n    phone_keyboard.setAttribute(\"autocorrect\", \"off\");\n    phone_keyboard.setAttribute(\"autocapitalize\", \"off\");\n    phone_keyboard.setAttribute(\"spellcheck\", \"false\");\n    phone_keyboard.tabIndex = 0;\n\n    $(\"take_screenshot\").onclick = function()\n    {\n        const image = emulator.screen_make_screenshot();\n        try {\n            const w = window.open(\"\");\n            w.document.write(image.outerHTML);\n        }\n        catch(e) {}\n        $(\"take_screenshot\").blur();\n    };\n\n    if(emulator.speaker_adapter)\n    {\n        let is_muted = false;\n\n        $(\"mute\").onclick = function()\n        {\n            if(is_muted)\n            {\n                emulator.speaker_adapter.mixer.set_volume(1, undefined);\n                is_muted = false;\n                $(\"mute\").textContent = \"Mute\";\n            }\n            else\n            {\n                emulator.speaker_adapter.mixer.set_volume(0, undefined);\n                is_muted = true;\n                $(\"mute\").textContent = \"Unmute\";\n            }\n\n            $(\"mute\").blur();\n        };\n    }\n    else\n    {\n        $(\"mute\").remove();\n    }\n\n    window.addEventListener(\"keydown\", ctrl_w_rescue, false);\n    window.addEventListener(\"keyup\", ctrl_w_rescue, false);\n    window.addEventListener(\"blur\", ctrl_w_rescue, false);\n\n    function ctrl_w_rescue(e)\n    {\n        if(e.ctrlKey)\n        {\n            window.onbeforeunload = function()\n            {\n                window.onbeforeunload = null;\n                return \"CTRL-W cannot be sent to the emulator.\";\n            };\n        }\n        else\n        {\n            window.onbeforeunload = null;\n        }\n    }\n\n    const script = document.createElement(\"script\");\n    script.src = \"build/xterm.js\";\n    script.async = true;\n    script.onload = function()\n    {\n        emulator.set_serial_container_xtermjs($(\"terminal\"));\n        emulator.serial_adapter.term.write(\"This is the serial console. Whatever you type or paste here will be sent to COM1\");\n    };\n    document.body.appendChild(script);\n}\n\nfunction init_filesystem_panel(emulator)\n{\n    $(\"filesystem_panel\").style.display = \"block\";\n\n    /**\n     * @this HTMLElement\n     */\n    $(\"filesystem_send_file\").onchange = function()\n    {\n        Array.prototype.forEach.call(this.files, function(file)\n        {\n            var loader = new SyncFileBuffer(file);\n            loader.onload = function()\n            {\n                loader.get_buffer(async function(buffer)\n                {\n                    await emulator.create_file(\"/\" + file.name, new Uint8Array(buffer));\n                });\n            };\n            loader.load();\n        }, this);\n\n        this.value = \"\";\n        this.blur();\n    };\n\n    /**\n     * @this HTMLElement\n     */\n    $(\"filesystem_get_file\").onkeypress = async function(e)\n    {\n        if(e.which !== 13)\n        {\n            return;\n        }\n\n        this.disabled = true;\n\n        let result;\n        try\n        {\n             result = await emulator.read_file(this.value);\n        }\n        catch(err)\n        {\n            console.log(err);\n        }\n\n        this.disabled = false;\n\n        if(result)\n        {\n            var filename = this.value.replace(/\\/$/, \"\").split(\"/\");\n            filename = filename[filename.length - 1] || \"root\";\n\n            dump_file(result, filename);\n            this.value = \"\";\n        }\n        else\n        {\n            alert(\"Can't read file\");\n        }\n    };\n}\n\nfunction debug_start(emulator)\n{\n    if(!emulator.v86)\n    {\n        return;\n    }\n\n    // called as soon as soon as emulation is started, in debug mode\n    const cpu = emulator.v86.cpu;\n\n    $(\"dump_gdt\").onclick = cpu.dump_gdt_ldt.bind(cpu);\n    $(\"dump_idt\").onclick = cpu.dump_idt.bind(cpu);\n    $(\"dump_regs\").onclick = () => { cpu.dump_regs_short(); cpu.dump_state(); };\n    $(\"dump_pt\").onclick = cpu.dump_page_structures.bind(cpu);\n\n    $(\"dump_log\").onclick = function()\n    {\n        dump_file(log_data.join(\"\"), \"v86.log\");\n    };\n\n    $(\"debug_panel\").style.display = \"block\";\n    setInterval(function()\n    {\n        $(\"debug_panel\").textContent =\n            cpu.get_regs_short().join(\"\\n\") + \"\\n\" + cpu.debug_get_state();\n\n        $(\"dump_log\").textContent = \"Dump log\" + (log_data.length ? \" (\" + log_data.length + \" lines)\" : \"\");\n    }, 1000);\n\n    // helps debugging\n    window.cpu = cpu;\n    window.h = h;\n    window.dump_file = dump_file;\n}\n\nfunction onpopstate(e)\n{\n    location.reload();\n}\n\nfunction push_state(params)\n{\n    if(window.history.pushState)\n    {\n        let search = \"?\" + Array.from(params.entries()).map(([key, value]) => key + \"=\" + value.replace(/[?&=#+]/g, encodeURIComponent)).join(\"&\");\n        window.history.pushState({ search }, \"\", search);\n    }\n}\n"
  },
  {
    "path": "src/browser/mouse.js",
    "content": "import { dbg_log } from \"../log.js\";\n\n// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/**\n * @constructor\n *\n * @param {BusConnector} bus\n */\nexport function MouseAdapter(bus, screen_container)\n{\n    const SPEED_FACTOR = 1;\n\n    var left_down = false,\n        right_down = false,\n        middle_down = false,\n\n        last_x = 0,\n        last_y = 0,\n\n        mouse = this;\n\n    // set by controller\n    this.enabled = false;\n\n    // set by emulator\n    this.emu_enabled = true;\n\n    this.bus = bus;\n\n    this.bus.register(\"mouse-enable\", function(enabled)\n    {\n        this.enabled = enabled;\n    }, this);\n\n    // TODO: Should probably not use bus for this\n    this.is_running = false;\n    this.bus.register(\"emulator-stopped\", function()\n    {\n        this.is_running = false;\n    }, this);\n    this.bus.register(\"emulator-started\", function()\n    {\n        this.is_running = true;\n    }, this);\n\n    this.destroy = function()\n    {\n        if(typeof window === \"undefined\")\n        {\n            return;\n        }\n        window.removeEventListener(\"touchstart\", touch_start_handler, false);\n        window.removeEventListener(\"touchend\", touch_end_handler, false);\n        window.removeEventListener(\"touchmove\", mousemove_handler, false);\n        window.removeEventListener(\"mousemove\", mousemove_handler, false);\n        window.removeEventListener(\"mousedown\", mousedown_handler, false);\n        window.removeEventListener(\"mouseup\", mouseup_handler, false);\n        window.removeEventListener(\"wheel\", mousewheel_handler, { passive: false });\n    };\n\n    this.init = function()\n    {\n        if(typeof window === \"undefined\")\n        {\n            return;\n        }\n        this.destroy();\n\n        window.addEventListener(\"touchstart\", touch_start_handler, false);\n        window.addEventListener(\"touchend\", touch_end_handler, false);\n        window.addEventListener(\"touchmove\", mousemove_handler, false);\n        window.addEventListener(\"mousemove\", mousemove_handler, false);\n        window.addEventListener(\"mousedown\", mousedown_handler, false);\n        window.addEventListener(\"mouseup\", mouseup_handler, false);\n        window.addEventListener(\"wheel\", mousewheel_handler, { passive: false });\n    };\n    this.init();\n\n    function is_child(child, parent)\n    {\n        while(child.parentNode)\n        {\n            if(child === parent)\n            {\n                return true;\n            }\n            child = child.parentNode;\n        }\n\n        return false;\n    }\n\n    function may_handle(e)\n    {\n        if(!mouse.enabled || !mouse.emu_enabled)\n        {\n            return false;\n        }\n\n        const MOVE_MOUSE_WHEN_OVER_SCREEN_ONLY = true;\n\n        if(MOVE_MOUSE_WHEN_OVER_SCREEN_ONLY)\n        {\n            var parent = screen_container || document.body;\n            return document.pointerLockElement || is_child(e.target, parent);\n        }\n        else\n        {\n            if(e.type === \"mousemove\" || e.type === \"touchmove\")\n            {\n                return true;\n            }\n\n            if(e.type === \"mousewheel\" || e.type === \"DOMMouseScroll\")\n            {\n                return is_child(e.target, parent);\n            }\n\n            return !e.target || e.target.nodeName !== \"INPUT\" && e.target.nodeName !== \"TEXTAREA\";\n        }\n    }\n\n    function touch_start_handler(e)\n    {\n        if(may_handle(e))\n        {\n            var touches = e[\"changedTouches\"];\n\n            if(touches && touches.length)\n            {\n                var touch = touches[touches.length - 1];\n                last_x = touch.clientX;\n                last_y = touch.clientY;\n            }\n        }\n    }\n\n    function touch_end_handler(e)\n    {\n        if(left_down || middle_down || right_down)\n        {\n            mouse.bus.send(\"mouse-click\", [false, false, false]);\n            left_down = middle_down = right_down = false;\n        }\n    }\n\n    function mousemove_handler(e)\n    {\n        if(!mouse.bus)\n        {\n            return;\n        }\n\n        if(!may_handle(e))\n        {\n            return;\n        }\n\n        if(!mouse.is_running)\n        {\n            return;\n        }\n\n        var delta_x = 0;\n        var delta_y = 0;\n\n        var touches = e[\"changedTouches\"];\n\n        if(touches)\n        {\n            if(touches.length)\n            {\n                var touch = touches[touches.length - 1];\n                delta_x = touch.clientX - last_x;\n                delta_y = touch.clientY - last_y;\n\n                last_x = touch.clientX;\n                last_y = touch.clientY;\n\n                e.preventDefault();\n            }\n        }\n        else\n        {\n            if(typeof e[\"movementX\"] === \"number\")\n            {\n                delta_x = e[\"movementX\"];\n                delta_y = e[\"movementY\"];\n            }\n            else if(typeof e[\"webkitMovementX\"] === \"number\")\n            {\n                delta_x = e[\"webkitMovementX\"];\n                delta_y = e[\"webkitMovementY\"];\n            }\n            else if(typeof e[\"mozMovementX\"] === \"number\")\n            {\n                delta_x = e[\"mozMovementX\"];\n                delta_y = e[\"mozMovementY\"];\n            }\n            else\n            {\n                // Fallback for other browsers?\n                delta_x = e.clientX - last_x;\n                delta_y = e.clientY - last_y;\n\n                last_x = e.clientX;\n                last_y = e.clientY;\n            }\n        }\n\n        delta_x *= SPEED_FACTOR;\n        delta_y *= SPEED_FACTOR;\n\n        //if(Math.abs(delta_x) > 100 || Math.abs(delta_y) > 100)\n        //{\n        //    // Large mouse delta, drop?\n        //}\n\n        delta_y = -delta_y;\n\n        // NOTE: affected by https://issues.chromium.org/issues/40737979\n        //       Causes cursor jumps on multi-monitor and/or 120+ HZ monitors\n\n        mouse.bus.send(\"mouse-delta\", [delta_x, delta_y]);\n\n        if(screen_container)\n        {\n            const absolute_x = e.pageX - screen_container.offsetLeft;\n            const absolute_y = e.pageY - screen_container.offsetTop;\n            mouse.bus.send(\"mouse-absolute\", [\n                absolute_x, absolute_y, screen_container.offsetWidth, screen_container.offsetHeight]);\n        }\n    }\n\n    function mousedown_handler(e)\n    {\n        if(may_handle(e))\n        {\n            click_event(e, true);\n        }\n    }\n\n    function mouseup_handler(e)\n    {\n        if(may_handle(e))\n        {\n            click_event(e, false);\n        }\n    }\n\n    function click_event(e, down)\n    {\n        if(!mouse.bus)\n        {\n            return;\n        }\n\n        if(e.which === 1)\n        {\n            left_down = down;\n        }\n        else if(e.which === 2)\n        {\n            middle_down = down;\n        }\n        else if(e.which === 3)\n        {\n            right_down = down;\n        }\n        else\n        {\n            dbg_log(\"Unknown event.which: \" + e.which);\n        }\n        mouse.bus.send(\"mouse-click\", [left_down, middle_down, right_down]);\n        e.preventDefault();\n    }\n\n    function mousewheel_handler(e)\n    {\n        if(!may_handle(e))\n        {\n            return;\n        }\n\n        var delta_x = e.wheelDelta || -e.detail;\n        var delta_y = 0;\n\n        if(delta_x < 0)\n        {\n            delta_x = -1;\n        }\n        else if(delta_x > 0)\n        {\n            delta_x = 1;\n        }\n\n        mouse.bus.send(\"mouse-wheel\", [delta_x, delta_y]);\n        e.preventDefault();\n    }\n}\n"
  },
  {
    "path": "src/browser/network.js",
    "content": "// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/**\n * An ethernet-through-websocket adapter, to be used with\n *     https://github.com/benjamincburns/websockproxy\n *\n * emulated ethernet card <--> this <--> websocket proxy <--> network\n *\n * @constructor\n *\n * @param {string} url\n * @param {BusConnector} bus\n * @param {number} [id=0] id\n */\nexport function NetworkAdapter(url, bus, id)\n{\n    this.bus = bus;\n    this.socket = undefined;\n    this.id = id || 0;\n\n    // TODO: circular buffer?\n    this.send_queue = [];\n    this.url = url;\n\n    this.reconnect_interval = 10000;\n    this.last_connect_attempt = Date.now() - this.reconnect_interval;\n    this.send_queue_limit = 64;\n    this.destroyed = false;\n\n    this.bus.register(\"net\" + this.id + \"-send\", function(data)\n    {\n        this.send(data);\n    }, this);\n}\n\nNetworkAdapter.prototype.handle_message = function(e)\n{\n    if(this.bus)\n    {\n        this.bus.send(\"net\" + this.id + \"-receive\", new Uint8Array(e.data));\n    }\n};\n\nNetworkAdapter.prototype.handle_close = function(e)\n{\n    //console.log(\"onclose\", e);\n\n    if(!this.destroyed)\n    {\n        this.connect();\n        setTimeout(this.connect.bind(this), this.reconnect_interval);\n    }\n};\n\nNetworkAdapter.prototype.handle_open = function(e)\n{\n    //console.log(\"open\", e);\n\n    for(var i = 0; i < this.send_queue.length; i++)\n    {\n        this.send(this.send_queue[i]);\n    }\n\n    this.send_queue = [];\n};\n\nNetworkAdapter.prototype.handle_error = function(e)\n{\n    //console.log(\"onerror\", e);\n};\n\nNetworkAdapter.prototype.destroy = function()\n{\n    this.destroyed = true;\n    if(this.socket)\n    {\n        this.socket.close();\n    }\n};\n\nNetworkAdapter.prototype.connect = function()\n{\n    if(typeof WebSocket === \"undefined\")\n    {\n        return;\n    }\n\n    if(this.socket)\n    {\n        var state = this.socket.readyState;\n\n        if(state === 0 || state === 1)\n        {\n            // already or almost there\n            return;\n        }\n    }\n\n    var now = Date.now();\n\n    if(this.last_connect_attempt + this.reconnect_interval > now)\n    {\n        return;\n    }\n\n    this.last_connect_attempt = Date.now();\n\n    try\n    {\n        this.socket = new WebSocket(this.url);\n    }\n    catch(e)\n    {\n        console.error(e);\n        return;\n    }\n\n    this.socket.binaryType = \"arraybuffer\";\n\n    this.socket.onopen = this.handle_open.bind(this);\n    this.socket.onmessage = this.handle_message.bind(this);\n    this.socket.onclose = this.handle_close.bind(this);\n    this.socket.onerror = this.handle_error.bind(this);\n};\n\nNetworkAdapter.prototype.send = function(data)\n{\n    //console.log(\"send\", data);\n\n    if(!this.socket || this.socket.readyState !== 1)\n    {\n        this.send_queue.push(data);\n\n        if(this.send_queue.length > 2 * this.send_queue_limit)\n        {\n            this.send_queue = this.send_queue.slice(-this.send_queue_limit);\n        }\n\n        this.connect();\n    }\n    else\n    {\n        this.socket.send(data);\n    }\n};\n\nNetworkAdapter.prototype.change_proxy = function(url)\n{\n    this.url = url;\n\n    if(this.socket)\n    {\n        this.socket.onclose = function() {};\n        this.socket.onerror = function() {};\n        this.socket.close();\n        this.socket = undefined;\n    }\n};\n"
  },
  {
    "path": "src/browser/print_stats.js",
    "content": "import { pads } from \"../lib.js\";\n\nexport function stats_to_string(cpu)\n{\n    return print_misc_stats(cpu) + print_instruction_counts(cpu);\n}\n\nfunction print_misc_stats(cpu)\n{\n    let text = \"\";\n\n    const stat_names = [\n        \"COMPILE\",\n        \"COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS\",\n        \"COMPILE_WRONG_ADDRESS_SPACE\",\n        \"COMPILE_CUT_OFF_AT_END_OF_PAGE\",\n        \"COMPILE_WITH_LOOP_SAFETY\",\n        \"COMPILE_PAGE\",\n        \"COMPILE_PAGE/COMPILE\",\n        \"COMPILE_BASIC_BLOCK\",\n        \"COMPILE_DUPLICATED_BASIC_BLOCK\",\n        \"COMPILE_WASM_BLOCK\",\n        \"COMPILE_WASM_LOOP\",\n        \"COMPILE_DISPATCHER\",\n        \"COMPILE_ENTRY_POINT\",\n        \"COMPILE_WASM_TOTAL_BYTES\",\n        \"COMPILE_WASM_TOTAL_BYTES/COMPILE_PAGE\",\n        \"RUN_INTERPRETED\",\n        \"RUN_INTERPRETED_NEW_PAGE\",\n        \"RUN_INTERPRETED_PAGE_HAS_CODE\",\n        \"RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK\",\n        \"RUN_INTERPRETED_NEAR_END_OF_PAGE\",\n        \"RUN_INTERPRETED_DIFFERENT_STATE\",\n        \"RUN_INTERPRETED_DIFFERENT_STATE_CPL3\",\n        \"RUN_INTERPRETED_DIFFERENT_STATE_FLAT\",\n        \"RUN_INTERPRETED_DIFFERENT_STATE_IS32\",\n        \"RUN_INTERPRETED_DIFFERENT_STATE_SS32\",\n        \"RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED\",\n        \"RUN_INTERPRETED_STEPS\",\n        \"RUN_FROM_CACHE\",\n        \"RUN_FROM_CACHE_STEPS\",\n        \"RUN_FROM_CACHE_STEPS/RUN_FROM_CACHE\",\n        \"RUN_FROM_CACHE_STEPS/RUN_INTERPRETED_STEPS\",\n        \"DIRECT_EXIT\",\n        \"INDIRECT_JUMP\",\n        \"INDIRECT_JUMP_NO_ENTRY\",\n        \"NORMAL_PAGE_CHANGE\",\n        \"NORMAL_FALLTHRU\",\n        \"NORMAL_FALLTHRU_WITH_TARGET_BLOCK\",\n        \"NORMAL_BRANCH\",\n        \"NORMAL_BRANCH_WITH_TARGET_BLOCK\",\n        \"CONDITIONAL_JUMP\",\n        \"CONDITIONAL_JUMP_PAGE_CHANGE\",\n        \"CONDITIONAL_JUMP_EXIT\",\n        \"CONDITIONAL_JUMP_FALLTHRU\",\n        \"CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK\",\n        \"CONDITIONAL_JUMP_BRANCH\",\n        \"CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK\",\n        \"DISPATCHER_SMALL\",\n        \"DISPATCHER_LARGE\",\n        \"LOOP\",\n        \"LOOP_SAFETY\",\n        \"CONDITION_OPTIMISED\",\n        \"CONDITION_UNOPTIMISED\",\n        \"CONDITION_UNOPTIMISED_PF\",\n        \"CONDITION_UNOPTIMISED_UNHANDLED_L\",\n        \"CONDITION_UNOPTIMISED_UNHANDLED_LE\",\n        \"FAILED_PAGE_CHANGE\",\n        \"SAFE_READ_FAST\",\n        \"SAFE_READ_SLOW_PAGE_CROSSED\",\n        \"SAFE_READ_SLOW_NOT_VALID\",\n        \"SAFE_READ_SLOW_NOT_USER\",\n        \"SAFE_READ_SLOW_IN_MAPPED_RANGE\",\n        \"SAFE_WRITE_FAST\",\n        \"SAFE_WRITE_SLOW_PAGE_CROSSED\",\n        \"SAFE_WRITE_SLOW_NOT_VALID\",\n        \"SAFE_WRITE_SLOW_NOT_USER\",\n        \"SAFE_WRITE_SLOW_IN_MAPPED_RANGE\",\n        \"SAFE_WRITE_SLOW_READ_ONLY\",\n        \"SAFE_WRITE_SLOW_HAS_CODE\",\n        \"SAFE_READ_WRITE_FAST\",\n        \"SAFE_READ_WRITE_SLOW_PAGE_CROSSED\",\n        \"SAFE_READ_WRITE_SLOW_NOT_VALID\",\n        \"SAFE_READ_WRITE_SLOW_NOT_USER\",\n        \"SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE\",\n        \"SAFE_READ_WRITE_SLOW_READ_ONLY\",\n        \"SAFE_READ_WRITE_SLOW_HAS_CODE\",\n        \"PAGE_FAULT\",\n        \"TLB_MISS\",\n        \"MAIN_LOOP\",\n        \"MAIN_LOOP_IDLE\",\n        \"DO_MANY_CYCLES\",\n        \"CYCLE_INTERNAL\",\n        \"INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES\",\n        \"INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED\",\n        \"INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE\",\n        \"INVALIDATE_MODULE_DIRTY_PAGE\",\n        \"INVALIDATE_PAGE_HAD_CODE\",\n        \"INVALIDATE_PAGE_HAD_ENTRY_POINTS\",\n        \"DIRTY_PAGE_DID_NOT_HAVE_CODE\",\n        \"RUN_FROM_CACHE_EXIT_SAME_PAGE\",\n        \"RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE\",\n        \"RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE\",\n        \"CLEAR_TLB\",\n        \"FULL_CLEAR_TLB\",\n        \"TLB_FULL\",\n        \"TLB_GLOBAL_FULL\",\n        \"MODRM_SIMPLE_REG\",\n        \"MODRM_SIMPLE_REG_WITH_OFFSET\",\n        \"MODRM_SIMPLE_CONST_OFFSET\",\n        \"MODRM_COMPLEX\",\n        \"SEG_OFFSET_OPTIMISED\",\n        \"SEG_OFFSET_NOT_OPTIMISED\",\n        \"SEG_OFFSET_NOT_OPTIMISED_ES\",\n        \"SEG_OFFSET_NOT_OPTIMISED_FS\",\n        \"SEG_OFFSET_NOT_OPTIMISED_GS\",\n        \"SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT\",\n    ];\n\n    let j = 0;\n    const stat_values = {};\n    for(let i = 0; i < stat_names.length; i++)\n    {\n        const name = stat_names[i];\n        let value;\n        if(name.includes(\"/\"))\n        {\n            j++; // skip profiler_stat_get\n            const [left, right] = name.split(\"/\");\n            value = stat_values[left] / stat_values[right];\n        }\n        else\n        {\n            const stat = stat_values[name] = cpu.wm.exports[\"profiler_stat_get\"](i - j);\n            value = stat >= 100e6 ? Math.round(stat / 1e6) + \"m\" : stat >= 100e3 ? Math.round(stat / 1e3) + \"k\" : stat;\n        }\n        text += name + \"=\" + value + \"\\n\";\n    }\n\n    text += \"\\n\";\n\n    const tlb_entries = cpu.wm.exports[\"get_valid_tlb_entries_count\"]();\n    const global_tlb_entries = cpu.wm.exports[\"get_valid_global_tlb_entries_count\"]();\n    const nonglobal_tlb_entries = tlb_entries - global_tlb_entries;\n\n    text += \"TLB_ENTRIES=\" + tlb_entries + \" (\" + global_tlb_entries + \" global, \" + nonglobal_tlb_entries + \" non-global)\\n\";\n    text += \"WASM_TABLE_FREE=\" + cpu.wm.exports[\"jit_get_wasm_table_index_free_list_count\"]() + \"\\n\";\n    text += \"JIT_CACHE_SIZE=\" + cpu.wm.exports[\"jit_get_cache_size\"]() + \"\\n\";\n    text += \"FLAT_SEGMENTS=\" + cpu.wm.exports[\"has_flat_segmentation\"]() + \"\\n\";\n\n    text += \"wasm memory size: \" + (cpu.wasm_memory.buffer.byteLength >> 20) + \"m\\n\";\n\n    text += \"Config:\\n\";\n    text += \"JIT_DISABLED=\" + cpu.wm.exports[\"get_jit_config\"](0) + \"\\n\";\n    text += \"MAX_PAGES=\" + cpu.wm.exports[\"get_jit_config\"](1) + \"\\n\";\n    text += \"JIT_USE_LOOP_SAFETY=\" + Boolean(cpu.wm.exports[\"get_jit_config\"](2)) + \"\\n\";\n    text += \"MAX_EXTRA_BASIC_BLOCKS=\" + cpu.wm.exports[\"get_jit_config\"](3) + \"\\n\";\n\n    return text;\n}\n\nfunction print_instruction_counts(cpu)\n{\n    return [\n        print_instruction_counts_offset(cpu, false, false, false, false),\n        print_instruction_counts_offset(cpu, true, false, false, false),\n        print_instruction_counts_offset(cpu, false, true, false, false),\n        print_instruction_counts_offset(cpu, false, false, true, false),\n        print_instruction_counts_offset(cpu, false, false, false, true),\n    ].join(\"\\n\\n\");\n}\n\nfunction print_instruction_counts_offset(cpu, compiled, jit_exit, unguarded_register, wasm_size)\n{\n    let text = \"\";\n\n    const counts = [];\n\n    const label =\n        compiled ? \"compiled\" :\n        jit_exit ? \"jit exit\" :\n        unguarded_register ? \"unguarded register\" :\n        wasm_size ? \"wasm size\" :\n        \"executed\";\n\n    for(let opcode = 0; opcode < 0x100; opcode++)\n    {\n        for(let fixed_g = 0; fixed_g < 8; fixed_g++)\n        {\n            for(const is_mem of [false, true])\n            {\n                const count = cpu.wm.exports[\"get_opstats_buffer\"](compiled, jit_exit, unguarded_register, wasm_size, opcode, false, is_mem, fixed_g);\n                counts.push({ opcode, count, is_mem, fixed_g });\n\n                const count_0f = cpu.wm.exports[\"get_opstats_buffer\"](compiled, jit_exit, unguarded_register, wasm_size, opcode, true, is_mem, fixed_g);\n                counts.push({ opcode: 0x0f00 | opcode, count: count_0f, is_mem, fixed_g });\n            }\n        }\n    }\n\n    let total = 0;\n    const prefixes = new Set([\n        0x26, 0x2E, 0x36, 0x3E,\n        0x64, 0x65, 0x66, 0x67,\n        0xF0, 0xF2, 0xF3,\n    ]);\n    for(const { count, opcode } of counts)\n    {\n        if(!prefixes.has(opcode))\n        {\n            total += count;\n        }\n    }\n\n    if(total === 0)\n    {\n        return \"\";\n    }\n\n    const per_opcode = new Uint32Array(0x100);\n    const per_opcode0f = new Uint32Array(0x100);\n\n    for(const { opcode, count } of counts)\n    {\n        if((opcode & 0xFF00) === 0x0F00)\n        {\n            per_opcode0f[opcode & 0xFF] += count;\n        }\n        else\n        {\n            per_opcode[opcode & 0xFF] += count;\n        }\n    }\n\n    text += \"------------------\\n\";\n    text += \"Total: \" + total + \"\\n\";\n\n    const factor = total > 1e7 ? 1000 : 1;\n\n    const max_count = Math.max.apply(Math,\n        counts.map(({ count }) => Math.round(count / factor))\n    );\n    const pad_length = String(max_count).length;\n\n    text += `Instruction counts ${label} (in ${factor}):\\n`;\n\n    for(let i = 0; i < 0x100; i++)\n    {\n        text += i.toString(16).padStart(2, \"0\") + \":\" + pads(Math.round(per_opcode[i] / factor), pad_length);\n\n        if(i % 16 === 15)\n            text += \"\\n\";\n        else\n            text += \" \";\n    }\n\n    text += \"\\n\";\n    text += `Instruction counts ${label} (0f, in ${factor}):\\n`;\n\n    for(let i = 0; i < 0x100; i++)\n    {\n        text += (i & 0xFF).toString(16).padStart(2, \"0\") + \":\" + pads(Math.round(per_opcode0f[i] / factor), pad_length);\n\n        if(i % 16 === 15)\n            text += \"\\n\";\n        else\n            text += \" \";\n    }\n    text += \"\\n\";\n\n    const top_counts = counts.filter(({ count }) => count).sort(({ count: count1 }, { count: count2 }) => count2 - count1);\n\n    for(const { opcode, is_mem, fixed_g, count } of top_counts.slice(0, 200))\n    {\n        const opcode_description = opcode.toString(16) + \"_\" + fixed_g + (is_mem ? \"_m\" : \"_r\");\n        text += opcode_description + \":\" + (count / total * 100).toFixed(2) + \" \";\n    }\n    text += \"\\n\";\n\n    return text;\n}\n"
  },
  {
    "path": "src/browser/screen.js",
    "content": "import { dbg_assert } from \"../log.js\";\nimport { get_charmap } from \"../lib.js\";\n\n// Draws entire buffer and visualizes the layers that would be drawn\nexport const DEBUG_SCREEN_LAYERS = DEBUG && false;\n\n/**\n * Adapter to use visual screen in browsers (in contrast to node)\n * @constructor\n * @param {Object} options\n * @param {function()} screen_fill_buffer\n */\nexport function ScreenAdapter(options, screen_fill_buffer)\n{\n    const screen_container = options.container;\n    this.screen_fill_buffer = screen_fill_buffer;\n\n    console.assert(screen_container, \"options.container must be provided\");\n\n    const MODE_TEXT = 0;\n    const MODE_GRAPHICAL = 1;\n    const MODE_GRAPHICAL_TEXT = 2;\n\n    const CHARACTER_INDEX = 0;\n    const FLAGS_INDEX = 1;\n    const BG_COLOR_INDEX = 2;\n    const FG_COLOR_INDEX = 3;\n    const TEXT_BUF_COMPONENT_SIZE = 4;\n\n    const FLAG_BLINKING = 0x01;\n    const FLAG_FONT_PAGE_B = 0x02;\n\n    this.FLAG_BLINKING = FLAG_BLINKING;\n    this.FLAG_FONT_PAGE_B = FLAG_FONT_PAGE_B;\n\n    let graphic_screen = screen_container.getElementsByTagName(\"canvas\")[0];\n    if(!graphic_screen)\n    {\n        graphic_screen = document.createElement(\"canvas\");\n        screen_container.appendChild(graphic_screen);\n    }\n    const graphic_context = graphic_screen.getContext(\"2d\", { alpha: false });\n\n    let text_screen = screen_container.getElementsByTagName(\"div\")[0];\n    if(!text_screen)\n    {\n        text_screen = document.createElement(\"div\");\n        screen_container.appendChild(text_screen);\n    }\n\n    const cursor_element = document.createElement(\"div\");\n\n    var\n        /** @type {number} */\n        cursor_row,\n\n        /** @type {number} */\n        cursor_col,\n\n        /** @type {number} */\n        scale_x = options.scale !== undefined ? options.scale : 1,\n\n        /** @type {number} */\n        scale_y = options.scale !== undefined ? options.scale : 1,\n\n        base_scale = 1,\n\n        changed_rows,\n\n        // current display mode: MODE_GRAPHICAL or either MODE_TEXT/MODE_GRAPHICAL_TEXT\n        mode,\n\n        // Index 0: ASCII code\n        // Index 1: Flags bitset (see FLAG_...)\n        // Index 2: Background color\n        // Index 3: Foreground color\n        text_mode_data,\n\n        // number of columns\n        text_mode_width,\n\n        // number of rows\n        text_mode_height,\n\n        // graphical text mode's offscreen canvas contexts\n        offscreen_context,\n        offscreen_extra_context,\n\n        // fonts\n        font_context,\n        font_image_data,\n        font_is_visible = new Int8Array(8 * 256),\n        font_height,\n        font_width,\n        font_width_9px,\n        font_width_dbl,\n        font_copy_8th_col,\n        font_page_a = 0,\n        font_page_b = 0,\n\n        // blink state\n        blink_visible,\n        tm_last_update = 0,\n\n        // cursor attributes\n        cursor_start,\n        cursor_end,\n        cursor_enabled,\n\n        // 8-bit-text to Unicode character map\n        charmap = get_charmap(options.encoding),\n\n        // render loop state\n        timer_id = 0,\n        paused = false;\n\n    // 0x12345 -> \"#012345\"\n    function number_as_color(n)\n    {\n        n = n.toString(16);\n        return \"#\" + \"0\".repeat(6 - n.length) + n;\n    }\n\n    function render_font_bitmap(vga_bitmap)\n    {\n        // - Browsers impose limts on the X- and Y-axes of bitmaps (typically around 8 to 32k).\n        //   Draw the 8 VGA font pages of 256 glyphs in 8 rows of 256 columns, this results\n        //   in 2048, 2304 or 4096px on the X-axis (for 8, 9 or 16px VGA font width, resp.).\n        //   This 2d layout is also convenient for glyph lookup when rendering text.\n        // - Font bitmap pixels are black and either fully opaque (alpha 255) or fully transparent (0).\n        const bitmap_width = font_width * 256;\n        const bitmap_height = font_height * 8;\n\n        let font_canvas = font_context ? font_context.canvas : null;\n        if(!font_canvas || font_canvas.width !== bitmap_width || font_canvas.height !== bitmap_height)\n        {\n            if(!font_canvas)\n            {\n                font_canvas = new OffscreenCanvas(bitmap_width, bitmap_height);\n                font_context = font_canvas.getContext(\"2d\");\n            }\n            else\n            {\n                font_canvas.width = bitmap_width;\n                font_canvas.height = bitmap_height;\n            }\n            font_image_data = font_context.createImageData(bitmap_width, bitmap_height);\n        }\n\n        const font_bitmap = font_image_data.data;\n        let i_dst = 0, is_visible;\n        const put_bit = font_width_dbl ?\n            function(value)\n            {\n                is_visible = is_visible || value;\n                font_bitmap[i_dst + 3] = value;\n                font_bitmap[i_dst + 7] = value;\n                i_dst += 8;\n            } :\n            function(value)\n            {\n                is_visible = is_visible || value;\n                font_bitmap[i_dst + 3] = value;\n                i_dst += 4;\n            };\n\n        // move i_vga from end of glyph to start of next glyph\n        const vga_inc_chr = 32 - font_height;\n        // move i_dst from end of font page (bitmap row) to start of next font page\n        const dst_inc_row = bitmap_width * (font_height - 1) * 4;\n        // move i_dst from end of glyph (bitmap column) to start of next glyph\n        const dst_inc_col = (font_width - bitmap_width * font_height) * 4;\n        // move i_dst from end of a glyph's scanline to start of its next scanline\n        const dst_inc_line = font_width * 255 * 4;\n\n        for(let i_chr_all = 0, i_vga = 0; i_chr_all < 2048; ++i_chr_all, i_vga += vga_inc_chr, i_dst += dst_inc_col)\n        {\n            const i_chr = i_chr_all % 256;\n            if(i_chr_all && !i_chr)\n            {\n                i_dst += dst_inc_row;\n            }\n            is_visible = false;\n            for(let i_line = 0; i_line < font_height; ++i_line, ++i_vga, i_dst += dst_inc_line)\n            {\n                const line_bits = vga_bitmap[i_vga];\n                for(let i_bit = 0x80; i_bit > 0; i_bit >>= 1)\n                {\n                    put_bit(line_bits & i_bit ? 255 : 0);\n                }\n                if(font_width_9px)\n                {\n                    put_bit(font_copy_8th_col && i_chr >= 0xC0 && i_chr <= 0xDF && line_bits & 1 ? 255 : 0);\n                }\n            }\n            font_is_visible[i_chr_all] = is_visible ? 1 : 0;\n        }\n\n        font_context.putImageData(font_image_data, 0, 0);\n    }\n\n    function render_changed_rows()\n    {\n        const font_canvas = font_context.canvas;\n        const offscreen_extra_canvas = offscreen_extra_context.canvas;\n        const txt_row_size = text_mode_width * TEXT_BUF_COMPONENT_SIZE;\n        const gfx_width = text_mode_width * font_width;\n        const row_extra_1_y = 0;\n        const row_extra_2_y = font_height;\n\n        let n_rows_rendered = 0;\n        for(let row_i = 0, row_y = 0, txt_i = 0; row_i < text_mode_height; ++row_i, row_y += font_height)\n        {\n            if(!changed_rows[row_i])\n            {\n                txt_i += txt_row_size;\n                continue;\n            }\n            ++n_rows_rendered;\n\n            // clear extra row 2\n            offscreen_extra_context.clearRect(0, row_extra_2_y, gfx_width, font_height);\n\n            let fg_rgba, fg_x, bg_rgba, bg_x;\n            for(let col_x = 0; col_x < gfx_width; col_x += font_width, txt_i += TEXT_BUF_COMPONENT_SIZE)\n            {\n                const chr = text_mode_data[txt_i + CHARACTER_INDEX];\n                const chr_flags = text_mode_data[txt_i + FLAGS_INDEX];\n                const chr_bg_rgba = text_mode_data[txt_i + BG_COLOR_INDEX];\n                const chr_fg_rgba = text_mode_data[txt_i + FG_COLOR_INDEX];\n                const chr_font_page = chr_flags & FLAG_FONT_PAGE_B ? font_page_b : font_page_a;\n                const chr_visible = (!(chr_flags & FLAG_BLINKING) || blink_visible) && font_is_visible[(chr_font_page << 8) + chr];\n\n                if(bg_rgba !== chr_bg_rgba)\n                {\n                    if(bg_rgba !== undefined)\n                    {\n                        // draw opaque block of background color into offscreen_context\n                        offscreen_context.fillStyle = number_as_color(bg_rgba);\n                        offscreen_context.fillRect(bg_x, row_y, col_x - bg_x, font_height);\n                    }\n                    bg_rgba = chr_bg_rgba;\n                    bg_x = col_x;\n                }\n\n                if(fg_rgba !== chr_fg_rgba)\n                {\n                    if(fg_rgba !== undefined)\n                    {\n                        // draw opaque block of foreground color into extra row 1\n                        offscreen_extra_context.fillStyle = number_as_color(fg_rgba);\n                        offscreen_extra_context.fillRect(fg_x, row_extra_1_y, col_x - fg_x, font_height);\n                    }\n                    fg_rgba = chr_fg_rgba;\n                    fg_x = col_x;\n                }\n\n                if(chr_visible)\n                {\n                    // copy transparent glyphs into extra row 2\n                    offscreen_extra_context.drawImage(font_canvas,\n                        chr * font_width, chr_font_page * font_height, font_width, font_height,\n                        col_x, row_extra_2_y, font_width, font_height);\n                }\n            }\n\n            // draw rightmost block of foreground color into extra row 1\n            offscreen_extra_context.fillStyle = number_as_color(fg_rgba);\n            offscreen_extra_context.fillRect(fg_x, row_extra_1_y, gfx_width - fg_x, font_height);\n\n            // combine extra row 1 (colors) and 2 (glyphs) into extra row 1 (colored glyphs)\n            offscreen_extra_context.globalCompositeOperation = \"destination-in\";\n            offscreen_extra_context.drawImage(offscreen_extra_canvas,\n                0, row_extra_2_y, gfx_width, font_height,\n                0, row_extra_1_y, gfx_width, font_height);\n            offscreen_extra_context.globalCompositeOperation = \"source-over\";\n\n            // draw rightmost block of background color into offscreen_context\n            offscreen_context.fillStyle = number_as_color(bg_rgba);\n            offscreen_context.fillRect(bg_x, row_y, gfx_width - bg_x, font_height);\n\n            // copy colored glyphs from extra row 1 into offscreen_context (on top of background colors)\n            offscreen_context.drawImage(offscreen_extra_canvas,\n                0, row_extra_1_y, gfx_width, font_height,\n                0, row_y, gfx_width, font_height);\n        }\n\n        if(n_rows_rendered)\n        {\n            if(blink_visible && cursor_enabled && changed_rows[cursor_row])\n            {\n                const cursor_txt_i = (cursor_row * text_mode_width + cursor_col) * TEXT_BUF_COMPONENT_SIZE;\n                const cursor_rgba = text_mode_data[cursor_txt_i + FG_COLOR_INDEX];\n                offscreen_context.fillStyle = number_as_color(cursor_rgba);\n                offscreen_context.fillRect(\n                    cursor_col * font_width,\n                    cursor_row * font_height + cursor_start,\n                    font_width,\n                    cursor_end - cursor_start + 1);\n            }\n            changed_rows.fill(0);\n        }\n\n        return n_rows_rendered;\n    }\n\n    function mark_blinking_rows_dirty()\n    {\n        const txt_row_size = text_mode_width * TEXT_BUF_COMPONENT_SIZE;\n        for(let row_i = 0, txt_i = 0; row_i < text_mode_height; ++row_i)\n        {\n            if(changed_rows[row_i])\n            {\n                txt_i += txt_row_size;\n                continue;\n            }\n            for(let col_i = 0; col_i < text_mode_width; ++col_i, txt_i += TEXT_BUF_COMPONENT_SIZE)\n            {\n                if(text_mode_data[txt_i + FLAGS_INDEX] & FLAG_BLINKING)\n                {\n                    changed_rows[row_i] = 1;\n                    txt_i += txt_row_size - col_i * TEXT_BUF_COMPONENT_SIZE;\n                    break;\n                }\n            }\n        }\n    }\n\n    this.init = function()\n    {\n        // setup text mode cursor DOM element\n        cursor_element.classList.add(\"cursor\");\n        cursor_element.style.position = \"absolute\";\n        cursor_element.style.backgroundColor = \"#ccc\";\n        cursor_element.style.width = \"7px\";\n        cursor_element.style.display = \"inline-block\";\n\n        // initialize display mode and size to 80x25 text with 9x16 font\n        this.set_mode(false);\n        this.set_size_text(80, 25);\n        if(mode === MODE_GRAPHICAL_TEXT)\n        {\n            this.set_size_graphical(720, 400, 720, 400);\n        }\n\n        // initialize CSS scaling\n        this.set_scale(scale_x, scale_y);\n\n        this.timer();\n    };\n\n    this.make_screenshot = function()\n    {\n        const image = new Image();\n\n        if(mode === MODE_GRAPHICAL || mode === MODE_GRAPHICAL_TEXT)\n        {\n            image.src = graphic_screen.toDataURL(\"image/png\");\n        }\n        else\n        {\n            // Default 720x400, but can be [8, 16] at 640x400\n            const char_size = [9, 16];\n\n            const canvas = document.createElement(\"canvas\");\n            canvas.width = text_mode_width * char_size[0];\n            canvas.height = text_mode_height * char_size[1];\n            const context = canvas.getContext(\"2d\");\n            context.imageSmoothingEnabled = false;\n            context.font = window.getComputedStyle(text_screen).font;\n            context.textBaseline = \"top\";\n\n            for(let y = 0; y < text_mode_height; y++)\n            {\n                for(let x = 0; x < text_mode_width; x++)\n                {\n                    const index = (y * text_mode_width + x) * TEXT_BUF_COMPONENT_SIZE;\n                    const character = text_mode_data[index + CHARACTER_INDEX];\n                    const bg_color = text_mode_data[index + BG_COLOR_INDEX];\n                    const fg_color = text_mode_data[index + FG_COLOR_INDEX];\n\n                    context.fillStyle = number_as_color(bg_color);\n                    context.fillRect(x * char_size[0], y * char_size[1], char_size[0], char_size[1]);\n                    context.fillStyle = number_as_color(fg_color);\n                    context.fillText(charmap[character], x * char_size[0], y * char_size[1]);\n                }\n            }\n\n            if(cursor_element.style.display !== \"none\" && cursor_row < text_mode_height && cursor_col < text_mode_width)\n            {\n                context.fillStyle = cursor_element.style.backgroundColor;\n                context.fillRect(\n                    cursor_col * char_size[0],\n                    cursor_row * char_size[1] + parseInt(cursor_element.style.marginTop, 10),\n                    parseInt(cursor_element.style.width, 10),\n                    parseInt(cursor_element.style.height, 10)\n                );\n            }\n\n            image.src = canvas.toDataURL(\"image/png\");\n        }\n        return image;\n    };\n\n    this.put_char = function(row, col, chr, flags, bg_color, fg_color)\n    {\n        dbg_assert(row >= 0 && row < text_mode_height);\n        dbg_assert(col >= 0 && col < text_mode_width);\n        dbg_assert(chr >= 0 && chr < 0x100);\n\n        const p = TEXT_BUF_COMPONENT_SIZE * (row * text_mode_width + col);\n\n        text_mode_data[p + CHARACTER_INDEX] = chr;\n        text_mode_data[p + FLAGS_INDEX] = flags;\n        text_mode_data[p + BG_COLOR_INDEX] = bg_color;\n        text_mode_data[p + FG_COLOR_INDEX] = fg_color;\n\n        changed_rows[row] = 1;\n    };\n\n    this.timer = function()\n    {\n        timer_id = requestAnimationFrame(() => this.update_screen());\n    };\n\n    this.update_screen = function()\n    {\n        if(!paused)\n        {\n            if(mode === MODE_TEXT)\n            {\n                this.update_text();\n            }\n            else if(mode === MODE_GRAPHICAL)\n            {\n                this.update_graphical();\n            }\n            else\n            {\n                this.update_graphical_text();\n            }\n        }\n        this.timer();\n    };\n\n    this.update_text = function()\n    {\n        for(var i = 0; i < text_mode_height; i++)\n        {\n            if(changed_rows[i])\n            {\n                this.text_update_row(i);\n                changed_rows[i] = 0;\n            }\n        }\n    };\n\n    this.update_graphical = function()\n    {\n        this.screen_fill_buffer();\n    };\n\n    this.update_graphical_text = function()\n    {\n        if(offscreen_context)\n        {\n            // toggle cursor and blinking character visibility at a frequency of ~3.75hz\n            const tm_now = performance.now();\n            if(tm_now - tm_last_update > 266)\n            {\n                blink_visible = !blink_visible;\n                if(cursor_enabled)\n                {\n                    changed_rows[cursor_row] = 1;\n                }\n                mark_blinking_rows_dirty();\n                tm_last_update = tm_now;\n            }\n            // copy to DOM canvas only if anything new was rendered\n            if(render_changed_rows())\n            {\n                graphic_context.drawImage(offscreen_context.canvas, 0, 0);\n            }\n        }\n    };\n\n    this.destroy = function()\n    {\n        if(timer_id)\n        {\n            cancelAnimationFrame(timer_id);\n            timer_id = 0;\n        }\n    };\n\n    this.pause = function()\n    {\n        paused = true;\n        cursor_element.classList.remove(\"blinking-cursor\");\n    };\n\n    this.continue = function()\n    {\n        paused = false;\n        cursor_element.classList.add(\"blinking-cursor\");\n    };\n\n    this.set_mode = function(graphical)\n    {\n        mode = graphical ? MODE_GRAPHICAL : (options.use_graphical_text ? MODE_GRAPHICAL_TEXT : MODE_TEXT);\n\n        if(mode === MODE_TEXT)\n        {\n            text_screen.style.display = \"block\";\n            graphic_screen.style.display = \"none\";\n        }\n        else\n        {\n            text_screen.style.display = \"none\";\n            graphic_screen.style.display = \"block\";\n\n            if(mode === MODE_GRAPHICAL_TEXT && changed_rows)\n            {\n                changed_rows.fill(1);\n            }\n        }\n    };\n\n    this.set_font_bitmap = function(height, width_9px, width_dbl, copy_8th_col, vga_bitmap, vga_bitmap_changed)\n    {\n        const width = width_dbl ? 16 : (width_9px ? 9 : 8);\n        if(font_height !== height || font_width !== width || font_width_9px !== width_9px ||\n            font_width_dbl !== width_dbl || font_copy_8th_col !== copy_8th_col ||\n            vga_bitmap_changed)\n        {\n            const size_changed = font_width !== width || font_height !== height;\n            font_height = height;\n            font_width = width;\n            font_width_9px = width_9px;\n            font_width_dbl = width_dbl;\n            font_copy_8th_col = copy_8th_col;\n            if(mode === MODE_GRAPHICAL_TEXT)\n            {\n                render_font_bitmap(vga_bitmap);\n                changed_rows.fill(1);\n                if(size_changed)\n                {\n                    this.set_size_graphical_text();\n                }\n            }\n        }\n    };\n\n    this.set_font_page = function(page_a, page_b)\n    {\n        if(font_page_a !== page_a || font_page_b !== page_b)\n        {\n            font_page_a = page_a;\n            font_page_b = page_b;\n            changed_rows.fill(1);\n        }\n    };\n\n    this.clear_screen = function()\n    {\n        graphic_context.fillStyle = \"#000\";\n        graphic_context.fillRect(0, 0, graphic_screen.width, graphic_screen.height);\n    };\n\n    this.set_size_graphical_text = function()\n    {\n        if(!font_context)\n        {\n            return;\n        }\n\n        const gfx_width = font_width * text_mode_width;\n        const gfx_height = font_height * text_mode_height;\n        const offscreen_extra_height = font_height * 2;\n\n        if(!offscreen_context || offscreen_context.canvas.width !== gfx_width ||\n            offscreen_context.canvas.height !== gfx_height ||\n            offscreen_extra_context.canvas.height !== offscreen_extra_height)\n        {\n            // resize offscreen canvases\n            if(!offscreen_context)\n            {\n                const offscreen_canvas = new OffscreenCanvas(gfx_width, gfx_height);\n                offscreen_context = offscreen_canvas.getContext(\"2d\", { alpha: false });\n                const offscreen_extra_canvas = new OffscreenCanvas(gfx_width, offscreen_extra_height);\n                offscreen_extra_context = offscreen_extra_canvas.getContext(\"2d\");\n            }\n            else\n            {\n                offscreen_context.canvas.width = gfx_width;\n                offscreen_context.canvas.height = gfx_height;\n                offscreen_extra_context.canvas.width = gfx_width;\n                offscreen_extra_context.canvas.height = offscreen_extra_height;\n            }\n\n            // resize DOM canvas graphic_screen\n            this.set_size_graphical(gfx_width, gfx_height, gfx_width, gfx_height);\n\n            changed_rows.fill(1);\n        }\n    };\n\n    /**\n     * @param {number} cols\n     * @param {number} rows\n     */\n    this.set_size_text = function(cols, rows)\n    {\n        if(cols === text_mode_width && rows === text_mode_height)\n        {\n            return;\n        }\n\n        changed_rows = new Int8Array(rows);\n        text_mode_data = new Int32Array(cols * rows * TEXT_BUF_COMPONENT_SIZE);\n\n        text_mode_width = cols;\n        text_mode_height = rows;\n\n        if(mode === MODE_TEXT)\n        {\n            while(text_screen.childNodes.length > rows)\n            {\n                text_screen.removeChild(text_screen.firstChild);\n            }\n\n            while(text_screen.childNodes.length < rows)\n            {\n                text_screen.appendChild(document.createElement(\"div\"));\n            }\n\n            for(var i = 0; i < rows; i++)\n            {\n                this.text_update_row(i);\n            }\n\n            update_scale_text();\n        }\n        else if(mode === MODE_GRAPHICAL_TEXT)\n        {\n            this.set_size_graphical_text();\n        }\n    };\n\n    this.set_size_graphical = function(width, height, buffer_width, buffer_height)\n    {\n        if(DEBUG_SCREEN_LAYERS)\n        {\n            // Draw the entire buffer. Useful for debugging\n            // panning / page flipping / screen splitting code for both\n            // v86 developers and os developers\n            width = buffer_width;\n            height = buffer_height;\n        }\n\n        graphic_screen.style.display = \"block\";\n\n        graphic_screen.width = width;\n        graphic_screen.height = height;\n\n        // graphic_context must be reconfigured whenever its graphic_screen is resized\n        graphic_context.imageSmoothingEnabled = false;\n\n        // add some scaling to tiny resolutions\n        if(width <= 640 &&\n            width * 2 < window.innerWidth * window.devicePixelRatio &&\n            height * 2 < window.innerHeight * window.devicePixelRatio)\n        {\n            base_scale = 2;\n        }\n        else\n        {\n            base_scale = 1;\n        }\n\n        update_scale_graphic();\n    };\n\n    this.set_scale = function(s_x, s_y)\n    {\n        scale_x = s_x;\n        scale_y = s_y;\n\n        update_scale_text();\n        update_scale_graphic();\n    };\n\n    function update_scale_text()\n    {\n        elem_set_scale(text_screen, scale_x, scale_y, true);\n    }\n\n    function update_scale_graphic()\n    {\n        elem_set_scale(graphic_screen, scale_x * base_scale, scale_y * base_scale, false);\n    }\n\n    function elem_set_scale(elem, scale_x, scale_y, use_scale)\n    {\n        if(!scale_x || !scale_y)\n        {\n            return;\n        }\n\n        elem.style.width = \"\";\n        elem.style.height = \"\";\n\n        if(use_scale)\n        {\n            elem.style.transform = \"\";\n        }\n\n        var rectangle = elem.getBoundingClientRect();\n\n        if(use_scale)\n        {\n            var scale_str = \"\";\n\n            scale_str += scale_x === 1 ? \"\" : \" scaleX(\" + scale_x + \")\";\n            scale_str += scale_y === 1 ? \"\" : \" scaleY(\" + scale_y + \")\";\n\n            elem.style.transform = scale_str;\n        }\n        else\n        {\n            // unblur non-fractional scales\n            if(scale_x % 1 === 0 && scale_y % 1 === 0)\n            {\n                graphic_screen.style[\"imageRendering\"] = \"crisp-edges\"; // firefox\n                graphic_screen.style[\"imageRendering\"] = \"pixelated\";\n                graphic_screen.style[\"-ms-interpolation-mode\"] = \"nearest-neighbor\";\n            }\n            else\n            {\n                graphic_screen.style[\"imageRendering\"] = \"\";\n                graphic_screen.style[\"-ms-interpolation-mode\"] = \"\";\n            }\n\n            // undo fractional css-to-device pixel ratios\n            var device_pixel_ratio = window.devicePixelRatio || 1;\n            if(device_pixel_ratio % 1 !== 0)\n            {\n                scale_x /= device_pixel_ratio;\n                scale_y /= device_pixel_ratio;\n            }\n        }\n\n        if(scale_x !== 1)\n        {\n            elem.style.width = rectangle.width * scale_x + \"px\";\n        }\n        if(scale_y !== 1)\n        {\n            elem.style.height = rectangle.height * scale_y + \"px\";\n        }\n    }\n\n    this.update_cursor_scanline = function(start, end, enabled)\n    {\n        if(start !== cursor_start || end !== cursor_end || enabled !== cursor_enabled)\n        {\n            if(mode === MODE_TEXT)\n            {\n                if(enabled)\n                {\n                    cursor_element.style.display = \"inline\";\n                    cursor_element.style.height = (end - start) + \"px\";\n                    cursor_element.style.marginTop = start + \"px\";\n                }\n                else\n                {\n                    cursor_element.style.display = \"none\";\n                }\n            }\n            else if(mode === MODE_GRAPHICAL_TEXT)\n            {\n                if(cursor_row < text_mode_height)\n                {\n                    changed_rows[cursor_row] = 1;\n                }\n            }\n\n            cursor_start = start;\n            cursor_end = end;\n            cursor_enabled = enabled;\n        }\n    };\n\n    this.update_cursor = function(row, col)\n    {\n        if(row !== cursor_row || col !== cursor_col)\n        {\n            if(row < text_mode_height)\n            {\n                changed_rows[row] = 1;\n            }\n            if(cursor_row < text_mode_height)\n            {\n                changed_rows[cursor_row] = 1;\n            }\n\n            cursor_row = row;\n            cursor_col = col;\n        }\n    };\n\n    this.text_update_row = function(row)\n    {\n        var offset = TEXT_BUF_COMPONENT_SIZE * row * text_mode_width,\n            row_element,\n            color_element,\n            fragment;\n\n        var blinking,\n            bg_color,\n            fg_color,\n            text;\n\n        row_element = text_screen.childNodes[row];\n        fragment = document.createElement(\"div\");\n\n        for(var i = 0; i < text_mode_width; )\n        {\n            color_element = document.createElement(\"span\");\n\n            blinking = text_mode_data[offset + FLAGS_INDEX] & FLAG_BLINKING;\n            bg_color = text_mode_data[offset + BG_COLOR_INDEX];\n            fg_color = text_mode_data[offset + FG_COLOR_INDEX];\n\n            if(blinking)\n            {\n                color_element.classList.add(\"blink\");\n            }\n\n            color_element.style.backgroundColor = number_as_color(bg_color);\n            color_element.style.color = number_as_color(fg_color);\n\n            text = \"\";\n\n            // put characters of the same color in one element\n            while(i < text_mode_width &&\n                (text_mode_data[offset + FLAGS_INDEX] & FLAG_BLINKING) === blinking &&\n                text_mode_data[offset + BG_COLOR_INDEX] === bg_color &&\n                text_mode_data[offset + FG_COLOR_INDEX] === fg_color)\n            {\n                const chr = charmap[text_mode_data[offset + CHARACTER_INDEX]];\n\n                text += chr;\n                dbg_assert(chr);\n\n                i++;\n                offset += TEXT_BUF_COMPONENT_SIZE;\n\n                if(row === cursor_row)\n                {\n                    if(i === cursor_col)\n                    {\n                        // next row will be cursor\n                        // create new element\n                        break;\n                    }\n                    else if(i === cursor_col + 1)\n                    {\n                        // found the cursor\n                        cursor_element.style.backgroundColor = color_element.style.color;\n                        fragment.appendChild(cursor_element);\n                        break;\n                    }\n                }\n            }\n\n            color_element.textContent = text;\n            fragment.appendChild(color_element);\n        }\n\n        row_element.parentNode.replaceChild(fragment, row_element);\n    };\n\n    this.update_buffer = function(layers)\n    {\n        if(DEBUG_SCREEN_LAYERS)\n        {\n            // For each visible layer that would've been drawn, draw a\n            // rectangle to visualise the layer instead.\n            graphic_context.strokeStyle = \"#0F0\";\n            graphic_context.lineWidth = 4;\n            for(const layer of layers)\n            {\n                graphic_context.strokeRect(\n                    layer.buffer_x,\n                    layer.buffer_y,\n                    layer.buffer_width,\n                    layer.buffer_height\n                );\n            }\n            graphic_context.lineWidth = 1;\n            return;\n        }\n\n        for(const layer of layers)\n        {\n            graphic_context.putImageData(\n                layer.image_data,\n                layer.screen_x - layer.buffer_x,\n                layer.screen_y - layer.buffer_y,\n                layer.buffer_x,\n                layer.buffer_y,\n                layer.buffer_width,\n                layer.buffer_height\n            );\n        }\n    };\n\n    // XXX: duplicated in DummyScreenAdapter\n    this.get_text_screen = function()\n    {\n        var screen = [];\n\n        for(var i = 0; i < text_mode_height; i++)\n        {\n            screen.push(this.get_text_row(i));\n        }\n\n        return screen;\n    };\n\n    this.get_text_row = function(y)\n    {\n        const begin = y * text_mode_width * TEXT_BUF_COMPONENT_SIZE + CHARACTER_INDEX;\n        const end = begin + text_mode_width * TEXT_BUF_COMPONENT_SIZE;\n        let row = \"\";\n        for(let i = begin; i < end; i += TEXT_BUF_COMPONENT_SIZE)\n        {\n            row += charmap[text_mode_data[i]];\n        }\n        return row;\n    };\n\n    this.init();\n}\n"
  },
  {
    "path": "src/browser/serial.js",
    "content": "import { dbg_assert, dbg_log } from \"../log.js\";\n\n// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/**\n * @constructor\n *\n * @param {HTMLTextAreaElement} element\n */\nfunction TextAreaAdapter(element)\n{\n    var serial = this;\n\n    this.enabled = true;\n    this.text = \"\";\n    this.text_new_line = false;\n\n    this.last_update = 0;\n\n    this.destroy = function()\n    {\n        this.enabled = false;\n\n        element.removeEventListener(\"keypress\", keypress_handler, false);\n        element.removeEventListener(\"keydown\", keydown_handler, false);\n        element.removeEventListener(\"paste\", paste_handler, false);\n        window.removeEventListener(\"mousedown\", window_click_handler, false);\n    };\n\n    this.init = function()\n    {\n        this.destroy();\n        this.enabled = true;\n\n        element.style.display = \"block\";\n        element.addEventListener(\"keypress\", keypress_handler, false);\n        element.addEventListener(\"keydown\", keydown_handler, false);\n        element.addEventListener(\"paste\", paste_handler, false);\n        window.addEventListener(\"mousedown\", window_click_handler, false);\n    };\n    this.init();\n\n    this.show_char = function(chr)\n    {\n        if(chr === \"\\x08\")\n        {\n            this.text = this.text.slice(0, -1);\n            this.update();\n        }\n        else if(chr === \"\\r\")\n        {\n            // do nothing\n        }\n        else\n        {\n            this.text += chr;\n\n            if(chr === \"\\n\")\n            {\n                this.text_new_line = true;\n            }\n\n            this.update();\n        }\n    };\n\n    this.update = function()\n    {\n        var now = Date.now();\n        var delta = now - this.last_update;\n\n        if(delta < 16)\n        {\n            if(this.update_timer === undefined)\n            {\n                this.update_timer = setTimeout(() => {\n                    this.update_timer = undefined;\n                    var now = Date.now();\n                    dbg_assert(now - this.last_update >= 15);\n                    this.last_update = now;\n                    this.render();\n                }, 16 - delta);\n            }\n        }\n        else\n        {\n            if(this.update_timer !== undefined)\n            {\n                clearTimeout(this.update_timer);\n                this.update_timer = undefined;\n            }\n\n            this.last_update = now;\n            this.render();\n        }\n    };\n\n    this.render = function()\n    {\n        element.value = this.text;\n\n        if(this.text_new_line)\n        {\n            this.text_new_line = false;\n            element.scrollTop = 1e9;\n        }\n    };\n\n    /**\n     * @param {number} chr_code\n     */\n    this.send_char = function(chr_code)\n    {\n        // placeholder\n    };\n\n    function may_handle(e)\n    {\n        if(!serial.enabled)\n        {\n            return false;\n        }\n\n        // Something here?\n\n        return true;\n    }\n\n    function keypress_handler(e)\n    {\n        if(!may_handle(e))\n        {\n            return;\n        }\n\n        var chr = e.which;\n\n        serial.send_char(chr);\n        e.preventDefault();\n    }\n\n    function keydown_handler(e)\n    {\n        var chr = e.which;\n\n        if(chr === 8)\n        {\n            // supress backspace\n            serial.send_char(127);\n            e.preventDefault();\n        }\n        else if(chr === 9)\n        {\n            // tab\n            serial.send_char(9);\n            e.preventDefault();\n        }\n    }\n\n    function paste_handler(e)\n    {\n        if(!may_handle(e))\n        {\n            return;\n        }\n\n        var data = e.clipboardData.getData(\"text/plain\");\n\n        for(var i = 0; i < data.length; i++)\n        {\n            serial.send_char(data.charCodeAt(i));\n        }\n\n        e.preventDefault();\n    }\n\n    function window_click_handler(e)\n    {\n        if(e.target !== element)\n        {\n            element.blur();\n        }\n    }\n}\n\n/**\n * @constructor\n *\n * @param {HTMLTextAreaElement} element\n * @param {BusConnector} bus\n */\nexport function SerialAdapter(element, bus)\n{\n    var adapter = Reflect.construct(TextAreaAdapter, [element], SerialAdapter);\n\n    adapter.send_char = function(chr_code)\n    {\n        bus.send(\"serial0-input\", chr_code);\n    };\n\n    bus.register(\"serial0-output-byte\", function(byte)\n    {\n        var chr = String.fromCharCode(byte);\n        adapter.show_char && adapter.show_char(chr);\n    }, adapter);\n\n    return adapter;\n}\n\nReflect.setPrototypeOf(SerialAdapter.prototype, TextAreaAdapter.prototype);\nReflect.setPrototypeOf(SerialAdapter, TextAreaAdapter);\n\n/**\n * @constructor\n *\n * @param {HTMLTextAreaElement} element\n * @param {BusConnector} bus\n */\nexport function VirtioConsoleAdapter(element, bus)\n{\n    var adapter = Reflect.construct(TextAreaAdapter, [element], VirtioConsoleAdapter);\n\n    adapter.send_char = function(chr_code)\n    {\n        bus.send(\"virtio-console0-input-bytes\", new Uint8Array([chr_code]));\n    };\n\n    const decoder = new TextDecoder();\n    bus.register(\"virtio-console0-output-bytes\", function(bytes)\n    {\n        for(const chr of decoder.decode(bytes))\n        {\n            adapter.show_char && adapter.show_char(chr);\n        }\n    }, adapter);\n\n    return adapter;\n}\n\nReflect.setPrototypeOf(VirtioConsoleAdapter.prototype, TextAreaAdapter.prototype);\nReflect.setPrototypeOf(VirtioConsoleAdapter, TextAreaAdapter);\n\n/**\n * @constructor\n *\n * @param {BusConnector} bus\n */\nfunction SerialRecordingAdapter(bus)\n{\n    var serial = this;\n    this.text = \"\";\n\n    bus.register(\"serial0-output-byte\", function(byte)\n    {\n        var chr = String.fromCharCode(byte);\n        this.text += chr;\n    }, this);\n}\n\n/**\n * @constructor\n *\n * @param {HTMLElement} element\n * @param {Function} xterm_lib\n */\nfunction XtermJSAdapter(element, xterm_lib)\n{\n    this.element = element;\n\n    var term = this.term = new xterm_lib({\n        \"logLevel\": \"off\",\n        \"convertEol\": \"true\",\n    });\n\n    this.destroy = function() {\n        this.on_data_disposable && this.on_data_disposable[\"dispose\"]();\n        term[\"dispose\"]();\n    };\n}\n\nXtermJSAdapter.prototype.show = function()\n{\n    this.term && this.term.open(this.element);\n};\n\n/**\n * @constructor\n *\n * @extends XtermJSAdapter\n * @param {HTMLElement} element\n * @param {BusConnector} bus\n * @param {Function} xterm_lib\n */\nexport function SerialAdapterXtermJS(element, bus, xterm_lib)\n{\n    if(!xterm_lib)\n    {\n        return;\n    }\n\n    var adapter = Reflect.construct(XtermJSAdapter, [element, xterm_lib], SerialAdapterXtermJS);\n\n    bus.register(\"serial0-output-byte\", function(utf8_byte)\n    {\n        adapter.term.write(Uint8Array.of(utf8_byte));\n    }, adapter);\n\n    const utf8_encoder = new TextEncoder();\n    adapter.on_data_disposable = adapter.term[\"onData\"](function(data_str) {\n        for(const utf8_byte of utf8_encoder.encode(data_str))\n        {\n            bus.send(\"serial0-input\", utf8_byte);\n        }\n    });\n\n    return adapter;\n}\n\nReflect.setPrototypeOf(SerialAdapterXtermJS.prototype, XtermJSAdapter.prototype);\nReflect.setPrototypeOf(SerialAdapterXtermJS, XtermJSAdapter);\n\n/**\n * @constructor\n *\n * @extends XtermJSAdapter\n * @param {HTMLElement} element\n * @param {BusConnector} bus\n * @param {Function} xterm_lib\n */\nexport function VirtioConsoleAdapterXtermJS(element, bus, xterm_lib)\n{\n    if(!xterm_lib)\n    {\n        return;\n    }\n\n    var adapter = Reflect.construct(XtermJSAdapter, [element, xterm_lib], VirtioConsoleAdapterXtermJS);\n\n    bus.register(\"virtio-console0-output-bytes\", function(utf8_bytes)\n    {\n        adapter.term.write(utf8_bytes);\n    }, adapter);\n\n    const utf8_encoder = new TextEncoder();\n    adapter.on_data_disposable = adapter.term[\"onData\"](function(data_str) {\n        bus.send(\"virtio-console0-input-bytes\", utf8_encoder.encode(data_str));\n    });\n\n    return adapter;\n}\n\nReflect.setPrototypeOf(VirtioConsoleAdapterXtermJS.prototype, XtermJSAdapter.prototype);\nReflect.setPrototypeOf(VirtioConsoleAdapterXtermJS, XtermJSAdapter);\n"
  },
  {
    "path": "src/browser/speaker.js",
    "content": "import {\n    MIXER_CHANNEL_BOTH, MIXER_CHANNEL_LEFT, MIXER_CHANNEL_RIGHT,\n    MIXER_SRC_PCSPEAKER, MIXER_SRC_DAC, MIXER_SRC_MASTER,\n} from \"../const.js\";\nimport { dbg_assert, dbg_log } from \"../log.js\";\nimport { OSCILLATOR_FREQ } from \"../pit.js\";\nimport { dump_file } from \"../lib.js\";\n\n// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/* global registerProcessor, sampleRate */\n\nconst DAC_QUEUE_RESERVE = 0.2;\n\nconst AUDIOBUFFER_MINIMUM_SAMPLING_RATE = 8000;\n\n/**\n * @constructor\n * @param {!BusConnector} bus\n */\nexport function SpeakerAdapter(bus)\n{\n    if(typeof window === \"undefined\")\n    {\n        return;\n    }\n    if(!window.AudioContext && !window[\"webkitAudioContext\"])\n    {\n        console.warn(\"Web browser doesn't support Web Audio API\");\n        return;\n    }\n\n    var SpeakerDAC = window.AudioWorklet ? SpeakerWorkletDAC : SpeakerBufferSourceDAC;\n\n    /** @const */\n    this.bus = bus;\n\n    this.audio_context = window.AudioContext ? new AudioContext() : new webkitAudioContext();\n\n    /** @const */\n    this.mixer = new SpeakerMixer(bus, this.audio_context);\n\n    /** @const */\n    this.pcspeaker = new PCSpeaker(bus, this.audio_context, this.mixer);\n\n    this.dac = new SpeakerDAC(bus, this.audio_context, this.mixer);\n\n    this.pcspeaker.start();\n\n    bus.register(\"emulator-stopped\", function()\n    {\n        this.audio_context.suspend();\n    }, this);\n\n    bus.register(\"emulator-started\", function()\n    {\n        this.audio_context.resume();\n    }, this);\n\n    bus.register(\"speaker-confirm-initialized\", function()\n    {\n        bus.send(\"speaker-has-initialized\");\n    }, this);\n    bus.send(\"speaker-has-initialized\");\n}\n\nSpeakerAdapter.prototype.destroy = function()\n{\n    this.audio_context && this.audio_context.close();\n    this.audio_context = null;\n    this.dac && this.dac.node_processor && this.dac.node_processor.port.close();\n    this.dac = null;\n};\n\n/**\n * @constructor\n * @param {!BusConnector} bus\n * @param {!AudioContext} audio_context\n */\nfunction SpeakerMixer(bus, audio_context)\n{\n    /** @const */\n    this.audio_context = audio_context;\n\n    this.sources = new Map();\n\n    // States\n\n    this.volume_both = 1;\n    this.volume_left = 1;\n    this.volume_right = 1;\n    this.gain_left = 1;\n    this.gain_right = 1;\n\n    // Nodes\n    // TODO: Find / calibrate / verify the filter frequencies\n\n    this.node_treble_left = this.audio_context.createBiquadFilter();\n    this.node_treble_right = this.audio_context.createBiquadFilter();\n    this.node_treble_left.type = \"highshelf\";\n    this.node_treble_right.type = \"highshelf\";\n    this.node_treble_left.frequency.setValueAtTime(2000, this.audio_context.currentTime);\n    this.node_treble_right.frequency.setValueAtTime(2000, this.audio_context.currentTime);\n\n    this.node_bass_left = this.audio_context.createBiquadFilter();\n    this.node_bass_right = this.audio_context.createBiquadFilter();\n    this.node_bass_left.type = \"lowshelf\";\n    this.node_bass_right.type = \"lowshelf\";\n    this.node_bass_left.frequency.setValueAtTime(200, this.audio_context.currentTime);\n    this.node_bass_right.frequency.setValueAtTime(200, this.audio_context.currentTime);\n\n    this.node_gain_left = this.audio_context.createGain();\n    this.node_gain_right = this.audio_context.createGain();\n\n    this.node_merger = this.audio_context.createChannelMerger(2);\n\n    // Graph\n\n    this.input_left = this.node_treble_left;\n    this.input_right = this.node_treble_right;\n\n    this.node_treble_left.connect(this.node_bass_left);\n    this.node_bass_left.connect(this.node_gain_left);\n    this.node_gain_left.connect(this.node_merger, 0, 0);\n\n    this.node_treble_right.connect(this.node_bass_right);\n    this.node_bass_right.connect(this.node_gain_right);\n    this.node_gain_right.connect(this.node_merger, 0, 1);\n\n    this.node_merger.connect(this.audio_context.destination);\n\n    // Interface\n\n    bus.register(\"mixer-connect\", function(data)\n    {\n        var source_id = data[0];\n        var channel = data[1];\n        this.connect_source(source_id, channel);\n    }, this);\n\n    bus.register(\"mixer-disconnect\", function(data)\n    {\n        var source_id = data[0];\n        var channel = data[1];\n        this.disconnect_source(source_id, channel);\n    }, this);\n\n    bus.register(\"mixer-volume\", function(data)\n    {\n        var source_id = data[0];\n        var channel = data[1];\n        var decibels = data[2];\n\n        var gain = Math.pow(10, decibels / 20);\n\n        var source = source_id === MIXER_SRC_MASTER ? this : this.sources.get(source_id);\n\n        if(source === undefined)\n        {\n            dbg_assert(false, \"Mixer set volume - cannot set volume for undefined source: \" + source_id);\n            return;\n        }\n\n        source.set_volume(gain, channel);\n    }, this);\n\n    bus.register(\"mixer-gain-left\", function(/** number */ decibels)\n    {\n        this.gain_left = Math.pow(10, decibels / 20);\n        this.update();\n    }, this);\n\n    bus.register(\"mixer-gain-right\", function(/** number */ decibels)\n    {\n        this.gain_right = Math.pow(10, decibels / 20);\n        this.update();\n    }, this);\n\n    function create_gain_handler(audio_node)\n    {\n        return function(decibels)\n        {\n            audio_node.gain.setValueAtTime(decibels, this.audio_context.currentTime);\n        };\n    }\n    bus.register(\"mixer-treble-left\", create_gain_handler(this.node_treble_left), this);\n    bus.register(\"mixer-treble-right\", create_gain_handler(this.node_treble_right), this);\n    bus.register(\"mixer-bass-left\", create_gain_handler(this.node_bass_left), this);\n    bus.register(\"mixer-bass-right\", create_gain_handler(this.node_bass_right), this);\n}\n\n/**\n * @param {!AudioNode} source_node\n * @param {number} source_id\n * @return {SpeakerMixerSource}\n */\nSpeakerMixer.prototype.add_source = function(source_node, source_id)\n{\n    var source = new SpeakerMixerSource(\n        this.audio_context,\n        source_node,\n        this.input_left,\n        this.input_right\n    );\n\n    dbg_assert(!this.sources.has(source_id), \"Mixer add source - overwritting source: \" + source_id);\n\n    this.sources.set(source_id, source);\n    return source;\n};\n\n/**\n * @param {number} source_id\n * @param {number=} channel\n */\nSpeakerMixer.prototype.connect_source = function(source_id, channel)\n{\n    var source = this.sources.get(source_id);\n\n    if(source === undefined)\n    {\n        dbg_assert(false, \"Mixer connect - cannot connect undefined source: \" + source_id);\n        return;\n    }\n\n    source.connect(channel);\n};\n\n/**\n * @param {number} source_id\n * @param {number=} channel\n */\nSpeakerMixer.prototype.disconnect_source = function(source_id, channel)\n{\n    var source = this.sources.get(source_id);\n\n    if(source === undefined)\n    {\n        dbg_assert(false, \"Mixer disconnect - cannot disconnect undefined source: \" + source_id);\n        return;\n    }\n\n    source.disconnect(channel);\n};\n\n/**\n * @param {number} value\n * @param {number=} channel\n */\nSpeakerMixer.prototype.set_volume = function(value, channel)\n{\n    if(channel === undefined)\n    {\n        channel = MIXER_CHANNEL_BOTH;\n    }\n\n    switch(channel)\n    {\n        case MIXER_CHANNEL_LEFT:\n            this.volume_left = value;\n            break;\n        case MIXER_CHANNEL_RIGHT:\n            this.volume_right = value;\n            break;\n        case MIXER_CHANNEL_BOTH:\n            this.volume_both = value;\n            break;\n        default:\n            dbg_assert(false, \"Mixer set master volume - unknown channel: \" + channel);\n            return;\n    }\n\n    this.update();\n};\n\nSpeakerMixer.prototype.update = function()\n{\n    var net_gain_left = this.volume_both * this.volume_left * this.gain_left;\n    var net_gain_right = this.volume_both * this.volume_right * this.gain_right;\n\n    this.node_gain_left.gain.setValueAtTime(net_gain_left, this.audio_context.currentTime);\n    this.node_gain_right.gain.setValueAtTime(net_gain_right, this.audio_context.currentTime);\n};\n\n/**\n * @constructor\n * @param {!AudioContext} audio_context\n * @param {!AudioNode} source_node\n * @param {!AudioNode} destination_left\n * @param {!AudioNode} destination_right\n */\nfunction SpeakerMixerSource(audio_context, source_node, destination_left, destination_right)\n{\n    /** @const */\n    this.audio_context = audio_context;\n\n    // States\n\n    this.connected_left = true;\n    this.connected_right = true;\n    this.gain_hidden = 1;\n    this.volume_both = 1;\n    this.volume_left = 1;\n    this.volume_right = 1;\n\n    // Nodes\n\n    this.node_splitter = audio_context.createChannelSplitter(2);\n    this.node_gain_left = audio_context.createGain();\n    this.node_gain_right = audio_context.createGain();\n\n    // Graph\n\n    source_node.connect(this.node_splitter);\n\n    this.node_splitter.connect(this.node_gain_left, 0);\n    this.node_gain_left.connect(destination_left);\n\n    this.node_splitter.connect(this.node_gain_right, 1);\n    this.node_gain_right.connect(destination_right);\n}\n\nSpeakerMixerSource.prototype.update = function()\n{\n    var net_gain_left = this.connected_left * this.gain_hidden * this.volume_both * this.volume_left;\n    var net_gain_right = this.connected_right * this.gain_hidden * this.volume_both * this.volume_right;\n\n    this.node_gain_left.gain.setValueAtTime(net_gain_left, this.audio_context.currentTime);\n    this.node_gain_right.gain.setValueAtTime(net_gain_right, this.audio_context.currentTime);\n};\n\n/** @param {number=} channel */\nSpeakerMixerSource.prototype.connect = function(channel)\n{\n    var both = !channel || channel === MIXER_CHANNEL_BOTH;\n    if(both || channel === MIXER_CHANNEL_LEFT)\n    {\n        this.connected_left = true;\n    }\n    if(both || channel === MIXER_CHANNEL_RIGHT)\n    {\n        this.connected_right = true;\n    }\n    this.update();\n};\n\n/** @param {number=} channel */\nSpeakerMixerSource.prototype.disconnect = function(channel)\n{\n    var both = !channel || channel === MIXER_CHANNEL_BOTH;\n    if(both || channel === MIXER_CHANNEL_LEFT)\n    {\n        this.connected_left = false;\n    }\n    if(both || channel === MIXER_CHANNEL_RIGHT)\n    {\n        this.connected_right = false;\n    }\n    this.update();\n};\n\n/**\n * @param {number} value\n * @param {number=} channel\n */\nSpeakerMixerSource.prototype.set_volume = function(value, channel)\n{\n    if(channel === undefined)\n    {\n        channel = MIXER_CHANNEL_BOTH;\n    }\n\n    switch(channel)\n    {\n        case MIXER_CHANNEL_LEFT:\n            this.volume_left = value;\n            break;\n        case MIXER_CHANNEL_RIGHT:\n            this.volume_right = value;\n            break;\n        case MIXER_CHANNEL_BOTH:\n            this.volume_both = value;\n            break;\n        default:\n            dbg_assert(false, \"Mixer set volume - unknown channel: \" + channel);\n            return;\n    }\n\n    this.update();\n};\n\nSpeakerMixerSource.prototype.set_gain_hidden = function(value)\n{\n    this.gain_hidden = value;\n};\n\n/**\n * @constructor\n * @param {!BusConnector} bus\n * @param {!AudioContext} audio_context\n * @param {!SpeakerMixer} mixer\n */\nfunction PCSpeaker(bus, audio_context, mixer)\n{\n    // Nodes\n\n    this.node_oscillator = audio_context.createOscillator();\n    this.node_oscillator.type = \"square\";\n    this.node_oscillator.frequency.setValueAtTime(440, audio_context.currentTime);\n\n    // Interface\n\n    this.mixer_connection = mixer.add_source(this.node_oscillator, MIXER_SRC_PCSPEAKER);\n    this.mixer_connection.disconnect();\n\n    bus.register(\"pcspeaker-enable\", function()\n    {\n        mixer.connect_source(MIXER_SRC_PCSPEAKER);\n    }, this);\n\n    bus.register(\"pcspeaker-disable\", function()\n    {\n        mixer.disconnect_source(MIXER_SRC_PCSPEAKER);\n    }, this);\n\n    bus.register(\"pcspeaker-update\", function(data)\n    {\n        var counter_mode = data[0];\n        var counter_reload = data[1];\n\n        var frequency = 0;\n        var beep_enabled = counter_mode === 3;\n\n        if(beep_enabled)\n        {\n            frequency = OSCILLATOR_FREQ * 1000 / counter_reload;\n            frequency = Math.min(frequency, this.node_oscillator.frequency.maxValue);\n            frequency = Math.max(frequency, 0);\n        }\n\n        this.node_oscillator.frequency.setValueAtTime(frequency, audio_context.currentTime);\n    }, this);\n}\n\nPCSpeaker.prototype.start = function()\n{\n    this.node_oscillator.start();\n};\n\n/**\n * @constructor\n * @param {!BusConnector} bus\n * @param {!AudioContext} audio_context\n * @param {!SpeakerMixer} mixer\n */\nfunction SpeakerWorkletDAC(bus, audio_context, mixer)\n{\n    /** @const */\n    this.bus = bus;\n\n    /** @const */\n    this.audio_context = audio_context;\n\n    // State\n\n    this.enabled = false;\n    this.sampling_rate = 48000;\n\n    // Worklet\n\n    function worklet()\n    {\n        const RENDER_QUANTUM = 128;\n        const MINIMUM_BUFFER_SIZE = 2 * RENDER_QUANTUM;\n        const QUEUE_RESERVE = 1024;\n\n        function sinc(x)\n        {\n            if(x === 0) return 1;\n            x *= Math.PI;\n            return Math.sin(x) / x;\n        }\n\n        var EMPTY_BUFFER =\n        [\n            new Float32Array(MINIMUM_BUFFER_SIZE),\n            new Float32Array(MINIMUM_BUFFER_SIZE),\n        ];\n\n        /**\n         * @constructor\n         * @extends AudioWorkletProcessor\n         */\n        function DACProcessor()\n        {\n            var self = Reflect.construct(AudioWorkletProcessor, [], DACProcessor);\n\n            // Params\n\n            self.kernel_size = 3;\n\n            // States\n\n            // Buffers waiting for their turn to be consumed\n            self.queue_data = new Array(1024);\n            self.queue_start = 0;\n            self.queue_end = 0;\n            self.queue_length = 0;\n            self.queue_size = self.queue_data.length;\n            self.queued_samples = 0;\n\n            // Buffers being actively consumed\n            /** @type{Array<Float32Array>} */\n            self.source_buffer_previous = EMPTY_BUFFER;\n            /** @type{Array<Float32Array>} */\n            self.source_buffer_current = EMPTY_BUFFER;\n\n            // Ratio of alienland sample rate to homeland sample rate.\n            self.source_samples_per_destination = 1.0;\n\n            // Integer representing the position of the first destination sample\n            // for the current block, relative to source_buffer_current.\n            self.source_block_start = 0;\n\n            // Real number representing the position of the current destination\n            // sample relative to source_buffer_current, since source_block_start.\n            self.source_time = 0.0;\n\n            // Same as source_time but rounded down to an index.\n            self.source_offset = 0;\n\n            // Interface\n\n            self.port.onmessage = (event) =>\n            {\n                switch(event.data.type)\n                {\n                    case \"queue\":\n                        self.queue_push(event.data.value);\n                        break;\n                    case \"sampling-rate\":\n                        self.source_samples_per_destination = event.data.value / sampleRate;\n                        break;\n                }\n            };\n\n            return self;\n        }\n\n        Reflect.setPrototypeOf(DACProcessor.prototype, AudioWorkletProcessor.prototype);\n        Reflect.setPrototypeOf(DACProcessor, AudioWorkletProcessor);\n\n        DACProcessor.prototype[\"process\"] =\n        DACProcessor.prototype.process = function(inputs, outputs, parameters)\n        {\n            for(var i = 0; i < outputs[0][0].length; i++)\n            {\n                // Lanczos resampling\n                var sum0 = 0;\n                var sum1 = 0;\n\n                var start = this.source_offset - this.kernel_size + 1;\n                var end = this.source_offset + this.kernel_size;\n\n                for(var j = start; j <= end; j++)\n                {\n                    var convolute_index = this.source_block_start + j;\n                    sum0 += this.get_sample(convolute_index, 0) * this.kernel(this.source_time - j);\n                    sum1 += this.get_sample(convolute_index, 1) * this.kernel(this.source_time - j);\n                }\n\n                if(isNaN(sum0) || isNaN(sum1))\n                {\n                    // NaN values cause entire audio graph to cease functioning.\n                    sum0 = sum1 = 0;\n                    this.dbg_log(\"ERROR: NaN values! Ignoring for now.\");\n                }\n\n                outputs[0][0][i] = sum0;\n                outputs[0][1][i] = sum1;\n\n                this.source_time += this.source_samples_per_destination;\n                this.source_offset = Math.floor(this.source_time);\n            }\n\n            // +2 to safeguard against rounding variations\n            var samples_needed_per_block = this.source_offset;\n            samples_needed_per_block += this.kernel_size + 2;\n\n            this.source_time -= this.source_offset;\n            this.source_block_start += this.source_offset;\n            this.source_offset = 0;\n\n            // Note: This needs to be done after source_block_start is updated.\n            this.ensure_enough_data(samples_needed_per_block);\n\n            return true;\n        };\n\n        DACProcessor.prototype.kernel = function(x)\n        {\n            return sinc(x) * sinc(x / this.kernel_size);\n        };\n\n        DACProcessor.prototype.get_sample = function(index, channel)\n        {\n            if(index < 0)\n            {\n                // -ve index represents previous buffer\n                //          <-------|\n                // [Previous buffer][Current buffer]\n                index += this.source_buffer_previous[0].length;\n                return this.source_buffer_previous[channel][index];\n            }\n            else\n            {\n                return this.source_buffer_current[channel][index];\n            }\n        };\n\n        DACProcessor.prototype.ensure_enough_data = function(needed)\n        {\n            var current_length = this.source_buffer_current[0].length;\n            var remaining = current_length - this.source_block_start;\n\n            if(remaining < needed)\n            {\n                this.prepare_next_buffer();\n                this.source_block_start -= current_length;\n            }\n        };\n\n        DACProcessor.prototype.prepare_next_buffer = function()\n        {\n            if(this.queued_samples < MINIMUM_BUFFER_SIZE && this.queue_length)\n            {\n                this.dbg_log(\"Not enough samples - should not happen during midway of playback\");\n            }\n\n            this.source_buffer_previous = this.source_buffer_current;\n            this.source_buffer_current = this.queue_shift();\n\n            var sample_count = this.source_buffer_current[0].length;\n\n            if(sample_count < MINIMUM_BUFFER_SIZE)\n            {\n                // Unfortunately, this single buffer is too small :(\n\n                var queue_pos = this.queue_start;\n                var buffer_count = 0;\n\n                // Figure out how many small buffers to combine.\n                while(sample_count < MINIMUM_BUFFER_SIZE && buffer_count < this.queue_length)\n                {\n                    sample_count += this.queue_data[queue_pos][0].length;\n\n                    queue_pos = queue_pos + 1 & this.queue_size - 1;\n                    buffer_count++;\n                }\n\n                // Note: if not enough buffers, this will be end-padded with zeros:\n                var new_big_buffer_size = Math.max(sample_count, MINIMUM_BUFFER_SIZE);\n                var new_big_buffer =\n                [\n                    new Float32Array(new_big_buffer_size),\n                    new Float32Array(new_big_buffer_size),\n                ];\n\n                // Copy the first, already-shifted, small buffer into the new buffer.\n                new_big_buffer[0].set(this.source_buffer_current[0]);\n                new_big_buffer[1].set(this.source_buffer_current[1]);\n                var new_big_buffer_pos = this.source_buffer_current[0].length;\n\n                // Copy the rest.\n                for(var i = 0; i < buffer_count; i++)\n                {\n                    var small_buffer = this.queue_shift();\n                    new_big_buffer[0].set(small_buffer[0], new_big_buffer_pos);\n                    new_big_buffer[1].set(small_buffer[1], new_big_buffer_pos);\n                    new_big_buffer_pos += small_buffer[0].length;\n                }\n\n                // Pretend that everything's just fine.\n                this.source_buffer_current = new_big_buffer;\n            }\n\n            this.pump();\n        };\n\n        DACProcessor.prototype.pump = function()\n        {\n            if(this.queued_samples / this.source_samples_per_destination < QUEUE_RESERVE)\n            {\n                this.port.postMessage(\n                {\n                    type: \"pump\",\n                });\n            }\n        };\n\n        DACProcessor.prototype.queue_push = function(item)\n        {\n            if(this.queue_length < this.queue_size)\n            {\n                this.queue_data[this.queue_end] = item;\n                this.queue_end = this.queue_end + 1 & this.queue_size - 1;\n                this.queue_length++;\n\n                this.queued_samples += item[0].length;\n\n                this.pump();\n            }\n        };\n\n        DACProcessor.prototype.queue_shift = function()\n        {\n            if(!this.queue_length)\n            {\n                return EMPTY_BUFFER;\n            }\n\n            var item = this.queue_data[this.queue_start];\n\n            this.queue_data[this.queue_start] = null;\n            this.queue_start = this.queue_start + 1 & this.queue_size - 1;\n            this.queue_length--;\n\n            this.queued_samples -= item[0].length;\n\n            return item;\n        };\n\n        DACProcessor.prototype.dbg_log = function(message)\n        {\n            if(DEBUG)\n            {\n                this.port.postMessage(\n                {\n                    type: \"debug-log\",\n                    value: message,\n                });\n            }\n        };\n\n        registerProcessor(\"dac-processor\", DACProcessor);\n    }\n\n    var worklet_string = worklet.toString();\n\n    var worklet_code_start = worklet_string.indexOf(\"{\") + 1;\n    var worklet_code_end = worklet_string.lastIndexOf(\"}\");\n    var worklet_code = worklet_string.substring(worklet_code_start, worklet_code_end);\n\n    if(DEBUG)\n    {\n        worklet_code = \"var DEBUG = true;\\n\" + worklet_code;\n    }\n\n    var worklet_blob = new Blob([worklet_code], { type: \"application/javascript\" });\n    var worklet_url = URL.createObjectURL(worklet_blob);\n\n    /** @type {AudioWorkletNode} */\n    this.node_processor = null;\n\n    // Placeholder pass-through node to connect to, when worklet node is not ready yet.\n    this.node_output = this.audio_context.createGain();\n\n    this.audio_context\n        .audioWorklet\n        .addModule(worklet_url)\n        .then(() =>\n    {\n        URL.revokeObjectURL(worklet_url);\n\n        this.node_processor = new AudioWorkletNode(this.audio_context, \"dac-processor\",\n        {\n            numberOfInputs: 0,\n            numberOfOutputs: 1,\n            outputChannelCount: [2],\n            parameterData: {},\n            processorOptions: {},\n        });\n\n        this.node_processor.port.postMessage(\n        {\n            type: \"sampling-rate\",\n            value: this.sampling_rate,\n        });\n\n        this.node_processor.port.onmessage = (event) =>\n        {\n            switch(event.data.type)\n            {\n                case \"pump\":\n                    this.pump();\n                    break;\n                case \"debug-log\":\n                    dbg_log(\"SpeakerWorkletDAC - Worklet: \" + event.data.value);\n                    break;\n            }\n        };\n\n        // Graph\n\n        this.node_processor.connect(this.node_output);\n    });\n\n    // Interface\n\n    this.mixer_connection = mixer.add_source(this.node_output, MIXER_SRC_DAC);\n    this.mixer_connection.set_gain_hidden(3);\n\n    bus.register(\"dac-send-data\", function(data)\n    {\n        this.queue(data);\n    }, this);\n\n    bus.register(\"dac-enable\", function(enabled)\n    {\n        this.enabled = true;\n    }, this);\n\n    bus.register(\"dac-disable\", function()\n    {\n        this.enabled = false;\n    }, this);\n\n    bus.register(\"dac-tell-sampling-rate\", function(/** number */ rate)\n    {\n        dbg_assert(rate > 0, \"Sampling rate should be nonzero\");\n        this.sampling_rate = rate;\n\n        if(!this.node_processor)\n        {\n            return;\n        }\n\n        this.node_processor.port.postMessage(\n        {\n            type: \"sampling-rate\",\n            value: rate,\n        });\n    }, this);\n\n    if(DEBUG)\n    {\n        this.debugger = new SpeakerDACDebugger(this.audio_context, this.node_output);\n    }\n}\n\nSpeakerWorkletDAC.prototype.queue = function(data)\n{\n    if(!this.node_processor)\n    {\n        return;\n    }\n\n    if(DEBUG)\n    {\n        this.debugger.push_queued_data(data);\n    }\n\n    this.node_processor.port.postMessage(\n    {\n        type: \"queue\",\n        value: data,\n    }, [data[0].buffer, data[1].buffer]);\n};\n\nSpeakerWorkletDAC.prototype.pump = function()\n{\n    if(!this.enabled)\n    {\n        return;\n    }\n    this.bus.send(\"dac-request-data\");\n};\n\n/**\n * @constructor\n * @param {!BusConnector} bus\n * @param {!AudioContext} audio_context\n * @param {!SpeakerMixer} mixer\n */\nfunction SpeakerBufferSourceDAC(bus, audio_context, mixer)\n{\n    /** @const */\n    this.bus = bus;\n\n    /** @const */\n    this.audio_context = audio_context;\n\n    // States\n\n    this.enabled = false;\n    this.sampling_rate = 22050;\n    this.buffered_time = 0;\n    this.rate_ratio = 1;\n\n    // Nodes\n\n    this.node_lowpass = this.audio_context.createBiquadFilter();\n    this.node_lowpass.type = \"lowpass\";\n\n    // Interface\n\n    this.node_output = this.node_lowpass;\n\n    this.mixer_connection = mixer.add_source(this.node_output, MIXER_SRC_DAC);\n    this.mixer_connection.set_gain_hidden(3);\n\n    bus.register(\"dac-send-data\", function(data)\n    {\n        this.queue(data);\n    }, this);\n\n    bus.register(\"dac-enable\", function(enabled)\n    {\n        this.enabled = true;\n        this.pump();\n    }, this);\n\n    bus.register(\"dac-disable\", function()\n    {\n        this.enabled = false;\n    }, this);\n\n    bus.register(\"dac-tell-sampling-rate\", function(/** number */ rate)\n    {\n        dbg_assert(rate > 0, \"Sampling rate should be nonzero\");\n        this.sampling_rate = rate;\n        this.rate_ratio = Math.ceil(AUDIOBUFFER_MINIMUM_SAMPLING_RATE / rate);\n        this.node_lowpass.frequency.setValueAtTime(rate / 2, this.audio_context.currentTime);\n    }, this);\n\n    if(DEBUG)\n    {\n        this.debugger = new SpeakerDACDebugger(this.audio_context, this.node_output);\n    }\n}\n\nSpeakerBufferSourceDAC.prototype.queue = function(data)\n{\n    if(DEBUG)\n    {\n        this.debugger.push_queued_data(data);\n    }\n\n    var sample_count = data[0].length;\n    var block_duration = sample_count / this.sampling_rate;\n\n    var buffer;\n    if(this.rate_ratio > 1)\n    {\n        var new_sample_count = sample_count * this.rate_ratio;\n        var new_sampling_rate = this.sampling_rate * this.rate_ratio;\n        buffer = this.audio_context.createBuffer(2, new_sample_count, new_sampling_rate);\n        var buffer_data0 = buffer.getChannelData(0);\n        var buffer_data1 = buffer.getChannelData(1);\n\n        var buffer_index = 0;\n        for(var i = 0; i < sample_count; i++)\n        {\n            for(var j = 0; j < this.rate_ratio; j++, buffer_index++)\n            {\n                buffer_data0[buffer_index] = data[0][i];\n                buffer_data1[buffer_index] = data[1][i];\n            }\n        }\n    }\n    else\n    {\n        // Allocating new AudioBuffer every block\n        // - Memory profiles show insignificant improvements if recycling old buffers.\n        buffer = this.audio_context.createBuffer(2, sample_count, this.sampling_rate);\n        if(buffer.copyToChannel)\n        {\n            buffer.copyToChannel(data[0], 0);\n            buffer.copyToChannel(data[1], 1);\n        }\n        else\n        {\n            // Safari doesn't support copyToChannel yet. See #286\n            buffer.getChannelData(0).set(data[0]);\n            buffer.getChannelData(1).set(data[1]);\n        }\n    }\n\n    var source = this.audio_context.createBufferSource();\n    source.buffer = buffer;\n    source.connect(this.node_lowpass);\n    source.addEventListener(\"ended\", this.pump.bind(this));\n\n    var current_time = this.audio_context.currentTime;\n\n    if(this.buffered_time < current_time)\n    {\n        dbg_log(\"Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback\");\n\n        // Schedule pump() to queue evenly, starting from current time\n        this.buffered_time = current_time;\n        var target_silence_duration = DAC_QUEUE_RESERVE - block_duration;\n        var current_silence_duration = 0;\n        while(current_silence_duration <= target_silence_duration)\n        {\n            current_silence_duration += block_duration;\n            this.buffered_time += block_duration;\n            setTimeout(() => this.pump(), current_silence_duration * 1000);\n        }\n    }\n\n    source.start(this.buffered_time);\n    this.buffered_time += block_duration;\n\n    // Chase the schedule - ensure reserve is full\n    setTimeout(() => this.pump(), 0);\n};\n\nSpeakerBufferSourceDAC.prototype.pump = function()\n{\n    if(!this.enabled)\n    {\n        return;\n    }\n    if(this.buffered_time - this.audio_context.currentTime > DAC_QUEUE_RESERVE)\n    {\n        return;\n    }\n    this.bus.send(\"dac-request-data\");\n};\n\n/**\n * @constructor\n */\nfunction SpeakerDACDebugger(audio_context, source_node)\n{\n    /** @const */\n    this.audio_context = audio_context;\n\n    /** @const */\n    this.node_source = source_node;\n\n    this.node_processor = null;\n\n    this.node_gain = this.audio_context.createGain();\n    this.node_gain.gain.setValueAtTime(0, this.audio_context.currentTime);\n\n    this.node_gain.connect(this.audio_context.destination);\n\n    this.is_active = false;\n    this.queued_history = [];\n    this.output_history = [];\n    this.queued = [[], []];\n    this.output = [[], []];\n}\n\n/** @suppress {deprecated} */\nSpeakerDACDebugger.prototype.start = function(duration_ms)\n{\n    this.is_active = true;\n    this.queued = [[], []];\n    this.output = [[], []];\n    this.queued_history.push(this.queued);\n    this.output_history.push(this.output);\n\n    this.node_processor = this.audio_context.createScriptProcessor(1024, 2, 2);\n    this.node_processor.onaudioprocess = (event) =>\n    {\n        this.output[0].push(event.inputBuffer.getChannelData(0).slice());\n        this.output[1].push(event.inputBuffer.getChannelData(1).slice());\n    };\n\n    this.node_source.connect(this.node_processor);\n    this.node_processor.connect(this.node_gain);\n\n    setTimeout(() =>\n    {\n        this.stop();\n    }, duration_ms);\n};\n\nSpeakerDACDebugger.prototype.stop = function()\n{\n    this.is_active = false;\n    this.node_source.disconnect(this.node_processor);\n    this.node_processor.disconnect();\n    this.node_processor = null;\n};\n\nSpeakerDACDebugger.prototype.push_queued_data = function(data)\n{\n    if(this.is_active)\n    {\n        this.queued[0].push(data[0].slice());\n        this.queued[1].push(data[1].slice());\n    }\n};\n\n// Useful for Audacity imports\nSpeakerDACDebugger.prototype.download_txt = function(history_id, channel)\n{\n    var txt = this.output_history[history_id][channel]\n        .map((v) => v.join(\" \"))\n        .join(\" \");\n\n    dump_file(txt, \"dacdata.txt\");\n};\n\n// Useful for general plotting\nSpeakerDACDebugger.prototype.download_csv = function(history_id)\n{\n    var buffers = this.output_history[history_id];\n    var csv_rows = [];\n    for(var buffer_id = 0; buffer_id < buffers[0].length; buffer_id++)\n    {\n        for(var i = 0; i < buffers[0][buffer_id].length; i++)\n        {\n            csv_rows.push(`${buffers[0][buffer_id][i]},${buffers[1][buffer_id][i]}`);\n        }\n    }\n    dump_file(csv_rows.join(\"\\n\"), \"dacdata.csv\");\n};\n"
  },
  {
    "path": "src/browser/starter.js",
    "content": "import { v86 } from \"../main.js\";\nimport { LOG_CPU, WASM_TABLE_OFFSET, WASM_TABLE_SIZE } from \"../const.js\";\nimport { get_rand_int, load_file, read_sized_string_from_mem } from \"../lib.js\";\nimport { dbg_assert, dbg_trace, dbg_log, set_log_level } from \"../log.js\";\nimport * as print_stats from \"./print_stats.js\";\nimport { Bus } from \"../bus.js\";\nimport { BOOT_ORDER_FD_FIRST, BOOT_ORDER_HD_FIRST, BOOT_ORDER_CD_FIRST } from \"../rtc.js\";\nimport { EEXIST, ENOENT } from \"../../lib/9p.js\";\n\nimport { SpeakerAdapter } from \"./speaker.js\";\nimport { NetworkAdapter } from \"./network.js\";\nimport { FetchNetworkAdapter } from \"./fetch_network.js\";\nimport { WispNetworkAdapter } from \"./wisp_network.js\";\nimport { KeyboardAdapter } from \"./keyboard.js\";\nimport { MouseAdapter } from \"./mouse.js\";\nimport { ScreenAdapter } from \"./screen.js\";\nimport { DummyScreenAdapter } from \"./dummy_screen.js\";\nimport { SerialAdapter, VirtioConsoleAdapter, SerialAdapterXtermJS, VirtioConsoleAdapterXtermJS } from \"./serial.js\";\nimport { InBrowserNetworkAdapter } from \"./inbrowser_network.js\";\n\nimport { MemoryFileStorage, ServerFileStorageWrapper } from \"./filestorage.js\";\nimport { SyncBuffer, buffer_from_object } from \"../buffer.js\";\nimport { FS } from \"../../lib/filesystem.js\";\n\n/**\n * Constructor for emulator instances.\n *\n * For API usage, see v86.d.ts in the root of this repository.\n *\n * @param {{\n      disable_mouse: (boolean|undefined),\n      disable_keyboard: (boolean|undefined),\n      wasm_fn: (Function|undefined),\n      screen: ({\n          scale: (number|undefined),\n      } | undefined),\n    }} options\n * @constructor\n */\nexport function V86(options)\n{\n    if(typeof options.log_level === \"number\")\n    {\n        // XXX: Shared between all emulator instances\n        set_log_level(options.log_level);\n    }\n\n    //var worker = new Worker(\"src/browser/worker.js\");\n    //var adapter_bus = this.bus = WorkerBus.init(worker);\n\n    this.cpu_is_running = false;\n    this.cpu_exception_hook = function(n) {};\n\n    const bus = Bus.create();\n    this.bus = bus[0];\n    this.emulator_bus = bus[1];\n\n    var cpu;\n    var wasm_memory;\n\n    const wasm_table = new WebAssembly.Table({ element: \"anyfunc\", initial: WASM_TABLE_SIZE + WASM_TABLE_OFFSET });\n\n    const wasm_shared_funcs = {\n        \"cpu_exception_hook\": n => this.cpu_exception_hook(n),\n        \"run_hardware_timers\": function(a, t) { return cpu.run_hardware_timers(a, t); },\n        \"cpu_event_halt\": () => { this.emulator_bus.send(\"cpu-event-halt\"); },\n        \"abort\": function() { dbg_assert(false); },\n        \"microtick\": v86.microtick,\n        \"get_rand_int\": function() { return get_rand_int(); },\n        \"stop_idling\": function() { return cpu.stop_idling(); },\n\n        \"io_port_read8\": function(addr) { return cpu.io.port_read8(addr); },\n        \"io_port_read16\": function(addr) { return cpu.io.port_read16(addr); },\n        \"io_port_read32\": function(addr) { return cpu.io.port_read32(addr); },\n        \"io_port_write8\": function(addr, value) { cpu.io.port_write8(addr, value); },\n        \"io_port_write16\": function(addr, value) { cpu.io.port_write16(addr, value); },\n        \"io_port_write32\": function(addr, value) { cpu.io.port_write32(addr, value); },\n\n        \"mmap_read8\": function(addr) { return cpu.mmap_read8(addr); },\n        \"mmap_read32\": function(addr) { return cpu.mmap_read32(addr); },\n        \"mmap_write8\": function(addr, value) { cpu.mmap_write8(addr, value); },\n        \"mmap_write16\": function(addr, value) { cpu.mmap_write16(addr, value); },\n        \"mmap_write32\": function(addr, value) { cpu.mmap_write32(addr, value); },\n        \"mmap_write64\": function(addr, value0, value1) { cpu.mmap_write64(addr, value0, value1); },\n        \"mmap_write128\": function(addr, value0, value1, value2, value3) {\n            cpu.mmap_write128(addr, value0, value1, value2, value3);\n        },\n\n        \"log_from_wasm\": function(offset, len) {\n            const str = read_sized_string_from_mem(wasm_memory, offset, len);\n            dbg_log(str, LOG_CPU);\n        },\n        \"console_log_from_wasm\": function(offset, len) {\n            const str = read_sized_string_from_mem(wasm_memory, offset, len);\n            console.error(str);\n        },\n        \"dbg_trace_from_wasm\": function() {\n            dbg_trace(LOG_CPU);\n        },\n\n        \"codegen_finalize\": (wasm_table_index, start, state_flags, ptr, len) => {\n            cpu.codegen_finalize(wasm_table_index, start, state_flags, ptr, len);\n        },\n        \"jit_clear_func\": (wasm_table_index) => cpu.jit_clear_func(wasm_table_index),\n        \"jit_clear_all_funcs\": () => cpu.jit_clear_all_funcs(),\n\n        \"__indirect_function_table\": wasm_table,\n    };\n\n    let wasm_fn = options.wasm_fn;\n\n    if(!wasm_fn)\n    {\n        wasm_fn = env =>\n        {\n            /* global __dirname */\n\n            return new Promise(resolve => {\n                let v86_bin = DEBUG ? \"v86-debug.wasm\" : \"v86.wasm\";\n                let v86_bin_fallback = \"v86-fallback.wasm\";\n\n                if(options.wasm_path)\n                {\n                    v86_bin = options.wasm_path;\n                    v86_bin_fallback = v86_bin.replace(\"v86.wasm\", \"v86-fallback.wasm\");\n                }\n                else if(typeof window === \"undefined\" && typeof __dirname === \"string\")\n                {\n                    v86_bin = __dirname + \"/\" + v86_bin;\n                    v86_bin_fallback = __dirname + \"/\" + v86_bin_fallback;\n                }\n                else\n                {\n                    v86_bin = \"build/\" + v86_bin;\n                    v86_bin_fallback = \"build/\" + v86_bin_fallback;\n                }\n\n                load_file(v86_bin, {\n                    done: async bytes =>\n                    {\n                        try\n                        {\n                            const { instance } = await WebAssembly.instantiate(bytes, env);\n                            this.wasm_source = bytes;\n                            resolve(instance.exports);\n                        }\n                        catch(err)\n                        {\n                            load_file(v86_bin_fallback, {\n                                    done: async bytes => {\n                                        const { instance } = await WebAssembly.instantiate(bytes, env);\n                                        this.wasm_source = bytes;\n                                        resolve(instance.exports);\n                                    },\n                                });\n                        }\n                    },\n                    progress: e =>\n                    {\n                        this.emulator_bus.send(\"download-progress\", {\n                            file_index: 0,\n                            file_count: 1,\n                            file_name: v86_bin,\n\n                            lengthComputable: e.lengthComputable,\n                            total: e.total,\n                            loaded: e.loaded,\n                        });\n                    }\n                });\n            });\n        };\n    }\n\n    wasm_fn({ \"env\": wasm_shared_funcs })\n        .then((exports) => {\n            wasm_memory = exports.memory;\n            exports[\"rust_init\"]();\n\n            const emulator = this.v86 = new v86(this.emulator_bus, { exports, wasm_table });\n            cpu = emulator.cpu;\n\n            this.continue_init(emulator, options);\n        });\n\n    this.zstd_worker = null;\n    this.zstd_worker_request_id = 0;\n}\n\nV86.prototype.continue_init = async function(emulator, options)\n{\n    this.bus.register(\"emulator-stopped\", function()\n    {\n        this.cpu_is_running = false;\n        this.screen_adapter.pause();\n    }, this);\n\n    this.bus.register(\"emulator-started\", function()\n    {\n        this.cpu_is_running = true;\n        this.screen_adapter.continue();\n    }, this);\n\n    var settings = {};\n\n    const boot_order =\n        options.boot_order ? options.boot_order :\n        options.fda ? BOOT_ORDER_FD_FIRST :\n        options.hda ? BOOT_ORDER_HD_FIRST : BOOT_ORDER_CD_FIRST;\n\n    settings.acpi = options.acpi;\n    settings.disable_jit = options.disable_jit;\n    settings.load_devices = true;\n    settings.memory_size = options.memory_size || 64 * 1024 * 1024;\n    settings.vga_memory_size = options.vga_memory_size || 8 * 1024 * 1024;\n    settings.boot_order = boot_order;\n    settings.fastboot = options.fastboot || false;\n    settings.fda = undefined;\n    settings.fdb = undefined;\n    settings.uart1 = options.uart1;\n    settings.uart2 = options.uart2;\n    settings.uart3 = options.uart3;\n    settings.cmdline = options.cmdline;\n    settings.preserve_mac_from_state_image = options.preserve_mac_from_state_image;\n    settings.mac_address_translation = options.mac_address_translation;\n    settings.cpuid_level = options.cpuid_level;\n    settings.virtio_balloon = options.virtio_balloon;\n    settings.virtio_console = !!options.virtio_console;\n\n    const relay_url = options.network_relay_url || options.net_device && options.net_device.relay_url;\n    if(relay_url)\n    {\n        // TODO: remove bus, use direct calls instead\n        if(relay_url === \"fetch\")\n        {\n            this.network_adapter = new FetchNetworkAdapter(this.bus, options.net_device);\n        }\n        else if(relay_url === \"inbrowser\")\n        {\n            // NOTE: experimental, will change when usage of options.net_device gets refactored in favour of emulator.bus\n            this.network_adapter = new InBrowserNetworkAdapter(this.bus, options.net_device);\n        }\n        else if(relay_url.startsWith(\"wisp://\") || relay_url.startsWith(\"wisps://\"))\n        {\n            this.network_adapter = new WispNetworkAdapter(relay_url, this.bus, options.net_device);\n        }\n        else\n        {\n            this.network_adapter = new NetworkAdapter(relay_url, this.bus);\n        }\n    }\n\n    // Enable unconditionally, so that state images don't miss hardware\n    // TODO: Should be properly fixed in restore_state\n    settings.net_device = options.net_device || { type: \"ne2k\" };\n\n    const screen_options = options.screen || {};\n    if(options.screen_container)\n    {\n        screen_options.container = options.screen_container;\n    }\n\n    if(!options.disable_keyboard)\n    {\n        this.keyboard_adapter = new KeyboardAdapter(this.bus);\n    }\n    if(!options.disable_mouse)\n    {\n        this.mouse_adapter = new MouseAdapter(this.bus, screen_options.container);\n    }\n\n    if(screen_options.container)\n    {\n        this.screen_adapter = new ScreenAdapter(screen_options, () => this.v86.cpu.devices.vga && this.v86.cpu.devices.vga.screen_fill_buffer());\n    }\n    else\n    {\n        this.screen_adapter = new DummyScreenAdapter(screen_options);\n    }\n    settings.screen = this.screen_adapter;\n    settings.screen_options = screen_options;\n\n    settings.serial_console = options.serial_console || { type: \"none\" };\n\n    // NOTE: serial_container_xtermjs and serial_container are deprecated\n    if(options.serial_container_xtermjs)\n    {\n        settings.serial_console.type = \"xtermjs\";\n        settings.serial_console.container = options.serial_container_xtermjs;\n    }\n    else if(options.serial_container)\n    {\n        settings.serial_console.type = \"textarea\";\n        settings.serial_console.container = options.serial_container;\n    }\n\n    if(settings.serial_console?.type === \"xtermjs\")\n    {\n        const xterm_lib = settings.serial_console.xterm_lib || window[\"Terminal\"];\n        this.serial_adapter = new SerialAdapterXtermJS(settings.serial_console.container, this.bus, xterm_lib);\n    }\n    else if(settings.serial_console?.type === \"textarea\")\n    {\n        this.serial_adapter = new SerialAdapter(settings.serial_console.container, this.bus);\n        //this.recording_adapter = new SerialRecordingAdapter(this.bus);\n    }\n\n    const virtio_console_settings = (options.virtio_console && typeof options.virtio_console === \"boolean\") ? { type: \"none\" } : options.virtio_console;\n\n    if(virtio_console_settings?.type === \"xtermjs\")\n    {\n        const xterm_lib = virtio_console_settings.xterm_lib || window[\"Terminal\"];\n        this.virtio_console_adapter = new VirtioConsoleAdapterXtermJS(virtio_console_settings.container, this.bus, xterm_lib);\n    }\n    else if(virtio_console_settings?.type === \"textarea\")\n    {\n        this.virtio_console_adapter = new VirtioConsoleAdapter(virtio_console_settings.container, this.bus);\n    }\n\n    if(!options.disable_speaker)\n    {\n        this.speaker_adapter = new SpeakerAdapter(this.bus);\n    }\n\n    // ugly, but required for closure compiler compilation\n    function put_on_settings(name, buffer)\n    {\n        switch(name)\n        {\n            case \"hda\":\n                settings.hda = buffer;\n                break;\n            case \"hdb\":\n                settings.hdb = buffer;\n                break;\n            case \"cdrom\":\n                settings.cdrom = buffer;\n                break;\n            case \"fda\":\n                settings.fda = buffer;\n                break;\n            case \"fdb\":\n                settings.fdb = buffer;\n                break;\n\n            case \"multiboot\":\n                settings.multiboot = buffer.buffer;\n                break;\n            case \"bzimage\":\n                settings.bzimage = buffer.buffer;\n                break;\n            case \"initrd\":\n                settings.initrd = buffer.buffer;\n                break;\n\n            case \"bios\":\n                settings.bios = buffer.buffer;\n                break;\n            case \"vga_bios\":\n                settings.vga_bios = buffer.buffer;\n                break;\n            case \"initial_state\":\n                settings.initial_state = buffer.buffer;\n                break;\n            case \"fs9p_json\":\n                settings.fs9p_json = buffer;\n                break;\n            default:\n                dbg_assert(false, name);\n        }\n    }\n\n    var files_to_load = [];\n\n    const add_file = (name, file) =>\n    {\n        if(!file)\n        {\n            return;\n        }\n\n        if(file.get && file.set && file.load)\n        {\n            files_to_load.push({\n                name: name,\n                loadable: file,\n            });\n            return;\n        }\n\n        if(name === \"bios\" || name === \"vga_bios\" ||\n            name === \"initial_state\" || name === \"multiboot\" ||\n            name === \"bzimage\" || name === \"initrd\")\n        {\n            // Ignore async for these because they must be available before boot.\n            // This should make result.buffer available after the object is loaded\n            file.async = false;\n        }\n\n        if(name === \"fda\" || name === \"fdb\")\n        {\n            // small, doesn't make sense loading asynchronously\n            file.async = false;\n        }\n\n        if(file.url && !file.async)\n        {\n            files_to_load.push({\n                name: name,\n                url: file.url,\n                size: file.size,\n            });\n        }\n        else\n        {\n            files_to_load.push({\n                name,\n                loadable: buffer_from_object(file, this.zstd_decompress_worker.bind(this)),\n            });\n        }\n    };\n\n    if(options.state)\n    {\n        console.warn(\"Warning: Unknown option 'state'. Did you mean 'initial_state'?\");\n    }\n\n    add_file(\"bios\", options.bios);\n    add_file(\"vga_bios\", options.vga_bios);\n    add_file(\"cdrom\", options.cdrom);\n    add_file(\"hda\", options.hda);\n    add_file(\"hdb\", options.hdb);\n    add_file(\"fda\", options.fda);\n    add_file(\"fdb\", options.fdb);\n    add_file(\"initial_state\", options.initial_state);\n    add_file(\"multiboot\", options.multiboot);\n    add_file(\"bzimage\", options.bzimage);\n    add_file(\"initrd\", options.initrd);\n\n    if(options.filesystem && options.filesystem.handle9p)\n    {\n        settings.handle9p = options.filesystem.handle9p;\n    }\n    else if(options.filesystem && options.filesystem.proxy_url)\n    {\n        settings.proxy9p = options.filesystem.proxy_url;\n    }\n    else if(options.filesystem)\n    {\n        var fs_url = options.filesystem.basefs;\n        var base_url = options.filesystem.baseurl;\n\n        let file_storage = new MemoryFileStorage();\n\n        if(base_url)\n        {\n            file_storage = new ServerFileStorageWrapper(file_storage, base_url, this.zstd_decompress.bind(this));\n        }\n        settings.fs9p = this.fs9p = new FS(file_storage);\n\n        if(fs_url)\n        {\n            dbg_assert(base_url, \"Filesystem: baseurl must be specified\");\n\n            var size;\n\n            if(typeof fs_url === \"object\")\n            {\n                size = fs_url.size;\n                fs_url = fs_url.url;\n            }\n            dbg_assert(typeof fs_url === \"string\");\n\n            files_to_load.push({\n                name: \"fs9p_json\",\n                url: fs_url,\n                size: size,\n                as_json: true,\n            });\n        }\n    }\n\n    var starter = this;\n    var total = files_to_load.length;\n\n    var cont = function(index)\n    {\n        if(index === total)\n        {\n            setTimeout(done.bind(this), 0);\n            return;\n        }\n\n        var f = files_to_load[index];\n\n        if(f.loadable)\n        {\n            f.loadable.onload = function(e)\n            {\n                put_on_settings.call(this, f.name, f.loadable);\n                cont(index + 1);\n            }.bind(this);\n            f.loadable.load();\n        }\n        else\n        {\n            load_file(f.url, {\n                done: function(result)\n                {\n                    if(f.url.endsWith(\".zst\") && f.name !== \"initial_state\")\n                    {\n                        dbg_assert(f.size, \"A size must be provided for compressed images\");\n                        result = this.zstd_decompress(f.size, new Uint8Array(result));\n                    }\n\n                    put_on_settings.call(this, f.name, f.as_json ? result : new SyncBuffer(result));\n                    cont(index + 1);\n                }.bind(this),\n                progress: function progress(e)\n                {\n                    if(e.target.status === 200)\n                    {\n                        starter.emulator_bus.send(\"download-progress\", {\n                            file_index: index,\n                            file_count: total,\n                            file_name: f.url,\n\n                            lengthComputable: e.lengthComputable,\n                            total: e.total || f.size,\n                            loaded: e.loaded,\n                        });\n                    }\n                    else\n                    {\n                        starter.emulator_bus.send(\"download-error\", {\n                            file_index: index,\n                            file_count: total,\n                            file_name: f.url,\n                            request: e.target,\n                        });\n                    }\n                },\n                as_json: f.as_json,\n            });\n        }\n    }.bind(this);\n    cont(0);\n\n    async function done()\n    {\n        //if(settings.initial_state)\n        //{\n        //    // avoid large allocation now, memory will be restored later anyway\n        //    settings.memory_size = 0;\n        //}\n\n        if(settings.fs9p && settings.fs9p_json)\n        {\n            if(!settings.initial_state)\n            {\n                settings.fs9p.load_from_json(settings.fs9p_json);\n\n                if(options.bzimage_initrd_from_filesystem)\n                {\n                    const { bzimage_path, initrd_path } = this.get_bzimage_initrd_from_filesystem(settings.fs9p);\n\n                    dbg_log(\"Found bzimage: \" + bzimage_path + \" and initrd: \" + initrd_path);\n\n                    const [initrd, bzimage] = await Promise.all([\n                        settings.fs9p.read_file(initrd_path),\n                        settings.fs9p.read_file(bzimage_path),\n                    ]);\n                    put_on_settings.call(this, \"initrd\", new SyncBuffer(initrd.buffer));\n                    put_on_settings.call(this, \"bzimage\", new SyncBuffer(bzimage.buffer));\n                }\n            }\n            else\n            {\n                dbg_log(\"Filesystem basefs ignored: Overridden by state image\");\n            }\n        }\n        else\n        {\n            dbg_assert(\n                !options.bzimage_initrd_from_filesystem || settings.initial_state,\n                \"bzimage_initrd_from_filesystem: Requires a filesystem\");\n        }\n\n        this.serial_adapter && this.serial_adapter.show && this.serial_adapter.show();\n        this.virtio_console_adapter && this.virtio_console_adapter.show && this.virtio_console_adapter.show();\n\n        this.v86.init(settings);\n\n        if(settings.initial_state)\n        {\n            emulator.restore_state(settings.initial_state);\n\n            // The GC can't free settings, since it is referenced from\n            // several closures. This isn't needed anymore, so we delete it\n            // here\n            settings.initial_state = undefined;\n        }\n\n        if(options.autostart)\n        {\n            this.v86.run();\n        }\n\n        this.emulator_bus.send(\"emulator-loaded\");\n    }\n};\n\n/**\n * @param {number} decompressed_size\n * @param {Uint8Array} src\n * @return {ArrayBuffer}\n */\nV86.prototype.zstd_decompress = function(decompressed_size, src)\n{\n    const cpu = this.v86.cpu;\n\n    dbg_assert(!this.zstd_context);\n    this.zstd_context = cpu.zstd_create_ctx(src.length);\n\n    new Uint8Array(cpu.wasm_memory.buffer).set(src, cpu.zstd_get_src_ptr(this.zstd_context));\n\n    const ptr = cpu.zstd_read(this.zstd_context, decompressed_size);\n    const result = cpu.wasm_memory.buffer.slice(ptr, ptr + decompressed_size);\n    cpu.zstd_read_free(ptr, decompressed_size);\n\n    cpu.zstd_free_ctx(this.zstd_context);\n    this.zstd_context = null;\n\n    return result;\n};\n\n/**\n * @param {number} decompressed_size\n * @param {Uint8Array} src\n * @return {Promise<ArrayBuffer>}\n */\nV86.prototype.zstd_decompress_worker = async function(decompressed_size, src)\n{\n    if(!this.zstd_worker)\n    {\n        function the_worker()\n        {\n            let wasm;\n\n            globalThis.onmessage = function(e)\n            {\n                if(!wasm)\n                {\n                    const env = Object.fromEntries([\n                        \"cpu_exception_hook\", \"run_hardware_timers\",\n                        \"cpu_event_halt\", \"microtick\", \"get_rand_int\", \"stop_idling\",\n                        \"io_port_read8\", \"io_port_read16\", \"io_port_read32\",\n                        \"io_port_write8\", \"io_port_write16\", \"io_port_write32\",\n                        \"mmap_read8\", \"mmap_read32\",\n                        \"mmap_write8\", \"mmap_write16\", \"mmap_write32\", \"mmap_write64\", \"mmap_write128\",\n                        \"codegen_finalize\",\n                        \"jit_clear_func\", \"jit_clear_all_funcs\",\n                    ].map(f => [f, () => console.error(\"zstd worker unexpectedly called \" + f)]));\n\n                    env[\"__indirect_function_table\"] = new WebAssembly.Table({ element: \"anyfunc\", initial: 1024 });\n                    env[\"abort\"] = () => { throw new Error(\"zstd worker aborted\"); };\n                    env[\"log_from_wasm\"] = env[\"console_log_from_wasm\"] = (off, len) => {\n                        console.log(read_sized_string_from_mem(wasm.exports.memory.buffer, off, len));\n                    };\n                    env[\"dbg_trace_from_wasm\"] = () => console.trace();\n\n                    wasm = new WebAssembly.Instance(new WebAssembly.Module(e.data), { \"env\": env });\n                    return;\n                }\n\n                const { src, decompressed_size, id } = e.data;\n                const exports = wasm.exports;\n\n                const zstd_context = exports[\"zstd_create_ctx\"](src.length);\n                new Uint8Array(exports.memory.buffer).set(src, exports[\"zstd_get_src_ptr\"](zstd_context));\n\n                const ptr = exports[\"zstd_read\"](zstd_context, decompressed_size);\n                const result = exports.memory.buffer.slice(ptr, ptr + decompressed_size);\n                exports[\"zstd_read_free\"](ptr, decompressed_size);\n\n                exports[\"zstd_free_ctx\"](zstd_context);\n\n                postMessage({ result, id }, [result]);\n            };\n        }\n\n        const url = URL.createObjectURL(new Blob([\"(\" + the_worker.toString() + \")()\"], { type: \"text/javascript\" }));\n        this.zstd_worker = new Worker(url);\n        URL.revokeObjectURL(url);\n        this.zstd_worker.postMessage(this.wasm_source, [this.wasm_source]);\n    }\n\n    return new Promise(resolve => {\n        const id = this.zstd_worker_request_id++;\n        const done = async e =>\n        {\n            if(e.data.id === id)\n            {\n                this.zstd_worker.removeEventListener(\"message\", done);\n                dbg_assert(decompressed_size === e.data.result.byteLength);\n                resolve(e.data.result);\n            }\n        };\n        this.zstd_worker.addEventListener(\"message\", done);\n        this.zstd_worker.postMessage({ src, decompressed_size, id }, [src.buffer]);\n    });\n};\n\nV86.prototype.get_bzimage_initrd_from_filesystem = function(filesystem)\n{\n    const root = (filesystem.read_dir(\"/\") || []).map(x => \"/\" + x);\n    const boot = (filesystem.read_dir(\"/boot/\") || []).map(x => \"/boot/\" + x);\n\n    let initrd_path;\n    let bzimage_path;\n\n    for(const f of [].concat(root, boot))\n    {\n        const old = /old/i.test(f) || /fallback/i.test(f);\n        const is_bzimage = /vmlinuz/i.test(f) || /bzimage/i.test(f);\n        const is_initrd = /initrd/i.test(f) || /initramfs/i.test(f);\n\n        if(is_bzimage && (!bzimage_path || !old))\n        {\n            bzimage_path = f;\n        }\n\n        if(is_initrd && (!initrd_path || !old))\n        {\n            initrd_path = f;\n        }\n    }\n\n    if(!initrd_path || !bzimage_path)\n    {\n        console.log(\"Failed to find bzimage or initrd in filesystem. Files:\");\n        console.log(root.join(\" \"));\n        console.log(boot.join(\" \"));\n    }\n\n    return { initrd_path, bzimage_path };\n};\n\n/**\n * Start emulation. Do nothing if emulator is running already. Can be asynchronous.\n */\nV86.prototype.run = async function()\n{\n    this.v86.run();\n};\n\n/**\n * Stop emulation. Do nothing if emulator is not running. Can be asynchronous.\n */\nV86.prototype.stop = async function()\n{\n    if(!this.cpu_is_running)\n    {\n        return;\n    }\n\n    await new Promise(resolve => {\n        const listener = () => {\n            this.remove_listener(\"emulator-stopped\", listener);\n            resolve();\n        };\n        this.add_listener(\"emulator-stopped\", listener);\n        this.v86.stop();\n    });\n};\n\n/**\n * Free resources associated with this instance\n */\nV86.prototype.destroy = async function()\n{\n    await this.stop();\n\n    this.v86.destroy();\n    this.keyboard_adapter && this.keyboard_adapter.destroy();\n    this.network_adapter && this.network_adapter.destroy();\n    this.mouse_adapter && this.mouse_adapter.destroy();\n    this.screen_adapter && this.screen_adapter.destroy();\n    this.serial_adapter && this.serial_adapter.destroy();\n    this.speaker_adapter && this.speaker_adapter.destroy();\n    this.virtio_console_adapter && this.virtio_console_adapter.destroy();\n};\n\n/**\n * Restart (force a reboot).\n */\nV86.prototype.restart = function()\n{\n    this.v86.restart();\n};\n\n/**\n * Add an event listener (the emulator is an event emitter).\n *\n * The callback function gets a single argument which depends on the event.\n *\n * @param {string} event Name of the event.\n * @param {function(?)} listener The callback function.\n */\nV86.prototype.add_listener = function(event, listener)\n{\n    this.bus.register(event, listener, this);\n};\n\n/**\n * Remove an event listener.\n *\n * @param {string} event\n * @param {function(*)} listener\n */\nV86.prototype.remove_listener = function(event, listener)\n{\n    this.bus.unregister(event, listener);\n};\n\n/**\n * Restore the emulator state from the given state, which must be an\n * ArrayBuffer returned by\n * [`save_state`](#save_statefunctionobject-arraybuffer-callback).\n *\n * Note that the state can only be restored correctly if this constructor has\n * been created with the same options as the original instance (e.g., same disk\n * images, memory size, etc.).\n *\n * Different versions of the emulator might use a different format for the\n * state buffer.\n *\n * @param {ArrayBuffer} state\n */\nV86.prototype.restore_state = async function(state)\n{\n    dbg_assert(arguments.length === 1);\n    this.v86.restore_state(state);\n};\n\n/**\n * Asynchronously save the current state of the emulator.\n *\n * @return {Promise<ArrayBuffer>}\n */\nV86.prototype.save_state = async function()\n{\n    dbg_assert(arguments.length === 0);\n    return this.v86.save_state();\n};\n\n/**\n * @return {number}\n * @ignore\n */\nV86.prototype.get_instruction_counter = function()\n{\n    if(this.v86)\n    {\n        return this.v86.cpu.instruction_counter[0] >>> 0;\n    }\n    else\n    {\n        // TODO: Should be handled using events\n        return 0;\n    }\n};\n\n/**\n * @return {boolean}\n */\nV86.prototype.is_running = function()\n{\n    return this.cpu_is_running;\n};\n\n/**\n * Set the image inserted in the floppy drive. Can be changed at runtime, as\n * when physically changing the floppy disk.\n */\nV86.prototype.set_fda = async function(file)\n{\n    const fda = this.v86.cpu.devices.fdc.drives[0];\n    if(file.url && !file.async)\n    {\n        await new Promise(resolve => {\n            load_file(file.url, {\n                done: result =>\n                {\n                    fda.insert_disk(new SyncBuffer(result));\n                    resolve();\n                }\n            });\n        });\n    }\n    else\n    {\n        const image = buffer_from_object(file, this.zstd_decompress_worker.bind(this));\n        image.onload = () =>\n        {\n            fda.insert_disk(image);\n        };\n        await image.load();\n    }\n};\n\n/**\n * Set the image inserted in the second floppy drive, also at runtime.\n */\nV86.prototype.set_fdb = async function(file)\n{\n    const fdb = this.v86.cpu.devices.fdc.drives[1];\n    if(file.url && !file.async)\n    {\n        await new Promise(resolve => {\n            load_file(file.url, {\n                done: result =>\n                {\n                    fdb.insert_disk(new SyncBuffer(result));\n                    resolve();\n                }\n            });\n        });\n    }\n    else\n    {\n        const image = buffer_from_object(file, this.zstd_decompress_worker.bind(this));\n        image.onload = () =>\n        {\n            fdb.insert_disk(image);\n        };\n        await image.load();\n    }\n};\n\n/**\n * Eject floppy drive fda.\n */\nV86.prototype.eject_fda = function()\n{\n    this.v86.cpu.devices.fdc.drives[0].eject_disk();\n};\n\n/**\n * Eject second floppy drive fdb.\n */\nV86.prototype.eject_fdb = function()\n{\n    this.v86.cpu.devices.fdc.drives[1].eject_disk();\n};\n\n/**\n * Return buffer object of floppy disk of drive fda or null if the drive is empty.\n * @return {Uint8Array|null}\n */\nV86.prototype.get_disk_fda = function()\n{\n    return this.v86.cpu.devices.fdc.drives[0].get_buffer();\n};\n\n/**\n * Return buffer object of second floppy disk of drive fdb or null if the drive is empty.\n * @return {Uint8Array|null}\n */\nV86.prototype.get_disk_fdb = function()\n{\n    return this.v86.cpu.devices.fdc.drives[1].get_buffer();\n};\n\n/**\n * Set the image inserted in the CD-ROM drive. Can be changed at runtime, as\n * when physically changing the CD-ROM.\n */\nV86.prototype.set_cdrom = async function(file)\n{\n    if(file.url && !file.async)\n    {\n        load_file(file.url, {\n            done: result =>\n            {\n                this.v86.cpu.devices.cdrom.set_cdrom(new SyncBuffer(result));\n            },\n        });\n    }\n    else\n    {\n        const image = buffer_from_object(file, this.zstd_decompress_worker.bind(this));\n        image.onload = () =>\n        {\n            this.v86.cpu.devices.cdrom.set_cdrom(image);\n        };\n        await image.load();\n    }\n};\n\n/**\n * Eject the CD-ROM.\n */\nV86.prototype.eject_cdrom = function()\n{\n    this.v86.cpu.devices.cdrom.eject();\n};\n\n/**\n * Send a sequence of scan codes to the emulated PS2 controller. A list of\n * codes can be found at http://stanislavs.org/helppc/make_codes.html.\n * Do nothing if there is no keyboard controller.\n *\n * @param {Array.<number>} codes\n * @param {number=} delay\n */\nV86.prototype.keyboard_send_scancodes = async function(codes, delay)\n{\n    for(var i = 0; i < codes.length; i++)\n    {\n        this.bus.send(\"keyboard-code\", codes[i]);\n        if(delay) await new Promise(resolve => setTimeout(resolve, delay));\n    }\n};\n\n/**\n * Send translated keys\n * @param {Array.<number>} codes\n * @param {number=} delay\n */\nV86.prototype.keyboard_send_keys = async function(codes, delay)\n{\n    for(var i = 0; i < codes.length; i++)\n    {\n        this.keyboard_adapter.simulate_press(codes[i]);\n        if(delay) await new Promise(resolve => setTimeout(resolve, delay));\n    }\n};\n\n/**\n * Send text, assuming the guest OS uses a US keyboard layout\n * @param {string} string\n * @param {number=} delay\n */\nV86.prototype.keyboard_send_text = async function(string, delay)\n{\n    for(var i = 0; i < string.length; i++)\n    {\n        this.keyboard_adapter.simulate_char(string[i]);\n        if(delay) await new Promise(resolve => setTimeout(resolve, delay));\n    }\n};\n\n/**\n * Download a screenshot (returns an <img> element, only works in browsers)\n */\nV86.prototype.screen_make_screenshot = function()\n{\n    if(this.screen_adapter)\n    {\n        return this.screen_adapter.make_screenshot();\n    }\n    return null;\n};\n\n/**\n * Set the scaling level of the emulated screen.\n *\n * @param {number} sx\n * @param {number} sy\n */\nV86.prototype.screen_set_scale = function(sx, sy)\n{\n    if(this.screen_adapter)\n    {\n        this.screen_adapter.set_scale(sx, sy);\n    }\n};\n\n/**\n * Go fullscreen (only browsers)\n */\nV86.prototype.screen_go_fullscreen = function()\n{\n    if(!this.screen_adapter)\n    {\n        return;\n    }\n\n    var elem = document.getElementById(\"screen_container\");\n\n    if(!elem)\n    {\n        return;\n    }\n\n    // bracket notation because otherwise they get renamed by closure compiler\n    var fn = elem[\"requestFullScreen\"] ||\n            elem[\"webkitRequestFullscreen\"] ||\n            elem[\"mozRequestFullScreen\"] ||\n            elem[\"msRequestFullScreen\"];\n\n    if(fn)\n    {\n        fn.call(elem);\n\n        // This is necessary, because otherwise chromium keyboard doesn't work anymore.\n        // Might (but doesn't seem to) break something else\n        var focus_element = document.getElementsByClassName(\"phone_keyboard\")[0];\n        focus_element && focus_element.focus();\n    }\n\n    try {\n        navigator.keyboard.lock();\n    } catch(e) {}\n\n    this.lock_mouse();\n};\n\n/**\n * Lock the mouse cursor: It becomes invisble and is not moved out of the\n * browser window.\n */\nV86.prototype.lock_mouse = async function()\n{\n    const elem = document.body;\n\n    try\n    {\n        await elem.requestPointerLock({\n            unadjustedMovement: true,\n        });\n    }\n    catch(e)\n    {\n        // as per MDN, retry without unadjustedMovement option\n        await elem.requestPointerLock();\n    }\n};\n\n/**\n * Enable or disable sending mouse events to the emulated PS2 controller.\n *\n * @param {boolean} enabled\n */\nV86.prototype.mouse_set_enabled = function(enabled)\n{\n    if(this.mouse_adapter)\n    {\n        this.mouse_adapter.emu_enabled = enabled;\n    }\n};\nV86.prototype.mouse_set_status = V86.prototype.mouse_set_enabled;\n\n/**\n * Enable or disable sending keyboard events to the emulated PS2 controller.\n *\n * @param {boolean} enabled\n */\nV86.prototype.keyboard_set_enabled = function(enabled)\n{\n    if(this.keyboard_adapter)\n    {\n        this.keyboard_adapter.emu_enabled = enabled;\n    }\n};\nV86.prototype.keyboard_set_status = V86.prototype.keyboard_set_enabled;\n\n/**\n * Send a string to the first emulated serial terminal.\n *\n * @param {string} data\n */\nV86.prototype.serial0_send = function(data)\n{\n    for(var i = 0; i < data.length; i++)\n    {\n        this.bus.send(\"serial0-input\", data.charCodeAt(i));\n    }\n};\n\n/**\n * Send bytes to a serial port (to be received by the emulated PC).\n *\n * @param {Uint8Array} data\n */\nV86.prototype.serial_send_bytes = function(serial, data)\n{\n    for(var i = 0; i < data.length; i++)\n    {\n        this.bus.send(\"serial\" + serial + \"-input\", data[i]);\n    }\n};\n\n/**\n * Set the modem status of a serial port.\n */\nV86.prototype.serial_set_modem_status = function(serial, status)\n{\n    this.bus.send(\"serial\" + serial + \"-modem-status-input\", status);\n};\n\n/**\n * Set the carrier detect status of a serial port.\n */\nV86.prototype.serial_set_carrier_detect = function(serial, status)\n{\n    this.bus.send(\"serial\" + serial + \"-carrier-detect-input\", status);\n};\n\n/**\n * Set the ring indicator status of a serial port.\n */\nV86.prototype.serial_set_ring_indicator = function(serial, status)\n{\n    this.bus.send(\"serial\" + serial + \"-ring-indicator-input\", status);\n};\n\n/**\n * Set the data set ready status of a serial port.\n */\nV86.prototype.serial_set_data_set_ready = function(serial, status)\n{\n    this.bus.send(\"serial\" + serial + \"-data-set-ready-input\", status);\n};\n\n/**\n * Set the clear to send status of a serial port.\n */\nV86.prototype.serial_set_clear_to_send = function(serial, status)\n{\n    this.bus.send(\"serial\" + serial + \"-clear-to-send-input\", status);\n};\n\n/**\n * Write to a file in the 9p filesystem. Nothing happens if no filesystem has\n * been initialized.\n *\n * @param {string} file\n * @param {Uint8Array} data\n */\nV86.prototype.create_file = async function(file, data)\n{\n    dbg_assert(arguments.length === 2);\n    var fs = this.fs9p;\n\n    if(!fs)\n    {\n        return;\n    }\n\n    var parts = file.split(\"/\");\n    var filename = parts[parts.length - 1];\n\n    var path_infos = fs.SearchPath(file);\n    var parent_id = path_infos.parentid;\n    var not_found = filename === \"\" || parent_id === -1;\n\n    if(!not_found)\n    {\n        await fs.CreateBinaryFile(filename, parent_id, data);\n    }\n    else\n    {\n        return Promise.reject(new FileNotFoundError());\n    }\n};\n\n/**\n * Read a file in the 9p filesystem. Nothing happens if no filesystem has been\n * initialized.\n *\n * @param {string} file\n */\nV86.prototype.read_file = async function(file)\n{\n    dbg_assert(arguments.length === 1);\n    var fs = this.fs9p;\n\n    if(!fs)\n    {\n        return;\n    }\n\n    const result = await fs.read_file(file);\n\n    if(result)\n    {\n        return result;\n    }\n    else\n    {\n        return Promise.reject(new FileNotFoundError());\n    }\n};\n\n/*\n * @deprecated\n * Use wait_until_vga_screen_contains etc.\n */\nV86.prototype.automatically = function(steps)\n{\n    const run = (steps) =>\n    {\n        const step = steps[0];\n\n        if(!step)\n        {\n            return;\n        }\n\n        const remaining_steps = steps.slice(1);\n\n        if(step.sleep)\n        {\n            setTimeout(() => run(remaining_steps), step.sleep * 1000);\n            return;\n        }\n\n        if(step.vga_text)\n        {\n            this.wait_until_vga_screen_contains(step.vga_text).then(() => run(remaining_steps));\n            return;\n        }\n\n        if(step.keyboard_send)\n        {\n            if(Array.isArray(step.keyboard_send))\n            {\n                this.keyboard_send_scancodes(step.keyboard_send);\n            }\n            else\n            {\n                dbg_assert(typeof step.keyboard_send === \"string\");\n                this.keyboard_send_text(step.keyboard_send);\n            }\n\n            run(remaining_steps);\n            return;\n        }\n\n        if(step.call)\n        {\n            step.call();\n            run(remaining_steps);\n            return;\n        }\n\n        dbg_assert(false, step);\n    };\n\n    run(steps);\n};\n\n/**\n * Wait until expected text is present on the VGA text screen.\n *\n * Returns immediately if the expected text is already present on screen\n * at the time this funtion is called.\n *\n * An optional timeout may be specified in `options.timeout_msec`, returns\n * false if the timeout expires before the expected text could be detected.\n *\n * Expected text (or texts, see below) must be of type string or RegExp,\n * strings are tested against the beginning of a screen line, regular\n * expressions against the full line but may use wildcards for partial\n * matching.\n *\n * Two methods of text detection are supported depending on the type of the\n * argument `expected`:\n *\n * 1. If `expected` is a string or RegExp then the given text string or\n *    regular expression may match any line on screen for this function\n *    to succeed.\n *\n * 2. If `expected` is an array of strings and/or RegExp objects then the\n *    list of expected lines must match exactly at \"the bottom\" of the\n *    screen. The \"bottom\" line is the first non-empty line starting from\n *    the screen's end.\n *    Expected lines should not contain any trailing whitespace and/or\n *    newline characters. Expecting an empty line is valid.\n *\n * Returns `true` on success and `false` when the timeout has expired.\n *\n * @param {string|RegExp|Array<string|RegExp>} expected\n * @param {{timeout_msec:(number|undefined)}=} options\n */\nV86.prototype.wait_until_vga_screen_contains = async function(expected, options)\n{\n    const match_multi = Array.isArray(expected);\n    const timeout_msec = options?.timeout_msec || 0;\n    const changed_rows = new Set();\n    const screen_put_char = args => changed_rows.add(args[0]);\n    const contains_expected = (screen_line, pattern) => pattern.test ? pattern.test(screen_line) : screen_line.startsWith(pattern);\n    const screen_lines = [];\n\n    this.add_listener(\"screen-put-char\", screen_put_char);\n\n    for(const screen_line of this.screen_adapter.get_text_screen())\n    {\n        if(match_multi)\n        {\n            screen_lines.push(screen_line.trimRight());\n        }\n        else if(contains_expected(screen_line, expected))\n        {\n            this.remove_listener(\"screen-put-char\", screen_put_char);\n            return true;\n        }\n    }\n\n    let succeeded = false;\n    const end = timeout_msec ? performance.now() + timeout_msec : 0;\n    loop: while(!end || performance.now() < end)\n    {\n        if(match_multi)\n        {\n            let screen_height = screen_lines.length;\n            while(screen_height > 0 && screen_lines[screen_height - 1] === \"\")\n            {\n                screen_height--;\n            }\n            const screen_offset = screen_height - expected.length;\n            if(screen_offset >= 0)\n            {\n                let matches = true;\n                for(let i = 0; i < expected.length && matches; i++)\n                {\n                    matches = contains_expected(screen_lines[screen_offset + i], expected[i]);\n                }\n                if(matches)\n                {\n                    succeeded = true;\n                    break;\n                }\n            }\n        }\n\n        await new Promise(resolve => setTimeout(resolve, 100));\n\n        for(const row of changed_rows)\n        {\n            const screen_line = this.screen_adapter.get_text_row(row);\n            if(match_multi)\n            {\n                screen_lines[row] = screen_line.trimRight();\n            }\n            else if(contains_expected(screen_line, expected))\n            {\n                succeeded = true;\n                break loop;\n            }\n        }\n        changed_rows.clear();\n    }\n\n    this.remove_listener(\"screen-put-char\", screen_put_char);\n    return succeeded;\n};\n\n/**\n * Reads data from memory at specified offset.\n *\n * @param {number} offset\n * @param {number} length\n * @returns\n */\nV86.prototype.read_memory = function(offset, length)\n{\n    return this.v86.cpu.read_blob(offset, length);\n};\n\n/**\n * Writes data to memory at specified offset.\n *\n * @param {Array.<number>|Uint8Array} blob\n * @param {number} offset\n */\nV86.prototype.write_memory = function(blob, offset)\n{\n    this.v86.cpu.write_blob(blob, offset);\n};\n\n/*\n * @param {HTMLElement} element\n * @param {Function} [xterm_lib]\n */\nV86.prototype.set_serial_container_xtermjs = function(element, xterm_lib = window[\"Terminal\"])\n{\n    this.serial_adapter && this.serial_adapter.destroy && this.serial_adapter.destroy();\n    this.serial_adapter = new SerialAdapterXtermJS(element, this.bus, xterm_lib);\n    this.serial_adapter.show();\n};\n\n/*\n * @param {HTMLElement} element\n * @param {Function} [xterm_lib]\n */\nV86.prototype.set_virtio_console_container_xtermjs = function(element, xterm_lib = window[\"Terminal\"])\n{\n    this.virtio_console_adapter && this.virtio_console_adapter.destroy && this.virtio_console_adapter.destroy();\n    this.virtio_console_adapter = new VirtioConsoleAdapterXtermJS(element, this.bus, xterm_lib);\n    this.virtio_console_adapter.show();\n};\n\nV86.prototype.get_instruction_stats = function()\n{\n    return print_stats.stats_to_string(this.v86.cpu);\n};\n\n/**\n * @ignore\n * @constructor\n *\n * @param {string=} message\n */\nfunction FileExistsError(message)\n{\n    this.message = message || \"File already exists\";\n}\nFileExistsError.prototype = Error.prototype;\n\n/**\n * @ignore\n * @constructor\n *\n * @param {string=} message\n */\nfunction FileNotFoundError(message)\n{\n    this.message = message || \"File not found\";\n}\nFileNotFoundError.prototype = Error.prototype;\n\n/* global module, self */\n\nif(typeof module !== \"undefined\" && typeof module.exports !== \"undefined\")\n{\n    module.exports[\"V86\"] = V86;\n}\nelse if(typeof window !== \"undefined\")\n{\n    window[\"V86\"] = V86;\n}\nelse if(typeof importScripts === \"function\")\n{\n    // web worker\n    self[\"V86\"] = V86;\n}\n"
  },
  {
    "path": "src/browser/wisp_network.js",
    "content": "import { LOG_NET } from \"../const.js\";\nimport { dbg_log } from \"../log.js\";\n\nimport {\n    create_eth_encoder_buf,\n    handle_fake_networking,\n    TCPConnection,\n    TCP_STATE_SYN_RECEIVED,\n} from \"./fake_network.js\";\n\n// For Types Only\nimport { BusConnector } from \"../bus.js\";\n\n/**\n * @constructor\n *\n * @param {String} wisp_url\n * @param {BusConnector} bus\n * @param {*=} config\n */\nexport function WispNetworkAdapter(wisp_url, bus, config)\n{\n    this.register_ws(wisp_url);\n    this.last_stream = 1;\n    this.connections = {0: {congestion: 0}};\n    this.congested_buffer = [];\n\n    config = config || {};\n    this.bus = bus;\n    this.id = config.id || 0;\n    this.router_mac = new Uint8Array((config.router_mac || \"52:54:0:1:2:3\").split(\":\").map(function(x) { return parseInt(x, 16); }));\n    this.router_ip = new Uint8Array((config.router_ip || \"192.168.86.1\").split(\".\").map(function(x) { return parseInt(x, 10); }));\n    this.vm_ip = new Uint8Array((config.vm_ip || \"192.168.86.100\").split(\".\").map(function(x) { return parseInt(x, 10); }));\n    this.masquerade = config.masquerade === undefined || !!config.masquerade;\n    this.vm_mac = new Uint8Array(6);\n    this.dns_method = config.dns_method || \"doh\";\n    this.doh_server = config.doh_server;\n    this.tcp_conn = {};\n    this.mtu = config.mtu;\n    this.eth_encoder_buf = create_eth_encoder_buf(this.mtu);\n\n    this.bus.register(\"net\" + this.id + \"-mac\", function(mac) {\n        this.vm_mac = new Uint8Array(mac.split(\":\").map(function(x) { return parseInt(x, 16); }));\n    }, this);\n    this.bus.register(\"net\" + this.id + \"-send\", function(data) {\n        this.send(data);\n    }, this);\n}\n\nWispNetworkAdapter.prototype.register_ws = function(wisp_url) {\n    this.wispws = new WebSocket(wisp_url.replace(\"wisp://\", \"ws://\").replace(\"wisps://\", \"wss://\"));\n    this.wispws.binaryType = \"arraybuffer\";\n    this.wispws.onmessage = (event) => {\n        this.process_incoming_wisp_frame(new Uint8Array(event.data));\n    };\n    this.wispws.onclose = () => {\n        setTimeout(() => {\n            this.register_ws(wisp_url);\n        }, 10000); // wait 10s before reconnecting\n    };\n};\n\nWispNetworkAdapter.prototype.send_packet = function(data, type, stream_id) {\n    if(this.connections[stream_id]) {\n        if(this.connections[stream_id].congestion > 0) {\n            if(type === \"DATA\") {\n                this.connections[stream_id].congestion--;\n            }\n            this.wispws.send(data);\n        } else {\n            this.connections[stream_id].congested = true;\n            this.congested_buffer.push({data: data, type: type});\n        }\n    }\n};\n\nWispNetworkAdapter.prototype.process_incoming_wisp_frame = function(frame) {\n    const view = new DataView(frame.buffer);\n    const stream_id = view.getUint32(1, true);\n    switch(frame[0]) {\n        case 1: // CONNECT\n            // The server should never send this actually\n            dbg_log(\"Server sent client-only packet CONNECT\", LOG_NET);\n            break;\n        case 2: // DATA\n            if(this.connections[stream_id])\n                this.connections[stream_id].data_callback(frame.slice(5));\n            else\n                throw new Error(\"Got a DATA packet but stream not registered. ID: \" + stream_id);\n            break;\n        case 3: // CONTINUE\n            if(this.connections[stream_id]) {\n                this.connections[stream_id].congestion = view.getUint32(5, true);\n            }\n\n            if(this.connections[stream_id].congested) {\n                const buffer = this.congested_buffer.slice(0);\n                this.congested_buffer.length = 0;\n                this.connections[stream_id].congested = false;\n                for(const packet of buffer) {\n                    this.send_packet(packet.data, packet.type, stream_id);\n                }\n            }\n            break;\n        case 4: // CLOSE\n            if(this.connections[stream_id])\n                this.connections[stream_id].close_callback(view.getUint8(5));\n            delete this.connections[stream_id];\n            break;\n        case 5: // PROTOEXT\n            dbg_log(\"got a wisp V2 upgrade request, ignoring\", LOG_NET);\n            // Not responding, this is wisp v1 client not wisp v2;\n            break;\n        default:\n            dbg_log(\"Wisp server returned unknown packet: \" + frame[0], LOG_NET);\n    }\n};\n\n\n// FrameObj will be the following\n// FrameObj.stream_id (number)\n//\n// FrameObj.type -- CONNECT\n//      FrameObj.hostname (string)\n//      FrameObj.port (number)\n//      FrameObj.data_callback (function (Uint8Array))\n//      FrameObj.close_callback (function (number)) OPTIONAL\n//\n//\n// FrameObj.type -- DATA\n//      FrameObj.data (Uint8Array)\n//\n// FrameObj.type -- CLOSE\n//      FrameObj.reason (number)\n//\n//\n\nWispNetworkAdapter.prototype.send_wisp_frame = function(frame_obj) {\n    let full_packet;\n    let view;\n    switch(frame_obj.type) {\n        case \"CONNECT\":\n            const hostname_buffer = new TextEncoder().encode(frame_obj.hostname);\n            full_packet = new Uint8Array(5 + 1 + 2 + hostname_buffer.length);\n            view = new DataView(full_packet.buffer);\n            view.setUint8(0, 0x01);                       // TYPE\n            view.setUint32(1, frame_obj.stream_id, true); // Stream ID\n            view.setUint8(5, 0x01);                       // TCP\n            view.setUint16(6, frame_obj.port, true);      // PORT\n            full_packet.set(hostname_buffer, 8);          // hostname\n\n            // Setting callbacks\n            this.connections[frame_obj.stream_id] = {\n                data_callback: frame_obj.data_callback,\n                close_callback: frame_obj.close_callback,\n                congestion: this.connections[0].congestion\n            };\n            break;\n        case \"DATA\":\n            full_packet = new Uint8Array(5 + frame_obj.data.length);\n            view = new DataView(full_packet.buffer);\n            view.setUint8(0, 0x02);                       // TYPE\n            view.setUint32(1, frame_obj.stream_id, true); // Stream ID\n            full_packet.set(frame_obj.data, 5);           // Actual data\n            break;\n        case \"CLOSE\":\n            full_packet = new Uint8Array(5 + 1);\n            view = new DataView(full_packet.buffer);\n            view.setUint8(0, 0x04);                       // TYPE\n            view.setUint32(1, frame_obj.stream_id, true); // Stream ID\n            view.setUint8(5, frame_obj.reason);           // Packet size\n            break;\n        default:\n            dbg_log(\"Client tried to send unknown packet: \" + frame_obj.type, LOG_NET);\n\n    }\n    this.send_packet(full_packet, frame_obj.type, frame_obj.stream_id);\n};\n\nWispNetworkAdapter.prototype.destroy = function()\n{\n    if(this.wispws) {\n        this.wispws.onmessage = null;\n        this.wispws.onclose = null;\n        this.wispws.close();\n        this.wispws = null;\n    }\n};\n\n/**\n * @param {TCPConnection} conn\n * @param {Uint8Array} packet\n */\nWispNetworkAdapter.prototype.on_tcp_connection = function(conn, packet)\n{\n    conn.stream_id = this.last_stream++;\n\n    conn.on(\"data\", data => {\n        if(data.length !== 0) {\n            this.send_wisp_frame({\n                type: \"DATA\",\n                stream_id: conn.stream_id,\n                data: data\n            });\n        }\n    });\n\n    conn.on_close = () => {\n        this.send_wisp_frame({\n            type: \"CLOSE\",\n            stream_id: conn.stream_id,\n            reason: 0x02    // 0x02: Voluntary stream closure\n        });\n    };\n\n    // WISP doesn't implement shutdown, use close as workaround\n    conn.on_shutdown = conn.on_close;\n\n    this.send_wisp_frame({\n        type: \"CONNECT\",\n        stream_id: conn.stream_id,\n        hostname: packet.ipv4.dest.join(\".\"),\n        port: conn.sport,\n        data_callback: (data) => {\n            conn.write(data);\n        },\n        close_callback: (data) => {\n            conn.close();\n        }\n    });\n\n    conn.accept();\n    return true;\n};\n\n/**\n * @param {Uint8Array} data\n */\nWispNetworkAdapter.prototype.send = function(data)\n{\n    // TODO: forward UDP traffic to WISP server once this WISP client supports UDP\n    handle_fake_networking(data, this);\n};\n\n/**\n * @param {Uint8Array} data\n */\nWispNetworkAdapter.prototype.receive = function(data)\n{\n    this.bus.send(\"net\" + this.id + \"-receive\", new Uint8Array(data));\n};\n"
  },
  {
    "path": "src/browser/worker_bus.js",
    "content": "import { dbg_assert } from \"../log.js\";\n\n/** @constructor */\nexport var Connector = function(pair)\n{\n    this.listeners = {};\n    this.pair = pair;\n\n    pair.addEventListener(\"message\", function(e)\n    {\n        var data = e.data;\n        var listeners = this.listeners[data[0]];\n\n        for(var i = 0; i < listeners.length; i++)\n        {\n            var listener = listeners[i];\n            listener.fn.call(listener.this_value, data[1]);\n        }\n    }.bind(this), false);\n\n};\n\nConnector.prototype.register = function(name, fn, this_value)\n{\n    var listeners = this.listeners[name];\n\n    if(listeners === undefined)\n    {\n        listeners = this.listeners[name] = [];\n    }\n\n    listeners.push({\n        fn: fn,\n        this_value: this_value,\n    });\n};\n\n/**\n * Send (\"emit\") a message\n *\n * @param {string} name\n * @param {*=} value\n * @param {*=} transfer_list\n */\nConnector.prototype.send = function(name, value, transfer_list)\n{\n    dbg_assert(arguments.length >= 1);\n\n    if(!this.pair)\n    {\n        return;\n    }\n\n    this.pair.postMessage([name, value], transfer_list);\n};\n\n\nexport var init = function(worker)\n{\n    return new Connector(worker);\n};\n"
  },
  {
    "path": "src/buffer.js",
    "content": "import { CPU } from \"./cpu.js\";\nimport { load_file, get_file_size } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// The smallest size the emulated hardware can emit\nconst BLOCK_SIZE = 256;\n\nconst ASYNC_SAFE = false;\n\n/**\n * Synchronous access to ArrayBuffer\n * @constructor\n */\nexport function SyncBuffer(buffer)\n{\n    dbg_assert(buffer instanceof ArrayBuffer);\n\n    this.buffer = buffer;\n    this.byteLength = buffer.byteLength;\n    this.onload = undefined;\n    this.onprogress = undefined;\n}\n\nSyncBuffer.prototype.load = function()\n{\n    this.onload && this.onload({ buffer: this.buffer });\n};\n\n/**\n * @this {SyncBuffer|SyncFileBuffer}\n * @param {number} start\n * @param {number} len\n * @param {function(!Uint8Array)} fn\n */\nSyncBuffer.prototype.get = function(start, len, fn)\n{\n    dbg_assert(start + len <= this.byteLength);\n    fn(new Uint8Array(this.buffer, start, len));\n};\n\n/**\n * @this {SyncBuffer|SyncFileBuffer}\n * @param {number} start\n * @param {!Uint8Array} slice\n * @param {function()} fn\n */\nSyncBuffer.prototype.set = function(start, slice, fn)\n{\n    dbg_assert(start + slice.byteLength <= this.byteLength);\n\n    new Uint8Array(this.buffer, start, slice.byteLength).set(slice);\n    fn();\n};\n\n/**\n * @this {SyncBuffer|SyncFileBuffer}\n * @param {function(!ArrayBuffer)} fn\n */\nSyncBuffer.prototype.get_buffer = function(fn)\n{\n    fn(this.buffer);\n};\n\n/**\n * @this {SyncBuffer|SyncFileBuffer}\n */\nSyncBuffer.prototype.get_state = function()\n{\n    const state = [];\n    state[0] = this.byteLength;\n    state[1] = new Uint8Array(this.buffer);\n    return state;\n};\n\n/**\n * @this {SyncBuffer|SyncFileBuffer}\n */\nSyncBuffer.prototype.set_state = function(state)\n{\n    this.byteLength = state[0];\n    this.buffer = state[1].slice().buffer;\n};\n\n/**\n * Asynchronous access to ArrayBuffer, loading blocks lazily as needed,\n * using the `Range: bytes=...` header\n *\n * @constructor\n * @param {string} filename Name of the file to download\n * @param {number|undefined} size\n * @param {number|undefined} fixed_chunk_size\n */\nfunction AsyncXHRBuffer(filename, size, fixed_chunk_size)\n{\n    this.filename = filename;\n\n    this.byteLength = size;\n\n    this.block_cache = new Map();\n    this.block_cache_is_write = new Set();\n\n    this.fixed_chunk_size = fixed_chunk_size;\n    this.cache_reads = !!fixed_chunk_size; // TODO: could also be useful in other cases (needs testing)\n\n    this.onload = undefined;\n    this.onprogress = undefined;\n}\n\nAsyncXHRBuffer.prototype.load = async function()\n{\n    if(this.byteLength !== undefined)\n    {\n        this.onload && this.onload(Object.create(null));\n        return;\n    }\n\n    const size = await get_file_size(this.filename);\n    this.byteLength = size;\n    this.onload && this.onload(Object.create(null));\n};\n\n/**\n * @param {number} offset\n * @param {number} len\n * @this {AsyncXHRBuffer|AsyncXHRPartfileBuffer|AsyncFileBuffer}\n */\nAsyncXHRBuffer.prototype.get_from_cache = function(offset, len)\n{\n    var number_of_blocks = len / BLOCK_SIZE;\n    var block_index = offset / BLOCK_SIZE;\n\n    for(var i = 0; i < number_of_blocks; i++)\n    {\n        var block = this.block_cache.get(block_index + i);\n\n        if(!block)\n        {\n            return;\n        }\n    }\n\n    if(number_of_blocks === 1)\n    {\n        return this.block_cache.get(block_index);\n    }\n    else\n    {\n        var result = new Uint8Array(len);\n        for(var i = 0; i < number_of_blocks; i++)\n        {\n            result.set(this.block_cache.get(block_index + i), i * BLOCK_SIZE);\n        }\n        return result;\n    }\n};\n\n/**\n * @param {number} offset\n * @param {number} len\n * @param {function(!Uint8Array)} fn\n */\nAsyncXHRBuffer.prototype.get = function(offset, len, fn)\n{\n    dbg_assert(offset + len <= this.byteLength);\n    dbg_assert(offset % BLOCK_SIZE === 0);\n    dbg_assert(len % BLOCK_SIZE === 0);\n    dbg_assert(len);\n\n    var block = this.get_from_cache(offset, len);\n    if(block)\n    {\n        if(ASYNC_SAFE)\n        {\n            setTimeout(fn.bind(this, block), 0);\n        }\n        else\n        {\n            fn(block);\n        }\n        return;\n    }\n\n    var requested_start = offset;\n    var requested_length = len;\n    if(this.fixed_chunk_size)\n    {\n        requested_start = offset - (offset % this.fixed_chunk_size);\n        requested_length = Math.ceil((offset - requested_start + len) / this.fixed_chunk_size) * this.fixed_chunk_size;\n    }\n\n    load_file(this.filename, {\n        done: function done(buffer)\n        {\n            var block = new Uint8Array(buffer);\n            this.handle_read(requested_start, requested_length, block);\n            if(requested_start === offset && requested_length === len)\n            {\n                fn(block);\n            }\n            else\n            {\n                fn(block.subarray(offset - requested_start, offset - requested_start + len));\n            }\n        }.bind(this),\n        range: { start: requested_start, length: requested_length },\n    });\n};\n\n/**\n * Relies on this.byteLength and this.block_cache\n *\n * @this {AsyncXHRBuffer|AsyncXHRPartfileBuffer|AsyncFileBuffer}\n *\n * @param {number} start\n * @param {!Uint8Array} data\n * @param {function()} fn\n */\nAsyncXHRBuffer.prototype.set = function(start, data, fn)\n{\n    var len = data.length;\n    dbg_assert(start + data.byteLength <= this.byteLength);\n    dbg_assert(start % BLOCK_SIZE === 0);\n    dbg_assert(len % BLOCK_SIZE === 0);\n    dbg_assert(len);\n\n    var start_block = start / BLOCK_SIZE;\n    var block_count = len / BLOCK_SIZE;\n\n    for(var i = 0; i < block_count; i++)\n    {\n        var block = this.block_cache.get(start_block + i);\n\n        if(block === undefined)\n        {\n            const data_slice = data.slice(i * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);\n            this.block_cache.set(start_block + i, data_slice);\n        }\n        else\n        {\n            const data_slice = data.subarray(i * BLOCK_SIZE, (i + 1) * BLOCK_SIZE);\n            dbg_assert(block.byteLength === data_slice.length);\n            block.set(data_slice);\n        }\n\n        this.block_cache_is_write.add(start_block + i);\n    }\n\n    fn();\n};\n\n/**\n * @this {AsyncXHRBuffer|AsyncXHRPartfileBuffer|AsyncFileBuffer}\n * @param {number} offset\n * @param {number} len\n * @param {!Uint8Array} block\n */\nAsyncXHRBuffer.prototype.handle_read = function(offset, len, block)\n{\n    // Used by AsyncXHRBuffer, AsyncXHRPartfileBuffer and AsyncFileBuffer\n    // Overwrites blocks from the original source that have been written since\n\n    var start_block = offset / BLOCK_SIZE;\n    var block_count = len / BLOCK_SIZE;\n\n    for(var i = 0; i < block_count; i++)\n    {\n        const cached_block = this.block_cache.get(start_block + i);\n\n        if(cached_block)\n        {\n            block.set(cached_block, i * BLOCK_SIZE);\n        }\n        else if(this.cache_reads)\n        {\n            this.block_cache.set(start_block + i, block.slice(i * BLOCK_SIZE, (i + 1) * BLOCK_SIZE));\n        }\n    }\n};\n\nAsyncXHRBuffer.prototype.get_buffer = function(fn)\n{\n    // We must download all parts, unlikely a good idea for big files\n    fn();\n};\n\n///**\n// * @this {AsyncXHRBuffer|AsyncXHRPartfileBuffer|AsyncFileBuffer}\n// */\n//AsyncXHRBuffer.prototype.get_block_cache = function()\n//{\n//    var count = Object.keys(this.block_cache).length;\n\n//    var buffer = new Uint8Array(count * BLOCK_SIZE);\n//    var indices = [];\n\n//    var i = 0;\n//    for(var index of Object.keys(this.block_cache))\n//    {\n//        var block = this.block_cache.get(index);\n//        dbg_assert(block.length === BLOCK_SIZE);\n//        index = +index;\n//        indices.push(index);\n//        buffer.set(\n//            block,\n//            i * BLOCK_SIZE\n//        );\n//        i++;\n//    }\n\n//    return {\n//        buffer,\n//        indices,\n//        block_size: BLOCK_SIZE,\n//    };\n//};\n\n/**\n * @this {AsyncXHRBuffer|AsyncXHRPartfileBuffer|AsyncFileBuffer}\n */\nAsyncXHRBuffer.prototype.get_state = function()\n{\n    const state = [];\n    const block_cache = [];\n\n    for(const [index, block] of this.block_cache)\n    {\n        dbg_assert(isFinite(index));\n        if(this.block_cache_is_write.has(index))\n        {\n            block_cache.push([index, block]);\n        }\n    }\n\n    state[0] = block_cache;\n    return state;\n};\n\n/**\n * @this {AsyncXHRBuffer|AsyncXHRPartfileBuffer|AsyncFileBuffer}\n */\nAsyncXHRBuffer.prototype.set_state = function(state)\n{\n    const block_cache = state[0];\n    this.block_cache.clear();\n    this.block_cache_is_write.clear();\n\n    for(const [index, block] of block_cache)\n    {\n        dbg_assert(isFinite(index));\n        this.block_cache.set(index, block);\n        this.block_cache_is_write.add(index);\n    }\n};\n\n/**\n * Asynchronous access to ArrayBuffer, loading blocks lazily as needed,\n * downloading files named filename-%d-%d.ext (where the %d are start and end offset).\n * Or, if partfile_alt_format is set, filename-%08d.ext (where %d is the part number, compatible with gnu split).\n *\n * @constructor\n * @param {string} filename Name of the file to download\n * @param {number|undefined} size\n * @param {number|undefined} fixed_chunk_size\n * @param {boolean|undefined} partfile_alt_format\n */\nexport function AsyncXHRPartfileBuffer(filename, size, fixed_chunk_size, partfile_alt_format, zstd_decompress)\n{\n    const parts = filename.match(/\\.[^\\.]+(\\.zst)?$/);\n\n    this.extension = parts ? parts[0] : \"\";\n    this.basename = filename.substring(0, filename.length - this.extension.length);\n\n    this.is_zstd = this.extension.endsWith(\".zst\");\n\n    if(!this.basename.endsWith(\"/\"))\n    {\n        this.basename += \"-\";\n    }\n\n    this.block_cache = new Map();\n    this.block_cache_is_write = new Set();\n\n    this.byteLength = size;\n    this.fixed_chunk_size = fixed_chunk_size;\n    this.partfile_alt_format = !!partfile_alt_format;\n    this.zstd_decompress = zstd_decompress;\n\n    this.cache_reads = !!fixed_chunk_size; // TODO: could also be useful in other cases (needs testing)\n\n    this.onload = undefined;\n    this.onprogress = undefined;\n}\n\nAsyncXHRPartfileBuffer.prototype.load = function()\n{\n    if(this.byteLength !== undefined)\n    {\n        this.onload && this.onload(Object.create(null));\n        return;\n    }\n    dbg_assert(false);\n    this.onload && this.onload(Object.create(null));\n};\n\n/**\n * @param {number} offset\n * @param {number} len\n * @param {function(!Uint8Array)} fn\n */\nAsyncXHRPartfileBuffer.prototype.get = function(offset, len, fn)\n{\n    dbg_assert(offset + len <= this.byteLength);\n    dbg_assert(offset % BLOCK_SIZE === 0);\n    dbg_assert(len % BLOCK_SIZE === 0);\n    dbg_assert(len);\n\n    const block = this.get_from_cache(offset, len);\n\n    if(block)\n    {\n        if(ASYNC_SAFE)\n        {\n            setTimeout(fn.bind(this, block), 0);\n        }\n        else\n        {\n            fn(block);\n        }\n        return;\n    }\n\n    if(this.fixed_chunk_size)\n    {\n        const start_index = Math.floor(offset / this.fixed_chunk_size);\n        const m_offset = offset - start_index * this.fixed_chunk_size;\n        dbg_assert(m_offset >= 0);\n        const total_count = Math.ceil((m_offset + len) / this.fixed_chunk_size);\n        const blocks = new Uint8Array(total_count * this.fixed_chunk_size);\n        let finished = 0;\n\n        for(let i = 0; i < total_count; i++)\n        {\n            const offset = (start_index + i) * this.fixed_chunk_size;\n\n            const part_filename =\n                this.partfile_alt_format ?\n                    // matches output of gnu split:\n                    //   split -b 512 -a8 -d --additional-suffix .img w95.img w95-\n                    this.basename + (start_index + i + \"\").padStart(8, \"0\") + this.extension\n                :\n                    this.basename + offset + \"-\" + (offset + this.fixed_chunk_size) + this.extension;\n\n            // XXX: unnecessary allocation\n            const block = this.get_from_cache(offset, this.fixed_chunk_size);\n\n            if(block)\n            {\n                blocks.set(block, i * this.fixed_chunk_size);\n                finished++;\n                if(finished === total_count)\n                {\n                    fn(blocks.subarray(m_offset, m_offset + len));\n                }\n            }\n            else\n            {\n                load_file(part_filename, {\n                    done: async function done(buffer)\n                    {\n                        let block = new Uint8Array(buffer);\n\n                        if(this.is_zstd)\n                        {\n                            const decompressed = await this.zstd_decompress(this.fixed_chunk_size, block);\n                            block = new Uint8Array(decompressed);\n                        }\n\n                        blocks.set(block, i * this.fixed_chunk_size);\n                        this.handle_read((start_index + i) * this.fixed_chunk_size, this.fixed_chunk_size|0, block);\n\n                        finished++;\n                        if(finished === total_count)\n                        {\n                            fn(blocks.subarray(m_offset, m_offset + len));\n                        }\n                    }.bind(this),\n                });\n            }\n        }\n    }\n    else\n    {\n        const part_filename = this.basename + offset + \"-\" + (offset + len) + this.extension;\n\n        load_file(part_filename, {\n            done: function done(buffer)\n            {\n                dbg_assert(buffer.byteLength === len);\n                var block = new Uint8Array(buffer);\n                this.handle_read(offset, len, block);\n                fn(block);\n            }.bind(this),\n        });\n    }\n};\n\nAsyncXHRPartfileBuffer.prototype.get_from_cache = AsyncXHRBuffer.prototype.get_from_cache;\nAsyncXHRPartfileBuffer.prototype.set = AsyncXHRBuffer.prototype.set;\nAsyncXHRPartfileBuffer.prototype.handle_read = AsyncXHRBuffer.prototype.handle_read;\n//AsyncXHRPartfileBuffer.prototype.get_block_cache = AsyncXHRBuffer.prototype.get_block_cache;\nAsyncXHRPartfileBuffer.prototype.get_state = AsyncXHRBuffer.prototype.get_state;\nAsyncXHRPartfileBuffer.prototype.set_state = AsyncXHRBuffer.prototype.set_state;\n\n/**\n * Synchronous access to File, loading blocks from the input type=file\n * The whole file is loaded into memory during initialisation\n *\n * @constructor\n */\nexport function SyncFileBuffer(file)\n{\n    this.file = file;\n    this.byteLength = file.size;\n\n    if(file.size > (1 << 30))\n    {\n        console.warn(\"SyncFileBuffer: Allocating buffer of \" + (file.size >> 20) + \" MB ...\");\n    }\n\n    this.buffer = new ArrayBuffer(file.size);\n\n    this.onload = undefined;\n    this.onprogress = undefined;\n}\n\nSyncFileBuffer.prototype.load = function()\n{\n    this.load_next(0);\n};\n\n/**\n * @param {number} start\n */\nSyncFileBuffer.prototype.load_next = function(start)\n{\n    const PART_SIZE = 4 << 20;\n\n    var filereader = new FileReader();\n\n    filereader.onload = function(e)\n    {\n        var buffer = new Uint8Array(e.target.result);\n        new Uint8Array(this.buffer, start).set(buffer);\n        this.load_next(start + PART_SIZE);\n    }.bind(this);\n\n    if(this.onprogress)\n    {\n        this.onprogress({\n            loaded: start,\n            total: this.byteLength,\n            lengthComputable: true,\n        });\n    }\n\n    if(start < this.byteLength)\n    {\n        var end = Math.min(start + PART_SIZE, this.byteLength);\n        var slice = this.file.slice(start, end);\n        filereader.readAsArrayBuffer(slice);\n    }\n    else\n    {\n        this.file = undefined;\n        this.onload && this.onload({ buffer: this.buffer });\n    }\n};\n\nSyncFileBuffer.prototype.get = SyncBuffer.prototype.get;\nSyncFileBuffer.prototype.set = SyncBuffer.prototype.set;\nSyncFileBuffer.prototype.get_buffer = SyncBuffer.prototype.get_buffer;\nSyncFileBuffer.prototype.get_state = SyncBuffer.prototype.get_state;\nSyncFileBuffer.prototype.set_state = SyncBuffer.prototype.set_state;\n\n/**\n * Asynchronous access to File, loading blocks from the input type=file\n *\n * @constructor\n */\nexport function AsyncFileBuffer(file)\n{\n    this.file = file;\n    this.byteLength = file.size;\n\n    this.block_cache = new Map();\n    this.block_cache_is_write = new Set();\n\n    this.onload = undefined;\n    this.onprogress = undefined;\n}\n\nAsyncFileBuffer.prototype.load = function()\n{\n    this.onload && this.onload(Object.create(null));\n};\n\n/**\n * @param {number} offset\n * @param {number} len\n * @param {function(!Uint8Array)} fn\n */\nAsyncFileBuffer.prototype.get = function(offset, len, fn)\n{\n    dbg_assert(offset % BLOCK_SIZE === 0);\n    dbg_assert(len % BLOCK_SIZE === 0);\n    dbg_assert(len);\n\n    var block = this.get_from_cache(offset, len);\n    if(block)\n    {\n        fn(block);\n        return;\n    }\n\n    var fr = new FileReader();\n\n    fr.onload = function(e)\n    {\n        var buffer = e.target.result;\n        var block = new Uint8Array(buffer);\n\n        this.handle_read(offset, len, block);\n        fn(block);\n    }.bind(this);\n\n    fr.readAsArrayBuffer(this.file.slice(offset, offset + len));\n};\nAsyncFileBuffer.prototype.get_from_cache = AsyncXHRBuffer.prototype.get_from_cache;\nAsyncFileBuffer.prototype.set = AsyncXHRBuffer.prototype.set;\nAsyncFileBuffer.prototype.handle_read = AsyncXHRBuffer.prototype.handle_read;\nAsyncFileBuffer.prototype.get_state = AsyncXHRBuffer.prototype.get_state;\nAsyncFileBuffer.prototype.set_state = AsyncXHRBuffer.prototype.set_state;\n\nAsyncFileBuffer.prototype.get_buffer = function(fn)\n{\n    // We must load all parts, unlikely a good idea for big files\n    fn();\n};\n\nAsyncFileBuffer.prototype.get_as_file = function(name)\n{\n    var parts = [];\n    var existing_blocks = Array.from(this.block_cache.keys()).sort(function(x, y) { return x - y; });\n\n    var current_offset = 0;\n\n    for(var i = 0; i < existing_blocks.length; i++)\n    {\n        var block_index = existing_blocks[i];\n        var block = this.block_cache.get(block_index);\n        var start = block_index * BLOCK_SIZE;\n        dbg_assert(start >= current_offset);\n\n        if(start !== current_offset)\n        {\n            parts.push(this.file.slice(current_offset, start));\n            current_offset = start;\n        }\n\n        parts.push(block);\n        current_offset += block.length;\n    }\n\n    if(current_offset !== this.file.size)\n    {\n        parts.push(this.file.slice(current_offset));\n    }\n\n    var file = new File(parts, name);\n    dbg_assert(file.size === this.file.size);\n\n    return file;\n};\n\nexport function buffer_from_object(obj, zstd_decompress_worker)\n{\n    // TODO: accept Uint8Array, ArrayBuffer, File, url rather than { url }\n\n    if(obj.buffer instanceof ArrayBuffer)\n    {\n        return new SyncBuffer(obj.buffer);\n    }\n    else if(typeof File !== \"undefined\" && obj.buffer instanceof File)\n    {\n        // SyncFileBuffer:\n        // - loads the whole disk image into memory, impossible for large files (more than 1GB)\n        // - can later serve get/set operations fast and synchronously\n        // - takes some time for first load, neglectable for small files (up to 100Mb)\n        //\n        // AsyncFileBuffer:\n        // - loads slices of the file asynchronously as requested\n        // - slower get/set\n\n        // Heuristics: If file is larger than or equal to 256M, use AsyncFileBuffer\n        let is_async = obj.async;\n        if(is_async === undefined)\n        {\n            is_async = obj.buffer.size >= 256 * 1024 * 1024;\n        }\n\n        if(is_async)\n        {\n            return new AsyncFileBuffer(obj.buffer);\n        }\n        else\n        {\n            return new SyncFileBuffer(obj.buffer);\n        }\n    }\n    else if(obj.url)\n    {\n        // Note: Only async for now\n\n        if(obj.use_parts)\n        {\n            return new AsyncXHRPartfileBuffer(obj.url, obj.size, obj.fixed_chunk_size, false, zstd_decompress_worker);\n        }\n        else\n        {\n            return new AsyncXHRBuffer(obj.url, obj.size, obj.fixed_chunk_size);\n        }\n    }\n    else\n    {\n        dbg_log(\"Ignored file: url=\" + obj.url + \" buffer=\" + obj.buffer);\n    }\n}\n"
  },
  {
    "path": "src/bus.js",
    "content": "import { dbg_assert } from \"./log.js\";\n\nexport var Bus = {};\n\n/** @constructor */\nexport function BusConnector()\n{\n    this.listeners = {};\n    this.pair = undefined;\n}\n\n/**\n * @param {string} name\n * @param {function(?)} fn\n * @param {Object} this_value\n */\nBusConnector.prototype.register = function(name, fn, this_value)\n{\n    var listeners = this.listeners[name];\n\n    if(listeners === undefined)\n    {\n        listeners = this.listeners[name] = [];\n    }\n\n    listeners.push({\n        fn: fn,\n        this_value: this_value,\n    });\n};\n\n/**\n * Unregister one message with the given name and callback\n *\n * @param {string} name\n * @param {function(?)} fn\n */\nBusConnector.prototype.unregister = function(name, fn)\n{\n    var listeners = this.listeners[name];\n\n    if(listeners === undefined)\n    {\n        return;\n    }\n\n    this.listeners[name] = listeners.filter(function(l)\n    {\n        return l.fn !== fn;\n    });\n};\n\n/**\n * Send (\"emit\") a message\n *\n * @param {string} name\n * @param {*=} value\n * @param {*=} unused_transfer\n */\nBusConnector.prototype.send = function(name, value, unused_transfer)\n{\n    if(!this.pair)\n    {\n        return;\n    }\n\n    var listeners = this.pair.listeners[name];\n\n    if(listeners === undefined)\n    {\n        return;\n    }\n\n    for(var i = 0; i < listeners.length; i++)\n    {\n        var listener = listeners[i];\n        listener.fn.call(listener.this_value, value);\n    }\n};\n\n/**\n * Send a message, guaranteeing that it is received asynchronously\n *\n * @param {string} name\n * @param {Object=} value\n */\nBusConnector.prototype.send_async = function(name, value)\n{\n    dbg_assert(arguments.length === 1 || arguments.length === 2);\n\n    setTimeout(this.send.bind(this, name, value), 0);\n};\n\nBus.create = function()\n{\n    var c0 = new BusConnector();\n    var c1 = new BusConnector();\n\n    c0.pair = c1;\n    c1.pair = c0;\n\n    return [c0, c1];\n};\n"
  },
  {
    "path": "src/cjs.js",
    "content": "/**\n * @define {boolean}\n * Overridden for production by closure compiler\n */\nvar DEBUG = true;\n"
  },
  {
    "path": "src/const.js",
    "content": "export const\n    LOG_ALL = -1,\n    LOG_NONE = 0,\n\n    LOG_OTHER =  0x0000001,\n    LOG_CPU =    0x0000002,\n    LOG_FPU =    0x0000004,\n    LOG_MEM =    0x0000008,\n    LOG_DMA =    0x0000010,\n    LOG_IO =     0x0000020,\n    LOG_PS2 =    0x0000040,\n    LOG_PIC =    0x0000080,\n    LOG_VGA =    0x0000100,\n    LOG_PIT =    0x0000200,\n    LOG_MOUSE =  0x0000400,\n    LOG_PCI =    0x0000800,\n    LOG_BIOS =   0x0001000,\n    LOG_FLOPPY = 0x0002000,\n    LOG_SERIAL = 0x0004000,\n    LOG_DISK =   0x0008000,\n    LOG_RTC =    0x0010000,\n    // unused    0x0020000,\n    LOG_ACPI =   0x0040000,\n    LOG_APIC =   0x0080000,\n    LOG_NET =    0x0100000,\n    LOG_VIRTIO = 0x0200000,\n    LOG_9P =     0x0400000,\n    LOG_SB16 =   0x0800000,\n    LOG_FETCH =  0x1000000;\n\n\n/**\n * @type {Array<Array<string|number>>}\n */\nexport const LOG_NAMES = [\n    [1, \"\"],\n    [LOG_CPU, \"CPU\"],\n    [LOG_DISK, \"DISK\"],\n    [LOG_FPU, \"FPU\"],\n    [LOG_MEM, \"MEM\"],\n    [LOG_DMA, \"DMA\"],\n    [LOG_IO, \"IO\"],\n    [LOG_PS2, \"PS2\"],\n    [LOG_PIC, \"PIC\"],\n    [LOG_VGA, \"VGA\"],\n    [LOG_PIT, \"PIT\"],\n    [LOG_MOUSE, \"MOUS\"],\n    [LOG_PCI, \"PCI\"],\n    [LOG_BIOS, \"BIOS\"],\n    [LOG_FLOPPY, \"FLOP\"],\n    [LOG_SERIAL, \"SERI\"],\n    [LOG_RTC, \"RTC\"],\n    [LOG_ACPI, \"ACPI\"],\n    [LOG_APIC, \"APIC\"],\n    [LOG_NET, \"NET\"],\n    [LOG_VIRTIO, \"VIO\"],\n    [LOG_9P, \"9P\"],\n    [LOG_SB16, \"SB16\"],\n    [LOG_FETCH, \"FETC\"],\n];\n\nexport const\n    // flags register bitflags\n    FLAG_CARRY = 1,\n    FLAG_PARITY = 4,\n    FLAG_ADJUST = 16,\n    FLAG_ZERO = 64,\n    FLAG_SIGN = 128,\n    FLAG_TRAP = 256,\n    FLAG_INTERRUPT = 512,\n    FLAG_DIRECTION = 1024,\n    FLAG_OVERFLOW = 2048,\n    FLAG_IOPL = 1 << 12 | 1 << 13,\n    FLAG_NT = 1 << 14,\n    FLAG_RF = 1 << 16,\n    FLAG_VM = 1 << 17,\n    FLAG_AC = 1 << 18,\n    FLAG_VIF = 1 << 19,\n    FLAG_VIP = 1 << 20,\n    FLAG_ID = 1 << 21,\n\n    // default values of reserved flags bits\n    FLAGS_DEFAULT = 1 << 1,\n\n    REG_EAX = 0,\n    REG_ECX = 1,\n    REG_EDX = 2,\n    REG_EBX = 3,\n    REG_ESP = 4,\n    REG_EBP = 5,\n    REG_ESI = 6,\n    REG_EDI = 7,\n\n    REG_ES = 0,\n    REG_CS = 1,\n    REG_SS = 2,\n    REG_DS = 3,\n    REG_FS = 4,\n    REG_GS = 5,\n\n    REG_LDTR = 7; // local descriptor table register\n\nexport const\n    // The minimum number of bytes that can be memory-mapped by one device.\n    MMAP_BLOCK_BITS = 17,\n    MMAP_BLOCK_SIZE = 1 << MMAP_BLOCK_BITS,\n    MMAP_MAX = 0x100000000;\n\nexport const CR0_PG = 1 << 31;\nexport const CR4_PAE = 1 << 5;\n\n\n// https://github.com/qemu/seabios/blob/14221cd86eadba82255fdc55ed174d401c7a0a04/src/fw/paravirt.c#L205-L219\n\nexport const FW_CFG_SIGNATURE = 0x00;\nexport const FW_CFG_ID = 0x01;\nexport const FW_CFG_RAM_SIZE = 0x03;\nexport const FW_CFG_NB_CPUS = 0x05;\nexport const FW_CFG_MAX_CPUS = 0x0F;\nexport const FW_CFG_NUMA = 0x0D;\nexport const FW_CFG_FILE_DIR = 0x19;\n\nexport const FW_CFG_CUSTOM_START = 0x8000;\n// This value is specific to v86, choosen to hopefully not collide with other indexes\nexport const FW_CFG_FILE_START = 0xC000;\nexport const FW_CFG_SIGNATURE_QEMU = 0x554D4551;\n\n\n// See same constant in jit.rs\nexport const WASM_TABLE_SIZE = 900;\n\nexport const WASM_TABLE_OFFSET = 1024;\n\nexport const MIXER_CHANNEL_LEFT = 0;\nexport const MIXER_CHANNEL_RIGHT = 1;\nexport const MIXER_CHANNEL_BOTH = 2;\nexport const MIXER_SRC_MASTER = 0;\nexport const MIXER_SRC_PCSPEAKER = 1;\nexport const MIXER_SRC_DAC = 2;\n"
  },
  {
    "path": "src/cpu.js",
    "content": "import {\n    LOG_CPU, LOG_BIOS,\n    FW_CFG_SIGNATURE, FW_CFG_SIGNATURE_QEMU,\n    WASM_TABLE_SIZE, WASM_TABLE_OFFSET, FW_CFG_ID,\n    FW_CFG_RAM_SIZE, FW_CFG_NB_CPUS, FW_CFG_MAX_CPUS,\n    FW_CFG_NUMA, FW_CFG_FILE_DIR, FW_CFG_FILE_START,\n    FW_CFG_CUSTOM_START, FLAGS_DEFAULT,\n    MMAP_BLOCK_BITS, MMAP_BLOCK_SIZE, MMAP_MAX,\n    REG_ESP, REG_EBP, REG_ESI, REG_EAX, REG_EBX, REG_ECX, REG_EDX, REG_EDI,\n    REG_CS, REG_DS, REG_ES, REG_FS, REG_GS, REG_SS, CR0_PG, CR4_PAE, REG_LDTR,\n    FLAG_VM, FLAG_INTERRUPT, FLAG_CARRY, FLAG_ADJUST, FLAG_ZERO, FLAG_SIGN, FLAG_TRAP,\n    FLAG_DIRECTION, FLAG_OVERFLOW, FLAG_PARITY,\n} from \"./const.js\";\nimport { h, view, pads, Bitmap, dump_file } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\nimport { SB16 } from \"./sb16.js\";\nimport { ACPI } from \"./acpi.js\";\nimport { PIT } from \"./pit.js\";\nimport { DMA } from \"./dma.js\";\nimport { UART } from \"./uart.js\";\nimport { Ne2k } from \"./ne2k.js\";\nimport { IO } from \"./io.js\";\nimport { VirtioConsole } from \"./virtio_console.js\";\nimport { PCI } from \"./pci.js\";\nimport { PS2 } from \"./ps2.js\";\nimport { read_elf } from \"./elf.js\";\n\nimport { FloppyController } from \"./floppy.js\";\nimport { IDEController } from \"./ide.js\";\nimport { VirtioNet } from \"./virtio_net.js\";\nimport { VGAScreen } from \"./vga.js\";\nimport { VirtioBalloon } from \"./virtio_balloon.js\";\nimport { Virtio9p, Virtio9pHandler, Virtio9pProxy } from \"../lib/9p.js\";\n\nimport { load_kernel } from \"./kernel.js\";\n\nimport {\n    RTC,\n    CMOS_EQUIPMENT_INFO, CMOS_BIOS_SMP_COUNT,\n    CMOS_MEM_HIGHMEM_HIGH, CMOS_MEM_HIGHMEM_MID, CMOS_MEM_HIGHMEM_LOW,\n    CMOS_DISK_DATA, CMOS_BIOS_DISKTRANSFLAG, CMOS_FLOPPY_DRIVE_TYPE,\n    BOOT_ORDER_CD_FIRST, CMOS_BIOS_BOOTFLAG1, CMOS_BIOS_BOOTFLAG2,\n    CMOS_MEM_BASE_LOW, CMOS_MEM_BASE_HIGH,\n    CMOS_MEM_OLD_EXT_LOW, CMOS_MEM_OLD_EXT_HIGH, CMOS_MEM_EXTMEM_LOW,\n    CMOS_MEM_EXTMEM_HIGH, CMOS_MEM_EXTMEM2_LOW, CMOS_MEM_EXTMEM2_HIGH\n} from \"./rtc.js\";\n\n\n// For Types Only\n\nimport { BusConnector } from \"./bus.js\";\n\n// Resources:\n// https://pdos.csail.mit.edu/6.828/2006/readings/i386/toc.htm\n// https://www-ssl.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html\n// http://ref.x86asm.net/geek32.html\n\nconst DUMP_GENERATED_WASM = false;\nconst DUMP_UNCOMPILED_ASSEMBLY = false;\n\n/** @constructor */\nexport function CPU(bus, wm, stop_idling)\n{\n    this.stop_idling = stop_idling;\n    this.wm = wm;\n    this.wasm_patch();\n    this.create_jit_imports();\n\n    const memory = this.wm.exports.memory;\n\n    this.wasm_memory = memory;\n\n    this.memory_size = view(Uint32Array, memory, 812, 1);\n\n    this.mem8 = new Uint8Array(0);\n    this.mem32s = new Int32Array(this.mem8.buffer);\n\n    this.segment_is_null = view(Uint8Array, memory, 724, 8);\n    this.segment_offsets = view(Int32Array, memory, 736, 8);\n    this.segment_limits = view(Uint32Array, memory, 768, 8);\n    this.segment_access_bytes = view(Uint8Array, memory, 512, 8);\n\n    /**\n     * Wheter or not in protected mode\n     */\n    this.protected_mode = view(Int32Array, memory, 800, 1);\n\n    this.idtr_size = view(Int32Array, memory, 564, 1);\n    this.idtr_offset = view(Int32Array, memory, 568, 1);\n\n    /**\n     * global descriptor table register\n     */\n    this.gdtr_size = view(Int32Array, memory, 572, 1);\n    this.gdtr_offset = view(Int32Array, memory, 576, 1);\n\n    this.tss_size_32 = view(Int32Array, memory, 1128, 1);\n\n    /*\n     * whether or not a page fault occured\n     */\n    this.page_fault = view(Uint32Array, memory, 540, 8);\n\n    this.cr = view(Int32Array, memory, 580, 8);\n\n    // current privilege level\n    this.cpl = view(Uint8Array, memory, 612, 1);\n\n    // current operand/address size\n    this.is_32 = view(Int32Array, memory, 804, 1);\n\n    this.stack_size_32 = view(Int32Array, memory, 808, 1);\n\n    /**\n     * Was the last instruction a hlt?\n     */\n    this.in_hlt = view(Uint8Array, memory, 616, 1);\n\n    this.last_virt_eip = view(Int32Array, memory, 620, 1);\n    this.eip_phys = view(Int32Array, memory, 624, 1);\n\n\n    this.sysenter_cs = view(Int32Array, memory, 636, 1);\n\n    this.sysenter_esp = view(Int32Array, memory, 640, 1);\n\n    this.sysenter_eip = view(Int32Array, memory, 644, 1);\n\n    this.prefixes = view(Int32Array, memory, 648, 1);\n\n    this.flags = view(Int32Array, memory, 120, 1);\n\n    /**\n     * bitmap of flags which are not updated in the flags variable\n     * changed by arithmetic instructions, so only relevant to arithmetic flags\n     */\n    this.flags_changed = view(Int32Array, memory, 100, 1);\n\n    /**\n     * enough infos about the last arithmetic operation to compute eflags\n     */\n    this.last_op_size = view(Int32Array, memory, 96, 1);\n    this.last_op1 = view(Int32Array, memory, 104, 1);\n    this.last_result = view(Int32Array, memory, 112, 1);\n\n    this.current_tsc = view(Uint32Array, memory, 960, 2); // 64 bit\n\n    /** @type {!Object} */\n    this.devices = {};\n\n    this.instruction_pointer = view(Int32Array, memory, 556, 1);\n    this.previous_ip = view(Int32Array, memory, 560, 1);\n\n    // configured by guest\n    this.apic_enabled = view(Uint8Array, memory, 548, 1);\n    // configured when the emulator starts (changes bios initialisation)\n    this.acpi_enabled = view(Uint8Array, memory, 552, 1);\n\n    // managed in io.js\n    /** @const */ this.memory_map_read8 = [];\n    /** @const */ this.memory_map_write8 = [];\n    /** @const */ this.memory_map_read32 = [];\n    /** @const */ this.memory_map_write32 = [];\n\n    /**\n     * @const\n     * @type {{main: ArrayBuffer, vga: ArrayBuffer}}\n     */\n    this.bios = {\n        main: null,\n        vga: null,\n    };\n\n    this.instruction_counter = view(Uint32Array, memory, 664, 1);\n\n    // registers\n    this.reg32 = view(Int32Array, memory, 64, 8);\n\n    this.fpu_st = view(Int32Array, memory, 1152, 4 * 8);\n\n    this.fpu_stack_empty = view(Uint8Array, memory, 816, 1);\n    this.fpu_stack_empty[0] = 0xFF;\n    this.fpu_stack_ptr = view(Uint8Array, memory, 1032, 1);\n    this.fpu_stack_ptr[0] = 0;\n\n    this.fpu_control_word = view(Uint16Array, memory, 1036, 1);\n    this.fpu_control_word[0] = 0x37F;\n    this.fpu_status_word = view(Uint16Array, memory, 1040, 1);\n    this.fpu_status_word[0] = 0;\n    this.fpu_ip = view(Int32Array, memory, 1048, 1);\n    this.fpu_ip[0] = 0;\n    this.fpu_ip_selector = view(Int32Array, memory, 1052, 1);\n    this.fpu_ip_selector[0] = 0;\n    this.fpu_opcode = view(Int32Array, memory, 1044, 1);\n    this.fpu_opcode[0] = 0;\n    this.fpu_dp = view(Int32Array, memory, 1056, 1);\n    this.fpu_dp[0] = 0;\n    this.fpu_dp_selector = view(Int32Array, memory, 1060, 1);\n    this.fpu_dp_selector[0] = 0;\n\n    this.reg_xmm32s = view(Int32Array, memory, 832, 8 * 4);\n\n    this.mxcsr = view(Int32Array, memory, 824, 1);\n\n    // segment registers, tr and ldtr\n    this.sreg = view(Uint16Array, memory, 668, 8);\n\n    // debug registers\n    this.dreg = view(Int32Array, memory, 684, 8);\n\n    this.reg_pdpte = view(Int32Array, memory, 968, 8);\n\n    this.svga_dirty_bitmap_min_offset = view(Uint32Array, memory, 716, 1);\n    this.svga_dirty_bitmap_max_offset = view(Uint32Array, memory, 720, 1);\n\n    this.fw_value = [];\n    this.fw_pointer = 0;\n    this.option_roms = [];\n\n    this.io = undefined;\n\n    this.bus = bus;\n\n    this.set_tsc(0, 0);\n\n    if(DEBUG)\n    {\n        this.seen_code = {};\n        this.seen_code_uncompiled = {};\n    }\n\n    //Object.seal(this);\n}\n\nCPU.prototype.mmap_read8 = function(addr)\n{\n    const value = this.memory_map_read8[addr >>> MMAP_BLOCK_BITS](addr);\n    dbg_assert(value >= 0 && value <= 0xFF);\n    return value;\n};\n\nCPU.prototype.mmap_write8 = function(addr, value)\n{\n    dbg_assert(value >= 0 && value <= 0xFF);\n    this.memory_map_write8[addr >>> MMAP_BLOCK_BITS](addr, value);\n};\n\nCPU.prototype.mmap_write16 = function(addr, value)\n{\n    var fn = this.memory_map_write8[addr >>> MMAP_BLOCK_BITS];\n\n    dbg_assert(value >= 0 && value <= 0xFFFF);\n    fn(addr, value & 0xFF);\n    fn(addr + 1 | 0, value >> 8);\n};\n\nCPU.prototype.mmap_read32 = function(addr)\n{\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n\n    return this.memory_map_read32[aligned_addr](addr);\n};\n\nCPU.prototype.mmap_write32 = function(addr, value)\n{\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n\n    this.memory_map_write32[aligned_addr](addr, value);\n};\n\nCPU.prototype.mmap_write64 = function(addr, value0, value1)\n{\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n    // This should hold since writes across pages are split up\n    dbg_assert(aligned_addr === (addr + 7) >>> MMAP_BLOCK_BITS);\n\n    var write_func32 = this.memory_map_write32[aligned_addr];\n    write_func32(addr, value0);\n    write_func32(addr + 4, value1);\n};\n\nCPU.prototype.mmap_write128 = function(addr, value0, value1, value2, value3)\n{\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n    // This should hold since writes across pages are split up\n    dbg_assert(aligned_addr === (addr + 12) >>> MMAP_BLOCK_BITS);\n\n    var write_func32 = this.memory_map_write32[aligned_addr];\n    write_func32(addr, value0);\n    write_func32(addr + 4, value1);\n    write_func32(addr + 8, value2);\n    write_func32(addr + 12, value3);\n};\n\n/**\n * @param {Array.<number>|Uint8Array} blob\n * @param {number} offset\n */\nCPU.prototype.write_blob = function(blob, offset)\n{\n    dbg_assert(blob && blob.length >= 0);\n\n    if(blob.length)\n    {\n        dbg_assert(!this.in_mapped_range(offset));\n        dbg_assert(!this.in_mapped_range(offset + blob.length - 1));\n\n        this.jit_dirty_cache(offset, offset + blob.length);\n        this.mem8.set(blob, offset);\n    }\n};\n\nCPU.prototype.read_blob = function(offset, length)\n{\n    if(length)\n    {\n        dbg_assert(!this.in_mapped_range(offset));\n        dbg_assert(!this.in_mapped_range(offset + length - 1));\n    }\n    return this.mem8.subarray(offset, offset + length);\n};\n\nCPU.prototype.clear_opstats = function()\n{\n    new Uint8Array(this.wasm_memory.buffer, 0x8000, 0x20000).fill(0);\n    this.wm.exports[\"profiler_init\"]();\n};\n\nCPU.prototype.create_jit_imports = function()\n{\n    // Set this.jit_imports as generated WASM modules will expect\n\n    const jit_imports = Object.create(null);\n\n    jit_imports[\"m\"] = this.wm.exports[\"memory\"];\n\n    for(const name of Object.keys(this.wm.exports))\n    {\n        if(name.startsWith(\"_\") || name.startsWith(\"zstd\") || name.endsWith(\"_js\"))\n        {\n            continue;\n        }\n\n        jit_imports[name] = this.wm.exports[name];\n    }\n\n    this.jit_imports = jit_imports;\n};\n\nCPU.prototype.wasm_patch = function()\n{\n    const get_optional_import = name => this.wm.exports[name];\n\n    const get_import = name =>\n    {\n        const f = get_optional_import(name);\n        console.assert(f, \"Missing import: \" + name);\n        return f;\n    };\n\n    this.reset_cpu = get_import(\"reset_cpu\");\n\n    this.getiopl = get_import(\"getiopl\");\n    this.get_eflags = get_import(\"get_eflags\");\n\n    this.handle_irqs = get_import(\"handle_irqs\");\n\n    this.main_loop = get_import(\"main_loop\");\n\n    this.set_jit_config = get_import(\"set_jit_config\");\n\n    this.read8 = get_import(\"read8\");\n    this.read16 = get_import(\"read16\");\n    this.read32s = get_import(\"read32s\");\n    this.write8 = get_import(\"write8\");\n    this.write16 = get_import(\"write16\");\n    this.write32 = get_import(\"write32\");\n    this.in_mapped_range = get_import(\"in_mapped_range\");\n\n    // used by nasmtests\n    this.fpu_load_tag_word = get_import(\"fpu_load_tag_word\");\n    this.fpu_load_status_word = get_import(\"fpu_load_status_word\");\n    this.fpu_get_sti_f64 = get_import(\"fpu_get_sti_f64\");\n\n    this.translate_address_system_read = get_import(\"translate_address_system_read_js\");\n\n    this.get_seg_cs = get_import(\"get_seg_cs\");\n    this.get_real_eip = get_import(\"get_real_eip\");\n\n    this.clear_tlb = get_import(\"clear_tlb\");\n    this.full_clear_tlb = get_import(\"full_clear_tlb\");\n    this.update_state_flags = get_import(\"update_state_flags\");\n\n    this.set_tsc = get_import(\"set_tsc\");\n    this.store_current_tsc = get_import(\"store_current_tsc\");\n\n    this.set_cpuid_level = get_import(\"set_cpuid_level\");\n\n    this.device_raise_irq = get_import(\"device_raise_irq\");\n    this.device_lower_irq = get_import(\"device_lower_irq\");\n\n    this.apic_timer = get_import(\"apic_timer\");\n\n    if(DEBUG)\n    {\n        this.jit_force_generate_unsafe = get_optional_import(\"jit_force_generate_unsafe\");\n    }\n\n    this.jit_clear_cache = get_import(\"jit_clear_cache_js\");\n    this.jit_dirty_cache = get_import(\"jit_dirty_cache\");\n    this.codegen_finalize_finished = get_import(\"codegen_finalize_finished\");\n\n    this.allocate_memory = get_import(\"allocate_memory\");\n    this.zero_memory = get_import(\"zero_memory\");\n    this.is_memory_zeroed = get_import(\"is_memory_zeroed\");\n\n    this.svga_allocate_memory = get_import(\"svga_allocate_memory\");\n    this.svga_allocate_dest_buffer = get_import(\"svga_allocate_dest_buffer\");\n    this.svga_fill_pixel_buffer = get_import(\"svga_fill_pixel_buffer\");\n    this.svga_mark_dirty = get_import(\"svga_mark_dirty\");\n\n    this.get_pic_addr_master = get_import(\"get_pic_addr_master\");\n    this.get_pic_addr_slave = get_import(\"get_pic_addr_slave\");\n    this.get_apic_addr = get_import(\"get_apic_addr\");\n    this.get_ioapic_addr = get_import(\"get_ioapic_addr\");\n\n    this.zstd_create_ctx = get_import(\"zstd_create_ctx\");\n    this.zstd_get_src_ptr = get_import(\"zstd_get_src_ptr\");\n    this.zstd_free_ctx = get_import(\"zstd_free_ctx\");\n    this.zstd_read = get_import(\"zstd_read\");\n    this.zstd_read_free = get_import(\"zstd_read_free\");\n};\n\nCPU.prototype.jit_force_generate = function(addr)\n{\n    if(!this.jit_force_generate_unsafe)\n    {\n        dbg_assert(false, \"Not supported in this wasm build: jit_force_generate_unsafe\");\n        return;\n    }\n\n    this.jit_force_generate_unsafe(addr);\n};\n\nCPU.prototype.jit_clear_func = function(index)\n{\n    dbg_assert(index >= 0 && index < WASM_TABLE_SIZE);\n    this.wm.wasm_table.set(index + WASM_TABLE_OFFSET, null);\n};\n\nCPU.prototype.jit_clear_all_funcs = function()\n{\n    const table = this.wm.wasm_table;\n\n    for(let i = 0; i < WASM_TABLE_SIZE; i++)\n    {\n        table.set(WASM_TABLE_OFFSET + i, null);\n    }\n};\n\nCPU.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.memory_size[0];\n    state[1] = new Uint8Array([...this.segment_is_null, ...this.segment_access_bytes]);\n    state[2] = this.segment_offsets;\n    state[3] = this.segment_limits;\n    state[4] = this.protected_mode[0];\n    state[5] = this.idtr_offset[0];\n    state[6] = this.idtr_size[0];\n    state[7] = this.gdtr_offset[0];\n    state[8] = this.gdtr_size[0];\n    state[9] = this.page_fault[0];\n    state[10] = this.cr;\n    state[11] = this.cpl[0];\n\n    state[13] = this.is_32[0];\n\n    state[16] = this.stack_size_32[0];\n    state[17] = this.in_hlt[0];\n    state[18] = this.last_virt_eip[0];\n    state[19] = this.eip_phys[0];\n\n    state[22] = this.sysenter_cs[0];\n    state[23] = this.sysenter_eip[0];\n    state[24] = this.sysenter_esp[0];\n    state[25] = this.prefixes[0];\n    state[26] = this.flags[0];\n    state[27] = this.flags_changed[0];\n    state[28] = this.last_op1[0];\n\n    state[30] = this.last_op_size[0];\n\n    state[37] = this.instruction_pointer[0];\n    state[38] = this.previous_ip[0];\n    state[39] = this.reg32;\n    state[40] = this.sreg;\n    state[41] = this.dreg;\n    state[42] = this.reg_pdpte;\n\n    this.store_current_tsc();\n    state[43] = this.current_tsc;\n\n    state[45] = this.devices.virtio_9p;\n    state[46] = this.get_state_apic();\n    state[47] = this.devices.rtc;\n    state[48] = this.devices.pci;\n    state[49] = this.devices.dma;\n    state[50] = this.devices.acpi;\n    // 51 (formerly hpet)\n    state[52] = this.devices.vga;\n    state[53] = this.devices.ps2;\n    state[54] = this.devices.uart0;\n    state[55] = this.devices.fdc;\n\n    if(!this.devices.ide.secondary)\n    {\n        if(this.devices.ide.primary?.master.is_atapi)\n        {\n            state[56] = this.devices.ide.primary;\n        }\n        else\n        {\n            state[57] = this.devices.ide.primary;\n        }\n    }\n    else\n    {\n        state[85] = this.devices.ide;\n    }\n\n    state[58] = this.devices.pit;\n    state[59] = this.devices.net;\n    state[60] = this.get_state_pic();\n    state[61] = this.devices.sb16;\n\n    state[62] = this.fw_value;\n\n    state[63] = this.get_state_ioapic();\n\n    state[64] = this.tss_size_32[0];\n\n    state[66] = this.reg_xmm32s;\n\n    state[67] = this.fpu_st;\n    state[68] = this.fpu_stack_empty[0];\n    state[69] = this.fpu_stack_ptr[0];\n    state[70] = this.fpu_control_word[0];\n    state[71] = this.fpu_ip[0];\n    state[72] = this.fpu_ip_selector[0];\n    state[73] = this.fpu_dp[0];\n    state[74] = this.fpu_dp_selector[0];\n    state[75] = this.fpu_opcode[0];\n\n    const { packed_memory, bitmap } = this.pack_memory();\n    state[77] = packed_memory;\n    state[78] = new Uint8Array(bitmap.get_buffer());\n\n    state[79] = this.devices.uart1;\n    state[80] = this.devices.uart2;\n    state[81] = this.devices.uart3;\n    state[82] = this.devices.virtio_console;\n    state[83] = this.devices.virtio_net;\n    state[84] = this.devices.virtio_balloon;\n\n    // state[85] new ide set above\n\n    state[86] = this.last_result;\n    state[87] = this.fpu_status_word;\n    state[88] = this.mxcsr;\n\n    return state;\n};\n\nCPU.prototype.get_state_pic = function()\n{\n    const pic_size = 13;\n    const pic = new Uint8Array(this.wasm_memory.buffer, this.get_pic_addr_master(), pic_size);\n    const pic_slave = new Uint8Array(this.wasm_memory.buffer, this.get_pic_addr_slave(), pic_size);\n\n    const state = [];\n    const state_slave = [];\n\n    state[0] = pic[0]; // irq_mask\n    state[1] = pic[1]; // irq_map\n    state[2] = pic[2]; // isr\n    state[3] = pic[3]; // irr\n    state[4] = pic[4]; // is_master\n    state[5] = state_slave;\n    state[6] = pic[6]; // expect_icw4\n    state[7] = pic[7]; // state\n    state[8] = pic[8]; // read_isr\n    state[9] = pic[9]; // auto_eoi\n    state[10] = pic[10]; // elcr\n    state[11] = pic[11]; // irq_value\n    state[12] = pic[12]; // special_mask_mode\n\n    state_slave[0] = pic_slave[0]; // irq_mask\n    state_slave[1] = pic_slave[1]; // irq_map\n    state_slave[2] = pic_slave[2]; // isr\n    state_slave[3] = pic_slave[3]; // irr\n    state_slave[4] = pic_slave[4]; // is_master\n    state_slave[5] = null;\n    state_slave[6] = pic_slave[6]; // expect_icw4\n    state_slave[7] = pic_slave[7]; // state\n    state_slave[8] = pic_slave[8]; // read_isr\n    state_slave[9] = pic_slave[9]; // auto_eoi\n    state_slave[10] = pic_slave[10]; // elcr\n    state_slave[11] = pic_slave[11]; // irq_value\n    state_slave[12] = pic_slave[12]; // special_mask_mode\n\n    return state;\n};\n\nCPU.prototype.get_state_apic = function()\n{\n    const APIC_STRUCT_SIZE = 4 * 46; // keep in sync with apic.rs\n    return new Uint8Array(this.wasm_memory.buffer, this.get_apic_addr(), APIC_STRUCT_SIZE);\n};\n\nCPU.prototype.get_state_ioapic = function()\n{\n    const IOAPIC_STRUCT_SIZE = 4 * 52; // keep in sync with ioapic.rs\n    return new Uint8Array(this.wasm_memory.buffer, this.get_ioapic_addr(), IOAPIC_STRUCT_SIZE);\n};\n\nCPU.prototype.set_state = function(state)\n{\n    this.memory_size[0] = state[0];\n\n    if(this.mem8.length !== this.memory_size[0])\n    {\n        console.warn(\"Note: Memory size mismatch. we=\" + this.mem8.length + \" state=\" + this.memory_size[0]);\n    }\n\n    if(state[1].length === 8)\n    {\n        // NOTE: support for old state images; delete this when bumping STATE_VERSION\n        this.segment_is_null.set(state[1]);\n        this.segment_access_bytes.fill(0x80 | (3 << 5) | 0x10 | 0x02);\n        this.segment_access_bytes[REG_CS] = 0x80 | (3 << 5) | 0x10 | 0x08 | 0x02;\n    }\n    else if(state[1].length === 16)\n    {\n        this.segment_is_null.set(state[1].subarray(0, 8));\n        this.segment_access_bytes.set(state[1].subarray(8, 16));\n    }\n    else\n    {\n        dbg_assert(\"Unexpected cpu segment state length:\" + state[1].length);\n    }\n    this.segment_offsets.set(state[2]);\n    this.segment_limits.set(state[3]);\n\n    this.protected_mode[0] = state[4];\n    this.idtr_offset[0] = state[5];\n    this.idtr_size[0] = state[6];\n    this.gdtr_offset[0] = state[7];\n    this.gdtr_size[0] = state[8];\n    this.page_fault[0] = state[9];\n    this.cr.set(state[10]);\n    this.cpl[0] = state[11];\n\n    this.is_32[0] = state[13];\n\n    this.stack_size_32[0] = state[16];\n\n    this.in_hlt[0] = state[17];\n    this.last_virt_eip[0] = state[18];\n    this.eip_phys[0] = state[19];\n\n    this.sysenter_cs[0] = state[22];\n    this.sysenter_eip[0] = state[23];\n    this.sysenter_esp[0] = state[24];\n    this.prefixes[0] = state[25];\n\n    this.flags[0] = state[26];\n    this.flags_changed[0] = state[27];\n    this.last_op1[0] = state[28];\n\n    this.last_op_size[0] = state[30];\n\n    this.instruction_pointer[0] = state[37];\n    this.previous_ip[0] = state[38];\n    this.reg32.set(state[39]);\n    this.sreg.set(state[40]);\n    this.dreg.set(state[41]);\n    state[42] && this.reg_pdpte.set(state[42]);\n\n    this.set_tsc(state[43][0], state[43][1]);\n\n    this.devices.virtio_9p && this.devices.virtio_9p.set_state(state[45]);\n    state[46] && this.set_state_apic(state[46]);\n    this.devices.rtc && this.devices.rtc.set_state(state[47]);\n    this.devices.dma && this.devices.dma.set_state(state[49]);\n    this.devices.acpi && this.devices.acpi.set_state(state[50]);\n    // 51 (formerly hpet)\n    this.devices.vga && this.devices.vga.set_state(state[52]);\n    this.devices.ps2 && this.devices.ps2.set_state(state[53]);\n    this.devices.uart0 && this.devices.uart0.set_state(state[54]);\n    this.devices.fdc && this.devices.fdc.set_state(state[55]);\n\n    if(state[56] || state[57])\n    {\n        // ide device from older version of v86, only primary: state[56] contains cdrom, state[57] contains hard drive\n\n        const ide_config = [[undefined, undefined], [undefined, undefined]];\n        if(state[56])\n        {\n            ide_config[0][0] = { is_cdrom: true, buffer: this.devices.cdrom.buffer };\n        }\n        else\n        {\n            ide_config[0][0] = { is_cdrom: false, buffer: this.devices.ide.primary.master.buffer };\n\n        }\n        this.devices.ide = new IDEController(this, this.devices.ide.bus, ide_config);\n        this.devices.cdrom = state[56] ? this.devices.ide.primary.master : undefined;\n        this.devices.ide.primary.set_state(state[56] || state[57]);\n    }\n    else if(state[85])\n    {\n        this.devices.ide.set_state(state[85]);\n    }\n\n    this.devices.pci && this.devices.pci.set_state(state[48]);\n\n    this.devices.pit && this.devices.pit.set_state(state[58]);\n    this.devices.net && this.devices.net.set_state(state[59]);\n    this.set_state_pic(state[60]);\n    this.devices.sb16 && this.devices.sb16.set_state(state[61]);\n\n    this.devices.uart1 && this.devices.uart1.set_state(state[79]);\n    this.devices.uart2 && this.devices.uart2.set_state(state[80]);\n    this.devices.uart3 && this.devices.uart3.set_state(state[81]);\n    this.devices.virtio_console && this.devices.virtio_console.set_state(state[82]);\n    this.devices.virtio_net && this.devices.virtio_net.set_state(state[83]);\n    this.devices.virtio_balloon && this.devices.virtio_balloon.set_state(state[84]);\n\n    this.fw_value = state[62];\n\n    state[63] && this.set_state_ioapic(state[63]);\n\n    this.tss_size_32[0] = state[64];\n\n    this.reg_xmm32s.set(state[66]);\n\n    this.fpu_st.set(state[67]);\n    this.fpu_stack_empty[0] = state[68];\n    this.fpu_stack_ptr[0] = state[69];\n    this.fpu_control_word[0] = state[70];\n    this.fpu_ip[0] = state[71];\n    this.fpu_ip_selector[0] = state[72];\n    this.fpu_dp[0] = state[73];\n    this.fpu_dp_selector[0] = state[74];\n    this.fpu_opcode[0] = state[75];\n\n    if(state[86] !== undefined) this.last_result = state[86];\n    if(state[87] !== undefined) this.fpu_status_word = state[87];\n    if(state[88] !== undefined) this.mxcsr = state[88];\n\n    const bitmap = new Bitmap(state[78].buffer);\n    const packed_memory = state[77];\n    this.unpack_memory(bitmap, packed_memory);\n\n    this.update_state_flags();\n\n    this.full_clear_tlb();\n\n    this.jit_clear_cache();\n};\n\nCPU.prototype.set_state_pic = function(state)\n{\n    // Note: This could exists for compatibility with old state images\n    // It should be deleted when the state version changes\n\n    const pic_size = 13;\n    const pic = new Uint8Array(this.wasm_memory.buffer, this.get_pic_addr_master(), pic_size);\n    const pic_slave = new Uint8Array(this.wasm_memory.buffer, this.get_pic_addr_slave(), pic_size);\n\n    pic[0] = state[0]; // irq_mask\n    pic[1] = state[1]; // irq_map\n    pic[2] = state[2]; // isr\n    pic[3] = state[3]; // irr\n    pic[4] = state[4]; // is_master\n    const state_slave = state[5];\n    pic[6] = state[6]; // expect_icw4\n    pic[7] = state[7]; // state\n    pic[8] = state[8]; // read_isr\n    pic[9] = state[9]; // auto_eoi\n    pic[10] = state[10]; // elcr\n    pic[11] = state[11]; // irq_value (undefined in old state images)\n    pic[12] = state[12]; // special_mask_mode (undefined in old state images)\n\n    pic_slave[0] = state_slave[0]; // irq_mask\n    pic_slave[1] = state_slave[1]; // irq_map\n    pic_slave[2] = state_slave[2]; // isr\n    pic_slave[3] = state_slave[3]; // irr\n    pic_slave[4] = state_slave[4]; // is_master\n    // dummy\n    pic_slave[6] = state_slave[6]; // expect_icw4\n    pic_slave[7] = state_slave[7]; // state\n    pic_slave[8] = state_slave[8]; // read_isr\n    pic_slave[9] = state_slave[9]; // auto_eoi\n    pic_slave[10] = state_slave[10]; // elcr\n    pic_slave[11] = state_slave[11]; // irq_value (undefined in old state images)\n    pic_slave[12] = state_slave[12]; // special_mask_mode (undefined in old state images)\n};\n\nCPU.prototype.set_state_apic = function(state)\n{\n    const APIC_STRUCT_SIZE = 4 * 46; // keep in sync with apic.rs\n    const IOAPIC_CONFIG_MASKED = 1 << 16;\n\n    if(state instanceof Array)\n    {\n        // old js state image; delete this code path when the state version changes\n        const apic = new Int32Array(this.wasm_memory.buffer, this.get_apic_addr(), APIC_STRUCT_SIZE >> 2);\n        apic[0] = state[0]; // apic_id\n        apic[1] = state[1]; // timer_divier\n        apic[2] = state[2]; // timer_divider_shift\n        apic[3] = state[3]; // timer_initial_count\n        apic[4] = state[4]; // timer_current_count\n        // skip next_tick (in js: state[4]; in rust: apic[6] and apic[7])\n        apic[8] = state[6]; // lvt_timer\n        apic[9] = state[7]; // lvt_perf_counter\n        apic[10] = state[8]; // lvt_int0\n        apic[11] = state[9]; // lvt_int1\n        apic[12] = state[10]; // lvt_error\n        apic[13] = state[11]; // tpr\n        apic[14] = state[12]; // icr0\n        apic[15] = state[13]; // icr1\n        apic.set(state[15], 16); // irr\n        apic.set(state[15], 24); // isr\n        apic.set(state[16], 32); // tmr\n        apic[40] = state[17]; // spurious_vector\n        apic[41] = state[18]; // destination_format\n        apic[42] = state[19]; // local_destination\n        apic[43] = state[20]; // error\n        apic[44] = state[21]; // read_error\n        apic[45] = state[22] || IOAPIC_CONFIG_MASKED; // lvt_thermal_sensor\n    }\n    else\n    {\n        const apic = new Uint8Array(this.wasm_memory.buffer, this.get_apic_addr(), APIC_STRUCT_SIZE);\n        dbg_assert(state instanceof Uint8Array);\n        dbg_assert(state.length === apic.length); // later versions might need to handle state upgrades here\n        apic.set(state);\n    }\n};\n\nCPU.prototype.set_state_ioapic = function(state)\n{\n    const IOAPIC_STRUCT_SIZE = 4 * 52; // keep in sync with ioapic.rs\n\n    if(state instanceof Array)\n    {\n        // old js state image; delete this code path when the state version changes\n        dbg_assert(state[0].length === 24);\n        dbg_assert(state[1].length === 24);\n        dbg_assert(state.length === 6);\n        const ioapic = new Int32Array(this.wasm_memory.buffer, this.get_ioapic_addr(), IOAPIC_STRUCT_SIZE >> 2);\n        ioapic.set(state[0], 0); // ioredtbl_config\n        ioapic.set(state[1], 24); // ioredtbl_destination\n        ioapic[48] = state[2]; // ioregsel\n        ioapic[49] = state[3]; // ioapic_id\n        ioapic[50] = state[4]; // irr\n        ioapic[51] = state[5]; // irq_value\n    }\n    else\n    {\n        const ioapic = new Uint8Array(this.wasm_memory.buffer, this.get_ioapic_addr(), IOAPIC_STRUCT_SIZE);\n        dbg_assert(state instanceof Uint8Array);\n        dbg_assert(state.length === ioapic.length); // later versions might need to handle state upgrades here\n        ioapic.set(state);\n    }\n};\n\nCPU.prototype.pack_memory = function()\n{\n    dbg_assert((this.mem8.length & 0xFFF) === 0);\n\n    const page_count = this.mem8.length >> 12;\n    const nonzero_pages = [];\n    for(let page = 0; page < page_count; page++)\n    {\n        if(!this.is_memory_zeroed(page << 12, 0x1000))\n        {\n            nonzero_pages.push(page);\n        }\n    }\n\n    const bitmap = new Bitmap(page_count);\n    const packed_memory = new Uint8Array(nonzero_pages.length << 12);\n\n    for(const [i, page] of nonzero_pages.entries())\n    {\n        bitmap.set(page, 1);\n\n        const offset = page << 12;\n        const page_contents = this.mem8.subarray(offset, offset + 0x1000);\n        packed_memory.set(page_contents, i << 12);\n    }\n\n    return { bitmap, packed_memory };\n};\n\nCPU.prototype.unpack_memory = function(bitmap, packed_memory)\n{\n    this.zero_memory(0, this.memory_size[0]);\n\n    const page_count = this.memory_size[0] >> 12;\n    let packed_page = 0;\n\n    for(let page = 0; page < page_count; page++)\n    {\n        if(bitmap.get(page))\n        {\n            const offset = packed_page << 12;\n            const view = packed_memory.subarray(offset, offset + 0x1000);\n            this.mem8.set(view, page << 12);\n            packed_page++;\n        }\n    }\n};\n\nCPU.prototype.reboot_internal = function()\n{\n    this.reset_cpu();\n\n    this.fw_value = [];\n\n    if(this.devices.virtio_9p)\n    {\n        this.devices.virtio_9p.reset();\n    }\n    if(this.devices.virtio_console)\n    {\n        this.devices.virtio_console.reset();\n    }\n    if(this.devices.virtio_net)\n    {\n        this.devices.virtio_net.reset();\n    }\n    if(this.devices.ps2)\n    {\n        this.devices.ps2.reset();\n    }\n\n    this.load_bios();\n};\n\nCPU.prototype.reset_memory = function()\n{\n    this.mem8.fill(0);\n};\n\nCPU.prototype.create_memory = function(size, minimum_size)\n{\n    if(size < minimum_size)\n    {\n        size = minimum_size;\n        dbg_log(\"Rounding memory size up to \" + size, LOG_CPU);\n    }\n    else if((size | 0) < 0)\n    {\n        size = Math.pow(2, 31) - MMAP_BLOCK_SIZE;\n        dbg_log(\"Rounding memory size down to \" + size, LOG_CPU);\n    }\n\n    size = ((size - 1) | (MMAP_BLOCK_SIZE - 1)) + 1 | 0;\n    dbg_assert((size | 0) > 0);\n    dbg_assert((size & MMAP_BLOCK_SIZE - 1) === 0);\n\n    console.assert(this.memory_size[0] === 0, \"Expected uninitialised memory\");\n\n    this.memory_size[0] = size;\n\n    const memory_offset = this.allocate_memory(size);\n\n    this.mem8 = view(Uint8Array, this.wasm_memory, memory_offset, size);\n    this.mem32s = view(Uint32Array, this.wasm_memory, memory_offset, size >> 2);\n};\n\n/**\n * @param {BusConnector} device_bus\n */\nCPU.prototype.init = function(settings, device_bus)\n{\n    this.create_memory(\n        settings.memory_size || 64 * 1024 * 1024,\n        settings.initrd ? 64 * 1024 * 1024 : 1024 * 1024,\n    );\n\n    if(settings.disable_jit)\n    {\n        this.set_jit_config(0, 1);\n    }\n\n    settings.cpuid_level && this.set_cpuid_level(settings.cpuid_level);\n\n    this.acpi_enabled[0] = +settings.acpi;\n\n    this.reset_cpu();\n\n    var io = new IO(this);\n    this.io = io;\n\n    this.bios.main = settings.bios;\n    this.bios.vga = settings.vga_bios;\n\n    this.load_bios();\n\n    if(settings.bzimage)\n    {\n        const option_rom = load_kernel(this.mem8, settings.bzimage, settings.initrd, settings.cmdline || \"\");\n\n        if(option_rom)\n        {\n            this.option_roms.push(option_rom);\n        }\n    }\n\n    io.register_read(0xB3, this, function()\n    {\n        // seabios smm_relocate_and_restore\n        dbg_log(\"port 0xB3 read\");\n        return 0;\n    });\n\n    var a20_byte = 0;\n\n    io.register_read(0x92, this, function()\n    {\n        return a20_byte;\n    });\n\n    io.register_write(0x92, this, function(out_byte)\n    {\n        a20_byte = out_byte;\n    });\n\n    io.register_read(0x511, this, function()\n    {\n        // bios config port (used by seabios and kvm-unit-test)\n        if(this.fw_pointer < this.fw_value.length)\n        {\n            return this.fw_value[this.fw_pointer++];\n        }\n        else\n        {\n            dbg_assert(false, \"config port: Read past value\");\n            return 0;\n        }\n    });\n    io.register_write(0x510, this, undefined, function(value)\n    {\n        // https://wiki.osdev.org/QEMU_fw_cfg\n        // https://github.com/qemu/qemu/blob/master/docs/specs/fw_cfg.txt\n\n        dbg_log(\"bios config port, index=\" + h(value));\n\n        function i32(x)\n        {\n            return new Uint8Array(Int32Array.of(x).buffer);\n        }\n\n        function to_be16(x)\n        {\n            return x >> 8 | x << 8 & 0xFF00;\n        }\n\n        function to_be32(x)\n        {\n            return x << 24 | x << 8 & 0xFF0000 | x >> 8 & 0xFF00 | x >>> 24;\n        }\n\n        this.fw_pointer = 0;\n\n        if(value === FW_CFG_SIGNATURE)\n        {\n            // Pretend to be qemu (for seabios)\n            this.fw_value = i32(FW_CFG_SIGNATURE_QEMU);\n        }\n        else if(value === FW_CFG_ID)\n        {\n            this.fw_value = i32(0);\n        }\n        else if(value === FW_CFG_RAM_SIZE)\n        {\n            this.fw_value = i32(this.memory_size[0]);\n        }\n        else if(value === FW_CFG_NB_CPUS)\n        {\n            this.fw_value = i32(1);\n        }\n        else if(value === FW_CFG_MAX_CPUS)\n        {\n            this.fw_value = i32(1);\n        }\n        else if(value === FW_CFG_NUMA)\n        {\n            this.fw_value = new Uint8Array(16);\n        }\n        else if(value === FW_CFG_FILE_DIR)\n        {\n            const buffer_size = 4 + 64 * this.option_roms.length;\n            const buffer32 = new Int32Array(buffer_size);\n            const buffer8 = new Uint8Array(buffer32.buffer);\n\n            buffer32[0] = to_be32(this.option_roms.length);\n\n            for(let i = 0; i < this.option_roms.length; i++)\n            {\n                const { name, data } = this.option_roms[i];\n                const file_struct_ptr = 4 + 64 * i;\n\n                dbg_assert(FW_CFG_FILE_START + i < 0x10000);\n                buffer32[file_struct_ptr + 0 >> 2] = to_be32(data.length);\n                buffer32[file_struct_ptr + 4 >> 2] = to_be16(FW_CFG_FILE_START + i);\n\n                dbg_assert(name.length < 64 - 8);\n\n                for(let j = 0; j < name.length; j++)\n                {\n                    buffer8[file_struct_ptr + 8 + j] = name.charCodeAt(j);\n                }\n            }\n\n            this.fw_value = buffer8;\n        }\n        else if(value >= FW_CFG_CUSTOM_START && value < FW_CFG_FILE_START)\n        {\n            this.fw_value = i32(0);\n        }\n        else if(value >= FW_CFG_FILE_START && value - FW_CFG_FILE_START < this.option_roms.length)\n        {\n            const i = value - FW_CFG_FILE_START;\n            this.fw_value = this.option_roms[i].data;\n        }\n        else\n        {\n            dbg_log(\"Warning: Unimplemented fw index: \" + h(value));\n            this.fw_value = i32(0);\n        }\n    });\n\n    if(DEBUG)\n    {\n        // Avoid logging noisey ports\n        io.register_write(0x80, this, function(out_byte) {});\n        io.register_read(0x80, this, function() { return 0xFF; });\n        io.register_write(0xE9, this, function(out_byte) {});\n    }\n\n    this.devices = {};\n\n    // TODO: Make this more configurable\n    if(settings.load_devices)\n    {\n        this.devices.pci = new PCI(this);\n\n        if(this.acpi_enabled[0])\n        {\n            this.devices.acpi = new ACPI(this);\n        }\n\n        this.devices.rtc = new RTC(this);\n        this.fill_cmos(this.devices.rtc, settings);\n\n        this.devices.dma = new DMA(this);\n\n        this.devices.vga = new VGAScreen(this, device_bus, settings.screen, settings.vga_memory_size || 8 * 1024 * 1024);\n\n        this.devices.ps2 = new PS2(this, device_bus);\n\n        this.devices.uart0 = new UART(this, 0x3F8, device_bus);\n\n        if(settings.uart1)\n        {\n            this.devices.uart1 = new UART(this, 0x2F8, device_bus);\n        }\n        if(settings.uart2)\n        {\n            this.devices.uart2 = new UART(this, 0x3E8, device_bus);\n        }\n        if(settings.uart3)\n        {\n            this.devices.uart3 = new UART(this, 0x2E8, device_bus);\n        }\n\n        this.devices.fdc = new FloppyController(this, settings.fda, settings.fdb);\n\n        const ide_config = [[undefined, undefined], [undefined, undefined]];\n        if(settings.hda)\n        {\n            ide_config[0][0] = { buffer: settings.hda };\n            ide_config[0][1] = { buffer: settings.hdb };\n        }\n        ide_config[1][0] = { is_cdrom: true, buffer: settings.cdrom };\n        this.devices.ide = new IDEController(this, device_bus, ide_config);\n        this.devices.cdrom = this.devices.ide.secondary.master;\n\n        this.devices.pit = new PIT(this, device_bus);\n\n        if(settings.net_device.type === \"ne2k\")\n        {\n            this.devices.net = new Ne2k(this, device_bus, settings.preserve_mac_from_state_image, settings.mac_address_translation);\n        }\n        else if(settings.net_device.type === \"virtio\")\n        {\n            this.devices.virtio_net = new VirtioNet(this, device_bus, settings.preserve_mac_from_state_image, settings.net_device.mtu);\n        }\n\n        if(settings.fs9p)\n        {\n            this.devices.virtio_9p = new Virtio9p(settings.fs9p, this, device_bus);\n        }\n        else if(settings.handle9p)\n        {\n            this.devices.virtio_9p = new Virtio9pHandler(settings.handle9p, this);\n        }\n        else if(settings.proxy9p)\n        {\n            this.devices.virtio_9p = new Virtio9pProxy(settings.proxy9p, this);\n        }\n        if(settings.virtio_console)\n        {\n            this.devices.virtio_console = new VirtioConsole(this, device_bus);\n        }\n        if(settings.virtio_balloon)\n        {\n            this.devices.virtio_balloon = new VirtioBalloon(this, device_bus);\n        }\n\n        if(true)\n        {\n            this.devices.sb16 = new SB16(this, device_bus);\n        }\n    }\n\n    if(settings.multiboot)\n    {\n        dbg_log(\"loading multiboot\", LOG_CPU);\n        const option_rom = this.load_multiboot_option_rom(settings.multiboot, settings.initrd, settings.cmdline);\n\n        if(option_rom)\n        {\n            if(this.bios.main)\n            {\n                dbg_log(\"adding option rom for multiboot\", LOG_CPU);\n                this.option_roms.push(option_rom);\n            }\n            else\n            {\n                dbg_log(\"loaded multiboot without bios\", LOG_CPU);\n                this.reg32[REG_EAX] = this.io.port_read32(0xF4);\n            }\n        }\n    }\n\n    this.debug_init();\n};\n\nCPU.prototype.load_multiboot = function (buffer)\n{\n    if(this.bios.main)\n    {\n        dbg_assert(false, \"load_multiboot not supported with BIOS\");\n    }\n\n    const option_rom = this.load_multiboot_option_rom(buffer, undefined, \"\");\n    if(option_rom)\n    {\n        dbg_log(\"loaded multiboot\", LOG_CPU);\n        this.reg32[REG_EAX] = this.io.port_read32(0xF4);\n    }\n};\n\nCPU.prototype.load_multiboot_option_rom = function(buffer, initrd, cmdline)\n{\n    // https://www.gnu.org/software/grub/manual/multiboot/multiboot.html\n\n    dbg_log(\"Trying multiboot from buffer of size \" + buffer.byteLength, LOG_CPU);\n\n    const ELF_MAGIC = 0x464C457F;\n    const MULTIBOOT_HEADER_MAGIC = 0x1BADB002;\n    const MULTIBOOT_HEADER_MEMORY_INFO = 0x2;\n    const MULTIBOOT_HEADER_ADDRESS = 0x10000;\n    const MULTIBOOT_BOOTLOADER_MAGIC = 0x2BADB002;\n    const MULTIBOOT_SEARCH_BYTES = 8192;\n    const MULTIBOOT_INFO_STRUCT_LEN = 116;\n    const MULTIBOOT_INFO_CMDLINE = 0x4;\n    const MULTIBOOT_INFO_MODS = 0x8;\n    const MULTIBOOT_INFO_MEM_MAP = 0x40;\n\n    if(buffer.byteLength < MULTIBOOT_SEARCH_BYTES)\n    {\n        var buf32 = new Int32Array(MULTIBOOT_SEARCH_BYTES / 4);\n        new Uint8Array(buf32.buffer).set(new Uint8Array(buffer));\n    }\n    else\n    {\n        var buf32 = new Int32Array(buffer, 0, MULTIBOOT_SEARCH_BYTES / 4);\n    }\n\n    for(var offset = 0; offset < MULTIBOOT_SEARCH_BYTES; offset += 4)\n    {\n        if(buf32[offset >> 2] === MULTIBOOT_HEADER_MAGIC)\n        {\n            var flags = buf32[offset + 4 >> 2];\n            var checksum = buf32[offset + 8 >> 2];\n            var total = MULTIBOOT_HEADER_MAGIC + flags + checksum | 0;\n\n            if(total)\n            {\n                dbg_log(\"Multiboot checksum check failed\", LOG_CPU);\n                continue;\n            }\n        }\n        else\n        {\n            continue;\n        }\n\n        dbg_log(\"Multiboot magic found, flags: \" + h(flags >>> 0, 8), LOG_CPU);\n        // bit 0 : load modules on page boundaries (may as well, if we load modules)\n        // bit 1 : provide a memory map (which we always will)\n        dbg_assert((flags & ~MULTIBOOT_HEADER_ADDRESS & ~3) === 0, \"TODO\");\n\n        // do this in a io register hook, so it can happen after BIOS does its work\n        var cpu = this;\n\n        this.io.register_read(0xF4, this, function () {return 0;} , function () { return 0;}, function () {\n            // actually do the load and return the multiboot magic\n            const multiboot_info_addr = 0x7C00;\n            let multiboot_data = multiboot_info_addr + MULTIBOOT_INFO_STRUCT_LEN;\n            let info = 0;\n\n            // command line\n            if(cmdline)\n            {\n                info |= MULTIBOOT_INFO_CMDLINE;\n\n                cpu.write32(multiboot_info_addr + 16, multiboot_data);\n\n                cmdline += \"\\x00\";\n                const encoder = new TextEncoder();\n                const cmdline_utf8 = encoder.encode(cmdline);\n                cpu.write_blob(cmdline_utf8, multiboot_data);\n                multiboot_data += cmdline_utf8.length;\n            }\n\n            // memory map\n            if(flags & MULTIBOOT_HEADER_MEMORY_INFO)\n            {\n                info |= MULTIBOOT_INFO_MEM_MAP;\n                let multiboot_mmap_count = 0;\n                cpu.write32(multiboot_info_addr + 44, 0);\n                cpu.write32(multiboot_info_addr + 48, multiboot_data);\n\n                // Create a memory map for the multiboot kernel\n                // does not exclude traditional bios exclusions\n                let start = 0;\n                let was_memory = false;\n                for(let addr = 0; addr < MMAP_MAX; addr += MMAP_BLOCK_SIZE)\n                {\n                    if(was_memory && cpu.memory_map_read8[addr >>> MMAP_BLOCK_BITS] !== undefined)\n                    {\n                        cpu.write32(multiboot_data, 20); // size\n                        cpu.write32(multiboot_data + 4, start); //addr (64-bit)\n                        cpu.write32(multiboot_data + 8, 0);\n                        cpu.write32(multiboot_data + 12, addr - start); // len (64-bit)\n                        cpu.write32(multiboot_data + 16, 0);\n                        cpu.write32(multiboot_data + 20, 1); // type (MULTIBOOT_MEMORY_AVAILABLE)\n                        multiboot_data += 24;\n                        multiboot_mmap_count += 24;\n                        was_memory = false;\n                    }\n                    else if(!was_memory && cpu.memory_map_read8[addr >>> MMAP_BLOCK_BITS] === undefined)\n                    {\n                        start = addr;\n                        was_memory = true;\n                    }\n                }\n                dbg_assert (!was_memory, \"top of 4GB shouldn't have memory\");\n                cpu.write32(multiboot_info_addr + 44, multiboot_mmap_count);\n            }\n\n            let entrypoint = 0;\n            let top_of_load = 0;\n\n            if(flags & MULTIBOOT_HEADER_ADDRESS)\n            {\n                dbg_log(\"Multiboot specifies its own address table\", LOG_CPU);\n\n                var header_addr = buf32[offset + 12 >> 2];\n                var load_addr = buf32[offset + 16 >> 2];\n                var load_end_addr = buf32[offset + 20 >> 2];\n                var bss_end_addr = buf32[offset + 24 >> 2];\n                var entry_addr = buf32[offset + 28 >> 2];\n\n                dbg_log(\"header=\" + h(header_addr, 8) +\n                        \" load=\" + h(load_addr, 8) +\n                        \" load_end=\" + h(load_end_addr, 8) +\n                        \" bss_end=\" + h(bss_end_addr, 8) +\n                        \" entry=\" + h(entry_addr, 8));\n\n                dbg_assert(load_addr <= header_addr);\n\n                var file_start = offset - (header_addr - load_addr);\n\n                if(load_end_addr === 0)\n                {\n                    var length = undefined;\n                }\n                else\n                {\n                    dbg_assert(load_end_addr >= load_addr);\n                    var length = load_end_addr - load_addr;\n                }\n\n                const blob = new Uint8Array(buffer, file_start, length);\n                cpu.write_blob(blob, load_addr);\n\n                entrypoint = entry_addr | 0;\n                top_of_load = Math.max(load_end_addr, bss_end_addr);\n            }\n            else if(buf32[0] === ELF_MAGIC)\n            {\n                dbg_log(\"Multiboot image is in elf format\", LOG_CPU);\n\n                const elf = read_elf(buffer);\n\n                entrypoint = elf.header.entry;\n\n                for(const program of elf.program_headers)\n                {\n                    if(program.type === 0)\n                    {\n                        // null\n                    }\n                    else if(program.type === 1)\n                    {\n                        // load\n\n                        dbg_assert(program.filesz <= program.memsz);\n\n                        if(program.paddr + program.memsz < cpu.memory_size[0])\n                        {\n                            if(program.filesz) // offset might be outside of buffer if filesz is 0\n                            {\n                                const blob = new Uint8Array(buffer, program.offset, program.filesz);\n                                cpu.write_blob(blob, program.paddr);\n                            }\n                            top_of_load = Math.max(top_of_load, program.paddr + program.memsz);\n                            dbg_log(\"prg load \" + program.paddr + \" to \" + (program.paddr + program.memsz), LOG_CPU);\n\n                            // Since multiboot specifies that paging is disabled, we load to the physical address;\n                            // but the entry point is specified in virtual addresses so adjust the entrypoint if needed\n\n                            if(entrypoint === elf.header.entry && program.vaddr <= entrypoint && (program.vaddr + program.memsz) > entrypoint)\n                            {\n                                entrypoint = (entrypoint - program.vaddr) + program.paddr;\n                            }\n                        }\n                        else\n                        {\n                            dbg_log(\"Warning: Skipped loading section, paddr=\" + h(program.paddr) + \" memsz=\" + program.memsz, LOG_CPU);\n                        }\n                    }\n                    else if(\n                        program.type === 2 || // dynamic\n                        program.type === 3 || // interp\n                        program.type === 4 || // note\n                        program.type === 6 || // phdr\n                        program.type === 7 || // tls\n                        program.type === 0x6474e550 || // gnu_eh_frame\n                        program.type === 0x6474e551 || // gnu_stack\n                        program.type === 0x6474e552 || // gnu_relro\n                        program.type === 0x6474e553)   // gnu_property\n                    {\n                        dbg_log(\"skip load type \" + program.type + \" \" + program.paddr + \" to \" + (program.paddr + program.memsz), LOG_CPU);\n                        // ignore for now\n                    }\n                    else\n                    {\n                        dbg_assert(false, \"unimplemented elf section type: \" + h(program.type));\n                    }\n                }\n            }\n            else\n            {\n                dbg_assert(false, \"Not a bootable multiboot format\");\n            }\n\n            if(initrd)\n            {\n                info |= MULTIBOOT_INFO_MODS;\n\n                cpu.write32(multiboot_info_addr + 20, 1); // mods_count\n                cpu.write32(multiboot_info_addr + 24, multiboot_data); // mods_addr;\n\n                var ramdisk_address = top_of_load;\n                if((ramdisk_address & 4095) !== 0)\n                {\n                    ramdisk_address = (ramdisk_address & ~4095) + 4096;\n                }\n                dbg_log(\"ramdisk address \" + ramdisk_address);\n                var ramdisk_top = ramdisk_address + initrd.byteLength;\n\n                cpu.write32(multiboot_data, ramdisk_address); // mod_start\n                cpu.write32(multiboot_data + 4, ramdisk_top); // mod_end\n                cpu.write32(multiboot_data + 8, 0); // string\n                cpu.write32(multiboot_data + 12, 0); // reserved\n                multiboot_data += 16;\n\n                dbg_assert(ramdisk_top < cpu.memory_size[0]);\n\n                cpu.write_blob(new Uint8Array(initrd), ramdisk_address);\n            }\n\n            cpu.write32(multiboot_info_addr, info);\n\n            // set state for multiboot\n\n            cpu.reg32[REG_EBX] = multiboot_info_addr;\n            cpu.cr[0] = 1;\n            cpu.protected_mode[0] = +true;\n            cpu.flags[0] = FLAGS_DEFAULT;\n            cpu.is_32[0] = +true;\n            cpu.stack_size_32[0] = +true;\n\n            for(var i = 0; i < 6; i++)\n            {\n                cpu.segment_is_null[i] = 0;\n                cpu.segment_offsets[i] = 0;\n                cpu.segment_limits[i] = 0xFFFFFFFF;\n                // cpu.segment_access_bytes[i]\n                // Value doesn't matter, OS isn't allowed to reload without setting\n                // up a proper GDT\n                cpu.sreg[i] = 0xB002;\n            }\n            cpu.instruction_pointer[0] = cpu.get_seg_cs() + entrypoint | 0;\n            cpu.update_state_flags();\n            dbg_log(\"Starting multiboot kernel at:\", LOG_CPU);\n            cpu.dump_state();\n            cpu.dump_regs_short();\n\n            return MULTIBOOT_BOOTLOADER_MAGIC;\n        });\n\n        // only for kvm-unit-test\n        this.io.register_write_consecutive(0xF4, this,\n            function(value)\n            {\n                console.log(\"Test exited with code \" + h(value, 2));\n                throw \"HALT\";\n            },\n            function() {},\n            function() {},\n            function() {});\n\n        // only for kvm-unit-test\n        for(let i = 0; i <= 0xF; i++)\n        {\n            function handle_write(value)\n            {\n                dbg_log(\"kvm-unit-test: Set irq \" + h(i) + \" to \" + h(value, 2));\n                if(value)\n                {\n                    this.device_raise_irq(i);\n                }\n                else\n                {\n                    this.device_lower_irq(i);\n                }\n            }\n\n            this.io.register_write(0x2000 + i, this, handle_write, handle_write, handle_write);\n        }\n\n        // This rom will be executed by seabios after its initialisation\n        // It sets up the multiboot environment.\n        const SIZE = 0x200;\n\n        const data8 = new Uint8Array(SIZE);\n        const data16 = new Uint16Array(data8.buffer);\n\n        data16[0] = 0xAA55;\n        data8[2] = SIZE / 0x200;\n        let i = 3;\n        // trigger load\n        data8[i++] = 0x66; // in 0xF4\n        data8[i++] = 0xE5;\n        data8[i++] = 0xF4;\n\n        dbg_assert(i < SIZE);\n\n        const checksum_index = i;\n        data8[checksum_index] = 0;\n\n        let rom_checksum = 0;\n\n        for(let i = 0; i < data8.length; i++)\n        {\n            rom_checksum += data8[i];\n        }\n\n        data8[checksum_index] = -rom_checksum;\n\n        return {\n            name: \"genroms/multiboot.bin\",\n            data: data8\n        };\n    }\n    dbg_log(\"Multiboot header not found\", LOG_CPU);\n};\n\nCPU.prototype.fill_cmos = function(rtc, settings)\n{\n    var boot_order = settings.boot_order || BOOT_ORDER_CD_FIRST;\n\n    // Used by seabios to determine the boot order\n    //   Nibble\n    //   1: FloppyPrio\n    //   2: HDPrio\n    //   3: CDPrio\n    //   4: BEVPrio\n    // bootflag 1, high nibble, lowest priority\n    // Low nibble: Disable floppy signature check (1)\n    rtc.cmos_write(CMOS_BIOS_BOOTFLAG1 , 1 | boot_order >> 4 & 0xF0);\n\n    // bootflag 2, both nibbles, high and middle priority\n    rtc.cmos_write(CMOS_BIOS_BOOTFLAG2, boot_order & 0xFF);\n\n    // 640k or less if less memory is used\n    rtc.cmos_write(CMOS_MEM_BASE_LOW, 640 & 0xFF);\n    rtc.cmos_write(CMOS_MEM_BASE_HIGH, 640 >> 8);\n\n    var memory_above_1m = 0; // in k\n    if(this.memory_size[0] >= 1024 * 1024)\n    {\n        memory_above_1m = (this.memory_size[0] - 1024 * 1024) >> 10;\n        memory_above_1m = Math.min(memory_above_1m, 0xFFFF);\n    }\n\n    rtc.cmos_write(CMOS_MEM_OLD_EXT_LOW, memory_above_1m & 0xFF);\n    rtc.cmos_write(CMOS_MEM_OLD_EXT_HIGH, memory_above_1m >> 8 & 0xFF);\n    rtc.cmos_write(CMOS_MEM_EXTMEM_LOW, memory_above_1m & 0xFF);\n    rtc.cmos_write(CMOS_MEM_EXTMEM_HIGH, memory_above_1m >> 8 & 0xFF);\n\n    var memory_above_16m = 0; // in 64k blocks\n    if(this.memory_size[0] >= 16 * 1024 * 1024)\n    {\n        memory_above_16m = (this.memory_size[0] - 16 * 1024 * 1024) >> 16;\n        memory_above_16m = Math.min(memory_above_16m, 0xFFFF);\n    }\n    rtc.cmos_write(CMOS_MEM_EXTMEM2_LOW, memory_above_16m & 0xFF);\n    rtc.cmos_write(CMOS_MEM_EXTMEM2_HIGH, memory_above_16m >> 8 & 0xFF);\n\n    // memory above 4G (not supported by this emulator)\n    rtc.cmos_write(CMOS_MEM_HIGHMEM_LOW, 0);\n    rtc.cmos_write(CMOS_MEM_HIGHMEM_MID, 0);\n    rtc.cmos_write(CMOS_MEM_HIGHMEM_HIGH, 0);\n\n    rtc.cmos_write(CMOS_EQUIPMENT_INFO, 0x2F);\n\n    rtc.cmos_write(CMOS_BIOS_SMP_COUNT, 0);\n\n    // Used by bochs BIOS to skip the boot menu delay.\n    if(settings.fastboot) rtc.cmos_write(0x3f, 0x01);\n};\n\nCPU.prototype.load_bios = function()\n{\n    var bios = this.bios.main;\n    var vga_bios = this.bios.vga;\n\n    if(!bios)\n    {\n        dbg_log(\"Warning: No BIOS\");\n        return;\n    }\n\n    dbg_assert(bios instanceof ArrayBuffer);\n\n    // load bios\n    var data = new Uint8Array(bios),\n        start = 0x100000 - bios.byteLength;\n\n    this.write_blob(data, start);\n\n    if(vga_bios)\n    {\n        dbg_assert(vga_bios instanceof ArrayBuffer);\n\n        // load vga bios\n        var vga_bios8 = new Uint8Array(vga_bios);\n\n        // older versions of seabios\n        this.write_blob(vga_bios8, 0xC0000);\n\n        // newer versions of seabios (needs to match pci rom address, see vga.js)\n        this.io.mmap_register(0xFEB00000, 0x100000,\n            function(addr)\n            {\n                addr = (addr - 0xFEB00000) | 0;\n                if(addr < vga_bios8.length)\n                {\n                    return vga_bios8[addr];\n                }\n                else\n                {\n                    return 0;\n                }\n            },\n            function(addr, value)\n            {\n                dbg_assert(false, \"Unexpected write to VGA rom\");\n            });\n    }\n    else\n    {\n        dbg_log(\"Warning: No VGA BIOS\");\n    }\n\n    // seabios expects the bios to be mapped to 0xFFF00000 also\n    this.io.mmap_register(0xFFF00000, 0x100000,\n        function(addr)\n        {\n            addr &= 0xFFFFF;\n            return this.mem8[addr];\n        }.bind(this),\n        function(addr, value)\n        {\n            addr &= 0xFFFFF;\n            this.mem8[addr] = value;\n        }.bind(this));\n};\n\nCPU.prototype.codegen_finalize = function(wasm_table_index, start, state_flags, ptr, len)\n{\n    ptr >>>= 0;\n    len >>>= 0;\n\n    dbg_assert(wasm_table_index >= 0 && wasm_table_index < WASM_TABLE_SIZE);\n\n    const code = new Uint8Array(this.wasm_memory.buffer, ptr, len);\n\n    if(DEBUG)\n    {\n        if(DUMP_GENERATED_WASM && !this.seen_code[start])\n        {\n            this.dump_wasm(code);\n\n            const DUMP_ASSEMBLY = false;\n\n            if(DUMP_ASSEMBLY)\n            {\n                let end = 0;\n\n                if((start ^ end) & ~0xFFF)\n                {\n                    dbg_log(\"truncated disassembly start=\" + h(start >>> 0) + \" end=\" + h(end >>> 0));\n                    end = (start | 0xFFF) + 1; // until the end of the page\n                }\n\n                dbg_assert(end >= start);\n\n                const buffer = new Uint8Array(end - start);\n\n                for(let i = start; i < end; i++)\n                {\n                    buffer[i - start] = this.read8(i);\n                }\n\n                this.debug_dump_code(this.is_32[0] ? 1 : 0, buffer, start);\n            }\n        }\n\n        this.seen_code[start] = (this.seen_code[start] || 0) + 1;\n\n        if(this.test_hook_did_generate_wasm)\n        {\n            this.test_hook_did_generate_wasm(code);\n        }\n    }\n\n    const SYNC_COMPILATION = false;\n\n    if(SYNC_COMPILATION)\n    {\n        const module = new WebAssembly.Module(code);\n        const result = new WebAssembly.Instance(module, { \"e\": this.jit_imports });\n        const f = result.exports[\"f\"];\n\n        this.wm.wasm_table.set(wasm_table_index + WASM_TABLE_OFFSET, f);\n        this.codegen_finalize_finished(wasm_table_index, start, state_flags);\n\n        if(this.test_hook_did_finalize_wasm)\n        {\n            this.test_hook_did_finalize_wasm(code);\n        }\n\n        return;\n    }\n\n    const result = WebAssembly.instantiate(code, { \"e\": this.jit_imports }).then(result => {\n        const f = result.instance.exports[\"f\"];\n\n        this.wm.wasm_table.set(wasm_table_index + WASM_TABLE_OFFSET, f);\n        this.codegen_finalize_finished(wasm_table_index, start, state_flags);\n\n        if(this.test_hook_did_finalize_wasm)\n        {\n            this.test_hook_did_finalize_wasm(code);\n        }\n    });\n\n    if(DEBUG)\n    {\n        result.catch(e => {\n            console.log(e);\n            debugger;\n            throw e;\n        });\n    }\n};\n\nCPU.prototype.log_uncompiled_code = function(start, end)\n{\n    if(!DEBUG || !DUMP_UNCOMPILED_ASSEMBLY)\n    {\n        return;\n    }\n\n    if((this.seen_code_uncompiled[start] || 0) < 100)\n    {\n        this.seen_code_uncompiled[start] = (this.seen_code_uncompiled[start] || 0) + 1;\n\n        end += 8; // final jump is not included\n\n        if((start ^ end) & ~0xFFF)\n        {\n            dbg_log(\"truncated disassembly start=\" + h(start >>> 0) + \" end=\" + h(end >>> 0));\n            end = (start | 0xFFF) + 1; // until the end of the page\n        }\n\n        if(end < start) end = start;\n\n        dbg_assert(end >= start);\n\n        const buffer = new Uint8Array(end - start);\n\n        for(let i = start; i < end; i++)\n        {\n            buffer[i - start] = this.read8(i);\n        }\n\n        dbg_log(\"Uncompiled code:\");\n        this.debug_dump_code(this.is_32[0] ? 1 : 0, buffer, start);\n    }\n};\n\nCPU.prototype.dump_function_code = function(block_ptr, count)\n{\n    if(!DEBUG || !DUMP_GENERATED_WASM)\n    {\n        return;\n    }\n\n    const SIZEOF_BASIC_BLOCK_IN_DWORDS = 7;\n\n    const mem32 = new Int32Array(this.wasm_memory.buffer);\n\n    dbg_assert((block_ptr & 3) === 0);\n\n    const is_32 = this.is_32[0];\n\n    for(let i = 0; i < count; i++)\n    {\n        const struct_start = (block_ptr >> 2) + i * SIZEOF_BASIC_BLOCK_IN_DWORDS;\n        const start = mem32[struct_start + 0];\n        const end = mem32[struct_start + 1];\n        const is_entry_block = mem32[struct_start + 6] & 0xFF00;\n\n        const buffer = new Uint8Array(end - start);\n\n        for(let i = start; i < end; i++)\n        {\n            buffer[i - start] = this.read8(this.translate_address_system_read(i));\n        }\n\n        dbg_log(\"---\" + (is_entry_block ? \" entry\" : \"\"));\n        this.debug_dump_code(is_32 ? 1 : 0, buffer, start);\n    }\n};\n\nCPU.prototype.run_hardware_timers = function(acpi_enabled, now)\n{\n    const pit_time = this.devices.pit.timer(now, false);\n    const rtc_time = this.devices.rtc.timer(now, false);\n\n    let acpi_time = 100;\n    let apic_time = 100;\n    if(acpi_enabled)\n    {\n        acpi_time = this.devices.acpi.timer(now);\n        apic_time = this.apic_timer(now);\n    }\n\n    return Math.min(pit_time, rtc_time, acpi_time, apic_time);\n};\n\nCPU.prototype.debug_init = function()\n{\n    if(!DEBUG) return;\n\n    if(this.io)\n    {\n        // write seabios debug output to console\n        var seabios_debug = \"\";\n\n        this.io.register_write(0x402, this, handle); // seabios\n        this.io.register_write(0x500, this, handle); // vgabios\n    }\n\n    function handle(out_byte)\n    {\n        if(out_byte === 10)\n        {\n            dbg_log(seabios_debug, LOG_BIOS);\n            seabios_debug = \"\";\n        }\n        else\n        {\n            seabios_debug += String.fromCharCode(out_byte);\n        }\n    }\n};\n\nCPU.prototype.dump_stack = function(start, end)\n{\n    if(!DEBUG) return;\n\n    var esp = this.reg32[REG_ESP];\n    dbg_log(\"========= STACK ==========\");\n\n    if(end >= start || end === undefined)\n    {\n        start = 5;\n        end = -5;\n    }\n\n    for(var i = start; i > end; i--)\n    {\n        var line = \"    \";\n\n        if(!i) line = \"=>  \";\n\n        line += h(i, 2) + \" | \";\n\n        dbg_log(line + h(esp + 4 * i, 8) + \" | \" + h(this.read32s(esp + 4 * i) >>> 0));\n    }\n};\n\n/** @param {string=} where */\nCPU.prototype.debug_get_state = function(where)\n{\n    if(!DEBUG) return;\n\n    var mode = this.protected_mode[0] ? \"prot\" : \"real\";\n    var vm = (this.flags[0] & FLAG_VM) ? 1 : 0;\n    var flags = this.get_eflags();\n    var iopl = this.getiopl();\n    var cpl = this.cpl[0];\n    var cs_eip = h(this.sreg[REG_CS], 4) + \":\" + h(this.get_real_eip() >>> 0, 8);\n    var ss_esp = h(this.sreg[REG_SS], 4) + \":\" + h(this.reg32[REG_ES] >>> 0, 8);\n    var op_size = this.is_32[0] ? \"32\" : \"16\";\n    var if_ = (this.flags[0] & FLAG_INTERRUPT) ? 1 : 0;\n\n    var flag_names = {\n        [FLAG_CARRY]: \"c\",\n        [FLAG_PARITY]: \"p\",\n        [FLAG_ADJUST]: \"a\",\n        [FLAG_ZERO]: \"z\",\n        [FLAG_SIGN]: \"s\",\n        [FLAG_TRAP]: \"t\",\n        [FLAG_INTERRUPT]: \"i\",\n        [FLAG_DIRECTION]: \"d\",\n        [FLAG_OVERFLOW]: \"o\",\n    };\n    var flag_string = \"\";\n\n    for(var i = 0; i < 16; i++)\n    {\n        if(flag_names[1 << i])\n        {\n            if(flags & 1 << i)\n            {\n                flag_string += flag_names[1 << i];\n            }\n            else\n            {\n                flag_string += \" \";\n            }\n        }\n    }\n\n    return (\"mode=\" + mode + \"/\" + op_size + \" paging=\" + (+((this.cr[0] & CR0_PG) !== 0)) +\n        \" pae=\" + (+((this.cr[4] & CR4_PAE) !== 0)) +\n        \" iopl=\" + iopl + \" cpl=\" + cpl + \" if=\" + if_ + \" cs:eip=\" + cs_eip +\n        \" cs_off=\" + h(this.get_seg_cs() >>> 0, 8) +\n        \" flgs=\" + h(this.get_eflags() >>> 0, 6) + \" (\" + flag_string + \")\" +\n        \" ss:esp=\" + ss_esp +\n        \" ssize=\" + (+this.stack_size_32[0]) +\n        (where ? \" in \" + where : \"\"));\n};\n\n/** @param {string=} where */\nCPU.prototype.dump_state = function(where)\n{\n    if(!DEBUG) return;\n\n    dbg_log(this.debug_get_state(where), LOG_CPU);\n};\n\nCPU.prototype.get_regs_short = function()\n{\n    if(!DEBUG) return;\n\n    var\n    r32 = { \"eax\": REG_EAX, \"ecx\": REG_ECX, \"edx\": REG_EDX, \"ebx\": REG_EBX,\n        \"esp\": REG_ESP, \"ebp\": REG_EBP, \"esi\": REG_ESI, \"edi\": REG_EDI },\n        r32_names = [\"eax\", \"ecx\", \"edx\", \"ebx\", \"esp\", \"ebp\", \"esi\", \"edi\"],\n        s = { \"cs\": REG_CS, \"ds\": REG_DS, \"es\": REG_ES, \"fs\": REG_FS, \"gs\": REG_GS, \"ss\": REG_SS },\n        line1 = \"\",\n        line2 = \"\";\n\n    for(var i = 0; i < 4; i++)\n    {\n        line1 += r32_names[i] + \"=\"  + h(this.reg32[r32[r32_names[i]]] >>> 0, 8) + \" \";\n        line2 += r32_names[i+4] + \"=\"  + h(this.reg32[r32[r32_names[i+4]]] >>> 0, 8) + \" \";\n    }\n\n    //line1 += \" eip=\" + h(this.get_real_eip() >>> 0, 8);\n    //line2 += \" flg=\" + h(this.get_eflags(), 8);\n\n    line1 += \"  ds=\" + h(this.sreg[REG_DS], 4) + \" es=\" + h(this.sreg[REG_ES], 4) + \" fs=\" + h(this.sreg[REG_FS], 4);\n    line2 += \"  gs=\" + h(this.sreg[REG_GS], 4) + \" cs=\" + h(this.sreg[REG_CS], 4) + \" ss=\" + h(this.sreg[REG_SS], 4);\n\n    return [line1, line2];\n};\n\nCPU.prototype.dump_regs_short = function()\n{\n    if(!DEBUG) return;\n\n    var lines = this.get_regs_short();\n\n    dbg_log(lines[0], LOG_CPU);\n    dbg_log(lines[1], LOG_CPU);\n};\n\nCPU.prototype.dump_gdt_ldt = function()\n{\n    if(!DEBUG) return;\n\n    dbg_log(\"gdt: (len = \" + h(this.gdtr_size[0]) + \")\");\n    dump_table(this.translate_address_system_read(this.gdtr_offset[0]), this.gdtr_size[0]);\n\n    dbg_log(\"\\nldt: (len = \" + h(this.segment_limits[REG_LDTR]) + \")\");\n    dump_table(this.translate_address_system_read(this.segment_offsets[REG_LDTR]), this.segment_limits[REG_LDTR]);\n\n    function dump_table(addr, size)\n    {\n        for(var i = 0; i < size; i += 8, addr += 8)\n        {\n            var base = this.read16(addr + 2) |\n                this.read8(addr + 4) << 16 |\n                this.read8(addr + 7) << 24,\n\n                limit = this.read16(addr) | (this.read8(addr + 6) & 0xF) << 16,\n                access = this.read8(addr + 5),\n                flags = this.read8(addr + 6) >> 4,\n                flags_str = \"\",\n                dpl = access >> 5 & 3;\n\n            if(!(access & 128))\n            {\n                // present bit not set\n                //continue;\n                flags_str += \"NP \";\n            }\n            else\n            {\n                flags_str += \" P \";\n            }\n\n            if(access & 16)\n            {\n                if(flags & 4)\n                {\n                    flags_str += \"32b \";\n                }\n                else\n                {\n                    flags_str += \"16b \";\n                }\n\n                if(access & 8)\n                {\n                    // executable\n                    flags_str += \"X \";\n\n                    if(access & 4)\n                    {\n                        flags_str += \"C \";\n                    }\n                }\n                else\n                {\n                    // data\n                    flags_str += \"R \";\n                }\n\n                flags_str += \"RW \";\n            }\n            else\n            {\n                // system\n                flags_str += \"sys: \" + h(access & 15);\n            }\n\n            if(flags & 8)\n            {\n                limit = limit << 12 | 0xFFF;\n            }\n\n            dbg_log(h(i & ~7, 4) + \" \" + h(base >>> 0, 8) + \" (\" + h(limit >>> 0, 8) + \" bytes) \" +\n                flags_str + \";  dpl = \" + dpl + \", a = \" + access.toString(2) +\n                \", f = \" + flags.toString(2));\n        }\n    }\n};\n\nCPU.prototype.dump_idt = function()\n{\n    if(!DEBUG) return;\n\n    for(var i = 0; i < this.idtr_size[0]; i += 8)\n    {\n        var addr = this.translate_address_system_read(this.idtr_offset[0] + i),\n            base = this.read16(addr) | this.read16(addr + 6) << 16,\n            selector = this.read16(addr + 2),\n            type = this.read8(addr + 5),\n            line,\n            dpl = type >> 5 & 3;\n\n        if((type & 31) === 5)\n        {\n            line = \"task gate \";\n        }\n        else if((type & 31) === 14)\n        {\n            line = \"intr gate \";\n        }\n        else if((type & 31) === 15)\n        {\n            line = \"trap gate \";\n        }\n        else\n        {\n            line = \"invalid   \";\n        }\n\n\n        if(type & 128)\n        {\n            line += \" P\";\n        }\n        else\n        {\n            // present bit not set\n            //continue;\n            line += \"NP\";\n        }\n\n\n        dbg_log(h(i >> 3, 4) + \" \" + h(base >>> 0, 8) + \", \" +\n            h(selector, 4) + \"; \" + line + \";  dpl = \" + dpl + \", t = \" + type.toString(2));\n    }\n};\n\nCPU.prototype.dump_page_structures = function()\n{\n    var pae = !!(this.cr[4] & CR4_PAE);\n    if(pae)\n    {\n        dbg_log(\"PAE enabled\");\n\n        for(var i = 0; i < 4; i++) {\n            var addr = this.cr[3] + 8 * i;\n            var dword = this.read32s(addr);\n            if(dword & 1)\n            {\n                this.dump_page_directory(dword & 0xFFFFF000, true, i << 30);\n            }\n        }\n    }\n    else\n    {\n        dbg_log(\"PAE disabled\");\n        this.dump_page_directory(this.cr[3], false, 0);\n    }\n};\n\n// NOTE: PAE entries are 64-bits, we ignore the high half here.\nCPU.prototype.dump_page_directory = function(pd_addr, pae, start)\n{\n    if(!DEBUG) return;\n\n    function load_page_entry(dword_entry, pae, is_directory)\n    {\n        if(!DEBUG) return;\n\n        if(!(dword_entry & 1))\n        {\n            // present bit not set\n            return false;\n        }\n\n        var size = (dword_entry & 128) === 128,\n            address;\n\n        if(size && !is_directory)\n        {\n            address = dword_entry & (pae ? 0xFFE00000 : 0xFFC00000);\n        }\n        else\n        {\n            address = dword_entry & 0xFFFFF000;\n        }\n\n        return {\n            size: size,\n            global: (dword_entry & 256) === 256,\n            accessed: (dword_entry & 0x20) === 0x20,\n            dirty: (dword_entry & 0x40) === 0x40,\n            cache_disable : (dword_entry & 16) === 16,\n            user : (dword_entry & 4) === 4,\n            read_write : (dword_entry & 2) === 2,\n            address : address >>> 0\n        };\n    }\n\n    var n = pae ? 512 : 1024;\n    var entry_size = pae ? 8 : 4;\n    var pd_shift = pae ? 21 : 22;\n\n    for(var i = 0; i < n; i++)\n    {\n        var addr = pd_addr + i * entry_size,\n            dword = this.read32s(addr),\n            entry = load_page_entry(dword, pae, true);\n\n        if(!entry)\n        {\n            continue;\n        }\n\n        var flags = \"\";\n\n        flags += entry.size ? \"S \" : \"  \";\n        flags += entry.accessed ? \"A \" : \"  \";\n        flags += entry.cache_disable ? \"Cd \" : \"  \";\n        flags += entry.user ? \"U \" : \"  \";\n        flags += entry.read_write ? \"Rw \" : \"   \";\n\n        if(entry.size)\n        {\n            dbg_log(\"=== \" + h(start + (i << pd_shift) >>> 0, 8) + \" -> \" +\n                h(entry.address >>> 0, 8) + \" | \" + flags);\n            continue;\n        }\n        else\n        {\n            dbg_log(\"=== \" + h(start + (i << pd_shift) >>> 0, 8) + \" | \" + flags);\n        }\n\n        for(var j = 0; j < n; j++)\n        {\n            var sub_addr = entry.address + j * entry_size;\n            dword = this.read32s(sub_addr);\n\n            var subentry = load_page_entry(dword, pae, false);\n\n            if(subentry)\n            {\n                flags = \"\";\n\n                flags += subentry.cache_disable ? \"Cd \" : \"   \";\n                flags += subentry.user ? \"U \" : \"  \";\n                flags += subentry.read_write ? \"Rw \" : \"   \";\n                flags += subentry.global ? \"G \" : \"  \";\n                flags += subentry.accessed ? \"A \" : \"  \";\n                flags += subentry.dirty ? \"Di \" : \"   \";\n\n                dbg_log(\"# \" + h(start + (i << pd_shift | j << 12) >>> 0, 8) + \" -> \" +\n                    h(subentry.address, 8) + \" | \" + flags + \"        (at \" + h(sub_addr, 8) + \")\");\n            }\n        }\n    }\n};\n\nCPU.prototype.get_memory_dump = function(start, count)\n{\n    if(!DEBUG) return;\n\n    if(start === undefined)\n    {\n        start = 0;\n        count = this.memory_size[0];\n    }\n    else if(count === undefined)\n    {\n        count = start;\n        start = 0;\n    }\n\n    return this.mem8.slice(start, start + count).buffer;\n};\n\nCPU.prototype.memory_hex_dump = function(addr, length)\n{\n    if(!DEBUG) return;\n\n    length = length || 4 * 0x10;\n    var line, byt;\n\n    for(var i = 0; i < length >> 4; i++)\n    {\n        line = h(addr + (i << 4), 5) + \"   \";\n\n        for(var j = 0; j < 0x10; j++)\n        {\n            byt = this.read8(addr + (i << 4) + j);\n            line += h(byt, 2) + \" \";\n        }\n\n        line += \"  \";\n\n        for(j = 0; j < 0x10; j++)\n        {\n            byt = this.read8(addr + (i << 4) + j);\n            line += (byt < 33 || byt > 126) ? \".\" : String.fromCharCode(byt);\n        }\n\n        dbg_log(line);\n    }\n};\n\nCPU.prototype.used_memory_dump = function()\n{\n    if(!DEBUG) return;\n\n    var width = 0x80,\n        height = 0x10,\n        block_size = this.memory_size[0] / width / height | 0,\n        row;\n\n    for(var i = 0; i < height; i++)\n    {\n        row = h(i * width * block_size, 8) + \" | \";\n\n        for(var j = 0; j < width; j++)\n        {\n            var used = this.mem32s[(i * width + j) * block_size] > 0;\n\n            row += used ? \"X\" : \" \";\n        }\n\n        dbg_log(row);\n    }\n};\n\nCPU.prototype.debug_interrupt = function(interrupt_nr)\n{\n    //if(interrupt_nr === 0x20)\n    //{\n    //    //var vxd_device = this.safe_read16(this.instruction_pointer + 2);\n    //    //var vxd_sub = this.safe_read16(this.instruction_pointer + 0);\n    //    //var service = \"\";\n    //    //if(vxd_device === 1)\n    //    //{\n    //    //    service = vxd_table1[vxd_sub];\n    //    //}\n    //    //dbg_log(\"vxd: \" + h(vxd_device, 4) + \" \" + h(vxd_sub, 4) + \" \" + service);\n    //}\n\n    //if(interrupt_nr >= 0x21 && interrupt_nr < 0x30)\n    //{\n    //    dbg_log(\"dos: \" + h(interrupt_nr, 2) + \" ah=\" + h(this.reg8[reg_ah], 2) + \" ax=\" + h(this.reg16[reg_ax], 4));\n    //}\n\n    //if(interrupt_nr === 0x13 && (this.reg8[reg_ah] | 1) === 0x43)\n    //{\n    //    this.debug.memory_hex_dump(this.get_seg(reg_ds) + this.reg16[reg_si], 0x18);\n    //}\n\n    //if(interrupt_nr == 0x10)\n    //{\n    //    dbg_log(\"int10 ax=\" + h(this.reg16[reg_ax], 4) + \" '\" + String.fromCharCode(this.reg8[reg_al]) + \"'\");\n    //    this.debug.dump_regs_short();\n    //    if(this.reg8[reg_ah] == 0xe) vga.tt_write(this.reg8[reg_al]);\n    //}\n\n    //if(interrupt_nr === 0x13)\n    //{\n    //    this.debug.dump_regs_short();\n    //}\n\n    //if(interrupt_nr === 6)\n    //{\n    //    this.instruction_pointer += 2;\n    //    dbg_log(\"BUG()\", LOG_CPU);\n    //    dbg_log(\"line=\" + this.read_imm16() + \" \" +\n    //            \"file=\" + this.read_string(this.translate_address_read(this.read_imm32s())), LOG_CPU);\n    //    this.instruction_pointer -= 8;\n    //    this.debug.dump_regs_short();\n    //}\n\n    //if(interrupt_nr === 0x80)\n    //{\n    //    dbg_log(\"linux syscall\");\n    //    this.debug.dump_regs_short();\n    //}\n\n    //if(interrupt_nr === 0x40)\n    //{\n    //    dbg_log(\"kolibri syscall\");\n    //    this.debug.dump_regs_short();\n    //}\n};\n\nCPU.prototype.debug_dump_code = function(is_32, buffer, start)\n{\n    if(!DEBUG) return;\n\n    if(!this.capstone_decoder)\n    {\n        let cs = window.cs;\n\n        /* global require */\n        if(typeof require === \"function\")\n        {\n            cs = require(\"./capstone-x86.min.js\");\n        }\n\n        if(cs === undefined)\n        {\n            dbg_log(\"Warning: Missing capstone library, disassembly not available\");\n            return;\n        }\n\n        this.capstone_decoder = [\n            new cs.Capstone(cs.ARCH_X86, cs.MODE_16),\n            new cs.Capstone(cs.ARCH_X86, cs.MODE_32),\n        ];\n    }\n\n    if(buffer instanceof Array)\n    {\n        buffer = new Uint8Array(buffer);\n    }\n\n    try\n    {\n        const instructions = this.capstone_decoder[+is_32].disasm(buffer, start);\n\n        instructions.forEach(function (instr) {\n            dbg_log(h(instr.address >>> 0) + \": \" +\n                pads(instr.bytes.map(x => h(x, 2).slice(-2)).join(\" \"), 20) + \" \" +\n                instr.mnemonic + \" \" + instr.op_str);\n        });\n        dbg_log(\"\");\n    }\n    catch(e)\n    {\n        dbg_log(\"Could not disassemble: \" + Array.from(buffer).map(x => h(x, 2)).join(\" \"));\n    }\n};\n\nCPU.prototype.dump_wasm = function(buffer)\n{\n    if(!DEBUG) return;\n\n    /* global require */\n    if(this.wabt === undefined)\n    {\n        if(typeof require === \"function\")\n        {\n            this.wabt = require(\"./libwabt.cjs\");\n        }\n        else\n        {\n            this.wabt = new window.WabtModule;\n        }\n\n        if(this.wabt === undefined)\n        {\n            dbg_log(\"Warning: Missing libwabt, wasm dump not available\");\n            return;\n        }\n    }\n\n    // Need to make a small copy otherwise libwabt goes nuts trying to copy\n    // the whole underlying buffer\n    buffer = buffer.slice();\n\n    try\n    {\n        var module = this.wabt.readWasm(buffer, { readDebugNames: false });\n        module.generateNames();\n        module.applyNames();\n        const result = module.toText({ foldExprs: true, inlineExport: true });\n        dbg_log(result);\n    }\n    catch(e)\n    {\n        dump_file(buffer, \"failed.wasm\");\n        console.log(e.toString());\n    }\n    finally\n    {\n        if(module)\n        {\n            module.destroy();\n        }\n    }\n};\n"
  },
  {
    "path": "src/dma.js",
    "content": "import { LOG_DMA } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\n\n/**\n * @constructor\n * @param {CPU} cpu\n */\nexport function DMA(cpu)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    this.channel_page = new Uint8Array(8);\n    this.channel_pagehi = new Uint8Array(8);\n    this.channel_addr = new Uint16Array(8);\n    this.channel_addr_init = new Uint16Array(8);\n    this.channel_count = new Uint16Array(8);\n    this.channel_count_init = new Uint16Array(8);\n    this.channel_mask = new Uint8Array(8);\n    this.channel_mode = new Uint8Array(8);\n    this.unmask_listeners = [];\n\n    this.lsb_msb_flipflop = 0;\n\n    var io = cpu.io;\n\n    io.register_write(0x00, this, this.port_addr_write.bind(this, 0));\n    io.register_write(0x02, this, this.port_addr_write.bind(this, 1));\n    io.register_write(0x04, this, this.port_addr_write.bind(this, 2));\n    io.register_write(0x06, this, this.port_addr_write.bind(this, 3));\n    io.register_write(0x01, this, this.port_count_write.bind(this, 0));\n    io.register_write(0x03, this, this.port_count_write.bind(this, 1));\n    io.register_write(0x05, this, this.port_count_write.bind(this, 2));\n    io.register_write(0x07, this, this.port_count_write.bind(this, 3));\n\n    io.register_read(0x00, this, this.port_addr_read.bind(this, 0));\n    io.register_read(0x02, this, this.port_addr_read.bind(this, 1));\n    io.register_read(0x04, this, this.port_addr_read.bind(this, 2));\n    io.register_read(0x06, this, this.port_addr_read.bind(this, 3));\n    io.register_read(0x01, this, this.port_count_read.bind(this, 0));\n    io.register_read(0x03, this, this.port_count_read.bind(this, 1));\n    io.register_read(0x05, this, this.port_count_read.bind(this, 2));\n    io.register_read(0x07, this, this.port_count_read.bind(this, 3));\n\n    io.register_write(0xC0, this, this.port_addr_write.bind(this, 4));\n    io.register_write(0xC4, this, this.port_addr_write.bind(this, 5));\n    io.register_write(0xC8, this, this.port_addr_write.bind(this, 6));\n    io.register_write(0xCC, this, this.port_addr_write.bind(this, 7));\n    io.register_write(0xC2, this, this.port_count_write.bind(this, 4));\n    io.register_write(0xC6, this, this.port_count_write.bind(this, 5));\n    io.register_write(0xCA, this, this.port_count_write.bind(this, 6));\n    io.register_write(0xCE, this, this.port_count_write.bind(this, 7));\n\n    io.register_read(0xC0, this, this.port_addr_read.bind(this, 4));\n    io.register_read(0xC4, this, this.port_addr_read.bind(this, 5));\n    io.register_read(0xC8, this, this.port_addr_read.bind(this, 6));\n    io.register_read(0xCC, this, this.port_addr_read.bind(this, 7));\n    io.register_read(0xC2, this, this.port_count_read.bind(this, 4));\n    io.register_read(0xC6, this, this.port_count_read.bind(this, 5));\n    io.register_read(0xCA, this, this.port_count_read.bind(this, 6));\n    io.register_read(0xCE, this, this.port_count_read.bind(this, 7));\n\n    io.register_write(0x87, this, this.port_page_write.bind(this, 0));\n    io.register_write(0x83, this, this.port_page_write.bind(this, 1));\n    io.register_write(0x81, this, this.port_page_write.bind(this, 2));\n    io.register_write(0x82, this, this.port_page_write.bind(this, 3));\n    io.register_write(0x8F, this, this.port_page_write.bind(this, 4));\n    io.register_write(0x8B, this, this.port_page_write.bind(this, 5));\n    io.register_write(0x89, this, this.port_page_write.bind(this, 6));\n    io.register_write(0x8A, this, this.port_page_write.bind(this, 7));\n\n    io.register_read(0x87, this, this.port_page_read.bind(this, 0));\n    io.register_read(0x83, this, this.port_page_read.bind(this, 1));\n    io.register_read(0x81, this, this.port_page_read.bind(this, 2));\n    io.register_read(0x82, this, this.port_page_read.bind(this, 3));\n    io.register_read(0x8F, this, this.port_page_read.bind(this, 4));\n    io.register_read(0x8B, this, this.port_page_read.bind(this, 5));\n    io.register_read(0x89, this, this.port_page_read.bind(this, 6));\n    io.register_read(0x8A, this, this.port_page_read.bind(this, 7));\n\n    io.register_write(0x487, this, this.port_pagehi_write.bind(this, 0));\n    io.register_write(0x483, this, this.port_pagehi_write.bind(this, 1));\n    io.register_write(0x481, this, this.port_pagehi_write.bind(this, 2));\n    io.register_write(0x482, this, this.port_pagehi_write.bind(this, 3));\n    io.register_write(0x48B, this, this.port_pagehi_write.bind(this, 5));\n    io.register_write(0x489, this, this.port_pagehi_write.bind(this, 6));\n    io.register_write(0x48A, this, this.port_pagehi_write.bind(this, 7));\n\n    io.register_read(0x487, this, this.port_pagehi_read.bind(this, 0));\n    io.register_read(0x483, this, this.port_pagehi_read.bind(this, 1));\n    io.register_read(0x481, this, this.port_pagehi_read.bind(this, 2));\n    io.register_read(0x482, this, this.port_pagehi_read.bind(this, 3));\n    io.register_read(0x48B, this, this.port_pagehi_read.bind(this, 5));\n    io.register_read(0x489, this, this.port_pagehi_read.bind(this, 6));\n    io.register_read(0x48A, this, this.port_pagehi_read.bind(this, 7));\n\n    io.register_write(0x0A, this, this.port_singlemask_write.bind(this, 0));\n    io.register_write(0xD4, this, this.port_singlemask_write.bind(this, 4));\n    io.register_write(0x0F, this, this.port_multimask_write.bind(this, 0));\n    io.register_write(0xDE, this, this.port_multimask_write.bind(this, 4));\n\n    io.register_read(0x0F, this, this.port_multimask_read.bind(this, 0));\n    io.register_read(0xDE, this, this.port_multimask_read.bind(this, 4));\n\n    io.register_write(0x0B, this, this.port_mode_write.bind(this, 0));\n    io.register_write(0xD6, this, this.port_mode_write.bind(this, 4));\n\n    io.register_write(0x0C, this, this.portC_write);\n    io.register_write(0xD8, this, this.portC_write);\n}\n\nDMA.prototype.get_state = function()\n{\n    return [\n        this.channel_page,\n        this.channel_pagehi,\n        this.channel_addr,\n        this.channel_addr_init,\n        this.channel_count,\n        this.channel_count_init,\n        this.channel_mask,\n        this.channel_mode,\n        this.lsb_msb_flipflop,\n    ];\n};\n\nDMA.prototype.set_state = function(state)\n{\n    this.channel_page = state[0];\n    this.channel_pagehi = state[1];\n    this.channel_addr = state[2];\n    this.channel_addr_init = state[3];\n    this.channel_count = state[4];\n    this.channel_count_init = state[5];\n    this.channel_mask = state[6];\n    this.channel_mode = state[7];\n    this.lsb_msb_flipflop = state[8];\n};\n\nDMA.prototype.port_count_write = function(channel, data_byte)\n{\n    dbg_log(\"count write [\" + channel + \"] = \" + h(data_byte), LOG_DMA);\n\n    this.channel_count[channel] =\n        this.flipflop_get(this.channel_count[channel], data_byte, false);\n\n    this.channel_count_init[channel] =\n        this.flipflop_get(this.channel_count_init[channel], data_byte, true);\n};\n\nDMA.prototype.port_count_read = function(channel)\n{\n    dbg_log(\"count read [\" + channel + \"] -> \" + h(this.channel_count[channel]), LOG_DMA);\n    return this.flipflop_read(this.channel_count[channel]);\n};\n\nDMA.prototype.port_addr_write = function(channel, data_byte)\n{\n    dbg_log(\"addr write [\" + channel + \"] = \" + h(data_byte), LOG_DMA);\n\n    this.channel_addr[channel] =\n        this.flipflop_get(this.channel_addr[channel], data_byte, false);\n\n    this.channel_addr_init[channel] =\n        this.flipflop_get(this.channel_addr_init[channel], data_byte, true);\n};\n\nDMA.prototype.port_addr_read = function(channel)\n{\n    dbg_log(\"addr read [\" + channel + \"] -> \" + h(this.channel_addr[channel]), LOG_DMA);\n    return this.flipflop_read(this.channel_addr[channel]);\n};\n\nDMA.prototype.port_pagehi_write = function(channel, data_byte)\n{\n    dbg_log(\"pagehi write [\" + channel + \"] = \" + h(data_byte), LOG_DMA);\n    this.channel_pagehi[channel] = data_byte;\n};\n\nDMA.prototype.port_pagehi_read = function(channel)\n{\n    dbg_log(\"pagehi read [\" + channel + \"]\", LOG_DMA);\n    return this.channel_pagehi[channel];\n};\n\nDMA.prototype.port_page_write = function(channel, data_byte)\n{\n    dbg_log(\"page write [\" + channel + \"] = \" + h(data_byte), LOG_DMA);\n    this.channel_page[channel] = data_byte;\n};\n\nDMA.prototype.port_page_read = function(channel)\n{\n    dbg_log(\"page read [\" + channel + \"]\", LOG_DMA);\n    return this.channel_page[channel];\n};\n\nDMA.prototype.port_singlemask_write = function(channel_offset, data_byte)\n{\n    var channel = (data_byte & 0x3) + channel_offset;\n    var value = data_byte & 0x4 ? 1 : 0;\n    dbg_log(\"singlechannel mask write [\" + channel + \"] = \" + value, LOG_DMA);\n    this.update_mask(channel, value);\n};\n\nDMA.prototype.port_multimask_write = function(channel_offset, data_byte)\n{\n    dbg_log(\"multichannel mask write: \" + h(data_byte), LOG_DMA);\n    for(var i = 0; i < 4; i++)\n    {\n        this.update_mask(channel_offset + i, data_byte & (1 << i));\n    }\n};\n\nDMA.prototype.port_multimask_read = function(channel_offset)\n{\n    var value = 0;\n    value |= this.channel_mask[channel_offset + 0];\n    value |= this.channel_mask[channel_offset + 1] << 1;\n    value |= this.channel_mask[channel_offset + 2] << 2;\n    value |= this.channel_mask[channel_offset + 3] << 3;\n    dbg_log(\"multichannel mask read: \" + h(value), LOG_DMA);\n    return value;\n};\n\nDMA.prototype.port_mode_write = function(channel_offset, data_byte)\n{\n    var channel = (data_byte & 0x3) + channel_offset;\n    dbg_log(\"mode write [\" + channel + \"] = \" + h(data_byte), LOG_DMA);\n    this.channel_mode[channel] = data_byte;\n};\n\nDMA.prototype.portC_write = function(data_byte)\n{\n    dbg_log(\"flipflop reset\", LOG_DMA);\n    this.lsb_msb_flipflop = 0;\n};\n\nDMA.prototype.on_unmask = function(fn, this_value)\n{\n    this.unmask_listeners.push({\n        fn: fn,\n        this_value: this_value,\n    });\n};\n\nDMA.prototype.update_mask = function(channel, value)\n{\n    if(this.channel_mask[channel] !== value)\n    {\n        this.channel_mask[channel] = value;\n\n        if(!value)\n        {\n            dbg_log(\"firing on_unmask(\" + channel + \")\", LOG_DMA);\n            for(var i = 0; i < this.unmask_listeners.length; i++)\n            {\n                this.unmask_listeners[i].fn.call(\n                    this.unmask_listeners[i].this_value,\n                    channel\n                );\n            }\n        }\n    }\n};\n\n// read data, write to memory\nDMA.prototype.do_read = function(buffer, start, len, channel, fn)\n{\n    var read_count = this.count_get_8bit(channel),\n        addr = this.address_get_8bit(channel);\n\n    dbg_log(\"DMA write channel \" + channel, LOG_DMA);\n    dbg_log(\"to \" + h(addr) + \" len \" + h(read_count), LOG_DMA);\n\n    if(len < read_count)\n    {\n        dbg_log(\"DMA should read more than provided: \" + h(len) + \" \" + h(read_count), LOG_DMA);\n    }\n\n    if(start + read_count > buffer.byteLength)\n    {\n        dbg_log(\"DMA read outside of buffer\", LOG_DMA);\n        fn(true);\n    }\n    else\n    {\n        var cpu = this.cpu;\n        this.channel_addr[channel] += read_count;\n\n        buffer.get(start, read_count, function(data)\n        {\n            cpu.write_blob(data, addr);\n            fn(false);\n        });\n    }\n};\n\n// write data, read memory\n// start and len in bytes\nDMA.prototype.do_write = function(buffer, start, len, channel, fn)\n{\n    var read_count = (this.channel_count[channel] + 1) & 0xFFFF,\n        bytes_per_count = channel >= 5 ? 2 : 1,\n        read_bytes = read_count * bytes_per_count,\n        addr = this.address_get_8bit(channel),\n        unfinished = false,\n        want_more = false,\n        autoinit = this.channel_mode[channel] & 0x10;\n\n    dbg_log(\"DMA write channel \" + channel, LOG_DMA);\n    dbg_log(\"to \" + h(addr) + \" len \" + h(read_bytes), LOG_DMA);\n\n    if(len < read_bytes)\n    {\n        dbg_log(\"DMA should read more than provided\", LOG_DMA);\n        read_count = Math.floor(len / bytes_per_count);\n        read_bytes = read_count * bytes_per_count;\n        unfinished = true;\n    }\n    else if(len > read_bytes)\n    {\n        dbg_log(\"DMA attempted to read more than provided\", LOG_DMA);\n        want_more = true;\n    }\n\n    if(start + read_bytes > buffer.byteLength)\n    {\n        dbg_log(\"DMA write outside of buffer\", LOG_DMA);\n        fn(true);\n    }\n    else\n    {\n        this.channel_addr[channel] += read_count;\n        this.channel_count[channel] -= read_count;\n        // when complete, counter should underflow to 0xFFFF\n\n        if(!unfinished && autoinit)\n        {\n            dbg_log(\"DMA autoinit\", LOG_DMA);\n            this.channel_addr[channel] = this.channel_addr_init[channel];\n            this.channel_count[channel] = this.channel_count_init[channel];\n        }\n\n        buffer.set(start,\n                this.cpu.mem8.subarray(addr, addr + read_bytes),\n                () =>\n                {\n                    if(want_more && autoinit)\n                    {\n                        dbg_log(\"DMA continuing from start\", LOG_DMA);\n                        this.do_write(buffer, start + read_bytes, len - read_bytes, channel, fn);\n                    }\n                    else\n                    {\n                        fn(false);\n                    }\n                }\n            );\n    }\n};\n\nDMA.prototype.address_get_8bit = function(channel)\n{\n    var addr = this.channel_addr[channel];\n\n    // http://wiki.osdev.org/ISA_DMA#16_bit_issues\n    if(channel >= 5)\n    {\n        addr = (addr << 1);\n    }\n\n    addr &= 0xFFFF;\n    addr |= this.channel_page[channel] << 16;\n    addr |= this.channel_pagehi[channel] << 24;\n\n    return addr;\n};\n\nDMA.prototype.count_get_8bit = function(channel)\n{\n    var count = this.channel_count[channel] + 1;\n\n    if(channel >= 5)\n    {\n        count *= 2;\n    }\n\n    return count;\n};\n\nDMA.prototype.flipflop_get = function(old_dword, new_byte, continuing)\n{\n    if(!continuing)\n    {\n        this.lsb_msb_flipflop ^= 1;\n    }\n\n    if(this.lsb_msb_flipflop)\n    {\n        // low byte\n        return old_dword & ~0xFF | new_byte;\n    }\n    else\n    {\n        // high byte\n        return old_dword & ~0xFF00 | new_byte << 8;\n    }\n};\n\nDMA.prototype.flipflop_read = function(dword)\n{\n    this.lsb_msb_flipflop ^= 1;\n\n    if(this.lsb_msb_flipflop)\n    {\n        // low byte\n        return dword & 0xFF;\n    }\n    else\n    {\n        // high byte\n        return (dword >> 8) & 0xFF;\n    }\n};\n"
  },
  {
    "path": "src/elf.js",
    "content": "import { dbg_log, LOG_LEVEL } from \"./log.js\";\n\n// A minimal elf parser for loading 32 bit, x86, little endian, executable elf files\n\nconst ELF_MAGIC = 0x464C457F;\n\nconst types = DataView.prototype;\nconst U8 = { size: 1, get: types.getUint8, set: types.setUint8, };\nconst U16 = { size: 2, get: types.getUint16, set: types.setUint16, };\nconst U32 = { size: 4, get: types.getUint32, set: types.setUint32, };\nconst pad = function(size)\n{\n    return {\n        size,\n        get: offset => -1,\n    };\n};\n\nconst Header = create_struct([\n    { magic: U32, },\n\n    { class: U8, },\n    { data: U8, },\n    { version0: U8, },\n    { osabi: U8, },\n\n    { abiversion: U8, },\n    { pad0: pad(7) },\n\n    { type: U16, },\n    { machine: U16, },\n\n    { version1: U32, },\n    { entry: U32, },\n    { phoff: U32, },\n    { shoff: U32, },\n    { flags: U32, },\n\n    { ehsize: U16, },\n    { phentsize: U16, },\n    { phnum: U16, },\n    { shentsize: U16, },\n    { shnum: U16, },\n    { shstrndx: U16, },\n]);\nconsole.assert(Header.reduce((a, entry) => a + entry.size, 0) === 52);\n\nconst ProgramHeader = create_struct([\n    { type: U32, },\n    { offset: U32, },\n    { vaddr: U32, },\n    { paddr: U32, },\n    { filesz: U32, },\n    { memsz: U32, },\n    { flags: U32, },\n    { align: U32, },\n]);\nconsole.assert(ProgramHeader.reduce((a, entry) => a + entry.size, 0) === 32);\n\nconst SectionHeader = create_struct([\n    { name: U32, },\n    { type: U32, },\n    { flags: U32, },\n    { addr: U32, },\n    { offset: U32, },\n    { size: U32, },\n    { link: U32, },\n    { info: U32, },\n    { addralign: U32, },\n    { entsize: U32, },\n]);\nconsole.assert(SectionHeader.reduce((a, entry) => a + entry.size, 0) === 40);\n\n\n// From [{ name: type }, ...] to [{ name, type, size, get, set }, ...]\nfunction create_struct(struct)\n{\n    return struct.map(function(entry)\n    {\n        const keys = Object.keys(entry);\n        console.assert(keys.length === 1);\n        const name = keys[0];\n        const type = entry[name];\n\n        console.assert(type.size > 0);\n\n        return {\n            name,\n            type,\n            size: type.size,\n            get: type.get,\n            set: type.set,\n        };\n    });\n}\n\n/** @param {ArrayBuffer} buffer */\nexport function read_elf(buffer)\n{\n    const view = new DataView(buffer);\n\n    const [header, offset] = read_struct(view, Header);\n    console.assert(offset === 52);\n\n    if(DEBUG)\n    {\n        for(const key of Object.keys(header))\n        {\n            dbg_log(key + \": 0x\" + (header[key].toString(16) >>> 0));\n        }\n    }\n\n    console.assert(header.magic === ELF_MAGIC, \"Bad magic\");\n    console.assert(header.class === 1, \"Unimplemented: 64 bit elf\");\n    console.assert(header.data === 1, \"Unimplemented: big endian\");\n    console.assert(header.version0 === 1, \"Bad version0\");\n\n    // 1, 2, 3, 4 specify whether the object is relocatable, executable,\n    // shared, or core, respectively.\n    console.assert(header.type === 2, \"Unimplemented type\");\n\n    console.assert(header.version1 === 1, \"Bad version1\");\n\n    // these are different in 64 bit\n    console.assert(header.ehsize === 52, \"Bad header size\");\n    console.assert(header.phentsize === 32, \"Bad program header size\");\n    console.assert(header.shentsize === 40, \"Bad section header size\");\n\n    const [program_headers, ph_offset] = read_structs(\n        view_slice(view, header.phoff, header.phentsize * header.phnum),\n        ProgramHeader,\n        header.phnum);\n\n    const [sections_headers, sh_offset] = read_structs(\n        view_slice(view, header.shoff, header.shentsize * header.shnum),\n        SectionHeader,\n        header.shnum);\n\n    if(DEBUG && LOG_LEVEL)\n    {\n        console.log(\"%d program headers:\", program_headers.length);\n        for(const program of program_headers)\n        {\n            console.log(\n                \"type=%s offset=%s vaddr=%s paddr=%s \" +\n                \"filesz=%s memsz=%s flags=%s align=%s\",\n                program.type.toString(16),\n                program.offset.toString(16),\n                program.vaddr.toString(16),\n                program.paddr.toString(16),\n                program.filesz.toString(16),\n                program.memsz.toString(16),\n                program.flags.toString(16),\n                program.align.toString(16)\n            );\n        }\n\n        console.log(\"%d section headers:\", sections_headers.length);\n        for(const section of sections_headers)\n        {\n            console.log(\n                \"name=%s type=%s flags=%s addr=%s offset=%s \" +\n                \"size=%s link=%s info=%s addralign=%s entsize=%s\",\n                section.name.toString(16),\n                section.type.toString(16),\n                section.flags.toString(16),\n                section.addr.toString(16),\n                section.offset.toString(16),\n                section.size.toString(16),\n                section.link.toString(16),\n                section.info.toString(16),\n                section.addralign.toString(16),\n                section.entsize.toString(16)\n            );\n        }\n    }\n\n    return {\n        header,\n        program_headers,\n        sections_headers,\n    };\n}\n\nfunction read_struct(view, Struct)\n{\n    const result = {};\n    let offset = 0;\n    const LITTLE_ENDIAN = true; // big endian not supported yet\n\n    for(const entry of Struct)\n    {\n        const value = entry.get.call(view, offset, LITTLE_ENDIAN);\n        console.assert(result[entry.name] === undefined);\n        result[entry.name] = value;\n        offset += entry.size;\n    }\n\n    return [result, offset];\n}\n\nfunction read_structs(view, Struct, count)\n{\n    const result = [];\n    let offset = 0;\n\n    for(var i = 0; i < count; i++)\n    {\n        const [s, size] = read_struct(view_slice(view, offset), Struct);\n        result.push(s);\n        offset += size;\n    }\n\n    return [result, offset];\n}\n\n/** @param {number=} length */\nfunction view_slice(view, offset, length)\n{\n    return new DataView(view.buffer, view.byteOffset + offset, length);\n}\n"
  },
  {
    "path": "src/externs.js",
    "content": "var global = {};\nvar process = { hrtime: function() {} };\n\n/**\n * @param {string} name\n * @param {function()} processor\n */\nvar registerProcessor = function(name, processor) {};\n\nconst sampleRate = 0;\n\nvar WabtModule = {\n    readWasm: function(buf, opt) {},\n    generateNames: function() {},\n    applyNames: function() {},\n    toText: function() {},\n};\nvar cs = {\n    Capstone: function() {},\n    ARCH_X86: 0,\n    MODE_16: 0,\n    MODE_32: 0,\n    disasm: { bytes: \"\", mnemonic: \"\", op_str: \"\", },\n};\n\nconst Buffer = {\n    allocUnsafe : function(length) {},\n};\n"
  },
  {
    "path": "src/floppy.js",
    "content": "// v86 Floppy Disk Controller emulation\n//\n// This file is licensed under both BSD and MIT, see LICENSE and LICENSE.MIT.\n//\n// Links\n// - Intel 82078 44 Pin CHMOS Single-Chip Floppy Disk Controller\n//   https://wiki.qemu.org/images/f/f0/29047403.pdf\n// - qemu: fdc.c\n//   https://github.com/qemu/qemu/blob/master/hw/block/fdc.c\n// - Programming Floppy Disk Controllers\n//   https://www.isdaman.com/alsos/hardware/fdc/floppy.htm\n// - OSDev: Floppy Disk Controller\n//   https://wiki.osdev.org/Floppy_Disk_Controller\n\nimport { LOG_FLOPPY } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\nimport { CMOS_FLOPPY_DRIVE_TYPE } from \"./rtc.js\";\nimport { SyncBuffer } from \"./buffer.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { DMA } from \"./dma.js\";\nimport { IO } from \"./io.js\";\n\n// System resources\nconst FDC_IRQ_CHANNEL = 6;\nconst FDC_DMA_CHANNEL = 2;\n\n/**\n * Floppy drive types\n * CMOS register 0x10 bits: upper nibble: fda, lower nibble: fdb\n * @see {@link https://wiki.osdev.org/CMOS#Register_0x10}\n */\nconst CMOS_FDD_TYPE_NO_DRIVE = 0x0; // no floppy drive\nconst CMOS_FDD_TYPE_360      = 0x1; // 360 KB 5\"1/4 drive\nconst CMOS_FDD_TYPE_1200     = 0x2; // 1.2 MB 5\"1/4 drive\nconst CMOS_FDD_TYPE_720      = 0x3; // 720 KB 3\"1/2 drive\nconst CMOS_FDD_TYPE_1440     = 0x4; // 1.44 MB 3\"1/2 drive\nconst CMOS_FDD_TYPE_2880     = 0x5; // 2.88 MB 3\"1/2 drive\n\n// Physical floppy disk size identifier of each drive type\nconst CMOS_FDD_TYPE_MEDIUM = {\n    [CMOS_FDD_TYPE_NO_DRIVE]: 0,    // no disk\n    [CMOS_FDD_TYPE_360]:      525,  // 5\"1/4 disk\n    [CMOS_FDD_TYPE_1200]:     525,  // 5\"1/4 disk\n    [CMOS_FDD_TYPE_720]:      350,  // 3\"1/2 disk\n    [CMOS_FDD_TYPE_1440]:     350,  // 3\"1/2 disk\n    [CMOS_FDD_TYPE_2880]:     350,  // 3\"1/2 disk\n};\n\n// Floppy Controller PIO Register offsets (base: 0x3F0/0x370, offset 0x6 is reserved for ATA IDE)\nconst REG_SRA       = 0x0;  // R,  Status Register A (SRA)\nconst REG_SRB       = 0x1;  // R,  Status Register B (SRB)\nconst REG_DOR       = 0x2;  // RW, Digital Output Register (DOR)\nconst REG_TDR       = 0x3;  // RW, Tape Drive Register (TDR)\nconst REG_MSR       = 0x4;  // R,  Main Status Register (MSR)\nconst REG_DSR       = 0x4;  // W,  Datarate Select Register (DSR)\nconst REG_FIFO      = 0x5;  // RW, W: command bytes, R: response bytes (FIFO)\nconst REG_DIR       = 0x7;  // R,  Digital Input Register (DIR)\nconst REG_CCR       = 0x7;  // W,  Configuration Control Register (CCR)\n\n// Status Register A (SRA) bits\nconst SRA_NDRV2     = 0x40; // true: second drive is not connected\nconst SRA_INTPEND   = 0x80; // true: interrupt pending\n\n// Status Register B (SRB) bits\nconst SRB_MTR0      = 0x1;  // follows DOR.DOR_MOT0\nconst SRB_MTR1      = 0x2;  // follows DOR.DOR_MOT1\nconst SRB_DR0       = 0x20; // follows DOR.DOR_SEL_LO\nconst SRB_RESET     = 0xc0; // magic value after reset\n\n// Digital Output Register (DOR) bits\nconst DOR_SEL_LO    = 0x1;  // lower bit of selected FDD number\nconst DOR_SEL_HI    = 0x2;  // upper bit of selected FDD number\nconst DOR_NRESET    = 0x4;  // true: normal controller mode, false: reset mode (\"not RESET\")\nconst DOR_DMAEN     = 0x8;  // true: use DMA\nconst DOR_MOTEN0    = 0x10; // true: enable motor of FDD0\nconst DOR_MOTEN1    = 0x20; // true: enable motor of FDD1\nconst DOR_MOTEN2    = 0x40; // true: enable motor of FDD2\nconst DOR_MOTEN3    = 0x80; // true: enable motor of FDD3\nconst DOR_SELMASK   = 0x01;\n\n// Tape Drive Register (TDR) bits\nconst TDR_BOOTSEL   = 0x4;\n\n// Main Status Register (MSR) bits\nconst MSR_FDD0      = 0x1;  // true: FDD0 is busy in seek mode\nconst MSR_FDD1      = 0x2;  // true: FDD1 is busy in seek mode\nconst MSR_FDD2      = 0x4;  // true: FDD2 is busy in seek mode\nconst MSR_FDD3      = 0x8;  // true: FDD3 is busy in seek mode\nconst MSR_CMDBUSY   = 0x10; // true: FDC busy, Read/Write command in progress, cleared at end of Result phase\nconst MSR_NDMA      = 0x20; // Non-DMA mode, set in Execution phase of PIO mode read/write commands only.\nconst MSR_DIO       = 0x40; // Data Input/Output, true: FDC has data for CPU, false: FDC expects data from CPU\nconst MSR_RQM       = 0x80; // true: DATA register is ready for I/O\n\n// Datarate Select Register (DSR) bits\nconst DSR_DRATEMASK = 0x3;\nconst DSR_PWRDOWN   = 0x40;\nconst DSR_SWRESET   = 0x80;\n\n// Digital Input Register (DIR) bits\nconst DIR_DOOR      = 0x80; // true: No disk or disk changed since last command\n\n// Status Register 0 (SR0) bits\nconst SR0_DS0       = 0x1;  // Drive select 0..3 lower bit\nconst SR0_DS1       = 0x2;  // Drive select 0..3 upper bit\nconst SR0_HEAD      = 0x4;  // true: Use 2nd head\nconst SR0_EQPMT     = 0x10; // (?)\nconst SR0_SEEK      = 0x20; // (?)\nconst SR0_ABNTERM   = 0x40; // true: Command failed\nconst SR0_INVCMD    = 0x80; // true: Unknown/unimplemented command code\nconst SR0_RDYCHG    = SR0_ABNTERM | SR0_INVCMD; // 0xC0 (?)\n\n// Status Register 1 (SR1) bits\nconst SR1_MA        = 0x1;  // true: Missing address mark error\nconst SR1_NW        = 0x2;  // true: Not writable error\nconst SR1_EC        = 0x80; // true: End of cylinder error\n\n// Status Register 2 (SR2) bits\nconst SR2_SNS       = 0x4;  // true: Scan not satisfied (?)\nconst SR2_SEH       = 0x8;  // true: Scan equal hit (?)\n\n/**\n * FDC command codes\n * We declare all known floppy commands but implement only the subset that\n * we actually observe in the field. See also build_cmd_lookup_table().\n * @see {@link https://github.com/qemu/qemu/blob/6e1571533fd92bec67e5ab9b1dd1e15032925757/hw/block/fdc.c#L619}\n */\nconst CMD_READ_TRACK             = 0x2; // unimplemented\nconst CMD_SPECIFY                = 0x3;\nconst CMD_SENSE_DRIVE_STATUS     = 0x4;\nconst CMD_WRITE                  = 0x5;\nconst CMD_READ                   = 0x6;\nconst CMD_RECALIBRATE            = 0x7;\nconst CMD_SENSE_INTERRUPT_STATUS = 0x8;\nconst CMD_WRITE_DELETED_DATA     = 0x9; // unimplemented\nconst CMD_READ_ID                = 0xa;\nconst CMD_READ_DELETED_DATA      = 0xc; // unimplemented\nconst CMD_FORMAT_TRACK           = 0xd;\nconst CMD_DUMP_REGS              = 0xe;\nconst CMD_SEEK                   = 0xf;\nconst CMD_VERSION                = 0x10;\nconst CMD_SCAN_EQUAL             = 0x11; // unimplemented\nconst CMD_PERPENDICULAR_MODE     = 0x12;\nconst CMD_CONFIGURE              = 0x13;\nconst CMD_LOCK                   = 0x14;\nconst CMD_VERIFY                 = 0x16; // unimplemented\nconst CMD_POWERDOWN_MODE         = 0x17; // unimplemented\nconst CMD_PART_ID                = 0x18;\nconst CMD_SCAN_LOW_OR_EQUAL      = 0x19; // unimplemented\nconst CMD_SCAN_HIGH_OR_EQUAL     = 0x1d; // unimplemented\nconst CMD_SAVE                   = 0x2e; // unimplemented\nconst CMD_OPTION                 = 0x33; // unimplemented\nconst CMD_RESTORE                = 0x4e; // unimplemented\nconst CMD_DRIVE_SPECIFICATION    = 0x8e; // unimplemented\nconst CMD_RELATIVE_SEEK_OUT      = 0x8f; // unimplemented\nconst CMD_FORMAT_AND_WRITE       = 0xcd; // unimplemented\nconst CMD_RELATIVE_SEEK_IN       = 0xcf; // unimplemented\n\n// FDC command flags\nconst CMD_FLAG_MULTI_TRACK  = 0x1;  // MT: multi-track selector (use both heads) in READ/WRITE\n\n// FDC command execution phases\nconst CMD_PHASE_COMMAND     = 1;\nconst CMD_PHASE_EXECUTION   = 2;\nconst CMD_PHASE_RESULT      = 3;\n\n// FDC config bits\nconst CONFIG_PRETRK     = 0xff; // Pre-compensation set to track 0\nconst CONFIG_FIFOTHR    = 0x0f; // FIFO threshold set to 1 byte\nconst CONFIG_POLL       = 0x10; // Poll enabled\nconst CONFIG_EFIFO      = 0x20; // FIFO disabled\nconst CONFIG_EIS        = 0x40; // No implied seeks\n\n// Number of CMD_SENSE_INTERRUPT_STATUS expected after reset\nconst RESET_SENSE_INT_MAX = 4;\n\n// Sector size\nconst SECTOR_SIZE       = 512;  // fixed size of 512 bytes/sector\nconst SECTOR_SIZE_CODE  = 2;    // sector size code 2: 512 bytes/sector\n\n// class FloppyController ----------------------------------------------------\n\n/**\n * @constructor\n *\n * @param {CPU} cpu\n * @param {SyncBuffer|Uint8Array|null|undefined} fda_image\n * @param {SyncBuffer|Uint8Array|null|undefined} fdb_image\n * @param {Object=} fdc_config\n *\n * Structure and defaults of optional configuration object fdc_config:\n *\n *   fdc_config = {\n *       fda: { drive_type: undefined, read_only: false },\n *       fdb: { drive_type: undefined, read_only: false }\n *   }\n *\n * drive_type:\n *     Fixed drive type code whether or not a buffer is defined:\n *       0: no floppy drive\n *       1: 360 KB 5\"1/4 drive\n *       2: 1.2 MB 5\"1/4 drive\n *       3: 720 KB 3\"1/2 drive\n *       4: 1.44 MB 3\"1/2 drive (default)\n *       5: 2.88 MB 3\"1/2 drive\n *     If undefined, the drive type is either derived from the given\n *     buffer or set to the default of 4 if no buffer is specified.\n * read_only:\n *     If true, treat the given disk image as write-protected.\n *     Ignored if no disk image is provided. Default: false.\n *\n * NOTE: To hide fdb from the guest set its drive_type to 0, i.e.:\n *\n *   fdc_config = { fdb: { drive_type: 0 } }\n */\nexport function FloppyController(cpu, fda_image, fdb_image, fdc_config)\n{\n    /** @const @type {IO|undefined} */\n    this.io = cpu.io;\n\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    /** @const @type {DMA} */\n    this.dma = cpu.devices.dma;\n\n    /** @const */\n    this.cmd_table = this.build_cmd_lookup_table();\n\n    this.sra = 0;\n    this.srb = SRB_RESET;\n    this.dor = DOR_NRESET | DOR_DMAEN;\n    this.tdr = 0;\n    this.msr = MSR_RQM;\n    this.dsr = 0;\n\n    this.cmd_phase = CMD_PHASE_COMMAND;\n    this.cmd_code = 0;\n    this.cmd_flags = 0;\n    this.cmd_buffer = new Uint8Array(17);    // CMD_RESTORE has 17 argument bytes\n    this.cmd_cursor = 0;\n    this.cmd_remaining = 0;\n\n    this.response_data = new Uint8Array(15); // CMD_SAVE response size is 15 bytes\n    this.response_cursor = 0;\n    this.response_length = 0;\n    this.status0 = 0;\n    this.status1 = 0;\n\n    this.curr_drive_no = 0;         // was: this.drive; qemu: fdctrl->cur_drv\n    this.reset_sense_int_count = 0; // see SENSE INTERRUPT\n    this.locked = false;            // see LOCK\n    this.step_rate_interval = 0;    // see SPECIFY, qemu: timer0\n    this.head_load_time = 0;        // see SPECIFY, qemu: timer1\n    this.fdc_config = CONFIG_EIS | CONFIG_EFIFO;    // see CONFIGURE, qemu: config\n    this.precomp_trk = 0;           // see CONFIGURE\n    this.eot = 0;                   // see READ/WRITE\n\n    this.drives = [\n        new FloppyDrive(\"fda\", fdc_config?.fda, fda_image, CMOS_FDD_TYPE_1440),\n        new FloppyDrive(\"fdb\", fdc_config?.fdb, fdb_image, CMOS_FDD_TYPE_1440)\n    ];\n\n    Object.seal(this);\n\n    // To make the floppy drives visible to the guest OS we MUST write a\n    // drive type other than 0 (CMOS_FDD_TYPE_NO_DRIVE) to either nibble of\n    // CMOS register 0x10 before the guest is started (CMOS registers are\n    // usually read only once at startup).\n    this.cpu.devices.rtc.cmos_write(CMOS_FLOPPY_DRIVE_TYPE, (this.drives[0].drive_type << 4) | this.drives[1].drive_type);\n\n    const fdc_io_base = 0x3F0;  // alt: 0x370\n\n    this.io.register_read(fdc_io_base | REG_SRA, this, this.read_reg_sra);\n    this.io.register_read(fdc_io_base | REG_SRB, this, this.read_reg_srb);\n    this.io.register_read(fdc_io_base | REG_DOR, this, this.read_reg_dor);\n    this.io.register_read(fdc_io_base | REG_TDR, this, this.read_reg_tdr);\n    this.io.register_read(fdc_io_base | REG_MSR, this, this.read_reg_msr);\n    this.io.register_read(fdc_io_base | REG_FIFO, this, this.read_reg_fifo);\n    this.io.register_read(fdc_io_base | REG_DIR, this, this.read_reg_dir);\n\n    this.io.register_write(fdc_io_base | REG_DOR, this, this.write_reg_dor);\n    this.io.register_write(fdc_io_base | REG_TDR, this, this.write_reg_tdr);\n    this.io.register_write(fdc_io_base | REG_DSR, this, this.write_reg_dsr);\n    this.io.register_write(fdc_io_base | REG_FIFO, this, this.write_reg_fifo);\n    this.io.register_write(fdc_io_base | REG_CCR, this, this.write_reg_ccr);\n\n    dbg_log(\"floppy controller ready\", LOG_FLOPPY);\n}\n\nFloppyController.prototype.build_cmd_lookup_table = function()\n{\n    /**\n     * NOTE: The order of items in the table below is significant.\n     * @see {@link https://github.com/qemu/qemu/blob/aec6836c73403cffa56b9a4c5556451ee16071fe/hw/block/fdc.c#L2160}\n     */\n    const CMD_DESCRIPTOR = [\n        { code: CMD_READ, mask: 0x1f, argc: 8, name: \"READ\", handler: this.exec_read },\n        { code: CMD_WRITE, mask: 0x3f, argc: 8, name: \"WRITE\", handler: this.exec_write },\n        { code: CMD_SEEK, mask: 0xff, argc: 2, name: \"SEEK\", handler: this.exec_seek },\n        { code: CMD_SENSE_INTERRUPT_STATUS, mask: 0xff, argc: 0, name: \"SENSE INTERRUPT STATUS\", handler: this.exec_sense_interrupt_status },\n        { code: CMD_RECALIBRATE, mask: 0xff, argc: 1, name: \"RECALIBRATE\", handler: this.exec_recalibrate },\n        { code: CMD_FORMAT_TRACK, mask: 0xbf, argc: 5, name: \"FORMAT TRACK\", handler: this.exec_format_track },\n        { code: CMD_READ_TRACK, mask: 0xbf, argc: 8, name: \"READ TRACK\", handler: this.exec_unimplemented },\n        { code: CMD_RESTORE, mask: 0xff, argc: 17, name: \"RESTORE\", handler: this.exec_unimplemented },\n        { code: CMD_SAVE, mask: 0xff, argc: 0, name: \"SAVE\", handler: this.exec_unimplemented },\n        { code: CMD_READ_DELETED_DATA, mask: 0x1f, argc: 8, name: \"READ DELETED DATA\", handler: this.exec_unimplemented },\n        { code: CMD_SCAN_EQUAL, mask: 0x1f, argc: 8, name: \"SCAN EQUAL\", handler: this.exec_unimplemented },\n        { code: CMD_VERIFY, mask: 0x1f, argc: 8, name: \"VERIFY\", handler: this.exec_unimplemented },\n        { code: CMD_SCAN_LOW_OR_EQUAL, mask: 0x1f, argc: 8, name: \"SCAN LOW OR EQUAL\", handler: this.exec_unimplemented },\n        { code: CMD_SCAN_HIGH_OR_EQUAL, mask: 0x1f, argc: 8, name: \"SCAN HIGH OR EQUAL\", handler: this.exec_unimplemented },\n        { code: CMD_WRITE_DELETED_DATA, mask: 0x3f, argc: 8, name: \"WRITE DELETED DATA\", handler: this.exec_unimplemented },\n        { code: CMD_READ_ID, mask: 0xbf, argc: 1, name: \"READ ID\", handler: this.exec_read_id },\n        { code: CMD_SPECIFY, mask: 0xff, argc: 2, name: \"SPECIFY\", handler: this.exec_specify },\n        { code: CMD_SENSE_DRIVE_STATUS, mask: 0xff, argc: 1, name: \"SENSE DRIVE STATUS\", handler: this.exec_sense_drive_status },\n        { code: CMD_PERPENDICULAR_MODE, mask: 0xff, argc: 1, name: \"PERPENDICULAR MODE\", handler: this.exec_perpendicular_mode },\n        { code: CMD_CONFIGURE, mask: 0xff, argc: 3, name: \"CONFIGURE\", handler: this.exec_configure },\n        { code: CMD_POWERDOWN_MODE, mask: 0xff, argc: 2, name: \"POWERDOWN MODE\", handler: this.exec_unimplemented },\n        { code: CMD_OPTION, mask: 0xff, argc: 1, name: \"OPTION\", handler: this.exec_unimplemented },\n        { code: CMD_DRIVE_SPECIFICATION, mask: 0xff, argc: 5, name: \"DRIVE SPECIFICATION\", handler: this.exec_unimplemented },\n        { code: CMD_RELATIVE_SEEK_OUT, mask: 0xff, argc: 2, name: \"RELATIVE SEEK OUT\", handler: this.exec_unimplemented },\n        { code: CMD_FORMAT_AND_WRITE, mask: 0xff, argc: 10, name: \"FORMAT AND WRITE\", handler: this.exec_unimplemented },\n        { code: CMD_RELATIVE_SEEK_IN, mask: 0xff, argc: 2, name: \"RELATIVE SEEK IN\", handler: this.exec_unimplemented },\n        { code: CMD_LOCK, mask: 0x7f, argc: 0, name: \"LOCK\", handler: this.exec_lock },\n        { code: CMD_DUMP_REGS, mask: 0xff, argc: 0, name: \"DUMP REGISTERS\", handler: this.exec_dump_regs },\n        { code: CMD_VERSION, mask: 0xff, argc: 0, name: \"VERSION\", handler: this.exec_version },\n        { code: CMD_PART_ID, mask: 0xff, argc: 0, name: \"PART ID\", handler: this.exec_part_id },\n\n        { code: 0, mask: 0x00, argc: 0, name: \"UNKNOWN COMMAND\", handler: this.exec_unimplemented },    // default handler\n    ];\n\n    const cmd_table = new Array(256);\n    for(let i = CMD_DESCRIPTOR.length-1; i >= 0; i--)\n    {\n        const cmd_desc = CMD_DESCRIPTOR[i];\n        if(cmd_desc.mask === 0xff)\n        {\n            cmd_table[cmd_desc.code] = cmd_desc;\n        }\n        else\n        {\n            for(let j = 0; j < 256; j++)\n            {\n                if((j & cmd_desc.mask) === cmd_desc.code)\n                {\n                    cmd_table[j] = cmd_desc;\n                }\n            }\n        }\n    }\n    return cmd_table;\n};\n\nFloppyController.prototype.raise_irq = function(reason)\n{\n    if(!(this.sra & SRA_INTPEND))\n    {\n        this.cpu.device_raise_irq(FDC_IRQ_CHANNEL);\n        this.sra |= SRA_INTPEND;\n        dbg_log(\"IRQ raised, reason: \" + reason, LOG_FLOPPY);\n    }\n    this.reset_sense_int_count = 0;\n};\n\nFloppyController.prototype.lower_irq = function(reason)\n{\n    this.status0 = 0;\n    if(this.sra & SRA_INTPEND)\n    {\n        this.cpu.device_lower_irq(FDC_IRQ_CHANNEL);\n        this.sra &= ~SRA_INTPEND;\n        dbg_log(\"IRQ lowered, reason: \" + reason, LOG_FLOPPY);\n    }\n};\n\nFloppyController.prototype.set_curr_drive_no = function(curr_drive_no)\n{\n    this.curr_drive_no = curr_drive_no & 1;\n    return this.drives[this.curr_drive_no];\n};\n\nFloppyController.prototype.enter_command_phase = function()\n{\n    this.cmd_phase = CMD_PHASE_COMMAND;\n    this.cmd_cursor = 0;\n    this.cmd_remaining = 0;\n    this.msr &= ~(MSR_CMDBUSY | MSR_DIO);\n    this.msr |= MSR_RQM;\n};\n\nFloppyController.prototype.enter_result_phase = function(fifo_len)\n{\n    this.cmd_phase = CMD_PHASE_RESULT;\n    this.response_cursor = 0;\n    this.response_length = fifo_len;\n    this.msr |= MSR_CMDBUSY | MSR_RQM | MSR_DIO;\n};\n\nFloppyController.prototype.reset_fdc = function()\n{\n    dbg_log(\"resetting controller\", LOG_FLOPPY);\n    this.lower_irq(\"controller reset\");\n\n    this.sra = 0;   // NOTE: set SRA to SRA_NDRV2 if fdb does not exist\n    this.srb = SRB_RESET;\n    this.dor = DOR_NRESET | DOR_DMAEN;\n    this.msr = MSR_RQM;\n    this.curr_drive_no = 0;\n    this.status0 |= SR0_RDYCHG;\n\n    this.response_cursor = 0;\n    this.response_length = 0;\n\n    this.drives[0].seek(0, 0, 1);\n    this.drives[1].seek(0, 0, 1);\n\n    // raise interrupt\n    this.enter_command_phase();\n    this.raise_irq(\"controller reset\");\n    this.reset_sense_int_count = RESET_SENSE_INT_MAX;\n};\n\n// Register I/O callbacks ----------------------------------------------------\n\nFloppyController.prototype.read_reg_sra = function()\n{\n    dbg_log(\"SRA read: \" + h(this.sra), LOG_FLOPPY);\n    return this.sra;\n};\n\nFloppyController.prototype.read_reg_srb = function()\n{\n    dbg_log(\"SRB read: \" + h(this.srb), LOG_FLOPPY);\n    return this.srb;\n};\n\nFloppyController.prototype.read_reg_dor = function()\n{\n    const dor_byte = (this.dor & ~(DOR_SEL_LO|DOR_SEL_HI)) | this.curr_drive_no;\n    dbg_log(\"DOR read: \" + h(dor_byte), LOG_FLOPPY);\n    return dor_byte;\n};\n\nFloppyController.prototype.read_reg_tdr = function()\n{\n    dbg_log(\"TDR read: \" + h(this.tdr), LOG_FLOPPY);\n    return this.tdr;\n};\n\nFloppyController.prototype.read_reg_msr = function()\n{\n    dbg_log(\"MSR read: \" + h(this.msr), LOG_FLOPPY);\n    this.dsr &= ~DSR_PWRDOWN;\n    this.dor |= DOR_NRESET;\n    return this.msr;\n};\n\nFloppyController.prototype.read_reg_fifo = function()\n{\n    this.dsr &= ~DSR_PWRDOWN;\n    if(!(this.msr & MSR_RQM) || !(this.msr & MSR_DIO))\n    {\n        dbg_log(\"FIFO read rejected: controller not ready for reading\", LOG_FLOPPY);\n        return 0;\n    }\n    else if(this.cmd_phase !== CMD_PHASE_RESULT)\n    {\n        dbg_log(\"FIFO read rejected: floppy controller not in RESULT phase, phase: \" + this.cmd_phase, LOG_FLOPPY);\n        return 0;\n    }\n\n    if(this.response_cursor < this.response_length)\n    {\n        const fifo_byte = this.response_data[this.response_cursor++];\n        if(this.response_cursor === this.response_length)\n        {\n            const lower_irq_reason = DEBUG ? \"end of \" + this.cmd_table[this.cmd_code].name + \" response\" : \"\";\n            this.msr &= ~MSR_RQM;\n            this.enter_command_phase();\n            this.lower_irq(lower_irq_reason);\n        }\n        return fifo_byte;\n    }\n    else\n    {\n        dbg_log(\"FIFO read: empty\", LOG_FLOPPY);\n        return 0;\n    }\n};\n\nFloppyController.prototype.read_reg_dir = function()\n{\n    const curr_drive = this.drives[this.curr_drive_no];\n    const dir_byte = curr_drive.media_changed ? DIR_DOOR : 0;\n    dbg_log(\"DIR read: \" + h(dir_byte), LOG_FLOPPY);\n    return dir_byte;\n};\n\nFloppyController.prototype.write_reg_dor = function(dor_byte)\n{\n    // update motor and drive bits in Status Register B\n    this.srb = (this.srb & ~(SRB_MTR0 | SRB_MTR1 | SRB_DR0)) |\n        (dor_byte & DOR_MOTEN0 ? SRB_MTR0 : 0) |\n        (dor_byte & DOR_MOTEN1 ? SRB_MTR1 : 0) |\n        (dor_byte & DOR_SEL_LO ? SRB_DR0 : 0);\n\n    // RESET-state transitions\n    if(this.dor & DOR_NRESET)\n    {\n        if(!(dor_byte & DOR_NRESET))\n        {\n            dbg_log(\"enter RESET state\", LOG_FLOPPY);\n        }\n    }\n    else\n    {\n        if(dor_byte & DOR_NRESET)\n        {\n            this.reset_fdc();\n            this.dsr &= ~DSR_PWRDOWN;\n            dbg_log(\"exit RESET state\", LOG_FLOPPY);\n        }\n    }\n\n    // select current drive\n    const new_drive_no = dor_byte & (DOR_SEL_LO|DOR_SEL_HI);\n    dbg_log(\"DOR write: \" + h(dor_byte) + \", motors: \" + h(dor_byte >> 4) +\n        \", dma: \" + !!(dor_byte & DOR_DMAEN) + \", reset: \" + !(dor_byte & DOR_NRESET) +\n        \", drive: \" + new_drive_no, LOG_FLOPPY);\n    if(new_drive_no > 1)\n    {\n        dbg_log(\"*** WARNING: floppy drive number \" + new_drive_no + \" not implemented!\", LOG_FLOPPY);\n    }\n    this.curr_drive_no = new_drive_no & DOR_SEL_LO;\n\n    this.dor = dor_byte;\n};\n\nFloppyController.prototype.write_reg_tdr = function(tdr_byte)\n{\n    if(!(this.dor & DOR_NRESET))\n    {\n        dbg_log(\"TDR write \" + h(tdr_byte) + \" rejected: Floppy controller in RESET mode!\", LOG_FLOPPY);\n        return;\n    }\n\n    dbg_log(\"TDR write: \" + h(tdr_byte), LOG_FLOPPY);\n    this.tdr = tdr_byte & TDR_BOOTSEL;  // Disk boot selection indicator\n};\n\nFloppyController.prototype.write_reg_dsr = function(dsr_byte)\n{\n    if(!(this.dor & DOR_NRESET))\n    {\n        dbg_log(\"DSR write: \" + h(dsr_byte) + \" rejected: Floppy controller in RESET mode!\", LOG_FLOPPY);\n        return;\n    }\n\n    dbg_log(\"DSR write: \" + h(dsr_byte), LOG_FLOPPY);\n    if(dsr_byte & DSR_SWRESET)\n    {\n        this.dor &= ~DOR_NRESET;\n        this.reset_fdc();\n        this.dor |= DOR_NRESET;\n    }\n    if(dsr_byte & DSR_PWRDOWN)\n    {\n        this.reset_fdc();\n    }\n    this.dsr = dsr_byte;\n};\n\nFloppyController.prototype.write_reg_fifo = function(fifo_byte)\n{\n    this.dsr &= ~DSR_PWRDOWN;\n    if(!(this.dor & DOR_NRESET))\n    {\n        dbg_log(\"FIFO write \" + h(fifo_byte) + \" rejected: floppy controller in RESET mode!\", LOG_FLOPPY);\n        return;\n    }\n    else if(!(this.msr & MSR_RQM) || (this.msr & MSR_DIO))\n    {\n        dbg_log(\"FIFO write \" + h(fifo_byte) + \" rejected: controller not ready for writing\", LOG_FLOPPY);\n        return;\n    }\n    else if(this.cmd_phase !== CMD_PHASE_COMMAND)\n    {\n        dbg_log(\"FIFO write \" + h(fifo_byte) + \" rejected: floppy controller not in COMMAND phase, phase: \" + this.cmd_phase, LOG_FLOPPY);\n        return;\n    }\n\n    if(this.cmd_remaining === 0)\n    {\n        // start reading command, fifo_byte contains the command code\n        const cmd_desc = this.cmd_table[fifo_byte];\n        this.cmd_code = fifo_byte;\n        this.cmd_remaining = cmd_desc.argc;\n        this.cmd_cursor = 0;\n        this.cmd_flags = 0;\n        if((cmd_desc.code === CMD_READ || cmd_desc.code === CMD_WRITE) && (this.cmd_code & 0x80)) // 0x80: Multi-track (MT)\n        {\n            this.cmd_flags |= CMD_FLAG_MULTI_TRACK;\n        }\n        if(this.cmd_remaining)\n        {\n            this.msr |= MSR_RQM;\n        }\n        this.msr |= MSR_CMDBUSY;\n    }\n    else\n    {\n        // continue reading command, fifo_byte contains an argument value\n        this.cmd_buffer[this.cmd_cursor++] = fifo_byte;\n        this.cmd_remaining--;\n    }\n\n    if(this.cmd_remaining === 0)\n    {\n        // done reading command: execute\n        this.cmd_phase = CMD_PHASE_EXECUTION;\n        const cmd_desc = this.cmd_table[this.cmd_code];\n        const args = this.cmd_buffer.slice(0, this.cmd_cursor);\n        if(DEBUG)\n        {\n            const args_hex = [];\n            for(const arg of args)\n            {\n                args_hex.push(h(arg, 2));\n            }\n            dbg_log(\"FD command \" + h(this.cmd_code) + \": \" + cmd_desc.name + \"(\" + args_hex.join(\", \") + \")\", LOG_FLOPPY);\n        }\n        cmd_desc.handler.call(this, args);\n    }\n};\n\nFloppyController.prototype.write_reg_ccr = function(ccr_byte)\n{\n    if(!(this.dor & DOR_NRESET))\n    {\n        dbg_log(\"CCR write: \" + h(ccr_byte) + \" rejected: Floppy controller in RESET mode!\", LOG_FLOPPY);\n        return;\n    }\n\n    dbg_log(\"CCR write: \" + h(ccr_byte), LOG_FLOPPY);\n    // only the rate selection bits used in AT mode, and we store those in the DSR\n    this.dsr = (this.dsr & ~DSR_DRATEMASK) | (ccr_byte & DSR_DRATEMASK);\n};\n\n// Floppy command handler ----------------------------------------------------\n\nFloppyController.prototype.exec_unimplemented = function(args)\n{\n    dbg_assert(false, \"Unimplemented floppy command code \" + h(this.cmd_code) + \"!\");\n    this.status0 = SR0_INVCMD;\n    this.response_data[0] = this.status0;\n\n    // no interrupt\n    this.enter_result_phase(1);\n};\n\nFloppyController.prototype.exec_read = function(args)\n{\n    this.start_read_write(args, false);\n};\n\nFloppyController.prototype.exec_write = function(args)\n{\n    this.start_read_write(args, true);\n};\n\nFloppyController.prototype.exec_seek = function(args)\n{\n    const curr_drive = this.set_curr_drive_no(args[0] & DOR_SELMASK);\n    const track = args[1];\n\n    this.enter_command_phase();\n    curr_drive.seek(curr_drive.curr_head, track, curr_drive.curr_sect);\n\n    // raise interrupt without response\n    this.status0 |= SR0_SEEK;\n    this.raise_irq(\"SEEK command\");\n};\n\nFloppyController.prototype.exec_sense_interrupt_status = function(args)\n{\n    const curr_drive = this.drives[this.curr_drive_no];\n\n    let status0;\n    if(this.reset_sense_int_count > 0)\n    {\n        const drv_nr = RESET_SENSE_INT_MAX - this.reset_sense_int_count--;\n        status0 = SR0_RDYCHG | drv_nr;\n    }\n    else if(this.sra & SRA_INTPEND)\n    {\n        status0 = (this.status0 & ~(SR0_HEAD | SR0_DS1 | SR0_DS0)) | this.curr_drive_no;\n    }\n    else\n    {\n        dbg_log(\"No interrupt pending, aborting SENSE INTERRUPT command!\", LOG_FLOPPY);\n        this.response_data[0] = SR0_INVCMD;\n        this.enter_result_phase(1);\n        return;\n    }\n\n    this.response_data[0] = status0;\n    this.response_data[1] = curr_drive.curr_track;\n\n    // lower interrupt\n    this.enter_result_phase(2);\n    this.lower_irq(\"SENSE INTERRUPT command\");\n    this.status0 = SR0_RDYCHG;\n};\n\nFloppyController.prototype.exec_recalibrate = function(args)\n{\n    const curr_drive = this.set_curr_drive_no(args[0] & DOR_SELMASK);\n\n    curr_drive.seek(0, 0, 1);\n\n    // raise interrupt without response\n    this.enter_command_phase();\n    this.status0 |= SR0_SEEK;\n    this.raise_irq(\"RECALIBRATE command\");\n};\n\nFloppyController.prototype.exec_format_track = function(args)\n{\n    const curr_drive = this.set_curr_drive_no(args[0] & DOR_SELMASK);\n\n    let status0 = 0, status1 = 0;\n    if(curr_drive.read_only)\n    {\n        status0 = SR0_ABNTERM | SR0_SEEK;\n        status1 = SR1_NW;\n    }\n\n    // raise interrupt\n    this.end_read_write(status0, status1, 0);\n};\n\nFloppyController.prototype.exec_read_id = function(args)\n{\n    const head_sel = args[0];\n    const curr_drive = this.drives[this.curr_drive_no];\n\n    curr_drive.curr_head = (head_sel >> 2) & 1;\n    if(curr_drive.max_sect !== 0)\n    {\n        curr_drive.curr_sect = (curr_drive.curr_sect % curr_drive.max_sect) + 1;\n    }\n\n    // raise interrupt\n    this.end_read_write(0, 0, 0);\n};\n\nFloppyController.prototype.exec_specify = function(args)\n{\n    const hut_srt = args[0]; // 0..3: Head Unload Time (HUT), 4..7: Step Rate Interval (SRT)\n    const nd_hlt = args[1];  // 0: Non-DMA mode flag (ND), 1..7: Head Load Time (HLT)\n\n    this.step_rate_interval = hut_srt >> 4;\n    this.head_load_time = nd_hlt >> 1;\n    if(nd_hlt & 0x1)\n    {\n        this.dor &= ~DOR_DMAEN;\n    }\n    else\n    {\n        this.dor |= DOR_DMAEN;\n    }\n\n    // no interrupt or response\n    this.enter_command_phase();\n};\n\nFloppyController.prototype.exec_sense_drive_status = function(args)\n{\n    const drv_sel = args[0];\n    const curr_drive = this.set_curr_drive_no(drv_sel & DOR_SELMASK);\n    curr_drive.curr_head = (drv_sel >> 2) & 1;\n\n    this.response_data[0] = (curr_drive.read_only ? 0x40 : 0) |    // response byte is Status Register 3\n        (curr_drive.curr_track === 0 ? 0x10 : 0) |\n        (curr_drive.curr_head << 2) |\n        this.curr_drive_no |\n        0x28;   // bits 3 and 5 unused, always \"1\"\n\n    // no interrupt\n    this.enter_result_phase(1);\n};\n\nFloppyController.prototype.exec_perpendicular_mode = function(args)\n{\n    const perp_mode = args[0];\n\n    if(perp_mode & 0x80)  // 0x80: OW, bits D0 and D1 can be overwritten\n    {\n        const curr_drive = this.drives[this.curr_drive_no];\n        curr_drive.perpendicular = perp_mode & 0x7;\n    }\n\n    // no interrupt or response\n    this.enter_command_phase();\n};\n\nFloppyController.prototype.exec_configure = function(args)\n{\n    // args[0] is always 0\n    this.fdc_config = args[1];\n    this.precomp_trk = args[2];\n\n    // no interrupt or response\n    this.enter_command_phase();\n};\n\nFloppyController.prototype.exec_lock = function(args)\n{\n    if(this.cmd_code & 0x80)\n    {\n        // LOCK command\n        this.locked = true;\n        this.response_data[0] = 0x10;\n    }\n    else\n    {\n        // UNLOCK command\n        this.locked = false;\n        this.response_data[0] = 0;\n    }\n\n    // no interrupt\n    this.enter_result_phase(1);\n};\n\nFloppyController.prototype.exec_dump_regs = function(args)\n{\n    const curr_drive = this.drives[this.curr_drive_no];\n\n    // drive positions\n    this.response_data[0] = this.drives[0].curr_track;\n    this.response_data[1] = this.drives[1].curr_track;\n    this.response_data[2] = 0;\n    this.response_data[3] = 0;\n    // timers\n    this.response_data[4] = this.step_rate_interval;\n    this.response_data[5] = (this.head_load_time << 1) | ((this.dor & DOR_DMAEN) ? 1 : 0);\n    this.response_data[6] = curr_drive.max_sect;\n    this.response_data[7] = (this.locked ? 0x80 : 0) | (curr_drive.perpendicular << 2);\n    this.response_data[8] = this.fdc_config;\n    this.response_data[9] = this.precomp_trk;\n\n    // no interrupt\n    this.enter_result_phase(10);\n};\n\nFloppyController.prototype.exec_version = function(args)\n{\n    this.response_data[0] = 0x90;   // 0x80: Standard controller, 0x81: Intel 82077, 0x90: Intel 82078\n\n    // no interrupt\n    this.enter_result_phase(1);\n};\n\nFloppyController.prototype.exec_part_id = function(args)\n{\n    this.response_data[0] = 0x41;   // Stepping 1 (PS/2 mode)\n\n    // no interrupt\n    this.enter_result_phase(1);\n};\n\n// ---------------------------------------------------------------------------\n\nFloppyController.prototype.start_read_write = function(args, do_write)\n{\n    const curr_drive = this.set_curr_drive_no(args[0] & DOR_SELMASK);\n    const track = args[1];\n    const head = args[2];\n    const sect = args[3];\n    const ssc = args[4];    // sector size code 0..7 (0:128, 1:256, 2:512, ..., 7:16384 bytes/sect)\n    const eot = args[5];    // last sector number of current track\n    const dtl = args[7] < 128 ? args[7] : 128;  // data length in bytes if ssc is 0, else unused\n\n    switch(curr_drive.seek(head, track, sect))\n    {\n    case 2: // error: sect too big\n        this.end_read_write(SR0_ABNTERM, 0, 0);\n        this.response_data[3] = track;\n        this.response_data[4] = head;\n        this.response_data[5] = sect;\n        return;\n    case 3: // error: track too big\n        this.end_read_write(SR0_ABNTERM, SR1_EC, 0);\n        this.response_data[3] = track;\n        this.response_data[4] = head;\n        this.response_data[5] = sect;\n        return;\n    case 1: // track changed\n        this.status0 |= SR0_SEEK;\n        break;\n    }\n\n    const sect_size = 128 << (ssc > 7 ? 7 : ssc);               // sector size in bytes\n    const sect_start = curr_drive.chs2lba(track, head, sect);   // linear start sector\n    const data_offset = sect_start * sect_size;                 // data offset in bytes\n    let data_length;                                            // data length in bytes\n    if(sect_size === 128)\n    {\n        // if requested data length (dtl) is < 128:\n        // - READ: return only dtl bytes, skipping the sector's remaining bytes (OK)\n        // - WRITE: we must fill the sector's remaining bytes with 0 (TODO!)\n        if(do_write && dtl < 128)\n        {\n            dbg_assert(false, \"dtl=\" + dtl + \" is less than 128, zero-padding is still unimplemented!\");\n        }\n        data_length = dtl;\n    }\n    else\n    {\n        if(this.cmd_flags & CMD_FLAG_MULTI_TRACK)\n        {\n            data_length = (2 * eot - sect + 1) * sect_size;\n        }\n        else\n        {\n            data_length = (eot - sect + 1) * sect_size;\n        }\n        if(data_length <= 0)\n        {\n            dbg_log(\"invalid data_length: \" + data_length + \" sect=\" + sect + \" eot=\" + eot, LOG_FLOPPY);\n            this.end_read_write(SR0_ABNTERM, SR1_MA, 0);\n            this.response_data[3] = track;\n            this.response_data[4] = head;\n            this.response_data[5] = sect;\n            return;\n        }\n    }\n    this.eot = eot;\n\n    if(DEBUG)\n    {\n        dbg_log(\"Floppy \" + this.cmd_table[this.cmd_code].name +\n            \" from: \" + h(data_offset) + \", length: \" + h(data_length) +\n            \", C/H/S: \" + track + \"/\" + head + \"/\" + sect + \", ro: \" + curr_drive.read_only +\n            \", #S: \" + curr_drive.max_sect + \", #H: \" + curr_drive.max_head,\n            LOG_FLOPPY);\n    }\n\n    if(do_write && curr_drive.read_only)\n    {\n        this.end_read_write(SR0_ABNTERM | SR0_SEEK, SR1_NW, 0);\n        this.response_data[3] = track;\n        this.response_data[4] = head;\n        this.response_data[5] = sect;\n        return;\n    }\n\n    if(this.dor & DOR_DMAEN)\n    {\n        // start DMA transfer\n        this.msr &= ~MSR_RQM;\n        const do_dma = do_write ? this.dma.do_write : this.dma.do_read;\n        do_dma.call(this.dma,\n            curr_drive.buffer,\n            data_offset,\n            data_length,\n            FDC_DMA_CHANNEL,\n            dma_error => {\n                if(dma_error)\n                {\n                    dbg_log(\"DMA floppy error\", LOG_FLOPPY);\n                    this.end_read_write(SR0_ABNTERM, 0, 0);\n                }\n                else\n                {\n                    this.seek_next_sect();\n                    this.end_read_write(0, 0, 0);\n                }\n            }\n        );\n    }\n    else\n    {\n        // start PIO transfer\n        dbg_assert(false, this.cmd_table[this.cmd_code].name + \" in PIO mode not supported!\");\n    }\n};\n\nFloppyController.prototype.end_read_write = function(status0, status1, status2)\n{\n    const curr_drive = this.drives[this.curr_drive_no];\n\n    this.status0 &= ~(SR0_DS0 | SR0_DS1 | SR0_HEAD);\n    this.status0 |= this.curr_drive_no;\n    if(curr_drive.curr_head)\n    {\n        this.status0 |= SR0_HEAD;\n    }\n    this.status0 |= status0;\n\n    this.msr |= MSR_RQM | MSR_DIO;\n    this.msr &= ~MSR_NDMA;\n\n    this.response_data[0] = this.status0;\n    this.response_data[1] = status1;\n    this.response_data[2] = status2;\n    this.response_data[3] = curr_drive.curr_track;\n    this.response_data[4] = curr_drive.curr_head;\n    this.response_data[5] = curr_drive.curr_sect;\n    this.response_data[6] = SECTOR_SIZE_CODE;\n\n    // raise interrupt\n    this.enter_result_phase(7);\n    this.raise_irq(this.cmd_table[this.cmd_code].name + \" command\");\n};\n\nFloppyController.prototype.seek_next_sect = function()\n{\n    // Seek to next sector\n    // returns 0 when end of track reached (for DBL_SIDES on head 1). otherwise returns 1\n    const curr_drive = this.drives[this.curr_drive_no];\n\n    // XXX: curr_sect >= max_sect should be an error in fact (TODO: this comment comes from qemu, not sure what to make of it)\n    let new_track = curr_drive.curr_track;\n    let new_head = curr_drive.curr_head;\n    let new_sect = curr_drive.curr_sect;\n    let ret = 1;\n\n    if(new_sect >= curr_drive.max_sect || new_sect === this.eot)\n    {\n        new_sect = 1;\n        if(this.cmd_flags & CMD_FLAG_MULTI_TRACK)\n        {\n            if(new_head === 0 && curr_drive.max_head === 2)\n            {\n                new_head = 1;\n            }\n            else\n            {\n                new_head = 0;\n                new_track++;\n                this.status0 |= SR0_SEEK;\n                if(curr_drive.max_head === 1)\n                {\n                    ret = 0;\n                }\n            }\n        }\n        else\n        {\n            this.status0 |= SR0_SEEK;\n            new_track++;\n            ret = 0;\n        }\n    }\n    else\n    {\n        new_sect++;\n    }\n\n    curr_drive.seek(new_head, new_track, new_sect);\n    return ret;\n};\n\nFloppyController.prototype.get_state = function()\n{\n    // NOTE: Old-style state snapshots (state indices 0..18) did not include\n    // the disk image buffer, only a few register states, so a floppy drive\n    // remained essentially unchangd when a state snapshot was applied.\n    // The snapshotted registers can be safely ignored when restoring state,\n    // hence the entire old-style state is now ignored and deprecated.\n    const state = [];\n    state[19] = this.sra;\n    state[20] = this.srb;\n    state[21] = this.dor;\n    state[22] = this.tdr;\n    state[23] = this.msr;\n    state[24] = this.dsr;\n    state[25] = this.cmd_phase;\n    state[26] = this.cmd_code;\n    state[27] = this.cmd_flags;\n    state[28] = this.cmd_buffer;    // Uint8Array\n    state[29] = this.cmd_cursor;\n    state[30] = this.cmd_remaining;\n    state[31] = this.response_data; // Uint8Array\n    state[32] = this.response_cursor;\n    state[33] = this.response_length;\n    state[34] = this.status0;\n    state[35] = this.status1;\n    state[36] = this.curr_drive_no;\n    state[37] = this.reset_sense_int_count;\n    state[38] = this.locked;\n    state[39] = this.step_rate_interval;\n    state[40] = this.head_load_time;\n    state[41] = this.fdc_config;\n    state[42] = this.precomp_trk;\n    state[43] = this.eot;\n    state[44] = this.drives[0].get_state();\n    state[45] = this.drives[1].get_state();\n    return state;\n};\n\nFloppyController.prototype.set_state = function(state)\n{\n    if(typeof state[19] === \"undefined\")\n    {\n        // see comment above in get_state()\n        return;\n    }\n    this.sra = state[19];\n    this.srb = state[20];\n    this.dor = state[21];\n    this.tdr = state[22];\n    this.msr = state[23];\n    this.dsr = state[24];\n    this.cmd_phase = state[25];\n    this.cmd_code = state[26];\n    this.cmd_flags = state[27];\n    this.cmd_buffer.set(state[28]);     // Uint8Array\n    this.cmd_cursor = state[29];\n    this.cmd_remaining = state[30];\n    this.response_data.set(state[31]);  // Uint8Array\n    this.response_cursor = state[32];\n    this.response_length = state[33];\n    this.status0 = state[34];\n    this.status1 = state[35];\n    this.curr_drive_no = state[36];\n    this.reset_sense_int_count = state[37];\n    this.locked = state[38];\n    this.step_rate_interval = state[39];\n    this.head_load_time = state[40];\n    this.fdc_config = state[41];\n    this.precomp_trk = state[42];\n    this.eot = state[43];\n    this.drives[0].set_state(state[44]);\n    this.drives[1].set_state(state[45]);\n};\n\n// class FloppyDrive ---------------------------------------------------------\n\n/**\n * Floppy disk formats\n *\n * In many cases, the total sector count (sectors*tracks*heads) of a format\n * is enough to uniquely identify it. However, there are some total sector\n * collisions between formats of different physical size (drive_type), these\n * are highlighed below in the \"collides\" column.\n *\n * Matches that are higher up in the table take precedence over later matches.\n *\n * @see {@link https://github.com/qemu/qemu/blob/e240f6cc25917f3138d9e95e0343ae23b63a3f8c/hw/block/fdc.c#L99}\n * @see {@link https://en.wikipedia.org/wiki/List_of_floppy_disk_formats}\n */\nconst DISK_FORMATS = [\n    //                                                                 ttl_sect     size     collides\n    // 1.44 MB 3\"1/2 floppy disks                                             |     |        |\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 18, tracks: 80, heads: 2 }, // 2880  1.44 MB  3.5\"\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 20, tracks: 80, heads: 2 }, // 3200  1.6 MB   3.5\"\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 21, tracks: 80, heads: 2 }, // 3360  1.68 MB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 21, tracks: 82, heads: 2 }, // 3444  1.72 MB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 21, tracks: 83, heads: 2 }, // 3486  1.74 MB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 22, tracks: 80, heads: 2 }, // 3520  1.76 MB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 23, tracks: 80, heads: 2 }, // 3680  1.84 MB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 24, tracks: 80, heads: 2 }, // 3840  1.92 MB\n    // 2.88 MB 3\"1/2 floppy disks\n    { drive_type: CMOS_FDD_TYPE_2880, sectors: 36, tracks: 80, heads: 2 }, // 5760  2.88 MB\n    { drive_type: CMOS_FDD_TYPE_2880, sectors: 39, tracks: 80, heads: 2 }, // 6240  3.12 MB\n    { drive_type: CMOS_FDD_TYPE_2880, sectors: 40, tracks: 80, heads: 2 }, // 6400  3.2 MB\n    { drive_type: CMOS_FDD_TYPE_2880, sectors: 44, tracks: 80, heads: 2 }, // 7040  3.52 MB\n    { drive_type: CMOS_FDD_TYPE_2880, sectors: 48, tracks: 80, heads: 2 }, // 7680  3.84 MB\n    // 720 kB 3\"1/2 floppy disks\n    { drive_type: CMOS_FDD_TYPE_1440, sectors:  8, tracks: 80, heads: 2 }, // 1280  640 kB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors:  9, tracks: 80, heads: 2 }, // 1440  720 kB   3.5\"\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 10, tracks: 80, heads: 2 }, // 1600  800 kB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 10, tracks: 82, heads: 2 }, // 1640  820 kB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 10, tracks: 83, heads: 2 }, // 1660  830 kB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 13, tracks: 80, heads: 2 }, // 2080  1.04 MB\n    { drive_type: CMOS_FDD_TYPE_1440, sectors: 14, tracks: 80, heads: 2 }, // 2240  1.12 MB\n    // 1.2 MB 5\"1/4 floppy disks\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 15, tracks: 80, heads: 2 }, // 2400  1.2 MB\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 18, tracks: 80, heads: 2 }, // 2880  1.44 MB  5.25\"\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 18, tracks: 82, heads: 2 }, // 2952  1.48 MB\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 18, tracks: 83, heads: 2 }, // 2988  1.49 MB\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 20, tracks: 80, heads: 2 }, // 3200  1.6 MB   5.25\"\n    // 720 kB 5\"1/4 floppy disks\n    { drive_type: CMOS_FDD_TYPE_1200, sectors:  9, tracks: 80, heads: 2 }, // 1440  720 kB   5.25\"\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 11, tracks: 80, heads: 2 }, // 1760  880 kB\n    // 360 kB 5\"1/4 floppy disks\n    { drive_type: CMOS_FDD_TYPE_1200, sectors:  9, tracks: 40, heads: 2 }, // 720   360 kB   5.25\"\n    { drive_type: CMOS_FDD_TYPE_1200, sectors:  9, tracks: 40, heads: 1 }, // 360   180 kB\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 10, tracks: 41, heads: 2 }, // 820   410 kB\n    { drive_type: CMOS_FDD_TYPE_1200, sectors: 10, tracks: 42, heads: 2 }, // 840   420 kB\n    // 320 kB 5\"1/4 floppy disks\n    { drive_type: CMOS_FDD_TYPE_1200, sectors:  8, tracks: 40, heads: 2 }, // 640   320 kB\n    { drive_type: CMOS_FDD_TYPE_1200, sectors:  8, tracks: 40, heads: 1 }, // 320   160 kB\n    // 360 kB must match 5\"1/4 better than 3\"1/2...\n    { drive_type: CMOS_FDD_TYPE_1440, sectors:  9, tracks: 80, heads: 1 }, // 720   360 kB   3.5\"\n    // types defined in earlier v86 releases (not defined in qemu)\n    { drive_type: CMOS_FDD_TYPE_360,  sectors: 10, tracks: 40, heads: 1 }, // 400   200 kB\n    { drive_type: CMOS_FDD_TYPE_360,  sectors: 10, tracks: 40, heads: 2 }, // 800   400 kB\n];\n\n/**\n * @constructor\n *\n * @param {string} name\n * @param {Object|undefined} fdd_config\n * @param {SyncBuffer|Uint8Array|null|undefined} buffer\n * @param {number} fallback_drive_type\n */\nfunction FloppyDrive(name, fdd_config, buffer, fallback_drive_type)\n{\n    /** @const */\n    this.name = name;\n\n    // drive state\n    this.drive_type = CMOS_FDD_TYPE_NO_DRIVE;\n\n    // disk state\n    this.max_track = 0;     // disk's max. track, was: FloppyController.number_of_cylinders (qemu: max_track)\n    this.max_head = 0;      // disk's max. head (1 or 2), was: FloppyController.number_of_heads\n    this.max_sect = 0;      // disk's max. sect, was: FloppyController.sectors_per_track (qemu: last_sect)\n    this.curr_track = 0;    // >= 0, was: FloppyController.last_cylinder (qemu: track)\n    this.curr_head = 0;     // 0 or 1, was: FloppyController.last_head (qemu: head)\n    this.curr_sect = 1;     // > 0, was: FloppyController.last_sector (qemu: sect)\n    this.perpendicular = 0;\n    this.read_only = false;\n    this.media_changed = true;\n    this.buffer = null;\n\n    Object.seal(this);\n\n    // Drive type this.drive_type is either (in this order):\n    // - specified in fdd_config.drive_type (if defined),\n    // - derived from given image buffer (if provided), or\n    // - specfied in fallback_drive_type.\n    // If buffer is undefined and fdd_config.drive_type is\n    // CMOS_FDD_TYPE_NO_DRIVE then the drive will be invisible to the guest.\n    const cfg_drive_type = fdd_config?.drive_type;\n    if(cfg_drive_type !== undefined && cfg_drive_type >= 0 && cfg_drive_type <= 5)\n    {\n        this.drive_type = cfg_drive_type;\n    }\n\n    this.insert_disk(buffer, !! fdd_config?.read_only);\n\n    if(this.drive_type === CMOS_FDD_TYPE_NO_DRIVE && cfg_drive_type === undefined)\n    {\n        this.drive_type = fallback_drive_type;\n    }\n\n    dbg_log(\"floppy drive \" + this.name + \" ready, drive type: \" + this.drive_type, LOG_FLOPPY);\n}\n\n/**\n * Insert disk image into floppy drive.\n *\n * @param {SyncBuffer|Uint8Array|null|undefined} buffer\n * @param {boolean=} read_only\n * @return {boolean} true if the given buffer was accepted\n */\nFloppyDrive.prototype.insert_disk = function(buffer, read_only)\n{\n    if(!buffer)\n    {\n        return false;\n    }\n\n    if(buffer instanceof Uint8Array)\n    {\n        buffer = new SyncBuffer(buffer.buffer);\n    }\n\n    const [new_buffer, disk_format] = this.find_disk_format(buffer, this.drive_type);\n    if(!new_buffer)\n    {\n        dbg_log(\"WARNING: disk rejected, no suitable disk format found for image of size \" +\n            buffer.byteLength + \" bytes\", LOG_FLOPPY);\n        return false;\n    }\n\n    this.max_track = disk_format.tracks;\n    this.max_head = disk_format.heads;\n    this.max_sect = disk_format.sectors;\n    this.read_only = !!read_only;\n    this.media_changed = true;\n    this.buffer = new_buffer;\n\n    if(this.drive_type === CMOS_FDD_TYPE_NO_DRIVE)\n    {\n        // auto-select drive type once during construction\n        this.drive_type = disk_format.drive_type;\n    }\n\n    if(DEBUG)\n    {\n        dbg_log(\"disk inserted into \" + this.name + \": type: \" + disk_format.drive_type +\n            \", C/H/S: \" + disk_format.tracks + \"/\" + disk_format.heads + \"/\" +\n            disk_format.sectors + \", size: \" + new_buffer.byteLength,\n            LOG_FLOPPY);\n    }\n    return true;\n};\n\n/**\n * Eject disk from this floppy drive.\n */\nFloppyDrive.prototype.eject_disk = function()\n{\n    this.max_track = 0;\n    this.max_head = 0;\n    this.max_sect = 0;\n    this.media_changed = true;\n    this.buffer = null;\n};\n\n/**\n * Returns this drive's current image buffer or null if no disk is inserted.\n * @return {Uint8Array|null}\n */\nFloppyDrive.prototype.get_buffer = function()\n{\n    return this.buffer ? new Uint8Array(this.buffer.buffer) : null;\n};\n\n/**\n * Map structured C/H/S address to linear block address (LBA).\n * @param {number} track\n * @param {number} head\n * @param {number} sect\n * @return {number} lba the linear block address\n */\nFloppyDrive.prototype.chs2lba = function(track, head, sect)\n{\n    return (track * this.max_head + head) * this.max_sect + sect - 1;\n};\n\n/**\n * Find best-matching disk format for the given image buffer.\n *\n * If the given drive_type is CMOS_FDD_TYPE_NO_DRIVE then drive types are\n * ignored and the first matching disk format is selected (auto-detect).\n *\n * If the size of the given buffer does not match any known floppy disk\n * format then the smallest format larger than the image buffer is\n * selected, and buffer is copied into a new, format-sized buffer with\n * trailing zeroes.\n *\n * Returns [null, null] if the given buffer is larger than any known\n * floppy disk format.\n *\n * @param {SyncBuffer} buffer\n * @param {number} drive_type\n * @return [{SyncBuffer|null}, {Object|null}] [buffer, disk_format]\n */\nFloppyDrive.prototype.find_disk_format = function(buffer, drive_type)\n{\n    const autodetect = drive_type === CMOS_FDD_TYPE_NO_DRIVE;\n    const buffer_size = buffer.byteLength;\n\n    let preferred_match=-1, medium_match=-1, size_match=-1, nearest_match=-1, nearest_size=-1;\n    for(let i = 0; i < DISK_FORMATS.length; i++)\n    {\n        const disk_format = DISK_FORMATS[i];\n        const disk_size = disk_format.sectors * disk_format.tracks * disk_format.heads * SECTOR_SIZE;\n        if(buffer_size === disk_size)\n        {\n            if(autodetect || disk_format.drive_type === drive_type)\n            {\n                // (1) same size and CMOS drive type\n                preferred_match = i;\n                break;\n            }\n            else if(!autodetect && CMOS_FDD_TYPE_MEDIUM[disk_format.drive_type] === CMOS_FDD_TYPE_MEDIUM[drive_type])\n            {\n                // (2) same size and physical medium size (5\"1/4 or 3\"1/2)\n                medium_match = (medium_match === -1) ? i : medium_match;\n            }\n            else\n            {\n                // (3) same size\n                size_match = (size_match === -1) ? i : size_match;\n            }\n        }\n        else if(buffer_size < disk_size)\n        {\n            if(nearest_size === -1 || disk_size < nearest_size)\n            {\n                // (4) nearest matching size\n                nearest_match = i;\n                nearest_size = disk_size;\n            }\n        }\n    }\n\n    if(preferred_match !== -1)\n    {\n        return [buffer, DISK_FORMATS[preferred_match]];\n    }\n    else if(medium_match !== -1)\n    {\n        return [buffer, DISK_FORMATS[medium_match]];\n    }\n    else if(size_match !== -1)\n    {\n        return [buffer, DISK_FORMATS[size_match]];\n    }\n    else if(nearest_match !== -1)\n    {\n        const tmp_buffer = new Uint8Array(nearest_size);\n        tmp_buffer.set(new Uint8Array(buffer.buffer));\n        return [new SyncBuffer(tmp_buffer.buffer), DISK_FORMATS[nearest_match]];\n    }\n    else\n    {\n        return [null, null];\n    }\n};\n\n/**\n * Seek to a new position, returns:\n *   0 if already on right track\n *   1 if track changed\n *   2 if track is invalid\n *   3 if sector is invalid\n *\n * @param {number} head\n * @param {number} track\n * @param {number} sect\n * @return {number}\n */\nFloppyDrive.prototype.seek = function(head, track, sect)\n{\n    if((track > this.max_track) || (head !== 0 && this.max_head === 1))\n    {\n        dbg_log(\"WARNING: attempt to seek to invalid track: head: \" + head + \", track: \" + track + \", sect: \" + sect, LOG_FLOPPY);\n        return 2;\n    }\n    if(sect > this.max_sect)\n    {\n        dbg_log(\"WARNING: attempt to seek beyond last sector: \" + sect + \" (max: \" + this.max_sect + \")\", LOG_FLOPPY);\n        return 3;\n    }\n\n    let result = 0;\n    const curr_lba = this.chs2lba(this.curr_track, this.curr_head, this.curr_sect);\n    const new_lba = this.chs2lba(track, head, sect);\n    if(curr_lba !== new_lba)\n    {\n        if(this.curr_track !== track)\n        {\n            if(this.buffer)\n            {\n                this.media_changed = false;\n            }\n            result = 1;\n        }\n        this.curr_head = head;\n        this.curr_track = track;\n        this.curr_sect = sect;\n    }\n\n    if(!this.buffer)\n    {\n        result = 2;\n    }\n\n    return result;\n};\n\nFloppyDrive.prototype.get_state = function()\n{\n    const state = [];\n    state[0]  = this.drive_type;\n    state[1]  = this.max_track;\n    state[2]  = this.max_head;\n    state[3]  = this.max_sect;\n    state[4]  = this.curr_track;\n    state[5]  = this.curr_head;\n    state[6]  = this.curr_sect;\n    state[7]  = this.perpendicular;\n    state[8]  = this.read_only;\n    state[9]  = this.media_changed;\n    state[10] = this.buffer ? this.buffer.get_state() : null;   // SyncBuffer\n    return state;\n};\n\nFloppyDrive.prototype.set_state = function(state)\n{\n    this.drive_type = state[0];\n    this.max_track = state[1];\n    this.max_head = state[2];\n    this.max_sect = state[3];\n    this.curr_track = state[4];\n    this.curr_head = state[5];\n    this.curr_sect = state[6];\n    this.perpendicular = state[7];\n    this.read_only = state[8];\n    this.media_changed = state[9];\n    if(state[10])\n    {\n        if(!this.buffer)\n        {\n            this.buffer = new SyncBuffer(new ArrayBuffer(0));\n        }\n        this.buffer.set_state(state[10]);\n    }\n    else\n    {\n        this.buffer = null;\n    }\n};\n"
  },
  {
    "path": "src/ide.js",
    "content": "import { LOG_DISK } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\nimport { CMOS_BIOS_DISKTRANSFLAG, CMOS_DISK_DATA, CMOS_DISK_DRIVE1_CYL, CMOS_DISK_DRIVE2_CYL } from \"./rtc.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { BusConnector } from \"./bus.js\";\n\n// ATA/ATAPI-6/8 IDE Controller\n//\n// References\n// - [ATA8-ACS]\n//   ATA/ATAPI Command Set - 3 (ACS-3) (Rev. 5, Oct. 28, 2013)\n//   https://read.seas.harvard.edu/cs161/2019/pdf/ata-atapi-8.pdf\n// - [ATA-6]\n//   AT Attachment with Packet Interface - 6 (ATA/ATAPI-6) (Rev. 3a; Dec. 14, 2001)\n//   https://technion-csl.github.io/ose/readings/hardware/ATA-d1410r3a.pdf\n// - [CD-SCSI-2]\n//   PROPOSAL FOR CD-ROM IN SCSI-2 (X3T9.2/87) (Rev. 0, Jun. 30, 1987)\n//   https://www.t10.org/ftp/x3t9.2/document.87/87-106r0.txt\n//   https://www.t10.org/ftp/x3t9.2/document.87/87-106r1.txt (errata to r0)\n// - [SAM-3]\n//   SCSI Architecture Model - 3 (SAM-3) (Sep. 21, 2004)\n//   https://dn790004.ca.archive.org/0/items/SCSISpecificationDocumentsSCSIDocuments/SCSI%20Architecture%20Model/SCSI%20Architecture%20Model%203%20rev%2014.pdf\n// - [SPC-3]\n//   SCSI Primary Commands - 3 (SPC-3) (July 20, 2008)\n//   https://www.t10.org/ftp/t10/document.08/08-309r0.pdf\n// - [MMC-3]\n//   SCSI Multimedia Commands - 3 (MMC-3) (Rev. 10g, Nov. 12, 2001)\n//   https://ia902808.us.archive.org/33/items/mmc3r10g/mmc3r10g.pdf\n// - [MMC-2]\n//   Packet Commands for C/DVD Devices (1997)\n//   https://www.t10.org/ftp/t10/document.97/97-108r0.pdf\n// - [BMI-1]\n//   Programming Interface for Bus Master IDE Controller, Revision 1.0, 5/16/94\n//   https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/u9proj/idems100.pdf\n// - [SFF-8020]\n//   ATA Packet Interface for CD-ROMs (Rev. 1.2, Feb. 12, 1994)\n//   https://dn790009.ca.archive.org/0/items/SCSISpecificationDocumentsATAATAPI/SFF-8020_%20ATA%20Packet%20Interface%20for%20CD-ROMs%20-%20SFF.pdf\n\nconst CDROM_SECTOR_SIZE = 2048;\nconst HD_SECTOR_SIZE = 512;\n\nconst BUS_MASTER_BASE = 0xB400;\n\n// Per-channel ATA register offsets, legend:\n//   (*1*) Control block register (BAR1/3), else: Command block register (BAR0/2)\n// Read-only registers:\nconst ATA_REG_ERROR      = 0x01;  // Error register, see [ATA-6] 7.9\nconst ATA_REG_STATUS     = 0x07;  // Status register, see [ATA-6] 7.15\nconst ATA_REG_ALT_STATUS = 0x00;  // (*1*) Alternate Status register, see [ATA-6] 7.3\n// Read-/writable registers:\nconst ATA_REG_DATA       = 0x00;  // Data register, see [ATA-6] 7.6\nconst ATA_REG_SECTOR     = 0x02;  // Sector Count register, see [ATA-6] 7.14\nconst ATA_REG_LBA_LOW    = 0x03;  // LBA Low register, see [ATA-6] 7.12\nconst ATA_REG_LBA_MID    = 0x04;  // LBA Mid register, see [ATA-6] 7.13\nconst ATA_REG_LBA_HIGH   = 0x05;  // LBA High register, see [ATA-6] 7.11\nconst ATA_REG_DEVICE     = 0x06;  // Device register, see [ATA-6] 7.7\n// Write-only registers:\nconst ATA_REG_FEATURES   = 0x01;  // Features register, see [ATA-6] 7.10\nconst ATA_REG_COMMAND    = 0x07;  // Command register, see [ATA-6] 7.4\nconst ATA_REG_CONTROL    = 0x00;  // (*1*) Device Control register, see [ATA-6] 7.8\n\n// Per-channel Bus Master IDE register offsets (BAR4), see [BMI-1] 2.0\n// these are the primary channel's offsets, add 8 for secondary\nconst BMI_REG_COMMAND = 0x00;     // Bus Master IDE Command register\nconst BMI_REG_STATUS = 0x02;      // Bus Master IDE Status register\nconst BMI_REG_PRDT = 0x04;        // Bus Master IDE PRD Table Address register\n\n// Error register bits:\n// All bits except for bit 0x04 are command dependent.\nconst ATA_ER_ABRT = 0x04;  // Command aborted\n\n// Status register bits:\nconst ATA_SR_ERR  = 0x01;  // Error (ATA)\nconst ATA_SR_COND = 0x01;  // Check Condition (ATAPI)\nconst ATA_SR_SENS = 0x02;  // Sense Available (ATAPI)\nconst ATA_SR_AERR = 0x04;  // Alignment Error\nconst ATA_SR_DRQ  = 0x08;  // Data Request\nconst ATA_SR_DSC  = 0x10;  // Drive Seek Complete / Deferred Write Error\nconst ATA_SR_DF   = 0x20;  // Device Fault / Stream Error\nconst ATA_SR_DRDY = 0x40;  // Drive Ready\nconst ATA_SR_BSY  = 0x80;  // Busy\n\n// Device register bits:\n// Bits 0x20/0x80 are obsolete and 0x01/0x02/0x04/0x08/0x40 are command dependent.\nconst ATA_DR_DEV = 0x10;   // Device select; slave device if set, else master device\n\n// Device Control register bits:\n// Bits 0x08/0x10/0x20/0x40 are reserved and bit 0x01 is always zero.\nconst ATA_CR_NIEN = 0x02;  // Interrupt disable (not Interrupt ENable)\nconst ATA_CR_SRST = 0x04;  // Software reset\nconst ATA_CR_HOB = 0x80;   // 48-bit Address feature set\n\n// ATA commands\nconst ATA_CMD_DEVICE_RESET = 0x08;                    // see [ATA8-ACS] 7.6\nconst ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC = 0x90;       // see [ATA8-ACS] 7.9\nconst ATA_CMD_FLUSH_CACHE = 0xE7;                     // see [ATA8-ACS] 7.10\nconst ATA_CMD_FLUSH_CACHE_EXT = 0xEA;                 // see [ATA8-ACS] 7.11\nconst ATA_CMD_GET_MEDIA_STATUS = 0xDA;                // see [ATA-6] 8.14\nconst ATA_CMD_IDENTIFY_DEVICE = 0xEC;                 // see [ATA8-ACS] 7.12\nconst ATA_CMD_IDENTIFY_PACKET_DEVICE = 0xA1;          // see [ATA8-ACS] 7.13\nconst ATA_CMD_IDLE_IMMEDIATE = 0xE1;                  // see [ATA8-ACS] 7.15\nconst ATA_CMD_INITIALIZE_DEVICE_PARAMETERS = 0x91;    // not mentioned in [ATA-6] or [ATA8-ACS]\nconst ATA_CMD_MEDIA_LOCK = 0xDE;                      // see [ATA-6] 8.20\nconst ATA_CMD_NOP = 0x00;                             // see [ATA8-ACS] 7.17\nconst ATA_CMD_PACKET = 0xA0;                          // see [ATA8-ACS] 7.18\nconst ATA_CMD_READ_DMA = 0xC8;                        // see [ATA8-ACS] 7.21\nconst ATA_CMD_READ_DMA_EXT = 0x25;                    // see [ATA8-ACS] 7.22\nconst ATA_CMD_READ_MULTIPLE = 0x29;                   // see [ATA8-ACS] 7.26\nconst ATA_CMD_READ_MULTIPLE_EXT = 0xC4;               // see [ATA8-ACS] 7.27\nconst ATA_CMD_READ_NATIVE_MAX_ADDRESS = 0xF8;         // see [ATA-6] 8.32\nconst ATA_CMD_READ_NATIVE_MAX_ADDRESS_EXT = 0x27;     // see [ATA-6] 8.33\nconst ATA_CMD_READ_SECTORS = 0x20;                    // see [ATA8-ACS] 7.28\nconst ATA_CMD_READ_SECTORS_EXT = 0x24;                // see [ATA8-ACS] 7.29\nconst ATA_CMD_READ_VERIFY_SECTORS = 0x40;             // see [ATA8-ACS] 7.32\nconst ATA_CMD_SECURITY_FREEZE_LOCK = 0xF5;            // see [ATA8-ACS] 7.40\nconst ATA_CMD_SET_FEATURES = 0xEF;                    // see [ATA8-ACS] 7.45\nconst ATA_CMD_SET_MAX = 0xF9;                         // see [ATA-6] 8.47\nconst ATA_CMD_SET_MULTIPLE_MODE = 0xC6;               // see [ATA8-ACS] 7.46\nconst ATA_CMD_STANDBY_IMMEDIATE = 0xE0;               // see [ATA8-ACS] 7.50\nconst ATA_CMD_WRITE_DMA = 0xCA;                       // see [ATA8-ACS] 7.58\nconst ATA_CMD_WRITE_DMA_EXT = 0x35;                   // see [ATA8-ACS] 7.59\nconst ATA_CMD_WRITE_MULTIPLE = 0x39;                  // see [ATA8-ACS] 7.64\nconst ATA_CMD_WRITE_MULTIPLE_EXT = 0xC5;              // see [ATA8-ACS] 7.65\nconst ATA_CMD_WRITE_SECTORS = 0x30;                   // see [ATA8-ACS] 7.67\nconst ATA_CMD_WRITE_SECTORS_EXT = 0x34;               // see [ATA8-ACS] 7.68\nconst ATA_CMD_10h = 0x10;                             // command obsolete/unknown, see [ATA-6] Table E.2\nconst ATA_CMD_F0h = 0xF0;                             // vendor-specific\n\nconst ATA_CMD_NAME =\n{\n    [ATA_CMD_DEVICE_RESET]:                 \"DEVICE RESET\",\n    [ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC]:    \"EXECUTE DEVICE DIAGNOSTIC\",\n    [ATA_CMD_FLUSH_CACHE]:                  \"FLUSH CACHE\",\n    [ATA_CMD_FLUSH_CACHE_EXT]:              \"FLUSH CACHE EXT\",\n    [ATA_CMD_GET_MEDIA_STATUS]:             \"GET MEDIA STATUS\",\n    [ATA_CMD_IDENTIFY_DEVICE]:              \"IDENTIFY DEVICE\",\n    [ATA_CMD_IDENTIFY_PACKET_DEVICE]:       \"IDENTIFY PACKET DEVICE\",\n    [ATA_CMD_IDLE_IMMEDIATE]:               \"IDLE IMMEDIATE\",\n    [ATA_CMD_INITIALIZE_DEVICE_PARAMETERS]: \"INITIALIZE DEVICE PARAMETERS\",\n    [ATA_CMD_MEDIA_LOCK]:                   \"MEDIA LOCK\",\n    [ATA_CMD_NOP]:                          \"NOP\",\n    [ATA_CMD_PACKET]:                       \"PACKET\",\n    [ATA_CMD_READ_DMA]:                     \"READ DMA\",\n    [ATA_CMD_READ_DMA_EXT]:                 \"READ DMA EXT\",\n    [ATA_CMD_READ_MULTIPLE]:                \"READ MULTIPLE\",\n    [ATA_CMD_READ_MULTIPLE_EXT]:            \"READ MULTIPLE EXT\",\n    [ATA_CMD_READ_NATIVE_MAX_ADDRESS]:      \"READ NATIVE MAX ADDRESS\",\n    [ATA_CMD_READ_NATIVE_MAX_ADDRESS_EXT]:  \"READ NATIVE MAX ADDRESS EXT\",\n    [ATA_CMD_READ_SECTORS]:                 \"READ SECTORS\",\n    [ATA_CMD_READ_SECTORS_EXT]:             \"READ SECTORS EXT\",\n    [ATA_CMD_READ_VERIFY_SECTORS]:          \"READ VERIFY SECTORS\",\n    [ATA_CMD_SECURITY_FREEZE_LOCK]:         \"SECURITY FREEZE LOCK\",\n    [ATA_CMD_SET_FEATURES]:                 \"SET FEATURES\",\n    [ATA_CMD_SET_MAX]:                      \"SET MAX\",\n    [ATA_CMD_SET_MULTIPLE_MODE]:            \"SET MULTIPLE MODE\",\n    [ATA_CMD_STANDBY_IMMEDIATE]:            \"STANDBY IMMEDIATE\",\n    [ATA_CMD_WRITE_DMA]:                    \"WRITE DMA\",\n    [ATA_CMD_WRITE_DMA_EXT]:                \"WRITE DMA EXT\",\n    [ATA_CMD_WRITE_MULTIPLE]:               \"WRITE MULTIPLE\",\n    [ATA_CMD_WRITE_MULTIPLE_EXT]:           \"WRITE MULTIPLE EXT\",\n    [ATA_CMD_WRITE_SECTORS]:                \"WRITE SECTORS\",\n    [ATA_CMD_WRITE_SECTORS_EXT]:            \"WRITE SECTORS EXT\",\n    [ATA_CMD_10h]:                          \"<UNKNOWN 10h>\",\n    [ATA_CMD_F0h]:                          \"<VENDOR-SPECIFIC F0h>\",\n};\n\n// ATAPI (SCSI-2/MMC-2) commands\nconst ATAPI_CMD_GET_CONFIGURATION = 0x46;             // see [CD-SCSI-2]\nconst ATAPI_CMD_GET_EVENT_STATUS_NOTIFICATION = 0x4A; // see [MMC-2] 9.1.2\nconst ATAPI_CMD_INQUIRY = 0x12;                       // see [MMC-2] 9.1.3\nconst ATAPI_CMD_MECHANISM_STATUS = 0xBD;              // see [MMC-2] 9.1.5\nconst ATAPI_CMD_MODE_SENSE_6 = 0x1A;                  // see [CD-SCSI-2]\nconst ATAPI_CMD_MODE_SENSE_10 = 0x5A;                 // see [MMC-2] 9.1.7\nconst ATAPI_CMD_PAUSE = 0x45;                         // see [CD-SCSI-2]\nconst ATAPI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL = 0x1E;  // see [MMC-2] 9.1.9\nconst ATAPI_CMD_READ_10 = 0x28;                       // see [CD-SCSI-2]\nconst ATAPI_CMD_READ_12 = 0xA8;                       // see [SFF-8020] 9.8.14\nconst ATAPI_CMD_READ_CAPACITY = 0x25;                 // see [MMC-2] 9.1.12\nconst ATAPI_CMD_READ_CD = 0xBE;                       // see [CD-SCSI-2]\nconst ATAPI_CMD_READ_DISK_INFORMATION = 0x51;         // see [CD-SCSI-2]\nconst ATAPI_CMD_READ_SUBCHANNEL = 0x42;               // see [CD-SCSI-2]\nconst ATAPI_CMD_READ_TOC_PMA_ATIP = 0x43;             // see [CD-SCSI-2]\nconst ATAPI_CMD_READ_TRACK_INFORMATION = 0x52;        // see [CD-SCSI-2]\nconst ATAPI_CMD_REQUEST_SENSE = 0x03;                 // see [MMC-2] 9.1.18\nconst ATAPI_CMD_START_STOP_UNIT = 0x1B;               // see [CD-SCSI-2]\nconst ATAPI_CMD_TEST_UNIT_READY = 0x00;               // see [MMC-2] 9.1.20\n\n// ATAPI command flags\nconst ATAPI_CF_NONE = 0x00;         // no flags\nconst ATAPI_CF_NEEDS_DISK = 0x01;   // command needs inserted disk\nconst ATAPI_CF_UNIT_ATTN = 0x02;    // bounce command if unit attention condition is active\n\n// ATAPI commands, for flags see [MMC-3] 4.2.6\nconst ATAPI_CMD =\n{\n    [ATAPI_CMD_GET_CONFIGURATION]:             {name: \"GET CONFIGURATION\",             flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_GET_EVENT_STATUS_NOTIFICATION]: {name: \"GET EVENT STATUS NOTIFICATION\", flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_INQUIRY]:                       {name: \"INQUIRY\",                       flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_MECHANISM_STATUS]:              {name: \"MECHANISM STATUS\",              flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_MODE_SENSE_6]:                  {name: \"MODE SENSE (6)\",                flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_MODE_SENSE_10]:                 {name: \"MODE SENSE (10)\",               flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_PAUSE]:                         {name: \"PAUSE\",                         flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL]:  {name: \"PREVENT ALLOW MEDIUM REMOVAL\",  flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_READ_10]:                       {name: \"READ (10)\",                     flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_12]:                       {name: \"READ (12)\",                     flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_CAPACITY]:                 {name: \"READ CAPACITY\",                 flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_CD]:                       {name: \"READ CD\",                       flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_DISK_INFORMATION]:         {name: \"READ DISK INFORMATION\",         flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_SUBCHANNEL]:               {name: \"READ SUBCHANNEL\",               flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_TOC_PMA_ATIP]:             {name: \"READ TOC PMA ATIP\",             flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_READ_TRACK_INFORMATION]:        {name: \"READ TRACK INFORMATION\",        flags: ATAPI_CF_NEEDS_DISK},\n    [ATAPI_CMD_REQUEST_SENSE]:                 {name: \"REQUEST SENSE\",                 flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_START_STOP_UNIT]:               {name: \"START STOP UNIT\",               flags: ATAPI_CF_NONE},\n    [ATAPI_CMD_TEST_UNIT_READY]:               {name: \"TEST UNIT READY\",               flags: ATAPI_CF_NEEDS_DISK},\n};\n\n// ATAPI device signature\nconst ATAPI_SIGNATURE_LO = 0x14;\nconst ATAPI_SIGNATURE_HI = 0xEB;\n\n// ATAPI 4-bit Sense Keys, see [MMC-2] 9.1.18.3, Table 123\nconst ATAPI_SK_NO_SENSE = 0;\nconst ATAPI_SK_RECOVERED_ERROR = 1;\nconst ATAPI_SK_NOT_READY = 2;\nconst ATAPI_SK_MEDIUM_ERROR = 3;\nconst ATAPI_SK_HARDWARE_ERROR = 4;\nconst ATAPI_SK_ILLEGAL_REQUEST = 5;\nconst ATAPI_SK_UNIT_ATTENTION = 6;\nconst ATAPI_SK_DATA_PROTECT = 7;\nconst ATAPI_SK_BLANK_CHECK = 8;\nconst ATAPI_SK_ABORTED_COMMAND = 11;\n\n// ATAPI 8-bit Additional Sense Codes, see [MMC-2] 9.1.18.3, Table 124\n// https://github.com/qemu/qemu/blob/3c5a5e213e5f08fbfe70728237f7799ac70f5b99/hw/ide/ide-internal.h#L288\nconst ATAPI_ASC_INV_FIELD_IN_CMD_PACKET = 0x24;\nconst ATAPI_ASC_MEDIUM_MAY_HAVE_CHANGED = 0x28;\nconst ATAPI_ASC_MEDIUM_NOT_PRESENT = 0x3A;\n\n// Debug log detail bits (internal to this module)\nconst LOG_DETAIL_NONE = 0x00;   // disable debug logging of details\nconst LOG_DETAIL_REG_IO = 0x01; // log register read/write access\nconst LOG_DETAIL_IRQ = 0x02;    // log IRQ raise/lower events\nconst LOG_DETAIL_RW = 0x04;     // log data read/write-related events\nconst LOG_DETAIL_RW_DMA = 0x08; // log DMA data read/write-related events\nconst LOG_DETAIL_CHS = 0x10;    // log register-CHS to LBA conversions\nconst LOG_DETAIL_ALL = 0xFF;    // log all details\n// the bitset of active log details (should be 0 when not in DEBUG mode)\nconst LOG_DETAILS = DEBUG ? LOG_DETAIL_NONE : 0;\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n *\n * ide_config: [ [primary-master, primary-slave], [secondary-master, secondary-slave] ]\n *\n *   Each of the four arguments (primary-master, primary-slave, ...) is either\n *   undefined or an object of the form:\n *\n *       { buffer: Uint8Array, is_cdrom: bool }\n *\n *   If is_cdrom is defined and true:\n *   - If buffer is defined: create an ATAPI CD-ROM device using buffer as inserted disk\n *   - If buffer is undefined: create an ATAPI CD-ROM device with ejectd disk\n *   If is_cdrom is undefined or false:\n *   - If buffer is defined: create an ATA Hard-Disk device using buffer as disk image\n *   - If buffer is undefined: represents a missing device\n *\n *   A slave drive can only exist if a master drive also exists.\n * */\nexport function IDEController(cpu, bus, ide_config)\n{\n    this.cpu = cpu;\n    this.bus = bus;\n\n    this.primary = undefined;\n    this.secondary = undefined;\n\n    const has_primary = ide_config && ide_config[0][0];\n    const has_secondary = ide_config && ide_config[1][0];\n    if(has_primary || has_secondary)\n    {\n        if(has_primary)\n        {\n            this.primary = new IDEChannel(this, 0, ide_config[0], 0x1F0, 0x3F6, 14);\n        }\n        if(has_secondary)\n        {\n            this.secondary = new IDEChannel(this, 1, ide_config[1], 0x170, 0x376, 15);\n        }\n\n        const vendor_id = 0x8086;    // Intel Corporation\n        const device_id = 0x7010;    // 82371SB PIIX3 IDE [Natoma/Triton II]\n        const class_code = 0x01;     // Mass Storage Controller\n        const subclass = 0x01;       // IDE Controller\n        const prog_if = 0x80;        // ISA Compatibility mode-only controller, supports bus mastering\n        const interrupt_line = 0x00; // IRQs 14 and 15 are predefined in Compatibility mode and this field is ignored\n        const command_base0 = has_primary ? this.primary.command_base : 0;\n        const control_base0 = has_primary ? this.primary.control_base : 0;\n        const command_base1 = has_secondary ? this.secondary.command_base : 0;\n        const control_base1 = has_secondary ? this.secondary.control_base : 0;\n\n        this.name = \"ide\";\n        this.pci_id = 0x1E << 3;\n        this.pci_space = [\n            vendor_id & 0xFF, vendor_id >> 8, device_id & 0xFF, device_id >> 8, 0x05, 0x00, 0xA0, 0x02,\n            0x00, prog_if, subclass, class_code, 0x00, 0x00, 0x00, 0x00,\n            command_base0 & 0xFF   | 1, command_base0 >> 8,   0x00, 0x00,\n            control_base0 & 0xFF   | 1, control_base0 >> 8,   0x00, 0x00,\n            command_base1 & 0xFF   | 1, command_base1 >> 8,   0x00, 0x00,\n            control_base1 & 0xFF   | 1, control_base1 >> 8,   0x00, 0x00,\n            BUS_MASTER_BASE & 0xFF | 1, BUS_MASTER_BASE >> 8, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00,\n            0x43, 0x10, 0xD4, 0x82,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, interrupt_line, 0x01, 0x00, 0x00,\n            // 0x40\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            // 0x80\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n        ];\n        this.pci_bars = [\n            has_primary ? { size: 8 } : undefined,   // BAR0: Command block register address of primary channel\n            has_primary ? { size: 1 } : undefined,   // BAR1: Control block register address of primary channel\n            has_secondary ? { size: 8 } : undefined, // BAR2: Command block register address of secondary channel\n            has_secondary ? { size: 1 } : undefined, // BAR3: Control block register address of secondary channel\n            { size: 16 }                             // BAR4: Bus Master I/O register address of both channels (8+8)\n        ];\n        cpu.devices.pci.register_device(this);\n    }\n\n    Object.seal(this);\n}\n\nIDEController.prototype.get_state = function()\n{\n    const state = [];\n    state[0] = this.primary;\n    state[1] = this.secondary;\n    return state;\n};\n\nIDEController.prototype.set_state = function(state)\n{\n    this.primary && this.primary.set_state(state[0]);\n    this.secondary && this.secondary.set_state(state[1]);\n};\n\n/**\n * @constructor\n * @param {IDEController} controller\n * @param {number} channel_nr\n * */\nfunction IDEChannel(controller, channel_nr, channel_config, command_base, control_base, irq)\n{\n    this.controller = controller;\n    this.channel_nr = channel_nr;\n    this.cpu = controller.cpu;\n    this.bus = controller.bus;\n    this.command_base = command_base;\n    this.control_base = control_base;\n    this.irq = irq;\n    this.name = \"ide\" + channel_nr;\n\n    const master_cfg = channel_config ? channel_config[0] : undefined;\n    const slave_cfg = channel_config ? channel_config[1] : undefined;\n    this.master = new IDEInterface(this, 0, master_cfg?.buffer, master_cfg?.is_cdrom);\n    this.slave = new IDEInterface(this, 1, slave_cfg?.buffer, slave_cfg?.is_cdrom);\n\n    this.current_interface = this.master;\n\n    /** @type {number} */\n    this.device_control_reg = ATA_CR_NIEN;\n\n    /** @type {number} */\n    this.prdt_addr = 0;\n\n    /** @type {number} */\n    this.dma_status = 0;\n\n    /** @type {number} */\n    this.dma_command = 0;\n\n    const cpu = controller.cpu;\n\n    //\n    // Command Block Registers: command_base + 0...7 (BAR0: 1F0h, BAR2: 170h)\n    //\n\n    cpu.io.register_read(this.command_base | ATA_REG_DATA, this, function()\n    {\n        return this.current_interface.read_data(1);\n    }, function()\n    {\n        return this.current_interface.read_data(2);\n    }, function()\n    {\n        return this.current_interface.read_data(4);\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_ERROR, this, function()\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": read Error register: \" +\n                h(this.current_interface.error_reg & 0xFF), LOG_DISK);\n        }\n        return this.current_interface.error_reg & 0xFF;\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_SECTOR, this, function()\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": read Sector Count register: \" +\n                h(this.current_interface.sector_count_reg & 0xFF), LOG_DISK);\n        }\n        return this.current_interface.sector_count_reg & 0xFF;\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_LBA_LOW, this, function()\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": read LBA Low register: \" +\n                h(this.current_interface.lba_low_reg & 0xFF), LOG_DISK);\n        }\n        return this.current_interface.lba_low_reg & 0xFF;\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_LBA_MID, this, function()\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": read LBA Mid register: \" +\n                h(this.current_interface.lba_mid_reg & 0xFF), LOG_DISK);\n        }\n        return this.current_interface.lba_mid_reg & 0xFF;\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_LBA_HIGH, this, function()\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": read LBA High register: \" +\n                h(this.current_interface.lba_high_reg & 0xFF), LOG_DISK);\n        }\n        return this.current_interface.lba_high_reg & 0xFF;\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_DEVICE, this, function()\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": read Device register\", LOG_DISK);\n        }\n        return this.current_interface.device_reg & 0xFF;\n    });\n\n    cpu.io.register_read(this.command_base | ATA_REG_STATUS, this, function()\n    {\n        const status = this.read_status();\n        if(LOG_DETAILS & (LOG_DETAIL_REG_IO | LOG_DETAIL_IRQ))\n        {\n            dbg_log(`${this.current_interface.name}: read Status register: ${h(status, 2)} (lower IRQ ${this.irq})`, LOG_DISK);\n        }\n        this.cpu.device_lower_irq(this.irq);\n        return status;\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_DATA, this, function(data)\n    {\n        this.current_interface.write_data_port8(data);\n    }, function(data)\n    {\n        this.current_interface.write_data_port16(data);\n    }, function(data)\n    {\n        this.current_interface.write_data_port32(data);\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_FEATURES, this, function(data)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write Features register: \" + h(data), LOG_DISK);\n        }\n        this.current_interface.features_reg = (this.current_interface.features_reg << 8 | data) & 0xFFFF;\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_SECTOR, this, function(data)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write Sector Count register: \" + h(data), LOG_DISK);\n        }\n        this.current_interface.sector_count_reg = (this.current_interface.sector_count_reg << 8 | data) & 0xFFFF;\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_LBA_LOW, this, function(data)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write LBA Low register: \" + h(data), LOG_DISK);\n        }\n        this.current_interface.lba_low_reg = (this.current_interface.lba_low_reg << 8 | data) & 0xFFFF;\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_LBA_MID, this, function(data)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write LBA Mid register: \" + h(data), LOG_DISK);\n        }\n        this.current_interface.lba_mid_reg = (this.current_interface.lba_mid_reg << 8 | data) & 0xFFFF;\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_LBA_HIGH, this, function(data)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write LBA High register: \" + h(data), LOG_DISK);\n        }\n        this.current_interface.lba_high_reg = (this.current_interface.lba_high_reg << 8 | data) & 0xFFFF;\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_DEVICE, this, function(data)\n    {\n        const select_slave = data & ATA_DR_DEV;\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write Device register: \" + h(data, 2), LOG_DISK);\n        }\n        if((select_slave && this.current_interface === this.master) || (!select_slave && this.current_interface === this.slave))\n        {\n            if(select_slave)\n            {\n                dbg_log(`${this.current_interface.name}: select slave device (${this.channel_nr ? \"secondary\" : \"primary\"})`, LOG_DISK);\n                this.current_interface = this.slave;\n            }\n            else\n            {\n                dbg_log(`${this.current_interface.name}: select master device (${this.channel_nr ? \"secondary\" : \"primary\"})`, LOG_DISK);\n                this.current_interface = this.master;\n            }\n        }\n        this.current_interface.device_reg = data;\n        this.current_interface.is_lba = data >> 6 & 1; // TODO: where does this definition of bit 6 come from? not in [ATA-6] or [ATA-4]!\n        this.current_interface.head = data & 0xF;      // TODO: same for lower nibble?\n    });\n\n    cpu.io.register_write(this.command_base | ATA_REG_COMMAND, this, function(data)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_REG_IO)\n        {\n            dbg_log(this.current_interface.name + \": write Command register\", LOG_DISK);\n        }\n        this.current_interface.status_reg &= ~(ATA_SR_ERR|ATA_SR_DF);\n        this.current_interface.ata_command(data);\n        if(LOG_DETAILS & LOG_DETAIL_IRQ)\n        {\n            dbg_log(this.current_interface.name + \": lower IRQ \" + this.irq, LOG_DISK);\n        }\n        this.cpu.device_lower_irq(this.irq);\n    });\n\n    //\n    // Control Block Register: control_base (BAR1: 3F6h, BAR3: 376h)\n    //\n\n    // read Alternate Status register\n    cpu.io.register_read(this.control_base | ATA_REG_ALT_STATUS, this, this.read_status);\n\n    // write Device Control register\n    cpu.io.register_write(this.control_base | ATA_REG_CONTROL, this, this.write_control);\n\n    //\n    // Bus Master Registers: bus_master_base + 0...15 (BAR4: B400h)\n    // primary channel: bus_master_base + 0...7, secondary: bus_master_base + 8...15\n    //\n\n    const bus_master_base = BUS_MASTER_BASE + channel_nr * 8;\n\n    // read/write Bus Master IDE Command register\n    cpu.io.register_read(bus_master_base | BMI_REG_COMMAND,\n        this, this.dma_read_command8, undefined, this.dma_read_command);\n    cpu.io.register_write(bus_master_base | BMI_REG_COMMAND,\n        this, this.dma_write_command8, undefined, this.dma_write_command);\n\n    // read/write Bus Master IDE Status register\n    cpu.io.register_read(bus_master_base | BMI_REG_STATUS,\n        this, this.dma_read_status);\n    cpu.io.register_write(bus_master_base | BMI_REG_STATUS,\n        this, this.dma_write_status);\n\n    // read/write Bus Master IDE PRD Table Address register\n    cpu.io.register_read(bus_master_base | BMI_REG_PRDT,\n        this, undefined, undefined, this.dma_read_addr);\n    cpu.io.register_write(bus_master_base | BMI_REG_PRDT,\n        this, undefined, undefined, this.dma_set_addr);\n\n    DEBUG && Object.seal(this);\n}\n\nIDEChannel.prototype.read_status = function()\n{\n    return this.current_interface.drive_connected ? this.current_interface.status_reg : 0;\n};\n\nIDEChannel.prototype.write_control = function(data)\n{\n    if(LOG_DETAILS & (LOG_DETAIL_REG_IO | LOG_DETAIL_IRQ))\n    {\n        dbg_log(this.current_interface.name + \": write Device Control register: \" +\n            h(data, 2) + \" interrupts \" + ((data & ATA_CR_NIEN) ? \"disabled\" : \"enabled\"), LOG_DISK);\n    }\n    if(data & ATA_CR_SRST)\n    {\n        dbg_log(`${this.current_interface.name}: soft reset via control port (lower IRQ ${this.irq})`, LOG_DISK);\n        this.cpu.device_lower_irq(this.irq);\n        this.master.device_reset();\n        this.slave.device_reset();\n    }\n    this.device_control_reg = data;\n};\n\nIDEChannel.prototype.dma_read_addr = function()\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA get address: \" + h(this.prdt_addr, 8), LOG_DISK);\n    }\n    return this.prdt_addr;\n};\n\nIDEChannel.prototype.dma_set_addr = function(data)\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA set address: \" + h(data, 8), LOG_DISK);\n    }\n    this.prdt_addr = data;\n};\n\nIDEChannel.prototype.dma_read_status = function()\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA read status: \" + h(this.dma_status), LOG_DISK);\n    }\n    return this.dma_status;\n};\n\nIDEChannel.prototype.dma_write_status = function(value)\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA write status: \" + h(value), LOG_DISK);\n    }\n    this.dma_status &= ~(value & 6);\n};\n\nIDEChannel.prototype.dma_read_command = function()\n{\n    return this.dma_read_command8() | this.dma_read_status() << 16;\n};\n\nIDEChannel.prototype.dma_read_command8 = function()\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA read command: \" + h(this.dma_command), LOG_DISK);\n    }\n    return this.dma_command;\n};\n\nIDEChannel.prototype.dma_write_command = function(value)\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA write command: \" + h(value), LOG_DISK);\n    }\n    this.dma_write_command8(value & 0xFF);\n    this.dma_write_status(value >> 16 & 0xFF);\n};\n\nIDEChannel.prototype.dma_write_command8 = function(value)\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.current_interface.name + \": DMA write command8: \" + h(value), LOG_DISK);\n    }\n\n    const old_command = this.dma_command;\n    this.dma_command = value & 0x09;\n\n    if((old_command & 1) === (value & 1))\n    {\n        return;\n    }\n\n    if((value & 1) === 0)\n    {\n        this.dma_status &= ~1;\n        return;\n    }\n\n    this.dma_status |= 1;\n\n    switch(this.current_interface.current_command)\n    {\n        case ATA_CMD_READ_DMA:\n        case ATA_CMD_READ_DMA_EXT:\n            this.current_interface.do_ata_read_sectors_dma();\n            break;\n        case ATA_CMD_WRITE_DMA:\n        case ATA_CMD_WRITE_DMA_EXT:\n            this.current_interface.do_ata_write_sectors_dma();\n            break;\n        case ATA_CMD_PACKET:\n            this.current_interface.do_atapi_dma();\n            break;\n        default:\n            dbg_log(this.current_interface.name + \": spurious DMA command write, current command: \" +\n                    h(this.current_interface.current_command), LOG_DISK);\n            dbg_log(this.current_interface.name + \": DMA clear status bit 1h, set status bit 2h\", LOG_DISK);\n            this.dma_status &= ~1;\n            this.dma_status |= 2;\n            this.push_irq();\n            break;\n    }\n};\n\nIDEChannel.prototype.push_irq = function()\n{\n    if((this.device_control_reg & ATA_CR_NIEN) === 0)\n    {\n        if(LOG_DETAILS & LOG_DETAIL_IRQ)\n        {\n            dbg_log(this.current_interface.name + \": push IRQ \" + this.irq, LOG_DISK);\n        }\n        this.dma_status |= 4;\n        this.cpu.device_raise_irq(this.irq);\n    }\n};\n\nIDEChannel.prototype.get_state = function()\n{\n    var state = [];\n    state[0] = this.master;\n    state[1] = this.slave;\n    state[2] = this.command_base;\n    state[3] = this.irq;\n    // state[4] = this.pci_id;\n    state[5] = this.control_base;\n    // state[6] = this.bus_master_base;\n    state[7] = this.name;\n    state[8] = this.device_control_reg;\n    state[9] = this.prdt_addr;\n    state[10] = this.dma_status;\n    state[11] = this.current_interface === this.master;\n    state[12] = this.dma_command;\n    return state;\n};\n\nIDEChannel.prototype.set_state = function(state)\n{\n    this.master.set_state(state[0]);\n    this.slave.set_state(state[1]);\n    this.command_base = state[2];\n    this.irq = state[3];\n    // this.pci_id = state[4];\n    this.control_base = state[5];\n    // this.bus_master_base = state[6];\n    this.name = state[7];\n    this.device_control_reg = state[8];\n    this.prdt_addr = state[9];\n    this.dma_status = state[10];\n    this.current_interface = state[11] ? this.master : this.slave;\n    this.dma_command = state[12];\n};\n\n/**\n * @constructor\n * @param {IDEChannel} channel\n * @param {number} interface_nr\n * @param {boolean} is_cd\n */\nfunction IDEInterface(channel, interface_nr, buffer, is_cd)\n{\n    this.channel = channel;\n    this.name = channel.name + \".\" + interface_nr;\n\n    /** @const @type {BusConnector} */\n    this.bus = channel.bus;\n\n    /** @const @type {number} */\n    this.channel_nr = channel.channel_nr;\n\n    /** @const @type {number} */\n    this.interface_nr = interface_nr;\n\n    /** @const @type {CPU} */\n    this.cpu = channel.cpu;\n\n    this.buffer = null;\n\n    /** @type {boolean} */\n    this.drive_connected = is_cd || !!buffer;\n\n    /** @type {number} */\n    this.sector_size = is_cd ? CDROM_SECTOR_SIZE : HD_SECTOR_SIZE;\n\n    /** @type {boolean} */\n    this.is_atapi = is_cd;\n\n    /** @type {number} */\n    this.sector_count = 0;\n\n    /** @type {number} */\n    this.head_count = this.is_atapi ? 1 : 0;\n\n    /** @type {number} */\n    this.sectors_per_track = 0;\n\n    /** @type {number} */\n    this.cylinder_count = 0;\n\n    /** @type {number} */\n    this.is_lba = 0;\n\n    /** @type {number} */\n    this.sector_count_reg = 0;\n\n    /** @type {number} */\n    this.lba_low_reg = 0;\n\n    /** @type {number} */\n    this.features_reg = 0;\n\n    /** @type {number} */\n    this.lba_mid_reg = 0;\n\n    /** @type {number} */\n    this.lba_high_reg = 0;\n\n    /** @type {number} */\n    this.head = 0;\n\n    /** @type {number} */\n    this.device_reg = 0;\n\n    /** @type {number} */\n    this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n\n    /** @type {number} */\n    this.sectors_per_drq = 0x80;\n\n    /** @type {number} */\n    this.error_reg = 0;\n\n    /** @type {number} */\n    this.data_pointer = 0;\n\n    this.data = new Uint8Array(64 * 1024);\n    this.data16 = new Uint16Array(this.data.buffer);\n    this.data32 = new Int32Array(this.data.buffer);\n\n    /** @type {number} */\n    this.data_length = 0;\n\n    /** @type {number} */\n    this.data_end = 0;\n\n    /** @type {number} */\n    this.current_command = -1;\n\n    /** @type {number} */\n    this.write_dest = 0;\n\n    // cancellation support\n    this.last_io_id = 0;\n    this.in_progress_io_ids = new Set();\n    this.cancelled_io_ids = new Set();\n\n    // ATAPI-only\n    /** @type {number} */\n    this.current_atapi_command = -1;\n\n    /** @type {number} */\n    this.atapi_sense_key = 0;\n\n    /** @type {number} */\n    this.atapi_add_sense = 0;\n\n    /** @type {boolean} */\n    this.medium_changed = false;\n\n    this.set_disk_buffer(buffer);\n\n    if(this.drive_connected)\n    {\n        dbg_log(`${this.name}: ${this.is_atapi ? \"ATAPI CD-ROM\" : \"ATA HD\"} device ready`, LOG_DISK);\n    }\n\n    Object.seal(this);\n}\n\nIDEInterface.prototype.has_disk = function()\n{\n    return !!this.buffer;\n};\n\nIDEInterface.prototype.eject = function()\n{\n    if(this.is_atapi && this.buffer)\n    {\n        this.medium_changed = true;\n        this.buffer = null;\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ|ATA_SR_COND;\n        this.error_reg = ATAPI_SK_UNIT_ATTENTION << 4;\n        this.push_irq();\n    }\n};\n\nIDEInterface.prototype.set_cdrom = function(buffer)\n{\n    if(this.is_atapi && buffer)\n    {\n        this.set_disk_buffer(buffer);\n        this.medium_changed = true;\n    }\n};\n\nIDEInterface.prototype.set_disk_buffer = function(buffer)\n{\n    if(!buffer)\n    {\n        return;\n    }\n\n    this.buffer = buffer;\n    if(this.is_atapi)\n    {\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ|ATA_SR_COND;\n        this.error_reg = ATAPI_SK_UNIT_ATTENTION << 4;\n    }\n    this.sector_count = this.buffer.byteLength / this.sector_size;\n\n    if(this.sector_count !== (this.sector_count | 0))\n    {\n        dbg_log(this.name + \": warning: disk size not aligned with sector size\", LOG_DISK);\n        this.sector_count = Math.ceil(this.sector_count);\n    }\n\n    if(this.is_atapi)\n    {\n        // default values: 1/2048\n        this.head_count = 1;\n        this.sectors_per_track = 2048;\n    }\n    else\n    {\n        // \"default\" values: 16/63\n        // common: 255, 63\n        this.head_count = 16;\n        this.sectors_per_track = 63;\n    }\n\n    this.cylinder_count = this.sector_count / this.head_count / this.sectors_per_track;\n\n    if(this.cylinder_count !== (this.cylinder_count | 0))\n    {\n        dbg_log(this.name + \": warning: rounding up cylinder count, choose different head number\", LOG_DISK);\n        this.cylinder_count = Math.floor(this.cylinder_count);\n    }\n\n    if(this.interface_nr === 0)\n    {\n        // for CMOS see:\n        //   https://github.com/copy/v86/blob/master/src/rtc.js\n        //   https://github.com/coreboot/seabios/blob/master/src/hw/rtc.h\n        //   https://web.archive.org/web/20240119203005/http://www.bioscentral.com/misc/cmosmap.htm\n        const rtc = this.cpu.devices.rtc;\n\n        // master\n        rtc.cmos_write(CMOS_BIOS_DISKTRANSFLAG,     // TODO: what is this doing, setting LBA translation?\n                       rtc.cmos_read(CMOS_BIOS_DISKTRANSFLAG) | 1 << this.channel_nr * 4);\n\n        // set hard disk type (CMOS_DISK_DATA = 0x12) of C: to 0b1111, keep type of D:\n        //   bits 0-3: hard disk type of D:\n        //   bits 4-7: hard disk type of C:\n        // TODO: should this not also set CMOS_DISK_DRIVE1_TYPE to a hard disk type (see SeaBIOS rtc.h)?\n        rtc.cmos_write(CMOS_DISK_DATA, rtc.cmos_read(CMOS_DISK_DATA) & 0x0F | 0xF0);\n\n        const drive_reg = this.channel_nr === 0 ? CMOS_DISK_DRIVE1_CYL : CMOS_DISK_DRIVE2_CYL;  // 0x1B : 0x24 (drive C: or D:)\n        rtc.cmos_write(drive_reg + 0, this.cylinder_count & 0xFF);        // number of cylinders least significant byte\n        rtc.cmos_write(drive_reg + 1, this.cylinder_count >> 8 & 0xFF);   // number of cylinders most significant byte\n        rtc.cmos_write(drive_reg + 2, this.head_count & 0xFF);            // number of heads\n        rtc.cmos_write(drive_reg + 3, 0xFF);                              // write precomp cylinder least significant byte\n        rtc.cmos_write(drive_reg + 4, 0xFF);                              // write precomp cylinder most significant byte\n        rtc.cmos_write(drive_reg + 5, 0xC8);                              // control byte\n        rtc.cmos_write(drive_reg + 6, this.cylinder_count & 0xFF);        // landing zone least significant byte\n        rtc.cmos_write(drive_reg + 7, this.cylinder_count >> 8 & 0xFF);   // landing zone most significant byte\n        rtc.cmos_write(drive_reg + 8, this.sectors_per_track & 0xFF);     // number of sectors\n    }\n\n    if(this.channel.cpu)\n    {\n        this.push_irq();\n    }\n};\n\nIDEInterface.prototype.device_reset = function()\n{\n    if(this.is_atapi)\n    {\n        this.status_reg = 0;\n        this.sector_count_reg = 1;\n        this.error_reg = 1;\n        this.lba_low_reg = 1;\n        this.lba_mid_reg = ATAPI_SIGNATURE_LO;  // TODO: missing documentation\n        this.lba_high_reg = ATAPI_SIGNATURE_HI;\n    }\n    else\n    {\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_ERR;\n        this.sector_count_reg = 1;\n        this.error_reg = 1;\n        this.lba_low_reg = 1;\n        this.lba_mid_reg = 0;\n        this.lba_high_reg = 0;\n    }\n    this.cancel_io_operations();\n};\n\nIDEInterface.prototype.push_irq = function()\n{\n    this.channel.push_irq();\n};\n\nIDEInterface.prototype.ata_abort_command = function()\n{\n    this.error_reg = ATA_ER_ABRT;\n    this.status_reg = ATA_SR_DRDY|ATA_SR_ERR;\n    this.push_irq();\n};\n\nIDEInterface.prototype.capture_regs = function()\n{\n    return `ST=${h(this.status_reg & 0xFF)} ER=${h(this.error_reg & 0xFF)} ` +\n        `SC=${h(this.sector_count_reg & 0xFF)} LL=${h(this.lba_low_reg & 0xFF)} ` +\n        `LM=${h(this.lba_mid_reg & 0xFF)} LH=${h(this.lba_high_reg & 0xFF)} ` +\n        `FE=${h(this.features_reg & 0xFF)}`;\n};\n\nIDEInterface.prototype.ata_command = function(cmd)\n{\n    if(!this.drive_connected && cmd !== ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC)\n    {\n        dbg_log(`${this.name}: ATA command ${ATA_CMD_NAME[cmd]} (${h(cmd)}) ignored: no slave drive connected`, LOG_DISK);\n        return;\n    }\n\n    const regs_pre = DEBUG ? this.capture_regs() : undefined;\n    let do_dbg_log = DEBUG;\n\n    this.current_command = cmd;\n    this.error_reg = 0;\n\n    switch(cmd)\n    {\n        case ATA_CMD_DEVICE_RESET:\n            this.data_pointer = 0;\n            this.data_end = 0;\n            this.data_length = 0;\n            this.device_reset();\n            this.push_irq();\n            break;\n\n        case ATA_CMD_10h:\n            this.lba_mid_reg = 0;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_READ_NATIVE_MAX_ADDRESS:\n            var last_sector = this.sector_count - 1;\n            this.lba_low_reg = last_sector & 0xFF;\n            this.lba_mid_reg = last_sector >> 8 & 0xFF;\n            this.lba_high_reg = last_sector >> 16 & 0xFF;\n            this.device_reg = this.device_reg & 0xF0 | last_sector >> 24 & 0x0F;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_READ_NATIVE_MAX_ADDRESS_EXT:\n            var last_sector = this.sector_count - 1;\n            this.lba_low_reg = last_sector & 0xFF;\n            this.lba_mid_reg = last_sector >> 8 & 0xFF;\n            this.lba_high_reg = last_sector >> 16 & 0xFF;\n            this.lba_low_reg |= last_sector >> 24 << 8 & 0xFF00;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_READ_SECTORS:\n            do_dbg_log = false;\n            if(this.is_atapi)\n            {\n                this.lba_mid_reg = ATAPI_SIGNATURE_LO;  // see [ATA8-ACS] 4.3\n                this.lba_high_reg = ATAPI_SIGNATURE_HI;\n                this.ata_abort_command();\n            }\n            else\n            {\n                this.ata_read_sectors(cmd);\n            }\n            break;\n\n        case ATA_CMD_READ_SECTORS_EXT:\n        case ATA_CMD_READ_MULTIPLE:\n        case ATA_CMD_READ_MULTIPLE_EXT:\n            do_dbg_log = false;\n            if(this.is_atapi)\n            {\n                this.ata_abort_command();\n            }\n            else\n            {\n                this.ata_read_sectors(cmd);\n            }\n            break;\n\n        case ATA_CMD_WRITE_SECTORS:\n        case ATA_CMD_WRITE_SECTORS_EXT:\n        case ATA_CMD_WRITE_MULTIPLE:\n        case ATA_CMD_WRITE_MULTIPLE_EXT:\n            do_dbg_log = false;\n            if(this.is_atapi)\n            {\n                this.ata_abort_command();\n            }\n            else\n            {\n                this.ata_write_sectors(cmd);\n            }\n            break;\n\n        case ATA_CMD_EXECUTE_DEVICE_DIAGNOSTIC:\n            // the behaviour of this command is independent of the selected device\n            this.channel.master.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.channel.master.error_reg = 0x01;    // Master drive passed, slave drive passed or not present\n            this.channel.master.push_irq();\n            if(this.channel.slave.drive_connected)\n            {\n                this.channel.slave.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n                this.channel.slave.error_reg = 0x01; // Slave drive passed\n                this.channel.slave.push_irq();\n            }\n            break;\n\n        case ATA_CMD_INITIALIZE_DEVICE_PARAMETERS:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_PACKET:\n            if(this.is_atapi)\n            {\n                do_dbg_log = false;\n                this.data_allocate(12);\n                this.data_end = 12;\n                this.sector_count_reg = 0x01;   // 0x01: indicates transfer of a command packet (C/D)\n                this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n                this.push_irq();\n            }\n            else\n            {\n                this.ata_abort_command();\n            }\n            break;\n\n        case ATA_CMD_IDENTIFY_PACKET_DEVICE:\n            if(this.is_atapi)\n            {\n                this.create_identify_packet();\n                this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n                this.push_irq();\n            }\n            else\n            {\n                this.ata_abort_command();\n            }\n            break;\n\n        case ATA_CMD_SET_MULTIPLE_MODE:\n            // Logical sectors per DRQ Block in word 1\n            dbg_log(this.name + \": logical sectors per DRQ Block: \" + h(this.sector_count_reg & 0xFF), LOG_DISK);\n            this.sectors_per_drq = this.sector_count_reg & 0xFF;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_READ_DMA:\n        case ATA_CMD_READ_DMA_EXT:\n            do_dbg_log = false;\n            this.ata_read_sectors_dma(cmd);\n            break;\n\n        case ATA_CMD_WRITE_DMA:\n        case ATA_CMD_WRITE_DMA_EXT:\n            do_dbg_log = false;\n            this.ata_write_sectors_dma(cmd);\n            break;\n\n        case ATA_CMD_READ_VERIFY_SECTORS:\n            // TODO: check that lba_low/mid/high and sector_count regs are within the bounds of the disk's size\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_GET_MEDIA_STATUS:\n            if(this.is_atapi)\n            {\n                if(!this.buffer)\n                {\n                    this.error_reg |= 0x02; // NM: No Media\n                }\n                if(this.medium_changed)\n                {\n                    this.error_reg |= 0x20; // MC: Media Change\n                    this.medium_changed = false;\n                }\n                this.error_reg |= 0x40;     // WP: Write Protect\n            }\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_STANDBY_IMMEDIATE:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_IDLE_IMMEDIATE:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_FLUSH_CACHE:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_FLUSH_CACHE_EXT:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_IDENTIFY_DEVICE:\n            if(this.is_atapi)\n            {\n                this.lba_mid_reg = ATAPI_SIGNATURE_LO;  // see [ATA8-ACS] 4.3\n                this.lba_high_reg = ATAPI_SIGNATURE_HI;\n                this.ata_abort_command();\n            }\n            else\n            {\n                this.create_identify_packet();\n                this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n                this.push_irq();\n            }\n            break;\n\n        case ATA_CMD_SET_FEATURES:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_MEDIA_LOCK:\n            this.status_reg = ATA_SR_DRDY;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_SECURITY_FREEZE_LOCK:\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.push_irq();\n            break;\n\n        case ATA_CMD_SET_MAX:\n            this.ata_abort_command();\n            break;\n\n        case ATA_CMD_NOP:\n            this.ata_abort_command();\n            break;\n\n        case ATA_CMD_F0h:\n            dbg_log(`${this.name}: error: unimplemented vendor-specific ATA command ${h(cmd)}: ABORT [${this.capture_regs()}]`, LOG_DISK);\n            this.ata_abort_command();\n            break;\n\n        default:\n            dbg_assert(false, `${this.name}: error: unimplemented ATA command ${h(cmd)}: ABORT [${this.capture_regs()}]`, LOG_DISK);\n            this.ata_abort_command();\n            break;\n    }\n\n    if(DEBUG && do_dbg_log)\n    {\n        const regs_msg = `[${regs_pre}] -> [${this.capture_regs()}]`;\n        const result = this.status_reg & ATA_SR_ERR ? (this.error_reg & ATA_ER_ABRT ? \"ABORT\" : \"ERROR\") : \"OK\";\n        dbg_log(`${this.name}: ATA command ${ATA_CMD_NAME[cmd]} (${h(cmd)}): ${result} ${regs_msg}`, LOG_DISK);\n    }\n};\n\nIDEInterface.prototype.atapi_handle = function()\n{\n    const cmd = this.data[0];\n    const cmd_name = ATAPI_CMD[cmd] ? ATAPI_CMD[cmd].name : \"<undefined>\";\n    const cmd_flags = ATAPI_CMD[cmd] ? ATAPI_CMD[cmd].flags : ATAPI_CF_NONE;\n    const regs_pre = DEBUG ? this.capture_regs() : undefined;\n\n    let do_dbg_log = DEBUG;\n    let dbg_log_extra;\n\n    this.data_pointer = 0;\n    this.current_atapi_command = cmd;\n\n    if(cmd !== ATAPI_CMD_REQUEST_SENSE) // TODO\n    {\n        this.atapi_sense_key = 0;\n        this.atapi_add_sense = 0;\n    }\n\n    if(!this.buffer && cmd_flags & ATAPI_CF_NEEDS_DISK)\n    {\n        this.atapi_check_condition_response(ATAPI_SK_NOT_READY, ATAPI_ASC_MEDIUM_NOT_PRESENT);\n        this.push_irq();\n        if(DEBUG)\n        {\n            dbg_log(`${this.name}: ATAPI command ${cmd_name} (${h(cmd)}) without medium: ERROR [${regs_pre}]`, LOG_DISK);\n        }\n        return;\n    }\n\n    switch(cmd)\n    {\n        case ATAPI_CMD_TEST_UNIT_READY:\n            if(this.buffer)\n            {\n                this.data_allocate(0);\n                this.data_end = this.data_length;\n                this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            }\n            else\n            {\n                this.atapi_check_condition_response(ATAPI_SK_NOT_READY, ATAPI_ASC_MEDIUM_NOT_PRESENT);\n            }\n            break;\n\n        case ATAPI_CMD_REQUEST_SENSE:\n            this.data_allocate(this.data[4]);\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.data[0] = 0x80 | 0x70;             // valid | SCSI error code\n            this.data[2] = this.atapi_sense_key;    // SCSI sense key\n            this.data[7] = 8;                       // SCSI additional sense length (fixed 8 for this error code 0x70)\n            this.data[12] = this.atapi_add_sense;   // SCSI additional sense code\n            this.atapi_sense_key = 0;\n            this.atapi_add_sense = 0;\n            break;\n\n        case ATAPI_CMD_INQUIRY:\n            var length = this.data[4];\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            dbg_log_extra = \"lun=\" + h(this.data[1], 2) + \" length=\" + length;\n            // for data layout see [CD-SCSI-2] \"INQUIRY Command\"\n            this.data.set([\n                // 0: Device-type, Removable, ANSI-Version, Response Format\n                0x05, 0x80, 0x01, 0x31,\n                // 4: Additional length, Reserved, Reserved, Reserved\n                31, 0, 0, 0,\n                // 8: Vendor Identification \"SONY    \"\n                0x53, 0x4F, 0x4E, 0x59,\n                0x20, 0x20, 0x20, 0x20,\n                // 16: Product Identification \"CD-ROM CDU-1000 \"\n                0x43, 0x44, 0x2D, 0x52,\n                0x4F, 0x4D, 0x20, 0x43,\n                0x44, 0x55, 0x2D, 0x31,\n                0x30, 0x30, 0x30, 0x20,\n                // 32: Product Revision Level \"1.1a\"\n                0x31, 0x2E, 0x31, 0x61,\n            ]);\n            this.data_end = this.data_length = Math.min(36, length);\n            break;\n\n        case ATAPI_CMD_MODE_SENSE_6:\n            this.data_allocate(this.data[4]);\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:\n            this.data_allocate(0);\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            break;\n\n        case ATAPI_CMD_READ_CAPACITY:\n            var count = this.sector_count - 1;\n            this.data_set(new Uint8Array([\n                count >> 24 & 0xFF,\n                count >> 16 & 0xFF,\n                count >> 8 & 0xFF,\n                count & 0xFF,\n                0,\n                0,\n                this.sector_size >> 8 & 0xFF,\n                this.sector_size & 0xFF,\n            ]));\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_READ_10:\n        case ATAPI_CMD_READ_12:\n            do_dbg_log = false;\n            if(this.features_reg & 1)\n            {\n                this.atapi_read_dma(this.data);\n            }\n            else\n            {\n                this.atapi_read(this.data);\n            }\n            break;\n\n        case ATAPI_CMD_READ_SUBCHANNEL:\n            var length = this.data[8];\n            dbg_log_extra = \"length=\" + length;\n            this.data_allocate(Math.min(8, length));\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_READ_TOC_PMA_ATIP:\n            var length = this.data[8] | this.data[7] << 8;\n            var format = this.data[9] >> 6;\n            dbg_log_extra = `${h(format, 2)} length=${length} ${!!(this.data[1] & 2)} ${h(this.data[6])}`;\n\n            this.data_allocate(length);\n            this.data_end = this.data_length;\n            if(format === 0)\n            {\n                const sector_count = this.sector_count;\n                this.data.set(new Uint8Array([\n                    0, 18, // length\n                    1, 1, // first and last session\n\n                    0,\n                    0x14,\n                    1, // track number\n                    0,\n                    0, 0, 0, 0,\n\n                    0,\n                    0x16,\n                    0xAA, // track number\n                    0,\n                    sector_count >> 24,\n                    sector_count >> 16 & 0xFF,\n                    sector_count >> 8 & 0xFF,\n                    sector_count & 0xFF,\n                ]));\n            }\n            else if(format === 1)\n            {\n                this.data.set(new Uint8Array([\n                    0, 10, // length\n                    1, 1, // first and last session\n                    0, 0,\n                    0, 0,\n                    0, 0,\n                    0, 0,\n                ]));\n            }\n            else\n            {\n                dbg_assert(false, this.name + \": error: unimplemented format: \" + format);\n            }\n\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_GET_CONFIGURATION:\n            var length = Math.min(this.data[8] | this.data[7] << 8, 32);\n            dbg_log_extra = \"length=\" + length;\n            this.data_allocate(length);\n            this.data_end = this.data_length;\n            this.data[0] = length - 4 >> 24 & 0xFF;\n            this.data[1] = length - 4 >> 16 & 0xFF;\n            this.data[2] = length - 4 >> 8 & 0xFF;\n            this.data[3] = length - 4 & 0xFF;\n            this.data[6] = 0x08;\n            this.data[10] = 3;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_READ_DISK_INFORMATION:\n            this.data_allocate(0);\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            break;\n\n        case ATAPI_CMD_READ_TRACK_INFORMATION:\n            dbg_log_extra = \"unimplemented\";\n            this.atapi_check_condition_response(ATAPI_SK_ILLEGAL_REQUEST, ATAPI_ASC_INV_FIELD_IN_CMD_PACKET);\n            break;\n\n        case ATAPI_CMD_MODE_SENSE_10:\n            var length = this.data[8] | this.data[7] << 8;\n            var page_code = this.data[2];\n            dbg_log_extra = \"page_code=\" + h(page_code) + \" length=\" + length;\n            if(page_code === 0x2A)\n            {\n                this.data_allocate(Math.min(30, length));\n            }\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_MECHANISM_STATUS:\n            this.data_allocate(this.data[9] | this.data[8] << 8);\n            this.data_end = this.data_length;\n            this.data[5] = 1;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            break;\n\n        case ATAPI_CMD_START_STOP_UNIT:\n            var loej_start = this.data[4] & 0x3;\n            dbg_log_extra = `Immed=${h(this.data[1] & 1)} LoEj/Start=${h(loej_start)}`;\n            if(this.buffer && loej_start === 0x2)\n            {\n                dbg_log_extra += \": disk ejected\";\n                this.medium_changed = true;\n                this.buffer = null;\n            }\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.data_allocate(0);\n            this.data_end = this.data_length;\n            break;\n\n        case ATAPI_CMD_PAUSE:\n        case ATAPI_CMD_GET_EVENT_STATUS_NOTIFICATION:\n            dbg_log_extra = \"unimplemented\";\n            this.atapi_check_condition_response(ATAPI_SK_ILLEGAL_REQUEST, ATAPI_ASC_INV_FIELD_IN_CMD_PACKET);\n            break;\n\n        case ATAPI_CMD_READ_CD:\n            dbg_log_extra = \"unimplemented\";\n            this.data_allocate(0);\n            this.data_end = this.data_length;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            break;\n\n        default:\n            dbg_assert(false, `${this.name}: error: unimplemented ATAPI command ${h(this.data[0])}`, LOG_DISK);\n            this.atapi_check_condition_response(ATAPI_SK_ILLEGAL_REQUEST, ATAPI_ASC_INV_FIELD_IN_CMD_PACKET);\n            break;\n    }\n\n    this.sector_count_reg = this.sector_count_reg & ~7 | 2;\n\n    if((this.status_reg & ATA_SR_BSY) === 0)\n    {\n        this.push_irq();\n    }\n\n    if((this.status_reg & ATA_SR_BSY) === 0 && this.data_length === 0)\n    {\n        this.sector_count_reg |= 1;\n        this.status_reg &= ~ATA_SR_DRQ;\n    }\n\n    if(DEBUG && do_dbg_log)\n    {\n        const regs_msg = `[${regs_pre}] -> [${this.capture_regs()}]`;\n        const result = this.status_reg & ATA_SR_ERR ? (this.error_reg & ATA_ER_ABRT ? \"ABORT\" : \"ERROR\") : \"OK\";\n        dbg_log_extra = dbg_log_extra ? ` ${dbg_log_extra}:` : \"\";\n        dbg_log(`${this.name}: ATAPI command ${cmd_name} (${h(cmd)}):${dbg_log_extra} ${result} ${regs_msg}`, LOG_DISK);\n    }\n};\n\nIDEInterface.prototype.atapi_check_condition_response = function(sense_key, additional_sense)\n{\n    // Setup ATA registers to CHECK CONDITION state.\n    // The sense state (sense_key and additional_sense) must be requested\n    // by the host using ATAPI_CMD_REQUEST_SENSE immediately following a\n    // CHECK CONDITION response or else it will be lost.\n    // https://github.com/qemu/qemu/blob/757a34115e7491744a63dfc3d291fd1de5297ee2/hw/ide/atapi.c#L186\n    this.data_allocate(0);\n    this.data_end = this.data_length;\n    this.status_reg = ATA_SR_DRDY|ATA_SR_COND;\n    this.error_reg = sense_key << 4;\n    this.sector_count_reg = (this.sector_count_reg & ~7) | 2 | 1;\n    this.atapi_sense_key = sense_key;\n    this.atapi_add_sense = additional_sense;\n};\n\nIDEInterface.prototype.do_write = function()\n{\n    this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n\n    dbg_assert(this.data_length <= this.data.length);\n    var data = this.data.subarray(0, this.data_length);\n\n    //dbg_log(hex_dump(data), LOG_DISK);\n    dbg_assert(this.data_length % 512 === 0);\n    this.ata_advance(this.current_command, this.data_length / 512);\n    this.push_irq();\n\n    this.buffer.set(this.write_dest, data, function()\n    {\n    });\n\n    this.report_write(this.data_length);\n};\n\nIDEInterface.prototype.atapi_read = function(cmd)\n{\n    // Note: Big Endian\n    var lba = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];\n    var count = cmd[0] === ATAPI_CMD_READ_12 ? (cmd[6] << 24 | cmd[7] << 16 | cmd[8] << 8 | cmd[9]) : (cmd[7] << 8 | cmd[8]);\n    count >>>= 0;\n    var flags = cmd[1];\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    if(LOG_DETAILS & LOG_DETAIL_RW)\n    {\n        dbg_log(this.name + \": CD read lba=\" + h(lba) +\n                \" lbacount=\" + h(count) +\n                \" bytecount=\" + h(byte_count) +\n                \" flags=\" + h(flags), LOG_DISK);\n    }\n\n    this.data_length = 0;\n    var req_length = this.lba_high_reg << 8 & 0xFF00 | this.lba_mid_reg & 0xFF;\n    //dbg_log(this.name + \": \" + h(this.lba_high_reg, 2) + \" \" + h(this.lba_mid_reg, 2), LOG_DISK);\n    this.lba_mid_reg = this.lba_high_reg = 0; // oak technology driver (windows 3.0)\n\n    if(req_length === 0xFFFF)\n        req_length--;\n\n    if(req_length > byte_count)\n    {\n        req_length = byte_count;\n    }\n\n    if(!this.buffer)\n    {\n        dbg_assert(false, this.name + \": CD read: no buffer\", LOG_DISK);\n        this.status_reg = 0xFF;\n        this.error_reg = 0x41;\n        this.push_irq();\n    }\n    else if(start >= this.buffer.byteLength)\n    {\n        dbg_assert(false, this.name + \": CD read: Outside of disk  end=\" + h(start + byte_count) +\n                          \" size=\" + h(this.buffer.byteLength), LOG_DISK);\n\n        this.status_reg = 0xFF;\n        this.push_irq();\n    }\n    else if(byte_count === 0)\n    {\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n\n        this.data_pointer = 0;\n        //this.push_irq();\n    }\n    else\n    {\n        byte_count = Math.min(byte_count, this.buffer.byteLength - start);\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_BSY;\n        this.report_read_start();\n\n        this.read_buffer(start, byte_count, (data) =>\n        {\n            if(LOG_DETAILS & LOG_DETAIL_RW)\n            {\n                dbg_log(this.name + \": CD read: data arrived\", LOG_DISK);\n            }\n            this.data_set(data);\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.sector_count_reg = this.sector_count_reg & ~7 | 2;\n\n            this.push_irq();\n\n            req_length &= ~3;\n\n            this.data_end = req_length;\n            if(this.data_end > this.data_length)\n            {\n                this.data_end = this.data_length;\n            }\n            this.lba_mid_reg = this.data_end & 0xFF;\n            this.lba_high_reg = this.data_end >> 8 & 0xFF;\n\n            this.report_read_end(byte_count);\n        });\n    }\n};\n\nIDEInterface.prototype.atapi_read_dma = function(cmd)\n{\n    // Note: Big Endian\n    var lba = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];\n    var count = cmd[0] === ATAPI_CMD_READ_12 ? (cmd[6] << 24 | cmd[7] << 16 | cmd[8] << 8 | cmd[9]) : (cmd[7] << 8 | cmd[8]);\n    count >>>= 0;\n    var flags = cmd[1];\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    dbg_log(this.name + \": CD read DMA lba=\" + h(lba) +\n            \" lbacount=\" + h(count) +\n            \" bytecount=\" + h(byte_count) +\n            \" flags=\" + h(flags), LOG_DISK);\n\n    if(start >= this.buffer.byteLength)\n    {\n        dbg_assert(false, this.name + \": CD read: Outside of disk  end=\" + h(start + byte_count) +\n                          \" size=\" + h(this.buffer.byteLength), LOG_DISK);\n\n        this.status_reg = 0xFF;\n        this.push_irq();\n    }\n    else\n    {\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_BSY;\n        this.report_read_start();\n\n        this.read_buffer(start, byte_count, (data) =>\n        {\n            dbg_log(this.name + \": atapi_read_dma: Data arrived\", LOG_DISK);\n            this.report_read_end(byte_count);\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.sector_count_reg = this.sector_count_reg & ~7 | 2;\n            this.data_set(data);\n\n            this.do_atapi_dma();\n        });\n    }\n};\n\nIDEInterface.prototype.do_atapi_dma = function()\n{\n    if((this.channel.dma_status & 1) === 0)\n    {\n        dbg_log(this.name + \": do_atapi_dma: Status not set\", LOG_DISK);\n        return;\n    }\n\n    if((this.status_reg & ATA_SR_DRQ) === 0)\n    {\n        dbg_log(this.name + \": do_atapi_dma: DRQ not set\", LOG_DISK);\n        return;\n    }\n\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.name + \": ATAPI DMA transfer len=\" + this.data_length, LOG_DISK);\n    }\n\n    var prdt_start = this.channel.prdt_addr;\n    var offset = 0;\n\n    var data = this.data;\n\n    do {\n        var addr = this.cpu.read32s(prdt_start);\n        var count = this.cpu.read16(prdt_start + 4);\n        var end = this.cpu.read8(prdt_start + 7) & 0x80;\n\n        if(!count)\n        {\n            count = 0x10000;\n        }\n\n        if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n        {\n            dbg_log(this.name + \": DMA read dest=\" + h(addr) + \" count=\" + h(count) + \" datalen=\" + h(this.data_length), LOG_DISK);\n        }\n        this.cpu.write_blob(data.subarray(offset, Math.min(offset + count, this.data_length)), addr);\n\n        offset += count;\n        prdt_start += 8;\n\n        if(offset >= this.data_length && !end)\n        {\n            if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n            {\n                dbg_log(this.name + \": leave early end=\" + (+end) +\n                        \" offset=\" + h(offset) +\n                        \" data_length=\" + h(this.data_length) +\n                        \" cmd=\" + h(this.current_command), LOG_DISK);\n            }\n            break;\n        }\n    }\n    while(!end);\n\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.name + \": end offset=\" + offset, LOG_DISK);\n    }\n\n    this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n    this.channel.dma_status &= ~1;\n    this.sector_count_reg = this.sector_count_reg & ~7 | 3;\n    this.push_irq();\n};\n\nIDEInterface.prototype.read_data = function(length)\n{\n    if(this.data_pointer < this.data_end)\n    {\n        dbg_assert(this.data_pointer + length - 1 < this.data_end);\n        dbg_assert(this.data_pointer % length === 0, h(this.data_pointer) + \" \" + length);\n\n        if(length === 1)\n        {\n            var result = this.data[this.data_pointer];\n        }\n        else if(length === 2)\n        {\n            var result = this.data16[this.data_pointer >>> 1];\n        }\n        else\n        {\n            var result = this.data32[this.data_pointer >>> 2];\n        }\n\n        this.data_pointer += length;\n\n        var align = (this.data_end & 0xFFF) === 0 ? 0xFFF : 0xFF;\n\n        if(LOG_DETAILS & LOG_DETAIL_RW)\n        {\n            if((this.data_pointer & align) === 0)\n            {\n                dbg_log(this.name + \": read 1F0: \" + h(this.data[this.data_pointer], 2) +\n                            \" cur=\" + h(this.data_pointer) +\n                            \" cnt=\" + h(this.data_length), LOG_DISK);\n            }\n        }\n\n        if(this.data_pointer >= this.data_end)\n        {\n            this.read_end();\n        }\n\n        return result;\n    }\n    else\n    {\n        if(LOG_DETAILS & LOG_DETAIL_RW)\n        {\n            dbg_log(this.name + \": read 1F0: empty\", LOG_DISK);\n        }\n        this.data_pointer += length;\n        return 0;\n    }\n};\n\nIDEInterface.prototype.read_end = function()\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW)\n    {\n        dbg_log(this.name + \": read_end cmd=\" + h(this.current_command) +\n                \" data_pointer=\" + h(this.data_pointer) + \" end=\" + h(this.data_end) +\n                \" length=\" + h(this.data_length), LOG_DISK);\n    }\n\n    if(this.current_command === ATA_CMD_PACKET)\n    {\n        if(this.data_end === this.data_length)\n        {\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n            this.sector_count_reg = this.sector_count_reg & ~7 | 3;\n            this.push_irq();\n        }\n        else\n        {\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.sector_count_reg = this.sector_count_reg & ~7 | 2;\n            this.push_irq();\n            var byte_count = this.lba_high_reg << 8 & 0xFF00 | this.lba_mid_reg & 0xFF;\n\n            if(this.data_end + byte_count > this.data_length)\n            {\n                this.lba_mid_reg = (this.data_length - this.data_end) & 0xFF;\n                this.lba_high_reg = (this.data_length - this.data_end) >> 8 & 0xFF;\n                this.data_end = this.data_length;\n            }\n            else\n            {\n                this.data_end += byte_count;\n            }\n            if(LOG_DETAILS & LOG_DETAIL_RW)\n            {\n                dbg_log(this.name + \": data_end=\" + h(this.data_end), LOG_DISK);\n            }\n        }\n    }\n    else\n    {\n        this.error_reg = 0;\n        if(this.data_pointer >= this.data_length)\n        {\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n        }\n        else\n        {\n            if(this.current_command === ATA_CMD_READ_MULTIPLE || this.current_command === ATA_CMD_READ_MULTIPLE_EXT)\n            {\n                var sector_count = Math.min(this.sectors_per_drq,\n                    (this.data_length - this.data_end) / 512);\n                dbg_assert(sector_count % 1 === 0);\n            }\n            else\n            {\n                dbg_assert(this.current_command === ATA_CMD_READ_SECTORS || this.current_command === ATA_CMD_READ_SECTORS_EXT);\n                var sector_count = 1;\n            }\n            this.ata_advance(this.current_command, sector_count);\n            this.data_end += 512 * sector_count;\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.push_irq();\n        }\n    }\n};\n\nIDEInterface.prototype.write_data_port = function(data, length)\n{\n    dbg_assert(this.data_pointer % length === 0);\n\n    if(this.data_pointer >= this.data_end)\n    {\n        dbg_log(this.name + \": redundant write to data port: \" + h(data) + \" count=\" +\n                h(this.data_end) + \" cur=\" + h(this.data_pointer), LOG_DISK);\n    }\n    else\n    {\n        var align = (this.data_end & 0xFFF) === 0 ? 0xFFF : 0xFF;\n        if(LOG_DETAILS & LOG_DETAIL_RW)\n        {\n            if((this.data_pointer + length & align) === 0 || this.data_end < 20)\n            {\n                dbg_log(this.name + \": data port: \" + h(data >>> 0) + \" count=\" +\n                        h(this.data_end) + \" cur=\" + h(this.data_pointer), LOG_DISK);\n            }\n        }\n\n        if(length === 1)\n        {\n            this.data[this.data_pointer++] = data;\n        }\n        else if(length === 2)\n        {\n            this.data16[this.data_pointer >>> 1] = data;\n            this.data_pointer += 2;\n        }\n        else\n        {\n            this.data32[this.data_pointer >>> 2] = data;\n            this.data_pointer += 4;\n        }\n\n        dbg_assert(this.data_pointer <= this.data_end);\n        if(this.data_pointer === this.data_end)\n        {\n            this.write_end();\n        }\n    }\n};\n\nIDEInterface.prototype.write_data_port8 = function(data)\n{\n    this.write_data_port(data, 1);\n};\n\nIDEInterface.prototype.write_data_port16 = function(data)\n{\n    this.write_data_port(data, 2);\n};\n\nIDEInterface.prototype.write_data_port32 = function(data)\n{\n    this.write_data_port(data, 4);\n};\n\nIDEInterface.prototype.write_end = function()\n{\n    if(this.current_command === ATA_CMD_PACKET)\n    {\n        this.atapi_handle();\n    }\n    else\n    {\n        if(LOG_DETAILS & LOG_DETAIL_RW)\n        {\n            dbg_log(this.name + \": write_end data_pointer=\" + h(this.data_pointer) +\n                \" data_length=\" + h(this.data_length), LOG_DISK);\n        }\n\n        if(this.data_pointer >= this.data_length)\n        {\n            this.do_write();\n        }\n        else\n        {\n            dbg_assert(this.current_command === ATA_CMD_WRITE_SECTORS ||\n                this.current_command === ATA_CMD_WRITE_SECTORS_EXT ||\n                this.current_command === ATA_CMD_WRITE_MULTIPLE_EXT,\n                \"Unexpected command: \" + h(this.current_command));\n\n            // XXX: Should advance here, but do_write does all the advancing\n            //this.ata_advance(this.current_command, 1);\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.data_end += 512;\n            this.push_irq();\n        }\n    }\n};\n\nIDEInterface.prototype.ata_advance = function(cmd, sectors)\n{\n    if(LOG_DETAILS & LOG_DETAIL_RW)\n    {\n        dbg_log(this.name + \": advance sectors=\" + sectors + \" old_sector_count_reg=\" + this.sector_count_reg, LOG_DISK);\n    }\n    this.sector_count_reg -= sectors;\n\n    if(cmd === ATA_CMD_READ_SECTORS_EXT ||\n            cmd === ATA_CMD_READ_MULTIPLE ||\n            cmd === ATA_CMD_READ_DMA_EXT ||\n            cmd === ATA_CMD_WRITE_SECTORS_EXT ||\n            cmd === ATA_CMD_WRITE_MULTIPLE ||\n            cmd === ATA_CMD_WRITE_DMA_EXT)\n    {\n        var new_sector = sectors + this.get_lba48();\n        this.lba_low_reg = new_sector & 0xFF | new_sector >> 16 & 0xFF00;\n        this.lba_mid_reg = new_sector >> 8 & 0xFF;\n        this.lba_high_reg = new_sector >> 16 & 0xFF;\n    }\n    else if(this.is_lba)\n    {\n        var new_sector = sectors + this.get_lba28();\n        this.lba_low_reg = new_sector & 0xFF;\n        this.lba_mid_reg = new_sector >> 8 & 0xFF;\n        this.lba_high_reg = new_sector >> 16 & 0xFF;\n        this.head = this.head & ~0xF | new_sector & 0xF;\n    }\n    else // chs\n    {\n        var new_sector = sectors + this.get_chs();\n\n        var c = new_sector / (this.head_count * this.sectors_per_track) | 0;\n        this.lba_mid_reg = c & 0xFF;\n        this.lba_high_reg = c >> 8 & 0xFF;\n        this.head = (new_sector / this.sectors_per_track | 0) % this.head_count & 0xF;\n        this.lba_low_reg = (new_sector % this.sectors_per_track + 1) & 0xFF;\n\n        dbg_assert(new_sector === this.get_chs());\n    }\n};\n\nIDEInterface.prototype.ata_read_sectors = function(cmd)\n{\n    var is_lba48 = cmd === ATA_CMD_READ_SECTORS_EXT || cmd === ATA_CMD_READ_MULTIPLE;\n    var count = this.get_count(is_lba48);\n    var lba = this.get_lba(is_lba48);\n\n    var is_single = cmd === ATA_CMD_READ_SECTORS || cmd === ATA_CMD_READ_SECTORS_EXT;\n\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    if(LOG_DETAILS & LOG_DETAIL_RW)\n    {\n        dbg_log(this.name + \": ATA read cmd=\" + h(cmd) +\n                \" mode=\" + (this.is_lba ? \"lba\" : \"chs\") +\n                \" lba=\" + h(lba) +\n                \" lbacount=\" + h(count) +\n                \" bytecount=\" + h(byte_count), LOG_DISK);\n    }\n\n    if(start + byte_count > this.buffer.byteLength)\n    {\n        dbg_assert(false, this.name + \": ATA read: Outside of disk\", LOG_DISK);\n\n        this.status_reg = 0xFF;\n        this.push_irq();\n    }\n    else\n    {\n        this.status_reg = ATA_SR_DRDY|ATA_SR_BSY;\n        this.report_read_start();\n\n        this.read_buffer(start, byte_count, (data) =>\n        {\n            if(LOG_DETAILS & LOG_DETAIL_RW)\n            {\n                dbg_log(this.name + \": ata_read: Data arrived\", LOG_DISK);\n            }\n\n            this.data_set(data);\n            this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n            this.data_end = is_single ? 512 : Math.min(byte_count, this.sectors_per_drq * 512);\n            this.ata_advance(cmd, is_single ? 1 : Math.min(count, this.sectors_per_track));\n\n            this.push_irq();\n            this.report_read_end(byte_count);\n        });\n    }\n};\n\nIDEInterface.prototype.ata_read_sectors_dma = function(cmd)\n{\n    var is_lba48 = cmd === ATA_CMD_READ_DMA_EXT;\n    var count = this.get_count(is_lba48);\n    var lba = this.get_lba(is_lba48);\n\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.name + \": ATA DMA read lba=\" + h(lba) +\n                \" lbacount=\" + h(count) +\n                \" bytecount=\" + h(byte_count), LOG_DISK);\n    }\n\n    if(start + byte_count > this.buffer.byteLength)\n    {\n        dbg_assert(false, this.name + \": ATA read: Outside of disk\", LOG_DISK);\n\n        this.status_reg = 0xFF;\n        this.push_irq();\n        return;\n    }\n\n    this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n    this.channel.dma_status |= 1;\n};\n\nIDEInterface.prototype.do_ata_read_sectors_dma = function()\n{\n    var cmd = this.current_command;\n\n    var is_lba48 = cmd === ATA_CMD_READ_DMA_EXT;\n    var count = this.get_count(is_lba48);\n    var lba = this.get_lba(is_lba48);\n\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    dbg_assert(lba < this.buffer.byteLength);\n\n    this.report_read_start();\n\n    var orig_prdt_start = this.channel.prdt_addr;\n\n    this.read_buffer(start, byte_count, (data) =>\n    {\n        if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n        {\n            dbg_log(this.name + \": do_ata_read_sectors_dma: Data arrived\", LOG_DISK);\n        }\n        var prdt_start = this.channel.prdt_addr;\n        var offset = 0;\n\n        dbg_assert(orig_prdt_start === prdt_start);\n\n        do {\n            var prd_addr = this.cpu.read32s(prdt_start);\n            var prd_count = this.cpu.read16(prdt_start + 4);\n            var end = this.cpu.read8(prdt_start + 7) & 0x80;\n\n            if(!prd_count)\n            {\n                prd_count = 0x10000;\n                if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n                {\n                    dbg_log(this.name + \": DMA: prd count was 0\", LOG_DISK);\n                }\n            }\n\n            if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n            {\n                dbg_log(this.name + \": DMA read transfer dest=\" + h(prd_addr) +\n                    \" prd_count=\" + h(prd_count), LOG_DISK);\n            }\n            this.cpu.write_blob(data.subarray(offset, offset + prd_count), prd_addr);\n\n            offset += prd_count;\n            prdt_start += 8;\n        }\n        while(!end);\n\n        dbg_assert(offset === byte_count);\n\n        this.ata_advance(this.current_command, count);\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n        this.channel.dma_status &= ~1;\n        this.current_command = -1;\n\n        this.report_read_end(byte_count);\n\n        this.push_irq();\n    });\n};\n\nIDEInterface.prototype.ata_write_sectors = function(cmd)\n{\n    var is_lba48 = cmd === ATA_CMD_WRITE_SECTORS_EXT || cmd === ATA_CMD_WRITE_MULTIPLE;\n    var count = this.get_count(is_lba48);\n    var lba = this.get_lba(is_lba48);\n\n    var is_single = cmd === ATA_CMD_WRITE_SECTORS || cmd === ATA_CMD_WRITE_SECTORS_EXT;\n\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    if(LOG_DETAILS & LOG_DETAIL_RW)\n    {\n        dbg_log(this.name + \": ATA write lba=\" + h(lba) +\n                \" mode=\" + (this.is_lba ? \"lba\" : \"chs\") +\n                \" lbacount=\" + h(count) +\n                \" bytecount=\" + h(byte_count), LOG_DISK);\n    }\n\n    if(start + byte_count > this.buffer.byteLength)\n    {\n        dbg_assert(false, this.name + \": ATA write: Outside of disk\", LOG_DISK);\n\n        this.status_reg = 0xFF;\n        this.push_irq();\n    }\n    else\n    {\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n        this.data_allocate_noclear(byte_count);\n        this.data_end = is_single ? 512 : Math.min(byte_count, this.sectors_per_drq * 512);\n        this.write_dest = start;\n    }\n};\n\nIDEInterface.prototype.ata_write_sectors_dma = function(cmd)\n{\n    var is_lba48 = cmd === ATA_CMD_WRITE_DMA_EXT;\n    var count = this.get_count(is_lba48);\n    var lba = this.get_lba(is_lba48);\n\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.name + \": ATA DMA write lba=\" + h(lba) +\n                \" lbacount=\" + h(count) +\n                \" bytecount=\" + h(byte_count), LOG_DISK);\n    }\n\n    if(start + byte_count > this.buffer.byteLength)\n    {\n        dbg_assert(false, this.name + \": ATA DMA write: Outside of disk\", LOG_DISK);\n\n        this.status_reg = 0xFF;\n        this.push_irq();\n        return;\n    }\n\n    this.status_reg = ATA_SR_DRDY|ATA_SR_DSC|ATA_SR_DRQ;\n    this.channel.dma_status |= 1;\n};\n\nIDEInterface.prototype.do_ata_write_sectors_dma = function()\n{\n    var cmd = this.current_command;\n\n    var is_lba48 = cmd === ATA_CMD_WRITE_DMA_EXT;\n    var count = this.get_count(is_lba48);\n    var lba = this.get_lba(is_lba48);\n\n    var byte_count = count * this.sector_size;\n    var start = lba * this.sector_size;\n\n    var prdt_start = this.channel.prdt_addr;\n    var offset = 0;\n\n    if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n    {\n        dbg_log(this.name + \": prdt addr: \" + h(prdt_start, 8), LOG_DISK);\n    }\n\n    const buffer = new Uint8Array(byte_count);\n\n    do {\n        var prd_addr = this.cpu.read32s(prdt_start);\n        var prd_count = this.cpu.read16(prdt_start + 4);\n        var end = this.cpu.read8(prdt_start + 7) & 0x80;\n\n        if(!prd_count)\n        {\n            prd_count = 0x10000;\n            dbg_log(this.name + \": DMA: prd count was 0\", LOG_DISK);\n        }\n\n        if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n        {\n            dbg_log(this.name + \": DMA write transfer dest=\" + h(prd_addr) + \" prd_count=\" + h(prd_count), LOG_DISK);\n        }\n\n        var slice = this.cpu.mem8.subarray(prd_addr, prd_addr + prd_count);\n        dbg_assert(slice.length === prd_count);\n\n        buffer.set(slice, offset);\n\n        //if(DEBUG)\n        //{\n        //    dbg_log(hex_dump(slice), LOG_DISK);\n        //}\n\n        offset += prd_count;\n        prdt_start += 8;\n    }\n    while(!end);\n\n    dbg_assert(offset === buffer.length);\n\n    this.buffer.set(start, buffer, () =>\n    {\n        if(LOG_DETAILS & LOG_DETAIL_RW_DMA)\n        {\n            dbg_log(this.name + \": DMA write completed\", LOG_DISK);\n        }\n        this.ata_advance(this.current_command, count);\n        this.status_reg = ATA_SR_DRDY|ATA_SR_DSC;\n        this.push_irq();\n        this.channel.dma_status &= ~1;\n        this.current_command = -1;\n    });\n\n    this.report_write(byte_count);\n};\n\nIDEInterface.prototype.get_chs = function()\n{\n    var c = this.lba_mid_reg & 0xFF | this.lba_high_reg << 8 & 0xFF00;\n    var h = this.head;\n    var s = this.lba_low_reg & 0xFF;\n\n    if(LOG_DETAILS & LOG_DETAIL_CHS)\n    {\n        dbg_log(this.name + \": get_chs: c=\" + c + \" h=\" + h + \" s=\" + s, LOG_DISK);\n    }\n\n    return (c * this.head_count + h) * this.sectors_per_track + s - 1;\n};\n\nIDEInterface.prototype.get_lba28 = function()\n{\n    return this.lba_low_reg & 0xFF |\n            this.lba_mid_reg << 8 & 0xFF00 |\n            this.lba_high_reg << 16 & 0xFF0000 |\n            (this.head & 0xF) << 24;\n};\n\nIDEInterface.prototype.get_lba48 = function()\n{\n    // Note: Bits over 32 missing\n    return (this.lba_low_reg & 0xFF |\n            this.lba_mid_reg << 8 & 0xFF00 |\n            this.lba_high_reg << 16 & 0xFF0000 |\n            (this.lba_low_reg >> 8) << 24 & 0xFF000000) >>> 0;\n};\n\nIDEInterface.prototype.get_lba = function(is_lba48)\n{\n    if(is_lba48)\n    {\n        return this.get_lba48();\n    }\n    else if(this.is_lba)\n    {\n        return this.get_lba28();\n    }\n    else\n    {\n        return this.get_chs();\n    }\n};\n\nIDEInterface.prototype.get_count = function(is_lba48)\n{\n    if(is_lba48)\n    {\n        var count = this.sector_count_reg;\n        if(count === 0) count = 0x10000;\n        return count;\n    }\n    else\n    {\n        var count = this.sector_count_reg & 0xFF;\n        if(count === 0) count = 0x100;\n        return count;\n    }\n};\n\nIDEInterface.prototype.create_identify_packet = function()\n{\n    const cylinder_count = Math.min(16383, this.cylinder_count);\n    const strcpy_be16 = (out_buffer, ofs16, len16, str) => {\n        let ofs8 = ofs16 << 1;\n        const len8 = len16 << 1;\n        const end8 = ofs8 + len8;\n        out_buffer.fill(32, ofs8, len8); // fill output buffer with ASCII whitespace\n        for(let i_str = 0; i_str < str.length && ofs8 < end8; i_str++) {\n            if(i_str & 1) {\n                out_buffer[ofs8] = str.charCodeAt(i_str);\n                ofs8 += 2;\n            }\n            else {\n                out_buffer[ofs8 + 1] = str.charCodeAt(i_str);\n            }\n        }\n    };\n\n    // Initialize array of 256 16-bit words (big-endian)\n    // Best source for the lower 64 words of the memory layout used below:\n    // - [ATA-retro]\n    //   AT Attachment Interface for Disk Drives, Revision 4c\n    //   https://dn790009.ca.archive.org/0/items/SCSISpecificationDocumentsATAATAPI/ATA_ATAPI/AT%20Attachment%20Interface%20for%20Disk%20Drives%20Revision%204c.pdf\n    // For the words above 64 see [ATA-6] Table 27, [ATA8-ACS] 7.12 and 7.13.\n    //\n    // dead link: http://bochs.sourceforge.net/cgi-bin/lxr/source/iodev/harddrv.cc#L2821\n\n    // most significant bit indicates ATAPI CD-ROM device\n    const general_cfg = this.is_atapi ? 0x8540 : 0x0040;\n    // multiword DMA transfer mode, meaning of 0x0407:\n    // - 0x0007: Multiword DMA modes 2, 1 and 0 are supported\n    // - 0x0400: Multiword DMA mode 2 is selected\n    const multiword_dma_mode = this.current_command === ATA_CMD_PACKET ? 0 : 0x0407;\n    // Major version number: bits 3/4/5/6 indicate support for ATA/ATAPI-3/4/5/6 (bits 0/1/2 are obsolete in [ATA-6])\n    const major_version = 0x0000;   // device does not report version\n    // supported ATA:   NOP, FLUSH CACHE, FLUSH CACHE EXT, 48-bit addr\n    // supported ATAPI: NOP, DEVICE RESET, PACKET and FLUSH CACHE\n    const feat_82 = this.is_atapi ? 1 << 14 | 1 << 9 | 1 << 5 : 1 << 14;\n    const feat_83 = this.is_atapi ? 1 << 14 | 1 << 12 : 1 << 14 | 1 << 13 | 1 << 12 | 1 << 10;\n    const feat_84 = this.is_atapi ? 1 << 14 : 1 << 14;\n\n    this.data.fill(0, 0, 512);\n    this.data_set([\n        // 0: General configuration\n        general_cfg & 0xFF, general_cfg >> 8 & 0xFF,\n        // 1: Number of cylinders\n        cylinder_count & 0xFF, cylinder_count >> 8 & 0xFF,\n        // 2: reserved\n        0, 0,\n        // 3: Number of heads\n        this.head_count & 0xFF, this.head_count >> 8 & 0xFF,\n        // 4: Number of unformatted bytes per track\n        this.sectors_per_track / 512 & 0xFF, this.sectors_per_track / 512 >> 8 & 0xFF,\n        // 5: Number of unformatted bytes per sector\n        0, 512 >> 8,\n        // 6: Number of sectors per track\n        this.sectors_per_track & 0xFF, this.sectors_per_track >> 8 & 0xFF,\n        // 7-9: Vendor-unique\n        0, 0, 0, 0,  0, 0,\n        // 10-19: Serial number (20 ASCII characters, filled below)\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,\n        // 20: Buffer type\n        3, 0,\n        // 21: Buffer size in 512 byte increments\n        0, 2,\n        // 22: Number of ECC bytes avail on read/write long cmds\n        4, 0,\n        // 23-26: Firmware revision (8 ASCII characters, filled below)\n        0, 0, 0, 0,  0, 0, 0, 0,\n        // 27-46: Model number (40 ASCII characters, filled below)\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,  0, 0, 0, 0,\n        // 47: Max. number of sectors per interrupt on read/write multiple commands (1st byte) and Vendor-unique (2nd)\n        0x80, 0,\n        // 48: Indicates whether can perform doubleword I/O (1st byte) [0: no, 1: yes]\n        1, 0,\n        // 49: Vendor-unique (1st byte) and Capabilities (2nd) [2: Only LBA, 3: LBA and DMA]\n        0, 2,\n        // 50: reserved\n        0, 0,\n        // 51: PIO data transfer cycle timing mode\n        0, 2,\n        // 52: DMA data transfer cycle timing mode\n        0, 2,\n        // 53: Indicates whether fields 54-58 are valid (1st byte) [0: no, 1: yes]\n        7, 0,\n        // 54: Number of current cylinders\n        cylinder_count & 0xFF, cylinder_count >> 8 & 0xFF,\n        // 55: Number of current heads\n        this.head_count & 0xFF, this.head_count >> 8 & 0xFF,\n        // 56: Number of current sectors per track\n        this.sectors_per_track, 0,\n        // 57-58: Current capacity in sectors\n        this.sector_count & 0xFF, this.sector_count >> 8 & 0xFF,\n        this.sector_count >> 16 & 0xFF, this.sector_count >> 24 & 0xFF,\n        // 59:  Multiple sector setting\n        0, 0,\n        // 60-61: Total number of user addressable sectors (LBA mode only)\n        this.sector_count & 0xFF, this.sector_count >> 8 & 0xFF,\n        this.sector_count >> 16 & 0xFF, this.sector_count >> 24 & 0xFF,\n        // 62: Single word DMA transfer mode\n        0, 0,\n        // 63: Multiword DMA transfer mode (DMA supported mode, DMA selected mode)\n        multiword_dma_mode & 0xFF, multiword_dma_mode >> 8 & 0xFF,\n\n        // 64: PIO modes supported\n        0, 0,\n        // 65-68: fields related to cycle-time\n        30, 0, 30, 0, 30, 0, 30, 0,\n        // 69-74: reserved\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0, 0, 0,\n        // 75: Queue depth\n        0, 0,\n        // 76-79: reserved\n        0, 0, 0, 0,  0, 0, 0, 0,\n        // 80: Major version number\n        major_version & 0xFF, major_version >> 8 & 0xFF,\n        // 81: Minor version number\n        0, 0,\n        // 82: Command set supported\n        feat_82 & 0xFF, feat_82 >> 8 & 0xFF,\n        // 83: Command set supported\n        feat_83 & 0xFF, feat_83 >> 8 & 0xFF,\n        // 84: Command set/feature supported extension\n        feat_84 & 0xFF, feat_84 >> 8 & 0xFF,\n        // 85: Command set/feature enabled (copy of 82)\n        feat_82 & 0xFF, feat_82 >> 8 & 0xFF,\n        // 86: Command set/feature enabled (copy of 83)\n        feat_83 & 0xFF, feat_83 >> 8 & 0xFF,\n        // 87: Command set/feature default (copy of 84)\n        feat_84 & 0xFF, feat_84 >> 8 & 0xFF,\n        // 88: DMA related field\n        0, 0,\n        // 89: Time required for security erase unit completion\n        0, 0,\n        // 90: Time required for Enhanced security erase completion\n        0, 0,\n        // 91: Current advanced power management value\n        0, 0,\n        // 92: Master Password Revision Code\n        0, 0,\n        // 93: Hardware reset result\n        1, 0x60,\n        // 94: Acoustic management value\n        0, 0,\n        // 95-99: reserved\n        0, 0, 0, 0,  0, 0, 0, 0,\n        0, 0,\n        // 100-101: Maximum user LBA for 48-bit Address feature set.\n        this.sector_count & 0xFF, this.sector_count >> 8 & 0xFF,\n        this.sector_count >> 16 & 0xFF, this.sector_count >> 24 & 0xFF,\n    ]);\n\n    // 10-19 serial number\n    strcpy_be16(this.data, 10, 10, `8086-86${this.channel_nr}${this.interface_nr}`);\n    // 23-26 firmware revision\n    strcpy_be16(this.data, 23, 4, \"1.00\");\n    // 27-46 model number\n    strcpy_be16(this.data, 27, 20, this.is_atapi ? \"v86 ATAPI CD-ROM\" : \"v86 ATA HD\");\n\n    this.data_length = 512;\n    this.data_end = 512;\n};\n\nIDEInterface.prototype.data_allocate = function(len)\n{\n    this.data_allocate_noclear(len);\n    this.data32.fill(0, 0, len + 3 >> 2);\n};\n\nIDEInterface.prototype.data_allocate_noclear = function(len)\n{\n    if(this.data.length < len)\n    {\n        this.data = new Uint8Array(len + 3 & ~3);\n        this.data16 = new Uint16Array(this.data.buffer);\n        this.data32 = new Int32Array(this.data.buffer);\n    }\n\n    this.data_length = len;\n    this.data_pointer = 0;\n};\n\nIDEInterface.prototype.data_set = function(data)\n{\n    this.data_allocate_noclear(data.length);\n    this.data.set(data);\n};\n\nIDEInterface.prototype.report_read_start = function()\n{\n    this.bus.send(\"ide-read-start\");\n};\n\nIDEInterface.prototype.report_read_end = function(byte_count)\n{\n    const sector_count = byte_count / this.sector_size | 0;\n    this.bus.send(\"ide-read-end\", [this.channel_nr, byte_count, sector_count]);\n};\n\nIDEInterface.prototype.report_write = function(byte_count)\n{\n    const sector_count = byte_count / this.sector_size | 0;\n    this.bus.send(\"ide-write-end\", [this.channel_nr, byte_count, sector_count]);\n};\n\nIDEInterface.prototype.read_buffer = function(start, length, callback)\n{\n    const id = this.last_io_id++;\n    this.in_progress_io_ids.add(id);\n\n    this.buffer.get(start, length, data =>\n    {\n        if(this.cancelled_io_ids.delete(id))\n        {\n            dbg_assert(!this.in_progress_io_ids.has(id));\n            return;\n        }\n\n        const removed = this.in_progress_io_ids.delete(id);\n        dbg_assert(removed);\n\n        callback(data);\n    });\n};\n\nIDEInterface.prototype.cancel_io_operations = function()\n{\n    for(const id of this.in_progress_io_ids)\n    {\n        this.cancelled_io_ids.add(id);\n    }\n    this.in_progress_io_ids.clear();\n};\n\nIDEInterface.prototype.get_state = function()\n{\n    var state = [];\n    state[0] = this.sector_count_reg;\n    state[1] = this.cylinder_count;\n    state[2] = this.lba_high_reg;\n    state[3] = this.lba_mid_reg;\n    state[4] = this.data_pointer;\n    state[5] = 0;\n    state[6] = 0;\n    state[7] = 0;\n    state[8] = 0;\n    state[9] = this.device_reg;\n    state[10] = this.error_reg;\n    state[11] = this.head;\n    state[12] = this.head_count;\n    state[13] = this.is_atapi;\n    state[14] = this.is_lba;\n    state[15] = this.features_reg;\n    state[16] = this.data;\n    state[17] = this.data_length;\n    state[18] = this.lba_low_reg;\n    state[19] = this.sector_count;\n    state[20] = this.sector_size;\n    state[21] = this.sectors_per_drq;\n    state[22] = this.sectors_per_track;\n    state[23] = this.status_reg;\n    state[24] = this.write_dest;\n    state[25] = this.current_command;\n    state[26] = this.data_end;\n    state[27] = this.current_atapi_command;\n    state[28] = this.buffer;\n    return state;\n};\n\nIDEInterface.prototype.set_state = function(state)\n{\n    this.sector_count_reg = state[0];\n    this.cylinder_count = state[1];\n    this.lba_high_reg = state[2];\n    this.lba_mid_reg = state[3];\n    this.data_pointer = state[4];\n\n    this.device_reg = state[9];\n    this.error_reg = state[10];\n    this.head = state[11];\n    this.head_count = state[12];\n    this.is_atapi = state[13];\n    this.is_lba = state[14];\n    this.features_reg = state[15];\n    this.data = state[16];\n    this.data_length = state[17];\n    this.lba_low_reg = state[18];\n    this.sector_count = state[19];\n    this.sector_size = state[20];\n    this.sectors_per_drq = state[21];\n    this.sectors_per_track = state[22];\n    this.status_reg = state[23];\n    this.write_dest = state[24];\n    this.current_command = state[25];\n\n    this.data_end = state[26];\n    this.current_atapi_command = state[27];\n\n    this.data16 = new Uint16Array(this.data.buffer);\n    this.data32 = new Int32Array(this.data.buffer);\n\n    this.buffer && this.buffer.set_state(state[28]);\n\n    this.drive_connected = this.is_atapi || this.buffer;\n    this.medium_changed = false;\n};\n"
  },
  {
    "path": "src/io.js",
    "content": "import { LOG_IO, MMAP_BLOCK_BITS, MMAP_BLOCK_SIZE, MMAP_MAX } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\n\n// Enables logging all IO port reads and writes. Very verbose\nconst LOG_ALL_IO = false;\n\n/**\n * The ISA IO bus\n * Devices register their ports here\n *\n * @constructor\n * @param {CPU} cpu\n */\nexport function IO(cpu)\n{\n    /** @const */\n    this.ports = [];\n\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    for(var i = 0; i < 0x10000; i++)\n    {\n        this.ports[i] = this.create_empty_entry();\n    }\n\n    var memory_size = cpu.memory_size[0];\n\n    for(var i = 0; (i << MMAP_BLOCK_BITS) < memory_size; i++)\n    {\n        // avoid sparse arrays\n        cpu.memory_map_read8[i] = cpu.memory_map_write8[i] = undefined;\n        cpu.memory_map_read32[i] = cpu.memory_map_write32[i] = undefined;\n    }\n\n    this.mmap_register(memory_size, MMAP_MAX - memory_size,\n        function(addr) {\n            // read outside of the memory size\n            dbg_log(\"Read from unmapped memory space, addr=\" + h(addr >>> 0, 8), LOG_IO);\n            return 0xFF;\n        },\n        function(addr, value) {\n            // write outside of the memory size\n            dbg_log(\"Write to unmapped memory space, addr=\" + h(addr >>> 0, 8) + \" value=\" + h(value, 2), LOG_IO);\n        },\n        function(addr) {\n            dbg_log(\"Read from unmapped memory space, addr=\" + h(addr >>> 0, 8), LOG_IO);\n            return -1;\n        },\n        function(addr, value) {\n            dbg_log(\"Write to unmapped memory space, addr=\" + h(addr >>> 0, 8) + \" value=\" + h(value >>> 0, 8), LOG_IO);\n        }\n    );\n}\n\nIO.prototype.create_empty_entry = function()\n{\n    return {\n        read8: this.empty_port_read8,\n        read16: this.empty_port_read16,\n        read32: this.empty_port_read32,\n\n        write8: this.empty_port_write,\n        write16: this.empty_port_write,\n        write32: this.empty_port_write,\n\n        device: undefined,\n    };\n};\n\nIO.prototype.empty_port_read8 = function()\n{\n    return 0xFF;\n};\n\nIO.prototype.empty_port_read16 = function()\n{\n    return 0xFFFF;\n};\n\nIO.prototype.empty_port_read32 = function()\n{\n    return -1;\n};\n\nIO.prototype.empty_port_write = function(x)\n{\n};\n\n\n/**\n * @param {number} port_addr\n * @param {Object} device\n * @param {function(number):number=} r8\n * @param {function(number):number=} r16\n * @param {function(number):number=} r32\n */\nIO.prototype.register_read = function(port_addr, device, r8, r16, r32)\n{\n    dbg_assert(typeof port_addr === \"number\");\n    dbg_assert(typeof device === \"object\");\n    dbg_assert(!r8 || typeof r8 === \"function\");\n    dbg_assert(!r16 || typeof r16 === \"function\");\n    dbg_assert(!r32 || typeof r32 === \"function\");\n    dbg_assert(r8 || r16 || r32);\n\n    if(DEBUG)\n    {\n        var fail = function(n) {\n            dbg_assert(false, \"Overlapped read\" + n + \" \" + h(port_addr, 4) + \" (\" + device.name + \")\");\n            return -1 >>> (32 - n) | 0;\n        };\n        if(!r8) r8 = fail.bind(this, 8);\n        if(!r16) r16 = fail.bind(this, 16);\n        if(!r32) r32 = fail.bind(this, 32);\n    }\n\n    if(r8) this.ports[port_addr].read8 = r8;\n    if(r16) this.ports[port_addr].read16 = r16;\n    if(r32) this.ports[port_addr].read32 = r32;\n    this.ports[port_addr].device = device;\n};\n\n/**\n * @param {number} port_addr\n * @param {Object} device\n * @param {function(number)=} w8\n * @param {function(number)=} w16\n * @param {function(number)=} w32\n */\nIO.prototype.register_write = function(port_addr, device, w8, w16, w32)\n{\n    dbg_assert(typeof port_addr === \"number\");\n    dbg_assert(typeof device === \"object\");\n    dbg_assert(!w8 || typeof w8 === \"function\");\n    dbg_assert(!w16 || typeof w16 === \"function\");\n    dbg_assert(!w32 || typeof w32 === \"function\");\n    dbg_assert(w8 || w16 || w32);\n\n    if(DEBUG)\n    {\n        var fail = function(n) {\n            dbg_assert(false, \"Overlapped write\" + n + \" \" + h(port_addr) + \" (\" + device.name + \")\");\n        };\n        if(!w8) w8 = fail.bind(this, 8);\n        if(!w16) w16 = fail.bind(this, 16);\n        if(!w32) w32 = fail.bind(this, 32);\n    }\n\n    if(w8) this.ports[port_addr].write8 = w8;\n    if(w16) this.ports[port_addr].write16 = w16;\n    if(w32) this.ports[port_addr].write32 = w32;\n    this.ports[port_addr].device = device;\n};\n\n/**\n * > Any two consecutive 8-bit ports can be treated as a 16-bit port;\n * > and four consecutive 8-bit ports can be treated as a 32-bit port\n * > http://css.csail.mit.edu/6.858/2012/readings/i386/s08_01.htm\n *\n * This info is not correct for all ports, but handled by the following functions\n *\n * Register the write of 2 or 4 consecutive 8-bit ports, 1 or 2 16-bit\n * ports and 0 or 1 32-bit ports\n *\n * @param {number} port_addr\n * @param {!Object} device\n * @param {function():number} r8_1\n * @param {function():number} r8_2\n * @param {function():number=} r8_3\n * @param {function():number=} r8_4\n */\nIO.prototype.register_read_consecutive = function(port_addr, device, r8_1, r8_2, r8_3, r8_4)\n{\n    dbg_assert(arguments.length === 4 || arguments.length === 6);\n\n    function r16_1()\n    {\n        return r8_1.call(this) |\n                r8_2.call(this) << 8;\n    }\n    function r16_2()\n    {\n        return r8_3.call(this) |\n                r8_4.call(this) << 8;\n    }\n    function r32()\n    {\n        return r8_1.call(this) |\n                r8_2.call(this) << 8 |\n                r8_3.call(this) << 16 |\n                r8_4.call(this) << 24;\n    }\n\n    if(r8_3 && r8_4)\n    {\n        this.register_read(port_addr, device, r8_1, r16_1, r32);\n        this.register_read(port_addr + 1, device, r8_2);\n        this.register_read(port_addr + 2, device, r8_3, r16_2);\n        this.register_read(port_addr + 3, device, r8_4);\n    }\n    else\n    {\n        this.register_read(port_addr, device, r8_1, r16_1);\n        this.register_read(port_addr + 1, device, r8_2);\n    }\n};\n\n/**\n * @param {number} port_addr\n * @param {!Object} device\n * @param {function(number)} w8_1\n * @param {function(number)} w8_2\n * @param {function(number)=} w8_3\n * @param {function(number)=} w8_4\n */\nIO.prototype.register_write_consecutive = function(port_addr, device, w8_1, w8_2, w8_3, w8_4)\n{\n    dbg_assert(arguments.length === 4 || arguments.length === 6);\n\n    function w16_1(data)\n    {\n        w8_1.call(this, data & 0xFF);\n        w8_2.call(this, data >> 8 & 0xFF);\n    }\n    function w16_2(data)\n    {\n        w8_3.call(this, data & 0xFF);\n        w8_4.call(this, data >> 8 & 0xFF);\n    }\n    function w32(data)\n    {\n        w8_1.call(this, data & 0xFF);\n        w8_2.call(this, data >> 8 & 0xFF);\n        w8_3.call(this, data >> 16 & 0xFF);\n        w8_4.call(this, data >>> 24);\n    }\n\n    if(w8_3 && w8_4)\n    {\n        this.register_write(port_addr,     device, w8_1, w16_1, w32);\n        this.register_write(port_addr + 1, device, w8_2);\n        this.register_write(port_addr + 2, device, w8_3, w16_2);\n        this.register_write(port_addr + 3, device, w8_4);\n    }\n    else\n    {\n        this.register_write(port_addr,     device, w8_1, w16_1);\n        this.register_write(port_addr + 1, device, w8_2);\n    }\n};\n\nIO.prototype.mmap_read32_shim = function(addr)\n{\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n    var fn = this.cpu.memory_map_read8[aligned_addr];\n\n    return fn(addr) | fn(addr + 1) << 8 |\n            fn(addr + 2) << 16 | fn(addr + 3) << 24;\n};\n\nIO.prototype.mmap_write32_shim = function(addr, value)\n{\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n    var fn = this.cpu.memory_map_write8[aligned_addr];\n\n    fn(addr, value & 0xFF);\n    fn(addr + 1, value >> 8 & 0xFF);\n    fn(addr + 2, value >> 16 & 0xFF);\n    fn(addr + 3, value >>> 24);\n};\n\n/**\n * @param {number} addr\n * @param {number} size\n * @param {*} read_func8\n * @param {*} write_func8\n * @param {*=} read_func32\n * @param {*=} write_func32\n */\nIO.prototype.mmap_register = function(addr, size, read_func8, write_func8, read_func32, write_func32)\n{\n    dbg_log(\"mmap_register addr=\" + h(addr >>> 0, 8) + \" size=\" + h(size, 8), LOG_IO);\n\n    dbg_assert((addr & MMAP_BLOCK_SIZE - 1) === 0);\n    dbg_assert(size && (size & MMAP_BLOCK_SIZE - 1) === 0);\n\n    if(!read_func32)\n        read_func32 = this.mmap_read32_shim.bind(this);\n\n    if(!write_func32)\n        write_func32 = this.mmap_write32_shim.bind(this);\n\n    var aligned_addr = addr >>> MMAP_BLOCK_BITS;\n\n    for(; size > 0; aligned_addr++)\n    {\n        this.cpu.memory_map_read8[aligned_addr] = read_func8;\n        this.cpu.memory_map_write8[aligned_addr] = write_func8;\n        this.cpu.memory_map_read32[aligned_addr] = read_func32;\n        this.cpu.memory_map_write32[aligned_addr] = write_func32;\n\n        size -= MMAP_BLOCK_SIZE;\n    }\n};\n\n\nIO.prototype.port_write8 = function(port_addr, data)\n{\n    var entry = this.ports[port_addr];\n\n    if(entry.write8 === this.empty_port_write || LOG_ALL_IO)\n    {\n        dbg_log(\n            \"write8 port #\" + h(port_addr, 4) + \" <- \" + h(data, 2) + this.get_port_description(port_addr),\n            LOG_IO\n        );\n    }\n    return entry.write8.call(entry.device, data);\n};\n\nIO.prototype.port_write16 = function(port_addr, data)\n{\n    var entry = this.ports[port_addr];\n\n    if(entry.write16 === this.empty_port_write || LOG_ALL_IO)\n    {\n        dbg_log(\n            \"write16 port #\" + h(port_addr, 4) + \" <- \" + h(data, 4) + this.get_port_description(port_addr),\n            LOG_IO\n        );\n    }\n    return entry.write16.call(entry.device, data);\n};\n\nIO.prototype.port_write32 = function(port_addr, data)\n{\n    var entry = this.ports[port_addr];\n\n    if(entry.write32 === this.empty_port_write || LOG_ALL_IO)\n    {\n        dbg_log(\n            \"write32 port #\" + h(port_addr, 4) + \" <- \" + h(data >>> 0, 8) + this.get_port_description(port_addr),\n            LOG_IO\n        );\n    }\n    return entry.write32.call(entry.device, data);\n};\n\nIO.prototype.port_read8 = function(port_addr)\n{\n    var entry = this.ports[port_addr];\n\n    if(entry.read8 === this.empty_port_read8 || LOG_ALL_IO)\n    {\n        dbg_log(\n            \"read8 port  #\" + h(port_addr, 4) + this.get_port_description(port_addr),\n            LOG_IO\n        );\n    }\n    var value = entry.read8.call(entry.device, port_addr);\n    dbg_assert(typeof value === \"number\");\n    if(value < 0 || value >= 0x100) dbg_assert(false, \"8 bit port returned large value: \" + h(port_addr));\n    return value;\n};\n\nIO.prototype.port_read16 = function(port_addr)\n{\n    var entry = this.ports[port_addr];\n\n    if(entry.read16 === this.empty_port_read16 || LOG_ALL_IO)\n    {\n        dbg_log(\n            \"read16 port  #\" + h(port_addr, 4) + this.get_port_description(port_addr),\n            LOG_IO\n        );\n    }\n    var value = entry.read16.call(entry.device, port_addr);\n    dbg_assert(typeof value === \"number\");\n    if(value < 0 || value >= 0x10000) dbg_assert(false, \"16 bit port returned large value: \" + h(port_addr));\n    return value;\n};\n\nIO.prototype.port_read32 = function(port_addr)\n{\n    var entry = this.ports[port_addr];\n\n    if(entry.read32 === this.empty_port_read32 || LOG_ALL_IO)\n    {\n        dbg_log(\n            \"read32 port  #\" + h(port_addr, 4) + this.get_port_description(port_addr),\n            LOG_IO\n        );\n    }\n    var value = entry.read32.call(entry.device, port_addr);\n    dbg_assert((value | 0) === value);\n    return value;\n};\n\n// via seabios ioport.h\nvar debug_port_list = {\n    0x0004: \"PORT_DMA_ADDR_2\",\n    0x0005: \"PORT_DMA_CNT_2\",\n    0x000a: \"PORT_DMA1_MASK_REG\",\n    0x000b: \"PORT_DMA1_MODE_REG\",\n    0x000c: \"PORT_DMA1_CLEAR_FF_REG\",\n    0x000d: \"PORT_DMA1_MASTER_CLEAR\",\n    0x0020: \"PORT_PIC1_CMD\",\n    0x0021: \"PORT_PIC1_DATA\",\n    0x0040: \"PORT_PIT_COUNTER0\",\n    0x0041: \"PORT_PIT_COUNTER1\",\n    0x0042: \"PORT_PIT_COUNTER2\",\n    0x0043: \"PORT_PIT_MODE\",\n    0x0060: \"PORT_PS2_DATA\",\n    0x0061: \"PORT_PS2_CTRLB\",\n    0x0064: \"PORT_PS2_STATUS\",\n    0x0070: \"PORT_CMOS_INDEX\",\n    0x0071: \"PORT_CMOS_DATA\",\n    0x0080: \"PORT_DIAG\",\n    0x0081: \"PORT_DMA_PAGE_2\",\n    0x0092: \"PORT_A20\",\n    0x00a0: \"PORT_PIC2_CMD\",\n    0x00a1: \"PORT_PIC2_DATA\",\n    0x00b2: \"PORT_SMI_CMD\",\n    0x00b3: \"PORT_SMI_STATUS\",\n    0x00d4: \"PORT_DMA2_MASK_REG\",\n    0x00d6: \"PORT_DMA2_MODE_REG\",\n    0x00da: \"PORT_DMA2_MASTER_CLEAR\",\n    0x00f0: \"PORT_MATH_CLEAR\",\n    0x0170: \"PORT_ATA2_CMD_BASE\",\n    0x01f0: \"PORT_ATA1_CMD_BASE\",\n    0x0278: \"PORT_LPT2\",\n    0x02e8: \"PORT_SERIAL4\",\n    0x02f8: \"PORT_SERIAL2\",\n    0x0374: \"PORT_ATA2_CTRL_BASE\",\n    0x0378: \"PORT_LPT1\",\n    0x03e8: \"PORT_SERIAL3\",\n    //0x03f4: \"PORT_ATA1_CTRL_BASE\",\n    0x03f0: \"PORT_FD_BASE\",\n    0x03f2: \"PORT_FD_DOR\",\n    0x03f4: \"PORT_FD_STATUS\",\n    0x03f5: \"PORT_FD_DATA\",\n    0x03f6: \"PORT_HD_DATA\",\n    0x03f7: \"PORT_FD_DIR\",\n    0x03f8: \"PORT_SERIAL1\",\n    0x0cf8: \"PORT_PCI_CMD\",\n    0x0cf9: \"PORT_PCI_REBOOT\",\n    0x0cfc: \"PORT_PCI_DATA\",\n    0x0402: \"PORT_BIOS_DEBUG\",\n    0x0510: \"PORT_QEMU_CFG_CTL\",\n    0x0511: \"PORT_QEMU_CFG_DATA\",\n    0xb000: \"PORT_ACPI_PM_BASE\",\n    0xb100: \"PORT_SMB_BASE\",\n    0x8900: \"PORT_BIOS_APM\"\n};\n\nIO.prototype.get_port_description = function(addr)\n{\n    if(debug_port_list[addr])\n    {\n        return \"  (\" + debug_port_list[addr] + \")\";\n    }\n    else\n    {\n        return \"\";\n    }\n};\n"
  },
  {
    "path": "src/iso9660.js",
    "content": "// Source: https://wiki.osdev.org/ISO_9660\n\n// Limitations:\n// - can only generate iso files\n// - only supports a single directory, no file system hierarchy\n// - root directory entry is limited to 2 KiB (~42 files)\n// - filenames are normalised to 8.3 length and [A-Z0-9_.]\n\nimport { dbg_assert } from \"./log.js\";\n\nconst BLOCK_SIZE = 2 * 1024; // 0x800\n\nconst FILE_FLAGS_HIDDEN = 1 << 0;\nconst FILE_FLAGS_DIRECTORY = 1 << 1;\nconst FILE_FLAGS_ASSOCIATED_FILE = 1 << 2;\nconst FILE_FLAGS_HAS_EXTENDED_ATTRIBUTE_RECORD = 1 << 3;\nconst FILE_FLAGS_HAS_PERMISSIONS = 1 << 4;\nconst FILE_FLAGS_NOT_FINAL = 1 << 7;\n\n/**\n * @param {Array.<{ name: string, contents: Uint8Array}>} files\n */\nexport function generate(files)\n{\n    const te = new TextEncoder();\n    const date = new Date;\n\n    const write8 = (b, v) => { b.buffer[b.offset++] = v; };\n    const write_le16 = (b, v) => { b.buffer[b.offset++] = v; b.buffer[b.offset++] = v >> 8; };\n    const write_le32 = (b, v) => { b.buffer[b.offset++] = v; b.buffer[b.offset++] = v >> 8; b.buffer[b.offset++] = v >> 16; b.buffer[b.offset++] = v >> 24; };\n    const write_be16 = (b, v) => { b.buffer[b.offset++] = v >> 8; b.buffer[b.offset++] = v; };\n    const write_be32 = (b, v) => { b.buffer[b.offset++] = v >> 24; b.buffer[b.offset++] = v >> 16; b.buffer[b.offset++] = v >> 8; b.buffer[b.offset++] = v; };\n    const write_lebe16 = (b, v) => { write_le16(b, v); write_be16(b, v); };\n    const write_lebe32 = (b, v) => { write_le32(b, v); write_be32(b, v); };\n    const fill = (b, len, v) => { b.buffer.fill(v, b.offset, b.offset += len); };\n    const write_ascii = (b, v) => { b.offset += te.encodeInto(v, b.buffer.subarray(b.offset)).written; };\n    const write_padded_ascii = (b, len, v) => { b.offset += te.encodeInto(v.padEnd(len), b.buffer.subarray(b.offset)).written; };\n    const write_dummy_date_ascii = b => { fill(b, 16, 0x20); write8(b, 0); };\n    const write_date_compact = b => {\n        write8(b, date.getUTCFullYear() - 1900);\n        write8(b, 1 + date.getUTCMonth());\n        write8(b, date.getUTCDate());\n        write8(b, date.getUTCHours());\n        write8(b, date.getUTCMinutes());\n        write8(b, date.getUTCSeconds());\n        write8(b, 0);\n    };\n    const skip = (b, len) => { b.offset += len; };\n\n    const write_record = (b, name, flags, is_special, lba, len) => {\n        if(!is_special) name = sanitise_filename(name) + \";1\";\n        // write name first and get its length\n        const START = buffer.offset;\n        const NAME_OFFSET = 33;\n        const name_len = te.encodeInto(name, b.buffer.subarray(b.offset + NAME_OFFSET)).written;\n        const pad = (name_len & 1) ? 0 : 1;\n        const len_field = 33 + name_len + pad;\n        dbg_assert(len_field < 256);\n        write8(buffer, len_field);      // Length of directory record\n        write8(buffer, 0);              // Extended Attribute Record length\n        write_lebe32(buffer, lba);      // Location of extent (LBA)\n        write_lebe32(buffer, len);      // Data length (size of extent)\n        write_date_compact(buffer);\n        write8(buffer, flags);\n        write8(buffer, 0);              // File unit size for files recorded in interleaved mode, zero otherwise\n        write8(buffer, 0);              // Interleave gap size for files recorded in interleaved mode, zero otherwise\n        write_lebe16(buffer, 1);        // Volume sequence number - the volume that this extent is recorded on\n        write8(buffer, name_len);       // length of file name\n        dbg_assert(buffer.offset === START + NAME_OFFSET);\n        skip(buffer, name_len + pad);   // File name: was already written\n        dbg_assert(buffer.offset === START + len_field);\n    };\n    const write_special_directory_record = (b, name, lba, len) => write_record(b, name, FILE_FLAGS_DIRECTORY, true, lba, len);\n    const write_file_record = (b, name, lba, len) => write_record(b, name, 0, false, lba, len);\n\n    function round_byte_size_to_block_size(n)\n    {\n        return 1 + Math.floor((n - 1) / BLOCK_SIZE);\n    }\n    dbg_assert(round_byte_size_to_block_size(0) === 0);\n    dbg_assert(round_byte_size_to_block_size(1) === 1);\n    dbg_assert(round_byte_size_to_block_size(BLOCK_SIZE - 1) === 1);\n    dbg_assert(round_byte_size_to_block_size(BLOCK_SIZE) === 1);\n    dbg_assert(round_byte_size_to_block_size(BLOCK_SIZE + 1) === 2);\n    dbg_assert(round_byte_size_to_block_size(2 * BLOCK_SIZE) === 2);\n    dbg_assert(round_byte_size_to_block_size(2 * BLOCK_SIZE + 1) === 3);\n    dbg_assert(round_byte_size_to_block_size(10 * BLOCK_SIZE + 1) === 11);\n\n    function to_msdos_filename(name)\n    {\n        const dot = name.lastIndexOf(\".\");\n        if(dot === -1) return name.substr(0, 8);\n        return name.substr(0, Math.min(8, dot)) + \".\" + name.substr(dot + 1, 3);\n    }\n\n    dbg_assert(to_msdos_filename(\"abcdefghijkl.qwerty\") === \"abcdefgh.qwe\");\n    dbg_assert(to_msdos_filename(\"abcdefghijkl\") === \"abcdefgh\");\n\n    function sanitise_filename(name)\n    {\n        return to_msdos_filename(name.toUpperCase().replace(/[^A-Z0-9_.]/g, \"\"));\n    }\n\n    // layout:\n    // (lba = one block of BLOCK_SIZE bytes)\n    // LBA   | contents\n    // ------+--------\n    // 0..15 | System Area (could be used for mbr, but not used by us)\n    //    16 | Primary Volume Descriptor\n    //    17 | Volume Descriptor Set Terminator\n    //    18 | empty\n    //    19 | Little Endian Path Table\n    //    20 | empty\n    //    21 | Big Endian Path Table\n    //    22 | empty\n    //    23 | Root directory\n    // 24..n | File contents\n    const SYSTEM_AREA_SIZE = 16 * BLOCK_SIZE;\n    const PRIMARY_VOLUME_LBA = 16;\n    const VOLUME_SET_TERMINATOR_LBA = 17;\n    const LE_PATH_TABLE_LBA = 19;\n    const BE_PATH_TABLE_LBA = 21;\n    const ROOT_DIRECTORY_LBA = 23;\n    const LE_PATH_TABLE_SIZE = BLOCK_SIZE;\n    const BE_PATH_TABLE_SIZE = BLOCK_SIZE;\n    const ROOT_DIRECTORY_SIZE = BLOCK_SIZE;\n\n    let next_file_lba = 24;\n    files = files.map(({ name, contents }) => {\n        const lba = next_file_lba;\n        next_file_lba += round_byte_size_to_block_size(contents.length);\n        name = to_msdos_filename(name);\n        return { name, contents, lba };\n    });\n\n    const N_LBAS = next_file_lba;\n    const total_size = N_LBAS * BLOCK_SIZE;\n\n    const buffer = {\n        buffer: new Uint8Array(total_size),\n        offset: SYSTEM_AREA_SIZE,\n    };\n\n    // LBA 16: Primary Volume Descriptor\n    dbg_assert(buffer.offset === PRIMARY_VOLUME_LBA * BLOCK_SIZE);\n    write8(buffer, 0x01);                    // Volume Descriptor type: Primary Volume Descriptor\n    write_ascii(buffer, \"CD001\");            // Always CD001\n    write8(buffer, 0x01);                    // Version\n    write8(buffer, 0x00);                    // unused\n    write_padded_ascii(buffer, 32, \"V86\");   // System Identifier\n    write_padded_ascii(buffer, 32, \"CDROM\"); // Identification of this volume\n    skip(buffer, 8);                         // unused\n    write_lebe32(buffer, N_LBAS);\n    skip(buffer, 32);                        // unused\n    dbg_assert(buffer.offset === 0x8000 + 120);\n\n    write_lebe16(buffer, 1); // Volume Set Size\n    write_lebe16(buffer, 1); // Volume Sequence Number\n    dbg_assert(buffer.offset === 0x8080);\n\n    write_lebe16(buffer, BLOCK_SIZE);\n\n    write_lebe32(buffer, 10);               // Path Table Size\n    write_le32(buffer, LE_PATH_TABLE_LBA);  // Location of Type-L Path Table\n    write_le32(buffer, 0);                  // Location of the Optional Type-L Path Table\n    write_be32(buffer, BE_PATH_TABLE_LBA);  // Location of Type-M Path Table\n    write_be32(buffer, 0);                  // Location of the Optional Type-M Path Table\n    dbg_assert(buffer.offset === 0x8000 + 156);\n\n    // Directory entry for the root directory\n    write_special_directory_record(buffer, \"\\x00\", ROOT_DIRECTORY_LBA, 0x800);\n    dbg_assert(buffer.offset === 0x8000 + 190);\n\n    fill(buffer, 128, 0x20); // Volume Set Identifier\n    fill(buffer, 128, 0x20); // Publisher Identifier\n    fill(buffer, 128, 0x20); // Data Preparer Identifier\n    fill(buffer, 128, 0x20); // Application Identifier\n    fill(buffer,  37, 0x20); // Copyright File Identifier\n    fill(buffer,  37, 0x20); // Abstract File Identifier\n    fill(buffer,  37, 0x20); // Bibliographic File Identifier\n\n    dbg_assert(buffer.offset === 0x8000 + 813);\n\n    write_dummy_date_ascii(buffer); // Volume Creation Date and Time\n    write_dummy_date_ascii(buffer); // Volume Modification Date and Time\n    write_dummy_date_ascii(buffer); // Volume Expiration Date and Time\n    write_dummy_date_ascii(buffer); // Volume Effective Date and Time\n\n    write8(buffer, 0x01); // File Structure Version\n    dbg_assert(buffer.offset === 0x8000 + 882);\n\n    write8(buffer, 0x00); // Unused\n    skip(buffer, 512);    // Application Used\n    skip(buffer, 653);    // Reserved\n\n    // LBA 17: Volume Descriptor Set Terminator\n    dbg_assert(buffer.offset === VOLUME_SET_TERMINATOR_LBA * BLOCK_SIZE);\n    write8(buffer, 0xFF);           // 0xFF: Volume Descriptor Set Terminator\n    write_ascii(buffer, \"CD001\");   // Always CD001\n    write8(buffer, 0x01);           // Version\n\n    // LBA 19: Little Endian Path Table\n    buffer.offset = LE_PATH_TABLE_LBA * BLOCK_SIZE;\n    write8(buffer, 0x01);                   // Length of Directory Identifier\n    write8(buffer, 0x00);                   // Extended Attribute Record Length\n    write_le32(buffer, ROOT_DIRECTORY_LBA); // Location of Extent (LBA)\n    write_le16(buffer, 1);                  // Directory number of parent directory\n    write_ascii(buffer, \"\\x00\");            // file name\n    dbg_assert(buffer.offset < LE_PATH_TABLE_LBA * BLOCK_SIZE + LE_PATH_TABLE_SIZE);\n\n    // LBA 21: Big Endian Path Table\n    buffer.offset = BE_PATH_TABLE_LBA * BLOCK_SIZE;\n    write8(buffer, 0x01);                   // Length of Directory Identifier\n    write8(buffer, 0x00);                   // Extended Attribute Record Length\n    write_be32(buffer, ROOT_DIRECTORY_LBA); // Location of Extent (LBA)\n    write_be16(buffer, 1);                  // Directory number of parent directory\n    write_ascii(buffer, \"\\x00\");            // file name\n    dbg_assert(buffer.offset < BE_PATH_TABLE_LBA * BLOCK_SIZE + BE_PATH_TABLE_SIZE);\n\n    // LBA 23: root directory\n    buffer.offset = ROOT_DIRECTORY_LBA * BLOCK_SIZE;\n    write_special_directory_record(buffer, \"\\x00\", ROOT_DIRECTORY_LBA, 0x800);  // \".\"\n    write_special_directory_record(buffer, \"\\x01\", ROOT_DIRECTORY_LBA, 0x800);  // \"..\"\n    for(const { name, contents, lba } of files)\n    {\n        write_file_record(buffer, name, lba, contents.length);\n    }\n    // TODO: this assertion can fail if too many files are used as input\n    // ROOT_DIRECTORY_SIZE should be choosen dynamically\n    dbg_assert(buffer.offset < ROOT_DIRECTORY_LBA * BLOCK_SIZE + ROOT_DIRECTORY_SIZE);\n\n    // file contents\n    for(let { contents, lba } of files)\n    {\n        buffer.buffer.set(contents, lba * BLOCK_SIZE);\n    }\n\n    return buffer.buffer;\n}\n\n/**\n * @param {Uint8Array} buffer\n */\nexport function is_probably_iso9660_file(buffer)\n{\n    return (\n        buffer.length >= 17 * BLOCK_SIZE &&\n        buffer[BLOCK_SIZE + 0] ===  1 && // Primary Volume Descriptor\n        buffer[BLOCK_SIZE + 1] === 67 && // \"C\"\n        buffer[BLOCK_SIZE + 2] === 68 && // \"D\"\n        buffer[BLOCK_SIZE + 3] === 48 && // \"0\"\n        buffer[BLOCK_SIZE + 4] === 48 && // \"0\"\n        buffer[BLOCK_SIZE + 5] === 49    // \"1\"\n    );\n}\n"
  },
  {
    "path": "src/kernel.js",
    "content": "import { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n\n// https://www.kernel.org/doc/Documentation/x86/boot.txt\n\nconst LINUX_BOOT_HDR_SETUP_SECTS = 0x1F1;\nconst LINUX_BOOT_HDR_SYSSIZE = 0x1F4;\nconst LINUX_BOOT_HDR_VIDMODE = 0x1FA;\nconst LINUX_BOOT_HDR_BOOT_FLAG = 0x1FE;\nconst LINUX_BOOT_HDR_HEADER = 0x202;\nconst LINUX_BOOT_HDR_VERSION = 0x206;\nconst LINUX_BOOT_HDR_TYPE_OF_LOADER = 0x210;\nconst LINUX_BOOT_HDR_LOADFLAGS = 0x211;\nconst LINUX_BOOT_HDR_CODE32_START = 0x214;\nconst LINUX_BOOT_HDR_RAMDISK_IMAGE = 0x218;\nconst LINUX_BOOT_HDR_RAMDISK_SIZE = 0x21C;\nconst LINUX_BOOT_HDR_HEAP_END_PTR = 0x224;\nconst LINUX_BOOT_HDR_CMD_LINE_PTR = 0x228;\nconst LINUX_BOOT_HDR_INITRD_ADDR_MAX = 0x22C;\nconst LINUX_BOOT_HDR_KERNEL_ALIGNMENT = 0x230;\nconst LINUX_BOOT_HDR_RELOCATABLE_KERNEL = 0x234;\nconst LINUX_BOOT_HDR_MIN_ALIGNMENT = 0x235;\nconst LINUX_BOOT_HDR_XLOADFLAGS = 0x236;\nconst LINUX_BOOT_HDR_CMDLINE_SIZE = 0x238;\nconst LINUX_BOOT_HDR_PAYLOAD_OFFSET = 0x248;\nconst LINUX_BOOT_HDR_PAYLOAD_LENGTH = 0x24C;\nconst LINUX_BOOT_HDR_PREF_ADDRESS = 0x258;\nconst LINUX_BOOT_HDR_INIT_SIZE = 0x260;\n\nconst LINUX_BOOT_HDR_CHECKSUM1 = 0xAA55;\nconst LINUX_BOOT_HDR_CHECKSUM2 = 0x53726448;\n\nconst LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED = 0xFF;\n\nconst LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH = 1 << 0;\nconst LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG = 1 << 5;\nconst LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS = 1 << 6;\nconst LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS = 1 << 7;\n\n\nexport function load_kernel(mem8, bzimage, initrd, cmdline)\n{\n    dbg_log(\"Trying to load kernel of size \" + bzimage.byteLength);\n\n    const KERNEL_HIGH_ADDRESS = 0x100000;\n\n    // Put the initrd at the 64 MB boundary. This means the minimum memory size\n    // is 64 MB plus the size of the initrd.\n    // Note: If set too low, kernel may fail to load the initrd with \"invalid magic at start of compressed archive\"\n    const INITRD_ADDRESS = 64 << 20;\n\n    const quiet = false;\n\n    const bzimage8 = new Uint8Array(bzimage);\n    const bzimage16 = new Uint16Array(bzimage);\n    const bzimage32 = new Uint32Array(bzimage);\n\n    const setup_sects = bzimage8[LINUX_BOOT_HDR_SETUP_SECTS] || 4;\n    const syssize = bzimage32[LINUX_BOOT_HDR_SYSSIZE >> 2] << 4;\n\n    const vidmode = bzimage16[LINUX_BOOT_HDR_VIDMODE >> 1];\n\n    const checksum1 = bzimage16[LINUX_BOOT_HDR_BOOT_FLAG >> 1];\n    if(checksum1 !== LINUX_BOOT_HDR_CHECKSUM1)\n    {\n        dbg_log(\"Bad checksum1: \" + h(checksum1));\n        return;\n    }\n\n    // Not aligned, so split into two 16-bit reads\n    const checksum2 =\n        bzimage16[LINUX_BOOT_HDR_HEADER >> 1] |\n        bzimage16[LINUX_BOOT_HDR_HEADER + 2 >> 1] << 16;\n    if(checksum2 !== LINUX_BOOT_HDR_CHECKSUM2)\n    {\n        dbg_log(\"Bad checksum2: \" + h(checksum2));\n        return;\n    }\n\n    const protocol = bzimage16[LINUX_BOOT_HDR_VERSION >> 1];\n    dbg_assert(protocol >= 0x202); // older not supported by us\n\n    const flags = bzimage8[LINUX_BOOT_HDR_LOADFLAGS];\n    dbg_assert(flags & LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH); // low kernels not supported by us\n\n    // we don't relocate the kernel, so we don't care much about most of these\n\n    const flags2 = bzimage16[LINUX_BOOT_HDR_XLOADFLAGS >> 1];\n    const initrd_addr_max = bzimage32[LINUX_BOOT_HDR_INITRD_ADDR_MAX >> 2];\n    const kernel_alignment = bzimage32[LINUX_BOOT_HDR_KERNEL_ALIGNMENT >> 2];\n    const relocatable_kernel = bzimage8[LINUX_BOOT_HDR_RELOCATABLE_KERNEL];\n    const min_alignment = bzimage8[LINUX_BOOT_HDR_MIN_ALIGNMENT];\n    const cmdline_size = protocol >= 0x206 ? bzimage32[LINUX_BOOT_HDR_CMDLINE_SIZE >> 2] : 255;\n    const payload_offset = bzimage32[LINUX_BOOT_HDR_PAYLOAD_OFFSET >> 2];\n    const payload_length = bzimage32[LINUX_BOOT_HDR_PAYLOAD_LENGTH >> 2];\n    const pref_address = bzimage32[LINUX_BOOT_HDR_PREF_ADDRESS >> 2];\n    const pref_address_high = bzimage32[LINUX_BOOT_HDR_PREF_ADDRESS + 4 >> 2];\n    const init_size = bzimage32[LINUX_BOOT_HDR_INIT_SIZE >> 2];\n\n    dbg_log(\"kernel boot protocol version: \" + h(protocol));\n    dbg_log(\"flags=\" + h(flags) + \" xflags=\" + h(flags2));\n    dbg_log(\"code32_start=\" + h(bzimage32[LINUX_BOOT_HDR_CODE32_START >> 2]));\n    dbg_log(\"initrd_addr_max=\" + h(initrd_addr_max));\n    dbg_log(\"kernel_alignment=\" + h(kernel_alignment));\n    dbg_log(\"relocatable=\" + relocatable_kernel);\n    dbg_log(\"min_alignment=\" + h(min_alignment));\n    dbg_log(\"cmdline max=\" + h(cmdline_size));\n    dbg_log(\"payload offset=\" + h(payload_offset) + \" size=\" + h(payload_length));\n    dbg_log(\"pref_address=\" + h(pref_address_high) + \":\" + h(pref_address));\n    dbg_log(\"init_size=\" + h(init_size));\n\n    const real_mode_segment = 0x8000;\n    const base_ptr = real_mode_segment << 4;\n\n    const heap_end = 0xE000;\n    const heap_end_ptr = heap_end - 0x200;\n\n    // fill in the kernel boot header with infos the kernel needs to know\n\n    bzimage8[LINUX_BOOT_HDR_TYPE_OF_LOADER] = LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED;\n\n    const new_flags =\n        (quiet ? flags | LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG : flags & ~LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG)\n        & ~LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS\n        | LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS;\n    bzimage8[LINUX_BOOT_HDR_LOADFLAGS] = new_flags;\n\n    bzimage16[LINUX_BOOT_HDR_HEAP_END_PTR >> 1] = heap_end_ptr;\n\n    // should parse the vga=... paramter from cmdline here, but we don't really care\n    bzimage16[LINUX_BOOT_HDR_VIDMODE >> 1] = 0xFFFF; // normal\n\n    dbg_log(\"heap_end_ptr=\" + h(heap_end_ptr));\n\n    cmdline += \"\\x00\";\n    dbg_assert(cmdline.length < cmdline_size);\n\n    const cmd_line_ptr = base_ptr + heap_end;\n    dbg_log(\"cmd_line_ptr=\" + h(cmd_line_ptr));\n\n    bzimage32[LINUX_BOOT_HDR_CMD_LINE_PTR >> 2] = cmd_line_ptr;\n    for(let i = 0; i < cmdline.length; i++)\n    {\n        mem8[cmd_line_ptr + i] = cmdline.charCodeAt(i);\n    }\n\n    const prot_mode_kernel_start = (setup_sects + 1) * 512;\n    dbg_log(\"prot_mode_kernel_start=\" + h(prot_mode_kernel_start));\n\n    const real_mode_kernel = new Uint8Array(bzimage, 0, prot_mode_kernel_start);\n    const protected_mode_kernel = new Uint8Array(bzimage, prot_mode_kernel_start);\n\n    let ramdisk_address = 0;\n    let ramdisk_size = 0;\n\n    if(initrd)\n    {\n        ramdisk_address = INITRD_ADDRESS;\n        ramdisk_size = initrd.byteLength;\n\n        dbg_assert(KERNEL_HIGH_ADDRESS + protected_mode_kernel.length < ramdisk_address);\n\n        mem8.set(new Uint8Array(initrd), ramdisk_address);\n    }\n\n    bzimage32[LINUX_BOOT_HDR_RAMDISK_IMAGE >> 2] = ramdisk_address;\n    bzimage32[LINUX_BOOT_HDR_RAMDISK_SIZE >> 2] = ramdisk_size;\n\n    dbg_assert(base_ptr + real_mode_kernel.length < 0xA0000);\n\n    mem8.set(real_mode_kernel, base_ptr);\n    mem8.set(protected_mode_kernel, KERNEL_HIGH_ADDRESS);\n\n    return {\n        name: \"genroms/kernel.bin\",\n        data: make_linux_boot_rom(real_mode_segment, heap_end),\n    };\n}\n\nfunction make_linux_boot_rom(real_mode_segment, heap_end)\n{\n    // This rom will be executed by seabios after its initialisation\n    // It sets up segment registers, the stack and calls the kernel real mode entry point\n\n    const SIZE = 0x200;\n\n    const data8 = new Uint8Array(SIZE);\n    const data16 = new Uint16Array(data8.buffer);\n\n    data16[0] = 0xAA55;\n    data8[2] = SIZE / 0x200;\n\n    let i = 3;\n\n    data8[i++] = 0xFA; // cli\n    data8[i++] = 0xB8; // mov ax, real_mode_segment\n    data8[i++] = real_mode_segment >> 0;\n    data8[i++] = real_mode_segment >> 8;\n    data8[i++] = 0x8E; // mov es, ax\n    data8[i++] = 0xC0;\n    data8[i++] = 0x8E; // mov ds, ax\n    data8[i++] = 0xD8;\n    data8[i++] = 0x8E; // mov fs, ax\n    data8[i++] = 0xE0;\n    data8[i++] = 0x8E; // mov gs, ax\n    data8[i++] = 0xE8;\n    data8[i++] = 0x8E; // mov ss, ax\n    data8[i++] = 0xD0;\n    data8[i++] = 0xBC; // mov sp, heap_end\n    data8[i++] = heap_end >> 0;\n    data8[i++] = heap_end >> 8;\n    data8[i++] = 0xEA; // jmp (real_mode_segment+0x20):0x0\n    data8[i++] = 0x00;\n    data8[i++] = 0x00;\n    data8[i++] = real_mode_segment + 0x20 >> 0;\n    data8[i++] = real_mode_segment + 0x20 >> 8;\n\n    dbg_assert(i < SIZE);\n\n    const checksum_index = i;\n    data8[checksum_index] = 0;\n\n    let checksum = 0;\n\n    for(let i = 0; i < data8.length; i++)\n    {\n        checksum += data8[i];\n    }\n\n    data8[checksum_index] = -checksum;\n\n    return data8;\n}\n"
  },
  {
    "path": "src/lib.js",
    "content": "import { dbg_assert } from \"./log.js\";\n\n// pad string with spaces on the right\nexport function pads(str, len)\n{\n    str = (str || str === 0) ? str + \"\" : \"\";\n    return str.padEnd(len, \" \");\n}\n\n// pad string with zeros on the left\nexport function pad0(str, len)\n{\n    str = (str || str === 0) ? str + \"\" : \"\";\n    return str.padStart(len, \"0\");\n}\n\nexport var view = function(constructor, memory, offset, length)\n{\n    dbg_assert(offset >= 0);\n    return new Proxy({},\n        {\n            get: function(target, property, receiver)\n            {\n                const b = new constructor(memory.buffer, offset, length);\n                const x = b[property];\n                if(typeof x === \"function\")\n                {\n                    return x.bind(b);\n                }\n                dbg_assert(/^\\d+$/.test(property) || property === \"buffer\" || property === \"length\" ||\n                    property === \"BYTES_PER_ELEMENT\" || property === \"byteOffset\");\n                return x;\n            },\n            set: function(target, property, value, receiver)\n            {\n                dbg_assert(/^\\d+$/.test(property));\n                new constructor(memory.buffer, offset, length)[property] = value;\n                return true;\n            },\n        });\n};\n\n/**\n * number to hex\n * @param {number} n\n * @param {number=} len\n * @return {string}\n */\nexport function h(n, len)\n{\n    if(!n)\n    {\n        var str = \"\";\n    }\n    else\n    {\n        var str = n.toString(16);\n    }\n\n    return \"0x\" + pad0(str.toUpperCase(), len || 1);\n}\n\nexport function hex_dump(buffer)\n{\n    function hex(n, len)\n    {\n        return pad0(n.toString(16).toUpperCase(), len);\n    }\n\n    const result = [];\n    let offset = 0;\n\n    for(; offset + 15 < buffer.length; offset += 16)\n    {\n        let line = hex(offset, 5) + \"   \";\n\n        for(let j = 0; j < 0x10; j++)\n        {\n            line += hex(buffer[offset + j], 2) + \" \";\n        }\n\n        line += \"  \";\n\n        for(let j = 0; j < 0x10; j++)\n        {\n            const x = buffer[offset + j];\n            line += (x >= 33 && x !== 34 && x !== 92 && x <= 126) ? String.fromCharCode(x) : \".\";\n        }\n\n        result.push(line);\n    }\n\n    let line = hex(offset, 5) + \"   \";\n\n    for(; offset < buffer.length; offset++)\n    {\n        line += hex(buffer[offset], 2) + \" \";\n    }\n\n    const remainder = offset & 0xF;\n    line += \"   \".repeat(0x10 - remainder);\n    line += \"  \";\n\n    for(let j = 0; j < remainder; j++)\n    {\n        const x = buffer[offset + j];\n        line += (x >= 33 && x !== 34 && x !== 92 && x <= 126) ? String.fromCharCode(x) : \".\";\n    }\n\n    result.push(line);\n\n    return \"\\n\" + result.join(\"\\n\") + \"\\n\";\n}\n\n/* global require */\nexport var get_rand_int;\nif(typeof crypto !== \"undefined\" && crypto.getRandomValues)\n{\n    const rand_data = new Int32Array(1);\n\n    get_rand_int = function()\n    {\n        crypto.getRandomValues(rand_data);\n        return rand_data[0];\n    };\n}\nelse if(typeof require !== \"undefined\")\n{\n    /** @type {{ randomBytes: Function }} */\n    const crypto = require(\"crypto\");\n\n    get_rand_int = function()\n    {\n        return crypto.randomBytes(4)[\"readInt32LE\"](0);\n    };\n}\nelse if(typeof process !== \"undefined\")\n    {\n        import(\"node:\" + \"crypto\").then(crypto => {\n            get_rand_int = function()\n            {\n                return crypto[\"randomBytes\"](4)[\"readInt32LE\"](0);\n            };\n        });\n    }\nelse\n{\n    dbg_assert(false, \"Unsupported platform: No cryptographic random values\");\n}\n\nexport var int_log2;\n\nif(typeof Math.clz32 === \"function\" && Math.clz32(0) === 32 && Math.clz32(0x12345) === 15 && Math.clz32(-1) === 0)\n{\n    /**\n     * calculate the integer logarithm base 2\n     * @param {number} x\n     * @return {number}\n     */\n    int_log2 = function(x)\n    {\n        dbg_assert(x > 0);\n\n        return 31 - Math.clz32(x);\n    };\n} else {\n\n    var int_log2_table = new Int8Array(256);\n\n    for(var i = 0, b = -2; i < 256; i++)\n    {\n        if(!(i & i - 1))\n            b++;\n\n        int_log2_table[i] = b;\n    }\n\n    /**\n     * calculate the integer logarithm base 2\n     * @param {number} x\n     * @return {number}\n     */\n    int_log2 = function(x)\n    {\n        x >>>= 0;\n        dbg_assert(x > 0);\n\n        // http://jsperf.com/integer-log2/6\n        var tt = x >>> 16;\n\n        if(tt)\n        {\n            var t = tt >>> 8;\n            if(t)\n            {\n                return 24 + int_log2_table[t];\n            }\n            else\n            {\n                return 16 + int_log2_table[tt];\n            }\n        }\n        else\n        {\n            var t = x >>> 8;\n            if(t)\n            {\n                return 8 + int_log2_table[t];\n            }\n            else\n            {\n                return int_log2_table[x];\n            }\n        }\n    };\n}\n\nexport const round_up_to_next_power_of_2 = function(x)\n{\n    dbg_assert(x >= 0);\n    return x <= 1 ? 1 : 1 << 1 + int_log2(x - 1);\n};\n\nif(typeof DEBUG !== \"undefined\" && DEBUG)\n{\n    dbg_assert(int_log2(1) === 0);\n    dbg_assert(int_log2(2) === 1);\n    dbg_assert(int_log2(7) === 2);\n    dbg_assert(int_log2(8) === 3);\n    dbg_assert(int_log2(123456789) === 26);\n\n    dbg_assert(round_up_to_next_power_of_2(0) === 1);\n    dbg_assert(round_up_to_next_power_of_2(1) === 1);\n    dbg_assert(round_up_to_next_power_of_2(2) === 2);\n    dbg_assert(round_up_to_next_power_of_2(7) === 8);\n    dbg_assert(round_up_to_next_power_of_2(8) === 8);\n    dbg_assert(round_up_to_next_power_of_2(123456789) === 134217728);\n}\n\n/**\n * @constructor\n *\n * Queue wrapper around Uint8Array\n * Used by devices such as the PS2 controller\n */\nexport function ByteQueue(size)\n{\n    var data = new Uint8Array(size),\n        start,\n        end;\n\n    dbg_assert((size & size - 1) === 0);\n\n    this.length = 0;\n\n    this.push = function(item)\n    {\n        if(this.length === size)\n        {\n            // intentional overwrite\n        }\n        else\n        {\n            this.length++;\n        }\n\n        data[end] = item;\n        end = end + 1 & size - 1;\n    };\n\n    this.shift = function()\n    {\n        if(!this.length)\n        {\n            return -1;\n        }\n        else\n        {\n            var item = data[start];\n\n            start = start + 1 & size - 1;\n            this.length--;\n\n            return item;\n        }\n    };\n\n    this.peek = function()\n    {\n        if(!this.length)\n        {\n            return -1;\n        }\n        else\n        {\n            return data[start];\n        }\n    };\n\n    this.clear = function()\n    {\n        start = 0;\n        end = 0;\n        this.length = 0;\n    };\n\n    this.clear();\n}\n\n\n/**\n * @constructor\n *\n * Queue wrapper around Float32Array\n * Used by devices such as the sound blaster sound card\n */\nexport function FloatQueue(size)\n{\n    this.size = size;\n    this.data = new Float32Array(size);\n    this.start = 0;\n    this.end = 0;\n    this.length = 0;\n\n    dbg_assert((size & size - 1) === 0);\n}\n\nFloatQueue.prototype.push = function(item)\n{\n    if(this.length === this.size)\n    {\n        // intentional overwrite\n        this.start = this.start + 1 & this.size - 1;\n    }\n    else\n    {\n        this.length++;\n    }\n\n    this.data[this.end] = item;\n    this.end = this.end + 1 & this.size - 1;\n};\n\nFloatQueue.prototype.shift = function()\n{\n    if(!this.length)\n    {\n        return undefined;\n    }\n    else\n    {\n        var item = this.data[this.start];\n\n        this.start = this.start + 1 & this.size - 1;\n        this.length--;\n\n        return item;\n    }\n};\n\nFloatQueue.prototype.shift_block = function(count)\n{\n    var slice = new Float32Array(count);\n\n    if(count > this.length)\n    {\n        count = this.length;\n    }\n    var slice_end = this.start + count;\n\n    var partial = this.data.subarray(this.start, slice_end);\n\n    slice.set(partial);\n    if(slice_end >= this.size)\n    {\n        slice_end -= this.size;\n        slice.set(this.data.subarray(0, slice_end), partial.length);\n    }\n    this.start = slice_end;\n\n    this.length -= count;\n\n    return slice;\n};\n\nFloatQueue.prototype.peek = function()\n{\n    if(!this.length)\n    {\n        return undefined;\n    }\n    else\n    {\n        return this.data[this.start];\n    }\n};\n\nFloatQueue.prototype.clear = function()\n{\n    this.start = 0;\n    this.end = 0;\n    this.length = 0;\n};\n\n\n/**\n * Simple circular queue for logs\n *\n * @param {number} size\n * @constructor\n */\nfunction CircularQueue(size)\n{\n    this.data = [];\n    this.index = 0;\n    this.size = size;\n}\n\nCircularQueue.prototype.add = function(item)\n{\n    this.data[this.index] = item;\n    this.index = (this.index + 1) % this.size;\n};\n\nCircularQueue.prototype.toArray = function()\n{\n    return [].slice.call(this.data, this.index).concat([].slice.call(this.data, 0, this.index));\n};\n\nCircularQueue.prototype.clear = function()\n{\n    this.data = [];\n    this.index = 0;\n};\n\n/**\n * @param {Array} new_data\n */\nCircularQueue.prototype.set = function(new_data)\n{\n    this.data = new_data;\n    this.index = 0;\n};\n\nexport function dump_file(ab, name)\n{\n    if(!Array.isArray(ab))\n    {\n        ab = [ab];\n    }\n\n    var blob = new Blob(ab);\n    download(blob, name);\n}\n\nexport function download(file_or_blob, name)\n{\n    var a = document.createElement(\"a\");\n    a[\"download\"] = name;\n    a.href = window.URL.createObjectURL(file_or_blob);\n    a.dataset[\"downloadurl\"] = [\"application/octet-stream\", a[\"download\"], a.href].join(\":\");\n\n    if(document.createEvent)\n    {\n        var ev = document.createEvent(\"MouseEvent\");\n        ev.initMouseEvent(\"click\", true, true, window,\n                          0, 0, 0, 0, 0, false, false, false, false, 0, null);\n        a.dispatchEvent(ev);\n    }\n    else\n    {\n        a.click();\n    }\n\n    window.URL.revokeObjectURL(a.href);\n}\n\n/**\n * A simple 1d bitmap\n * @constructor\n */\nexport var Bitmap = function(length_or_buffer)\n{\n    if(typeof length_or_buffer === \"number\")\n    {\n        this.view = new Uint8Array(length_or_buffer + 7 >> 3);\n    }\n    else if(length_or_buffer instanceof ArrayBuffer)\n    {\n        this.view = new Uint8Array(length_or_buffer);\n    }\n    else\n    {\n        dbg_assert(false, \"Bitmap: Invalid argument\");\n    }\n};\n\nBitmap.prototype.set = function(index, value)\n{\n    const bit_index = index & 7;\n    const byte_index = index >> 3;\n    const bit_mask = 1 << bit_index;\n\n    this.view[byte_index] =\n        value ? this.view[byte_index] | bit_mask : this.view[byte_index] & ~bit_mask;\n};\n\nBitmap.prototype.get = function(index)\n{\n    const bit_index = index & 7;\n    const byte_index = index >> 3;\n\n    return this.view[byte_index] >> bit_index & 1;\n};\n\nBitmap.prototype.get_buffer = function()\n{\n    return this.view.buffer;\n};\n\nexport var load_file;\nexport var get_file_size;\n\nif(typeof XMLHttpRequest === \"undefined\" ||\n    typeof process !== \"undefined\" && process.versions && process.versions.node)\n{\n    let fs;\n\n    /**\n     * @param {string} filename\n     * @param {Object} options\n     * @param {number=} n_tries\n     */\n    load_file = async function(filename, options, n_tries)\n    {\n        if(!fs)\n        {\n            // string concat to work around closure compiler 'Invalid module path \"node:fs/promises\" for resolution mode'\n            fs = await import(\"node:\" + \"fs/promises\");\n        }\n\n        if(options.range)\n        {\n            dbg_assert(!options.as_json);\n\n            const fd = await fs[\"open\"](filename, \"r\");\n\n            const length = options.range.length;\n            const buffer = Buffer.allocUnsafe(length);\n\n            try\n            {\n                /** @type {{ bytesRead: Number }} */\n                const result = await fd[\"read\"]({\n                    buffer,\n                    position: options.range.start\n                });\n                dbg_assert(result.bytesRead === length);\n            }\n            finally\n            {\n                await fd[\"close\"]();\n            }\n\n            options.done && options.done(new Uint8Array(buffer));\n        }\n        else\n        {\n            const o = {\n                encoding: options.as_json ? \"utf-8\" : null,\n            };\n\n            const data = await fs[\"readFile\"](filename, o);\n            const result = options.as_json ? JSON.parse(data) : new Uint8Array(data).buffer;\n\n            options.done(result);\n        }\n    };\n\n    get_file_size = async function(path)\n    {\n        if(!fs)\n        {\n            // string concat to work around closure compiler 'Invalid module path \"node:fs/promises\" for resolution mode'\n            fs = await import(\"node:\" + \"fs/promises\");\n        }\n        const stat = await fs[\"stat\"](path);\n        return stat.size;\n    };\n}\nelse\n{\n    /**\n     * @param {string} filename\n     * @param {Object} options\n     * @param {number=} n_tries\n     */\n    load_file = async function(filename, options, n_tries)\n    {\n        var http = new XMLHttpRequest();\n\n        http.open(options.method || \"get\", filename, true);\n\n        if(options.as_json)\n        {\n            http.responseType = \"json\";\n        }\n        else\n        {\n            http.responseType = \"arraybuffer\";\n        }\n\n        if(options.headers)\n        {\n            var header_names = Object.keys(options.headers);\n\n            for(var i = 0; i < header_names.length; i++)\n            {\n                var name = header_names[i];\n                http.setRequestHeader(name, options.headers[name]);\n            }\n        }\n\n        if(options.range)\n        {\n            const start = options.range.start;\n            const end = start + options.range.length - 1;\n            http.setRequestHeader(\"Range\", \"bytes=\" + start + \"-\" + end);\n            http.setRequestHeader(\"X-Accept-Encoding\", \"identity\");\n\n            // Abort if server responds with complete file in response to range\n            // request, to prevent downloading large files from broken http servers\n            http.onreadystatechange = function()\n            {\n                if(http.status === 200)\n                {\n                    console.error(\"Server sent full file in response to ranged request, aborting\", { filename });\n                    http.abort();\n                }\n            };\n        }\n\n        http.onload = function(e)\n        {\n            if(http.readyState === 4)\n            {\n                if(http.status !== 200 && http.status !== 206)\n                {\n                    console.error(\"Loading the image \" + filename + \" failed (status %d)\", http.status);\n                    if(http.status >= 500 && http.status < 600)\n                    {\n                        retry();\n                    }\n                }\n                else if(http.response)\n                {\n                    if(options.range)\n                    {\n                        const enc = http.getResponseHeader(\"Content-Encoding\");\n                        if(enc && enc !== \"identity\")\n                        {\n                            console.error(\"Server sent Content-Encoding in response to ranged request\", {filename, enc});\n                        }\n                    }\n                    options.done && options.done(http.response, http);\n                }\n            }\n        };\n\n        http.onerror = function(e)\n        {\n            console.error(\"Loading the image \" + filename + \" failed\", e);\n            retry();\n        };\n\n        if(options.progress)\n        {\n            http.onprogress = function(e)\n            {\n                options.progress(e);\n            };\n        }\n\n        http.send(null);\n\n        function retry()\n        {\n            const number_of_tries = n_tries || 0;\n            const timeout = [1, 1, 2, 3, 5, 8, 13, 21][number_of_tries] || 34;\n            setTimeout(() => {\n                load_file(filename, options, number_of_tries + 1);\n            }, 1000 * timeout);\n        }\n    };\n\n    get_file_size = async function(url)\n    {\n        return new Promise((resolve, reject) => {\n            load_file(url, {\n                done: (buffer, http) =>\n                {\n                    var header = http.getResponseHeader(\"Content-Range\") || \"\";\n                    var match = header.match(/\\/(\\d+)\\s*$/);\n\n                    if(match)\n                    {\n                        resolve(+match[1]);\n                    }\n                    else\n                    {\n                        const error = new Error(\"`Range: bytes=...` header not supported (Got `\" + header + \"`)\");\n                        reject(error);\n                    }\n                },\n                headers: {\n                    Range: \"bytes=0-0\",\n                    \"X-Accept-Encoding\": \"identity\"\n                }\n            });\n        });\n    };\n}\n\n// Reads len characters at offset from Memory object mem as a JS string\nexport function read_sized_string_from_mem(mem, offset, len)\n{\n    offset >>>= 0;\n    len >>>= 0;\n    return String.fromCharCode(...new Uint8Array(mem.buffer, offset, len));\n}\n\n/**\n * Unicode mappings of supported 8-bit code pages.\n * Each mapping is a string of 256 Unicode symbols used as a lookup table for 8-bit character codes.\n *\n * Supported mappings and their encoding labels:\n * - \"cp437\": CP437 (MS-DOS Latin US), default\n * - \"cp858\": CP858 (Western Europe), the lower 128 bytes are identical to CP437\n * - \"ascii\": ASCII (7-Bit), same as CP437 with lower 32 and upper 128 bytes mapped to \".\"\n *\n * @type {Object<string, string>}\n */\nconst CHARMAPS =\n{\n    cp437: \" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñÑªº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ \",\n    cp858: \"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø×ƒáíóúñÑªº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈ€ÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýÝ¯´­±‗¾¶§÷¸°¨·¹³²■ \"\n};\n\nCHARMAPS.cp858 = CHARMAPS.cp437.slice(0, 128) + CHARMAPS.cp858;\nCHARMAPS.ascii = CHARMAPS.cp437.split(\"\").map((c, i) => i > 31 && i < 128 ? c : \".\").join(\"\");\n\n/**\n * Return charmap for given encoding, default to CP437 if encoding is falsey or not defined in CHARMAPS.\n *\n * @param {string} encoding\n * @return {!string}\n */\nexport function get_charmap(encoding)\n{\n    return encoding && CHARMAPS[encoding] ? CHARMAPS[encoding] : CHARMAPS.cp437;\n}\n"
  },
  {
    "path": "src/log.js",
    "content": "if(typeof DEBUG === \"undefined\")\n{\n    globalThis.DEBUG = true;\n}\n\nimport { LOG_NAMES } from \"./const.js\";\nimport { pad0, pads } from \"./lib.js\";\nimport {\n    LOG_ALL, LOG_PS2, LOG_PIT, LOG_9P, LOG_PIC, LOG_DMA, LOG_NET, LOG_FLOPPY, LOG_DISK,\n    LOG_SERIAL, LOG_VGA, LOG_SB16, LOG_VIRTIO\n} from \"./const.js\";\n\n/** @const */\nexport var LOG_TO_FILE = false;\n\nexport var LOG_LEVEL = LOG_ALL & ~LOG_PS2 & ~LOG_PIT & ~LOG_VIRTIO & ~LOG_9P & ~LOG_PIC &\n                          ~LOG_DMA & ~LOG_SERIAL & ~LOG_NET & ~LOG_FLOPPY & ~LOG_DISK & ~LOG_VGA & ~LOG_SB16;\n\nexport function set_log_level(level) {\n    LOG_LEVEL = level;\n}\n\nexport var log_data = [];\n\nfunction do_the_log(message)\n{\n    if(LOG_TO_FILE)\n    {\n        log_data.push(message, \"\\n\");\n    }\n    else\n    {\n        console.log(message);\n    }\n}\n\n/**\n * @type {function((string|number), number=)}\n */\nexport const dbg_log = (function()\n{\n    if(!DEBUG)\n    {\n        return function() {};\n    }\n\n    /** @type {Object.<number, string>} */\n    const dbg_names = LOG_NAMES.reduce(function(a, x)\n    {\n        a[x[0]] = x[1];\n        return a;\n    }, {});\n\n    var log_last_message = \"\";\n    var log_message_repetitions = 0;\n\n    /**\n     * @param {number=} level\n     */\n    function dbg_log_(stuff, level)\n    {\n        if(!DEBUG) return;\n\n        level = level || 1;\n\n        if(level & LOG_LEVEL)\n        {\n            var level_name = dbg_names[level] || \"\",\n                message = \"[\" + pads(level_name, 4) + \"] \" + stuff;\n\n            if(message === log_last_message)\n            {\n                log_message_repetitions++;\n\n                if(log_message_repetitions < 2048)\n                {\n                    return;\n                }\n            }\n\n            var now = new Date();\n            var time_str = pad0(now.getHours(), 2) + \":\" +\n                           pad0(now.getMinutes(), 2) + \":\" +\n                           pad0(now.getSeconds(), 2) + \"+\" +\n                           pad0(now.getMilliseconds(), 3) + \" \";\n\n            if(log_message_repetitions)\n            {\n                if(log_message_repetitions === 1)\n                {\n                    do_the_log(time_str + log_last_message);\n                }\n                else\n                {\n                    do_the_log(\"Previous message repeated \" + log_message_repetitions + \" times\");\n                }\n\n                log_message_repetitions = 0;\n            }\n\n            do_the_log(time_str + message);\n            log_last_message = message;\n        }\n    }\n\n    return dbg_log_;\n})();\n\n/**\n * @param {number=} level\n */\nexport function dbg_trace(level)\n{\n    if(!DEBUG) return;\n\n    dbg_log(Error().stack, level);\n}\n\n/**\n * console.assert is fucking slow\n * @param {string=} msg\n * @param {number=} level\n */\nexport function dbg_assert(cond, msg, level)\n{\n    if(!DEBUG) return;\n\n    if(!cond)\n    {\n        dbg_assert_failed(msg);\n    }\n}\n\n\nexport function dbg_assert_failed(msg)\n{\n    debugger;\n    console.trace();\n\n    if(msg)\n    {\n        throw \"Assert failed: \" + msg;\n    }\n    else\n    {\n        throw \"Assert failed\";\n    }\n}\n"
  },
  {
    "path": "src/main.js",
    "content": "import { CPU } from \"./cpu.js\";\nimport { save_state, restore_state } from \"./state.js\";\nexport { V86 } from \"./browser/starter.js\";\n\n/**\n * @constructor\n * @param {Object=} wasm\n */\nexport function v86(bus, wasm)\n{\n    /** @type {boolean} */\n    this.running = false;\n\n    /** @type {boolean} */\n    this.stopping = false;\n\n    /** @type {boolean} */\n    this.idle = true;\n\n    this.tick_counter = 0;\n    this.worker = null;\n\n    /** @type {CPU} */\n    this.cpu = new CPU(bus, wasm, () => { this.idle && this.next_tick(0); });\n\n    this.bus = bus;\n\n    this.register_yield();\n}\n\nv86.prototype.run = function()\n{\n    this.stopping = false;\n\n    if(!this.running)\n    {\n        this.running = true;\n        this.bus.send(\"emulator-started\");\n    }\n\n    this.next_tick(0);\n};\n\nv86.prototype.do_tick = function()\n{\n    if(this.stopping || !this.running)\n    {\n        this.stopping = this.running = false;\n        this.bus.send(\"emulator-stopped\");\n        return;\n    }\n\n    this.idle = false;\n    const t = this.cpu.main_loop();\n\n    this.next_tick(t);\n};\n\nv86.prototype.next_tick = function(t)\n{\n    const tick = ++this.tick_counter;\n    this.idle = true;\n    this.yield(t, tick);\n};\n\nv86.prototype.yield_callback = function(tick)\n{\n    if(tick === this.tick_counter)\n    {\n        this.do_tick();\n    }\n};\n\nv86.prototype.stop = function()\n{\n    if(this.running)\n    {\n        this.stopping = true;\n    }\n};\n\nv86.prototype.destroy = function()\n{\n    this.unregister_yield();\n};\n\nv86.prototype.restart = function()\n{\n    this.cpu.reset_cpu();\n    this.cpu.load_bios();\n};\n\nv86.prototype.init = function(settings)\n{\n    this.cpu.init(settings, this.bus);\n    this.bus.send(\"emulator-ready\");\n};\n\nif(typeof process !== \"undefined\")\n{\n    v86.prototype.yield = function(t, tick)\n    {\n        /* global global */\n        if(t < 1)\n        {\n            global.setImmediate(tick => this.yield_callback(tick), tick);\n        }\n        else\n        {\n            setTimeout(tick => this.yield_callback(tick), t, tick);\n        }\n    };\n\n    v86.prototype.register_yield = function() {};\n    v86.prototype.unregister_yield = function() {};\n}\nelse if(globalThis[\"scheduler\"] && typeof globalThis[\"scheduler\"][\"postTask\"] === \"function\" && location.href.includes(\"use-scheduling-api\"))\n{\n    v86.prototype.yield = function(t, tick)\n    {\n        t = Math.max(0, t);\n        globalThis[\"scheduler\"][\"postTask\"](() => this.yield_callback(tick), { delay: t });\n    };\n\n    v86.prototype.register_yield = function() {};\n    v86.prototype.unregister_yield = function() {};\n}\nelse if(typeof Worker !== \"undefined\")\n{\n    // XXX: This has a slightly lower throughput compared to window.postMessage\n\n    function the_worker()\n    {\n        let timeout;\n        globalThis.onmessage = function(e)\n        {\n            const t = e.data.t;\n            timeout = timeout && clearTimeout(timeout);\n            if(t < 1) postMessage(e.data.tick);\n            else timeout = setTimeout(() => postMessage(e.data.tick), t);\n        };\n    }\n\n    v86.prototype.register_yield = function()\n    {\n        const url = URL.createObjectURL(new Blob([\"(\" + the_worker.toString() + \")()\"], { type: \"text/javascript\" }));\n        this.worker = new Worker(url);\n        this.worker.onmessage = e => this.yield_callback(e.data);\n        URL.revokeObjectURL(url);\n    };\n\n    v86.prototype.yield = function(t, tick)\n    {\n        this.worker.postMessage({ t, tick });\n    };\n\n    v86.prototype.unregister_yield = function()\n    {\n        this.worker && this.worker.terminate();\n        this.worker = null;\n    };\n}\n//else if(typeof window !== \"undefined\" && typeof postMessage !== \"undefined\")\n//{\n//    // setImmediate shim for the browser.\n//    // TODO: Make this deactivatable, for other applications\n//    //       using postMessage\n//\n//    const MAGIC_POST_MESSAGE = 0xAA55;\n//\n//    v86.prototype.yield = function(t)\n//    {\n//        // XXX: Use t\n//        window.postMessage(MAGIC_POST_MESSAGE, \"*\");\n//    };\n//\n//    let tick;\n//\n//    v86.prototype.register_yield = function()\n//    {\n//        tick = e =>\n//        {\n//            if(e.source === window && e.data === MAGIC_POST_MESSAGE)\n//            {\n//                this.do_tick();\n//            }\n//        };\n//\n//        window.addEventListener(\"message\", tick, false);\n//    };\n//\n//    v86.prototype.unregister_yield = function()\n//    {\n//        window.removeEventListener(\"message\", tick);\n//        tick = null;\n//    };\n//}\nelse\n{\n    v86.prototype.yield = function(t)\n    {\n        setTimeout(() => { this.do_tick(); }, t);\n    };\n\n    v86.prototype.register_yield = function() {};\n    v86.prototype.unregister_yield = function() {};\n}\n\nv86.prototype.save_state = function()\n{\n    // TODO: Should be implemented here, not on cpu\n    return save_state(this.cpu);\n};\n\nv86.prototype.restore_state = function(state)\n{\n    // TODO: Should be implemented here, not on cpu\n    return restore_state(this.cpu, state);\n};\n\n/* global require */\nif(typeof performance === \"object\" && performance.now)\n{\n    v86.microtick = performance.now.bind(performance);\n}\nelse if(typeof require === \"function\")\n{\n    const { performance } = require(\"perf_hooks\");\n    v86.microtick = performance.now.bind(performance);\n}\nelse if(typeof process === \"object\" && process.hrtime)\n{\n    v86.microtick = function()\n    {\n        var t = process.hrtime();\n        return t[0] * 1000 + t[1] / 1e6;\n    };\n}\nelse\n{\n    v86.microtick = Date.now;\n}\n"
  },
  {
    "path": "src/ne2k.js",
    "content": "import { LOG_NET } from \"./const.js\";\nimport { h, hex_dump } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { PCI } from \"./pci.js\";\nimport { BusConnector } from \"./bus.js\";\n\n// http://www.ethernut.de/pdf/8019asds.pdf\n\nconst NE2K_LOG_VERBOSE = false;\nconst NE2K_LOG_PACKETS = false;\n\nconst E8390_CMD = 0x00; /* The command register (for all pages) */\n\n/* Page 0 register offsets. */\nconst EN0_CLDALO = 0x01; /* Low byte of current local dma addr RD */\nconst EN0_STARTPG = 0x01; /* Starting page of ring bfr WR */\nconst EN0_CLDAHI = 0x02; /* High byte of current local dma addr RD */\nconst EN0_STOPPG = 0x02; /* Ending page +1 of ring bfr WR */\nconst EN0_BOUNDARY = 0x03; /* Boundary page of ring bfr RD WR */\nconst EN0_TSR = 0x04; /* Transmit status reg RD */\nconst EN0_TPSR = 0x04; /* Transmit starting page WR */\nconst EN0_NCR = 0x05; /* Number of collision reg RD */\nconst EN0_TCNTLO = 0x05; /* Low byte of tx byte count WR */\nconst EN0_FIFO = 0x06; /* FIFO RD */\nconst EN0_TCNTHI = 0x06; /* High byte of tx byte count WR */\nconst EN0_ISR = 0x07; /* Interrupt status reg RD WR */\nconst EN0_CRDALO = 0x08; /* low byte of current remote dma address RD */\nconst EN0_RSARLO = 0x08; /* Remote start address reg 0 */\nconst EN0_CRDAHI = 0x09; /* high byte, current remote dma address RD */\nconst EN0_RSARHI = 0x09; /* Remote start address reg 1 */\nconst EN0_RCNTLO = 0x0a; /* Remote byte count reg WR */\nconst EN0_RCNTHI = 0x0b; /* Remote byte count reg WR */\nconst EN0_RSR = 0x0c; /* rx status reg RD */\nconst EN0_RXCR = 0x0c; /* RX configuration reg WR */\nconst EN0_TXCR = 0x0d; /* TX configuration reg WR */\nconst EN0_COUNTER0 = 0x0d; /* Rcv alignment error counter RD */\nconst EN0_DCFG = 0x0e; /* Data configuration reg WR */\nconst EN0_COUNTER1 = 0x0e; /* Rcv CRC error counter RD */\nconst EN0_IMR = 0x0f; /* Interrupt mask reg WR */\nconst EN0_COUNTER2 = 0x0f; /* Rcv missed frame error counter RD */\n\nconst NE_DATAPORT = 0x10; /* NatSemi-defined port window offset. */\nconst NE_RESET = 0x1f; /* Issue a read to reset, a write to clear. */\n\n/* Bits in EN0_ISR - Interrupt status register */\nconst ENISR_RX = 0x01; /* Receiver, no error */\nconst ENISR_TX = 0x02; /* Transmitter, no error */\nconst ENISR_RX_ERR = 0x04; /* Receiver, with error */\nconst ENISR_TX_ERR = 0x08; /* Transmitter, with error */\nconst ENISR_OVER = 0x10; /* Receiver overwrote the ring */\nconst ENISR_COUNTERS = 0x20; /* Counters need emptying */\nconst ENISR_RDC = 0x40; /* remote dma complete */\nconst ENISR_RESET = 0x80; /* Reset completed */\nconst ENISR_ALL = 0x3f; /* Interrupts we will enable */\n\nconst ENRSR_RXOK = 0x01; /* Received a good packet */\n\nconst START_PAGE = 0x40;\nconst START_RX_PAGE = 0x40 + 12;\nconst STOP_PAGE = 0x80;\n\n\n// Search and replace MAC addresses in ethernet, arp and dhcp packets.\n// Used after restoring an OS from memory dump, so that multiple instances of\n// that OS can run at the same time with different external MAC addresses.\n// Crude but seems to work.\nfunction translate_mac_address(packet, search_mac, replacement_mac)\n{\n    if(packet[0] === search_mac[0] &&\n       packet[1] === search_mac[1] &&\n       packet[2] === search_mac[2] &&\n       packet[3] === search_mac[3] &&\n       packet[4] === search_mac[4] &&\n       packet[5] === search_mac[5])\n    {\n        dbg_log(\"Replace mac in eth destination field\", LOG_NET);\n\n        packet[0] = replacement_mac[0];\n        packet[1] = replacement_mac[1];\n        packet[2] = replacement_mac[2];\n        packet[3] = replacement_mac[3];\n        packet[4] = replacement_mac[4];\n        packet[5] = replacement_mac[5];\n    }\n\n    if(packet[6 + 0] === search_mac[0] &&\n       packet[6 + 1] === search_mac[1] &&\n       packet[6 + 2] === search_mac[2] &&\n       packet[6 + 3] === search_mac[3] &&\n       packet[6 + 4] === search_mac[4] &&\n       packet[6 + 5] === search_mac[5])\n    {\n        dbg_log(\"Replace mac in eth source field\", LOG_NET);\n\n        packet[6 + 0] = replacement_mac[0];\n        packet[6 + 1] = replacement_mac[1];\n        packet[6 + 2] = replacement_mac[2];\n        packet[6 + 3] = replacement_mac[3];\n        packet[6 + 4] = replacement_mac[4];\n        packet[6 + 5] = replacement_mac[5];\n    }\n\n    const ethertype = packet[12] << 8 | packet[13];\n\n    if(ethertype === 0x0800)\n    {\n        // ipv4\n        const ipv4_packet = packet.subarray(14);\n        const ipv4_version = ipv4_packet[0] >> 4;\n\n        if(ipv4_version !== 4)\n        {\n            dbg_log(\"Expected ipv4.version==4 but got: \" + ipv4_version, LOG_NET);\n            return;\n        }\n\n        const ipv4_ihl = ipv4_packet[0] & 0xF;\n        dbg_assert(ipv4_ihl === 5, \"TODO: ihl!=5\");\n\n        const ipv4_proto = ipv4_packet[9];\n        if(ipv4_proto === 0x11)\n        {\n            // udp\n            const udp_packet = ipv4_packet.subarray(5 * 4);\n            const source_port = udp_packet[0] << 8 | udp_packet[1];\n            const destination_port = udp_packet[2] << 8 | udp_packet[3];\n            const checksum = udp_packet[6] << 8 | udp_packet[7];\n\n            dbg_log(\"udp srcport=\" + source_port + \" dstport=\" + destination_port + \" checksum=\" + h(checksum, 4), LOG_NET);\n\n            if(source_port === 67 || destination_port === 67)\n            {\n                // dhcp\n                const dhcp_packet = udp_packet.subarray(8);\n                const dhcp_magic = dhcp_packet[0xEC] << 24 | dhcp_packet[0xED] << 16 | dhcp_packet[0xEE] << 8 | dhcp_packet[0xEF];\n\n                if(dhcp_magic !== 0x63825363)\n                {\n                    dbg_log(\"dhcp packet didn't match magic: \" + h(dhcp_magic, 8));\n                    return;\n                }\n\n                if(dhcp_packet[28 + 0] === search_mac[0] &&\n                   dhcp_packet[28 + 1] === search_mac[1] &&\n                   dhcp_packet[28 + 2] === search_mac[2] &&\n                   dhcp_packet[28 + 3] === search_mac[3] &&\n                   dhcp_packet[28 + 4] === search_mac[4] &&\n                   dhcp_packet[28 + 5] === search_mac[5])\n                {\n                    dbg_log(\"Replace mac in dhcp.chaddr\", LOG_NET);\n\n                    dhcp_packet[28 + 0] = replacement_mac[0];\n                    dhcp_packet[28 + 1] = replacement_mac[1];\n                    dhcp_packet[28 + 2] = replacement_mac[2];\n                    dhcp_packet[28 + 3] = replacement_mac[3];\n                    dhcp_packet[28 + 4] = replacement_mac[4];\n                    dhcp_packet[28 + 5] = replacement_mac[5];\n\n                    udp_packet[6] = udp_packet[7] = 0; // zero udp checksum\n                }\n\n                let offset = 0xF0;\n                while(offset < dhcp_packet.length)\n                {\n                    const dhcp_option_type = dhcp_packet[offset++];\n\n                    if(dhcp_option_type === 0xFF)\n                    {\n                        break;\n                    }\n\n                    const length = dhcp_packet[offset++];\n\n                    if(dhcp_option_type === 0x3D && // client identifier\n                       dhcp_packet[offset + 0] === 0x01 && // ethernet\n                       dhcp_packet[offset + 1] === search_mac[0] &&\n                       dhcp_packet[offset + 2] === search_mac[1] &&\n                       dhcp_packet[offset + 3] === search_mac[2] &&\n                       dhcp_packet[offset + 4] === search_mac[3] &&\n                       dhcp_packet[offset + 5] === search_mac[4] &&\n                       dhcp_packet[offset + 6] === search_mac[5])\n                    {\n                        dbg_log(\"Replace mac in dhcp.clientidentifier\", LOG_NET);\n\n                        dhcp_packet[offset + 1] = replacement_mac[0];\n                        dhcp_packet[offset + 2] = replacement_mac[1];\n                        dhcp_packet[offset + 3] = replacement_mac[2];\n                        dhcp_packet[offset + 4] = replacement_mac[3];\n                        dhcp_packet[offset + 5] = replacement_mac[4];\n                        dhcp_packet[offset + 6] = replacement_mac[5];\n\n                        udp_packet[6] = udp_packet[7] = 0; // zero udp checksum\n                    }\n\n                    offset += length;\n                }\n            }\n        }\n        else\n        {\n            // tcp, ...\n        }\n    }\n    else if(ethertype === 0x0806)\n    {\n        // arp\n        const arp_packet = packet.subarray(14);\n        dbg_log(\"arp oper=\" + arp_packet[7] + \" \" + format_mac(arp_packet.subarray(8, 8+6)) + \" \" + format_mac(arp_packet.subarray(18, 18+6)), LOG_NET);\n\n        if(arp_packet[8 + 0] === search_mac[0] &&\n           arp_packet[8 + 1] === search_mac[1] &&\n           arp_packet[8 + 2] === search_mac[2] &&\n           arp_packet[8 + 3] === search_mac[3] &&\n           arp_packet[8 + 4] === search_mac[4] &&\n           arp_packet[8 + 5] === search_mac[5])\n        {\n            dbg_log(\"Replace mac in arp.sha\", LOG_NET);\n\n            arp_packet[8 + 0] = replacement_mac[0];\n            arp_packet[8 + 1] = replacement_mac[1];\n            arp_packet[8 + 2] = replacement_mac[2];\n            arp_packet[8 + 3] = replacement_mac[3];\n            arp_packet[8 + 4] = replacement_mac[4];\n            arp_packet[8 + 5] = replacement_mac[5];\n        }\n    }\n    else\n    {\n        // TODO: ipv6, ...\n    }\n}\n\nexport function format_mac(mac)\n{\n    return [\n        mac[0].toString(16).padStart(2, \"0\"),\n        mac[1].toString(16).padStart(2, \"0\"),\n        mac[2].toString(16).padStart(2, \"0\"),\n        mac[3].toString(16).padStart(2, \"0\"),\n        mac[4].toString(16).padStart(2, \"0\"),\n        mac[5].toString(16).padStart(2, \"0\"),\n    ].join(\":\");\n}\n\nfunction dump_packet(packet, prefix)\n{\n    const ethertype = packet[12] << 8 | packet[13] << 0;\n    if(ethertype === 0x0800)\n    {\n        const ipv4_packet = packet.subarray(14);\n        const ipv4_len = ipv4_packet[2] << 8 | ipv4_packet[3];\n        const ipv4_proto = ipv4_packet[9];\n        if(ipv4_proto === 0x11)\n        {\n            const udp_packet = ipv4_packet.subarray(5 * 4);\n            const source_port = udp_packet[0] << 8 | udp_packet[1];\n            const destination_port = udp_packet[2] << 8 | udp_packet[3];\n            const checksum = udp_packet[6] << 8 | udp_packet[7];\n\n            if(source_port === 67 || destination_port === 67)\n            {\n                const dhcp_packet = udp_packet.subarray(8);\n                const dhcp_chaddr = dhcp_packet.subarray(28, 28+6);\n                dbg_log(prefix + \" len=\" + packet.length + \" ethertype=\" + h(ethertype) + \" ipv4.len=\" + ipv4_len + \" ipv4.proto=\" + h(packet[14 + 9]) + \" udp.srcport=\" + source_port + \" udp.dstport=\" + destination_port + \" udp.chksum=\" + h(checksum, 4) + \" dhcp.chaddr=\" + format_mac(dhcp_chaddr));\n            }\n            else\n            {\n                dbg_log(prefix + \" len=\" + packet.length + \" ethertype=\" + h(ethertype) + \" ipv4.len=\" + ipv4_len + \" ipv4.proto=\" + h(packet[14 + 9]) + \" udp.srcport=\" + source_port + \" udp.dstport=\" + destination_port + \" udp.chksum=\" + h(checksum, 4));\n            }\n        }\n        else if(ipv4_proto === 0x01)\n        {\n        }\n        else\n        {\n            dbg_log(prefix + \" len=\" + packet.length + \" ethertype=\" + h(ethertype) + \" ipv4.len=\" + ipv4_len + \" ipv4.proto=\" + h(packet[14 + 9]));\n        }\n    }\n    else\n    {\n        const arp_packet = packet.subarray(14);\n        dbg_log(prefix + \" len=\" + packet.length + \" ethertype=\" + h(ethertype) + \" arp\");\n    }\n    dbg_log(hex_dump(packet));\n}\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n * @param {Boolean} preserve_mac_from_state_image\n * @param {Boolean} mac_address_translation\n * @param {number} [id=0] id\n */\nexport function Ne2k(cpu, bus, preserve_mac_from_state_image, mac_address_translation, id)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    /** @const @type {PCI} */\n    this.pci = cpu.devices.pci;\n\n    this.id = id || 0;\n    this.preserve_mac_from_state_image = preserve_mac_from_state_image;\n    this.mac_address_translation = mac_address_translation;\n\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n    this.bus.register(\"net\" + this.id + \"-receive\", function(data)\n    {\n        this.receive(data);\n    }, this);\n\n    this.port = 0x300 + 0x100 * this.id;\n\n    this.name = \"ne2k\";\n\n    const use_pci = true;\n\n    if(use_pci)\n    {\n        this.pci_space = [\n            0xec, 0x10, 0x29, 0x80, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,\n            this.port & 0xFF | 1, this.port >> 8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x1a, 0x00, 0x11,\n            0x00, 0x00, 0xb8, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,\n        ];\n        this.pci_id = (this.id === 0 ? 0x05 : (0x07 + this.id)) << 3;\n        this.pci_bars = [\n            {\n                size: 32,\n            },\n        ];\n    }\n\n    this.isr = 0;\n    this.imr = 0; // interrupt mask register\n\n    this.cr = 1;\n\n    this.dcfg = 0;\n\n    this.rcnt = 0;\n\n    this.tcnt = 0;\n    this.tpsr = 0;\n    this.memory = new Uint8Array(256 * 0x80);\n\n    this.rxcr = 0;\n    this.txcr = 0;\n    this.tsr = 1;\n\n    // mac address\n    this.mac = new Uint8Array([\n        0x00, 0x22, 0x15,\n        Math.random() * 255 | 0,\n        Math.random() * 255 | 0,\n        Math.random() * 255 | 0,\n    ]);\n\n    this.bus.send(\"net\" + this.id + \"-mac\", format_mac(this.mac));\n\n    // multicast addresses\n    this.mar = Uint8Array.of(0xFF, 0xFF, 0xFF, 0xFF,  0xFF, 0xFF, 0xFF, 0xFF);\n\n    // Used for mac address translation\n    // The mac the OS thinks it has\n    this.mac_address_in_state = null;\n\n    for(var i = 0; i < 6; i++)\n    {\n        this.memory[i << 1] = this.memory[i << 1 | 1] = this.mac[i];\n    }\n\n    // the PROM signature of 0x57, 0x57 is also doubled\n    // resulting in setting the 4 bytes at the end, 28, 29, 30 and 31 to 0x57\n    this.memory[14 << 1] = this.memory[14 << 1 | 1] = 0x57;\n    this.memory[15 << 1] = this.memory[15 << 1 | 1] = 0x57;\n\n    dbg_log(\"Mac: \" + format_mac(this.mac), LOG_NET);\n\n    this.rsar = 0;\n\n    this.pstart = START_PAGE;\n    this.pstop = STOP_PAGE;\n\n    this.curpg = START_RX_PAGE;\n    this.boundary = START_RX_PAGE;\n\n    var io = cpu.io;\n\n    io.register_read(this.port | E8390_CMD, this, function()\n    {\n        dbg_log(\"Read cmd\", LOG_NET);\n        return this.cr;\n    }, function()\n    {\n        dbg_log(\"Read16 cmd\", LOG_NET);\n        return this.cr;\n    }\n    );\n\n    io.register_write(this.port | E8390_CMD, this, function(data_byte)\n    {\n        this.cr = data_byte;\n        dbg_log(\"Write command: \" + h(data_byte, 2) + \" newpg=\" + (this.cr >> 6) + \" txcr=\" + h(this.txcr, 2), LOG_NET);\n\n        if(this.cr & 1)\n        {\n            return;\n        }\n\n        if((data_byte & 0x18) && this.rcnt === 0)\n        {\n            this.do_interrupt(ENISR_RDC);\n        }\n\n        if(data_byte & 4)\n        {\n            var start = this.tpsr << 8;\n            var data = this.memory.subarray(start, start + this.tcnt);\n\n            if(NE2K_LOG_PACKETS)\n            {\n                dump_packet(data, \"send\");\n            }\n\n            if(this.mac_address_in_state)\n            {\n                data = new Uint8Array(data); // make a copy\n                translate_mac_address(data, this.mac_address_in_state, this.mac);\n            }\n\n            this.bus.send(\"net\" + this.id + \"-send\", data);\n            this.bus.send(\"eth-transmit-end\", [data.length]);\n            this.cr &= ~4;\n            this.do_interrupt(ENISR_TX);\n\n            dbg_log(\"Command: Transfer. length=\" + h(data.byteLength), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | EN0_COUNTER0, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 1)\n        {\n            dbg_log(\"Read mar5\", LOG_NET);\n            return this.mar[5];\n        }\n        else\n        {\n            dbg_log(\"Read counter0 pg=\" + pg, LOG_NET);\n            return 0;\n        }\n    });\n\n    io.register_read(this.port | EN0_COUNTER1, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 1)\n        {\n            dbg_log(\"Read mar6\", LOG_NET);\n            return this.mar[6];\n        }\n        else\n        {\n            dbg_log(\"Read8 counter1 pg=\" + pg, LOG_NET);\n            return 0;\n        }\n    }, function()\n    {\n        dbg_log(\"Read16 counter1 pg=\" + this.get_page(), LOG_NET);\n        // openbsd\n        return 0;\n    }\n    );\n\n    io.register_read(this.port | EN0_COUNTER2, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 1)\n        {\n            dbg_log(\"Read mar7\", LOG_NET);\n            return this.mar[7];\n        }\n        else\n        {\n            dbg_log(\"Read counter2 pg=\" + pg, LOG_NET);\n            return 0;\n        }\n    });\n\n    io.register_read(this.port | NE_RESET, this, function()\n    {\n        var pg = this.get_page();\n        dbg_log(\"Read reset\", LOG_NET);\n        this.do_interrupt(ENISR_RESET);\n        return 0;\n    });\n\n    io.register_write(this.port | NE_RESET, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        dbg_log(\"Write reset: \" + h(data_byte, 2), LOG_NET);\n        //this.isr &= ~ENISR_RESET;\n    });\n\n    io.register_read(this.port | EN0_STARTPG, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            return this.pstart;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read pg1/01 (mac[0])\", LOG_NET);\n            return this.mac[0];\n        }\n        else if(pg === 2)\n        {\n            return this.pstart;\n        }\n        else\n        {\n            dbg_log(\"Read pg\" + pg + \"/01\");\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_STARTPG, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"start page: \" + h(data_byte, 2), LOG_NET);\n            this.pstart = data_byte;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"mac[0] = \" + h(data_byte), LOG_NET);\n            this.mac[0] = data_byte;\n        }\n        else if(pg === 3)\n        {\n            dbg_log(\"Unimplemented: Write pg3/01 (9346CR): \" + h(data_byte), LOG_NET);\n        }\n        else\n        {\n            dbg_log(\"Write pg\" + pg + \"/01: \" + h(data_byte), LOG_NET);\n            dbg_assert(false);\n        }\n    });\n\n\n    io.register_read(this.port | EN0_STOPPG, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            return this.pstop;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read pg1/02 (mac[1])\", LOG_NET);\n            return this.mac[1];\n        }\n        else if(pg === 2)\n        {\n            return this.pstop;\n        }\n        else\n        {\n            dbg_log(\"Read pg\" + pg + \"/02\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_STOPPG, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"stop page: \" + h(data_byte, 2), LOG_NET);\n            if(data_byte > (this.memory.length >> 8))\n            {\n                data_byte = this.memory.length >> 8;\n                dbg_log(\"XXX: Adjusting stop page to \" + h(data_byte), LOG_NET);\n            }\n            this.pstop = data_byte;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"mac[1] = \" + h(data_byte), LOG_NET);\n            this.mac[1] = data_byte;\n        }\n        else\n        {\n            dbg_log(\"Write pg\" + pg + \"/02: \" + h(data_byte), LOG_NET);\n            dbg_assert(false);\n        }\n    });\n\n    io.register_read(this.port | EN0_ISR, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Read isr: \" + h(this.isr, 2), LOG_NET);\n            return this.isr;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read curpg: \" + h(this.curpg, 2), LOG_NET);\n            return this.curpg;\n        }\n        else\n        {\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_ISR, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            // acknowledge interrupts where bit is set\n            dbg_log(\"Write isr: \" + h(data_byte, 2), LOG_NET);\n            this.isr &= ~data_byte;\n            this.update_irq();\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Write curpg: \" + h(data_byte, 2), LOG_NET);\n            this.curpg = data_byte;\n        }\n        else\n        {\n            dbg_assert(false);\n        }\n    });\n\n    io.register_write(this.port | EN0_TXCR, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            this.txcr = data_byte;\n            dbg_log(\"Write tx config: \" + h(data_byte, 2), LOG_NET);\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/0d \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_write(this.port | EN0_DCFG, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write data configuration: \" + h(data_byte, 2), LOG_NET);\n            this.dcfg = data_byte;\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/0e \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | EN0_RCNTLO, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Read pg0/0a\", LOG_NET);\n            return 0x50;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read mar2\", LOG_NET);\n            return this.mar[2];\n        }\n        else\n        {\n            dbg_assert(false, \"TODO\");\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_RCNTLO, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write remote byte count low: \" + h(data_byte, 2), LOG_NET);\n            this.rcnt = this.rcnt & 0xFF00 | data_byte & 0xFF;\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/0a \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | EN0_RCNTHI, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Read pg0/0b\", LOG_NET);\n            return 0x43;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read mar3\", LOG_NET);\n            return this.mar[3];\n        }\n        else\n        {\n            dbg_assert(false, \"TODO\");\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_RCNTHI, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write remote byte count high: \" + h(data_byte, 2), LOG_NET);\n            this.rcnt = this.rcnt & 0xFF | data_byte << 8 & 0xFF00;\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/0b \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | EN0_RSARLO, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Read remote start address low\", LOG_NET);\n            return this.rsar & 0xFF;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read mar0\", LOG_NET);\n            return this.mar[0];\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Read pg\" + pg + \"/08\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_RSARLO, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write remote start address low: \" + h(data_byte, 2), LOG_NET);\n            this.rsar = this.rsar & 0xFF00 | data_byte & 0xFF;\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/08 \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | EN0_RSARHI, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Read remote start address high\", LOG_NET);\n            return this.rsar >> 8 & 0xFF;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read mar1\", LOG_NET);\n            return this.mar[1];\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Read pg\" + pg + \"/09\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_RSARHI, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write remote start address low: \" + h(data_byte, 2), LOG_NET);\n            this.rsar = this.rsar & 0xFF | data_byte << 8 & 0xFF00;\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/09 \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_write(this.port | EN0_IMR, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write interrupt mask register: \" + h(data_byte, 2) + \" isr=\" + h(this.isr, 2), LOG_NET);\n            this.imr = data_byte;\n            this.update_irq();\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/0f \" + h(data_byte, 2), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | EN0_BOUNDARY, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Read boundary: \" + h(this.boundary, 2), LOG_NET);\n            return this.boundary;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read pg1/03 (mac[2])\", LOG_NET);\n            return this.mac[2];\n        }\n        else if(pg === 3)\n        {\n            dbg_log(\"Unimplemented: Read pg3/03 (CONFIG0)\", LOG_NET);\n            return 0;\n        }\n        else\n        {\n            dbg_log(\"Read pg\" + pg + \"/03\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_BOUNDARY, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write boundary: \" + h(data_byte, 2), LOG_NET);\n            this.boundary = data_byte;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"mac[2] = \" + h(data_byte), LOG_NET);\n            this.mac[2] = data_byte;\n        }\n        else\n        {\n            dbg_log(\"Write pg\" + pg + \"/03: \" + h(data_byte), LOG_NET);\n            dbg_assert(false);\n        }\n    });\n\n    io.register_read(this.port | EN0_TSR, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            return this.tsr;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read pg1/04 (mac[3])\", LOG_NET);\n            return this.mac[3];\n        }\n        else\n        {\n            dbg_log(\"Read pg\" + pg + \"/04\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_TPSR, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write tpsr: \" + h(data_byte, 2), LOG_NET);\n            this.tpsr = data_byte;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"mac[3] = \" + h(data_byte), LOG_NET);\n            this.mac[3] = data_byte;\n        }\n        else\n        {\n            dbg_log(\"Write pg\" + pg + \"/04: \" + h(data_byte), LOG_NET);\n            dbg_assert(false);\n        }\n    });\n\n    io.register_read(this.port | EN0_TCNTLO, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)\", LOG_NET);\n            return 0;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read pg1/05 (mac[4])\", LOG_NET);\n            return this.mac[4];\n        }\n        else if(pg === 3)\n        {\n            dbg_log(\"Unimplemented: Read pg3/05 (CONFIG2)\", LOG_NET);\n            return 0;\n        }\n        else\n        {\n            dbg_log(\"Read pg\" + pg + \"/05\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_TCNTLO, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write tcnt low: \" + h(data_byte, 2), LOG_NET);\n            this.tcnt = this.tcnt & ~0xFF | data_byte;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"mac[4] = \" + h(data_byte), LOG_NET);\n            this.mac[4] = data_byte;\n        }\n        else if(pg === 3)\n        {\n            dbg_log(\"Unimplemented: Write pg3/05 (CONFIG2): \" + h(data_byte), LOG_NET);\n        }\n        else\n        {\n            dbg_log(\"Write pg\" + pg + \"/05: \" + h(data_byte), LOG_NET);\n            dbg_assert(false);\n        }\n    });\n\n    io.register_read(this.port | EN0_TCNTHI, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_assert(false, \"TODO\");\n            return 0;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read pg1/06 (mac[5])\", LOG_NET);\n            return this.mac[5];\n        }\n        else if(pg === 3)\n        {\n            dbg_log(\"Unimplemented: Read pg3/06 (CONFIG3)\", LOG_NET);\n            return 0;\n        }\n        else\n        {\n            dbg_log(\"Read pg\" + pg + \"/06\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_TCNTHI, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"Write tcnt high: \" + h(data_byte, 2), LOG_NET);\n            this.tcnt = this.tcnt & 0xFF | data_byte << 8;\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"mac[5] = \" + h(data_byte), LOG_NET);\n            this.mac[5] = data_byte;\n        }\n        else if(pg === 3)\n        {\n            dbg_log(\"Unimplemented: Write pg3/06 (CONFIG3): \" + h(data_byte), LOG_NET);\n        }\n        else\n        {\n            dbg_log(\"Write pg\" + pg + \"/06: \" + h(data_byte), LOG_NET);\n            dbg_assert(false);\n        }\n    });\n\n    io.register_read(this.port | EN0_RSR, this, function()\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            return 1 | 1 << 3; // receive status ok\n        }\n        else if(pg === 1)\n        {\n            dbg_log(\"Read mar4\", LOG_NET);\n            return this.mar[4];\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Read pg\" + pg + \"/0c\", LOG_NET);\n            dbg_assert(false);\n            return 0;\n        }\n    });\n\n    io.register_write(this.port | EN0_RXCR, this, function(data_byte)\n    {\n        var pg = this.get_page();\n        if(pg === 0)\n        {\n            dbg_log(\"RX configuration reg write: \" + h(data_byte, 2), LOG_NET);\n            this.rxcr = data_byte;\n        }\n        else\n        {\n            dbg_log(\"Unimplemented: Write pg\" + pg + \"/0c: \" + h(data_byte), LOG_NET);\n        }\n    });\n\n    io.register_read(this.port | NE_DATAPORT | 0, this,\n            this.data_port_read8,\n            this.data_port_read16,\n            this.data_port_read32);\n    io.register_write(this.port | NE_DATAPORT | 0, this,\n            this.data_port_write16,\n            this.data_port_write16,\n            this.data_port_write32);\n\n    if(use_pci)\n    {\n        cpu.devices.pci.register_device(this);\n    }\n}\n\nNe2k.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.isr;\n    state[1] = this.imr;\n    state[2] = this.cr;\n    state[3] = this.dcfg;\n    state[4] = this.rcnt;\n    state[5] = this.tcnt;\n    state[6] = this.tpsr;\n    state[7] = this.rsar;\n    state[8] = this.pstart;\n    state[9] = this.curpg;\n    state[10] = this.boundary;\n    state[11] = this.pstop;\n    state[12] = this.rxcr;\n    state[13] = this.txcr;\n    state[14] = this.tsr;\n    state[15] = this.mac;\n    state[16] = this.memory;\n\n    return state;\n};\n\nNe2k.prototype.set_state = function(state)\n{\n    this.isr = state[0];\n    this.imr = state[1];\n    this.cr = state[2];\n    this.dcfg = state[3];\n    this.rcnt = state[4];\n    this.tcnt = state[5];\n    this.tpsr = state[6];\n    this.rsar = state[7];\n    this.pstart = state[8];\n    this.curpg = state[9];\n    this.boundary = state[10];\n    this.pstop = state[11];\n    this.rxcr = state[12];\n    this.txcr = state[13];\n    this.tsr = state[14];\n\n    if(this.preserve_mac_from_state_image)\n    {\n        this.mac = state[15];\n        this.memory = state[16];\n    }\n    else if(this.mac_address_translation)\n    {\n        this.mac_address_in_state = state[15];\n        this.memory = state[16];\n\n        dbg_log(\"Using mac address translation\" +\n            \" guest_os_mac=\" + format_mac(this.mac_address_in_state) +\n            \" real_mac=\" + format_mac(this.mac), LOG_NET);\n    }\n    this.bus.send(\"net\" + this.id + \"-mac\", format_mac(this.mac));\n};\n\nNe2k.prototype.do_interrupt = function(ir_mask)\n{\n    dbg_log(\"Do interrupt \" + h(ir_mask, 2), LOG_NET);\n    this.isr |= ir_mask;\n    this.update_irq();\n};\n\nNe2k.prototype.update_irq = function()\n{\n    if(this.imr & this.isr)\n    {\n        this.pci.raise_irq(this.pci_id);\n    }\n    else\n    {\n        this.pci.lower_irq(this.pci_id);\n    }\n};\n\nNe2k.prototype.data_port_write = function(data_byte)\n{\n    if(NE2K_LOG_VERBOSE)\n    {\n        dbg_log(\"Write data port: data=\" + h(data_byte & 0xFF, 2) +\n                                \" rsar=\" + h(this.rsar, 4) +\n                                \" rcnt=\" + h(this.rcnt, 4), LOG_NET);\n    }\n\n    if(this.rsar <= 0x10 || this.rsar >= (START_PAGE << 8) && this.rsar < (STOP_PAGE << 8))\n    {\n        this.memory[this.rsar] = data_byte;\n    }\n\n    this.rsar++;\n    this.rcnt--;\n\n    if(this.rsar >= (this.pstop << 8))\n    {\n        this.rsar += (this.pstart - this.pstop) << 8;\n    }\n\n    if(this.rcnt === 0)\n    {\n        this.do_interrupt(ENISR_RDC);\n    }\n};\n\nNe2k.prototype.data_port_write16 = function(data)\n{\n    this.data_port_write(data);\n\n    if(this.dcfg & 1)\n    {\n        this.data_port_write(data >> 8);\n    }\n};\n\nNe2k.prototype.data_port_write32 = function(data)\n{\n    this.data_port_write(data);\n    this.data_port_write(data >> 8);\n    this.data_port_write(data >> 16);\n    this.data_port_write(data >> 24);\n};\n\nNe2k.prototype.data_port_read = function()\n{\n    let data = 0;\n\n    if(this.rsar < (STOP_PAGE << 8))\n    {\n        data = this.memory[this.rsar];\n    }\n\n    if(NE2K_LOG_VERBOSE)\n    {\n        dbg_log(\"Read data port: data=\" + h(data, 2) +\n                               \" rsar=\" + h(this.rsar, 4) +\n                               \" rcnt=\" + h(this.rcnt, 4), LOG_NET);\n    }\n\n    this.rsar++;\n    this.rcnt--;\n\n    if(this.rsar >= (this.pstop << 8))\n    {\n        this.rsar += (this.pstart - this.pstop) << 8;\n    }\n\n    if(this.rcnt === 0)\n    {\n        this.do_interrupt(ENISR_RDC);\n    }\n\n    return data;\n};\n\nNe2k.prototype.data_port_read8 = function()\n{\n    return this.data_port_read16() & 0xFF;\n};\n\nNe2k.prototype.data_port_read16 = function()\n{\n    if(this.dcfg & 1)\n    {\n        return this.data_port_read() | this.data_port_read() << 8;\n    }\n    else\n    {\n        return this.data_port_read();\n    }\n};\n\nNe2k.prototype.data_port_read32 = function()\n{\n    return this.data_port_read() | this.data_port_read() << 8 |\n            this.data_port_read() << 16 | this.data_port_read() << 24;\n};\n\nNe2k.prototype.receive = function(data)\n{\n    // called from the adapter when data is received over the network\n\n    if(this.cr & 1)\n    {\n        // stop bit set\n        return;\n    }\n\n    if(NE2K_LOG_PACKETS)\n    {\n        dump_packet(data, \"receive\");\n    }\n\n    this.bus.send(\"eth-receive-end\", [data.length]);\n\n    if(this.rxcr & 0x10)\n    {\n        // promiscuous\n    }\n    else if((this.rxcr & 4) &&\n            data[0] === 0xFF && data[1] === 0xFF && data[2] === 0xFF &&\n            data[3] === 0xFF && data[4] === 0xFF && data[5] === 0xFF)\n    {\n        // broadcast\n    }\n    else if((this.rxcr & 8) && (data[0] & 1) === 1)\n    {\n        // multicast\n        // XXX\n        return;\n    }\n    else if(data[0] === this.mac[0] && data[1] === this.mac[1] &&\n            data[2] === this.mac[2] && data[3] === this.mac[3] &&\n            data[4] === this.mac[4] && data[5] === this.mac[5])\n    {\n    }\n    else\n    {\n        return;\n    }\n\n    if(this.mac_address_in_state)\n    {\n        data = new Uint8Array(data); // make a copy\n        translate_mac_address(data, this.mac, this.mac_address_in_state);\n    }\n\n    var packet_length = Math.max(60, data.length);\n\n    var offset = this.curpg << 8;\n    var total_length = packet_length + 4;\n    var data_start = offset + 4;\n    var next = this.curpg + 1 + (total_length >> 8);\n\n    var end = offset + total_length;\n\n    const needed = 1 + (total_length >> 8);\n\n    // boundary == curpg interpreted as ringbuffer empty\n    const available = this.boundary > this.curpg ?\n        this.boundary - this.curpg :\n        this.pstop - this.curpg + this.boundary - this.pstart;\n\n    if(available < needed &&\n        this.boundary !== 0 // XXX: ReactOS sets this to 0 initially and never updates it unless it receives a packet\n    )\n    {\n        dbg_log(\"Buffer full, dropping packet pstart=\" + h(this.pstart) + \" pstop=\" + h(this.pstop) +\n            \" curpg=\" + h(this.curpg) + \" needed=\" + h(needed) + \" boundary=\" + h(this.boundary) + \" available=\" + h(available), LOG_NET);\n        return;\n    }\n\n    if(end > (this.pstop << 8))\n    {\n        // Shouldn't happen because at this size it can't cross a page,\n        // so we can skip filling with zeroes\n        dbg_assert(data.length >= 60);\n\n        var cut = (this.pstop << 8) - data_start;\n        dbg_assert(cut >= 0);\n\n        this.memory.set(data.subarray(0, cut), data_start);\n        this.memory.set(data.subarray(cut), this.pstart << 8);\n        dbg_log(\"rcv cut=\" + h(cut), LOG_NET);\n    }\n    else\n    {\n        this.memory.set(data, data_start);\n\n        if(data.length < 60)\n        {\n            this.memory.fill(0, data_start + data.length, data_start + 60);\n        }\n    }\n\n    if(next >= this.pstop)\n    {\n        next += this.pstart - this.pstop;\n    }\n\n    // write packet header\n    this.memory[offset] = ENRSR_RXOK; // status\n    this.memory[offset + 1] = next;\n    this.memory[offset + 2] = total_length;\n    this.memory[offset + 3] = total_length >> 8;\n\n    this.curpg = next;\n\n    dbg_log(\"rcv offset=\" + h(offset) + \" len=\" + h(total_length) + \" next=\" + h(next), LOG_NET);\n\n    this.do_interrupt(ENISR_RX);\n};\n\nNe2k.prototype.get_page = function()\n{\n    return this.cr >> 6 & 3;\n};\n"
  },
  {
    "path": "src/pci.js",
    "content": "import { LOG_PCI } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\n\n// http://wiki.osdev.org/PCI\n\nexport const PCI_CONFIG_ADDRESS = 0xCF8;\nexport const PCI_CONFIG_DATA = 0xCFC;\n\n/**\n * @constructor\n * @param {CPU} cpu\n */\nexport function PCI(cpu)\n{\n    this.pci_addr = new Uint8Array(4);\n    this.pci_value = new Uint8Array(4);\n    this.pci_response = new Uint8Array(4);\n    this.pci_status = new Uint8Array(4);\n\n    this.pci_addr32 = new Int32Array(this.pci_addr.buffer);\n    this.pci_value32 = new Int32Array(this.pci_value.buffer);\n    this.pci_response32 = new Int32Array(this.pci_response.buffer);\n    this.pci_status32 = new Int32Array(this.pci_status.buffer);\n\n    this.device_spaces = [];\n    this.devices = [];\n\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    for(var i = 0; i < 256; i++)\n    {\n        this.device_spaces[i] = undefined;\n        this.devices[i] = undefined;\n    }\n\n    this.io = cpu.io;\n\n    cpu.io.register_write(PCI_CONFIG_DATA, this,\n        function(value)\n        {\n            this.pci_write8(this.pci_addr32[0], value);\n        },\n        function(value)\n        {\n            this.pci_write16(this.pci_addr32[0], value);\n        },\n        function(value)\n        {\n            this.pci_write32(this.pci_addr32[0], value);\n        });\n\n    cpu.io.register_write(PCI_CONFIG_DATA + 1, this,\n        function(value)\n        {\n            this.pci_write8(this.pci_addr32[0] + 1 | 0, value);\n        });\n\n    cpu.io.register_write(PCI_CONFIG_DATA + 2, this,\n        function(value)\n        {\n            this.pci_write8(this.pci_addr32[0] + 2 | 0, value);\n        },\n        function(value)\n        {\n            this.pci_write16(this.pci_addr32[0] + 2 | 0, value);\n        });\n\n    cpu.io.register_write(PCI_CONFIG_DATA + 3, this,\n        function(value)\n        {\n            this.pci_write8(this.pci_addr32[0] + 3 | 0, value);\n        });\n\n    cpu.io.register_read_consecutive(PCI_CONFIG_DATA, this,\n        function()\n        {\n            return this.pci_response[0];\n        },\n        function()\n        {\n            return this.pci_response[1];\n        },\n        function()\n        {\n            return this.pci_response[2];\n        },\n        function()\n        {\n            return this.pci_response[3];\n        }\n    );\n\n    cpu.io.register_read_consecutive(PCI_CONFIG_ADDRESS, this,\n        function()\n        {\n            return this.pci_status[0];\n        },\n        function()\n        {\n            return this.pci_status[1];\n        },\n        function()\n        {\n            return this.pci_status[2];\n        },\n        function()\n        {\n            return this.pci_status[3];\n        }\n    );\n\n    cpu.io.register_write_consecutive(PCI_CONFIG_ADDRESS, this,\n        function(out_byte)\n        {\n            this.pci_addr[0] = out_byte & 0xFC;\n        },\n        function(out_byte)\n        {\n            if((this.pci_addr[1] & 0x06) === 0x02 && (out_byte & 0x06) === 0x06)\n            {\n                dbg_log(\"CPU reboot via PCI\");\n                cpu.reboot_internal();\n                return;\n            }\n\n            this.pci_addr[1] = out_byte;\n        },\n        function(out_byte)\n        {\n            this.pci_addr[2] = out_byte;\n        },\n        function(out_byte)\n        {\n            this.pci_addr[3] = out_byte;\n            this.pci_query();\n        }\n    );\n\n\n    // Some experimental PCI devices taken from my PC:\n\n    // 00:00.0 Host bridge: Intel Corporation 4 Series Chipset DRAM Controller (rev 02)\n    //var host_bridge = {\n    //    pci_id: 0,\n    //    pci_space: [\n    //        0x86, 0x80, 0x20, 0x2e, 0x06, 0x00, 0x90, 0x20, 0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,\n    //        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n    //        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x10, 0xd3, 0x82,\n    //        0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n    //    ],\n    //    pci_bars: [],\n    //};\n\n    // This needs to be set in order for seabios to not execute code outside of\n    // mapped memory. While we map the BIOS into high memory, we don't allow\n    // executing code there, which enables optimisations in read_imm8.\n    // See [make_bios_writable_intel] in src/fw/shadow.c in seabios for details\n    const PAM0 = 0x10;\n\n    var host_bridge = {\n        pci_id: 0,\n        pci_space: [\n            // 00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)\n            0x86, 0x80, 0x37, 0x12, 0x00, 0x00, 0x00, 0x00,  0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, PAM0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n        ],\n        pci_bars: [],\n        name: \"82441FX PMC\",\n    };\n    this.register_device(host_bridge);\n\n    this.isa_bridge = {\n        pci_id: 1 << 3,\n        pci_space: [\n            // 00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]\n            0x86, 0x80, 0x00, 0x70, 0x07, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x06, 0x00, 0x00, 0x80, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n        ],\n        pci_bars: [],\n        name: \"82371SB PIIX3 ISA\",\n    };\n    this.isa_bridge_space = this.register_device(this.isa_bridge);\n    this.isa_bridge_space8 = new Uint8Array(this.isa_bridge_space.buffer);\n\n    // 00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 90)\n    //this.register_device([\n    //    0x86, 0x80, 0x4e, 0x24, 0x07, 0x01, 0x10, 0x00, 0x90, 0x01, 0x04, 0x06, 0x00, 0x00, 0x01, 0x00,\n    //    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x05, 0x20, 0xe0, 0xe0, 0x80, 0x22,\n    //    0xb0, 0xfe, 0xb0, 0xfe, 0xf1, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n    //    0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x02, 0x00,\n    //], 0x1e << 3);\n}\n\nPCI.prototype.get_state = function()\n{\n    var state = [];\n\n    for(var i = 0; i < 256; i++)\n    {\n        state[i] = this.device_spaces[i];\n    }\n\n    state[256] = this.pci_addr;\n    state[257] = this.pci_value;\n    state[258] = this.pci_response;\n    state[259] = this.pci_status;\n\n    return state;\n};\n\nPCI.prototype.set_state = function(state)\n{\n    for(var i = 0; i < 256; i++)\n    {\n        var device = this.devices[i];\n        var space = state[i];\n\n        if(!device || !space)\n        {\n            if(device)\n            {\n                dbg_log(\"Warning: While restoring PCI device: Device exists in current \" +\n                        \"configuration but not in snapshot (\" + device.name + \")\");\n            }\n            if(space)\n            {\n                dbg_log(\"Warning: While restoring PCI device: Device doesn't exist in current \" +\n                        \"configuration but does in snapshot (device \" + h(i, 2) + \")\");\n            }\n            continue;\n        }\n\n        for(var bar_nr = 0; bar_nr < device.pci_bars.length; bar_nr++)\n        {\n            var value = space[(0x10 >> 2) + bar_nr];\n\n            if(value & 1)\n            {\n                var bar = device.pci_bars[bar_nr];\n                var from = bar.original_bar & ~1 & 0xFFFF;\n                var to = value & ~1 & 0xFFFF;\n                this.set_io_bars(bar, from, to);\n            }\n            else\n            {\n                // memory, cannot be changed\n            }\n        }\n\n        this.device_spaces[i].set(space);\n    }\n\n    this.pci_addr.set(state[256]);\n    this.pci_value.set(state[257]);\n    this.pci_response.set(state[258]);\n    this.pci_status.set(state[259]);\n};\n\nPCI.prototype.pci_query = function()\n{\n    var dbg_line = \"query\";\n\n    // Bit | .31                     .0\n    // Fmt | EBBBBBBBBDDDDDFFFRRRRRR00\n\n    var bdf = this.pci_addr[2] << 8 | this.pci_addr[1],\n        addr = this.pci_addr[0] & 0xFC,\n        //devfn = bdf & 0xFF,\n        //bus = bdf >> 8,\n        dev = bdf >> 3 & 0x1F,\n        //fn = bdf & 7,\n        enabled = this.pci_addr[3] >> 7;\n\n    dbg_line += \" enabled=\" + enabled;\n    dbg_line += \" bdf=\" + h(bdf, 4);\n    dbg_line += \" dev=\" + h(dev, 2);\n    dbg_line += \" addr=\" + h(addr, 2);\n\n    var device = this.device_spaces[bdf];\n\n    if(device !== undefined)\n    {\n        this.pci_status32[0] = 0x80000000 | 0;\n\n        if(addr < device.byteLength)\n        {\n            this.pci_response32[0] = device[addr >> 2];\n        }\n        else\n        {\n            // required by freebsd-9.1\n            this.pci_response32[0] = 0;\n        }\n\n        dbg_line += \" \" + h(this.pci_addr32[0] >>> 0, 8) + \" -> \" + h(this.pci_response32[0] >>> 0, 8);\n\n        if(addr >= device.byteLength)\n        {\n            dbg_line += \" (undef)\";\n        }\n\n        dbg_line += \" (\" + this.devices[bdf].name + \")\";\n\n        dbg_log(dbg_line, LOG_PCI);\n    }\n    else\n    {\n        this.pci_response32[0] = -1;\n        this.pci_status32[0] = 0;\n    }\n};\n\nPCI.prototype.pci_write8 = function(address, written)\n{\n    var bdf = address >> 8 & 0xFFFF;\n    var addr = address & 0xFF;\n\n    var space = new Uint8Array(this.device_spaces[bdf].buffer);\n    var device = this.devices[bdf];\n\n    if(!space)\n    {\n        return;\n    }\n\n    dbg_assert(!(addr >= 0x10 && addr < 0x2C || addr >= 0x30 && addr < 0x34),\n               \"PCI: Expected 32-bit write, got 8-bit (addr: \" + h(addr) + \")\");\n\n    dbg_log(\"PCI write8 dev=\" + h(bdf >> 3, 2) + \" (\" + device.name + \") addr=\" + h(addr, 4) +\n            \" value=\" + h(written, 2), LOG_PCI);\n\n    space[addr] = written;\n};\n\nPCI.prototype.pci_write16 = function(address, written)\n{\n    dbg_assert((address & 1) === 0);\n\n    var bdf = address >> 8 & 0xFFFF;\n    var addr = address & 0xFF;\n\n    var space = new Uint16Array(this.device_spaces[bdf].buffer);\n    var device = this.devices[bdf];\n\n    if(!space)\n    {\n        return;\n    }\n\n    if(addr >= 0x10 && addr < 0x2C)\n    {\n        // Bochs bios\n        dbg_log(\"Warning: PCI: Expected 32-bit write, got 16-bit (addr: \" + h(addr) + \")\");\n        return;\n    }\n\n    dbg_assert(!(addr >= 0x30 && addr < 0x34),\n        \"PCI: Expected 32-bit write, got 16-bit (addr: \" + h(addr) + \")\");\n\n    dbg_log(\"PCI writ16 dev=\" + h(bdf >> 3, 2) + \" (\" + device.name + \") addr=\" + h(addr, 4) +\n            \" value=\" + h(written, 4), LOG_PCI);\n\n    space[addr >>> 1] = written;\n};\n\nPCI.prototype.pci_write32 = function(address, written)\n{\n    dbg_assert((address & 3) === 0);\n\n    var bdf = address >> 8 & 0xFFFF;\n    var addr = address & 0xFF;\n\n    var space = this.device_spaces[bdf];\n    var device = this.devices[bdf];\n\n    if(!space)\n    {\n        return;\n    }\n\n    if(addr >= 0x10 && addr < 0x28)\n    {\n        var bar_nr = addr - 0x10 >> 2;\n        var bar = device.pci_bars[bar_nr];\n\n        dbg_log(\"BAR\" + bar_nr + \" exists=\" + (bar ? \"y\" : \"n\") + \" changed from \" + h(space[addr >> 2]) + \" to \" +\n                h(written >>> 0) + \" dev=\" + h(bdf >> 3, 2) + \" (\" + device.name + \") \", LOG_PCI);\n\n        if(bar)\n        {\n            dbg_assert(!(bar.size & bar.size - 1), \"bar size should be power of 2\");\n\n            var space_addr = addr >> 2;\n            var type = space[space_addr] & 1;\n\n            if((written | 3 | bar.size - 1)  === -1) // size check\n            {\n                written = ~(bar.size - 1) | type;\n\n                if(type === 0)\n                {\n                    space[space_addr] = written;\n                }\n            }\n            else\n            {\n                if(type === 0)\n                {\n                    // memory\n                    var original_bar = bar.original_bar;\n\n                    if((written & ~0xF) !== (original_bar & ~0xF))\n                    {\n                        // seabios\n                        dbg_log(\"Warning: Changing memory bar not supported, ignored\", LOG_PCI);\n                    }\n\n                    // changing isn't supported yet, reset to default\n                    space[space_addr] = original_bar;\n                }\n            }\n\n            if(type === 1)\n            {\n                // io\n                dbg_assert(type === 1);\n\n                var from = space[space_addr] & ~1 & 0xFFFF;\n                var to = written & ~1 & 0xFFFF;\n                dbg_log(\"io bar changed from \" + h(from >>> 0, 8) +\n                        \" to \" + h(to >>> 0, 8) + \" size=\" + bar.size, LOG_PCI);\n                this.set_io_bars(bar, from, to);\n                space[space_addr] = written | 1;\n            }\n        }\n        else\n        {\n            space[addr >> 2] = 0;\n        }\n\n        dbg_log(\"BAR effective value: \" + h(space[addr >> 2] >>> 0), LOG_PCI);\n    }\n    else if(addr === 0x30)\n    {\n        dbg_log(\"PCI write rom address dev=\" + h(bdf >> 3, 2) + \" (\" + device.name + \")\" +\n                \" value=\" + h(written >>> 0, 8), LOG_PCI);\n\n        if(device.pci_rom_size)\n        {\n            if((written | 0x7FF) === (0xFFFFFFFF|0))\n            {\n                space[addr >> 2] = -device.pci_rom_size | 0;\n            }\n            else\n            {\n                space[addr >> 2] = device.pci_rom_address | 0;\n            }\n        }\n        else\n        {\n            space[addr >> 2] = 0;\n        }\n    }\n    else if(addr === 0x04)\n    {\n        dbg_log(\"PCI write dev=\" + h(bdf >> 3, 2) + \" (\" + device.name + \") addr=\" + h(addr, 4) +\n                \" value=\" + h(written >>> 0, 8), LOG_PCI);\n    }\n    else\n    {\n        dbg_log(\"PCI write dev=\" + h(bdf >> 3, 2) + \" (\" + device.name + \") addr=\" + h(addr, 4) +\n                \" value=\" + h(written >>> 0, 8), LOG_PCI);\n        space[addr >>> 2] = written;\n    }\n};\n\nPCI.prototype.register_device = function(device)\n{\n    dbg_assert(device.pci_id !== undefined);\n    dbg_assert(device.pci_space !== undefined);\n    dbg_assert(device.pci_bars !== undefined);\n\n    var device_id = device.pci_id;\n\n    dbg_log(\"PCI register bdf=\" + h(device_id) + \" (\" + device.name + \")\", LOG_PCI);\n\n    if(this.devices[device_id])\n    {\n        dbg_log(\"warning: overwriting device \" + this.devices[device_id].name + \" with \" + device.name, LOG_PCI);\n    }\n    dbg_assert(device.pci_space.length >= 64);\n    dbg_assert(device_id < this.devices.length);\n\n    // convert bytewise notation from lspci to double words\n    var space = new Int32Array(64);\n    space.set(new Int32Array(new Uint8Array(device.pci_space).buffer));\n    this.device_spaces[device_id] = space;\n    this.devices[device_id] = device;\n\n    var bar_space = space.slice(4, 10);\n\n    for(var i = 0; i < device.pci_bars.length; i++)\n    {\n        var bar = device.pci_bars[i];\n\n        if(!bar)\n        {\n            continue;\n        }\n\n        var bar_base = bar_space[i];\n        var type = bar_base & 1;\n        dbg_log(\"device \"+ device.name +\" register bar of size \"+bar.size +\" at \" + h(bar_base), LOG_PCI);\n\n        bar.original_bar = bar_base;\n        bar.entries = [];\n\n        if(type === 0)\n        {\n            // memory, not needed currently\n        }\n        else\n        {\n            dbg_assert(type === 1);\n            var port = bar_base & ~1;\n\n            for(var j = 0; j < bar.size; j++)\n            {\n                bar.entries[j] = this.io.ports[port + j];\n            }\n        }\n    }\n\n    return space;\n};\n\nPCI.prototype.set_io_bars = function(bar, from, to)\n{\n    var count = bar.size;\n    dbg_log(\"Move io bars: from=\" + h(from) + \" to=\" + h(to) + \" count=\" + count, LOG_PCI);\n\n    var ports = this.io.ports;\n\n    for(var i = 0; i < count; i++)\n    {\n        var old_entry = ports[from + i];\n\n        if(from + i >= 0x1000)\n        {\n            ports[from + i] = this.io.create_empty_entry();\n        }\n\n        var entry = bar.entries[i];\n        var empty_entry = ports[to + i];\n        dbg_assert(entry && empty_entry);\n\n        if(to + i >= 0x1000)\n        {\n            ports[to + i] = entry;\n        }\n    }\n};\n\nPCI.prototype.raise_irq = function(pci_id)\n{\n    var space = this.device_spaces[pci_id];\n    dbg_assert(space);\n\n    var pin = (space[0x3C >>> 2] >> 8 & 0xFF) - 1;\n    var device = (pci_id >> 3) - 1 & 0xFF;\n    var parent_pin = pin + device & 3;\n    var irq = this.isa_bridge_space8[0x60 + parent_pin];\n\n    //dbg_log(\"PCI raise irq \" + h(irq) + \" dev=\" + h(device, 2) +\n    //        \" (\" + this.devices[pci_id].name + \")\", LOG_PCI);\n    this.cpu.device_raise_irq(irq);\n};\n\nPCI.prototype.lower_irq = function(pci_id)\n{\n    var space = this.device_spaces[pci_id];\n    dbg_assert(space);\n\n    var pin = space[0x3C >>> 2] >> 8 & 0xFF;\n    var device = pci_id >> 3 & 0xFF;\n    var parent_pin = pin + device - 2 & 3;\n    var irq = this.isa_bridge_space8[0x60 + parent_pin];\n\n    //dbg_log(\"PCI lower irq \" + h(irq) + \" dev=\" + h(device, 2) +\n    //        \" (\" + this.devices[pci_id].name + \")\", LOG_PCI);\n    this.cpu.device_lower_irq(irq);\n};\n"
  },
  {
    "path": "src/pit.js",
    "content": "import { v86 } from \"./main.js\";\nimport { LOG_PIT } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\n\n\n// In kHz\nexport const OSCILLATOR_FREQ = 1193.1816666; // 1.193182 MHz\n\n/**\n * @constructor\n *\n * Programmable Interval Timer\n */\nexport function PIT(cpu, bus)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    this.bus = bus;\n\n    this.counter_start_time = new Float64Array(3);\n    this.counter_start_value = new Uint16Array(3);\n\n    this.counter_next_low = new Uint8Array(4);\n    this.counter_enabled = new Uint8Array(4);\n    this.counter_mode = new Uint8Array(4);\n    this.counter_read_mode = new Uint8Array(4);\n\n    // 2 = latch low, 1 = latch high, 0 = no latch\n    this.counter_latch = new Uint8Array(4);\n    this.counter_latch_value = new Uint16Array(3);\n\n    this.counter_reload = new Uint16Array(3);\n\n    // TODO:\n    // - counter2 can be controlled by an input\n\n    cpu.io.register_read(0x61, this, function()\n    {\n        var now = v86.microtick();\n\n        var ref_toggle = (now * (1000 * 1000 / 15000)) & 1;\n        var counter2_out = this.did_rollover(2, now);\n\n        return ref_toggle << 4 | counter2_out << 5;\n    });\n    cpu.io.register_write(0x61, this, function(data)\n    {\n        if(data & 1)\n        {\n            this.bus.send(\"pcspeaker-enable\");\n        }\n        else\n        {\n            this.bus.send(\"pcspeaker-disable\");\n        }\n    });\n\n    cpu.io.register_read(0x40, this, function() { return this.counter_read(0); });\n    cpu.io.register_read(0x41, this, function() { return this.counter_read(1); });\n    cpu.io.register_read(0x42, this, function() { return this.counter_read(2); });\n\n    cpu.io.register_write(0x40, this, function(data) { this.counter_write(0, data); });\n    cpu.io.register_write(0x41, this, function(data) { this.counter_write(1, data); });\n    cpu.io.register_write(0x42, this, function(data) {\n        this.counter_write(2, data);\n        this.bus.send(\"pcspeaker-update\", [this.counter_mode[2], this.counter_reload[2]]);\n    });\n\n    cpu.io.register_write(0x43, this, this.port43_write);\n}\n\nPIT.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.counter_next_low;\n    state[1] = this.counter_enabled;\n    state[2] = this.counter_mode;\n    state[3] = this.counter_read_mode;\n    state[4] = this.counter_latch;\n    state[5] = this.counter_latch_value;\n    state[6] = this.counter_reload;\n    state[7] = this.counter_start_time;\n    state[8] = this.counter_start_value;\n\n    return state;\n};\n\nPIT.prototype.set_state = function(state)\n{\n    this.counter_next_low = state[0];\n    this.counter_enabled = state[1];\n    this.counter_mode = state[2];\n    this.counter_read_mode = state[3];\n    this.counter_latch = state[4];\n    this.counter_latch_value = state[5];\n    this.counter_reload = state[6];\n    this.counter_start_time = state[7];\n    this.counter_start_value = state[8];\n};\n\nPIT.prototype.timer = function(now, no_irq)\n{\n    var time_to_next_interrupt = 100;\n\n    // counter 0 produces interrupts\n    if(!no_irq)\n    {\n        if(this.counter_enabled[0] && this.did_rollover(0, now))\n        {\n            this.counter_start_value[0] = this.get_counter_value(0, now);\n            this.counter_start_time[0] = now;\n\n            dbg_log(\"pit interrupt. new value: \" + this.counter_start_value[0], LOG_PIT);\n\n            // This isn't strictly correct, but it's necessary since browsers\n            // may sleep longer than necessary to trigger the else branch below\n            // and clear the irq\n            this.cpu.device_lower_irq(0);\n\n            this.cpu.device_raise_irq(0);\n            var mode = this.counter_mode[0];\n\n            if(mode === 0)\n            {\n                this.counter_enabled[0] = 0;\n            }\n        }\n        else\n        {\n            this.cpu.device_lower_irq(0);\n        }\n\n        if(this.counter_enabled[0])\n        {\n            const diff = now - this.counter_start_time[0];\n            const diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);\n            const ticks_missing = this.counter_start_value[0] - diff_in_ticks; // XXX: to simplify\n            time_to_next_interrupt = ticks_missing / OSCILLATOR_FREQ;\n        }\n    }\n\n    return time_to_next_interrupt;\n};\n\nPIT.prototype.get_counter_value = function(i, now)\n{\n    if(!this.counter_enabled[i])\n    {\n        return 0;\n    }\n\n    var diff = now - this.counter_start_time[i];\n    var diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);\n\n    var value = this.counter_start_value[i] - diff_in_ticks;\n\n    dbg_log(\"diff=\" + diff + \" dticks=\" + diff_in_ticks + \" value=\" + value + \" reload=\" + this.counter_reload[i], LOG_PIT);\n\n    var reload = this.counter_reload[i];\n\n    if(value >= reload)\n    {\n        dbg_log(\"Warning: Counter\" + i + \" value \" + value  + \" is larger than reload \" + reload, LOG_PIT);\n        value %= reload;\n    }\n    else if(value < 0)\n    {\n        value = value % reload + reload;\n    }\n\n    return value;\n};\n\nPIT.prototype.did_rollover = function(i, now)\n{\n    var diff = now - this.counter_start_time[i];\n\n    if(diff < 0)\n    {\n        // should only happen after restore_state\n        dbg_log(\"Warning: PIT timer difference is negative, resetting (timer \" + i + \")\");\n        return true;\n    }\n    var diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);\n    //dbg_log(i + \": diff=\" + diff + \" start_time=\" + this.counter_start_time[i] + \" diff_in_ticks=\" + diff_in_ticks + \" (\" + diff * OSCILLATOR_FREQ + \") start_value=\" + this.counter_start_value[i] + \" did_rollover=\" + (this.counter_start_value[i] < diff_in_ticks), LOG_PIT);\n\n    return this.counter_start_value[i] < diff_in_ticks;\n};\n\nPIT.prototype.counter_read = function(i)\n{\n    var latch = this.counter_latch[i];\n\n    if(latch)\n    {\n        this.counter_latch[i]--;\n\n        if(latch === 2)\n        {\n            return this.counter_latch_value[i] & 0xFF;\n        }\n        else\n        {\n            return this.counter_latch_value[i] >> 8;\n        }\n    }\n    else\n    {\n        var next_low = this.counter_next_low[i];\n\n        if(this.counter_mode[i] === 3)\n        {\n            this.counter_next_low[i] ^= 1;\n        }\n\n        var value = this.get_counter_value(i, v86.microtick());\n\n        if(next_low)\n        {\n            return value & 0xFF;\n        }\n        else\n        {\n            return value >> 8;\n        }\n    }\n};\n\nPIT.prototype.counter_write = function(i, value)\n{\n    if(this.counter_next_low[i])\n    {\n        this.counter_reload[i] = this.counter_reload[i] & ~0xFF | value;\n    }\n    else\n    {\n        this.counter_reload[i] = this.counter_reload[i] & 0xFF | value << 8;\n    }\n\n    if(this.counter_read_mode[i] !== 3 || !this.counter_next_low[i])\n    {\n        if(!this.counter_reload[i])\n        {\n            this.counter_reload[i] = 0xFFFF;\n        }\n\n        // depends on the mode, should actually\n        // happen on the first tick\n        this.counter_start_value[i] = this.counter_reload[i];\n\n        this.counter_enabled[i] = true;\n\n        this.counter_start_time[i] = v86.microtick();\n\n        dbg_log(\"counter\" + i + \" reload=\" + h(this.counter_reload[i]) +\n                \" tick=\" + (this.counter_reload[i] || 0x10000) / OSCILLATOR_FREQ + \"ms\", LOG_PIT);\n    }\n\n    if(this.counter_read_mode[i] === 3)\n    {\n        this.counter_next_low[i] ^= 1;\n    }\n};\n\nPIT.prototype.port43_write = function(reg_byte)\n{\n    var mode = reg_byte >> 1 & 7,\n        binary_mode = reg_byte & 1,\n        i = reg_byte >> 6 & 3,\n        read_mode = reg_byte >> 4 & 3;\n\n    if(i === 1)\n    {\n        dbg_log(\"Unimplemented timer1\", LOG_PIT);\n    }\n\n    if(i === 3)\n    {\n        dbg_log(\"Unimplemented read back\", LOG_PIT);\n        return;\n    }\n\n    if(read_mode === 0)\n    {\n        // latch\n        this.counter_latch[i] = 2;\n        var value = this.get_counter_value(i, v86.microtick());\n        dbg_log(\"latch: \" + value, LOG_PIT);\n        this.counter_latch_value[i] = value ? value - 1 : 0;\n\n        return;\n    }\n\n    if(mode >= 6)\n    {\n        // 6 and 7 are aliased to 2 and 3\n        mode &= ~4;\n    }\n\n    dbg_log(\"Control: mode=\" + mode + \" ctr=\" + i +\n            \" read_mode=\" + read_mode + \" bcd=\" + binary_mode, LOG_PIT);\n\n    if(read_mode === 1)\n    {\n        // lsb\n        this.counter_next_low[i] = 1;\n    }\n    else if(read_mode === 2)\n    {\n        // msb\n        this.counter_next_low[i] = 0;\n    }\n    else\n    {\n        // first lsb then msb\n        this.counter_next_low[i] = 1;\n    }\n\n    if(i === 0)\n    {\n        this.cpu.device_lower_irq(0);\n    }\n\n    if(mode === 0)\n    {\n    }\n    else if(mode === 3 || mode === 2)\n    {\n        // what is the difference\n    }\n    else\n    {\n        dbg_log(\"Unimplemented counter mode: \" + h(mode), LOG_PIT);\n    }\n\n    this.counter_mode[i] = mode;\n    this.counter_read_mode[i] = read_mode;\n\n    if(i === 2)\n    {\n        this.bus.send(\"pcspeaker-update\", [this.counter_mode[2], this.counter_reload[2]]);\n    }\n};\n\nPIT.prototype.dump = function()\n{\n    const reload = this.counter_reload[0];\n    const time = (reload || 0x10000) / OSCILLATOR_FREQ;\n    dbg_log(\"counter0 ticks every \" + time + \"ms (reload=\" + reload + \")\");\n};\n"
  },
  {
    "path": "src/ps2.js",
    "content": "import { LOG_PS2 } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { BusConnector } from \"./bus.js\";\nimport { ByteQueue } from \"./lib.js\";\n\nconst PS2_LOG_VERBOSE = false;\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n */\nexport function PS2(cpu, bus)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n\n    this.reset();\n\n    this.bus.register(\"keyboard-code\", function(code)\n    {\n        this.kbd_send_code(code);\n    }, this);\n\n    this.bus.register(\"mouse-click\", function(data)\n    {\n        this.mouse_send_click(data[0], data[1], data[2]);\n    }, this);\n\n    this.bus.register(\"mouse-delta\", function(data)\n    {\n        this.mouse_send_delta(data[0], data[1]);\n    }, this);\n\n    this.bus.register(\"mouse-wheel\", function(data)\n    {\n        this.wheel_movement -= data[0];\n        this.wheel_movement -= data[1] * 2; // X Wheel Movement\n        this.wheel_movement = Math.min(7, Math.max(-8, this.wheel_movement));\n        this.send_mouse_packet(0, 0);\n    }, this);\n\n    cpu.io.register_read(0x60, this, this.port60_read);\n    cpu.io.register_read(0x64, this, this.port64_read);\n\n    cpu.io.register_write(0x60, this, this.port60_write);\n    cpu.io.register_write(0x64, this, this.port64_write);\n}\n\nPS2.prototype.reset = function()\n{\n    /** @type {boolean} */\n    this.enable_mouse_stream = false;\n\n    /** @type {boolean} */\n    this.use_mouse = false;\n\n    /** @type {boolean} */\n    this.have_mouse = true;\n\n    /** @type {number} */\n    this.mouse_delta_x = 0;\n    /** @type {number} */\n    this.mouse_delta_y = 0;\n    /** @type {number} */\n    this.mouse_clicks = 0;\n\n    /** @type {boolean} */\n    this.have_keyboard = true;\n\n    /** @type {boolean} */\n    this.enable_keyboard_stream = false;\n\n    /** @type {boolean} */\n    this.next_is_mouse_command = false;\n\n    /** @type {boolean} */\n    this.next_read_sample = false;\n\n    /** @type {boolean} */\n    this.next_read_led = false;\n\n    /** @type {boolean} */\n    this.next_handle_scan_code_set = false;\n\n    /** @type {boolean} */\n    this.next_read_rate = false;\n\n    /** @type {boolean} */\n    this.next_read_resolution = false;\n\n    /**\n     * @type {ByteQueue}\n     */\n    this.kbd_buffer = new ByteQueue(1024);\n\n    this.last_port60_byte = 0;\n\n    /** @type {number} */\n    this.sample_rate = 100;\n\n    /** @type {number} */\n    this.mouse_detect_state = 0;\n\n    /** @type {number} */\n    this.mouse_id = 0x00;\n\n    /** @type {boolean} */\n    this.mouse_reset_workaround = false;\n\n    /** @type {number} */\n    this.wheel_movement = 0;\n\n    /** @type {number} */\n    this.resolution = 4;\n\n    /** @type {boolean} */\n    this.scaling2 = false;\n\n    /** @type {number} */\n    this.last_mouse_packet = -1;\n\n    /**\n     * @type {ByteQueue}\n     */\n    this.mouse_buffer = new ByteQueue(1024);\n\n    /**\n     * @type {boolean}\n     * Also known as DBBOUT OBF - Output Buffer Full flag\n     */\n    this.next_byte_is_ready = false;\n\n    /** @type {boolean} */\n    this.next_byte_is_aux = false;\n\n    this.command_register = 1 | 4;\n    // TODO: What should be the initial value?\n    this.controller_output_port = 0;\n    this.read_output_register = false;\n    this.read_command_register = false;\n    this.read_controller_output_port = false;\n};\n\nPS2.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.enable_mouse_stream;\n    state[1] = this.use_mouse;\n    state[2] = this.have_mouse;\n    state[3] = this.mouse_delta_x;\n    state[4] = this.mouse_delta_y;\n    state[5] = this.mouse_clicks;\n    state[6] = this.have_keyboard;\n    state[7] = this.enable_keyboard_stream;\n    state[8] = this.next_is_mouse_command;\n    state[9] = this.next_read_sample;\n    state[10] = this.next_read_led;\n    state[11] = this.next_handle_scan_code_set;\n    state[12] = this.next_read_rate;\n    state[13] = this.next_read_resolution;\n    //state[14] = this.kbd_buffer;\n    state[15] = this.last_port60_byte;\n    state[16] = this.sample_rate;\n    state[17] = this.resolution;\n    state[18] = this.scaling2;\n    //state[19] = this.mouse_buffer;\n    state[20] = this.command_register;\n    state[21] = this.read_output_register;\n    state[22] = this.read_command_register;\n    state[23] = this.controller_output_port;\n    state[24] = this.read_controller_output_port;\n    state[25] = this.mouse_id;\n    state[26] = this.mouse_detect_state;\n    state[27] = this.mouse_reset_workaround;\n\n    return state;\n};\n\nPS2.prototype.set_state = function(state)\n{\n    this.enable_mouse_stream = state[0];\n    this.use_mouse = state[1];\n    this.have_mouse = state[2];\n    this.mouse_delta_x = state[3];\n    this.mouse_delta_y = state[4];\n    this.mouse_clicks = state[5];\n    this.have_keyboard = state[6];\n    this.enable_keyboard_stream = state[7];\n    this.next_is_mouse_command = state[8];\n    this.next_read_sample = state[9];\n    this.next_read_led = state[10];\n    this.next_handle_scan_code_set = state[11];\n    this.next_read_rate = state[12];\n    this.next_read_resolution = state[13];\n    //this.kbd_buffer = state[14];\n    this.last_port60_byte = state[15];\n    this.sample_rate = state[16];\n    this.resolution = state[17];\n    this.scaling2 = state[18];\n    //this.mouse_buffer = state[19];\n    this.command_register = state[20];\n    this.read_output_register = state[21];\n    this.read_command_register = state[22];\n    this.controller_output_port = state[23];\n    this.read_controller_output_port = state[24];\n    this.mouse_id = state[25] || 0;\n    this.mouse_detect_state = state[26] || 0;\n    this.mouse_reset_workaround = state[27] || false;\n\n    this.next_byte_is_ready = false;\n    this.next_byte_is_aux = false;\n    this.kbd_buffer.clear();\n    this.mouse_buffer.clear();\n\n    this.bus.send(\"mouse-enable\", this.use_mouse);\n};\n\nPS2.prototype.raise_irq = function()\n{\n    if(this.next_byte_is_ready)\n    {\n        // Wait until previous byte is read\n        // http://halicery.com/Hardware/8042/8042_1503033_TXT.htm\n        return;\n    }\n\n    // Kbd has priority over aux\n    if(this.kbd_buffer.length)\n    {\n        this.kbd_irq();\n    }\n    else if(this.mouse_buffer.length)\n    {\n        this.mouse_irq();\n    }\n};\n\nPS2.prototype.mouse_irq = function()\n{\n    this.next_byte_is_ready = true;\n    this.next_byte_is_aux = true;\n\n    if(this.command_register & 2)\n    {\n        dbg_log(\"Mouse irq\", LOG_PS2);\n\n        // Pulse the irq line\n        // Note: can't lower immediately after rising, so lower before rising\n        // http://www.os2museum.com/wp/ibm-ps2-model-50-keyboard-controller/\n        this.cpu.device_lower_irq(12);\n        this.cpu.device_raise_irq(12);\n    }\n};\n\nPS2.prototype.kbd_irq = function()\n{\n    this.next_byte_is_ready = true;\n    this.next_byte_is_aux = false;\n\n    if(this.command_register & 1)\n    {\n        dbg_log(\"Keyboard irq\", LOG_PS2);\n\n        // Pulse the irq line\n        // Note: can't lower immediately after rising, so lower before rising\n        // http://www.os2museum.com/wp/ibm-ps2-model-50-keyboard-controller/\n        this.cpu.device_lower_irq(1);\n        this.cpu.device_raise_irq(1);\n    }\n};\n\nPS2.prototype.kbd_send_code = function(code)\n{\n    if(this.enable_keyboard_stream)\n    {\n        dbg_log(\"adding kbd code: \" + h(code), LOG_PS2);\n        this.kbd_buffer.push(code);\n        this.raise_irq();\n    }\n};\n\nPS2.prototype.mouse_send_delta = function(delta_x, delta_y)\n{\n    if(!this.have_mouse || !this.use_mouse)\n    {\n        return;\n    }\n\n    // note: delta_x or delta_y can be floating point numbers\n\n    //const factor = this.resolution * this.sample_rate / 80;\n    const factor = 1;\n\n    this.mouse_delta_x += delta_x * factor;\n    this.mouse_delta_y += delta_y * factor;\n\n    if(this.enable_mouse_stream)\n    {\n        var change_x = this.mouse_delta_x | 0,\n            change_y = this.mouse_delta_y | 0;\n\n        if(change_x || change_y)\n        {\n            //var now = Date.now();\n            //if(now - this.last_mouse_packet < 1000 / this.sample_rate)\n            //{\n            //    // TODO: set timeout\n            //    return;\n            //}\n\n            this.mouse_delta_x -= change_x;\n            this.mouse_delta_y -= change_y;\n\n            this.send_mouse_packet(change_x, change_y);\n        }\n    }\n};\n\nPS2.prototype.mouse_send_click = function(left, middle, right)\n{\n    if(!this.have_mouse || !this.use_mouse)\n    {\n        return;\n    }\n\n    this.mouse_clicks = left | right << 1 | middle << 2;\n\n    if(this.enable_mouse_stream)\n    {\n        this.send_mouse_packet(0, 0);\n    }\n};\n\nPS2.prototype.send_mouse_packet = function(dx, dy)\n{\n    var info_byte =\n            (dy < 0) << 5 |\n            (dx < 0) << 4 |\n            1 << 3 |\n            this.mouse_clicks,\n        delta_x = dx,\n        delta_y = dy;\n\n    this.last_mouse_packet = Date.now();\n\n    //if(this.scaling2)\n    //{\n    //    // only in automatic packets, not 0xEB requests\n    //    delta_x = this.apply_scaling2(delta_x);\n    //    delta_y = this.apply_scaling2(delta_y);\n    //}\n\n    this.mouse_buffer.push(info_byte);\n    this.mouse_buffer.push(delta_x);\n    this.mouse_buffer.push(delta_y);\n\n    if(this.mouse_id === 0x04)\n    {\n        this.mouse_buffer.push(\n            0 << 5 | // TODO: 5th button\n            0 << 4 | // TODO: 4th button\n            this.wheel_movement & 0x0F\n        );\n        this.wheel_movement = 0;\n    }\n    else if(this.mouse_id === 0x03)\n    {\n        this.mouse_buffer.push(this.wheel_movement & 0xFF); // Byte 4 - Z Movement\n        this.wheel_movement = 0;\n    }\n\n    if(PS2_LOG_VERBOSE)\n    {\n        dbg_log(\"adding mouse packets: \" + [info_byte, dx, dy], LOG_PS2);\n    }\n\n    this.raise_irq();\n};\n\nPS2.prototype.apply_scaling2 = function(n)\n{\n    // http://www.computer-engineering.org/ps2mouse/#Inputs.2C_Resolution.2C_and_Scaling\n    var abs = Math.abs(n),\n        sign = n >> 31;\n\n    switch(abs)\n    {\n        case 0:\n        case 1:\n        case 3:\n            return n;\n        case 2:\n            return sign;\n        case 4:\n            return 6 * sign;\n        case 5:\n            return 9 * sign;\n        default:\n            return n << 1;\n    }\n};\n\nPS2.prototype.port60_read = function()\n{\n    //dbg_log(\"port 60 read: \" + (buffer[0] || \"(none)\"));\n\n    this.next_byte_is_ready = false;\n\n    if(!this.kbd_buffer.length && !this.mouse_buffer.length)\n    {\n        // should not happen\n        dbg_log(\"Port 60 read: Empty\", LOG_PS2);\n        return this.last_port60_byte;\n    }\n\n    if(this.next_byte_is_aux)\n    {\n        this.cpu.device_lower_irq(12);\n        this.last_port60_byte = this.mouse_buffer.shift();\n        dbg_log(\"Port 60 read (mouse): \" + h(this.last_port60_byte), LOG_PS2);\n    }\n    else\n    {\n        this.cpu.device_lower_irq(1);\n        this.last_port60_byte = this.kbd_buffer.shift();\n        dbg_log(\"Port 60 read (kbd)  : \" + h(this.last_port60_byte), LOG_PS2);\n    }\n\n    if(this.kbd_buffer.length || this.mouse_buffer.length)\n    {\n        this.raise_irq();\n    }\n\n    return this.last_port60_byte;\n};\n\nPS2.prototype.port64_read = function()\n{\n    // status port\n\n    var status_byte = 0x10;\n\n    if(this.next_byte_is_ready)\n    {\n        status_byte |= 0x1;\n    }\n    if(this.next_byte_is_aux)\n    {\n        status_byte |= 0x20;\n    }\n\n    dbg_log(\"port 64 read: \" + h(status_byte), LOG_PS2);\n\n    return status_byte;\n};\n\nPS2.prototype.port60_write = function(write_byte)\n{\n    dbg_log(\"port 60 write: \" + h(write_byte), LOG_PS2);\n\n    if(this.read_command_register)\n    {\n        this.command_register = write_byte;\n        this.read_command_register = false;\n\n        // not sure, causes \"spurious ack\" in Linux\n        //this.kbd_buffer.push(0xFA);\n        //this.kbd_irq();\n\n        dbg_log(\"Keyboard command register = \" + h(this.command_register), LOG_PS2);\n    }\n    else if(this.read_output_register)\n    {\n        this.read_output_register = false;\n\n        this.mouse_buffer.clear();\n        this.mouse_buffer.push(write_byte);\n        this.mouse_irq();\n    }\n    else if(this.next_read_sample)\n    {\n        this.next_read_sample = false;\n        this.mouse_buffer.clear();\n        this.mouse_buffer.push(0xFA);\n\n        this.sample_rate = write_byte;\n\n        switch(this.mouse_detect_state)\n        {\n            case -1:\n                if(write_byte === 60)\n                {\n                    // Detect Windows NT and turn on workaround the bug\n                    // 200->100->80->60\n                    this.mouse_reset_workaround = true;\n                    this.mouse_detect_state = 0;\n                }\n                else\n                {\n                    this.mouse_reset_workaround = false;\n                    this.mouse_detect_state = (write_byte === 200) ? 1 : 0;\n                }\n                break;\n            case 0:\n                if(write_byte === 200) this.mouse_detect_state = 1;\n                break;\n            case 1:\n                if(write_byte === 100) this.mouse_detect_state = 2;\n                else if(write_byte === 200) this.mouse_detect_state = 3;\n                else this.mouse_detect_state = 0;\n                break;\n            case 2:\n                // Host sends sample rate 200->100->80 to activate Intellimouse wheel\n                if(write_byte === 80) this.mouse_id = 0x03;\n                this.mouse_detect_state = -1;\n                break;\n            case 3:\n                // Host sends sample rate 200->200->80 to activate Intellimouse 4th, 5th buttons\n                if(write_byte === 80) this.mouse_id = 0x04;\n                this.mouse_detect_state = -1;\n                break;\n        }\n\n        dbg_log(\"mouse sample rate: \" + h(write_byte) + \", mouse id: \" + h(this.mouse_id), LOG_PS2);\n\n        if(!this.sample_rate)\n        {\n            dbg_log(\"invalid sample rate, reset to 100\", LOG_PS2);\n            this.sample_rate = 100;\n        }\n\n        this.mouse_irq();\n    }\n    else if(this.next_read_resolution)\n    {\n        this.next_read_resolution = false;\n        this.mouse_buffer.clear();\n        this.mouse_buffer.push(0xFA);\n\n        if(write_byte > 3)\n        {\n            this.resolution = 4;\n            dbg_log(\"invalid resolution, resetting to 4\", LOG_PS2);\n        }\n        else\n        {\n            this.resolution = 1 << write_byte;\n            dbg_log(\"resolution: \" + this.resolution, LOG_PS2);\n        }\n        this.mouse_irq();\n    }\n    else if(this.next_read_led)\n    {\n        // nope\n        this.next_read_led = false;\n        this.kbd_buffer.push(0xFA);\n        this.kbd_irq();\n    }\n    else if(this.next_handle_scan_code_set)\n    {\n        this.next_handle_scan_code_set = false;\n\n        this.kbd_buffer.push(0xFA);\n        this.kbd_irq();\n\n        if(write_byte)\n        {\n            // set scan code set\n        }\n        else\n        {\n            this.kbd_buffer.push(1);\n        }\n    }\n    else if(this.next_read_rate)\n    {\n        // nope\n        this.next_read_rate = false;\n        this.kbd_buffer.push(0xFA);\n        this.kbd_irq();\n    }\n    else if(this.next_is_mouse_command)\n    {\n        this.next_is_mouse_command = false;\n        dbg_log(\"Port 60 data register write: \" + h(write_byte), LOG_PS2);\n\n        if(!this.have_mouse)\n        {\n            return;\n        }\n\n        // send ack\n        this.kbd_buffer.clear();\n        this.mouse_buffer.clear();\n        this.mouse_buffer.push(0xFA);\n\n        switch(write_byte)\n        {\n        case 0xE6:\n            // set scaling to 1:1\n            dbg_log(\"Scaling 1:1\", LOG_PS2);\n            this.scaling2 = false;\n            break;\n        case 0xE7:\n            // set scaling to 2:1\n            dbg_log(\"Scaling 2:1\", LOG_PS2);\n            this.scaling2 = true;\n            break;\n        case 0xE8:\n            // set mouse resolution\n            this.next_read_resolution = true;\n            break;\n        case 0xE9:\n            // status request - send one packet\n            this.send_mouse_packet(0, 0);\n            break;\n        case 0xEB:\n            // request single packet\n            dbg_log(\"unimplemented request single packet\", LOG_PS2);\n            this.send_mouse_packet(0, 0);\n            break;\n        case 0xF2:\n            //  MouseID Byte\n            dbg_log(\"required id: \" + h(this.mouse_id), LOG_PS2);\n            this.mouse_buffer.push(this.mouse_id);\n\n            this.mouse_clicks = this.mouse_delta_x = this.mouse_delta_y = 0;\n            // this.send_mouse_packet(0, 0);\n            this.raise_irq();\n            break;\n        case 0xF3:\n            // sample rate\n            this.next_read_sample = true;\n            break;\n        case 0xF4:\n            // enable streaming\n            this.enable_mouse_stream = true;\n            this.use_mouse = true;\n            this.bus.send(\"mouse-enable\", true);\n\n            this.mouse_clicks = this.mouse_delta_x = this.mouse_delta_y = 0;\n            break;\n        case 0xF5:\n            // disable streaming\n            this.enable_mouse_stream = false;\n            break;\n        case 0xF6:\n            // set defaults\n            this.enable_mouse_stream = false;\n            this.sample_rate = 100;\n            this.scaling2 = false;\n            this.resolution = 4;\n            break;\n        case 0xFF:\n            // reset, send completion code\n            dbg_log(\"Mouse reset\", LOG_PS2);\n            this.mouse_buffer.push(0xAA);\n            this.mouse_buffer.push(0);\n\n            this.use_mouse = true;\n            this.bus.send(\"mouse-enable\", true);\n\n            this.enable_mouse_stream = false;\n            this.sample_rate = 100;\n            this.scaling2 = false;\n            this.resolution = 4;\n\n            if(!this.mouse_reset_workaround)\n            {\n                this.mouse_id = 0x00;\n            }\n\n            this.mouse_clicks = this.mouse_delta_x = this.mouse_delta_y = 0;\n            break;\n\n        default:\n            dbg_log(\"Unimplemented mouse command: \" + h(write_byte), LOG_PS2);\n        }\n\n        this.mouse_irq();\n    }\n    else if(this.read_controller_output_port)\n    {\n        this.read_controller_output_port = false;\n        this.controller_output_port = write_byte;\n        // If we ever want to implement A20 masking, here is where\n        // we should turn the masking off if the second bit is on\n    }\n    else\n    {\n        dbg_log(\"Port 60 data register write: \" + h(write_byte), LOG_PS2);\n\n        // send ack\n        this.mouse_buffer.clear();\n        this.kbd_buffer.clear();\n        this.kbd_buffer.push(0xFA);\n\n        switch(write_byte)\n        {\n        case 0xED:\n            this.next_read_led = true;\n            break;\n        case 0xF0:\n            // get/set scan code set\n            this.next_handle_scan_code_set = true;\n            break;\n        case 0xF2:\n            // identify\n            this.kbd_buffer.push(0xAB);\n            this.kbd_buffer.push(0x83);\n            break;\n        case 0xF3:\n            //  Set typematic rate and delay\n            this.next_read_rate = true;\n            break;\n        case 0xF4:\n            // enable scanning\n            dbg_log(\"kbd enable scanning\", LOG_PS2);\n            this.enable_keyboard_stream = true;\n            break;\n        case 0xF5:\n            // disable scanning\n            dbg_log(\"kbd disable scanning\", LOG_PS2);\n            this.enable_keyboard_stream = false;\n            break;\n        case 0xF6:\n            // reset defaults\n            //this.enable_keyboard_stream = false;\n            break;\n        case 0xFF:\n            this.kbd_buffer.clear();\n            this.kbd_buffer.push(0xFA);\n            this.kbd_buffer.push(0xAA);\n            this.kbd_buffer.push(0);\n            break;\n        default:\n            dbg_log(\"Unimplemented keyboard command: \" + h(write_byte), LOG_PS2);\n        }\n\n        this.kbd_irq();\n    }\n};\n\nPS2.prototype.port64_write = function(write_byte)\n{\n    dbg_log(\"port 64 write: \" + h(write_byte), LOG_PS2);\n\n    switch(write_byte)\n    {\n    case 0x20:\n        this.kbd_buffer.clear();\n        this.mouse_buffer.clear();\n        this.kbd_buffer.push(this.command_register);\n        this.kbd_irq();\n        break;\n    case 0x60:\n        this.read_command_register = true;\n        break;\n    case 0xD1:\n        this.read_controller_output_port = true;\n        break;\n    case 0xD3:\n        this.read_output_register = true;\n        break;\n    case 0xD4:\n        this.next_is_mouse_command = true;\n        break;\n    case 0xA7:\n        // Disable second port\n        dbg_log(\"Disable second port\", LOG_PS2);\n        this.command_register |= 0x20;\n        break;\n    case 0xA8:\n        // Enable second port\n        dbg_log(\"Enable second port\", LOG_PS2);\n        this.command_register &= ~0x20;\n        break;\n    case 0xA9:\n        // test second ps/2 port\n        this.kbd_buffer.clear();\n        this.mouse_buffer.clear();\n        this.kbd_buffer.push(0);\n        this.kbd_irq();\n        break;\n    case 0xAA:\n        this.kbd_buffer.clear();\n        this.mouse_buffer.clear();\n        this.kbd_buffer.push(0x55);\n        this.kbd_irq();\n        break;\n    case 0xAB:\n        // Test first PS/2 port\n        this.kbd_buffer.clear();\n        this.mouse_buffer.clear();\n        this.kbd_buffer.push(0);\n        this.kbd_irq();\n        break;\n    case 0xAD:\n        // Disable Keyboard\n        dbg_log(\"Disable Keyboard\", LOG_PS2);\n        this.command_register |= 0x10;\n        break;\n    case 0xAE:\n        // Enable Keyboard\n        dbg_log(\"Enable Keyboard\", LOG_PS2);\n        this.command_register &= ~0x10;\n        break;\n    case 0xFE:\n        dbg_log(\"CPU reboot via PS2\");\n        this.cpu.reboot_internal();\n        break;\n    default:\n        dbg_log(\"port 64: Unimplemented command byte: \" + h(write_byte), LOG_PS2);\n    }\n};\n"
  },
  {
    "path": "src/rtc.js",
    "content": "import { v86 } from \"./main.js\";\nimport { LOG_RTC } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { DMA } from \"./dma.js\";\n\n\nexport const CMOS_RTC_SECONDS = 0x00;\nexport const CMOS_RTC_SECONDS_ALARM = 0x01;\nexport const CMOS_RTC_MINUTES = 0x02;\nexport const CMOS_RTC_MINUTES_ALARM = 0x03;\nexport const CMOS_RTC_HOURS = 0x04;\nexport const CMOS_RTC_HOURS_ALARM = 0x05;\nexport const CMOS_RTC_DAY_WEEK = 0x06;\nexport const CMOS_RTC_DAY_MONTH = 0x07;\nexport const CMOS_RTC_MONTH = 0x08;\nexport const CMOS_RTC_YEAR = 0x09;\nexport const CMOS_STATUS_A = 0x0a;\nexport const CMOS_STATUS_B = 0x0b;\nexport const CMOS_STATUS_C = 0x0c;\nexport const CMOS_STATUS_D = 0x0d;\nexport const CMOS_DIAG_STATUS = 0x0e;\nexport const CMOS_RESET_CODE = 0x0f;\n\nexport const CMOS_FLOPPY_DRIVE_TYPE = 0x10;\nexport const CMOS_DISK_DATA = 0x12;\nexport const CMOS_EQUIPMENT_INFO = 0x14;\nexport const CMOS_MEM_BASE_LOW = 0x15;\nexport const CMOS_MEM_BASE_HIGH = 0x16;\nexport const CMOS_MEM_OLD_EXT_LOW = 0x17;\nexport const CMOS_MEM_OLD_EXT_HIGH = 0x18;\nexport const CMOS_DISK_DRIVE1_TYPE = 0x19;\nexport const CMOS_DISK_DRIVE2_TYPE = 0x1a;\nexport const CMOS_DISK_DRIVE1_CYL = 0x1b;\nexport const CMOS_DISK_DRIVE2_CYL = 0x24;\nexport const CMOS_MEM_EXTMEM_LOW = 0x30;\nexport const CMOS_MEM_EXTMEM_HIGH = 0x31;\nexport const CMOS_CENTURY = 0x32;\nexport const CMOS_MEM_EXTMEM2_LOW = 0x34;\nexport const CMOS_MEM_EXTMEM2_HIGH = 0x35;\nexport const CMOS_CENTURY2 = 0x37;\nexport const CMOS_BIOS_BOOTFLAG1 = 0x38;\nexport const CMOS_BIOS_DISKTRANSFLAG = 0x39;\nexport const CMOS_BIOS_BOOTFLAG2 = 0x3d;\nexport const CMOS_MEM_HIGHMEM_LOW = 0x5b;\nexport const CMOS_MEM_HIGHMEM_MID = 0x5c;\nexport const CMOS_MEM_HIGHMEM_HIGH = 0x5d;\nexport const CMOS_BIOS_SMP_COUNT = 0x5f;\n\n// see CPU.prototype.fill_cmos\nexport const BOOT_ORDER_CD_FIRST = 0x123;\nexport const BOOT_ORDER_HD_FIRST = 0x312;\nexport const BOOT_ORDER_FD_FIRST = 0x321;\n\n/**\n * RTC (real time clock) and CMOS\n * @constructor\n * @param {CPU} cpu\n */\nexport function RTC(cpu)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    this.cmos_index = 0;\n    this.cmos_data = new Uint8Array(128);\n\n    // used for cmos entries\n    this.rtc_time = Date.now();\n    this.last_update = this.rtc_time;\n\n    // used for periodic interrupt\n    this.next_interrupt = 0;\n\n    // next alarm interrupt\n    this.next_interrupt_alarm = 0;\n\n    this.periodic_interrupt = false;\n\n    // corresponds to default value for cmos_a\n    this.periodic_interrupt_time = 1000 / 1024;\n\n    this.cmos_a = 0x26;\n    this.cmos_b = 2;\n    this.cmos_c = 0;\n\n    this.cmos_diag_status = 0;\n\n    this.nmi_disabled = 0;\n\n    this.update_interrupt = false;\n    this.update_interrupt_time = 0;\n\n    cpu.io.register_write(0x70, this, function(out_byte)\n    {\n        this.cmos_index = out_byte & 0x7F;\n        this.nmi_disabled = out_byte >> 7;\n    });\n\n    cpu.io.register_write(0x71, this, this.cmos_port_write);\n    cpu.io.register_read(0x71, this, this.cmos_port_read);\n}\n\nRTC.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.cmos_index;\n    state[1] = this.cmos_data;\n    state[2] = this.rtc_time;\n    state[3] = this.last_update;\n    state[4] = this.next_interrupt;\n    state[5] = this.next_interrupt_alarm;\n    state[6] = this.periodic_interrupt;\n    state[7] = this.periodic_interrupt_time;\n    state[8] = this.cmos_a;\n    state[9] = this.cmos_b;\n    state[10] = this.cmos_c;\n    state[11] = this.nmi_disabled;\n    state[12] = this.update_interrupt;\n    state[13] = this.update_interrupt_time;\n    state[14] = this.cmos_diag_status;\n\n    return state;\n};\n\nRTC.prototype.set_state = function(state)\n{\n    this.cmos_index = state[0];\n    this.cmos_data = state[1];\n    this.rtc_time = state[2];\n    this.last_update = state[3];\n    this.next_interrupt = state[4];\n    this.next_interrupt_alarm = state[5];\n    this.periodic_interrupt = state[6];\n    this.periodic_interrupt_time = state[7];\n    this.cmos_a = state[8];\n    this.cmos_b = state[9];\n    this.cmos_c = state[10];\n    this.nmi_disabled = state[11];\n    this.update_interrupt = state[12] || false;\n    this.update_interrupt_time = state[13] || 0;\n    this.cmos_diag_status = state[14] || 0;\n};\n\nRTC.prototype.timer = function(time, legacy_mode)\n{\n    time = Date.now(); // XXX\n    this.rtc_time += time - this.last_update;\n    this.last_update = time;\n\n    if(this.periodic_interrupt && this.next_interrupt < time)\n    {\n        this.cpu.device_raise_irq(8);\n        this.cmos_c |= 1 << 6 | 1 << 7;\n\n        this.next_interrupt += this.periodic_interrupt_time *\n                Math.ceil((time - this.next_interrupt) / this.periodic_interrupt_time);\n    }\n    else if(this.next_interrupt_alarm && this.next_interrupt_alarm < time)\n    {\n        this.cpu.device_raise_irq(8);\n        this.cmos_c |= 1 << 5 | 1 << 7;\n\n        this.next_interrupt_alarm = 0;\n    }\n    else if(this.update_interrupt && this.update_interrupt_time < time)\n    {\n        this.cpu.device_raise_irq(8);\n        this.cmos_c |= 1 << 4 | 1 << 7;\n\n        this.update_interrupt_time = time + 1000; // 1 second\n    }\n\n    let t = 100;\n\n    if(this.periodic_interrupt && this.next_interrupt)\n    {\n        t = Math.min(t, Math.max(0, this.next_interrupt - time));\n    }\n    if(this.next_interrupt_alarm)\n    {\n        t = Math.min(t, Math.max(0, this.next_interrupt_alarm - time));\n    }\n    if(this.update_interrupt)\n    {\n        t = Math.min(t, Math.max(0, this.update_interrupt_time - time));\n    }\n\n    return t;\n};\n\nRTC.prototype.bcd_pack = function(n)\n{\n    var i = 0,\n        result = 0,\n        digit;\n\n    while(n)\n    {\n        digit = n % 10;\n\n        result |= digit << (4 * i);\n        i++;\n        n = (n - digit) / 10;\n    }\n\n    return result;\n};\n\nRTC.prototype.bcd_unpack = function(n)\n{\n    const low = n & 0xF;\n    const high = n >> 4 & 0xF;\n\n    dbg_assert(n < 0x100);\n    dbg_assert(low < 10);\n    dbg_assert(high < 10);\n\n    return low + 10 * high;\n};\n\nRTC.prototype.encode_time = function(t)\n{\n    if(this.cmos_b & 4)\n    {\n        // binary mode\n        return t;\n    }\n    else\n    {\n        return this.bcd_pack(t);\n    }\n};\n\nRTC.prototype.decode_time = function(t)\n{\n    if(this.cmos_b & 4)\n    {\n        // binary mode\n        return t;\n    }\n    else\n    {\n        return this.bcd_unpack(t);\n    }\n};\n\n// TODO\n// - interrupt on update\n// - countdown\n// - letting bios/os set values\n// (none of these are used by seabios or the OSes we're\n// currently testing)\nRTC.prototype.cmos_port_read = function()\n{\n    var index = this.cmos_index;\n\n    //this.cmos_index = 0xD;\n\n    switch(index)\n    {\n        case CMOS_RTC_SECONDS:\n            dbg_log(\"read second: \" + h(this.encode_time(new Date(this.rtc_time).getUTCSeconds())), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCSeconds());\n        case CMOS_RTC_MINUTES:\n            dbg_log(\"read minute: \" + h(this.encode_time(new Date(this.rtc_time).getUTCMinutes())), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCMinutes());\n        case CMOS_RTC_HOURS:\n            dbg_log(\"read hour: \" + h(this.encode_time(new Date(this.rtc_time).getUTCHours())), LOG_RTC);\n            // TODO: 12 hour mode\n            return this.encode_time(new Date(this.rtc_time).getUTCHours());\n        case CMOS_RTC_DAY_WEEK:\n            dbg_log(\"read day: \" + h(this.encode_time(new Date(this.rtc_time).getUTCDay() + 1)), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCDay() + 1);\n        case CMOS_RTC_DAY_MONTH:\n            dbg_log(\"read day of month: \" + h(this.encode_time(new Date(this.rtc_time).getUTCDate())), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCDate());\n        case CMOS_RTC_MONTH:\n            dbg_log(\"read month: \" + h(this.encode_time(new Date(this.rtc_time).getUTCMonth() + 1)), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCMonth() + 1);\n        case CMOS_RTC_YEAR:\n            dbg_log(\"read year: \" + h(this.encode_time(new Date(this.rtc_time).getUTCFullYear() % 100)), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCFullYear() % 100);\n\n        case CMOS_STATUS_A:\n            if(v86.microtick() % 1000 >= 999)\n            {\n                // Set update-in-progress for one millisecond every second (we\n                // may not have precision higher than that in browser\n                // environments)\n                return this.cmos_a | 0x80;\n            }\n            return this.cmos_a;\n        case CMOS_STATUS_B:\n            //dbg_log(\"cmos read from index \" + h(index));\n            return this.cmos_b;\n\n        case CMOS_STATUS_C:\n            // It is important to know that upon a IRQ 8, Status Register C\n            // will contain a bitmask telling which interrupt happened.\n            // What is important is that if register C is not read after an\n            // IRQ 8, then the interrupt will not happen again.\n            this.cpu.device_lower_irq(8);\n\n            dbg_log(\"cmos reg C read\", LOG_RTC);\n            // Missing IRQF flag\n            //return cmos_b & 0x70;\n            var c = this.cmos_c;\n\n            this.cmos_c &= ~0xF0;\n\n            return c;\n\n        case CMOS_STATUS_D:\n            return 1 << 7; // CMOS battery charged\n\n        case CMOS_DIAG_STATUS:\n            dbg_log(\"cmos diagnostic status read\", LOG_RTC);\n            return this.cmos_diag_status;\n\n        case CMOS_CENTURY:\n        case CMOS_CENTURY2:\n            dbg_log(\"read century: \" + h(this.encode_time(new Date(this.rtc_time).getUTCFullYear() / 100 | 0)), LOG_RTC);\n            return this.encode_time(new Date(this.rtc_time).getUTCFullYear() / 100 | 0);\n\n        default:\n            dbg_log(\"cmos read from index \" + h(index), LOG_RTC);\n            return this.cmos_data[this.cmos_index];\n    }\n};\n\nRTC.prototype.cmos_port_write = function(data_byte)\n{\n    switch(this.cmos_index)\n    {\n        case 0xA:\n            this.cmos_a = data_byte & 0x7F;\n            this.periodic_interrupt_time = 1000 / (32768 >> (this.cmos_a & 0xF) - 1);\n\n            dbg_log(\"Periodic interrupt, a=\" + h(this.cmos_a, 2) + \" t=\" + this.periodic_interrupt_time , LOG_RTC);\n            break;\n        case 0xB:\n            this.cmos_b = data_byte;\n            if(this.cmos_b & 0x80)\n            {\n                // remove update interrupt flag\n                this.cmos_b &= 0xEF;\n            }\n            if(this.cmos_b & 0x40)\n            {\n                this.next_interrupt = Date.now();\n            }\n\n            if(this.cmos_b & 0x20)\n            {\n                const now = new Date();\n\n                const seconds = this.decode_time(this.cmos_data[CMOS_RTC_SECONDS_ALARM]);\n                const minutes = this.decode_time(this.cmos_data[CMOS_RTC_MINUTES_ALARM]);\n                const hours = this.decode_time(this.cmos_data[CMOS_RTC_HOURS_ALARM]);\n\n                const alarm_date = new Date(Date.UTC(\n                    now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),\n                    hours, minutes, seconds\n                ));\n\n                const ms_from_now = alarm_date - now;\n                dbg_log(\"RTC alarm scheduled for \" + alarm_date +\n                        \" hh:mm:ss=\" + hours + \":\" + minutes + \":\" + seconds +\n                        \" ms_from_now=\" + ms_from_now, LOG_RTC);\n\n                this.next_interrupt_alarm = +alarm_date;\n            }\n\n            if(this.cmos_b & 0x10)\n            {\n                dbg_log(\"update interrupt\", LOG_RTC);\n                this.update_interrupt_time = Date.now();\n            }\n\n            dbg_log(\"cmos b=\" + h(this.cmos_b, 2), LOG_RTC);\n            break;\n\n        case CMOS_DIAG_STATUS:\n            this.cmos_diag_status = data_byte;\n            break;\n\n        case CMOS_RTC_SECONDS_ALARM:\n        case CMOS_RTC_MINUTES_ALARM:\n        case CMOS_RTC_HOURS_ALARM:\n            this.cmos_write(this.cmos_index, data_byte);\n            break;\n\n        default:\n            dbg_log(\"cmos write index \" + h(this.cmos_index) + \": \" + h(data_byte), LOG_RTC);\n    }\n\n    this.update_interrupt = (this.cmos_b & 0x10) === 0x10 && (this.cmos_a & 0xF) > 0;\n    this.periodic_interrupt = (this.cmos_b & 0x40) === 0x40 && (this.cmos_a & 0xF) > 0;\n};\n\n/**\n * @param {number} index\n */\nRTC.prototype.cmos_read = function(index)\n{\n    dbg_assert(index < 128);\n    return this.cmos_data[index];\n};\n\n/**\n * @param {number} index\n * @param {number} value\n */\nRTC.prototype.cmos_write = function(index, value)\n{\n    dbg_log(\"cmos \" + h(index) + \" <- \" + h(value), LOG_RTC);\n    dbg_assert(index < 128);\n    this.cmos_data[index] = value;\n};\n"
  },
  {
    "path": "src/rust/analysis.rs",
    "content": "#![allow(non_snake_case)]\n\nuse crate::cpu_context::CpuContext;\nuse crate::gen;\nuse crate::modrm;\nuse crate::prefix::{PREFIX_66, PREFIX_67, PREFIX_F2, PREFIX_F3};\nuse crate::regs::{CS, DS, ES, FS, GS, SS};\n\n#[derive(PartialEq, Eq)]\npub enum AnalysisType {\n    Normal,\n    BlockBoundary,\n    Jump {\n        offset: i32,\n        is_32: bool,\n        condition: Option<u8>,\n    },\n    STI,\n}\n\npub struct Analysis {\n    pub no_next_instruction: bool,\n    pub absolute_jump: bool,\n    pub ty: AnalysisType,\n}\n\npub fn analyze_step(mut cpu: &mut CpuContext) -> Analysis {\n    let mut analysis = Analysis {\n        no_next_instruction: false,\n        absolute_jump: false,\n        ty: AnalysisType::Normal,\n    };\n    cpu.prefixes = 0;\n    let opcode = cpu.read_imm8() as u32 | (cpu.osize_32() as u32) << 8;\n    gen::analyzer::analyzer(opcode, &mut cpu, &mut analysis);\n    analysis\n}\n\npub fn analyze_step_handle_prefix(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    gen::analyzer::analyzer(\n        cpu.read_imm8() as u32 | (cpu.osize_32() as u32) << 8,\n        cpu,\n        analysis,\n    )\n}\npub fn analyze_step_handle_segment_prefix(\n    segment: u32,\n    cpu: &mut CpuContext,\n    analysis: &mut Analysis,\n) {\n    dbg_assert!(segment <= 5);\n    cpu.prefixes |= segment as u8 + 1;\n    analyze_step_handle_prefix(cpu, analysis)\n}\n\npub fn instr16_0F_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    gen::analyzer0f::analyzer(cpu.read_imm8() as u32, cpu, analysis)\n}\npub fn instr32_0F_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    gen::analyzer0f::analyzer(cpu.read_imm8() as u32 | 0x100, cpu, analysis)\n}\npub fn instr_26_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    analyze_step_handle_segment_prefix(ES, cpu, analysis)\n}\npub fn instr_2E_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    analyze_step_handle_segment_prefix(CS, cpu, analysis)\n}\npub fn instr_36_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    analyze_step_handle_segment_prefix(SS, cpu, analysis)\n}\npub fn instr_3E_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    analyze_step_handle_segment_prefix(DS, cpu, analysis)\n}\npub fn instr_64_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    analyze_step_handle_segment_prefix(FS, cpu, analysis)\n}\npub fn instr_65_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    analyze_step_handle_segment_prefix(GS, cpu, analysis)\n}\npub fn instr_66_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    cpu.prefixes |= PREFIX_66;\n    analyze_step_handle_prefix(cpu, analysis)\n}\npub fn instr_67_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    cpu.prefixes |= PREFIX_67;\n    analyze_step_handle_prefix(cpu, analysis)\n}\npub fn instr_F0_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    // lock: Ignored\n    analyze_step_handle_prefix(cpu, analysis)\n}\npub fn instr_F2_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    cpu.prefixes |= PREFIX_F2;\n    analyze_step_handle_prefix(cpu, analysis)\n}\npub fn instr_F3_analyze(cpu: &mut CpuContext, analysis: &mut Analysis) {\n    cpu.prefixes |= PREFIX_F3;\n    analyze_step_handle_prefix(cpu, analysis)\n}\n\npub fn modrm_analyze(ctx: &mut CpuContext, modrm_byte: u8) { modrm::skip(ctx, modrm_byte); }\n"
  },
  {
    "path": "src/rust/codegen.rs",
    "content": "use crate::cpu::cpu::{\n    tlb_data, FLAG_CARRY, FLAG_OVERFLOW, FLAG_SIGN, FLAG_ZERO, OPSIZE_16, OPSIZE_32, OPSIZE_8,\n    TLB_GLOBAL, TLB_HAS_CODE, TLB_NO_USER, TLB_READONLY, TLB_VALID,\n};\nuse crate::cpu::global_pointers;\nuse crate::cpu::memory;\nuse crate::jit::{Instruction, InstructionOperand, InstructionOperandDest, JitContext};\nuse crate::modrm;\nuse crate::modrm::ModrmByte;\nuse crate::opstats;\nuse crate::profiler;\nuse crate::regs;\nuse crate::wasmgen::wasm_builder::{WasmBuilder, WasmLocal, WasmLocalI64};\n\npub fn gen_add_cs_offset(ctx: &mut JitContext) {\n    if !ctx.cpu.has_flat_segmentation() {\n        ctx.builder\n            .load_fixed_i32(global_pointers::get_seg_offset(regs::CS));\n        ctx.builder.add_i32();\n    }\n}\n\npub fn gen_get_eip(builder: &mut WasmBuilder) {\n    builder.load_fixed_i32(global_pointers::instruction_pointer as u32);\n}\n\npub fn gen_set_eip_to_after_current_instruction(ctx: &mut JitContext) {\n    ctx.builder\n        .const_i32(global_pointers::instruction_pointer as i32);\n    gen_get_eip(ctx.builder);\n    ctx.builder.const_i32(!0xFFF);\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(ctx.cpu.eip as i32 & 0xFFF);\n    ctx.builder.or_i32();\n    ctx.builder.store_aligned_i32(0);\n}\n\npub fn gen_set_previous_eip_offset_from_eip_with_low_bits(\n    builder: &mut WasmBuilder,\n    low_bits: i32,\n) {\n    // previous_ip = instruction_pointer & ~0xFFF | low_bits;\n    dbg_assert!(low_bits & !0xFFF == 0);\n    builder.const_i32(global_pointers::previous_ip as i32);\n    gen_get_eip(builder);\n    builder.const_i32(!0xFFF);\n    builder.and_i32();\n    builder.const_i32(low_bits);\n    builder.or_i32();\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_set_eip_low_bits(builder: &mut WasmBuilder, low_bits: i32) {\n    // instruction_pointer = instruction_pointer & ~0xFFF | low_bits;\n    dbg_assert!(low_bits & !0xFFF == 0);\n    builder.const_i32(global_pointers::instruction_pointer as i32);\n    gen_get_eip(builder);\n    builder.const_i32(!0xFFF);\n    builder.and_i32();\n    builder.const_i32(low_bits);\n    builder.or_i32();\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_set_eip_low_bits_and_jump_rel32(builder: &mut WasmBuilder, low_bits: i32, n: i32) {\n    // instruction_pointer = (instruction_pointer & ~0xFFF | low_bits) + n;\n    dbg_assert!(low_bits & !0xFFF == 0);\n    builder.const_i32(global_pointers::instruction_pointer as i32);\n    gen_get_eip(builder);\n    builder.const_i32(!0xFFF);\n    builder.and_i32();\n    builder.const_i32(low_bits);\n    builder.or_i32();\n    if n != 0 {\n        builder.const_i32(n);\n        builder.add_i32();\n    }\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_relative_jump(builder: &mut WasmBuilder, n: i32) {\n    // add n to instruction_pointer\n    if n != 0 {\n        builder.const_i32(global_pointers::instruction_pointer as i32);\n        gen_get_eip(builder);\n        builder.const_i32(n);\n        builder.add_i32();\n        builder.store_aligned_i32(0);\n    }\n}\n\npub fn gen_page_switch_check(\n    ctx: &mut JitContext,\n    next_block_addr: u32,\n    last_instruction_addr: u32,\n) {\n    // After switching a page while in jitted code, check if the page mapping still holds\n\n    gen_get_eip(ctx.builder);\n    let address_local = ctx.builder.set_new_local();\n    gen_get_phys_eip_plus_mem(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n\n    ctx.builder\n        .const_i32(next_block_addr as i32 + unsafe { memory::mem8 } as i32);\n    ctx.builder.ne_i32();\n\n    if cfg!(debug_assertions) {\n        ctx.builder.if_void();\n        gen_profiler_stat_increment(ctx.builder, profiler::stat::FAILED_PAGE_CHANGE);\n        gen_debug_track_jit_exit(ctx.builder, last_instruction_addr);\n        ctx.builder.br(ctx.exit_label);\n        ctx.builder.block_end();\n    }\n    else {\n        ctx.builder.br_if(ctx.exit_label);\n    }\n}\n\npub fn gen_update_instruction_counter(ctx: &mut JitContext) {\n    ctx.builder\n        .const_i32(global_pointers::instruction_counter as i32);\n    ctx.builder\n        .load_fixed_i32(global_pointers::instruction_counter as u32);\n    ctx.builder.get_local(&ctx.instruction_counter);\n    ctx.builder.add_i32();\n    ctx.builder.store_aligned_i32(0);\n}\n\npub fn gen_get_reg8(ctx: &mut JitContext, r: u32) {\n    match r {\n        regs::AL | regs::CL | regs::DL | regs::BL => {\n            ctx.builder.get_local(&ctx.register_locals[r as usize]);\n            ctx.builder.const_i32(0xFF);\n            ctx.builder.and_i32();\n        },\n        regs::AH | regs::CH | regs::DH | regs::BH => {\n            ctx.builder\n                .get_local(&ctx.register_locals[(r - 4) as usize]);\n            ctx.builder.const_i32(8);\n            ctx.builder.shr_u_i32();\n            ctx.builder.const_i32(0xFF);\n            ctx.builder.and_i32();\n        },\n        _ => assert!(false),\n    }\n}\n\n/// Return a new local referencing one of the 8 bit registers or a direct reference to one of the\n/// register locals. Higher bits might be garbage (suitable for gen_cmp8 etc.). Must be freed with\n/// gen_free_reg8_or_alias.\npub fn gen_get_reg8_or_alias_to_reg32(ctx: &mut JitContext, r: u32) -> WasmLocal {\n    match r {\n        regs::AL | regs::CL | regs::DL | regs::BL => ctx.register_locals[r as usize].unsafe_clone(),\n        regs::AH | regs::CH | regs::DH | regs::BH => {\n            ctx.builder\n                .get_local(&ctx.register_locals[(r - 4) as usize]);\n            ctx.builder.const_i32(8);\n            ctx.builder.shr_u_i32();\n            ctx.builder.set_new_local()\n        },\n        _ => panic!(),\n    }\n}\n\npub fn gen_free_reg8_or_alias(ctx: &mut JitContext, r: u32, local: WasmLocal) {\n    match r {\n        regs::AL | regs::CL | regs::DL | regs::BL => {},\n        regs::AH | regs::CH | regs::DH | regs::BH => ctx.builder.free_local(local),\n        _ => panic!(),\n    }\n}\n\npub fn gen_get_reg16(ctx: &mut JitContext, r: u32) {\n    ctx.builder.get_local(&ctx.register_locals[r as usize]);\n    ctx.builder.const_i32(0xFFFF);\n    ctx.builder.and_i32();\n}\n\npub fn gen_get_reg32(ctx: &mut JitContext, r: u32) {\n    ctx.builder.get_local(&ctx.register_locals[r as usize]);\n}\n\npub fn gen_set_reg8(ctx: &mut JitContext, r: u32) {\n    match r {\n        regs::AL | regs::CL | regs::DL | regs::BL => {\n            // reg32[r] = stack_value & 0xFF | reg32[r] & ~0xFF\n            ctx.builder.const_i32(0xFF);\n            ctx.builder.and_i32();\n\n            ctx.builder.get_local(&ctx.register_locals[r as usize]);\n            ctx.builder.const_i32(!0xFF);\n            ctx.builder.and_i32();\n\n            ctx.builder.or_i32();\n            ctx.builder.set_local(&ctx.register_locals[r as usize]);\n        },\n        regs::AH | regs::CH | regs::DH | regs::BH => {\n            // reg32[r] = stack_value << 8 & 0xFF00 | reg32[r] & ~0xFF00\n            ctx.builder.const_i32(8);\n            ctx.builder.shl_i32();\n            ctx.builder.const_i32(0xFF00);\n            ctx.builder.and_i32();\n\n            ctx.builder\n                .get_local(&ctx.register_locals[(r - 4) as usize]);\n            ctx.builder.const_i32(!0xFF00);\n            ctx.builder.and_i32();\n\n            ctx.builder.or_i32();\n            ctx.builder\n                .set_local(&ctx.register_locals[(r - 4) as usize]);\n        },\n        _ => assert!(false),\n    }\n}\n\npub fn gen_set_reg8_unmasked(ctx: &mut JitContext, r: u32) {\n    if cfg!(debug_assertions) {\n        let val = ctx.builder.set_new_local();\n        ctx.builder.get_local(&val);\n        ctx.builder.const_i32(!0xFF);\n        ctx.builder.and_i32();\n        ctx.builder.if_void();\n        ctx.builder.unreachable();\n        ctx.builder.block_end();\n        ctx.builder.get_local(&val);\n        ctx.builder.free_local(val);\n    }\n\n    match r {\n        regs::AL | regs::CL | regs::DL | regs::BL => {\n            // reg32[r] = stack_value | reg32[r] & ~0xFF\n            ctx.builder.get_local(&ctx.register_locals[r as usize]);\n            ctx.builder.const_i32(!0xFF);\n            ctx.builder.and_i32();\n\n            ctx.builder.or_i32();\n            ctx.builder.set_local(&ctx.register_locals[r as usize]);\n        },\n        regs::AH | regs::CH | regs::DH | regs::BH => {\n            // reg32[r] = stack_value << 8 | reg32[r] & ~0xFF00\n            ctx.builder.const_i32(8);\n            ctx.builder.shl_i32();\n            ctx.builder.const_i32(0xFF00);\n            ctx.builder.and_i32();\n\n            ctx.builder\n                .get_local(&ctx.register_locals[(r - 4) as usize]);\n            ctx.builder.const_i32(!0xFF00);\n            ctx.builder.and_i32();\n\n            ctx.builder.or_i32();\n            ctx.builder\n                .set_local(&ctx.register_locals[(r - 4) as usize]);\n        },\n        _ => assert!(false),\n    }\n}\n\npub fn gen_set_reg16(ctx: &mut JitContext, r: u32) {\n    gen_set_reg16_local(ctx.builder, &ctx.register_locals[r as usize]);\n}\n\npub fn gen_set_reg16_unmasked(ctx: &mut JitContext, r: u32) {\n    if cfg!(debug_assertions) {\n        let val = ctx.builder.set_new_local();\n        ctx.builder.get_local(&val);\n        ctx.builder.const_i32(!0xFFFF);\n        ctx.builder.and_i32();\n        ctx.builder.if_void();\n        ctx.builder.unreachable();\n        ctx.builder.block_end();\n        ctx.builder.get_local(&val);\n        ctx.builder.free_local(val);\n    }\n\n    ctx.builder.get_local(&ctx.reg(r));\n    ctx.builder.const_i32(!0xFFFF);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n    ctx.builder.set_local(&ctx.reg(r));\n}\n\npub fn gen_set_reg16_local(builder: &mut WasmBuilder, local: &WasmLocal) {\n    // reg32[r] = v & 0xFFFF | reg32[r] & ~0xFFFF\n    builder.const_i32(0xFFFF);\n    builder.and_i32();\n    builder.get_local(local);\n    builder.const_i32(!0xFFFF);\n    builder.and_i32();\n    builder.or_i32();\n    builder.set_local(local);\n}\n\npub fn gen_set_reg32(ctx: &mut JitContext, r: u32) {\n    ctx.builder.set_local(&ctx.register_locals[r as usize]);\n}\n\npub fn decr_exc_asize(ctx: &mut JitContext) {\n    gen_get_reg32(ctx, regs::ECX);\n    ctx.builder.const_i32(1);\n    ctx.builder.sub_i32();\n    if ctx.cpu.asize_32() {\n        gen_set_reg32(ctx, regs::ECX);\n    }\n    else {\n        gen_set_reg16(ctx, regs::CX);\n    }\n}\n\npub fn gen_read_reg_xmm128_into_scratch(ctx: &mut JitContext, r: u32) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    ctx.builder.const_i32(dest as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.store_aligned_i64(0);\n\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32 + 8);\n    let dest = global_pointers::get_reg_xmm_offset(r) + 8;\n    ctx.builder.const_i32(dest as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.store_aligned_i64(0);\n}\n\npub fn gen_get_sreg(ctx: &mut JitContext, r: u32) {\n    ctx.builder\n        .load_fixed_u16(global_pointers::get_sreg_offset(r))\n}\n\npub fn gen_get_ss_offset(ctx: &mut JitContext) {\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_seg_offset(regs::SS));\n}\n\npub fn gen_get_flags(builder: &mut WasmBuilder) {\n    builder.load_fixed_i32(global_pointers::flags as u32);\n}\nfn gen_get_flags_changed(builder: &mut WasmBuilder) {\n    builder.load_fixed_i32(global_pointers::flags_changed as u32);\n}\nfn gen_get_last_result(builder: &mut WasmBuilder, previous_instruction: &Instruction) {\n    match previous_instruction {\n        Instruction::Add {\n            dest: InstructionOperandDest::WasmLocal(l),\n            opsize: OPSIZE_32,\n            ..\n        }\n        | Instruction::AdcSbb {\n            dest: InstructionOperandDest::WasmLocal(l),\n            opsize: OPSIZE_32,\n            ..\n        }\n        | Instruction::Sub {\n            dest: InstructionOperandDest::WasmLocal(l),\n            opsize: OPSIZE_32,\n            ..\n        }\n        | Instruction::Bitwise {\n            dest: InstructionOperandDest::WasmLocal(l),\n            opsize: OPSIZE_32,\n        }\n        | Instruction::NonZeroShift {\n            dest: InstructionOperandDest::WasmLocal(l),\n            opsize: OPSIZE_32,\n        } => builder.get_local(&l),\n        Instruction::Cmp {\n            dest: InstructionOperandDest::WasmLocal(l),\n            source,\n            opsize: OPSIZE_32,\n        } => {\n            if source.is_zero() {\n                builder.get_local(&l)\n            }\n            else {\n                builder.load_fixed_i32(global_pointers::last_result as u32)\n            }\n        },\n        _ => builder.load_fixed_i32(global_pointers::last_result as u32),\n    }\n}\nfn gen_get_last_op_size(builder: &mut WasmBuilder) {\n    builder.load_fixed_i32(global_pointers::last_op_size as u32);\n}\nfn gen_get_last_op1(builder: &mut WasmBuilder, previous_instruction: &Instruction) {\n    match previous_instruction {\n        Instruction::Cmp {\n            dest: InstructionOperandDest::WasmLocal(l),\n            source: _,\n            opsize: OPSIZE_32,\n        } => builder.get_local(&l),\n        _ => builder.load_fixed_i32(global_pointers::last_op1 as u32),\n    }\n}\n\npub fn gen_get_page_fault(builder: &mut WasmBuilder) {\n    builder.load_fixed_u8(global_pointers::page_fault as u32);\n}\n\n/// sign-extend a byte value on the stack and leave it on the stack\npub fn sign_extend_i8(builder: &mut WasmBuilder) {\n    builder.const_i32(24);\n    builder.shl_i32();\n    builder.const_i32(24);\n    builder.shr_s_i32();\n}\n\n/// sign-extend a two byte value on the stack and leave it on the stack\npub fn sign_extend_i16(builder: &mut WasmBuilder) {\n    builder.const_i32(16);\n    builder.shl_i32();\n    builder.const_i32(16);\n    builder.shr_s_i32();\n}\n\npub fn gen_fn0_const(builder: &mut WasmBuilder, name: &str) { builder.call_fn0(name) }\npub fn gen_fn1_const(builder: &mut WasmBuilder, name: &str, arg0: u32) {\n    builder.const_i32(arg0 as i32);\n    builder.call_fn1(name);\n}\npub fn gen_fn2_const(builder: &mut WasmBuilder, name: &str, arg0: u32, arg1: u32) {\n    builder.const_i32(arg0 as i32);\n    builder.const_i32(arg1 as i32);\n    builder.call_fn2(name);\n}\n\n// helper functions for gen/generate_jit.js\npub fn gen_modrm_fn0(builder: &mut WasmBuilder, name: &str) {\n    // generates: fn( _ )\n    builder.call_fn1(name);\n}\npub fn gen_modrm_fn1(builder: &mut WasmBuilder, name: &str, arg0: u32) {\n    // generates: fn( _, arg0 )\n    builder.const_i32(arg0 as i32);\n    builder.call_fn2(name);\n}\n\npub fn gen_modrm_resolve(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    modrm::gen(ctx, modrm_byte, 0)\n}\npub fn gen_modrm_resolve_with_local(\n    ctx: &mut JitContext,\n    modrm_byte: ModrmByte,\n    gen: &dyn Fn(&mut JitContext, &WasmLocal),\n) {\n    if let Some(r) = modrm::get_as_reg_index_if_possible(ctx, &modrm_byte) {\n        gen(ctx, &ctx.reg(r));\n    }\n    else {\n        gen_modrm_resolve(ctx, modrm_byte);\n        let address = ctx.builder.set_new_local();\n        gen(ctx, &address);\n        ctx.builder.free_local(address);\n    }\n}\npub fn gen_modrm_resolve_with_esp_offset(\n    ctx: &mut JitContext,\n    modrm_byte: ModrmByte,\n    esp_offset: i32,\n) {\n    modrm::gen(ctx, modrm_byte, esp_offset)\n}\n\npub fn gen_set_reg8_r(ctx: &mut JitContext, dest: u32, src: u32) {\n    // generates: reg8[r_dest] = reg8[r_src]\n    if src != dest {\n        gen_get_reg8(ctx, src);\n        gen_set_reg8_unmasked(ctx, dest);\n    }\n}\npub fn gen_set_reg16_r(ctx: &mut JitContext, dest: u32, src: u32) {\n    // generates: reg16[r_dest] = reg16[r_src]\n    if src != dest {\n        gen_get_reg16(ctx, src);\n        gen_set_reg16_unmasked(ctx, dest);\n    }\n}\npub fn gen_set_reg32_r(ctx: &mut JitContext, dest: u32, src: u32) {\n    // generates: reg32[r_dest] = reg32[r_src]\n    if src != dest {\n        gen_get_reg32(ctx, src);\n        gen_set_reg32(ctx, dest);\n    }\n}\n\npub fn gen_modrm_resolve_safe_read8(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read8(ctx, addr));\n}\npub fn gen_modrm_resolve_safe_read16(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read16(ctx, addr));\n}\npub fn gen_modrm_resolve_safe_read32(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read32(ctx, addr));\n}\npub fn gen_modrm_resolve_safe_read64(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read64(ctx, addr));\n}\npub fn gen_modrm_resolve_safe_read128(\n    ctx: &mut JitContext,\n    modrm_byte: ModrmByte,\n    where_to_write: u32,\n) {\n    gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        gen_safe_read128(ctx, addr, where_to_write)\n    });\n}\n\npub fn gen_safe_read8(ctx: &mut JitContext, address_local: &WasmLocal) {\n    gen_safe_read(ctx, BitSize::BYTE, address_local, None);\n}\npub fn gen_safe_read16(ctx: &mut JitContext, address_local: &WasmLocal) {\n    gen_safe_read(ctx, BitSize::WORD, address_local, None);\n}\npub fn gen_safe_read32(ctx: &mut JitContext, address_local: &WasmLocal) {\n    gen_safe_read(ctx, BitSize::DWORD, address_local, None);\n}\npub fn gen_safe_read64(ctx: &mut JitContext, address_local: &WasmLocal) {\n    gen_safe_read(ctx, BitSize::QWORD, &address_local, None);\n}\npub fn gen_safe_read128(ctx: &mut JitContext, address_local: &WasmLocal, where_to_write: u32) {\n    gen_safe_read(ctx, BitSize::DQWORD, &address_local, Some(where_to_write));\n}\n\n// only used internally for gen_safe_write\nenum GenSafeWriteValue<'a> {\n    I32(&'a WasmLocal),\n    I64(&'a WasmLocalI64),\n    TwoI64s(&'a WasmLocalI64, &'a WasmLocalI64),\n}\n\nenum GenSafeReadWriteValue {\n    I32(WasmLocal),\n    I64(WasmLocalI64),\n}\n\n#[derive(Copy, Clone, Eq, PartialEq)]\npub enum BitSize {\n    BYTE,\n    WORD,\n    DWORD,\n    QWORD,\n    DQWORD,\n}\nimpl BitSize {\n    pub fn bytes(&self) -> u32 {\n        match self {\n            BitSize::BYTE => 1,\n            BitSize::WORD => 2,\n            BitSize::DWORD => 4,\n            BitSize::QWORD => 8,\n            BitSize::DQWORD => 16,\n        }\n    }\n}\n\npub fn gen_safe_write8(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {\n    gen_safe_write(\n        ctx,\n        BitSize::BYTE,\n        address_local,\n        GenSafeWriteValue::I32(value_local),\n    )\n}\npub fn gen_safe_write16(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {\n    gen_safe_write(\n        ctx,\n        BitSize::WORD,\n        address_local,\n        GenSafeWriteValue::I32(value_local),\n    )\n}\npub fn gen_safe_write32(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {\n    gen_safe_write(\n        ctx,\n        BitSize::DWORD,\n        address_local,\n        GenSafeWriteValue::I32(value_local),\n    )\n}\npub fn gen_safe_write64(\n    ctx: &mut JitContext,\n    address_local: &WasmLocal,\n    value_local: &WasmLocalI64,\n) {\n    gen_safe_write(\n        ctx,\n        BitSize::QWORD,\n        address_local,\n        GenSafeWriteValue::I64(value_local),\n    )\n}\n\npub fn gen_safe_write128(\n    ctx: &mut JitContext,\n    address_local: &WasmLocal,\n    value_local_low: &WasmLocalI64,\n    value_local_high: &WasmLocalI64,\n) {\n    gen_safe_write(\n        ctx,\n        BitSize::DQWORD,\n        address_local,\n        GenSafeWriteValue::TwoI64s(value_local_low, value_local_high),\n    )\n}\n\nfn gen_safe_read(\n    ctx: &mut JitContext,\n    bits: BitSize,\n    address_local: &WasmLocal,\n    where_to_write: Option<u32>,\n) {\n    // Execute a virtual memory read. All slow paths (memory-mapped IO, tlb miss, page fault and\n    // read across page boundary are handled in safe_read_jit_slow\n\n    //   entry <- tlb_data[addr >> 12 << 2]\n    //   if entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes: goto fast\n    //   entry <- safe_read_jit_slow(addr, instruction_pointer)\n    //   if page_fault: goto exit-with-pagefault\n    //   fast: mem[(entry & ~0xFFF) ^ addr]\n\n    let cont = ctx.builder.block_void();\n    ctx.builder.get_local(&address_local);\n\n    ctx.builder.const_i32(12);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(2);\n    ctx.builder.shl_i32();\n\n    ctx.builder\n        .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });\n    let entry_local = ctx.builder.tee_new_local();\n\n    ctx.builder.const_i32(\n        (0xFFF\n            & !TLB_READONLY\n            & !TLB_GLOBAL\n            & !TLB_HAS_CODE\n            & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32,\n    );\n    ctx.builder.and_i32();\n\n    ctx.builder.const_i32(TLB_VALID as i32);\n    ctx.builder.eq_i32();\n\n    if bits != BitSize::BYTE {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.const_i32(0xFFF);\n        ctx.builder.and_i32();\n        ctx.builder.const_i32(0x1000 - bits.bytes() as i32);\n        ctx.builder.le_i32();\n\n        ctx.builder.and_i32();\n    }\n\n    ctx.builder.br_if(cont);\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.call_fn2(\"report_safe_read_jit_slow\");\n    }\n\n    ctx.builder.get_local(&address_local);\n    ctx.builder\n        .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.call_fn2_ret(\"safe_read8_slow_jit\");\n        },\n        BitSize::WORD => {\n            ctx.builder.call_fn2_ret(\"safe_read16_slow_jit\");\n        },\n        BitSize::DWORD => {\n            ctx.builder.call_fn2_ret(\"safe_read32s_slow_jit\");\n        },\n        BitSize::QWORD => {\n            ctx.builder.call_fn2_ret(\"safe_read64s_slow_jit\");\n        },\n        BitSize::DQWORD => {\n            ctx.builder.call_fn2_ret(\"safe_read128s_slow_jit\");\n        },\n    }\n    ctx.builder.tee_local(&entry_local);\n    ctx.builder.const_i32(1);\n    ctx.builder.and_i32();\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.if_void();\n        gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n        ctx.builder.block_end();\n\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.const_i32(1);\n        ctx.builder.and_i32();\n    }\n\n    ctx.builder.br_if(ctx.exit_with_fault_label);\n\n    ctx.builder.block_end();\n\n    gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_FAST); // XXX: Both fast and slow\n\n    ctx.builder.get_local(&entry_local);\n    ctx.builder.const_i32(!0xFFF);\n    ctx.builder.and_i32();\n    ctx.builder.get_local(&address_local);\n    ctx.builder.xor_i32();\n\n    // where_to_write is only used by dqword\n    dbg_assert!((where_to_write != None) == (bits == BitSize::DQWORD));\n\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.load_u8(0);\n        },\n        BitSize::WORD => {\n            ctx.builder.load_unaligned_u16(0);\n        },\n        BitSize::DWORD => {\n            ctx.builder.load_unaligned_i32(0);\n        },\n        BitSize::QWORD => {\n            ctx.builder.load_unaligned_i64(0);\n        },\n        BitSize::DQWORD => {\n            let where_to_write = where_to_write.unwrap();\n            let virt_address_local = ctx.builder.set_new_local();\n            ctx.builder.const_i32(0);\n            ctx.builder.get_local(&virt_address_local);\n            ctx.builder.load_unaligned_i64(0);\n            ctx.builder.store_unaligned_i64(where_to_write);\n\n            ctx.builder.const_i32(0);\n            ctx.builder.get_local(&virt_address_local);\n            ctx.builder.load_unaligned_i64(8);\n            ctx.builder.store_unaligned_i64(where_to_write + 8);\n\n            ctx.builder.free_local(virt_address_local);\n        },\n    }\n\n    ctx.builder.free_local(entry_local);\n}\n\npub fn gen_get_phys_eip_plus_mem(ctx: &mut JitContext, address_local: &WasmLocal) {\n    // Similar to gen_safe_read, but return the physical eip + memory::mem rather than reading from memory\n    // In functions that need to use this value we need to fix it by substracting memory::mem\n    // this is done in order to remove one instruction from the fast path of memory accesses (no need to add\n    // memory::mem anymore ).\n    // We need to account for this in gen_page_switch_check and we compare with next_block_addr + memory::mem8\n    // We cannot the same while processing an AbsoluteEip flow control change so there we need to fix the value\n    // by subscracting memory::mem. Overall, since AbsoluteEip is encountered less often than memory accesses so\n    // this ends up improving perf.\n    // Does not (need to) handle mapped memory\n    // XXX: Currently does not use ctx.start_of_current_instruction, but rather assumes that eip is\n    //      already correct (pointing at the current instruction)\n\n    let cont = ctx.builder.block_void();\n    ctx.builder.get_local(&address_local);\n\n    ctx.builder.const_i32(12);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(2);\n    ctx.builder.shl_i32();\n\n    ctx.builder\n        .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });\n    let entry_local = ctx.builder.tee_new_local();\n\n    ctx.builder.const_i32(\n        (0xFFF\n            & !TLB_READONLY\n            & !TLB_GLOBAL\n            & !TLB_HAS_CODE\n            & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32,\n    );\n    ctx.builder.and_i32();\n\n    ctx.builder.const_i32(TLB_VALID as i32);\n    ctx.builder.eq_i32();\n\n    ctx.builder.br_if(cont);\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.call_fn2(\"report_safe_read_jit_slow\");\n    }\n\n    ctx.builder.get_local(&address_local);\n    ctx.builder.call_fn1_ret(\"get_phys_eip_slow_jit\");\n\n    ctx.builder.tee_local(&entry_local);\n    ctx.builder.const_i32(1);\n    ctx.builder.and_i32();\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.if_void();\n        gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction); // XXX\n        ctx.builder.block_end();\n\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.const_i32(1);\n        ctx.builder.and_i32();\n    }\n\n    ctx.builder.br_if(ctx.exit_with_fault_label);\n\n    ctx.builder.block_end();\n\n    gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_FAST); // XXX: Both fast and slow\n\n    ctx.builder.get_local(&entry_local);\n    ctx.builder.const_i32(!0xFFF);\n    ctx.builder.and_i32();\n    ctx.builder.get_local(&address_local);\n    ctx.builder.xor_i32();\n\n    ctx.builder.free_local(entry_local);\n}\n\nfn gen_safe_write(\n    ctx: &mut JitContext,\n    bits: BitSize,\n    address_local: &WasmLocal,\n    value_local: GenSafeWriteValue,\n) {\n    // Execute a virtual memory write. All slow paths (memory-mapped IO, tlb miss, page fault,\n    // write across page boundary and page containing jitted code are handled in safe_write_jit_slow\n\n    //   entry <- tlb_data[addr >> 12 << 2]\n    //   if entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes: goto fast\n    //   entry <- safe_write_jit_slow(addr, value, instruction_pointer)\n    //   if page_fault: goto exit-with-pagefault\n    //   fast: mem[(entry & ~0xFFF) ^ addr] <- value\n\n    let cont = ctx.builder.block_void();\n    ctx.builder.get_local(&address_local);\n\n    ctx.builder.const_i32(12);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(2);\n    ctx.builder.shl_i32();\n\n    ctx.builder\n        .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });\n    let entry_local = ctx.builder.tee_new_local();\n\n    ctx.builder\n        .const_i32((0xFFF & !TLB_GLOBAL & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32);\n    ctx.builder.and_i32();\n\n    ctx.builder.const_i32(TLB_VALID as i32);\n    ctx.builder.eq_i32();\n\n    if bits != BitSize::BYTE {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.const_i32(0xFFF);\n        ctx.builder.and_i32();\n        ctx.builder.const_i32(0x1000 - bits.bytes() as i32);\n        ctx.builder.le_i32();\n\n        ctx.builder.and_i32();\n    }\n\n    ctx.builder.br_if(cont);\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.call_fn2(\"report_safe_write_jit_slow\");\n    }\n\n    ctx.builder.get_local(&address_local);\n    match value_local {\n        GenSafeWriteValue::I32(local) => ctx.builder.get_local(local),\n        GenSafeWriteValue::I64(local) => ctx.builder.get_local_i64(local),\n        GenSafeWriteValue::TwoI64s(local1, local2) => {\n            ctx.builder.get_local_i64(local1);\n            ctx.builder.get_local_i64(local2)\n        },\n    }\n    ctx.builder\n        .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.call_fn3_ret(\"safe_write8_slow_jit\");\n        },\n        BitSize::WORD => {\n            ctx.builder.call_fn3_ret(\"safe_write16_slow_jit\");\n        },\n        BitSize::DWORD => {\n            ctx.builder.call_fn3_ret(\"safe_write32_slow_jit\");\n        },\n        BitSize::QWORD => {\n            ctx.builder\n                .call_fn3_i32_i64_i32_ret(\"safe_write64_slow_jit\");\n        },\n        BitSize::DQWORD => {\n            ctx.builder\n                .call_fn4_i32_i64_i64_i32_ret(\"safe_write128_slow_jit\");\n        },\n    }\n    ctx.builder.tee_local(&entry_local);\n    ctx.builder.const_i32(1);\n    ctx.builder.and_i32();\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.if_void();\n        gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n        ctx.builder.block_end();\n\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.const_i32(1);\n        ctx.builder.and_i32();\n    }\n\n    ctx.builder.br_if(ctx.exit_with_fault_label);\n\n    ctx.builder.block_end();\n\n    gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_WRITE_FAST); // XXX: Both fast and slow\n\n    ctx.builder.get_local(&entry_local);\n    ctx.builder.const_i32(!0xFFF);\n    ctx.builder.and_i32();\n    ctx.builder.get_local(&address_local);\n    ctx.builder.xor_i32();\n\n    match value_local {\n        GenSafeWriteValue::I32(local) => ctx.builder.get_local(local),\n        GenSafeWriteValue::I64(local) => ctx.builder.get_local_i64(local),\n        GenSafeWriteValue::TwoI64s(local1, local2) => {\n            assert!(bits == BitSize::DQWORD);\n\n            let virt_address_local = ctx.builder.tee_new_local();\n            ctx.builder.get_local_i64(local1);\n            ctx.builder.store_unaligned_i64(0);\n\n            ctx.builder.get_local(&virt_address_local);\n            ctx.builder.get_local_i64(local2);\n            ctx.builder.store_unaligned_i64(8);\n            ctx.builder.free_local(virt_address_local);\n        },\n    }\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.store_u8(0);\n        },\n        BitSize::WORD => {\n            ctx.builder.store_unaligned_u16(0);\n        },\n        BitSize::DWORD => {\n            ctx.builder.store_unaligned_i32(0);\n        },\n        BitSize::QWORD => {\n            ctx.builder.store_unaligned_i64(0);\n        },\n        BitSize::DQWORD => {}, // handled above\n    }\n\n    ctx.builder.free_local(entry_local);\n}\n\npub fn gen_safe_read_write(\n    ctx: &mut JitContext,\n    bits: BitSize,\n    address_local: &WasmLocal,\n    f: &dyn Fn(&mut JitContext),\n) {\n    // Execute a virtual memory read+write. All slow paths (memory-mapped IO, tlb miss, page fault,\n    // write across page boundary and page containing jitted code are handled in\n    // safe_read_write_jit_slow\n\n    //   entry <- tlb_data[addr >> 12 << 2]\n    //   can_use_fast_path <- entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes\n    //   if can_use_fast_path: goto fast\n    //   entry <- safe_read_write_jit_slow(addr, instruction_pointer)\n    //   if page_fault: goto exit-with-pagefault\n    //   fast: value <- f(mem[(entry & ~0xFFF) ^ addr])\n    //   if !can_use_fast_path { safe_write_jit_slow(addr, value, instruction_pointer) }\n    //   mem[(entry & ~0xFFF) ^ addr] <- value\n\n    let cont = ctx.builder.block_void();\n    ctx.builder.get_local(address_local);\n\n    ctx.builder.const_i32(12);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(2);\n    ctx.builder.shl_i32();\n\n    ctx.builder\n        .load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });\n    let entry_local = ctx.builder.tee_new_local();\n\n    ctx.builder\n        .const_i32((0xFFF & !TLB_GLOBAL & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32);\n    ctx.builder.and_i32();\n\n    ctx.builder.const_i32(TLB_VALID as i32);\n    ctx.builder.eq_i32();\n\n    if bits != BitSize::BYTE {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.const_i32(0xFFF);\n        ctx.builder.and_i32();\n        ctx.builder.const_i32(0x1000 - bits.bytes() as i32);\n        ctx.builder.le_i32();\n        ctx.builder.and_i32();\n    }\n\n    let can_use_fast_path_local = ctx.builder.tee_new_local();\n\n    ctx.builder.br_if(cont);\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.get_local(&address_local);\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.call_fn2(\"report_safe_read_write_jit_slow\");\n    }\n\n    ctx.builder.get_local(&address_local);\n    ctx.builder\n        .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);\n\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.call_fn2_ret(\"safe_read_write8_slow_jit\");\n        },\n        BitSize::WORD => {\n            ctx.builder.call_fn2_ret(\"safe_read_write16_slow_jit\");\n        },\n        BitSize::DWORD => {\n            ctx.builder.call_fn2_ret(\"safe_read_write32s_slow_jit\");\n        },\n        BitSize::QWORD => {\n            ctx.builder.call_fn2_ret(\"safe_read_write64_slow_jit\");\n        },\n        BitSize::DQWORD => {\n            dbg_assert!(false);\n        },\n    }\n    ctx.builder.tee_local(&entry_local);\n    ctx.builder.const_i32(1);\n    ctx.builder.and_i32();\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.if_void();\n        gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n        ctx.builder.block_end();\n\n        ctx.builder.get_local(&entry_local);\n        ctx.builder.const_i32(1);\n        ctx.builder.and_i32();\n    }\n\n    ctx.builder.br_if(ctx.exit_with_fault_label);\n\n    ctx.builder.block_end();\n\n    gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_WRITE_FAST); // XXX: Also slow\n\n    ctx.builder.get_local(&entry_local);\n    ctx.builder.const_i32(!0xFFF);\n    ctx.builder.and_i32();\n    ctx.builder.get_local(&address_local);\n    ctx.builder.xor_i32();\n\n    ctx.builder.free_local(entry_local);\n    let phys_addr_local = ctx.builder.tee_new_local();\n\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.load_u8(0);\n        },\n        BitSize::WORD => {\n            ctx.builder.load_unaligned_u16(0);\n        },\n        BitSize::DWORD => {\n            ctx.builder.load_unaligned_i32(0);\n        },\n        BitSize::QWORD => {\n            ctx.builder.load_unaligned_i64(0);\n        },\n        BitSize::DQWORD => assert!(false), // not used\n    }\n\n    // value is now on stack\n\n    f(ctx);\n\n    // TODO: Could get rid of this local by returning one from f\n    let value_local = if bits == BitSize::QWORD {\n        GenSafeReadWriteValue::I64(ctx.builder.set_new_local_i64())\n    }\n    else {\n        GenSafeReadWriteValue::I32(ctx.builder.set_new_local())\n    };\n\n    ctx.builder.get_local(&can_use_fast_path_local);\n\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    {\n        ctx.builder.get_local(&address_local);\n\n        match &value_local {\n            GenSafeReadWriteValue::I32(l) => ctx.builder.get_local(l),\n            GenSafeReadWriteValue::I64(l) => ctx.builder.get_local_i64(l),\n        }\n\n        ctx.builder\n            .const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);\n\n        match bits {\n            BitSize::BYTE => {\n                ctx.builder.call_fn3_ret(\"safe_write8_slow_jit\");\n            },\n            BitSize::WORD => {\n                ctx.builder.call_fn3_ret(\"safe_write16_slow_jit\");\n            },\n            BitSize::DWORD => {\n                ctx.builder.call_fn3_ret(\"safe_write32_slow_jit\");\n            },\n            BitSize::QWORD => {\n                ctx.builder\n                    .call_fn3_i32_i64_i32_ret(\"safe_write64_slow_jit\");\n            },\n            BitSize::DQWORD => {\n                dbg_assert!(false);\n            },\n        }\n\n        if cfg!(debug_assertions) {\n            ctx.builder.const_i32(1);\n            ctx.builder.and_i32();\n\n            ctx.builder.if_void();\n            {\n                // handled above\n                ctx.builder.const_i32(match bits {\n                    BitSize::BYTE => 8,\n                    BitSize::WORD => 16,\n                    BitSize::DWORD => 32,\n                    BitSize::QWORD => 64,\n                    _ => {\n                        dbg_assert!(false);\n                        0\n                    },\n                });\n                ctx.builder.get_local(&address_local);\n                ctx.builder.call_fn2(\"bug_gen_safe_read_write_page_fault\");\n            }\n            ctx.builder.block_end();\n        }\n        else {\n            ctx.builder.drop_();\n        }\n    }\n    ctx.builder.block_end();\n\n    ctx.builder.get_local(&phys_addr_local);\n    match &value_local {\n        GenSafeReadWriteValue::I32(l) => ctx.builder.get_local(l),\n        GenSafeReadWriteValue::I64(l) => ctx.builder.get_local_i64(l),\n    }\n\n    match bits {\n        BitSize::BYTE => {\n            ctx.builder.store_u8(0);\n        },\n        BitSize::WORD => {\n            ctx.builder.store_unaligned_u16(0);\n        },\n        BitSize::DWORD => {\n            ctx.builder.store_unaligned_i32(0);\n        },\n        BitSize::QWORD => {\n            ctx.builder.store_unaligned_i64(0);\n        },\n        BitSize::DQWORD => {\n            dbg_assert!(false);\n        },\n    }\n\n    match value_local {\n        GenSafeReadWriteValue::I32(l) => ctx.builder.free_local(l),\n        GenSafeReadWriteValue::I64(l) => ctx.builder.free_local_i64(l),\n    }\n    ctx.builder.free_local(can_use_fast_path_local);\n    ctx.builder.free_local(phys_addr_local);\n}\n\n#[cfg(debug_assertions)]\n#[no_mangle]\npub fn bug_gen_safe_read_write_page_fault(bits: i32, addr: u32) {\n    dbg_log!(\"bug: gen_safe_read_write_page_fault {} {:x}\", bits, addr);\n    dbg_assert!(false);\n}\n\npub fn gen_jmp_rel16(builder: &mut WasmBuilder, rel16: u16) {\n    let cs_offset_addr = global_pointers::get_seg_offset(regs::CS);\n    builder.load_fixed_i32(cs_offset_addr);\n    let local = builder.set_new_local();\n\n    // generate:\n    // *instruction_pointer = cs_offset + ((*instruction_pointer - cs_offset + rel16) & 0xFFFF);\n    {\n        builder.const_i32(global_pointers::instruction_pointer as i32);\n\n        gen_get_eip(builder);\n        builder.get_local(&local);\n        builder.sub_i32();\n\n        builder.const_i32(rel16 as i32);\n        builder.add_i32();\n\n        builder.const_i32(0xFFFF);\n        builder.and_i32();\n\n        builder.get_local(&local);\n        builder.add_i32();\n\n        builder.store_aligned_i32(0);\n    }\n    builder.free_local(local);\n}\n\npub fn gen_pop16_ss16(ctx: &mut JitContext) {\n    // sp = segment_offsets[SS] + reg16[SP] (or just reg16[SP] if has_flat_segmentation)\n    gen_get_reg16(ctx, regs::SP);\n\n    if !ctx.cpu.has_flat_segmentation() {\n        gen_get_ss_offset(ctx);\n        ctx.builder.add_i32();\n    }\n\n    // result = safe_read16(sp)\n    let address_local = ctx.builder.set_new_local();\n    gen_safe_read16(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n\n    // reg16[SP] += 2;\n    gen_get_reg16(ctx, regs::SP);\n    ctx.builder.const_i32(2);\n    ctx.builder.add_i32();\n    gen_set_reg16(ctx, regs::SP);\n\n    // return value is already on stack\n}\n\npub fn gen_pop16_ss32(ctx: &mut JitContext) {\n    // esp = segment_offsets[SS] + reg32[ESP] (or just reg32[ESP] if has_flat_segmentation)\n    gen_get_reg32(ctx, regs::ESP);\n\n    if !ctx.cpu.has_flat_segmentation() {\n        gen_get_ss_offset(ctx);\n        ctx.builder.add_i32();\n    }\n\n    // result = safe_read16(esp)\n    let address_local = ctx.builder.set_new_local();\n    gen_safe_read16(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n\n    // reg32[ESP] += 2;\n    gen_get_reg32(ctx, regs::ESP);\n    ctx.builder.const_i32(2);\n    ctx.builder.add_i32();\n    gen_set_reg32(ctx, regs::ESP);\n\n    // return value is already on stack\n}\n\npub fn gen_pop16(ctx: &mut JitContext) {\n    if ctx.cpu.ssize_32() {\n        gen_pop16_ss32(ctx);\n    }\n    else {\n        gen_pop16_ss16(ctx);\n    }\n}\n\npub fn gen_pop32s_ss16(ctx: &mut JitContext) {\n    // sp = reg16[SP]\n    gen_get_reg16(ctx, regs::SP);\n\n    // result = safe_read32s(segment_offsets[SS] + sp) (or just sp if has_flat_segmentation)\n    if !ctx.cpu.has_flat_segmentation() {\n        gen_get_ss_offset(ctx);\n        ctx.builder.add_i32();\n    }\n\n    let address_local = ctx.builder.set_new_local();\n    gen_safe_read32(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n\n    // reg16[SP] = sp + 4;\n    gen_get_reg16(ctx, regs::SP);\n    ctx.builder.const_i32(4);\n    ctx.builder.add_i32();\n    gen_set_reg16(ctx, regs::SP);\n\n    // return value is already on stack\n}\n\npub fn gen_pop32s_ss32(ctx: &mut JitContext) {\n    if !ctx.cpu.has_flat_segmentation() {\n        gen_get_reg32(ctx, regs::ESP);\n        gen_get_ss_offset(ctx);\n        ctx.builder.add_i32();\n        let address_local = ctx.builder.set_new_local();\n        gen_safe_read32(ctx, &address_local);\n        ctx.builder.free_local(address_local);\n    }\n    else {\n        let reg = ctx.register_locals[regs::ESP as usize].unsafe_clone();\n        gen_safe_read32(ctx, &reg);\n    }\n\n    gen_get_reg32(ctx, regs::ESP);\n    ctx.builder.const_i32(4);\n    ctx.builder.add_i32();\n    gen_set_reg32(ctx, regs::ESP);\n\n    // return value is already on stack\n}\n\npub fn gen_pop32s(ctx: &mut JitContext) {\n    if ctx.cpu.ssize_32() {\n        gen_pop32s_ss32(ctx);\n    }\n    else {\n        gen_pop32s_ss16(ctx);\n    }\n}\n\npub fn gen_adjust_stack_reg(ctx: &mut JitContext, offset: u32) {\n    if ctx.cpu.ssize_32() {\n        gen_get_reg32(ctx, regs::ESP);\n        ctx.builder.const_i32(offset as i32);\n        ctx.builder.add_i32();\n        gen_set_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_get_reg16(ctx, regs::SP);\n        ctx.builder.const_i32(offset as i32);\n        ctx.builder.add_i32();\n        gen_set_reg16(ctx, regs::SP);\n    }\n}\n\npub fn gen_leave(ctx: &mut JitContext, os32: bool) {\n    // [e]bp = safe_read{16,32}([e]bp)\n\n    if ctx.cpu.ssize_32() {\n        gen_get_reg32(ctx, regs::EBP);\n    }\n    else {\n        gen_get_reg16(ctx, regs::BP);\n    }\n\n    let old_vbp = ctx.builder.tee_new_local();\n\n    if !ctx.cpu.has_flat_segmentation() {\n        gen_get_ss_offset(ctx);\n        ctx.builder.add_i32();\n    }\n    if os32 {\n        let address_local = ctx.builder.set_new_local();\n        gen_safe_read32(ctx, &address_local);\n        ctx.builder.free_local(address_local);\n        gen_set_reg32(ctx, regs::EBP);\n    }\n    else {\n        let address_local = ctx.builder.set_new_local();\n        gen_safe_read16(ctx, &address_local);\n        ctx.builder.free_local(address_local);\n        gen_set_reg16(ctx, regs::BP);\n    }\n\n    // [e]sp = [e]bp + (os32 ? 4 : 2)\n\n    if ctx.cpu.ssize_32() {\n        ctx.builder.get_local(&old_vbp);\n        ctx.builder.const_i32(if os32 { 4 } else { 2 });\n        ctx.builder.add_i32();\n        gen_set_reg32(ctx, regs::ESP);\n    }\n    else {\n        ctx.builder.get_local(&old_vbp);\n        ctx.builder.const_i32(if os32 { 4 } else { 2 });\n        ctx.builder.add_i32();\n        gen_set_reg16(ctx, regs::SP);\n    }\n\n    ctx.builder.free_local(old_vbp);\n}\n\npub fn gen_task_switch_test(ctx: &mut JitContext) {\n    // generate if(cr[0] & (CR0_EM | CR0_TS)) { task_switch_test_jit(); goto exit_with_fault; }\n    let cr0_offset = global_pointers::get_creg_offset(0);\n\n    dbg_assert!(regs::CR0_EM | regs::CR0_TS <= 0xFF);\n    ctx.builder.load_fixed_u8(cr0_offset);\n    ctx.builder.const_i32((regs::CR0_EM | regs::CR0_TS) as i32);\n    ctx.builder.and_i32();\n\n    ctx.builder.if_void();\n    {\n        gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n        gen_fn1_const(\n            ctx.builder,\n            \"task_switch_test_jit\",\n            ctx.start_of_current_instruction & 0xFFF,\n        );\n        ctx.builder.br(ctx.exit_with_fault_label);\n    }\n    ctx.builder.block_end();\n}\n\npub fn gen_task_switch_test_mmx(ctx: &mut JitContext) {\n    // generate if(cr[0] & (CR0_EM | CR0_TS)) { task_switch_test_mmx_jit(); goto exit_with_fault; }\n    let cr0_offset = global_pointers::get_creg_offset(0);\n\n    dbg_assert!(regs::CR0_EM | regs::CR0_TS <= 0xFF);\n    ctx.builder.load_fixed_u8(cr0_offset);\n    ctx.builder.const_i32((regs::CR0_EM | regs::CR0_TS) as i32);\n    ctx.builder.and_i32();\n\n    ctx.builder.if_void();\n    {\n        gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n        gen_fn1_const(\n            ctx.builder,\n            \"task_switch_test_mmx_jit\",\n            ctx.start_of_current_instruction & 0xFFF,\n        );\n        ctx.builder.br(ctx.exit_with_fault_label);\n    }\n    ctx.builder.block_end();\n}\n\npub fn gen_push16(ctx: &mut JitContext, value_local: &WasmLocal) {\n    if ctx.cpu.ssize_32() {\n        gen_get_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_get_reg16(ctx, regs::SP);\n    };\n\n    ctx.builder.const_i32(2);\n    ctx.builder.sub_i32();\n\n    let reg_updated_local = if !ctx.cpu.ssize_32() || !ctx.cpu.has_flat_segmentation() {\n        let reg_updated_local = ctx.builder.tee_new_local();\n        if !ctx.cpu.ssize_32() {\n            ctx.builder.const_i32(0xFFFF);\n            ctx.builder.and_i32();\n        }\n\n        if !ctx.cpu.has_flat_segmentation() {\n            gen_get_ss_offset(ctx);\n            ctx.builder.add_i32();\n        }\n\n        let sp_local = ctx.builder.set_new_local();\n        gen_safe_write16(ctx, &sp_local, &value_local);\n        ctx.builder.free_local(sp_local);\n\n        ctx.builder.get_local(&reg_updated_local);\n        reg_updated_local\n    }\n    else {\n        // short path: The address written to is equal to ESP/SP minus two\n        let reg_updated_local = ctx.builder.tee_new_local();\n        gen_safe_write16(ctx, &reg_updated_local, &value_local);\n        reg_updated_local\n    };\n\n    if ctx.cpu.ssize_32() {\n        gen_set_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_set_reg16(ctx, regs::SP);\n    };\n    ctx.builder.free_local(reg_updated_local);\n}\n\npub fn gen_push32(ctx: &mut JitContext, value_local: &WasmLocal) {\n    if ctx.cpu.ssize_32() {\n        gen_get_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_get_reg16(ctx, regs::SP);\n    };\n\n    ctx.builder.const_i32(4);\n    ctx.builder.sub_i32();\n\n    let new_sp_local = if !ctx.cpu.ssize_32() || !ctx.cpu.has_flat_segmentation() {\n        let new_sp_local = ctx.builder.tee_new_local();\n        if !ctx.cpu.ssize_32() {\n            ctx.builder.const_i32(0xFFFF);\n            ctx.builder.and_i32();\n        }\n\n        if !ctx.cpu.has_flat_segmentation() {\n            gen_get_ss_offset(ctx);\n            ctx.builder.add_i32();\n        }\n\n        let sp_local = ctx.builder.set_new_local();\n\n        gen_safe_write32(ctx, &sp_local, &value_local);\n        ctx.builder.free_local(sp_local);\n\n        ctx.builder.get_local(&new_sp_local);\n        new_sp_local\n    }\n    else {\n        // short path: The address written to is equal to ESP/SP minus four\n        let new_sp_local = ctx.builder.tee_new_local();\n        gen_safe_write32(ctx, &new_sp_local, &value_local);\n        new_sp_local\n    };\n\n    if ctx.cpu.ssize_32() {\n        gen_set_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_set_reg16(ctx, regs::SP);\n    };\n    ctx.builder.free_local(new_sp_local);\n}\n\npub fn gen_push32_sreg(ctx: &mut JitContext, reg: u32) {\n    gen_get_sreg(ctx, reg);\n    let value_local = ctx.builder.set_new_local();\n\n    if ctx.cpu.ssize_32() {\n        gen_get_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_get_reg16(ctx, regs::SP);\n    };\n\n    ctx.builder.const_i32(4);\n    ctx.builder.sub_i32();\n\n    let new_sp_local = if !ctx.cpu.ssize_32() || !ctx.cpu.has_flat_segmentation() {\n        let new_sp_local = ctx.builder.tee_new_local();\n        if !ctx.cpu.ssize_32() {\n            ctx.builder.const_i32(0xFFFF);\n            ctx.builder.and_i32();\n        }\n\n        if !ctx.cpu.has_flat_segmentation() {\n            gen_get_ss_offset(ctx);\n            ctx.builder.add_i32();\n        }\n\n        let sp_local = ctx.builder.set_new_local();\n\n        gen_safe_write16(ctx, &sp_local, &value_local);\n        ctx.builder.free_local(sp_local);\n\n        ctx.builder.get_local(&new_sp_local);\n        new_sp_local\n    }\n    else {\n        // short path: The address written to is equal to ESP/SP minus four\n        let new_sp_local = ctx.builder.tee_new_local();\n        gen_safe_write16(ctx, &new_sp_local, &value_local);\n        new_sp_local\n    };\n\n    if ctx.cpu.ssize_32() {\n        gen_set_reg32(ctx, regs::ESP);\n    }\n    else {\n        gen_set_reg16(ctx, regs::SP);\n    };\n    ctx.builder.free_local(new_sp_local);\n    ctx.builder.free_local(value_local);\n}\n\npub fn gen_get_real_eip(ctx: &mut JitContext) {\n    gen_get_eip(ctx.builder);\n    ctx.builder.const_i32(!0xFFF);\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(ctx.cpu.eip as i32 & 0xFFF);\n    ctx.builder.or_i32();\n    if !ctx.cpu.has_flat_segmentation() {\n        ctx.builder\n            .load_fixed_i32(global_pointers::get_seg_offset(regs::CS));\n        ctx.builder.sub_i32();\n    }\n}\n\npub fn gen_set_last_op1(builder: &mut WasmBuilder, source: &WasmLocal) {\n    builder.const_i32(global_pointers::last_op1 as i32);\n    builder.get_local(&source);\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_set_last_result(builder: &mut WasmBuilder, source: &WasmLocal) {\n    builder.const_i32(global_pointers::last_result as i32);\n    builder.get_local(&source);\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_clear_flags_changed_bits(builder: &mut WasmBuilder, bits_to_clear: i32) {\n    builder.const_i32(global_pointers::flags_changed as i32);\n    gen_get_flags_changed(builder);\n    builder.const_i32(!bits_to_clear);\n    builder.and_i32();\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_set_last_op_size_and_flags_changed(\n    builder: &mut WasmBuilder,\n    last_op_size: i32,\n    flags_changed: i32,\n) {\n    dbg_assert!(last_op_size == OPSIZE_8 || last_op_size == OPSIZE_16 || last_op_size == OPSIZE_32);\n    dbg_assert!(global_pointers::last_op_size as i32 % 8 == 0);\n    dbg_assert!(global_pointers::last_op_size as i32 + 4 == global_pointers::flags_changed as i32);\n    builder.const_i32(global_pointers::last_op_size as i32);\n    builder.const_i64(last_op_size as u32 as i64 | (flags_changed as u32 as i64) << 32);\n    builder.store_aligned_i64(0);\n}\n\npub fn gen_set_flags_bits(builder: &mut WasmBuilder, bits_to_set: i32) {\n    builder.const_i32(global_pointers::flags as i32);\n    gen_get_flags(builder);\n    builder.const_i32(bits_to_set);\n    builder.or_i32();\n    builder.store_aligned_i32(0);\n}\n\npub fn gen_clear_flags_bits(builder: &mut WasmBuilder, bits_to_clear: i32) {\n    builder.const_i32(global_pointers::flags as i32);\n    gen_get_flags(builder);\n    builder.const_i32(!bits_to_clear);\n    builder.and_i32();\n    builder.store_aligned_i32(0);\n}\n\n#[derive(PartialEq)]\npub enum ConditionNegate {\n    True,\n    False,\n}\n\npub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp {\n            dest: InstructionOperandDest::WasmLocal(dest),\n            source: InstructionOperand::WasmLocal(source),\n            opsize: OPSIZE_32,\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            ctx.builder.get_local(dest);\n            ctx.builder.get_local(source);\n            if negate == ConditionNegate::False {\n                ctx.builder.eq_i32();\n            }\n            else {\n                ctx.builder.ne_i32();\n            }\n        },\n        Instruction::Cmp {\n            dest: InstructionOperandDest::WasmLocal(dest),\n            source: InstructionOperand::Immediate(0),\n            opsize: OPSIZE_32,\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            ctx.builder.get_local(dest);\n            if negate == ConditionNegate::False {\n                ctx.builder.eqz_i32();\n            }\n        },\n        Instruction::Cmp {\n            dest: InstructionOperandDest::WasmLocal(dest),\n            source: InstructionOperand::Immediate(i),\n            opsize: OPSIZE_32,\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            ctx.builder.get_local(dest);\n            ctx.builder.const_i32(*i);\n            if negate == ConditionNegate::False {\n                ctx.builder.eq_i32();\n            }\n            else {\n                ctx.builder.ne_i32();\n            }\n        },\n        Instruction::Cmp { .. }\n        | Instruction::Sub { .. }\n        | Instruction::Add { .. }\n        | Instruction::AdcSbb { .. }\n        | Instruction::NonZeroShift { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            if negate == ConditionNegate::False {\n                ctx.builder.eqz_i32();\n            }\n        },\n        Instruction::Bitwise { opsize, .. } => {\n            let &opsize = opsize;\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            // Note: Necessary because test{8,16} don't mask either last_result or any of their operands\n            // TODO: Use local instead of last_result for 8-bit/16-bit\n            if opsize == OPSIZE_32 {\n                gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            }\n            else if opsize == OPSIZE_16 {\n                ctx.builder\n                    .load_fixed_u16(global_pointers::last_result as u32);\n            }\n            else if opsize == OPSIZE_8 {\n                ctx.builder\n                    .load_fixed_u8(global_pointers::last_result as u32);\n            }\n            if negate == ConditionNegate::False {\n                ctx.builder.eqz_i32();\n            }\n        },\n        &Instruction::Other => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n            gen_get_flags_changed(ctx.builder);\n            ctx.builder.const_i32(FLAG_ZERO);\n            ctx.builder.and_i32();\n            ctx.builder.if_i32();\n\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            let last_result = ctx.builder.tee_new_local();\n            ctx.builder.const_i32(-1);\n            ctx.builder.xor_i32();\n            ctx.builder.get_local(&last_result);\n            ctx.builder.free_local(last_result);\n            ctx.builder.const_i32(1);\n            ctx.builder.sub_i32();\n            ctx.builder.and_i32();\n            gen_get_last_op_size(ctx.builder);\n            ctx.builder.shr_u_i32();\n            ctx.builder.const_i32(1);\n            ctx.builder.and_i32();\n\n            ctx.builder.else_();\n            gen_get_flags(ctx.builder);\n            ctx.builder.const_i32(FLAG_ZERO);\n            ctx.builder.and_i32();\n            ctx.builder.block_end();\n\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n    }\n}\n\npub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp { source, opsize, .. }\n        | Instruction::Sub {\n            source,\n            opsize,\n            is_dec: false,\n            ..\n        } => {\n            // Note: x < y and x < x - y can be used interchangeably (see getcf)\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            match (opsize, source) {\n                (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),\n                (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(i),\n                _ => gen_get_last_result(ctx.builder, &ctx.previous_instruction),\n            }\n            if negate == ConditionNegate::True {\n                ctx.builder.geu_i32();\n            }\n            else {\n                ctx.builder.ltu_i32();\n            }\n        },\n        Instruction::Add {\n            source,\n            opsize,\n            is_inc: false,\n            ..\n        } => {\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            match (opsize, source) {\n                (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),\n                (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(i),\n                _ => gen_get_last_op1(ctx.builder, &ctx.previous_instruction),\n            }\n            if negate == ConditionNegate::True {\n                ctx.builder.geu_i32();\n            }\n            else {\n                ctx.builder.ltu_i32();\n            }\n        },\n        Instruction::Add { is_inc: true, .. } | Instruction::Sub { is_dec: true, .. } => {\n            gen_get_flags(ctx.builder);\n            ctx.builder.const_i32(FLAG_CARRY);\n            ctx.builder.and_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n        Instruction::Bitwise { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            ctx.builder\n                .const_i32(if negate == ConditionNegate::True { 1 } else { 0 });\n        },\n        Instruction::NonZeroShift { .. } | Instruction::AdcSbb { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_flags(ctx.builder);\n            ctx.builder.const_i32(FLAG_CARRY);\n            ctx.builder.and_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n        &Instruction::Other => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n\n            gen_get_flags_changed(ctx.builder);\n            let flags_changed = ctx.builder.tee_new_local();\n            ctx.builder.const_i32(FLAG_CARRY);\n            ctx.builder.and_i32();\n            ctx.builder.if_i32();\n\n            ctx.builder.get_local(&flags_changed);\n            ctx.builder.const_i32(31);\n            ctx.builder.shr_s_i32();\n            ctx.builder.free_local(flags_changed);\n            let sub_mask = ctx.builder.set_new_local();\n\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            ctx.builder.get_local(&sub_mask);\n            ctx.builder.xor_i32();\n\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            ctx.builder.get_local(&sub_mask);\n            ctx.builder.xor_i32();\n\n            ctx.builder.ltu_i32();\n\n            ctx.builder.else_();\n            gen_get_flags(ctx.builder);\n            ctx.builder.const_i32(FLAG_CARRY);\n            ctx.builder.and_i32();\n            ctx.builder.block_end();\n\n            ctx.builder.free_local(sub_mask);\n\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n    }\n}\n\npub fn gen_getsf(ctx: &mut JitContext, negate: ConditionNegate) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp { opsize, .. }\n        | Instruction::Sub { opsize, .. }\n        | Instruction::Add { opsize, .. }\n        | Instruction::AdcSbb { opsize, .. }\n        | Instruction::Bitwise { opsize, .. }\n        | Instruction::NonZeroShift { opsize, .. } => {\n            let &opsize = opsize;\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            if opsize == OPSIZE_32 {\n                ctx.builder.const_i32(0);\n                if negate == ConditionNegate::True {\n                    ctx.builder.ge_i32();\n                }\n                else {\n                    ctx.builder.lt_i32();\n                }\n            }\n            else {\n                // TODO: use register (see get_last_result)\n                ctx.builder\n                    .const_i32(if opsize == OPSIZE_16 { 0x8000 } else { 0x80 });\n                ctx.builder.and_i32();\n                if negate == ConditionNegate::True {\n                    ctx.builder.eqz_i32();\n                }\n            }\n        },\n        &Instruction::Other => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n            gen_get_flags_changed(ctx.builder);\n            ctx.builder.const_i32(FLAG_SIGN);\n            ctx.builder.and_i32();\n            ctx.builder.if_i32();\n            {\n                gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                gen_get_last_op_size(ctx.builder);\n                ctx.builder.shr_u_i32();\n                ctx.builder.const_i32(1);\n                ctx.builder.and_i32();\n            }\n            ctx.builder.else_();\n            {\n                gen_get_flags(ctx.builder);\n                ctx.builder.const_i32(FLAG_SIGN);\n                ctx.builder.and_i32();\n            }\n            ctx.builder.block_end();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n    }\n}\n\npub fn gen_getof(ctx: &mut JitContext) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp { opsize, .. } | Instruction::Sub { opsize, .. } => {\n            // TODO: a better formula might be possible\n            let &opsize = opsize;\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            ctx.builder.xor_i32();\n\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            ctx.builder.sub_i32();\n            ctx.builder.xor_i32();\n            ctx.builder.and_i32();\n\n            ctx.builder.const_i32(if opsize == OPSIZE_32 {\n                0x8000_0000u32 as i32\n            }\n            else if opsize == OPSIZE_16 {\n                0x8000\n            }\n            else {\n                0x80\n            });\n            ctx.builder.and_i32();\n        },\n        Instruction::Add { opsize, .. } => {\n            // TODO: a better formula might be possible\n            let &opsize = opsize;\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            ctx.builder.xor_i32();\n\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            ctx.builder.sub_i32();\n            ctx.builder.xor_i32();\n            ctx.builder.and_i32();\n\n            ctx.builder.const_i32(if opsize == OPSIZE_32 {\n                0x8000_0000u32 as i32\n            }\n            else if opsize == OPSIZE_16 {\n                0x8000\n            }\n            else {\n                0x80\n            });\n            ctx.builder.and_i32();\n        },\n        Instruction::Bitwise { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            ctx.builder.const_i32(0);\n        },\n        Instruction::NonZeroShift { .. } | Instruction::AdcSbb { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_flags(ctx.builder);\n            ctx.builder.const_i32(FLAG_OVERFLOW);\n            ctx.builder.and_i32();\n        },\n        &Instruction::Other => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n            gen_get_flags_changed(ctx.builder);\n            let flags_changed = ctx.builder.tee_new_local();\n            ctx.builder.const_i32(FLAG_OVERFLOW);\n            ctx.builder.and_i32();\n            ctx.builder.if_i32();\n            {\n                gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                let last_op1 = ctx.builder.tee_new_local();\n                gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                let last_result = ctx.builder.tee_new_local();\n                ctx.builder.xor_i32();\n\n                ctx.builder.get_local(&last_result);\n                ctx.builder.get_local(&last_op1);\n                ctx.builder.sub_i32();\n                gen_get_flags_changed(ctx.builder);\n                ctx.builder.const_i32(31);\n                ctx.builder.shr_u_i32();\n                ctx.builder.sub_i32();\n\n                ctx.builder.get_local(&last_result);\n                ctx.builder.xor_i32();\n\n                ctx.builder.and_i32();\n\n                gen_get_last_op_size(ctx.builder);\n                ctx.builder.shr_u_i32();\n                ctx.builder.const_i32(1);\n                ctx.builder.and_i32();\n\n                ctx.builder.free_local(last_op1);\n                ctx.builder.free_local(last_result);\n            }\n            ctx.builder.else_();\n            {\n                gen_get_flags(ctx.builder);\n                ctx.builder.const_i32(FLAG_OVERFLOW);\n                ctx.builder.and_i32();\n            }\n            ctx.builder.block_end();\n            ctx.builder.free_local(flags_changed);\n        },\n    }\n}\n\npub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp {\n            dest,\n            source,\n            opsize,\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            match dest {\n                InstructionOperandDest::WasmLocal(l) => {\n                    ctx.builder.get_local(l);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });\n                        ctx.builder.and_i32();\n                    }\n                },\n                InstructionOperandDest::Other => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                },\n            }\n            match source {\n                InstructionOperand::WasmLocal(l) => {\n                    ctx.builder.get_local(l);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });\n                        ctx.builder.and_i32();\n                    }\n                },\n                InstructionOperand::Other => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                    ctx.builder.sub_i32();\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });\n                        ctx.builder.and_i32();\n                    }\n                },\n                &InstructionOperand::Immediate(i) => {\n                    dbg_assert!(*opsize != OPSIZE_8 || i >= 0 && i < 0x100);\n                    dbg_assert!(*opsize != OPSIZE_16 || i >= 0 && i < 0x10000);\n                    ctx.builder.const_i32(i);\n                },\n            }\n\n            if negate == ConditionNegate::True {\n                ctx.builder.gtu_i32();\n            }\n            else {\n                ctx.builder.leu_i32();\n            }\n        },\n        Instruction::Sub {\n            opsize,\n            source,\n            is_dec: false,\n            ..\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            match (opsize, source) {\n                (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),\n                (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(i),\n                _ => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                    ctx.builder.sub_i32();\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 0xFF } else { 0xFFFF });\n                        ctx.builder.and_i32();\n                    }\n                },\n            }\n\n            if negate == ConditionNegate::True {\n                ctx.builder.gtu_i32();\n            }\n            else {\n                ctx.builder.leu_i32();\n            }\n        },\n        &Instruction::Bitwise { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_getzf(ctx, negate);\n        },\n        &Instruction::Add { .. } | &Instruction::Sub { is_dec: true, .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            // not the best code generation, but reasonable for this fairly uncommon case\n            gen_getcf(ctx, ConditionNegate::False);\n            gen_getzf(ctx, ConditionNegate::False);\n            ctx.builder.or_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n        Instruction::Other | Instruction::NonZeroShift { .. } | Instruction::AdcSbb { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n            gen_getcf(ctx, ConditionNegate::False);\n            gen_getzf(ctx, ConditionNegate::False);\n            ctx.builder.or_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n    }\n}\n\npub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp {\n            dest,\n            source,\n            opsize,\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            match dest {\n                InstructionOperandDest::WasmLocal(l) => {\n                    ctx.builder.get_local(l);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n                InstructionOperandDest::Other => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n            }\n            match source {\n                InstructionOperand::WasmLocal(l) => {\n                    ctx.builder.get_local(l);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n                InstructionOperand::Other => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                    ctx.builder.sub_i32();\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n                &InstructionOperand::Immediate(i) => {\n                    ctx.builder.const_i32(i);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n            }\n            if negate == ConditionNegate::True {\n                ctx.builder.ge_i32();\n            }\n            else {\n                ctx.builder.lt_i32();\n            }\n        },\n        Instruction::Sub { opsize, source, .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                ctx.builder\n                    .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                ctx.builder.shl_i32();\n            }\n            match (opsize, source) {\n                (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),\n                (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(\n                    i << if *opsize == OPSIZE_32 {\n                        0\n                    }\n                    else if *opsize == OPSIZE_16 {\n                        16\n                    }\n                    else {\n                        24\n                    },\n                ),\n                _ => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                    ctx.builder.sub_i32();\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n            }\n            if negate == ConditionNegate::True {\n                ctx.builder.ge_i32();\n            }\n            else {\n                ctx.builder.lt_i32();\n            }\n        },\n        &Instruction::Bitwise { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_getsf(ctx, negate);\n        },\n        &Instruction::Other\n        | Instruction::Add { .. }\n        | Instruction::NonZeroShift { .. }\n        | Instruction::AdcSbb { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n            if let Instruction::Add { .. } = ctx.previous_instruction {\n                gen_profiler_stat_increment(\n                    ctx.builder,\n                    profiler::stat::CONDITION_UNOPTIMISED_UNHANDLED_L,\n                );\n            }\n            gen_getsf(ctx, ConditionNegate::False);\n            ctx.builder.eqz_i32();\n            gen_getof(ctx);\n            ctx.builder.eqz_i32();\n            ctx.builder.xor_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n    }\n}\n\npub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) {\n    match &ctx.previous_instruction {\n        Instruction::Cmp {\n            dest,\n            source,\n            opsize,\n        } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            match dest {\n                InstructionOperandDest::WasmLocal(l) => {\n                    ctx.builder.get_local(l);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n                InstructionOperandDest::Other => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n            }\n            match source {\n                InstructionOperand::WasmLocal(l) => {\n                    ctx.builder.get_local(l);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n                InstructionOperand::Other => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                    ctx.builder.sub_i32();\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n                &InstructionOperand::Immediate(i) => {\n                    ctx.builder.const_i32(i);\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n            }\n            if negate == ConditionNegate::True {\n                ctx.builder.gt_i32();\n            }\n            else {\n                ctx.builder.le_i32();\n            }\n        },\n        Instruction::Sub { opsize, source, .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n            if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                ctx.builder\n                    .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                ctx.builder.shl_i32();\n            }\n            match (opsize, source) {\n                (&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),\n                (_, &InstructionOperand::Immediate(i)) => ctx.builder.const_i32(\n                    i << if *opsize == OPSIZE_32 {\n                        0\n                    }\n                    else if *opsize == OPSIZE_16 {\n                        16\n                    }\n                    else {\n                        24\n                    },\n                ),\n                _ => {\n                    gen_get_last_op1(ctx.builder, &ctx.previous_instruction);\n                    gen_get_last_result(ctx.builder, &ctx.previous_instruction);\n                    ctx.builder.sub_i32();\n                    if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {\n                        ctx.builder\n                            .const_i32(if *opsize == OPSIZE_8 { 24 } else { 16 });\n                        ctx.builder.shl_i32();\n                    }\n                },\n            }\n            if negate == ConditionNegate::True {\n                ctx.builder.gt_i32();\n            }\n            else {\n                ctx.builder.le_i32();\n            }\n        },\n        &Instruction::Bitwise { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);\n            // TODO: Could probably be improved (<= 0)\n            gen_test_l(ctx, ConditionNegate::False);\n            gen_getzf(ctx, ConditionNegate::False);\n            ctx.builder.or_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n        Instruction::Other\n        | Instruction::Add { .. }\n        | Instruction::NonZeroShift { .. }\n        | Instruction::AdcSbb { .. } => {\n            gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n            if let Instruction::Add { .. } = ctx.previous_instruction {\n                gen_profiler_stat_increment(\n                    ctx.builder,\n                    profiler::stat::CONDITION_UNOPTIMISED_UNHANDLED_LE,\n                );\n            }\n            gen_test_l(ctx, ConditionNegate::False);\n            gen_getzf(ctx, ConditionNegate::False);\n            ctx.builder.or_i32();\n            if negate == ConditionNegate::True {\n                ctx.builder.eqz_i32();\n            }\n        },\n    }\n}\n\npub fn gen_test_loopnz(ctx: &mut JitContext, is_asize_32: bool) {\n    gen_test_loop(ctx, is_asize_32);\n    ctx.builder.eqz_i32();\n    gen_getzf(ctx, ConditionNegate::False);\n    ctx.builder.or_i32();\n    ctx.builder.eqz_i32();\n}\npub fn gen_test_loopz(ctx: &mut JitContext, is_asize_32: bool) {\n    gen_test_loop(ctx, is_asize_32);\n    ctx.builder.eqz_i32();\n    gen_getzf(ctx, ConditionNegate::False);\n    ctx.builder.eqz_i32();\n    ctx.builder.or_i32();\n    ctx.builder.eqz_i32();\n}\npub fn gen_test_loop(ctx: &mut JitContext, is_asize_32: bool) {\n    if is_asize_32 {\n        gen_get_reg32(ctx, regs::ECX);\n    }\n    else {\n        gen_get_reg16(ctx, regs::CX);\n    }\n}\npub fn gen_test_jcxz(ctx: &mut JitContext, is_asize_32: bool) {\n    if is_asize_32 {\n        gen_get_reg32(ctx, regs::ECX);\n    }\n    else {\n        gen_get_reg16(ctx, regs::CX);\n    }\n    ctx.builder.eqz_i32();\n}\n\npub fn gen_fpu_get_sti(ctx: &mut JitContext, i: u32) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    ctx.builder.const_i32(i as i32);\n    ctx.builder.call_fn2(\"fpu_get_sti_jit\");\n    ctx.builder\n        .load_fixed_i64(global_pointers::sse_scratch_register as u32);\n    ctx.builder\n        .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);\n}\n\npub fn gen_fpu_load_m32(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.call_fn2(\"f32_to_f80_jit\");\n    ctx.builder\n        .load_fixed_i64(global_pointers::sse_scratch_register as u32);\n    ctx.builder\n        .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);\n}\n\npub fn gen_fpu_load_m64(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.call_fn2_i32_i64(\"f64_to_f80_jit\");\n    ctx.builder\n        .load_fixed_i64(global_pointers::sse_scratch_register as u32);\n    ctx.builder\n        .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);\n}\n\npub fn gen_fpu_load_i16(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    sign_extend_i16(ctx.builder);\n    ctx.builder.call_fn2(\"i32_to_f80_jit\");\n    ctx.builder\n        .load_fixed_i64(global_pointers::sse_scratch_register as u32);\n    ctx.builder\n        .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);\n}\npub fn gen_fpu_load_i32(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.call_fn2(\"i32_to_f80_jit\");\n    ctx.builder\n        .load_fixed_i64(global_pointers::sse_scratch_register as u32);\n    ctx.builder\n        .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);\n}\npub fn gen_fpu_load_i64(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder\n        .const_i32(global_pointers::sse_scratch_register as i32);\n    gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.call_fn2_i32_i64(\"i64_to_f80_jit\");\n    ctx.builder\n        .load_fixed_i64(global_pointers::sse_scratch_register as u32);\n    ctx.builder\n        .load_fixed_u16(global_pointers::sse_scratch_register as u32 + 8);\n}\n\npub fn gen_trigger_de(ctx: &mut JitContext) {\n    gen_fn1_const(\n        ctx.builder,\n        \"trigger_de_jit\",\n        ctx.start_of_current_instruction & 0xFFF,\n    );\n    gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_with_fault_label);\n}\n\npub fn gen_trigger_ud(ctx: &mut JitContext) {\n    gen_fn1_const(\n        ctx.builder,\n        \"trigger_ud_jit\",\n        ctx.start_of_current_instruction & 0xFFF,\n    );\n    gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_with_fault_label);\n}\n\npub fn gen_trigger_gp(ctx: &mut JitContext, error_code: u32) {\n    gen_fn2_const(\n        ctx.builder,\n        \"trigger_gp_jit\",\n        error_code,\n        ctx.start_of_current_instruction & 0xFFF,\n    );\n    gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_with_fault_label);\n}\n\npub fn gen_condition_fn_negated(ctx: &mut JitContext, condition: u8) {\n    gen_condition_fn(ctx, condition ^ 1)\n}\n\npub fn gen_condition_fn(ctx: &mut JitContext, condition: u8) {\n    if condition & 0xF0 == 0x00 || condition & 0xF0 == 0x70 || condition & 0xF0 == 0x80 {\n        match condition & 0xF {\n            0x0 => {\n                gen_getof(ctx);\n            },\n            0x1 => {\n                gen_getof(ctx);\n                ctx.builder.eqz_i32();\n            },\n            0x2 => {\n                gen_getcf(ctx, ConditionNegate::False);\n            },\n            0x3 => {\n                gen_getcf(ctx, ConditionNegate::True);\n            },\n            0x4 => {\n                gen_getzf(ctx, ConditionNegate::False);\n            },\n            0x5 => {\n                gen_getzf(ctx, ConditionNegate::True);\n            },\n            0x6 => {\n                gen_test_be(ctx, ConditionNegate::False);\n            },\n            0x7 => {\n                gen_test_be(ctx, ConditionNegate::True);\n            },\n            0x8 => {\n                gen_getsf(ctx, ConditionNegate::False);\n            },\n            0x9 => {\n                gen_getsf(ctx, ConditionNegate::True);\n            },\n            0xA => {\n                gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n                gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED_PF);\n                ctx.builder.call_fn0_ret(\"test_p\");\n            },\n            0xB => {\n                gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);\n                gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED_PF);\n                ctx.builder.call_fn0_ret(\"test_np\");\n            },\n            0xC => {\n                gen_test_l(ctx, ConditionNegate::False);\n            },\n            0xD => {\n                gen_test_l(ctx, ConditionNegate::True);\n            },\n            0xE => {\n                gen_test_le(ctx, ConditionNegate::False);\n            },\n            0xF => {\n                gen_test_le(ctx, ConditionNegate::True);\n            },\n            _ => {\n                dbg_assert!(false);\n            },\n        }\n    }\n    else {\n        // loop, loopnz, loopz, jcxz\n        dbg_assert!(condition & !0x3 == 0xE0);\n        if condition == 0xE0 {\n            gen_test_loopnz(ctx, ctx.cpu.asize_32());\n        }\n        else if condition == 0xE1 {\n            gen_test_loopz(ctx, ctx.cpu.asize_32());\n        }\n        else if condition == 0xE2 {\n            gen_test_loop(ctx, ctx.cpu.asize_32());\n        }\n        else if condition == 0xE3 {\n            gen_test_jcxz(ctx, ctx.cpu.asize_32());\n        }\n    }\n}\n\npub fn gen_move_registers_from_locals_to_memory(ctx: &mut JitContext) {\n    if cfg!(feature = \"profiler\") {\n        let instruction = memory::read32s(ctx.start_of_current_instruction) as u32;\n        opstats::gen_opstat_unguarded_register(ctx.builder, instruction);\n    }\n\n    for i in 0..8 {\n        ctx.builder\n            .const_i32(global_pointers::get_reg32_offset(i as u32) as i32);\n        ctx.builder.get_local(&ctx.register_locals[i]);\n        ctx.builder.store_aligned_i32(0);\n    }\n}\npub fn gen_move_registers_from_memory_to_locals(ctx: &mut JitContext) {\n    if cfg!(feature = \"profiler\") {\n        let instruction = memory::read32s(ctx.start_of_current_instruction) as u32;\n        opstats::gen_opstat_unguarded_register(ctx.builder, instruction);\n    }\n\n    for i in 0..8 {\n        ctx.builder\n            .const_i32(global_pointers::get_reg32_offset(i as u32) as i32);\n        ctx.builder.load_aligned_i32(0);\n        ctx.builder.set_local(&ctx.register_locals[i]);\n    }\n}\n\npub fn gen_profiler_stat_increment(builder: &mut WasmBuilder, stat: profiler::stat) {\n    if !cfg!(feature = \"profiler\") {\n        return;\n    }\n    let addr = unsafe { &raw mut profiler::stat_array[stat as usize] } as u32;\n    builder.increment_fixed_i64(addr, 1)\n}\n\npub fn gen_debug_track_jit_exit(builder: &mut WasmBuilder, address: u32) {\n    if cfg!(feature = \"profiler\") {\n        gen_fn1_const(builder, \"track_jit_exit\", address);\n    }\n}\n"
  },
  {
    "path": "src/rust/config.rs",
    "content": "pub const LOG_PAGE_FAULTS: bool = false;\n\npub const VMWARE_HYPERVISOR_PORT: bool = true;\n"
  },
  {
    "path": "src/rust/control_flow.rs",
    "content": "use std::collections::HashSet;\nuse std::collections::{BTreeMap, BTreeSet};\nuse std::iter;\n\nuse crate::jit::{BasicBlock, BasicBlockType, MAX_EXTRA_BASIC_BLOCKS};\nuse crate::profiler;\n\nconst ENTRY_NODE_ID: u32 = 0xffff_ffff;\n\n// this code works fine with either BTree or Hash Maps/Sets\n// - HashMap / HashSet: slightly faster\n// - BTreeMap / BTreeSet: stable iteration order (graphs don't change between rust versions, required for expect tests)\ntype Set = BTreeSet<u32>;\ntype Graph = BTreeMap<u32, Set>;\n\n/// Reverse the direction of all edges in the graph\nfn rev_graph_edges(nodes: &Graph) -> Graph {\n    let mut rev_nodes = Graph::new();\n    for (from, tos) in nodes {\n        for to in tos {\n            rev_nodes\n                .entry(*to)\n                .or_insert_with(|| Set::new())\n                .insert(*from);\n        }\n    }\n    rev_nodes\n}\n\npub fn make_graph(basic_blocks: &Vec<BasicBlock>) -> Graph {\n    let mut nodes = Graph::new();\n    let mut entry_edges = Set::new();\n\n    for b in basic_blocks.iter() {\n        let mut edges = Set::new();\n\n        match &b.ty {\n            &BasicBlockType::ConditionalJump {\n                next_block_addr,\n                next_block_branch_taken_addr,\n                ..\n            } => {\n                if let Some(next_block_addr) = next_block_addr {\n                    edges.insert(next_block_addr);\n                }\n                if let Some(next_block_branch_taken_addr) = next_block_branch_taken_addr {\n                    edges.insert(next_block_branch_taken_addr);\n                }\n            },\n            &BasicBlockType::Normal {\n                next_block_addr: Some(next_block_addr),\n                ..\n            } => {\n                edges.insert(next_block_addr);\n            },\n            &BasicBlockType::Normal {\n                next_block_addr: None,\n                ..\n            } => {},\n            BasicBlockType::Exit => {},\n            BasicBlockType::AbsoluteEip => {\n                // Not necessary: We generate a loop around the outer brtable unconditionally\n                //edges.insert(ENTRY_NODE_ID);\n            },\n        }\n\n        nodes.insert(b.addr, edges);\n\n        if b.is_entry_block {\n            entry_edges.insert(b.addr);\n        }\n    }\n\n    // Entry node that represents the initial basic block of the generated function (must be\n    // able to reach all entry nodes)\n    nodes.insert(ENTRY_NODE_ID, entry_edges);\n    return nodes;\n}\n\npub enum WasmStructure {\n    BasicBlock(u32),\n    Dispatcher(Vec<u32>),\n    Loop(Vec<WasmStructure>),\n    Block(Vec<WasmStructure>),\n}\nimpl WasmStructure {\n    pub fn print(&self, depth: usize) {\n        match self {\n            Self::BasicBlock(addr) => {\n                dbg_log!(\"{} 0x{:x}\", \" \".repeat(depth), addr);\n            },\n            Self::Dispatcher(entries) => {\n                dbg_log!(\"{} Dispatcher entries:\", \" \".repeat(depth));\n                for e in entries {\n                    dbg_log!(\"{}  {:x}\", \" \".repeat(depth), e);\n                }\n            },\n            Self::Loop(elements) => {\n                dbg_log!(\"{} loop_void({})\", \" \".repeat(depth), elements.len());\n                for e in elements {\n                    e.print(depth + 1)\n                }\n                dbg_log!(\"{} loop_end({})\", \" \".repeat(depth), elements.len());\n            },\n            Self::Block(elements) => {\n                dbg_log!(\"{} block_void({})\", \" \".repeat(depth), elements.len());\n                for e in elements {\n                    e.print(depth + 1)\n                }\n                dbg_log!(\"{} block_end({})\", \" \".repeat(depth), elements.len());\n            },\n        }\n    }\n\n    fn branches(&self, edges: &Graph) -> HashSet<u32> {\n        fn handle(block: &WasmStructure, edges: &Graph, result: &mut HashSet<u32>) {\n            match block {\n                WasmStructure::BasicBlock(addr) => result.extend(edges.get(&addr).unwrap()),\n                WasmStructure::Dispatcher(entries) => result.extend(entries),\n                WasmStructure::Loop(children) | WasmStructure::Block(children) => {\n                    for c in children.iter() {\n                        handle(c, edges, result);\n                    }\n                },\n            }\n        }\n\n        let mut result = HashSet::new();\n        handle(self, edges, &mut result);\n        result\n    }\n\n    pub fn head(&self) -> Box<dyn iter::Iterator<Item = u32> + '_> {\n        match self {\n            Self::BasicBlock(addr) => Box::new(iter::once(*addr)),\n            Self::Dispatcher(entries) => Box::new(entries.iter().copied()),\n            Self::Loop(children) => children.first().unwrap().head(),\n            Self::Block(elements) => elements.first().unwrap().head(),\n        }\n    }\n}\n\n/// Check:\n/// - Dispatcher appears at the beginning of a loop\n/// - No two nested blocks at the end\n/// - No two nested loops at the beginning\n/// - No empty blocks or loops\n/// - The entry node block is not present\npub fn assert_invariants(blocks: &Vec<WasmStructure>) {\n    fn check(node: &WasmStructure, in_tail_block: bool, in_head_loop: bool, is_first: bool) {\n        match node {\n            WasmStructure::Block(children) => {\n                dbg_assert!(!in_tail_block);\n                dbg_assert!(!children.is_empty());\n                for (i, c) in children.iter().enumerate() {\n                    let is_first = i == 0;\n                    let is_last = i == children.len() - 1;\n                    check(c, is_last, in_head_loop && is_first, is_first);\n                }\n            },\n            WasmStructure::Loop(children) => {\n                dbg_assert!(!in_head_loop);\n                dbg_assert!(!children.is_empty());\n                for (i, c) in children.iter().enumerate() {\n                    let is_first = i == 0;\n                    let is_last = i == children.len() - 1;\n                    check(c, in_tail_block && is_last, is_first, is_first);\n                }\n            },\n            &WasmStructure::BasicBlock(addr) => {\n                dbg_assert!(addr != ENTRY_NODE_ID);\n            },\n            WasmStructure::Dispatcher(_) => {\n                dbg_assert!(is_first);\n                //dbg_assert!(in_head_loop); // fails for module dispatcher\n            },\n        }\n    }\n\n    for (i, b) in blocks.iter().enumerate() {\n        check(b, false, false, i == 0);\n    }\n}\n\n/// Strongly connected components via Kosaraju's algorithm\nfn scc(edges: &Graph, rev_edges: &Graph) -> Vec<Vec<u32>> {\n    fn visit(\n        node: u32,\n        edges: &Graph,\n        rev_edges: &Graph,\n        visited: &mut HashSet<u32>,\n        l: &mut Vec<u32>,\n    ) {\n        if visited.contains(&node) {\n            return;\n        }\n        visited.insert(node);\n        for &next in edges.get(&node).unwrap() {\n            visit(next, edges, rev_edges, visited, l);\n        }\n        l.push(node);\n    }\n\n    let mut l = Vec::new();\n    let mut visited = HashSet::new();\n    for &node in edges.keys() {\n        visit(node, edges, rev_edges, &mut visited, &mut l);\n    }\n\n    fn assign(\n        node: u32,\n        edges: &Graph,\n        rev_edges: &Graph,\n        assigned: &mut HashSet<u32>,\n        group: &mut Vec<u32>,\n    ) {\n        if assigned.contains(&node) {\n            return;\n        }\n        assigned.insert(node);\n        group.push(node);\n        if let Some(nexts) = rev_edges.get(&node) {\n            for &next in nexts {\n                assign(next, edges, rev_edges, assigned, group);\n            }\n        }\n    }\n    let mut assigned = HashSet::new();\n    let mut assignment = Vec::new();\n    for &node in l.iter().rev() {\n        let mut group = Vec::new();\n        assign(node, edges, rev_edges, &mut assigned, &mut group);\n        if !group.is_empty() {\n            assignment.push(group);\n        }\n    }\n\n    assignment\n}\n\npub fn loopify(nodes: &Graph) -> Vec<WasmStructure> {\n    let rev_nodes = rev_graph_edges(nodes);\n    let groups = scc(nodes, &rev_nodes);\n\n    return groups\n        .iter()\n        .flat_map(|group| {\n            dbg_assert!(!group.is_empty());\n            if group.len() == 1 {\n                let addr = group[0];\n                if addr == ENTRY_NODE_ID {\n                    let entries = nodes.get(&ENTRY_NODE_ID).unwrap().iter().copied().collect();\n                    return vec![WasmStructure::Dispatcher(entries)].into_iter();\n                }\n                let block = WasmStructure::BasicBlock(addr);\n                // self-loops\n                if nodes.get(&group[0]).unwrap().contains(&group[0]) {\n                    return vec![WasmStructure::Loop(vec![block])].into_iter();\n                }\n                else {\n                    return vec![block].into_iter();\n                }\n            }\n\n            let entries_to_group: Vec<u32> = group\n                .iter()\n                .filter(|addr| {\n                    // reachable from outside of the group\n                    rev_nodes.get(addr).map_or(false, |x| {\n                        x.iter().any(|incoming| !group.contains(incoming))\n                    })\n                })\n                .copied()\n                .collect();\n\n            if entries_to_group.len() != 1 {\n                //dbg_log!(\n                //    \"Compiling multi-entry loop with {} entries and {} basic blocks\",\n                //    entries_to_group.len(),\n                //    group.len()\n                //);\n            }\n\n            let max_extra_basic_blocks = unsafe { MAX_EXTRA_BASIC_BLOCKS } as usize;\n\n            if entries_to_group.len() * group.len() > max_extra_basic_blocks {\n                let mut subgroup_edges: Graph = Graph::new();\n                for elem in group {\n                    subgroup_edges.insert(\n                        *elem,\n                        nodes\n                            .get(&elem)\n                            .unwrap()\n                            .iter()\n                            .filter(|dest| {\n                                // XXX: This might remove forward edges to other loop entries\n                                //      Probably not an issue since it can go through the\n                                //      dispatcher\n                                group.contains(dest) && !entries_to_group.contains(dest)\n                            })\n                            .copied()\n                            .collect(),\n                    );\n                }\n\n                let mut loop_nodes = loopify(&subgroup_edges);\n\n                if entries_to_group.len() > 1 {\n                    loop_nodes.insert(0, WasmStructure::Dispatcher(entries_to_group));\n                }\n\n                return vec![WasmStructure::Loop(loop_nodes)].into_iter();\n            }\n            else {\n                profiler::stat_increment_by(\n                    profiler::stat::COMPILE_DUPLICATED_BASIC_BLOCK,\n                    ((entries_to_group.len() - 1) * group.len()) as u64,\n                );\n\n                let nodes: Vec<WasmStructure> = entries_to_group\n                    .iter()\n                    .map(|&entry| {\n                        let mut subgroup_edges: Graph = Graph::new();\n                        for &elem in group {\n                            subgroup_edges.insert(\n                                elem,\n                                nodes\n                                    .get(&elem)\n                                    .unwrap()\n                                    .iter()\n                                    .copied()\n                                    .filter(|dest| group.contains(dest) && *dest != entry)\n                                    .collect(),\n                            );\n                        }\n                        let loop_nodes = loopify(&subgroup_edges);\n                        WasmStructure::Loop(loop_nodes)\n                    })\n                    .collect();\n\n                nodes.into_iter()\n            }\n        })\n        .collect();\n}\n\npub fn blockify(blocks: &mut Vec<WasmStructure>, edges: &Graph) {\n    let mut cached_branches: Vec<HashSet<u32>> = Vec::new();\n    for i in 0..blocks.len() {\n        cached_branches.push(blocks[i].branches(edges));\n    }\n\n    let mut i = 0;\n    while i < blocks.len() {\n        match &mut blocks[i] {\n            WasmStructure::BasicBlock(_) | WasmStructure::Dispatcher(_) => {},\n            WasmStructure::Loop (\n                blocks\n            )\n            // TODO: Might be faster to do this *after* inserting blocks in this block\n            | WasmStructure::Block(blocks) => blockify(blocks, edges),\n        }\n\n        let source = {\n            let mut source = None;\n            for j in 0..i {\n                if blocks[i].head().any(|bb| cached_branches[j].contains(&bb)) {\n                    source = Some(j);\n                    break;\n                }\n            }\n            match source {\n                Some(s) => s,\n                None => {\n                    i += 1;\n                    continue;\n                },\n            }\n        };\n\n        // This is optional: Avoid putting a single basic block into a block\n        if source == i - 1 {\n            match &blocks[source] {\n                &WasmStructure::BasicBlock(_) => {\n                    i += 1;\n                    continue;\n                },\n                _ => {},\n            }\n        }\n\n        let replacement = WasmStructure::Block(Vec::new());\n        let children: Vec<WasmStructure> =\n            blocks.splice(source..i, iter::once(replacement)).collect();\n        match &mut blocks[source] {\n            WasmStructure::Block(c) => c.extend(children),\n            _ => {\n                dbg_assert!(false);\n            },\n        }\n        match &blocks[source + 1] {\n            WasmStructure::BasicBlock(_) =>\n                //dbg_assert!(*b == bbs.next().unwrap())\n                {},\n            WasmStructure::Dispatcher(_) => {},\n            WasmStructure::Loop(_blocks) | WasmStructure::Block(_blocks) => {}, //dbg_assert!(blocks[0].head() == bb),\n        }\n\n        {\n            let replacement = HashSet::new();\n            let children: Vec<HashSet<u32>> = cached_branches\n                .splice(source..i, iter::once(replacement))\n                .collect();\n            dbg_assert!(cached_branches[source].len() == 0);\n            let mut iter = children.into_iter();\n            cached_branches[source] = iter.next().unwrap();\n            for c in iter {\n                cached_branches[source].extend(c);\n            }\n        }\n\n        // skip the inserted block and this block\n        i = source + 2;\n    }\n}\n"
  },
  {
    "path": "src/rust/cpu/apic.rs",
    "content": "// See Intel's System Programming Guide\n\nuse crate::cpu::{cpu::js, global_pointers::acpi_enabled, ioapic};\nuse std::sync::{Mutex, MutexGuard};\n\nconst APIC_LOG_VERBOSE: bool = false;\n\n// should probably be kept in sync with TSC_RATE in cpu.rs\nconst APIC_TIMER_FREQ: f64 = 1.0 * 1000.0 * 1000.0;\n\nconst APIC_TIMER_MODE_MASK: u32 = 3 << 17;\n\nconst APIC_TIMER_MODE_ONE_SHOT: u32 = 0;\nconst APIC_TIMER_MODE_PERIODIC: u32 = 1 << 17;\n\nconst _APIC_TIMER_MODE_TSC: u32 = 2 << 17;\n\nconst DELIVERY_MODES: [&str; 8] = [\n    \"Fixed (0)\",\n    \"Lowest Prio (1)\",\n    \"SMI (2)\",\n    \"Reserved (3)\",\n    \"NMI (4)\",\n    \"INIT (5)\",\n    \"Reserved (6)\",\n    \"ExtINT (7)\",\n];\n\nconst DESTINATION_MODES: [&str; 2] = [\"physical\", \"logical\"];\n\nconst IOAPIC_CONFIG_MASKED: u32 = 0x10000;\n\nconst IOAPIC_DELIVERY_INIT: u8 = 5;\nconst IOAPIC_DELIVERY_NMI: u8 = 4;\nconst IOAPIC_DELIVERY_FIXED: u8 = 0;\n\n// keep in sync with cpu.js\n#[allow(dead_code)]\nconst APIC_STRUCT_SIZE: usize = 4 * 46;\n\n// Note: JavaScript (cpu.get_state_apic) depens on this layout\nconst _: () = assert!(std::mem::offset_of!(Apic, timer_last_tick) == 6 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, lvt_timer) == 8 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, lvt_perf_counter) == 9 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, icr0) == 14 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, icr1) == 15 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, irr) == 16 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, isr) == 24 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, tmr) == 32 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, spurious_vector) == 40 * 4);\nconst _: () = assert!(std::mem::offset_of!(Apic, lvt_thermal_sensor) == 45 * 4);\nconst _: () = assert!(std::mem::size_of::<Apic>() == APIC_STRUCT_SIZE);\n#[repr(C)]\npub struct Apic {\n    apic_id: u32,\n    timer_divider: u32,\n    timer_divider_shift: u32,\n    timer_initial_count: u32,\n    timer_current_count: u32,\n    timer_last_tick: f64,\n    lvt_timer: u32,\n    lvt_perf_counter: u32,\n    lvt_int0: u32,\n    lvt_int1: u32,\n    lvt_error: u32,\n    tpr: u32,\n    icr0: u32,\n    icr1: u32,\n    irr: [u32; 8],\n    isr: [u32; 8],\n    tmr: [u32; 8],\n    spurious_vector: u32,\n    destination_format: u32,\n    local_destination: u32,\n    error: u32,\n    read_error: u32,\n    lvt_thermal_sensor: u32,\n}\n\nstatic APIC: Mutex<Apic> = Mutex::new(Apic {\n    apic_id: 0,\n    timer_divider: 0,\n    timer_divider_shift: 1,\n    timer_initial_count: 0,\n    timer_current_count: 0,\n    timer_last_tick: 0.0,\n    lvt_timer: IOAPIC_CONFIG_MASKED,\n    lvt_thermal_sensor: IOAPIC_CONFIG_MASKED,\n    lvt_perf_counter: IOAPIC_CONFIG_MASKED,\n    lvt_int0: IOAPIC_CONFIG_MASKED,\n    lvt_int1: IOAPIC_CONFIG_MASKED,\n    lvt_error: IOAPIC_CONFIG_MASKED,\n    tpr: 0,\n    icr0: 0,\n    icr1: 0,\n    irr: [0; 8],\n    isr: [0; 8],\n    tmr: [0; 8],\n    spurious_vector: 0xFE,\n    destination_format: !0,\n    local_destination: 0,\n    error: 0,\n    read_error: 0,\n});\n\npub fn get_apic() -> MutexGuard<'static, Apic> { APIC.try_lock().unwrap() }\n\n#[no_mangle]\npub fn get_apic_addr() -> u32 { &raw mut *get_apic() as u32 }\n\npub fn read32(addr: u32) -> u32 {\n    if unsafe { !*acpi_enabled } {\n        return 0;\n    }\n    read32_internal(&mut get_apic(), addr)\n}\n\nfn read32_internal(apic: &mut Apic, addr: u32) -> u32 {\n    match addr {\n        0x20 => {\n            dbg_log!(\"APIC read id\");\n            apic.apic_id\n        },\n\n        0x30 => {\n            // version\n            dbg_log!(\"APIC read version\");\n            0x50014\n        },\n\n        0x80 => {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"APIC read tpr\");\n            }\n            apic.tpr\n        },\n\n        0xB0 => {\n            // write-only (written by DSL)\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"APIC read eoi register\");\n            }\n            0\n        },\n\n        0xD0 => {\n            dbg_log!(\"Read local destination\");\n            apic.local_destination\n        },\n\n        0xE0 => {\n            dbg_log!(\"Read destination format\");\n            apic.destination_format\n        },\n\n        0xF0 => apic.spurious_vector,\n\n        0x100 | 0x110 | 0x120 | 0x130 | 0x140 | 0x150 | 0x160 | 0x170 => {\n            let index = ((addr - 0x100) >> 4) as usize;\n            dbg_log!(\"Read isr {}: {:08x}\", index, apic.isr[index] as u32);\n            apic.isr[index]\n        },\n\n        0x180 | 0x190 | 0x1A0 | 0x1B0 | 0x1C0 | 0x1D0 | 0x1E0 | 0x1F0 => {\n            let index = ((addr - 0x180) >> 4) as usize;\n            dbg_log!(\"Read tmr {}: {:08x}\", index, apic.tmr[index] as u32);\n            apic.tmr[index]\n        },\n\n        0x200 | 0x210 | 0x220 | 0x230 | 0x240 | 0x250 | 0x260 | 0x270 => {\n            let index = ((addr - 0x200) >> 4) as usize;\n            dbg_log!(\"Read irr {}: {:08x}\", index, apic.irr[index] as u32);\n            apic.irr[index]\n        },\n\n        0x280 => {\n            dbg_log!(\"Read error: {:08x}\", apic.read_error);\n            apic.read_error\n        },\n\n        0x300 => {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"APIC read icr0\");\n            }\n            apic.icr0\n        },\n\n        0x310 => {\n            dbg_log!(\"APIC read icr1\");\n            apic.icr1\n        },\n\n        0x320 => {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"read timer lvt\");\n            }\n            apic.lvt_timer\n        },\n\n        0x330 => {\n            dbg_log!(\"read lvt thermal sensor\");\n            apic.lvt_thermal_sensor\n        },\n\n        0x340 => {\n            dbg_log!(\"read lvt perf counter\");\n            apic.lvt_perf_counter\n        },\n\n        0x350 => {\n            dbg_log!(\"read lvt int0\");\n            apic.lvt_int0\n        },\n\n        0x360 => {\n            dbg_log!(\"read lvt int1\");\n            apic.lvt_int1\n        },\n\n        0x370 => {\n            dbg_log!(\"read lvt error\");\n            apic.lvt_error\n        },\n\n        0x3E0 => {\n            // divider\n            dbg_log!(\"read timer divider\");\n            apic.timer_divider\n        },\n\n        0x380 => {\n            dbg_log!(\"read timer initial count\");\n            apic.timer_initial_count\n        },\n\n        0x390 => {\n            let now = unsafe { js::microtick() };\n            if apic.timer_last_tick > now {\n                // should only happen after restore_state\n                dbg_log!(\"warning: APIC last_tick is in the future, resetting\");\n                apic.timer_last_tick = now;\n            }\n            let diff = now - apic.timer_last_tick;\n            let diff_in_ticks = diff * APIC_TIMER_FREQ / (1 << apic.timer_divider_shift) as f64;\n            dbg_assert!(diff_in_ticks >= 0.0);\n            let diff_in_ticks = diff_in_ticks as u64;\n            let result = if diff_in_ticks < apic.timer_initial_count as u64 {\n                apic.timer_initial_count - diff_in_ticks as u32\n            }\n            else {\n                let mode = apic.lvt_timer & APIC_TIMER_MODE_MASK;\n                if mode == APIC_TIMER_MODE_PERIODIC {\n                    apic.timer_initial_count\n                        - (diff_in_ticks % (apic.timer_initial_count as u64 + 1)) as u32\n                }\n                else if mode == APIC_TIMER_MODE_ONE_SHOT {\n                    0\n                }\n                else {\n                    dbg_assert!(false, \"apic unimplemented timer mode: {:x}\", mode);\n                    0\n                }\n            };\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"read timer current count: {}\", result);\n            }\n            result\n        },\n\n        _ => {\n            dbg_log!(\"APIC read {:x}\", addr);\n            dbg_assert!(false);\n            0\n        },\n    }\n}\n\npub fn write32(addr: u32, value: u32) {\n    if unsafe { !*acpi_enabled } {\n        return;\n    }\n    write32_internal(&mut get_apic(), addr, value)\n}\n\nfn write32_internal(apic: &mut Apic, addr: u32, value: u32) {\n    match addr {\n        0x20 => {\n            dbg_log!(\"APIC write id: {:08x}\", value >> 8);\n            apic.apic_id = value;\n        },\n\n        0x30 => {\n            // version\n            dbg_log!(\"APIC write version: {:08x}, ignored\", value);\n        },\n\n        0x80 => {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"Set tpr: {:02x}\", value & 0xFF);\n            }\n            apic.tpr = value & 0xFF;\n        },\n\n        0xB0 => {\n            if let Some(highest_isr) = highest_isr(apic) {\n                if APIC_LOG_VERBOSE {\n                    dbg_log!(\"eoi: {:08x} for vector {:x}\", value, highest_isr);\n                }\n                register_clear_bit(&mut apic.isr, highest_isr);\n                if register_get_bit(&apic.tmr, highest_isr) {\n                    // Send eoi to all IO APICs\n                    ioapic::remote_eoi(apic, highest_isr);\n                }\n            }\n            else {\n                dbg_log!(\"Bad eoi: No isr set\");\n            }\n        },\n\n        0xD0 => {\n            dbg_log!(\"Set local destination: {:08x}\", value);\n            apic.local_destination = value & 0xFF000000;\n        },\n\n        0xE0 => {\n            dbg_log!(\"Set destination format: {:08x}\", value);\n            apic.destination_format = value | 0xFFFFFF;\n        },\n\n        0xF0 => {\n            dbg_log!(\"Set spurious vector: {:08x}\", value);\n            apic.spurious_vector = value;\n        },\n\n        0x280 => {\n            // updated readable error register with real error\n            dbg_log!(\"Write error: {:08x}\", value);\n            apic.read_error = apic.error;\n            apic.error = 0;\n        },\n\n        0x300 => {\n            let vector = (value & 0xFF) as u8;\n            let delivery_mode = ((value >> 8) & 7) as u8;\n            let destination_mode = ((value >> 11) & 1) as u8;\n            let is_level = value & ioapic::IOAPIC_CONFIG_TRIGGER_MODE_LEVEL\n                == ioapic::IOAPIC_CONFIG_TRIGGER_MODE_LEVEL;\n            let destination_shorthand = (value >> 18) & 3;\n            let destination = (apic.icr1 >> 24) as u8;\n            dbg_log!(\n                \"APIC write icr0: {:08x} vector={:02x} destination_mode={} delivery_mode={} destination_shorthand={}\",\n                value,\n                vector,\n                DESTINATION_MODES[destination_mode as usize],\n                DELIVERY_MODES[delivery_mode as usize],\n                [\"no\", \"self\", \"all with self\", \"all without self\"][destination_shorthand as usize]\n            );\n\n            let mut value = value;\n            value &= !(1 << 12);\n            apic.icr0 = value;\n\n            if destination_shorthand == 0 {\n                // no shorthand\n                route(\n                    apic,\n                    vector,\n                    delivery_mode,\n                    is_level,\n                    destination,\n                    destination_mode,\n                );\n            }\n            else if destination_shorthand == 1 {\n                // self\n                deliver(apic, vector, IOAPIC_DELIVERY_FIXED, is_level);\n            }\n            else if destination_shorthand == 2 {\n                // all including self\n                deliver(apic, vector, delivery_mode, is_level);\n            }\n            else if destination_shorthand == 3 {\n                // all but self\n            }\n            else {\n                dbg_assert!(false);\n            }\n        },\n\n        0x310 => {\n            dbg_log!(\"APIC write icr1: {:08x}\", value);\n            apic.icr1 = value;\n        },\n\n        0x320 => {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"timer lvt: {:08x}\", value);\n            }\n            // TODO: check if unmasking and if this should trigger an interrupt immediately\n            apic.lvt_timer = value;\n        },\n\n        0x330 => {\n            dbg_log!(\"lvt thermal sensor: {:08x}\", value);\n            apic.lvt_thermal_sensor = value;\n        },\n\n        0x340 => {\n            dbg_log!(\"lvt perf counter: {:08x}\", value);\n            apic.lvt_perf_counter = value;\n        },\n\n        0x350 => {\n            dbg_log!(\"lvt int0: {:08x}\", value);\n            apic.lvt_int0 = value;\n        },\n\n        0x360 => {\n            dbg_log!(\"lvt int1: {:08x}\", value);\n            apic.lvt_int1 = value;\n        },\n\n        0x370 => {\n            dbg_log!(\"lvt error: {:08x}\", value);\n            apic.lvt_error = value;\n        },\n\n        0x3E0 => {\n            apic.timer_divider = value;\n\n            let divide_shift = (value & 0b11) | ((value & 0b1000) >> 1);\n            apic.timer_divider_shift = if divide_shift == 0b111 { 0 } else { divide_shift + 1 };\n            dbg_log!(\n                \"APIC timer divider: {:08x} shift={} tick={:.6}ms\",\n                apic.timer_divider,\n                apic.timer_divider_shift,\n                (1 << apic.timer_divider_shift) as f64 / APIC_TIMER_FREQ\n            );\n        },\n\n        0x380 => {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\n                    \"APIC timer initial: {} next_interrupt={:.2}ms\",\n                    value,\n                    value as f64 * (1 << apic.timer_divider_shift) as f64 / APIC_TIMER_FREQ,\n                );\n            }\n            apic.timer_initial_count = value;\n            apic.timer_current_count = value;\n            apic.timer_last_tick = unsafe { js::microtick() };\n        },\n\n        0x390 => {\n            dbg_log!(\"write timer current: {:08x}\", value);\n            dbg_assert!(false, \"read-only register\");\n        },\n\n        _ => {\n            dbg_log!(\"APIC write32 {:x} <- {:08x}\", addr, value);\n            dbg_assert!(false);\n        },\n    }\n}\n\n#[no_mangle]\npub fn apic_timer(now: f64) -> f64 { timer(&mut get_apic(), now) }\n\nfn timer(apic: &mut Apic, now: f64) -> f64 {\n    if apic.timer_initial_count == 0 || apic.timer_current_count == 0 {\n        return 100.0;\n    }\n\n    if apic.timer_last_tick > now {\n        // should only happen after restore_state\n        dbg_log!(\"warning: APIC last_tick is in the future, resetting\");\n        apic.timer_last_tick = now;\n    }\n\n    let diff = now - apic.timer_last_tick;\n    let diff_in_ticks = diff * APIC_TIMER_FREQ / (1 << apic.timer_divider_shift) as f64;\n    dbg_assert!(diff_in_ticks >= 0.0);\n    let diff_in_ticks = diff_in_ticks as u64;\n\n    let time_per_interrupt =\n        apic.timer_initial_count as f64 * (1 << apic.timer_divider_shift) as f64 / APIC_TIMER_FREQ;\n\n    if diff_in_ticks >= apic.timer_initial_count as u64 {\n        let mode = apic.lvt_timer & APIC_TIMER_MODE_MASK;\n        if mode == APIC_TIMER_MODE_PERIODIC {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"APIC timer periodic interrupt\");\n            }\n\n            if diff_in_ticks >= 2 * apic.timer_initial_count as u64 {\n                dbg_log!(\n                    \"warning: APIC skipping {} interrupts initial={} ticks={} last_tick={:.1}ms now={:.1}ms d={:.1}ms\",\n                    diff_in_ticks / apic.timer_initial_count as u64 - 1,\n                    apic.timer_initial_count,\n                    diff_in_ticks,\n                    apic.timer_last_tick,\n                    now,\n                    diff,\n                );\n                apic.timer_last_tick = now;\n            }\n            else {\n                apic.timer_last_tick += time_per_interrupt;\n                dbg_assert!(apic.timer_last_tick <= now);\n            }\n        }\n        else if mode == APIC_TIMER_MODE_ONE_SHOT {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"APIC timer one shot end\");\n            }\n            apic.timer_current_count = 0;\n        }\n        else {\n            dbg_assert!(false, \"apic unimplemented timer mode: {:x}\", mode);\n        }\n\n        if apic.lvt_timer & IOAPIC_CONFIG_MASKED == 0 {\n            deliver(\n                apic,\n                (apic.lvt_timer & 0xFF) as u8,\n                IOAPIC_DELIVERY_FIXED,\n                false,\n            );\n        }\n    }\n\n    apic.timer_last_tick + time_per_interrupt - now\n}\n\npub fn route(\n    apic: &mut Apic,\n    vector: u8,\n    mode: u8,\n    is_level: bool,\n    _destination: u8,\n    _destination_mode: u8,\n) {\n    // TODO\n    deliver(apic, vector, mode, is_level);\n}\n\nfn deliver(apic: &mut Apic, vector: u8, mode: u8, is_level: bool) {\n    if APIC_LOG_VERBOSE {\n        dbg_log!(\"Deliver {:02x} mode={} level={}\", vector, mode, is_level);\n    }\n\n    if mode == IOAPIC_DELIVERY_INIT {\n        // TODO\n        return;\n    }\n\n    if mode == IOAPIC_DELIVERY_NMI {\n        // TODO\n        return;\n    }\n\n    if vector < 0x10 || vector == 0xFF {\n        dbg_assert!(false, \"TODO: Invalid vector: {:x}\", vector);\n    }\n\n    if register_get_bit(&apic.irr, vector) {\n        dbg_log!(\"Not delivered: irr already set, vector={:02x}\", vector);\n        return;\n    }\n\n    register_set_bit(&mut apic.irr, vector);\n\n    if is_level {\n        register_set_bit(&mut apic.tmr, vector);\n    }\n    else {\n        register_clear_bit(&mut apic.tmr, vector);\n    }\n}\n\nfn highest_irr(apic: &mut Apic) -> Option<u8> {\n    let highest = register_get_highest_bit(&apic.irr);\n    if let Some(x) = highest {\n        dbg_assert!(x >= 0x10);\n        dbg_assert!(x != 0xFF);\n    }\n    highest\n}\n\nfn highest_isr(apic: &mut Apic) -> Option<u8> {\n    let highest = register_get_highest_bit(&apic.isr);\n    if let Some(x) = highest {\n        dbg_assert!(x >= 0x10);\n        dbg_assert!(x != 0xFF);\n    }\n    highest\n}\n\npub fn acknowledge_irq() -> Option<u8> { acknowledge_irq_internal(&mut get_apic()) }\n\nfn acknowledge_irq_internal(apic: &mut Apic) -> Option<u8> {\n    let highest_irr = match highest_irr(apic) {\n        None => return None,\n        Some(x) => x,\n    };\n\n    if let Some(highest_isr) = highest_isr(apic) {\n        if highest_isr >= highest_irr {\n            if APIC_LOG_VERBOSE {\n                dbg_log!(\"Higher isr, isr={:x} irr={:x}\", highest_isr, highest_irr);\n            }\n            return None;\n        }\n    }\n\n    if highest_irr & 0xF0 <= apic.tpr as u8 & 0xF0 {\n        if APIC_LOG_VERBOSE {\n            dbg_log!(\n                \"Higher tpr, tpr={:x} irr={:x}\",\n                apic.tpr & 0xF0,\n                highest_irr\n            );\n        }\n        return None;\n    }\n\n    register_clear_bit(&mut apic.irr, highest_irr);\n    register_set_bit(&mut apic.isr, highest_irr);\n\n    if APIC_LOG_VERBOSE {\n        dbg_log!(\"Calling vector {:x}\", highest_irr);\n    }\n\n    dbg_assert!(acknowledge_irq_internal(apic).is_none());\n\n    Some(highest_irr)\n}\n\n// functions operating on 256-bit registers (for irr, isr, tmr)\nfn register_get_bit(v: &[u32; 8], bit: u8) -> bool { v[(bit >> 5) as usize] & 1 << (bit & 31) != 0 }\n\nfn register_set_bit(v: &mut [u32; 8], bit: u8) { v[(bit >> 5) as usize] |= 1 << (bit & 31); }\n\nfn register_clear_bit(v: &mut [u32; 8], bit: u8) { v[(bit >> 5) as usize] &= !(1 << (bit & 31)); }\n\nfn register_get_highest_bit(v: &[u32; 8]) -> Option<u8> {\n    dbg_assert!(v.as_ptr().addr() & std::mem::align_of::<u64>() - 1 == 0);\n    let v: &[u64; 4] = unsafe { std::mem::transmute(v) };\n    for i in (0..4).rev() {\n        let word = v[i];\n\n        if word != 0 {\n            return Some(word.ilog2() as u8 | (i as u8) << 6);\n        }\n    }\n\n    None\n}\n"
  },
  {
    "path": "src/rust/cpu/arith.rs",
    "content": "use crate::cpu::cpu::*;\nuse crate::cpu::global_pointers::*;\nuse crate::cpu::memory;\nuse crate::cpu::misc_instr::{getaf, getcf, getzf};\n\nfn int_log2(x: i32) -> i32 { 31 - x.leading_zeros() as i32 }\n\nfn opsize_to_mask(op_size: i32) -> i32 {\n    dbg_assert!(op_size == OPSIZE_8 || op_size == OPSIZE_16 || op_size == OPSIZE_32);\n    (2 << op_size) - 1\n}\n\nunsafe fn add(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let res = (dest_operand + source_operand) & opsize_to_mask(op_size);\n    *last_op1 = dest_operand;\n    *last_result = res;\n    *last_op_size = op_size;\n    *flags_changed = FLAGS_ALL;\n    return res;\n}\nunsafe fn adc(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let cf = getcf() as i32;\n    let res = (dest_operand + source_operand + cf) & opsize_to_mask(op_size);\n    *last_op1 = dest_operand;\n    *last_result = res;\n    *last_op_size = op_size;\n    *flags_changed = FLAGS_ALL & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW;\n    *flags = *flags & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW\n        | (dest_operand ^ ((dest_operand ^ source_operand) & (source_operand ^ res))) >> op_size\n            & FLAG_CARRY\n        | (dest_operand ^ source_operand ^ res) & FLAG_ADJUST\n        | ((source_operand ^ res) & (dest_operand ^ res)) >> op_size << 11 & FLAG_OVERFLOW;\n    return res;\n}\nunsafe fn sub(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let res = (dest_operand - source_operand) & opsize_to_mask(op_size);\n    *last_op1 = dest_operand;\n    *last_result = res;\n    *last_op_size = op_size;\n    *flags_changed = FLAGS_ALL | FLAG_SUB;\n    return res;\n}\nunsafe fn sbb(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let cf = getcf() as i32;\n    let res = (dest_operand - source_operand - cf) & opsize_to_mask(op_size);\n    *last_op1 = dest_operand;\n    *last_result = res;\n    *last_op_size = op_size;\n    *flags_changed = FLAGS_ALL & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW | FLAG_SUB;\n    *flags = *flags & !FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW\n        | (res ^ ((res ^ source_operand) & (source_operand ^ dest_operand))) >> op_size\n            & FLAG_CARRY\n        | (dest_operand ^ source_operand ^ res) & FLAG_ADJUST\n        | ((source_operand ^ dest_operand) & (res ^ dest_operand)) >> op_size << 11 & FLAG_OVERFLOW;\n    return res;\n}\npub unsafe fn add8(x: i32, y: i32) -> i32 {\n    dbg_assert!(x >= 0 && x < 0x100);\n    dbg_assert!(y >= 0 && y < 0x100);\n    return add(x, y, OPSIZE_8);\n}\n#[no_mangle]\npub unsafe fn add16(x: i32, y: i32) -> i32 {\n    dbg_assert!(x >= 0 && x < 0x10000);\n    dbg_assert!(y >= 0 && y < 0x10000);\n    return add(x, y, OPSIZE_16);\n}\npub unsafe fn add32(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_32); }\npub unsafe fn sub8(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn sub16(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_16); }\npub unsafe fn sub32(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_32); }\n#[no_mangle]\npub unsafe fn adc8(x: i32, y: i32) -> i32 { return adc(x, y, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn adc16(x: i32, y: i32) -> i32 { return adc(x, y, OPSIZE_16); }\npub unsafe fn adc32(x: i32, y: i32) -> i32 { return adc(x, y, OPSIZE_32); }\n#[no_mangle]\npub unsafe fn sbb8(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn sbb16(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_16); }\npub unsafe fn sbb32(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_32); }\npub unsafe fn cmp8(x: i32, y: i32) {\n    dbg_assert!(x >= 0 && x < 0x100);\n    dbg_assert!(y >= 0 && y < 0x100);\n    sub(x, y, OPSIZE_8);\n}\npub unsafe fn cmp16(x: i32, y: i32) {\n    dbg_assert!(x >= 0 && x < 0x10000);\n    dbg_assert!(y >= 0 && y < 0x10000);\n    sub(x, y, OPSIZE_16);\n}\npub unsafe fn cmp32(x: i32, y: i32) { sub(x, y, OPSIZE_32); }\nunsafe fn inc(dest_operand: i32, op_size: i32) -> i32 {\n    *flags = *flags & !1 | getcf() as i32;\n    let res = (dest_operand + 1) & opsize_to_mask(op_size);\n    *last_op1 = dest_operand;\n    *last_result = res;\n    *last_op_size = op_size;\n    *flags_changed = FLAGS_ALL & !1;\n    return res;\n}\nunsafe fn dec(dest_operand: i32, op_size: i32) -> i32 {\n    *flags = *flags & !1 | getcf() as i32;\n    let res = (dest_operand - 1) & opsize_to_mask(op_size);\n    *last_op1 = dest_operand;\n    *last_result = res;\n    *last_op_size = op_size;\n    *flags_changed = FLAGS_ALL & !1 | FLAG_SUB;\n    return res;\n}\n#[no_mangle]\npub unsafe fn inc8(x: i32) -> i32 { return inc(x, OPSIZE_8); }\npub unsafe fn inc16(x: i32) -> i32 { return inc(x, OPSIZE_16); }\npub unsafe fn inc32(x: i32) -> i32 { return inc(x, OPSIZE_32); }\n#[no_mangle]\npub unsafe fn dec8(x: i32) -> i32 { return dec(x, OPSIZE_8); }\npub unsafe fn dec16(x: i32) -> i32 { return dec(x, OPSIZE_16); }\npub unsafe fn dec32(x: i32) -> i32 { return dec(x, OPSIZE_32); }\n\nunsafe fn neg(dest_operand: i32, op_size: i32) -> i32 { sub(0, dest_operand, op_size) }\n#[no_mangle]\npub unsafe fn not8(x: i32) -> i32 { return !x; }\n#[no_mangle]\npub unsafe fn neg8(x: i32) -> i32 { return neg(x, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn neg16(x: i32) -> i32 { return neg(x, OPSIZE_16); }\npub unsafe fn neg32(x: i32) -> i32 { return neg(x, OPSIZE_32); }\n\n#[no_mangle]\npub unsafe fn mul8(source_operand: i32) {\n    let result = source_operand * read_reg8(AL);\n    write_reg16(AX, result);\n    *last_result = result & 255;\n    *last_op_size = OPSIZE_8;\n    if result < 256 {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    else {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n}\n#[no_mangle]\npub unsafe fn imul8(source_operand: i32) {\n    let result = source_operand * (read_reg8(AL) << 24 >> 24);\n    write_reg16(AX, result);\n    *last_result = result & 255;\n    *last_op_size = OPSIZE_8;\n    if result > 127 || result < -128 {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    else {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n}\n#[no_mangle]\npub unsafe fn mul16(source_operand: u32) {\n    let result = source_operand * read_reg16(AX) as u32;\n    let high_result = result >> 16;\n    write_reg16(AX, result as i32);\n    write_reg16(DX, high_result as i32);\n    *last_result = (result & 0xFFFF) as i32;\n    *last_op_size = OPSIZE_16;\n    if high_result == 0 {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    else {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n}\n#[no_mangle]\npub unsafe fn imul16(source_operand: i32) {\n    let result = source_operand * (read_reg16(AX) << 16 >> 16);\n    write_reg16(AX, result);\n    write_reg16(DX, result >> 16);\n    *last_result = result & 0xFFFF;\n    *last_op_size = OPSIZE_16;\n    if result > 32767 || result < -32768 {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    else {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n}\n#[no_mangle]\npub unsafe fn imul_reg16(mut operand1: i32, mut operand2: i32) -> i32 {\n    operand1 = operand1 << 16 >> 16;\n    operand2 = operand2 << 16 >> 16;\n    let result = operand1 * operand2;\n    *last_result = result & 0xFFFF;\n    *last_op_size = OPSIZE_16;\n    if result > 32767 || result < -32768 {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    else {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n    return result;\n}\n#[no_mangle]\npub unsafe fn mul32(source_operand: i32) {\n    let dest_operand = read_reg32(EAX);\n    let result = (dest_operand as u32 as u64) * (source_operand as u32 as u64);\n    let result_low = result as i32;\n    let result_high = (result >> 32) as i32;\n    write_reg32(EAX, result_low);\n    write_reg32(EDX, result_high);\n    *last_result = result_low;\n    *last_op_size = OPSIZE_32;\n    if result_high == 0 {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    else {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n}\npub unsafe fn imul32(source_operand: i32) {\n    let dest_operand = read_reg32(EAX);\n    let result = dest_operand as i64 * source_operand as i64;\n    let result_low = result as i32;\n    let result_high = (result >> 32) as i32;\n    write_reg32(EAX, result_low);\n    write_reg32(EDX, result_high);\n    *last_result = result_low;\n    *last_op_size = OPSIZE_32;\n    if result_high == result_low >> 31 {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    else {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n}\npub unsafe fn imul_reg32(operand1: i32, operand2: i32) -> i32 {\n    let result = operand1 as i64 * operand2 as i64;\n    let result_low = result as i32;\n    let result_high = (result >> 32) as i32;\n    *last_result = result_low;\n    *last_op_size = OPSIZE_32;\n    if result_high == result_low >> 31 {\n        *flags &= !1 & !FLAG_OVERFLOW\n    }\n    else {\n        *flags |= 1 | FLAG_OVERFLOW\n    }\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n    return result_low;\n}\n\n#[no_mangle]\npub unsafe fn xadd8(source_operand: i32, reg: i32) -> i32 {\n    let tmp = read_reg8(reg);\n    write_reg8(reg, source_operand);\n    return add(source_operand, tmp, OPSIZE_8);\n}\n#[no_mangle]\npub unsafe fn xadd16(source_operand: i32, reg: i32) -> i32 {\n    let tmp = read_reg16(reg);\n    write_reg16(reg, source_operand);\n    return add(source_operand, tmp, OPSIZE_16);\n}\npub unsafe fn xadd32(source_operand: i32, reg: i32) -> i32 {\n    let tmp = read_reg32(reg);\n    write_reg32(reg, source_operand);\n    return add(source_operand, tmp, OPSIZE_32);\n}\n\n#[no_mangle]\npub unsafe fn cmpxchg8(data: i32, r: i32) -> i32 {\n    cmp8(read_reg8(AL), data);\n    if getzf() {\n        read_reg8(r)\n    }\n    else {\n        write_reg8(AL, data);\n        data\n    }\n}\n#[no_mangle]\npub unsafe fn cmpxchg16(data: i32, r: i32) -> i32 {\n    cmp16(read_reg16(AX), data);\n    if getzf() {\n        read_reg16(r)\n    }\n    else {\n        write_reg16(AX, data);\n        data\n    }\n}\npub unsafe fn cmpxchg32(data: i32, r: i32) -> i32 {\n    cmp32(read_reg32(EAX), data);\n    if getzf() {\n        read_reg32(r)\n    }\n    else {\n        write_reg32(EAX, data);\n        data\n    }\n}\n\n#[no_mangle]\npub unsafe fn bcd_daa() {\n    let old_al = read_reg8(AL);\n    let old_cf = getcf();\n    let old_af = getaf();\n    *flags &= !1 & !FLAG_ADJUST;\n    if old_al & 15 > 9 || old_af {\n        write_reg8(AL, read_reg8(AL) + 6);\n        *flags |= FLAG_ADJUST\n    }\n    if old_al > 153 || old_cf {\n        write_reg8(AL, read_reg8(AL) + 96);\n        *flags |= 1\n    }\n    *last_result = read_reg8(AL);\n    *last_op_size = OPSIZE_8;\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;\n}\n#[no_mangle]\npub unsafe fn bcd_das() {\n    let old_al = read_reg8(AL);\n    let old_cf = getcf();\n    *flags &= !1;\n    if old_al & 15 > 9 || getaf() {\n        write_reg8(AL, read_reg8(AL) - 6);\n        *flags |= FLAG_ADJUST;\n        *flags = *flags & !1 | old_cf as i32 | (old_al < 6) as i32\n    }\n    else {\n        *flags &= !FLAG_ADJUST\n    }\n    if old_al > 153 || old_cf {\n        write_reg8(AL, read_reg8(AL) - 96);\n        *flags |= 1\n    }\n    *last_result = read_reg8(AL);\n    *last_op_size = OPSIZE_8;\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;\n}\n#[no_mangle]\npub unsafe fn bcd_aad(imm8: i32) {\n    let result = read_reg8(AL) + read_reg8(AH) * imm8;\n    *last_result = result & 255;\n    write_reg16(AX, *last_result);\n    *last_op_size = OPSIZE_8;\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;\n    *flags &= !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;\n    if result > 0xFFFF {\n        *flags |= 1\n    };\n}\n#[no_mangle]\npub unsafe fn bcd_aam(imm8: i32) {\n    // ascii adjust after multiplication\n    if imm8 == 0 {\n        trigger_de();\n    }\n    else {\n        let temp = read_reg8(AL);\n        write_reg8(AH, temp as i32 / imm8);\n        write_reg8(AL, temp as i32 % imm8);\n        *last_result = read_reg8(AL);\n        *last_op_size = OPSIZE_8;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_ADJUST & !FLAG_OVERFLOW;\n        *flags &= !1 & !FLAG_ADJUST & !FLAG_OVERFLOW\n    };\n}\n#[no_mangle]\npub unsafe fn bcd_aaa() {\n    if read_reg8(AL) & 15 > 9 || getaf() {\n        write_reg16(AX, read_reg16(AX) + 6);\n        write_reg8(AH, read_reg8(AH) + 1);\n        *flags |= FLAG_ADJUST | 1\n    }\n    else {\n        *flags &= !FLAG_ADJUST & !1\n    }\n    write_reg8(AL, read_reg8(AL) & 15);\n    *flags_changed &= !FLAG_ADJUST & !1;\n}\n#[no_mangle]\npub unsafe fn bcd_aas() {\n    if read_reg8(AL) & 15 > 9 || getaf() {\n        write_reg16(AX, read_reg16(AX) - 6);\n        write_reg8(AH, read_reg8(AH) - 1);\n        *flags |= FLAG_ADJUST | 1\n    }\n    else {\n        *flags &= !FLAG_ADJUST & !1\n    }\n    write_reg8(AL, read_reg8(AL) & 15);\n    *flags_changed &= !FLAG_ADJUST & !1;\n}\nunsafe fn and(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let result = dest_operand & source_operand;\n    *last_result = result;\n    *last_op_size = op_size;\n    *flags &= !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;\n    return result;\n}\nunsafe fn or(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let result = dest_operand | source_operand;\n    *last_result = result;\n    *last_op_size = op_size;\n    *flags &= !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;\n    return result;\n}\nunsafe fn xor(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {\n    let result = dest_operand ^ source_operand;\n    *last_result = result;\n    *last_op_size = op_size;\n    *flags &= !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;\n    *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW & !FLAG_ADJUST;\n    return result;\n}\npub unsafe fn and8(x: i32, y: i32) -> i32 { return and(x, y, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn and16(x: i32, y: i32) -> i32 { return and(x, y, OPSIZE_16); }\npub unsafe fn and32(x: i32, y: i32) -> i32 { return and(x, y, OPSIZE_32); }\npub unsafe fn test8(x: i32, y: i32) { and(x, y, OPSIZE_8); }\npub unsafe fn test16(x: i32, y: i32) { and(x, y, OPSIZE_16); }\npub unsafe fn test32(x: i32, y: i32) { and(x, y, OPSIZE_32); }\npub unsafe fn or8(x: i32, y: i32) -> i32 { return or(x, y, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn or16(x: i32, y: i32) -> i32 { return or(x, y, OPSIZE_16); }\npub unsafe fn or32(x: i32, y: i32) -> i32 { return or(x, y, OPSIZE_32); }\npub unsafe fn xor8(x: i32, y: i32) -> i32 { return xor(x, y, OPSIZE_8); }\n#[no_mangle]\npub unsafe fn xor16(x: i32, y: i32) -> i32 { return xor(x, y, OPSIZE_16); }\npub unsafe fn xor32(x: i32, y: i32) -> i32 { return xor(x, y, OPSIZE_32); }\n\n#[no_mangle]\npub unsafe fn rol8(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        count &= 7;\n        let result = dest_operand << count | dest_operand >> 8 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result & 1\n            | (result << 11 ^ result << 4) & FLAG_OVERFLOW;\n        return result & 0xFF;\n    };\n}\n#[no_mangle]\npub unsafe fn rol16(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        count &= 15;\n        let result = dest_operand << count | dest_operand >> 16 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result & 1\n            | (result << 11 ^ result >> 4) & FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\n#[no_mangle]\npub unsafe fn rol32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let result = ((dest_operand << count) as u32 | dest_operand as u32 >> 32 - count) as i32;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result & 1\n            | (result << 11 ^ result >> 20) & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn rcl8(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    count %= 9;\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let result =\n            dest_operand << count | (getcf() as i32) << count - 1 | dest_operand >> 9 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 8 & 1\n            | (result << 3 ^ result << 4) & FLAG_OVERFLOW;\n        return result & 0xFF;\n    };\n}\n#[no_mangle]\npub unsafe fn rcl16(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    count %= 17;\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let result =\n            dest_operand << count | (getcf() as i32) << count - 1 | dest_operand >> 17 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 16 & 1\n            | (result >> 5 ^ result >> 4) & FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\n#[no_mangle]\npub unsafe fn rcl32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let mut result = dest_operand << count | (getcf() as i32) << count - 1;\n        if count > 1 {\n            result = (result as u32 | dest_operand as u32 >> 33 - count) as i32\n        }\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        let b = (dest_operand as u32 >> 32 - count & 1) as i32;\n        *flags = (*flags & !1 & !FLAG_OVERFLOW | b) | (b << 11 ^ result >> 20) & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn ror8(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        count &= 7;\n        let result = dest_operand >> count | dest_operand << 8 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 7 & 1\n            | (result << 4 ^ result << 5) & FLAG_OVERFLOW;\n        return result & 0xFF;\n    };\n}\n#[no_mangle]\npub unsafe fn ror16(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        count &= 15;\n        let result = dest_operand >> count | dest_operand << 16 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 15 & 1\n            | (result >> 4 ^ result >> 3) & FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\n#[no_mangle]\npub unsafe fn ror32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let result = (dest_operand as u32 >> count | (dest_operand << 32 - count) as u32) as i32;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 31 & 1\n            | (result >> 20 ^ result >> 19) & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn rcr8(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    count %= 9;\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let result =\n            dest_operand >> count | (getcf() as i32) << 8 - count | dest_operand << 9 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 8 & 1\n            | (result << 4 ^ result << 5) & FLAG_OVERFLOW;\n        return result & 0xFF;\n    };\n}\n#[no_mangle]\npub unsafe fn rcr16(dest_operand: i32, mut count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    count %= 17;\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let result =\n            dest_operand >> count | (getcf() as i32) << 16 - count | dest_operand << 17 - count;\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 16 & 1\n            | (result >> 4 ^ result >> 3) & FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\n#[no_mangle]\npub unsafe fn rcr32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if 0 == count {\n        return dest_operand;\n    }\n    else {\n        let mut result =\n            (dest_operand as u32 >> count | ((getcf() as i32) << 32 - count) as u32) as i32;\n        if count > 1 {\n            result |= dest_operand << 33 - count\n        }\n        *flags_changed &= !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | dest_operand >> count - 1 & 1\n            | (result >> 20 ^ result >> 19) & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn div8(source_operand: u32) {\n    if source_operand == 0 {\n        trigger_de();\n        return;\n    }\n    let target_operand = read_reg16(AX) as u32;\n    let result = target_operand / source_operand;\n    if result >= 0x100 {\n        trigger_de();\n        return;\n    }\n    write_reg8(AL, result as i32);\n    write_reg8(AH, (target_operand % source_operand) as i32);\n}\n\n#[no_mangle]\npub unsafe fn idiv8(source_operand: i32) {\n    if source_operand == 0 {\n        trigger_de();\n        return;\n    }\n    let target_operand = read_reg16(AX) << 16 >> 16;\n    let result = target_operand / source_operand;\n    if result >= 0x80 || result < -0x80 {\n        trigger_de();\n        return;\n    }\n    write_reg8(AL, result);\n    write_reg8(AH, target_operand % source_operand);\n}\n\n#[no_mangle]\npub unsafe fn div16_without_fault(source_operand: u32) -> bool {\n    let target_operand = (read_reg16(AX) | read_reg16(DX) << 16) as u32;\n    let result = match target_operand.checked_div(source_operand) {\n        None => return false,\n        Some(r) => r,\n    };\n    if result >= 0x10000 {\n        return false;\n    }\n    write_reg16(AX, result as i32);\n    write_reg16(DX, (target_operand % source_operand) as i32);\n    return true;\n}\npub unsafe fn div16(source_operand: u32) {\n    if !div16_without_fault(source_operand) {\n        trigger_de()\n    }\n}\n#[no_mangle]\npub unsafe fn idiv16_without_fault(source_operand: i32) -> bool {\n    let target_operand = read_reg16(AX) | read_reg16(DX) << 16;\n    let result = match target_operand.checked_div(source_operand) {\n        None => return false,\n        Some(r) => r,\n    };\n    if result >= 0x8000 || result < -0x8000 {\n        return false;\n    }\n    write_reg16(AX, result);\n    write_reg16(DX, (target_operand % source_operand) as i32);\n    return true;\n}\npub unsafe fn idiv16(source_operand: i32) {\n    if !idiv16_without_fault(source_operand) {\n        trigger_de()\n    }\n}\n\n#[no_mangle]\npub unsafe fn div32_without_fault(source_operand: u32) -> bool {\n    let source_operand = source_operand as u64;\n    let target_low = read_reg32(EAX) as u32;\n    let target_high = read_reg32(EDX) as u32;\n    let target_operand = (target_high as u64) << 32 | target_low as u64;\n    let result = match target_operand.checked_div(source_operand) {\n        None => return false,\n        Some(r) => r,\n    };\n    if result > 0xFFFFFFFF {\n        return false;\n    }\n    let modulo = target_operand % source_operand;\n    write_reg32(EAX, result as i32);\n    write_reg32(EDX, modulo as i32);\n    return true;\n}\npub unsafe fn div32(source_operand: u32) {\n    if !div32_without_fault(source_operand) {\n        trigger_de()\n    }\n}\n#[no_mangle]\npub unsafe fn idiv32_without_fault(source_operand: i32) -> bool {\n    let source_operand = source_operand as i64;\n    let target_low = read_reg32(EAX) as u32;\n    let target_high = read_reg32(EDX) as u32;\n    let target_operand = (target_high as i64) << 32 | target_low as i64;\n    let result = match target_operand.checked_div(source_operand) {\n        None => return false,\n        Some(r) => r,\n    };\n    if result < -0x80000000 || result > 0x7FFFFFFF {\n        return false;\n    }\n    let modulo = target_operand % source_operand;\n    write_reg32(EAX, result as i32);\n    write_reg32(EDX, modulo as i32);\n    return true;\n}\npub unsafe fn idiv32(source_operand: i32) {\n    if !idiv32_without_fault(source_operand) {\n        trigger_de()\n    }\n}\n\n#[no_mangle]\npub unsafe fn shl8(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = dest_operand << count;\n        *last_result = result;\n        *last_op_size = OPSIZE_8;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 8 & 1\n            | (result << 3 ^ result << 4) & FLAG_OVERFLOW;\n        return result & 0xFF;\n    };\n}\n#[no_mangle]\npub unsafe fn shl16(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = dest_operand << count;\n        *last_result = result;\n        *last_op_size = OPSIZE_16;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | result >> 16 & 1\n            | (result >> 5 ^ result >> 4) & FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\npub unsafe fn shl32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = dest_operand << count;\n        *last_result = result;\n        *last_op_size = OPSIZE_32;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        let b = dest_operand >> 32 - count & 1;\n        *flags = *flags & !1 & !FLAG_OVERFLOW | b | (b ^ result >> 31) << 11 & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn shr8(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = dest_operand >> count;\n        *last_result = result;\n        *last_op_size = OPSIZE_8;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | dest_operand >> count - 1 & 1\n            | (dest_operand >> 7 & 1) << 11 & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn shr16(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = dest_operand >> count;\n        *last_result = result;\n        *last_op_size = OPSIZE_16;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !1 & !FLAG_OVERFLOW\n            | dest_operand >> count - 1 & 1\n            | dest_operand >> 4 & FLAG_OVERFLOW;\n        return result;\n    };\n}\npub unsafe fn shr32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = (dest_operand as u32 >> count) as i32;\n        *last_result = result;\n        *last_op_size = OPSIZE_32;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = (*flags & !1 & !FLAG_OVERFLOW)\n            | (dest_operand as u32 >> count - 1 & 1) as i32\n            | (dest_operand >> 20 & FLAG_OVERFLOW);\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn sar8(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result;\n        if count < 8 {\n            result = dest_operand << 24 >> count + 24;\n            // of is zero\n            *flags = *flags & !1 & !FLAG_OVERFLOW | dest_operand >> count - 1 & 1\n        }\n        else {\n            result = dest_operand << 24 >> 31;\n            *flags = *flags & !1 & !FLAG_OVERFLOW | result & 1\n        }\n        *last_result = result;\n        *last_op_size = OPSIZE_8;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        return result & 0xFF;\n    };\n}\n#[no_mangle]\npub unsafe fn sar16(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result;\n        if count < 16 {\n            result = dest_operand << 16 >> count + 16;\n            *flags = *flags & !1 & !FLAG_OVERFLOW | dest_operand >> count - 1 & 1\n        }\n        else {\n            result = dest_operand << 16 >> 31;\n            *flags = *flags & !1 & !FLAG_OVERFLOW | result & 1\n        }\n        *last_result = result;\n        *last_op_size = OPSIZE_16;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\npub unsafe fn sar32(dest_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = dest_operand >> count;\n        *last_result = result;\n        *last_op_size = OPSIZE_32;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = (*flags & !1 & !FLAG_OVERFLOW) | (dest_operand as u32 >> count - 1 & 1) as i32;\n        return result;\n    };\n}\n\n#[no_mangle]\npub unsafe fn shrd16(dest_operand: i32, source_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result;\n        if count <= 16 {\n            result = dest_operand >> count | source_operand << 16 - count;\n            *flags = *flags & !1 | dest_operand >> count - 1 & 1\n        }\n        else {\n            result = dest_operand << 32 - count | source_operand >> count - 16;\n            *flags = *flags & !1 | source_operand >> count - 17 & 1\n        }\n        *last_result = result;\n        *last_op_size = OPSIZE_16;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !FLAG_OVERFLOW | (result ^ dest_operand) >> 4 & FLAG_OVERFLOW;\n        return result & 0xFFFF;\n    };\n}\n#[no_mangle]\npub unsafe fn shrd32(dest_operand: i32, source_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = (dest_operand as u32 >> count | (source_operand << 32 - count) as u32) as i32;\n        *last_result = result;\n        *last_op_size = OPSIZE_32;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = ((*flags & !1 & !FLAG_OVERFLOW) | (dest_operand as u32 >> count - 1 & 1) as i32)\n            | (result ^ dest_operand) >> 20 & FLAG_OVERFLOW;\n        return result;\n    };\n}\n#[no_mangle]\npub unsafe fn shld16(dest_operand: i32, source_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result;\n        if count <= 16 {\n            result = ((dest_operand << count) as u32 | source_operand as u32 >> 16 - count) as i32;\n            *flags = (*flags & !1) | (dest_operand as u32 >> 16 - count & 1) as i32;\n        }\n        else {\n            result = dest_operand >> 32 - count | source_operand << count - 16;\n            *flags = (*flags & !1) | (source_operand as u32 >> 32 - count & 1) as i32;\n        }\n        *last_result = result;\n        *last_op_size = OPSIZE_16;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = *flags & !FLAG_OVERFLOW | (*flags & 1 ^ result >> 15 & 1) << 11;\n        return result & 0xFFFF;\n    };\n}\n#[no_mangle]\npub unsafe fn shld32(dest_operand: i32, source_operand: i32, count: i32) -> i32 {\n    dbg_assert!(count >= 0 && count < 32);\n    if count == 0 {\n        return dest_operand;\n    }\n    else {\n        let result = ((dest_operand << count) as u32 | source_operand as u32 >> 32 - count) as i32;\n        *last_result = result;\n        *last_op_size = OPSIZE_32;\n        *flags_changed = FLAGS_ALL & !1 & !FLAG_OVERFLOW;\n        *flags = (*flags & !1) | (dest_operand as u32 >> 32 - count & 1) as i32;\n        if count == 1 {\n            *flags = *flags & !FLAG_OVERFLOW | (*flags & 1 ^ result >> 31 & 1) << 11\n        }\n        else {\n            *flags &= !FLAG_OVERFLOW\n        }\n        return result;\n    };\n}\n\npub unsafe fn bt_reg(bit_base: i32, bit_offset: i32) {\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n}\npub unsafe fn btc_reg(bit_base: i32, bit_offset: i32) -> i32 {\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n    return bit_base ^ 1 << bit_offset;\n}\npub unsafe fn bts_reg(bit_base: i32, bit_offset: i32) -> i32 {\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n    return bit_base | 1 << bit_offset;\n}\npub unsafe fn btr_reg(bit_base: i32, bit_offset: i32) -> i32 {\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n    return bit_base & !(1 << bit_offset);\n}\n\npub unsafe fn bt_mem(virt_addr: i32, mut bit_offset: i32) {\n    let bit_base = return_on_pagefault!(safe_read8(virt_addr + (bit_offset >> 3)));\n    bit_offset &= 7;\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n}\npub unsafe fn btc_mem(virt_addr: i32, mut bit_offset: i32) {\n    let phys_addr = return_on_pagefault!(translate_address_write(virt_addr + (bit_offset >> 3)));\n    let bit_base = memory::read8(phys_addr);\n    bit_offset &= 7;\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n    memory::write8(phys_addr, bit_base ^ 1 << bit_offset);\n}\npub unsafe fn btr_mem(virt_addr: i32, mut bit_offset: i32) {\n    let phys_addr = return_on_pagefault!(translate_address_write(virt_addr + (bit_offset >> 3)));\n    let bit_base = memory::read8(phys_addr);\n    bit_offset &= 7;\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n    memory::write8(phys_addr, bit_base & !(1 << bit_offset));\n}\npub unsafe fn bts_mem(virt_addr: i32, mut bit_offset: i32) {\n    let phys_addr = return_on_pagefault!(translate_address_write(virt_addr + (bit_offset >> 3)));\n    let bit_base = memory::read8(phys_addr);\n    bit_offset &= 7;\n    *flags = *flags & !1 | bit_base >> bit_offset & 1;\n    *flags_changed &= !1;\n    memory::write8(phys_addr, bit_base | 1 << bit_offset);\n}\n\n#[no_mangle]\npub unsafe fn bsf16(old: i32, bit_base: i32) -> i32 {\n    *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;\n    *flags &= !FLAG_CARRY;\n    *last_op_size = OPSIZE_16;\n    if bit_base == 0 {\n        *flags |= FLAG_ZERO;\n        *last_result = bit_base;\n        // not defined in the docs, but value doesn't change on my intel machine\n        return old;\n    }\n    else {\n        *flags &= !FLAG_ZERO;\n        *last_result = int_log2(-bit_base & bit_base);\n        return *last_result;\n    };\n}\n#[no_mangle]\npub unsafe fn bsf32(old: i32, bit_base: i32) -> i32 {\n    *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;\n    *flags &= !FLAG_CARRY;\n    *last_op_size = OPSIZE_32;\n    if bit_base == 0 {\n        *flags |= FLAG_ZERO;\n        *last_result = bit_base;\n        return old;\n    }\n    else {\n        *flags &= !FLAG_ZERO;\n        *last_result = int_log2(-bit_base & bit_base);\n        return *last_result;\n    };\n}\n#[no_mangle]\npub unsafe fn bsr16(old: i32, bit_base: i32) -> i32 {\n    *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;\n    *flags &= !FLAG_CARRY;\n    *last_op_size = OPSIZE_16;\n    if bit_base == 0 {\n        *flags |= FLAG_ZERO;\n        *last_result = bit_base;\n        return old;\n    }\n    else {\n        *flags &= !FLAG_ZERO;\n        *last_result = int_log2(bit_base);\n        return *last_result;\n    };\n}\n#[no_mangle]\npub unsafe fn bsr32(old: i32, bit_base: i32) -> i32 {\n    *flags_changed = FLAGS_ALL & !FLAG_ZERO & !FLAG_CARRY;\n    *flags &= !FLAG_CARRY;\n    *last_op_size = OPSIZE_32;\n    if bit_base == 0 {\n        *flags |= FLAG_ZERO;\n        *last_result = bit_base;\n        return old;\n    }\n    else {\n        *flags &= !FLAG_ZERO;\n        *last_result = int_log2(bit_base);\n        return *last_result;\n    };\n}\n#[no_mangle]\npub unsafe fn popcnt(v: i32) -> i32 {\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    if 0 != v {\n        return v.count_ones() as i32;\n    }\n    else {\n        *flags |= FLAG_ZERO;\n        return 0;\n    };\n}\n\npub unsafe fn saturate_sw_to_ub(v: u16) -> u8 {\n    let mut ret = v;\n    if ret >= 32768 {\n        ret = 0\n    }\n    else if ret > 255 {\n        ret = 255\n    }\n    return ret as u8;\n}\npub unsafe fn saturate_sw_to_sb(v: i32) -> u8 {\n    dbg_assert!(v as u32 & 0xFFFF_0000 == 0);\n    let mut ret = v;\n    if ret > 65408 {\n        ret = ret & 255\n    }\n    else if ret > 32767 {\n        ret = 128\n    }\n    else if ret > 127 {\n        ret = 127\n    }\n    dbg_assert!(ret as u32 & 0xFFFF_FF00 == 0);\n    return ret as u8;\n}\npub unsafe fn saturate_sd_to_sw(v: u32) -> u16 {\n    let mut ret = v;\n    if ret > 4294934528 {\n        ret = ret & 0xFFFF\n    }\n    else if ret > 0x7FFFFFFF {\n        ret = 32768\n    }\n    else if ret > 32767 {\n        ret = 32767\n    }\n    dbg_assert!(ret & 0xFFFF_0000 == 0);\n    return ret as u16;\n}\npub unsafe fn saturate_sd_to_sb(v: u32) -> i8 {\n    let mut ret = v;\n    if ret > 0xFFFFFF80 {\n        ret = ret & 255\n    }\n    else if ret > 0x7FFFFFFF {\n        ret = 128\n    }\n    else if ret > 127 {\n        ret = 127\n    }\n    dbg_assert!(ret & 0xFFFF_FF00 == 0);\n    return ret as i8;\n}\npub unsafe fn saturate_sd_to_ub(v: i32) -> i32 {\n    let mut ret = v;\n    if ret < 0 {\n        ret = 0\n    }\n    dbg_assert!(ret as u32 & 0xFFFF_FF00 == 0);\n    return ret;\n}\npub unsafe fn saturate_ud_to_ub(v: u32) -> u8 {\n    let mut ret = v;\n    if ret > 255 {\n        ret = 255\n    }\n    dbg_assert!(ret & 0xFFFF_FF00 == 0);\n    return ret as u8;\n}\npub unsafe fn saturate_uw(v: u32) -> u16 {\n    let mut ret = v;\n    if ret > 0x7FFFFFFF {\n        ret = 0\n    }\n    else if ret > 0xFFFF {\n        ret = 0xFFFF\n    }\n    dbg_assert!(ret & 0xFFFF_0000 == 0);\n    return ret as u16;\n}\n"
  },
  {
    "path": "src/rust/cpu/call_indirect.rs",
    "content": "#[no_mangle]\npub fn call_indirect1(f: fn(u16), x: u16) { f(x); }\n"
  },
  {
    "path": "src/rust/cpu/cpu.rs",
    "content": "#![allow(non_upper_case_globals)]\n\nuse crate::config;\nuse crate::cpu::fpu::fpu_set_tag_word;\nuse crate::cpu::global_pointers::*;\nuse crate::cpu::memory;\nuse crate::cpu::misc_instr::{\n    adjust_stack_reg, get_stack_pointer, getaf, getcf, getof, getpf, getsf, getzf, pop16, pop32s,\n    push16, push32,\n};\nuse crate::cpu::modrm::{resolve_modrm16, resolve_modrm32};\nuse crate::cpu::{apic, ioapic, pic};\nuse crate::dbg::dbg_trace;\nuse crate::gen;\nuse crate::jit;\nuse crate::jit::is_near_end_of_page;\nuse crate::opstats;\nuse crate::page::Page;\nuse crate::paging::OrPageFault;\nuse crate::prefix;\nuse crate::profiler;\nuse crate::profiler::stat;\nuse crate::softfloat;\nuse crate::state_flags::CachedStateFlags;\n\nuse std::collections::HashSet;\nuse std::ptr;\n\nmod wasm {\n    extern \"C\" {\n        pub fn call_indirect1(f: i32, x: u16);\n    }\n}\n\npub mod js {\n    extern \"C\" {\n        pub fn cpu_exception_hook(interrupt: i32) -> bool;\n        pub fn microtick() -> f64;\n        pub fn run_hardware_timers(acpi_enabled: bool, t: f64) -> f64;\n        pub fn cpu_event_halt();\n        pub fn stop_idling();\n\n        pub fn io_port_read8(port: i32) -> i32;\n        pub fn io_port_read16(port: i32) -> i32;\n        pub fn io_port_read32(port: i32) -> i32;\n\n        pub fn io_port_write8(port: i32, value: i32);\n        pub fn io_port_write16(port: i32, value: i32);\n        pub fn io_port_write32(port: i32, value: i32);\n\n        pub fn get_rand_int() -> i32;\n    }\n}\n\n/// The offset for our generated functions in the wasm table. Every index less than this is\n/// reserved for rustc's indirect functions\npub const WASM_TABLE_OFFSET: u32 = 1024;\n\n#[derive(Copy, Clone)]\n#[repr(C)]\n#[repr(align(16))]\npub union reg128 {\n    pub i8: [i8; 16],\n    pub i16: [i16; 8],\n    pub i32: [i32; 4],\n    pub i64: [i64; 2],\n    pub u8: [u8; 16],\n    pub u16: [u16; 8],\n    pub u32: [u32; 4],\n    pub u64: [u64; 2],\n    pub f32: [f32; 4],\n    pub f64: [f64; 2],\n}\n\npub const CHECK_MISSED_ENTRY_POINTS: bool = false;\n\npub const INTERPRETER_ITERATION_LIMIT: u32 = 100_001;\n\n// How often, in milliseconds, to yield to the browser for rendering and running events\npub const TIME_PER_FRAME: f64 = 1.0;\n\npub const FLAG_SUB: i32 = -0x8000_0000;\npub const FLAG_CARRY: i32 = 1;\npub const FLAG_PARITY: i32 = 4;\npub const FLAG_ADJUST: i32 = 16;\npub const FLAG_ZERO: i32 = 64;\npub const FLAG_SIGN: i32 = 128;\npub const FLAG_TRAP: i32 = 256;\npub const FLAG_INTERRUPT: i32 = 512;\npub const FLAG_DIRECTION: i32 = 1024;\npub const FLAG_OVERFLOW: i32 = 2048;\npub const FLAG_IOPL: i32 = 1 << 12 | 1 << 13;\npub const FLAG_NT: i32 = 1 << 14;\npub const FLAG_RF: i32 = 1 << 16;\npub const FLAG_VM: i32 = 1 << 17;\npub const FLAG_AC: i32 = 1 << 18;\npub const FLAG_VIF: i32 = 1 << 19;\npub const FLAG_VIP: i32 = 1 << 20;\npub const FLAG_ID: i32 = 1 << 21;\npub const FLAGS_DEFAULT: i32 = 1 << 1;\npub const FLAGS_MASK: i32 = FLAG_CARRY\n    | FLAG_PARITY\n    | FLAG_ADJUST\n    | FLAG_ZERO\n    | FLAG_SIGN\n    | FLAG_TRAP\n    | FLAG_INTERRUPT\n    | FLAG_DIRECTION\n    | FLAG_OVERFLOW\n    | FLAG_IOPL\n    | FLAG_NT\n    | FLAG_RF\n    | FLAG_VM\n    | FLAG_AC\n    | FLAG_VIF\n    | FLAG_VIP\n    | FLAG_ID;\npub const FLAGS_ALL: i32 =\n    FLAG_CARRY | FLAG_PARITY | FLAG_ADJUST | FLAG_ZERO | FLAG_SIGN | FLAG_OVERFLOW;\npub const OPSIZE_8: i32 = 7;\npub const OPSIZE_16: i32 = 15;\npub const OPSIZE_32: i32 = 31;\n\npub const EAX: i32 = 0;\npub const ECX: i32 = 1;\npub const EDX: i32 = 2;\npub const EBX: i32 = 3;\npub const ESP: i32 = 4;\npub const EBP: i32 = 5;\npub const ESI: i32 = 6;\npub const EDI: i32 = 7;\n\npub const AX: i32 = 0;\npub const CX: i32 = 1;\npub const DX: i32 = 2;\npub const BX: i32 = 3;\npub const SP: i32 = 4;\npub const BP: i32 = 5;\npub const SI: i32 = 6;\npub const DI: i32 = 7;\n\npub const AL: i32 = 0;\npub const CL: i32 = 1;\npub const DL: i32 = 2;\npub const BL: i32 = 3;\npub const AH: i32 = 4;\npub const CH: i32 = 5;\npub const DH: i32 = 6;\npub const BH: i32 = 7;\n\npub const ES: i32 = 0;\npub const CS: i32 = 1;\npub const SS: i32 = 2;\npub const DS: i32 = 3;\npub const FS: i32 = 4;\npub const GS: i32 = 5;\npub const TR: i32 = 6;\n\npub const LDTR: i32 = 7;\npub const PAGE_TABLE_PRESENT_MASK: i32 = 1 << 0;\npub const PAGE_TABLE_RW_MASK: i32 = 1 << 1;\npub const PAGE_TABLE_USER_MASK: i32 = 1 << 2;\npub const PAGE_TABLE_ACCESSED_MASK: i32 = 1 << 5;\npub const PAGE_TABLE_DIRTY_MASK: i32 = 1 << 6;\npub const PAGE_TABLE_PSE_MASK: i32 = 1 << 7;\npub const PAGE_TABLE_GLOBAL_MASK: i32 = 1 << 8;\npub const MMAP_BLOCK_BITS: i32 = 17;\npub const MMAP_BLOCK_SIZE: i32 = 1 << MMAP_BLOCK_BITS;\npub const CR0_PE: i32 = 1;\npub const CR0_MP: i32 = 1 << 1;\npub const CR0_EM: i32 = 1 << 2;\npub const CR0_TS: i32 = 1 << 3;\npub const CR0_ET: i32 = 1 << 4;\npub const CR0_WP: i32 = 1 << 16;\npub const CR0_AM: i32 = 1 << 18;\npub const CR0_NW: i32 = 1 << 29;\npub const CR0_CD: i32 = 1 << 30;\npub const CR0_PG: i32 = 1 << 31;\npub const CR4_VME: i32 = 1;\npub const CR4_PVI: i32 = 1 << 1;\npub const CR4_TSD: i32 = 1 << 2;\npub const CR4_PSE: i32 = 1 << 4;\npub const CR4_DE: i32 = 1 << 3;\npub const CR4_PAE: i32 = 1 << 5;\npub const CR4_PGE: i32 = 1 << 7;\npub const CR4_OSFXSR: i32 = 1 << 9;\npub const CR4_OSXMMEXCPT: i32 = 1 << 10;\npub const CR4_SMEP: i32 = 1 << 20;\n\npub const TSR_BACKLINK: i32 = 0x00;\npub const TSR_CR3: i32 = 0x1C;\npub const TSR_EIP: i32 = 0x20;\npub const TSR_EFLAGS: i32 = 0x24;\n\npub const TSR_EAX: i32 = 0x28;\npub const TSR_ECX: i32 = 0x2c;\npub const TSR_EDX: i32 = 0x30;\npub const TSR_EBX: i32 = 0x34;\npub const TSR_ESP: i32 = 0x38;\npub const TSR_EBP: i32 = 0x3c;\npub const TSR_ESI: i32 = 0x40;\npub const TSR_EDI: i32 = 0x44;\n\npub const TSR_ES: i32 = 0x48;\npub const TSR_CS: i32 = 0x4c;\npub const TSR_SS: i32 = 0x50;\npub const TSR_DS: i32 = 0x54;\npub const TSR_FS: i32 = 0x58;\npub const TSR_GS: i32 = 0x5c;\npub const TSR_LDT: i32 = 0x60;\n\npub const IA32_TIME_STAMP_COUNTER: i32 = 0x10;\npub const IA32_PLATFORM_ID: i32 = 0x17;\npub const IA32_APIC_BASE: i32 = 0x1B;\npub const MSR_TEST_CTRL: i32 = 0x33;\npub const MSR_SMI_COUNT: i32 = 0x34;\npub const IA32_FEAT_CTL: i32 = 0x3A;\npub const IA32_SPEC_CTRL: i32 = 0x48;\npub const IA32_BIOS_UPDT_TRIG: i32 = 0x79;\npub const IA32_BIOS_SIGN_ID: i32 = 0x8B;\npub const IA32_PMC0: i32 = 0xC1;\npub const IA32_PMC1: i32 = 0xC2;\npub const MSR_PLATFORM_INFO: i32 = 0xCE;\npub const MSR_TSX_FORCE_ABORT: i32 = 0x10F;\npub const IA32_TSX_CTRL: i32 = 0x122;\npub const IA32_MCU_OPT_CTRL: i32 = 0x123;\npub const MISC_FEATURE_ENABLES: i32 = 0x140;\npub const IA32_SYSENTER_CS: i32 = 0x174;\npub const IA32_SYSENTER_ESP: i32 = 0x175;\npub const IA32_SYSENTER_EIP: i32 = 0x176;\npub const IA32_MCG_CAP: i32 = 0x179;\npub const IA32_PERFEVTSEL0: i32 = 0x186;\npub const IA32_PERFEVTSEL1: i32 = 0x187;\npub const IA32_MISC_ENABLE: i32 = 0x1A0;\npub const IA32_PAT: i32 = 0x277;\npub const IA32_RTIT_CTL: i32 = 0x570;\npub const MSR_PKG_C2_RESIDENCY: i32 = 0x60D;\npub const IA32_KERNEL_GS_BASE: i32 = 0xC0000101u32 as i32;\npub const MSR_AMD64_LS_CFG: i32 = 0xC0011020u32 as i32;\npub const MSR_AMD64_DE_CFG: i32 = 0xC0011029u32 as i32;\n\npub const IA32_APIC_BASE_BSP: i32 = 1 << 8;\npub const IA32_APIC_BASE_EXTD: i32 = 1 << 10;\npub const IA32_APIC_BASE_EN: i32 = 1 << 11;\n\npub const IOAPIC_MEM_ADDRESS: u32 = 0xFEC00000;\npub const IOAPIC_MEM_SIZE: u32 = 32;\npub const APIC_MEM_ADDRESS: u32 = 0xFEE00000;\npub const APIC_MEM_SIZE: u32 = 0x1000;\n\npub const MXCSR_MASK: i32 = 0xffff;\npub const MXCSR_FZ: i32 = 1 << 15;\npub const MXCSR_DAZ: i32 = 1 << 6;\npub const MXCSR_RC_SHIFT: i32 = 13;\n\npub const VALID_TLB_ENTRY_MAX: i32 = 10000;\npub const TLB_VALID: i32 = 1 << 0;\npub const TLB_READONLY: i32 = 1 << 1;\npub const TLB_NO_USER: i32 = 1 << 2;\npub const TLB_IN_MAPPED_RANGE: i32 = 1 << 3;\npub const TLB_GLOBAL: i32 = 1 << 4;\npub const TLB_HAS_CODE: i32 = 1 << 5;\npub const IVT_SIZE: u32 = 0x400;\npub const CPU_EXCEPTION_DE: i32 = 0;\npub const CPU_EXCEPTION_DB: i32 = 1;\npub const CPU_EXCEPTION_NMI: i32 = 2;\npub const CPU_EXCEPTION_BP: i32 = 3;\npub const CPU_EXCEPTION_OF: i32 = 4;\npub const CPU_EXCEPTION_BR: i32 = 5;\npub const CPU_EXCEPTION_UD: i32 = 6;\npub const CPU_EXCEPTION_NM: i32 = 7;\npub const CPU_EXCEPTION_DF: i32 = 8;\npub const CPU_EXCEPTION_TS: i32 = 10;\npub const CPU_EXCEPTION_NP: i32 = 11;\npub const CPU_EXCEPTION_SS: i32 = 12;\npub const CPU_EXCEPTION_GP: i32 = 13;\npub const CPU_EXCEPTION_PF: i32 = 14;\npub const CPU_EXCEPTION_MF: i32 = 16;\npub const CPU_EXCEPTION_AC: i32 = 17;\npub const CPU_EXCEPTION_MC: i32 = 18;\npub const CPU_EXCEPTION_XM: i32 = 19;\npub const CPU_EXCEPTION_VE: i32 = 20;\n\npub const CHECK_TLB_INVARIANTS: bool = false;\n\npub const DEBUG: bool = cfg!(debug_assertions);\n\npub const LOOP_COUNTER: i32 = 100_003;\n\n// should probably be kept in sync with APIC_TIMER_FREQ in apic.js\npub const TSC_RATE: f64 = 1_000_000.0;\n\npub static mut cpuid_level: u32 = 0x16;\n\npub static mut jit_block_boundary: bool = false;\n\nconst TSC_ENABLE_IMPRECISE_BROWSER_WORKAROUND: bool = true;\n\n#[cfg(debug_assertions)]\nconst TSC_VERBOSE_LOGGING: bool = false;\n#[cfg(debug_assertions)]\npub static mut tsc_last_extra: u64 = 0;\n\n// the last value returned by rdtsc\npub static mut tsc_last_value: u64 = 0;\n// the smallest difference between two rdtsc readings (depends on the browser's performance.now resolution)\npub static mut tsc_resolution: u64 = u64::MAX;\n// how many times rdtsc was called and had to return the same value (due to browser's performance.now resolution)\npub static mut tsc_number_of_same_readings: u64 = 0;\n// how often rdtsc was previously called without its value changing, used for interpolating quick\n// consecutive calls between rdtsc (when it's called faster than the browser's performance.now\n// changes)\npub static mut tsc_speed: u64 = 1;\n\n// used for restoring the state\npub static mut tsc_offset: u64 = 0;\n\npub struct Code {\n    pub wasm_table_index: jit::WasmTableIndex,\n    pub state_flags: CachedStateFlags,\n    pub state_table: [u16; 0x1000],\n}\n\npub static mut tlb_data: [i32; 0x100000] = [0; 0x100000];\npub static mut tlb_code: [Option<ptr::NonNull<Code>>; 0x100000] = [None; 0x100000];\n\npub static mut valid_tlb_entries: [i32; 10000] = [0; 10000];\npub static mut valid_tlb_entries_count: i32 = 0;\n\npub static mut in_jit: bool = false;\n\npub static mut jit_fault: Option<(i32, Option<i32>)> = None;\n\npub enum LastJump {\n    Interrupt {\n        phys_addr: u32,\n        int: u8,\n        software: bool,\n        error: Option<u32>,\n    },\n    Compiled {\n        phys_addr: u32,\n    },\n    Interpreted {\n        phys_addr: u32,\n    },\n    None,\n}\nimpl LastJump {\n    pub fn phys_address(&self) -> Option<u32> {\n        match self {\n            LastJump::Interrupt { phys_addr, .. } => Some(*phys_addr),\n            LastJump::Compiled { phys_addr } => Some(*phys_addr),\n            LastJump::Interpreted { phys_addr } => Some(*phys_addr),\n            LastJump::None => None,\n        }\n    }\n    pub fn name(&self) -> &'static str {\n        match self {\n            LastJump::Interrupt { .. } => \"interrupt\",\n            LastJump::Compiled { .. } => \"compiled\",\n            LastJump::Interpreted { .. } => \"interpreted\",\n            LastJump::None => \"none\",\n        }\n    }\n}\npub static mut debug_last_jump: LastJump = LastJump::None;\n\n#[derive(Copy, Clone)]\npub struct SegmentSelector {\n    raw: u16,\n}\n\nimpl SegmentSelector {\n    pub fn of_u16(raw: u16) -> SegmentSelector { SegmentSelector { raw } }\n    pub fn rpl(&self) -> u8 { (self.raw & 3) as u8 }\n    pub fn is_gdt(&self) -> bool { (self.raw & 4) == 0 }\n    pub fn descriptor_offset(&self) -> u16 { (self.raw & !7) as u16 }\n\n    pub fn is_null(&self) -> bool { self.is_gdt() && self.descriptor_offset() == 0 }\n}\n\n// Used to indicate early that the selector cannot be used to fetch a descriptor\n#[derive(PartialEq)]\npub enum SelectorNullOrInvalid {\n    IsNull,\n    OutsideOfTableLimit,\n}\n\npub struct SegmentDescriptor {\n    pub raw: u64,\n}\n\nimpl SegmentDescriptor {\n    pub fn of_u64(raw: u64) -> SegmentDescriptor { SegmentDescriptor { raw } }\n    pub fn base(&self) -> i32 {\n        ((self.raw >> 16) & 0xffff | (self.raw & 0xff_00000000) >> 16 | (self.raw >> 56 << 24))\n            as i32\n    }\n    pub fn limit(&self) -> u32 { (self.raw & 0xffff | ((self.raw >> 48) & 0xf) << 16) as u32 }\n    pub fn access_byte(&self) -> u8 { ((self.raw >> 40) & 0xff) as u8 }\n    pub fn flags(&self) -> u8 { ((self.raw >> 48 >> 4) & 0xf) as u8 }\n\n    pub fn is_system(&self) -> bool { self.access_byte() & 0x10 == 0 }\n    pub fn system_type(&self) -> u8 { self.access_byte() & 0xF }\n\n    pub fn accessed(&self) -> bool { self.access_byte() & 1 == 1 }\n    pub fn is_rw(&self) -> bool { self.access_byte() & 2 == 2 }\n    pub fn is_dc(&self) -> bool { self.access_byte() & 4 == 4 }\n    pub fn is_executable(&self) -> bool { self.access_byte() & 8 == 8 }\n    pub fn is_present(&self) -> bool { self.access_byte() & 0x80 == 0x80 }\n    pub fn is_writable(&self) -> bool { self.is_rw() && !self.is_executable() }\n    pub fn is_readable(&self) -> bool { self.is_rw() || !self.is_executable() }\n    pub fn is_conforming_executable(&self) -> bool { self.is_dc() && self.is_executable() }\n    pub fn dpl(&self) -> u8 { (self.access_byte() >> 5) & 3 }\n    pub fn is_32(&self) -> bool { self.flags() & 4 == 4 }\n    pub fn effective_limit(&self) -> u32 {\n        if self.flags() & 8 == 8 {\n            self.limit() << 12 | 0xFFF\n        }\n        else {\n            self.limit()\n        }\n    }\n    pub fn set_busy(&self) -> SegmentDescriptor {\n        SegmentDescriptor {\n            raw: self.raw | 2 << 40,\n        }\n    }\n    pub fn set_accessed(&self) -> SegmentDescriptor {\n        SegmentDescriptor {\n            raw: self.raw | 1 << 40,\n        }\n    }\n}\n\npub struct InterruptDescriptor {\n    raw: u64,\n}\n\nimpl InterruptDescriptor {\n    pub fn of_u64(raw: u64) -> InterruptDescriptor { InterruptDescriptor { raw } }\n    pub fn offset(&self) -> i32 { (self.raw & 0xffff | self.raw >> 32 & 0xffff0000) as i32 }\n    pub fn selector(&self) -> u16 { (self.raw >> 16 & 0xffff) as u16 }\n    pub fn access_byte(&self) -> u8 { (self.raw >> 40 & 0xff) as u8 }\n    pub fn dpl(&self) -> u8 { (self.access_byte() >> 5 & 3) as u8 }\n    pub fn gate_type(&self) -> u8 { self.access_byte() & 7 }\n    pub fn is_32(&self) -> bool { self.access_byte() & 8 == 8 }\n    pub fn is_present(&self) -> bool { self.access_byte() & 0x80 == 0x80 }\n    pub fn reserved_zeros_are_valid(&self) -> bool { self.access_byte() & 16 == 0 }\n\n    const TASK_GATE: u8 = 0b101;\n    const INTERRUPT_GATE: u8 = 0b110;\n    const TRAP_GATE: u8 = 0b111;\n}\n\npub unsafe fn switch_cs_real_mode(selector: i32) {\n    dbg_assert!(!*protected_mode || vm86_mode());\n\n    *sreg.offset(CS as isize) = selector as u16;\n    *segment_is_null.offset(CS as isize) = false;\n    *segment_offsets.offset(CS as isize) = selector << 4;\n    update_cs_size(false);\n}\n\nunsafe fn get_tss_ss_esp(dpl: u8) -> OrPageFault<(i32, i32)> {\n    Ok(if *tss_size_32 {\n        let tss_stack_offset = ((dpl << 3) + 4) as u32;\n        if tss_stack_offset + 7 > *segment_limits.offset(TR as isize) {\n            panic!(\"#TS handler\");\n        }\n        let addr = translate_address_system_read(\n            *segment_offsets.offset(TR as isize) + tss_stack_offset as i32,\n        )?;\n        dbg_assert!(addr & 0xFFF <= 0x1000 - 6);\n        (memory::read16(addr + 4), memory::read32s(addr))\n    }\n    else {\n        let tss_stack_offset = ((dpl << 2) + 2) as u32;\n        if tss_stack_offset + 3 > *segment_limits.offset(TR as isize) {\n            panic!(\"#TS handler\");\n        }\n        let addr = translate_address_system_read(\n            *segment_offsets.offset(TR as isize) + tss_stack_offset as i32,\n        )?;\n        dbg_assert!(addr & 0xFFF <= 0x1000 - 4);\n        (memory::read16(addr + 2), memory::read16(addr))\n    })\n}\n\npub unsafe fn iret16() { iret(true); }\npub unsafe fn iret32() { iret(false); }\n\npub unsafe fn iret(is_16: bool) {\n    if vm86_mode() && getiopl() < 3 {\n        // vm86 mode, iopl != 3\n        dbg_log!(\"#gp iret vm86 mode, iopl != 3\");\n        trigger_gp(0);\n        return;\n    }\n\n    let (new_eip, new_cs, mut new_flags) = if is_16 {\n        (\n            return_on_pagefault!(safe_read16(get_stack_pointer(0))),\n            return_on_pagefault!(safe_read16(get_stack_pointer(2))),\n            return_on_pagefault!(safe_read16(get_stack_pointer(4))),\n        )\n    }\n    else {\n        (\n            return_on_pagefault!(safe_read32s(get_stack_pointer(0))),\n            return_on_pagefault!(safe_read16(get_stack_pointer(4))),\n            return_on_pagefault!(safe_read32s(get_stack_pointer(8))),\n        )\n    };\n\n    if !*protected_mode || (vm86_mode() && getiopl() == 3) {\n        if new_eip as u32 & 0xFFFF0000 != 0 {\n            panic!(\"#GP handler\");\n        }\n\n        switch_cs_real_mode(new_cs);\n        *instruction_pointer = get_seg_cs() + new_eip;\n\n        if is_16 {\n            update_eflags(new_flags | *flags & !0xFFFF);\n            adjust_stack_reg(3 * 2);\n        }\n        else {\n            if !*protected_mode {\n                update_eflags((new_flags & 0x257FD5) | (*flags & 0x1A0000));\n            }\n            else {\n                update_eflags(new_flags);\n            }\n            adjust_stack_reg(3 * 4);\n        }\n\n        update_state_flags();\n        handle_irqs();\n        return;\n    }\n\n    dbg_assert!(!vm86_mode());\n\n    if *flags & FLAG_NT != 0 {\n        if DEBUG {\n            panic!(\"NT\");\n        }\n        trigger_gp(0);\n        return;\n    }\n\n    if new_flags & FLAG_VM != 0 {\n        if *cpl == 0 {\n            // return to virtual 8086 mode\n\n            // vm86 cannot be set in 16 bit flag\n            dbg_assert!(!is_16);\n\n            let temp_esp = return_on_pagefault!(safe_read32s(get_stack_pointer(12)));\n            let temp_ss = return_on_pagefault!(safe_read16(get_stack_pointer(16)));\n\n            let new_es = return_on_pagefault!(safe_read16(get_stack_pointer(20)));\n            let new_ds = return_on_pagefault!(safe_read16(get_stack_pointer(24)));\n            let new_fs = return_on_pagefault!(safe_read16(get_stack_pointer(28)));\n            let new_gs = return_on_pagefault!(safe_read16(get_stack_pointer(32)));\n\n            // no exceptions below\n\n            update_eflags(new_flags);\n            *flags |= FLAG_VM;\n\n            switch_cs_real_mode(new_cs);\n            *instruction_pointer = get_seg_cs() + (new_eip & 0xFFFF);\n\n            if !switch_seg(ES, new_es)\n                || !switch_seg(DS, new_ds)\n                || !switch_seg(FS, new_fs)\n                || !switch_seg(GS, new_gs)\n            {\n                // XXX: Should be checked before side effects\n                dbg_assert!(false);\n            }\n\n            adjust_stack_reg(9 * 4); // 9 dwords: eip, cs, flags, esp, ss, es, ds, fs, gs\n\n            write_reg32(ESP, temp_esp);\n            if !switch_seg(SS, temp_ss) {\n                // XXX\n                dbg_assert!(false);\n            }\n\n            *cpl = 3;\n            cpl_changed();\n\n            update_cs_size(false);\n            update_state_flags();\n\n            // iret end\n            return;\n        }\n        else {\n            dbg_log!(\"vm86 flag ignored because cpl != 0\");\n            new_flags &= !FLAG_VM;\n        }\n    }\n\n    // protected mode return\n\n    let cs_selector = SegmentSelector::of_u16(new_cs as u16);\n    let cs_descriptor = match return_on_pagefault!(lookup_segment_selector(cs_selector)) {\n        Ok((desc, _)) => desc,\n        Err(SelectorNullOrInvalid::IsNull) => panic!(\"Unimplemented: CS selector is null\"),\n        Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n            panic!(\"Unimplemented: CS selector is invalid\")\n        },\n    };\n\n    if new_eip as u32 > cs_descriptor.effective_limit() {\n        dbg_log!(\n            \"#gp iret: new_eip > cs_descriptor.effective_limit, new_eip={:x} cs_descriptor.effective_limit={:x}\",\n            new_eip as u32,\n            cs_descriptor.effective_limit()\n        );\n        trigger_gp(new_cs & !3);\n        return;\n    }\n\n    if !cs_descriptor.is_present() {\n        panic!(\"not present\");\n    }\n    if !cs_descriptor.is_executable() {\n        panic!(\"not exec\");\n    }\n    if cs_selector.rpl() < *cpl {\n        panic!(\"rpl < cpl\");\n    }\n    if cs_descriptor.is_dc() && cs_descriptor.dpl() > cs_selector.rpl() {\n        panic!(\"conforming and dpl > rpl\");\n    }\n\n    if !cs_descriptor.is_dc() && cs_selector.rpl() != cs_descriptor.dpl() {\n        dbg_log!(\n            \"#gp iret: non-conforming cs and rpl != dpl, dpl={} rpl={}\",\n            cs_descriptor.dpl(),\n            cs_selector.rpl()\n        );\n        trigger_gp(new_cs & !3);\n        return;\n    }\n\n    if cs_selector.rpl() > *cpl {\n        // outer privilege return\n        let (temp_esp, temp_ss) = if is_16 {\n            (\n                return_on_pagefault!(safe_read16(get_stack_pointer(6))),\n                return_on_pagefault!(safe_read16(get_stack_pointer(8))),\n            )\n        }\n        else {\n            (\n                return_on_pagefault!(safe_read32s(get_stack_pointer(12))),\n                return_on_pagefault!(safe_read16(get_stack_pointer(16))),\n            )\n        };\n\n        let ss_selector = SegmentSelector::of_u16(temp_ss as u16);\n        let ss_descriptor = match return_on_pagefault!(lookup_segment_selector(ss_selector)) {\n            Ok((desc, _)) => desc,\n            Err(SelectorNullOrInvalid::IsNull) => {\n                dbg_log!(\"#GP for loading 0 in SS sel={:x}\", temp_ss);\n                dbg_trace();\n                trigger_gp(0);\n                return;\n            },\n            Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                dbg_log!(\"#GP for loading invalid in SS sel={:x}\", temp_ss);\n                trigger_gp(temp_ss & !3);\n                return;\n            },\n        };\n        let new_cpl = cs_selector.rpl();\n\n        if ss_descriptor.is_system()\n            || ss_selector.rpl() != new_cpl\n            || !ss_descriptor.is_writable()\n            || ss_descriptor.dpl() != new_cpl\n        {\n            dbg_log!(\"#GP for loading invalid in SS sel={:x}\", temp_ss);\n            dbg_trace();\n            trigger_gp(temp_ss & !3);\n            return;\n        }\n\n        if !ss_descriptor.is_present() {\n            dbg_log!(\"#SS for loading non-present in SS sel={:x}\", temp_ss);\n            dbg_trace();\n            trigger_ss(temp_ss & !3);\n            return;\n        }\n\n        // no exceptions below\n\n        if is_16 {\n            update_eflags(new_flags | *flags & !0xFFFF);\n        }\n        else {\n            update_eflags(new_flags);\n        }\n\n        *cpl = cs_selector.rpl();\n        cpl_changed();\n\n        if !switch_seg(SS, temp_ss) {\n            // XXX\n            dbg_assert!(false);\n        }\n\n        set_stack_reg(temp_esp);\n\n        if *cpl == 0 && !is_16 {\n            *flags = *flags & !FLAG_VIF & !FLAG_VIP | (new_flags & (FLAG_VIF | FLAG_VIP));\n        }\n\n        for reg in [ES, DS, FS, GS] {\n            let access = *segment_access_bytes.offset(reg as isize);\n            let dpl = access >> 5 & 3;\n            let executable = access & 8 == 8;\n            let conforming = access & 4 == 4;\n            if dpl < *cpl && !(executable && conforming) {\n                //dbg_log!(\n                //    \"set segment to null sreg={} dpl={} executable={} conforming={}\",\n                //    reg,\n                //    dpl,\n                //    executable,\n                //    conforming\n                //);\n                *segment_is_null.offset(reg as isize) = true;\n                *sreg.offset(reg as isize) = 0;\n            }\n        }\n    }\n    else if cs_selector.rpl() == *cpl {\n        // same privilege return\n        // no exceptions below\n        if is_16 {\n            adjust_stack_reg(3 * 2);\n            update_eflags(new_flags | *flags & !0xFFFF);\n        }\n        else {\n            adjust_stack_reg(3 * 4);\n            update_eflags(new_flags);\n        }\n\n        // update vip and vif, which are not changed by update_eflags\n        if *cpl == 0 && !is_16 {\n            *flags = *flags & !FLAG_VIF & !FLAG_VIP | (new_flags & (FLAG_VIF | FLAG_VIP));\n        }\n    }\n    else {\n        dbg_assert!(false);\n    }\n\n    *sreg.offset(CS as isize) = new_cs as u16;\n    dbg_assert!((new_cs & 3) == *cpl as i32);\n\n    update_cs_size(cs_descriptor.is_32());\n\n    *segment_limits.offset(CS as isize) = cs_descriptor.effective_limit();\n    *segment_offsets.offset(CS as isize) = cs_descriptor.base();\n    *segment_access_bytes.offset(CS as isize) = cs_descriptor.access_byte();\n\n    *instruction_pointer = new_eip + get_seg_cs();\n\n    update_state_flags();\n\n    // iret end\n\n    handle_irqs();\n}\n\npub unsafe fn call_interrupt_vector(\n    interrupt_nr: i32,\n    is_software_int: bool,\n    error_code: Option<i32>,\n) {\n    if *protected_mode {\n        if vm86_mode() && *cr.offset(4) & CR4_VME != 0 {\n            panic!(\"Unimplemented: VME\");\n        }\n\n        if vm86_mode() && is_software_int && getiopl() < 3 {\n            dbg_log!(\"call_interrupt_vector #GP. vm86 && software int && iopl < 3\");\n            dbg_trace();\n            trigger_gp(0);\n            return;\n        }\n\n        if interrupt_nr << 3 | 7 > *idtr_size {\n            dbg_log!(\"interrupt_nr={:x} idtr_size={:x}\", interrupt_nr, *idtr_size);\n            dbg_trace();\n            panic!(\"Unimplemented: #GP handler\");\n        }\n\n        let descriptor_address = return_on_pagefault!(translate_address_system_read(\n            *idtr_offset + (interrupt_nr << 3)\n        ));\n\n        let descriptor = InterruptDescriptor::of_u64(memory::read64s(descriptor_address) as u64);\n\n        let mut offset = descriptor.offset();\n        let selector = descriptor.selector() as i32;\n        let dpl = descriptor.dpl();\n        let gate_type = descriptor.gate_type();\n\n        if is_software_int && dpl < *cpl {\n            dbg_log!(\"#gp software interrupt ({:x}) and dpl < cpl\", interrupt_nr);\n            dbg_trace();\n            trigger_gp(interrupt_nr << 3 | 2);\n            return;\n        }\n\n        if gate_type != InterruptDescriptor::TRAP_GATE\n            && gate_type != InterruptDescriptor::INTERRUPT_GATE\n            && gate_type != InterruptDescriptor::TASK_GATE\n        {\n            // invalid gate_type\n            dbg_log!(\n                \"gate type invalid. gate_type=0b{:b} raw={:b}\",\n                gate_type,\n                descriptor.raw\n            );\n            dbg_trace();\n            panic!(\"Unimplemented: #GP handler\");\n        }\n\n        if !descriptor.reserved_zeros_are_valid() {\n            dbg_log!(\n                \"reserved 0s violated. gate_type=0b{:b} raw={:b}\",\n                gate_type,\n                descriptor.raw\n            );\n            dbg_trace();\n            panic!(\"Unimplemented: #GP handler\");\n        }\n\n        if !descriptor.is_present() {\n            // present bit not set\n            dbg_log!(\"#np int descriptor not present, int={}\", interrupt_nr);\n            trigger_np(interrupt_nr << 3 | 2);\n            return;\n        }\n\n        if gate_type == InterruptDescriptor::TASK_GATE {\n            // task gate\n            dbg_log!(\n                \"interrupt to task gate: int={:x} sel={:x} dpl={}\",\n                interrupt_nr,\n                selector,\n                dpl\n            );\n            dbg_trace();\n            dbg_assert!(descriptor.is_32(), \"TODO: Check this (likely #GP)\");\n            dbg_assert!(offset == 0, \"TODO: Check this (likely #GP)\");\n            do_task_switch(selector, error_code);\n            return;\n        }\n\n        let cs_segment_descriptor = match return_on_pagefault!(lookup_segment_selector(\n            SegmentSelector::of_u16(selector as u16)\n        )) {\n            Ok((desc, _)) => desc,\n            Err(SelectorNullOrInvalid::IsNull) => {\n                dbg_log!(\"is null\");\n                panic!(\"Unimplemented: #GP handler\");\n            },\n            Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                dbg_log!(\"is invalid\");\n                panic!(\"Unimplemented: #GP handler (error code)\");\n            },\n        };\n\n        dbg_assert!(offset as u32 <= cs_segment_descriptor.effective_limit());\n\n        if !cs_segment_descriptor.is_executable() || cs_segment_descriptor.dpl() > *cpl {\n            dbg_log!(\"not exec\");\n            panic!(\"Unimplemented: #GP handler\");\n        }\n        if !cs_segment_descriptor.is_present() {\n            // kvm-unit-test\n            dbg_log!(\"not present\");\n            trigger_np(interrupt_nr << 3 | 2);\n            return;\n        }\n\n        let old_flags = get_eflags();\n\n        if !cs_segment_descriptor.is_dc() && cs_segment_descriptor.dpl() < *cpl {\n            // inter privilege level interrupt\n            // interrupt from vm86 mode\n\n            if old_flags & FLAG_VM != 0 && cs_segment_descriptor.dpl() != 0 {\n                panic!(\"Unimplemented: #GP handler for non-0 cs segment dpl when in vm86 mode\");\n            }\n\n            let (new_ss, new_esp) =\n                return_on_pagefault!(get_tss_ss_esp(cs_segment_descriptor.dpl()));\n\n            let ss_segment_selector = SegmentSelector::of_u16(new_ss as u16);\n            let ss_segment_descriptor =\n                match return_on_pagefault!(lookup_segment_selector(ss_segment_selector)) {\n                    Ok((desc, _)) => desc,\n                    Err(\n                        SelectorNullOrInvalid::IsNull | SelectorNullOrInvalid::OutsideOfTableLimit,\n                    ) => {\n                        panic!(\"Unimplemented: #TS handler\");\n                    },\n                };\n\n            if ss_segment_descriptor.is_dc() {\n                dbg_assert!(new_esp as u32 > ss_segment_descriptor.effective_limit());\n            }\n            else {\n                dbg_assert!(new_esp as u32 - 1 <= ss_segment_descriptor.effective_limit());\n            }\n            dbg_assert!(!ss_segment_descriptor.is_system() && ss_segment_descriptor.is_writable());\n\n            if ss_segment_selector.rpl() != cs_segment_descriptor.dpl() {\n                panic!(\"Unimplemented: #TS handler\");\n            }\n            if ss_segment_descriptor.dpl() != cs_segment_descriptor.dpl()\n                || !ss_segment_descriptor.is_rw()\n            {\n                panic!(\"Unimplemented: #TS handler\");\n            }\n            if !ss_segment_descriptor.is_present() {\n                panic!(\"Unimplemented: #TS handler\");\n            }\n\n            let old_esp = read_reg32(ESP);\n            let old_ss = *sreg.offset(SS as isize) as i32;\n\n            let error_code_space = if error_code.is_some() { 1 } else { 0 };\n            let vm86_space = if (old_flags & FLAG_VM) == FLAG_VM { 4 } else { 0 };\n            let bytes_per_arg = if descriptor.is_32() { 4 } else { 2 };\n\n            let stack_space = bytes_per_arg * (5 + error_code_space + vm86_space);\n            let new_stack_pointer = ss_segment_descriptor.base()\n                + if ss_segment_descriptor.is_32() {\n                    new_esp - stack_space\n                }\n                else {\n                    new_esp - stack_space & 0xFFFF\n                };\n\n            return_on_pagefault!(translate_address_system_write(new_stack_pointer));\n            return_on_pagefault!(translate_address_system_write(\n                ss_segment_descriptor.base() + new_esp - 1\n            ));\n\n            // no exceptions below\n            *cpl = cs_segment_descriptor.dpl();\n            cpl_changed();\n\n            update_cs_size(cs_segment_descriptor.is_32());\n\n            *flags &= !FLAG_VM & !FLAG_RF;\n\n            if !switch_seg(SS, new_ss) {\n                // XXX\n                dbg_assert!(false);\n            }\n            set_stack_reg(new_esp);\n\n            // XXX: #SS if stack would cross stack limit\n\n            if old_flags & FLAG_VM != 0 {\n                if !descriptor.is_32() {\n                    dbg_assert!(false);\n                }\n                else {\n                    push32(*sreg.offset(GS as isize) as i32).unwrap();\n                    push32(*sreg.offset(FS as isize) as i32).unwrap();\n                    push32(*sreg.offset(DS as isize) as i32).unwrap();\n                    push32(*sreg.offset(ES as isize) as i32).unwrap();\n                }\n            }\n\n            if descriptor.is_32() {\n                push32(old_ss).unwrap();\n                push32(old_esp).unwrap();\n            }\n            else {\n                push16(old_ss).unwrap();\n                push16(old_esp & 0xFFFF).unwrap();\n            }\n        }\n        else if cs_segment_descriptor.is_dc() || cs_segment_descriptor.dpl() == *cpl {\n            // intra privilege level interrupt\n\n            //dbg_log!(\"Intra privilege interrupt gate=\" + h(selector, 4) + \":\" + h(offset >>> 0, 8) +\n            //        \" gate_type=\" + gate_type + \" 16bit=\" + descriptor.is_32() +\n            //        \" cpl=\" + *cpl + \" dpl=\" + segment_descriptor.dpl() + \" conforming=\" + +segment_descriptor.is_dc(), );\n            //debug.dump_regs_short();\n\n            if *flags & FLAG_VM != 0 {\n                dbg_assert!(false, \"check error code\");\n                trigger_gp(selector & !3);\n                return;\n            }\n\n            let bytes_per_arg = if descriptor.is_32() { 4 } else { 2 };\n            let error_code_space = if error_code.is_some() { 1 } else { 0 };\n\n            let stack_space = bytes_per_arg * (3 + error_code_space);\n\n            // XXX: with current cpl or with cpl 0?\n            return_on_pagefault!(writable_or_pagefault(\n                get_stack_pointer(-stack_space),\n                stack_space\n            ));\n\n        // no exceptions below\n        }\n        else {\n            panic!(\"Unimplemented: #GP handler\");\n        }\n\n        // XXX: #SS if stack would cross stack limit\n        if descriptor.is_32() {\n            push32(old_flags).unwrap();\n            push32(*sreg.offset(CS as isize) as i32).unwrap();\n            push32(get_real_eip()).unwrap();\n\n            if let Some(ec) = error_code {\n                push32(ec).unwrap();\n            }\n        }\n        else {\n            push16(old_flags & 0xFFFF).unwrap();\n            push16(*sreg.offset(CS as isize) as i32).unwrap();\n            push16(get_real_eip() & 0xFFFF).unwrap();\n\n            if let Some(ec) = error_code {\n                dbg_assert!(ec >= 0 && ec < 0x10000);\n                push16(ec).unwrap();\n            }\n\n            offset &= 0xFFFF;\n        }\n\n        if old_flags & FLAG_VM != 0 {\n            if !switch_seg(GS, 0) || !switch_seg(FS, 0) || !switch_seg(DS, 0) || !switch_seg(ES, 0)\n            {\n                // can't fail\n                dbg_assert!(false);\n            }\n        }\n\n        *sreg.offset(CS as isize) = (selector as u16) & !3 | *cpl as u16;\n        dbg_assert!((*sreg.offset(CS as isize) & 3) == *cpl as u16);\n\n        update_cs_size(cs_segment_descriptor.is_32());\n\n        *segment_limits.offset(CS as isize) = cs_segment_descriptor.effective_limit();\n        *segment_offsets.offset(CS as isize) = cs_segment_descriptor.base();\n        *segment_access_bytes.offset(CS as isize) = cs_segment_descriptor.access_byte();\n\n        *instruction_pointer = get_seg_cs() + offset;\n\n        *flags &= !FLAG_NT & !FLAG_VM & !FLAG_RF & !FLAG_TRAP;\n\n        if gate_type == InterruptDescriptor::INTERRUPT_GATE {\n            // clear int flag for interrupt gates\n            *flags &= !FLAG_INTERRUPT;\n        }\n        else {\n            if *flags & FLAG_INTERRUPT != 0 && old_flags & FLAG_INTERRUPT == 0 {\n                handle_irqs();\n            }\n        }\n\n        update_state_flags();\n    }\n    else {\n        // call 4 byte cs:ip interrupt vector from ivt at cpu.memory 0\n\n        let index = (interrupt_nr << 2) as u32;\n        let new_ip = memory::read16(index);\n        let new_cs = memory::read16(index + 2);\n\n        dbg_assert!(\n            index | 3 <= IVT_SIZE,\n            \"Unimplemented: #GP for interrupt number out of IVT bounds\"\n        );\n\n        // XXX: #SS if stack would cross stack limit\n\n        // push flags, cs:ip\n        push16(get_eflags() & 0xFFFF).unwrap();\n        push16(*sreg.offset(CS as isize) as i32).unwrap();\n        push16(get_real_eip() & 0xFFFF).unwrap();\n\n        *flags &= !FLAG_INTERRUPT & !FLAG_AC & !FLAG_TRAP;\n\n        switch_cs_real_mode(new_cs);\n        *instruction_pointer = get_seg_cs() + new_ip;\n        update_state_flags();\n    }\n}\n\npub unsafe fn far_jump(eip: i32, selector: i32, is_call: bool, is_osize_32: bool) {\n    dbg_assert!(selector < 0x10000 && selector >= 0);\n\n    if !*protected_mode || vm86_mode() {\n        if is_call {\n            if is_osize_32 {\n                return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-8), 8));\n\n                push32(*sreg.offset(CS as isize) as i32).unwrap();\n                push32(get_real_eip()).unwrap();\n            }\n            else {\n                return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-4), 4));\n\n                push16(*sreg.offset(CS as isize) as i32).unwrap();\n                push16(get_real_eip()).unwrap();\n            }\n        }\n        switch_cs_real_mode(selector);\n        *instruction_pointer = get_seg_cs() + eip;\n        update_state_flags();\n        return;\n    }\n\n    let cs_selector = SegmentSelector::of_u16(selector as u16);\n    let info = match return_on_pagefault!(lookup_segment_selector(cs_selector)) {\n        Ok((desc, _)) => desc,\n        Err(SelectorNullOrInvalid::IsNull) => {\n            dbg_log!(\"#gp null cs\");\n            trigger_gp(0);\n            return;\n        },\n        Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n            dbg_log!(\"#gp invalid cs: {:x}\", selector);\n            trigger_gp(selector & !3);\n            return;\n        },\n    };\n\n    if info.is_system() {\n        dbg_assert!(is_call, \"TODO: Jump\");\n\n        dbg_log!(\"system type cs: {:x}\", selector);\n\n        if info.system_type() == 0xC || info.system_type() == 4 {\n            // call gate\n            let is_16 = info.system_type() == 4;\n\n            if info.dpl() < *cpl || info.dpl() < cs_selector.rpl() {\n                dbg_log!(\"#gp cs gate dpl < cpl or dpl < rpl: {:x}\", selector);\n                trigger_gp(selector & !3);\n                return;\n            }\n\n            if !info.is_present() {\n                dbg_log!(\"#NP for loading not-present in gate cs sel={:x}\", selector);\n                trigger_np(selector & !3);\n                return;\n            }\n\n            let cs_selector = (info.raw >> 16) as i32;\n\n            let cs_info = match return_on_pagefault!(lookup_segment_selector(\n                SegmentSelector::of_u16(cs_selector as u16)\n            )) {\n                Ok((desc, _)) => desc,\n                Err(SelectorNullOrInvalid::IsNull) => {\n                    dbg_log!(\"#gp null cs\");\n                    trigger_gp(0);\n                    return;\n                },\n                Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                    dbg_log!(\"#gp invalid cs: {:x}\", cs_selector);\n                    trigger_gp(cs_selector & !3);\n                    return;\n                },\n            };\n\n            if cs_info.is_system() {\n                dbg_log!(\"#gp non-code cs: {:x}\", cs_selector);\n                trigger_gp(cs_selector & !3);\n                return;\n            }\n\n            if !cs_info.is_executable() {\n                dbg_log!(\"#gp non-executable cs: {:x}\", cs_selector);\n                trigger_gp(cs_selector & !3);\n                return;\n            }\n\n            if cs_info.dpl() > *cpl {\n                dbg_log!(\"#gp dpl > cpl: {:x}\", cs_selector);\n                trigger_gp(cs_selector & !3);\n                return;\n            }\n\n            if !cs_info.is_present() {\n                dbg_log!(\"#NP for loading not-present in cs sel={:x}\", cs_selector);\n                trigger_np(cs_selector & !3);\n                return;\n            }\n\n            if !cs_info.is_dc() && cs_info.dpl() < *cpl {\n                dbg_log!(\n                    \"more privilege call gate is_16={} from={} to={}\",\n                    is_16,\n                    *cpl,\n                    cs_info.dpl()\n                );\n                let (new_ss, new_esp) = return_on_pagefault!(get_tss_ss_esp(cs_info.dpl()));\n\n                let ss_selector = SegmentSelector::of_u16(new_ss as u16);\n                let ss_info = match return_on_pagefault!(lookup_segment_selector(ss_selector)) {\n                    Ok((desc, _)) => desc,\n                    Err(SelectorNullOrInvalid::IsNull) => {\n                        panic!(\"null ss: {}\", new_ss);\n                    },\n                    Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                        panic!(\"invalid ss: {}\", new_ss);\n                    },\n                };\n\n                if ss_info.is_dc() {\n                    dbg_assert!(new_esp as u32 > ss_info.effective_limit());\n                }\n                else {\n                    dbg_assert!(new_esp as u32 - 1 <= ss_info.effective_limit());\n                }\n                dbg_assert!(!ss_info.is_system() && ss_info.is_writable());\n\n                if ss_selector.rpl() != cs_info.dpl()\n                // xxx: 0 in v86 mode\n                {\n                    panic!(\"#TS handler\");\n                }\n                if ss_info.dpl() != cs_info.dpl() || !ss_info.is_writable() {\n                    panic!(\"#TS handler\");\n                }\n                if !ss_info.is_present() {\n                    panic!(\"#SS handler\");\n                }\n\n                let parameter_count = (info.raw >> 32 & 0x1F) as i32;\n                let mut stack_space = if is_16 { 4 } else { 8 };\n                if is_call {\n                    stack_space +=\n                        if is_16 { 4 + 2 * parameter_count } else { 8 + 4 * parameter_count };\n                }\n                if ss_info.is_32() {\n                    return_on_pagefault!(writable_or_pagefault_cpl(\n                        cs_info.dpl(),\n                        ss_info.base() + new_esp - stack_space,\n                        stack_space\n                    ));\n                }\n                else {\n                    return_on_pagefault!(writable_or_pagefault_cpl(\n                        cs_info.dpl(),\n                        ss_info.base() + (new_esp - stack_space & 0xFFFF),\n                        stack_space\n                    ));\n                }\n\n                let old_esp = read_reg32(ESP);\n                let old_ss = *sreg.offset(SS as isize);\n                let old_stack_pointer = get_stack_pointer(0);\n\n                //dbg_log!(\"old_esp=\" + h(old_esp));\n\n                *cpl = cs_info.dpl();\n                cpl_changed();\n\n                update_cs_size(cs_info.is_32());\n\n                dbg_assert!(new_ss & 3 == cs_info.dpl() as i32);\n                // XXX: Should be checked before side effects\n                if !switch_seg(SS, new_ss) {\n                    dbg_assert!(false);\n                };\n                set_stack_reg(new_esp);\n\n                //dbg_log!(\"parameter_count=\" + parameter_count);\n                //dbg_assert!(parameter_count == 0, \"TODO\");\n\n                if is_16 {\n                    push16(old_ss as i32).unwrap();\n                    push16(old_esp).unwrap();\n                }\n                else {\n                    push32(old_ss as i32).unwrap();\n                    push32(old_esp).unwrap();\n                }\n\n                if is_call {\n                    if is_16 {\n                        for i in (0..parameter_count).rev() {\n                            let parameter = safe_read16(old_stack_pointer + 2 * i).unwrap();\n                            push16(parameter).unwrap();\n                        }\n\n                        //writable_or_pagefault(get_stack_pointer(-4), 4);\n                        push16(*sreg.offset(CS as isize) as i32).unwrap();\n                        push16(get_real_eip()).unwrap();\n                    }\n                    else {\n                        for i in (0..parameter_count).rev() {\n                            let parameter = safe_read32s(old_stack_pointer + 4 * i).unwrap();\n                            push32(parameter).unwrap();\n                        }\n\n                        //writable_or_pagefault(get_stack_pointer(-8), 8);\n                        push32(*sreg.offset(CS as isize) as i32).unwrap();\n                        push32(get_real_eip()).unwrap();\n                    }\n                }\n            }\n            else {\n                dbg_log!(\n                    \"same privilege call gate is_16={} from={} to={} conforming={}\",\n                    is_16,\n                    *cpl,\n                    cs_info.dpl(),\n                    cs_info.is_dc()\n                );\n\n                if is_call {\n                    if is_16 {\n                        return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-4), 4));\n\n                        push16(*sreg.offset(CS as isize) as i32).unwrap();\n                        push16(get_real_eip()).unwrap();\n                    }\n                    else {\n                        return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-8), 8));\n\n                        push32(*sreg.offset(CS as isize) as i32).unwrap();\n                        push32(get_real_eip()).unwrap();\n                    }\n                }\n\n                dbg_assert!(*cpl == cs_info.dpl());\n            }\n\n            // Note: eip from call is ignored\n            let mut new_eip = (info.raw & 0xFFFF) as i32;\n            if !is_16 {\n                new_eip |= ((info.raw >> 32) & 0xFFFF0000) as i32;\n            }\n\n            dbg_log!(\n                \"call gate eip={:x} cs={:x} conforming={}\",\n                new_eip as u32,\n                cs_selector,\n                cs_info.is_dc()\n            );\n            dbg_assert!((new_eip as u32) <= cs_info.effective_limit(), \"todo: #gp\");\n\n            update_cs_size(cs_info.is_32());\n\n            *segment_is_null.offset(CS as isize) = false;\n            *segment_limits.offset(CS as isize) = cs_info.effective_limit();\n            *segment_offsets.offset(CS as isize) = cs_info.base();\n            *segment_access_bytes.offset(CS as isize) = cs_info.access_byte();\n            *sreg.offset(CS as isize) = cs_selector as u16 & !3 | *cpl as u16;\n            dbg_assert!(*sreg.offset(CS as isize) & 3 == *cpl as u16);\n\n            *instruction_pointer = get_seg_cs() + new_eip;\n\n            update_state_flags();\n        }\n        else if info.system_type() == 1 || info.system_type() == 9 {\n            dbg_assert!(false, \"TODO: far call task gate\");\n        }\n        else {\n            dbg_assert!(false, \"TODO: #gp invalid system type\");\n        }\n    }\n    else {\n        if !info.is_executable() {\n            dbg_log!(\"#gp non-executable cs: {:x}\", selector);\n            trigger_gp(selector & !3);\n            return;\n        }\n\n        if info.is_dc() {\n            // conforming code segment\n            if info.dpl() > *cpl {\n                dbg_log!(\"#gp cs dpl > cpl: {:x}\", selector);\n                trigger_gp(selector & !3);\n                return;\n            }\n        }\n        else {\n            // non-conforming code segment\n\n            if cs_selector.rpl() > *cpl || info.dpl() != *cpl {\n                dbg_log!(\"#gp cs rpl > cpl or dpl != cpl: {:x}\", selector);\n                trigger_gp(selector & !3);\n                return;\n            }\n        }\n\n        if !info.is_present() {\n            dbg_log!(\"#NP for loading not-present in cs sel={:x}\", selector);\n            dbg_trace();\n            trigger_np(selector & !3);\n            return;\n        }\n\n        if is_call {\n            if is_osize_32 {\n                return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-8), 8));\n\n                push32(*sreg.offset(CS as isize) as i32).unwrap();\n                push32(get_real_eip()).unwrap();\n            }\n            else {\n                return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-4), 4));\n\n                push16(*sreg.offset(CS as isize) as i32).unwrap();\n                push16(get_real_eip()).unwrap();\n            }\n        }\n\n        dbg_assert!((eip as u32) <= info.effective_limit(), \"todo: #gp\");\n\n        update_cs_size(info.is_32());\n\n        *segment_is_null.offset(CS as isize) = false;\n        *segment_limits.offset(CS as isize) = info.effective_limit();\n        *segment_access_bytes.offset(CS as isize) = info.access_byte();\n\n        *segment_offsets.offset(CS as isize) = info.base();\n        *sreg.offset(CS as isize) = selector as u16 & !3 | *cpl as u16;\n\n        *instruction_pointer = get_seg_cs() + eip;\n\n        update_state_flags();\n    }\n}\n\npub unsafe fn far_return(eip: i32, selector: i32, stack_adjust: i32, is_osize_32: bool) {\n    dbg_assert!(selector < 0x10000 && selector >= 0);\n\n    if !*protected_mode {\n        dbg_assert!(!*is_32);\n    }\n\n    if !*protected_mode || vm86_mode() {\n        switch_cs_real_mode(selector);\n        *instruction_pointer = get_seg_cs() + eip;\n        adjust_stack_reg(2 * (if is_osize_32 { 4 } else { 2 }) + stack_adjust);\n        update_state_flags();\n        return;\n    }\n\n    let cs_selector = SegmentSelector::of_u16(selector as u16);\n    let info = match return_on_pagefault!(lookup_segment_selector(cs_selector)) {\n        Ok((desc, _)) => desc,\n        Err(SelectorNullOrInvalid::IsNull) => {\n            dbg_log!(\"far return: #gp null cs\");\n            trigger_gp(0);\n            return;\n        },\n        Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n            dbg_log!(\"far return: #gp invalid cs: {:x}\", selector);\n            trigger_gp(selector & !3);\n            return;\n        },\n    };\n\n    if info.is_system() {\n        dbg_assert!(false, \"is system in far return\");\n        trigger_gp(selector & !3);\n        return;\n    }\n\n    if !info.is_executable() {\n        dbg_log!(\"non-executable cs: {:x}\", selector);\n        trigger_gp(selector & !3);\n        return;\n    }\n\n    if cs_selector.rpl() < *cpl {\n        dbg_log!(\"cs rpl < cpl: {:x}\", selector);\n        trigger_gp(selector & !3);\n        return;\n    }\n\n    if info.is_dc() && info.dpl() > cs_selector.rpl() {\n        dbg_log!(\"cs conforming and dpl > rpl: {:x}\", selector);\n        trigger_gp(selector & !3);\n        return;\n    }\n\n    if !info.is_dc() && info.dpl() != cs_selector.rpl() {\n        dbg_log!(\"cs non-conforming and dpl != rpl: {:x}\", selector);\n        trigger_gp(selector & !3);\n        return;\n    }\n\n    if !info.is_present() {\n        dbg_log!(\"#NP for loading not-present in cs sel={:x}\", selector);\n        dbg_trace();\n        trigger_np(selector & !3);\n        return;\n    }\n\n    if cs_selector.rpl() > *cpl {\n        dbg_log!(\n            \"far return privilege change cs: {:x} from={} to={} is_16={}\",\n            selector,\n            *cpl,\n            cs_selector.rpl(),\n            is_osize_32\n        );\n\n        let temp_esp;\n        let temp_ss;\n        if is_osize_32 {\n            //dbg_log!(\"esp read from \" + h(translate_address_system_read(get_stack_pointer(stack_adjust + 8))))\n            temp_esp = safe_read32s(get_stack_pointer(stack_adjust + 8)).unwrap();\n            //dbg_log!(\"esp=\" + h(temp_esp));\n            temp_ss = safe_read16(get_stack_pointer(stack_adjust + 12)).unwrap();\n        }\n        else {\n            //dbg_log!(\"esp read from \" + h(translate_address_system_read(get_stack_pointer(stack_adjust + 4))));\n            temp_esp = safe_read16(get_stack_pointer(stack_adjust + 4)).unwrap();\n            //dbg_log!(\"esp=\" + h(temp_esp));\n            temp_ss = safe_read16(get_stack_pointer(stack_adjust + 6)).unwrap();\n        }\n\n        *cpl = cs_selector.rpl();\n        cpl_changed();\n\n        // XXX: This failure should be checked before side effects\n        if !switch_seg(SS, temp_ss) {\n            dbg_assert!(false);\n        }\n        set_stack_reg(temp_esp + stack_adjust);\n\n        //if(is_osize_32)\n        //{\n        //    adjust_stack_reg(2 * 4);\n        //}\n        //else\n        //{\n        //    adjust_stack_reg(2 * 2);\n        //}\n\n        //throw debug.unimpl(\"privilege change\");\n\n        //adjust_stack_reg(stack_adjust);\n\n        // TODO: invalidate segments that are not accessible at this cpl (see iret)\n    }\n    else {\n        if is_osize_32 {\n            adjust_stack_reg(2 * 4 + stack_adjust);\n        }\n        else {\n            adjust_stack_reg(2 * 2 + stack_adjust);\n        }\n    }\n\n    //dbg_assert(*cpl == info.dpl);\n\n    update_cs_size(info.is_32());\n\n    *segment_is_null.offset(CS as isize) = false;\n    *segment_limits.offset(CS as isize) = info.effective_limit();\n    *segment_access_bytes.offset(CS as isize) = info.access_byte();\n\n    *segment_offsets.offset(CS as isize) = info.base();\n    *sreg.offset(CS as isize) = selector as u16;\n    dbg_assert!(selector & 3 == *cpl as i32);\n\n    *instruction_pointer = get_seg_cs() + eip;\n\n    update_state_flags();\n}\n\npub unsafe fn do_task_switch(selector: i32, error_code: Option<i32>) {\n    dbg_log!(\"do_task_switch sel={:x}\", selector);\n\n    dbg_assert!(*tss_size_32, \"TODO: 16-bit TSS in task switch\");\n\n    let selector = SegmentSelector::of_u16(selector as u16);\n    let (descriptor, descriptor_address) =\n        match lookup_segment_selector(selector).expect(\"TODO: handle pagefault\") {\n            Ok(desc) => desc,\n            Err(_) => {\n                panic!(\"#GP handler\");\n            },\n        };\n\n    dbg_assert!(selector.is_gdt());\n    dbg_assert!((descriptor.system_type() & !2) == 1 || (descriptor.system_type() & !2) == 9);\n    let tss_is_16 = descriptor.system_type() <= 3;\n    let tss_is_busy = (descriptor.system_type() & 2) == 2;\n\n    if (descriptor.system_type() & 2) == 2 {\n        // is busy\n        panic!(\"#GP handler\");\n    }\n\n    if !descriptor.is_present() {\n        panic!(\"#NP handler\");\n    }\n\n    if descriptor.effective_limit() < 103 {\n        panic!(\"#NP handler\");\n    }\n\n    let _tsr_size = *segment_limits.offset(TR as isize);\n    let tsr_offset = *segment_offsets.offset(TR as isize);\n\n    let mut old_eflags = get_eflags();\n\n    if tss_is_busy {\n        old_eflags &= !FLAG_NT;\n    }\n\n    writable_or_pagefault(tsr_offset, 0x66).unwrap();\n\n    //safe_write32(tsr_offset + TSR_CR3, *cr.offset(3));\n\n    // TODO: Write 16 bit values if old tss is 16 bit\n    safe_write32(tsr_offset + TSR_EIP, get_real_eip()).unwrap();\n    safe_write32(tsr_offset + TSR_EFLAGS, old_eflags).unwrap();\n\n    safe_write32(tsr_offset + TSR_EAX, read_reg32(EAX)).unwrap();\n    safe_write32(tsr_offset + TSR_ECX, read_reg32(ECX)).unwrap();\n    safe_write32(tsr_offset + TSR_EDX, read_reg32(EDX)).unwrap();\n    safe_write32(tsr_offset + TSR_EBX, read_reg32(EBX)).unwrap();\n\n    safe_write32(tsr_offset + TSR_ESP, read_reg32(ESP)).unwrap();\n    safe_write32(tsr_offset + TSR_EBP, read_reg32(EBP)).unwrap();\n    safe_write32(tsr_offset + TSR_ESI, read_reg32(ESI)).unwrap();\n    safe_write32(tsr_offset + TSR_EDI, read_reg32(EDI)).unwrap();\n\n    safe_write32(tsr_offset + TSR_ES, *sreg.offset(ES as isize) as i32).unwrap();\n    safe_write32(tsr_offset + TSR_CS, *sreg.offset(CS as isize) as i32).unwrap();\n    safe_write32(tsr_offset + TSR_SS, *sreg.offset(SS as isize) as i32).unwrap();\n    safe_write32(tsr_offset + TSR_DS, *sreg.offset(DS as isize) as i32).unwrap();\n    safe_write32(tsr_offset + TSR_FS, *sreg.offset(FS as isize) as i32).unwrap();\n    safe_write32(tsr_offset + TSR_GS, *sreg.offset(GS as isize) as i32).unwrap();\n\n    //safe_write32(tsr_offset + TSR_LDT, *sreg.offset(reg_ldtr));\n\n    if true\n    /* is jump or call or int */\n    {\n        safe_write64(descriptor_address, descriptor.set_busy().raw).unwrap();\n    }\n\n    //let new_tsr_size = descriptor.effective_limit;\n    let new_tsr_offset = descriptor.base();\n\n    dbg_assert!(!tss_is_16, \"unimplemented\");\n\n    if true\n    /* is call or int */\n    {\n        safe_write16(\n            new_tsr_offset + TSR_BACKLINK,\n            *sreg.offset(TR as isize) as i32,\n        )\n        .unwrap();\n    }\n\n    let new_cr3 = safe_read32s(new_tsr_offset + TSR_CR3).unwrap();\n\n    *flags &= !FLAG_VM;\n\n    let new_eip = safe_read32s(new_tsr_offset + TSR_EIP).unwrap();\n    let new_cs = safe_read16(new_tsr_offset + TSR_CS).unwrap();\n    let new_cs_selector = SegmentSelector::of_u16(new_cs as u16);\n    let new_cs_descriptor =\n        match lookup_segment_selector(new_cs_selector).expect(\"TODO: handle pagefault\") {\n            Ok((desc, _)) => desc,\n            Err(SelectorNullOrInvalid::IsNull) => {\n                dbg_log!(\"null cs\");\n                panic!(\"#TS handler\");\n            },\n            Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                dbg_log!(\"invalid cs: {:x}\", new_cs);\n                panic!(\"#TS handler\");\n            },\n        };\n\n    if new_cs_descriptor.is_system() {\n        panic!(\"#TS handler\");\n    }\n\n    if !new_cs_descriptor.is_executable() {\n        panic!(\"#TS handler\");\n    }\n\n    if new_cs_descriptor.is_dc() && new_cs_descriptor.dpl() > new_cs_selector.rpl() {\n        dbg_log!(\"cs conforming and dpl > rpl: {:x}\", selector.raw);\n        panic!(\"#TS handler\");\n    }\n\n    if !new_cs_descriptor.is_dc() && new_cs_descriptor.dpl() != new_cs_selector.rpl() {\n        dbg_log!(\"cs non-conforming and dpl != rpl: {:x}\", selector.raw);\n        panic!(\"#TS handler\");\n    }\n\n    if !new_cs_descriptor.is_present() {\n        dbg_log!(\"#NP for loading not-present in cs sel={:x}\", selector.raw);\n        panic!(\"#TS handler\");\n    }\n\n    *segment_is_null.offset(CS as isize) = false;\n    *segment_limits.offset(CS as isize) = new_cs_descriptor.effective_limit();\n    *segment_offsets.offset(CS as isize) = new_cs_descriptor.base();\n    *segment_access_bytes.offset(CS as isize) = new_cs_descriptor.access_byte();\n    *sreg.offset(CS as isize) = new_cs as u16;\n\n    *cpl = new_cs_descriptor.dpl();\n    cpl_changed();\n\n    dbg_assert!((*sreg.offset(CS as isize) & 3) as u8 == *cpl);\n\n    dbg_assert!(\n        new_eip as u32 <= new_cs_descriptor.effective_limit(),\n        \"todo: #gp\"\n    );\n    update_cs_size(new_cs_descriptor.is_32());\n\n    let mut new_eflags = safe_read32s(new_tsr_offset + TSR_EFLAGS).unwrap();\n\n    if true\n    /* is call or int */\n    {\n        safe_write32(tsr_offset + TSR_BACKLINK, selector.raw as i32).unwrap();\n        new_eflags |= FLAG_NT;\n    }\n\n    if new_eflags & FLAG_VM != 0 {\n        panic!(\"task switch to VM mode\");\n    }\n\n    update_eflags(new_eflags);\n\n    if true\n    /* call or int */\n    {\n        *flags |= FLAG_NT;\n    }\n\n    let new_ldt = safe_read16(new_tsr_offset + TSR_LDT).unwrap();\n    load_ldt(new_ldt).unwrap();\n\n    write_reg32(EAX, safe_read32s(new_tsr_offset + TSR_EAX).unwrap());\n    write_reg32(ECX, safe_read32s(new_tsr_offset + TSR_ECX).unwrap());\n    write_reg32(EDX, safe_read32s(new_tsr_offset + TSR_EDX).unwrap());\n    write_reg32(EBX, safe_read32s(new_tsr_offset + TSR_EBX).unwrap());\n\n    write_reg32(ESP, safe_read32s(new_tsr_offset + TSR_ESP).unwrap());\n    write_reg32(EBP, safe_read32s(new_tsr_offset + TSR_EBP).unwrap());\n    write_reg32(ESI, safe_read32s(new_tsr_offset + TSR_ESI).unwrap());\n    write_reg32(EDI, safe_read32s(new_tsr_offset + TSR_EDI).unwrap());\n\n    if !switch_seg(ES, safe_read16(new_tsr_offset + TSR_ES).unwrap())\n        || !switch_seg(SS, safe_read16(new_tsr_offset + TSR_SS).unwrap())\n        || !switch_seg(DS, safe_read16(new_tsr_offset + TSR_DS).unwrap())\n        || !switch_seg(FS, safe_read16(new_tsr_offset + TSR_FS).unwrap())\n        || !switch_seg(GS, safe_read16(new_tsr_offset + TSR_GS).unwrap())\n    {\n        // XXX: Should be checked before side effects\n        dbg_assert!(false);\n    }\n\n    *instruction_pointer = get_seg_cs() + new_eip;\n\n    *segment_offsets.offset(TR as isize) = descriptor.base();\n    *segment_limits.offset(TR as isize) = descriptor.effective_limit();\n    *sreg.offset(TR as isize) = selector.raw;\n\n    set_cr3(new_cr3);\n\n    *cr.offset(0) |= CR0_TS;\n\n    if let Some(error_code) = error_code {\n        if tss_is_16 {\n            push16(error_code & 0xFFFF).unwrap();\n        }\n        else {\n            push32(error_code).unwrap();\n        }\n    }\n\n    update_state_flags();\n}\n\npub unsafe fn after_block_boundary() { jit_block_boundary = true; }\n\n#[no_mangle]\npub fn track_jit_exit(phys_addr: u32) {\n    unsafe {\n        debug_last_jump = LastJump::Compiled { phys_addr };\n    }\n}\n\n#[no_mangle]\npub unsafe fn get_eflags() -> i32 {\n    return *flags & !FLAGS_ALL\n        | getcf() as i32\n        | (getpf() as i32) << 2\n        | (getaf() as i32) << 4\n        | (getzf() as i32) << 6\n        | (getsf() as i32) << 7\n        | (getof() as i32) << 11;\n}\n\npub unsafe fn readable_or_pagefault(addr: i32, size: i32) -> OrPageFault<()> {\n    dbg_assert!(size < 0x1000);\n    dbg_assert!(size > 0);\n\n    let user = *cpl == 3;\n    translate_address(addr, false, user, false, true)?;\n\n    let end = addr + size - 1 & !0xFFF;\n    if addr & !0xFFF != end & !0xFFF {\n        translate_address(end, false, user, false, true)?;\n    }\n\n    return Ok(());\n}\n\npub unsafe fn writable_or_pagefault(addr: i32, size: i32) -> OrPageFault<()> {\n    writable_or_pagefault_cpl(*cpl, addr, size)\n}\n\npub unsafe fn writable_or_pagefault_cpl(other_cpl: u8, addr: i32, size: i32) -> OrPageFault<()> {\n    dbg_assert!(size < 0x1000);\n    dbg_assert!(size > 0);\n\n    let user = other_cpl == 3;\n    translate_address(addr, true, user, false, true)?;\n\n    let end = addr + size - 1 & !0xFFF;\n    if addr & !0xFFF != end & !0xFFF {\n        translate_address(end, true, user, false, true)?;\n    }\n\n    return Ok(());\n}\n\npub fn translate_address_read_no_side_effects(address: i32) -> OrPageFault<u32> {\n    unsafe { translate_address(address, false, *cpl == 3, false, false) }\n}\npub fn translate_address_read(address: i32) -> OrPageFault<u32> {\n    unsafe { translate_address(address, false, *cpl == 3, false, true) }\n}\npub unsafe fn translate_address_read_jit(address: i32) -> OrPageFault<u32> {\n    translate_address(address, false, *cpl == 3, true, true)\n}\n\npub unsafe fn translate_address_write(address: i32) -> OrPageFault<u32> {\n    translate_address(address, true, *cpl == 3, false, true)\n}\npub unsafe fn translate_address_write_jit_and_can_skip_dirty(\n    address: i32,\n) -> OrPageFault<(u32, bool)> {\n    let mut entry = tlb_data[(address as u32 >> 12) as usize];\n    let user = *cpl == 3;\n    if entry & (TLB_VALID | if user { TLB_NO_USER } else { 0 } | TLB_READONLY) != TLB_VALID {\n        entry = do_page_walk(address, true, user, true, true)?.get();\n    }\n    Ok((\n        (entry & !0xFFF ^ address) as u32 - memory::mem8 as u32,\n        entry & TLB_HAS_CODE == 0,\n    ))\n}\n\npub unsafe fn translate_address_system_read(address: i32) -> OrPageFault<u32> {\n    translate_address(address, false, false, false, true)\n}\npub unsafe fn translate_address_system_write(address: i32) -> OrPageFault<u32> {\n    translate_address(address, true, false, false, true)\n}\n\n#[inline(always)]\npub unsafe fn translate_address(\n    address: i32,\n    for_writing: bool,\n    user: bool,\n    jit: bool,\n    side_effects: bool,\n) -> OrPageFault<u32> {\n    let mut entry = tlb_data[(address as u32 >> 12) as usize];\n    if entry\n        & (TLB_VALID\n            | if user { TLB_NO_USER } else { 0 }\n            | if for_writing { TLB_READONLY } else { 0 })\n        != TLB_VALID\n    {\n        entry = do_page_walk(address, for_writing, user, jit, side_effects)?.get();\n    }\n    Ok((entry & !0xFFF ^ address) as u32 - memory::mem8 as u32)\n}\n\npub unsafe fn translate_address_write_and_can_skip_dirty(address: i32) -> OrPageFault<(u32, bool)> {\n    let mut entry = tlb_data[(address as u32 >> 12) as usize];\n    let user = *cpl == 3;\n    if entry & (TLB_VALID | if user { TLB_NO_USER } else { 0 } | TLB_READONLY) != TLB_VALID {\n        entry = do_page_walk(address, true, user, false, true)?.get();\n    }\n    Ok((\n        (entry & !0xFFF ^ address) as u32 - memory::mem8 as u32,\n        entry & TLB_HAS_CODE == 0,\n    ))\n}\n\n// 32-bit paging:\n// - 10 bits PD | 10 bits PT | 12 bits offset\n// - 10 bits PD | 22 bits offset (4MB huge page)\n//\n// PAE paging:\n// - 2 bits PDPT | 9 bits PD | 9 bits PT | 12 bits offset\n// - 2 bits PDPT | 9 bits PD | 21 bits offset (2MB huge page)\n//\n// Note that PAE entries are 64-bit, and can describe physical addresses over 32\n// bits. However, since we support only 32-bit physical addresses, we require\n// the high half of the entry to be 0.\n#[cold]\npub unsafe fn do_page_walk(\n    addr: i32,\n    for_writing: bool,\n    user: bool,\n    jit: bool,\n    side_effects: bool,\n) -> OrPageFault<std::num::NonZeroI32> {\n    let global;\n    let mut allow_user = true;\n    let page = (addr as u32 >> 12) as i32;\n    let high;\n\n    let cr0 = *cr;\n    let cr4 = *cr.offset(4);\n\n    if cr0 & CR0_PG == 0 {\n        // paging disabled\n        high = addr as u32 & 0xFFFFF000;\n        global = false\n    }\n    else {\n        profiler::stat_increment(stat::TLB_MISS);\n\n        let pae = cr4 & CR4_PAE != 0;\n\n        let (page_dir_addr, page_dir_entry) = if pae {\n            let pdpt_entry = *reg_pdpte.offset(((addr as u32) >> 30) as isize);\n            if pdpt_entry as i32 & PAGE_TABLE_PRESENT_MASK == 0 {\n                if side_effects {\n                    trigger_pagefault(addr, false, for_writing, user, jit);\n                }\n                return Err(());\n            }\n\n            let page_dir_addr =\n                (pdpt_entry as u32 & 0xFFFFF000) + ((((addr as u32) >> 21) & 0x1FF) << 3);\n            let page_dir_entry = memory::read64s(page_dir_addr);\n            dbg_assert!(\n                page_dir_entry as u64 & 0x7FFF_FFFF_0000_0000 == 0,\n                \"Unsupported: Page directory entry larger than 32 bits\"\n            );\n            dbg_assert!(\n                page_dir_entry & 0x8000_0000_0000_0000u64 as i64 == 0,\n                \"Unsupported: NX bit\"\n            );\n\n            (page_dir_addr, page_dir_entry as i32)\n        }\n        else {\n            let page_dir_addr = *cr.offset(3) as u32 + (((addr as u32) >> 22) << 2);\n            let page_dir_entry = memory::read32s(page_dir_addr);\n            (page_dir_addr, page_dir_entry)\n        };\n\n        if page_dir_entry & PAGE_TABLE_PRESENT_MASK == 0 {\n            if side_effects {\n                trigger_pagefault(addr, false, for_writing, user, jit);\n            }\n            return Err(());\n        }\n\n        let kernel_write_override = !user && 0 == cr0 & CR0_WP;\n        let mut allow_write = page_dir_entry & PAGE_TABLE_RW_MASK != 0;\n        allow_user &= page_dir_entry & PAGE_TABLE_USER_MASK != 0;\n\n        if 0 != page_dir_entry & PAGE_TABLE_PSE_MASK && 0 != cr4 & CR4_PSE {\n            // size bit is set\n\n            if for_writing && !allow_write && !kernel_write_override || user && !allow_user {\n                if side_effects {\n                    trigger_pagefault(addr, true, for_writing, user, jit);\n                }\n                return Err(());\n            }\n\n            // set the accessed and dirty bits\n\n            let new_page_dir_entry = page_dir_entry\n                | PAGE_TABLE_ACCESSED_MASK\n                | if for_writing { PAGE_TABLE_DIRTY_MASK } else { 0 };\n\n            if side_effects && page_dir_entry != new_page_dir_entry {\n                memory::write8(page_dir_addr, new_page_dir_entry);\n            }\n\n            high = if pae {\n                page_dir_entry as u32 & 0xFFE00000 | (addr & 0x1FF000) as u32\n            }\n            else {\n                page_dir_entry as u32 & 0xFFC00000 | (addr & 0x3FF000) as u32\n            };\n            global = page_dir_entry & PAGE_TABLE_GLOBAL_MASK == PAGE_TABLE_GLOBAL_MASK\n        }\n        else {\n            let (page_table_addr, page_table_entry) = if pae {\n                let page_table_addr =\n                    (page_dir_entry as u32 & 0xFFFFF000) + (((addr as u32 >> 12) & 0x1FF) << 3);\n                let page_table_entry = memory::read64s(page_table_addr);\n                dbg_assert!(\n                    page_table_entry as u64 & 0x7FFF_FFFF_0000_0000 == 0,\n                    \"Unsupported: Page table entry larger than 32 bits\"\n                );\n                dbg_assert!(\n                    page_table_entry & 0x8000_0000_0000_0000u64 as i64 == 0,\n                    \"Unsupported: NX bit\"\n                );\n\n                (page_table_addr, page_table_entry as i32)\n            }\n            else {\n                let page_table_addr =\n                    (page_dir_entry as u32 & 0xFFFFF000) + (((addr as u32 >> 12) & 0x3FF) << 2);\n                let page_table_entry = memory::read32s(page_table_addr);\n                (page_table_addr, page_table_entry)\n            };\n\n            let present = page_table_entry & PAGE_TABLE_PRESENT_MASK != 0;\n            allow_write &= page_table_entry & PAGE_TABLE_RW_MASK != 0;\n            allow_user &= page_table_entry & PAGE_TABLE_USER_MASK != 0;\n\n            if !present\n                || for_writing && !allow_write && !kernel_write_override\n                || user && !allow_user\n            {\n                if side_effects {\n                    trigger_pagefault(addr, present, for_writing, user, jit);\n                }\n                return Err(());\n            }\n\n            // Set the accessed and dirty bits\n            // Note: dirty bit is only set on the page table entry\n            let new_page_dir_entry = page_dir_entry | PAGE_TABLE_ACCESSED_MASK;\n            if side_effects && new_page_dir_entry != page_dir_entry {\n                memory::write8(page_dir_addr, new_page_dir_entry);\n            }\n            let new_page_table_entry = page_table_entry\n                | PAGE_TABLE_ACCESSED_MASK\n                | if for_writing { PAGE_TABLE_DIRTY_MASK } else { 0 };\n            if side_effects && page_table_entry != new_page_table_entry {\n                memory::write8(page_table_addr, new_page_table_entry);\n            }\n\n            high = page_table_entry as u32 & 0xFFFFF000;\n            global = page_table_entry & PAGE_TABLE_GLOBAL_MASK == PAGE_TABLE_GLOBAL_MASK\n        }\n    }\n\n    if side_effects && tlb_data[page as usize] == 0 {\n        if valid_tlb_entries_count == VALID_TLB_ENTRY_MAX {\n            profiler::stat_increment(stat::TLB_FULL);\n            clear_tlb();\n            // also clear global entries if tlb is almost full after clearing non-global pages\n            if valid_tlb_entries_count > VALID_TLB_ENTRY_MAX * 3 / 4 {\n                profiler::stat_increment(stat::TLB_GLOBAL_FULL);\n                full_clear_tlb();\n            }\n        }\n        dbg_assert!(valid_tlb_entries_count < VALID_TLB_ENTRY_MAX);\n        valid_tlb_entries[valid_tlb_entries_count as usize] = page;\n        valid_tlb_entries_count += 1;\n    // TODO: Check that there are no duplicates in valid_tlb_entries\n    // XXX: There will probably be duplicates due to invlpg deleting\n    // entries from tlb_data but not from valid_tlb_entries\n    }\n    else if side_effects && CHECK_TLB_INVARIANTS {\n        let mut found = false;\n        for i in 0..valid_tlb_entries_count {\n            if valid_tlb_entries[i as usize] == page {\n                found = true;\n                break;\n            }\n        }\n        dbg_assert!(found);\n    }\n\n    let is_in_mapped_range = memory::in_mapped_range(high);\n    let has_code = if side_effects {\n        !is_in_mapped_range && jit::jit_page_has_code(Page::page_of(high))\n    }\n    else {\n        // If side_effects is false, don't call into jit::jit_page_has_code. This value is not used\n        // anyway (we only get here by translate_address_read_no_side_effects, which only uses the\n        // address part)\n        true\n    };\n    let info_bits = TLB_VALID\n        | if for_writing { 0 } else { TLB_READONLY }\n        | if allow_user { 0 } else { TLB_NO_USER }\n        | if is_in_mapped_range { TLB_IN_MAPPED_RANGE } else { 0 }\n        | if global && 0 != cr4 & CR4_PGE { TLB_GLOBAL } else { 0 }\n        | if has_code { TLB_HAS_CODE } else { 0 };\n\n    let tlb_entry = (high + memory::mem8 as u32) as i32 ^ page << 12 | info_bits as i32;\n\n    dbg_assert!((high ^ (page as u32) << 12) & 0xFFF == 0);\n    if side_effects {\n        // bake in the addition with memory::mem8 to save an instruction from the fast path\n        // of memory accesses\n        tlb_data[page as usize] = tlb_entry;\n\n        jit::update_tlb_code(Page::page_of(addr as u32), Page::page_of(high));\n    }\n\n    Ok(if DEBUG {\n        std::num::NonZeroI32::new(tlb_entry).unwrap()\n    }\n    else {\n        std::num::NonZeroI32::new_unchecked(tlb_entry)\n    })\n}\n\n#[no_mangle]\npub unsafe fn full_clear_tlb() {\n    profiler::stat_increment(stat::FULL_CLEAR_TLB);\n    // clear tlb including global pages\n    *last_virt_eip = -1;\n    for i in 0..valid_tlb_entries_count {\n        let page = valid_tlb_entries[i as usize];\n        clear_tlb_code(page);\n        tlb_data[page as usize] = 0;\n    }\n    valid_tlb_entries_count = 0;\n\n    if CHECK_TLB_INVARIANTS {\n        #[allow(static_mut_refs)]\n        for &entry in tlb_data.iter() {\n            dbg_assert!(entry == 0);\n        }\n    };\n}\n\n#[no_mangle]\npub unsafe fn clear_tlb() {\n    profiler::stat_increment(stat::CLEAR_TLB);\n    // clear tlb excluding global pages\n    *last_virt_eip = -1;\n    let mut global_page_offset = 0;\n    for i in 0..valid_tlb_entries_count {\n        let page = valid_tlb_entries[i as usize];\n        let entry = tlb_data[page as usize];\n        if 0 != entry & TLB_GLOBAL {\n            // reinsert at the front\n            valid_tlb_entries[global_page_offset as usize] = page;\n            global_page_offset += 1;\n        }\n        else {\n            clear_tlb_code(page);\n            tlb_data[page as usize] = 0;\n        }\n    }\n    valid_tlb_entries_count = global_page_offset;\n\n    if CHECK_TLB_INVARIANTS {\n        #[allow(static_mut_refs)]\n        for &entry in tlb_data.iter() {\n            dbg_assert!(entry == 0 || 0 != entry & TLB_GLOBAL);\n        }\n    };\n}\n\n#[no_mangle]\npub unsafe fn trigger_de_jit(eip_offset_in_page: i32) {\n    dbg_log!(\"#de in jit mode\");\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n    jit_fault = Some((CPU_EXCEPTION_DE, None))\n}\n\n#[no_mangle]\npub unsafe fn trigger_ud_jit(eip_offset_in_page: i32) {\n    dbg_log!(\"#ud in jit mode\");\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n    jit_fault = Some((CPU_EXCEPTION_UD, None))\n}\n\n#[no_mangle]\npub unsafe fn trigger_nm_jit(eip_offset_in_page: i32) {\n    dbg_log!(\"#nm in jit mode\");\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n    jit_fault = Some((CPU_EXCEPTION_NM, None))\n}\n\n#[no_mangle]\npub unsafe fn trigger_gp_jit(code: i32, eip_offset_in_page: i32) {\n    dbg_log!(\"#gp in jit mode\");\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n    jit_fault = Some((CPU_EXCEPTION_GP, Some(code)))\n}\n\n#[no_mangle]\npub unsafe fn trigger_fault_end_jit() {\n    #[allow(static_mut_refs)]\n    let (code, error_code) = jit_fault.take().unwrap();\n    if DEBUG {\n        if js::cpu_exception_hook(code) {\n            return;\n        }\n    }\n    call_interrupt_vector(code, false, error_code);\n}\n\n/// Pagefault handling with the jit works as follows:\n/// - If the slow path is taken, it calls safe_{read,write}*_jit\n/// - safe_{read,write}*_jit call translate_address_{read,write}_jit\n/// - translate_address_{read,write}_jit do the normal page walk and call this method with\n///   jit=true when a page fault happens\n/// - this method prepares a page fault by setting cr2, and writes the error code\n///   into jit_fault. This method *doesn't* trigger the interrupt, as registers are\n///   still stored in the wasm module\n/// - back in the wasm module, the generated code detects the page fault, restores the registers\n///   and finally calls trigger_fault_end_jit, which does the interrupt\n///\n/// Non-jit resets the instruction pointer and does the PF interrupt directly\npub unsafe fn trigger_pagefault(addr: i32, present: bool, write: bool, user: bool, jit: bool) {\n    if config::LOG_PAGE_FAULTS {\n        dbg_log!(\n            \"page fault{} w={} u={} p={} eip={:x} cr2={:x}\",\n            if jit { \"jit\" } else { \"\" },\n            write as i32,\n            user as i32,\n            present as i32,\n            *previous_ip,\n            addr\n        );\n        dbg_trace();\n    }\n    profiler::stat_increment(stat::PAGE_FAULT);\n    *cr.offset(2) = addr;\n    // invalidate tlb entry\n    let page = ((addr as u32) >> 12) as i32;\n    clear_tlb_code(page);\n    tlb_data[page as usize] = 0;\n    let error_code = (user as i32) << 2 | (write as i32) << 1 | present as i32;\n    if jit {\n        jit_fault = Some((CPU_EXCEPTION_PF, Some(error_code)));\n    }\n    else {\n        *instruction_pointer = *previous_ip;\n        call_interrupt_vector(CPU_EXCEPTION_PF, false, Some(error_code));\n    }\n}\n\npub fn tlb_set_has_code(physical_page: Page, has_code: bool) {\n    for i in 0..unsafe { valid_tlb_entries_count } {\n        let page = unsafe { valid_tlb_entries[i as usize] };\n        let entry = unsafe { tlb_data[page as usize] };\n        if 0 != entry {\n            let tlb_physical_page = Page::of_u32(\n                (entry as u32 >> 12 ^ page as u32) - (unsafe { memory::mem8 } as u32 >> 12),\n            );\n            if physical_page == tlb_physical_page {\n                unsafe {\n                    tlb_data[page as usize] =\n                        if has_code { entry | TLB_HAS_CODE } else { entry & !TLB_HAS_CODE }\n                }\n                if !has_code {\n                    clear_tlb_code(page);\n                }\n            }\n        }\n    }\n\n    check_tlb_invariants();\n}\npub fn tlb_set_has_code_multiple(physical_pages: &HashSet<Page>, has_code: bool) {\n    let physical_pages: Vec<Page> = physical_pages.into_iter().copied().collect();\n    for i in 0..unsafe { valid_tlb_entries_count } {\n        let page = unsafe { valid_tlb_entries[i as usize] };\n        let entry = unsafe { tlb_data[page as usize] };\n        if 0 != entry {\n            let tlb_physical_page = Page::of_u32(\n                (entry as u32 >> 12 ^ page as u32) - (unsafe { memory::mem8 } as u32 >> 12),\n            );\n            if physical_pages.contains(&tlb_physical_page) {\n                unsafe {\n                    tlb_data[page as usize] =\n                        if has_code { entry | TLB_HAS_CODE } else { entry & !TLB_HAS_CODE }\n                }\n            }\n        }\n    }\n\n    check_tlb_invariants();\n}\n\npub fn check_tlb_invariants() {\n    if !CHECK_TLB_INVARIANTS {\n        return;\n    }\n\n    for i in 0..unsafe { valid_tlb_entries_count } {\n        let page = unsafe { valid_tlb_entries[i as usize] };\n        let entry = unsafe { tlb_data[page as usize] };\n\n        if 0 == entry || 0 != entry & TLB_IN_MAPPED_RANGE {\n            // there's no code in mapped memory\n            continue;\n        }\n\n        let target = (entry ^ page << 12) as u32 - unsafe { memory::mem8 } as u32;\n        dbg_assert!(!memory::in_mapped_range(target));\n\n        let entry_has_code = entry & TLB_HAS_CODE != 0;\n        let has_code = jit::jit_page_has_code(Page::page_of(target));\n\n        // If some code has been created in a page, the corresponding tlb entries must be marked\n        dbg_assert!(!has_code || entry_has_code);\n    }\n}\n\npub const DISABLE_EIP_TRANSLATION_OPTIMISATION: bool = false;\n\npub unsafe fn read_imm8() -> OrPageFault<i32> {\n    let eip = *instruction_pointer;\n    if DISABLE_EIP_TRANSLATION_OPTIMISATION || 0 != eip & !0xFFF ^ *last_virt_eip {\n        *eip_phys = (translate_address_read(eip)? ^ eip as u32) as i32;\n        *last_virt_eip = eip & !0xFFF\n    }\n    dbg_assert!(!memory::in_mapped_range((*eip_phys ^ eip) as u32));\n    let data8 = *memory::mem8.offset((*eip_phys ^ eip) as isize) as i32;\n    *instruction_pointer = eip + 1;\n    return Ok(data8);\n}\n\npub unsafe fn read_imm8s() -> OrPageFault<i32> { return Ok(read_imm8()? << 24 >> 24); }\n\npub unsafe fn read_imm16() -> OrPageFault<i32> {\n    // Two checks in one comparison:\n    // 1. Did the high 20 bits of eip change\n    // or 2. Are the low 12 bits of eip 0xFFF (and this read crosses a page boundary)\n    if DISABLE_EIP_TRANSLATION_OPTIMISATION\n        || (*instruction_pointer ^ *last_virt_eip) as u32 > 0xFFE\n    {\n        return Ok(read_imm8()? | read_imm8()? << 8);\n    }\n    else {\n        let data16 = memory::read16((*eip_phys ^ *instruction_pointer) as u32);\n        *instruction_pointer = *instruction_pointer + 2;\n        return Ok(data16);\n    };\n}\n\npub unsafe fn read_imm32s() -> OrPageFault<i32> {\n    // Analogue to the above comment\n    if DISABLE_EIP_TRANSLATION_OPTIMISATION\n        || (*instruction_pointer ^ *last_virt_eip) as u32 > 0xFFC\n    {\n        return Ok(read_imm16()? | read_imm16()? << 16);\n    }\n    else {\n        let data32 = memory::read32s((*eip_phys ^ *instruction_pointer) as u32);\n        *instruction_pointer = *instruction_pointer + 4;\n        return Ok(data32);\n    };\n}\n\npub unsafe fn is_osize_32() -> bool {\n    dbg_assert!(!in_jit);\n    return *is_32 != (*prefixes & prefix::PREFIX_MASK_OPSIZE == prefix::PREFIX_MASK_OPSIZE);\n}\n\npub unsafe fn is_asize_32() -> bool {\n    dbg_assert!(!in_jit);\n    return *is_32 != (*prefixes & prefix::PREFIX_MASK_ADDRSIZE == prefix::PREFIX_MASK_ADDRSIZE);\n}\n\npub unsafe fn lookup_segment_selector(\n    selector: SegmentSelector,\n) -> OrPageFault<Result<(SegmentDescriptor, i32), SelectorNullOrInvalid>> {\n    if selector.is_null() {\n        return Ok(Err(SelectorNullOrInvalid::IsNull));\n    }\n\n    let (table_offset, table_limit) = if selector.is_gdt() {\n        (*gdtr_offset as u32, *gdtr_size as u32)\n    }\n    else {\n        (\n            *segment_offsets.offset(LDTR as isize) as u32,\n            *segment_limits.offset(LDTR as isize) as u32,\n        )\n    };\n\n    if selector.descriptor_offset() as u32 > table_limit {\n        dbg_log!(\n            \"segment outside of table limit: selector={:x} offset={:x} isgdt={} table_limit={:x}\",\n            selector.raw,\n            selector.descriptor_offset(),\n            selector.is_gdt(),\n            table_limit\n        );\n        return Ok(Err(SelectorNullOrInvalid::OutsideOfTableLimit));\n    }\n\n    let descriptor_address = selector.descriptor_offset() as i32 + table_offset as i32;\n\n    let descriptor = SegmentDescriptor::of_u64(memory::read64s(translate_address_system_read(\n        descriptor_address,\n    )?) as u64);\n\n    Ok(Ok((descriptor, descriptor_address)))\n}\n\n#[inline(never)]\npub unsafe fn switch_seg(reg: i32, selector_raw: i32) -> bool {\n    dbg_assert!(reg >= 0 && reg <= 5);\n    dbg_assert!(reg != CS);\n    dbg_assert!(selector_raw >= 0 && selector_raw < 0x10000);\n\n    if vm86_mode() {\n        // TODO: Should set segment_limits and segment_access_bytes if ever implemented in get_seg\n        //       (only vm86, not in real mode)\n    }\n\n    if !*protected_mode || vm86_mode() {\n        *sreg.offset(reg as isize) = selector_raw as u16;\n        *segment_is_null.offset(reg as isize) = false;\n        *segment_offsets.offset(reg as isize) = selector_raw << 4;\n\n        if reg == SS {\n            *stack_size_32 = false;\n        }\n        update_state_flags();\n        return true;\n    }\n\n    let selector = SegmentSelector::of_u16(selector_raw as u16);\n    let (mut descriptor, descriptor_address) =\n        match return_on_pagefault!(lookup_segment_selector(selector), false) {\n            Ok(desc) => desc,\n            Err(SelectorNullOrInvalid::IsNull) => {\n                if reg == SS {\n                    dbg_log!(\"#GP for loading 0 in SS sel={:x}\", selector_raw);\n                    trigger_gp(0);\n                    return false;\n                }\n                else {\n                    // es, ds, fs, gs\n                    *sreg.offset(reg as isize) = selector_raw as u16;\n                    *segment_is_null.offset(reg as isize) = true;\n                    update_state_flags();\n                    return true;\n                }\n            },\n            Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                dbg_log!(\n                    \"#GP for loading invalid in seg={} sel={:x}\",\n                    reg,\n                    selector_raw,\n                );\n                dbg_trace();\n                trigger_gp(selector_raw & !3);\n                return false;\n            },\n        };\n\n    if reg == SS {\n        if descriptor.is_system()\n            || selector.rpl() != *cpl\n            || !descriptor.is_writable()\n            || descriptor.dpl() != *cpl\n        {\n            dbg_log!(\"#GP for loading invalid in SS sel={:x}\", selector_raw);\n            trigger_gp(selector_raw & !3);\n            return false;\n        }\n\n        if !descriptor.is_present() {\n            dbg_log!(\"#SS for loading non-present in SS sel={:x}\", selector_raw);\n            trigger_ss(selector_raw & !3);\n            return false;\n        }\n\n        *stack_size_32 = descriptor.is_32();\n    }\n    else {\n        if descriptor.is_system()\n            || !descriptor.is_readable()\n            || (!descriptor.is_conforming_executable()\n                && (selector.rpl() > descriptor.dpl() || *cpl > descriptor.dpl()))\n        {\n            dbg_log!(\n                \"#GP for loading invalid in seg {} sel={:x} sys={} readable={} dc={} exec={} rpl={} dpl={} cpl={} present={} paging={}\",\n                reg,\n                selector_raw,\n                descriptor.is_system(),\n                descriptor.is_readable(),\n                descriptor.is_dc(),\n                descriptor.is_executable(),\n                selector.rpl(),\n                descriptor.dpl(),\n                *cpl,\n                descriptor.is_present(),\n                *cr & CR0_PG != 0,\n            );\n            dbg_trace();\n            trigger_gp(selector_raw & !3);\n            return false;\n        }\n\n        if !descriptor.is_present() {\n            dbg_log!(\n                \"#NP for loading not-present in seg {} sel={:x}\",\n                reg,\n                selector_raw,\n            );\n            trigger_np(selector_raw & !3);\n            return false;\n        }\n    }\n\n    if !descriptor.accessed() {\n        descriptor = descriptor.set_accessed();\n\n        memory::write8(\n            translate_address_system_write(descriptor_address + 5).unwrap(),\n            descriptor.access_byte() as i32,\n        );\n    }\n\n    *segment_is_null.offset(reg as isize) = false;\n    *segment_limits.offset(reg as isize) = descriptor.effective_limit();\n    *segment_offsets.offset(reg as isize) = descriptor.base();\n    *segment_access_bytes.offset(reg as isize) = descriptor.access_byte();\n    *sreg.offset(reg as isize) = selector_raw as u16;\n\n    update_state_flags();\n\n    true\n}\n\npub unsafe fn load_tr(selector: i32) {\n    let selector = SegmentSelector::of_u16(selector as u16);\n    dbg_assert!(selector.is_gdt(), \"TODO: TR can only be loaded from GDT\");\n\n    let (descriptor, descriptor_address) =\n        match return_on_pagefault!(lookup_segment_selector(selector)) {\n            Ok((desc, addr)) => (desc, addr),\n            Err(SelectorNullOrInvalid::IsNull) => {\n                panic!(\"TODO: null TR\");\n            },\n            Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n                panic!(\"TODO: TR selector outside of table limit\");\n            },\n        };\n\n    //dbg_log!(\n    //    \"load tr: {:x} offset={:x} limit={:x} is32={}\",\n    //    selector.raw,\n    //    descriptor.base(),\n    //    descriptor.effective_limit(),\n    //    descriptor.system_type() == 9,\n    //);\n\n    if !descriptor.is_system() {\n        panic!(\"#GP | ltr: not a system entry (happens when running kvm-unit-test without ACPI)\");\n    }\n\n    if descriptor.system_type() != 9 && descriptor.system_type() != 1 {\n        // 0xB: busy 386 TSS (GP)\n        // 0x9: 386 TSS\n        // 0x3: busy 286 TSS (GP)\n        // 0x1: 286 TSS (??)\n        panic!(\n            \"#GP | ltr: invalid type (type = 0x{:x})\",\n            descriptor.system_type()\n        );\n    }\n\n    if !descriptor.is_present() {\n        panic!(\"#NT | present bit not set (ltr)\");\n    }\n\n    *tss_size_32 = descriptor.system_type() == 9;\n    *segment_limits.offset(TR as isize) = descriptor.effective_limit();\n    *segment_offsets.offset(TR as isize) = descriptor.base();\n    *sreg.offset(TR as isize) = selector.raw;\n\n    // Mark task as busy\n    memory::write8(\n        translate_address_system_write(descriptor_address + 5).unwrap(),\n        descriptor.set_busy().access_byte() as i32,\n    );\n}\n\npub unsafe fn load_ldt(selector: i32) -> OrPageFault<()> {\n    let selector = SegmentSelector::of_u16(selector as u16);\n\n    if selector.is_null() {\n        dbg_log!(\"lldt: null loaded\");\n        *segment_limits.offset(LDTR as isize) = 0;\n        *segment_offsets.offset(LDTR as isize) = 0;\n        *sreg.offset(LDTR as isize) = selector.raw;\n        return Ok(());\n    }\n\n    dbg_assert!(selector.is_gdt(), \"TODO: LDT can only be loaded from GDT\");\n\n    let (descriptor, _) = match lookup_segment_selector(selector)? {\n        Ok((desc, addr)) => (desc, addr),\n        Err(SelectorNullOrInvalid::IsNull) => {\n            panic!(\"TODO: null TR\");\n        },\n        Err(SelectorNullOrInvalid::OutsideOfTableLimit) => {\n            panic!(\"TODO: TR selector outside of table limit\");\n        },\n    };\n\n    if !descriptor.is_present() {\n        panic!(\"#NT | present bit not set (lldt)\");\n    }\n\n    if !descriptor.is_system() {\n        panic!(\"#GP | lldt: not a system entry\");\n    }\n\n    if descriptor.system_type() != 2 {\n        panic!(\n            \"#GP | lldt: invalid type (type = 0x{:x})\",\n            descriptor.system_type()\n        );\n    }\n\n    dbg_log!(\n        \"lldt: {:x} offset={:x} limit={:x}\",\n        selector.raw,\n        descriptor.base(),\n        descriptor.effective_limit()\n    );\n    *segment_limits.offset(LDTR as isize) = descriptor.effective_limit();\n    *segment_offsets.offset(LDTR as isize) = descriptor.base();\n    *sreg.offset(LDTR as isize) = selector.raw;\n\n    Ok(())\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub unsafe fn log_segment_null(segment: i32) {\n    dbg_assert!(segment >= 0 && segment < 8);\n    if *segment_is_null.offset(segment as isize) {\n        dbg_assert!(segment != CS && segment != SS);\n        dbg_log!(\"#gp: Access null segment in jit\");\n    }\n}\n\npub unsafe fn get_seg(segment: i32) -> OrPageFault<i32> {\n    dbg_assert!(segment >= 0 && segment < 8);\n    if *segment_is_null.offset(segment as isize) {\n        dbg_assert!(segment != CS && segment != SS);\n        dbg_log!(\"#gp: Access null segment {}\", segment);\n        dbg_trace();\n        dbg_assert!(!in_jit);\n        trigger_gp(0);\n        return Err(());\n    }\n    return Ok(*segment_offsets.offset(segment as isize));\n}\n\npub unsafe fn set_cr0(cr0: i32) {\n    let old_cr0 = *cr;\n\n    if old_cr0 & CR0_AM == 0 && cr0 & CR0_AM != 0 {\n        dbg_log!(\"Warning: Unimplemented: cr0 alignment mask\");\n    }\n    if (cr0 & (CR0_PE | CR0_PG)) == CR0_PG {\n        panic!(\"cannot load PG without PE\");\n    }\n\n    *cr = cr0;\n    *cr |= CR0_ET;\n\n    if old_cr0 & (CR0_PG | CR0_WP) != cr0 & (CR0_PG | CR0_WP) {\n        full_clear_tlb();\n    }\n\n    if *cr.offset(4) & CR4_PAE != 0\n        && old_cr0 & (CR0_CD | CR0_NW | CR0_PG) != cr0 & (CR0_CD | CR0_NW | CR0_PG)\n    {\n        load_pdpte(*cr.offset(3))\n    }\n\n    *protected_mode = (*cr & CR0_PE) == CR0_PE;\n    *segment_access_bytes.offset(CS as isize) = 0x80 | 0x10 | 0x08 | 0x02; // P dpl0 S E RW\n}\n\npub unsafe fn set_cr3(mut cr3: i32) {\n    if false {\n        dbg_log!(\"cr3 <- {:x}\", cr3);\n    }\n    if *cr.offset(4) & CR4_PAE != 0 {\n        cr3 &= !0b1111;\n        load_pdpte(cr3);\n    }\n    else {\n        cr3 &= !0b111111100111;\n        dbg_assert!(cr3 & 0xFFF == 0, \"TODO\");\n    }\n    *cr.offset(3) = cr3;\n    clear_tlb();\n}\n\npub unsafe fn load_pdpte(cr3: i32) {\n    dbg_assert!(cr3 & 0b1111 == 0);\n    for i in 0..4 {\n        let mut pdpt_entry = memory::read64s(cr3 as u32 + 8 * i as u32) as u64;\n        pdpt_entry &= !0b1110_0000_0000;\n        dbg_assert!(pdpt_entry & 0b11000 == 0, \"TODO\");\n        dbg_assert!(\n            pdpt_entry as u64 & 0xFFFF_FFFF_0000_0000 == 0,\n            \"Unsupported: PDPT entry larger than 32 bits\"\n        );\n        if pdpt_entry as i32 & PAGE_TABLE_PRESENT_MASK != 0 {\n            dbg_assert!(\n                pdpt_entry & 0b1_1110_0110 == 0,\n                \"TODO: #gp reserved bit in pdpte\"\n            );\n        }\n        *reg_pdpte.offset(i) = pdpt_entry;\n    }\n}\n\npub unsafe fn cpl_changed() { *last_virt_eip = -1 }\n\npub unsafe fn update_cs_size(new_size: bool) {\n    if *is_32 != new_size {\n        *is_32 = new_size;\n    }\n}\n\n#[inline(never)]\npub unsafe fn test_privileges_for_io(port: i32, size: i32) -> bool {\n    if *protected_mode && (*cpl > getiopl() as u8 || (*flags & FLAG_VM != 0)) {\n        if !*tss_size_32 {\n            dbg_log!(\"#GP for port io, 16-bit TSS  port={:x} size={}\", port, size);\n            trigger_gp(0);\n            return false;\n        }\n\n        let tsr_size = *segment_limits.offset(TR as isize);\n        let tsr_offset = *segment_offsets.offset(TR as isize);\n\n        if tsr_size >= 0x67 {\n            dbg_assert!(tsr_offset + 0x64 + 2 & 0xFFF < 0xFFF);\n\n            let iomap_base = memory::read16(return_on_pagefault!(\n                translate_address_system_read(tsr_offset + 0x64 + 2),\n                false\n            ));\n            let high_port = port + size - 1;\n\n            if tsr_size >= (iomap_base + (high_port >> 3)) as u32 {\n                let mask = ((1 << size) - 1) << (port & 7);\n                let addr = return_on_pagefault!(\n                    translate_address_system_read(tsr_offset + iomap_base + (port >> 3)),\n                    false\n                );\n                let port_info =\n                    if mask & 0xFF00 != 0 { memory::read16(addr) } else { memory::read8(addr) };\n\n                dbg_assert!(addr & 0xFFF < 0xFFF);\n\n                if port_info & mask == 0 {\n                    return true;\n                }\n            }\n        }\n\n        dbg_log!(\"#GP for port io  port={:x} size={}\", port, size);\n        trigger_gp(0);\n        return false;\n    }\n\n    return true;\n}\n\npub unsafe fn popa16() {\n    return_on_pagefault!(readable_or_pagefault(get_stack_pointer(0), 16));\n\n    write_reg16(DI, pop16().unwrap());\n    write_reg16(SI, pop16().unwrap());\n    write_reg16(BP, pop16().unwrap());\n    adjust_stack_reg(2);\n    write_reg16(BX, pop16().unwrap());\n    write_reg16(DX, pop16().unwrap());\n    write_reg16(CX, pop16().unwrap());\n    write_reg16(AX, pop16().unwrap());\n}\n\npub unsafe fn popa32() {\n    return_on_pagefault!(readable_or_pagefault(get_stack_pointer(0), 32));\n\n    write_reg32(EDI, pop32s().unwrap());\n    write_reg32(ESI, pop32s().unwrap());\n    write_reg32(EBP, pop32s().unwrap());\n    adjust_stack_reg(4);\n    write_reg32(EBX, pop32s().unwrap());\n    write_reg32(EDX, pop32s().unwrap());\n    write_reg32(ECX, pop32s().unwrap());\n    write_reg32(EAX, pop32s().unwrap());\n}\n\npub fn get_state_flags() -> CachedStateFlags { unsafe { *state_flags } }\n\n#[no_mangle]\npub fn get_seg_cs() -> i32 { unsafe { *segment_offsets.offset(CS as isize) } }\n\npub unsafe fn get_seg_ss() -> i32 { return *segment_offsets.offset(SS as isize); }\n\npub unsafe fn segment_prefix(default_segment: i32) -> i32 {\n    let prefix = *prefixes & prefix::PREFIX_MASK_SEGMENT;\n    if 0 != prefix {\n        dbg_assert!(prefix != prefix::SEG_PREFIX_ZERO);\n        prefix as i32 - 1\n    }\n    else {\n        default_segment\n    }\n}\n\npub unsafe fn get_seg_prefix(default_segment: i32) -> OrPageFault<i32> {\n    dbg_assert!(!in_jit);\n    let prefix = *prefixes & prefix::PREFIX_MASK_SEGMENT;\n    if 0 != prefix {\n        if prefix == prefix::SEG_PREFIX_ZERO {\n            return Ok(0);\n        }\n        else {\n            return get_seg(prefix as i32 - 1);\n        }\n    }\n    else {\n        return get_seg(default_segment);\n    };\n}\n\npub unsafe fn get_seg_prefix_ds(offset: i32) -> OrPageFault<i32> {\n    Ok(get_seg_prefix(DS)? + offset)\n}\n\npub unsafe fn get_seg_prefix_ss(offset: i32) -> OrPageFault<i32> {\n    Ok(get_seg_prefix(SS)? + offset)\n}\n\npub unsafe fn modrm_resolve(modrm_byte: i32) -> OrPageFault<i32> {\n    if is_asize_32() {\n        resolve_modrm32(modrm_byte)\n    }\n    else {\n        resolve_modrm16(modrm_byte)\n    }\n}\n\npub unsafe fn run_instruction(opcode: i32) { gen::interpreter::run(opcode as u32) }\npub unsafe fn run_instruction0f_16(opcode: i32) { gen::interpreter0f::run(opcode as u32) }\npub unsafe fn run_instruction0f_32(opcode: i32) { gen::interpreter0f::run(opcode as u32 | 0x100) }\n\npub unsafe fn cycle_internal() {\n    profiler::stat_increment(stat::CYCLE_INTERNAL);\n    let mut jit_entry = None;\n    let initial_eip = *instruction_pointer;\n    let initial_state_flags = *state_flags;\n\n    match tlb_code[(initial_eip as u32 >> 12) as usize] {\n        None => {},\n        Some(c) => {\n            let c = c.as_ref();\n\n            if initial_state_flags == c.state_flags {\n                let state = c.state_table[initial_eip as usize & 0xFFF];\n                if state != u16::MAX {\n                    jit_entry = Some((c.wasm_table_index.to_u16(), state));\n                }\n                else {\n                    profiler::stat_increment(if is_near_end_of_page(initial_eip as u32) {\n                        stat::RUN_INTERPRETED_NEAR_END_OF_PAGE\n                    }\n                    else {\n                        stat::RUN_INTERPRETED_PAGE_HAS_CODE\n                    })\n                }\n            }\n            else {\n                profiler::stat_increment(stat::RUN_INTERPRETED_DIFFERENT_STATE);\n                let s = *state_flags;\n                if c.state_flags.cpl3() != s.cpl3() {\n                    profiler::stat_increment(stat::RUN_INTERPRETED_DIFFERENT_STATE_CPL3);\n                }\n                if c.state_flags.has_flat_segmentation() != s.has_flat_segmentation() {\n                    profiler::stat_increment(stat::RUN_INTERPRETED_DIFFERENT_STATE_FLAT);\n                }\n                if c.state_flags.is_32() != s.is_32() {\n                    profiler::stat_increment(stat::RUN_INTERPRETED_DIFFERENT_STATE_IS32);\n                }\n                if c.state_flags.ssize_32() != s.ssize_32() {\n                    profiler::stat_increment(stat::RUN_INTERPRETED_DIFFERENT_STATE_SS32);\n                }\n            }\n        },\n    }\n\n    if let Some((wasm_table_index, initial_state)) = jit_entry {\n        if jit::CHECK_JIT_STATE_INVARIANTS {\n            match get_phys_eip() {\n                Err(()) => dbg_assert!(false),\n                Ok(phys_eip) => {\n                    let entry = jit::jit_find_cache_entry(phys_eip, initial_state_flags);\n                    dbg_assert!(entry.wasm_table_index.to_u16() == wasm_table_index);\n                    dbg_assert!(entry.initial_state == initial_state);\n                },\n            }\n        }\n        profiler::stat_increment(stat::RUN_FROM_CACHE);\n        let initial_instruction_counter = *instruction_counter;\n        #[cfg(debug_assertions)]\n        {\n            in_jit = true;\n        }\n        wasm::call_indirect1(\n            wasm_table_index as i32 + WASM_TABLE_OFFSET as i32,\n            initial_state,\n        );\n        #[cfg(debug_assertions)]\n        {\n            in_jit = false;\n        }\n        profiler::stat_increment_by(\n            stat::RUN_FROM_CACHE_STEPS,\n            (*instruction_counter - initial_instruction_counter) as u64,\n        );\n        dbg_assert!(\n            *instruction_counter != initial_instruction_counter,\n            \"Instruction counter didn't change\"\n        );\n\n        if cfg!(feature = \"profiler\") {\n            dbg_assert!(match debug_last_jump {\n                LastJump::Compiled { .. } => true,\n                _ => false,\n            });\n            #[allow(static_mut_refs)]\n            let last_jump_addr = debug_last_jump.phys_address().unwrap();\n            let last_jump_opcode = if last_jump_addr != 0 {\n                memory::read32s(last_jump_addr)\n            }\n            else {\n                // Happens during exit due to loop iteration limit\n                0\n            };\n\n            opstats::record_opstat_jit_exit(last_jump_opcode as u32);\n        }\n\n        if is_near_end_of_page(*instruction_pointer as u32) {\n            profiler::stat_increment(stat::RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE);\n        }\n        else if Page::page_of(initial_eip as u32) == Page::page_of(*instruction_pointer as u32) {\n            profiler::stat_increment(stat::RUN_FROM_CACHE_EXIT_SAME_PAGE);\n        }\n        else {\n            profiler::stat_increment(stat::RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE);\n        }\n    }\n    else {\n        *previous_ip = initial_eip;\n        let phys_addr = return_on_pagefault!(get_phys_eip());\n\n        match tlb_code[(initial_eip as u32 >> 12) as usize] {\n            None => {},\n            Some(c) => {\n                let c = c.as_ref();\n\n                if initial_state_flags == c.state_flags\n                    && c.state_table[initial_eip as usize & 0xFFF] != u16::MAX\n                {\n                    profiler::stat_increment(stat::RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK);\n                    return;\n                }\n            },\n        }\n\n        #[cfg(feature = \"profiler\")]\n        {\n            if CHECK_MISSED_ENTRY_POINTS {\n                jit::check_missed_entry_points(phys_addr, initial_state_flags);\n            }\n        }\n\n        let initial_instruction_counter = *instruction_counter;\n        jit_run_interpreted(phys_addr);\n\n        jit::jit_increase_hotness_and_maybe_compile(\n            initial_eip,\n            phys_addr,\n            get_seg_cs() as u32,\n            initial_state_flags,\n            *instruction_counter - initial_instruction_counter,\n        );\n\n        profiler::stat_increment_by(\n            stat::RUN_INTERPRETED_STEPS,\n            (*instruction_counter - initial_instruction_counter) as u64,\n        );\n        dbg_assert!(\n            *instruction_counter != initial_instruction_counter,\n            \"Instruction counter didn't change\"\n        );\n    };\n}\n\npub unsafe fn get_phys_eip() -> OrPageFault<u32> {\n    let eip = *instruction_pointer;\n    if 0 != eip & !0xFFF ^ *last_virt_eip {\n        *eip_phys = (translate_address_read(eip)? ^ eip as u32) as i32;\n        *last_virt_eip = eip & !0xFFF\n    }\n    let phys_addr = (*eip_phys ^ eip) as u32;\n    dbg_assert!(!memory::in_mapped_range(phys_addr));\n    return Ok(phys_addr);\n}\n\nunsafe fn jit_run_interpreted(mut phys_addr: u32) {\n    profiler::stat_increment(stat::RUN_INTERPRETED);\n    dbg_assert!(!memory::in_mapped_range(phys_addr));\n\n    jit_block_boundary = false;\n    let mut i = 0;\n\n    loop {\n        if CHECK_MISSED_ENTRY_POINTS {\n            let entry = jit::jit_find_cache_entry(phys_addr, *state_flags);\n            if entry != jit::CachedCode::NONE {\n                profiler::stat_increment(\n                    stat::RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED,\n                );\n            }\n        }\n\n        i += 1;\n        let start_eip = *instruction_pointer;\n        let opcode = *memory::mem8.offset(phys_addr as isize) as i32;\n        *instruction_pointer += 1;\n        dbg_assert!(*prefixes == 0);\n        run_instruction(opcode | (*is_32 as i32) << 8);\n        dbg_assert!(*prefixes == 0);\n\n        if jit_block_boundary\n            || Page::page_of(start_eip as u32) != Page::page_of(*instruction_pointer as u32)\n                // Limit the number of iterations, as jumps within the same page are not counted as\n                // block boundaries for the interpreter, but only on the next backwards jump\n            || (i >= INTERPRETER_ITERATION_LIMIT\n                && (start_eip as u32) >= (*instruction_pointer as u32))\n        {\n            break;\n        }\n\n        *previous_ip = *instruction_pointer;\n        phys_addr = return_on_pagefault!(get_phys_eip()) as u32;\n    }\n\n    if cfg!(debug_assertions) {\n        debug_last_jump = LastJump::Interpreted { phys_addr };\n    }\n\n    *instruction_counter += i;\n}\n\n#[no_mangle]\npub fn update_state_flags() {\n    unsafe {\n        *state_flags = CachedStateFlags::of_u32(\n            (*is_32 as u32) << 0\n                | (*stack_size_32 as u32) << 1\n                | ((*cpl == 3) as u32) << 2\n                | (has_flat_segmentation() as u32) << 3,\n        )\n    }\n}\n\n#[no_mangle]\npub unsafe fn has_flat_segmentation() -> bool {\n    // cs/ss can't be null\n    return *segment_offsets.offset(SS as isize) == 0\n        && !*segment_is_null.offset(DS as isize)\n        && *segment_offsets.offset(DS as isize) == 0\n        && *segment_offsets.offset(CS as isize) == 0;\n}\n\npub unsafe fn run_prefix_instruction() {\n    run_instruction(return_on_pagefault!(read_imm8()) | (is_osize_32() as i32) << 8);\n}\n\npub unsafe fn segment_prefix_op(seg: i32) {\n    dbg_assert!(seg <= 5 && seg >= 0);\n    *prefixes |= seg as u8 + 1;\n    run_prefix_instruction();\n    *prefixes = 0\n}\n\n#[no_mangle]\npub unsafe fn main_loop() -> f64 {\n    profiler::stat_increment(stat::MAIN_LOOP);\n\n    let start = js::microtick();\n\n    if *in_hlt {\n        if *flags & FLAG_INTERRUPT != 0 {\n            let t = js::run_hardware_timers(*acpi_enabled, start);\n            handle_irqs();\n            if *in_hlt {\n                profiler::stat_increment(stat::MAIN_LOOP_IDLE);\n                return t;\n            }\n        }\n        else {\n            // dead\n            return 100.0;\n        }\n    }\n\n    loop {\n        do_many_cycles_native();\n\n        let now = js::microtick();\n        let t = js::run_hardware_timers(*acpi_enabled, now);\n        handle_irqs();\n        if *in_hlt {\n            return t;\n        }\n\n        if now - start > TIME_PER_FRAME {\n            break;\n        }\n    }\n\n    return 0.0;\n}\n\npub unsafe fn do_many_cycles_native() {\n    profiler::stat_increment(stat::DO_MANY_CYCLES);\n    let initial_instruction_counter = *instruction_counter;\n    while (*instruction_counter).wrapping_sub(initial_instruction_counter) < LOOP_COUNTER as u32\n        && !*in_hlt\n    {\n        cycle_internal();\n    }\n}\n\n#[cold]\npub unsafe fn trigger_de() {\n    dbg_log!(\"#de\");\n    *instruction_pointer = *previous_ip;\n    if DEBUG {\n        if js::cpu_exception_hook(CPU_EXCEPTION_DE) {\n            return;\n        }\n    }\n    call_interrupt_vector(CPU_EXCEPTION_DE, false, None);\n}\n\n#[inline(never)]\npub unsafe fn trigger_ud() {\n    dbg_log!(\"#ud\");\n    dbg_trace();\n    *instruction_pointer = *previous_ip;\n    if DEBUG {\n        if js::cpu_exception_hook(CPU_EXCEPTION_UD) {\n            return;\n        }\n    }\n    call_interrupt_vector(CPU_EXCEPTION_UD, false, None);\n}\n\n#[inline(never)]\npub unsafe fn trigger_nm() {\n    dbg_log!(\"#nm eip={:x}\", *previous_ip);\n    dbg_trace();\n    *instruction_pointer = *previous_ip;\n    if DEBUG {\n        if js::cpu_exception_hook(CPU_EXCEPTION_NM) {\n            return;\n        }\n    }\n    call_interrupt_vector(CPU_EXCEPTION_NM, false, None);\n}\n\n#[inline(never)]\npub unsafe fn trigger_gp(code: i32) {\n    dbg_log!(\"#gp\");\n    *instruction_pointer = *previous_ip;\n    if DEBUG {\n        if js::cpu_exception_hook(CPU_EXCEPTION_GP) {\n            return;\n        }\n    }\n    call_interrupt_vector(CPU_EXCEPTION_GP, false, Some(code));\n}\n\n#[cold]\npub unsafe fn virt_boundary_read16(low: u32, high: u32) -> i32 {\n    dbg_assert!(low & 0xFFF == 0xFFF);\n    dbg_assert!(high & 0xFFF == 0);\n    return memory::read8(low as u32) | memory::read8(high as u32) << 8;\n}\n\n#[cold]\npub unsafe fn virt_boundary_read32s(low: u32, high: u32) -> i32 {\n    dbg_assert!(low & 0xFFF >= 0xFFD);\n    dbg_assert!(high - 3 & 0xFFF == low & 0xFFF);\n    let mid;\n    if 0 != low & 1 {\n        if 0 != low & 2 {\n            // 0xFFF\n            mid = memory::read16(high - 2)\n        }\n        else {\n            // 0xFFD\n            mid = memory::read16(low + 1)\n        }\n    }\n    else {\n        // 0xFFE\n        mid = virt_boundary_read16(low + 1, high - 1)\n    }\n    return memory::read8(low as u32) | mid << 8 | memory::read8(high as u32) << 24;\n}\n\n#[cold]\npub unsafe fn virt_boundary_write16(low: u32, high: u32, value: i32) {\n    dbg_assert!(low & 0xFFF == 0xFFF);\n    dbg_assert!(high & 0xFFF == 0);\n    memory::write8(low as u32, value);\n    memory::write8(high as u32, value >> 8);\n}\n\n#[cold]\npub unsafe fn virt_boundary_write32(low: u32, high: u32, value: i32) {\n    dbg_assert!(low & 0xFFF >= 0xFFD);\n    dbg_assert!(high - 3 & 0xFFF == low & 0xFFF);\n    memory::write8(low as u32, value);\n    if 0 != low & 1 {\n        if 0 != low & 2 {\n            // 0xFFF\n            memory::write8((high - 2) as u32, value >> 8);\n            memory::write8((high - 1) as u32, value >> 16);\n        }\n        else {\n            // 0xFFD\n            memory::write8((low + 1) as u32, value >> 8);\n            memory::write8((low + 2) as u32, value >> 16);\n        }\n    }\n    else {\n        // 0xFFE\n        memory::write8((low + 1) as u32, value >> 8);\n        memory::write8((high - 1) as u32, value >> 16);\n    }\n    memory::write8(high as u32, value >> 24);\n}\n\npub unsafe fn safe_read8(addr: i32) -> OrPageFault<i32> {\n    Ok(memory::read8(translate_address_read(addr)?))\n}\n\npub unsafe fn safe_read16(addr: i32) -> OrPageFault<i32> {\n    if addr & 0xFFF == 0xFFF {\n        Ok(safe_read8(addr)? | safe_read8(addr + 1)? << 8)\n    }\n    else {\n        Ok(memory::read16(translate_address_read(addr)?))\n    }\n}\n\npub unsafe fn safe_read32s(addr: i32) -> OrPageFault<i32> {\n    if addr & 0xFFF >= 0xFFD {\n        Ok(safe_read16(addr)? | safe_read16(addr + 2)? << 16)\n    }\n    else {\n        Ok(memory::read32s(translate_address_read(addr)?))\n    }\n}\n\npub unsafe fn safe_read_f32(addr: i32) -> OrPageFault<f32> {\n    Ok(f32::from_bits(i32::cast_unsigned(safe_read32s(addr)?)))\n}\n\npub unsafe fn safe_read64s(addr: i32) -> OrPageFault<u64> {\n    if addr & 0xFFF > 0x1000 - 8 {\n        Ok(safe_read32s(addr)? as u32 as u64 | (safe_read32s(addr + 4)? as u32 as u64) << 32)\n    }\n    else {\n        Ok(memory::read64s(translate_address_read(addr)?) as u64)\n    }\n}\n\npub unsafe fn safe_read128s(addr: i32) -> OrPageFault<reg128> {\n    if addr & 0xFFF > 0x1000 - 16 {\n        Ok(reg128 {\n            u64: [safe_read64s(addr)?, safe_read64s(addr + 8)?],\n        })\n    }\n    else {\n        Ok(memory::read128(translate_address_read(addr)?))\n    }\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub fn report_safe_read_jit_slow(address: u32, entry: i32) {\n    if entry & TLB_VALID == 0 {\n        profiler::stat_increment(stat::SAFE_READ_SLOW_NOT_VALID);\n    }\n    else if entry & TLB_IN_MAPPED_RANGE != 0 {\n        profiler::stat_increment(stat::SAFE_READ_SLOW_IN_MAPPED_RANGE);\n    }\n    else if entry & TLB_NO_USER != 0 {\n        profiler::stat_increment(stat::SAFE_READ_SLOW_NOT_USER);\n    }\n    else if address & 0xFFF > 0x1000 - 16 {\n        profiler::stat_increment(stat::SAFE_READ_SLOW_PAGE_CROSSED);\n    }\n    else {\n        dbg_log!(\"Unexpected entry bit: {:x} (read at {:x})\", entry, address);\n        dbg_assert!(false);\n    }\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub fn report_safe_write_jit_slow(address: u32, entry: i32) {\n    if entry & TLB_VALID == 0 {\n        profiler::stat_increment(stat::SAFE_WRITE_SLOW_NOT_VALID);\n    }\n    else if entry & TLB_IN_MAPPED_RANGE != 0 {\n        profiler::stat_increment(stat::SAFE_WRITE_SLOW_IN_MAPPED_RANGE);\n    }\n    else if entry & TLB_HAS_CODE != 0 {\n        profiler::stat_increment(stat::SAFE_WRITE_SLOW_HAS_CODE);\n    }\n    else if entry & TLB_READONLY != 0 {\n        profiler::stat_increment(stat::SAFE_WRITE_SLOW_READ_ONLY);\n    }\n    else if entry & TLB_NO_USER != 0 {\n        profiler::stat_increment(stat::SAFE_WRITE_SLOW_NOT_USER);\n    }\n    else if address & 0xFFF > 0x1000 - 16 {\n        profiler::stat_increment(stat::SAFE_WRITE_SLOW_PAGE_CROSSED);\n    }\n    else {\n        dbg_assert!(false);\n    }\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub fn report_safe_read_write_jit_slow(address: u32, entry: i32) {\n    if entry & TLB_VALID == 0 {\n        profiler::stat_increment(stat::SAFE_READ_WRITE_SLOW_NOT_VALID);\n    }\n    else if entry & TLB_IN_MAPPED_RANGE != 0 {\n        profiler::stat_increment(stat::SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE);\n    }\n    else if entry & TLB_HAS_CODE != 0 {\n        profiler::stat_increment(stat::SAFE_READ_WRITE_SLOW_HAS_CODE);\n    }\n    else if entry & TLB_READONLY != 0 {\n        profiler::stat_increment(stat::SAFE_READ_WRITE_SLOW_READ_ONLY);\n    }\n    else if entry & TLB_NO_USER != 0 {\n        profiler::stat_increment(stat::SAFE_READ_WRITE_SLOW_NOT_USER);\n    }\n    else if address & 0xFFF > 0x1000 - 16 {\n        profiler::stat_increment(stat::SAFE_READ_WRITE_SLOW_PAGE_CROSSED);\n    }\n    else {\n        dbg_assert!(false);\n    }\n}\n\n#[repr(align(0x1000))]\nstruct ScratchBuffer([u8; 0x1000 * 2]);\nstatic mut jit_paging_scratch_buffer: ScratchBuffer = ScratchBuffer([0; 2 * 0x1000]);\n\npub unsafe fn safe_read_slow_jit(\n    addr: i32,\n    bitsize: i32,\n    eip_offset_in_page: i32,\n    is_write: bool,\n) -> i32 {\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    if is_write && Page::page_of(*instruction_pointer as u32) == Page::page_of(addr as u32) {\n        // XXX: Check based on virtual address\n        dbg_log!(\n            \"SMC (rmw): bits={} eip={:x} writeaddr={:x}\",\n            bitsize,\n            (*instruction_pointer & !0xFFF | eip_offset_in_page) as u32,\n            addr as u32\n        );\n    }\n    let crosses_page = (addr & 0xFFF) + bitsize / 8 > 0x1000;\n    let addr_low = match if is_write {\n        translate_address_write_jit_and_can_skip_dirty(addr).map(|x| x.0)\n    }\n    else {\n        translate_address_read_jit(addr)\n    } {\n        Err(()) => {\n            *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n            return 1;\n        },\n        Ok(addr) => addr,\n    };\n    if crosses_page {\n        let boundary_addr = (addr | 0xFFF) + 1;\n        let addr_high = match if is_write {\n            translate_address_write_jit_and_can_skip_dirty(boundary_addr).map(|x| x.0)\n        }\n        else {\n            translate_address_read_jit(boundary_addr)\n        } {\n            Err(()) => {\n                *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n                return 1;\n            },\n            Ok(addr) => addr,\n        };\n        // TODO: Could check if virtual pages point to consecutive physical and go to fast path\n        // do read, write into scratch buffer\n\n        let scratch = &raw mut jit_paging_scratch_buffer.0 as u32;\n        dbg_assert!(scratch & 0xFFF == 0);\n\n        for s in addr_low..((addr_low | 0xFFF) + 1) {\n            *(scratch as *mut u8).offset((s & 0xFFF) as isize) = memory::read8(s) as u8\n        }\n        for s in addr_high..(addr_high + (addr + bitsize / 8 & 0xFFF) as u32) {\n            *(scratch as *mut u8).offset((0x1000 | s & 0xFFF) as isize) = memory::read8(s) as u8\n        }\n\n        ((scratch as i32) ^ addr) & !0xFFF\n    }\n    else if memory::in_mapped_range(addr_low) {\n        let scratch = &raw mut jit_paging_scratch_buffer.0[0];\n\n        match bitsize {\n            128 => ptr::write_unaligned(\n                scratch.offset(addr_low as isize & 0xFFF) as *mut reg128,\n                memory::read128(addr_low),\n            ),\n            64 => ptr::write_unaligned(\n                scratch.offset(addr_low as isize & 0xFFF) as *mut i64,\n                memory::read64s(addr_low),\n            ),\n            32 => ptr::write_unaligned(\n                scratch.offset(addr_low as isize & 0xFFF) as *mut i32,\n                memory::read32s(addr_low),\n            ),\n            16 => ptr::write_unaligned(\n                scratch.offset(addr_low as isize & 0xFFF) as *mut u16,\n                memory::read16(addr_low) as u16,\n            ),\n            8 => {\n                *(scratch.offset(addr_low as isize & 0xFFF) as *mut u8) =\n                    memory::read8(addr_low) as u8\n            },\n            _ => {\n                dbg_assert!(false);\n            },\n        }\n\n        ((scratch as i32) ^ addr) & !0xFFF\n    }\n    else {\n        ((addr_low as i32 + memory::mem8 as i32) ^ addr) & !0xFFF\n    }\n}\n\n#[no_mangle]\npub unsafe fn safe_read8_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 8, eip, false)\n}\n#[no_mangle]\npub unsafe fn safe_read16_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 16, eip, false)\n}\n#[no_mangle]\npub unsafe fn safe_read32s_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 32, eip, false)\n}\n#[no_mangle]\npub unsafe fn safe_read64s_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 64, eip, false)\n}\n#[no_mangle]\npub unsafe fn safe_read128s_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 128, eip, false)\n}\n\n#[no_mangle]\npub unsafe fn get_phys_eip_slow_jit(addr: i32) -> i32 {\n    match translate_address_read_jit(addr) {\n        Err(()) => 1,\n        Ok(addr_low) => {\n            dbg_assert!(!memory::in_mapped_range(addr_low as u32)); // same assumption as in read_imm8\n            ((addr_low as i32 + memory::mem8 as i32) ^ addr) & !0xFFF\n        },\n    }\n}\n\n#[no_mangle]\npub unsafe fn safe_read_write8_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 8, eip, true)\n}\n#[no_mangle]\npub unsafe fn safe_read_write16_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 16, eip, true)\n}\n#[no_mangle]\npub unsafe fn safe_read_write32s_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 32, eip, true)\n}\n#[no_mangle]\npub unsafe fn safe_read_write64_slow_jit(addr: i32, eip: i32) -> i32 {\n    safe_read_slow_jit(addr, 64, eip, true)\n}\n\npub unsafe fn safe_write_slow_jit(\n    addr: i32,\n    bitsize: i32,\n    value_low: u64,\n    value_high: u64,\n    eip_offset_in_page: i32,\n) -> i32 {\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    if Page::page_of(*instruction_pointer as u32) == Page::page_of(addr as u32) {\n        // XXX: Check based on virtual address\n        dbg_log!(\n            \"SMC: bits={} eip={:x} writeaddr={:x} value={:x}\",\n            bitsize,\n            (*instruction_pointer & !0xFFF | eip_offset_in_page) as u32,\n            addr as u32,\n            value_low,\n        );\n    }\n    let crosses_page = (addr & 0xFFF) + bitsize / 8 > 0x1000;\n    let (addr_low, can_skip_dirty_page) = match translate_address_write_jit_and_can_skip_dirty(addr)\n    {\n        Err(()) => {\n            *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n            return 1;\n        },\n        Ok(x) => x,\n    };\n    if crosses_page {\n        let (addr_high, _) =\n            match translate_address_write_jit_and_can_skip_dirty((addr | 0xFFF) + 1) {\n                Err(()) => {\n                    *instruction_pointer = *instruction_pointer & !0xFFF | eip_offset_in_page;\n                    return 1;\n                },\n                Ok(x) => x,\n            };\n        // TODO: Could check if virtual pages point to consecutive physical and go to fast path\n\n        // do write, return dummy pointer for fast path to write into\n\n        match bitsize {\n            128 => safe_write128(\n                addr,\n                reg128 {\n                    u64: [value_low, value_high],\n                },\n            )\n            .unwrap(),\n            64 => safe_write64(addr, value_low).unwrap(),\n            32 => virt_boundary_write32(\n                addr_low,\n                addr_high | (addr as u32 + 3 & 3),\n                value_low as i32,\n            ),\n            16 => virt_boundary_write16(addr_low, addr_high, value_low as i32),\n            8 => {\n                dbg_assert!(false);\n            },\n            _ => {\n                dbg_assert!(false);\n            },\n        }\n\n        let scratch = &raw mut jit_paging_scratch_buffer.0 as u32;\n        dbg_assert!(scratch & 0xFFF == 0);\n        ((scratch as i32) ^ addr) & !0xFFF\n    }\n    else if memory::in_mapped_range(addr_low) {\n        match bitsize {\n            128 => memory::mmap_write128(addr_low, value_low, value_high),\n            64 => memory::mmap_write64(addr_low, value_low),\n            32 => memory::mmap_write32(addr_low, value_low as i32),\n            16 => memory::mmap_write16(addr_low, (value_low & 0xFFFF) as i32),\n            8 => memory::mmap_write8(addr_low, (value_low & 0xFF) as i32),\n            _ => {\n                dbg_assert!(false);\n            },\n        }\n\n        let scratch = &raw mut jit_paging_scratch_buffer.0 as u32;\n        dbg_assert!(scratch & 0xFFF == 0);\n        ((scratch as i32) ^ addr) & !0xFFF\n    }\n    else {\n        if !can_skip_dirty_page {\n            jit::jit_dirty_page(Page::page_of(addr_low));\n        }\n        ((addr_low as i32 + memory::mem8 as i32) ^ addr) & !0xFFF\n    }\n}\n\n#[no_mangle]\npub unsafe fn safe_write8_slow_jit(addr: i32, value: u32, eip_offset_in_page: i32) -> i32 {\n    safe_write_slow_jit(addr, 8, value as u64, 0, eip_offset_in_page)\n}\n#[no_mangle]\npub unsafe fn safe_write16_slow_jit(addr: i32, value: u32, eip_offset_in_page: i32) -> i32 {\n    safe_write_slow_jit(addr, 16, value as u64, 0, eip_offset_in_page)\n}\n#[no_mangle]\npub unsafe fn safe_write32_slow_jit(addr: i32, value: u32, eip_offset_in_page: i32) -> i32 {\n    safe_write_slow_jit(addr, 32, value as u64, 0, eip_offset_in_page)\n}\n#[no_mangle]\npub unsafe fn safe_write64_slow_jit(addr: i32, value: u64, eip_offset_in_page: i32) -> i32 {\n    safe_write_slow_jit(addr, 64, value, 0, eip_offset_in_page)\n}\n#[no_mangle]\npub unsafe fn safe_write128_slow_jit(\n    addr: i32,\n    low: u64,\n    high: u64,\n    eip_offset_in_page: i32,\n) -> i32 {\n    safe_write_slow_jit(addr, 128, low, high, eip_offset_in_page)\n}\n\npub unsafe fn safe_write8(addr: i32, value: i32) -> OrPageFault<()> {\n    let (phys_addr, can_skip_dirty_page) = translate_address_write_and_can_skip_dirty(addr)?;\n    if memory::in_mapped_range(phys_addr) {\n        memory::mmap_write8(phys_addr, value);\n    }\n    else {\n        if !can_skip_dirty_page {\n            jit::jit_dirty_page(Page::page_of(phys_addr));\n        }\n        else {\n            dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n        }\n        memory::write8_no_mmap_or_dirty_check(phys_addr, value);\n    };\n    Ok(())\n}\n\npub unsafe fn safe_write16(addr: i32, value: i32) -> OrPageFault<()> {\n    let (phys_addr, can_skip_dirty_page) = translate_address_write_and_can_skip_dirty(addr)?;\n    dbg_assert!(value >= 0 && value < 0x10000);\n    if addr & 0xFFF == 0xFFF {\n        virt_boundary_write16(phys_addr, translate_address_write(addr + 1)?, value);\n    }\n    else if memory::in_mapped_range(phys_addr) {\n        memory::mmap_write16(phys_addr, value);\n    }\n    else {\n        if !can_skip_dirty_page {\n            jit::jit_dirty_page(Page::page_of(phys_addr));\n        }\n        else {\n            dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n        }\n        memory::write16_no_mmap_or_dirty_check(phys_addr, value);\n    };\n    Ok(())\n}\n\npub unsafe fn safe_write32(addr: i32, value: i32) -> OrPageFault<()> {\n    let (phys_addr, can_skip_dirty_page) = translate_address_write_and_can_skip_dirty(addr)?;\n    if addr & 0xFFF > 0x1000 - 4 {\n        virt_boundary_write32(\n            phys_addr,\n            translate_address_write(addr + 3 & !3)? | (addr as u32 + 3 & 3),\n            value,\n        );\n    }\n    else if memory::in_mapped_range(phys_addr) {\n        memory::mmap_write32(phys_addr, value);\n    }\n    else {\n        if !can_skip_dirty_page {\n            jit::jit_dirty_page(Page::page_of(phys_addr));\n        }\n        else {\n            dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n        }\n        memory::write32_no_mmap_or_dirty_check(phys_addr, value);\n    };\n    Ok(())\n}\n\npub unsafe fn safe_write64(addr: i32, value: u64) -> OrPageFault<()> {\n    if addr & 0xFFF > 0x1000 - 8 {\n        writable_or_pagefault(addr, 8)?;\n        safe_write32(addr, value as i32).unwrap();\n        safe_write32(addr + 4, (value >> 32) as i32).unwrap();\n    }\n    else {\n        let (phys_addr, can_skip_dirty_page) = translate_address_write_and_can_skip_dirty(addr)?;\n        if memory::in_mapped_range(phys_addr) {\n            memory::mmap_write64(phys_addr, value);\n        }\n        else {\n            if !can_skip_dirty_page {\n                jit::jit_dirty_page(Page::page_of(phys_addr));\n            }\n            else {\n                dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n            }\n            memory::write64_no_mmap_or_dirty_check(phys_addr, value);\n        }\n    };\n    Ok(())\n}\n\npub unsafe fn safe_write128(addr: i32, value: reg128) -> OrPageFault<()> {\n    if addr & 0xFFF > 0x1000 - 16 {\n        writable_or_pagefault(addr, 16)?;\n        safe_write64(addr, value.u64[0]).unwrap();\n        safe_write64(addr + 8, value.u64[1]).unwrap();\n    }\n    else {\n        let (phys_addr, can_skip_dirty_page) = translate_address_write_and_can_skip_dirty(addr)?;\n        if memory::in_mapped_range(phys_addr) {\n            memory::mmap_write128(phys_addr, value.u64[0], value.u64[1]);\n        }\n        else {\n            if !can_skip_dirty_page {\n                jit::jit_dirty_page(Page::page_of(phys_addr));\n            }\n            else {\n                dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n            }\n            memory::write128_no_mmap_or_dirty_check(phys_addr, value);\n        }\n    };\n    Ok(())\n}\n\n#[inline(always)]\npub unsafe fn safe_read_write8(addr: i32, instruction: &dyn Fn(i32) -> i32) {\n    let (phys_addr, can_skip_dirty_page) =\n        return_on_pagefault!(translate_address_write_and_can_skip_dirty(addr));\n    let x = memory::read8(phys_addr);\n    let value = instruction(x);\n    dbg_assert!(value >= 0 && value < 0x100);\n    if memory::in_mapped_range(phys_addr) {\n        memory::mmap_write8(phys_addr, value);\n    }\n    else {\n        if !can_skip_dirty_page {\n            jit::jit_dirty_page(Page::page_of(phys_addr));\n        }\n        else {\n            dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n        }\n        memory::write8_no_mmap_or_dirty_check(phys_addr, value);\n    }\n}\n\n#[inline(always)]\npub unsafe fn safe_read_write16(addr: i32, instruction: &dyn Fn(i32) -> i32) {\n    let (phys_addr, can_skip_dirty_page) =\n        return_on_pagefault!(translate_address_write_and_can_skip_dirty(addr));\n    if phys_addr & 0xFFF == 0xFFF {\n        let phys_addr_high = return_on_pagefault!(translate_address_write(addr + 1));\n        let x = virt_boundary_read16(phys_addr, phys_addr_high);\n        virt_boundary_write16(phys_addr, phys_addr_high, instruction(x));\n    }\n    else {\n        let x = memory::read16(phys_addr);\n        let value = instruction(x);\n        dbg_assert!(value >= 0 && value < 0x10000);\n        if memory::in_mapped_range(phys_addr) {\n            memory::mmap_write16(phys_addr, value);\n        }\n        else {\n            if !can_skip_dirty_page {\n                jit::jit_dirty_page(Page::page_of(phys_addr));\n            }\n            else {\n                dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n            }\n            memory::write16_no_mmap_or_dirty_check(phys_addr, value);\n        };\n    }\n}\n\n#[inline(always)]\npub unsafe fn safe_read_write32(addr: i32, instruction: &dyn Fn(i32) -> i32) {\n    let (phys_addr, can_skip_dirty_page) =\n        return_on_pagefault!(translate_address_write_and_can_skip_dirty(addr));\n    if phys_addr & 0xFFF >= 0xFFD {\n        let phys_addr_high = return_on_pagefault!(translate_address_write(addr + 3 & !3));\n        let phys_addr_high = phys_addr_high | (addr as u32) + 3 & 3;\n        let x = virt_boundary_read32s(phys_addr, phys_addr_high);\n        virt_boundary_write32(phys_addr, phys_addr_high, instruction(x));\n    }\n    else {\n        let x = memory::read32s(phys_addr);\n        let value = instruction(x);\n        if memory::in_mapped_range(phys_addr) {\n            memory::mmap_write32(phys_addr, value);\n        }\n        else {\n            if !can_skip_dirty_page {\n                jit::jit_dirty_page(Page::page_of(phys_addr));\n            }\n            else {\n                dbg_assert!(!jit::jit_page_has_code(Page::page_of(phys_addr as u32)));\n            }\n            memory::write32_no_mmap_or_dirty_check(phys_addr, value);\n        };\n    }\n}\n\nfn get_reg8_index(index: i32) -> i32 { return index << 2 & 12 | index >> 2 & 1; }\n\npub unsafe fn read_reg8(index: i32) -> i32 {\n    dbg_assert!(index >= 0 && index < 8);\n    return *reg8.offset(get_reg8_index(index) as isize) as i32;\n}\n\npub unsafe fn write_reg8(index: i32, value: i32) {\n    dbg_assert!(index >= 0 && index < 8);\n    *reg8.offset(get_reg8_index(index) as isize) = value as u8;\n}\n\nfn get_reg16_index(index: i32) -> i32 { return index << 1; }\n\npub unsafe fn read_reg16(index: i32) -> i32 {\n    dbg_assert!(index >= 0 && index < 8);\n    return *reg16.offset(get_reg16_index(index) as isize) as i32;\n}\n\npub unsafe fn write_reg16(index: i32, value: i32) {\n    dbg_assert!(index >= 0 && index < 8);\n    *reg16.offset(get_reg16_index(index) as isize) = value as u16;\n}\n\npub unsafe fn read_reg32(index: i32) -> i32 {\n    dbg_assert!(index >= 0 && index < 8);\n    *reg32.offset(index as isize)\n}\n\npub unsafe fn write_reg32(index: i32, value: i32) {\n    dbg_assert!(index >= 0 && index < 8);\n    *reg32.offset(index as isize) = value;\n}\n\npub unsafe fn read_mmx32s(r: i32) -> i32 { (*fpu_st.offset(r as isize)).mantissa as i32 }\n\npub unsafe fn read_mmx64s(r: i32) -> u64 { (*fpu_st.offset(r as isize)).mantissa }\n\npub unsafe fn write_mmx_reg64(r: i32, data: u64) { (*fpu_st.offset(r as isize)).mantissa = data; }\n\npub unsafe fn read_xmm_f32(r: i32) -> f32 { return (*reg_xmm.offset(r as isize)).f32[0]; }\n\npub unsafe fn read_xmm32(r: i32) -> i32 { return (*reg_xmm.offset(r as isize)).u32[0] as i32; }\n\npub unsafe fn read_xmm64s(r: i32) -> u64 { (*reg_xmm.offset(r as isize)).u64[0] }\n\npub unsafe fn read_xmm128s(r: i32) -> reg128 { return *reg_xmm.offset(r as isize); }\n\npub unsafe fn write_xmm_f32(r: i32, data: f32) { (*reg_xmm.offset(r as isize)).f32[0] = data; }\n\npub unsafe fn write_xmm32(r: i32, data: i32) { (*reg_xmm.offset(r as isize)).i32[0] = data; }\n\npub unsafe fn write_xmm64(r: i32, data: u64) { (*reg_xmm.offset(r as isize)).u64[0] = data }\npub unsafe fn write_xmm_f64(r: i32, data: f64) { (*reg_xmm.offset(r as isize)).f64[0] = data }\n\npub unsafe fn write_xmm128(r: i32, i0: i32, i1: i32, i2: i32, i3: i32) {\n    let x = reg128 {\n        u32: [i0 as u32, i1 as u32, i2 as u32, i3 as u32],\n    };\n    *reg_xmm.offset(r as isize) = x;\n}\n\npub unsafe fn write_xmm128_2(r: i32, i0: u64, i1: u64) {\n    *reg_xmm.offset(r as isize) = reg128 { u64: [i0, i1] };\n}\n\npub unsafe fn write_xmm_reg128(r: i32, data: reg128) { *reg_xmm.offset(r as isize) = data; }\n\n/// Set the fpu tag word to valid and the top-of-stack to 0 on mmx instructions\npub fn transition_fpu_to_mmx() {\n    unsafe {\n        fpu_set_tag_word(0);\n        *fpu_stack_ptr = 0;\n    }\n}\n\npub unsafe fn task_switch_test() -> bool {\n    if 0 != *cr & (CR0_EM | CR0_TS) {\n        trigger_nm();\n        return false;\n    }\n    else {\n        return true;\n    };\n}\n\npub unsafe fn set_mxcsr(new_mxcsr: i32) {\n    dbg_assert!(new_mxcsr & !MXCSR_MASK == 0); // checked by caller\n\n    if *mxcsr & MXCSR_DAZ == 0 && new_mxcsr & MXCSR_DAZ != 0 {\n        dbg_log!(\"Warning: Unimplemented MXCSR bit: Denormals Are Zero\");\n    }\n    if *mxcsr & MXCSR_FZ == 0 && new_mxcsr & MXCSR_FZ != 0 {\n        dbg_log!(\"Warning: Unimplemented MXCSR bit: Flush To Zero\");\n    }\n\n    let rounding_mode = new_mxcsr >> MXCSR_RC_SHIFT & 3;\n    if *mxcsr >> MXCSR_RC_SHIFT & 3 == 0 && rounding_mode != 0 {\n        dbg_log!(\n            \"Warning: Unimplemented MXCSR rounding mode: {}\",\n            rounding_mode\n        );\n    }\n\n    let exception_mask = new_mxcsr >> 7 & 0b111111;\n    if *mxcsr >> 7 & 0b111111 != exception_mask && exception_mask != 0b111111 {\n        dbg_log!(\n            \"Warning: Unimplemented MXCSR exception mask: 0b{:b}\",\n            exception_mask\n        );\n    }\n\n    *mxcsr = new_mxcsr;\n}\n\n#[no_mangle]\npub unsafe fn task_switch_test_jit(eip_offset_in_page: i32) {\n    dbg_assert!(0 != *cr & (CR0_EM | CR0_TS));\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    trigger_nm_jit(eip_offset_in_page);\n}\n\npub unsafe fn task_switch_test_mmx() -> bool {\n    if *cr.offset(4) & CR4_OSFXSR == 0 {\n        dbg_log!(\"Warning: Unimplemented task switch test with cr4.osfxsr=0\");\n    }\n    if 0 != *cr & CR0_EM {\n        trigger_ud();\n        return false;\n    }\n    else if 0 != *cr & CR0_TS {\n        trigger_nm();\n        return false;\n    }\n    else {\n        return true;\n    };\n}\n\n#[no_mangle]\npub unsafe fn task_switch_test_mmx_jit(eip_offset_in_page: i32) {\n    dbg_assert!(eip_offset_in_page >= 0 && eip_offset_in_page < 0x1000);\n    if *cr.offset(4) & CR4_OSFXSR == 0 {\n        dbg_log!(\"Warning: Unimplemented task switch test with cr4.osfxsr=0\");\n    }\n    if 0 != *cr & CR0_EM {\n        trigger_ud_jit(eip_offset_in_page);\n    }\n    else if 0 != *cr & CR0_TS {\n        trigger_nm_jit(eip_offset_in_page);\n    }\n    else {\n        dbg_assert!(false);\n    }\n}\n\npub unsafe fn read_moffs() -> OrPageFault<i32> {\n    // read 2 or 4 byte from ip, depending on address size attribute\n    if is_asize_32() {\n        read_imm32s()\n    }\n    else {\n        read_imm16()\n    }\n}\n\n#[no_mangle]\npub unsafe fn get_real_eip() -> i32 {\n    // Returns the 'real' instruction pointer, without segment offset\n    return *instruction_pointer - get_seg_cs();\n}\n\npub unsafe fn get_stack_reg() -> i32 {\n    if *stack_size_32 {\n        return read_reg32(ESP);\n    }\n    else {\n        return read_reg16(SP);\n    };\n}\n\npub unsafe fn set_stack_reg(value: i32) {\n    if *stack_size_32 {\n        write_reg32(ESP, value)\n    }\n    else {\n        write_reg16(SP, value)\n    };\n}\n\npub unsafe fn get_reg_asize(reg: i32) -> i32 {\n    dbg_assert!(reg == ECX || reg == ESI || reg == EDI);\n    let r = read_reg32(reg);\n    if is_asize_32() {\n        return r;\n    }\n    else {\n        return r & 0xFFFF;\n    };\n}\n\npub unsafe fn set_reg_asize(is_asize_32: bool, reg: i32, value: i32) {\n    dbg_assert!(reg == ECX || reg == ESI || reg == EDI);\n    if is_asize_32 {\n        write_reg32(reg, value)\n    }\n    else {\n        write_reg16(reg, value)\n    };\n}\n\npub unsafe fn decr_ecx_asize(is_asize_32: bool) -> i32 {\n    return if is_asize_32 {\n        write_reg32(ECX, read_reg32(ECX) - 1);\n        read_reg32(ECX)\n    }\n    else {\n        write_reg16(CX, read_reg16(CX) - 1);\n        read_reg16(CX)\n    };\n}\n\n#[no_mangle]\npub unsafe fn set_tsc(low: u32, high: u32) {\n    let new_value = low as u64 | (high as u64) << 32;\n    let current_value = read_tsc();\n    tsc_offset = current_value - new_value;\n}\n\n#[no_mangle]\npub unsafe fn read_tsc() -> u64 {\n    let value = (js::microtick() * TSC_RATE) as u64 - tsc_offset;\n\n    if !TSC_ENABLE_IMPRECISE_BROWSER_WORKAROUND {\n        return value;\n    }\n\n    if value == tsc_last_value {\n        // If the browser returns the same value as last time, extrapolate based on the number of\n        // rdtsc calls between the last two changes\n        tsc_number_of_same_readings += 1;\n        let extra = (tsc_number_of_same_readings * tsc_resolution) / tsc_speed;\n        let extra = u64::min(extra, tsc_resolution - 1);\n        #[cfg(debug_assertions)]\n        {\n            tsc_last_extra = extra;\n        }\n        return value + extra;\n    }\n\n    #[cfg(debug_assertions)]\n    if tsc_last_extra != 0 {\n        if TSC_VERBOSE_LOGGING || tsc_last_extra >= tsc_resolution {\n            dbg_log!(\n                \"rdtsc: jump from {}+{} to {} (diff {}, {}%)\",\n                tsc_last_value as u64,\n                tsc_last_extra as u64,\n                value,\n                value - (tsc_last_value + tsc_last_extra),\n                (100 * tsc_last_extra) / tsc_resolution,\n            );\n            dbg_assert!(tsc_last_extra < tsc_resolution, \"XXX: Overshot tsc\");\n        }\n        tsc_last_extra = 0;\n    }\n\n    let d = value - tsc_last_value;\n    if d < tsc_resolution {\n        dbg_log!(\"rdtsc resolution: {}\", d);\n    }\n    tsc_resolution = tsc_resolution.min(d);\n    tsc_last_value = value;\n    if tsc_number_of_same_readings != 0 {\n        tsc_speed = tsc_number_of_same_readings;\n        tsc_number_of_same_readings = 0;\n    }\n\n    value\n}\n\npub unsafe fn vm86_mode() -> bool { return *flags & FLAG_VM == FLAG_VM; }\n\n#[no_mangle]\npub unsafe fn getiopl() -> i32 { return *flags >> 12 & 3; }\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub unsafe fn get_opstats_buffer(\n    compiled: bool,\n    jit_exit: bool,\n    unguarded_register: bool,\n    wasm_size: bool,\n    opcode: u8,\n    is_0f: bool,\n    is_mem: bool,\n    fixed_g: u8,\n) -> f64 {\n    {\n        let index = (is_0f as usize) << 12\n            | (opcode as usize) << 4\n            | (is_mem as usize) << 3\n            | fixed_g as usize;\n        (if compiled {\n            opstats::opstats_compiled_buffer[index]\n        }\n        else if jit_exit {\n            opstats::opstats_jit_exit_buffer[index]\n        }\n        else if unguarded_register {\n            opstats::opstats_unguarded_register_buffer[index]\n        }\n        else if wasm_size {\n            opstats::opstats_wasm_size[index]\n        }\n        else {\n            opstats::opstats_buffer[index]\n        }) as f64\n    }\n}\n\n#[no_mangle]\n#[cfg(not(feature = \"profiler\"))]\npub unsafe fn get_opstats_buffer() -> f64 { 0.0 }\n\npub fn clear_tlb_code(page: i32) {\n    unsafe {\n        if let Some(c) = tlb_code[page as usize] {\n            drop(Box::from_raw(c.as_ptr()));\n        }\n        tlb_code[page as usize] = None;\n    }\n}\n\npub unsafe fn invlpg(addr: i32) {\n    let page = (addr as u32 >> 12) as i32;\n    // Note: Doesn't remove this page from valid_tlb_entries: This isn't\n    // necessary, because when valid_tlb_entries grows too large, it will be\n    // empties by calling clear_tlb, which removes this entry as it isn't global.\n    // This however means that valid_tlb_entries can contain some invalid entries\n    clear_tlb_code(page);\n    tlb_data[page as usize] = 0;\n    *last_virt_eip = -1;\n}\n\n#[no_mangle]\npub unsafe fn update_eflags(new_flags: i32) {\n    let mut dont_update = FLAG_RF | FLAG_VM | FLAG_VIP | FLAG_VIF;\n    let mut clear = !FLAG_VIP & !FLAG_VIF & FLAGS_MASK;\n    if 0 != *flags & FLAG_VM {\n        // other case needs to be handled in popf or iret\n        dbg_assert!(getiopl() == 3);\n        dont_update |= FLAG_IOPL;\n        // don't clear vip or vif\n        clear |= FLAG_VIP | FLAG_VIF\n    }\n    else {\n        if !*protected_mode {\n            dbg_assert!(*cpl == 0);\n        }\n        if 0 != *cpl {\n            // cpl > 0\n            // cannot update iopl\n            dont_update |= FLAG_IOPL;\n            if *cpl as i32 > getiopl() {\n                // cpl > iopl\n                // cannot update interrupt flag\n                dont_update |= FLAG_INTERRUPT\n            }\n        }\n    }\n    *flags = (new_flags ^ (*flags ^ new_flags) & dont_update) & clear | FLAGS_DEFAULT;\n    *flags_changed = 0;\n\n    if *flags & FLAG_TRAP != 0 {\n        dbg_log!(\"Not supported: trap flag\");\n    }\n    *flags &= !FLAG_TRAP;\n}\n\n#[no_mangle]\npub unsafe fn get_valid_tlb_entries_count() -> i32 {\n    if !cfg!(feature = \"profiler\") {\n        return 0;\n    }\n    let mut result = 0;\n    for i in 0..valid_tlb_entries_count {\n        let page = valid_tlb_entries[i as usize];\n        let entry = tlb_data[page as usize];\n        if 0 != entry {\n            result += 1\n        }\n    }\n    return result;\n}\n\n#[no_mangle]\npub unsafe fn get_valid_global_tlb_entries_count() -> i32 {\n    if !cfg!(feature = \"profiler\") {\n        return 0;\n    }\n    let mut result = 0;\n    for i in 0..valid_tlb_entries_count {\n        let page = valid_tlb_entries[i as usize];\n        let entry = tlb_data[page as usize];\n        if 0 != entry & TLB_GLOBAL {\n            result += 1\n        }\n    }\n    return result;\n}\n\n#[inline(never)]\npub unsafe fn trigger_np(code: i32) {\n    dbg_log!(\"#np\");\n    *instruction_pointer = *previous_ip;\n    if DEBUG {\n        if js::cpu_exception_hook(CPU_EXCEPTION_NP) {\n            return;\n        }\n    }\n    call_interrupt_vector(CPU_EXCEPTION_NP, false, Some(code));\n}\n\n#[inline(never)]\npub unsafe fn trigger_ss(code: i32) {\n    dbg_log!(\"#ss\");\n    *instruction_pointer = *previous_ip;\n    if DEBUG {\n        if js::cpu_exception_hook(CPU_EXCEPTION_SS) {\n            return;\n        }\n    }\n    call_interrupt_vector(CPU_EXCEPTION_SS, false, Some(code));\n}\n\n#[no_mangle]\npub unsafe fn store_current_tsc() { *current_tsc = read_tsc(); }\n\n#[no_mangle]\npub unsafe fn handle_irqs() {\n    if *flags & FLAG_INTERRUPT != 0 {\n        if let Some(irq) = pic::pic_acknowledge_irq() {\n            pic_call_irq(irq)\n        }\n        else if *acpi_enabled {\n            if let Some(irq) = apic::acknowledge_irq() {\n                pic_call_irq(irq)\n            }\n        }\n    }\n}\n\nunsafe fn pic_call_irq(interrupt_nr: u8) {\n    *previous_ip = *instruction_pointer; // XXX: What if called after instruction (port IO)\n    if *in_hlt {\n        js::stop_idling();\n        *in_hlt = false;\n    }\n    call_interrupt_vector(interrupt_nr as i32, false, None);\n}\n\n#[no_mangle]\nunsafe fn device_raise_irq(i: u8) {\n    pic::set_irq(i);\n    if *acpi_enabled {\n        ioapic::set_irq(i);\n    }\n    handle_irqs()\n}\n\n#[no_mangle]\nunsafe fn device_lower_irq(i: u8) {\n    pic::clear_irq(i);\n    if *acpi_enabled {\n        ioapic::clear_irq(i);\n    }\n    handle_irqs()\n}\n\npub fn io_port_read8(port: i32) -> i32 {\n    unsafe {\n        match port {\n            0x20 => pic::port20_read() as i32,\n            0x21 => pic::port21_read() as i32,\n            0xA0 => pic::portA0_read() as i32,\n            0xA1 => pic::portA1_read() as i32,\n            0x4D0 => pic::port4D0_read() as i32,\n            0x4D1 => pic::port4D1_read() as i32,\n            _ => js::io_port_read8(port),\n        }\n    }\n}\npub fn io_port_read16(port: i32) -> i32 { unsafe { js::io_port_read16(port) } }\npub fn io_port_read32(port: i32) -> i32 { unsafe { js::io_port_read32(port) } }\n\npub fn io_port_write8(port: i32, value: i32) {\n    unsafe {\n        match port {\n            0x20 | 0x21 | 0xA0 | 0xA1 | 0x4D0 | 0x4D1 => {\n                match port {\n                    0x20 => pic::port20_write(value as u8),\n                    0x21 => pic::port21_write(value as u8),\n                    0xA0 => pic::portA0_write(value as u8),\n                    0xA1 => pic::portA1_write(value as u8),\n                    0x4D0 => pic::port4D0_write(value as u8),\n                    0x4D1 => pic::port4D1_write(value as u8),\n                    _ => dbg_assert!(false),\n                };\n                handle_irqs()\n            },\n            _ => js::io_port_write8(port, value),\n        }\n    }\n}\npub fn io_port_write16(port: i32, value: i32) { unsafe { js::io_port_write16(port, value) } }\npub fn io_port_write32(port: i32, value: i32) { unsafe { js::io_port_write32(port, value) } }\n\n#[no_mangle]\n#[cfg(debug_assertions)]\npub unsafe fn check_page_switch(block_addr: u32, next_block_addr: u32) {\n    let x = translate_address_read_jit(*instruction_pointer);\n    if x != Ok(next_block_addr) {\n        dbg_log!(\n            \"page switch from={:x} to={:x} prev_eip={:x} eip={:x} phys_eip={:x}\",\n            block_addr,\n            next_block_addr,\n            *previous_ip,\n            *instruction_pointer,\n            x.unwrap_or(0),\n        );\n    }\n    dbg_assert!(next_block_addr & 0xFFF == *instruction_pointer as u32 & 0xFFF);\n    dbg_assert!(x.is_ok());\n    dbg_assert!(x == Ok(next_block_addr));\n}\n\n#[no_mangle]\npub unsafe fn reset_cpu() {\n    for i in 0..8 {\n        *segment_is_null.offset(i) = false;\n        *segment_limits.offset(i) = 0;\n        *segment_offsets.offset(i) = 0;\n        *segment_access_bytes.offset(i) = 0x80 | (0 << 5) | 0x10 | 0x02; // P dpl0 S RW\n\n        *reg32.offset(i) = 0;\n\n        *sreg.offset(i) = 0;\n        *dreg.offset(i) = 0;\n\n        write_xmm128_2(i as i32, 0, 0);\n\n        *fpu_st.offset(i) = softfloat::F80::ZERO;\n    }\n    *segment_access_bytes.offset(CS as isize) = 0x80 | (0 << 5) | 0x10 | 0x08 | 0x02; // P dpl0 S E RW\n\n    for i in 0..4 {\n        *reg_pdpte.offset(i) = 0\n    }\n\n    *fpu_stack_empty = 0xFF;\n    *fpu_stack_ptr = 0;\n    *fpu_control_word = 0x37F;\n    *fpu_status_word = 0;\n    *fpu_ip = 0;\n    *fpu_ip_selector = 0;\n    *fpu_opcode = 0;\n    *fpu_dp = 0;\n    *fpu_dp_selector = 0;\n\n    *mxcsr = 0x1F80;\n\n    full_clear_tlb();\n\n    *protected_mode = false;\n\n    // http://www.sandpile.org/x86/initial.htm\n    *idtr_size = 0;\n    *idtr_offset = 0;\n\n    *gdtr_size = 0;\n    *gdtr_offset = 0;\n\n    *page_fault = false;\n    *cr = 1 << 30 | 1 << 29 | 1 << 4;\n    *cr.offset(2) = 0;\n    *cr.offset(3) = 0;\n    *cr.offset(4) = 0;\n    *dreg.offset(6) = 0xFFFF0FF0u32 as i32;\n    *dreg.offset(7) = 0x400;\n    *cpl = 0;\n\n    *is_32 = false;\n    *stack_size_32 = false;\n    *prefixes = 0;\n\n    *last_virt_eip = -1;\n\n    *instruction_counter = 0;\n    *previous_ip = 0;\n    *in_hlt = false;\n\n    *sysenter_cs = 0;\n    *sysenter_esp = 0;\n    *sysenter_eip = 0;\n\n    *flags = FLAGS_DEFAULT;\n    *flags_changed = 0;\n    *last_result = 0;\n    *last_op1 = 0;\n    *last_op_size = 0;\n\n    set_tsc(0, 0);\n\n    *instruction_pointer = 0xFFFF0;\n    switch_cs_real_mode(0xF000);\n\n    switch_seg(SS, 0x30);\n    write_reg32(ESP, 0x100);\n\n    update_state_flags();\n\n    jit::jit_clear_cache_js();\n}\n\n#[no_mangle]\npub unsafe fn set_cpuid_level(level: u32) { cpuid_level = level }\n"
  },
  {
    "path": "src/rust/cpu/fpu.rs",
    "content": "use crate::cpu::cpu::*;\nuse crate::cpu::global_pointers::*;\nuse crate::paging::OrPageFault;\nuse crate::softfloat::{Precision, RoundingMode, F80};\n\nuse std::f64;\n\nconst FPU_C0: u16 = 0x100;\nconst FPU_C1: u16 = 0x200;\nconst FPU_C2: u16 = 0x400;\nconst FPU_C3: u16 = 0x4000;\nconst FPU_RESULT_FLAGS: u16 = FPU_C0 | FPU_C1 | FPU_C2 | FPU_C3;\n\nconst FPU_EX_I: u16 = 1 << 0; // invalid operation\n#[allow(dead_code)]\nconst FPU_EX_D: u16 = 1 << 1; // denormal operand\nconst FPU_EX_Z: u16 = 1 << 2; // zero divide\n#[allow(dead_code)]\nconst FPU_EX_O: u16 = 1 << 3; // overflow\nconst FPU_EX_U: u16 = 1 << 4; // underflow\n#[allow(dead_code)]\nconst FPU_EX_P: u16 = 1 << 5; // precision\nconst FPU_EX_SF: u16 = 1 << 6;\n\npub fn fpu_write_st(index: i32, value: F80) {\n    dbg_assert!(index >= 0 && index < 8);\n    unsafe {\n        *fpu_st.offset(index as isize) = value;\n    }\n}\n\npub unsafe fn fpu_get_st0() -> F80 {\n    dbg_assert!(*fpu_stack_ptr < 8);\n    if 0 != *fpu_stack_empty >> *fpu_stack_ptr & 1 {\n        *fpu_status_word &= !FPU_C1;\n        fpu_stack_fault();\n        return F80::INDEFINITE_NAN;\n    }\n    else {\n        return *fpu_st.offset(*fpu_stack_ptr as isize);\n    };\n}\npub unsafe fn fpu_stack_fault() {\n    // TODO: Interrupt\n    *fpu_status_word |= FPU_EX_SF | FPU_EX_I;\n}\n\npub unsafe fn fpu_zero_fault() {\n    // TODO: Interrupt\n    *fpu_status_word |= FPU_EX_Z;\n}\n\npub unsafe fn fpu_underflow_fault() {\n    // TODO: Interrupt\n    *fpu_status_word |= FPU_EX_U;\n}\n\npub unsafe fn fpu_sti_empty(mut i: i32) -> bool {\n    dbg_assert!(i >= 0 && i < 8);\n    i = i + *fpu_stack_ptr as i32 & 7;\n    return 0 != *fpu_stack_empty >> i & 1;\n}\n\n#[no_mangle]\npub unsafe fn fpu_get_sti_jit(dst: *mut F80, i: i32) { *dst = fpu_get_sti(i); }\n\npub unsafe fn fpu_get_sti(mut i: i32) -> F80 {\n    dbg_assert!(i >= 0 && i < 8);\n    i = i + *fpu_stack_ptr as i32 & 7;\n    if 0 != *fpu_stack_empty >> i & 1 {\n        *fpu_status_word &= !FPU_C1;\n        fpu_stack_fault();\n        return F80::INDEFINITE_NAN;\n    }\n    else {\n        return *fpu_st.offset(i as isize);\n    };\n}\n\n// only used for debugging\n#[no_mangle]\npub unsafe fn fpu_get_sti_f64(mut i: i32) -> f64 {\n    i = i + *fpu_stack_ptr as i32 & 7;\n    f64::from_bits((*fpu_st.offset(i as isize)).to_f64())\n}\n\n#[no_mangle]\npub unsafe fn f32_to_f80_jit(dst: *mut F80, v: i32) { *dst = f32_to_f80(v) }\npub unsafe fn f32_to_f80(v: i32) -> F80 {\n    F80::clear_exception_flags();\n    let x = F80::of_f32(v);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\n#[no_mangle]\npub unsafe fn f64_to_f80_jit(dst: *mut F80, v: u64) { *dst = f64_to_f80(v) }\npub unsafe fn f64_to_f80(v: u64) -> F80 {\n    F80::clear_exception_flags();\n    let x = F80::of_f64(v);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\n#[no_mangle]\npub unsafe fn f80_to_f32(v: F80) -> i32 {\n    F80::clear_exception_flags();\n    let x = v.to_f32();\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\n#[no_mangle]\npub unsafe fn f80_to_f64(v: F80) -> u64 {\n    F80::clear_exception_flags();\n    let x = v.to_f64();\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\n\n#[no_mangle]\npub unsafe fn i32_to_f80_jit(dst: *mut F80, v: i32) { *dst = i32_to_f80(v) }\npub unsafe fn i32_to_f80(v: i32) -> F80 { F80::of_i32(v) }\n#[no_mangle]\npub unsafe fn i64_to_f80_jit(dst: *mut F80, v: i64) { *dst = i64_to_f80(v) }\npub unsafe fn i64_to_f80(v: i64) -> F80 { F80::of_i64(v) }\n\npub unsafe fn fpu_load_i16(addr: i32) -> OrPageFault<F80> {\n    let v = safe_read16(addr)? as i16 as i32;\n    Ok(F80::of_i32(v))\n}\npub unsafe fn fpu_load_i32(addr: i32) -> OrPageFault<F80> {\n    let v = safe_read32s(addr)?;\n    Ok(F80::of_i32(v))\n}\npub unsafe fn fpu_load_i64(addr: i32) -> OrPageFault<F80> {\n    let v = safe_read64s(addr)? as i64;\n    Ok(F80::of_i64(v))\n}\n\npub unsafe fn fpu_load_m32(addr: i32) -> OrPageFault<F80> {\n    F80::clear_exception_flags();\n    let v = F80::of_f32(safe_read32s(addr)?);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    Ok(v)\n}\npub unsafe fn fpu_load_m64(addr: i32) -> OrPageFault<F80> {\n    F80::clear_exception_flags();\n    let v = F80::of_f64(safe_read64s(addr)?);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    Ok(v)\n}\npub unsafe fn fpu_load_m80(addr: i32) -> OrPageFault<F80> {\n    let mantissa = safe_read64s(addr)?;\n    let sign_exponent = safe_read16(addr + 8)? as u16;\n    // TODO: Canonical form\n    Ok(F80 {\n        mantissa,\n        sign_exponent,\n    })\n}\n\n#[no_mangle]\npub unsafe fn fpu_load_status_word() -> u16 {\n    dbg_assert!(*fpu_stack_ptr < 8);\n    return *fpu_status_word & !(7 << 11) | (*fpu_stack_ptr as u16) << 11;\n}\n#[no_mangle]\npub unsafe fn fpu_fadd(target_index: i32, val: F80) {\n    F80::clear_exception_flags();\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32 + target_index & 7, st0 + val);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\npub unsafe fn fpu_fclex() { *fpu_status_word = 0; }\npub unsafe fn fpu_fcmovcc(condition: bool, r: i32) {\n    // outside of the condition is correct: A stack fault happens even if the condition is not\n    // fulfilled\n    let x = fpu_get_sti(r);\n    if fpu_sti_empty(r) {\n        fpu_write_st(*fpu_stack_ptr as i32, F80::INDEFINITE_NAN)\n    }\n    else {\n        if condition {\n            fpu_write_st(*fpu_stack_ptr as i32, x);\n            *fpu_stack_empty &= !(1 << *fpu_stack_ptr)\n        };\n    }\n}\n\n#[no_mangle]\npub unsafe fn fpu_fcom(y: F80) {\n    F80::clear_exception_flags();\n    let x = fpu_get_st0();\n    *fpu_status_word &= !FPU_RESULT_FLAGS;\n    match x.partial_cmp(&y) {\n        Some(std::cmp::Ordering::Greater) => {},\n        Some(std::cmp::Ordering::Less) => *fpu_status_word |= FPU_C0,\n        Some(std::cmp::Ordering::Equal) => *fpu_status_word |= FPU_C3,\n        None => *fpu_status_word |= FPU_C0 | FPU_C2 | FPU_C3,\n    }\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\n\n#[no_mangle]\npub unsafe fn fpu_fcomi(r: i32) {\n    F80::clear_exception_flags();\n    let x = fpu_get_st0();\n    let y = fpu_get_sti(r);\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    match x.partial_cmp(&y) {\n        Some(std::cmp::Ordering::Greater) => {},\n        Some(std::cmp::Ordering::Less) => *flags |= 1,\n        Some(std::cmp::Ordering::Equal) => *flags |= FLAG_ZERO,\n        None => *flags |= 1 | FLAG_PARITY | FLAG_ZERO,\n    }\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\n\n#[no_mangle]\npub unsafe fn fpu_fcomip(r: i32) {\n    fpu_fcomi(r);\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_pop() {\n    dbg_assert!(*fpu_stack_ptr < 8);\n    *fpu_stack_empty |= 1 << *fpu_stack_ptr;\n    *fpu_stack_ptr = *fpu_stack_ptr + 1 & 7;\n}\n\n#[no_mangle]\npub unsafe fn fpu_fcomp(val: F80) {\n    fpu_fcom(val);\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fdiv(target_index: i32, val: F80) {\n    F80::clear_exception_flags();\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32 + target_index & 7, st0 / val);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\n#[no_mangle]\npub unsafe fn fpu_fdivr(target_index: i32, val: F80) {\n    F80::clear_exception_flags();\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32 + target_index & 7, val / st0);\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\n#[no_mangle]\npub unsafe fn fpu_ffree(r: i32) { *fpu_stack_empty |= 1 << (*fpu_stack_ptr as i32 + r & 7); }\n\npub unsafe fn fpu_fildm16(addr: i32) { fpu_push(return_on_pagefault!(fpu_load_i16(addr))); }\npub unsafe fn fpu_fildm32(addr: i32) { fpu_push(return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn fpu_fildm64(addr: i32) { fpu_push(return_on_pagefault!(fpu_load_i64(addr))); }\n\n#[no_mangle]\npub unsafe fn fpu_push(x: F80) {\n    *fpu_stack_ptr = *fpu_stack_ptr - 1 & 7;\n    if 0 != *fpu_stack_empty >> *fpu_stack_ptr & 1 {\n        *fpu_status_word &= !FPU_C1;\n        *fpu_stack_empty &= !(1 << *fpu_stack_ptr);\n        fpu_write_st(*fpu_stack_ptr as i32, x);\n    }\n    else {\n        *fpu_status_word |= FPU_C1;\n        fpu_stack_fault();\n        fpu_write_st(*fpu_stack_ptr as i32, F80::INDEFINITE_NAN);\n    };\n}\npub unsafe fn fpu_finit() {\n    set_control_word(0x37F);\n    *fpu_status_word = 0;\n    *fpu_ip = 0;\n    *fpu_dp = 0;\n    *fpu_opcode = 0;\n    *fpu_stack_empty = 0xFF;\n    *fpu_stack_ptr = 0;\n}\n\n#[no_mangle]\npub unsafe fn set_control_word(cw: u16) {\n    *fpu_control_word = cw;\n\n    let rc = cw >> 10 & 3;\n    F80::set_rounding_mode(match rc {\n        0 => RoundingMode::NearEven,\n        1 => RoundingMode::Floor,\n        2 => RoundingMode::Ceil,\n        3 => RoundingMode::Trunc,\n        _ => {\n            dbg_assert!(false);\n            RoundingMode::NearEven\n        },\n    });\n\n    let precision_control = cw >> 8 & 3;\n    F80::set_precision(match precision_control {\n        0 => Precision::P32,\n        1 => Precision::P80, // undefined\n        2 => Precision::P64,\n        3 => Precision::P80,\n        _ => {\n            dbg_assert!(false);\n            Precision::P80\n        },\n    });\n}\n\npub unsafe fn fpu_invalid_arithmetic() { *fpu_status_word |= FPU_EX_I; }\n\n#[no_mangle]\npub unsafe fn fpu_convert_to_i16(f: F80) -> i16 {\n    let st0 = fpu_convert_to_i32(f);\n    if st0 < -0x8000 || st0 > 0x7FFF {\n        fpu_invalid_arithmetic();\n        -0x8000\n    }\n    else {\n        st0 as i16\n    }\n}\npub unsafe fn fpu_fistm16(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 2));\n    let v = fpu_convert_to_i16(fpu_get_st0());\n    safe_write16(addr, v as i32 & 0xFFFF).unwrap();\n}\npub unsafe fn fpu_fistm16p(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 2));\n    let v = fpu_convert_to_i16(fpu_get_st0());\n    safe_write16(addr, v as i32 & 0xFFFF).unwrap();\n    fpu_pop();\n}\n#[no_mangle]\npub unsafe fn fpu_truncate_to_i16(f: F80) -> i16 {\n    let st0 = fpu_truncate_to_i32(f);\n    if st0 < -0x8000 || st0 > 0x7FFF {\n        fpu_invalid_arithmetic();\n        -0x8000\n    }\n    else {\n        st0 as i16\n    }\n}\npub unsafe fn fpu_fisttpm16(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 2));\n    let v = fpu_truncate_to_i16(fpu_get_st0());\n    safe_write16(addr, v as i32 & 0xFFFF).unwrap();\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_convert_to_i32(f: F80) -> i32 {\n    F80::clear_exception_flags();\n    let x = f.to_i32();\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\npub unsafe fn fpu_fistm32(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 4));\n    let v = fpu_convert_to_i32(fpu_get_st0());\n    safe_write32(addr, v).unwrap();\n}\npub unsafe fn fpu_fistm32p(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 4));\n    let v = fpu_convert_to_i32(fpu_get_st0());\n    safe_write32(addr, v).unwrap();\n    fpu_pop();\n}\n#[no_mangle]\npub unsafe fn fpu_truncate_to_i32(f: F80) -> i32 {\n    F80::clear_exception_flags();\n    let x = f.truncate_to_i32();\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\npub unsafe fn fpu_fisttpm32(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 4));\n    let v = fpu_truncate_to_i32(fpu_get_st0());\n    safe_write32(addr, v).unwrap();\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_convert_to_i64(f: F80) -> i64 {\n    F80::clear_exception_flags();\n    let x = f.to_i64();\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\npub unsafe fn fpu_fistm64p(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 8));\n    let v = fpu_convert_to_i64(fpu_get_st0());\n    safe_write64(addr, v as u64).unwrap();\n    fpu_pop();\n}\n#[no_mangle]\npub unsafe fn fpu_truncate_to_i64(f: F80) -> i64 {\n    F80::clear_exception_flags();\n    let x = f.truncate_to_i64();\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    x\n}\npub unsafe fn fpu_fisttpm64(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 8));\n    let v = fpu_truncate_to_i64(fpu_get_st0());\n    safe_write64(addr, v as u64).unwrap();\n    fpu_pop();\n}\n\npub unsafe fn fpu_fldcw(addr: i32) {\n    let word = return_on_pagefault!(safe_read16(addr)) as u16;\n    set_control_word(word);\n}\n\n#[no_mangle]\npub unsafe fn fpu_fldenv16(_addr: i32) {\n    dbg_log!(\"fldenv16\");\n    fpu_unimpl();\n}\n#[no_mangle]\npub unsafe fn fpu_fldenv32(addr: i32) {\n    if let Err(()) = readable_or_pagefault(addr, 28) {\n        *page_fault = true;\n        return;\n    }\n    *page_fault = false;\n    set_control_word(safe_read16(addr).unwrap() as u16);\n    fpu_set_status_word(safe_read16(addr + 4).unwrap() as u16);\n    fpu_set_tag_word(safe_read16(addr + 8).unwrap());\n    *fpu_ip = safe_read32s(addr + 12).unwrap();\n    *fpu_ip_selector = safe_read16(addr + 16).unwrap();\n    *fpu_opcode = safe_read16(addr + 18).unwrap();\n    *fpu_dp = safe_read32s(addr + 20).unwrap();\n    *fpu_dp_selector = safe_read16(addr + 24).unwrap()\n}\npub unsafe fn fpu_unimpl() {\n    dbg_assert!(false);\n    trigger_ud();\n}\npub unsafe fn fpu_set_tag_word(tag_word: i32) {\n    *fpu_stack_empty = 0;\n    for i in 0..8 {\n        let empty = tag_word >> (2 * i) & 3 == 3;\n        *fpu_stack_empty |= (empty as u8) << i;\n    }\n}\npub unsafe fn fpu_set_status_word(sw: u16) {\n    *fpu_status_word = sw & !(7 << 11);\n    *fpu_stack_ptr = (sw >> 11 & 7) as u8;\n}\n\npub unsafe fn fpu_fldm32(addr: i32) { fpu_push(return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn fpu_fldm64(addr: i32) { fpu_push(return_on_pagefault!(fpu_load_m64(addr))); }\n#[no_mangle]\npub unsafe fn fpu_fldm80(addr: i32) {\n    match fpu_load_m80(addr) {\n        Ok(x) => {\n            *page_fault = false;\n            fpu_push(x)\n        },\n        Err(()) => {\n            *page_fault = true;\n        },\n    }\n}\n\n#[no_mangle]\npub unsafe fn fpu_fmul(target_index: i32, val: F80) {\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32 + target_index & 7, st0 * val);\n}\npub unsafe fn fpu_fnstsw_mem(addr: i32) {\n    return_on_pagefault!(safe_write16(addr, fpu_load_status_word().into()));\n}\npub unsafe fn fpu_fnstsw_reg() { write_reg16(AX, fpu_load_status_word().into()); }\npub unsafe fn fpu_fprem(ieee: bool) {\n    // false: Faster, fails nasmtests\n    // true: Slower, fails qemutests\n    let intel_compatibility = false;\n\n    let st0 = fpu_get_st0();\n    let st1 = fpu_get_sti(1);\n\n    if st1 == F80::ZERO {\n        if st0 == F80::ZERO {\n            fpu_invalid_arithmetic();\n        }\n        else {\n            fpu_zero_fault();\n        }\n        fpu_write_st(*fpu_stack_ptr as i32, F80::INDEFINITE_NAN);\n        return;\n    }\n\n    let exp0 = st0.log2();\n    let exp1 = st1.log2();\n    let d = (exp0 - exp1).abs();\n    if !intel_compatibility || d < F80::of_f64(f64::to_bits(64.0)) {\n        let fprem_quotient =\n            (if ieee { (st0 / st1).round() } else { (st0 / st1).trunc() }).to_i32();\n        fpu_write_st(*fpu_stack_ptr as i32, st0 % st1);\n        *fpu_status_word &= !(FPU_C0 | FPU_C1 | FPU_C3);\n        if 0 != fprem_quotient & 1 {\n            *fpu_status_word |= FPU_C1\n        }\n        if 0 != fprem_quotient & 1 << 1 {\n            *fpu_status_word |= FPU_C3\n        }\n        if 0 != fprem_quotient & 1 << 2 {\n            *fpu_status_word |= FPU_C0\n        }\n        *fpu_status_word &= !FPU_C2;\n    }\n    else {\n        let n = F80::of_f64(f64::to_bits(32.0));\n        let fprem_quotient =\n            (if ieee { (st0 / st1).round() } else { (st0 / st1).trunc() } / (d - n).two_pow());\n        fpu_write_st(\n            *fpu_stack_ptr as i32,\n            st0 - st1 * fprem_quotient * (d - n).two_pow(),\n        );\n        *fpu_status_word |= FPU_C2;\n    }\n}\n\npub unsafe fn fpu_frstor16(_addr: i32) {\n    dbg_log!(\"frstor16\");\n    fpu_unimpl();\n}\npub unsafe fn fpu_frstor32(mut addr: i32) {\n    return_on_pagefault!(readable_or_pagefault(addr, 28 + 8 * 10));\n    fpu_fldenv32(addr);\n    addr += 28;\n    for i in 0..8 {\n        let reg_index = *fpu_stack_ptr as i32 + i & 7;\n        *fpu_st.offset(reg_index as isize) = fpu_load_m80(addr).unwrap();\n        addr += 10;\n    }\n}\n\npub unsafe fn fpu_fsave16(_addr: i32) {\n    dbg_log!(\"fsave16\");\n    fpu_unimpl();\n}\npub unsafe fn fpu_fsave32(mut addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 108));\n    fpu_fstenv32(addr);\n    addr += 28;\n    for i in 0..8 {\n        let reg_index = i + *fpu_stack_ptr as i32 & 7;\n        fpu_store_m80(addr, *fpu_st.offset(reg_index as isize));\n        addr += 10;\n    }\n    fpu_finit();\n}\n\npub unsafe fn fpu_store_m80(addr: i32, f: F80) {\n    // writable_or_pagefault must have checked called by the caller!\n    safe_write64(addr, f.mantissa).unwrap();\n    safe_write16(addr + 8, f.sign_exponent as i32).unwrap();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fstenv16(_addr: i32) {\n    dbg_log!(\"fstenv16\");\n    fpu_unimpl();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fstenv32(addr: i32) {\n    match writable_or_pagefault(addr, 26) {\n        Ok(()) => *page_fault = false,\n        Err(()) => {\n            *page_fault = true;\n            return;\n        },\n    }\n    let high_bits = 0xFFFF0000u32 as i32;\n    safe_write32(addr + 0, high_bits + *fpu_control_word as i32).unwrap();\n    safe_write32(addr + 4, high_bits + fpu_load_status_word() as i32).unwrap();\n    safe_write32(addr + 8, high_bits + fpu_load_tag_word()).unwrap();\n    safe_write32(addr + 12, *fpu_ip).unwrap();\n    safe_write16(addr + 16, *fpu_ip_selector).unwrap();\n    safe_write16(addr + 18, *fpu_opcode).unwrap();\n    safe_write32(addr + 20, *fpu_dp).unwrap();\n    safe_write32(addr + 24, high_bits | *fpu_dp_selector).unwrap();\n}\n#[no_mangle]\npub unsafe fn fpu_load_tag_word() -> i32 {\n    let mut tag_word = 0;\n    for i in 0..8 {\n        let value = *fpu_st.offset(i as isize);\n        if 0 != *fpu_stack_empty >> i & 1 {\n            tag_word |= 3 << (i << 1)\n        }\n        else if value == F80::ZERO {\n            tag_word |= 1 << (i << 1)\n        }\n        else if !value.is_finite() {\n            tag_word |= 2 << (i << 1)\n        }\n    }\n    return tag_word;\n}\n#[no_mangle]\npub unsafe fn fpu_fst(r: i32) { fpu_write_st(*fpu_stack_ptr as i32 + r & 7, fpu_get_st0()); }\npub unsafe fn fpu_fst80p(addr: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 10));\n    fpu_store_m80(addr, fpu_get_st0());\n    fpu_pop();\n}\n\npub unsafe fn fpu_fstcw(addr: i32) {\n    return_on_pagefault!(safe_write16(addr, (*fpu_control_word).into()));\n}\n\npub unsafe fn fpu_fstm32(addr: i32) {\n    return_on_pagefault!(fpu_store_m32(addr, fpu_get_st0()));\n}\npub unsafe fn fpu_store_m32(addr: i32, x: F80) -> OrPageFault<()> {\n    F80::clear_exception_flags();\n    safe_write32(addr, x.to_f32())?;\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n    Ok(())\n}\npub unsafe fn fpu_fstm32p(addr: i32) {\n    return_on_pagefault!(fpu_store_m32(addr, fpu_get_st0()));\n    fpu_pop();\n}\npub unsafe fn fpu_fstm64(addr: i32) {\n    return_on_pagefault!(fpu_store_m64(addr, fpu_get_st0()));\n}\npub unsafe fn fpu_store_m64(addr: i32, x: F80) -> OrPageFault<()> { safe_write64(addr, x.to_f64()) }\npub unsafe fn fpu_fstm64p(addr: i32) {\n    // XXX: writable_or_pagefault before get_st0\n    return_on_pagefault!(fpu_store_m64(addr, fpu_get_st0()));\n    fpu_pop();\n}\n#[no_mangle]\npub unsafe fn fpu_fstp(r: i32) {\n    fpu_fst(r);\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fbstp(addr: i32) {\n    match writable_or_pagefault(addr, 26) {\n        Ok(()) => *page_fault = false,\n        Err(()) => {\n            *page_fault = true;\n            return;\n        },\n    }\n    let st0 = fpu_get_st0();\n    let mut x = st0.to_i64().unsigned_abs();\n    if x <= 99_9999_9999_9999_9999 {\n        for i in 0..=8 {\n            let low = x % 10;\n            x /= 10;\n            let high = x % 10;\n            x /= 10;\n            safe_write8(addr + i, (high as i32) << 4 | low as i32).unwrap();\n        }\n        safe_write8(addr + 9, if st0.sign() { 0x80 } else { 0 }).unwrap();\n    }\n    else {\n        fpu_invalid_arithmetic();\n        safe_write64(addr + 0, 0xC000_0000_0000_0000).unwrap();\n        safe_write16(addr + 8, 0xFFFF).unwrap();\n    }\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fsub(target_index: i32, val: F80) {\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32 + target_index & 7, st0 - val)\n}\n#[no_mangle]\npub unsafe fn fpu_fsubr(target_index: i32, val: F80) {\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32 + target_index & 7, val - st0)\n}\n\npub unsafe fn fpu_ftst() {\n    let x = fpu_get_st0();\n    *fpu_status_word &= !FPU_RESULT_FLAGS;\n    if x.is_nan() {\n        *fpu_status_word |= FPU_C3 | FPU_C2 | FPU_C0\n    }\n    else if x == F80::ZERO {\n        *fpu_status_word |= FPU_C3\n    }\n    else if x < F80::ZERO {\n        *fpu_status_word |= FPU_C0\n    }\n    // TODO: unordered (x is nan, etc)\n}\n\n#[no_mangle]\npub unsafe fn fpu_fucom(r: i32) {\n    F80::clear_exception_flags();\n    let x = fpu_get_st0();\n    let y = fpu_get_sti(r);\n    *fpu_status_word &= !FPU_RESULT_FLAGS;\n    match x.partial_cmp_quiet(&y) {\n        Some(std::cmp::Ordering::Greater) => {},\n        Some(std::cmp::Ordering::Less) => *fpu_status_word |= FPU_C0,\n        Some(std::cmp::Ordering::Equal) => *fpu_status_word |= FPU_C3,\n        None => *fpu_status_word |= FPU_C0 | FPU_C2 | FPU_C3,\n    }\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\n\n#[no_mangle]\npub unsafe fn fpu_fucomi(r: i32) {\n    F80::clear_exception_flags();\n    let x = fpu_get_st0();\n    let y = fpu_get_sti(r);\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    match x.partial_cmp_quiet(&y) {\n        Some(std::cmp::Ordering::Greater) => {},\n        Some(std::cmp::Ordering::Less) => *flags |= 1,\n        Some(std::cmp::Ordering::Equal) => *flags |= FLAG_ZERO,\n        None => *flags |= 1 | FLAG_PARITY | FLAG_ZERO,\n    }\n    *fpu_status_word |= F80::get_exception_flags() as u16;\n}\n\n#[no_mangle]\npub unsafe fn fpu_fucomip(r: i32) {\n    fpu_fucomi(r);\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fucomp(r: i32) {\n    fpu_fucom(r);\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn fpu_fucompp() {\n    fpu_fucom(1);\n    fpu_pop();\n    fpu_pop();\n}\n\npub unsafe fn fpu_fxam() {\n    let x = fpu_get_st0();\n    *fpu_status_word &= !FPU_RESULT_FLAGS;\n    *fpu_status_word |= (x.sign() as u16) << 9;\n    if 0 != *fpu_stack_empty >> *fpu_stack_ptr & 1 {\n        *fpu_status_word |= FPU_C3 | FPU_C0\n    }\n    else if x.is_nan() {\n        *fpu_status_word |= FPU_C0\n    }\n    else if x == F80::ZERO {\n        *fpu_status_word |= FPU_C3\n    }\n    else if !x.is_finite() {\n        *fpu_status_word |= FPU_C2 | FPU_C0\n    }\n    else {\n        *fpu_status_word |= FPU_C2\n    }\n    // TODO:\n    // Unsupported, Denormal\n}\n\n#[no_mangle]\npub unsafe fn fpu_fxch(i: i32) {\n    let sti = fpu_get_sti(i);\n    fpu_write_st(*fpu_stack_ptr as i32 + i & 7, fpu_get_st0());\n    fpu_write_st(*fpu_stack_ptr as i32, sti);\n}\npub unsafe fn fpu_fyl2x() {\n    let st0 = fpu_get_st0();\n    if st0 < F80::ZERO {\n        fpu_invalid_arithmetic();\n    }\n    else if st0 == F80::ZERO {\n        fpu_zero_fault();\n    }\n    fpu_write_st(\n        *fpu_stack_ptr as i32 + 1 & 7,\n        fpu_get_sti(1) * st0.ln() / F80::LN_2,\n    );\n    fpu_pop();\n}\n\npub unsafe fn fpu_fxtract() {\n    let st0 = fpu_get_st0();\n    if st0 == F80::ZERO {\n        fpu_zero_fault();\n        fpu_write_st(*fpu_stack_ptr as i32, F80::NEG_INFINITY);\n        fpu_push(st0);\n    }\n    else {\n        let exp = st0.exponent();\n        fpu_write_st(*fpu_stack_ptr as i32, F80::of_i32(exp.into()));\n        fpu_push(F80 {\n            sign_exponent: 0x3FFF,\n            mantissa: st0.mantissa,\n        });\n    }\n}\n\npub unsafe fn fwait() {\n    // NOP unless FPU instructions run in parallel with CPU instructions\n}\n\npub unsafe fn fpu_fchs() {\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32, -st0);\n}\n\npub unsafe fn fpu_fabs() {\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32, st0.abs());\n}\n\npub unsafe fn fpu_f2xm1() {\n    let st0 = fpu_get_st0();\n    let r = st0.two_pow() - F80::ONE;\n    fpu_write_st(*fpu_stack_ptr as i32, r)\n}\n\npub unsafe fn fpu_fptan() {\n    let st0 = fpu_get_st0();\n    //if -pow(2.0, 63.0) < st0 && st0 < pow(2.0, 63.0) {\n    fpu_write_st(*fpu_stack_ptr as i32, st0.tan());\n    // no bug: push constant 1\n    fpu_push(F80::ONE);\n    *fpu_status_word &= !FPU_C2;\n    //}\n    //else {\n    //    *fpu_status_word |= FPU_C2;\n    //}\n}\n\npub unsafe fn fpu_fpatan() {\n    let st0 = fpu_get_st0();\n    let st1 = fpu_get_sti(1);\n    fpu_write_st(*fpu_stack_ptr as i32 + 1 & 7, st1.atan2(st0));\n    fpu_pop();\n}\n\npub unsafe fn fpu_fyl2xp1() {\n    // fyl2xp1: y * log2(x+1) and pop\n    let st0 = fpu_get_st0();\n    let st1 = fpu_get_sti(1);\n    let y = st1 * (st0 + F80::ONE).ln() / F80::LN_2;\n    fpu_write_st(*fpu_stack_ptr as i32 + 1 & 7, y);\n    fpu_pop();\n}\n\npub unsafe fn fpu_fsqrt() {\n    let st0 = fpu_get_st0();\n    //if st0 < 0.0 {\n    //    fpu_invalid_arithmetic();\n    //}\n    fpu_write_st(*fpu_stack_ptr as i32, st0.sqrt())\n}\n\npub unsafe fn fpu_fsincos() {\n    let st0 = fpu_get_st0();\n    //if pow(-2.0, 63.0) < st0 && st0 < pow(2.0, 63.0) {\n    fpu_write_st(*fpu_stack_ptr as i32, st0.sin());\n    fpu_push(st0.cos());\n    *fpu_status_word &= !FPU_C2;\n    //}\n    //else {\n    //    *fpu_status_word |= FPU_C2;\n    //}\n}\n\npub unsafe fn fpu_frndint() {\n    let st0 = fpu_get_st0();\n    fpu_write_st(*fpu_stack_ptr as i32, st0.round());\n}\n\npub unsafe fn fpu_fscale() {\n    let st0 = fpu_get_st0();\n    let y = st0 * fpu_get_sti(1).trunc().two_pow();\n    fpu_write_st(*fpu_stack_ptr as i32, y);\n}\n\npub unsafe fn fpu_fsin() {\n    let st0 = fpu_get_st0();\n    //if pow(-2.0, 63.0) < st0 && st0 < pow(2.0, 63.0) {\n    fpu_write_st(*fpu_stack_ptr as i32, st0.sin());\n    *fpu_status_word &= !FPU_C2;\n    //}\n    //else {\n    //    *fpu_status_word |= FPU_C2;\n    //}\n}\n\npub unsafe fn fpu_fcos() {\n    let st0 = fpu_get_st0();\n    //if pow(-2.0, 63.0) < st0 && st0 < pow(2.0, 63.0) {\n    fpu_write_st(*fpu_stack_ptr as i32, st0.cos());\n    *fpu_status_word &= !FPU_C2;\n    //}\n    //else {\n    //    *fpu_status_word |= FPU_C2;\n    //}\n}\n\npub unsafe fn fpu_fdecstp() {\n    *fpu_stack_ptr = *fpu_stack_ptr - 1 & 7;\n    *fpu_status_word &= !FPU_C1\n}\n\npub unsafe fn fpu_fincstp() {\n    *fpu_stack_ptr = *fpu_stack_ptr + 1 & 7;\n    *fpu_status_word &= !FPU_C1\n}\n"
  },
  {
    "path": "src/rust/cpu/global_pointers.rs",
    "content": "#![allow(non_upper_case_globals)]\n\nuse crate::cpu::cpu::reg128;\nuse crate::softfloat::F80;\nuse crate::state_flags::CachedStateFlags;\n\npub const reg8: *mut u8 = 64 as *mut u8;\npub const reg16: *mut u16 = 64 as *mut u16;\npub const reg32: *mut i32 = 64 as *mut i32;\n\npub const last_op_size: *mut i32 = 96 as *mut i32;\npub const flags_changed: *mut i32 = 100 as *mut i32;\npub const last_op1: *mut i32 = 104 as *mut i32;\npub const state_flags: *mut CachedStateFlags = 108 as *mut CachedStateFlags;\npub const last_result: *mut i32 = 112 as *mut i32;\npub const flags: *mut i32 = 120 as *mut i32;\n\npub const segment_access_bytes: *mut u8 = 512 as *mut u8; // TODO: reorder below segment_limits\n\npub const page_fault: *mut bool = 540 as *mut bool;\n\npub const apic_enabled: *mut bool = 548 as *mut bool;\npub const acpi_enabled: *mut bool = 552 as *mut bool;\n\npub const instruction_pointer: *mut i32 = 556 as *mut i32;\npub const previous_ip: *mut i32 = 560 as *mut i32;\npub const idtr_size: *mut i32 = 564 as *mut i32;\npub const idtr_offset: *mut i32 = 568 as *mut i32;\npub const gdtr_size: *mut i32 = 572 as *mut i32;\npub const gdtr_offset: *mut i32 = 576 as *mut i32;\npub const cr: *mut i32 = 580 as *mut i32;\npub const cpl: *mut u8 = 612 as *mut u8;\npub const in_hlt: *mut bool = 616 as *mut bool;\npub const last_virt_eip: *mut i32 = 620 as *mut i32;\npub const eip_phys: *mut i32 = 624 as *mut i32;\n\npub const sysenter_cs: *mut i32 = 636 as *mut i32;\npub const sysenter_esp: *mut i32 = 640 as *mut i32;\npub const sysenter_eip: *mut i32 = 644 as *mut i32;\npub const prefixes: *mut u8 = 648 as *mut u8;\npub const instruction_counter: *mut u32 = 664 as *mut u32;\npub const sreg: *mut u16 = 668 as *mut u16;\npub const dreg: *mut i32 = 684 as *mut i32;\n\n// filled in by svga_fill_pixel_buffer, read by javacsript for optimised putImageData calls\npub const svga_dirty_bitmap_min_offset: *mut u32 = 716 as *mut u32;\npub const svga_dirty_bitmap_max_offset: *mut u32 = 720 as *mut u32;\n\npub const segment_is_null: *mut bool = 724 as *mut bool;\npub const segment_offsets: *mut i32 = 736 as *mut i32;\npub const segment_limits: *mut u32 = 768 as *mut u32;\n\npub const protected_mode: *mut bool = 800 as *mut bool;\npub const is_32: *mut bool = 804 as *mut bool;\npub const stack_size_32: *mut bool = 808 as *mut bool;\npub const memory_size: *mut u32 = 812 as *mut u32;\npub const fpu_stack_empty: *mut u8 = 816 as *mut u8;\npub const mxcsr: *mut i32 = 824 as *mut i32;\n\npub const reg_xmm: *mut reg128 = 832 as *mut reg128;\npub const current_tsc: *mut u64 = 960 as *mut u64;\n\npub const reg_pdpte: *mut u64 = 968 as *mut u64; // 4 64-bit entries\n\npub const fpu_stack_ptr: *mut u8 = 1032 as *mut u8;\npub const fpu_control_word: *mut u16 = 1036 as *mut u16;\npub const fpu_status_word: *mut u16 = 1040 as *mut u16;\npub const fpu_opcode: *mut i32 = 1044 as *mut i32;\npub const fpu_ip: *mut i32 = 1048 as *mut i32;\npub const fpu_ip_selector: *mut i32 = 1052 as *mut i32;\npub const fpu_dp: *mut i32 = 1056 as *mut i32;\npub const fpu_dp_selector: *mut i32 = 1060 as *mut i32;\npub const tss_size_32: *mut bool = 1128 as *mut bool;\n\npub const sse_scratch_register: *mut reg128 = 1136 as *mut reg128;\n\npub const fpu_st: *mut F80 = 1152 as *mut F80;\n\npub fn get_reg32_offset(r: u32) -> u32 {\n    dbg_assert!(r < 8);\n    (unsafe { reg32.offset(r as isize) }) as u32\n}\n\npub fn get_reg_mmx_offset(r: u32) -> u32 {\n    dbg_assert!(r < 8);\n    (unsafe { fpu_st.offset(r as isize) }) as u32\n}\n\npub fn get_reg_xmm_offset(r: u32) -> u32 {\n    dbg_assert!(r < 8);\n    (unsafe { reg_xmm.offset(r as isize) }) as u32\n}\n\npub fn get_sreg_offset(s: u32) -> u32 {\n    dbg_assert!(s < 6);\n    (unsafe { sreg.offset(s as isize) }) as u32\n}\n\npub fn get_seg_offset(s: u32) -> u32 {\n    dbg_assert!(s < 8);\n    (unsafe { segment_offsets.offset(s as isize) }) as u32\n}\n\npub fn get_segment_is_null_offset(s: u32) -> u32 {\n    dbg_assert!(s < 8);\n    (unsafe { segment_is_null.offset(s as isize) }) as u32\n}\n\npub fn get_creg_offset(i: u32) -> u32 {\n    dbg_assert!(i < 8);\n    (unsafe { cr.offset(i as isize) }) as u32\n}\n"
  },
  {
    "path": "src/rust/cpu/instructions.rs",
    "content": "#![allow(non_snake_case)]\n\nuse crate::cpu::arith::*;\nuse crate::cpu::cpu::js;\nuse crate::cpu::cpu::*;\nuse crate::cpu::fpu::*;\nuse crate::cpu::global_pointers::*;\nuse crate::cpu::misc_instr::*;\nuse crate::cpu::string::*;\nuse crate::prefix;\nuse crate::softfloat::F80;\n\npub unsafe fn instr_00_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| add8(x, read_reg8(r))) }\npub unsafe fn instr_00_reg(r1: i32, r: i32) { write_reg8(r1, add8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_01_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| add16(x, read_reg16(r)))\n}\npub unsafe fn instr16_01_reg(r1: i32, r: i32) {\n    write_reg16(r1, add16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_01_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| add32(x, read_reg32(r)))\n}\npub unsafe fn instr32_01_reg(r1: i32, r: i32) {\n    write_reg32(r1, add32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_02_mem(addr: i32, r: i32) {\n    write_reg8(\n        r,\n        add8(read_reg8(r), return_on_pagefault!(safe_read8(addr))),\n    );\n}\npub unsafe fn instr_02_reg(r1: i32, r: i32) { write_reg8(r, add8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_03_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        add16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_03_reg(r1: i32, r: i32) {\n    write_reg16(r, add16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_03_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        add32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_03_reg(r1: i32, r: i32) {\n    write_reg32(r, add32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_04(imm8: i32) { write_reg8(AL, add8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_05(imm16: i32) { write_reg16(AX, add16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_05(imm32: i32) { write_reg32(EAX, add32(read_reg32(EAX), imm32)); }\npub unsafe fn instr16_06() {\n    return_on_pagefault!(push16(*sreg.offset(ES as isize) as i32));\n}\npub unsafe fn instr32_06() { return_on_pagefault!(push32_sreg(ES)) }\n\n#[no_mangle]\npub unsafe fn instr16_07() {\n    if !switch_seg(ES, return_on_pagefault!(safe_read16(get_stack_pointer(0)))) {\n        return;\n    }\n    adjust_stack_reg(2);\n}\n#[no_mangle]\npub unsafe fn instr32_07() {\n    if !switch_seg(\n        ES,\n        return_on_pagefault!(safe_read32s(get_stack_pointer(0))) & 0xFFFF,\n    ) {\n        return;\n    }\n    adjust_stack_reg(4);\n}\n\npub unsafe fn instr_08_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| or8(x, read_reg8(r))) }\npub unsafe fn instr_08_reg(r1: i32, r: i32) { write_reg8(r1, or8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_09_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| or16(x, read_reg16(r)))\n}\npub unsafe fn instr16_09_reg(r1: i32, r: i32) {\n    write_reg16(r1, or16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_09_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| or32(x, read_reg32(r)))\n}\npub unsafe fn instr32_09_reg(r1: i32, r: i32) {\n    write_reg32(r1, or32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_0A_mem(addr: i32, r: i32) {\n    write_reg8(r, or8(read_reg8(r), return_on_pagefault!(safe_read8(addr))));\n}\npub unsafe fn instr_0A_reg(r1: i32, r: i32) { write_reg8(r, or8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_0B_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        or16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_0B_reg(r1: i32, r: i32) {\n    write_reg16(r, or16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_0B_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        or32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_0B_reg(r1: i32, r: i32) {\n    write_reg32(r, or32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_0C(imm8: i32) { write_reg8(AL, or8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_0D(imm16: i32) { write_reg16(AX, or16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_0D(imm32: i32) { write_reg32(EAX, or32(read_reg32(EAX), imm32)); }\n\npub unsafe fn instr16_0E() {\n    return_on_pagefault!(push16(*sreg.offset(CS as isize) as i32));\n}\npub unsafe fn instr32_0E() { return_on_pagefault!(push32_sreg(CS)) }\n\npub unsafe fn instr16_0F() { run_instruction0f_16(return_on_pagefault!(read_imm8())); }\npub unsafe fn instr32_0F() { run_instruction0f_32(return_on_pagefault!(read_imm8())); }\n\npub unsafe fn instr_10_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| adc8(x, read_reg8(r))) }\npub unsafe fn instr_10_reg(r1: i32, r: i32) { write_reg8(r1, adc8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_11_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| adc16(x, read_reg16(r)))\n}\npub unsafe fn instr16_11_reg(r1: i32, r: i32) {\n    write_reg16(r1, adc16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_11_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| adc32(x, read_reg32(r)))\n}\npub unsafe fn instr32_11_reg(r1: i32, r: i32) {\n    write_reg32(r1, adc32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_12_mem(addr: i32, r: i32) {\n    write_reg8(\n        r,\n        adc8(read_reg8(r), return_on_pagefault!(safe_read8(addr))),\n    );\n}\npub unsafe fn instr_12_reg(r1: i32, r: i32) { write_reg8(r, adc8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_13_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        adc16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_13_reg(r1: i32, r: i32) {\n    write_reg16(r, adc16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_13_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        adc32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_13_reg(r1: i32, r: i32) {\n    write_reg32(r, adc32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_14(imm8: i32) { write_reg8(AL, adc8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_15(imm16: i32) { write_reg16(AX, adc16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_15(imm32: i32) { write_reg32(EAX, adc32(read_reg32(EAX), imm32)); }\n\npub unsafe fn instr16_16() {\n    return_on_pagefault!(push16(*sreg.offset(SS as isize) as i32));\n}\npub unsafe fn instr32_16() { return_on_pagefault!(push32_sreg(SS)) }\n\n#[no_mangle]\npub unsafe fn instr16_17() {\n    if !switch_seg(SS, return_on_pagefault!(safe_read16(get_stack_pointer(0)))) {\n        return;\n    }\n    adjust_stack_reg(2);\n}\n#[no_mangle]\npub unsafe fn instr32_17() {\n    if !switch_seg(\n        SS,\n        return_on_pagefault!(safe_read32s(get_stack_pointer(0))) & 0xFFFF,\n    ) {\n        return;\n    }\n    adjust_stack_reg(4);\n}\n\npub unsafe fn instr_18_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| sbb8(x, read_reg8(r))) }\npub unsafe fn instr_18_reg(r1: i32, r: i32) { write_reg8(r1, sbb8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_19_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| sbb16(x, read_reg16(r)))\n}\npub unsafe fn instr16_19_reg(r1: i32, r: i32) {\n    write_reg16(r1, sbb16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_19_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| sbb32(x, read_reg32(r)))\n}\npub unsafe fn instr32_19_reg(r1: i32, r: i32) {\n    write_reg32(r1, sbb32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_1A_mem(addr: i32, r: i32) {\n    write_reg8(\n        r,\n        sbb8(read_reg8(r), return_on_pagefault!(safe_read8(addr))),\n    );\n}\npub unsafe fn instr_1A_reg(r1: i32, r: i32) { write_reg8(r, sbb8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_1B_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        sbb16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_1B_reg(r1: i32, r: i32) {\n    write_reg16(r, sbb16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_1B_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        sbb32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_1B_reg(r1: i32, r: i32) {\n    write_reg32(r, sbb32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_1C(imm8: i32) { write_reg8(AL, sbb8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_1D(imm16: i32) { write_reg16(AX, sbb16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_1D(imm32: i32) { write_reg32(EAX, sbb32(read_reg32(EAX), imm32)); }\n\npub unsafe fn instr16_1E() {\n    return_on_pagefault!(push16(*sreg.offset(DS as isize) as i32));\n}\npub unsafe fn instr32_1E() { return_on_pagefault!(push32_sreg(DS)) }\n\n#[no_mangle]\npub unsafe fn instr16_1F() {\n    if !switch_seg(DS, return_on_pagefault!(safe_read16(get_stack_pointer(0)))) {\n        return;\n    }\n    adjust_stack_reg(2);\n}\n#[no_mangle]\npub unsafe fn instr32_1F() {\n    if !switch_seg(\n        DS,\n        return_on_pagefault!(safe_read32s(get_stack_pointer(0))) & 0xFFFF,\n    ) {\n        return;\n    }\n    adjust_stack_reg(4);\n}\n\npub unsafe fn instr_20_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| and8(x, read_reg8(r))) }\npub unsafe fn instr_20_reg(r1: i32, r: i32) { write_reg8(r1, and8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_21_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| and16(x, read_reg16(r)))\n}\npub unsafe fn instr16_21_reg(r1: i32, r: i32) {\n    write_reg16(r1, and16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_21_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| and32(x, read_reg32(r)))\n}\npub unsafe fn instr32_21_reg(r1: i32, r: i32) {\n    write_reg32(r1, and32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_22_mem(addr: i32, r: i32) {\n    write_reg8(\n        r,\n        and8(read_reg8(r), return_on_pagefault!(safe_read8(addr))),\n    );\n}\npub unsafe fn instr_22_reg(r1: i32, r: i32) { write_reg8(r, and8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_23_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        and16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_23_reg(r1: i32, r: i32) {\n    write_reg16(r, and16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_23_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        and32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_23_reg(r1: i32, r: i32) {\n    write_reg32(r, and32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_24(imm8: i32) { write_reg8(AL, and8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_25(imm16: i32) { write_reg16(AX, and16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_25(imm32: i32) { write_reg32(EAX, and32(read_reg32(EAX), imm32)); }\n\npub unsafe fn instr_26() { segment_prefix_op(ES); }\n\n#[no_mangle]\npub unsafe fn instr_27() { bcd_daa(); }\n\npub unsafe fn instr_28_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| sub8(x, read_reg8(r))) }\npub unsafe fn instr_28_reg(r1: i32, r: i32) { write_reg8(r1, sub8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_29_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| sub16(x, read_reg16(r)))\n}\npub unsafe fn instr16_29_reg(r1: i32, r: i32) {\n    write_reg16(r1, sub16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_29_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| sub32(x, read_reg32(r)))\n}\npub unsafe fn instr32_29_reg(r1: i32, r: i32) {\n    write_reg32(r1, sub32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_2A_mem(addr: i32, r: i32) {\n    write_reg8(\n        r,\n        sub8(read_reg8(r), return_on_pagefault!(safe_read8(addr))),\n    );\n}\npub unsafe fn instr_2A_reg(r1: i32, r: i32) { write_reg8(r, sub8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_2B_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        sub16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_2B_reg(r1: i32, r: i32) {\n    write_reg16(r, sub16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_2B_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        sub32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_2B_reg(r1: i32, r: i32) {\n    write_reg32(r, sub32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_2C(imm8: i32) { write_reg8(AL, sub8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_2D(imm16: i32) { write_reg16(AX, sub16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_2D(imm32: i32) { write_reg32(EAX, sub32(read_reg32(EAX), imm32)); }\n\npub unsafe fn instr_2E() { segment_prefix_op(CS); }\n\n#[no_mangle]\npub unsafe fn instr_2F() { bcd_das(); }\n\npub unsafe fn instr_30_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| xor8(x, read_reg8(r))) }\npub unsafe fn instr_30_reg(r1: i32, r: i32) { write_reg8(r1, xor8(read_reg8(r1), read_reg8(r))); }\npub unsafe fn instr16_31_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| xor16(x, read_reg16(r)))\n}\npub unsafe fn instr16_31_reg(r1: i32, r: i32) {\n    write_reg16(r1, xor16(read_reg16(r1), read_reg16(r)));\n}\npub unsafe fn instr32_31_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| xor32(x, read_reg32(r)))\n}\npub unsafe fn instr32_31_reg(r1: i32, r: i32) {\n    write_reg32(r1, xor32(read_reg32(r1), read_reg32(r)));\n}\npub unsafe fn instr_32_mem(addr: i32, r: i32) {\n    write_reg8(\n        r,\n        xor8(read_reg8(r), return_on_pagefault!(safe_read8(addr))),\n    );\n}\npub unsafe fn instr_32_reg(r1: i32, r: i32) { write_reg8(r, xor8(read_reg8(r), read_reg8(r1))); }\npub unsafe fn instr16_33_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        xor16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_33_reg(r1: i32, r: i32) {\n    write_reg16(r, xor16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_33_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        xor32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_33_reg(r1: i32, r: i32) {\n    write_reg32(r, xor32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr_34(imm8: i32) { write_reg8(AL, xor8(read_reg8(AL), imm8)); }\npub unsafe fn instr16_35(imm16: i32) { write_reg16(AX, xor16(read_reg16(AX), imm16)); }\npub unsafe fn instr32_35(imm32: i32) { write_reg32(EAX, xor32(read_reg32(EAX), imm32)); }\n\npub unsafe fn instr_36() { segment_prefix_op(SS); }\n\n#[no_mangle]\npub unsafe fn instr_37() { bcd_aaa(); }\n\npub unsafe fn instr_38_mem(addr: i32, r: i32) {\n    cmp8(return_on_pagefault!(safe_read8(addr)), read_reg8(r));\n}\npub unsafe fn instr_38_reg(r1: i32, r: i32) { cmp8(read_reg8(r1), read_reg8(r)); }\npub unsafe fn instr16_39_mem(addr: i32, r: i32) {\n    cmp16(return_on_pagefault!(safe_read16(addr)), read_reg16(r));\n}\npub unsafe fn instr16_39_reg(r1: i32, r: i32) { cmp16(read_reg16(r1), read_reg16(r)); }\npub unsafe fn instr32_39_mem(addr: i32, r: i32) {\n    cmp32(return_on_pagefault!(safe_read32s(addr)), read_reg32(r));\n}\npub unsafe fn instr32_39_reg(r1: i32, r: i32) { cmp32(read_reg32(r1), read_reg32(r)); }\npub unsafe fn instr_3A_mem(addr: i32, r: i32) {\n    cmp8(read_reg8(r), return_on_pagefault!(safe_read8(addr)));\n}\npub unsafe fn instr_3A_reg(r1: i32, r: i32) { cmp8(read_reg8(r), read_reg8(r1)); }\npub unsafe fn instr16_3B_mem(addr: i32, r: i32) {\n    cmp16(read_reg16(r), return_on_pagefault!(safe_read16(addr)));\n}\npub unsafe fn instr16_3B_reg(r1: i32, r: i32) { cmp16(read_reg16(r), read_reg16(r1)); }\npub unsafe fn instr32_3B_mem(addr: i32, r: i32) {\n    cmp32(read_reg32(r), return_on_pagefault!(safe_read32s(addr)));\n}\npub unsafe fn instr32_3B_reg(r1: i32, r: i32) { cmp32(read_reg32(r), read_reg32(r1)); }\npub unsafe fn instr_3C(imm8: i32) { cmp8(read_reg8(AL), imm8); }\npub unsafe fn instr16_3D(imm16: i32) { cmp16(read_reg16(AX), imm16); }\npub unsafe fn instr32_3D(imm32: i32) { cmp32(read_reg32(EAX), imm32); }\n\npub unsafe fn instr_3E() { segment_prefix_op(DS); }\n\n#[no_mangle]\npub unsafe fn instr_3F() { bcd_aas(); }\n\npub unsafe fn instr16_40() { write_reg16(AX, inc16(read_reg16(AX))); }\npub unsafe fn instr32_40() { write_reg32(EAX, inc32(read_reg32(EAX))); }\npub unsafe fn instr16_41() { write_reg16(CX, inc16(read_reg16(CX))); }\npub unsafe fn instr32_41() { write_reg32(ECX, inc32(read_reg32(ECX))); }\npub unsafe fn instr16_42() { write_reg16(DX, inc16(read_reg16(DX))); }\npub unsafe fn instr32_42() { write_reg32(EDX, inc32(read_reg32(EDX))); }\npub unsafe fn instr16_43() { write_reg16(BX, inc16(read_reg16(BX))); }\npub unsafe fn instr32_43() { write_reg32(EBX, inc32(read_reg32(EBX))); }\npub unsafe fn instr16_44() { write_reg16(SP, inc16(read_reg16(SP))); }\npub unsafe fn instr32_44() { write_reg32(ESP, inc32(read_reg32(ESP))); }\npub unsafe fn instr16_45() { write_reg16(BP, inc16(read_reg16(BP))); }\npub unsafe fn instr32_45() { write_reg32(EBP, inc32(read_reg32(EBP))); }\npub unsafe fn instr16_46() { write_reg16(SI, inc16(read_reg16(SI))); }\npub unsafe fn instr32_46() { write_reg32(ESI, inc32(read_reg32(ESI))); }\npub unsafe fn instr16_47() { write_reg16(DI, inc16(read_reg16(DI))); }\npub unsafe fn instr32_47() { write_reg32(EDI, inc32(read_reg32(EDI))); }\npub unsafe fn instr16_48() { write_reg16(AX, dec16(read_reg16(AX))); }\npub unsafe fn instr32_48() { write_reg32(EAX, dec32(read_reg32(EAX))); }\npub unsafe fn instr16_49() { write_reg16(CX, dec16(read_reg16(CX))); }\npub unsafe fn instr32_49() { write_reg32(ECX, dec32(read_reg32(ECX))); }\npub unsafe fn instr16_4A() { write_reg16(DX, dec16(read_reg16(DX))); }\npub unsafe fn instr32_4A() { write_reg32(EDX, dec32(read_reg32(EDX))); }\npub unsafe fn instr16_4B() { write_reg16(BX, dec16(read_reg16(BX))); }\npub unsafe fn instr32_4B() { write_reg32(EBX, dec32(read_reg32(EBX))); }\npub unsafe fn instr16_4C() { write_reg16(SP, dec16(read_reg16(SP))); }\npub unsafe fn instr32_4C() { write_reg32(ESP, dec32(read_reg32(ESP))); }\npub unsafe fn instr16_4D() { write_reg16(BP, dec16(read_reg16(BP))); }\npub unsafe fn instr32_4D() { write_reg32(EBP, dec32(read_reg32(EBP))); }\npub unsafe fn instr16_4E() { write_reg16(SI, dec16(read_reg16(SI))); }\npub unsafe fn instr32_4E() { write_reg32(ESI, dec32(read_reg32(ESI))); }\npub unsafe fn instr16_4F() { write_reg16(DI, dec16(read_reg16(DI))); }\npub unsafe fn instr32_4F() { write_reg32(EDI, dec32(read_reg32(EDI))); }\n\npub unsafe fn push16_reg(r: i32) {\n    return_on_pagefault!(push16(read_reg16(r)));\n}\npub unsafe fn push32_reg(r: i32) {\n    return_on_pagefault!(push32(read_reg32(r)));\n}\n\npub unsafe fn instr16_50() { push16_reg(AX) }\npub unsafe fn instr32_50() { push32_reg(EAX) }\npub unsafe fn instr16_51() { push16_reg(CX) }\npub unsafe fn instr32_51() { push32_reg(ECX) }\npub unsafe fn instr16_52() { push16_reg(DX) }\npub unsafe fn instr32_52() { push32_reg(EDX) }\npub unsafe fn instr16_53() { push16_reg(BX) }\npub unsafe fn instr32_53() { push32_reg(EBX) }\npub unsafe fn instr16_54() { push16_reg(SP) }\npub unsafe fn instr32_54() { push32_reg(ESP) }\npub unsafe fn instr16_55() { push16_reg(BP) }\npub unsafe fn instr32_55() { push32_reg(EBP) }\npub unsafe fn instr16_56() { push16_reg(SI) }\npub unsafe fn instr32_56() { push32_reg(ESI) }\npub unsafe fn instr16_57() { push16_reg(DI) }\npub unsafe fn instr32_57() { push32_reg(EDI) }\npub unsafe fn instr16_58() { write_reg16(AX, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_58() { write_reg32(EAX, return_on_pagefault!(pop32s())); }\npub unsafe fn instr16_59() { write_reg16(CX, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_59() { write_reg32(ECX, return_on_pagefault!(pop32s())); }\npub unsafe fn instr16_5A() { write_reg16(DX, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_5A() { write_reg32(EDX, return_on_pagefault!(pop32s())); }\npub unsafe fn instr16_5B() { write_reg16(BX, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_5B() { write_reg32(EBX, return_on_pagefault!(pop32s())); }\npub unsafe fn instr16_5C() {\n    write_reg16(SP, return_on_pagefault!(safe_read16(get_stack_pointer(0))));\n}\npub unsafe fn instr32_5C() {\n    write_reg32(\n        ESP,\n        return_on_pagefault!(safe_read32s(get_stack_pointer(0))),\n    );\n}\npub unsafe fn instr16_5D() { write_reg16(BP, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_5D() { write_reg32(EBP, return_on_pagefault!(pop32s())); }\npub unsafe fn instr16_5E() { write_reg16(SI, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_5E() { write_reg32(ESI, return_on_pagefault!(pop32s())); }\npub unsafe fn instr16_5F() { write_reg16(DI, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_5F() { write_reg32(EDI, return_on_pagefault!(pop32s())); }\n\n#[no_mangle]\npub unsafe fn instr16_60() { pusha16(); }\n#[no_mangle]\npub unsafe fn instr32_60() { pusha32(); }\n#[no_mangle]\npub unsafe fn instr16_61() { popa16(); }\n#[no_mangle]\npub unsafe fn instr32_61() { popa32(); }\n\n#[no_mangle]\npub unsafe fn instr_62_reg(_r2: i32, _r: i32) {\n    // bound\n    dbg_log!(\"Unimplemented BOUND instruction\");\n    dbg_assert!(false);\n}\n#[no_mangle]\npub unsafe fn instr_62_mem(_addr: i32, _r: i32) {\n    dbg_log!(\"Unimplemented BOUND instruction\");\n    dbg_assert!(false);\n}\n\npub unsafe fn arpl(seg: i32, r16: i32) -> i32 {\n    *flags_changed &= !FLAG_ZERO;\n\n    if (seg & 3) < (r16 & 3) {\n        *flags |= FLAG_ZERO;\n        seg & !3 | r16 & 3\n    }\n    else {\n        *flags &= !FLAG_ZERO;\n        seg\n    }\n}\n\n#[no_mangle]\npub unsafe fn instr_63_mem(addr: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"arpl #ud\");\n        trigger_ud();\n        return;\n    }\n    safe_read_write16(addr, &|x| arpl(x, read_reg16(r)))\n}\n#[no_mangle]\npub unsafe fn instr_63_reg(r1: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"arpl #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg16(r1, arpl(read_reg16(r1), read_reg16(r)));\n}\n\npub unsafe fn instr_64() { segment_prefix_op(FS); }\npub unsafe fn instr_65() { segment_prefix_op(GS); }\n\npub unsafe fn instr_66() {\n    // Operand-size override prefix\n    *prefixes |= prefix::PREFIX_MASK_OPSIZE;\n    run_prefix_instruction();\n    *prefixes = 0;\n}\npub unsafe fn instr_67() {\n    // Address-size override prefix\n    *prefixes |= prefix::PREFIX_MASK_ADDRSIZE;\n    run_prefix_instruction();\n    *prefixes = 0;\n}\n\npub unsafe fn instr16_68(imm16: i32) {\n    return_on_pagefault!(push16(imm16));\n}\npub unsafe fn instr32_68(imm32: i32) {\n    return_on_pagefault!(push32(imm32));\n}\npub unsafe fn instr16_69_mem(addr: i32, r: i32, imm: i32) {\n    write_reg16(r, imul_reg16(return_on_pagefault!(safe_read16(addr)), imm));\n}\npub unsafe fn instr16_69_reg(r1: i32, r: i32, imm: i32) {\n    write_reg16(r, imul_reg16(read_reg16(r1), imm));\n}\npub unsafe fn instr32_69_mem(addr: i32, r: i32, imm: i32) {\n    write_reg32(r, imul_reg32(return_on_pagefault!(safe_read32s(addr)), imm));\n}\npub unsafe fn instr32_69_reg(r1: i32, r: i32, imm: i32) {\n    write_reg32(r, imul_reg32(read_reg32(r1), imm));\n}\n\npub unsafe fn instr16_6A(imm8: i32) {\n    return_on_pagefault!(push16(imm8 & 0xFFFF));\n}\npub unsafe fn instr32_6A(imm8: i32) {\n    return_on_pagefault!(push32(imm8));\n}\npub unsafe fn instr16_6B_mem(addr: i32, r: i32, imm: i32) {\n    write_reg16(r, imul_reg16(return_on_pagefault!(safe_read16(addr)), imm));\n}\npub unsafe fn instr16_6B_reg(r1: i32, r: i32, imm: i32) {\n    write_reg16(r, imul_reg16(read_reg16(r1), imm));\n}\npub unsafe fn instr32_6B_mem(addr: i32, r: i32, imm: i32) {\n    write_reg32(r, imul_reg32(return_on_pagefault!(safe_read32s(addr)), imm));\n}\npub unsafe fn instr32_6B_reg(r1: i32, r: i32, imm: i32) {\n    write_reg32(r, imul_reg32(read_reg32(r1), imm));\n}\n\npub unsafe fn instr_6C() { insb_no_rep(is_asize_32()); }\npub unsafe fn instr_F26C() { insb_rep(is_asize_32()); }\npub unsafe fn instr_F36C() { insb_rep(is_asize_32()); }\npub unsafe fn instr16_6D() { insw_no_rep(is_asize_32()); }\npub unsafe fn instr32_6D() { insd_no_rep(is_asize_32()); }\npub unsafe fn instr16_F26D() { insw_rep(is_asize_32()); }\npub unsafe fn instr16_F36D() { insw_rep(is_asize_32()); }\npub unsafe fn instr32_F26D() { insd_rep(is_asize_32()); }\npub unsafe fn instr32_F36D() { insd_rep(is_asize_32()); }\n\npub unsafe fn instr_6E() { outsb_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F26E() { outsb_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F36E() { outsb_rep(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr16_6F() { outsw_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_6F() { outsd_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F26F() { outsw_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F36F() { outsw_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F26F() { outsd_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F36F() { outsd_rep(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr16_70(imm8: i32) { jmpcc16(test_o(), imm8); }\npub unsafe fn instr16_71(imm8: i32) { jmpcc16(!test_o(), imm8); }\npub unsafe fn instr16_72(imm8: i32) { jmpcc16(test_b(), imm8); }\npub unsafe fn instr16_73(imm8: i32) { jmpcc16(!test_b(), imm8); }\npub unsafe fn instr16_74(imm8: i32) { jmpcc16(test_z(), imm8); }\npub unsafe fn instr16_75(imm8: i32) { jmpcc16(!test_z(), imm8); }\npub unsafe fn instr16_76(imm8: i32) { jmpcc16(test_be(), imm8); }\npub unsafe fn instr16_77(imm8: i32) { jmpcc16(!test_be(), imm8); }\npub unsafe fn instr16_78(imm8: i32) { jmpcc16(test_s(), imm8); }\npub unsafe fn instr16_79(imm8: i32) { jmpcc16(!test_s(), imm8); }\npub unsafe fn instr16_7A(imm8: i32) { jmpcc16(test_p(), imm8); }\npub unsafe fn instr16_7B(imm8: i32) { jmpcc16(!test_p(), imm8); }\npub unsafe fn instr16_7C(imm8: i32) { jmpcc16(test_l(), imm8); }\npub unsafe fn instr16_7D(imm8: i32) { jmpcc16(!test_l(), imm8); }\npub unsafe fn instr16_7E(imm8: i32) { jmpcc16(test_le(), imm8); }\npub unsafe fn instr16_7F(imm8: i32) { jmpcc16(!test_le(), imm8); }\npub unsafe fn instr32_70(imm8: i32) { jmpcc32(test_o(), imm8); }\npub unsafe fn instr32_71(imm8: i32) { jmpcc32(!test_o(), imm8); }\npub unsafe fn instr32_72(imm8: i32) { jmpcc32(test_b(), imm8); }\npub unsafe fn instr32_73(imm8: i32) { jmpcc32(!test_b(), imm8); }\npub unsafe fn instr32_74(imm8: i32) { jmpcc32(test_z(), imm8); }\npub unsafe fn instr32_75(imm8: i32) { jmpcc32(!test_z(), imm8); }\npub unsafe fn instr32_76(imm8: i32) { jmpcc32(test_be(), imm8); }\npub unsafe fn instr32_77(imm8: i32) { jmpcc32(!test_be(), imm8); }\npub unsafe fn instr32_78(imm8: i32) { jmpcc32(test_s(), imm8); }\npub unsafe fn instr32_79(imm8: i32) { jmpcc32(!test_s(), imm8); }\npub unsafe fn instr32_7A(imm8: i32) { jmpcc32(test_p(), imm8); }\npub unsafe fn instr32_7B(imm8: i32) { jmpcc32(!test_p(), imm8); }\npub unsafe fn instr32_7C(imm8: i32) { jmpcc32(test_l(), imm8); }\npub unsafe fn instr32_7D(imm8: i32) { jmpcc32(!test_l(), imm8); }\npub unsafe fn instr32_7E(imm8: i32) { jmpcc32(test_le(), imm8); }\npub unsafe fn instr32_7F(imm8: i32) { jmpcc32(!test_le(), imm8); }\n\npub unsafe fn instr_80_0_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| add8(x, imm)) }\npub unsafe fn instr_80_0_reg(r1: i32, imm: i32) { write_reg8(r1, add8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_1_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| or8(x, imm)) }\npub unsafe fn instr_80_1_reg(r1: i32, imm: i32) { write_reg8(r1, or8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_2_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| adc8(x, imm)) }\npub unsafe fn instr_80_2_reg(r1: i32, imm: i32) { write_reg8(r1, adc8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_3_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| sbb8(x, imm)) }\npub unsafe fn instr_80_3_reg(r1: i32, imm: i32) { write_reg8(r1, sbb8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_4_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| and8(x, imm)) }\npub unsafe fn instr_80_4_reg(r1: i32, imm: i32) { write_reg8(r1, and8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_5_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| sub8(x, imm)) }\npub unsafe fn instr_80_5_reg(r1: i32, imm: i32) { write_reg8(r1, sub8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_6_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| xor8(x, imm)) }\npub unsafe fn instr_80_6_reg(r1: i32, imm: i32) { write_reg8(r1, xor8(read_reg8(r1), imm)); }\npub unsafe fn instr_80_7_reg(r: i32, imm: i32) { cmp8(read_reg8(r), imm); }\npub unsafe fn instr_80_7_mem(addr: i32, imm: i32) {\n    cmp8(return_on_pagefault!(safe_read8(addr)), imm);\n}\npub unsafe fn instr16_81_0_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| add16(x, imm)) }\npub unsafe fn instr16_81_0_reg(r1: i32, imm: i32) { write_reg16(r1, add16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_1_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| or16(x, imm)) }\npub unsafe fn instr16_81_1_reg(r1: i32, imm: i32) { write_reg16(r1, or16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_2_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| adc16(x, imm)) }\npub unsafe fn instr16_81_2_reg(r1: i32, imm: i32) { write_reg16(r1, adc16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_3_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| sbb16(x, imm)) }\npub unsafe fn instr16_81_3_reg(r1: i32, imm: i32) { write_reg16(r1, sbb16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_4_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| and16(x, imm)) }\npub unsafe fn instr16_81_4_reg(r1: i32, imm: i32) { write_reg16(r1, and16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_5_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| sub16(x, imm)) }\npub unsafe fn instr16_81_5_reg(r1: i32, imm: i32) { write_reg16(r1, sub16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_6_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| xor16(x, imm)) }\npub unsafe fn instr16_81_6_reg(r1: i32, imm: i32) { write_reg16(r1, xor16(read_reg16(r1), imm)); }\npub unsafe fn instr16_81_7_reg(r: i32, imm: i32) { cmp16(read_reg16(r), imm); }\npub unsafe fn instr16_81_7_mem(addr: i32, imm: i32) {\n    cmp16(return_on_pagefault!(safe_read16(addr)), imm);\n}\npub unsafe fn instr32_81_0_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| add32(x, imm)) }\npub unsafe fn instr32_81_0_reg(r1: i32, imm: i32) { write_reg32(r1, add32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_1_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| or32(x, imm)) }\npub unsafe fn instr32_81_1_reg(r1: i32, imm: i32) { write_reg32(r1, or32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_2_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| adc32(x, imm)) }\npub unsafe fn instr32_81_2_reg(r1: i32, imm: i32) { write_reg32(r1, adc32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_3_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| sbb32(x, imm)) }\npub unsafe fn instr32_81_3_reg(r1: i32, imm: i32) { write_reg32(r1, sbb32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_4_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| and32(x, imm)) }\npub unsafe fn instr32_81_4_reg(r1: i32, imm: i32) { write_reg32(r1, and32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_5_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| sub32(x, imm)) }\npub unsafe fn instr32_81_5_reg(r1: i32, imm: i32) { write_reg32(r1, sub32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_6_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| xor32(x, imm)) }\npub unsafe fn instr32_81_6_reg(r1: i32, imm: i32) { write_reg32(r1, xor32(read_reg32(r1), imm)); }\npub unsafe fn instr32_81_7_reg(r: i32, imm: i32) { cmp32(read_reg32(r), imm); }\npub unsafe fn instr32_81_7_mem(addr: i32, imm: i32) {\n    cmp32(return_on_pagefault!(safe_read32s(addr)), imm);\n}\npub unsafe fn instr_82_0_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| add8(x, imm)) }\npub unsafe fn instr_82_0_reg(r1: i32, imm: i32) { write_reg8(r1, add8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_1_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| or8(x, imm)) }\npub unsafe fn instr_82_1_reg(r1: i32, imm: i32) { write_reg8(r1, or8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_2_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| adc8(x, imm)) }\npub unsafe fn instr_82_2_reg(r1: i32, imm: i32) { write_reg8(r1, adc8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_3_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| sbb8(x, imm)) }\npub unsafe fn instr_82_3_reg(r1: i32, imm: i32) { write_reg8(r1, sbb8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_4_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| and8(x, imm)) }\npub unsafe fn instr_82_4_reg(r1: i32, imm: i32) { write_reg8(r1, and8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_5_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| sub8(x, imm)) }\npub unsafe fn instr_82_5_reg(r1: i32, imm: i32) { write_reg8(r1, sub8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_6_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| xor8(x, imm)) }\npub unsafe fn instr_82_6_reg(r1: i32, imm: i32) { write_reg8(r1, xor8(read_reg8(r1), imm)); }\npub unsafe fn instr_82_7_reg(r: i32, imm: i32) { cmp8(read_reg8(r), imm); }\npub unsafe fn instr_82_7_mem(addr: i32, imm: i32) {\n    cmp8(return_on_pagefault!(safe_read8(addr)), imm);\n}\npub unsafe fn instr16_83_0_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| add16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_0_reg(r1: i32, imm: i32) {\n    write_reg16(r1, add16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_1_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| or16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_1_reg(r1: i32, imm: i32) {\n    write_reg16(r1, or16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_2_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| adc16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_2_reg(r1: i32, imm: i32) {\n    write_reg16(r1, adc16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_3_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| sbb16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_3_reg(r1: i32, imm: i32) {\n    write_reg16(r1, sbb16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_4_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| and16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_4_reg(r1: i32, imm: i32) {\n    write_reg16(r1, and16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_5_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| sub16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_5_reg(r1: i32, imm: i32) {\n    write_reg16(r1, sub16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_6_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| xor16(x, imm & 0xFFFF))\n}\npub unsafe fn instr16_83_6_reg(r1: i32, imm: i32) {\n    write_reg16(r1, xor16(read_reg16(r1), imm & 0xFFFF));\n}\npub unsafe fn instr16_83_7_reg(r: i32, imm: i32) { cmp16(read_reg16(r), imm & 0xFFFF); }\npub unsafe fn instr16_83_7_mem(addr: i32, imm: i32) {\n    cmp16(return_on_pagefault!(safe_read16(addr)), imm & 0xFFFF);\n}\n\npub unsafe fn instr32_83_0_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| add32(x, imm)) }\npub unsafe fn instr32_83_0_reg(r1: i32, imm: i32) { write_reg32(r1, add32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_1_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| or32(x, imm)) }\npub unsafe fn instr32_83_1_reg(r1: i32, imm: i32) { write_reg32(r1, or32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_2_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| adc32(x, imm)) }\npub unsafe fn instr32_83_2_reg(r1: i32, imm: i32) { write_reg32(r1, adc32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_3_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| sbb32(x, imm)) }\npub unsafe fn instr32_83_3_reg(r1: i32, imm: i32) { write_reg32(r1, sbb32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_4_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| and32(x, imm)) }\npub unsafe fn instr32_83_4_reg(r1: i32, imm: i32) { write_reg32(r1, and32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_5_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| sub32(x, imm)) }\npub unsafe fn instr32_83_5_reg(r1: i32, imm: i32) { write_reg32(r1, sub32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_6_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| xor32(x, imm)) }\npub unsafe fn instr32_83_6_reg(r1: i32, imm: i32) { write_reg32(r1, xor32(read_reg32(r1), imm)); }\npub unsafe fn instr32_83_7_reg(r: i32, imm: i32) { cmp32(read_reg32(r), imm); }\npub unsafe fn instr32_83_7_mem(addr: i32, imm: i32) {\n    cmp32(return_on_pagefault!(safe_read32s(addr)), imm);\n}\n\npub unsafe fn instr_84_mem(addr: i32, r: i32) {\n    test8(return_on_pagefault!(safe_read8(addr)), read_reg8(r));\n}\npub unsafe fn instr_84_reg(r1: i32, r: i32) { test8(read_reg8(r1), read_reg8(r)); }\npub unsafe fn instr16_85_mem(addr: i32, r: i32) {\n    test16(return_on_pagefault!(safe_read16(addr)), read_reg16(r));\n}\npub unsafe fn instr16_85_reg(r1: i32, r: i32) { test16(read_reg16(r1), read_reg16(r)); }\npub unsafe fn instr32_85_mem(addr: i32, r: i32) {\n    test32(return_on_pagefault!(safe_read32s(addr)), read_reg32(r));\n}\npub unsafe fn instr32_85_reg(r1: i32, r: i32) { test32(read_reg32(r1), read_reg32(r)); }\npub unsafe fn instr_86_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| xchg8(x, r)) }\npub unsafe fn instr_86_reg(r1: i32, r: i32) { write_reg8(r1, xchg8(read_reg8(r1), r)); }\npub unsafe fn instr16_87_mem(addr: i32, r: i32) { safe_read_write16(addr, &|x| xchg16(x, r)) }\npub unsafe fn instr16_87_reg(r1: i32, r: i32) { write_reg16(r1, xchg16(read_reg16(r1), r)); }\npub unsafe fn instr32_87_mem(addr: i32, r: i32) { safe_read_write32(addr, &|x| xchg32(x, r)) }\npub unsafe fn instr32_87_reg(r1: i32, r: i32) { write_reg32(r1, xchg32(read_reg32(r1), r)); }\npub unsafe fn instr_88_reg(r2: i32, r: i32) { write_reg8(r2, read_reg8(r)); }\npub unsafe fn instr_88_mem(addr: i32, r: i32) {\n    return_on_pagefault!(safe_write8(addr, read_reg8(r)));\n}\npub unsafe fn instr16_89_reg(r2: i32, r: i32) { write_reg16(r2, read_reg16(r)); }\npub unsafe fn instr16_89_mem(addr: i32, r: i32) {\n    return_on_pagefault!(safe_write16(addr, read_reg16(r)));\n}\npub unsafe fn instr32_89_reg(r2: i32, r: i32) { write_reg32(r2, read_reg32(r)); }\npub unsafe fn instr32_89_mem(addr: i32, r: i32) {\n    return_on_pagefault!(safe_write32(addr, read_reg32(r)));\n}\npub unsafe fn instr_8A_mem(addr: i32, r: i32) {\n    write_reg8(r, return_on_pagefault!(safe_read8(addr)));\n}\npub unsafe fn instr_8A_reg(r1: i32, r: i32) { write_reg8(r, read_reg8(r1)); }\npub unsafe fn instr16_8B_mem(addr: i32, r: i32) {\n    write_reg16(r, return_on_pagefault!(safe_read16(addr)));\n}\npub unsafe fn instr16_8B_reg(r1: i32, r: i32) { write_reg16(r, read_reg16(r1)); }\npub unsafe fn instr32_8B_mem(addr: i32, r: i32) {\n    write_reg32(r, return_on_pagefault!(safe_read32s(addr)));\n}\npub unsafe fn instr32_8B_reg(r1: i32, r: i32) { write_reg32(r, read_reg32(r1)); }\n\npub unsafe fn instr_8C_check_sreg(seg: i32) -> bool {\n    if seg >= 6 {\n        dbg_log!(\"mov sreg #ud\");\n        trigger_ud();\n        return false;\n    }\n    else {\n        return true;\n    };\n}\npub unsafe fn instr16_8C_reg(r: i32, seg: i32) {\n    if instr_8C_check_sreg(seg) {\n        write_reg16(r, *sreg.offset(seg as isize) as i32);\n    };\n}\npub unsafe fn instr16_8C_mem(addr: i32, seg: i32) {\n    if instr_8C_check_sreg(seg) {\n        return_on_pagefault!(safe_write16(addr, *sreg.offset(seg as isize) as i32));\n    };\n}\npub unsafe fn instr32_8C_reg(r: i32, seg: i32) {\n    if instr_8C_check_sreg(seg) {\n        write_reg32(r, *sreg.offset(seg as isize) as i32);\n    };\n}\npub unsafe fn instr32_8C_mem(addr: i32, seg: i32) {\n    if instr_8C_check_sreg(seg) {\n        return_on_pagefault!(safe_write16(addr, *sreg.offset(seg as isize) as i32));\n    };\n}\n\npub unsafe fn instr16_8D_reg(_r: i32, _r2: i32) {\n    dbg_log!(\"lea #ud\");\n    trigger_ud();\n}\npub unsafe fn instr16_8D_mem(modrm_byte: i32, r: i32) {\n    // lea\n    *prefixes |= prefix::SEG_PREFIX_ZERO;\n    if let Ok(addr) = modrm_resolve(modrm_byte) {\n        write_reg16(r, addr);\n    }\n    *prefixes = 0;\n}\npub unsafe fn instr32_8D_reg(_r: i32, _r2: i32) {\n    dbg_log!(\"lea #ud\");\n    trigger_ud();\n}\npub unsafe fn instr32_8D_mem(modrm_byte: i32, r: i32) {\n    // lea\n    // override prefix, so modrm_resolve does not return the segment part\n    *prefixes |= prefix::SEG_PREFIX_ZERO;\n    if let Ok(addr) = modrm_resolve(modrm_byte) {\n        write_reg32(r, addr);\n    }\n    *prefixes = 0;\n}\n\n#[no_mangle]\npub unsafe fn instr_8E_mem(addr: i32, r: i32) {\n    if r == ES || r == SS || r == DS || r == FS || r == GS {\n        if !switch_seg(r, return_on_pagefault!(safe_read16(addr))) {\n            return;\n        }\n    }\n    else {\n        dbg_log!(\"mov sreg #ud\");\n        trigger_ud();\n    }\n}\n#[no_mangle]\npub unsafe fn instr_8E_reg(r1: i32, r: i32) {\n    if r == ES || r == SS || r == DS || r == FS || r == GS {\n        switch_seg(r, read_reg16(r1));\n    }\n    else {\n        dbg_log!(\"mov sreg #ud\");\n        trigger_ud();\n    }\n}\n\npub unsafe fn instr16_8F_0_mem(modrm_byte: i32) {\n    // pop\n    // Update esp *before* resolving the address\n    adjust_stack_reg(2);\n    match modrm_resolve(modrm_byte) {\n        Err(()) => {\n            // a pagefault happened, reset esp\n            adjust_stack_reg(-2);\n        },\n        Ok(addr) => {\n            adjust_stack_reg(-2);\n            let stack_value = return_on_pagefault!(safe_read16(get_stack_pointer(0)));\n            return_on_pagefault!(safe_write16(addr, stack_value));\n            adjust_stack_reg(2);\n        },\n    }\n}\npub unsafe fn instr16_8F_0_reg(r: i32) { write_reg16(r, return_on_pagefault!(pop16())); }\npub unsafe fn instr32_8F_0_mem(modrm_byte: i32) {\n    // Update esp *before* resolving the address\n    adjust_stack_reg(4);\n    match modrm_resolve(modrm_byte) {\n        Err(()) => {\n            // a pagefault happened, reset esp\n            adjust_stack_reg(-4);\n        },\n        Ok(addr) => {\n            adjust_stack_reg(-4);\n            let stack_value = return_on_pagefault!(safe_read32s(get_stack_pointer(0)));\n            return_on_pagefault!(safe_write32(addr, stack_value));\n            adjust_stack_reg(4);\n        },\n    }\n}\npub unsafe fn instr32_8F_0_reg(r: i32) { write_reg32(r, return_on_pagefault!(pop32s())); }\n\npub unsafe fn instr_90() {}\npub unsafe fn instr16_91() { xchg16r(CX); }\npub unsafe fn instr32_91() { xchg32r(ECX); }\npub unsafe fn instr16_92() { xchg16r(DX); }\npub unsafe fn instr32_92() { xchg32r(EDX); }\npub unsafe fn instr16_93() { xchg16r(BX); }\npub unsafe fn instr32_93() { xchg32r(EBX); }\npub unsafe fn instr16_94() { xchg16r(SP); }\npub unsafe fn instr32_94() { xchg32r(ESP); }\npub unsafe fn instr16_95() { xchg16r(BP); }\npub unsafe fn instr32_95() { xchg32r(EBP); }\npub unsafe fn instr16_96() { xchg16r(SI); }\npub unsafe fn instr32_96() { xchg32r(ESI); }\npub unsafe fn instr16_97() { xchg16r(DI); }\npub unsafe fn instr32_97() { xchg32r(EDI); }\n\npub unsafe fn instr16_98() { write_reg16(AX, read_reg8(AL) << 24 >> 24); }\npub unsafe fn instr32_98() { write_reg32(EAX, read_reg16(AX) as i16 as i32); }\npub unsafe fn instr16_99() { write_reg16(DX, read_reg16(AX) as i16 as i32 >> 15); }\npub unsafe fn instr32_99() { write_reg32(EDX, read_reg32(EAX) >> 31); }\n\n#[no_mangle]\npub unsafe fn instr16_9A(new_ip: i32, new_cs: i32) {\n    // callf\n    far_jump(new_ip, new_cs, true, false);\n}\n#[no_mangle]\npub unsafe fn instr32_9A(new_ip: i32, new_cs: i32) {\n    if !*protected_mode || vm86_mode() {\n        if 0 != new_ip as u32 & 0xFFFF0000 {\n            dbg_assert!(false);\n        }\n    }\n    far_jump(new_ip, new_cs, true, true);\n}\n#[no_mangle]\npub unsafe fn instr_9B() {\n    // fwait: check for pending fpu exceptions\n    if *cr & (CR0_MP | CR0_TS) == CR0_MP | CR0_TS {\n        // Note: Different from task_switch_test\n        // Triggers when TS and MP bits are set (EM bit is ignored)\n        trigger_nm();\n    }\n    else {\n        fwait();\n    };\n}\nunsafe fn instr_pushf_popf_check() -> bool { 0 != *flags & FLAG_VM && getiopl() < 3 }\npub unsafe fn instr16_9C() {\n    // pushf\n    if instr_pushf_popf_check() {\n        dbg_assert!(*protected_mode);\n        dbg_log!(\"pushf #gp\");\n        trigger_gp(0);\n    }\n    else {\n        return_on_pagefault!(push16(get_eflags() & 0xFFFF));\n    };\n}\npub unsafe fn instr32_9C() {\n    // pushf\n    if instr_pushf_popf_check() {\n        // trap to virtual 8086 monitor\n        dbg_assert!(*protected_mode);\n        dbg_log!(\"pushf #gp\");\n        trigger_gp(0);\n    }\n    else {\n        // vm and rf flag are cleared in image stored on the stack\n        return_on_pagefault!(push32(get_eflags() & 0xFCFFFF));\n    };\n}\n\npub unsafe fn instr16_9D() {\n    // popf\n    if instr_pushf_popf_check() {\n        dbg_log!(\"popf #gp\");\n        trigger_gp(0);\n        return;\n    }\n    let old_eflags = *flags;\n    update_eflags(*flags & !0xFFFF | return_on_pagefault!(pop16()));\n    if old_eflags & FLAG_INTERRUPT == 0 && *flags & FLAG_INTERRUPT != 0 {\n        handle_irqs();\n    }\n}\npub unsafe fn instr32_9D() {\n    // popf\n    if instr_pushf_popf_check() {\n        dbg_log!(\"popf #gp\");\n        trigger_gp(0);\n        return;\n    }\n    let old_eflags = *flags;\n    update_eflags(return_on_pagefault!(pop32s()));\n    if old_eflags & FLAG_INTERRUPT == 0 && *flags & FLAG_INTERRUPT != 0 {\n        handle_irqs();\n    }\n}\n\npub unsafe fn instr_9E() {\n    // sahf\n    *flags = *flags & !255 | read_reg8(AH);\n    *flags = *flags & FLAGS_MASK | FLAGS_DEFAULT;\n    *flags_changed &= !255;\n}\npub unsafe fn instr_9F() {\n    // lahf\n    write_reg8(AH, get_eflags());\n}\n\npub unsafe fn instr_A0(moffs: i32) {\n    // mov\n    let data = return_on_pagefault!(safe_read8(return_on_pagefault!(get_seg_prefix_ds(moffs))));\n    write_reg8(AL, data);\n}\npub unsafe fn instr16_A1(moffs: i32) {\n    // mov\n    let data = return_on_pagefault!(safe_read16(return_on_pagefault!(get_seg_prefix_ds(moffs))));\n    write_reg16(AX, data);\n}\npub unsafe fn instr32_A1(moffs: i32) {\n    let data = return_on_pagefault!(safe_read32s(return_on_pagefault!(get_seg_prefix_ds(moffs))));\n    write_reg32(EAX, data);\n}\npub unsafe fn instr_A2(moffs: i32) {\n    // mov\n    return_on_pagefault!(safe_write8(\n        return_on_pagefault!(get_seg_prefix_ds(moffs)),\n        read_reg8(AL)\n    ));\n}\npub unsafe fn instr16_A3(moffs: i32) {\n    // mov\n    return_on_pagefault!(safe_write16(\n        return_on_pagefault!(get_seg_prefix_ds(moffs)),\n        read_reg16(AX)\n    ));\n}\npub unsafe fn instr32_A3(moffs: i32) {\n    return_on_pagefault!(safe_write32(\n        return_on_pagefault!(get_seg_prefix_ds(moffs)),\n        read_reg32(EAX)\n    ));\n}\n\npub unsafe fn instr_A4() { movsb_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F2A4() { movsb_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F3A4() { movsb_rep(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr16_A5() { movsw_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_A5() { movsd_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F2A5() { movsw_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F3A5() { movsw_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F2A5() { movsd_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F3A5() { movsd_rep(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr_A6() { cmpsb_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F2A6() { cmpsb_repnz(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F3A6() { cmpsb_repz(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_A7() { cmpsw_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_A7() { cmpsd_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F2A7() { cmpsw_repnz(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F3A7() { cmpsw_repz(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F2A7() { cmpsd_repnz(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F3A7() { cmpsd_repz(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr_A8(imm8: i32) { test8(read_reg8(AL), imm8); }\npub unsafe fn instr16_A9(imm16: i32) { test16(read_reg16(AX), imm16); }\npub unsafe fn instr32_A9(imm32: i32) { test32(read_reg32(EAX), imm32); }\n\npub unsafe fn instr_AA() { stosb_no_rep(is_asize_32()); }\npub unsafe fn instr_F2AA() { stosb_rep(is_asize_32()); }\npub unsafe fn instr_F3AA() { stosb_rep(is_asize_32()); }\n\npub unsafe fn instr16_AB() { stosw_no_rep(is_asize_32()); }\npub unsafe fn instr32_AB() { stosd_no_rep(is_asize_32()); }\npub unsafe fn instr16_F2AB() { stosw_rep(is_asize_32()); }\npub unsafe fn instr16_F3AB() { stosw_rep(is_asize_32()); }\npub unsafe fn instr32_F2AB() { stosd_rep(is_asize_32()); }\npub unsafe fn instr32_F3AB() { stosd_rep(is_asize_32()); }\n\npub unsafe fn instr_AC() { lodsb_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F2AC() { lodsb_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr_F3AC() { lodsb_rep(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr16_AD() { lodsw_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_AD() { lodsd_no_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F2AD() { lodsw_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr16_F3AD() { lodsw_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F2AD() { lodsd_rep(is_asize_32(), segment_prefix(DS)); }\npub unsafe fn instr32_F3AD() { lodsd_rep(is_asize_32(), segment_prefix(DS)); }\n\npub unsafe fn instr_AE() { scasb_no_rep(is_asize_32()); }\npub unsafe fn instr_F2AE() { scasb_repnz(is_asize_32()); }\npub unsafe fn instr_F3AE() { scasb_repz(is_asize_32()); }\n\npub unsafe fn instr16_AF() { scasw_no_rep(is_asize_32()); }\npub unsafe fn instr32_AF() { scasd_no_rep(is_asize_32()); }\npub unsafe fn instr16_F2AF() { scasw_repnz(is_asize_32()); }\npub unsafe fn instr16_F3AF() { scasw_repz(is_asize_32()); }\npub unsafe fn instr32_F2AF() { scasd_repnz(is_asize_32()); }\npub unsafe fn instr32_F3AF() { scasd_repz(is_asize_32()); }\n\npub unsafe fn instr_B0(imm8: i32) { write_reg8(AL, imm8); }\npub unsafe fn instr_B1(imm8: i32) { write_reg8(CL, imm8); }\npub unsafe fn instr_B2(imm8: i32) { write_reg8(DL, imm8); }\npub unsafe fn instr_B3(imm8: i32) { write_reg8(BL, imm8); }\npub unsafe fn instr_B4(imm8: i32) { write_reg8(AH, imm8); }\npub unsafe fn instr_B5(imm8: i32) { write_reg8(CH, imm8); }\npub unsafe fn instr_B6(imm8: i32) { write_reg8(DH, imm8); }\npub unsafe fn instr_B7(imm8: i32) { write_reg8(BH, imm8); }\npub unsafe fn instr16_B8(imm: i32) { write_reg16(AX, imm); }\npub unsafe fn instr32_B8(imm: i32) { write_reg32(EAX, imm); }\npub unsafe fn instr16_B9(imm: i32) { write_reg16(CX, imm); }\npub unsafe fn instr32_B9(imm: i32) { write_reg32(ECX, imm); }\npub unsafe fn instr16_BA(imm: i32) { write_reg16(DX, imm); }\npub unsafe fn instr32_BA(imm: i32) { write_reg32(EDX, imm); }\npub unsafe fn instr16_BB(imm: i32) { write_reg16(BX, imm); }\npub unsafe fn instr32_BB(imm: i32) { write_reg32(EBX, imm); }\npub unsafe fn instr16_BC(imm: i32) { write_reg16(SP, imm); }\npub unsafe fn instr32_BC(imm: i32) { write_reg32(ESP, imm); }\npub unsafe fn instr16_BD(imm: i32) { write_reg16(BP, imm); }\npub unsafe fn instr32_BD(imm: i32) { write_reg32(EBP, imm); }\npub unsafe fn instr16_BE(imm: i32) { write_reg16(SI, imm); }\npub unsafe fn instr32_BE(imm: i32) { write_reg32(ESI, imm); }\npub unsafe fn instr16_BF(imm: i32) { write_reg16(DI, imm); }\npub unsafe fn instr32_BF(imm: i32) { write_reg32(EDI, imm); }\n\npub unsafe fn instr_C0_0_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| rol8(x, imm & 31)) }\npub unsafe fn instr_C0_0_reg(r1: i32, imm: i32) { write_reg8(r1, rol8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_1_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| ror8(x, imm & 31)) }\npub unsafe fn instr_C0_1_reg(r1: i32, imm: i32) { write_reg8(r1, ror8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_2_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| rcl8(x, imm & 31)) }\npub unsafe fn instr_C0_2_reg(r1: i32, imm: i32) { write_reg8(r1, rcl8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_3_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| rcr8(x, imm & 31)) }\npub unsafe fn instr_C0_3_reg(r1: i32, imm: i32) { write_reg8(r1, rcr8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_4_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| shl8(x, imm & 31)) }\npub unsafe fn instr_C0_4_reg(r1: i32, imm: i32) { write_reg8(r1, shl8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_5_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| shr8(x, imm & 31)) }\npub unsafe fn instr_C0_5_reg(r1: i32, imm: i32) { write_reg8(r1, shr8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_6_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| shl8(x, imm & 31)) }\npub unsafe fn instr_C0_6_reg(r1: i32, imm: i32) { write_reg8(r1, shl8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr_C0_7_mem(addr: i32, imm: i32) { safe_read_write8(addr, &|x| sar8(x, imm & 31)) }\npub unsafe fn instr_C0_7_reg(r1: i32, imm: i32) { write_reg8(r1, sar8(read_reg8(r1), imm & 31)); }\npub unsafe fn instr16_C1_0_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| rol16(x, imm & 31))\n}\npub unsafe fn instr16_C1_0_reg(r1: i32, imm: i32) {\n    write_reg16(r1, rol16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_1_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| ror16(x, imm & 31))\n}\npub unsafe fn instr16_C1_1_reg(r1: i32, imm: i32) {\n    write_reg16(r1, ror16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_2_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| rcl16(x, imm & 31))\n}\npub unsafe fn instr16_C1_2_reg(r1: i32, imm: i32) {\n    write_reg16(r1, rcl16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_3_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| rcr16(x, imm & 31))\n}\npub unsafe fn instr16_C1_3_reg(r1: i32, imm: i32) {\n    write_reg16(r1, rcr16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_4_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| shl16(x, imm & 31))\n}\npub unsafe fn instr16_C1_4_reg(r1: i32, imm: i32) {\n    write_reg16(r1, shl16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_5_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| shr16(x, imm & 31))\n}\npub unsafe fn instr16_C1_5_reg(r1: i32, imm: i32) {\n    write_reg16(r1, shr16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_6_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| shl16(x, imm & 31))\n}\npub unsafe fn instr16_C1_6_reg(r1: i32, imm: i32) {\n    write_reg16(r1, shl16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr16_C1_7_mem(addr: i32, imm: i32) {\n    safe_read_write16(addr, &|x| sar16(x, imm & 31))\n}\npub unsafe fn instr16_C1_7_reg(r1: i32, imm: i32) {\n    write_reg16(r1, sar16(read_reg16(r1), imm & 31));\n}\npub unsafe fn instr32_C1_0_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| rol32(x, imm & 31))\n}\npub unsafe fn instr32_C1_0_reg(r1: i32, imm: i32) {\n    write_reg32(r1, rol32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_1_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| ror32(x, imm & 31))\n}\npub unsafe fn instr32_C1_1_reg(r1: i32, imm: i32) {\n    write_reg32(r1, ror32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_2_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| rcl32(x, imm & 31))\n}\npub unsafe fn instr32_C1_2_reg(r1: i32, imm: i32) {\n    write_reg32(r1, rcl32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_3_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| rcr32(x, imm & 31))\n}\npub unsafe fn instr32_C1_3_reg(r1: i32, imm: i32) {\n    write_reg32(r1, rcr32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_4_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| shl32(x, imm & 31))\n}\npub unsafe fn instr32_C1_4_reg(r1: i32, imm: i32) {\n    write_reg32(r1, shl32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_5_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| shr32(x, imm & 31))\n}\npub unsafe fn instr32_C1_5_reg(r1: i32, imm: i32) {\n    write_reg32(r1, shr32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_6_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| shl32(x, imm & 31))\n}\npub unsafe fn instr32_C1_6_reg(r1: i32, imm: i32) {\n    write_reg32(r1, shl32(read_reg32(r1), imm & 31));\n}\npub unsafe fn instr32_C1_7_mem(addr: i32, imm: i32) {\n    safe_read_write32(addr, &|x| sar32(x, imm & 31))\n}\npub unsafe fn instr32_C1_7_reg(r1: i32, imm: i32) {\n    write_reg32(r1, sar32(read_reg32(r1), imm & 31));\n}\n\npub unsafe fn instr16_C2(imm16: i32) {\n    // retn\n    let cs = get_seg_cs();\n    *instruction_pointer = cs + return_on_pagefault!(pop16());\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n    adjust_stack_reg(imm16);\n}\npub unsafe fn instr32_C2(imm16: i32) {\n    // retn\n    let cs = get_seg_cs();\n    let ip = return_on_pagefault!(pop32s());\n    dbg_assert!(*is_32 || ip < 0x10000);\n    *instruction_pointer = cs + ip;\n    adjust_stack_reg(imm16);\n}\npub unsafe fn instr16_C3() {\n    // retn\n    let cs = get_seg_cs();\n    *instruction_pointer = cs + return_on_pagefault!(pop16());\n}\npub unsafe fn instr32_C3() {\n    // retn\n    let cs = get_seg_cs();\n    let ip = return_on_pagefault!(pop32s());\n    dbg_assert!(*is_32 || ip < 0x10000);\n    *instruction_pointer = cs + ip;\n}\n\n#[no_mangle]\npub unsafe fn instr16_C4_reg(_unused1: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_C4_mem(addr: i32, r: i32) { lss16(addr, r, ES); }\n#[no_mangle]\npub unsafe fn instr32_C4_reg(_unused1: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_C4_mem(addr: i32, r: i32) { lss32(addr, r, ES); }\n#[no_mangle]\npub unsafe fn instr16_C5_reg(_unused1: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_C5_mem(addr: i32, r: i32) { lss16(addr, r, DS); }\n#[no_mangle]\npub unsafe fn instr32_C5_reg(_unused1: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_C5_mem(addr: i32, r: i32) { lss32(addr, r, DS); }\n\npub unsafe fn instr_C6_0_reg(r: i32, imm: i32) { write_reg8(r, imm); }\npub unsafe fn instr_C6_0_mem(addr: i32, imm: i32) {\n    return_on_pagefault!(safe_write8(addr, imm));\n}\npub unsafe fn instr16_C7_0_reg(r: i32, imm: i32) { write_reg16(r, imm); }\npub unsafe fn instr16_C7_0_mem(addr: i32, imm: i32) {\n    return_on_pagefault!(safe_write16(addr, imm));\n}\npub unsafe fn instr32_C7_0_reg(r: i32, imm: i32) { write_reg32(r, imm); }\npub unsafe fn instr32_C7_0_mem(addr: i32, imm: i32) {\n    return_on_pagefault!(safe_write32(addr, imm));\n}\n\n#[no_mangle]\npub unsafe fn instr16_C8(size: i32, nesting: i32) { enter16(size, nesting); }\n#[no_mangle]\npub unsafe fn instr32_C8(size: i32, nesting: i32) { enter32(size, nesting); }\n\npub unsafe fn instr16_C9() {\n    // leave\n    let old_vbp = if *stack_size_32 { read_reg32(EBP) } else { read_reg16(BP) };\n    let new_bp = return_on_pagefault!(safe_read16(get_seg_ss() + old_vbp));\n    set_stack_reg(old_vbp + 2);\n    write_reg16(BP, new_bp);\n}\npub unsafe fn instr32_C9() {\n    let old_vbp = if *stack_size_32 { read_reg32(EBP) } else { read_reg16(BP) };\n    let new_ebp = return_on_pagefault!(safe_read32s(get_seg_ss() + old_vbp));\n    set_stack_reg(old_vbp + 4);\n    write_reg32(EBP, new_ebp);\n}\n#[no_mangle]\npub unsafe fn instr16_CA(imm16: i32) {\n    // retf\n    let ip = return_on_pagefault!(safe_read16(get_stack_pointer(0)));\n    let cs = return_on_pagefault!(safe_read16(get_stack_pointer(2)));\n    far_return(ip, cs, imm16, false);\n}\n#[no_mangle]\npub unsafe fn instr32_CA(imm16: i32) {\n    // retf\n    let ip = return_on_pagefault!(safe_read32s(get_stack_pointer(0)));\n    let cs = return_on_pagefault!(safe_read32s(get_stack_pointer(4))) & 0xFFFF;\n    far_return(ip, cs, imm16, true);\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\n#[no_mangle]\npub unsafe fn instr16_CB() {\n    // retf\n    let ip = return_on_pagefault!(safe_read16(get_stack_pointer(0)));\n    let cs = return_on_pagefault!(safe_read16(get_stack_pointer(2)));\n    far_return(ip, cs, 0, false);\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\n#[no_mangle]\npub unsafe fn instr32_CB() {\n    // retf\n    let ip = return_on_pagefault!(safe_read32s(get_stack_pointer(0)));\n    let cs = return_on_pagefault!(safe_read32s(get_stack_pointer(4))) & 0xFFFF;\n    far_return(ip, cs, 0, true);\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\n#[no_mangle]\npub unsafe fn instr_CC() {\n    // INT3\n    // TODO: inhibit iopl checks\n    dbg_log!(\"INT3\");\n    call_interrupt_vector(3, true, None);\n}\n#[no_mangle]\npub unsafe fn instr_CD(imm8: i32) {\n    // INT\n    call_interrupt_vector(imm8, true, None);\n}\n#[no_mangle]\npub unsafe fn instr_CE() {\n    // INTO\n    dbg_log!(\"INTO\");\n    if getof() {\n        // TODO: inhibit iopl checks\n        call_interrupt_vector(CPU_EXCEPTION_OF, true, None);\n    };\n}\n#[no_mangle]\npub unsafe fn instr16_CF() {\n    // iret\n    iret16();\n}\n#[no_mangle]\npub unsafe fn instr32_CF() { iret32(); }\n\npub unsafe fn instr_D0_0_mem(addr: i32) { safe_read_write8(addr, &|x| rol8(x, 1)) }\npub unsafe fn instr_D0_0_reg(r1: i32) { write_reg8(r1, rol8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_1_mem(addr: i32) { safe_read_write8(addr, &|x| ror8(x, 1)) }\npub unsafe fn instr_D0_1_reg(r1: i32) { write_reg8(r1, ror8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_2_mem(addr: i32) { safe_read_write8(addr, &|x| rcl8(x, 1)) }\npub unsafe fn instr_D0_2_reg(r1: i32) { write_reg8(r1, rcl8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_3_mem(addr: i32) { safe_read_write8(addr, &|x| rcr8(x, 1)) }\npub unsafe fn instr_D0_3_reg(r1: i32) { write_reg8(r1, rcr8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_4_mem(addr: i32) { safe_read_write8(addr, &|x| shl8(x, 1)) }\npub unsafe fn instr_D0_4_reg(r1: i32) { write_reg8(r1, shl8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_5_mem(addr: i32) { safe_read_write8(addr, &|x| shr8(x, 1)) }\npub unsafe fn instr_D0_5_reg(r1: i32) { write_reg8(r1, shr8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_6_mem(addr: i32) { safe_read_write8(addr, &|x| shl8(x, 1)) }\npub unsafe fn instr_D0_6_reg(r1: i32) { write_reg8(r1, shl8(read_reg8(r1), 1)); }\npub unsafe fn instr_D0_7_mem(addr: i32) { safe_read_write8(addr, &|x| sar8(x, 1)) }\npub unsafe fn instr_D0_7_reg(r1: i32) { write_reg8(r1, sar8(read_reg8(r1), 1)); }\npub unsafe fn instr16_D1_0_mem(addr: i32) { safe_read_write16(addr, &|x| rol16(x, 1)) }\npub unsafe fn instr16_D1_0_reg(r1: i32) { write_reg16(r1, rol16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_1_mem(addr: i32) { safe_read_write16(addr, &|x| ror16(x, 1)) }\npub unsafe fn instr16_D1_1_reg(r1: i32) { write_reg16(r1, ror16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_2_mem(addr: i32) { safe_read_write16(addr, &|x| rcl16(x, 1)) }\npub unsafe fn instr16_D1_2_reg(r1: i32) { write_reg16(r1, rcl16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_3_mem(addr: i32) { safe_read_write16(addr, &|x| rcr16(x, 1)) }\npub unsafe fn instr16_D1_3_reg(r1: i32) { write_reg16(r1, rcr16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_4_mem(addr: i32) { safe_read_write16(addr, &|x| shl16(x, 1)) }\npub unsafe fn instr16_D1_4_reg(r1: i32) { write_reg16(r1, shl16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_5_mem(addr: i32) { safe_read_write16(addr, &|x| shr16(x, 1)) }\npub unsafe fn instr16_D1_5_reg(r1: i32) { write_reg16(r1, shr16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_6_mem(addr: i32) { safe_read_write16(addr, &|x| shl16(x, 1)) }\npub unsafe fn instr16_D1_6_reg(r1: i32) { write_reg16(r1, shl16(read_reg16(r1), 1)); }\npub unsafe fn instr16_D1_7_mem(addr: i32) { safe_read_write16(addr, &|x| sar16(x, 1)) }\npub unsafe fn instr16_D1_7_reg(r1: i32) { write_reg16(r1, sar16(read_reg16(r1), 1)); }\npub unsafe fn instr32_D1_0_mem(addr: i32) { safe_read_write32(addr, &|x| rol32(x, 1)) }\npub unsafe fn instr32_D1_0_reg(r1: i32) { write_reg32(r1, rol32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_1_mem(addr: i32) { safe_read_write32(addr, &|x| ror32(x, 1)) }\npub unsafe fn instr32_D1_1_reg(r1: i32) { write_reg32(r1, ror32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_2_mem(addr: i32) { safe_read_write32(addr, &|x| rcl32(x, 1)) }\npub unsafe fn instr32_D1_2_reg(r1: i32) { write_reg32(r1, rcl32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_3_mem(addr: i32) { safe_read_write32(addr, &|x| rcr32(x, 1)) }\npub unsafe fn instr32_D1_3_reg(r1: i32) { write_reg32(r1, rcr32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_4_mem(addr: i32) { safe_read_write32(addr, &|x| shl32(x, 1)) }\npub unsafe fn instr32_D1_4_reg(r1: i32) { write_reg32(r1, shl32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_5_mem(addr: i32) { safe_read_write32(addr, &|x| shr32(x, 1)) }\npub unsafe fn instr32_D1_5_reg(r1: i32) { write_reg32(r1, shr32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_6_mem(addr: i32) { safe_read_write32(addr, &|x| shl32(x, 1)) }\npub unsafe fn instr32_D1_6_reg(r1: i32) { write_reg32(r1, shl32(read_reg32(r1), 1)); }\npub unsafe fn instr32_D1_7_mem(addr: i32) { safe_read_write32(addr, &|x| sar32(x, 1)) }\npub unsafe fn instr32_D1_7_reg(r1: i32) { write_reg32(r1, sar32(read_reg32(r1), 1)); }\npub unsafe fn instr_D2_0_mem(addr: i32) { safe_read_write8(addr, &|x| rol8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_0_reg(r1: i32) { write_reg8(r1, rol8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_1_mem(addr: i32) { safe_read_write8(addr, &|x| ror8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_1_reg(r1: i32) { write_reg8(r1, ror8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_2_mem(addr: i32) { safe_read_write8(addr, &|x| rcl8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_2_reg(r1: i32) { write_reg8(r1, rcl8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_3_mem(addr: i32) { safe_read_write8(addr, &|x| rcr8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_3_reg(r1: i32) { write_reg8(r1, rcr8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_4_mem(addr: i32) { safe_read_write8(addr, &|x| shl8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_4_reg(r1: i32) { write_reg8(r1, shl8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_5_mem(addr: i32) { safe_read_write8(addr, &|x| shr8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_5_reg(r1: i32) { write_reg8(r1, shr8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_6_mem(addr: i32) { safe_read_write8(addr, &|x| shl8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_6_reg(r1: i32) { write_reg8(r1, shl8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr_D2_7_mem(addr: i32) { safe_read_write8(addr, &|x| sar8(x, read_reg8(CL) & 31)) }\npub unsafe fn instr_D2_7_reg(r1: i32) { write_reg8(r1, sar8(read_reg8(r1), read_reg8(CL) & 31)); }\npub unsafe fn instr16_D3_0_mem(addr: i32) {\n    safe_read_write16(addr, &|x| rol16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_0_reg(r1: i32) {\n    write_reg16(r1, rol16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_1_mem(addr: i32) {\n    safe_read_write16(addr, &|x| ror16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_1_reg(r1: i32) {\n    write_reg16(r1, ror16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_2_mem(addr: i32) {\n    safe_read_write16(addr, &|x| rcl16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_2_reg(r1: i32) {\n    write_reg16(r1, rcl16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_3_mem(addr: i32) {\n    safe_read_write16(addr, &|x| rcr16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_3_reg(r1: i32) {\n    write_reg16(r1, rcr16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_4_mem(addr: i32) {\n    safe_read_write16(addr, &|x| shl16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_4_reg(r1: i32) {\n    write_reg16(r1, shl16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_5_mem(addr: i32) {\n    safe_read_write16(addr, &|x| shr16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_5_reg(r1: i32) {\n    write_reg16(r1, shr16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_6_mem(addr: i32) {\n    safe_read_write16(addr, &|x| shl16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_6_reg(r1: i32) {\n    write_reg16(r1, shl16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr16_D3_7_mem(addr: i32) {\n    safe_read_write16(addr, &|x| sar16(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr16_D3_7_reg(r1: i32) {\n    write_reg16(r1, sar16(read_reg16(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_0_mem(addr: i32) {\n    safe_read_write32(addr, &|x| rol32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_0_reg(r1: i32) {\n    write_reg32(r1, rol32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_1_mem(addr: i32) {\n    safe_read_write32(addr, &|x| ror32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_1_reg(r1: i32) {\n    write_reg32(r1, ror32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_2_mem(addr: i32) {\n    safe_read_write32(addr, &|x| rcl32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_2_reg(r1: i32) {\n    write_reg32(r1, rcl32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_3_mem(addr: i32) {\n    safe_read_write32(addr, &|x| rcr32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_3_reg(r1: i32) {\n    write_reg32(r1, rcr32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_4_mem(addr: i32) {\n    safe_read_write32(addr, &|x| shl32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_4_reg(r1: i32) {\n    write_reg32(r1, shl32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_5_mem(addr: i32) {\n    safe_read_write32(addr, &|x| shr32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_5_reg(r1: i32) {\n    write_reg32(r1, shr32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_6_mem(addr: i32) {\n    safe_read_write32(addr, &|x| shl32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_6_reg(r1: i32) {\n    write_reg32(r1, shl32(read_reg32(r1), read_reg8(CL) & 31));\n}\npub unsafe fn instr32_D3_7_mem(addr: i32) {\n    safe_read_write32(addr, &|x| sar32(x, read_reg8(CL) & 31))\n}\npub unsafe fn instr32_D3_7_reg(r1: i32) {\n    write_reg32(r1, sar32(read_reg32(r1), read_reg8(CL) & 31));\n}\n\n#[no_mangle]\npub unsafe fn instr_D4(arg: i32) { bcd_aam(arg); }\n#[no_mangle]\npub unsafe fn instr_D5(arg: i32) { bcd_aad(arg); }\n#[no_mangle]\npub unsafe fn instr_D6() {\n    // salc\n    write_reg8(AL, -(getcf() as i32));\n}\npub unsafe fn instr_D7() {\n    // xlat\n    dbg_assert!(!in_jit);\n    if is_asize_32() {\n        write_reg8(\n            AL,\n            return_on_pagefault!(safe_read8(\n                return_on_pagefault!(get_seg_prefix(DS)) + read_reg32(EBX) + read_reg8(AL),\n            )),\n        )\n    }\n    else {\n        write_reg8(\n            AL,\n            return_on_pagefault!(safe_read8(\n                return_on_pagefault!(get_seg_prefix(DS))\n                    + (read_reg16(BX) + read_reg8(AL) & 0xFFFF),\n            )),\n        )\n    };\n}\n\npub unsafe fn instr_D8_0_mem(addr: i32) { fpu_fadd(0, return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_0_reg(r: i32) { fpu_fadd(0, fpu_get_sti(r)); }\npub unsafe fn instr_D8_1_mem(addr: i32) { fpu_fmul(0, return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_1_reg(r: i32) { fpu_fmul(0, fpu_get_sti(r)); }\npub unsafe fn instr_D8_2_mem(addr: i32) { fpu_fcom(return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_2_reg(r: i32) { fpu_fcom(fpu_get_sti(r)); }\npub unsafe fn instr_D8_3_mem(addr: i32) { fpu_fcomp(return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_3_reg(r: i32) { fpu_fcomp(fpu_get_sti(r)); }\npub unsafe fn instr_D8_4_mem(addr: i32) { fpu_fsub(0, return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_4_reg(r: i32) { fpu_fsub(0, fpu_get_sti(r)); }\npub unsafe fn instr_D8_5_mem(addr: i32) { fpu_fsubr(0, return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_5_reg(r: i32) { fpu_fsubr(0, fpu_get_sti(r)); }\npub unsafe fn instr_D8_6_mem(addr: i32) { fpu_fdiv(0, return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_6_reg(r: i32) { fpu_fdiv(0, fpu_get_sti(r)); }\npub unsafe fn instr_D8_7_mem(addr: i32) { fpu_fdivr(0, return_on_pagefault!(fpu_load_m32(addr))); }\npub unsafe fn instr_D8_7_reg(r: i32) { fpu_fdivr(0, fpu_get_sti(r)); }\n\npub unsafe fn instr16_D9_0_mem(addr: i32) { fpu_fldm32(addr); }\npub unsafe fn instr16_D9_0_reg(r: i32) { fpu_push(fpu_get_sti(r)); }\npub unsafe fn instr16_D9_1_mem(_addr: i32) {\n    dbg_log!(\"d9/1\");\n    trigger_ud();\n}\npub unsafe fn instr16_D9_1_reg(r: i32) { fpu_fxch(r); }\npub unsafe fn instr16_D9_2_mem(addr: i32) { fpu_fstm32(addr); }\npub unsafe fn instr16_D9_2_reg(r: i32) {\n    if r != 0 {\n        trigger_ud();\n    };\n}\npub unsafe fn instr16_D9_3_mem(addr: i32) { fpu_fstm32p(addr); }\npub unsafe fn instr16_D9_3_reg(r: i32) { fpu_fstp(r) }\n#[no_mangle]\npub unsafe fn instr16_D9_4_mem(addr: i32) { fpu_fldenv16(addr); }\npub unsafe fn instr32_D9_4_mem(addr: i32) { fpu_fldenv32(addr); }\n#[no_mangle]\npub unsafe fn instr16_D9_4_reg(r: i32) {\n    match r {\n        0 => fpu_fchs(),\n        1 => fpu_fabs(),\n        4 => fpu_ftst(),\n        5 => fpu_fxam(),\n        _ => {\n            dbg_log!(\"{:x}\", r);\n            trigger_ud();\n        },\n    };\n}\n#[no_mangle]\npub unsafe fn instr16_D9_5_mem(addr: i32) { fpu_fldcw(addr); }\n#[no_mangle]\npub unsafe fn instr16_D9_5_reg(r: i32) {\n    // fld1/fldl2t/fldl2e/fldpi/fldlg2/fldln2/fldz\n    match r {\n        0 => fpu_push(F80::ONE),\n        1 => fpu_push(F80::LN_10 / F80::LN_2),\n        2 => fpu_push(F80::LOG2_E),\n        3 => fpu_push(F80::PI),\n        4 => fpu_push(F80::LN_2 / F80::LN_10),\n        5 => fpu_push(F80::LN_2),\n        6 => fpu_push(F80::ZERO),\n        7 => {\n            dbg_log!(\"d9/5/7\");\n            trigger_ud();\n        },\n        _ => {},\n    };\n}\npub unsafe fn instr16_D9_6_mem(addr: i32) { fpu_fstenv16(addr); }\npub unsafe fn instr32_D9_6_mem(addr: i32) { fpu_fstenv32(addr); }\n#[no_mangle]\npub unsafe fn instr16_D9_6_reg(r: i32) {\n    match r {\n        0 => fpu_f2xm1(),\n        1 => fpu_fyl2x(),\n        2 => fpu_fptan(),\n        3 => fpu_fpatan(),\n        4 => fpu_fxtract(),\n        5 => fpu_fprem(true), // fprem1\n        6 => fpu_fdecstp(),\n        7 => fpu_fincstp(),\n        _ => {\n            dbg_assert!(false);\n        },\n    };\n}\npub unsafe fn instr16_D9_7_mem(addr: i32) { fpu_fstcw(addr); }\n#[no_mangle]\npub unsafe fn instr16_D9_7_reg(r: i32) {\n    match r {\n        0 => fpu_fprem(false),\n        1 => fpu_fyl2xp1(),\n        2 => fpu_fsqrt(),\n        3 => fpu_fsincos(),\n        4 => fpu_frndint(),\n        5 => fpu_fscale(),\n        6 => fpu_fsin(),\n        7 => fpu_fcos(),\n        _ => {\n            dbg_assert!(false);\n        },\n    };\n}\n\npub unsafe fn instr32_D9_0_reg(r: i32) { instr16_D9_0_reg(r) }\npub unsafe fn instr32_D9_1_reg(r: i32) { instr16_D9_1_reg(r) }\npub unsafe fn instr32_D9_2_reg(r: i32) { instr16_D9_2_reg(r) }\npub unsafe fn instr32_D9_3_reg(r: i32) { instr16_D9_3_reg(r) }\npub unsafe fn instr32_D9_4_reg(r: i32) { instr16_D9_4_reg(r) }\npub unsafe fn instr32_D9_5_reg(r: i32) { instr16_D9_5_reg(r) }\npub unsafe fn instr32_D9_6_reg(r: i32) { instr16_D9_6_reg(r) }\npub unsafe fn instr32_D9_7_reg(r: i32) { instr16_D9_7_reg(r) }\n\npub unsafe fn instr32_D9_0_mem(r: i32) { instr16_D9_0_mem(r) }\npub unsafe fn instr32_D9_1_mem(r: i32) { instr16_D9_1_mem(r) }\npub unsafe fn instr32_D9_2_mem(r: i32) { instr16_D9_2_mem(r) }\npub unsafe fn instr32_D9_3_mem(r: i32) { instr16_D9_3_mem(r) }\npub unsafe fn instr32_D9_5_mem(r: i32) { instr16_D9_5_mem(r) }\npub unsafe fn instr32_D9_7_mem(r: i32) { instr16_D9_7_mem(r) }\n\npub unsafe fn instr_DA_0_mem(addr: i32) { fpu_fadd(0, return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_1_mem(addr: i32) { fpu_fmul(0, return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_2_mem(addr: i32) { fpu_fcom(return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_3_mem(addr: i32) { fpu_fcomp(return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_4_mem(addr: i32) { fpu_fsub(0, return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_5_mem(addr: i32) { fpu_fsubr(0, return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_6_mem(addr: i32) { fpu_fdiv(0, return_on_pagefault!(fpu_load_i32(addr))); }\npub unsafe fn instr_DA_7_mem(addr: i32) { fpu_fdivr(0, return_on_pagefault!(fpu_load_i32(addr))); }\n#[no_mangle]\npub unsafe fn instr_DA_0_reg(r: i32) { fpu_fcmovcc(test_b(), r); }\n#[no_mangle]\npub unsafe fn instr_DA_1_reg(r: i32) { fpu_fcmovcc(test_z(), r); }\n#[no_mangle]\npub unsafe fn instr_DA_2_reg(r: i32) { fpu_fcmovcc(test_be(), r); }\n#[no_mangle]\npub unsafe fn instr_DA_3_reg(r: i32) { fpu_fcmovcc(test_p(), r); }\npub unsafe fn instr_DA_4_reg(_r: i32) { trigger_ud(); }\npub unsafe fn instr_DA_5_reg(r: i32) {\n    if r == 1 {\n        fpu_fucompp();\n    }\n    else {\n        trigger_ud();\n    };\n}\npub unsafe fn instr_DA_6_reg(_r: i32) { trigger_ud(); }\npub unsafe fn instr_DA_7_reg(_r: i32) { trigger_ud(); }\n\npub unsafe fn instr_DB_0_mem(addr: i32) { fpu_fildm32(addr); }\npub unsafe fn instr_DB_1_mem(addr: i32) { fpu_fisttpm32(addr); }\npub unsafe fn instr_DB_2_mem(addr: i32) { fpu_fistm32(addr); }\npub unsafe fn instr_DB_3_mem(addr: i32) { fpu_fistm32p(addr); }\n#[no_mangle]\npub unsafe fn instr_DB_4_mem(_addr: i32) { trigger_ud(); }\npub unsafe fn instr_DB_5_mem(addr: i32) { fpu_fldm80(addr); }\npub unsafe fn instr_DB_6_mem(_addr: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_DB_7_mem(addr: i32) { fpu_fst80p(addr); }\n#[no_mangle]\npub unsafe fn instr_DB_0_reg(r: i32) { fpu_fcmovcc(!test_b(), r); }\n#[no_mangle]\npub unsafe fn instr_DB_1_reg(r: i32) { fpu_fcmovcc(!test_z(), r); }\n#[no_mangle]\npub unsafe fn instr_DB_2_reg(r: i32) { fpu_fcmovcc(!test_be(), r); }\n#[no_mangle]\npub unsafe fn instr_DB_3_reg(r: i32) { fpu_fcmovcc(!test_p(), r); }\n#[no_mangle]\npub unsafe fn instr_DB_4_reg(r: i32) {\n    if r == 3 {\n        fpu_finit();\n    }\n    else if r == 4 || r == 1 || r == 0 {\n        // fsetpm, fdisi, fneni; treated as nop\n    }\n    else if r == 2 {\n        fpu_fclex();\n    }\n    else {\n        trigger_ud();\n    };\n}\npub unsafe fn instr_DB_5_reg(r: i32) { fpu_fucomi(r); }\npub unsafe fn instr_DB_6_reg(r: i32) { fpu_fcomi(r); }\n#[no_mangle]\npub unsafe fn instr_DB_7_reg(_r: i32) { trigger_ud(); }\n\npub unsafe fn instr_DC_0_mem(addr: i32) { fpu_fadd(0, return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_1_mem(addr: i32) { fpu_fmul(0, return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_2_mem(addr: i32) { fpu_fcom(return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_3_mem(addr: i32) { fpu_fcomp(return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_4_mem(addr: i32) { fpu_fsub(0, return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_5_mem(addr: i32) { fpu_fsubr(0, return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_6_mem(addr: i32) { fpu_fdiv(0, return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_7_mem(addr: i32) { fpu_fdivr(0, return_on_pagefault!(fpu_load_m64(addr))); }\npub unsafe fn instr_DC_0_reg(r: i32) { fpu_fadd(r, fpu_get_sti(r)); }\npub unsafe fn instr_DC_1_reg(r: i32) { fpu_fmul(r, fpu_get_sti(r)); }\npub unsafe fn instr_DC_2_reg(r: i32) { fpu_fcom(fpu_get_sti(r)); }\npub unsafe fn instr_DC_3_reg(r: i32) { fpu_fcomp(fpu_get_sti(r)); }\npub unsafe fn instr_DC_4_reg(r: i32) { fpu_fsub(r, fpu_get_sti(r)); }\npub unsafe fn instr_DC_5_reg(r: i32) { fpu_fsubr(r, fpu_get_sti(r)); }\npub unsafe fn instr_DC_6_reg(r: i32) { fpu_fdiv(r, fpu_get_sti(r)); }\npub unsafe fn instr_DC_7_reg(r: i32) { fpu_fdivr(r, fpu_get_sti(r)); }\n\npub unsafe fn instr16_DD_0_mem(addr: i32) { fpu_fldm64(addr); }\npub unsafe fn instr16_DD_1_mem(addr: i32) { fpu_fisttpm64(addr); }\npub unsafe fn instr16_DD_2_mem(addr: i32) { fpu_fstm64(addr); }\npub unsafe fn instr16_DD_3_mem(addr: i32) { fpu_fstm64p(addr); }\n#[no_mangle]\npub unsafe fn instr16_DD_4_mem(addr: i32) { fpu_frstor16(addr); }\n#[no_mangle]\npub unsafe fn instr32_DD_4_mem(addr: i32) { fpu_frstor32(addr); }\npub unsafe fn instr16_DD_5_mem(_addr: i32) {\n    dbg_log!(\"dd/5\");\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr16_DD_6_mem(addr: i32) { fpu_fsave16(addr); }\n#[no_mangle]\npub unsafe fn instr32_DD_6_mem(addr: i32) { fpu_fsave32(addr); }\n#[no_mangle]\npub unsafe fn instr16_DD_7_mem(addr: i32) { fpu_fnstsw_mem(addr); }\npub unsafe fn instr16_DD_0_reg(r: i32) { fpu_ffree(r); }\npub unsafe fn instr16_DD_1_reg(r: i32) { fpu_fxch(r) }\npub unsafe fn instr16_DD_2_reg(r: i32) { fpu_fst(r); }\npub unsafe fn instr16_DD_3_reg(r: i32) { fpu_fstp(r); }\n#[no_mangle]\npub unsafe fn instr16_DD_4_reg(r: i32) { fpu_fucom(r); }\npub unsafe fn instr16_DD_5_reg(r: i32) { fpu_fucomp(r); }\n#[no_mangle]\npub unsafe fn instr16_DD_6_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_DD_7_reg(_r: i32) { trigger_ud(); }\n\npub unsafe fn instr32_DD_0_reg(r: i32) { instr16_DD_0_reg(r) }\n#[no_mangle]\npub unsafe fn instr32_DD_1_reg(r: i32) { instr16_DD_1_reg(r) }\npub unsafe fn instr32_DD_2_reg(r: i32) { instr16_DD_2_reg(r) }\npub unsafe fn instr32_DD_3_reg(r: i32) { instr16_DD_3_reg(r) }\n#[no_mangle]\npub unsafe fn instr32_DD_4_reg(r: i32) { instr16_DD_4_reg(r) }\npub unsafe fn instr32_DD_5_reg(r: i32) { instr16_DD_5_reg(r) }\n#[no_mangle]\npub unsafe fn instr32_DD_6_reg(r: i32) { instr16_DD_6_reg(r) }\n#[no_mangle]\npub unsafe fn instr32_DD_7_reg(r: i32) { instr16_DD_7_reg(r) }\n\npub unsafe fn instr32_DD_0_mem(r: i32) { instr16_DD_0_mem(r) }\n#[no_mangle]\npub unsafe fn instr32_DD_1_mem(r: i32) { instr16_DD_1_mem(r) }\npub unsafe fn instr32_DD_2_mem(r: i32) { instr16_DD_2_mem(r) }\npub unsafe fn instr32_DD_3_mem(r: i32) { instr16_DD_3_mem(r) }\npub unsafe fn instr32_DD_5_mem(r: i32) { instr16_DD_5_mem(r) }\n#[no_mangle]\npub unsafe fn instr32_DD_7_mem(r: i32) { instr16_DD_7_mem(r) }\n\n#[no_mangle]\npub unsafe fn instr_DE_0_mem(addr: i32) { fpu_fadd(0, return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_1_mem(addr: i32) { fpu_fmul(0, return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_2_mem(addr: i32) { fpu_fcom(return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_3_mem(addr: i32) { fpu_fcomp(return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_4_mem(addr: i32) { fpu_fsub(0, return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_5_mem(addr: i32) { fpu_fsubr(0, return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_6_mem(addr: i32) { fpu_fdiv(0, return_on_pagefault!(fpu_load_i16(addr))); }\n#[no_mangle]\npub unsafe fn instr_DE_7_mem(addr: i32) { fpu_fdivr(0, return_on_pagefault!(fpu_load_i16(addr))); }\n\n#[no_mangle]\npub unsafe fn instr_DE_0_reg(r: i32) {\n    fpu_fadd(r, fpu_get_sti(r));\n    fpu_pop();\n}\npub unsafe fn instr_DE_1_reg(r: i32) {\n    fpu_fmul(r, fpu_get_sti(r));\n    fpu_pop();\n}\npub unsafe fn instr_DE_2_reg(r: i32) {\n    fpu_fcom(fpu_get_sti(r));\n    fpu_pop();\n}\npub unsafe fn instr_DE_3_reg(r: i32) {\n    if r == 1 {\n        fpu_fcomp(fpu_get_sti(r));\n        fpu_pop();\n    }\n    else {\n        trigger_ud();\n    }\n}\npub unsafe fn instr_DE_4_reg(r: i32) {\n    fpu_fsub(r, fpu_get_sti(r));\n    fpu_pop();\n}\npub unsafe fn instr_DE_5_reg(r: i32) {\n    fpu_fsubr(r, fpu_get_sti(r));\n    fpu_pop();\n}\npub unsafe fn instr_DE_6_reg(r: i32) {\n    fpu_fdiv(r, fpu_get_sti(r));\n    fpu_pop();\n}\npub unsafe fn instr_DE_7_reg(r: i32) {\n    fpu_fdivr(r, fpu_get_sti(r));\n    fpu_pop();\n}\n\n#[no_mangle]\npub unsafe fn instr_DF_0_mem(addr: i32) { fpu_fildm16(addr) }\npub unsafe fn instr_DF_1_mem(addr: i32) { fpu_fisttpm16(addr); }\npub unsafe fn instr_DF_2_mem(addr: i32) { fpu_fistm16(addr); }\npub unsafe fn instr_DF_3_mem(addr: i32) { fpu_fistm16p(addr); }\npub unsafe fn instr_DF_4_mem(_addr: i32) {\n    dbg_log!(\"fbld\");\n    fpu_unimpl();\n}\npub unsafe fn instr_DF_5_mem(addr: i32) { fpu_fildm64(addr); }\npub unsafe fn instr_DF_6_mem(addr: i32) { fpu_fbstp(addr); }\npub unsafe fn instr_DF_7_mem(addr: i32) { fpu_fistm64p(addr); }\n\n#[no_mangle]\npub unsafe fn instr_DF_0_reg(r: i32) {\n    fpu_ffree(r);\n    fpu_pop();\n}\npub unsafe fn instr_DF_1_reg(r: i32) { fpu_fxch(r) }\npub unsafe fn instr_DF_2_reg(r: i32) { fpu_fstp(r); }\npub unsafe fn instr_DF_3_reg(r: i32) { fpu_fstp(r); }\npub unsafe fn instr_DF_4_reg(r: i32) {\n    if r == 0 {\n        fpu_fnstsw_reg();\n    }\n    else {\n        trigger_ud();\n    };\n}\npub unsafe fn instr_DF_5_reg(r: i32) { fpu_fucomip(r); }\npub unsafe fn instr_DF_6_reg(r: i32) { fpu_fcomip(r); }\npub unsafe fn instr_DF_7_reg(_r: i32) { trigger_ud(); }\n\npub unsafe fn instr16_E0(imm8s: i32) { loopne16(imm8s); }\npub unsafe fn instr16_E1(imm8s: i32) { loope16(imm8s); }\npub unsafe fn instr16_E2(imm8s: i32) { loop16(imm8s); }\npub unsafe fn instr16_E3(imm8s: i32) { jcxz16(imm8s); }\npub unsafe fn instr32_E0(imm8s: i32) { loopne32(imm8s); }\npub unsafe fn instr32_E1(imm8s: i32) { loope32(imm8s); }\npub unsafe fn instr32_E2(imm8s: i32) { loop32(imm8s); }\npub unsafe fn instr32_E3(imm8s: i32) { jcxz32(imm8s); }\n\n#[no_mangle]\npub unsafe fn instr_E4(port: i32) {\n    if test_privileges_for_io(port, 1) {\n        write_reg8(AL, io_port_read8(port));\n    }\n}\n#[no_mangle]\npub unsafe fn instr16_E5(port: i32) {\n    if test_privileges_for_io(port, 2) {\n        write_reg16(AX, io_port_read16(port));\n    }\n}\n#[no_mangle]\npub unsafe fn instr32_E5(port: i32) {\n    if test_privileges_for_io(port, 4) {\n        write_reg32(EAX, io_port_read32(port));\n    }\n}\n#[no_mangle]\npub unsafe fn instr_E6(port: i32) {\n    if test_privileges_for_io(port, 1) {\n        io_port_write8(port, read_reg8(AL));\n    }\n}\n#[no_mangle]\npub unsafe fn instr16_E7(port: i32) {\n    if test_privileges_for_io(port, 2) {\n        io_port_write16(port, read_reg16(AX));\n    }\n}\n#[no_mangle]\npub unsafe fn instr32_E7(port: i32) {\n    if test_privileges_for_io(port, 4) {\n        io_port_write32(port, read_reg32(EAX));\n    }\n}\n\npub unsafe fn instr16_E8(imm16: i32) {\n    // call\n    return_on_pagefault!(push16(get_real_eip()));\n    jmp_rel16(imm16);\n}\npub unsafe fn instr32_E8(imm32s: i32) {\n    // call\n    return_on_pagefault!(push32(get_real_eip()));\n    *instruction_pointer = *instruction_pointer + imm32s;\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\npub unsafe fn instr16_E9(imm16: i32) {\n    // jmp\n    jmp_rel16(imm16);\n}\npub unsafe fn instr32_E9(imm32s: i32) {\n    // jmp\n    *instruction_pointer = *instruction_pointer + imm32s;\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\n\n#[no_mangle]\npub unsafe fn instr16_EA(new_ip: i32, cs: i32) {\n    // jmpf\n    far_jump(new_ip, cs, false, false);\n}\n#[no_mangle]\npub unsafe fn instr32_EA(new_ip: i32, cs: i32) {\n    // jmpf\n    far_jump(new_ip, cs, false, true);\n}\n\npub unsafe fn instr16_EB(imm8: i32) {\n    // jmp near\n    jmp_rel16(imm8);\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\npub unsafe fn instr32_EB(imm8: i32) {\n    // jmp near\n    *instruction_pointer = *instruction_pointer + imm8;\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\n\n#[no_mangle]\npub unsafe fn instr_EC() {\n    let port = read_reg16(DX);\n    if test_privileges_for_io(port, 1) {\n        write_reg8(AL, io_port_read8(port));\n    }\n}\n#[no_mangle]\npub unsafe fn instr16_ED() {\n    let port = read_reg16(DX);\n    if test_privileges_for_io(port, 2) {\n        write_reg16(AX, io_port_read16(port));\n    }\n}\n#[no_mangle]\npub unsafe fn instr32_ED() {\n    let port = read_reg16(DX);\n    if test_privileges_for_io(port, 4) {\n        write_reg32(EAX, io_port_read32(port));\n    }\n}\n#[no_mangle]\npub unsafe fn instr_EE() {\n    let port = read_reg16(DX);\n    if test_privileges_for_io(port, 1) {\n        io_port_write8(port, read_reg8(AL));\n    }\n}\n#[no_mangle]\npub unsafe fn instr16_EF() {\n    let port = read_reg16(DX);\n    if test_privileges_for_io(port, 2) {\n        io_port_write16(port, read_reg16(AX));\n    }\n}\n#[no_mangle]\npub unsafe fn instr32_EF() {\n    let port = read_reg16(DX);\n    if test_privileges_for_io(port, 4) {\n        io_port_write32(port, read_reg32(EAX));\n    }\n}\n\npub unsafe fn instr_F0() {\n    // lock\n    if false {\n        dbg_log!(\"lock\");\n    }\n    // TODO\n    // This triggers UD when used with\n    // some instructions that don't write to memory\n    run_prefix_instruction();\n}\n\n#[no_mangle]\npub unsafe fn instr_F1() {\n    // INT1\n    // https://code.google.com/p/corkami/wiki/x86oddities#IceBP\n    dbg_assert!(false);\n}\n\npub unsafe fn instr_F2() {\n    // repnz\n    dbg_assert!(*prefixes & prefix::PREFIX_MASK_REP == 0);\n    *prefixes |= prefix::PREFIX_REPNZ;\n    run_prefix_instruction();\n    *prefixes = 0;\n}\npub unsafe fn instr_F3() {\n    // repz\n    dbg_assert!(*prefixes & prefix::PREFIX_MASK_REP == 0);\n    *prefixes |= prefix::PREFIX_REPZ;\n    run_prefix_instruction();\n    *prefixes = 0;\n}\n\n#[no_mangle]\npub unsafe fn instr_F4() {\n    if 0 != *cpl {\n        dbg_log!(\"#gp hlt with cpl != 0\");\n        trigger_gp(0);\n        return;\n    }\n\n    *in_hlt = true;\n\n    // Try an hlt loop right now: This will run timer interrupts, and if one is\n    // due it will immediately call call_interrupt_vector and continue\n    // execution without an unnecessary cycle through do_run\n    if *flags & FLAG_INTERRUPT != 0 {\n        js::run_hardware_timers(*acpi_enabled, js::microtick());\n        handle_irqs();\n    }\n    else {\n        // execution can never resume (until NMIs are supported)\n        js::cpu_event_halt();\n    }\n}\n#[no_mangle]\npub unsafe fn instr_F5() {\n    // cmc\n    *flags = (*flags | 1) ^ getcf() as i32;\n    *flags_changed &= !1;\n}\n\npub unsafe fn instr_F6_0_mem(addr: i32, imm: i32) {\n    test8(return_on_pagefault!(safe_read8(addr)), imm);\n}\npub unsafe fn instr_F6_0_reg(r1: i32, imm: i32) { test8(read_reg8(r1), imm); }\npub unsafe fn instr_F6_1_mem(addr: i32, imm: i32) {\n    test8(return_on_pagefault!(safe_read8(addr)), imm);\n}\npub unsafe fn instr_F6_1_reg(r1: i32, imm: i32) { test8(read_reg8(r1), imm); }\n\n#[no_mangle]\npub unsafe fn instr_F6_2_mem(addr: i32) { safe_read_write8(addr, &|x| !x & 0xFF) }\n#[no_mangle]\npub unsafe fn instr_F6_2_reg(r1: i32) { write_reg8(r1, !read_reg8(r1)); }\n#[no_mangle]\npub unsafe fn instr_F6_3_mem(addr: i32) { safe_read_write8(addr, &|x| neg8(x)) }\n#[no_mangle]\npub unsafe fn instr_F6_3_reg(r1: i32) { write_reg8(r1, neg8(read_reg8(r1))); }\n#[no_mangle]\npub unsafe fn instr_F6_4_mem(addr: i32) { mul8(return_on_pagefault!(safe_read8(addr))); }\n#[no_mangle]\npub unsafe fn instr_F6_4_reg(r1: i32) { mul8(read_reg8(r1)); }\n#[no_mangle]\npub unsafe fn instr_F6_5_mem(addr: i32) {\n    imul8(return_on_pagefault!(safe_read8(addr)) << 24 >> 24);\n}\n#[no_mangle]\npub unsafe fn instr_F6_5_reg(r1: i32) { imul8(read_reg8(r1) << 24 >> 24); }\n#[no_mangle]\npub unsafe fn instr_F6_6_mem(addr: i32) { div8(return_on_pagefault!(safe_read8(addr)) as u32); }\n#[no_mangle]\npub unsafe fn instr_F6_6_reg(r1: i32) { div8(read_reg8(r1) as u32); }\n#[no_mangle]\npub unsafe fn instr_F6_7_mem(addr: i32) {\n    idiv8(return_on_pagefault!(safe_read8(addr)) << 24 >> 24);\n}\n#[no_mangle]\npub unsafe fn instr_F6_7_reg(r1: i32) { idiv8(read_reg8(r1) << 24 >> 24); }\n\npub unsafe fn instr16_F7_0_mem(addr: i32, imm: i32) {\n    test16(return_on_pagefault!(safe_read16(addr)), imm);\n}\npub unsafe fn instr16_F7_0_reg(r1: i32, imm: i32) { test16(read_reg16(r1), imm); }\npub unsafe fn instr16_F7_1_mem(addr: i32, imm: i32) {\n    test16(return_on_pagefault!(safe_read16(addr)), imm);\n}\npub unsafe fn instr16_F7_1_reg(r1: i32, imm: i32) { test16(read_reg16(r1), imm); }\npub unsafe fn instr16_F7_2_mem(addr: i32) { safe_read_write16(addr, &|x| !x & 0xFFFF) }\npub unsafe fn instr16_F7_2_reg(r1: i32) { write_reg16(r1, !read_reg16(r1)); }\npub unsafe fn instr16_F7_3_mem(addr: i32) { safe_read_write16(addr, &|x| neg16(x)) }\npub unsafe fn instr16_F7_3_reg(r1: i32) { write_reg16(r1, neg16(read_reg16(r1))); }\npub unsafe fn instr16_F7_4_mem(addr: i32) { mul16(return_on_pagefault!(safe_read16(addr)) as u32); }\npub unsafe fn instr16_F7_4_reg(r1: i32) { mul16(read_reg16(r1) as u32); }\npub unsafe fn instr16_F7_5_mem(addr: i32) {\n    imul16(return_on_pagefault!(safe_read16(addr)) << 16 >> 16);\n}\npub unsafe fn instr16_F7_5_reg(r1: i32) { imul16(read_reg16(r1) << 16 >> 16); }\npub unsafe fn instr16_F7_6_mem(addr: i32) { div16(return_on_pagefault!(safe_read16(addr)) as u32); }\npub unsafe fn instr16_F7_6_reg(r1: i32) { div16(read_reg16(r1) as u32); }\npub unsafe fn instr16_F7_7_mem(addr: i32) {\n    idiv16(return_on_pagefault!(safe_read16(addr)) << 16 >> 16);\n}\npub unsafe fn instr16_F7_7_reg(r1: i32) { idiv16(read_reg16(r1) << 16 >> 16); }\n\npub unsafe fn instr32_F7_0_mem(addr: i32, imm: i32) {\n    test32(return_on_pagefault!(safe_read32s(addr)), imm);\n}\npub unsafe fn instr32_F7_0_reg(r1: i32, imm: i32) { test32(read_reg32(r1), imm); }\npub unsafe fn instr32_F7_1_mem(addr: i32, imm: i32) {\n    test32(return_on_pagefault!(safe_read32s(addr)), imm);\n}\npub unsafe fn instr32_F7_1_reg(r1: i32, imm: i32) { test32(read_reg32(r1), imm); }\npub unsafe fn instr32_F7_2_mem(addr: i32) { safe_read_write32(addr, &|x| !x) }\npub unsafe fn instr32_F7_2_reg(r1: i32) { write_reg32(r1, !read_reg32(r1)); }\npub unsafe fn instr32_F7_3_mem(addr: i32) { safe_read_write32(addr, &|x| neg32(x)) }\npub unsafe fn instr32_F7_3_reg(r1: i32) { write_reg32(r1, neg32(read_reg32(r1))); }\npub unsafe fn instr32_F7_4_mem(addr: i32) { mul32(return_on_pagefault!(safe_read32s(addr))); }\npub unsafe fn instr32_F7_4_reg(r1: i32) { mul32(read_reg32(r1)); }\npub unsafe fn instr32_F7_5_mem(addr: i32) { imul32(return_on_pagefault!(safe_read32s(addr))); }\npub unsafe fn instr32_F7_5_reg(r1: i32) { imul32(read_reg32(r1)); }\npub unsafe fn instr32_F7_6_mem(addr: i32) {\n    div32(return_on_pagefault!(safe_read32s(addr)) as u32);\n}\npub unsafe fn instr32_F7_6_reg(r1: i32) { div32(read_reg32(r1) as u32); }\npub unsafe fn instr32_F7_7_mem(addr: i32) { idiv32(return_on_pagefault!(safe_read32s(addr))); }\npub unsafe fn instr32_F7_7_reg(r1: i32) { idiv32(read_reg32(r1)); }\n\npub unsafe fn instr_F8() {\n    // clc\n    *flags &= !FLAG_CARRY;\n    *flags_changed &= !1;\n}\npub unsafe fn instr_F9() {\n    // stc\n    *flags |= FLAG_CARRY;\n    *flags_changed &= !1;\n}\n#[no_mangle]\npub unsafe fn instr_FA_without_fault() -> bool {\n    // cli\n    if !*protected_mode\n        || if 0 != *flags & FLAG_VM { getiopl() == 3 } else { getiopl() >= *cpl as i32 }\n    {\n        *flags &= !FLAG_INTERRUPT;\n        return true;\n    }\n    else if false\n        && getiopl() < 3\n        && if 0 != *flags & FLAG_VM {\n            0 != *cr.offset(4) & CR4_VME\n        }\n        else {\n            *cpl == 3 && 0 != *cr.offset(4) & CR4_PVI\n        }\n    {\n        *flags &= !FLAG_VIF;\n        return true;\n    }\n    else {\n        dbg_log!(\"cli #gp\");\n        return false;\n    };\n}\npub unsafe fn instr_FA() {\n    if !instr_FA_without_fault() {\n        trigger_gp(0);\n    }\n}\n\n#[no_mangle]\npub unsafe fn instr_FB_without_fault() -> bool {\n    // sti\n    if !*protected_mode\n        || if 0 != *flags & FLAG_VM { getiopl() == 3 } else { getiopl() >= *cpl as i32 }\n    {\n        *flags |= FLAG_INTERRUPT;\n        return true;\n    }\n    else if false\n        && getiopl() < 3\n        && *flags & FLAG_VIP == 0\n        && if 0 != *flags & FLAG_VM {\n            0 != *cr.offset(4) & CR4_VME\n        }\n        else {\n            *cpl == 3 && 0 != *cr.offset(4) & CR4_PVI\n        }\n    {\n        *flags |= FLAG_VIF;\n        return true;\n    }\n    else {\n        dbg_log!(\"sti #gp\");\n        return false;\n    };\n}\npub unsafe fn instr_FB() {\n    if !instr_FB_without_fault() {\n        trigger_gp(0);\n    }\n    else {\n        *prefixes = 0;\n        *previous_ip = *instruction_pointer;\n        *instruction_counter += 1;\n        run_instruction(return_on_pagefault!(read_imm8()) | (is_osize_32() as i32) << 8);\n\n        handle_irqs();\n    }\n}\n\npub unsafe fn instr_FC() {\n    // cld\n    *flags &= !FLAG_DIRECTION;\n}\npub unsafe fn instr_FD() {\n    // std\n    *flags |= FLAG_DIRECTION;\n}\n\npub unsafe fn instr_FE_0_mem(addr: i32) { safe_read_write8(addr, &|x| inc8(x)) }\npub unsafe fn instr_FE_0_reg(r1: i32) { write_reg8(r1, inc8(read_reg8(r1))); }\npub unsafe fn instr_FE_1_mem(addr: i32) { safe_read_write8(addr, &|x| dec8(x)) }\npub unsafe fn instr_FE_1_reg(r1: i32) { write_reg8(r1, dec8(read_reg8(r1))); }\npub unsafe fn instr16_FF_0_mem(addr: i32) { safe_read_write16(addr, &|x| inc16(x)) }\npub unsafe fn instr16_FF_0_reg(r1: i32) { write_reg16(r1, inc16(read_reg16(r1))); }\npub unsafe fn instr16_FF_1_mem(addr: i32) { safe_read_write16(addr, &|x| dec16(x)) }\npub unsafe fn instr16_FF_1_reg(r1: i32) { write_reg16(r1, dec16(read_reg16(r1))); }\npub unsafe fn instr16_FF_2_helper(data: i32) {\n    // call near\n    let cs = get_seg_cs();\n    return_on_pagefault!(push16(get_real_eip()));\n    *instruction_pointer = cs + data;\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\npub unsafe fn instr16_FF_2_mem(addr: i32) {\n    instr16_FF_2_helper(return_on_pagefault!(safe_read16(addr)));\n}\npub unsafe fn instr16_FF_2_reg(r1: i32) { instr16_FF_2_helper(read_reg16(r1)); }\n\n#[no_mangle]\npub unsafe fn instr16_FF_3_reg(_r: i32) {\n    dbg_log!(\"callf #ud\");\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr16_FF_3_mem(addr: i32) {\n    // callf\n    let new_ip = return_on_pagefault!(safe_read16(addr));\n    let new_cs = return_on_pagefault!(safe_read16(addr + 2));\n    far_jump(new_ip, new_cs, true, false);\n}\npub unsafe fn instr16_FF_4_helper(data: i32) {\n    // jmp near\n    *instruction_pointer = get_seg_cs() + data;\n    dbg_assert!(*is_32 || get_real_eip() < 0x10000);\n}\npub unsafe fn instr16_FF_4_mem(addr: i32) {\n    instr16_FF_4_helper(return_on_pagefault!(safe_read16(addr)));\n}\npub unsafe fn instr16_FF_4_reg(r1: i32) { instr16_FF_4_helper(read_reg16(r1)); }\n\n#[no_mangle]\npub unsafe fn instr16_FF_5_reg(_r: i32) {\n    dbg_log!(\"jmpf #ud\");\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr16_FF_5_mem(addr: i32) {\n    // jmpf\n    let new_ip = return_on_pagefault!(safe_read16(addr));\n    let new_cs = return_on_pagefault!(safe_read16(addr + 2));\n    far_jump(new_ip, new_cs, false, false);\n}\npub unsafe fn instr16_FF_6_mem(addr: i32) {\n    return_on_pagefault!(push16(return_on_pagefault!(safe_read16(addr))));\n}\npub unsafe fn instr16_FF_6_reg(r1: i32) {\n    return_on_pagefault!(push16(read_reg16(r1)));\n}\n\npub unsafe fn instr32_FF_0_mem(addr: i32) { safe_read_write32(addr, &|x| inc32(x)) }\npub unsafe fn instr32_FF_0_reg(r1: i32) { write_reg32(r1, inc32(read_reg32(r1))); }\npub unsafe fn instr32_FF_1_mem(addr: i32) { safe_read_write32(addr, &|x| dec32(x)) }\npub unsafe fn instr32_FF_1_reg(r1: i32) { write_reg32(r1, dec32(read_reg32(r1))); }\n\npub unsafe fn instr32_FF_2_helper(data: i32) {\n    // call near\n    let cs = get_seg_cs();\n    return_on_pagefault!(push32(get_real_eip()));\n    dbg_assert!(*is_32 || data < 0x10000);\n    *instruction_pointer = cs + data;\n}\npub unsafe fn instr32_FF_2_mem(addr: i32) {\n    instr32_FF_2_helper(return_on_pagefault!(safe_read32s(addr)));\n}\npub unsafe fn instr32_FF_2_reg(r1: i32) { instr32_FF_2_helper(read_reg32(r1)); }\n#[no_mangle]\npub unsafe fn instr32_FF_3_reg(_r: i32) {\n    dbg_log!(\"callf #ud\");\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr32_FF_3_mem(addr: i32) {\n    // callf\n    let new_ip = return_on_pagefault!(safe_read32s(addr));\n    let new_cs = return_on_pagefault!(safe_read16(addr + 4));\n    if !*protected_mode || vm86_mode() {\n        if 0 != new_ip as u32 & 0xFFFF0000 {\n            dbg_assert!(false);\n        }\n    }\n    far_jump(new_ip, new_cs, true, true);\n}\n\npub unsafe fn instr32_FF_4_helper(data: i32) {\n    // jmp near\n    dbg_assert!(*is_32 || data < 0x10000);\n    *instruction_pointer = get_seg_cs() + data;\n}\npub unsafe fn instr32_FF_4_mem(addr: i32) {\n    instr32_FF_4_helper(return_on_pagefault!(safe_read32s(addr)));\n}\npub unsafe fn instr32_FF_4_reg(r1: i32) { instr32_FF_4_helper(read_reg32(r1)); }\n\n#[no_mangle]\npub unsafe fn instr32_FF_5_reg(_r: i32) {\n    dbg_log!(\"jmpf #ud\");\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr32_FF_5_mem(addr: i32) {\n    // jmpf\n    let new_ip = return_on_pagefault!(safe_read32s(addr));\n    let new_cs = return_on_pagefault!(safe_read16(addr + 4));\n    if !*protected_mode || vm86_mode() {\n        if 0 != new_ip as u32 & 0xFFFF0000 {\n            dbg_assert!(false);\n        }\n    }\n    far_jump(new_ip, new_cs, false, true);\n}\npub unsafe fn instr32_FF_6_mem(addr: i32) {\n    return_on_pagefault!(push32(return_on_pagefault!(safe_read32s(addr))));\n}\npub unsafe fn instr32_FF_6_reg(r1: i32) {\n    return_on_pagefault!(push32(read_reg32(r1)));\n}\n"
  },
  {
    "path": "src/rust/cpu/instructions_0f.rs",
    "content": "#![allow(non_snake_case)]\n\nunsafe fn undefined_instruction() {\n    dbg_assert!(false, \"Undefined instructions\");\n    trigger_ud()\n}\nunsafe fn unimplemented_sse() {\n    dbg_assert!(false, \"Unimplemented SSE instruction\");\n    trigger_ud()\n}\n\nuse crate::config;\nuse crate::cpu::arith::{\n    bsf16, bsf32, bsr16, bsr32, bt_mem, bt_reg, btc_mem, btc_reg, btr_mem, btr_reg, bts_mem,\n    bts_reg, cmpxchg16, cmpxchg32, cmpxchg8, popcnt, shld16, shld32, shrd16, shrd32, xadd16,\n    xadd32, xadd8,\n};\nuse crate::cpu::arith::{\n    imul_reg16, imul_reg32, saturate_sd_to_sb, saturate_sd_to_sw, saturate_sd_to_ub,\n    saturate_sw_to_sb, saturate_sw_to_ub, saturate_ud_to_ub, saturate_uw,\n};\nuse crate::cpu::cpu::*;\nuse crate::cpu::fpu::fpu_set_tag_word;\nuse crate::cpu::global_pointers::*;\nuse crate::cpu::misc_instr::{\n    adjust_stack_reg, bswap, cmovcc16, cmovcc32, fxrstor, fxsave, get_stack_pointer, jmpcc16,\n    jmpcc32, push16, push32_sreg, setcc_mem, setcc_reg, test_b, test_be, test_l, test_le, test_o,\n    test_p, test_s, test_z,\n};\nuse crate::cpu::misc_instr::{lar, lsl, verr, verw};\nuse crate::cpu::misc_instr::{lss16, lss32};\nuse crate::cpu::sse_instr::*;\n\n#[no_mangle]\npub unsafe fn instr16_0F00_0_mem(addr: i32) {\n    // sldt\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n        return;\n    }\n    return_on_pagefault!(safe_write16(addr, *sreg.offset(LDTR as isize) as i32));\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_0_mem(addr: i32) { instr16_0F00_0_mem(addr) }\n#[no_mangle]\npub unsafe fn instr16_0F00_0_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n        return;\n    }\n    write_reg16(r, *sreg.offset(LDTR as isize) as i32);\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_0_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n        return;\n    }\n    write_reg32(r, *sreg.offset(LDTR as isize) as i32);\n}\n\n#[no_mangle]\npub unsafe fn instr16_0F00_1_mem(addr: i32) {\n    // str\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n        return;\n    }\n    return_on_pagefault!(safe_write16(addr, *sreg.offset(TR as isize) as i32));\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_1_mem(addr: i32) { instr16_0F00_1_mem(addr) }\n#[no_mangle]\npub unsafe fn instr16_0F00_1_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n        return;\n    }\n    write_reg16(r, *sreg.offset(TR as isize) as i32);\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_1_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n        return;\n    }\n    write_reg32(r, *sreg.offset(TR as isize) as i32);\n}\n\n#[no_mangle]\npub unsafe fn instr16_0F00_2_mem(addr: i32) {\n    // lldt\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n    }\n    else if 0 != *cpl {\n        trigger_gp(0);\n    }\n    else {\n        return_on_pagefault!(load_ldt(return_on_pagefault!(safe_read16(addr))));\n    };\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_2_mem(addr: i32) { instr16_0F00_2_mem(addr) }\n#[no_mangle]\npub unsafe fn instr16_0F00_2_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n    }\n    else if 0 != *cpl {\n        trigger_gp(0);\n    }\n    else {\n        return_on_pagefault!(load_ldt(read_reg16(r)));\n    };\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_2_reg(r: i32) { instr16_0F00_2_reg(r) }\n\n#[no_mangle]\npub unsafe fn instr16_0F00_3_mem(addr: i32) {\n    // ltr\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n    }\n    else if 0 != *cpl {\n        trigger_gp(0);\n    }\n    else {\n        load_tr(return_on_pagefault!(safe_read16(addr)));\n    };\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_3_mem(addr: i32) { instr16_0F00_3_mem(addr); }\n#[no_mangle]\npub unsafe fn instr16_0F00_3_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        trigger_ud();\n    }\n    else if 0 != *cpl {\n        trigger_gp(0);\n    }\n    else {\n        load_tr(read_reg16(r));\n    };\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_3_reg(r: i32) { instr16_0F00_3_reg(r) }\n\n#[no_mangle]\npub unsafe fn instr16_0F00_4_mem(addr: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"verr #ud\");\n        trigger_ud();\n        return;\n    }\n    verr(return_on_pagefault!(safe_read16(addr)));\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_4_mem(addr: i32) { instr16_0F00_4_mem(addr) }\n#[no_mangle]\npub unsafe fn instr16_0F00_4_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"verr #ud\");\n        trigger_ud();\n        return;\n    }\n    verr(read_reg16(r));\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_4_reg(r: i32) { instr16_0F00_4_reg(r) }\n#[no_mangle]\npub unsafe fn instr16_0F00_5_mem(addr: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"verw #ud\");\n        trigger_ud();\n        return;\n    }\n    verw(return_on_pagefault!(safe_read16(addr)));\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_5_mem(addr: i32) { instr16_0F00_5_mem(addr) }\n#[no_mangle]\npub unsafe fn instr16_0F00_5_reg(r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"verw #ud\");\n        trigger_ud();\n        return;\n    }\n    verw(read_reg16(r));\n}\n#[no_mangle]\npub unsafe fn instr32_0F00_5_reg(r: i32) { instr16_0F00_5_reg(r) }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_0_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0F01_0_reg(_r: i32) { trigger_ud(); }\n\nunsafe fn sgdt(addr: i32, mask: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 6));\n    safe_write16(addr, *gdtr_size).unwrap();\n    safe_write32(addr + 2, *gdtr_offset & mask).unwrap();\n}\n#[no_mangle]\npub unsafe fn instr16_0F01_0_mem(addr: i32) { sgdt(addr, 0xFFFFFF) }\n#[no_mangle]\npub unsafe fn instr32_0F01_0_mem(addr: i32) { sgdt(addr, -1) }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_1_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0F01_1_reg(_r: i32) { trigger_ud(); }\n\nunsafe fn sidt(addr: i32, mask: i32) {\n    return_on_pagefault!(writable_or_pagefault(addr, 6));\n    safe_write16(addr, *idtr_size).unwrap();\n    safe_write32(addr + 2, *idtr_offset & mask).unwrap();\n}\n#[no_mangle]\npub unsafe fn instr16_0F01_1_mem(addr: i32) { sidt(addr, 0xFFFFFF) }\n#[no_mangle]\npub unsafe fn instr32_0F01_1_mem(addr: i32) { sidt(addr, -1) }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_2_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0F01_2_reg(_r: i32) { trigger_ud(); }\n\nunsafe fn lgdt(addr: i32, mask: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n    let size = return_on_pagefault!(safe_read16(addr));\n    let offset = return_on_pagefault!(safe_read32s(addr + 2));\n    *gdtr_size = size;\n    *gdtr_offset = offset & mask;\n}\n#[no_mangle]\npub unsafe fn instr16_0F01_2_mem(addr: i32) { lgdt(addr, 0xFFFFFF); }\n#[no_mangle]\npub unsafe fn instr32_0F01_2_mem(addr: i32) { lgdt(addr, -1); }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_3_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0F01_3_reg(_r: i32) { trigger_ud(); }\n\nunsafe fn lidt(addr: i32, mask: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n    let size = return_on_pagefault!(safe_read16(addr));\n    let offset = return_on_pagefault!(safe_read32s(addr + 2));\n    *idtr_size = size;\n    *idtr_offset = offset & mask;\n}\n#[no_mangle]\npub unsafe fn instr16_0F01_3_mem(addr: i32) { lidt(addr, 0xFFFFFF); }\n#[no_mangle]\npub unsafe fn instr32_0F01_3_mem(addr: i32) { lidt(addr, -1); }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_4_reg(r: i32) {\n    // smsw\n    write_reg16(r, *cr);\n}\n#[no_mangle]\npub unsafe fn instr32_0F01_4_reg(r: i32) { write_reg32(r, *cr); }\n#[no_mangle]\npub unsafe fn instr16_0F01_4_mem(addr: i32) {\n    return_on_pagefault!(safe_write16(addr, *cr & 0xFFFF));\n}\n#[no_mangle]\npub unsafe fn instr32_0F01_4_mem(addr: i32) {\n    return_on_pagefault!(safe_write16(addr, *cr & 0xFFFF));\n}\n\n#[no_mangle]\npub unsafe fn lmsw(mut new_cr0: i32) {\n    new_cr0 = *cr & !15 | new_cr0 & 15;\n    if *protected_mode {\n        // lmsw cannot be used to switch back\n        new_cr0 |= CR0_PE\n    }\n    set_cr0(new_cr0);\n}\n#[no_mangle]\npub unsafe fn instr16_0F01_6_reg(r: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n    lmsw(read_reg16(r));\n}\n#[no_mangle]\npub unsafe fn instr32_0F01_6_reg(r: i32) { instr16_0F01_6_reg(r); }\n#[no_mangle]\npub unsafe fn instr16_0F01_6_mem(addr: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n    lmsw(return_on_pagefault!(safe_read16(addr)));\n}\n#[no_mangle]\npub unsafe fn instr32_0F01_6_mem(addr: i32) { instr16_0F01_6_mem(addr) }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_7_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0F01_7_reg(_r: i32) { trigger_ud(); }\n\n#[no_mangle]\npub unsafe fn instr16_0F01_7_mem(addr: i32) {\n    // invlpg\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n    invlpg(addr);\n}\n#[no_mangle]\npub unsafe fn instr32_0F01_7_mem(addr: i32) { instr16_0F01_7_mem(addr) }\n\n#[no_mangle]\npub unsafe fn instr16_0F02_mem(addr: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lar #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg16(\n        r,\n        lar(return_on_pagefault!(safe_read16(addr)), read_reg16(r)),\n    );\n}\n#[no_mangle]\npub unsafe fn instr16_0F02_reg(r1: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lar #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg16(r, lar(read_reg16(r1), read_reg16(r)));\n}\n#[no_mangle]\npub unsafe fn instr32_0F02_mem(addr: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lar #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg32(\n        r,\n        lar(return_on_pagefault!(safe_read16(addr)), read_reg32(r)),\n    );\n}\n#[no_mangle]\npub unsafe fn instr32_0F02_reg(r1: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lar #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg32(r, lar(read_reg16(r1), read_reg32(r)));\n}\n#[no_mangle]\npub unsafe fn instr16_0F03_mem(addr: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lsl #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg16(\n        r,\n        lsl(return_on_pagefault!(safe_read16(addr)), read_reg16(r)),\n    );\n}\n#[no_mangle]\npub unsafe fn instr16_0F03_reg(r1: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lsl #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg16(r, lsl(read_reg16(r1), read_reg16(r)));\n}\n#[no_mangle]\npub unsafe fn instr32_0F03_mem(addr: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lsl #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg32(\n        r,\n        lsl(return_on_pagefault!(safe_read16(addr)), read_reg32(r)),\n    );\n}\n#[no_mangle]\npub unsafe fn instr32_0F03_reg(r1: i32, r: i32) {\n    if !*protected_mode || vm86_mode() {\n        dbg_log!(\"lsl #ud\");\n        trigger_ud();\n        return;\n    }\n    write_reg32(r, lsl(read_reg16(r1), read_reg32(r)));\n}\n#[no_mangle]\npub unsafe fn instr_0F04() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F05() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F06() {\n    // clts\n    if 0 != *cpl {\n        dbg_log!(\"clts #gp\");\n        trigger_gp(0);\n    }\n    else {\n        if false {\n            dbg_log!(\"clts\");\n        }\n        *cr &= !CR0_TS;\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0F07() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F08() {\n    // invd\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr_0F09() {\n    if 0 != *cpl {\n        dbg_log!(\"wbinvd #gp\");\n        trigger_gp(0);\n    }\n    else {\n        // wbinvd\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0F0A() { undefined_instruction(); }\npub unsafe fn instr_0F0B() {\n    // UD2\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr_0F0C() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F0D() {\n    // nop\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr_0F0E() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F0F() { undefined_instruction(); }\n\npub unsafe fn instr_0F10(source: reg128, r: i32) {\n    // movups xmm, xmm/m128\n    mov_rm_r128(source, r);\n}\npub unsafe fn instr_0F10_reg(r1: i32, r2: i32) { instr_0F10(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F10_mem(addr: i32, r: i32) {\n    instr_0F10(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_F30F10_reg(r1: i32, r2: i32) {\n    // movss xmm, xmm/m32\n    let data = read_xmm128s(r1);\n    write_xmm32(r2, data.u32[0] as i32);\n}\npub unsafe fn instr_F30F10_mem(addr: i32, r: i32) {\n    // movss xmm, xmm/m32\n    let data = return_on_pagefault!(safe_read32s(addr));\n    write_xmm128(r, data, 0, 0, 0);\n}\npub unsafe fn instr_660F10(source: reg128, r: i32) {\n    // movupd xmm, xmm/m128\n    mov_rm_r128(source, r);\n}\npub unsafe fn instr_660F10_reg(r1: i32, r2: i32) { instr_660F10(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F10_mem(addr: i32, r: i32) {\n    instr_660F10(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_F20F10_reg(r1: i32, r2: i32) {\n    // movsd xmm, xmm/m64\n    let data = read_xmm128s(r1);\n    write_xmm64(r2, data.u64[0]);\n}\npub unsafe fn instr_F20F10_mem(addr: i32, r: i32) {\n    // movsd xmm, xmm/m64\n    let data = return_on_pagefault!(safe_read64s(addr));\n    write_xmm128_2(r, data, 0);\n}\npub unsafe fn instr_0F11_reg(r1: i32, r2: i32) {\n    // movups xmm/m128, xmm\n    mov_r_r128(r1, r2);\n}\npub unsafe fn instr_0F11_mem(addr: i32, r: i32) {\n    // movups xmm/m128, xmm\n    mov_r_m128(addr, r);\n}\npub unsafe fn instr_F30F11_reg(rm_dest: i32, reg_src: i32) {\n    // movss xmm/m32, xmm\n    let data = read_xmm128s(reg_src);\n    write_xmm32(rm_dest, data.u32[0] as i32);\n}\npub unsafe fn instr_F30F11_mem(addr: i32, r: i32) {\n    // movss xmm/m32, xmm\n    let data = read_xmm128s(r);\n    return_on_pagefault!(safe_write32(addr, data.u32[0] as i32));\n}\npub unsafe fn instr_660F11_reg(r1: i32, r2: i32) {\n    // movupd xmm/m128, xmm\n    mov_r_r128(r1, r2);\n}\npub unsafe fn instr_660F11_mem(addr: i32, r: i32) {\n    // movupd xmm/m128, xmm\n    mov_r_m128(addr, r);\n}\npub unsafe fn instr_F20F11_reg(r1: i32, r2: i32) {\n    // movsd xmm/m64, xmm\n    let data = read_xmm128s(r2);\n    write_xmm64(r1, data.u64[0]);\n}\npub unsafe fn instr_F20F11_mem(addr: i32, r: i32) {\n    // movsd xmm/m64, xmm\n    let data = read_xmm64s(r);\n    return_on_pagefault!(safe_write64(addr, data));\n}\npub unsafe fn instr_0F12_mem(addr: i32, r: i32) {\n    // movlps xmm, m64\n    let data = return_on_pagefault!(safe_read64s(addr));\n    write_xmm64(r, data);\n}\npub unsafe fn instr_0F12_reg(r1: i32, r2: i32) {\n    // movhlps xmm, xmm\n    let data = read_xmm128s(r1);\n    write_xmm64(r2, data.u64[1]);\n}\npub unsafe fn instr_660F12_reg(_r1: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F12_mem(addr: i32, r: i32) {\n    // movlpd xmm, m64\n    let data = return_on_pagefault!(safe_read64s(addr));\n    write_xmm64(r, data);\n}\n#[no_mangle]\npub unsafe fn instr_F20F12(source: u64, r: i32) {\n    // movddup xmm1, xmm2/m64\n    write_xmm_reg128(\n        r,\n        reg128 {\n            u64: [source, source],\n        },\n    );\n}\npub unsafe fn instr_F20F12_reg(r1: i32, r2: i32) { instr_F20F12(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F12_mem(addr: i32, r: i32) {\n    instr_F20F12(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F12(source: reg128, r: i32) {\n    // movsldup xmm1, xmm2/m128\n    write_xmm_reg128(\n        r,\n        reg128 {\n            u32: [source.u32[0], source.u32[0], source.u32[2], source.u32[2]],\n        },\n    );\n}\npub unsafe fn instr_F30F12_reg(r1: i32, r2: i32) { instr_F30F12(read_xmm128s(r1), r2); }\npub unsafe fn instr_F30F12_mem(addr: i32, r: i32) {\n    instr_F30F12(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_0F13_mem(addr: i32, r: i32) {\n    // movlps m64, xmm\n    movl_r128_m64(addr, r);\n}\npub unsafe fn instr_0F13_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_660F13_reg(_r1: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F13_mem(addr: i32, r: i32) {\n    // movlpd xmm/m64, xmm\n    movl_r128_m64(addr, r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F14(source: u64, r: i32) {\n    // unpcklps xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm64s(r);\n    write_xmm128(\n        r,\n        destination as i32,\n        source as i32,\n        (destination >> 32) as i32,\n        (source >> 32) as i32,\n    );\n}\npub unsafe fn instr_0F14_reg(r1: i32, r2: i32) { instr_0F14(read_xmm64s(r1), r2); }\npub unsafe fn instr_0F14_mem(addr: i32, r: i32) {\n    instr_0F14(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F14(source: u64, r: i32) {\n    // unpcklpd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm64s(r);\n    write_xmm128(\n        r,\n        destination as i32,\n        (destination >> 32) as i32,\n        source as i32,\n        (source >> 32) as i32,\n    );\n}\npub unsafe fn instr_660F14_reg(r1: i32, r2: i32) { instr_660F14(read_xmm64s(r1), r2); }\npub unsafe fn instr_660F14_mem(addr: i32, r: i32) {\n    instr_660F14(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F15(source: reg128, r: i32) {\n    // unpckhps xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[2] as i32,\n        source.u32[2] as i32,\n        destination.u32[3] as i32,\n        source.u32[3] as i32,\n    );\n}\npub unsafe fn instr_0F15_reg(r1: i32, r2: i32) { instr_0F15(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F15_mem(addr: i32, r: i32) {\n    instr_0F15(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F15(source: reg128, r: i32) {\n    // unpckhpd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[2] as i32,\n        destination.u32[3] as i32,\n        source.u32[2] as i32,\n        source.u32[3] as i32,\n    );\n}\npub unsafe fn instr_660F15_reg(r1: i32, r2: i32) { instr_660F15(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F15_mem(addr: i32, r: i32) {\n    instr_660F15(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F16(source: u64, r: i32) { (*reg_xmm.offset(r as isize)).u64[1] = source; }\npub unsafe fn instr_0F16_mem(addr: i32, r: i32) {\n    // movhps xmm, m64\n    instr_0F16(return_on_pagefault!(safe_read64s(addr)), r);\n}\npub unsafe fn instr_0F16_reg(r1: i32, r2: i32) {\n    // movlhps xmm, xmm\n    instr_0F16(read_xmm64s(r1), r2);\n}\npub unsafe fn instr_660F16_mem(addr: i32, r: i32) {\n    // movhpd xmm, m64\n    instr_0F16(return_on_pagefault!(safe_read64s(addr)), r);\n}\npub unsafe fn instr_660F16_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_F30F16(source: reg128, r: i32) {\n    // movshdup xmm1, xmm2/m128\n    write_xmm_reg128(\n        r,\n        reg128 {\n            u32: [source.u32[1], source.u32[1], source.u32[3], source.u32[3]],\n        },\n    );\n}\npub unsafe fn instr_F30F16_reg(r1: i32, r2: i32) { instr_F30F16(read_xmm128s(r1), r2); }\npub unsafe fn instr_F30F16_mem(addr: i32, r: i32) {\n    instr_F30F16(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_0F17_mem(addr: i32, r: i32) {\n    // movhps m64, xmm\n    movh_r128_m64(addr, r);\n}\npub unsafe fn instr_0F17_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_660F17_mem(addr: i32, r: i32) {\n    // movhpd m64, xmm\n    movh_r128_m64(addr, r);\n}\npub unsafe fn instr_660F17_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n\npub unsafe fn instr_0F18_reg(_r1: i32, _r2: i32) {\n    // reserved nop\n}\npub unsafe fn instr_0F18_mem(_addr: i32, _r: i32) {\n    // prefetch\n    // nop for us\n}\n\npub unsafe fn instr_0F19_reg(_r1: i32, _r2: i32) {}\npub unsafe fn instr_0F19_mem(_addr: i32, _r: i32) {}\n\n#[no_mangle]\npub unsafe fn instr_0F1A() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F1B() { undefined_instruction(); }\n\npub unsafe fn instr_0F1C_reg(_r1: i32, _r2: i32) {}\npub unsafe fn instr_0F1C_mem(_addr: i32, _r: i32) {}\npub unsafe fn instr_0F1D_reg(_r1: i32, _r2: i32) {}\npub unsafe fn instr_0F1D_mem(_addr: i32, _r: i32) {}\npub unsafe fn instr_0F1E_reg(_r1: i32, _r2: i32) {}\npub unsafe fn instr_0F1E_mem(_addr: i32, _r: i32) {}\npub unsafe fn instr_0F1F_reg(_r1: i32, _r2: i32) {}\npub unsafe fn instr_0F1F_mem(_addr: i32, _r: i32) {}\n\n#[no_mangle]\npub unsafe fn instr_0F20(r: i32, creg: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n\n    match creg {\n        0 => {\n            write_reg32(r, *cr);\n        },\n        2 => {\n            write_reg32(r, *cr.offset(2));\n        },\n        3 => {\n            write_reg32(r, *cr.offset(3));\n        },\n        4 => {\n            write_reg32(r, *cr.offset(4));\n        },\n        _ => {\n            dbg_log!(\"{}\", creg);\n            undefined_instruction();\n        },\n    }\n}\n#[no_mangle]\npub unsafe fn instr_0F21(r: i32, mut dreg_index: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n\n    if dreg_index == 4 || dreg_index == 5 {\n        if 0 != *cr.offset(4) & CR4_DE {\n            dbg_log!(\"#ud mov dreg 4/5 with cr4.DE set\");\n            trigger_ud();\n            return;\n        }\n        else {\n            // DR4 and DR5 refer to DR6 and DR7 respectively\n            dreg_index += 2\n        }\n    }\n    write_reg32(r, *dreg.offset(dreg_index as isize));\n\n    if false {\n        dbg_log!(\n            \"read dr{}: {:x}\",\n            dreg_index,\n            *dreg.offset(dreg_index as isize)\n        );\n    }\n}\n#[no_mangle]\npub unsafe fn instr_0F22(r: i32, creg: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n\n    let data = read_reg32(r);\n    // mov cr, addr\n    match creg {\n        0 => {\n            if false {\n                dbg_log!(\"cr0 <- {:x}\", data);\n            }\n            set_cr0(data);\n        },\n        2 => {\n            dbg_log!(\"cr2 <- {:x}\", data);\n            *cr.offset(2) = data\n        },\n        3 => set_cr3(data),\n        4 => {\n            dbg_log!(\"cr4 <- {:x}\", data);\n            if 0 != data as u32\n                & ((1 << 11 | 1 << 12 | 1 << 15 | 1 << 16 | 1 << 19) as u32 | 0xFFC00000)\n            {\n                dbg_log!(\"trigger_gp: Invalid cr4 bit\");\n                trigger_gp(0);\n                return;\n            }\n            else {\n                if 0 != (*cr.offset(4) ^ data) & (CR4_PGE | CR4_PSE | CR4_PAE) {\n                    full_clear_tlb();\n                }\n                if data & CR4_PAE != 0\n                    && 0 != (*cr.offset(4) ^ data) & (CR4_PGE | CR4_PSE | CR4_SMEP)\n                {\n                    load_pdpte(*cr.offset(3));\n                }\n                *cr.offset(4) = data;\n            }\n        },\n        _ => {\n            dbg_log!(\"{}\", creg);\n            undefined_instruction();\n        },\n    }\n}\n#[no_mangle]\npub unsafe fn instr_0F23(r: i32, mut dreg_index: i32) {\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n\n    if dreg_index == 4 || dreg_index == 5 {\n        if 0 != *cr.offset(4) & CR4_DE {\n            dbg_log!(\"#ud mov dreg 4/5 with cr4.DE set\");\n            trigger_ud();\n            return;\n        }\n        else {\n            // DR4 and DR5 refer to DR6 and DR7 respectively\n            dreg_index += 2\n        }\n    }\n    *dreg.offset(dreg_index as isize) = read_reg32(r);\n    if false {\n        dbg_log!(\n            \"write dr{}: {:x}\",\n            dreg_index,\n            *dreg.offset(dreg_index as isize)\n        );\n    }\n}\n#[no_mangle]\npub unsafe fn instr_0F24() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F25() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F26() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F27() { undefined_instruction(); }\n\npub unsafe fn instr_0F28(source: reg128, r: i32) {\n    // movaps xmm, xmm/m128\n    // XXX: Aligned read or #gp\n    mov_rm_r128(source, r);\n}\npub unsafe fn instr_0F28_reg(r1: i32, r2: i32) { instr_0F28(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F28_mem(addr: i32, r: i32) {\n    instr_0F28(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_660F28(source: reg128, r: i32) {\n    // movapd xmm, xmm/m128\n    // XXX: Aligned read or #gp\n    // Note: Same as movdqa (660F6F)\n    mov_rm_r128(source, r);\n}\npub unsafe fn instr_660F28_reg(r1: i32, r2: i32) { instr_660F28(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F28_mem(addr: i32, r: i32) {\n    instr_660F28(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_0F29_mem(addr: i32, r: i32) {\n    // movaps m128, xmm\n    let data = read_xmm128s(r);\n    // XXX: Aligned write or #gp\n    return_on_pagefault!(safe_write128(addr, data));\n}\npub unsafe fn instr_0F29_reg(r1: i32, r2: i32) {\n    // movaps xmm, xmm\n    mov_r_r128(r1, r2);\n}\npub unsafe fn instr_660F29_mem(addr: i32, r: i32) {\n    // movapd m128, xmm\n    let data = read_xmm128s(r);\n    // XXX: Aligned write or #gp\n    return_on_pagefault!(safe_write128(addr, data));\n}\npub unsafe fn instr_660F29_reg(r1: i32, r2: i32) {\n    // movapd xmm, xmm\n    mov_r_r128(r1, r2);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F2A(source: u64, r: i32) {\n    // cvtpi2ps xmm, mm/m64\n    // Note: Casts here can fail\n    // XXX: Should round according to round control\n    let source: [i32; 2] = std::mem::transmute(source);\n    let result = [source[0] as f32, source[1] as f32];\n    write_xmm64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F2A_reg(r1: i32, r2: i32) { instr_0F2A(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F2A_mem(addr: i32, r: i32) {\n    instr_0F2A(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F2A(source: u64, r: i32) {\n    // cvtpi2pd xmm, xmm/m64\n    // These casts can't fail\n    let source: [i32; 2] = std::mem::transmute(source);\n    let result = reg128 {\n        f64: [source[0] as f64, source[1] as f64],\n    };\n    write_xmm_reg128(r, result);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_660F2A_reg(r1: i32, r2: i32) { instr_660F2A(read_mmx64s(r1), r2); }\npub unsafe fn instr_660F2A_mem(addr: i32, r: i32) {\n    instr_660F2A(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F2A(source: i32, r: i32) {\n    // cvtsi2sd xmm, r32/m32\n    // This cast can't fail\n    write_xmm_f64(r, source as f64);\n}\npub unsafe fn instr_F20F2A_reg(r1: i32, r2: i32) { instr_F20F2A(read_reg32(r1), r2); }\npub unsafe fn instr_F20F2A_mem(addr: i32, r: i32) {\n    instr_F20F2A(return_on_pagefault!(safe_read32s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F2A(source: i32, r: i32) {\n    // cvtsi2ss xmm, r/m32\n    // Note: This cast can fail\n    // XXX: Should round according to round control\n    let result = source as f32;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F2A_reg(r1: i32, r2: i32) { instr_F30F2A(read_reg32(r1), r2); }\npub unsafe fn instr_F30F2A_mem(addr: i32, r: i32) {\n    instr_F30F2A(return_on_pagefault!(safe_read32s(addr)), r);\n}\n\npub unsafe fn instr_0F2B_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_0F2B_mem(addr: i32, r: i32) {\n    // movntps m128, xmm\n    // XXX: Aligned write or #gp\n    mov_r_m128(addr, r);\n}\npub unsafe fn instr_660F2B_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_660F2B_mem(addr: i32, r: i32) {\n    // movntpd m128, xmm\n    // XXX: Aligned write or #gp\n    mov_r_m128(addr, r);\n}\n\npub unsafe fn instr_0F2C(source: u64, r: i32) {\n    // cvttps2pi mm, xmm/m64\n    let low = f32::from_bits(source as u32);\n    let high = f32::from_bits((source >> 32) as u32);\n    write_mmx_reg64(\n        r,\n        sse_convert_with_truncation_f32_to_i32(low) as u32 as u64\n            | (sse_convert_with_truncation_f32_to_i32(high) as u32 as u64) << 32,\n    );\n    transition_fpu_to_mmx();\n}\n#[no_mangle]\npub unsafe fn instr_0F2C_mem(addr: i32, r: i32) {\n    instr_0F2C(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F2C_reg(r1: i32, r2: i32) { instr_0F2C(read_xmm64s(r1), r2); }\n\npub unsafe fn instr_660F2C(source: reg128, r: i32) {\n    // cvttpd2pi mm, xmm/m128\n    write_mmx_reg64(\n        r,\n        sse_convert_with_truncation_f64_to_i32(source.f64[0]) as u32 as u64\n            | (sse_convert_with_truncation_f64_to_i32(source.f64[1]) as u32 as u64) << 32,\n    );\n    transition_fpu_to_mmx();\n}\n#[no_mangle]\npub unsafe fn instr_660F2C_mem(addr: i32, r: i32) {\n    instr_660F2C(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F2C_reg(r1: i32, r2: i32) { instr_660F2C(read_xmm128s(r1), r2); }\n\npub unsafe fn instr_F20F2C(source: u64, r: i32) {\n    // cvttsd2si r32, xmm/m64\n    let source = f64::from_bits(source);\n    write_reg32(r, sse_convert_with_truncation_f64_to_i32(source));\n}\n#[no_mangle]\npub unsafe fn instr_F20F2C_reg(r1: i32, r2: i32) { instr_F20F2C(read_xmm64s(r1), r2); }\n#[no_mangle]\npub unsafe fn instr_F20F2C_mem(addr: i32, r: i32) {\n    instr_F20F2C(return_on_pagefault!(safe_read64s(addr)), r);\n}\n\npub unsafe fn instr_F30F2C(source: f32, r: i32) {\n    // cvttss2si\n    write_reg32(r, sse_convert_with_truncation_f32_to_i32(source));\n}\n#[no_mangle]\npub unsafe fn instr_F30F2C_mem(addr: i32, r: i32) {\n    instr_F30F2C(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F2C_reg(r1: i32, r2: i32) { instr_F30F2C(read_xmm_f32(r1), r2); }\n\npub unsafe fn instr_0F2D(source: u64, r: i32) {\n    // cvtps2pi mm, xmm/m64\n    let source: [f32; 2] = std::mem::transmute(source);\n    let result = [\n        sse_convert_f32_to_i32(source[0]),\n        sse_convert_f32_to_i32(source[1]),\n    ];\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\n#[no_mangle]\npub unsafe fn instr_0F2D_reg(r1: i32, r2: i32) { instr_0F2D(read_xmm64s(r1), r2); }\n#[no_mangle]\npub unsafe fn instr_0F2D_mem(addr: i32, r: i32) {\n    instr_0F2D(return_on_pagefault!(safe_read64s(addr)), r);\n}\n\npub unsafe fn instr_660F2D(source: reg128, r: i32) {\n    // cvtpd2pi mm, xmm/m128\n    let result = [\n        sse_convert_f64_to_i32(source.f64[0]),\n        sse_convert_f64_to_i32(source.f64[1]),\n    ];\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\n#[no_mangle]\npub unsafe fn instr_660F2D_reg(r1: i32, r2: i32) { instr_660F2D(read_xmm128s(r1), r2); }\n#[no_mangle]\npub unsafe fn instr_660F2D_mem(addr: i32, r: i32) {\n    instr_660F2D(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_F20F2D(source: u64, r: i32) {\n    // cvtsd2si r32, xmm/m64\n    write_reg32(r, sse_convert_f64_to_i32(f64::from_bits(source)));\n}\npub unsafe fn instr_F20F2D_reg(r1: i32, r2: i32) { instr_F20F2D(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F2D_mem(addr: i32, r: i32) {\n    instr_F20F2D(return_on_pagefault!(safe_read64s(addr)), r);\n}\npub unsafe fn instr_F30F2D(source: f32, r: i32) {\n    // cvtss2si r32, xmm1/m32\n    write_reg32(r, sse_convert_f32_to_i32(source));\n}\npub unsafe fn instr_F30F2D_reg(r1: i32, r2: i32) { instr_F30F2D(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F2D_mem(addr: i32, r: i32) {\n    instr_F30F2D(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F2E(source: f32, r: i32) {\n    // ucomiss xmm1, xmm2/m32\n    let destination = read_xmm_f32(r);\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    if destination == source {\n        *flags |= FLAG_ZERO\n    }\n    else if destination < source {\n        *flags |= FLAG_CARRY\n    }\n    else if destination > source {\n        // all flags cleared\n    }\n    else {\n        // TODO: Signal on SNaN\n        *flags |= FLAG_ZERO | FLAG_PARITY | FLAG_CARRY\n    }\n}\npub unsafe fn instr_0F2E_reg(r1: i32, r2: i32) { instr_0F2E(read_xmm_f32(r1), r2) }\npub unsafe fn instr_0F2E_mem(addr: i32, r: i32) {\n    instr_0F2E(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_660F2E(source: u64, r: i32) {\n    // ucomisd xmm1, xmm2/m64\n    let destination = f64::from_bits(read_xmm64s(r));\n    let source = f64::from_bits(source);\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    if destination == source {\n        *flags |= FLAG_ZERO\n    }\n    else if destination < source {\n        *flags |= FLAG_CARRY\n    }\n    else if destination > source {\n        // all flags cleared\n    }\n    else {\n        // TODO: Signal on SNaN\n        *flags |= FLAG_ZERO | FLAG_PARITY | FLAG_CARRY\n    }\n}\npub unsafe fn instr_660F2E_reg(r1: i32, r: i32) { instr_660F2E(read_xmm64s(r1), r); }\npub unsafe fn instr_660F2E_mem(addr: i32, r: i32) {\n    instr_660F2E(return_on_pagefault!(safe_read64s(addr)), r)\n}\n\n#[no_mangle]\npub unsafe fn instr_0F2F(source: f32, r: i32) {\n    // comiss xmm1, xmm2/m32\n    let destination = read_xmm_f32(r);\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    if destination == source {\n        *flags |= FLAG_ZERO\n    }\n    else if destination < source {\n        *flags |= FLAG_CARRY\n    }\n    else if destination > source {\n        // all flags cleared\n    }\n    else {\n        // TODO: Signal on SNaN or QNaN\n        *flags |= FLAG_ZERO | FLAG_PARITY | FLAG_CARRY\n    }\n}\npub unsafe fn instr_0F2F_reg(r1: i32, r2: i32) { instr_0F2F(read_xmm_f32(r1), r2) }\npub unsafe fn instr_0F2F_mem(addr: i32, r: i32) {\n    instr_0F2F(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_660F2F(source: u64, r: i32) {\n    // comisd xmm1, xmm2/m64\n    let destination = f64::from_bits(read_xmm64s(r));\n    let source = f64::from_bits(source);\n    *flags_changed = 0;\n    *flags &= !FLAGS_ALL;\n    if destination == source {\n        *flags |= FLAG_ZERO\n    }\n    else if destination < source {\n        *flags |= FLAG_CARRY\n    }\n    else if destination > source {\n        // all flags cleared\n    }\n    else {\n        // TODO: Signal on SNaN or QNaN\n        *flags |= FLAG_ZERO | FLAG_PARITY | FLAG_CARRY\n    }\n}\npub unsafe fn instr_660F2F_reg(r1: i32, r: i32) { instr_660F2F(read_xmm64s(r1), r); }\npub unsafe fn instr_660F2F_mem(addr: i32, r: i32) {\n    instr_660F2F(return_on_pagefault!(safe_read64s(addr)), r)\n}\n\n#[no_mangle]\npub unsafe fn instr_0F30() {\n    // wrmsr - write maschine specific register\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n\n    let index = read_reg32(ECX);\n    let low = read_reg32(EAX);\n    let high = read_reg32(EDX);\n\n    if index != IA32_SYSENTER_ESP {\n        dbg_log!(\"wrmsr ecx={:x} data={:x}:{:x}\", index, high, low);\n    }\n\n    match index {\n        IA32_SYSENTER_CS => *sysenter_cs = low & 0xFFFF,\n        IA32_SYSENTER_EIP => *sysenter_eip = low,\n        IA32_SYSENTER_ESP => *sysenter_esp = low,\n        IA32_FEAT_CTL => {}, // linux 5.x\n        MSR_TEST_CTRL => {}, // linux 5.x\n        IA32_APIC_BASE => {\n            dbg_assert!(\n                high == 0,\n                \"Changing APIC address (high 32 bits) not supported\"\n            );\n            let address = low & !(IA32_APIC_BASE_BSP | IA32_APIC_BASE_EXTD | IA32_APIC_BASE_EN);\n            dbg_assert!(\n                (address == 0 && !*acpi_enabled) // windows me\n                || address == APIC_MEM_ADDRESS as i32,\n                \"Changing APIC address not supported\"\n            );\n            dbg_assert!(low & IA32_APIC_BASE_EXTD == 0, \"x2apic not supported\");\n            *apic_enabled = low & IA32_APIC_BASE_EN == IA32_APIC_BASE_EN\n        },\n        IA32_TIME_STAMP_COUNTER => set_tsc(low as u32, high as u32),\n        IA32_BIOS_UPDT_TRIG => {}, // windows xp\n        IA32_BIOS_SIGN_ID => {},\n        MISC_FEATURE_ENABLES => {\n            // Linux 4, see: https://patchwork.kernel.org/patch/9528279/\n        },\n        IA32_MISC_ENABLE => {\n            // Enable Misc. Processor Features\n        },\n        IA32_MCG_CAP => {}, // netbsd\n        IA32_KERNEL_GS_BASE => {\n            // Only used in 64 bit mode (by SWAPGS), but set by kvm-unit-test\n            dbg_log!(\"GS Base written\");\n        },\n        IA32_PERFEVTSEL0 | IA32_PERFEVTSEL1 => {}, // linux/9legacy\n        IA32_PMC0 | IA32_PMC1 => {},               // linux\n        IA32_PAT => {},\n        IA32_SPEC_CTRL => {},      // linux 5.19\n        IA32_TSX_CTRL => {},       // linux 5.19\n        MSR_TSX_FORCE_ABORT => {}, // linux 5.19\n        IA32_MCU_OPT_CTRL => {},   // linux 5.19\n        MSR_AMD64_LS_CFG => {},    // linux 5.19\n        MSR_AMD64_DE_CFG => {},    // linux 6.1\n        _ => {\n            dbg_log!(\"Unknown msr: {:x}\", index);\n            dbg_assert!(false);\n        },\n    }\n}\n\npub unsafe fn instr_0F31() {\n    // rdtsc - read timestamp counter\n    if 0 == *cpl || 0 == *cr.offset(4) & CR4_TSD {\n        let tsc = read_tsc();\n        write_reg32(EAX, tsc as i32);\n        write_reg32(EDX, (tsc >> 32) as i32);\n        if false {\n            dbg_log!(\"rdtsc  edx:eax={:x}:{:x}\", read_reg32(EDX), read_reg32(EAX));\n        }\n    }\n    else {\n        trigger_gp(0);\n    };\n}\n\n#[no_mangle]\npub unsafe fn instr_0F32() {\n    // rdmsr - read maschine specific register\n    if 0 != *cpl {\n        trigger_gp(0);\n        return;\n    }\n\n    let index = read_reg32(ECX);\n    dbg_log!(\"rdmsr ecx={:x}\", index);\n\n    let mut low = 0;\n    let mut high = 0;\n\n    match index {\n        IA32_SYSENTER_CS => low = *sysenter_cs,\n        IA32_SYSENTER_EIP => low = *sysenter_eip,\n        IA32_SYSENTER_ESP => low = *sysenter_esp,\n        IA32_TIME_STAMP_COUNTER => {\n            let tsc = read_tsc();\n            low = tsc as i32;\n            high = (tsc >> 32) as i32\n        },\n        IA32_FEAT_CTL => {}, // linux 5.x\n        MSR_TEST_CTRL => {}, // linux 5.x\n        IA32_PLATFORM_ID => {},\n        IA32_APIC_BASE => {\n            if *acpi_enabled {\n                low = APIC_MEM_ADDRESS as i32;\n                if *apic_enabled {\n                    low |= IA32_APIC_BASE_EN\n                }\n            }\n        },\n        IA32_BIOS_SIGN_ID => {},\n        MSR_PLATFORM_INFO => low = 1 << 8,\n        MISC_FEATURE_ENABLES => {},\n        IA32_MISC_ENABLE => {\n            // Enable Misc. Processor Features\n            low = 1 << 0; // fast string\n        },\n        IA32_RTIT_CTL => {}, // linux4\n        MSR_SMI_COUNT => {},\n        IA32_MCG_CAP => {},                        // netbsd\n        IA32_PERFEVTSEL0 | IA32_PERFEVTSEL1 => {}, // linux/9legacy\n        IA32_PMC0 | IA32_PMC1 => {},               // linux\n        IA32_PAT => {},\n        MSR_PKG_C2_RESIDENCY => {},\n        IA32_SPEC_CTRL => {},      // linux 5.19\n        IA32_TSX_CTRL => {},       // linux 5.19\n        MSR_TSX_FORCE_ABORT => {}, // linux 5.19\n        IA32_MCU_OPT_CTRL => {},   // linux 5.19\n        MSR_AMD64_LS_CFG => {},    // linux 5.19\n        MSR_AMD64_DE_CFG => {},    // linux 6.1\n        _ => {\n            dbg_log!(\"Unknown msr: {:x}\", index);\n            dbg_assert!(false);\n        },\n    }\n\n    write_reg32(EAX, low);\n    write_reg32(EDX, high);\n}\n#[no_mangle]\npub unsafe fn instr_0F33() {\n    // rdpmc\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr_0F34() {\n    // sysenter\n    let seg = *sysenter_cs & 0xFFFC;\n    if !*protected_mode || seg == 0 {\n        trigger_gp(0);\n        return;\n    }\n    else {\n        *flags &= !FLAG_VM & !FLAG_INTERRUPT;\n        *instruction_pointer = *sysenter_eip;\n        write_reg32(ESP, *sysenter_esp);\n        *sreg.offset(CS as isize) = seg as u16;\n        *segment_is_null.offset(CS as isize) = false;\n        *segment_limits.offset(CS as isize) = -1i32 as u32;\n        *segment_offsets.offset(CS as isize) = 0;\n        *segment_access_bytes.offset(CS as isize) = 0x80 | (0 << 5) | 0x10 | 0x08 | 0x02; // P dpl0 S E RW\n        update_cs_size(true);\n        *cpl = 0;\n        cpl_changed();\n        *sreg.offset(SS as isize) = (seg + 8) as u16;\n        *segment_is_null.offset(SS as isize) = false;\n        *segment_limits.offset(SS as isize) = -1i32 as u32;\n        *segment_offsets.offset(SS as isize) = 0;\n        *segment_access_bytes.offset(SS as isize) = 0x80 | (0 << 5) | 0x10 | 0x02; // P dpl0 S RW\n        *stack_size_32 = true;\n        update_state_flags();\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0F35() {\n    // sysexit\n    let seg = *sysenter_cs & 0xFFFC;\n    if !*protected_mode || 0 != *cpl || seg == 0 {\n        trigger_gp(0);\n        return;\n    }\n    else {\n        *instruction_pointer = read_reg32(EDX);\n        write_reg32(ESP, read_reg32(ECX));\n        *sreg.offset(CS as isize) = (seg + 16 | 3) as u16;\n        *segment_is_null.offset(CS as isize) = false;\n        *segment_limits.offset(CS as isize) = -1i32 as u32;\n        *segment_offsets.offset(CS as isize) = 0;\n        *segment_access_bytes.offset(CS as isize) = 0x80 | (3 << 5) | 0x10 | 0x08 | 0x02; // P dpl3 S E RW\n        update_cs_size(true);\n        *cpl = 3;\n        cpl_changed();\n        *sreg.offset(SS as isize) = (seg + 24 | 3) as u16;\n        *segment_is_null.offset(SS as isize) = false;\n        *segment_limits.offset(SS as isize) = -1i32 as u32;\n        *segment_offsets.offset(SS as isize) = 0;\n        *segment_access_bytes.offset(SS as isize) = 0x80 | (3 << 5) | 0x10 | 0x02; // P dpl3 S RW\n        *stack_size_32 = true;\n        update_state_flags();\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0F36() { undefined_instruction(); }\n#[no_mangle]\npub unsafe fn instr_0F37() {\n    // getsec\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr_0F38() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F39() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F3A() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F3B() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F3C() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F3D() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F3E() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F3F() { unimplemented_sse(); }\n\npub unsafe fn instr16_0F40_mem(addr: i32, r: i32) {\n    cmovcc16(test_o(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F40_reg(r1: i32, r: i32) { cmovcc16(test_o(), read_reg16(r1), r); }\npub unsafe fn instr32_0F40_mem(addr: i32, r: i32) {\n    cmovcc32(test_o(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F40_reg(r1: i32, r: i32) { cmovcc32(test_o(), read_reg32(r1), r); }\npub unsafe fn instr16_0F41_mem(addr: i32, r: i32) {\n    cmovcc16(!test_o(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F41_reg(r1: i32, r: i32) { cmovcc16(!test_o(), read_reg16(r1), r); }\npub unsafe fn instr32_0F41_mem(addr: i32, r: i32) {\n    cmovcc32(!test_o(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F41_reg(r1: i32, r: i32) { cmovcc32(!test_o(), read_reg32(r1), r); }\npub unsafe fn instr16_0F42_mem(addr: i32, r: i32) {\n    cmovcc16(test_b(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F42_reg(r1: i32, r: i32) { cmovcc16(test_b(), read_reg16(r1), r); }\npub unsafe fn instr32_0F42_mem(addr: i32, r: i32) {\n    cmovcc32(test_b(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F42_reg(r1: i32, r: i32) { cmovcc32(test_b(), read_reg32(r1), r); }\npub unsafe fn instr16_0F43_mem(addr: i32, r: i32) {\n    cmovcc16(!test_b(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F43_reg(r1: i32, r: i32) { cmovcc16(!test_b(), read_reg16(r1), r); }\npub unsafe fn instr32_0F43_mem(addr: i32, r: i32) {\n    cmovcc32(!test_b(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F43_reg(r1: i32, r: i32) { cmovcc32(!test_b(), read_reg32(r1), r); }\npub unsafe fn instr16_0F44_mem(addr: i32, r: i32) {\n    cmovcc16(test_z(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F44_reg(r1: i32, r: i32) { cmovcc16(test_z(), read_reg16(r1), r); }\npub unsafe fn instr32_0F44_mem(addr: i32, r: i32) {\n    cmovcc32(test_z(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F44_reg(r1: i32, r: i32) { cmovcc32(test_z(), read_reg32(r1), r); }\npub unsafe fn instr16_0F45_mem(addr: i32, r: i32) {\n    cmovcc16(!test_z(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F45_reg(r1: i32, r: i32) { cmovcc16(!test_z(), read_reg16(r1), r); }\npub unsafe fn instr32_0F45_mem(addr: i32, r: i32) {\n    cmovcc32(!test_z(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F45_reg(r1: i32, r: i32) { cmovcc32(!test_z(), read_reg32(r1), r); }\npub unsafe fn instr16_0F46_mem(addr: i32, r: i32) {\n    cmovcc16(test_be(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F46_reg(r1: i32, r: i32) { cmovcc16(test_be(), read_reg16(r1), r); }\npub unsafe fn instr32_0F46_mem(addr: i32, r: i32) {\n    cmovcc32(test_be(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F46_reg(r1: i32, r: i32) { cmovcc32(test_be(), read_reg32(r1), r); }\npub unsafe fn instr16_0F47_mem(addr: i32, r: i32) {\n    cmovcc16(!test_be(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F47_reg(r1: i32, r: i32) { cmovcc16(!test_be(), read_reg16(r1), r); }\npub unsafe fn instr32_0F47_mem(addr: i32, r: i32) {\n    cmovcc32(!test_be(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F47_reg(r1: i32, r: i32) { cmovcc32(!test_be(), read_reg32(r1), r); }\npub unsafe fn instr16_0F48_mem(addr: i32, r: i32) {\n    cmovcc16(test_s(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F48_reg(r1: i32, r: i32) { cmovcc16(test_s(), read_reg16(r1), r); }\npub unsafe fn instr32_0F48_mem(addr: i32, r: i32) {\n    cmovcc32(test_s(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F48_reg(r1: i32, r: i32) { cmovcc32(test_s(), read_reg32(r1), r); }\npub unsafe fn instr16_0F49_mem(addr: i32, r: i32) {\n    cmovcc16(!test_s(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F49_reg(r1: i32, r: i32) { cmovcc16(!test_s(), read_reg16(r1), r); }\npub unsafe fn instr32_0F49_mem(addr: i32, r: i32) {\n    cmovcc32(!test_s(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F49_reg(r1: i32, r: i32) { cmovcc32(!test_s(), read_reg32(r1), r); }\npub unsafe fn instr16_0F4A_mem(addr: i32, r: i32) {\n    cmovcc16(test_p(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F4A_reg(r1: i32, r: i32) { cmovcc16(test_p(), read_reg16(r1), r); }\npub unsafe fn instr32_0F4A_mem(addr: i32, r: i32) {\n    cmovcc32(test_p(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F4A_reg(r1: i32, r: i32) { cmovcc32(test_p(), read_reg32(r1), r); }\npub unsafe fn instr16_0F4B_mem(addr: i32, r: i32) {\n    cmovcc16(!test_p(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F4B_reg(r1: i32, r: i32) { cmovcc16(!test_p(), read_reg16(r1), r); }\npub unsafe fn instr32_0F4B_mem(addr: i32, r: i32) {\n    cmovcc32(!test_p(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F4B_reg(r1: i32, r: i32) { cmovcc32(!test_p(), read_reg32(r1), r); }\npub unsafe fn instr16_0F4C_mem(addr: i32, r: i32) {\n    cmovcc16(test_l(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F4C_reg(r1: i32, r: i32) { cmovcc16(test_l(), read_reg16(r1), r); }\npub unsafe fn instr32_0F4C_mem(addr: i32, r: i32) {\n    cmovcc32(test_l(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F4C_reg(r1: i32, r: i32) { cmovcc32(test_l(), read_reg32(r1), r); }\npub unsafe fn instr16_0F4D_mem(addr: i32, r: i32) {\n    cmovcc16(!test_l(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F4D_reg(r1: i32, r: i32) { cmovcc16(!test_l(), read_reg16(r1), r); }\npub unsafe fn instr32_0F4D_mem(addr: i32, r: i32) {\n    cmovcc32(!test_l(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F4D_reg(r1: i32, r: i32) { cmovcc32(!test_l(), read_reg32(r1), r); }\npub unsafe fn instr16_0F4E_mem(addr: i32, r: i32) {\n    cmovcc16(test_le(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F4E_reg(r1: i32, r: i32) { cmovcc16(test_le(), read_reg16(r1), r); }\npub unsafe fn instr32_0F4E_mem(addr: i32, r: i32) {\n    cmovcc32(test_le(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F4E_reg(r1: i32, r: i32) { cmovcc32(test_le(), read_reg32(r1), r); }\npub unsafe fn instr16_0F4F_mem(addr: i32, r: i32) {\n    cmovcc16(!test_le(), return_on_pagefault!(safe_read16(addr)), r);\n}\npub unsafe fn instr16_0F4F_reg(r1: i32, r: i32) { cmovcc16(!test_le(), read_reg16(r1), r); }\npub unsafe fn instr32_0F4F_mem(addr: i32, r: i32) {\n    cmovcc32(!test_le(), return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr32_0F4F_reg(r1: i32, r: i32) { cmovcc32(!test_le(), read_reg32(r1), r); }\n\n#[no_mangle]\npub unsafe fn instr_0F50_reg(r1: i32, r2: i32) {\n    // movmskps r, xmm\n    let source = read_xmm128s(r1);\n    let data = (source.u32[0] >> 31\n        | source.u32[1] >> 31 << 1\n        | source.u32[2] >> 31 << 2\n        | source.u32[3] >> 31 << 3) as i32;\n    write_reg32(r2, data);\n}\n#[no_mangle]\npub unsafe fn instr_0F50_mem(_addr: i32, _r1: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660F50_reg(r1: i32, r2: i32) {\n    // movmskpd r, xmm\n    let source = read_xmm128s(r1);\n    let data = (source.u32[1] >> 31 | source.u32[3] >> 31 << 1) as i32;\n    write_reg32(r2, data);\n}\n#[no_mangle]\npub unsafe fn instr_660F50_mem(_addr: i32, _r1: i32) { trigger_ud(); }\n\n#[no_mangle]\npub unsafe fn instr_0F51(source: reg128, r: i32) {\n    // sqrtps xmm, xmm/mem128\n    // XXX: Should round according to round control\n    let result = reg128 {\n        f32: [\n            source.f32[0].sqrt(),\n            source.f32[1].sqrt(),\n            source.f32[2].sqrt(),\n            source.f32[3].sqrt(),\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F51_reg(r1: i32, r2: i32) { instr_0F51(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F51_mem(addr: i32, r: i32) {\n    instr_0F51(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F51(source: reg128, r: i32) {\n    // sqrtpd xmm, xmm/mem128\n    // XXX: Should round according to round control\n    let result = reg128 {\n        f64: [source.f64[0].sqrt(), source.f64[1].sqrt()],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F51_reg(r1: i32, r2: i32) { instr_660F51(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F51_mem(addr: i32, r: i32) {\n    instr_660F51(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F51(source: u64, r: i32) {\n    // sqrtsd xmm, xmm/mem64\n    // XXX: Should round according to round control\n    write_xmm_f64(r, f64::from_bits(source).sqrt());\n}\npub unsafe fn instr_F20F51_reg(r1: i32, r2: i32) { instr_F20F51(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F51_mem(addr: i32, r: i32) {\n    instr_F20F51(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F51(source: f32, r: i32) {\n    // sqrtss xmm, xmm/mem32\n    // XXX: Should round according to round control\n    write_xmm_f32(r, source.sqrt());\n}\npub unsafe fn instr_F30F51_reg(r1: i32, r2: i32) { instr_F30F51(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F51_mem(addr: i32, r: i32) {\n    instr_F30F51(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F52(source: reg128, r: i32) {\n    // rcpps xmm1, xmm2/m128\n    let result = reg128 {\n        f32: [\n            1.0 / source.f32[0].sqrt(),\n            1.0 / source.f32[1].sqrt(),\n            1.0 / source.f32[2].sqrt(),\n            1.0 / source.f32[3].sqrt(),\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F52_reg(r1: i32, r2: i32) { instr_0F52(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F52_mem(addr: i32, r: i32) {\n    instr_0F52(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F52(source: f32, r: i32) {\n    // rsqrtss xmm1, xmm2/m32\n    write_xmm_f32(r, 1.0 / source.sqrt());\n}\npub unsafe fn instr_F30F52_reg(r1: i32, r2: i32) { instr_F30F52(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F52_mem(addr: i32, r: i32) {\n    instr_F30F52(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F53(source: reg128, r: i32) {\n    // rcpps xmm, xmm/m128\n    let result = reg128 {\n        f32: [\n            1.0 / source.f32[0],\n            1.0 / source.f32[1],\n            1.0 / source.f32[2],\n            1.0 / source.f32[3],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F53_reg(r1: i32, r2: i32) { instr_0F53(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F53_mem(addr: i32, r: i32) {\n    instr_0F53(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F53(source: f32, r: i32) {\n    // rcpss xmm, xmm/m32\n    write_xmm_f32(r, 1.0 / source);\n}\npub unsafe fn instr_F30F53_reg(r1: i32, r2: i32) { instr_F30F53(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F53_mem(addr: i32, r: i32) {\n    instr_F30F53(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F54(source: reg128, r: i32) {\n    // andps xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    pand_r128(source, r);\n}\npub unsafe fn instr_0F54_reg(r1: i32, r2: i32) { instr_0F54(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F54_mem(addr: i32, r: i32) {\n    instr_0F54(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F54(source: reg128, r: i32) {\n    // andpd xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    pand_r128(source, r);\n}\npub unsafe fn instr_660F54_reg(r1: i32, r2: i32) { instr_660F54(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F54_mem(addr: i32, r: i32) {\n    instr_660F54(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F55(source: reg128, r: i32) {\n    // andnps xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    pandn_r128(source, r);\n}\npub unsafe fn instr_0F55_reg(r1: i32, r2: i32) { instr_0F55(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F55_mem(addr: i32, r: i32) {\n    instr_0F55(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F55(source: reg128, r: i32) {\n    // andnpd xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    pandn_r128(source, r);\n}\npub unsafe fn instr_660F55_reg(r1: i32, r2: i32) { instr_660F55(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F55_mem(addr: i32, r: i32) {\n    instr_660F55(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F56(source: reg128, r: i32) {\n    // orps xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    por_r128(source, r);\n}\npub unsafe fn instr_0F56_reg(r1: i32, r2: i32) { instr_0F56(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F56_mem(addr: i32, r: i32) {\n    instr_0F56(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F56(source: reg128, r: i32) {\n    // orpd xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    por_r128(source, r);\n}\npub unsafe fn instr_660F56_reg(r1: i32, r2: i32) { instr_660F56(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F56_mem(addr: i32, r: i32) {\n    instr_660F56(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F57(source: reg128, r: i32) {\n    // xorps xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    pxor_r128(source, r);\n}\npub unsafe fn instr_0F57_reg(r1: i32, r2: i32) { instr_0F57(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F57_mem(addr: i32, r: i32) {\n    instr_0F57(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F57(source: reg128, r: i32) {\n    // xorpd xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    pxor_r128(source, r);\n}\npub unsafe fn instr_660F57_reg(r1: i32, r2: i32) { instr_660F57(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F57_mem(addr: i32, r: i32) {\n    instr_660F57(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F58(source: reg128, r: i32) {\n    // addps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f32: [\n            source.f32[0] + destination.f32[0],\n            source.f32[1] + destination.f32[1],\n            source.f32[2] + destination.f32[2],\n            source.f32[3] + destination.f32[3],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F58_reg(r1: i32, r2: i32) { instr_0F58(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F58_mem(addr: i32, r: i32) {\n    instr_0F58(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F58(source: reg128, r: i32) {\n    // addpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f64: [\n            source.f64[0] + destination.f64[0],\n            source.f64[1] + destination.f64[1],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F58_reg(r1: i32, r2: i32) { instr_660F58(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F58_mem(addr: i32, r: i32) {\n    instr_660F58(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F58(source: u64, r: i32) {\n    // addsd xmm, xmm/mem64\n    let destination = read_xmm64s(r);\n    write_xmm_f64(r, f64::from_bits(source) + f64::from_bits(destination));\n}\npub unsafe fn instr_F20F58_reg(r1: i32, r2: i32) { instr_F20F58(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F58_mem(addr: i32, r: i32) {\n    instr_F20F58(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F58(source: f32, r: i32) {\n    // addss xmm, xmm/mem32\n    let destination = read_xmm_f32(r);\n    let result = source + destination;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F58_reg(r1: i32, r2: i32) { instr_F30F58(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F58_mem(addr: i32, r: i32) {\n    instr_F30F58(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F59(source: reg128, r: i32) {\n    // mulps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f32: [\n            source.f32[0] * destination.f32[0],\n            source.f32[1] * destination.f32[1],\n            source.f32[2] * destination.f32[2],\n            source.f32[3] * destination.f32[3],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F59_reg(r1: i32, r2: i32) { instr_0F59(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F59_mem(addr: i32, r: i32) {\n    instr_0F59(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F59(source: reg128, r: i32) {\n    // mulpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f64: [\n            source.f64[0] * destination.f64[0],\n            source.f64[1] * destination.f64[1],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F59_reg(r1: i32, r2: i32) { instr_660F59(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F59_mem(addr: i32, r: i32) {\n    instr_660F59(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F59(source: u64, r: i32) {\n    // mulsd xmm, xmm/mem64\n    let destination = read_xmm64s(r);\n    write_xmm_f64(r, f64::from_bits(source) * f64::from_bits(destination));\n}\npub unsafe fn instr_F20F59_reg(r1: i32, r2: i32) { instr_F20F59(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F59_mem(addr: i32, r: i32) {\n    instr_F20F59(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F59(source: f32, r: i32) {\n    // mulss xmm, xmm/mem32\n    let destination = read_xmm_f32(r);\n    let result = source * destination;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F59_reg(r1: i32, r2: i32) { instr_F30F59(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F59_mem(addr: i32, r: i32) {\n    instr_F30F59(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F5A(source: u64, r: i32) {\n    // cvtps2pd xmm1, xmm2/m64\n    let source: [f32; 2] = std::mem::transmute(source);\n    let result = reg128 {\n        f64: [source[0] as f64, source[1] as f64],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F5A_reg(r1: i32, r2: i32) { instr_0F5A(read_xmm64s(r1), r2); }\npub unsafe fn instr_0F5A_mem(addr: i32, r: i32) {\n    instr_0F5A(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F5A(source: reg128, r: i32) {\n    // cvtpd2ps xmm1, xmm2/m128\n    let result = reg128 {\n        // XXX: These conversions are lossy and should round according to the round control\n        f32: [source.f64[0] as f32, source.f64[1] as f32, 0., 0.],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F5A_reg(r1: i32, r2: i32) { instr_660F5A(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F5A_mem(addr: i32, r: i32) {\n    instr_660F5A(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F5A(source: u64, r: i32) {\n    // cvtsd2ss xmm1, xmm2/m64\n    // XXX: This conversions is lossy and should round according to the round control\n    write_xmm_f32(r, f64::from_bits(source) as f32);\n}\npub unsafe fn instr_F20F5A_reg(r1: i32, r2: i32) { instr_F20F5A(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F5A_mem(addr: i32, r: i32) {\n    instr_F20F5A(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F5A(source: f32, r: i32) {\n    // cvtss2sd xmm1, xmm2/m32\n    write_xmm_f64(r, source as f64);\n}\npub unsafe fn instr_F30F5A_reg(r1: i32, r2: i32) { instr_F30F5A(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F5A_mem(addr: i32, r: i32) {\n    instr_F30F5A(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F5B(source: reg128, r: i32) {\n    // cvtdq2ps xmm1, xmm2/m128\n    // XXX: Should round according to round control\n    let result = reg128 {\n        f32: [\n            // XXX: Precision exception\n            source.i32[0] as f32,\n            source.i32[1] as f32,\n            source.i32[2] as f32,\n            source.i32[3] as f32,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F5B_reg(r1: i32, r2: i32) { instr_0F5B(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F5B_mem(addr: i32, r: i32) {\n    instr_0F5B(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F5B(source: reg128, r: i32) {\n    // cvtps2dq xmm1, xmm2/m128\n    let result = reg128 {\n        i32: [\n            // XXX: Precision exception\n            sse_convert_f32_to_i32(source.f32[0]),\n            sse_convert_f32_to_i32(source.f32[1]),\n            sse_convert_f32_to_i32(source.f32[2]),\n            sse_convert_f32_to_i32(source.f32[3]),\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F5B_reg(r1: i32, r2: i32) { instr_660F5B(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F5B_mem(addr: i32, r: i32) {\n    instr_660F5B(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F5B(source: reg128, r: i32) {\n    // cvttps2dq xmm1, xmm2/m128\n    let result = reg128 {\n        i32: [\n            sse_convert_with_truncation_f32_to_i32(source.f32[0]),\n            sse_convert_with_truncation_f32_to_i32(source.f32[1]),\n            sse_convert_with_truncation_f32_to_i32(source.f32[2]),\n            sse_convert_with_truncation_f32_to_i32(source.f32[3]),\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_F30F5B_reg(r1: i32, r2: i32) { instr_F30F5B(read_xmm128s(r1), r2); }\npub unsafe fn instr_F30F5B_mem(addr: i32, r: i32) {\n    instr_F30F5B(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F5C(source: reg128, r: i32) {\n    // subps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f32: [\n            destination.f32[0] - source.f32[0],\n            destination.f32[1] - source.f32[1],\n            destination.f32[2] - source.f32[2],\n            destination.f32[3] - source.f32[3],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F5C_reg(r1: i32, r2: i32) { instr_0F5C(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F5C_mem(addr: i32, r: i32) {\n    instr_0F5C(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F5C(source: reg128, r: i32) {\n    // subpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f64: [\n            destination.f64[0] - source.f64[0],\n            destination.f64[1] - source.f64[1],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F5C_reg(r1: i32, r2: i32) { instr_660F5C(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F5C_mem(addr: i32, r: i32) {\n    instr_660F5C(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F5C(source: u64, r: i32) {\n    // subsd xmm, xmm/mem64\n    let destination = read_xmm64s(r);\n    write_xmm_f64(r, f64::from_bits(destination) - f64::from_bits(source));\n}\npub unsafe fn instr_F20F5C_reg(r1: i32, r2: i32) { instr_F20F5C(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F5C_mem(addr: i32, r: i32) {\n    instr_F20F5C(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F5C(source: f32, r: i32) {\n    // subss xmm, xmm/mem32\n    let destination = read_xmm_f32(r);\n    let result = destination - source;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F5C_reg(r1: i32, r2: i32) { instr_F30F5C(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F5C_mem(addr: i32, r: i32) {\n    instr_F30F5C(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F5D(source: reg128, r: i32) {\n    // minps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f32: [\n            sse_min(destination.f32[0] as f64, source.f32[0] as f64) as f32,\n            sse_min(destination.f32[1] as f64, source.f32[1] as f64) as f32,\n            sse_min(destination.f32[2] as f64, source.f32[2] as f64) as f32,\n            sse_min(destination.f32[3] as f64, source.f32[3] as f64) as f32,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F5D_reg(r1: i32, r2: i32) { instr_0F5D(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F5D_mem(addr: i32, r: i32) {\n    instr_0F5D(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F5D(source: reg128, r: i32) {\n    // minpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f64: [\n            sse_min(destination.f64[0], source.f64[0]),\n            sse_min(destination.f64[1], source.f64[1]),\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F5D_reg(r1: i32, r2: i32) { instr_660F5D(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F5D_mem(addr: i32, r: i32) {\n    instr_660F5D(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F5D(source: u64, r: i32) {\n    // minsd xmm, xmm/mem64\n    let destination = read_xmm64s(r);\n    write_xmm_f64(\n        r,\n        sse_min(f64::from_bits(destination), f64::from_bits(source)),\n    );\n}\npub unsafe fn instr_F20F5D_reg(r1: i32, r2: i32) { instr_F20F5D(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F5D_mem(addr: i32, r: i32) {\n    instr_F20F5D(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F5D(source: f32, r: i32) {\n    // minss xmm, xmm/mem32\n    let destination = read_xmm_f32(r);\n    let result = sse_min(destination as f64, source as f64) as f32;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F5D_reg(r1: i32, r2: i32) { instr_F30F5D(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F5D_mem(addr: i32, r: i32) {\n    instr_F30F5D(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F5E(source: reg128, r: i32) {\n    // divps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f32: [\n            destination.f32[0] / source.f32[0],\n            destination.f32[1] / source.f32[1],\n            destination.f32[2] / source.f32[2],\n            destination.f32[3] / source.f32[3],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F5E_reg(r1: i32, r2: i32) { instr_0F5E(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F5E_mem(addr: i32, r: i32) {\n    instr_0F5E(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F5E(source: reg128, r: i32) {\n    // divpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f64: [\n            destination.f64[0] / source.f64[0],\n            destination.f64[1] / source.f64[1],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F5E_reg(r1: i32, r2: i32) { instr_660F5E(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F5E_mem(addr: i32, r: i32) {\n    instr_660F5E(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F5E(source: u64, r: i32) {\n    // divsd xmm, xmm/mem64\n    let destination = read_xmm64s(r);\n    write_xmm_f64(r, f64::from_bits(destination) / f64::from_bits(source));\n}\npub unsafe fn instr_F20F5E_reg(r1: i32, r2: i32) { instr_F20F5E(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F5E_mem(addr: i32, r: i32) {\n    instr_F20F5E(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F5E(source: f32, r: i32) {\n    // divss xmm, xmm/mem32\n    let destination = read_xmm_f32(r);\n    let result = destination / source;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F5E_reg(r1: i32, r2: i32) { instr_F30F5E(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F5E_mem(addr: i32, r: i32) {\n    instr_F30F5E(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F5F(source: reg128, r: i32) {\n    // maxps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f32: [\n            sse_max(destination.f32[0] as f64, source.f32[0] as f64) as f32,\n            sse_max(destination.f32[1] as f64, source.f32[1] as f64) as f32,\n            sse_max(destination.f32[2] as f64, source.f32[2] as f64) as f32,\n            sse_max(destination.f32[3] as f64, source.f32[3] as f64) as f32,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0F5F_reg(r1: i32, r2: i32) { instr_0F5F(read_xmm128s(r1), r2); }\npub unsafe fn instr_0F5F_mem(addr: i32, r: i32) {\n    instr_0F5F(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F5F(source: reg128, r: i32) {\n    // maxpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        f64: [\n            sse_max(destination.f64[0], source.f64[0]),\n            sse_max(destination.f64[1], source.f64[1]),\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F5F_reg(r1: i32, r2: i32) { instr_660F5F(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F5F_mem(addr: i32, r: i32) {\n    instr_660F5F(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F5F(source: u64, r: i32) {\n    // maxsd xmm, xmm/mem64\n    let destination = read_xmm64s(r);\n    write_xmm_f64(\n        r,\n        sse_max(f64::from_bits(destination), f64::from_bits(source)),\n    );\n}\npub unsafe fn instr_F20F5F_reg(r1: i32, r2: i32) { instr_F20F5F(read_xmm64s(r1), r2); }\npub unsafe fn instr_F20F5F_mem(addr: i32, r: i32) {\n    instr_F20F5F(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F30F5F(source: f32, r: i32) {\n    // maxss xmm, xmm/mem32\n    let destination = read_xmm_f32(r);\n    let result = sse_max(destination as f64, source as f64) as f32;\n    write_xmm_f32(r, result);\n}\npub unsafe fn instr_F30F5F_reg(r1: i32, r2: i32) { instr_F30F5F(read_xmm_f32(r1), r2); }\npub unsafe fn instr_F30F5F_mem(addr: i32, r: i32) {\n    instr_F30F5F(return_on_pagefault!(safe_read_f32(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F60(source: i32, r: i32) {\n    // punpcklbw mm, mm/m32\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 4] = i32::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..4 {\n        result[2 * i + 0] = destination[i];\n        result[2 * i + 1] = source[i];\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F60_reg(r1: i32, r2: i32) { instr_0F60(read_mmx32s(r1), r2); }\npub unsafe fn instr_0F60_mem(addr: i32, r: i32) {\n    instr_0F60(return_on_pagefault!(safe_read32s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F60(source: reg128, r: i32) {\n    // punpcklbw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination: [u8; 8] = u64::to_le_bytes(read_xmm64s(r));\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u8[2 * i + 0] = destination[i];\n        result.u8[2 * i + 1] = source.u8[i];\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F60_reg(r1: i32, r2: i32) { instr_660F60(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F60_mem(addr: i32, r: i32) {\n    instr_660F60(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F61(source: i32, r: i32) {\n    // punpcklwd mm, mm/m32\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 2] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..2 {\n        result[2 * i + 0] = destination[i];\n        result[2 * i + 1] = source[i];\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F61_reg(r1: i32, r2: i32) { instr_0F61(read_mmx32s(r1), r2); }\npub unsafe fn instr_0F61_mem(addr: i32, r: i32) {\n    instr_0F61(return_on_pagefault!(safe_read32s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F61(source: reg128, r: i32) {\n    // punpcklwd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination: [u16; 4] = std::mem::transmute(read_xmm64s(r));\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..4 {\n        result.u16[2 * i + 0] = destination[i];\n        result.u16[2 * i + 1] = source.u16[i];\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F61_reg(r1: i32, r2: i32) { instr_660F61(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F61_mem(addr: i32, r: i32) {\n    instr_660F61(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F62(source: i32, r: i32) {\n    // punpckldq mm, mm/m32\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(\n        r,\n        (destination & 0xFFFF_FFFF) | (source as u32 as u64) << 32,\n    );\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F62_reg(r1: i32, r2: i32) { instr_0F62(read_mmx32s(r1), r2); }\npub unsafe fn instr_0F62_mem(addr: i32, r: i32) {\n    instr_0F62(return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr_660F62(source: reg128, r: i32) {\n    // punpckldq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[0] as i32,\n        source.u32[0] as i32,\n        destination.u32[1] as i32,\n        source.u32[1] as i32,\n    );\n}\npub unsafe fn instr_660F62_reg(r1: i32, r2: i32) { instr_660F62(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F62_mem(addr: i32, r: i32) {\n    instr_660F62(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F63(source: u64, r: i32) {\n    // packsswb mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result: [u8; 8] = [0; 8];\n    for i in 0..4 {\n        result[i + 0] = saturate_sw_to_sb(destination[i] as i32);\n        result[i + 4] = saturate_sw_to_sb(source[i] as i32);\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F63_reg(r1: i32, r2: i32) { instr_0F63(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F63_mem(addr: i32, r: i32) {\n    instr_0F63(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F63(source: reg128, r: i32) {\n    // packsswb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u8[i + 0] = saturate_sw_to_sb(destination.u16[i] as i32);\n        result.u8[i + 8] = saturate_sw_to_sb(source.u16[i] as i32);\n    }\n    write_xmm_reg128(r, result)\n}\npub unsafe fn instr_660F63_reg(r1: i32, r2: i32) { instr_660F63(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F63_mem(addr: i32, r: i32) {\n    instr_660F63(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F64(source: u64, r: i32) {\n    // pcmpgtb mm, mm/m64\n    let destination: [i8; 8] = std::mem::transmute(read_mmx64s(r));\n    let source: [i8; 8] = std::mem::transmute(source);\n    let mut result: [u8; 8] = [0; 8];\n    for i in 0..8 {\n        result[i] = if destination[i] > source[i] { 255 } else { 0 };\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F64_reg(r1: i32, r2: i32) { instr_0F64(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F64_mem(addr: i32, r: i32) {\n    instr_0F64(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F64(source: reg128, r: i32) {\n    // pcmpgtb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = if destination.i8[i] as i32 > source.i8[i] as i32 { 255 } else { 0 };\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F64_reg(r1: i32, r2: i32) { instr_660F64(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F64_mem(addr: i32, r: i32) {\n    instr_660F64(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F65(source: u64, r: i32) {\n    // pcmpgtw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result: [u16; 4] = [0; 4];\n    for i in 0..4 {\n        result[i] = if destination[i] > source[i] { 0xFFFF } else { 0 }\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F65_reg(r1: i32, r2: i32) { instr_0F65(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F65_mem(addr: i32, r: i32) {\n    instr_0F65(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F65(source: reg128, r: i32) {\n    // pcmpgtw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = if destination.i16[i] > source.i16[i] { 0xFFFF } else { 0 };\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F65_reg(r1: i32, r2: i32) { instr_660F65(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F65_mem(addr: i32, r: i32) {\n    instr_660F65(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F66(source: u64, r: i32) {\n    // pcmpgtd mm, mm/m64\n    let destination: [i32; 2] = std::mem::transmute(read_mmx64s(r));\n    let source: [i32; 2] = std::mem::transmute(source);\n    let mut result = [0; 2];\n    for i in 0..2 {\n        result[i] = if destination[i] > source[i] { -1 } else { 0 }\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F66_reg(r1: i32, r2: i32) { instr_0F66(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F66_mem(addr: i32, r: i32) {\n    instr_0F66(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F66(source: reg128, r: i32) {\n    // pcmpgtd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        if destination.i32[0] > source.i32[0] { -1 } else { 0 },\n        if destination.i32[1] > source.i32[1] { -1 } else { 0 },\n        if destination.i32[2] > source.i32[2] { -1 } else { 0 },\n        if destination.i32[3] > source.i32[3] { -1 } else { 0 },\n    );\n}\npub unsafe fn instr_660F66_reg(r1: i32, r2: i32) { instr_660F66(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F66_mem(addr: i32, r: i32) {\n    instr_660F66(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F67(source: u64, r: i32) {\n    // packuswb mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 8];\n    for i in 0..4 {\n        result[i + 0] = saturate_sw_to_ub(destination[i]);\n        result[i + 4] = saturate_sw_to_ub(source[i]);\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F67_reg(r1: i32, r2: i32) { instr_0F67(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F67_mem(addr: i32, r: i32) {\n    instr_0F67(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F67(source: reg128, r: i32) {\n    // packuswb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u8[i + 0] = saturate_sw_to_ub(destination.u16[i]);\n        result.u8[i + 8] = saturate_sw_to_ub(source.u16[i]);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F67_reg(r1: i32, r2: i32) { instr_660F67(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F67_mem(addr: i32, r: i32) {\n    instr_660F67(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F68(source: u64, r: i32) {\n    // punpckhbw mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result: [u8; 8] = [0; 8];\n    for i in 0..4 {\n        result[2 * i + 0] = destination[i + 4];\n        result[2 * i + 1] = source[i + 4];\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F68_reg(r1: i32, r2: i32) { instr_0F68(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F68_mem(addr: i32, r: i32) {\n    instr_0F68(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F68(source: reg128, r: i32) {\n    // punpckhbw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u8[2 * i + 0] = destination.u8[i + 8];\n        result.u8[2 * i + 1] = source.u8[i + 8];\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F68_reg(r1: i32, r2: i32) { instr_660F68(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F68_mem(addr: i32, r: i32) {\n    instr_660F68(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F69(source: u64, r: i32) {\n    // punpckhwd mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let result = [destination[2], source[2], destination[3], source[3]];\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F69_reg(r1: i32, r2: i32) { instr_0F69(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F69_mem(addr: i32, r: i32) {\n    instr_0F69(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F69(source: reg128, r: i32) {\n    // punpckhwd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..4 {\n        result.u16[2 * i + 0] = destination.u16[i + 4];\n        result.u16[2 * i + 1] = source.u16[i + 4];\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F69_reg(r1: i32, r2: i32) { instr_660F69(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F69_mem(addr: i32, r: i32) {\n    instr_660F69(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F6A(source: u64, r: i32) {\n    // punpckhdq mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, (destination >> 32) | (source >> 32 << 32));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F6A_reg(r1: i32, r2: i32) { instr_0F6A(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F6A_mem(addr: i32, r: i32) {\n    instr_0F6A(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F6A(source: reg128, r: i32) {\n    // punpckhdq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[2] as i32,\n        source.u32[2] as i32,\n        destination.u32[3] as i32,\n        source.u32[3] as i32,\n    );\n}\npub unsafe fn instr_660F6A_reg(r1: i32, r2: i32) { instr_660F6A(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F6A_mem(addr: i32, r: i32) {\n    instr_660F6A(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F6B(source: u64, r: i32) {\n    // packssdw mm, mm/m64\n    let destination: [u32; 2] = std::mem::transmute(read_mmx64s(r));\n    let source: [u32; 2] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..2 {\n        result[i + 0] = saturate_sd_to_sw(destination[i]);\n        result[i + 2] = saturate_sd_to_sw(source[i]);\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F6B_reg(r1: i32, r2: i32) { instr_0F6B(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F6B_mem(addr: i32, r: i32) {\n    instr_0F6B(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F6B(source: reg128, r: i32) {\n    // packssdw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..4 {\n        result.u16[i + 0] = saturate_sd_to_sw(destination.u32[i]);\n        result.u16[i + 4] = saturate_sd_to_sw(source.u32[i]);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F6B_reg(r1: i32, r2: i32) { instr_660F6B(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F6B_mem(addr: i32, r: i32) {\n    instr_660F6B(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F6C_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0F6C_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660F6C(source: reg128, r: i32) {\n    // punpcklqdq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[0] as i32,\n        destination.u32[1] as i32,\n        source.u32[0] as i32,\n        source.u32[1] as i32,\n    );\n}\npub unsafe fn instr_660F6C_reg(r1: i32, r2: i32) { instr_660F6C(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F6C_mem(addr: i32, r: i32) {\n    instr_660F6C(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F6D_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0F6D_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660F6D(source: reg128, r: i32) {\n    // punpckhqdq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[2] as i32,\n        destination.u32[3] as i32,\n        source.u32[2] as i32,\n        source.u32[3] as i32,\n    );\n}\npub unsafe fn instr_660F6D_reg(r1: i32, r2: i32) { instr_660F6D(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F6D_mem(addr: i32, r: i32) {\n    instr_660F6D(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F6E(source: i32, r: i32) {\n    // movd mm, r/m32\n    write_mmx_reg64(r, source as u32 as u64);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F6E_reg(r1: i32, r2: i32) { instr_0F6E(read_reg32(r1), r2); }\npub unsafe fn instr_0F6E_mem(addr: i32, r: i32) {\n    instr_0F6E(return_on_pagefault!(safe_read32s(addr)), r);\n}\npub unsafe fn instr_660F6E(source: i32, r: i32) {\n    // movd mm, r/m32\n    write_xmm128(r, source, 0, 0, 0);\n}\npub unsafe fn instr_660F6E_reg(r1: i32, r2: i32) { instr_660F6E(read_reg32(r1), r2); }\npub unsafe fn instr_660F6E_mem(addr: i32, r: i32) {\n    instr_660F6E(return_on_pagefault!(safe_read32s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F6F(source: u64, r: i32) {\n    // movq mm, mm/m64\n    write_mmx_reg64(r, source);\n    transition_fpu_to_mmx();\n}\n#[no_mangle]\npub unsafe fn instr_0F6F_reg(r1: i32, r2: i32) { instr_0F6F(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F6F_mem(addr: i32, r: i32) {\n    instr_0F6F(return_on_pagefault!(safe_read64s(addr)), r);\n}\npub unsafe fn instr_660F6F(source: reg128, r: i32) {\n    // movdqa xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    mov_rm_r128(source, r);\n}\npub unsafe fn instr_660F6F_reg(r1: i32, r2: i32) { instr_660F6F(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F6F_mem(addr: i32, r: i32) {\n    instr_660F6F(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_F30F6F(source: reg128, r: i32) {\n    // movdqu xmm, xmm/m128\n    mov_rm_r128(source, r);\n}\npub unsafe fn instr_F30F6F_reg(r1: i32, r2: i32) { instr_F30F6F(read_xmm128s(r1), r2); }\npub unsafe fn instr_F30F6F_mem(addr: i32, r: i32) {\n    instr_F30F6F(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F70(source: u64, r: i32, imm8: i32) {\n    // pshufw mm1, mm2/m64, imm8\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = source[(imm8 >> (2 * i) & 3) as usize]\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F70_reg(r1: i32, r2: i32, imm: i32) { instr_0F70(read_mmx64s(r1), r2, imm); }\npub unsafe fn instr_0F70_mem(addr: i32, r: i32, imm: i32) {\n    instr_0F70(return_on_pagefault!(safe_read64s(addr)), r, imm);\n}\npub unsafe fn instr_660F70(source: reg128, r: i32, imm8: i32) {\n    // pshufd xmm, xmm/mem128, imm8\n    // XXX: Aligned access or #gp\n    write_xmm128(\n        r,\n        source.u32[(imm8 & 3) as usize] as i32,\n        source.u32[(imm8 >> 2 & 3) as usize] as i32,\n        source.u32[(imm8 >> 4 & 3) as usize] as i32,\n        source.u32[(imm8 >> 6 & 3) as usize] as i32,\n    );\n}\npub unsafe fn instr_660F70_reg(r1: i32, r2: i32, imm: i32) {\n    instr_660F70(read_xmm128s(r1), r2, imm);\n}\npub unsafe fn instr_660F70_mem(addr: i32, r: i32, imm: i32) {\n    instr_660F70(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\n\n#[no_mangle]\npub unsafe fn instr_F20F70(source: reg128, r: i32, imm8: i32) {\n    // pshuflw xmm, xmm/m128, imm8\n    // XXX: Aligned access or #gp\n    write_xmm128(\n        r,\n        source.u16[(imm8 & 3) as usize] as i32\n            | (source.u16[(imm8 >> 2 & 3) as usize] as i32) << 16,\n        source.u16[(imm8 >> 4 & 3) as usize] as i32\n            | (source.u16[(imm8 >> 6 & 3) as usize] as i32) << 16,\n        source.u32[2] as i32,\n        source.u32[3] as i32,\n    );\n}\npub unsafe fn instr_F20F70_reg(r1: i32, r2: i32, imm: i32) {\n    instr_F20F70(read_xmm128s(r1), r2, imm);\n}\npub unsafe fn instr_F20F70_mem(addr: i32, r: i32, imm: i32) {\n    instr_F20F70(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\n#[no_mangle]\npub unsafe fn instr_F30F70(source: reg128, r: i32, imm8: i32) {\n    // pshufhw xmm, xmm/m128, imm8\n    // XXX: Aligned access or #gp\n    write_xmm128(\n        r,\n        source.u32[0] as i32,\n        source.u32[1] as i32,\n        source.u16[(imm8 & 3 | 4) as usize] as i32\n            | (source.u16[(imm8 >> 2 & 3 | 4) as usize] as i32) << 16,\n        source.u16[(imm8 >> 4 & 3 | 4) as usize] as i32\n            | (source.u16[(imm8 >> 6 & 3 | 4) as usize] as i32) << 16,\n    );\n}\npub unsafe fn instr_F30F70_reg(r1: i32, r2: i32, imm: i32) {\n    instr_F30F70(read_xmm128s(r1), r2, imm);\n}\npub unsafe fn instr_F30F70_mem(addr: i32, r: i32, imm: i32) {\n    instr_F30F70(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\npub unsafe fn instr_0F71_2_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_0F71_4_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_0F71_6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0F71_2_reg(r: i32, imm8: i32) {\n    // psrlw mm, imm8\n    psrlw_r64(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_0F71_4_reg(r: i32, imm8: i32) {\n    // psraw mm, imm8\n    psraw_r64(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_0F71_6_reg(r: i32, imm8: i32) {\n    // psllw mm, imm8\n    psllw_r64(r, imm8 as u64);\n}\npub unsafe fn instr_660F71_2_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F71_4_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F71_6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660F71_2_reg(r: i32, imm8: i32) {\n    // psrlw xmm, imm8\n    psrlw_r128(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_660F71_4_reg(r: i32, imm8: i32) {\n    // psraw xmm, imm8\n    psraw_r128(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_660F71_6_reg(r: i32, imm8: i32) {\n    // psllw xmm, imm8\n    psllw_r128(r, imm8 as u64);\n}\npub unsafe fn instr_0F72_2_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_0F72_4_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_0F72_6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0F72_2_reg(r: i32, imm8: i32) {\n    // psrld mm, imm8\n    psrld_r64(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_0F72_4_reg(r: i32, imm8: i32) {\n    // psrad mm, imm8\n    psrad_r64(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_0F72_6_reg(r: i32, imm8: i32) {\n    // pslld mm, imm8\n    pslld_r64(r, imm8 as u64);\n}\npub unsafe fn instr_660F72_2_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F72_4_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F72_6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660F72_2_reg(r: i32, imm8: i32) {\n    // psrld xmm, imm8\n    psrld_r128(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_660F72_4_reg(r: i32, imm8: i32) {\n    // psrad xmm, imm8\n    psrad_r128(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_660F72_6_reg(r: i32, imm8: i32) {\n    // pslld xmm, imm8\n    pslld_r128(r, imm8 as u64);\n}\npub unsafe fn instr_0F73_2_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_0F73_6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0F73_2_reg(r: i32, imm8: i32) {\n    // psrlq mm, imm8\n    psrlq_r64(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_0F73_6_reg(r: i32, imm8: i32) {\n    // psllq mm, imm8\n    psllq_r64(r, imm8 as u64);\n}\npub unsafe fn instr_660F73_2_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F73_3_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F73_6_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr_660F73_7_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660F73_2_reg(r: i32, imm8: i32) {\n    // psrlq xmm, imm8\n    psrlq_r128(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_660F73_3_reg(r: i32, imm8: i32) {\n    // psrldq xmm, imm8\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    if imm8 == 0 {\n        return;\n    }\n    let shift = (if imm8 > 15 { 128 } else { imm8 << 3 }) as u32;\n    if shift <= 63 {\n        result.u64[0] = destination.u64[0] >> shift | destination.u64[1] << (64 - shift);\n        result.u64[1] = destination.u64[1] >> shift\n    }\n    else if shift <= 127 {\n        result.u64[0] = destination.u64[1] >> (shift - 64);\n        result.u64[1] = 0\n    }\n    write_xmm_reg128(r, result);\n}\n#[no_mangle]\npub unsafe fn instr_660F73_6_reg(r: i32, imm8: i32) {\n    // psllq xmm, imm8\n    psllq_r128(r, imm8 as u64);\n}\n#[no_mangle]\npub unsafe fn instr_660F73_7_reg(r: i32, imm8: i32) {\n    // pslldq xmm, imm8\n    if imm8 == 0 {\n        return;\n    }\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    let shift = (if imm8 > 15 { 128 } else { imm8 << 3 }) as u32;\n    if shift <= 63 {\n        result.u64[0] = destination.u64[0] << shift;\n        result.u64[1] = destination.u64[1] << shift | destination.u64[0] >> (64 - shift)\n    }\n    else if shift <= 127 {\n        result.u64[0] = 0;\n        result.u64[1] = destination.u64[0] << (shift - 64)\n    }\n    write_xmm_reg128(r, result);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F74(source: u64, r: i32) {\n    // pcmpeqb mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result: [u8; 8] = [0; 8];\n    for i in 0..8 {\n        result[i] = if destination[i] == source[i] { 255 } else { 0 };\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F74_reg(r1: i32, r2: i32) { instr_0F74(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F74_mem(addr: i32, r: i32) {\n    instr_0F74(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F74(source: reg128, r: i32) {\n    // pcmpeqb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = if source.u8[i] == destination.u8[i] { 255 } else { 0 }\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F74_reg(r1: i32, r2: i32) { instr_660F74(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F74_mem(addr: i32, r: i32) {\n    instr_660F74(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F75(source: u64, r: i32) {\n    // pcmpeqw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result: [u16; 4] = [0; 4];\n    for i in 0..4 {\n        result[i] = if destination[i] == source[i] { 0xFFFF } else { 0 };\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F75_reg(r1: i32, r2: i32) { instr_0F75(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F75_mem(addr: i32, r: i32) {\n    instr_0F75(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F75(source: reg128, r: i32) {\n    // pcmpeqw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] =\n            (if source.u16[i] as i32 == destination.u16[i] as i32 { 0xFFFF } else { 0 }) as u16;\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F75_reg(r1: i32, r2: i32) { instr_660F75(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F75_mem(addr: i32, r: i32) {\n    instr_660F75(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F76(source: u64, r: i32) {\n    // pcmpeqd mm, mm/m64\n    let destination: [i32; 2] = std::mem::transmute(read_mmx64s(r));\n    let source: [i32; 2] = std::mem::transmute(source);\n    let mut result = [0; 2];\n    for i in 0..2 {\n        result[i] = if destination[i] == source[i] { -1 } else { 0 }\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0F76_reg(r1: i32, r2: i32) { instr_0F76(read_mmx64s(r1), r2); }\npub unsafe fn instr_0F76_mem(addr: i32, r: i32) {\n    instr_0F76(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660F76(source: reg128, r: i32) {\n    // pcmpeqd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..4 {\n        result.i32[i] = if source.u32[i] == destination.u32[i] { -1 } else { 0 }\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660F76_reg(r1: i32, r2: i32) { instr_660F76(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F76_mem(addr: i32, r: i32) {\n    instr_660F76(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0F77() {\n    // emms\n    fpu_set_tag_word(0xFFFF);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F78() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F79() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F7A() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F7B() { unimplemented_sse(); }\n\n#[no_mangle]\npub unsafe fn instr_0F7C() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0F7D() { unimplemented_sse(); }\n\n#[no_mangle]\npub unsafe fn instr_660F7C(source: reg128, r: i32) {\n    // haddpd xmm1, xmm2/m128\n    let destination = read_xmm128s(r);\n    write_xmm_reg128(\n        r,\n        reg128 {\n            f64: [\n                destination.f64[0] + destination.f64[1],\n                source.f64[0] + source.f64[1],\n            ],\n        },\n    );\n}\npub unsafe fn instr_660F7C_reg(r1: i32, r2: i32) { instr_660F7C(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F7C_mem(addr: i32, r: i32) {\n    instr_660F7C(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_F20F7C(source: reg128, r: i32) {\n    // haddps xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    write_xmm_reg128(\n        r,\n        reg128 {\n            f32: [\n                destination.f32[0] + destination.f32[1],\n                destination.f32[2] + destination.f32[3],\n                source.f32[0] + source.f32[1],\n                source.f32[2] + source.f32[3],\n            ],\n        },\n    );\n}\npub unsafe fn instr_F20F7C_reg(r1: i32, r2: i32) { instr_F20F7C(read_xmm128s(r1), r2); }\npub unsafe fn instr_F20F7C_mem(addr: i32, r: i32) {\n    instr_F20F7C(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_660F7D(source: reg128, r: i32) {\n    // hsubpd xmm1, xmm2/m128\n    let destination = read_xmm128s(r);\n    write_xmm_reg128(\n        r,\n        reg128 {\n            f64: [\n                destination.f64[0] - destination.f64[1],\n                source.f64[0] - source.f64[1],\n            ],\n        },\n    );\n}\npub unsafe fn instr_660F7D_reg(r1: i32, r2: i32) { instr_660F7D(read_xmm128s(r1), r2); }\npub unsafe fn instr_660F7D_mem(addr: i32, r: i32) {\n    instr_660F7D(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_F20F7D(source: reg128, r: i32) {\n    // hsubps xmm1, xmm2/m128\n    let destination = read_xmm128s(r);\n    write_xmm_reg128(\n        r,\n        reg128 {\n            f32: [\n                destination.f32[0] - destination.f32[1],\n                destination.f32[2] - destination.f32[3],\n                source.f32[0] - source.f32[1],\n                source.f32[2] - source.f32[3],\n            ],\n        },\n    );\n}\npub unsafe fn instr_F20F7D_reg(r1: i32, r2: i32) { instr_F20F7D(read_xmm128s(r1), r2); }\npub unsafe fn instr_F20F7D_mem(addr: i32, r: i32) {\n    instr_F20F7D(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F7E(r: i32) -> i32 {\n    // movd r/m32, mm\n    let data = read_mmx64s(r);\n    transition_fpu_to_mmx();\n    return data as i32;\n}\npub unsafe fn instr_0F7E_reg(r1: i32, r2: i32) { write_reg32(r1, instr_0F7E(r2)); }\npub unsafe fn instr_0F7E_mem(addr: i32, r: i32) {\n    return_on_pagefault!(safe_write32(addr, instr_0F7E(r)));\n}\npub unsafe fn instr_660F7E(r: i32) -> i32 {\n    // movd r/m32, xmm\n    let data = read_xmm64s(r);\n    return data as i32;\n}\npub unsafe fn instr_660F7E_reg(r1: i32, r2: i32) { write_reg32(r1, instr_660F7E(r2)); }\npub unsafe fn instr_660F7E_mem(addr: i32, r: i32) {\n    return_on_pagefault!(safe_write32(addr, instr_660F7E(r)));\n}\npub unsafe fn instr_F30F7E_mem(addr: i32, r: i32) {\n    // movq xmm, xmm/mem64\n    let data = return_on_pagefault!(safe_read64s(addr));\n    write_xmm128_2(r, data, 0);\n}\npub unsafe fn instr_F30F7E_reg(r1: i32, r2: i32) {\n    // movq xmm, xmm/mem64\n    write_xmm128_2(r2, read_xmm64s(r1), 0);\n}\n\n#[no_mangle]\npub unsafe fn instr_0F7F(r: i32) -> u64 {\n    // movq mm/m64, mm\n    transition_fpu_to_mmx();\n    read_mmx64s(r)\n}\npub unsafe fn instr_0F7F_mem(addr: i32, r: i32) {\n    // movq mm/m64, mm\n    mov_r_m64(addr, r);\n}\n#[no_mangle]\npub unsafe fn instr_0F7F_reg(r1: i32, r2: i32) {\n    // movq mm/m64, mm\n    write_mmx_reg64(r1, read_mmx64s(r2));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_660F7F_mem(addr: i32, r: i32) {\n    // movdqa xmm/m128, xmm\n    // XXX: Aligned write or #gp\n    mov_r_m128(addr, r);\n}\npub unsafe fn instr_660F7F_reg(r1: i32, r2: i32) {\n    // movdqa xmm/m128, xmm\n    // XXX: Aligned access or #gp\n    mov_r_r128(r1, r2);\n}\npub unsafe fn instr_F30F7F_mem(addr: i32, r: i32) {\n    // movdqu xmm/m128, xmm\n    mov_r_m128(addr, r);\n}\npub unsafe fn instr_F30F7F_reg(r1: i32, r2: i32) {\n    // movdqu xmm/m128, xmm\n    mov_r_r128(r1, r2);\n}\n\npub unsafe fn instr16_0F80(imm: i32) { jmpcc16(test_o(), imm); }\npub unsafe fn instr32_0F80(imm: i32) { jmpcc32(test_o(), imm); }\npub unsafe fn instr16_0F81(imm: i32) { jmpcc16(!test_o(), imm); }\npub unsafe fn instr32_0F81(imm: i32) { jmpcc32(!test_o(), imm); }\npub unsafe fn instr16_0F82(imm: i32) { jmpcc16(test_b(), imm); }\npub unsafe fn instr32_0F82(imm: i32) { jmpcc32(test_b(), imm); }\npub unsafe fn instr16_0F83(imm: i32) { jmpcc16(!test_b(), imm); }\npub unsafe fn instr32_0F83(imm: i32) { jmpcc32(!test_b(), imm); }\npub unsafe fn instr16_0F84(imm: i32) { jmpcc16(test_z(), imm); }\npub unsafe fn instr32_0F84(imm: i32) { jmpcc32(test_z(), imm); }\npub unsafe fn instr16_0F85(imm: i32) { jmpcc16(!test_z(), imm); }\npub unsafe fn instr32_0F85(imm: i32) { jmpcc32(!test_z(), imm); }\npub unsafe fn instr16_0F86(imm: i32) { jmpcc16(test_be(), imm); }\npub unsafe fn instr32_0F86(imm: i32) { jmpcc32(test_be(), imm); }\npub unsafe fn instr16_0F87(imm: i32) { jmpcc16(!test_be(), imm); }\npub unsafe fn instr32_0F87(imm: i32) { jmpcc32(!test_be(), imm); }\npub unsafe fn instr16_0F88(imm: i32) { jmpcc16(test_s(), imm); }\npub unsafe fn instr32_0F88(imm: i32) { jmpcc32(test_s(), imm); }\npub unsafe fn instr16_0F89(imm: i32) { jmpcc16(!test_s(), imm); }\npub unsafe fn instr32_0F89(imm: i32) { jmpcc32(!test_s(), imm); }\npub unsafe fn instr16_0F8A(imm: i32) { jmpcc16(test_p(), imm); }\npub unsafe fn instr32_0F8A(imm: i32) { jmpcc32(test_p(), imm); }\npub unsafe fn instr16_0F8B(imm: i32) { jmpcc16(!test_p(), imm); }\npub unsafe fn instr32_0F8B(imm: i32) { jmpcc32(!test_p(), imm); }\npub unsafe fn instr16_0F8C(imm: i32) { jmpcc16(test_l(), imm); }\npub unsafe fn instr32_0F8C(imm: i32) { jmpcc32(test_l(), imm); }\npub unsafe fn instr16_0F8D(imm: i32) { jmpcc16(!test_l(), imm); }\npub unsafe fn instr32_0F8D(imm: i32) { jmpcc32(!test_l(), imm); }\npub unsafe fn instr16_0F8E(imm: i32) { jmpcc16(test_le(), imm); }\npub unsafe fn instr32_0F8E(imm: i32) { jmpcc32(test_le(), imm); }\npub unsafe fn instr16_0F8F(imm: i32) { jmpcc16(!test_le(), imm); }\npub unsafe fn instr32_0F8F(imm: i32) { jmpcc32(!test_le(), imm); }\n\npub unsafe fn instr_0F90_reg(r: i32, _: i32) { setcc_reg(test_o(), r); }\npub unsafe fn instr_0F91_reg(r: i32, _: i32) { setcc_reg(!test_o(), r); }\npub unsafe fn instr_0F92_reg(r: i32, _: i32) { setcc_reg(test_b(), r); }\npub unsafe fn instr_0F93_reg(r: i32, _: i32) { setcc_reg(!test_b(), r); }\npub unsafe fn instr_0F94_reg(r: i32, _: i32) { setcc_reg(test_z(), r); }\npub unsafe fn instr_0F95_reg(r: i32, _: i32) { setcc_reg(!test_z(), r); }\npub unsafe fn instr_0F96_reg(r: i32, _: i32) { setcc_reg(test_be(), r); }\npub unsafe fn instr_0F97_reg(r: i32, _: i32) { setcc_reg(!test_be(), r); }\npub unsafe fn instr_0F98_reg(r: i32, _: i32) { setcc_reg(test_s(), r); }\npub unsafe fn instr_0F99_reg(r: i32, _: i32) { setcc_reg(!test_s(), r); }\npub unsafe fn instr_0F9A_reg(r: i32, _: i32) { setcc_reg(test_p(), r); }\npub unsafe fn instr_0F9B_reg(r: i32, _: i32) { setcc_reg(!test_p(), r); }\npub unsafe fn instr_0F9C_reg(r: i32, _: i32) { setcc_reg(test_l(), r); }\npub unsafe fn instr_0F9D_reg(r: i32, _: i32) { setcc_reg(!test_l(), r); }\npub unsafe fn instr_0F9E_reg(r: i32, _: i32) { setcc_reg(test_le(), r); }\npub unsafe fn instr_0F9F_reg(r: i32, _: i32) { setcc_reg(!test_le(), r); }\npub unsafe fn instr_0F90_mem(addr: i32, _: i32) { setcc_mem(test_o(), addr); }\npub unsafe fn instr_0F91_mem(addr: i32, _: i32) { setcc_mem(!test_o(), addr); }\npub unsafe fn instr_0F92_mem(addr: i32, _: i32) { setcc_mem(test_b(), addr); }\npub unsafe fn instr_0F93_mem(addr: i32, _: i32) { setcc_mem(!test_b(), addr); }\npub unsafe fn instr_0F94_mem(addr: i32, _: i32) { setcc_mem(test_z(), addr); }\npub unsafe fn instr_0F95_mem(addr: i32, _: i32) { setcc_mem(!test_z(), addr); }\npub unsafe fn instr_0F96_mem(addr: i32, _: i32) { setcc_mem(test_be(), addr); }\npub unsafe fn instr_0F97_mem(addr: i32, _: i32) { setcc_mem(!test_be(), addr); }\npub unsafe fn instr_0F98_mem(addr: i32, _: i32) { setcc_mem(test_s(), addr); }\npub unsafe fn instr_0F99_mem(addr: i32, _: i32) { setcc_mem(!test_s(), addr); }\npub unsafe fn instr_0F9A_mem(addr: i32, _: i32) { setcc_mem(test_p(), addr); }\npub unsafe fn instr_0F9B_mem(addr: i32, _: i32) { setcc_mem(!test_p(), addr); }\npub unsafe fn instr_0F9C_mem(addr: i32, _: i32) { setcc_mem(test_l(), addr); }\npub unsafe fn instr_0F9D_mem(addr: i32, _: i32) { setcc_mem(!test_l(), addr); }\npub unsafe fn instr_0F9E_mem(addr: i32, _: i32) { setcc_mem(test_le(), addr); }\npub unsafe fn instr_0F9F_mem(addr: i32, _: i32) { setcc_mem(!test_le(), addr); }\n\npub unsafe fn instr16_0FA0() {\n    return_on_pagefault!(push16(*sreg.offset(FS as isize) as i32));\n}\npub unsafe fn instr32_0FA0() { return_on_pagefault!(push32_sreg(FS)) }\n#[no_mangle]\npub unsafe fn instr16_0FA1() {\n    if !switch_seg(FS, return_on_pagefault!(safe_read16(get_stack_pointer(0)))) {\n        return;\n    }\n    else {\n        adjust_stack_reg(2);\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr32_0FA1() {\n    if !switch_seg(\n        FS,\n        return_on_pagefault!(safe_read32s(get_stack_pointer(0))) & 0xFFFF,\n    ) {\n        return;\n    }\n    else {\n        adjust_stack_reg(4);\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0FA2() {\n    // cpuid\n    // TODO: Fill in with less bogus values\n\n    // http://lxr.linux.no/linux+%2a/arch/x86/include/asm/cpufeature.h\n    // http://www.sandpile.org/x86/cpuid.htm\n    let mut eax = 0;\n    let mut ecx = 0;\n    let mut edx = 0;\n    let mut ebx = 0;\n\n    let level = read_reg32(EAX) as u32;\n\n    match level {\n        0 => {\n            // maximum supported level (default 0x16, overwritten to 2 as a workaround for Windows NT)\n            eax = cpuid_level as i32;\n\n            ebx = 0x756E6547 | 0; // Genu\n            edx = 0x49656E69 | 0; // ineI\n            ecx = 0x6C65746E | 0; // ntel\n        },\n\n        1 => {\n            eax = 3 | 7 << 4 | 6 << 8; // pentium3\n            ebx = 1 << 16 | 8 << 8; // cpu count, clflush size\n            ecx = 1 << 0 | 1 << 23 | 1 << 30; // sse3, popcnt, rdrand\n            let vme = 0 << 1;\n            if config::VMWARE_HYPERVISOR_PORT {\n                ecx |= 1 << 31\n            }; // hypervisor\n            edx = (if true /* have fpu */ { 1 } else {  0 }) |      // fpu\n                    vme | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6 |  // vme, pse, tsc, msr, pae\n                    1 << 8 | 1 << 11 | 1 << 13 | 1 << 15 | // cx8, sep, pge, cmov\n                    1 << 23 | 1 << 24 | 1 << 25 | 1 << 26; // mmx, fxsr, sse1, sse2\n\n            if *acpi_enabled\n            //&& this.apic_enabled[0])\n            {\n                edx |= 1 << 9; // apic\n            }\n        },\n\n        2 => {\n            // Taken from http://siyobik.info.gf/main/reference/instruction/CPUID\n            eax = 0x665B5001;\n            ebx = 0;\n            ecx = 0;\n            edx = 0x007A7000;\n        },\n\n        4 => {\n            // from my local machine\n            match read_reg32(ECX) {\n                0 => {\n                    eax = 0x00000121;\n                    ebx = 0x01c0003f;\n                    ecx = 0x0000003f;\n                    edx = 0x00000001;\n                },\n                1 => {\n                    eax = 0x00000122;\n                    ebx = 0x01c0003f;\n                    ecx = 0x0000003f;\n                    edx = 0x00000001;\n                },\n                2 => {\n                    eax = 0x00000143;\n                    ebx = 0x05c0003f;\n                    ecx = 0x00000fff;\n                    edx = 0x00000001;\n                },\n                _ => {},\n            }\n        },\n\n        5 => {\n            // from my local machine\n            eax = 0x40;\n            ebx = 0x40;\n            ecx = 3;\n            edx = 0x00142120;\n        },\n\n        7 => {\n            if read_reg32(ECX) == 0 {\n                eax = 0; // maximum supported sub-level\n                ebx = 1 << 9; // enhanced REP MOVSB/STOSB\n                ecx = 0;\n                edx = 0;\n            }\n        },\n\n        0x80000000 => {\n            // maximum supported extended level\n            eax = 5;\n            // other registers are reserved\n        },\n\n        0x40000000 => {\n            // hypervisor\n            if config::VMWARE_HYPERVISOR_PORT {\n                // h(\"Ware\".split(\"\").reduce((a, c, i) => a | c.charCodeAt(0) << i * 8, 0))\n                ebx = 0x61774D56 | 0; // VMwa\n                ecx = 0x4D566572 | 0; // reVM\n                edx = 0x65726177 | 0; // ware\n            }\n        },\n\n        0x15 => {\n            eax = 1; // denominator\n            ebx = 1; // numerator\n            ecx = (TSC_RATE * 1000.0) as u32 as i32; // core crystal clock frequency in Hz\n            dbg_assert!(ecx > 0);\n            //  (TSC frequency = core crystal clock frequency * EBX/EAX)\n        },\n\n        0x16 => {\n            eax = (TSC_RATE / 1000.0).floor() as u32 as i32; // core base frequency in MHz\n            ebx = (TSC_RATE / 1000.0).floor() as u32 as i32; // core maximum frequency in MHz\n            ecx = 10; // bus (reference) frequency in MHz\n\n            // 16-bit values\n            dbg_assert!(eax < 0x10000);\n            dbg_assert!(ebx < 0x10000);\n            dbg_assert!(ecx < 0x10000);\n        },\n\n        x => {\n            dbg_log!(\"cpuid: unimplemented eax: {:x}\", x);\n        },\n    }\n\n    if level == 4 || level == 7 {\n        dbg_log!(\n            \"cpuid: eax={:08x} ecx={:02x}\",\n            read_reg32(EAX),\n            read_reg32(ECX),\n        );\n    }\n    else if level != 0 && level != 2 && level != 0x80000000 {\n        dbg_log!(\"cpuid: eax={:08x}\", read_reg32(EAX));\n    }\n\n    write_reg32(EAX, eax);\n    write_reg32(ECX, ecx);\n    write_reg32(EDX, edx);\n    write_reg32(EBX, ebx);\n}\npub unsafe fn instr16_0FA3_reg(r1: i32, r2: i32) { bt_reg(read_reg16(r1), read_reg16(r2) & 15); }\npub unsafe fn instr16_0FA3_mem(addr: i32, r: i32) { bt_mem(addr, read_reg16(r) << 16 >> 16); }\npub unsafe fn instr32_0FA3_reg(r1: i32, r2: i32) { bt_reg(read_reg32(r1), read_reg32(r2) & 31); }\npub unsafe fn instr32_0FA3_mem(addr: i32, r: i32) { bt_mem(addr, read_reg32(r)); }\npub unsafe fn instr16_0FA4_mem(addr: i32, r: i32, imm: i32) {\n    safe_read_write16(addr, &|x| shld16(x, read_reg16(r), imm & 31))\n}\npub unsafe fn instr16_0FA4_reg(r1: i32, r: i32, imm: i32) {\n    write_reg16(r1, shld16(read_reg16(r1), read_reg16(r), imm & 31));\n}\npub unsafe fn instr32_0FA4_mem(addr: i32, r: i32, imm: i32) {\n    safe_read_write32(addr, &|x| shld32(x, read_reg32(r), imm & 31))\n}\npub unsafe fn instr32_0FA4_reg(r1: i32, r: i32, imm: i32) {\n    write_reg32(r1, shld32(read_reg32(r1), read_reg32(r), imm & 31));\n}\npub unsafe fn instr16_0FA5_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| shld16(x, read_reg16(r), read_reg8(CL) & 31))\n}\npub unsafe fn instr16_0FA5_reg(r1: i32, r: i32) {\n    write_reg16(\n        r1,\n        shld16(read_reg16(r1), read_reg16(r), read_reg8(CL) & 31),\n    );\n}\npub unsafe fn instr32_0FA5_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| shld32(x, read_reg32(r), read_reg8(CL) & 31))\n}\npub unsafe fn instr32_0FA5_reg(r1: i32, r: i32) {\n    write_reg32(\n        r1,\n        shld32(read_reg32(r1), read_reg32(r), read_reg8(CL) & 31),\n    );\n}\n#[no_mangle]\npub unsafe fn instr_0FA6() {\n    // obsolete cmpxchg (os/2)\n    trigger_ud();\n}\n#[no_mangle]\npub unsafe fn instr_0FA7() { undefined_instruction(); }\npub unsafe fn instr16_0FA8() {\n    return_on_pagefault!(push16(*sreg.offset(GS as isize) as i32));\n}\npub unsafe fn instr32_0FA8() { return_on_pagefault!(push32_sreg(GS)) }\n#[no_mangle]\npub unsafe fn instr16_0FA9() {\n    if !switch_seg(GS, return_on_pagefault!(safe_read16(get_stack_pointer(0)))) {\n        return;\n    }\n    else {\n        adjust_stack_reg(2);\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr32_0FA9() {\n    if !switch_seg(\n        GS,\n        return_on_pagefault!(safe_read32s(get_stack_pointer(0))) & 0xFFFF,\n    ) {\n        return;\n    }\n    else {\n        adjust_stack_reg(4);\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0FAA() {\n    // rsm\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr16_0FAB_reg(r1: i32, r2: i32) {\n    write_reg16(r1, bts_reg(read_reg16(r1), read_reg16(r2) & 15));\n}\n#[no_mangle]\npub unsafe fn instr16_0FAB_mem(addr: i32, r: i32) { bts_mem(addr, read_reg16(r) << 16 >> 16); }\n#[no_mangle]\npub unsafe fn instr32_0FAB_reg(r1: i32, r2: i32) {\n    write_reg32(r1, bts_reg(read_reg32(r1), read_reg32(r2) & 31));\n}\n#[no_mangle]\npub unsafe fn instr32_0FAB_mem(addr: i32, r: i32) { bts_mem(addr, read_reg32(r)); }\npub unsafe fn instr16_0FAC_mem(addr: i32, r: i32, imm: i32) {\n    safe_read_write16(addr, &|x| shrd16(x, read_reg16(r), imm & 31))\n}\npub unsafe fn instr16_0FAC_reg(r1: i32, r: i32, imm: i32) {\n    write_reg16(r1, shrd16(read_reg16(r1), read_reg16(r), imm & 31));\n}\npub unsafe fn instr32_0FAC_mem(addr: i32, r: i32, imm: i32) {\n    safe_read_write32(addr, &|x| shrd32(x, read_reg32(r), imm & 31))\n}\npub unsafe fn instr32_0FAC_reg(r1: i32, r: i32, imm: i32) {\n    write_reg32(r1, shrd32(read_reg32(r1), read_reg32(r), imm & 31));\n}\npub unsafe fn instr16_0FAD_mem(addr: i32, r: i32) {\n    safe_read_write16(addr, &|x| shrd16(x, read_reg16(r), read_reg8(CL) & 31))\n}\npub unsafe fn instr16_0FAD_reg(r1: i32, r: i32) {\n    write_reg16(\n        r1,\n        shrd16(read_reg16(r1), read_reg16(r), read_reg8(CL) & 31),\n    );\n}\npub unsafe fn instr32_0FAD_mem(addr: i32, r: i32) {\n    safe_read_write32(addr, &|x| shrd32(x, read_reg32(r), read_reg8(CL) & 31))\n}\npub unsafe fn instr32_0FAD_reg(r1: i32, r: i32) {\n    write_reg32(\n        r1,\n        shrd32(read_reg32(r1), read_reg32(r), read_reg8(CL) & 31),\n    );\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_0_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FAE_0_mem(addr: i32) { fxsave(addr); }\n#[no_mangle]\npub unsafe fn instr_0FAE_1_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FAE_1_mem(addr: i32) { fxrstor(addr); }\n#[no_mangle]\npub unsafe fn instr_0FAE_2_reg(_r: i32) { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0FAE_2_mem(addr: i32) {\n    // ldmxcsr\n    let new_mxcsr = return_on_pagefault!(safe_read32s(addr));\n    if 0 != new_mxcsr & !MXCSR_MASK {\n        dbg_log!(\"Invalid mxcsr bits: {:x}\", new_mxcsr & !MXCSR_MASK);\n        trigger_gp(0);\n        return;\n    }\n    else {\n        set_mxcsr(new_mxcsr);\n        return;\n    };\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_3_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FAE_3_mem(addr: i32) {\n    // stmxcsr\n    return_on_pagefault!(safe_write32(addr, *mxcsr));\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_4_reg(_r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FAE_4_mem(_addr: i32) {\n    // xsave\n    undefined_instruction();\n}\npub unsafe fn instr_0FAE_5_reg(_r: i32) {\n    // lfence\n}\npub unsafe fn instr_0FAE_5_mem(_addr: i32) {\n    // xrstor\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_6_reg(_r: i32) {\n    // mfence\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_6_mem(_addr: i32) {\n    // xsaveopt\n    undefined_instruction();\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_7_reg(_r: i32) {\n    // sfence\n}\n#[no_mangle]\npub unsafe fn instr_0FAE_7_mem(_addr: i32) {\n    // clflush\n    undefined_instruction();\n}\npub unsafe fn instr16_0FAF_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        imul_reg16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_0FAF_reg(r1: i32, r: i32) {\n    write_reg16(r, imul_reg16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_0FAF_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        imul_reg32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_0FAF_reg(r1: i32, r: i32) {\n    write_reg32(r, imul_reg32(read_reg32(r), read_reg32(r1)));\n}\n\n#[no_mangle]\npub unsafe fn instr_0FB0_reg(r1: i32, r2: i32) { write_reg8(r1, cmpxchg8(read_reg8(r1), r2)); }\n#[no_mangle]\npub unsafe fn instr_0FB0_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| cmpxchg8(x, r)) }\npub unsafe fn instr16_0FB1_reg(r1: i32, r2: i32) { write_reg16(r1, cmpxchg16(read_reg16(r1), r2)); }\npub unsafe fn instr16_0FB1_mem(addr: i32, r: i32) { safe_read_write16(addr, &|x| cmpxchg16(x, r)) }\npub unsafe fn instr32_0FB1_reg(r1: i32, r2: i32) { write_reg32(r1, cmpxchg32(read_reg32(r1), r2)); }\npub unsafe fn instr32_0FB1_mem(addr: i32, r: i32) { safe_read_write32(addr, &|x| cmpxchg32(x, r)) }\n\n#[no_mangle]\npub unsafe fn instr16_0FB2_reg(_unused: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_0FB2_mem(addr: i32, r: i32) { lss16(addr, r, SS); }\n#[no_mangle]\npub unsafe fn instr32_0FB2_reg(_unused: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0FB2_mem(addr: i32, r: i32) { lss32(addr, r, SS); }\n#[no_mangle]\npub unsafe fn instr16_0FB3_reg(r1: i32, r2: i32) {\n    write_reg16(r1, btr_reg(read_reg16(r1), read_reg16(r2) & 15));\n}\n#[no_mangle]\npub unsafe fn instr16_0FB3_mem(addr: i32, r: i32) { btr_mem(addr, read_reg16(r) << 16 >> 16); }\n#[no_mangle]\npub unsafe fn instr32_0FB3_reg(r1: i32, r2: i32) {\n    write_reg32(r1, btr_reg(read_reg32(r1), read_reg32(r2) & 31));\n}\n#[no_mangle]\npub unsafe fn instr32_0FB3_mem(addr: i32, r: i32) { btr_mem(addr, read_reg32(r)); }\n#[no_mangle]\npub unsafe fn instr16_0FB4_reg(_unused: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_0FB4_mem(addr: i32, r: i32) { lss16(addr, r, FS); }\n#[no_mangle]\npub unsafe fn instr32_0FB4_reg(_unused: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0FB4_mem(addr: i32, r: i32) { lss32(addr, r, FS); }\n#[no_mangle]\npub unsafe fn instr16_0FB5_reg(_unused: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_0FB5_mem(addr: i32, r: i32) { lss16(addr, r, GS); }\n#[no_mangle]\npub unsafe fn instr32_0FB5_reg(_unused: i32, _unused2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0FB5_mem(addr: i32, r: i32) { lss32(addr, r, GS); }\npub unsafe fn instr16_0FB6_mem(addr: i32, r: i32) {\n    write_reg16(r, return_on_pagefault!(safe_read8(addr)));\n}\npub unsafe fn instr16_0FB6_reg(r1: i32, r: i32) { write_reg16(r, read_reg8(r1)); }\npub unsafe fn instr32_0FB6_mem(addr: i32, r: i32) {\n    write_reg32(r, return_on_pagefault!(safe_read8(addr)));\n}\npub unsafe fn instr32_0FB6_reg(r1: i32, r: i32) { write_reg32(r, read_reg8(r1)); }\npub unsafe fn instr16_0FB7_mem(addr: i32, r: i32) {\n    write_reg16(r, return_on_pagefault!(safe_read16(addr)));\n}\npub unsafe fn instr16_0FB7_reg(r1: i32, r: i32) { write_reg16(r, read_reg16(r1)); }\npub unsafe fn instr32_0FB7_mem(addr: i32, r: i32) {\n    write_reg32(r, return_on_pagefault!(safe_read16(addr)));\n}\npub unsafe fn instr32_0FB7_reg(r1: i32, r: i32) { write_reg32(r, read_reg16(r1)); }\n#[no_mangle]\npub unsafe fn instr16_0FB8_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr16_0FB8_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr16_F30FB8_mem(addr: i32, r: i32) {\n    write_reg16(r, popcnt(return_on_pagefault!(safe_read16(addr))));\n}\npub unsafe fn instr16_F30FB8_reg(r1: i32, r: i32) { write_reg16(r, popcnt(read_reg16(r1))); }\n#[no_mangle]\npub unsafe fn instr32_0FB8_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0FB8_mem(_addr: i32, _r: i32) { trigger_ud(); }\npub unsafe fn instr32_F30FB8_mem(addr: i32, r: i32) {\n    write_reg32(r, popcnt(return_on_pagefault!(safe_read32s(addr))));\n}\npub unsafe fn instr32_F30FB8_reg(r1: i32, r: i32) { write_reg32(r, popcnt(read_reg32(r1))); }\n#[no_mangle]\npub unsafe fn instr_0FB9() {\n    // UD2\n    trigger_ud();\n}\npub unsafe fn instr16_0FBA_4_reg(r: i32, imm: i32) { bt_reg(read_reg16(r), imm & 15); }\npub unsafe fn instr16_0FBA_4_mem(addr: i32, imm: i32) { bt_mem(addr, imm & 15); }\n#[no_mangle]\npub unsafe fn instr16_0FBA_5_reg(r: i32, imm: i32) {\n    write_reg16(r, bts_reg(read_reg16(r), imm & 15));\n}\n#[no_mangle]\npub unsafe fn instr16_0FBA_5_mem(addr: i32, imm: i32) { bts_mem(addr, imm & 15); }\n#[no_mangle]\npub unsafe fn instr16_0FBA_6_reg(r: i32, imm: i32) {\n    write_reg16(r, btr_reg(read_reg16(r), imm & 15));\n}\n#[no_mangle]\npub unsafe fn instr16_0FBA_6_mem(addr: i32, imm: i32) { btr_mem(addr, imm & 15); }\n#[no_mangle]\npub unsafe fn instr16_0FBA_7_reg(r: i32, imm: i32) {\n    write_reg16(r, btc_reg(read_reg16(r), imm & 15));\n}\n#[no_mangle]\npub unsafe fn instr16_0FBA_7_mem(addr: i32, imm: i32) { btc_mem(addr, imm & 15); }\npub unsafe fn instr32_0FBA_4_reg(r: i32, imm: i32) { bt_reg(read_reg32(r), imm & 31); }\npub unsafe fn instr32_0FBA_4_mem(addr: i32, imm: i32) { bt_mem(addr, imm & 31); }\n#[no_mangle]\npub unsafe fn instr32_0FBA_5_reg(r: i32, imm: i32) {\n    write_reg32(r, bts_reg(read_reg32(r), imm & 31));\n}\n#[no_mangle]\npub unsafe fn instr32_0FBA_5_mem(addr: i32, imm: i32) { bts_mem(addr, imm & 31); }\n#[no_mangle]\npub unsafe fn instr32_0FBA_6_reg(r: i32, imm: i32) {\n    write_reg32(r, btr_reg(read_reg32(r), imm & 31));\n}\n#[no_mangle]\npub unsafe fn instr32_0FBA_6_mem(addr: i32, imm: i32) { btr_mem(addr, imm & 31); }\n#[no_mangle]\npub unsafe fn instr32_0FBA_7_reg(r: i32, imm: i32) {\n    write_reg32(r, btc_reg(read_reg32(r), imm & 31));\n}\n#[no_mangle]\npub unsafe fn instr32_0FBA_7_mem(addr: i32, imm: i32) { btc_mem(addr, imm & 31); }\n#[no_mangle]\npub unsafe fn instr16_0FBB_reg(r1: i32, r2: i32) {\n    write_reg16(r1, btc_reg(read_reg16(r1), read_reg16(r2) & 15));\n}\n#[no_mangle]\npub unsafe fn instr16_0FBB_mem(addr: i32, r: i32) { btc_mem(addr, read_reg16(r) << 16 >> 16); }\n#[no_mangle]\npub unsafe fn instr32_0FBB_reg(r1: i32, r2: i32) {\n    write_reg32(r1, btc_reg(read_reg32(r1), read_reg32(r2) & 31));\n}\n#[no_mangle]\npub unsafe fn instr32_0FBB_mem(addr: i32, r: i32) { btc_mem(addr, read_reg32(r)); }\npub unsafe fn instr16_0FBC_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        bsf16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_0FBC_reg(r1: i32, r: i32) {\n    write_reg16(r, bsf16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_0FBC_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        bsf32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_0FBC_reg(r1: i32, r: i32) {\n    write_reg32(r, bsf32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr16_0FBD_mem(addr: i32, r: i32) {\n    write_reg16(\n        r,\n        bsr16(read_reg16(r), return_on_pagefault!(safe_read16(addr))),\n    );\n}\npub unsafe fn instr16_0FBD_reg(r1: i32, r: i32) {\n    write_reg16(r, bsr16(read_reg16(r), read_reg16(r1)));\n}\npub unsafe fn instr32_0FBD_mem(addr: i32, r: i32) {\n    write_reg32(\n        r,\n        bsr32(read_reg32(r), return_on_pagefault!(safe_read32s(addr))),\n    );\n}\npub unsafe fn instr32_0FBD_reg(r1: i32, r: i32) {\n    write_reg32(r, bsr32(read_reg32(r), read_reg32(r1)));\n}\npub unsafe fn instr16_0FBE_mem(addr: i32, r: i32) {\n    write_reg16(r, return_on_pagefault!(safe_read8(addr)) << 24 >> 24);\n}\npub unsafe fn instr16_0FBE_reg(r1: i32, r: i32) { write_reg16(r, read_reg8(r1) << 24 >> 24); }\npub unsafe fn instr32_0FBE_mem(addr: i32, r: i32) {\n    write_reg32(r, return_on_pagefault!(safe_read8(addr)) << 24 >> 24);\n}\npub unsafe fn instr32_0FBE_reg(r1: i32, r: i32) { write_reg32(r, read_reg8(r1) << 24 >> 24); }\npub unsafe fn instr16_0FBF_mem(addr: i32, r: i32) {\n    write_reg16(r, return_on_pagefault!(safe_read16(addr)) << 16 >> 16);\n}\npub unsafe fn instr16_0FBF_reg(r1: i32, r: i32) { write_reg16(r, read_reg16(r1) << 16 >> 16); }\npub unsafe fn instr32_0FBF_mem(addr: i32, r: i32) {\n    write_reg32(r, return_on_pagefault!(safe_read16(addr)) << 16 >> 16);\n}\npub unsafe fn instr32_0FBF_reg(r1: i32, r: i32) { write_reg32(r, read_reg16(r1) << 16 >> 16); }\n#[no_mangle]\npub unsafe fn instr_0FC0_mem(addr: i32, r: i32) { safe_read_write8(addr, &|x| xadd8(x, r)) }\n#[no_mangle]\npub unsafe fn instr_0FC0_reg(r1: i32, r: i32) { write_reg8(r1, xadd8(read_reg8(r1), r)); }\npub unsafe fn instr16_0FC1_mem(addr: i32, r: i32) { safe_read_write16(addr, &|x| xadd16(x, r)) }\npub unsafe fn instr16_0FC1_reg(r1: i32, r: i32) { write_reg16(r1, xadd16(read_reg16(r1), r)); }\npub unsafe fn instr32_0FC1_mem(addr: i32, r: i32) { safe_read_write32(addr, &|x| xadd32(x, r)) }\npub unsafe fn instr32_0FC1_reg(r1: i32, r: i32) { write_reg32(r1, xadd32(read_reg32(r1), r)); }\n\n#[no_mangle]\npub unsafe fn instr_0FC2(source: reg128, r: i32, imm8: i32) {\n    // cmpps xmm, xmm/m128\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..4 {\n        result.i32[i] = if sse_comparison(imm8, destination.f32[i] as f64, source.f32[i] as f64) {\n            -1\n        }\n        else {\n            0\n        };\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_0FC2_reg(r1: i32, r2: i32, imm: i32) { instr_0FC2(read_xmm128s(r1), r2, imm); }\npub unsafe fn instr_0FC2_mem(addr: i32, r: i32, imm: i32) {\n    instr_0FC2(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\n#[no_mangle]\npub unsafe fn instr_660FC2(source: reg128, r: i32, imm8: i32) {\n    // cmppd xmm, xmm/m128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        i64: [\n            (if sse_comparison(imm8, destination.f64[0], source.f64[0]) { -1 } else { 0 }) as i64,\n            (if sse_comparison(imm8, destination.f64[1], source.f64[1]) { -1 } else { 0 }) as i64,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FC2_reg(r1: i32, r2: i32, imm: i32) {\n    instr_660FC2(read_xmm128s(r1), r2, imm);\n}\npub unsafe fn instr_660FC2_mem(addr: i32, r: i32, imm: i32) {\n    instr_660FC2(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\n#[no_mangle]\npub unsafe fn instr_F20FC2(source: u64, r: i32, imm8: i32) {\n    // cmpsd xmm, xmm/m64\n    let destination = read_xmm64s(r);\n    write_xmm64(\n        r,\n        if sse_comparison(imm8, f64::from_bits(destination), f64::from_bits(source)) {\n            (-1i32) as u64\n        }\n        else {\n            0\n        },\n    );\n}\npub unsafe fn instr_F20FC2_reg(r1: i32, r2: i32, imm: i32) {\n    instr_F20FC2(read_xmm64s(r1), r2, imm);\n}\npub unsafe fn instr_F20FC2_mem(addr: i32, r: i32, imm: i32) {\n    instr_F20FC2(return_on_pagefault!(safe_read64s(addr)), r, imm);\n}\n#[no_mangle]\npub unsafe fn instr_F30FC2(source: i32, r: i32, imm8: i32) {\n    // cmpss xmm, xmm/m32\n    let destination = read_xmm_f32(r);\n    let source: f32 = f32::from_bits(i32::cast_unsigned(source));\n    let result = if sse_comparison(imm8, destination as f64, source as f64) { -1 } else { 0 };\n    write_xmm32(r, result);\n}\npub unsafe fn instr_F30FC2_reg(r1: i32, r2: i32, imm: i32) {\n    instr_F30FC2(read_xmm64s(r1) as i32, r2, imm);\n}\npub unsafe fn instr_F30FC2_mem(addr: i32, r: i32, imm: i32) {\n    instr_F30FC2(return_on_pagefault!(safe_read32s(addr)), r, imm);\n}\n\npub unsafe fn instr_0FC3_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_0FC3_mem(addr: i32, r: i32) {\n    // movnti\n    return_on_pagefault!(safe_write32(addr, read_reg32(r)));\n}\n\n#[no_mangle]\npub unsafe fn instr_0FC4(source: i32, r: i32, imm8: i32) {\n    // pinsrw mm, r32/m16, imm8\n    let mut destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    destination[(imm8 & 3) as usize] = source as u16;\n    write_mmx_reg64(r, std::mem::transmute(destination));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FC4_reg(r1: i32, r2: i32, imm: i32) { instr_0FC4(read_reg32(r1), r2, imm); }\npub unsafe fn instr_0FC4_mem(addr: i32, r: i32, imm: i32) {\n    instr_0FC4(return_on_pagefault!(safe_read16(addr)), r, imm);\n}\npub unsafe fn instr_660FC4(source: i32, r: i32, imm8: i32) {\n    // pinsrw xmm, r32/m16, imm8\n    let mut destination = read_xmm128s(r);\n    let index = (imm8 & 7) as u32;\n    destination.u16[index as usize] = (source & 0xFFFF) as u16;\n    write_xmm_reg128(r, destination);\n}\npub unsafe fn instr_660FC4_reg(r1: i32, r2: i32, imm: i32) {\n    instr_660FC4(read_reg32(r1), r2, imm);\n}\npub unsafe fn instr_660FC4_mem(addr: i32, r: i32, imm: i32) {\n    instr_660FC4(return_on_pagefault!(safe_read16(addr)), r, imm);\n}\npub unsafe fn instr_0FC5_mem(_addr: i32, _r: i32, _imm8: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FC5_reg(r1: i32, r2: i32, imm8: i32) {\n    // pextrw r32, mm, imm8\n    let data: [u16; 4] = std::mem::transmute(read_mmx64s(r1));\n    write_reg32(r2, data[(imm8 & 3) as usize] as i32);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_660FC5_mem(_addr: i32, _r: i32, _imm8: i32) { trigger_ud(); }\npub unsafe fn instr_660FC5_reg(r1: i32, r2: i32, imm8: i32) {\n    // pextrw r32, xmm, imm8\n    let data = read_xmm128s(r1);\n    let index = (imm8 & 7) as u32;\n    let result = data.u16[index as usize] as u32;\n    write_reg32(r2, result as i32);\n}\n\n#[no_mangle]\npub unsafe fn instr_0FC6(source: reg128, r: i32, imm8: i32) {\n    // shufps xmm, xmm/mem128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.u32[(imm8 & 3) as usize] as i32,\n        destination.u32[(imm8 >> 2 & 3) as usize] as i32,\n        source.u32[(imm8 >> 4 & 3) as usize] as i32,\n        source.u32[(imm8 >> 6 & 3) as usize] as i32,\n    );\n}\npub unsafe fn instr_0FC6_reg(r1: i32, r2: i32, imm: i32) { instr_0FC6(read_xmm128s(r1), r2, imm); }\npub unsafe fn instr_0FC6_mem(addr: i32, r: i32, imm: i32) {\n    instr_0FC6(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\n\n#[no_mangle]\npub unsafe fn instr_660FC6(source: reg128, r: i32, imm8: i32) {\n    // shufpd xmm, xmm/mem128\n    let destination = read_xmm128s(r);\n    let result = reg128 {\n        i64: [\n            destination.i64[imm8 as usize & 1],\n            source.i64[imm8 as usize >> 1 & 1],\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FC6_reg(r1: i32, r2: i32, imm: i32) {\n    instr_660FC6(read_xmm128s(r1), r2, imm);\n}\npub unsafe fn instr_660FC6_mem(addr: i32, r: i32, imm: i32) {\n    instr_660FC6(return_on_pagefault!(safe_read128s(addr)), r, imm);\n}\n\npub unsafe fn instr16_0FC7_1_reg(_r: i32) { trigger_ud(); }\npub unsafe fn instr32_0FC7_1_reg(_r: i32) { trigger_ud(); }\npub unsafe fn instr16_0FC7_1_mem(addr: i32) {\n    // cmpxchg8b\n    return_on_pagefault!(writable_or_pagefault(addr, 8));\n    let m64 = safe_read64s(addr).unwrap();\n    let m64_low = m64 as i32;\n    let m64_high = (m64 >> 32) as i32;\n    if read_reg32(EAX) == m64_low && read_reg32(EDX) == m64_high {\n        *flags |= FLAG_ZERO;\n        safe_write64(\n            addr,\n            read_reg32(EBX) as u32 as u64 | (read_reg32(ECX) as u32 as u64) << 32,\n        )\n        .unwrap();\n    }\n    else {\n        *flags &= !FLAG_ZERO;\n        write_reg32(EAX, m64_low);\n        write_reg32(EDX, m64_high);\n    }\n    *flags_changed &= !FLAG_ZERO;\n}\npub unsafe fn instr32_0FC7_1_mem(addr: i32) { instr16_0FC7_1_mem(addr) }\n\n#[no_mangle]\npub unsafe fn instr16_0FC7_6_reg(r: i32) {\n    // rdrand\n    let rand = js::get_rand_int();\n    write_reg16(r, rand);\n    *flags &= !FLAGS_ALL;\n    *flags |= 1;\n    *flags_changed = 0;\n}\n#[no_mangle]\npub unsafe fn instr32_0FC7_6_reg(r: i32) {\n    // rdrand\n    let rand = js::get_rand_int();\n    write_reg32(r, rand);\n    *flags &= !FLAGS_ALL;\n    *flags |= 1;\n    *flags_changed = 0;\n}\n\n#[no_mangle]\npub unsafe fn instr16_0FC7_6_mem(_addr: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr32_0FC7_6_mem(_addr: i32) { trigger_ud(); }\n\n#[no_mangle]\npub unsafe fn instr_0FC8() { bswap(EAX); }\n#[no_mangle]\npub unsafe fn instr_0FC9() { bswap(ECX); }\n#[no_mangle]\npub unsafe fn instr_0FCA() { bswap(EDX); }\n#[no_mangle]\npub unsafe fn instr_0FCB() { bswap(EBX); }\n#[no_mangle]\npub unsafe fn instr_0FCC() { bswap(ESP); }\n#[no_mangle]\npub unsafe fn instr_0FCD() { bswap(EBP); }\n#[no_mangle]\npub unsafe fn instr_0FCE() { bswap(ESI); }\n#[no_mangle]\npub unsafe fn instr_0FCF() { bswap(EDI); }\n#[no_mangle]\npub unsafe fn instr_0FD0() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0FD1(source: u64, r: i32) {\n    // psrlw mm, mm/m64\n    psrlw_r64(r, source);\n}\npub unsafe fn instr_0FD1_reg(r1: i32, r2: i32) { instr_0FD1(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD1_mem(addr: i32, r: i32) {\n    instr_0FD1(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD1(source: reg128, r: i32) {\n    // psrlw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    psrlw_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FD1_reg(r1: i32, r2: i32) { instr_660FD1(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD1_mem(addr: i32, r: i32) {\n    instr_660FD1(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FD2(source: u64, r: i32) {\n    // psrld mm, mm/m64\n    psrld_r64(r, source);\n}\npub unsafe fn instr_0FD2_reg(r1: i32, r2: i32) { instr_0FD2(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD2_mem(addr: i32, r: i32) {\n    instr_0FD2(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD2(source: reg128, r: i32) {\n    // psrld xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    psrld_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FD2_reg(r1: i32, r2: i32) { instr_660FD2(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD2_mem(addr: i32, r: i32) {\n    instr_660FD2(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FD3(source: u64, r: i32) {\n    // psrlq mm, mm/m64\n    psrlq_r64(r, source);\n}\npub unsafe fn instr_0FD3_reg(r1: i32, r2: i32) { instr_0FD3(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD3_mem(addr: i32, r: i32) {\n    instr_0FD3(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD3(source: reg128, r: i32) {\n    // psrlq xmm, mm/m64\n    psrlq_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FD3_reg(r1: i32, r2: i32) { instr_660FD3(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD3_mem(addr: i32, r: i32) {\n    instr_660FD3(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FD4(source: u64, r: i32) {\n    // paddq mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, source + destination);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FD4_reg(r1: i32, r2: i32) { instr_0FD4(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD4_mem(addr: i32, r: i32) {\n    instr_0FD4(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD4(source: reg128, r: i32) {\n    // paddq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    result.u64[0] = destination.u64[0] + source.u64[0];\n    result.u64[1] = destination.u64[1] + source.u64[1];\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FD4_reg(r1: i32, r2: i32) { instr_660FD4(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD4_mem(addr: i32, r: i32) {\n    instr_660FD4(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FD5(source: u64, r: i32) {\n    // pmullw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = destination[i] * source[i];\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FD5_reg(r1: i32, r2: i32) { instr_0FD5(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD5_mem(addr: i32, r: i32) {\n    instr_0FD5(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD5(source: reg128, r: i32) {\n    // pmullw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = destination.u16[i] * source.u16[i]\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FD5_reg(r1: i32, r2: i32) { instr_660FD5(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD5_mem(addr: i32, r: i32) {\n    instr_660FD5(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0FD6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FD6_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_660FD6_mem(addr: i32, r: i32) {\n    // movq xmm/m64, xmm\n    movl_r128_m64(addr, r);\n}\npub unsafe fn instr_660FD6_reg(r1: i32, r2: i32) {\n    // movq xmm/m64, xmm\n    write_xmm128_2(r1, read_xmm64s(r2), 0);\n}\n\n#[no_mangle]\npub unsafe fn instr_F20FD6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_F20FD6_reg(r1: i32, r2: i32) {\n    // movdq2q mm, xmm\n    write_mmx_reg64(r2, read_xmm128s(r1).u64[0]);\n    transition_fpu_to_mmx();\n}\n#[no_mangle]\npub unsafe fn instr_F30FD6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_F30FD6_reg(r1: i32, r2: i32) {\n    // movq2dq xmm, mm\n    let source = read_mmx64s(r1);\n    write_xmm_reg128(r2, reg128 { u64: [source, 0] });\n    transition_fpu_to_mmx();\n}\n\npub unsafe fn instr_0FD7_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FD7(r1: i32) -> i32 {\n    // pmovmskb r, mm\n    let x: [u8; 8] = u64::to_le_bytes(read_mmx64s(r1));\n    let mut result = 0;\n    for i in 0..8 {\n        result |= x[i] as i32 >> 7 << i\n    }\n    transition_fpu_to_mmx();\n    result\n}\npub unsafe fn instr_0FD7_reg(r1: i32, r2: i32) { write_reg32(r2, instr_0FD7(r1)); }\npub unsafe fn instr_660FD7_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_660FD7(r1: i32) -> i32 {\n    // pmovmskb reg, xmm\n    let x = read_xmm128s(r1);\n    let mut result = 0;\n    for i in 0..16 {\n        result |= x.u8[i] as i32 >> 7 << i\n    }\n    result\n}\npub unsafe fn instr_660FD7_reg(r1: i32, r2: i32) { write_reg32(r2, instr_660FD7(r1)) }\n#[no_mangle]\npub unsafe fn instr_0FD8(source: u64, r: i32) {\n    // psubusb mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = saturate_sd_to_ub(destination[i] as i32 - source[i] as i32) as u8;\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FD8_reg(r1: i32, r2: i32) { instr_0FD8(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD8_mem(addr: i32, r: i32) {\n    instr_0FD8(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD8(source: reg128, r: i32) {\n    // psubusb xmm, xmm/m128\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = saturate_sd_to_ub(destination.u8[i] as i32 - source.u8[i] as i32) as u8;\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FD8_reg(r1: i32, r2: i32) { instr_660FD8(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD8_mem(addr: i32, r: i32) {\n    instr_660FD8(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FD9(source: u64, r: i32) {\n    // psubusw mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = saturate_uw(destination[i] as u32 - source[i] as u32)\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FD9_reg(r1: i32, r2: i32) { instr_0FD9(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FD9_mem(addr: i32, r: i32) {\n    instr_0FD9(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FD9(source: reg128, r: i32) {\n    // psubusw xmm, xmm/m128\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = saturate_uw(destination.u16[i] as u32 - source.u16[i] as u32)\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FD9_reg(r1: i32, r2: i32) { instr_660FD9(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FD9_mem(addr: i32, r: i32) {\n    instr_660FD9(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FDA(source: u64, r: i32) {\n    // pminub mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = u8::min(source[i], destination[i])\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FDA_reg(r1: i32, r2: i32) { instr_0FDA(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FDA_mem(addr: i32, r: i32) {\n    instr_0FDA(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FDA(source: reg128, r: i32) {\n    // pminub xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { u8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = u8::min(source.u8[i], destination.u8[i]);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FDA_reg(r1: i32, r2: i32) { instr_660FDA(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FDA_mem(addr: i32, r: i32) {\n    instr_660FDA(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FDB(source: u64, r: i32) {\n    // pand mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, source & destination);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FDB_reg(r1: i32, r2: i32) { instr_0FDB(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FDB_mem(addr: i32, r: i32) {\n    instr_0FDB(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FDB(source: reg128, r: i32) {\n    // pand xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    pand_r128(source, r);\n}\npub unsafe fn instr_660FDB_reg(r1: i32, r2: i32) { instr_660FDB(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FDB_mem(addr: i32, r: i32) {\n    instr_660FDB(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FDC(source: u64, r: i32) {\n    // paddusb mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = saturate_ud_to_ub(destination[i] as u32 + source[i] as u32);\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FDC_reg(r1: i32, r2: i32) { instr_0FDC(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FDC_mem(addr: i32, r: i32) {\n    instr_0FDC(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FDC(source: reg128, r: i32) {\n    // paddusb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = saturate_ud_to_ub(source.u8[i] as u32 + destination.u8[i] as u32);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FDC_reg(r1: i32, r2: i32) { instr_660FDC(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FDC_mem(addr: i32, r: i32) {\n    instr_660FDC(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FDD(source: u64, r: i32) {\n    // paddusw mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = saturate_uw(destination[i] as u32 + source[i] as u32)\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FDD_reg(r1: i32, r2: i32) { instr_0FDD(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FDD_mem(addr: i32, r: i32) {\n    instr_0FDD(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FDD(source: reg128, r: i32) {\n    // paddusw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = saturate_uw(source.u16[i] as u32 + destination.u16[i] as u32)\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FDD_reg(r1: i32, r2: i32) { instr_660FDD(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FDD_mem(addr: i32, r: i32) {\n    instr_660FDD(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FDE(source: u64, r: i32) {\n    // pmaxub mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = u8::max(source[i], destination[i])\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FDE_reg(r1: i32, r2: i32) { instr_0FDE(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FDE_mem(addr: i32, r: i32) {\n    instr_0FDE(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FDE(source: reg128, r: i32) {\n    // pmaxub xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = u8::max(source.u8[i], destination.u8[i]);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FDE_reg(r1: i32, r2: i32) { instr_660FDE(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FDE_mem(addr: i32, r: i32) {\n    instr_660FDE(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FDF(source: u64, r: i32) {\n    // pandn mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, source & !destination);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FDF_reg(r1: i32, r2: i32) { instr_0FDF(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FDF_mem(addr: i32, r: i32) {\n    instr_0FDF(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FDF(source: reg128, r: i32) {\n    // pandn xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    pandn_r128(source, r);\n}\npub unsafe fn instr_660FDF_reg(r1: i32, r2: i32) { instr_660FDF(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FDF_mem(addr: i32, r: i32) {\n    instr_660FDF(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE0(source: u64, r: i32) {\n    // pavgb mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = (destination[i] as i32 + source[i] as i32 + 1 >> 1) as u8;\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FE0_reg(r1: i32, r2: i32) { instr_0FE0(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE0_mem(addr: i32, r: i32) {\n    instr_0FE0(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE0(source: reg128, r: i32) {\n    // pavgb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = (destination.u8[i] as i32 + source.u8[i] as i32 + 1 >> 1) as u8;\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FE0_reg(r1: i32, r2: i32) { instr_660FE0(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE0_mem(addr: i32, r: i32) {\n    instr_660FE0(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE1(source: u64, r: i32) {\n    // psraw mm, mm/m64\n    psraw_r64(r, source);\n}\npub unsafe fn instr_0FE1_reg(r1: i32, r2: i32) { instr_0FE1(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE1_mem(addr: i32, r: i32) {\n    instr_0FE1(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE1(source: reg128, r: i32) {\n    // psraw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    psraw_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FE1_reg(r1: i32, r2: i32) { instr_660FE1(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE1_mem(addr: i32, r: i32) {\n    instr_660FE1(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE2(source: u64, r: i32) {\n    // psrad mm, mm/m64\n    psrad_r64(r, source);\n}\npub unsafe fn instr_0FE2_reg(r1: i32, r2: i32) { instr_0FE2(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE2_mem(addr: i32, r: i32) {\n    instr_0FE2(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE2(source: reg128, r: i32) {\n    // psrad xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    psrad_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FE2_reg(r1: i32, r2: i32) { instr_660FE2(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE2_mem(addr: i32, r: i32) {\n    instr_660FE2(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE3(source: u64, r: i32) {\n    // pavgw mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = (destination[i] as i32 + source[i] as i32 + 1 >> 1) as u16\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FE3_reg(r1: i32, r2: i32) { instr_0FE3(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE3_mem(addr: i32, r: i32) {\n    instr_0FE3(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE3(source: reg128, r: i32) {\n    // pavgw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let mut destination = read_xmm128s(r);\n    for i in 0..8 {\n        destination.u16[i] = (destination.u16[i] as i32 + source.u16[i] as i32 + 1 >> 1) as u16;\n    }\n    write_xmm_reg128(r, destination);\n}\npub unsafe fn instr_660FE3_reg(r1: i32, r2: i32) { instr_660FE3(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE3_mem(addr: i32, r: i32) {\n    instr_660FE3(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE4(source: u64, r: i32) {\n    // pmulhuw mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = ((destination[i] as i32 * source[i] as i32) >> 16) as u16\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FE4_reg(r1: i32, r2: i32) { instr_0FE4(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE4_mem(addr: i32, r: i32) {\n    instr_0FE4(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE4(source: reg128, r: i32) {\n    // pmulhuw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = (source.u16[i] as i32 * destination.u16[i] as i32 >> 16) as u16;\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FE4_reg(r1: i32, r2: i32) { instr_660FE4(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE4_mem(addr: i32, r: i32) {\n    instr_660FE4(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE5(source: u64, r: i32) {\n    // pmulhw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = ((destination[i] as i32 * source[i] as i32) >> 16) as i16\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FE5_reg(r1: i32, r2: i32) { instr_0FE5(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE5_mem(addr: i32, r: i32) {\n    instr_0FE5(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE5(source: reg128, r: i32) {\n    // pmulhw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = (destination.i16[i] as i32 * source.i16[i] as i32 >> 16) as u16\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FE5_reg(r1: i32, r2: i32) { instr_660FE5(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE5_mem(addr: i32, r: i32) {\n    instr_660FE5(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\n#[no_mangle]\npub unsafe fn instr_0FE6_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn instr_0FE6_reg(_r1: i32, _r2: i32) { trigger_ud(); }\n\n#[no_mangle]\npub unsafe fn instr_660FE6(source: reg128, r: i32) {\n    // cvttpd2dq xmm1, xmm2/m128\n    let result = reg128 {\n        i32: [\n            sse_convert_with_truncation_f64_to_i32(source.f64[0]),\n            sse_convert_with_truncation_f64_to_i32(source.f64[1]),\n            0,\n            0,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FE6_mem(addr: i32, r: i32) {\n    instr_660FE6(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_660FE6_reg(r1: i32, r2: i32) { instr_660FE6(read_xmm128s(r1), r2); }\n\n#[no_mangle]\npub unsafe fn instr_F20FE6(source: reg128, r: i32) {\n    // cvtpd2dq xmm1, xmm2/m128\n    let result = reg128 {\n        i32: [\n            // XXX: Precision exception\n            sse_convert_f64_to_i32(source.f64[0]),\n            sse_convert_f64_to_i32(source.f64[1]),\n            0,\n            0,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_F20FE6_mem(addr: i32, r: i32) {\n    instr_F20FE6(return_on_pagefault!(safe_read128s(addr)), r);\n}\npub unsafe fn instr_F20FE6_reg(r1: i32, r2: i32) { instr_F20FE6(read_xmm128s(r1), r2); }\n\n#[no_mangle]\npub unsafe fn instr_F30FE6(source: u64, r: i32) {\n    // cvtdq2pd xmm1, xmm2/m64\n    let result = reg128 {\n        f64: [\n            // Note: Conversion never fails (i32 fits into f64)\n            source as i32 as f64,\n            (source >> 32) as i32 as f64,\n        ],\n    };\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_F30FE6_mem(addr: i32, r: i32) {\n    instr_F30FE6(return_on_pagefault!(safe_read64s(addr)), r);\n}\npub unsafe fn instr_F30FE6_reg(r1: i32, r2: i32) { instr_F30FE6(read_xmm64s(r1), r2); }\n\n#[no_mangle]\npub unsafe fn instr_0FE7_mem(addr: i32, r: i32) {\n    // movntq m64, mm\n    mov_r_m64(addr, r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE7_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_660FE7_reg(_r1: i32, _r2: i32) { trigger_ud(); }\npub unsafe fn instr_660FE7_mem(addr: i32, r: i32) {\n    // movntdq m128, xmm\n    mov_r_m128(addr, r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE8(source: u64, r: i32) {\n    // psubsb mm, mm/m64\n    let destination: [i8; 8] = std::mem::transmute(read_mmx64s(r));\n    let source: [i8; 8] = std::mem::transmute(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = saturate_sd_to_sb(destination[i] as u32 - source[i] as u32);\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FE8_reg(r1: i32, r2: i32) { instr_0FE8(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE8_mem(addr: i32, r: i32) {\n    instr_0FE8(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE8(source: reg128, r: i32) {\n    // psubsb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.i8[i] = saturate_sd_to_sb(destination.i8[i] as u32 - source.i8[i] as u32);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FE8_reg(r1: i32, r2: i32) { instr_660FE8(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE8_mem(addr: i32, r: i32) {\n    instr_660FE8(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FE9(source: u64, r: i32) {\n    // psubsw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = saturate_sd_to_sw(destination[i] as u32 - source[i] as u32)\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FE9_reg(r1: i32, r2: i32) { instr_0FE9(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FE9_mem(addr: i32, r: i32) {\n    instr_0FE9(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FE9(source: reg128, r: i32) {\n    // psubsw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = saturate_sd_to_sw(destination.i16[i] as u32 - source.i16[i] as u32)\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FE9_reg(r1: i32, r2: i32) { instr_660FE9(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FE9_mem(addr: i32, r: i32) {\n    instr_660FE9(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FEA(source: u64, r: i32) {\n    // pminsw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = i16::min(destination[i], source[i])\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FEA_reg(r1: i32, r2: i32) { instr_0FEA(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FEA_mem(addr: i32, r: i32) {\n    instr_0FEA(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FEA(source: reg128, r: i32) {\n    // pminsw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.i16[i] = i16::min(destination.i16[i], source.i16[i])\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FEA_reg(r1: i32, r2: i32) { instr_660FEA(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FEA_mem(addr: i32, r: i32) {\n    instr_660FEA(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FEB(source: u64, r: i32) {\n    // por mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, source | destination);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FEB_reg(r1: i32, r2: i32) { instr_0FEB(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FEB_mem(addr: i32, r: i32) {\n    instr_0FEB(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FEB(source: reg128, r: i32) {\n    // por xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    por_r128(source, r);\n}\npub unsafe fn instr_660FEB_reg(r1: i32, r2: i32) { instr_660FEB(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FEB_mem(addr: i32, r: i32) {\n    instr_660FEB(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FEC(source: u64, r: i32) {\n    // paddsb mm, mm/m64\n    let destination: [i8; 8] = std::mem::transmute(read_mmx64s(r));\n    let source: [i8; 8] = std::mem::transmute(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = saturate_sd_to_sb(destination[i] as u32 + source[i] as u32);\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FEC_reg(r1: i32, r2: i32) { instr_0FEC(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FEC_mem(addr: i32, r: i32) {\n    instr_0FEC(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FEC(source: reg128, r: i32) {\n    // paddsb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.i8[i] = saturate_sd_to_sb(destination.i8[i] as u32 + source.i8[i] as u32);\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FEC_reg(r1: i32, r2: i32) { instr_660FEC(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FEC_mem(addr: i32, r: i32) {\n    instr_660FEC(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FED(source: u64, r: i32) {\n    // paddsw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = saturate_sd_to_sw(destination[i] as u32 + source[i] as u32)\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FED_reg(r1: i32, r2: i32) { instr_0FED(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FED_mem(addr: i32, r: i32) {\n    instr_0FED(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FED(source: reg128, r: i32) {\n    // paddsw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = saturate_sd_to_sw(destination.i16[i] as u32 + source.i16[i] as u32)\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FED_reg(r1: i32, r2: i32) { instr_660FED(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FED_mem(addr: i32, r: i32) {\n    instr_660FED(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FEE(source: u64, r: i32) {\n    // pmaxsw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = i16::max(destination[i], source[i])\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FEE_reg(r1: i32, r2: i32) { instr_0FEE(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FEE_mem(addr: i32, r: i32) {\n    instr_0FEE(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FEE(source: reg128, r: i32) {\n    // pmaxsw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.i16[i] = i16::max(destination.i16[i], source.i16[i])\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FEE_reg(r1: i32, r2: i32) { instr_660FEE(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FEE_mem(addr: i32, r: i32) {\n    instr_660FEE(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FEF(source: u64, r: i32) {\n    // pxor mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, source ^ destination);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FEF_reg(r1: i32, r2: i32) { instr_0FEF(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FEF_mem(addr: i32, r: i32) {\n    instr_0FEF(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FEF(source: reg128, r: i32) {\n    // pxor xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    pxor_r128(source, r);\n}\npub unsafe fn instr_660FEF_reg(r1: i32, r2: i32) { instr_660FEF(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FEF_mem(addr: i32, r: i32) {\n    instr_660FEF(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF0() { unimplemented_sse(); }\n#[no_mangle]\npub unsafe fn instr_0FF1(source: u64, r: i32) {\n    // psllw mm, mm/m64\n    psllw_r64(r, source);\n}\npub unsafe fn instr_0FF1_reg(r1: i32, r2: i32) { instr_0FF1(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF1_mem(addr: i32, r: i32) {\n    instr_0FF1(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF1(source: reg128, r: i32) {\n    // psllw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    psllw_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FF1_reg(r1: i32, r2: i32) { instr_660FF1(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF1_mem(addr: i32, r: i32) {\n    instr_660FF1(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF2(source: u64, r: i32) {\n    // pslld mm, mm/m64\n    pslld_r64(r, source);\n}\npub unsafe fn instr_0FF2_reg(r1: i32, r2: i32) { instr_0FF2(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF2_mem(addr: i32, r: i32) {\n    instr_0FF2(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF2(source: reg128, r: i32) {\n    // pslld xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    pslld_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FF2_reg(r1: i32, r2: i32) { instr_660FF2(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF2_mem(addr: i32, r: i32) {\n    instr_660FF2(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF3(source: u64, r: i32) {\n    // psllq mm, mm/m64\n    psllq_r64(r, source);\n}\npub unsafe fn instr_0FF3_reg(r1: i32, r2: i32) { instr_0FF3(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF3_mem(addr: i32, r: i32) {\n    instr_0FF3(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF3(source: reg128, r: i32) {\n    // psllq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    psllq_r128(r, source.u64[0]);\n}\npub unsafe fn instr_660FF3_reg(r1: i32, r2: i32) { instr_660FF3(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF3_mem(addr: i32, r: i32) {\n    instr_660FF3(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF4(source: u64, r: i32) {\n    // pmuludq mm, mm/m64\n    let destination = read_mmx64s(r);\n    write_mmx_reg64(r, (source as u32 as u64) * (destination as u32 as u64));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FF4_reg(r1: i32, r2: i32) { instr_0FF4(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF4_mem(addr: i32, r: i32) {\n    instr_0FF4(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF4(source: reg128, r: i32) {\n    // pmuludq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    result.u64[0] = source.u32[0] as u64 * destination.u32[0] as u64;\n    result.u64[1] = source.u32[2] as u64 * destination.u32[2] as u64;\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FF4_reg(r1: i32, r2: i32) { instr_660FF4(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF4_mem(addr: i32, r: i32) {\n    instr_660FF4(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF5(source: u64, r: i32) {\n    // pmaddwd mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mul0 = destination[0] as i32 * source[0] as i32;\n    let mul1 = destination[1] as i32 * source[1] as i32;\n    let mul2 = destination[2] as i32 * source[2] as i32;\n    let mul3 = destination[3] as i32 * source[3] as i32;\n    let low = mul0 + mul1;\n    let high = mul2 + mul3;\n    write_mmx_reg64(r, low as u32 as u64 | (high as u64) << 32);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FF5_reg(r1: i32, r2: i32) { instr_0FF5(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF5_mem(addr: i32, r: i32) {\n    instr_0FF5(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF5(source: reg128, r: i32) {\n    // pmaddwd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..4 {\n        result.i32[i] = destination.i16[2 * i] as i32 * source.i16[2 * i] as i32\n            + destination.i16[2 * i + 1] as i32 * source.i16[2 * i + 1] as i32\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FF5_reg(r1: i32, r2: i32) { instr_660FF5(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF5_mem(addr: i32, r: i32) {\n    instr_660FF5(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF6(source: u64, r: i32) {\n    // psadbw mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut sum = 0;\n    for i in 0..8 {\n        sum += (destination[i] as i32 - source[i] as i32).abs() as u64;\n    }\n    write_mmx_reg64(r, sum);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FF6_reg(r1: i32, r2: i32) { instr_0FF6(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF6_mem(addr: i32, r: i32) {\n    instr_0FF6(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF6(source: reg128, r: i32) {\n    // psadbw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut sum0 = 0;\n    let mut sum1 = 0;\n    for i in 0..8 {\n        sum0 += (destination.u8[i + 0] as i32 - source.u8[i + 0] as i32).abs() as u32;\n        sum1 += (destination.u8[i + 8] as i32 - source.u8[i + 8] as i32).abs() as u32;\n    }\n    write_xmm128(r, sum0 as i32, 0, sum1 as i32, 0);\n}\npub unsafe fn instr_660FF6_reg(r1: i32, r2: i32) { instr_660FF6(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF6_mem(addr: i32, r: i32) {\n    instr_660FF6(return_on_pagefault!(safe_read128s(addr)), r);\n}\n\npub unsafe fn instr_0FF7_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn maskmovq(r1: i32, r2: i32, addr: i32) {\n    // maskmovq mm, mm\n    let source: [u8; 8] = u64::to_le_bytes(read_mmx64s(r2));\n    let mask: [u8; 8] = u64::to_le_bytes(read_mmx64s(r1));\n    match writable_or_pagefault(addr, 8) {\n        Ok(()) => *page_fault = false,\n        Err(()) => {\n            *page_fault = true;\n            return;\n        },\n    }\n    for i in 0..8 {\n        if 0 != mask[i] & 0x80 {\n            safe_write8(addr + i as i32, source[i] as i32).unwrap();\n        }\n    }\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FF7_reg(r1: i32, r2: i32) {\n    maskmovq(\n        r1,\n        r2,\n        return_on_pagefault!(get_seg_prefix_ds(get_reg_asize(EDI))),\n    )\n}\n\npub unsafe fn instr_660FF7_mem(_addr: i32, _r: i32) { trigger_ud(); }\n#[no_mangle]\npub unsafe fn maskmovdqu(r1: i32, r2: i32, addr: i32) {\n    // maskmovdqu xmm, xmm\n    let source = read_xmm128s(r2);\n    let mask = read_xmm128s(r1);\n    match writable_or_pagefault(addr, 16) {\n        Ok(()) => *page_fault = false,\n        Err(()) => {\n            *page_fault = true;\n            return;\n        },\n    }\n    for i in 0..16 {\n        if 0 != mask.u8[i] & 0x80 {\n            safe_write8(addr + i as i32, source.u8[i] as i32).unwrap();\n        }\n    }\n}\npub unsafe fn instr_660FF7_reg(r1: i32, r2: i32) {\n    maskmovdqu(\n        r1,\n        r2,\n        return_on_pagefault!(get_seg_prefix_ds(get_reg_asize(EDI))),\n    )\n}\n#[no_mangle]\npub unsafe fn instr_0FF8(source: u64, r: i32) {\n    // psubb mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = destination[i] - source[i];\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FF8_reg(r1: i32, r2: i32) { instr_0FF8(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF8_mem(addr: i32, r: i32) {\n    instr_0FF8(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF8(source: reg128, r: i32) {\n    // psubb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = destination.u8[i] - source.u8[i];\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FF8_reg(r1: i32, r2: i32) { instr_660FF8(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF8_mem(addr: i32, r: i32) {\n    instr_660FF8(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FF9(source: u64, r: i32) {\n    // psubw mm, mm/m64\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [i16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = destination[i] - source[i]\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FF9_reg(r1: i32, r2: i32) { instr_0FF9(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FF9_mem(addr: i32, r: i32) {\n    instr_0FF9(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FF9(source: reg128, r: i32) {\n    // psubw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.i16[i] = destination.i16[i] - source.i16[i]\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FF9_reg(r1: i32, r2: i32) { instr_660FF9(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FF9_mem(addr: i32, r: i32) {\n    instr_660FF9(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FFA(source: u64, r: i32) {\n    // psubd mm, mm/m64\n    let destination: [i32; 2] = std::mem::transmute(read_mmx64s(r));\n    let source: [i32; 2] = std::mem::transmute(source);\n    let mut result = [0; 2];\n    for i in 0..2 {\n        result[i] = destination[i] - source[i]\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FFA_reg(r1: i32, r2: i32) { instr_0FFA(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FFA_mem(addr: i32, r: i32) {\n    instr_0FFA(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FFA(source: reg128, r: i32) {\n    // psubd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    write_xmm128(\n        r,\n        destination.i32[0] - source.i32[0],\n        destination.i32[1] - source.i32[1],\n        destination.i32[2] - source.i32[2],\n        destination.i32[3] - source.i32[3],\n    );\n}\npub unsafe fn instr_660FFA_reg(r1: i32, r2: i32) { instr_660FFA(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FFA_mem(addr: i32, r: i32) {\n    instr_660FFA(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FFB(source: u64, r: i32) {\n    // psubq mm, mm/m64\n    write_mmx_reg64(r, read_mmx64s(r) - source);\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FFB_reg(r1: i32, r2: i32) { instr_0FFB(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FFB_mem(addr: i32, r: i32) {\n    instr_0FFB(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FFB(source: reg128, r: i32) {\n    // psubq xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let mut destination = read_xmm128s(r);\n    destination.u64[0] = destination.u64[0] - source.u64[0];\n    destination.u64[1] = destination.u64[1] - source.u64[1];\n    write_xmm_reg128(r, destination);\n}\npub unsafe fn instr_660FFB_reg(r1: i32, r2: i32) { instr_660FFB(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FFB_mem(addr: i32, r: i32) {\n    instr_660FFB(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FFC(source: u64, r: i32) {\n    // paddb mm, mm/m64\n    let destination: [u8; 8] = u64::to_le_bytes(read_mmx64s(r));\n    let source: [u8; 8] = u64::to_le_bytes(source);\n    let mut result = [0; 8];\n    for i in 0..8 {\n        result[i] = destination[i] + source[i];\n    }\n    write_mmx_reg64(r, u64::from_le_bytes(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FFC_reg(r1: i32, r2: i32) { instr_0FFC(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FFC_mem(addr: i32, r: i32) {\n    instr_0FFC(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FFC(source: reg128, r: i32) {\n    // paddb xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..16 {\n        result.u8[i] = destination.u8[i] + source.u8[i];\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FFC_reg(r1: i32, r2: i32) { instr_660FFC(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FFC_mem(addr: i32, r: i32) {\n    instr_660FFC(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FFD(source: u64, r: i32) {\n    // paddw mm, mm/m64\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let source: [u16; 4] = std::mem::transmute(source);\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = destination[i] + source[i]\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FFD_reg(r1: i32, r2: i32) { instr_0FFD(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FFD_mem(addr: i32, r: i32) {\n    instr_0FFD(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FFD(source: reg128, r: i32) {\n    // paddw xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    for i in 0..8 {\n        result.u16[i] = (destination.u16[i] as i32 + source.u16[i] as i32 & 0xFFFF) as u16;\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn instr_660FFD_reg(r1: i32, r2: i32) { instr_660FFD(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FFD_mem(addr: i32, r: i32) {\n    instr_660FFD(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FFE(source: u64, r: i32) {\n    // paddd mm, mm/m64\n    let destination: [i32; 2] = std::mem::transmute(read_mmx64s(r));\n    let source: [i32; 2] = std::mem::transmute(source);\n    let mut result = [0; 2];\n    for i in 0..2 {\n        result[i] = destination[i] + source[i]\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn instr_0FFE_reg(r1: i32, r2: i32) { instr_0FFE(read_mmx64s(r1), r2); }\npub unsafe fn instr_0FFE_mem(addr: i32, r: i32) {\n    instr_0FFE(return_on_pagefault!(safe_read64s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_660FFE(source: reg128, r: i32) {\n    // paddd xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let dword0 = destination.i32[0] + source.i32[0];\n    let dword1 = destination.i32[1] + source.i32[1];\n    let dword2 = destination.i32[2] + source.i32[2];\n    let dword3 = destination.i32[3] + source.i32[3];\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn instr_660FFE_reg(r1: i32, r2: i32) { instr_660FFE(read_xmm128s(r1), r2); }\npub unsafe fn instr_660FFE_mem(addr: i32, r: i32) {\n    instr_660FFE(return_on_pagefault!(safe_read128s(addr)), r);\n}\n#[no_mangle]\npub unsafe fn instr_0FFF() {\n    // Windows 98\n    dbg_log!(\"#ud: 0F FF\");\n    trigger_ud();\n}\n"
  },
  {
    "path": "src/rust/cpu/ioapic.rs",
    "content": "// http://download.intel.com/design/chipsets/datashts/29056601.pdf\n\nuse crate::cpu::{apic, global_pointers::acpi_enabled};\nuse std::sync::{Mutex, MutexGuard};\n\nconst IOAPIC_LOG_VERBOSE: bool = false;\n\nconst IOREGSEL: u32 = 0;\nconst IOWIN: u32 = 0x10;\n\nconst IOAPIC_IRQ_COUNT: usize = 24;\n\nconst IOAPIC_FIRST_IRQ_REG: u32 = 0x10;\nconst IOAPIC_LAST_IRQ_REG: u32 = 0x10 + 2 * IOAPIC_IRQ_COUNT as u32;\n\nconst IOAPIC_ID: u32 = 0; // must match value in seabios\n\npub const IOAPIC_CONFIG_TRIGGER_MODE_LEVEL: u32 = 1 << 15;\n\nconst IOAPIC_CONFIG_MASKED: u32 = 1 << 16;\nconst IOAPIC_CONFIG_DELIVS: u32 = 1 << 12;\nconst IOAPIC_CONFIG_REMOTE_IRR: u32 = 1 << 14;\nconst IOAPIC_CONFIG_READONLY_MASK: u32 =\n    IOAPIC_CONFIG_REMOTE_IRR | IOAPIC_CONFIG_DELIVS | 0xFFFE0000;\n\nconst IOAPIC_DELIVERY_FIXED: u8 = 0;\nconst IOAPIC_DELIVERY_LOWEST_PRIORITY: u8 = 1;\nconst _IOAPIC_DELIVERY_NMI: u8 = 4;\nconst _IOAPIC_DELIVERY_INIT: u8 = 5;\n\nconst DELIVERY_MODES: [&str; 8] = [\n    \"Fixed (0)\",\n    \"Lowest Prio (1)\",\n    \"SMI (2)\",\n    \"Reserved (3)\",\n    \"NMI (4)\",\n    \"INIT (5)\",\n    \"Reserved (6)\",\n    \"ExtINT (7)\",\n];\n\nconst DESTINATION_MODES: [&str; 2] = [\"physical\", \"logical\"];\n\n// keep in sync with cpu.js\n#[allow(dead_code)]\nconst IOAPIC_STRUCT_SIZE: usize = 4 * 52;\n\n// Note: JavaScript (cpu.get_state_apic) depens on this layout\nconst _: () = assert!(std::mem::offset_of!(Ioapic, ioredtbl_destination) == 24 * 4);\nconst _: () = assert!(std::mem::offset_of!(Ioapic, ioregsel) == 48 * 4);\nconst _: () = assert!(std::mem::offset_of!(Ioapic, irq_value) == 51 * 4);\nconst _: () = assert!(std::mem::size_of::<Ioapic>() == IOAPIC_STRUCT_SIZE);\n#[repr(C)]\nstruct Ioapic {\n    ioredtbl_config: [u32; IOAPIC_IRQ_COUNT],\n    ioredtbl_destination: [u32; IOAPIC_IRQ_COUNT],\n    ioregsel: u32,\n    ioapic_id: u32,\n    irr: u32,\n    irq_value: u32,\n}\n\nstatic IOAPIC: Mutex<Ioapic> = Mutex::new(Ioapic {\n    ioredtbl_config: [IOAPIC_CONFIG_MASKED; IOAPIC_IRQ_COUNT],\n    ioredtbl_destination: [0; IOAPIC_IRQ_COUNT],\n    ioregsel: 0,\n    ioapic_id: IOAPIC_ID,\n    irr: 0,\n    irq_value: 0,\n});\n\nfn get_ioapic() -> MutexGuard<'static, Ioapic> { IOAPIC.try_lock().unwrap() }\n\n#[no_mangle]\npub fn get_ioapic_addr() -> u32 { &raw mut *get_ioapic() as u32 }\n\npub fn remote_eoi(apic: &mut apic::Apic, vector: u8) {\n    remote_eoi_internal(&mut get_ioapic(), apic, vector);\n}\n\nfn remote_eoi_internal(ioapic: &mut Ioapic, apic: &mut apic::Apic, vector: u8) {\n    for i in 0..IOAPIC_IRQ_COUNT as u8 {\n        let config = ioapic.ioredtbl_config[i as usize];\n\n        if (config & 0xFF) as u8 == vector && config & IOAPIC_CONFIG_REMOTE_IRR != 0 {\n            dbg_log!(\"Clear remote IRR for irq={:x}\", i);\n            ioapic.ioredtbl_config[i as usize] &= !IOAPIC_CONFIG_REMOTE_IRR;\n            check_irq(ioapic, apic, i);\n        }\n    }\n}\n\nfn check_irq(ioapic: &mut Ioapic, apic: &mut apic::Apic, irq: u8) {\n    let mask = 1 << irq;\n\n    if ioapic.irr & mask == 0 {\n        return;\n    }\n\n    let config = ioapic.ioredtbl_config[irq as usize];\n\n    if config & IOAPIC_CONFIG_MASKED == 0 {\n        let delivery_mode = ((config >> 8) & 7) as u8;\n        let destination_mode = ((config >> 11) & 1) as u8;\n        let vector = (config & 0xFF) as u8;\n        let destination = (ioapic.ioredtbl_destination[irq as usize] >> 24) as u8;\n        let is_level =\n            config & IOAPIC_CONFIG_TRIGGER_MODE_LEVEL == IOAPIC_CONFIG_TRIGGER_MODE_LEVEL;\n\n        if config & IOAPIC_CONFIG_TRIGGER_MODE_LEVEL == 0 {\n            ioapic.irr &= !mask;\n        }\n        else {\n            ioapic.ioredtbl_config[irq as usize] |= IOAPIC_CONFIG_REMOTE_IRR;\n\n            if config & IOAPIC_CONFIG_REMOTE_IRR != 0 {\n                dbg_log!(\"No route: level interrupt and remote IRR still set\");\n                return;\n            }\n        }\n\n        if delivery_mode == IOAPIC_DELIVERY_FIXED\n            || delivery_mode == IOAPIC_DELIVERY_LOWEST_PRIORITY\n        {\n            apic::route(\n                apic,\n                vector,\n                delivery_mode,\n                is_level,\n                destination,\n                destination_mode,\n            );\n        }\n        else {\n            dbg_assert!(false, \"TODO\");\n        }\n\n        ioapic.ioredtbl_config[irq as usize] &= !IOAPIC_CONFIG_DELIVS;\n    }\n}\n\npub fn set_irq(i: u8) { set_irq_internal(&mut get_ioapic(), &mut apic::get_apic(), i) }\n\nfn set_irq_internal(ioapic: &mut Ioapic, apic: &mut apic::Apic, i: u8) {\n    if i as usize >= IOAPIC_IRQ_COUNT {\n        dbg_assert!(false, \"Bad irq: {}\", i);\n        return;\n    }\n\n    let mask = 1 << i;\n\n    if ioapic.irq_value & mask == 0 {\n        if IOAPIC_LOG_VERBOSE {\n            dbg_log!(\"apic set irq {}\", i);\n        }\n\n        ioapic.irq_value |= mask;\n\n        let config = ioapic.ioredtbl_config[i as usize];\n        if config & (IOAPIC_CONFIG_TRIGGER_MODE_LEVEL | IOAPIC_CONFIG_MASKED)\n            == IOAPIC_CONFIG_MASKED\n        {\n            // edge triggered and masked\n            return;\n        }\n\n        ioapic.irr |= mask;\n\n        check_irq(ioapic, apic, i);\n    }\n}\n\npub fn clear_irq(i: u8) { clear_irq_internal(&mut get_ioapic(), i) }\n\nfn clear_irq_internal(ioapic: &mut Ioapic, i: u8) {\n    if i as usize >= IOAPIC_IRQ_COUNT {\n        dbg_assert!(false, \"Bad irq: {}\", i);\n        return;\n    }\n\n    let mask = 1 << i;\n\n    if ioapic.irq_value & mask == mask {\n        ioapic.irq_value &= !mask;\n\n        let config = ioapic.ioredtbl_config[i as usize];\n        if config & IOAPIC_CONFIG_TRIGGER_MODE_LEVEL != 0 {\n            ioapic.irr &= !mask;\n        }\n    }\n}\n\npub fn read32(addr: u32) -> u32 {\n    if unsafe { !*acpi_enabled } {\n        return 0;\n    }\n    read32_internal(&mut get_ioapic(), addr)\n}\n\nfn read32_internal(ioapic: &mut Ioapic, addr: u32) -> u32 {\n    match addr {\n        IOREGSEL => ioapic.ioregsel,\n        IOWIN => match ioapic.ioregsel {\n            0 => {\n                dbg_log!(\"IOAPIC Read id\");\n                ioapic.ioapic_id << 24\n            },\n            1 => {\n                dbg_log!(\"IOAPIC Read version\");\n                0x11 | (IOAPIC_IRQ_COUNT as u32 - 1) << 16\n            },\n            2 => {\n                dbg_log!(\"IOAPIC Read arbitration id\");\n                ioapic.ioapic_id << 24\n            },\n            IOAPIC_FIRST_IRQ_REG..IOAPIC_LAST_IRQ_REG => {\n                let irq = ((ioapic.ioregsel - IOAPIC_FIRST_IRQ_REG) >> 1) as u8;\n                let index = ioapic.ioregsel & 1;\n\n                if index != 0 {\n                    let value = ioapic.ioredtbl_destination[irq as usize];\n                    dbg_log!(\"IOAPIC Read destination irq={:x} -> {:08x}\", irq, value);\n                    value\n                }\n                else {\n                    let value = ioapic.ioredtbl_config[irq as usize];\n                    dbg_log!(\"IOAPIC Read config irq={:x} -> {:08x}\", irq, value);\n                    value\n                }\n            },\n            reg => {\n                dbg_assert!(false, \"IOAPIC register read outside of range {:x}\", reg);\n                0\n            },\n        },\n        _ => {\n            dbg_assert!(false, \"Unaligned or oob IOAPIC memory read: {:x}\", addr);\n            0\n        },\n    }\n}\n\npub fn write32(addr: u32, value: u32) {\n    if unsafe { !*acpi_enabled } {\n        return;\n    }\n    write32_internal(&mut get_ioapic(), &mut apic::get_apic(), addr, value)\n}\n\nfn write32_internal(ioapic: &mut Ioapic, apic: &mut apic::Apic, addr: u32, value: u32) {\n    //dbg_log!(\"IOAPIC write {:x} <- {:08x}\", reg, value);\n\n    match addr {\n        IOREGSEL => ioapic.ioregsel = value,\n        IOWIN => match ioapic.ioregsel {\n            0 => ioapic.ioapic_id = (value >> 24) & 0x0F,\n            1 | 2 => {\n                dbg_log!(\"IOAPIC Invalid write: {}\", ioapic.ioregsel);\n            },\n            IOAPIC_FIRST_IRQ_REG..IOAPIC_LAST_IRQ_REG => {\n                let irq = ((ioapic.ioregsel - IOAPIC_FIRST_IRQ_REG) >> 1) as u8;\n                let index = ioapic.ioregsel & 1;\n\n                if index != 0 {\n                    dbg_log!(\n                        \"Write destination {:08x} irq={:x} dest={:02x}\",\n                        value,\n                        irq,\n                        value >> 24\n                    );\n                    ioapic.ioredtbl_destination[irq as usize] = value & 0xFF000000;\n                }\n                else {\n                    let old_value = ioapic.ioredtbl_config[irq as usize] as u32;\n                    ioapic.ioredtbl_config[irq as usize] = (value & !IOAPIC_CONFIG_READONLY_MASK)\n                        | (old_value & IOAPIC_CONFIG_READONLY_MASK);\n\n                    let vector = value & 0xFF;\n                    let delivery_mode = (value >> 8) & 7;\n                    let destination_mode = (value >> 11) & 1;\n                    let is_level = (value >> 15) & 1;\n                    let disabled = (value >> 16) & 1;\n\n                    dbg_log!(\n                            \"Write config {:08x} irq={:x} vector={:02x} deliverymode={} destmode={} is_level={} disabled={}\",\n                            value,\n                            irq,\n                            vector,\n                            DELIVERY_MODES[delivery_mode as usize],\n                            DESTINATION_MODES[destination_mode as usize],\n                            is_level,\n                            disabled\n                        );\n\n                    check_irq(ioapic, apic, irq);\n                }\n            },\n            reg => {\n                dbg_assert!(\n                    false,\n                    \"IOAPIC register write outside of range {:x} <- {:x}\",\n                    reg,\n                    value\n                )\n            },\n        },\n        _ => {\n            dbg_assert!(\n                false,\n                \"Unaligned or oob IOAPIC memory write: {:x} <- {:x}\",\n                addr,\n                value\n            )\n        },\n    }\n}\n"
  },
  {
    "path": "src/rust/cpu/memory.rs",
    "content": "mod ext {\n    extern \"C\" {\n        pub fn mmap_read8(addr: u32) -> i32;\n        pub fn mmap_read32(addr: u32) -> i32;\n\n        pub fn mmap_write8(addr: u32, value: i32);\n        pub fn mmap_write16(addr: u32, value: i32);\n        pub fn mmap_write32(addr: u32, value: i32);\n        pub fn mmap_write64(addr: u32, v0: i32, v1: i32);\n        pub fn mmap_write128(addr: u32, v0: i32, v1: i32, v2: i32, v3: i32);\n    }\n}\n\nuse crate::cpu::apic;\nuse crate::cpu::cpu::{\n    handle_irqs, reg128, APIC_MEM_ADDRESS, APIC_MEM_SIZE, IOAPIC_MEM_ADDRESS, IOAPIC_MEM_SIZE,\n};\nuse crate::cpu::global_pointers::memory_size;\nuse crate::cpu::ioapic;\nuse crate::cpu::vga;\nuse crate::jit;\nuse crate::page::Page;\n\nuse std::alloc;\nuse std::ptr;\n\n#[allow(non_upper_case_globals)]\npub static mut mem8: *mut u8 = ptr::null_mut();\n\n#[no_mangle]\npub fn allocate_memory(size: u32) -> u32 {\n    unsafe {\n        dbg_assert!(mem8.is_null());\n    };\n    dbg_log!(\"Allocate memory size={}m\", size >> 20);\n    let layout = alloc::Layout::from_size_align(size as usize, 0x1000).unwrap();\n    let ptr = unsafe { alloc::alloc(layout) as u32 };\n    unsafe {\n        mem8 = ptr as *mut u8;\n    };\n    ptr\n}\n\n#[no_mangle]\npub unsafe fn zero_memory(addr: u32, size: u32) {\n    ptr::write_bytes(mem8.offset(addr as isize), 0, size as usize);\n}\n\n#[allow(non_upper_case_globals)]\npub static mut vga_mem8: *mut u8 = ptr::null_mut();\n#[allow(non_upper_case_globals)]\npub static mut vga_memory_size: u32 = 0;\n\n#[no_mangle]\npub fn svga_allocate_memory(size: u32) -> u32 {\n    unsafe {\n        dbg_assert!(vga_mem8.is_null());\n    };\n    let layout = alloc::Layout::from_size_align(size as usize, 0x1000).unwrap();\n    let ptr = unsafe { alloc::alloc(layout) };\n    dbg_assert!(\n        size & (1 << 12 << 6) == 0,\n        \"size not aligned to dirty_bitmap\"\n    );\n    unsafe {\n        vga_mem8 = ptr;\n        vga_memory_size = size;\n        vga::set_dirty_bitmap_size(size >> 12 >> 6);\n    };\n    ptr as u32\n}\n\n#[no_mangle]\npub fn in_mapped_range(addr: u32) -> bool {\n    return addr >= 0xA0000 && addr < 0xC0000 || addr >= unsafe { *memory_size };\n}\n\npub const VGA_LFB_ADDRESS: u32 = 0xE0000000;\npub fn in_svga_lfb(addr: u32) -> bool {\n    addr >= VGA_LFB_ADDRESS && addr <= unsafe { VGA_LFB_ADDRESS + (vga_memory_size - 1) }\n}\n\n#[no_mangle]\npub fn read8(addr: u32) -> i32 {\n    if in_mapped_range(addr) {\n        if in_svga_lfb(addr) {\n            unsafe { *vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as i32 }\n        }\n        else if addr >= APIC_MEM_ADDRESS && addr < APIC_MEM_ADDRESS + APIC_MEM_SIZE {\n            apic::read32((addr - APIC_MEM_ADDRESS) & !3) as i32 >> 8 * (addr & 3) & 0xFF\n        }\n        else if addr >= IOAPIC_MEM_ADDRESS && addr < IOAPIC_MEM_ADDRESS + IOAPIC_MEM_SIZE {\n            ioapic::read32((addr - IOAPIC_MEM_ADDRESS) & !3) as i32 >> 8 * (addr & 3) & 0xFF\n        }\n        else {\n            unsafe { ext::mmap_read8(addr) }\n        }\n    }\n    else {\n        read8_no_mmap_check(addr)\n    }\n}\npub fn read8_no_mmap_check(addr: u32) -> i32 { unsafe { *mem8.offset(addr as isize) as i32 } }\n\n#[no_mangle]\npub fn read16(addr: u32) -> i32 {\n    if in_mapped_range(addr) {\n        if in_svga_lfb(addr) {\n            unsafe {\n                ptr::read_unaligned(vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *const u16)\n                    as i32\n            }\n        }\n        else {\n            read8(addr) | read8(addr + 1) << 8\n        }\n    }\n    else {\n        read16_no_mmap_check(addr)\n    }\n}\npub fn read16_no_mmap_check(addr: u32) -> i32 {\n    unsafe { ptr::read_unaligned(mem8.offset(addr as isize) as *const u16) as i32 }\n}\n\n#[no_mangle]\npub fn read32s(addr: u32) -> i32 {\n    if in_mapped_range(addr) {\n        if in_svga_lfb(addr) {\n            unsafe {\n                ptr::read_unaligned(vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *const i32)\n            } // XXX\n        }\n        else if addr >= APIC_MEM_ADDRESS && addr < APIC_MEM_ADDRESS + APIC_MEM_SIZE {\n            apic::read32(addr - APIC_MEM_ADDRESS) as i32\n        }\n        else if addr >= IOAPIC_MEM_ADDRESS && addr < IOAPIC_MEM_ADDRESS + IOAPIC_MEM_SIZE {\n            ioapic::read32(addr - IOAPIC_MEM_ADDRESS) as i32\n        }\n        else {\n            unsafe { ext::mmap_read32(addr) }\n        }\n    }\n    else {\n        read32_no_mmap_check(addr)\n    }\n}\npub fn read32_no_mmap_check(addr: u32) -> i32 {\n    unsafe { ptr::read_unaligned(mem8.offset(addr as isize) as *const i32) }\n}\n\npub unsafe fn read64s(addr: u32) -> i64 {\n    if in_mapped_range(addr) {\n        if in_svga_lfb(addr) {\n            ptr::read_unaligned(vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *const i64)\n        }\n        else {\n            read32s(addr) as i64 | (read32s(addr + 4) as i64) << 32\n        }\n    }\n    else {\n        ptr::read_unaligned(mem8.offset(addr as isize) as *const i64)\n    }\n}\n\npub unsafe fn read128(addr: u32) -> reg128 {\n    if in_mapped_range(addr) {\n        if in_svga_lfb(addr) {\n            ptr::read_unaligned(vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *const reg128)\n        }\n        else {\n            reg128 {\n                i32: [\n                    read32s(addr + 0),\n                    read32s(addr + 4),\n                    read32s(addr + 8),\n                    read32s(addr + 12),\n                ],\n            }\n        }\n    }\n    else {\n        ptr::read_unaligned(mem8.offset(addr as isize) as *const reg128)\n    }\n}\n\n#[no_mangle]\npub unsafe fn write8(addr: u32, value: i32) {\n    if in_mapped_range(addr) {\n        mmap_write8(addr, value & 0xFF);\n    }\n    else {\n        jit::jit_dirty_page(Page::page_of(addr));\n        write8_no_mmap_or_dirty_check(addr, value);\n    };\n}\n\npub unsafe fn write8_no_mmap_or_dirty_check(addr: u32, value: i32) {\n    *mem8.offset(addr as isize) = value as u8\n}\n\n#[no_mangle]\npub unsafe fn write16(addr: u32, value: i32) {\n    if in_mapped_range(addr) {\n        mmap_write16(addr, value & 0xFFFF);\n    }\n    else {\n        jit::jit_dirty_cache_small(addr, addr + 2);\n        write16_no_mmap_or_dirty_check(addr, value);\n    };\n}\npub unsafe fn write16_no_mmap_or_dirty_check(addr: u32, value: i32) {\n    ptr::write_unaligned(mem8.offset(addr as isize) as *mut u16, value as u16)\n}\n\n#[no_mangle]\npub unsafe fn write32(addr: u32, value: i32) {\n    if in_mapped_range(addr) {\n        mmap_write32(addr, value);\n    }\n    else {\n        jit::jit_dirty_cache_small(addr, addr + 4);\n        write32_no_mmap_or_dirty_check(addr, value);\n    }\n}\n\npub unsafe fn write32_no_mmap_or_dirty_check(addr: u32, value: i32) {\n    ptr::write_unaligned(mem8.offset(addr as isize) as *mut i32, value)\n}\n\npub unsafe fn write64_no_mmap_or_dirty_check(addr: u32, value: u64) {\n    ptr::write_unaligned(mem8.offset(addr as isize) as *mut u64, value)\n}\n\npub unsafe fn write128_no_mmap_or_dirty_check(addr: u32, value: reg128) {\n    ptr::write_unaligned(mem8.offset(addr as isize) as *mut reg128, value)\n}\n\npub unsafe fn memset_no_mmap_or_dirty_check(addr: u32, value: u8, count: u32) {\n    ptr::write_bytes(mem8.offset(addr as isize), value, count as usize);\n}\n\npub unsafe fn memcpy_no_mmap_or_dirty_check(src_addr: u32, dst_addr: u32, count: u32) {\n    dbg_assert!(src_addr < *memory_size);\n    dbg_assert!(dst_addr < *memory_size);\n    ptr::copy(\n        mem8.offset(src_addr as isize),\n        mem8.offset(dst_addr as isize),\n        count as usize,\n    )\n}\n\npub unsafe fn memcpy_into_svga_lfb(src_addr: u32, dst_addr: u32, count: u32) {\n    dbg_assert!(src_addr < *memory_size);\n    dbg_assert!(in_svga_lfb(dst_addr));\n    dbg_assert!(Page::page_of(dst_addr) == Page::page_of(dst_addr + count - 1));\n    vga::mark_dirty(dst_addr);\n    ptr::copy_nonoverlapping(\n        mem8.offset(src_addr as isize),\n        vga_mem8.offset((dst_addr - VGA_LFB_ADDRESS) as isize),\n        count as usize,\n    )\n}\n\npub unsafe fn mmap_write8(addr: u32, value: i32) {\n    if in_svga_lfb(addr) {\n        vga::mark_dirty(addr);\n        *vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) = value as u8\n    }\n    else {\n        ext::mmap_write8(addr, value)\n    }\n}\npub unsafe fn mmap_write16(addr: u32, value: i32) {\n    if in_svga_lfb(addr) {\n        vga::mark_dirty(addr);\n        ptr::write_unaligned(\n            vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *mut u16,\n            value as u16,\n        )\n    }\n    else {\n        ext::mmap_write16(addr, value)\n    }\n}\npub unsafe fn mmap_write32(addr: u32, value: i32) {\n    if in_svga_lfb(addr) {\n        vga::mark_dirty(addr);\n        ptr::write_unaligned(\n            vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *mut i32,\n            value,\n        )\n    }\n    else if addr >= APIC_MEM_ADDRESS && addr < APIC_MEM_ADDRESS + APIC_MEM_SIZE {\n        apic::write32(addr - APIC_MEM_ADDRESS, value as u32);\n        handle_irqs();\n    }\n    else if addr >= IOAPIC_MEM_ADDRESS && addr < IOAPIC_MEM_ADDRESS + IOAPIC_MEM_SIZE {\n        ioapic::write32(addr - IOAPIC_MEM_ADDRESS, value as u32);\n        handle_irqs();\n    }\n    else {\n        ext::mmap_write32(addr, value)\n    }\n}\npub unsafe fn mmap_write64(addr: u32, value: u64) {\n    if in_svga_lfb(addr) {\n        vga::mark_dirty(addr);\n        ptr::write_unaligned(\n            vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *mut u64,\n            value,\n        )\n    }\n    else {\n        ext::mmap_write64(addr, value as i32, (value >> 32) as i32)\n    }\n}\npub unsafe fn mmap_write128(addr: u32, v0: u64, v1: u64) {\n    if in_svga_lfb(addr) {\n        vga::mark_dirty(addr);\n        ptr::write_unaligned(\n            vga_mem8.offset((addr - VGA_LFB_ADDRESS) as isize) as *mut u64,\n            v0,\n        );\n        ptr::write_unaligned(\n            vga_mem8.offset((addr - VGA_LFB_ADDRESS + 8) as isize) as *mut u64,\n            v1,\n        )\n    }\n    else {\n        ext::mmap_write128(\n            addr,\n            v0 as i32,\n            (v0 >> 32) as i32,\n            v1 as i32,\n            (v1 >> 32) as i32,\n        )\n    }\n}\n\n#[no_mangle]\npub unsafe fn is_memory_zeroed(addr: u32, length: u32) -> bool {\n    dbg_assert!(addr % 8 == 0);\n    dbg_assert!(length % 8 == 0);\n    for i in (addr..addr + length).step_by(8) {\n        if *(mem8.offset(i as isize) as *const i64) != 0 {\n            return false;\n        }\n    }\n    return true;\n}\n"
  },
  {
    "path": "src/rust/cpu/misc_instr.rs",
    "content": "use crate::cpu::cpu::*;\nuse crate::cpu::fpu::{\n    fpu_load_m80, fpu_load_status_word, fpu_set_status_word, fpu_store_m80, set_control_word,\n};\nuse crate::cpu::global_pointers::*;\nuse crate::paging::OrPageFault;\n\npub unsafe fn getcf() -> bool {\n    if 0 != *flags_changed & 1 {\n        let m = (2 << *last_op_size) - 1;\n        dbg_assert!((*last_op1 as u32) <= m);\n        dbg_assert!((*last_result as u32) <= m);\n\n        let sub_mask = *flags_changed >> 31;\n\n        // sub: last_op1 < last_result  (or last_op1 < last_op2) (or (result ^ ((result ^ b) & (b ^ a))))\n        // add: last_result < last_op1  (or last_result < last_op2) (or a ^ ((a ^ b) & (b ^ result)))\n        return ((*last_result as i32 ^ sub_mask) as u32) < (*last_op1 ^ sub_mask) as u32;\n    }\n    else {\n        return 0 != *flags & 1;\n    };\n}\n#[no_mangle]\npub unsafe fn getpf() -> bool {\n    if 0 != *flags_changed & FLAG_PARITY {\n        // inverted lookup table\n        return 0 != 0x9669 << 2 >> ((*last_result ^ *last_result >> 4) & 15) & FLAG_PARITY;\n    }\n    else {\n        return 0 != *flags & FLAG_PARITY;\n    };\n}\npub unsafe fn getaf() -> bool {\n    if 0 != *flags_changed & FLAG_ADJUST {\n        let is_sub = *flags_changed & FLAG_SUB != 0;\n        let last_op2 = (*last_result - *last_op1) * if is_sub { -1 } else { 1 };\n        return 0 != (*last_op1 ^ last_op2 ^ *last_result) & FLAG_ADJUST;\n    }\n    else {\n        return 0 != *flags & FLAG_ADJUST;\n    };\n}\npub unsafe fn getzf() -> bool {\n    if 0 != *flags_changed & FLAG_ZERO {\n        return 0 != (!*last_result & *last_result - 1) >> *last_op_size & 1;\n    }\n    else {\n        return 0 != *flags & FLAG_ZERO;\n    };\n}\npub unsafe fn getsf() -> bool {\n    if 0 != *flags_changed & FLAG_SIGN {\n        return 0 != *last_result >> *last_op_size & 1;\n    }\n    else {\n        return 0 != *flags & FLAG_SIGN;\n    };\n}\npub unsafe fn getof() -> bool {\n    if 0 != *flags_changed & FLAG_OVERFLOW {\n        let is_sub = (*flags_changed as u32) >> 31;\n\n        // add: (a ^ result) & (b ^ result)\n        // sub: (a ^ result) & (b ^ result ^ 1) (or (a ^ b) & (result ^ a))\n        let b_xor_1_if_sub = (*last_result - *last_op1) - is_sub as i32;\n        return 0\n            != ((*last_op1 ^ *last_result) & (b_xor_1_if_sub ^ *last_result)) >> *last_op_size & 1;\n    }\n    else {\n        return 0 != *flags & FLAG_OVERFLOW;\n    };\n}\n\npub unsafe fn test_o() -> bool { return getof(); }\npub unsafe fn test_b() -> bool { return getcf(); }\npub unsafe fn test_z() -> bool { return getzf(); }\npub unsafe fn test_s() -> bool { return getsf(); }\n#[no_mangle]\npub unsafe fn test_p() -> bool { return getpf(); }\npub unsafe fn test_be() -> bool { return getcf() || getzf(); }\npub unsafe fn test_l() -> bool { return getsf() != getof(); }\npub unsafe fn test_le() -> bool { return getzf() || getsf() != getof(); }\npub unsafe fn test_no() -> bool { return !test_o(); }\npub unsafe fn test_nb() -> bool { return !test_b(); }\npub unsafe fn test_nz() -> bool { return !test_z(); }\npub unsafe fn test_ns() -> bool { return !test_s(); }\n#[no_mangle]\npub unsafe fn test_np() -> bool { return !test_p(); }\npub unsafe fn test_nbe() -> bool { return !test_be(); }\npub unsafe fn test_nl() -> bool { return !test_l(); }\npub unsafe fn test_nle() -> bool { return !test_le(); }\n\npub unsafe fn jmp_rel16(rel16: i32) {\n    let cs_offset = get_seg_cs();\n    // limit ip to 16 bit\n    *instruction_pointer = cs_offset + (*instruction_pointer - cs_offset + rel16 & 0xFFFF);\n}\npub unsafe fn jmpcc16(condition: bool, imm16: i32) {\n    if condition {\n        jmp_rel16(imm16);\n    };\n}\npub unsafe fn jmpcc32(condition: bool, imm32: i32) {\n    if condition {\n        *instruction_pointer += imm32\n    };\n}\npub unsafe fn loope16(imm8s: i32) { jmpcc16(0 != decr_ecx_asize(is_asize_32()) && getzf(), imm8s); }\npub unsafe fn loopne16(imm8s: i32) {\n    jmpcc16(0 != decr_ecx_asize(is_asize_32()) && !getzf(), imm8s);\n}\npub unsafe fn loop16(imm8s: i32) { jmpcc16(0 != decr_ecx_asize(is_asize_32()), imm8s); }\npub unsafe fn jcxz16(imm8s: i32) { jmpcc16(get_reg_asize(ECX) == 0, imm8s); }\npub unsafe fn loope32(imm8s: i32) { jmpcc32(0 != decr_ecx_asize(is_asize_32()) && getzf(), imm8s); }\npub unsafe fn loopne32(imm8s: i32) {\n    jmpcc32(0 != decr_ecx_asize(is_asize_32()) && !getzf(), imm8s);\n}\npub unsafe fn loop32(imm8s: i32) { jmpcc32(0 != decr_ecx_asize(is_asize_32()), imm8s); }\npub unsafe fn jcxz32(imm8s: i32) { jmpcc32(get_reg_asize(ECX) == 0, imm8s); }\n\npub unsafe fn cmovcc16(condition: bool, value: i32, r: i32) {\n    if condition {\n        write_reg16(r, value);\n    };\n}\npub unsafe fn cmovcc32(condition: bool, value: i32, r: i32) {\n    if condition {\n        write_reg32(r, value);\n    };\n}\n\npub unsafe fn get_stack_pointer(offset: i32) -> i32 {\n    if *stack_size_32 {\n        return get_seg_ss() + read_reg32(ESP) + offset;\n    }\n    else {\n        return get_seg_ss() + (read_reg16(SP) + offset & 0xFFFF);\n    };\n}\npub unsafe fn adjust_stack_reg(adjustment: i32) {\n    if *stack_size_32 {\n        write_reg32(ESP, read_reg32(ESP) + adjustment);\n    }\n    else {\n        write_reg16(SP, read_reg16(SP) + adjustment);\n    };\n}\n\npub unsafe fn push16_ss16(imm16: i32) -> OrPageFault<()> {\n    let sp = get_seg_ss() + (read_reg16(SP) - 2 & 0xFFFF);\n    safe_write16(sp, imm16)?;\n    write_reg16(SP, read_reg16(SP) - 2);\n    Ok(())\n}\npub unsafe fn push16_ss32(imm16: i32) -> OrPageFault<()> {\n    let sp = get_seg_ss() + read_reg32(ESP) - 2;\n    safe_write16(sp, imm16)?;\n    write_reg32(ESP, read_reg32(ESP) - 2);\n    Ok(())\n}\n\npub unsafe fn push16_ss16_mem(addr: i32) -> OrPageFault<()> { push16_ss16(safe_read16(addr)?) }\npub unsafe fn push16_ss32_mem(addr: i32) -> OrPageFault<()> { push16_ss32(safe_read16(addr)?) }\n\npub unsafe fn push16(imm16: i32) -> OrPageFault<()> {\n    if *stack_size_32 {\n        push16_ss32(imm16)\n    }\n    else {\n        push16_ss16(imm16)\n    }\n}\n\npub unsafe fn push32_ss16(imm32: i32) -> OrPageFault<()> {\n    let new_sp = read_reg16(SP) - 4 & 0xFFFF;\n    safe_write32(get_seg_ss() + new_sp, imm32)?;\n    write_reg16(SP, new_sp);\n    Ok(())\n}\npub unsafe fn push32_ss32(imm32: i32) -> OrPageFault<()> {\n    let new_esp = read_reg32(ESP) - 4;\n    safe_write32(get_seg_ss() + new_esp, imm32)?;\n    write_reg32(ESP, new_esp);\n    Ok(())\n}\n\npub unsafe fn push32_ss16_mem(addr: i32) -> OrPageFault<()> { push32_ss16(safe_read32s(addr)?) }\npub unsafe fn push32_ss32_mem(addr: i32) -> OrPageFault<()> { push32_ss32(safe_read32s(addr)?) }\n\npub unsafe fn push32(imm32: i32) -> OrPageFault<()> {\n    if *stack_size_32 {\n        push32_ss32(imm32)\n    }\n    else {\n        push32_ss16(imm32)\n    }\n}\n\npub unsafe fn push32_sreg(i: i32) -> OrPageFault<()> {\n    // you can't make this up ...\n    if *stack_size_32 {\n        let new_esp = read_reg32(ESP) - 4;\n        safe_write16(get_seg_ss() + new_esp, *sreg.offset(i as isize) as i32)?;\n        write_reg32(ESP, new_esp);\n    }\n    else {\n        let new_sp = read_reg16(SP) - 4 & 0xFFFF;\n        safe_write16(get_seg_ss() + new_sp, *sreg.offset(i as isize) as i32)?;\n        write_reg16(SP, new_sp);\n    }\n    Ok(())\n}\n\npub unsafe fn pop16() -> OrPageFault<i32> {\n    if *stack_size_32 {\n        pop16_ss32()\n    }\n    else {\n        pop16_ss16()\n    }\n}\npub unsafe fn pop16_ss16() -> OrPageFault<i32> {\n    let sp = get_seg_ss() + read_reg16(SP);\n    let result = safe_read16(sp)?;\n    write_reg16(SP, read_reg16(SP) + 2);\n    Ok(result)\n}\npub unsafe fn pop16_ss32() -> OrPageFault<i32> {\n    let esp = get_seg_ss() + read_reg32(ESP);\n    let result = safe_read16(esp)?;\n    write_reg32(ESP, read_reg32(ESP) + 2);\n    Ok(result)\n}\npub unsafe fn pop32s() -> OrPageFault<i32> {\n    if *stack_size_32 {\n        pop32s_ss32()\n    }\n    else {\n        pop32s_ss16()\n    }\n}\npub unsafe fn pop32s_ss16() -> OrPageFault<i32> {\n    let sp = read_reg16(SP);\n    let result = safe_read32s(get_seg_ss() + sp)?;\n    write_reg16(SP, sp + 4);\n    Ok(result)\n}\npub unsafe fn pop32s_ss32() -> OrPageFault<i32> {\n    let esp = read_reg32(ESP);\n    let result = safe_read32s(get_seg_ss() + esp)?;\n    write_reg32(ESP, read_reg32(ESP) + 4);\n    Ok(result)\n}\npub unsafe fn pusha16() {\n    let temp = read_reg16(SP);\n    // make sure we don't get a pagefault after having\n    // pushed several registers already\n    return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-16), 16));\n    push16(read_reg16(AX)).unwrap();\n    push16(read_reg16(CX)).unwrap();\n    push16(read_reg16(DX)).unwrap();\n    push16(read_reg16(BX)).unwrap();\n    push16(temp as i32).unwrap();\n    push16(read_reg16(BP)).unwrap();\n    push16(read_reg16(SI)).unwrap();\n    push16(read_reg16(DI)).unwrap();\n}\npub unsafe fn pusha32() {\n    let temp = read_reg32(ESP);\n    return_on_pagefault!(writable_or_pagefault(get_stack_pointer(-32), 32));\n    push32(read_reg32(EAX)).unwrap();\n    push32(read_reg32(ECX)).unwrap();\n    push32(read_reg32(EDX)).unwrap();\n    push32(read_reg32(EBX)).unwrap();\n    push32(temp).unwrap();\n    push32(read_reg32(EBP)).unwrap();\n    push32(read_reg32(ESI)).unwrap();\n    push32(read_reg32(EDI)).unwrap();\n}\n\npub unsafe fn lss16(addr: i32, reg: i32, seg: i32) {\n    let new_reg = return_on_pagefault!(safe_read16(addr));\n    let new_seg = return_on_pagefault!(safe_read16(addr + 2));\n\n    if !switch_seg(seg, new_seg) {\n        return;\n    }\n\n    write_reg16(reg, new_reg);\n}\n\npub unsafe fn lss32(addr: i32, reg: i32, seg: i32) {\n    let new_reg = return_on_pagefault!(safe_read32s(addr));\n    let new_seg = return_on_pagefault!(safe_read16(addr + 4));\n\n    if !switch_seg(seg, new_seg) {\n        return;\n    }\n\n    write_reg32(reg, new_reg);\n}\n\npub unsafe fn enter16(size: i32, mut nesting_level: i32) {\n    nesting_level &= 31;\n\n    if nesting_level > 0 {\n        dbg_log!(\n            \"enter16 stack={} size={} nest={}\",\n            (if *stack_size_32 { 16 } else { 32 }),\n            size,\n            nesting_level,\n        );\n    }\n\n    let ss_mask = if *stack_size_32 { -1 } else { 0xFFFF };\n    let ss = get_seg_ss();\n    let frame_temp = read_reg32(ESP) - 2;\n\n    if nesting_level > 0 {\n        let mut tmp_ebp = read_reg32(EBP);\n        for _ in 1..nesting_level {\n            tmp_ebp -= 2;\n            push16(safe_read16(ss + (tmp_ebp & ss_mask)).unwrap()).unwrap();\n        }\n        push16(frame_temp).unwrap();\n    }\n\n    return_on_pagefault!(safe_write16(ss + (frame_temp & ss_mask), read_reg16(BP)));\n    write_reg16(BP, frame_temp);\n    adjust_stack_reg(-size - 2);\n}\n\npub unsafe fn enter32(size: i32, mut nesting_level: i32) {\n    nesting_level &= 31;\n\n    if nesting_level > 0 {\n        dbg_log!(\n            \"enter32 stack={} size={} nest={}\",\n            (if *stack_size_32 { 16 } else { 32 }),\n            size,\n            nesting_level,\n        );\n    }\n\n    let ss_mask = if *stack_size_32 { -1 } else { 0xFFFF };\n    let ss = get_seg_ss();\n    let frame_temp = read_reg32(ESP) - 4;\n\n    if nesting_level > 0 {\n        let mut tmp_ebp = read_reg32(EBP);\n        for _ in 1..nesting_level {\n            tmp_ebp -= 4;\n            push32(safe_read32s(ss + (tmp_ebp & ss_mask)).unwrap()).unwrap();\n        }\n        push32(frame_temp).unwrap();\n    }\n\n    return_on_pagefault!(safe_write32(ss + (frame_temp & ss_mask), read_reg32(EBP)));\n    write_reg32(EBP, frame_temp);\n    adjust_stack_reg(-size - 4);\n}\n\npub unsafe fn setcc_reg(condition: bool, r: i32) { write_reg8(r, condition as i32); }\npub unsafe fn setcc_mem(condition: bool, addr: i32) {\n    return_on_pagefault!(safe_write8(addr, condition as i32));\n}\n\npub unsafe fn fxsave(addr: i32) {\n    dbg_assert!(addr & 0xF == 0, \"TODO: #gp\");\n    return_on_pagefault!(writable_or_pagefault(addr, 288));\n\n    safe_write16(addr + 0, (*fpu_control_word).into()).unwrap();\n    safe_write16(addr + 2, fpu_load_status_word().into()).unwrap();\n    safe_write8(addr + 4, !*fpu_stack_empty as i32 & 0xFF).unwrap();\n    safe_write16(addr + 6, *fpu_opcode).unwrap();\n    safe_write32(addr + 8, *fpu_ip).unwrap();\n    safe_write16(addr + 12, *fpu_ip_selector).unwrap();\n    safe_write32(addr + 16, *fpu_dp).unwrap();\n    safe_write16(addr + 20, *fpu_dp_selector).unwrap();\n\n    safe_write32(addr + 24, *mxcsr).unwrap();\n    safe_write32(addr + 28, MXCSR_MASK).unwrap();\n\n    for i in 0..8 {\n        let reg_index = i + *fpu_stack_ptr as i32 & 7;\n        fpu_store_m80(addr + 32 + (i << 4), *fpu_st.offset(reg_index as isize));\n    }\n\n    // If the OSFXSR bit in control register CR4 is not set, the FXSAVE\n    // instruction may not save these registers. This behavior is\n    // implementation dependent.\n    for i in 0..8 {\n        safe_write128(addr + 160 + (i << 4), *reg_xmm.offset(i as isize)).unwrap();\n    }\n}\npub unsafe fn fxrstor(addr: i32) {\n    dbg_assert!(addr & 0xF == 0, \"TODO: #gp\");\n    return_on_pagefault!(readable_or_pagefault(addr, 288));\n\n    let new_mxcsr = safe_read32s(addr + 24).unwrap();\n\n    if 0 != new_mxcsr & !MXCSR_MASK {\n        dbg_log!(\"#gp Invalid mxcsr bits\");\n        trigger_gp(0);\n        return;\n    }\n\n    set_control_word(safe_read16(addr + 0).unwrap() as u16);\n    fpu_set_status_word(safe_read16(addr + 2).unwrap() as u16);\n    *fpu_stack_empty = !safe_read8(addr + 4).unwrap() as u8;\n    *fpu_opcode = safe_read16(addr + 6).unwrap();\n    *fpu_ip = safe_read32s(addr + 8).unwrap();\n    *fpu_ip_selector = safe_read16(addr + 12).unwrap();\n    *fpu_dp = safe_read32s(addr + 16).unwrap();\n    *fpu_dp_selector = safe_read16(addr + 20).unwrap();\n\n    set_mxcsr(new_mxcsr);\n\n    for i in 0..8 {\n        let reg_index = *fpu_stack_ptr as i32 + i & 7;\n        *fpu_st.offset(reg_index as isize) = fpu_load_m80(addr + 32 + (i << 4)).unwrap();\n    }\n\n    for i in 0..8 {\n        *reg_xmm.offset(i as isize) = safe_read128s(addr + 160 + (i << 4)).unwrap();\n    }\n}\n\npub unsafe fn xchg8(data: i32, r8: i32) -> i32 {\n    let tmp = read_reg8(r8);\n    write_reg8(r8, data);\n    return tmp;\n}\npub unsafe fn xchg16(data: i32, r16: i32) -> i32 {\n    let tmp = read_reg16(r16);\n    write_reg16(r16, data);\n    return tmp;\n}\npub unsafe fn xchg16r(r16: i32) {\n    let tmp = read_reg16(AX);\n    write_reg16(AX, read_reg16(r16));\n    write_reg16(r16, tmp);\n}\npub unsafe fn xchg32(data: i32, r32: i32) -> i32 {\n    let tmp = read_reg32(r32);\n    write_reg32(r32, data);\n    return tmp;\n}\npub unsafe fn xchg32r(r32: i32) {\n    let tmp = read_reg32(EAX);\n    write_reg32(EAX, read_reg32(r32));\n    write_reg32(r32, tmp);\n}\n\npub unsafe fn bswap(r: i32) { write_reg32(r, read_reg32(r).swap_bytes()) }\n\npub unsafe fn lar(selector: i32, original: i32) -> i32 {\n    if false {\n        dbg_log!(\"lar sel={:x}\", selector);\n    }\n\n    const LAR_INVALID_TYPE: u32 =\n        1 << 0 | 1 << 6 | 1 << 7 | 1 << 8 | 1 << 0xA | 1 << 0xD | 1 << 0xE | 1 << 0xF;\n\n    let sel = SegmentSelector::of_u16(selector as u16);\n    match lookup_segment_selector(sel) {\n        Err(()) => {\n            // pagefault\n            return original;\n        },\n        Ok(Err(_)) => {\n            *flags_changed &= !FLAG_ZERO;\n            *flags &= !FLAG_ZERO;\n            dbg_log!(\"lar: invalid selector={:x}: null or invalid\", selector);\n            return original;\n        },\n        Ok(Ok((desc, _))) => {\n            *flags_changed &= !FLAG_ZERO;\n            let dpl_bad = desc.dpl() < *cpl || desc.dpl() < sel.rpl();\n\n            if if desc.is_system() {\n                (LAR_INVALID_TYPE >> desc.system_type() & 1 == 1) || dpl_bad\n            }\n            else {\n                !desc.is_conforming_executable() && dpl_bad\n            } {\n                dbg_log!(\n                    \"lar: invalid selector={:x} is_null={} is_system={}\",\n                    selector,\n                    false,\n                    desc.is_system()\n                );\n                *flags &= !FLAG_ZERO;\n                return original;\n            }\n            else {\n                *flags |= FLAG_ZERO;\n                return (desc.raw >> 32) as i32 & 0x00FFFF00;\n            }\n        },\n    }\n}\n\npub unsafe fn lsl(selector: i32, original: i32) -> i32 {\n    if false {\n        dbg_log!(\"lsl sel={:x}\", selector);\n    }\n\n    const LSL_INVALID_TYPE: i32 = 1 << 0\n        | 1 << 4\n        | 1 << 5\n        | 1 << 6\n        | 1 << 7\n        | 1 << 8\n        | 1 << 0xA\n        | 1 << 0xC\n        | 1 << 0xD\n        | 1 << 0xE\n        | 1 << 0xF;\n\n    let sel = SegmentSelector::of_u16(selector as u16);\n    match lookup_segment_selector(sel) {\n        Err(()) => {\n            // pagefault\n            return original;\n        },\n        Ok(Err(_)) => {\n            *flags_changed &= !FLAG_ZERO;\n            *flags &= !FLAG_ZERO;\n            dbg_log!(\"lsl: invalid selector={:x}: null or invalid\", selector);\n            return original;\n        },\n        Ok(Ok((desc, _))) => {\n            *flags_changed &= !FLAG_ZERO;\n            let dpl_bad = desc.dpl() < *cpl || desc.dpl() < sel.rpl();\n\n            if if desc.is_system() {\n                (LSL_INVALID_TYPE >> desc.system_type() & 1 == 1) || dpl_bad\n            }\n            else {\n                !desc.is_conforming_executable() && dpl_bad\n            } {\n                dbg_log!(\n                    \"lsl: invalid  selector={:x} is_null={} is_system={}\",\n                    selector,\n                    false,\n                    desc.is_system(),\n                );\n                *flags &= !FLAG_ZERO;\n                return original;\n            }\n            else {\n                *flags |= FLAG_ZERO;\n                return desc.effective_limit() as i32;\n            }\n        },\n    }\n}\n\npub unsafe fn verr(selector: i32) {\n    *flags_changed &= !FLAG_ZERO;\n    let sel = SegmentSelector::of_u16(selector as u16);\n    match return_on_pagefault!(lookup_segment_selector(sel)) {\n        Err(_) => {\n            *flags &= !FLAG_ZERO;\n            dbg_log!(\"verr -> invalid. selector={:x}\", selector);\n        },\n        Ok((desc, _)) => {\n            if desc.is_system()\n                || !desc.is_readable()\n                || (!desc.is_conforming_executable()\n                    && (desc.dpl() < *cpl || desc.dpl() < sel.rpl()))\n            {\n                dbg_log!(\"verr -> invalid. selector={:x}\", selector);\n                *flags &= !FLAG_ZERO;\n            }\n            else {\n                dbg_log!(\"verr -> valid. selector={:x}\", selector);\n                *flags |= FLAG_ZERO;\n            }\n        },\n    }\n}\n\npub unsafe fn verw(selector: i32) {\n    *flags_changed &= !FLAG_ZERO;\n    let sel = SegmentSelector::of_u16(selector as u16);\n    match return_on_pagefault!(lookup_segment_selector(sel)) {\n        Err(_) => {\n            *flags &= !FLAG_ZERO;\n            dbg_log!(\"verw -> invalid. selector={:x}\", selector);\n        },\n        Ok((desc, _)) => {\n            if desc.is_system()\n                || !desc.is_writable()\n                || desc.dpl() < *cpl\n                || desc.dpl() < sel.rpl()\n            {\n                dbg_log!(\n                    \"verw invalid selector={:x} is_system={} is_writable={}\",\n                    selector,\n                    desc.is_system(),\n                    desc.is_writable(),\n                );\n                *flags &= !FLAG_ZERO;\n            }\n            else {\n                *flags |= FLAG_ZERO;\n            }\n        },\n    }\n}\n"
  },
  {
    "path": "src/rust/cpu/mod.rs",
    "content": "pub mod apic;\npub mod arith;\npub mod call_indirect;\npub mod cpu;\npub mod fpu;\npub mod global_pointers;\npub mod instructions;\npub mod instructions_0f;\npub mod ioapic;\npub mod memory;\npub mod misc_instr;\npub mod modrm;\npub mod pic;\npub mod sse_instr;\npub mod string;\npub mod vga;\n"
  },
  {
    "path": "src/rust/cpu/modrm.rs",
    "content": "use crate::cpu::cpu::*;\nuse crate::paging::OrPageFault;\n\npub unsafe fn resolve_modrm16(modrm_byte: i32) -> OrPageFault<i32> {\n    match modrm_byte & !0o070 {\n        0o000 => get_seg_prefix_ds(read_reg16(BX) + read_reg16(SI) & 0xFFFF),\n        0o100 => get_seg_prefix_ds(read_reg16(BX) + read_reg16(SI) + read_imm8s()? & 0xFFFF),\n        0o200 => get_seg_prefix_ds(read_reg16(BX) + read_reg16(SI) + read_imm16()? & 0xFFFF),\n        0o001 => get_seg_prefix_ds(read_reg16(BX) + read_reg16(DI) & 0xFFFF),\n        0o101 => get_seg_prefix_ds(read_reg16(BX) + read_reg16(DI) + read_imm8s()? & 0xFFFF),\n        0o201 => get_seg_prefix_ds(read_reg16(BX) + read_reg16(DI) + read_imm16()? & 0xFFFF),\n        0o002 => get_seg_prefix_ss(read_reg16(BP) + read_reg16(SI) & 0xFFFF),\n        0o102 => get_seg_prefix_ss(read_reg16(BP) + read_reg16(SI) + read_imm8s()? & 0xFFFF),\n        0o202 => get_seg_prefix_ss(read_reg16(BP) + read_reg16(SI) + read_imm16()? & 0xFFFF),\n        0o003 => get_seg_prefix_ss(read_reg16(BP) + read_reg16(DI) & 0xFFFF),\n        0o103 => get_seg_prefix_ss(read_reg16(BP) + read_reg16(DI) + read_imm8s()? & 0xFFFF),\n        0o203 => get_seg_prefix_ss(read_reg16(BP) + read_reg16(DI) + read_imm16()? & 0xFFFF),\n        0o004 => get_seg_prefix_ds(read_reg16(SI) & 0xFFFF),\n        0o104 => get_seg_prefix_ds(read_reg16(SI) + read_imm8s()? & 0xFFFF),\n        0o204 => get_seg_prefix_ds(read_reg16(SI) + read_imm16()? & 0xFFFF),\n        0o005 => get_seg_prefix_ds(read_reg16(DI) & 0xFFFF),\n        0o105 => get_seg_prefix_ds(read_reg16(DI) + read_imm8s()? & 0xFFFF),\n        0o205 => get_seg_prefix_ds(read_reg16(DI) + read_imm16()? & 0xFFFF),\n        0o006 => get_seg_prefix_ds(read_imm16()?),\n        0o106 => get_seg_prefix_ss(read_reg16(BP) + read_imm8s()? & 0xFFFF),\n        0o206 => get_seg_prefix_ss(read_reg16(BP) + read_imm16()? & 0xFFFF),\n        0o007 => get_seg_prefix_ds(read_reg16(BX) & 0xFFFF),\n        0o107 => get_seg_prefix_ds(read_reg16(BX) + read_imm8s()? & 0xFFFF),\n        0o207 => get_seg_prefix_ds(read_reg16(BX) + read_imm16()? & 0xFFFF),\n        _ => {\n            dbg_assert!(false);\n            std::hint::unreachable_unchecked()\n        },\n    }\n}\n\npub unsafe fn resolve_modrm32_(modrm_byte: i32) -> OrPageFault<i32> {\n    let r = (modrm_byte & 7) as u8;\n    dbg_assert!(modrm_byte < 192);\n    Ok(if r as i32 == 4 {\n        if modrm_byte < 64 {\n            resolve_sib(false)?\n        }\n        else {\n            resolve_sib(true)? + if modrm_byte < 128 { read_imm8s()? } else { read_imm32s()? }\n        }\n    }\n    else if r as i32 == 5 {\n        if modrm_byte < 64 {\n            get_seg_prefix_ds(read_imm32s()?)?\n        }\n        else {\n            get_seg_prefix_ss(\n                read_reg32(EBP) + if modrm_byte < 128 { read_imm8s()? } else { read_imm32s()? },\n            )?\n        }\n    }\n    else if modrm_byte < 64 {\n        get_seg_prefix_ds(read_reg32(r as i32))?\n    }\n    else {\n        get_seg_prefix_ds(\n            read_reg32(r as i32) + if modrm_byte < 128 { read_imm8s()? } else { read_imm32s()? },\n        )?\n    })\n}\nunsafe fn resolve_sib(with_imm: bool) -> OrPageFault<i32> {\n    let sib_byte = read_imm8()?;\n    let r = sib_byte & 7;\n    let m = sib_byte >> 3 & 7;\n    let base;\n    let seg;\n    if r == 4 {\n        base = read_reg32(ESP);\n        seg = SS\n    }\n    else if r == 5 {\n        if with_imm {\n            base = read_reg32(EBP);\n            seg = SS\n        }\n        else {\n            base = read_imm32s()?;\n            seg = DS\n        }\n    }\n    else {\n        base = read_reg32(r);\n        seg = DS\n    }\n    let offset;\n    if m == 4 {\n        offset = 0\n    }\n    else {\n        let s = sib_byte >> 6 & 3;\n        offset = read_reg32(m) << s\n    }\n    Ok(get_seg_prefix(seg)? + base + offset)\n}\n\npub unsafe fn resolve_modrm32(modrm_byte: i32) -> OrPageFault<i32> {\n    match modrm_byte & !0o070 {\n        0o000 => get_seg_prefix_ds(read_reg32(EAX)),\n        0o100 => get_seg_prefix_ds(read_reg32(EAX) + read_imm8s()?),\n        0o200 => get_seg_prefix_ds(read_reg32(EAX) + read_imm32s()?),\n        0o001 => get_seg_prefix_ds(read_reg32(ECX)),\n        0o101 => get_seg_prefix_ds(read_reg32(ECX) + read_imm8s()?),\n        0o201 => get_seg_prefix_ds(read_reg32(ECX) + read_imm32s()?),\n        0o002 => get_seg_prefix_ds(read_reg32(EDX)),\n        0o102 => get_seg_prefix_ds(read_reg32(EDX) + read_imm8s()?),\n        0o202 => get_seg_prefix_ds(read_reg32(EDX) + read_imm32s()?),\n        0o003 => get_seg_prefix_ds(read_reg32(EBX)),\n        0o103 => get_seg_prefix_ds(read_reg32(EBX) + read_imm8s()?),\n        0o203 => get_seg_prefix_ds(read_reg32(EBX) + read_imm32s()?),\n        0o004 => resolve_sib(false),\n        0o104 => Ok(resolve_sib(true)? + read_imm8s()?),\n        0o204 => Ok(resolve_sib(true)? + read_imm32s()?),\n        0o005 => get_seg_prefix_ds(read_imm32s()?),\n        0o105 => get_seg_prefix_ss(read_reg32(EBP) + read_imm8s()?),\n        0o205 => get_seg_prefix_ss(read_reg32(EBP) + read_imm32s()?),\n        0o006 => get_seg_prefix_ds(read_reg32(ESI)),\n        0o106 => get_seg_prefix_ds(read_reg32(ESI) + read_imm8s()?),\n        0o206 => get_seg_prefix_ds(read_reg32(ESI) + read_imm32s()?),\n        0o007 => get_seg_prefix_ds(read_reg32(EDI)),\n        0o107 => get_seg_prefix_ds(read_reg32(EDI) + read_imm8s()?),\n        0o207 => get_seg_prefix_ds(read_reg32(EDI) + read_imm32s()?),\n        _ => {\n            dbg_assert!(false);\n            std::hint::unreachable_unchecked()\n        },\n    }\n}\n"
  },
  {
    "path": "src/rust/cpu/pic.rs",
    "content": "#![allow(non_snake_case)]\n\n// Programmable Interrupt Controller\n// http://stanislavs.org/helppc/8259.html\n\nuse std::sync::{Mutex, MutexGuard};\n\npub const PIC_LOG: bool = false;\npub const PIC_LOG_VERBOSE: bool = false;\n\n// Note: This layout is deliberately chosen to match the old JavaScript pic state\n// (cpu.get_state_pic depens on this layout)\nconst _: () = assert!(std::mem::offset_of!(Pic0, special_mask_mode) == 12);\n#[repr(C)]\nstruct Pic0 {\n    irq_mask: u8,\n\n    irq_map: u8,\n\n    // in-service register\n    // Holds interrupts that are currently being serviced\n    isr: u8,\n\n    // interrupt request register\n    // Holds interrupts that have been requested\n    irr: u8,\n\n    master: bool,\n    dummy: u8, // remove when state image is updated\n\n    expect_icw4: bool,\n    state: u8,\n    read_isr: bool,\n    auto_eoi: bool,\n\n    elcr: u8,\n\n    irq_value: u8,\n    special_mask_mode: bool,\n}\n\nstruct Pic {\n    master: Pic0,\n    slave: Pic0,\n}\n\nstatic PIC: Mutex<Pic> = Mutex::new(Pic {\n    master: Pic0 {\n        // all irqs off\n        irq_mask: 0,\n        // Bogus default value (both master and slave mapped to 0).\n        // Will be initialized by the BIOS\n        irq_map: 0,\n        // in-service register\n        // Holds interrupts that are currently being serviced\n        isr: 0,\n        // interrupt request register\n        // Holds interrupts that have been requested\n        irr: 0,\n        irq_value: 0,\n        expect_icw4: false,\n        state: 0,\n        read_isr: false,\n        auto_eoi: false,\n        special_mask_mode: false,\n        elcr: 0,\n        master: true,\n        dummy: 0,\n    },\n    slave: Pic0 {\n        // all irqs off\n        irq_mask: 0,\n        // Bogus default value (both master and slave mapped to 0).\n        // Will be initialized by the BIOS\n        irq_map: 0,\n        // in-service register\n        // Holds interrupts that are currently being serviced\n        isr: 0,\n        // interrupt request register\n        // Holds interrupts that have been requested\n        irr: 0,\n        irq_value: 0,\n        expect_icw4: false,\n        state: 0,\n        read_isr: false,\n        auto_eoi: false,\n        special_mask_mode: false,\n        elcr: 0,\n        master: false,\n        dummy: 0,\n    },\n});\n\nfn get_pic() -> MutexGuard<'static, Pic> { PIC.try_lock().unwrap() }\n\n// called from javascript for saving/restoring state\n#[no_mangle]\npub fn get_pic_addr_master() -> u32 { &raw mut get_pic().master as u32 }\n#[no_mangle]\npub fn get_pic_addr_slave() -> u32 { &raw mut get_pic().slave as u32 }\n\nimpl Pic0 {\n    fn get_irq(&mut self) -> Option<u8> {\n        let enabled_irr = self.irr & self.irq_mask;\n\n        if enabled_irr == 0 {\n            if PIC_LOG_VERBOSE {\n                dbg_log!(\n                    \"[PIC] no unmasked irrs. irr={:x} mask={:x} isr={:x}\",\n                    self.irr,\n                    self.irq_mask,\n                    self.isr\n                );\n            }\n            return None;\n        }\n\n        let irq_mask = enabled_irr & (!enabled_irr + 1);\n        let special_mask = if self.special_mask_mode { self.irq_mask } else { 0xFF };\n\n        if self.isr != 0 && (self.isr & (!self.isr + 1) & special_mask) <= irq_mask {\n            // wait for eoi of higher or same priority interrupt\n            if PIC_LOG {\n                dbg_log!(\n                    \"[PIC] higher prio: master={} isr={:x} mask={:x} irq={:x}\",\n                    self.master,\n                    self.isr,\n                    self.irq_mask,\n                    irq_mask\n                );\n            }\n            return None;\n        }\n\n        dbg_assert!(irq_mask != 0);\n        let irq_number = irq_mask.ilog2() as u8;\n        dbg_assert!(irq_mask == 1 << irq_number);\n\n        if PIC_LOG_VERBOSE {\n            dbg_log!(\"[PIC] request irq {}\", irq_number);\n        }\n\n        Some(irq_number)\n    }\n\n    fn port0_read(self: &Pic0) -> u32 { (if self.read_isr { self.isr } else { self.irr }) as u32 }\n    fn port1_read(self: &Pic0) -> u32 { !self.irq_mask as u32 }\n}\n\nimpl Pic {\n    fn set_irq(self: &mut Pic, i: u8) {\n        let mask = 1 << (i & 7);\n        let dev = if i < 8 { &mut self.master } else { &mut self.slave };\n        if dev.irq_value & mask == 0 || dev.elcr & mask != 0 {\n            dev.irr |= mask;\n            dev.irq_value |= mask;\n            if i >= 8 {\n                self.check_irqs_slave()\n            }\n        }\n    }\n\n    fn clear_irq(self: &mut Pic, i: u8) {\n        let mask = 1 << (i & 7);\n        let dev = if i < 8 { &mut self.master } else { &mut self.slave };\n        dev.irq_value &= !mask;\n        if dev.elcr & mask != 0 {\n            dev.irr &= !mask;\n            if i >= 8 {\n                self.check_irqs_slave()\n            }\n        }\n    }\n\n    fn port0_write(&mut self, index: u8, v: u8) {\n        let dev = if index == 0 { &mut self.master } else { &mut self.slave };\n        if v & 0x10 != 0 {\n            // xxxx1xxx\n            // icw1\n            dbg_log!(\"icw1 = {:x}\", v);\n            dev.isr = 0;\n            dev.irr = 0;\n            dev.irq_mask = 0xff;\n            dev.irq_value = 0;\n            dev.auto_eoi = true;\n\n            dev.expect_icw4 = v & 1 != 0;\n            dbg_assert!(v & 2 == 0, \"unimplemented: single mode\");\n            dbg_assert!(v & 8 == 0, \"unimplemented: level mode\");\n            dev.state = 1;\n        }\n        else if v & 8 != 0 {\n            // xxx01xxx\n            // ocw3\n            dbg_log!(\"ocw3: {:x}\", v);\n            if v & 2 != 0 {\n                dev.read_isr = v & 1 != 0;\n            }\n            if v & 4 != 0 {\n                dbg_assert!(false, \"unimplemented: polling\");\n            }\n            if v & 0x40 != 0 {\n                dev.special_mask_mode = (v & 0x20) == 0x20;\n                dbg_log!(\"special mask mode: {}\", dev.special_mask_mode);\n            }\n        }\n        else {\n            // xxx00xxx\n            // ocw2\n            // end of interrupt\n            if PIC_LOG {\n                dbg_log!(\"eoi: {:x}\", v);\n            }\n\n            let eoi_type = v >> 5;\n\n            if eoi_type == 1 {\n                // non-specific eoi\n                dev.isr &= dev.isr - 1;\n                if PIC_LOG {\n                    dbg_log!(\"new isr: {:x}\", dev.isr);\n                }\n            }\n            else if eoi_type == 3 {\n                // specific eoi\n                dev.isr &= !(1 << (v & 7));\n            }\n            else if eoi_type == 6 {\n                // os2 v4, freebsd\n                let priority = v & 7;\n                dbg_log!(\"lowest priority: {:x}\", priority);\n            }\n            else {\n                dbg_log!(\"Unknown eoi: {:x} type={:x}\", v, eoi_type);\n                dbg_assert!(false);\n                dev.isr &= dev.isr - 1;\n            }\n\n            if index == 1 {\n                self.check_irqs_slave()\n            }\n        }\n    }\n\n    fn port1_write(&mut self, index: u8, v: u8) {\n        let dev = if index == 0 { &mut self.master } else { &mut self.slave };\n        if dev.state == 0 {\n            if dev.expect_icw4 {\n                // icw4\n                dev.expect_icw4 = false;\n                dev.auto_eoi = v & 2 != 0;\n                dbg_log!(\"icw4: {:x} autoeoi={}\", v, dev.auto_eoi);\n                dbg_assert!(v & 0x10 == 0, \"unimplemented: nested mode\");\n                dbg_assert!(v & 1 == 1, \"unimplemented: 8086/88 mode\");\n            }\n            else {\n                // ocw1\n                dev.irq_mask = !v;\n\n                if PIC_LOG_VERBOSE {\n                    dbg_log!(\"interrupt mask: {:x}\", dev.irq_mask);\n                }\n\n                if index == 1 {\n                    self.check_irqs_slave()\n                }\n            }\n        }\n        else if dev.state == 1 {\n            // icw2\n            dev.irq_map = v;\n            dbg_log!(\"interrupts are mapped to {:x}\", dev.irq_map);\n            dev.state += 1;\n        }\n        else if dev.state == 2 {\n            // icw3\n            dev.state = 0;\n            dbg_log!(\"icw3: {:x}\", v);\n        }\n    }\n\n    fn check_irqs_slave(&mut self) {\n        let is_set = self.slave.get_irq().is_some();\n        if is_set {\n            self.set_irq(2)\n        }\n        else {\n            self.clear_irq(2)\n        }\n    }\n}\n\n// called by the cpu\npub fn pic_acknowledge_irq() -> Option<u8> {\n    let mut pic = get_pic();\n    let irq = match pic.master.get_irq() {\n        Some(i) => i,\n        None => return None,\n    };\n\n    if pic.master.irr == 0 {\n        dbg_assert!(false);\n        //PIC_LOG_VERBOSE && dbg_log!(\"master> spurious requested=\" + irq);\n        //Some(pic.irq_map | 7)\n        return None;\n    }\n\n    let mask = 1 << irq;\n\n    if pic.master.elcr & mask == 0 {\n        // not in level mode\n        pic.master.irr &= !mask;\n    }\n\n    if !pic.master.auto_eoi {\n        pic.master.isr |= mask;\n    }\n\n    if PIC_LOG_VERBOSE {\n        dbg_log!(\"[PIC] master> acknowledge {}\", irq);\n    }\n\n    dbg_assert!(pic.master.get_irq().is_none());\n\n    if irq == 2 {\n        acknowledge_irq_slave(&mut pic)\n    }\n    else {\n        Some(pic.master.irq_map | irq)\n    }\n}\n\nfn acknowledge_irq_slave(pic: &mut Pic) -> Option<u8> {\n    let irq = match pic.slave.get_irq() {\n        Some(i) => i,\n        None => return None,\n    };\n\n    if pic.slave.irr == 0 {\n        //PIC_LOG_VERBOSE && dbg_log!(\"slave> spurious requested=\" + irq);\n        //Some(pic.irq_map | 7)\n        dbg_assert!(false);\n        return None;\n    }\n\n    let mask = 1 << irq;\n\n    if pic.slave.elcr & mask == 0 {\n        // not in level mode\n        pic.slave.irr &= !mask;\n    }\n\n    if !pic.slave.auto_eoi {\n        pic.slave.isr |= mask;\n    }\n\n    if PIC_LOG_VERBOSE {\n        dbg_log!(\"[PIC] slave> acknowledge {}\", irq);\n    }\n\n    dbg_assert!(pic.slave.get_irq().is_none());\n    pic.clear_irq(2);\n\n    Some(pic.slave.irq_map | irq)\n}\n\npub fn set_irq(i: u8) {\n    dbg_assert!(i < 16);\n\n    if PIC_LOG_VERBOSE {\n        dbg_log!(\"[PIC] set irq {}\", i);\n    }\n\n    get_pic().set_irq(i)\n}\n\npub fn clear_irq(i: u8) {\n    dbg_assert!(i < 16);\n\n    if PIC_LOG_VERBOSE {\n        dbg_log!(\"[PIC] clear irq {}\", i);\n    }\n\n    get_pic().clear_irq(i)\n}\n\npub fn port20_read() -> u32 { get_pic().master.port0_read() }\npub fn port21_read() -> u32 { get_pic().master.port1_read() }\n\npub fn portA0_read() -> u32 { get_pic().slave.port0_read() }\npub fn portA1_read() -> u32 { get_pic().slave.port1_read() }\n\npub fn port20_write(v: u8) { get_pic().port0_write(0, v) }\npub fn port21_write(v: u8) { get_pic().port1_write(0, v) }\n\npub fn portA0_write(v: u8) { get_pic().port0_write(1, v) }\npub fn portA1_write(v: u8) { get_pic().port1_write(1, v) }\n\npub fn port4D0_read() -> u32 { get_pic().master.elcr as u32 }\npub fn port4D1_read() -> u32 { get_pic().slave.elcr as u32 }\npub fn port4D0_write(v: u8) { get_pic().master.elcr = v }\npub fn port4D1_write(v: u8) { get_pic().slave.elcr = v }\n"
  },
  {
    "path": "src/rust/cpu/sse_instr.rs",
    "content": "use crate::cpu::cpu::*;\nuse crate::cpu::global_pointers::mxcsr;\n\npub unsafe fn mov_r_m64(addr: i32, r: i32) {\n    // mov* m64, mm\n    let data = read_mmx64s(r);\n    return_on_pagefault!(safe_write64(addr, data));\n    transition_fpu_to_mmx();\n}\npub unsafe fn movl_r128_m64(addr: i32, r: i32) {\n    // mov* m64, xmm\n    let data = read_xmm64s(r);\n    return_on_pagefault!(safe_write64(addr, data));\n}\npub unsafe fn mov_r_r128(r1: i32, r2: i32) {\n    // mov* xmm, xmm\n    let data = read_xmm128s(r2);\n    write_xmm_reg128(r1, data);\n}\npub unsafe fn mov_r_m128(addr: i32, r: i32) {\n    // mov* m128, xmm\n    let data = read_xmm128s(r);\n    return_on_pagefault!(safe_write128(addr, data));\n}\npub unsafe fn mov_rm_r128(source: reg128, r: i32) {\n    // mov* xmm, xmm/m128\n    write_xmm_reg128(r, source);\n}\npub unsafe fn movh_r128_m64(addr: i32, r: i32) {\n    // movhp* m64, xmm\n    let data = read_xmm128s(r);\n    return_on_pagefault!(safe_write64(addr, data.u64[1]));\n}\n\npub unsafe fn pand_r128(source: reg128, r: i32) {\n    // pand xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    result.u64[0] = source.u64[0] & destination.u64[0];\n    result.u64[1] = source.u64[1] & destination.u64[1];\n    write_xmm_reg128(r, result);\n}\npub unsafe fn pandn_r128(source: reg128, r: i32) {\n    // pandn xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    result.u64[0] = source.u64[0] & !destination.u64[0];\n    result.u64[1] = source.u64[1] & !destination.u64[1];\n    write_xmm_reg128(r, result);\n}\npub unsafe fn pxor_r128(source: reg128, r: i32) {\n    // pxor xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    result.u64[0] = source.u64[0] ^ destination.u64[0];\n    result.u64[1] = source.u64[1] ^ destination.u64[1];\n    write_xmm_reg128(r, result);\n}\npub unsafe fn por_r128(source: reg128, r: i32) {\n    // por xmm, xmm/m128\n    // XXX: Aligned access or #gp\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    result.u64[0] = source.u64[0] | destination.u64[0];\n    result.u64[1] = source.u64[1] | destination.u64[1];\n    write_xmm_reg128(r, result);\n}\n\npub unsafe fn psrlw_r64(r: i32, shift: u64) {\n    // psrlw mm, {shift}\n    let destination: [u16; 4] = std::mem::transmute(read_mmx64s(r));\n    let shift = if shift > 15 { 16 } else { shift };\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = ((destination[i] as u32) >> shift) as u16\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn psraw_r64(r: i32, shift: u64) {\n    // psraw mm, {shift}\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let shift = if shift > 15 { 16 } else { shift };\n    let mut result = [0; 4];\n    for i in 0..4 {\n        result[i] = (destination[i] as i32 >> shift) as i16\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn psllw_r64(r: i32, shift: u64) {\n    // psllw mm, {shift}\n    let destination: [i16; 4] = std::mem::transmute(read_mmx64s(r));\n    let mut result = [0; 4];\n    if shift <= 15 {\n        for i in 0..4 {\n            result[i] = destination[i] << shift\n        }\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn psrld_r64(r: i32, shift: u64) {\n    // psrld mm, {shift}\n    let destination: [u32; 2] = std::mem::transmute(read_mmx64s(r));\n    let mut result = [0; 2];\n    if shift <= 31 {\n        for i in 0..2 {\n            result[i] = destination[i] >> shift;\n        }\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn psrad_r64(r: i32, shift: u64) {\n    // psrad mm, {shift}\n    let destination: [i32; 2] = std::mem::transmute(read_mmx64s(r));\n    let shift = if shift > 31 { 31 } else { shift };\n    let mut result = [0; 2];\n    for i in 0..2 {\n        result[i] = destination[i] >> shift;\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn pslld_r64(r: i32, shift: u64) {\n    // pslld mm, {shift}\n    let destination: [i32; 2] = std::mem::transmute(read_mmx64s(r));\n    let mut result = [0; 2];\n    if shift <= 31 {\n        for i in 0..2 {\n            result[i] = destination[i] << shift;\n        }\n    }\n    write_mmx_reg64(r, std::mem::transmute(result));\n    transition_fpu_to_mmx();\n}\npub unsafe fn psrlq_r64(r: i32, shift: u64) {\n    // psrlq mm, {shift}\n    let destination = read_mmx64s(r);\n    let mut result = 0;\n    if shift <= 63 {\n        result = destination >> shift\n    }\n    write_mmx_reg64(r, result);\n    transition_fpu_to_mmx();\n}\npub unsafe fn psllq_r64(r: i32, shift: u64) {\n    // psllq mm, {shift}\n    let destination = read_mmx64s(r);\n    let mut result = 0;\n    if shift <= 63 {\n        result = destination << shift\n    }\n    write_mmx_reg64(r, result);\n    transition_fpu_to_mmx();\n}\npub unsafe fn psrlw_r128(r: i32, shift: u64) {\n    // psrlw xmm, {shift}\n    let destination = read_xmm128s(r);\n    let mut dword0 = 0;\n    let mut dword1 = 0;\n    let mut dword2 = 0;\n    let mut dword3 = 0;\n    if shift <= 15 {\n        dword0 = destination.u16[0] as i32 >> shift | destination.u16[1] as i32 >> shift << 16;\n        dword1 = destination.u16[2] as i32 >> shift | destination.u16[3] as i32 >> shift << 16;\n        dword2 = destination.u16[4] as i32 >> shift | destination.u16[5] as i32 >> shift << 16;\n        dword3 = destination.u16[6] as i32 >> shift | destination.u16[7] as i32 >> shift << 16\n    }\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn psraw_r128(r: i32, shift: u64) {\n    // psraw xmm, {shift}\n    let destination = read_xmm128s(r);\n    let shift_clamped = (if shift > 15 { 16 } else { shift as u32 }) as i32;\n    let dword0 = destination.i16[0] as i32 >> shift_clamped & 0xFFFF\n        | destination.i16[1] as i32 >> shift_clamped << 16;\n    let dword1 = destination.i16[2] as i32 >> shift_clamped & 0xFFFF\n        | destination.i16[3] as i32 >> shift_clamped << 16;\n    let dword2 = destination.i16[4] as i32 >> shift_clamped & 0xFFFF\n        | destination.i16[5] as i32 >> shift_clamped << 16;\n    let dword3 = destination.i16[6] as i32 >> shift_clamped & 0xFFFF\n        | destination.i16[7] as i32 >> shift_clamped << 16;\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn psllw_r128(r: i32, shift: u64) {\n    // psllw xmm, {shift}\n    let destination = read_xmm128s(r);\n    let mut dword0 = 0;\n    let mut dword1 = 0;\n    let mut dword2 = 0;\n    let mut dword3 = 0;\n    if shift <= 15 {\n        dword0 = (destination.u16[0] as i32) << shift & 0xFFFF\n            | (destination.u16[1] as i32) << shift << 16;\n        dword1 = (destination.u16[2] as i32) << shift & 0xFFFF\n            | (destination.u16[3] as i32) << shift << 16;\n        dword2 = (destination.u16[4] as i32) << shift & 0xFFFF\n            | (destination.u16[5] as i32) << shift << 16;\n        dword3 = (destination.u16[6] as i32) << shift & 0xFFFF\n            | (destination.u16[7] as i32) << shift << 16\n    }\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn psrld_r128(r: i32, shift: u64) {\n    // psrld xmm, {shift}\n    let destination = read_xmm128s(r);\n    let mut dword0 = 0;\n    let mut dword1 = 0;\n    let mut dword2 = 0;\n    let mut dword3 = 0;\n    if shift <= 31 {\n        dword0 = (destination.u32[0] >> shift) as i32;\n        dword1 = (destination.u32[1] >> shift) as i32;\n        dword2 = (destination.u32[2] >> shift) as i32;\n        dword3 = (destination.u32[3] >> shift) as i32\n    }\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn psrad_r128(r: i32, shift: u64) {\n    // psrad xmm, {shift}\n    let destination = read_xmm128s(r);\n    let shift_clamped = (if shift > 31 { 31 } else { shift }) as i32;\n    let dword0 = destination.i32[0] >> shift_clamped;\n    let dword1 = destination.i32[1] >> shift_clamped;\n    let dword2 = destination.i32[2] >> shift_clamped;\n    let dword3 = destination.i32[3] >> shift_clamped;\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn pslld_r128(r: i32, shift: u64) {\n    // pslld xmm, {shift}\n    let destination = read_xmm128s(r);\n    let mut dword0 = 0;\n    let mut dword1 = 0;\n    let mut dword2 = 0;\n    let mut dword3 = 0;\n    if shift <= 31 {\n        dword0 = destination.i32[0] << shift;\n        dword1 = destination.i32[1] << shift;\n        dword2 = destination.i32[2] << shift;\n        dword3 = destination.i32[3] << shift\n    }\n    write_xmm128(r, dword0, dword1, dword2, dword3);\n}\npub unsafe fn psrlq_r128(r: i32, shift: u64) {\n    // psrlq xmm, {shift}\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    if shift <= 63 {\n        result.u64[0] = destination.u64[0] >> shift;\n        result.u64[1] = destination.u64[1] >> shift\n    }\n    write_xmm_reg128(r, result);\n}\npub unsafe fn psllq_r128(r: i32, shift: u64) {\n    // psllq xmm, {shift}\n    let destination = read_xmm128s(r);\n    let mut result = reg128 { i8: [0; 16] };\n    if shift <= 63 {\n        result.u64[0] = destination.u64[0] << shift;\n        result.u64[1] = destination.u64[1] << shift\n    }\n    write_xmm_reg128(r, result);\n}\n\npub unsafe fn sse_comparison(op: i32, x: f64, y: f64) -> bool {\n    // TODO: Signaling\n    match op & 7 {\n        0 => return x == y,\n        1 => return x < y,\n        2 => return x <= y,\n        3 => return x.is_nan() || y.is_nan(),\n        4 => return x != y || x.is_nan() || y.is_nan(),\n        5 => return x >= y || x.is_nan() || y.is_nan(),\n        6 => return x > y || x.is_nan() || y.is_nan(),\n        7 => return !x.is_nan() && !y.is_nan(),\n        _ => {\n            dbg_assert!(false);\n            return false;\n        },\n    };\n}\npub unsafe fn sse_min(x: f64, y: f64) -> f64 {\n    // if both x and y are 0 or x is nan, y is returned\n    return if x < y { x } else { y };\n}\npub unsafe fn sse_max(x: f64, y: f64) -> f64 {\n    // if both x and y are 0 or x is nan, y is returned\n    return if x > y { x } else { y };\n}\n\n#[no_mangle]\npub unsafe fn sse_convert_with_truncation_f32_to_i32(x: f32) -> i32 {\n    let x = x.trunc();\n    if x >= -2147483648.0 && x < 2147483648.0 {\n        return x as i64 as i32;\n    }\n    else {\n        // TODO: Signal\n        return -0x80000000;\n    };\n}\n#[no_mangle]\npub unsafe fn sse_convert_f32_to_i32(x: f32) -> i32 {\n    let x = sse_integer_round(x as f64);\n    if x >= -2147483648.0 && x < 2147483648.0 {\n        return x as i64 as i32;\n    }\n    else {\n        // TODO: Signal\n        return -0x80000000;\n    };\n}\n\n#[no_mangle]\npub unsafe fn sse_convert_with_truncation_f64_to_i32(x: f64) -> i32 {\n    let x = x.trunc();\n    if x >= -2147483648.0 && x < 2147483648.0 {\n        return x as i64 as i32;\n    }\n    else {\n        // TODO: Signal\n        return -0x80000000;\n    };\n}\n#[no_mangle]\npub unsafe fn sse_convert_f64_to_i32(x: f64) -> i32 {\n    let x = sse_integer_round(x);\n    if x >= -2147483648.0 && x < 2147483648.0 {\n        return x as i64 as i32;\n    }\n    else {\n        // TODO: Signal\n        return -0x80000000;\n    };\n}\n\npub unsafe fn sse_integer_round(f: f64) -> f64 {\n    // see fpu_integer_round\n    let rc = *mxcsr >> MXCSR_RC_SHIFT & 3;\n    if rc == 0 {\n        // Round to nearest, or even if equidistant\n        let mut rounded = f.round();\n        let diff = rounded - f;\n        if diff == 0.5 || diff == -0.5 {\n            rounded = 2.0 * (f * 0.5).round()\n        }\n        return rounded;\n    }\n    else if rc == 1 || rc == 3 && f > 0.0 {\n        // rc=3 is truncate -> floor for positive numbers\n        return f.floor();\n    }\n    else {\n        return f.ceil();\n    };\n}\n"
  },
  {
    "path": "src/rust/cpu/string.rs",
    "content": "// string operations\n//\n//       cmp  si  di\n// movs   0    1   1/w    A4\n// cmps   1    1   1/r    A6\n// stos   0    0   1/w    AA\n// lods   0    1   0      AC\n// scas   1    0   1/r    AE\n// ins    0    0   1/w\n// outs   0    1   0\n\nuse crate::cpu::arith::{cmp16, cmp32, cmp8};\nuse crate::cpu::cpu::{\n    get_seg, io_port_read16, io_port_read32, io_port_read8, io_port_write16, io_port_write32,\n    io_port_write8, read_reg16, read_reg32, safe_read16, safe_read32s, safe_read8, safe_write16,\n    safe_write32, safe_write8, set_reg_asize, test_privileges_for_io, translate_address_read,\n    translate_address_write_and_can_skip_dirty, writable_or_pagefault, write_reg16, write_reg32,\n    write_reg8, AL, AX, DX, EAX, ECX, EDI, ES, ESI, FLAG_DIRECTION,\n};\nuse crate::cpu::global_pointers::{flags, instruction_pointer, previous_ip};\nuse crate::cpu::memory;\nuse crate::jit;\nuse crate::page::Page;\n\nfn count_until_end_of_page(direction: i32, size: i32, addr: u32) -> u32 {\n    (if direction == 1 {\n        (0x1000 - (addr & 0xFFF)) / size as u32\n    }\n    else {\n        (addr & 0xFFF) / size as u32 + 1\n    }) as u32\n}\n\n#[derive(Copy, Clone, PartialEq)]\nenum Instruction {\n    Movs,\n    Lods,\n    Stos,\n    Scas,\n    Cmps,\n    Ins,\n    Outs,\n}\n#[derive(PartialEq)]\nenum Size {\n    B,\n    W,\n    D,\n}\n#[derive(Copy, Clone)]\nenum Rep {\n    None,\n    Z,\n    NZ,\n}\n\n// We implement all string instructions here and rely on the inliner on doing its job of optimising\n// away anything known at compile time (check with `wasm-dis build/v86.wasm`)\n#[inline(always)]\nunsafe fn string_instruction(\n    is_asize_32: bool,\n    ds_or_prefix: i32,\n    instruction: Instruction,\n    size: Size,\n    rep: Rep,\n) {\n    let asize_mask = if is_asize_32 { -1 } else { 0xFFFF };\n\n    let direction = if 0 != *flags & FLAG_DIRECTION { -1 } else { 1 };\n\n    let mut count = match rep {\n        Rep::Z | Rep::NZ => {\n            let c = (read_reg32(ECX) & asize_mask) as u32;\n            if c == 0 {\n                return;\n            };\n            c\n        },\n        Rep::None => 0,\n    };\n\n    let es = match instruction {\n        Instruction::Movs\n        | Instruction::Cmps\n        | Instruction::Stos\n        | Instruction::Scas\n        | Instruction::Ins => return_on_pagefault!(get_seg(ES)),\n        _ => 0,\n    };\n    let ds = match instruction {\n        Instruction::Movs\n        | Instruction::Cmps\n        | Instruction::Lods\n        | Instruction::Scas\n        | Instruction::Outs => return_on_pagefault!(get_seg(ds_or_prefix)),\n        _ => 0,\n    };\n\n    let size_bytes = match size {\n        Size::B => 1,\n        Size::W => 2,\n        Size::D => 4,\n    };\n    let size_mask = match size {\n        Size::B => 0xFF,\n        Size::W => 0xFFFF,\n        Size::D => -1,\n    };\n\n    let increment = direction * size_bytes;\n\n    let data = match instruction {\n        Instruction::Stos | Instruction::Scas => read_reg32(EAX),\n        _ => 0,\n    };\n\n    let mut src = match instruction {\n        Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n            read_reg32(ESI) & asize_mask\n        },\n        _ => 0,\n    };\n    let mut dst = match instruction {\n        Instruction::Movs\n        | Instruction::Cmps\n        | Instruction::Stos\n        | Instruction::Scas\n        | Instruction::Ins => read_reg32(EDI) & asize_mask,\n        _ => 0,\n    };\n\n    let port = match instruction {\n        Instruction::Ins | Instruction::Outs => {\n            let port = read_reg16(DX);\n            if !test_privileges_for_io(port, size_bytes) {\n                return;\n            }\n            port\n        },\n        _ => 0,\n    };\n\n    let is_aligned = (ds + src) & (size_bytes - 1) == 0 && (es + dst) & (size_bytes - 1) == 0;\n\n    // unaligned movs is properly handled in the fast path\n    let mut rep_fast = (instruction == Instruction::Movs || is_aligned)\n        && is_asize_32 // 16-bit address wraparound\n        && match rep {\n            Rep::NZ | Rep::Z => true,\n            Rep::None => false,\n        };\n\n    let mut phys_dst = 0;\n    let mut phys_src = 0;\n    let mut skip_dirty_page = false;\n\n    let mut movs_into_svga_lfb = false;\n    let mut movs_reenter_fast_path = false;\n\n    let count_until_end_of_page = if rep_fast {\n        match instruction {\n            Instruction::Movs => {\n                let (addr, skip) =\n                    return_on_pagefault!(translate_address_write_and_can_skip_dirty(es + dst));\n                movs_into_svga_lfb = memory::in_svga_lfb(addr);\n                rep_fast = rep_fast && (!memory::in_mapped_range(addr) || movs_into_svga_lfb);\n                phys_dst = addr;\n                skip_dirty_page = skip;\n            },\n            Instruction::Stos | Instruction::Ins => {\n                let (addr, skip) =\n                    return_on_pagefault!(translate_address_write_and_can_skip_dirty(es + dst));\n                rep_fast = rep_fast && !memory::in_mapped_range(addr);\n                phys_dst = addr;\n                skip_dirty_page = skip;\n            },\n            Instruction::Cmps | Instruction::Scas => {\n                let addr = return_on_pagefault!(translate_address_read(es + dst));\n                rep_fast = rep_fast && !memory::in_mapped_range(addr);\n                phys_dst = addr;\n                skip_dirty_page = true;\n            },\n            _ => {},\n        };\n\n        match instruction {\n            Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n                let addr = return_on_pagefault!(translate_address_read(ds + src));\n                rep_fast = rep_fast && !memory::in_mapped_range(addr);\n                phys_src = addr;\n            },\n            _ => {},\n        };\n\n        let count_until_end_of_page = u32::min(\n            count,\n            match instruction {\n                Instruction::Movs | Instruction::Cmps => u32::min(\n                    count_until_end_of_page(direction, size_bytes, phys_src),\n                    count_until_end_of_page(direction, size_bytes, phys_dst),\n                ),\n                Instruction::Stos | Instruction::Ins | Instruction::Scas => {\n                    count_until_end_of_page(direction, size_bytes, phys_dst)\n                },\n                Instruction::Lods | Instruction::Outs => {\n                    count_until_end_of_page(direction, size_bytes, phys_src)\n                },\n            },\n        );\n\n        match instruction {\n            Instruction::Movs => {\n                let c = count_until_end_of_page * size_bytes as u32;\n\n                let overlap_interferes = if phys_src < phys_dst {\n                    // backward moves may overlap at the front of the destination string\n                    phys_dst - phys_src < c && direction == 1\n                }\n                else if phys_src > phys_dst {\n                    // forward moves may overlap at the front of the source string\n                    phys_src - phys_dst < c && direction == -1\n                }\n                else {\n                    false\n                };\n                rep_fast = rep_fast && !overlap_interferes;\n\n                // In case the following page-boundary check fails, re-enter instruction after\n                // one iteration of the slow path\n                movs_reenter_fast_path = rep_fast;\n                rep_fast = rep_fast\n                    && (phys_src & 0xFFF <= 0x1000 - size_bytes as u32)\n                    && (phys_dst & 0xFFF <= 0x1000 - size_bytes as u32);\n            },\n            _ => {},\n        }\n\n        count_until_end_of_page\n    }\n    else {\n        0 // not used\n    };\n\n    if rep_fast {\n        dbg_assert!(count_until_end_of_page > 0);\n\n        if !skip_dirty_page {\n            jit::jit_dirty_page(Page::page_of(phys_dst));\n        }\n\n        let mut rep_cmp_finished = false;\n\n        let mut i = 0;\n        while i < count_until_end_of_page {\n            i += 1;\n\n            let src_val = match instruction {\n                Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n                    match size {\n                        Size::B => memory::read8_no_mmap_check(phys_src),\n                        Size::W => memory::read16_no_mmap_check(phys_src),\n                        Size::D => memory::read32_no_mmap_check(phys_src),\n                    }\n                },\n                Instruction::Scas | Instruction::Stos => data & size_mask,\n                Instruction::Ins => match size {\n                    Size::B => io_port_read8(port),\n                    Size::W => io_port_read16(port),\n                    Size::D => io_port_read32(port),\n                },\n            };\n\n            let mut dst_val = 0;\n\n            match instruction {\n                Instruction::Cmps | Instruction::Scas => match size {\n                    Size::B => dst_val = memory::read8_no_mmap_check(phys_dst),\n                    Size::W => dst_val = memory::read16_no_mmap_check(phys_dst),\n                    Size::D => dst_val = memory::read32_no_mmap_check(phys_dst),\n                },\n                Instruction::Outs => match size {\n                    Size::B => io_port_write8(port, src_val),\n                    Size::W => io_port_write16(port, src_val),\n                    Size::D => io_port_write32(port, src_val),\n                },\n                Instruction::Lods => match size {\n                    Size::B => write_reg8(AL, src_val),\n                    Size::W => write_reg16(AX, src_val),\n                    Size::D => write_reg32(EAX, src_val),\n                },\n                Instruction::Ins => match size {\n                    Size::B => memory::write8_no_mmap_or_dirty_check(phys_dst, src_val),\n                    Size::W => memory::write16_no_mmap_or_dirty_check(phys_dst, src_val),\n                    Size::D => memory::write32_no_mmap_or_dirty_check(phys_dst, src_val),\n                },\n                Instruction::Movs => {\n                    if direction == -1 {\n                        phys_src -= (count_until_end_of_page - 1) * size_bytes as u32;\n                        phys_dst -= (count_until_end_of_page - 1) * size_bytes as u32;\n                    }\n                    if movs_into_svga_lfb {\n                        memory::memcpy_into_svga_lfb(\n                            phys_src,\n                            phys_dst,\n                            count_until_end_of_page * size_bytes as u32,\n                        );\n                    }\n                    else {\n                        memory::memcpy_no_mmap_or_dirty_check(\n                            phys_src,\n                            phys_dst,\n                            count_until_end_of_page * size_bytes as u32,\n                        );\n                    }\n                    i = count_until_end_of_page;\n                    break;\n                },\n                Instruction::Stos => match size {\n                    Size::B => {\n                        if direction == -1 {\n                            phys_dst -= count_until_end_of_page - 1\n                        }\n                        memory::memset_no_mmap_or_dirty_check(\n                            phys_dst,\n                            src_val as u8,\n                            count_until_end_of_page,\n                        );\n                        i = count_until_end_of_page;\n                        break;\n                    },\n                    Size::W => memory::write16_no_mmap_or_dirty_check(phys_dst, src_val),\n                    Size::D => memory::write32_no_mmap_or_dirty_check(phys_dst, src_val),\n                },\n            };\n\n            match instruction {\n                Instruction::Movs\n                | Instruction::Cmps\n                | Instruction::Stos\n                | Instruction::Scas\n                | Instruction::Ins => {\n                    phys_dst += increment as u32;\n                },\n                _ => {},\n            }\n            match instruction {\n                Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n                    phys_src += increment as u32;\n                },\n                _ => {},\n            };\n\n            match instruction {\n                Instruction::Scas | Instruction::Cmps => {\n                    let rep_cmp = match rep {\n                        Rep::Z => src_val == dst_val,\n                        Rep::NZ => src_val != dst_val,\n                        Rep::None => {\n                            dbg_assert!(false);\n                            true\n                        },\n                    };\n                    if !rep_cmp || count == i {\n                        match size {\n                            Size::B => cmp8(src_val, dst_val),\n                            Size::W => cmp16(src_val, dst_val),\n                            Size::D => cmp32(src_val, dst_val),\n                        };\n                        rep_cmp_finished = true;\n                        break;\n                    }\n                },\n                _ => {},\n            }\n        }\n\n        dbg_assert!(i <= count);\n        count -= i;\n\n        if !rep_cmp_finished && count != 0 {\n            // go back to the current instruction, since this loop just handles a single page\n            *instruction_pointer = *previous_ip;\n        }\n\n        src += i as i32 * increment;\n        dst += i as i32 * increment;\n    }\n    else {\n        loop {\n            match instruction {\n                Instruction::Ins => {\n                    // check fault *before* reading from port\n                    // (technically not necessary according to Intel manuals)\n                    break_on_pagefault!(writable_or_pagefault(es + dst, size_bytes));\n                },\n                _ => {},\n            };\n            let src_val = match instruction {\n                Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n                    break_on_pagefault!(match size {\n                        Size::B => safe_read8(ds + src),\n                        Size::W => safe_read16(ds + src),\n                        Size::D => safe_read32s(ds + src),\n                    })\n                },\n                Instruction::Scas | Instruction::Stos => data & size_mask,\n                Instruction::Ins => match size {\n                    Size::B => io_port_read8(port),\n                    Size::W => io_port_read16(port),\n                    Size::D => io_port_read32(port),\n                },\n            };\n\n            let mut dst_val = 0;\n\n            match instruction {\n                Instruction::Cmps | Instruction::Scas => match size {\n                    Size::B => dst_val = break_on_pagefault!(safe_read8(es + dst)),\n                    Size::W => dst_val = break_on_pagefault!(safe_read16(es + dst)),\n                    Size::D => dst_val = break_on_pagefault!(safe_read32s(es + dst)),\n                },\n                Instruction::Outs => match size {\n                    Size::B => io_port_write8(port, src_val),\n                    Size::W => io_port_write16(port, src_val),\n                    Size::D => io_port_write32(port, src_val),\n                },\n                Instruction::Lods => match size {\n                    Size::B => write_reg8(AL, src_val),\n                    Size::W => write_reg16(AX, src_val),\n                    Size::D => write_reg32(EAX, src_val),\n                },\n                Instruction::Movs | Instruction::Stos | Instruction::Ins => match size {\n                    Size::B => break_on_pagefault!(safe_write8(es + dst, src_val)),\n                    Size::W => break_on_pagefault!(safe_write16(es + dst, src_val)),\n                    Size::D => break_on_pagefault!(safe_write32(es + dst, src_val)),\n                },\n            };\n\n            match instruction {\n                Instruction::Movs\n                | Instruction::Cmps\n                | Instruction::Stos\n                | Instruction::Scas\n                | Instruction::Ins => dst = dst + increment & asize_mask,\n                _ => {},\n            }\n            match instruction {\n                Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n                    src = src + increment & asize_mask\n                },\n                _ => {},\n            };\n\n            count -= 1;\n\n            let finished = match rep {\n                Rep::Z | Rep::NZ => match (rep, instruction) {\n                    (Rep::Z, Instruction::Cmps) => src_val != dst_val || count == 0,\n                    (Rep::Z, Instruction::Scas) => src_val != dst_val || count == 0,\n                    (Rep::NZ, Instruction::Cmps) => src_val == dst_val || count == 0,\n                    (Rep::NZ, Instruction::Scas) => src_val == dst_val || count == 0,\n                    (Rep::NZ | Rep::Z, Instruction::Movs) => {\n                        if count == 0 {\n                            true\n                        }\n                        else if movs_reenter_fast_path {\n                            *instruction_pointer = *previous_ip;\n                            true\n                        }\n                        else {\n                            false\n                        }\n                    },\n                    _ => count == 0,\n                },\n                Rep::None => true,\n            };\n\n            if finished {\n                match instruction {\n                    Instruction::Scas | Instruction::Cmps => match size {\n                        Size::B => cmp8(src_val, dst_val),\n                        Size::W => cmp16(src_val, dst_val),\n                        Size::D => cmp32(src_val, dst_val),\n                    },\n                    _ => {},\n                }\n                break;\n            }\n        }\n    }\n\n    match instruction {\n        Instruction::Movs\n        | Instruction::Cmps\n        | Instruction::Stos\n        | Instruction::Scas\n        | Instruction::Ins => set_reg_asize(is_asize_32, EDI, dst),\n        _ => {},\n    }\n    match instruction {\n        Instruction::Movs | Instruction::Cmps | Instruction::Lods | Instruction::Outs => {\n            set_reg_asize(is_asize_32, ESI, src)\n        },\n        _ => {},\n    };\n\n    match rep {\n        Rep::Z | Rep::NZ => {\n            set_reg_asize(is_asize_32, ECX, count as i32);\n        },\n        Rep::None => {},\n    }\n}\n\n#[no_mangle]\npub unsafe fn movsb_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Movs, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn movsw_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Movs, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn movsd_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Movs, Size::D, Rep::Z)\n}\npub unsafe fn movsb_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Movs, Size::B, Rep::None)\n}\npub unsafe fn movsw_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Movs, Size::W, Rep::None)\n}\npub unsafe fn movsd_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Movs, Size::D, Rep::None)\n}\n\n#[no_mangle]\npub unsafe fn lodsb_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Lods, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn lodsw_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Lods, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn lodsd_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Lods, Size::D, Rep::Z)\n}\npub unsafe fn lodsb_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Lods, Size::B, Rep::None)\n}\npub unsafe fn lodsw_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Lods, Size::W, Rep::None)\n}\npub unsafe fn lodsd_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Lods, Size::D, Rep::None)\n}\n\n#[no_mangle]\npub unsafe fn stosb_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Stos, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn stosw_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Stos, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn stosd_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Stos, Size::D, Rep::Z)\n}\npub unsafe fn stosb_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Stos, Size::B, Rep::None)\n}\npub unsafe fn stosw_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Stos, Size::W, Rep::None)\n}\npub unsafe fn stosd_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Stos, Size::D, Rep::None)\n}\n\n#[no_mangle]\npub unsafe fn cmpsb_repz(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn cmpsw_repz(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn cmpsd_repz(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::D, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn cmpsb_repnz(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::B, Rep::NZ)\n}\n#[no_mangle]\npub unsafe fn cmpsw_repnz(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::W, Rep::NZ)\n}\n#[no_mangle]\npub unsafe fn cmpsd_repnz(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::D, Rep::NZ)\n}\n#[no_mangle]\npub unsafe fn cmpsb_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::B, Rep::None)\n}\n#[no_mangle]\npub unsafe fn cmpsw_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::W, Rep::None)\n}\n#[no_mangle]\npub unsafe fn cmpsd_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Cmps, Size::D, Rep::None)\n}\n\n#[no_mangle]\npub unsafe fn scasb_repz(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn scasw_repz(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn scasd_repz(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::D, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn scasb_repnz(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::B, Rep::NZ)\n}\n#[no_mangle]\npub unsafe fn scasw_repnz(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::W, Rep::NZ)\n}\n#[no_mangle]\npub unsafe fn scasd_repnz(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::D, Rep::NZ)\n}\npub unsafe fn scasb_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::B, Rep::None)\n}\npub unsafe fn scasw_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::W, Rep::None)\n}\npub unsafe fn scasd_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Scas, Size::D, Rep::None)\n}\n\n#[no_mangle]\npub unsafe fn outsb_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Outs, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn outsw_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Outs, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn outsd_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Outs, Size::D, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn outsb_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Outs, Size::B, Rep::None)\n}\n#[no_mangle]\npub unsafe fn outsw_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Outs, Size::W, Rep::None)\n}\n#[no_mangle]\npub unsafe fn outsd_no_rep(is_asize_32: bool, seg: i32) {\n    string_instruction(is_asize_32, seg, Instruction::Outs, Size::D, Rep::None)\n}\n\n#[no_mangle]\npub unsafe fn insb_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Ins, Size::B, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn insw_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Ins, Size::W, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn insd_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Ins, Size::D, Rep::Z)\n}\n#[no_mangle]\npub unsafe fn insb_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Ins, Size::B, Rep::None)\n}\n#[no_mangle]\npub unsafe fn insw_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Ins, Size::W, Rep::None)\n}\n#[no_mangle]\npub unsafe fn insd_no_rep(is_asize_32: bool) {\n    string_instruction(is_asize_32, 0, Instruction::Ins, Size::D, Rep::None)\n}\n"
  },
  {
    "path": "src/rust/cpu/vga.rs",
    "content": "#![allow(non_upper_case_globals)]\n#![allow(static_mut_refs)]\n\n// Safety of allow(static_mut_refs) in this file:\n// These following two globals are not passed anywhere, only built-in function are called on them\nstatic mut dirty_bitmap: Vec<u64> = Vec::new();\nstatic mut dest_buffer: Vec<u32> = Vec::new();\n\nuse crate::cpu::global_pointers;\nuse crate::cpu::memory;\n\nuse std::ptr;\n\n#[no_mangle]\npub unsafe fn svga_allocate_dest_buffer(size: u32) -> u32 {\n    dest_buffer.resize(size as usize, 0);\n    dest_buffer.as_mut_ptr() as u32\n}\n\npub unsafe fn set_dirty_bitmap_size(size: u32) { dirty_bitmap.resize(size as usize, 0); }\n\npub unsafe fn mark_dirty(addr: u32) {\n    let page = (addr - memory::VGA_LFB_ADDRESS) >> 12;\n    dbg_assert!(((page >> 6) as usize) < dirty_bitmap.len());\n    *dirty_bitmap.get_unchecked_mut((page >> 6) as usize) |= 1 << (page & 63)\n}\n\n#[no_mangle]\npub unsafe fn svga_mark_dirty() {\n    for v in dirty_bitmap.iter_mut() {\n        *v = u64::MAX\n    }\n}\n\nfn iter_dirty_pages(f: &dyn Fn(isize)) {\n    let mut min_off = u32::MAX;\n    let mut max_off = u32::MIN;\n\n    for (i, &word) in unsafe { dirty_bitmap.iter().enumerate() } {\n        if word == 0 {\n            continue;\n        }\n        for j in 0..64 {\n            if word & 1 << j == 0 {\n                continue;\n            }\n            let off = ((i << 6 | j) << 12) as isize;\n            dbg_assert!(off < unsafe { memory::vga_memory_size as isize });\n            if min_off == u32::MAX {\n                min_off = off as u32;\n            }\n            max_off = off as u32;\n            f(off);\n        }\n    }\n\n    unsafe {\n        *global_pointers::svga_dirty_bitmap_min_offset = min_off;\n        *global_pointers::svga_dirty_bitmap_max_offset = max_off + 0xFFF;\n    }\n}\n\n#[no_mangle]\npub unsafe fn svga_fill_pixel_buffer(bpp: u32, svga_dest_offset: u32) {\n    let debug_bounds = false;\n\n    match bpp {\n        32 => iter_dirty_pages(&|off| {\n            dbg_assert!(off >= 0);\n            let src = memory::vga_mem8.offset(off) as *const u32;\n            let dest_offset = off / 4 - svga_dest_offset as isize;\n            let dest = dest_buffer.as_mut_ptr().offset(dest_offset) as *mut u32;\n            let end = if dest_offset < 0 {\n                0\n            }\n            else {\n                isize::min(1024, dest_buffer.len() as isize - dest_offset)\n            };\n\n            dbg_assert!(src as u32 % 8 == 0);\n            dbg_assert!(dest as u32 % 8 == 0);\n            for i in 0..end {\n                dbg_assert!(off + i < memory::vga_memory_size as isize);\n                let dword = *src.offset(i);\n                let dword = if debug_bounds && (i == 0 || i == end - 1) { 0xFFFFFF } else { dword };\n                dbg_assert!(dest_offset + i < dest_buffer.len() as isize);\n                *dest.offset(i) = dword << 16 | dword >> 16 & 0xFF | dword & 0xFF00 | 0xFF00_0000;\n            }\n        }),\n        24 => iter_dirty_pages(&|off| {\n            dbg_assert!(off >= 0 && off < memory::vga_memory_size as isize);\n            let off = off - off % 3;\n            let src = memory::vga_mem8.offset(off);\n            let dest_offset = off / 3 - svga_dest_offset as isize;\n            let dest = dest_buffer.as_mut_ptr().offset(dest_offset) as *mut u32;\n            let end = if dest_offset < 0 {\n                0\n            }\n            else {\n                isize::min(4096 / 3 + 1, dest_buffer.len() as isize - dest_offset)\n            };\n            for i in 0..end {\n                let dword = ptr::read_unaligned(src.offset(3 * i) as *const u32);\n                let dword = if debug_bounds && (i == 0 || i == end - 1) { 0xFFFFFF } else { dword };\n                dbg_assert!(dest_offset + i < dest_buffer.len() as isize);\n                *dest.offset(i) = dword << 16 | dword >> 16 & 0xFF | dword & 0xFF00 | 0xFF00_0000;\n            }\n        }),\n        16 => iter_dirty_pages(&|off| {\n            dbg_assert!(off >= 0 && off + 2048 < memory::vga_memory_size as isize);\n            let src = memory::vga_mem8.offset(off) as *const u16;\n            let dest_offset = off / 2 - svga_dest_offset as isize;\n            let dest = dest_buffer.as_mut_ptr().offset(dest_offset) as *mut u32;\n            let end = if dest_offset < 0 {\n                0\n            }\n            else {\n                isize::min(2048, dest_buffer.len() as isize - dest_offset)\n            };\n            for i in 0..end {\n                dbg_assert!(off + i < memory::vga_memory_size as isize);\n                let word = *src.offset(i);\n                let word = if debug_bounds && (i == 0 || i == end - 1) { 0xFFFF } else { word };\n                let r = (word & 0x1F) * 0xFF / 0x1F;\n                let g = (word >> 5 & 0x3F) * 0xFF / 0x3F;\n                let b = (word >> 11) * 0xFF / 0x1F;\n                dbg_assert!(dest_offset + i < dest_buffer.len() as isize);\n                *dest.offset(i) = (r as u32) << 16 | (g as u32) << 8 | b as u32 | 0xFF00_0000;\n            }\n        }),\n        15 => iter_dirty_pages(&|off| {\n            dbg_assert!(off >= 0 && off + 2048 < memory::vga_memory_size as isize);\n            let src = memory::vga_mem8.offset(off) as *const u16;\n            let dest_offset = off / 2 - svga_dest_offset as isize;\n            let dest = dest_buffer.as_mut_ptr().offset(dest_offset) as *mut u32;\n            let end = if dest_offset < 0 {\n                0\n            }\n            else {\n                isize::min(2048, dest_buffer.len() as isize - dest_offset)\n            };\n            for i in 0..end {\n                dbg_assert!(off + i < memory::vga_memory_size as isize);\n                let word = *src.offset(i);\n                let word = if debug_bounds && (i == 0 || i == end - 1) { 0xFFFF } else { word };\n                let r = (word & 0x1F) * 0xFF / 0x1F;\n                let g = (word >> 5 & 0x1F) * 0xFF / 0x1F;\n                let b = (word >> 10 & 0x1F) * 0xFF / 0x1F;\n                dbg_assert!(dest_offset + i < dest_buffer.len() as isize);\n                *dest.offset(i) = (r as u32) << 16 | (g as u32) << 8 | b as u32 | 0xFF00_0000;\n            }\n        }),\n        _ => {\n            dbg_log!(\"{}\", bpp);\n            dbg_assert!(false, \"Unsupported bpp\");\n        },\n    }\n\n    //if cfg!(debug_assertions) {\n    //    let mut pages = 0;\n    //    for &word in dirty_bitmap.iter() {\n    //        pages += word.count_ones();\n    //    }\n    //    dbg_log!(\n    //        \"fill offset={:x} bpp={} pages={}\",\n    //        svga_dest_offset,\n    //        bpp,\n    //        pages,\n    //    );\n    //}\n\n    for v in dirty_bitmap.iter_mut() {\n        *v = 0\n    }\n}\n"
  },
  {
    "path": "src/rust/cpu_context.rs",
    "content": "use crate::cpu::memory;\nuse crate::prefix::{PREFIX_MASK_ADDRSIZE, PREFIX_MASK_OPSIZE};\nuse crate::state_flags::CachedStateFlags;\n\n#[derive(Clone)]\npub struct CpuContext {\n    pub eip: u32,\n    pub prefixes: u8,\n    pub cs_offset: u32,\n    pub state_flags: CachedStateFlags,\n}\n\nimpl CpuContext {\n    pub fn advance16(&mut self) {\n        dbg_assert!(self.eip & 0xFFF < 0xFFE);\n        self.eip += 2;\n    }\n    pub fn advance32(&mut self) {\n        dbg_assert!(self.eip & 0xFFF < 0xFFC);\n        self.eip += 4;\n    }\n    #[allow(unused)]\n    pub fn advance_moffs(&mut self) {\n        if self.asize_32() {\n            self.advance32()\n        }\n        else {\n            self.advance16()\n        }\n    }\n\n    pub fn read_imm8(&mut self) -> u8 {\n        dbg_assert!(self.eip & 0xFFF < 0xFFF);\n        let v = memory::read8(self.eip) as u8;\n        self.eip += 1;\n        v\n    }\n    pub fn read_imm8s(&mut self) -> i8 { self.read_imm8() as i8 }\n    pub fn read_imm16(&mut self) -> u16 {\n        dbg_assert!(self.eip & 0xFFF < 0xFFE);\n        let v = memory::read16(self.eip) as u16;\n        self.eip += 2;\n        v\n    }\n    pub fn read_imm32(&mut self) -> u32 {\n        dbg_assert!(self.eip & 0xFFF < 0xFFC);\n        let v = memory::read32s(self.eip) as u32;\n        self.eip += 4;\n        v\n    }\n    pub fn read_moffs(&mut self) -> u32 {\n        if self.asize_32() {\n            self.read_imm32()\n        }\n        else {\n            self.read_imm16() as u32\n        }\n    }\n\n    pub fn cpl3(&self) -> bool { self.state_flags.cpl3() }\n    pub fn has_flat_segmentation(&self) -> bool { self.state_flags.has_flat_segmentation() }\n    pub fn osize_32(&self) -> bool {\n        self.state_flags.is_32() != (self.prefixes & PREFIX_MASK_OPSIZE != 0)\n    }\n    pub fn asize_32(&self) -> bool {\n        self.state_flags.is_32() != (self.prefixes & PREFIX_MASK_ADDRSIZE != 0)\n    }\n    pub fn ssize_32(&self) -> bool { self.state_flags.ssize_32() }\n}\n"
  },
  {
    "path": "src/rust/dbg.rs",
    "content": "#[allow(dead_code)]\npub const DEBUG: bool = cfg!(debug_assertions);\n\n#[cfg(target_arch = \"wasm32\")]\nextern \"C\" {\n    pub fn log_from_wasm(ptr: *const u8, len: usize);\n    pub fn console_log_from_wasm(ptr: *const u8, len: usize);\n    pub fn dbg_trace_from_wasm();\n}\n\n#[cfg(target_arch = \"wasm32\")]\npub fn log_to_js_console<T: std::string::ToString>(s: T) {\n    let s = s.to_string();\n    let len = s.len();\n    unsafe {\n        log_from_wasm(s.as_bytes().as_ptr(), len);\n    }\n}\n\n#[cfg(target_arch = \"wasm32\")]\npub fn console_log_to_js_console<T: std::string::ToString>(s: T) {\n    let s = s.to_string();\n    let len = s.len();\n    unsafe {\n        console_log_from_wasm(s.as_bytes().as_ptr(), len);\n    }\n}\n\n#[cfg(target_arch = \"wasm32\")]\npub fn dbg_trace() {\n    if DEBUG {\n        unsafe {\n            dbg_trace_from_wasm();\n        }\n    }\n}\n\n#[cfg(not(target_arch = \"wasm32\"))]\npub fn dbg_trace() {}\n\n#[allow(unused_macros)]\nmacro_rules! dbg_log {\n    ($fmt:expr) => {\n        println!($fmt);\n    };\n    ($fmt:expr, $($arg:tt)*) => {\n        println!($fmt, $($arg)*);\n    }\n}\n\n#[allow(unused_macros)]\nmacro_rules! console_log {\n    ($fmt:expr) => {\n        println!($fmt);\n    };\n    ($fmt:expr, $($arg:tt)*) => {\n        println!($fmt, $($arg)*);\n    }\n}\n\n#[allow(unused_macros)]\nmacro_rules! dbg_assert {\n    ($($arg:tt)*) => {\n        debug_assert!($($arg)*)\n    };\n}\n\n#[cfg(target_arch = \"wasm32\")]\n#[allow(unused_macros)]\nmacro_rules! console_log {\n    ($fmt:expr) => {\n        {\n            crate::dbg::console_log_to_js_console($fmt);\n        }\n    };\n    ($fmt:expr, $($arg:tt)*) => {\n        {\n            crate::dbg::console_log_to_js_console(format!($fmt, $($arg)*));\n        }\n    };\n}\n\n#[cfg(target_arch = \"wasm32\")]\n#[allow(unused_macros)]\nmacro_rules! dbg_log {\n    ($fmt:expr) => {\n        {\n            use crate::dbg::{ DEBUG, log_to_js_console };\n            if DEBUG { log_to_js_console($fmt); }\n        }\n    };\n    ($fmt:expr, $($arg:tt)*) => {\n        {\n            use crate::dbg::{ DEBUG, log_to_js_console };\n            if DEBUG { log_to_js_console(format!($fmt, $($arg)*)); }\n        }\n    };\n}\n"
  },
  {
    "path": "src/rust/gen/mod.rs",
    "content": "#[rustfmt::skip]\npub mod interpreter;\n#[rustfmt::skip]\npub mod interpreter0f;\n\n#[rustfmt::skip]\npub mod jit;\n#[rustfmt::skip]\npub mod jit0f;\n\n#[rustfmt::skip]\npub mod analyzer;\n#[rustfmt::skip]\npub mod analyzer0f;\n"
  },
  {
    "path": "src/rust/jit.rs",
    "content": "use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};\nuse std::iter::FromIterator;\nuse std::mem::{self, MaybeUninit};\nuse std::ops::{Deref, DerefMut};\nuse std::ptr::NonNull;\nuse std::sync::{Mutex, MutexGuard};\n\nuse crate::analysis;\nuse crate::analysis::AnalysisType;\nuse crate::codegen;\nuse crate::control_flow;\nuse crate::control_flow::WasmStructure;\nuse crate::cpu::cpu;\nuse crate::cpu::global_pointers;\nuse crate::cpu::memory;\nuse crate::cpu_context::CpuContext;\nuse crate::jit_instructions;\nuse crate::opstats;\nuse crate::page::Page;\nuse crate::profiler;\nuse crate::profiler::stat;\nuse crate::state_flags::CachedStateFlags;\nuse crate::wasmgen::wasm_builder::{Label, WasmBuilder, WasmLocal};\n\n#[derive(Copy, Clone, Eq, Hash, PartialEq)]\n#[repr(transparent)]\npub struct WasmTableIndex(u16);\nimpl WasmTableIndex {\n    pub fn to_u16(self) -> u16 { self.0 }\n}\n\nmod unsafe_jit {\n    use super::{CachedStateFlags, WasmTableIndex};\n\n    extern \"C\" {\n        pub fn codegen_finalize(\n            wasm_table_index: WasmTableIndex,\n            phys_addr: u32,\n            state_flags: CachedStateFlags,\n            ptr: u32,\n            len: u32,\n        );\n        pub fn jit_clear_func(wasm_table_index: WasmTableIndex);\n    }\n}\n\nfn codegen_finalize(\n    wasm_table_index: WasmTableIndex,\n    phys_addr: u32,\n    state_flags: CachedStateFlags,\n    ptr: u32,\n    len: u32,\n) {\n    unsafe { unsafe_jit::codegen_finalize(wasm_table_index, phys_addr, state_flags, ptr, len) }\n}\n\npub fn jit_clear_func(wasm_table_index: WasmTableIndex) {\n    unsafe { unsafe_jit::jit_clear_func(wasm_table_index) }\n}\n\nstatic mut JIT_DISABLED: bool = false;\n\n// Maximum number of pages per wasm module. Necessary for the following reasons:\n// - There is an upper limit on the size of a single function in wasm (currently ~7MB in all browsers)\n//   See https://github.com/WebAssembly/design/issues/1138\n// - v8 poorly handles large br_table elements and OOMs on modules much smaller than the above limit\n//   See https://bugs.chromium.org/p/v8/issues/detail?id=9697 and https://bugs.chromium.org/p/v8/issues/detail?id=9141\n//   Will hopefully be fixed in the near future by generating direct control flow\nstatic mut MAX_PAGES: u32 = 3;\n\nstatic mut JIT_USE_LOOP_SAFETY: bool = true;\n\npub static mut MAX_EXTRA_BASIC_BLOCKS: u32 = 250;\n\npub const JIT_THRESHOLD: u32 = 200 * 1000;\n\n// less branches will generate if-else, more will generate brtable\npub const BRTABLE_CUTOFF: usize = 10;\n\n// needs to be synced to const.js\npub const WASM_TABLE_SIZE: u32 = 900;\n\npub const CHECK_JIT_STATE_INVARIANTS: bool = false;\n\nconst MAX_INSTRUCTION_LENGTH: u32 = 16;\n\nstatic JIT_STATE: Mutex<MaybeUninit<JitState>> = Mutex::new(MaybeUninit::uninit());\nfn get_jit_state() -> JitStateRef { JitStateRef(JIT_STATE.try_lock().unwrap()) }\n\nstruct JitStateRef(MutexGuard<'static, MaybeUninit<JitState>>);\n\nimpl Deref for JitStateRef {\n    type Target = JitState;\n    fn deref(&self) -> &Self::Target { unsafe { self.0.assume_init_ref() } }\n}\nimpl DerefMut for JitStateRef {\n    fn deref_mut(&mut self) -> &mut Self::Target { unsafe { self.0.assume_init_mut() } }\n}\n\n#[no_mangle]\npub fn rust_init() {\n    dbg_assert!(std::mem::size_of::<[Option<NonNull<cpu::Code>>; 0x100000]>() == 0x100000 * 4);\n\n    let _ = JIT_STATE\n        .try_lock()\n        .unwrap()\n        .write(JitState::create_and_initialise());\n\n    use std::panic;\n\n    panic::set_hook(Box::new(|panic_info| {\n        console_log!(\"{}\", panic_info.to_string());\n    }));\n}\n\nstruct PageInfo {\n    wasm_table_index: WasmTableIndex,\n    hidden_wasm_table_indices: Vec<WasmTableIndex>,\n    entry_points: Vec<(u16, u16)>,\n    state_flags: CachedStateFlags,\n}\n\nenum CompilingPageState {\n    Compiling { pages: HashMap<Page, PageInfo> },\n    CompilingWritten,\n}\n\nstruct JitState {\n    wasm_builder: WasmBuilder,\n\n    // as an alternative to HashSet, we could use a bitmap of 4096 bits here\n    // (faster, but uses much more memory)\n    // or a compressed bitmap (likely faster)\n    // or HashSet<u32> rather than nested\n    entry_points: HashMap<Page, (u32, HashSet<u16>)>,\n    pages: HashMap<Page, PageInfo>,\n    wasm_table_index_free_list: Vec<WasmTableIndex>,\n    compiling: Option<(WasmTableIndex, CompilingPageState)>,\n}\n\nfn check_jit_state_invariants(ctx: &mut JitState) {\n    if !CHECK_JIT_STATE_INVARIANTS {\n        return;\n    }\n\n    match &ctx.compiling {\n        Some((_, CompilingPageState::Compiling { pages })) => {\n            dbg_assert!(pages.keys().all(|page| ctx.entry_points.contains_key(page)));\n        },\n        _ => {},\n    }\n\n    let free: HashSet<WasmTableIndex> =\n        HashSet::from_iter(ctx.wasm_table_index_free_list.iter().cloned());\n    let used = HashSet::from_iter(ctx.pages.values().map(|info| info.wasm_table_index));\n    let compiling = HashSet::from_iter(ctx.compiling.as_ref().map(|&(index, _)| index));\n    dbg_assert!(free.intersection(&used).next().is_none());\n    dbg_assert!(used.intersection(&compiling).next().is_none());\n    dbg_assert!(free.len() + used.len() + compiling.len() == (WASM_TABLE_SIZE - 1) as usize);\n\n    match &ctx.compiling {\n        Some((_, CompilingPageState::Compiling { pages })) => {\n            dbg_assert!(pages.keys().all(|page| ctx.entry_points.contains_key(page)));\n        },\n        _ => {},\n    }\n\n    for i in 0..unsafe { cpu::valid_tlb_entries_count } {\n        let page = unsafe { cpu::valid_tlb_entries[i as usize] };\n        let entry = unsafe { cpu::tlb_data[page as usize] };\n        if 0 != entry {\n            let tlb_physical_page = Page::of_u32(\n                (entry as u32 >> 12 ^ page as u32) - (unsafe { memory::mem8 } as u32 >> 12),\n            );\n            let w = match unsafe { cpu::tlb_code[page as usize] } {\n                None => None,\n                Some(c) => unsafe { Some(c.as_ref().wasm_table_index) },\n            };\n            let tlb_has_code = entry & cpu::TLB_HAS_CODE == cpu::TLB_HAS_CODE;\n            let infos = ctx.pages.get(&tlb_physical_page);\n            let entry_points = ctx.entry_points.get(&tlb_physical_page);\n            dbg_assert!(tlb_has_code || !w.is_some());\n            dbg_assert!(tlb_has_code || !infos.is_some());\n            dbg_assert!(tlb_has_code || !entry_points.is_some());\n            //dbg_assert!((w.is_some() || page.is_some() || entry_points.is_some()) == tlb_has_code); // XXX: check this\n        }\n    }\n}\n\nimpl JitState {\n    pub fn create_and_initialise() -> JitState {\n        // don't assign 0 (XXX: Check)\n        let wasm_table_indices = (1..=(WASM_TABLE_SIZE - 1) as u16).map(|x| WasmTableIndex(x));\n\n        JitState {\n            wasm_builder: WasmBuilder::new(),\n\n            entry_points: HashMap::new(),\n            pages: HashMap::new(),\n\n            wasm_table_index_free_list: Vec::from_iter(wasm_table_indices),\n            compiling: None,\n        }\n    }\n}\n\n#[derive(PartialEq, Eq)]\npub enum BasicBlockType {\n    Normal {\n        next_block_addr: Option<u32>,\n        jump_offset: i32,\n        jump_offset_is_32: bool,\n    },\n    ConditionalJump {\n        next_block_addr: Option<u32>,\n        next_block_branch_taken_addr: Option<u32>,\n        condition: u8,\n        jump_offset: i32,\n        jump_offset_is_32: bool,\n    },\n    // Set eip to an absolute value (ret, jmp r/m, call r/m)\n    AbsoluteEip,\n    Exit,\n}\n\npub struct BasicBlock {\n    pub addr: u32,\n    pub virt_addr: i32,\n    pub last_instruction_addr: u32,\n    pub end_addr: u32,\n    pub is_entry_block: bool,\n    pub ty: BasicBlockType,\n    pub has_sti: bool,\n    pub number_of_instructions: u32,\n}\n\n#[derive(Copy, Clone, PartialEq)]\npub struct CachedCode {\n    pub wasm_table_index: WasmTableIndex,\n    pub initial_state: u16,\n}\n\nimpl CachedCode {\n    pub const NONE: CachedCode = CachedCode {\n        wasm_table_index: WasmTableIndex(0),\n        initial_state: 0,\n    };\n}\n\n#[derive(PartialEq)]\npub enum InstructionOperandDest {\n    WasmLocal(WasmLocal),\n    Other,\n}\n#[derive(PartialEq)]\npub enum InstructionOperand {\n    WasmLocal(WasmLocal),\n    Immediate(i32),\n    Other,\n}\nimpl InstructionOperand {\n    pub fn is_zero(&self) -> bool {\n        match self {\n            InstructionOperand::Immediate(0) => true,\n            _ => false,\n        }\n    }\n}\nimpl Into<InstructionOperand> for InstructionOperandDest {\n    fn into(self: InstructionOperandDest) -> InstructionOperand {\n        match self {\n            InstructionOperandDest::WasmLocal(l) => InstructionOperand::WasmLocal(l),\n            InstructionOperandDest::Other => InstructionOperand::Other,\n        }\n    }\n}\npub enum Instruction {\n    Cmp {\n        dest: InstructionOperandDest,\n        source: InstructionOperand,\n        opsize: i32,\n    },\n    Sub {\n        dest: InstructionOperandDest,\n        source: InstructionOperand,\n        opsize: i32,\n        is_dec: bool,\n    },\n    Add {\n        dest: InstructionOperandDest,\n        source: InstructionOperand,\n        opsize: i32,\n        is_inc: bool,\n    },\n    AdcSbb {\n        dest: InstructionOperandDest,\n        #[allow(dead_code)]\n        source: InstructionOperand,\n        opsize: i32,\n    },\n    NonZeroShift {\n        dest: InstructionOperandDest,\n        opsize: i32,\n    },\n    Bitwise {\n        dest: InstructionOperandDest,\n        opsize: i32,\n    },\n    Other,\n}\n\npub struct JitContext<'a> {\n    pub cpu: &'a mut CpuContext,\n    pub builder: &'a mut WasmBuilder,\n    pub register_locals: &'a mut Vec<WasmLocal>,\n    pub start_of_current_instruction: u32,\n    pub exit_with_fault_label: Label,\n    pub exit_label: Label,\n    pub current_instruction: Instruction,\n    pub previous_instruction: Instruction,\n    pub instruction_counter: WasmLocal,\n}\nimpl<'a> JitContext<'a> {\n    pub fn reg(&self, i: u32) -> WasmLocal {\n        match self.register_locals.get(i as usize) {\n            Some(x) => x.unsafe_clone(),\n            None => {\n                dbg_assert!(false);\n                unsafe { std::hint::unreachable_unchecked() }\n            },\n        }\n    }\n}\n\npub const JIT_INSTR_BLOCK_BOUNDARY_FLAG: u32 = 1 << 0;\n\npub fn is_near_end_of_page(address: u32) -> bool {\n    address & 0xFFF >= 0x1000 - MAX_INSTRUCTION_LENGTH\n}\n\npub fn jit_find_cache_entry(phys_address: u32, state_flags: CachedStateFlags) -> CachedCode {\n    // TODO: dedup with jit_find_cache_entry_in_page?\n    // NOTE: This is currently only used for invariant/missed-entry-point checking\n    let ctx = get_jit_state();\n\n    match ctx.pages.get(&Page::page_of(phys_address)) {\n        Some(PageInfo {\n            wasm_table_index,\n            state_flags: s,\n            entry_points,\n            hidden_wasm_table_indices: _,\n        }) => {\n            if *s == state_flags {\n                let page_offset = phys_address as u16 & 0xFFF;\n                if let Some(&(_, initial_state)) =\n                    entry_points.iter().find(|(p, _)| p == &page_offset)\n                {\n                    return CachedCode {\n                        wasm_table_index: *wasm_table_index,\n                        initial_state,\n                    };\n                }\n            }\n        },\n        None => {},\n    }\n\n    return CachedCode::NONE;\n}\n\n#[no_mangle]\npub fn jit_find_cache_entry_in_page(\n    virt_address: u32,\n    wasm_table_index: WasmTableIndex,\n    state_flags: u32,\n) -> i32 {\n    // TODO: generate code for this\n    profiler::stat_increment(stat::INDIRECT_JUMP);\n\n    let state_flags = CachedStateFlags::of_u32(state_flags);\n\n    unsafe {\n        match cpu::tlb_code[(virt_address >> 12) as usize] {\n            None => {},\n            Some(c) => {\n                let c = c.as_ref();\n                if state_flags == c.state_flags && wasm_table_index == c.wasm_table_index {\n                    let state = c.state_table[virt_address as usize & 0xFFF];\n                    if state != u16::MAX {\n                        return state.into();\n                    }\n                }\n            },\n        }\n    }\n\n    profiler::stat_increment(stat::INDIRECT_JUMP_NO_ENTRY);\n\n    return -1;\n}\n\nfn jit_find_basic_blocks(\n    ctx: &mut JitState,\n    entry_points: HashSet<i32>,\n    cpu: CpuContext,\n) -> Vec<BasicBlock> {\n    fn follow_jump(\n        virt_target: i32,\n        ctx: &mut JitState,\n        pages: &mut HashSet<Page>,\n        page_blacklist: &mut HashSet<Page>,\n        max_pages: u32,\n        marked_as_entry: &mut HashSet<i32>,\n        to_visit_stack: &mut Vec<i32>,\n    ) -> Option<u32> {\n        if is_near_end_of_page(virt_target as u32) {\n            return None;\n        }\n        let phys_target = match cpu::translate_address_read_no_side_effects(virt_target) {\n            Err(()) => {\n                dbg_log!(\"Not analysing {:x} (page not mapped)\", virt_target);\n                return None;\n            },\n            Ok(t) => t,\n        };\n\n        let phys_page = Page::page_of(phys_target);\n\n        if !pages.contains(&phys_page) && pages.len() as u32 == max_pages\n            || page_blacklist.contains(&phys_page)\n        {\n            return None;\n        }\n\n        if !pages.contains(&phys_page) {\n            // page seen for the first time, handle entry points\n            if let Some((hotness, entry_points)) = ctx.entry_points.get_mut(&phys_page) {\n                let existing_entry_points = match ctx.pages.get(&phys_page) {\n                    Some(PageInfo { entry_points, .. }) => {\n                        HashSet::from_iter(entry_points.iter().map(|x| x.0))\n                    },\n                    None => HashSet::new(),\n                };\n\n                if entry_points\n                    .iter()\n                    .all(|entry_point| existing_entry_points.contains(entry_point))\n                {\n                    page_blacklist.insert(phys_page);\n                    return None;\n                }\n\n                // XXX: Remove this paragraph\n                //let old_length = entry_points.len();\n                //entry_points.extend(existing_entry_points);\n                //dbg_assert!(\n                //    entry_points.union(&existing_entry_points).count() == entry_points.len()\n                //);\n\n                *hotness = 0;\n\n                for &addr_low in entry_points.iter() {\n                    let addr = virt_target & !0xFFF | addr_low as i32;\n                    to_visit_stack.push(addr);\n                    marked_as_entry.insert(addr);\n                }\n            }\n            else {\n                // no entry points: ignore this page?\n                page_blacklist.insert(phys_page);\n                return None;\n            }\n\n            pages.insert(phys_page);\n            dbg_assert!(pages.len() as u32 <= max_pages);\n        }\n\n        to_visit_stack.push(virt_target);\n        Some(phys_target)\n    }\n\n    let mut to_visit_stack: Vec<i32> = Vec::new();\n    let mut marked_as_entry: HashSet<i32> = HashSet::new();\n    let mut basic_blocks: BTreeMap<u32, BasicBlock> = BTreeMap::new();\n    let mut pages: HashSet<Page> = HashSet::new();\n    let mut page_blacklist = HashSet::new();\n\n    // 16-bit doesn't work correctly, most likely due to instruction pointer wrap-around\n    let max_pages = if cpu.state_flags.is_32() { unsafe { MAX_PAGES } } else { 1 };\n\n    for virt_addr in entry_points {\n        let ok = follow_jump(\n            virt_addr,\n            ctx,\n            &mut pages,\n            &mut page_blacklist,\n            max_pages,\n            &mut marked_as_entry,\n            &mut to_visit_stack,\n        );\n        dbg_assert!(ok.is_some());\n        dbg_assert!(marked_as_entry.contains(&virt_addr));\n    }\n\n    while let Some(to_visit) = to_visit_stack.pop() {\n        let phys_addr = match cpu::translate_address_read_no_side_effects(to_visit) {\n            Err(()) => {\n                dbg_log!(\"Not analysing {:x} (page not mapped)\", to_visit);\n                continue;\n            },\n            Ok(phys_addr) => phys_addr,\n        };\n\n        if basic_blocks.contains_key(&phys_addr) {\n            continue;\n        }\n\n        if is_near_end_of_page(phys_addr) {\n            // Empty basic block, don't insert\n            profiler::stat_increment(stat::COMPILE_CUT_OFF_AT_END_OF_PAGE);\n            continue;\n        }\n\n        let mut current_address = phys_addr;\n        let mut current_block = BasicBlock {\n            addr: current_address,\n            virt_addr: to_visit,\n            last_instruction_addr: 0,\n            end_addr: 0,\n            ty: BasicBlockType::Exit,\n            is_entry_block: false,\n            has_sti: false,\n            number_of_instructions: 0,\n        };\n        loop {\n            let addr_before_instruction = current_address;\n            let mut cpu = &mut CpuContext {\n                eip: current_address,\n                ..cpu\n            };\n            let analysis = analysis::analyze_step(&mut cpu);\n            let has_next_instruction = !analysis.no_next_instruction;\n            current_address = cpu.eip;\n\n            dbg_assert!(Page::page_of(current_address) == Page::page_of(addr_before_instruction));\n            let current_virt_addr = to_visit & !0xFFF | current_address as i32 & 0xFFF;\n\n            if analysis.ty == AnalysisType::STI && is_near_end_of_page(current_address) {\n                // cut off before the STI so that it is handled by interpreted mode\n                profiler::stat_increment(stat::COMPILE_CUT_OFF_AT_END_OF_PAGE);\n                break;\n            }\n\n            current_block.number_of_instructions += 1;\n            current_block.last_instruction_addr = addr_before_instruction;\n            current_block.end_addr = current_address;\n\n            match analysis.ty {\n                AnalysisType::Normal | AnalysisType::STI => {\n                    dbg_assert!(has_next_instruction);\n                    dbg_assert!(!analysis.absolute_jump);\n\n                    if current_block.has_sti {\n                        // Convert next instruction after STI (i.e., the current instruction) into block boundary\n\n                        marked_as_entry.insert(current_virt_addr);\n                        to_visit_stack.push(current_virt_addr);\n\n                        break;\n                    }\n\n                    if analysis.ty == AnalysisType::STI {\n                        dbg_assert!(\n                            !is_near_end_of_page(current_address),\n                            \"should be handled above\"\n                        );\n\n                        current_block.has_sti = true;\n                    }\n                    else {\n                        // Only split non-STI blocks (one instruction needs to run after STI before\n                        // handle_irqs may be called)\n\n                        if basic_blocks.contains_key(&current_address) {\n                            dbg_assert!(!is_near_end_of_page(current_address));\n                            current_block.ty = BasicBlockType::Normal {\n                                next_block_addr: Some(current_address),\n                                jump_offset: 0,\n                                jump_offset_is_32: true,\n                            };\n                            break;\n                        }\n                    }\n                },\n                AnalysisType::Jump {\n                    offset,\n                    is_32,\n                    condition: Some(condition),\n                } => {\n                    dbg_assert!(!analysis.absolute_jump);\n                    // conditional jump: continue at next and continue at jump target\n\n                    let jump_target = if is_32 {\n                        current_virt_addr + offset\n                    }\n                    else {\n                        cpu.cs_offset as i32\n                            + (current_virt_addr - cpu.cs_offset as i32 + offset & 0xFFFF)\n                    };\n\n                    dbg_assert!(has_next_instruction);\n                    to_visit_stack.push(current_virt_addr);\n\n                    let next_block_addr = if is_near_end_of_page(current_address) {\n                        None\n                    }\n                    else {\n                        Some(current_address)\n                    };\n\n                    current_block.ty = BasicBlockType::ConditionalJump {\n                        next_block_addr,\n                        next_block_branch_taken_addr: follow_jump(\n                            jump_target,\n                            ctx,\n                            &mut pages,\n                            &mut page_blacklist,\n                            max_pages,\n                            &mut marked_as_entry,\n                            &mut to_visit_stack,\n                        ),\n                        condition,\n                        jump_offset: offset,\n                        jump_offset_is_32: is_32,\n                    };\n\n                    break;\n                },\n                AnalysisType::Jump {\n                    offset,\n                    is_32,\n                    condition: None,\n                } => {\n                    dbg_assert!(!analysis.absolute_jump);\n                    // non-conditional jump: continue at jump target\n\n                    let jump_target = if is_32 {\n                        current_virt_addr + offset\n                    }\n                    else {\n                        cpu.cs_offset as i32\n                            + (current_virt_addr - cpu.cs_offset as i32 + offset & 0xFFFF)\n                    };\n\n                    if has_next_instruction {\n                        // Execution will eventually come back to the next instruction (CALL)\n                        marked_as_entry.insert(current_virt_addr);\n                        to_visit_stack.push(current_virt_addr);\n                    }\n\n                    current_block.ty = BasicBlockType::Normal {\n                        next_block_addr: follow_jump(\n                            jump_target,\n                            ctx,\n                            &mut pages,\n                            &mut page_blacklist,\n                            max_pages,\n                            &mut marked_as_entry,\n                            &mut to_visit_stack,\n                        ),\n                        jump_offset: offset,\n                        jump_offset_is_32: is_32,\n                    };\n\n                    break;\n                },\n                AnalysisType::BlockBoundary => {\n                    // a block boundary but not a jump, get out\n\n                    if has_next_instruction {\n                        // block boundary, but execution will eventually come back\n                        // to the next instruction. Create a new basic block\n                        // starting at the next instruction and register it as an\n                        // entry point\n                        marked_as_entry.insert(current_virt_addr);\n                        to_visit_stack.push(current_virt_addr);\n                    }\n\n                    if analysis.absolute_jump {\n                        current_block.ty = BasicBlockType::AbsoluteEip;\n                    }\n\n                    break;\n                },\n            }\n\n            if is_near_end_of_page(current_address) {\n                profiler::stat_increment(stat::COMPILE_CUT_OFF_AT_END_OF_PAGE);\n                break;\n            }\n        }\n\n        if current_block.number_of_instructions == 0 {\n            // Empty basic block, don't insert (only happens when STI is found near end of page)\n            continue;\n        }\n\n        let previous_block = basic_blocks\n            .range(..current_block.addr)\n            .next_back()\n            .filter(|(_, previous_block)| !previous_block.has_sti)\n            .map(|(_, previous_block)| previous_block);\n\n        if let Some(previous_block) = previous_block {\n            if current_block.addr < previous_block.end_addr {\n                // If this block overlaps with the previous block, re-analyze the previous block\n                to_visit_stack.push(previous_block.virt_addr);\n\n                let addr = previous_block.addr;\n                let old_block = basic_blocks.remove(&addr);\n                dbg_assert!(old_block.is_some());\n\n                // Note that this does not ensure the invariant that two consecutive blocks don't\n                // overlay. For that, we also need to check the following block.\n            }\n        }\n\n        dbg_assert!(current_block.addr < current_block.end_addr);\n        dbg_assert!(current_block.addr <= current_block.last_instruction_addr);\n        dbg_assert!(current_block.last_instruction_addr < current_block.end_addr);\n\n        basic_blocks.insert(current_block.addr, current_block);\n    }\n\n    dbg_assert!(pages.len() as u32 <= max_pages);\n\n    for block in basic_blocks.values_mut() {\n        if marked_as_entry.contains(&block.virt_addr) {\n            block.is_entry_block = true;\n        }\n    }\n\n    let basic_blocks: Vec<BasicBlock> = basic_blocks.into_iter().map(|(_, block)| block).collect();\n\n    for i in 0..basic_blocks.len() - 1 {\n        let next_block_addr = basic_blocks[i + 1].addr;\n        let next_block_end_addr = basic_blocks[i + 1].end_addr;\n        let next_block_is_entry = basic_blocks[i + 1].is_entry_block;\n        let block = &basic_blocks[i];\n        dbg_assert!(block.addr < next_block_addr);\n        if next_block_addr < block.end_addr {\n            dbg_log!(\n                \"Overlapping first=[from={:x} to={:x} is_entry={}] second=[from={:x} to={:x} is_entry={}]\",\n                block.addr,\n                block.end_addr,\n                block.is_entry_block as u8,\n                next_block_addr,\n                next_block_end_addr,\n                next_block_is_entry as u8\n            );\n        }\n    }\n\n    basic_blocks\n}\n\n#[no_mangle]\n#[cfg(debug_assertions)]\npub fn jit_force_generate_unsafe(virt_addr: i32) {\n    dbg_assert!(\n        !is_near_end_of_page(virt_addr as u32),\n        \"cannot force compile near end of page\"\n    );\n    jit_increase_hotness_and_maybe_compile(\n        virt_addr,\n        cpu::translate_address_read(virt_addr).unwrap(),\n        cpu::get_seg_cs() as u32,\n        cpu::get_state_flags(),\n        JIT_THRESHOLD,\n    );\n    dbg_assert!(get_jit_state().compiling.is_some());\n}\n\n#[inline(never)]\nfn jit_analyze_and_generate(\n    ctx: &mut JitState,\n    virt_entry_point: i32,\n    phys_entry_point: u32,\n    cs_offset: u32,\n    state_flags: CachedStateFlags,\n) {\n    let page = Page::page_of(phys_entry_point);\n\n    dbg_assert!(ctx.compiling.is_none());\n\n    let (_, entry_points) = match ctx.entry_points.get(&page) {\n        None => return,\n        Some(entry_points) => entry_points,\n    };\n\n    let existing_entry_points = match ctx.pages.get(&page) {\n        Some(PageInfo { entry_points, .. }) => HashSet::from_iter(entry_points.iter().map(|x| x.0)),\n        None => HashSet::new(),\n    };\n\n    if entry_points\n        .iter()\n        .all(|entry_point| existing_entry_points.contains(entry_point))\n    {\n        profiler::stat_increment(stat::COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS);\n        return;\n    }\n\n    // XXX: check and remove\n    //let old_length = entry_points.len();\n    //entry_points.extend(existing_entry_points);\n    //dbg_log!(\n    //    \"{} + {} = {}\",\n    //    entry_points.len(),\n    //    existing_entry_points.len(),\n    //    entry_points.union(&existing_entry_points).count()\n    //);\n    //dbg_assert!(entry_points.union(&existing_entry_points).count() == entry_points.len());\n\n    profiler::stat_increment(stat::COMPILE);\n\n    let cpu = CpuContext {\n        eip: 0,\n        prefixes: 0,\n        cs_offset,\n        state_flags,\n    };\n\n    dbg_assert!(\n        cpu::translate_address_read_no_side_effects(virt_entry_point).unwrap() == phys_entry_point\n    );\n    let virt_page = Page::page_of(virt_entry_point as u32);\n    let entry_points: HashSet<i32> = entry_points\n        .iter()\n        .map(|e| virt_page.to_address() as i32 | *e as i32)\n        .collect();\n    let basic_blocks = jit_find_basic_blocks(ctx, entry_points, cpu.clone());\n\n    let mut pages = HashSet::new();\n\n    for b in basic_blocks.iter() {\n        // Remove this assertion once page-crossing jit is enabled\n        dbg_assert!(Page::page_of(b.addr) == Page::page_of(b.end_addr));\n        pages.insert(Page::page_of(b.addr));\n    }\n\n    let print = false;\n\n    for b in basic_blocks.iter() {\n        if !print {\n            break;\n        }\n        let last_instruction_opcode = memory::read32s(b.last_instruction_addr);\n        let op = opstats::decode(last_instruction_opcode as u32);\n        dbg_log!(\n            \"BB: 0x{:x} {}{:02x} {} {}\",\n            b.addr,\n            if op.is_0f { \"0f\" } else { \"\" },\n            op.opcode,\n            if b.is_entry_block { \"entry\" } else { \"noentry\" },\n            match &b.ty {\n                BasicBlockType::ConditionalJump {\n                    next_block_addr: Some(next_block_addr),\n                    next_block_branch_taken_addr: Some(next_block_branch_taken_addr),\n                    ..\n                } => format!(\n                    \"0x{:x} 0x{:x}\",\n                    next_block_addr, next_block_branch_taken_addr\n                ),\n                BasicBlockType::ConditionalJump {\n                    next_block_addr: None,\n                    next_block_branch_taken_addr: Some(next_block_branch_taken_addr),\n                    ..\n                } => format!(\"0x{:x}\", next_block_branch_taken_addr),\n                BasicBlockType::ConditionalJump {\n                    next_block_addr: Some(next_block_addr),\n                    next_block_branch_taken_addr: None,\n                    ..\n                } => format!(\"0x{:x}\", next_block_addr),\n                BasicBlockType::ConditionalJump {\n                    next_block_addr: None,\n                    next_block_branch_taken_addr: None,\n                    ..\n                } => format!(\"\"),\n                BasicBlockType::Normal {\n                    next_block_addr: Some(next_block_addr),\n                    ..\n                } => format!(\"0x{:x}\", next_block_addr),\n                BasicBlockType::Normal {\n                    next_block_addr: None,\n                    ..\n                } => format!(\"\"),\n                BasicBlockType::Exit => format!(\"\"),\n                BasicBlockType::AbsoluteEip => format!(\"\"),\n            }\n        );\n    }\n\n    let graph = control_flow::make_graph(&basic_blocks);\n    let mut structure = control_flow::loopify(&graph);\n\n    if print {\n        dbg_log!(\"before blockify:\");\n        for group in &structure {\n            dbg_log!(\"=> Group\");\n            group.print(0);\n        }\n    }\n\n    control_flow::blockify(&mut structure, &graph);\n\n    if cfg!(debug_assertions) {\n        control_flow::assert_invariants(&structure);\n    }\n\n    if print {\n        dbg_log!(\"after blockify:\");\n        for group in &structure {\n            dbg_log!(\"=> Group\");\n            group.print(0);\n        }\n    }\n\n    if ctx.wasm_table_index_free_list.is_empty() {\n        dbg_log!(\"wasm_table_index_free_list empty, clearing cache\");\n\n        // When no free slots are available, delete all cached modules. We could increase the\n        // size of the table, but this way the initial size acts as an upper bound for the\n        // number of wasm modules that we generate, which we want anyway to avoid getting our\n        // tab killed by browsers due to memory constraints.\n        jit_clear_cache(ctx);\n\n        profiler::stat_increment(stat::INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES);\n\n        dbg_log!(\n            \"after jit_clear_cache: {} free\",\n            ctx.wasm_table_index_free_list.len(),\n        );\n\n        // This assertion can fail if all entries are pending (not possible unless\n        // WASM_TABLE_SIZE is set very low)\n        dbg_assert!(!ctx.wasm_table_index_free_list.is_empty());\n    }\n\n    // allocate an index in the wasm table\n    let wasm_table_index = ctx\n        .wasm_table_index_free_list\n        .pop()\n        .expect(\"allocate wasm table index\");\n    dbg_assert!(wasm_table_index != WasmTableIndex(0));\n\n    dbg_assert!(!pages.is_empty());\n    dbg_assert!(pages.len() <= unsafe { MAX_PAGES } as usize);\n\n    let basic_block_by_addr: HashMap<u32, BasicBlock> =\n        basic_blocks.into_iter().map(|b| (b.addr, b)).collect();\n\n    let entries = jit_generate_module(\n        structure,\n        &basic_block_by_addr,\n        cpu,\n        &mut ctx.wasm_builder,\n        wasm_table_index,\n        state_flags,\n    );\n    dbg_assert!(!entries.is_empty());\n\n    let mut page_info = HashMap::new();\n    for &(addr, state) in &entries {\n        let code = page_info\n            .entry(Page::page_of(addr))\n            .or_insert_with(|| PageInfo {\n                wasm_table_index,\n                state_flags,\n                entry_points: Vec::new(),\n                hidden_wasm_table_indices: Vec::new(),\n            });\n        code.entry_points.push((addr as u16 & 0xFFF, state));\n    }\n\n    profiler::stat_increment_by(\n        stat::COMPILE_WASM_TOTAL_BYTES,\n        ctx.wasm_builder.get_output_len() as u64,\n    );\n    profiler::stat_increment_by(stat::COMPILE_PAGE, pages.len() as u64);\n\n    for &p in &pages {\n        ctx.entry_points\n            .entry(p)\n            .or_insert_with(|| (0, HashSet::new()));\n    }\n\n    cpu::tlb_set_has_code_multiple(&pages, true);\n\n    dbg_assert!(ctx.compiling.is_none());\n    ctx.compiling = Some((\n        wasm_table_index,\n        CompilingPageState::Compiling { pages: page_info },\n    ));\n\n    let phys_addr = page.to_address();\n\n    // will call codegen_finalize_finished asynchronously when finished\n    codegen_finalize(\n        wasm_table_index,\n        phys_addr,\n        state_flags,\n        ctx.wasm_builder.get_output_ptr() as u32,\n        ctx.wasm_builder.get_output_len(),\n    );\n\n    check_jit_state_invariants(ctx);\n}\n\n#[no_mangle]\npub fn codegen_finalize_finished(\n    wasm_table_index: WasmTableIndex,\n    phys_addr: u32,\n    state_flags: CachedStateFlags,\n) {\n    let mut ctx = get_jit_state();\n\n    dbg_assert!(wasm_table_index != WasmTableIndex(0));\n\n    dbg_log!(\n        \"Finished compiling for page at {:x}\",\n        Page::page_of(phys_addr).to_address()\n    );\n\n    let pages = match mem::replace(&mut ctx.compiling, None) {\n        None => {\n            dbg_assert!(false);\n            return;\n        },\n        Some((in_progress_wasm_table_index, CompilingPageState::CompilingWritten)) => {\n            dbg_assert!(wasm_table_index == in_progress_wasm_table_index);\n\n            profiler::stat_increment(stat::INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED);\n            free_wasm_table_index(&mut ctx, wasm_table_index);\n            check_jit_state_invariants(&mut ctx);\n            return;\n        },\n        Some((in_progress_wasm_table_index, CompilingPageState::Compiling { pages })) => {\n            dbg_assert!(wasm_table_index == in_progress_wasm_table_index);\n            dbg_assert!(!pages.is_empty());\n            pages\n        },\n    };\n\n    for i in 0..unsafe { cpu::valid_tlb_entries_count } {\n        let page = unsafe { cpu::valid_tlb_entries[i as usize] };\n        let entry = unsafe { cpu::tlb_data[page as usize] };\n        if 0 != entry {\n            let tlb_physical_page = Page::of_u32(\n                (entry as u32 >> 12 ^ page as u32) - (unsafe { memory::mem8 } as u32 >> 12),\n            );\n            if let Some(info) = pages.get(&tlb_physical_page) {\n                set_tlb_code(\n                    Page::of_u32(page as u32),\n                    wasm_table_index,\n                    &info.entry_points,\n                    state_flags,\n                );\n            }\n        }\n    }\n\n    let mut check_for_unused_wasm_table_index = HashSet::new();\n\n    for (page, mut info) in pages {\n        if let Some(old_entry) = ctx.pages.remove(&page) {\n            info.hidden_wasm_table_indices\n                .extend(old_entry.hidden_wasm_table_indices);\n            info.hidden_wasm_table_indices\n                .push(old_entry.wasm_table_index);\n            check_for_unused_wasm_table_index.insert(old_entry.wasm_table_index);\n        }\n        ctx.pages.insert(page, info);\n    }\n\n    let unused: Vec<&WasmTableIndex> = check_for_unused_wasm_table_index\n        .iter()\n        .filter(|&&i| ctx.pages.values().all(|page| page.wasm_table_index != i))\n        .collect();\n\n    for &index in unused {\n        for p in ctx.pages.values_mut() {\n            p.hidden_wasm_table_indices.retain(|&w| w != index);\n        }\n\n        dbg_log!(\"unused after overwrite {}\", index.to_u16());\n        profiler::stat_increment(stat::INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE);\n        free_wasm_table_index(&mut ctx, index);\n    }\n\n    check_jit_state_invariants(&mut ctx);\n}\n\npub fn update_tlb_code(virt_page: Page, phys_page: Page) {\n    let ctx = get_jit_state();\n\n    match ctx.pages.get(&phys_page) {\n        Some(PageInfo {\n            wasm_table_index,\n            entry_points,\n            state_flags,\n            hidden_wasm_table_indices: _,\n        }) => set_tlb_code(virt_page, *wasm_table_index, entry_points, *state_flags),\n        None => cpu::clear_tlb_code(phys_page.to_u32() as i32),\n    };\n}\n\npub fn set_tlb_code(\n    virt_page: Page,\n    wasm_table_index: WasmTableIndex,\n    entries: &Vec<(u16, u16)>,\n    state_flags: CachedStateFlags,\n) {\n    let c = match unsafe { cpu::tlb_code[virt_page.to_u32() as usize] } {\n        None => {\n            let state_table = [u16::MAX; 0x1000];\n            unsafe {\n                let mut c = NonNull::new_unchecked(Box::into_raw(Box::new(cpu::Code {\n                    wasm_table_index,\n                    state_flags,\n                    state_table,\n                })));\n                cpu::tlb_code[virt_page.to_u32() as usize] = Some(c);\n                c.as_mut()\n            }\n        },\n        Some(mut c) => unsafe {\n            let c = c.as_mut();\n            c.state_table.fill(u16::MAX);\n            c.state_flags = state_flags;\n            c.wasm_table_index = wasm_table_index;\n            c\n        },\n    };\n\n    for &(addr, state) in entries {\n        dbg_assert!(state != u16::MAX);\n        c.state_table[addr as usize] = state;\n    }\n}\n\nfn jit_generate_module(\n    structure: Vec<WasmStructure>,\n    basic_blocks: &HashMap<u32, BasicBlock>,\n    mut cpu: CpuContext,\n    builder: &mut WasmBuilder,\n    wasm_table_index: WasmTableIndex,\n    state_flags: CachedStateFlags,\n) -> Vec<(u32, u16)> {\n    builder.reset();\n\n    let mut register_locals = (0..8)\n        .map(|i| {\n            builder.load_fixed_i32(global_pointers::get_reg32_offset(i));\n            builder.set_new_local()\n        })\n        .collect();\n\n    builder.const_i32(0);\n    let instruction_counter = builder.set_new_local();\n\n    let exit_label = builder.block_void();\n    let exit_with_fault_label = builder.block_void();\n    let main_loop_label = builder.loop_void();\n    if unsafe { JIT_USE_LOOP_SAFETY } {\n        builder.get_local(&instruction_counter);\n        builder.const_i32(cpu::LOOP_COUNTER);\n        builder.geu_i32();\n        if cfg!(feature = \"profiler\") {\n            builder.if_void();\n            codegen::gen_debug_track_jit_exit(builder, 0);\n            builder.br(exit_label);\n            builder.block_end();\n        }\n        else {\n            builder.br_if(exit_label);\n        }\n    }\n    let brtable_default = builder.block_void();\n\n    let ctx = &mut JitContext {\n        cpu: &mut cpu,\n        builder,\n        register_locals: &mut register_locals,\n        start_of_current_instruction: 0,\n        exit_with_fault_label,\n        exit_label,\n        current_instruction: Instruction::Other,\n        previous_instruction: Instruction::Other,\n        instruction_counter,\n    };\n\n    let entry_blocks = {\n        let mut nodes = &structure;\n        let result;\n        loop {\n            match &nodes[0] {\n                WasmStructure::Dispatcher(e) => {\n                    result = e.clone();\n                    break;\n                },\n                WasmStructure::Loop { .. } => {\n                    dbg_assert!(false);\n                },\n                WasmStructure::BasicBlock(_) => {\n                    dbg_assert!(false);\n                },\n                // Note: We could use these blocks as entry points, which will yield\n                // more entries for free, but it requires adding those to the dispatcher\n                // It's to be investigated if this yields a performance improvement\n                // See also the comment at the bottom of this function when creating entry\n                // points\n                WasmStructure::Block(children) => {\n                    nodes = children;\n                },\n            }\n        }\n        result\n    };\n\n    let mut index_for_addr = HashMap::new();\n    for (i, &addr) in entry_blocks.iter().enumerate() {\n        dbg_assert!(i < 0x10000);\n        index_for_addr.insert(addr, i as u16);\n    }\n    for b in basic_blocks.values() {\n        if !index_for_addr.contains_key(&b.addr) {\n            let i = index_for_addr.len();\n            dbg_assert!(i < 0x10000);\n            index_for_addr.insert(b.addr, i as u16);\n        }\n    }\n\n    let mut label_for_addr: HashMap<u32, (Label, Option<u16>)> = HashMap::new();\n\n    enum Work {\n        WasmStructure(WasmStructure),\n        BlockEnd {\n            label: Label,\n            targets: Vec<u32>,\n            olds: HashMap<u32, (Label, Option<u16>)>,\n        },\n        LoopEnd {\n            label: Label,\n            entries: Vec<u32>,\n            olds: HashMap<u32, (Label, Option<u16>)>,\n        },\n    }\n    let mut work: VecDeque<Work> = structure\n        .into_iter()\n        .map(|x| Work::WasmStructure(x))\n        .collect();\n\n    while let Some(block) = work.pop_front() {\n        let next_addr: Option<Vec<u32>> = work.iter().find_map(|x| match x {\n            Work::WasmStructure(l) => Some(l.head().collect()),\n            _ => None,\n        });\n        let target_block = &ctx.builder.arg_local_initial_state.unsafe_clone();\n\n        match block {\n            Work::WasmStructure(WasmStructure::BasicBlock(addr)) => {\n                let block = basic_blocks.get(&addr).unwrap();\n                jit_generate_basic_block(ctx, block);\n\n                if block.has_sti {\n                    match block.ty {\n                        BasicBlockType::ConditionalJump {\n                            condition,\n                            jump_offset,\n                            jump_offset_is_32,\n                            ..\n                        } => {\n                            codegen::gen_set_eip_low_bits(\n                                ctx.builder,\n                                block.end_addr as i32 & 0xFFF,\n                            );\n                            codegen::gen_condition_fn(ctx, condition);\n                            ctx.builder.if_void();\n                            if jump_offset_is_32 {\n                                codegen::gen_relative_jump(ctx.builder, jump_offset);\n                            }\n                            else {\n                                codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                            }\n                            ctx.builder.block_end();\n                        },\n                        BasicBlockType::Normal {\n                            jump_offset,\n                            jump_offset_is_32,\n                            ..\n                        } => {\n                            if jump_offset_is_32 {\n                                codegen::gen_set_eip_low_bits_and_jump_rel32(\n                                    ctx.builder,\n                                    block.end_addr as i32 & 0xFFF,\n                                    jump_offset,\n                                );\n                            }\n                            else {\n                                codegen::gen_set_eip_low_bits(\n                                    ctx.builder,\n                                    block.end_addr as i32 & 0xFFF,\n                                );\n                                codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                            }\n                        },\n                        BasicBlockType::Exit => {},\n                        BasicBlockType::AbsoluteEip => {},\n                    };\n                    codegen::gen_debug_track_jit_exit(ctx.builder, block.last_instruction_addr);\n                    codegen::gen_move_registers_from_locals_to_memory(ctx);\n                    codegen::gen_fn0_const(ctx.builder, \"handle_irqs\");\n                    codegen::gen_update_instruction_counter(ctx);\n                    ctx.builder.return_();\n                    continue;\n                }\n\n                match &block.ty {\n                    BasicBlockType::Exit => {\n                        // Exit this function\n                        codegen::gen_debug_track_jit_exit(ctx.builder, block.last_instruction_addr);\n                        codegen::gen_profiler_stat_increment(ctx.builder, stat::DIRECT_EXIT);\n                        ctx.builder.br(ctx.exit_label);\n                    },\n                    BasicBlockType::AbsoluteEip => {\n                        // Check if we can stay in this module, if not exit\n                        codegen::gen_get_eip(ctx.builder);\n                        ctx.builder.const_i32(wasm_table_index.to_u16() as i32);\n                        ctx.builder.const_i32(state_flags.to_u32() as i32);\n                        ctx.builder.call_fn3_ret(\"jit_find_cache_entry_in_page\");\n                        ctx.builder.tee_local(target_block);\n                        ctx.builder.const_i32(0);\n                        ctx.builder.ge_i32();\n                        // TODO: Could make this unconditional by including exit_label in the main br_table\n                        ctx.builder.br_if(main_loop_label);\n\n                        codegen::gen_debug_track_jit_exit(ctx.builder, block.last_instruction_addr);\n                        ctx.builder.br(ctx.exit_label);\n                    },\n                    &BasicBlockType::Normal {\n                        next_block_addr: None,\n                        jump_offset,\n                        jump_offset_is_32,\n                    } => {\n                        if jump_offset_is_32 {\n                            codegen::gen_set_eip_low_bits_and_jump_rel32(\n                                ctx.builder,\n                                block.end_addr as i32 & 0xFFF,\n                                jump_offset,\n                            );\n                        }\n                        else {\n                            codegen::gen_set_eip_low_bits(\n                                ctx.builder,\n                                block.end_addr as i32 & 0xFFF,\n                            );\n                            codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                        }\n\n                        codegen::gen_debug_track_jit_exit(ctx.builder, block.last_instruction_addr);\n                        codegen::gen_profiler_stat_increment(ctx.builder, stat::DIRECT_EXIT);\n                        ctx.builder.br(ctx.exit_label);\n                    },\n                    &BasicBlockType::Normal {\n                        next_block_addr: Some(next_block_addr),\n                        jump_offset,\n                        jump_offset_is_32,\n                    } => {\n                        // Unconditional jump to next basic block\n                        // - All instructions that don't change eip\n                        // - Unconditional jumps\n\n                        if Page::page_of(next_block_addr) != Page::page_of(block.addr) {\n                            if jump_offset_is_32 {\n                                codegen::gen_set_eip_low_bits_and_jump_rel32(\n                                    ctx.builder,\n                                    block.end_addr as i32 & 0xFFF,\n                                    jump_offset,\n                                );\n                            }\n                            else {\n                                codegen::gen_set_eip_low_bits(\n                                    ctx.builder,\n                                    block.end_addr as i32 & 0xFFF,\n                                );\n                                codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                            }\n\n                            codegen::gen_profiler_stat_increment(\n                                ctx.builder,\n                                stat::NORMAL_PAGE_CHANGE,\n                            );\n\n                            codegen::gen_page_switch_check(\n                                ctx,\n                                next_block_addr,\n                                block.last_instruction_addr,\n                            );\n\n                            #[cfg(debug_assertions)]\n                            codegen::gen_fn2_const(\n                                ctx.builder,\n                                \"check_page_switch\",\n                                block.addr,\n                                next_block_addr,\n                            );\n                        }\n\n                        if next_addr\n                            .as_ref()\n                            .map_or(false, |n| n.contains(&next_block_addr))\n                        {\n                            // Blocks are consecutive\n                            if next_addr.unwrap().len() > 1 {\n                                let target_index = *index_for_addr.get(&next_block_addr).unwrap();\n                                if cfg!(feature = \"profiler\") {\n                                    ctx.builder.const_i32(target_index.into());\n                                    ctx.builder.call_fn1(\"debug_set_dispatcher_target\");\n                                }\n                                ctx.builder.const_i32(target_index.into());\n                                ctx.builder.set_local(target_block);\n                                codegen::gen_profiler_stat_increment(\n                                    ctx.builder,\n                                    stat::NORMAL_FALLTHRU_WITH_TARGET_BLOCK,\n                                );\n                            }\n                            else {\n                                codegen::gen_profiler_stat_increment(\n                                    ctx.builder,\n                                    stat::NORMAL_FALLTHRU,\n                                );\n                            }\n                        }\n                        else {\n                            let &(br, target_index) = label_for_addr.get(&next_block_addr).unwrap();\n                            if let Some(target_index) = target_index {\n                                if cfg!(feature = \"profiler\") {\n                                    ctx.builder.const_i32(target_index.into());\n                                    ctx.builder.call_fn1(\"debug_set_dispatcher_target\");\n                                }\n                                ctx.builder.const_i32(target_index.into());\n                                ctx.builder.set_local(target_block);\n                                codegen::gen_profiler_stat_increment(\n                                    ctx.builder,\n                                    stat::NORMAL_BRANCH_WITH_TARGET_BLOCK,\n                                );\n                            }\n                            else {\n                                codegen::gen_profiler_stat_increment(\n                                    ctx.builder,\n                                    stat::NORMAL_BRANCH,\n                                );\n                            }\n                            ctx.builder.br(br);\n                        }\n                    },\n                    &BasicBlockType::ConditionalJump {\n                        next_block_addr,\n                        next_block_branch_taken_addr,\n                        condition,\n                        jump_offset,\n                        jump_offset_is_32,\n                    } => {\n                        // Conditional jump to next basic block\n                        // - jnz, jc, loop, jcxz, etc.\n\n                        // Generate:\n                        // (1) condition()\n                        // (2) br_if()\n                        // (3) br()\n                        // Except:\n                        // If we need to update eip in case (2), it's replaced by if { update_eip(); br() }\n                        // If case (3) can fall through to the next basic block, the branch is eliminated\n                        // Dispatcher target writes can be generated in either case\n                        // Condition may be inverted if it helps generate a fallthrough instead of the second branch\n\n                        codegen::gen_profiler_stat_increment(ctx.builder, stat::CONDITIONAL_JUMP);\n\n                        #[derive(PartialEq)]\n                        enum Case {\n                            BranchTaken,\n                            BranchNotTaken,\n                        }\n\n                        let mut handle_case = |case: Case, is_first| {\n                            // first case generates condition and *has* to branch away,\n                            // second case branches unconditionally or falls through\n\n                            if is_first {\n                                if case == Case::BranchNotTaken {\n                                    codegen::gen_condition_fn_negated(ctx, condition);\n                                }\n                                else {\n                                    codegen::gen_condition_fn(ctx, condition);\n                                }\n                            }\n\n                            let next_block_addr = if case == Case::BranchTaken {\n                                next_block_branch_taken_addr\n                            }\n                            else {\n                                next_block_addr\n                            };\n\n                            if let Some(next_block_addr) = next_block_addr {\n                                if Page::page_of(next_block_addr) != Page::page_of(block.addr) {\n                                    dbg_assert!(case == Case::BranchTaken); // currently not possible in other case\n                                    if is_first {\n                                        ctx.builder.if_i32();\n                                    }\n                                    if jump_offset_is_32 {\n                                        codegen::gen_set_eip_low_bits_and_jump_rel32(\n                                            ctx.builder,\n                                            block.end_addr as i32 & 0xFFF,\n                                            jump_offset,\n                                        );\n                                    }\n                                    else {\n                                        codegen::gen_set_eip_low_bits(\n                                            ctx.builder,\n                                            block.end_addr as i32 & 0xFFF,\n                                        );\n                                        codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                                    }\n\n                                    codegen::gen_profiler_stat_increment(\n                                        ctx.builder,\n                                        stat::CONDITIONAL_JUMP_PAGE_CHANGE,\n                                    );\n                                    codegen::gen_page_switch_check(\n                                        ctx,\n                                        next_block_addr,\n                                        block.last_instruction_addr,\n                                    );\n\n                                    #[cfg(debug_assertions)]\n                                    codegen::gen_fn2_const(\n                                        ctx.builder,\n                                        \"check_page_switch\",\n                                        block.addr,\n                                        next_block_addr,\n                                    );\n\n                                    if is_first {\n                                        ctx.builder.const_i32(1);\n                                        ctx.builder.else_();\n                                        ctx.builder.const_i32(0);\n                                        ctx.builder.block_end();\n                                    }\n                                }\n\n                                if next_addr\n                                    .as_ref()\n                                    .map_or(false, |n| n.contains(&next_block_addr))\n                                {\n                                    // blocks are consecutive\n\n                                    // fallthrough, has to be second\n                                    dbg_assert!(!is_first);\n\n                                    if next_addr.as_ref().unwrap().len() > 1 {\n                                        let target_index =\n                                            *index_for_addr.get(&next_block_addr).unwrap();\n                                        if cfg!(feature = \"profiler\") {\n                                            ctx.builder.const_i32(target_index.into());\n                                            ctx.builder.call_fn1(\"debug_set_dispatcher_target\");\n                                        }\n                                        ctx.builder.const_i32(target_index.into());\n                                        ctx.builder.set_local(target_block);\n                                        codegen::gen_profiler_stat_increment(\n                                            ctx.builder,\n                                            stat::CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK,\n                                        );\n                                    }\n                                    else {\n                                        codegen::gen_profiler_stat_increment(\n                                            ctx.builder,\n                                            stat::CONDITIONAL_JUMP_FALLTHRU,\n                                        );\n                                    }\n                                }\n                                else {\n                                    let &(br, target_index) =\n                                        label_for_addr.get(&next_block_addr).unwrap();\n                                    if let Some(target_index) = target_index {\n                                        if cfg!(feature = \"profiler\") {\n                                            // Note: Currently called unconditionally, even if the\n                                            // br_if below doesn't branch\n                                            ctx.builder.const_i32(target_index.into());\n                                            ctx.builder.call_fn1(\"debug_set_dispatcher_target\");\n                                        }\n                                        ctx.builder.const_i32(target_index.into());\n                                        ctx.builder.set_local(target_block);\n                                    }\n\n                                    if is_first {\n                                        if cfg!(feature = \"profiler\") {\n                                            ctx.builder.if_void();\n                                            codegen::gen_profiler_stat_increment(\n                                                ctx.builder,\n                                                if target_index.is_some() {\n                                                    stat::CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK\n                                                }\n                                                else {\n                                                    stat::CONDITIONAL_JUMP_BRANCH\n                                                },\n                                            );\n                                            ctx.builder.br(br);\n                                            ctx.builder.block_end();\n                                        }\n                                        else {\n                                            ctx.builder.br_if(br);\n                                        }\n                                    }\n                                    else {\n                                        codegen::gen_profiler_stat_increment(\n                                            ctx.builder,\n                                            if target_index.is_some() {\n                                                stat::CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK\n                                            }\n                                            else {\n                                                stat::CONDITIONAL_JUMP_BRANCH\n                                            },\n                                        );\n                                        ctx.builder.br(br);\n                                    }\n                                }\n                            }\n                            else {\n                                // target is outside of this module, update eip and exit\n                                if is_first {\n                                    ctx.builder.if_void();\n                                }\n\n                                if case == Case::BranchTaken {\n                                    if jump_offset_is_32 {\n                                        codegen::gen_set_eip_low_bits_and_jump_rel32(\n                                            ctx.builder,\n                                            block.end_addr as i32 & 0xFFF,\n                                            jump_offset,\n                                        );\n                                    }\n                                    else {\n                                        codegen::gen_set_eip_low_bits(\n                                            ctx.builder,\n                                            block.end_addr as i32 & 0xFFF,\n                                        );\n                                        codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                                    }\n                                }\n                                else {\n                                    codegen::gen_set_eip_low_bits(\n                                        ctx.builder,\n                                        block.end_addr as i32 & 0xFFF,\n                                    );\n                                }\n\n                                codegen::gen_debug_track_jit_exit(\n                                    ctx.builder,\n                                    block.last_instruction_addr,\n                                );\n                                codegen::gen_profiler_stat_increment(\n                                    ctx.builder,\n                                    stat::CONDITIONAL_JUMP_EXIT,\n                                );\n                                ctx.builder.br(ctx.exit_label);\n\n                                if is_first {\n                                    ctx.builder.block_end();\n                                }\n                            }\n                        };\n\n                        let branch_taken_is_fallthrough = next_block_branch_taken_addr\n                            .map_or(false, |addr| {\n                                next_addr.as_ref().map_or(false, |n| n.contains(&addr))\n                            });\n                        let branch_not_taken_is_fallthrough = next_block_addr\n                            .map_or(false, |addr| {\n                                next_addr.as_ref().map_or(false, |n| n.contains(&addr))\n                            });\n\n                        if branch_not_taken_is_fallthrough && branch_taken_is_fallthrough {\n                            let next_block_addr = next_block_addr.unwrap();\n                            let next_block_branch_taken_addr =\n                                next_block_branch_taken_addr.unwrap();\n\n                            dbg_log!(\n                                \"Conditional control flow: fallthrough in both cases, page_switch={} next_is_multi={}\",\n                                Page::page_of(next_block_branch_taken_addr)\n                                    != Page::page_of(block.addr),\n                                next_addr.as_ref().unwrap().len() > 1,\n                            );\n\n                            dbg_assert!(\n                                Page::page_of(next_block_addr) == Page::page_of(block.addr)\n                            ); // currently not possible\n\n                            if Page::page_of(next_block_branch_taken_addr)\n                                != Page::page_of(block.addr)\n                            {\n                                codegen::gen_condition_fn(ctx, condition);\n                                ctx.builder.if_void();\n\n                                if jump_offset_is_32 {\n                                    codegen::gen_set_eip_low_bits_and_jump_rel32(\n                                        ctx.builder,\n                                        block.end_addr as i32 & 0xFFF,\n                                        jump_offset,\n                                    );\n                                }\n                                else {\n                                    codegen::gen_set_eip_low_bits(\n                                        ctx.builder,\n                                        block.end_addr as i32 & 0xFFF,\n                                    );\n                                    codegen::gen_jmp_rel16(ctx.builder, jump_offset as u16);\n                                }\n\n                                codegen::gen_profiler_stat_increment(\n                                    ctx.builder,\n                                    stat::CONDITIONAL_JUMP_PAGE_CHANGE,\n                                );\n                                codegen::gen_page_switch_check(\n                                    ctx,\n                                    next_block_branch_taken_addr,\n                                    block.last_instruction_addr,\n                                );\n\n                                #[cfg(debug_assertions)]\n                                codegen::gen_fn2_const(\n                                    ctx.builder,\n                                    \"check_page_switch\",\n                                    block.addr,\n                                    next_block_branch_taken_addr,\n                                );\n\n                                dbg_assert!(next_addr.unwrap().len() > 1);\n\n                                let target_index_taken =\n                                    *index_for_addr.get(&next_block_branch_taken_addr).unwrap();\n                                let target_index_not_taken =\n                                    *index_for_addr.get(&next_block_addr).unwrap();\n\n                                ctx.builder.const_i32(target_index_taken.into());\n                                ctx.builder.set_local(target_block);\n\n                                ctx.builder.else_();\n                                ctx.builder.const_i32(target_index_not_taken.into());\n                                ctx.builder.set_local(target_block);\n\n                                ctx.builder.block_end();\n                            }\n                            else if next_addr.unwrap().len() > 1 {\n                                let target_index_taken =\n                                    *index_for_addr.get(&next_block_branch_taken_addr).unwrap();\n                                let target_index_not_taken =\n                                    *index_for_addr.get(&next_block_addr).unwrap();\n\n                                codegen::gen_condition_fn(ctx, condition);\n                                ctx.builder.if_i32();\n                                ctx.builder.const_i32(target_index_taken.into());\n                                ctx.builder.else_();\n                                ctx.builder.const_i32(target_index_not_taken.into());\n                                ctx.builder.block_end();\n                                ctx.builder.set_local(target_block);\n                            }\n                        }\n                        else if branch_taken_is_fallthrough {\n                            handle_case(Case::BranchNotTaken, true);\n                            handle_case(Case::BranchTaken, false);\n                        }\n                        else {\n                            handle_case(Case::BranchTaken, true);\n                            handle_case(Case::BranchNotTaken, false);\n                        }\n                    },\n                }\n            },\n            Work::WasmStructure(WasmStructure::Dispatcher(entries)) => {\n                profiler::stat_increment(stat::COMPILE_DISPATCHER);\n\n                if cfg!(feature = \"profiler\") {\n                    ctx.builder.get_local(target_block);\n                    ctx.builder.const_i32(index_for_addr.len() as i32);\n                    ctx.builder.call_fn2(\"check_dispatcher_target\");\n                }\n\n                if entries.len() > BRTABLE_CUTOFF {\n                    // generate a brtable\n                    codegen::gen_profiler_stat_increment(ctx.builder, stat::DISPATCHER_LARGE);\n                    let mut cases = Vec::new();\n                    for &addr in &entries {\n                        let &(label, target_index) = label_for_addr.get(&addr).unwrap();\n                        let &index = index_for_addr.get(&addr).unwrap();\n                        dbg_assert!(target_index.is_none() || target_index == Some(index));\n                        while index as usize >= cases.len() {\n                            cases.push(brtable_default);\n                        }\n                        cases[index as usize] = label;\n                    }\n                    ctx.builder.get_local(target_block);\n                    ctx.builder.brtable(brtable_default, &mut cases.iter());\n                }\n                else {\n                    // generate a if target == block.addr then br block.label ...\n                    codegen::gen_profiler_stat_increment(ctx.builder, stat::DISPATCHER_SMALL);\n                    let nexts: HashSet<u32> = next_addr\n                        .as_ref()\n                        .map_or(HashSet::new(), |nexts| nexts.iter().copied().collect());\n                    for &addr in &entries {\n                        if nexts.contains(&addr) {\n                            continue;\n                        }\n                        let index = *index_for_addr.get(&addr).unwrap();\n                        let &(label, _) = label_for_addr.get(&addr).unwrap();\n                        ctx.builder.get_local(target_block);\n                        ctx.builder.const_i32(index.into());\n                        ctx.builder.eq_i32();\n                        ctx.builder.br_if(label);\n                    }\n                }\n            },\n            Work::WasmStructure(WasmStructure::Loop(children)) => {\n                profiler::stat_increment(stat::COMPILE_WASM_LOOP);\n\n                let entries: Vec<u32> = children[0].head().collect();\n                let label = ctx.builder.loop_void();\n                codegen::gen_profiler_stat_increment(ctx.builder, stat::LOOP);\n\n                if entries.len() == 1 {\n                    let addr = entries[0];\n                    codegen::gen_set_eip_low_bits(ctx.builder, addr as i32 & 0xFFF);\n                    profiler::stat_increment(stat::COMPILE_WITH_LOOP_SAFETY);\n                    codegen::gen_profiler_stat_increment(ctx.builder, stat::LOOP_SAFETY);\n                    if unsafe { JIT_USE_LOOP_SAFETY } {\n                        ctx.builder.get_local(&ctx.instruction_counter);\n                        ctx.builder.const_i32(cpu::LOOP_COUNTER);\n                        ctx.builder.geu_i32();\n                        if cfg!(feature = \"profiler\") {\n                            ctx.builder.if_void();\n                            codegen::gen_debug_track_jit_exit(ctx.builder, addr);\n                            ctx.builder.br(exit_label);\n                            ctx.builder.block_end();\n                        }\n                        else {\n                            ctx.builder.br_if(exit_label);\n                        }\n                    }\n                }\n\n                let mut olds = HashMap::new();\n                for &target in entries.iter() {\n                    let index = if entries.len() == 1 {\n                        None\n                    }\n                    else {\n                        Some(*index_for_addr.get(&target).unwrap())\n                    };\n                    let old = label_for_addr.insert(target, (label, index));\n                    if let Some(old) = old {\n                        olds.insert(target, old);\n                    }\n                }\n\n                work.push_front(Work::LoopEnd {\n                    label,\n                    entries,\n                    olds,\n                });\n                for c in children.into_iter().rev() {\n                    work.push_front(Work::WasmStructure(c));\n                }\n            },\n            Work::LoopEnd {\n                label,\n                entries,\n                olds,\n            } => {\n                for target in entries {\n                    let old = label_for_addr.remove(&target);\n                    dbg_assert!(old.map(|(l, _)| l) == Some(label));\n                }\n                for (target, old) in olds {\n                    let old = label_for_addr.insert(target, old);\n                    dbg_assert!(old.is_none());\n                }\n\n                ctx.builder.block_end();\n            },\n            Work::WasmStructure(WasmStructure::Block(children)) => {\n                profiler::stat_increment(stat::COMPILE_WASM_BLOCK);\n\n                let targets = next_addr.clone().unwrap();\n                let label = ctx.builder.block_void();\n                let mut olds = HashMap::new();\n                for &target in targets.iter() {\n                    let index = if targets.len() == 1 {\n                        None\n                    }\n                    else {\n                        Some(*index_for_addr.get(&target).unwrap())\n                    };\n                    let old = label_for_addr.insert(target, (label, index));\n                    if let Some(old) = old {\n                        olds.insert(target, old);\n                    }\n                }\n\n                work.push_front(Work::BlockEnd {\n                    label,\n                    targets,\n                    olds,\n                });\n                for c in children.into_iter().rev() {\n                    work.push_front(Work::WasmStructure(c));\n                }\n            },\n            Work::BlockEnd {\n                label,\n                targets,\n                olds,\n            } => {\n                for target in targets {\n                    let old = label_for_addr.remove(&target);\n                    dbg_assert!(old.map(|(l, _)| l) == Some(label));\n                }\n                for (target, old) in olds {\n                    let old = label_for_addr.insert(target, old);\n                    dbg_assert!(old.is_none());\n                }\n\n                ctx.builder.block_end();\n            },\n        }\n    }\n\n    dbg_assert!(label_for_addr.is_empty());\n\n    {\n        ctx.builder.block_end(); // default case for the brtable\n        ctx.builder.unreachable();\n    }\n    {\n        ctx.builder.block_end(); // main loop\n    }\n    {\n        // exit-with-fault case\n        ctx.builder.block_end();\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        codegen::gen_fn0_const(ctx.builder, \"trigger_fault_end_jit\");\n        codegen::gen_update_instruction_counter(ctx);\n        ctx.builder.return_();\n    }\n    {\n        // exit\n        ctx.builder.block_end();\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        codegen::gen_update_instruction_counter(ctx);\n    }\n\n    for local in ctx.register_locals.drain(..) {\n        ctx.builder.free_local(local);\n    }\n    ctx.builder\n        .free_local(ctx.instruction_counter.unsafe_clone());\n\n    ctx.builder.finish();\n\n    let entries = Vec::from_iter(entry_blocks.iter().map(|addr| {\n        let block = basic_blocks.get(&addr).unwrap();\n        let index = *index_for_addr.get(&addr).unwrap();\n\n        profiler::stat_increment(stat::COMPILE_ENTRY_POINT);\n\n        dbg_assert!(block.addr < block.end_addr);\n        // Note: We also insert blocks that weren't originally marked as entries here\n        //       This doesn't have any downside, besides making the hash table slightly larger\n\n        (block.addr, index)\n    }));\n\n    for b in basic_blocks.values() {\n        if b.is_entry_block {\n            dbg_assert!(entries.iter().find(|(addr, _)| *addr == b.addr).is_some());\n        }\n    }\n\n    return entries;\n}\n\nfn jit_generate_basic_block(ctx: &mut JitContext, block: &BasicBlock) {\n    let needs_eip_updated = match block.ty {\n        BasicBlockType::Exit => true,\n        _ => false,\n    };\n\n    profiler::stat_increment(stat::COMPILE_BASIC_BLOCK);\n\n    let start_addr = block.addr;\n    let last_instruction_addr = block.last_instruction_addr;\n    let stop_addr = block.end_addr;\n\n    // First iteration of do-while assumes the caller confirms this condition\n    dbg_assert!(!is_near_end_of_page(start_addr));\n\n    if cfg!(feature = \"profiler\") {\n        ctx.builder.const_i32(start_addr as i32);\n        ctx.builder.call_fn1(\"enter_basic_block\");\n    }\n\n    ctx.builder.get_local(&ctx.instruction_counter);\n    ctx.builder.const_i32(block.number_of_instructions as i32);\n    ctx.builder.add_i32();\n    ctx.builder.set_local(&ctx.instruction_counter);\n\n    ctx.cpu.eip = start_addr;\n    ctx.current_instruction = Instruction::Other;\n    ctx.previous_instruction = Instruction::Other;\n\n    loop {\n        let mut instruction = 0;\n        if cfg!(feature = \"profiler\") {\n            instruction = memory::read32s(ctx.cpu.eip) as u32;\n            opstats::gen_opstats(ctx.builder, instruction);\n            opstats::record_opstat_compiled(instruction);\n        }\n\n        if ctx.cpu.eip == last_instruction_addr {\n            // Before the last instruction:\n            // - Set eip to *after* the instruction\n            // - Set previous_eip to *before* the instruction\n            if needs_eip_updated {\n                codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n                    ctx.builder,\n                    last_instruction_addr as i32 & 0xFFF,\n                );\n                codegen::gen_set_eip_low_bits(ctx.builder, stop_addr as i32 & 0xFFF);\n            }\n        }\n\n        let wasm_length_before = ctx.builder.instruction_body_length();\n\n        ctx.start_of_current_instruction = ctx.cpu.eip;\n        let start_eip = ctx.cpu.eip;\n        let mut instruction_flags = 0;\n        jit_instructions::jit_instruction(ctx, &mut instruction_flags);\n        let end_eip = ctx.cpu.eip;\n\n        let instruction_length = end_eip - start_eip;\n        let was_block_boundary = instruction_flags & JIT_INSTR_BLOCK_BOUNDARY_FLAG != 0;\n\n        let wasm_length = ctx.builder.instruction_body_length() - wasm_length_before;\n        opstats::record_opstat_size_wasm(instruction, wasm_length as u64);\n\n        dbg_assert!((end_eip == stop_addr) == (start_eip == last_instruction_addr));\n        dbg_assert!(instruction_length < MAX_INSTRUCTION_LENGTH);\n\n        let end_addr = ctx.cpu.eip;\n\n        if end_addr == stop_addr {\n            // no page was crossed\n            dbg_assert!(Page::page_of(end_addr) == Page::page_of(start_addr));\n            break;\n        }\n\n        if was_block_boundary || is_near_end_of_page(end_addr) || end_addr > stop_addr {\n            dbg_log!(\n                \"Overlapping basic blocks start={:x} expected_end={:x} end={:x} was_block_boundary={} near_end_of_page={}\",\n                start_addr,\n                stop_addr,\n                end_addr,\n                was_block_boundary,\n                is_near_end_of_page(end_addr)\n            );\n            dbg_assert!(false);\n            break;\n        }\n\n        ctx.previous_instruction = mem::replace(&mut ctx.current_instruction, Instruction::Other);\n    }\n}\n\npub fn jit_increase_hotness_and_maybe_compile(\n    virt_address: i32,\n    phys_address: u32,\n    cs_offset: u32,\n    state_flags: CachedStateFlags,\n    heat: u32,\n) {\n    if unsafe { JIT_DISABLED } {\n        return;\n    }\n\n    let mut ctx = get_jit_state();\n    let is_compiling = ctx.compiling.is_some();\n    let page = Page::page_of(phys_address);\n    let (hotness, entry_points) = ctx.entry_points.entry(page).or_insert_with(|| {\n        cpu::tlb_set_has_code(page, true);\n        profiler::stat_increment(stat::RUN_INTERPRETED_NEW_PAGE);\n        (0, HashSet::new())\n    });\n\n    if !is_near_end_of_page(phys_address) {\n        entry_points.insert(phys_address as u16 & 0xFFF);\n    }\n\n    *hotness += heat;\n    if *hotness >= JIT_THRESHOLD {\n        if is_compiling {\n            return;\n        }\n        // only try generating if we're in the correct address space\n        if cpu::translate_address_read_no_side_effects(virt_address) == Ok(phys_address) {\n            *hotness = 0;\n            jit_analyze_and_generate(&mut ctx, virt_address, phys_address, cs_offset, state_flags)\n        }\n        else {\n            profiler::stat_increment(stat::COMPILE_WRONG_ADDRESS_SPACE);\n        }\n    }\n}\n\nfn free_wasm_table_index(ctx: &mut JitState, wasm_table_index: WasmTableIndex) {\n    if CHECK_JIT_STATE_INVARIANTS {\n        dbg_assert!(!ctx.wasm_table_index_free_list.contains(&wasm_table_index));\n\n        match &ctx.compiling {\n            Some((wasm_table_index_compiling, _)) => {\n                dbg_assert!(\n                    *wasm_table_index_compiling != wasm_table_index,\n                    \"Attempt to free wasm table index that is currently being compiled\"\n                );\n            },\n            _ => {},\n        }\n\n        dbg_assert!(!ctx\n            .pages\n            .values()\n            .any(|info| info.wasm_table_index == wasm_table_index));\n\n        dbg_assert!(!ctx\n            .pages\n            .values()\n            .any(|info| info.hidden_wasm_table_indices.contains(&wasm_table_index)));\n\n        for i in 0..unsafe { cpu::valid_tlb_entries_count } {\n            let page = unsafe { cpu::valid_tlb_entries[i as usize] };\n            unsafe {\n                match cpu::tlb_code[page as usize] {\n                    None => {},\n                    Some(c) => {\n                        let c = c.as_ref();\n                        dbg_assert!(c.wasm_table_index != wasm_table_index);\n                    },\n                }\n            }\n        }\n    }\n\n    ctx.wasm_table_index_free_list.push(wasm_table_index);\n\n    // It is not strictly necessary to clear the function, but it will fail more predictably if we\n    // accidentally use the function and may garbage collect unused modules earlier\n    jit_clear_func(wasm_table_index);\n}\n\n/// Register a write in this page: Delete all present code\nfn jit_dirty_page_ctx(ctx: &mut JitState, page: Page) {\n    let mut did_have_code = false;\n\n    if let Some(PageInfo {\n        wasm_table_index,\n        hidden_wasm_table_indices,\n        state_flags: _,\n        entry_points: _,\n    }) = ctx.pages.remove(&page)\n    {\n        profiler::stat_increment(stat::INVALIDATE_PAGE_HAD_CODE);\n        did_have_code = true;\n\n        free(ctx, wasm_table_index);\n        for wasm_table_index in hidden_wasm_table_indices {\n            free(ctx, wasm_table_index);\n        }\n\n        fn free(ctx: &mut JitState, wasm_table_index: WasmTableIndex) {\n            for i in 0..unsafe { cpu::valid_tlb_entries_count } {\n                let page = unsafe { cpu::valid_tlb_entries[i as usize] };\n                let entry = unsafe { cpu::tlb_data[page as usize] };\n                if 0 != entry {\n                    let tlb_physical_page = Page::of_u32(\n                        (entry as u32 >> 12 ^ page as u32) - (unsafe { memory::mem8 } as u32 >> 12),\n                    );\n                    match unsafe { cpu::tlb_code[page as usize] } {\n                        None => {},\n                        Some(c) => unsafe {\n                            let w = c.as_ref().wasm_table_index;\n                            if wasm_table_index == w {\n                                drop(Box::from_raw(c.as_ptr()));\n                                cpu::tlb_code[page as usize] = None;\n                                if !ctx.entry_points.contains_key(&tlb_physical_page) {\n                                    // XXX\n                                    cpu::tlb_data[page as usize] &= !cpu::TLB_HAS_CODE;\n                                }\n                            }\n                        },\n                    }\n                }\n            }\n\n            ctx.pages.retain(\n                |_,\n                 &mut PageInfo {\n                     wasm_table_index: w,\n                     ..\n                 }| w != wasm_table_index,\n            );\n\n            for info in ctx.pages.values_mut() {\n                info.hidden_wasm_table_indices\n                    .retain(|&w| w != wasm_table_index)\n            }\n\n            free_wasm_table_index(ctx, wasm_table_index);\n        }\n    }\n\n    match ctx.entry_points.remove(&page) {\n        None => {},\n        Some(_) => {\n            profiler::stat_increment(stat::INVALIDATE_PAGE_HAD_ENTRY_POINTS);\n            did_have_code = true;\n\n            match &ctx.compiling {\n                Some((index, CompilingPageState::Compiling { pages })) => {\n                    if pages.contains_key(&page) {\n                        ctx.compiling = Some((*index, CompilingPageState::CompilingWritten));\n                    }\n                },\n                _ => {},\n            }\n        },\n    }\n\n    match &ctx.compiling {\n        Some((_, CompilingPageState::Compiling { pages })) => {\n            dbg_assert!(!pages.contains_key(&page));\n        },\n        _ => {},\n    }\n\n    check_jit_state_invariants(ctx);\n\n    dbg_assert!(!jit_page_has_code_ctx(ctx, page));\n\n    if did_have_code {\n        cpu::tlb_set_has_code(page, false);\n    }\n\n    if !did_have_code {\n        profiler::stat_increment(stat::DIRTY_PAGE_DID_NOT_HAVE_CODE);\n    }\n}\n\n#[no_mangle]\npub fn jit_dirty_cache(start_addr: u32, end_addr: u32) {\n    dbg_assert!(start_addr < end_addr);\n\n    let start_page = Page::page_of(start_addr);\n    let end_page = Page::page_of(end_addr - 1);\n\n    for page in start_page.to_u32()..end_page.to_u32() + 1 {\n        jit_dirty_page_ctx(&mut get_jit_state(), Page::page_of(page << 12));\n    }\n}\n\n#[no_mangle]\npub fn jit_dirty_page(page: Page) { jit_dirty_page_ctx(&mut get_jit_state(), page) }\n\n/// dirty pages in the range of start_addr and end_addr, which must span at most two pages\npub fn jit_dirty_cache_small(start_addr: u32, end_addr: u32) {\n    dbg_assert!(start_addr < end_addr);\n\n    let start_page = Page::page_of(start_addr);\n    let end_page = Page::page_of(end_addr - 1);\n\n    let mut ctx = get_jit_state();\n    jit_dirty_page_ctx(&mut ctx, start_page);\n\n    // Note: This can't happen when paging is enabled, as writes across\n    //       boundaries are split up on two pages\n    if start_page != end_page {\n        dbg_assert!(start_page.to_u32() + 1 == end_page.to_u32());\n        jit_dirty_page_ctx(&mut ctx, end_page);\n    }\n}\n\n#[no_mangle]\npub fn jit_clear_cache_js() { jit_clear_cache(&mut get_jit_state()) }\n\nfn jit_clear_cache(ctx: &mut JitState) {\n    let mut pages_with_code = HashSet::new();\n\n    for &p in ctx.entry_points.keys() {\n        pages_with_code.insert(p);\n    }\n    for &p in ctx.pages.keys() {\n        pages_with_code.insert(p);\n    }\n\n    for page in pages_with_code {\n        jit_dirty_page_ctx(ctx, page);\n    }\n}\n\npub fn jit_page_has_code(page: Page) -> bool { jit_page_has_code_ctx(&mut get_jit_state(), page) }\n\nfn jit_page_has_code_ctx(ctx: &mut JitState, page: Page) -> bool {\n    ctx.pages.contains_key(&page) || ctx.entry_points.contains_key(&page)\n}\n\n#[no_mangle]\npub fn jit_get_wasm_table_index_free_list_count() -> u32 {\n    if cfg!(feature = \"profiler\") {\n        get_jit_state().wasm_table_index_free_list.len() as u32\n    }\n    else {\n        0\n    }\n}\n#[no_mangle]\npub fn jit_get_cache_size() -> u32 {\n    if cfg!(feature = \"profiler\") {\n        get_jit_state()\n            .pages\n            .values()\n            .map(|p| p.entry_points.len() as u32)\n            .sum()\n    }\n    else {\n        0\n    }\n}\n\n#[cfg(feature = \"profiler\")]\npub fn check_missed_entry_points(phys_address: u32, state_flags: CachedStateFlags) {\n    let ctx = get_jit_state();\n\n    if let Some(infos) = ctx.pages.get(&Page::page_of(phys_address)) {\n        if infos.state_flags != state_flags {\n            return;\n        }\n\n        #[allow(static_mut_refs)]\n        let last_jump_type = unsafe { cpu::debug_last_jump.name() };\n        #[allow(static_mut_refs)]\n        let last_jump_addr = unsafe { cpu::debug_last_jump.phys_address() }.unwrap_or(0);\n        let last_jump_opcode =\n            if last_jump_addr != 0 { memory::read32s(last_jump_addr) } else { 0 };\n\n        let opcode = memory::read32s(phys_address);\n        dbg_log!(\n            \"Compiled exists, but no entry point, \\\n                 phys_addr={:x} opcode={:02x} {:02x} {:02x} {:02x}. \\\n                 Last jump at {:x} ({}) opcode={:02x} {:02x} {:02x} {:02x}\",\n            phys_address,\n            opcode & 0xFF,\n            opcode >> 8 & 0xFF,\n            opcode >> 16 & 0xFF,\n            opcode >> 16 & 0xFF,\n            last_jump_addr,\n            last_jump_type,\n            last_jump_opcode & 0xFF,\n            last_jump_opcode >> 8 & 0xFF,\n            last_jump_opcode >> 16 & 0xFF,\n            last_jump_opcode >> 16 & 0xFF,\n        );\n    }\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub fn debug_set_dispatcher_target(_target_index: i32) {\n    //dbg_log!(\"About to call dispatcher target_index={}\", target_index);\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub fn check_dispatcher_target(target_index: i32, max: i32) {\n    //dbg_log!(\"Dispatcher called target={}\", target_index);\n    dbg_assert!(target_index >= 0);\n    dbg_assert!(target_index < max);\n}\n\n#[no_mangle]\n#[cfg(feature = \"profiler\")]\npub fn enter_basic_block(phys_eip: u32) {\n    let eip =\n        unsafe { cpu::translate_address_read(*global_pointers::instruction_pointer).unwrap() };\n    if Page::page_of(eip) != Page::page_of(phys_eip) {\n        dbg_log!(\n            \"enter basic block failed block=0x{:x} actual eip=0x{:x}\",\n            phys_eip,\n            eip\n        );\n        panic!();\n    }\n}\n\n#[no_mangle]\npub unsafe fn set_jit_config(index: u32, value: u32) {\n    match index {\n        0 => JIT_DISABLED = value != 0,\n        1 => MAX_PAGES = value,\n        2 => JIT_USE_LOOP_SAFETY = value != 0,\n        3 => MAX_EXTRA_BASIC_BLOCKS = value,\n        _ => dbg_assert!(false),\n    }\n}\n\n#[no_mangle]\npub unsafe fn get_jit_config(index: u32) -> u32 {\n    match index {\n        0 => JIT_DISABLED as u32,\n        1 => MAX_PAGES as u32,\n        2 => JIT_USE_LOOP_SAFETY as u32,\n        3 => MAX_EXTRA_BASIC_BLOCKS as u32,\n        _ => 0,\n    }\n}\n"
  },
  {
    "path": "src/rust/jit_instructions.rs",
    "content": "#![allow(non_snake_case)]\n\nuse crate::codegen;\nuse crate::codegen::{BitSize, ConditionNegate};\nuse crate::cpu::cpu::{\n    FLAGS_ALL, FLAGS_DEFAULT, FLAGS_MASK, FLAG_ADJUST, FLAG_CARRY, FLAG_DIRECTION, FLAG_INTERRUPT,\n    FLAG_IOPL, FLAG_OVERFLOW, FLAG_SUB, FLAG_VM, FLAG_ZERO, OPSIZE_16, OPSIZE_32, OPSIZE_8,\n};\nuse crate::cpu::global_pointers;\nuse crate::gen;\nuse crate::jit::{Instruction, InstructionOperand, InstructionOperandDest, JitContext};\nuse crate::modrm::{jit_add_seg_offset, jit_add_seg_offset_no_override, ModrmByte};\nuse crate::prefix::{PREFIX_66, PREFIX_67, PREFIX_F2, PREFIX_F3};\nuse crate::prefix::{PREFIX_MASK_SEGMENT, SEG_PREFIX_ZERO};\nuse crate::regs;\nuse crate::regs::{AX, BP, BX, CX, DI, DX, SI, SP};\nuse crate::regs::{CS, DS, ES, FS, GS, SS};\nuse crate::regs::{EAX, EBP, EBX, ECX, EDI, EDX, ESI, ESP};\nuse crate::wasmgen::wasm_builder::{WasmBuilder, WasmLocal};\n\nenum LocalOrImmediate<'a> {\n    WasmLocal(&'a WasmLocal),\n    Immediate(i32),\n}\n\nimpl<'a> LocalOrImmediate<'a> {\n    pub fn gen_get(&self, builder: &mut WasmBuilder) {\n        match self {\n            LocalOrImmediate::WasmLocal(l) => builder.get_local(l),\n            LocalOrImmediate::Immediate(i) => builder.const_i32(*i),\n        }\n    }\n    pub fn gen_get_mask255(&self, builder: &mut WasmBuilder) {\n        match self {\n            LocalOrImmediate::WasmLocal(l) => {\n                builder.get_local(l);\n                builder.const_i32(0xFF);\n                builder.and_i32()\n            },\n            LocalOrImmediate::Immediate(i) => builder.const_i32(*i & 0xFF),\n        }\n    }\n    pub fn eq_local(&self, other_local: &WasmLocal) -> bool {\n        match self {\n            &LocalOrImmediate::WasmLocal(local) => local == other_local,\n            LocalOrImmediate::Immediate(_) => false,\n        }\n    }\n    pub fn is_zero(&self) -> bool {\n        match self {\n            LocalOrImmediate::Immediate(0) => true,\n            _ => false,\n        }\n    }\n\n    fn to_instruction_operand(&self, ctx: &mut JitContext) -> InstructionOperand {\n        match self {\n            &LocalOrImmediate::WasmLocal(source) => {\n                local_to_instruction_operand(ctx, source).into()\n            },\n            &LocalOrImmediate::Immediate(i) => InstructionOperand::Immediate(i),\n        }\n    }\n}\n\nfn local_to_instruction_operand(ctx: &mut JitContext, local: &WasmLocal) -> InstructionOperandDest {\n    if ctx.register_locals.iter().any(|l| l == local) {\n        // safe because register locals are alive for the duration of the entire function\n        InstructionOperandDest::WasmLocal(local.unsafe_clone())\n    }\n    else {\n        InstructionOperandDest::Other\n    }\n}\n\npub fn jit_instruction(ctx: &mut JitContext, instr_flags: &mut u32) {\n    ctx.cpu.prefixes = 0;\n    ctx.start_of_current_instruction = ctx.cpu.eip;\n    gen::jit::jit(\n        ctx.cpu.read_imm8() as u32 | (ctx.cpu.osize_32() as u32) << 8,\n        ctx,\n        instr_flags,\n    );\n}\n\npub fn jit_handle_prefix(ctx: &mut JitContext, instr_flags: &mut u32) {\n    gen::jit::jit(\n        ctx.cpu.read_imm8() as u32 | (ctx.cpu.osize_32() as u32) << 8,\n        ctx,\n        instr_flags,\n    );\n}\n\npub fn jit_handle_segment_prefix(segment: u32, ctx: &mut JitContext, instr_flags: &mut u32) {\n    dbg_assert!(segment <= 5);\n    ctx.cpu.prefixes |= segment as u8 + 1;\n    jit_handle_prefix(ctx, instr_flags)\n}\n\npub fn instr16_0F_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    gen::jit0f::jit(ctx.cpu.read_imm8() as u32, ctx, instr_flags)\n}\npub fn instr32_0F_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    gen::jit0f::jit(ctx.cpu.read_imm8() as u32 | 0x100, ctx, instr_flags)\n}\npub fn instr_26_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    jit_handle_segment_prefix(ES, ctx, instr_flags)\n}\npub fn instr_2E_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    jit_handle_segment_prefix(CS, ctx, instr_flags)\n}\npub fn instr_36_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    jit_handle_segment_prefix(SS, ctx, instr_flags)\n}\npub fn instr_3E_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    jit_handle_segment_prefix(DS, ctx, instr_flags)\n}\n\npub fn instr_64_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    jit_handle_segment_prefix(FS, ctx, instr_flags)\n}\npub fn instr_65_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    jit_handle_segment_prefix(GS, ctx, instr_flags)\n}\n\npub fn instr_66_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    ctx.cpu.prefixes |= PREFIX_66;\n    jit_handle_prefix(ctx, instr_flags)\n}\npub fn instr_67_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    ctx.cpu.prefixes |= PREFIX_67;\n    jit_handle_prefix(ctx, instr_flags)\n}\npub fn instr_F0_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    // lock: Ignore\n    jit_handle_prefix(ctx, instr_flags)\n}\npub fn instr_F2_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    ctx.cpu.prefixes |= PREFIX_F2;\n    jit_handle_prefix(ctx, instr_flags)\n}\npub fn instr_F3_jit(ctx: &mut JitContext, instr_flags: &mut u32) {\n    ctx.cpu.prefixes |= PREFIX_F3;\n    jit_handle_prefix(ctx, instr_flags)\n}\n\nfn sse_read_f32_xmm_mem(ctx: &mut JitContext, name: &str, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.reinterpret_i32_as_f32();\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2_f32_i32(name);\n}\nfn sse_read_f32_xmm_xmm(ctx: &mut JitContext, name: &str, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_f32(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2_f32_i32(name);\n}\n\nfn sse_read64_xmm_mem(ctx: &mut JitContext, name: &str, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2_i64_i32(name);\n}\nfn sse_read64_xmm_xmm(ctx: &mut JitContext, name: &str, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2_i64_i32(name);\n}\n\nfn sse_read128_xmm_mem(ctx: &mut JitContext, name: &str, modrm_byte: ModrmByte, r: u32) {\n    let dest = global_pointers::sse_scratch_register as u32;\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n    ctx.builder.const_i32(dest as i32);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2(name);\n}\nfn sse_read128_xmm_mem_imm(\n    ctx: &mut JitContext,\n    name: &str,\n    modrm_byte: ModrmByte,\n    r: u32,\n    imm: u32,\n) {\n    let dest = global_pointers::sse_scratch_register as u32;\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n    ctx.builder.const_i32(dest as i32);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm as i32);\n    ctx.builder.call_fn3(name);\n}\nfn sse_read128_xmm_xmm(ctx: &mut JitContext, name: &str, r1: u32, r2: u32) {\n    // Make a copy to avoid aliasing problems: Called function expects a reg128, which must not\n    // alias with memory\n    codegen::gen_read_reg_xmm128_into_scratch(ctx, r1);\n    let dest = global_pointers::sse_scratch_register;\n    ctx.builder.const_i32(dest as i32);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(name);\n}\nfn sse_read128_xmm_xmm_imm(ctx: &mut JitContext, name: &str, r1: u32, r2: u32, imm: u32) {\n    // Make a copy to avoid aliasing problems: Called function expects a reg128, which must not\n    // alias with memory\n    codegen::gen_read_reg_xmm128_into_scratch(ctx, r1);\n    let dest = global_pointers::sse_scratch_register;\n    ctx.builder.const_i32(dest as i32);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.const_i32(imm as i32);\n    ctx.builder.call_fn3(name);\n}\nfn sse_mov_xmm_xmm(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r2) as i32);\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.store_aligned_i64(0);\n\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r2) as i32 + 8);\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32 + 8);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.store_aligned_i64(0);\n}\n\nfn mmx_read64_mm_mem32(ctx: &mut JitContext, name: &str, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2(name)\n}\nfn mmx_read64_mm_mm32(ctx: &mut JitContext, name: &str, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_mmx_offset(r1) as i32);\n    ctx.builder.load_aligned_i32(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(name);\n}\nfn mmx_read64_mm_mem(ctx: &mut JitContext, name: &str, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2_i64_i32(name)\n}\nfn mmx_read64_mm_mm(ctx: &mut JitContext, name: &str, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_mmx_offset(r1) as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2_i64_i32(name);\n}\n\nfn push16_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg16(ctx, r);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\nfn push32_reg_jit(ctx: &mut JitContext, r: u32) {\n    let reg = ctx.reg(r);\n    codegen::gen_push32(ctx, &reg);\n}\nfn push16_imm_jit(ctx: &mut JitContext, imm: u32) {\n    ctx.builder.const_i32(imm as i32);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\nfn push32_imm_jit(ctx: &mut JitContext, imm: u32) {\n    ctx.builder.const_i32(imm as i32);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push32(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\nfn push16_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\nfn push32_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push32(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\n\nfn pop16_reg_jit(ctx: &mut JitContext, reg: u32) {\n    codegen::gen_pop16(ctx);\n    codegen::gen_set_reg16_unmasked(ctx, reg);\n}\n\nfn pop32_reg_jit(ctx: &mut JitContext, reg: u32) {\n    codegen::gen_pop32s(ctx);\n    codegen::gen_set_reg32(ctx, reg);\n}\n\nfn group_arith_al_imm8(\n    ctx: &mut JitContext,\n    op: &dyn Fn(&mut JitContext, &WasmLocal, &LocalOrImmediate),\n    imm8: u32,\n) {\n    op(\n        ctx,\n        &ctx.reg(regs::EAX),\n        &LocalOrImmediate::Immediate(imm8 as i32),\n    );\n    codegen::gen_set_reg8_unmasked(ctx, regs::EAX);\n}\n\nfn group_arith_ax_imm16(ctx: &mut JitContext, op: &str, imm16: u32) {\n    codegen::gen_get_reg16(ctx, regs::AX);\n    ctx.builder.const_i32(imm16 as i32);\n    ctx.builder.call_fn2_ret(op);\n    codegen::gen_set_reg16(ctx, regs::AX);\n}\n\nfn group_arith_eax_imm32(\n    ctx: &mut JitContext,\n    op: &dyn Fn(&mut JitContext, &WasmLocal, &LocalOrImmediate),\n    imm32: u32,\n) {\n    op(\n        ctx,\n        &ctx.reg(regs::EAX),\n        &LocalOrImmediate::Immediate(imm32 as i32),\n    );\n}\n\nmacro_rules! define_instruction_read8(\n    ($fn:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n            let dest_operand = ctx.builder.set_new_local();\n            let source_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&source_operand));\n            ctx.builder.free_local(dest_operand);\n            codegen::gen_free_reg8_or_alias(ctx, r, source_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r1);\n            let source_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r2);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&source_operand));\n            codegen::gen_free_reg8_or_alias(ctx, r1, dest_operand);\n            codegen::gen_free_reg8_or_alias(ctx, r2, source_operand);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n            let dest_operand = ctx.builder.set_new_local();\n            let imm = mask_imm!(imm, $imm);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::Immediate(imm as i32));\n            ctx.builder.free_local(dest_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r1);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::Immediate(imm as i32));\n            codegen::gen_free_reg8_or_alias(ctx, r1, dest_operand);\n        }\n    );\n);\n\nmacro_rules! define_instruction_read16(\n    ($fn:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n            let dest_operand = ctx.builder.set_new_local();\n            $fn(\n                ctx,\n                &dest_operand,\n                &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n            );\n            ctx.builder.free_local(dest_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            $fn(\n                ctx,\n                &ctx.reg(r1),\n                &LocalOrImmediate::WasmLocal(&ctx.reg(r2))\n            );\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n            let dest_operand = ctx.builder.set_new_local();\n            let imm = mask_imm!(imm, $imm);\n            $fn(\n                ctx,\n                &dest_operand,\n                &LocalOrImmediate::Immediate(imm as i32),\n            );\n            ctx.builder.free_local(dest_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            $fn(\n                ctx,\n                &ctx.reg(r),\n                &LocalOrImmediate::Immediate(imm as i32),\n            );\n        }\n    );\n);\n\nmacro_rules! define_instruction_read32(\n    ($fn:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n            let dest_operand = ctx.builder.set_new_local();\n            $fn(\n                ctx,\n                &dest_operand,\n                &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n            );\n            ctx.builder.free_local(dest_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            $fn(\n                ctx,\n                &ctx.reg(r1),\n                &LocalOrImmediate::WasmLocal(&ctx.reg(r2))\n            );\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n            let dest_operand = ctx.builder.set_new_local();\n            let imm = mask_imm!(imm, $imm);\n            $fn(\n                ctx,\n                &dest_operand,\n                &LocalOrImmediate::Immediate(imm as i32),\n            );\n            ctx.builder.free_local(dest_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            $fn(\n                ctx,\n                &ctx.reg(r),\n                &LocalOrImmediate::Immediate(imm as i32),\n            );\n        }\n    );\n);\n\nmacro_rules! define_instruction_write_reg8(\n    ($fn:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n            let source_operand = ctx.builder.set_new_local();\n            let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&source_operand));\n            codegen::gen_set_reg8_unmasked(ctx, r);\n            ctx.builder.free_local(source_operand);\n            codegen::gen_free_reg8_or_alias(ctx, r, dest_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            let source_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r1);\n            let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r2);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&source_operand));\n            codegen::gen_set_reg8_unmasked(ctx, r2);\n            codegen::gen_free_reg8_or_alias(ctx, r1, source_operand);\n            codegen::gen_free_reg8_or_alias(ctx, r2, dest_operand);\n        }\n    )\n);\n\nmacro_rules! define_instruction_write_reg16(\n    ($fn:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_get_reg16(ctx, r);\n            codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg16(ctx, r);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            codegen::gen_get_reg16(ctx, r2);\n            codegen::gen_get_reg16(ctx, r1);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg16(ctx, r2);\n        }\n    )\n);\n\nmacro_rules! define_instruction_write_reg32(\n    ($fn:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n            let source_operand = ctx.builder.set_new_local();\n            $fn(\n                ctx,\n                &ctx.reg(r),\n                &LocalOrImmediate::WasmLocal(&source_operand),\n            );\n            ctx.builder.free_local(source_operand);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            $fn(\n                ctx,\n                &ctx.reg(r2),\n                &LocalOrImmediate::WasmLocal(&ctx.reg(r1)),\n            );\n        }\n    );\n);\n\nmacro_rules! mask_imm(\n    ($imm:expr, imm8_5bits) => { $imm & 31 };\n    ($imm:expr, imm8) => { $imm };\n    ($imm:expr, imm8s) => { $imm };\n    ($imm:expr, imm8s_16bits) => { $imm & 0xFFFF };\n    ($imm:expr, imm16) => { $imm };\n    ($imm:expr, imm32) => { $imm };\n);\n\nmacro_rules! define_instruction_read_write_mem8(\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n                    let dest_operand = ctx.builder.set_new_local();\n                    let source_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r);\n                    $fn(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&source_operand));\n                    codegen::gen_free_reg8_or_alias(ctx, r, source_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            let source_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r2);\n            let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r1);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&source_operand));\n            codegen::gen_set_reg8_unmasked(ctx, r1);\n            codegen::gen_free_reg8_or_alias(ctx, r2, source_operand);\n            codegen::gen_free_reg8_or_alias(ctx, r1, dest_operand);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, constant_one) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n                    ctx.builder.const_i32(1);\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            codegen::gen_get_reg8(ctx, r1);\n            ctx.builder.const_i32(1);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg8(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, cl) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg8(ctx, regs::CL);\n                    ctx.builder.const_i32(31);\n                    ctx.builder.and_i32();\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            codegen::gen_get_reg8(ctx, r1);\n            codegen::gen_get_reg8(ctx, regs::CL);\n            ctx.builder.const_i32(31);\n            ctx.builder.and_i32();\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg8(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, none) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n                    ctx.builder.call_fn1_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            codegen::gen_get_reg8(ctx, r1);\n            ctx.builder.call_fn1_ret($fn);\n            codegen::gen_set_reg8(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, ximm8) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n                    let dest_operand = ctx.builder.set_new_local();\n                    $fn(ctx, &dest_operand, &LocalOrImmediate::Immediate(imm as i32));\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, imm: u32) {\n            let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r1);\n            $fn(ctx, &dest_operand, &LocalOrImmediate::Immediate(imm as i32));\n            codegen::gen_set_reg8_unmasked(ctx, r1);\n            codegen::gen_free_reg8_or_alias(ctx, r1, dest_operand);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                let imm = mask_imm!(imm, $imm) as i32;\n                codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n                    ctx.builder.const_i32(imm as i32);\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            codegen::gen_get_reg8(ctx, r1);\n            ctx.builder.const_i32(imm as i32);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg8(ctx, r1);\n        }\n    );\n);\n\nmacro_rules! define_instruction_read_write_mem16(\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg16(ctx, r);\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            codegen::gen_get_reg16(ctx, r1);\n            codegen::gen_get_reg16(ctx, r2);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg16(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, constant_one) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    ctx.builder.const_i32(1);\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            codegen::gen_get_reg16(ctx, r1);\n            ctx.builder.const_i32(1);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg16(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, cl) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg8(ctx, regs::CL);\n                    ctx.builder.const_i32(31);\n                    ctx.builder.and_i32();\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            codegen::gen_get_reg16(ctx, r1);\n            codegen::gen_get_reg8(ctx, regs::CL);\n                ctx.builder.const_i32(31);\n                ctx.builder.and_i32();\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg16(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg, cl) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg16(ctx, r);\n                    codegen::gen_get_reg8(ctx, regs::CL);\n                    ctx.builder.const_i32(31);\n                    ctx.builder.and_i32();\n                    ctx.builder.call_fn3_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            codegen::gen_get_reg16(ctx, r1);\n            codegen::gen_get_reg16(ctx, r2);\n            codegen::gen_get_reg8(ctx, regs::CL);\n            ctx.builder.const_i32(31);\n            ctx.builder.and_i32();\n            ctx.builder.call_fn3_ret($fn);\n            codegen::gen_set_reg16(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                let imm = mask_imm!(imm, $imm);\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg16(ctx, r);\n                    ctx.builder.const_i32(imm as i32);\n                    ctx.builder.call_fn3_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            codegen::gen_get_reg16(ctx, r1);\n            codegen::gen_get_reg16(ctx, r2);\n            ctx.builder.const_i32(imm as i32);\n            ctx.builder.call_fn3_ret($fn);\n            codegen::gen_set_reg16(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, none) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    let mut dest_operand = ctx.builder.set_new_local();\n                    $fn(ctx, &mut dest_operand);\n                    ctx.builder.get_local(&dest_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            $fn(ctx, &mut ctx.reg(r1));\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                let imm = mask_imm!(imm, $imm) as i32;\n                codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n                    ctx.builder.const_i32(imm as i32);\n                    ctx.builder.call_fn2_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            codegen::gen_get_reg16(ctx, r1);\n            ctx.builder.const_i32(imm as i32);\n            ctx.builder.call_fn2_ret($fn);\n            codegen::gen_set_reg16(ctx, r1);\n        }\n    );\n);\n\nmacro_rules! define_instruction_read_write_mem32(\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    let dest_operand = ctx.builder.set_new_local();\n                    $fn(\n                        ctx,\n                        &dest_operand,\n                        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n                    );\n                    ctx.builder.get_local(&dest_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            $fn(\n                ctx,\n                &ctx.reg(r1),\n                &LocalOrImmediate::WasmLocal(&ctx.reg(r2)),\n            );\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, constant_one) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    let dest_operand = ctx.builder.set_new_local();\n                    $fn(ctx, &dest_operand, &LocalOrImmediate::Immediate(1));\n                    ctx.builder.get_local(&dest_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            $fn(ctx, &ctx.reg(r1), &LocalOrImmediate::Immediate(1));\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, cl) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    let dest_operand = ctx.builder.set_new_local();\n                    $fn(\n                        ctx,\n                        &dest_operand,\n                        &LocalOrImmediate::WasmLocal(&ctx.reg(regs::ECX)),\n                    );\n                    ctx.builder.get_local(&dest_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            $fn(\n                ctx,\n                &ctx.reg(r1),\n                &LocalOrImmediate::WasmLocal(&ctx.reg(regs::ECX)),\n            );\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg, cl) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg32(ctx, r);\n                    codegen::gen_get_reg8(ctx, regs::CL);\n                    ctx.builder.const_i32(31);\n                    ctx.builder.and_i32();\n                    ctx.builder.call_fn3_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            codegen::gen_get_reg32(ctx, r1);\n            codegen::gen_get_reg32(ctx, r2);\n            codegen::gen_get_reg8(ctx, regs::CL);\n            ctx.builder.const_i32(31);\n            ctx.builder.and_i32();\n            ctx.builder.call_fn3_ret($fn);\n            codegen::gen_set_reg32(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, reg, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                let imm = mask_imm!(imm, $imm) as i32;\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    codegen::gen_get_reg32(ctx, r);\n                    ctx.builder.const_i32(imm as i32);\n                    ctx.builder.call_fn3_ret($fn);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            codegen::gen_get_reg32(ctx, r1);\n            codegen::gen_get_reg32(ctx, r2);\n            ctx.builder.const_i32(imm as i32);\n            ctx.builder.call_fn3_ret($fn);\n            codegen::gen_set_reg32(ctx, r1);\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, none) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    let mut dest_operand = ctx.builder.set_new_local();\n                    $fn(ctx, &mut dest_operand);\n                    ctx.builder.get_local(&dest_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32) {\n            $fn(ctx, &mut ctx.reg(r1));\n        }\n    );\n\n    ($fn:expr, $name_mem:ident, $name_reg:ident, $imm:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                let imm = mask_imm!(imm, $imm);\n                codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n                    let dest_operand = ctx.builder.set_new_local();\n                    $fn(\n                        ctx,\n                        &dest_operand,\n                        &LocalOrImmediate::Immediate(imm as i32),\n                    );\n                    ctx.builder.get_local(&dest_operand);\n                    ctx.builder.free_local(dest_operand);\n                });\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, imm: u32) {\n            let imm = mask_imm!(imm, $imm);\n            $fn(\n                ctx,\n                &ctx.reg(r1),\n                &LocalOrImmediate::Immediate(imm as i32),\n            );\n        }\n    );\n);\n\nfn gen_add8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Add {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n        is_inc: false,\n    };\n\n    ctx.builder.const_i32(global_pointers::last_op1 as i32);\n    ctx.builder.get_local(dest_operand);\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.add_i32();\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, OPSIZE_8, FLAGS_ALL);\n\n    ctx.builder\n        .load_fixed_u8(global_pointers::last_result as u32);\n}\nfn gen_add32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Add {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n        is_inc: false,\n    };\n\n    codegen::gen_set_last_op1(ctx.builder, &dest_operand);\n\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.add_i32();\n    ctx.builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(ctx.builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, OPSIZE_32, FLAGS_ALL);\n}\n\nfn gen_sub8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Sub {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n        is_dec: false,\n    };\n\n    ctx.builder.const_i32(global_pointers::last_op1 as i32);\n    ctx.builder.get_local(dest_operand);\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.sub_i32();\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, OPSIZE_8, FLAGS_ALL | FLAG_SUB);\n\n    ctx.builder\n        .load_fixed_u8(global_pointers::last_result as u32);\n}\nfn gen_sub32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Sub {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n        is_dec: false,\n    };\n\n    codegen::gen_set_last_op1(ctx.builder, &dest_operand);\n\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.sub_i32();\n    ctx.builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(ctx.builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, OPSIZE_32, FLAGS_ALL | FLAG_SUB);\n}\n\nfn gen_cmp(\n    ctx: &mut JitContext,\n    dest_operand: &WasmLocal,\n    source_operand: &LocalOrImmediate,\n    size: i32,\n) {\n    ctx.current_instruction = Instruction::Cmp {\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: source_operand.to_instruction_operand(ctx),\n        opsize: size,\n    };\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    if source_operand.is_zero() {\n        ctx.builder.get_local(&dest_operand);\n    }\n    else {\n        ctx.builder.get_local(&dest_operand);\n        source_operand.gen_get(ctx.builder);\n        ctx.builder.sub_i32();\n    }\n    if size == OPSIZE_8 || size == OPSIZE_16 {\n        ctx.builder\n            .const_i32(if size == OPSIZE_8 { 0xFF } else { 0xFFFF });\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.const_i32(global_pointers::last_op1 as i32);\n    ctx.builder.get_local(&dest_operand);\n    if size == OPSIZE_8 || size == OPSIZE_16 {\n        ctx.builder\n            .const_i32(if size == OPSIZE_8 { 0xFF } else { 0xFFFF });\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, size, FLAGS_ALL | FLAG_SUB);\n}\nfn gen_cmp8(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) {\n    gen_cmp(ctx, dest, source, OPSIZE_8)\n}\nfn gen_cmp16(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) {\n    gen_cmp(ctx, dest, source, OPSIZE_16)\n}\nfn gen_cmp32(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) {\n    gen_cmp(ctx, dest, source, OPSIZE_32)\n}\n\nfn gen_adc8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.builder.get_local(dest_operand);\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n    source_operand.gen_get_mask255(ctx.builder);\n    ctx.builder.call_fn2_ret(\"adc8\");\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n\n    ctx.current_instruction = Instruction::AdcSbb {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n    };\n}\nfn gen_adc32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.add_i32();\n    codegen::gen_getcf(ctx, ConditionNegate::False);\n    ctx.builder.add_i32();\n    let res = ctx.builder.set_new_local();\n\n    codegen::gen_set_last_result(ctx.builder, &res);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder\n        .const_i32(!FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW);\n    ctx.builder.and_i32();\n\n    // cf: (dest_operand ^ ((dest_operand ^ source_operand) & (source_operand ^ res))) >> op_size & FLAG_CARRY\n    ctx.builder.get_local(&dest_operand);\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.xor_i32();\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.get_local(&res);\n    ctx.builder.xor_i32();\n    ctx.builder.and_i32();\n    ctx.builder.xor_i32();\n    ctx.builder.const_i32(31);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(FLAG_CARRY);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n\n    // af: (dest_operand ^ source_operand ^ res) & FLAG_ADJUST\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.get_local(&res);\n    ctx.builder.xor_i32();\n    ctx.builder.xor_i32();\n    ctx.builder.const_i32(FLAG_ADJUST);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n\n    // of: ((source_operand ^ res) & (dest_operand ^ res)) >> op_size << 11 & FLAG_OVERFLOW\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.get_local(&res);\n    ctx.builder.xor_i32();\n    ctx.builder.get_local(&dest_operand);\n    ctx.builder.get_local(&res);\n    ctx.builder.xor_i32();\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(31 - 11);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(FLAG_OVERFLOW);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.get_local(&res);\n    ctx.builder.set_local(dest_operand);\n    ctx.builder.free_local(res);\n\n    ctx.current_instruction = Instruction::AdcSbb {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n    };\n}\n\nfn gen_sbb8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.builder.get_local(dest_operand);\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n    source_operand.gen_get_mask255(ctx.builder);\n    ctx.builder.call_fn2_ret(\"sbb8\");\n    ctx.builder.const_i32(0xFF);\n    ctx.builder.and_i32();\n\n    ctx.current_instruction = Instruction::AdcSbb {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n    };\n}\nfn gen_sbb32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.sub_i32();\n    codegen::gen_getcf(ctx, ConditionNegate::False);\n    ctx.builder.sub_i32();\n    let res = ctx.builder.set_new_local();\n\n    codegen::gen_set_last_result(ctx.builder, &res);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder\n        .const_i32(!FLAG_CARRY & !FLAG_ADJUST & !FLAG_OVERFLOW);\n    ctx.builder.and_i32();\n\n    // cf: (res ^ ((res ^ source_operand) & (source_operand ^ dest_operand))) >> op_size & FLAG_CARRY\n    ctx.builder.get_local(&res);\n    ctx.builder.get_local(&res);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.xor_i32();\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.get_local(&dest_operand);\n    ctx.builder.xor_i32();\n    ctx.builder.and_i32();\n    ctx.builder.xor_i32();\n    ctx.builder.const_i32(31);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(FLAG_CARRY);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n\n    // af: (dest_operand ^ source_operand ^ res) & FLAG_ADJUST\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.get_local(&res);\n    ctx.builder.xor_i32();\n    ctx.builder.xor_i32();\n    ctx.builder.const_i32(FLAG_ADJUST);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n\n    // of: ((source_operand ^ dest_operand) & (res ^ dest_operand)) >> op_size << 11 & FLAG_OVERFLOW\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.get_local(&dest_operand);\n    ctx.builder.xor_i32();\n    ctx.builder.get_local(&res);\n    ctx.builder.get_local(&dest_operand);\n    ctx.builder.xor_i32();\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(31 - 11);\n    ctx.builder.shr_u_i32();\n    ctx.builder.const_i32(FLAG_OVERFLOW);\n    ctx.builder.and_i32();\n    ctx.builder.or_i32();\n\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.get_local(&res);\n    ctx.builder.set_local(dest_operand);\n    ctx.builder.free_local(res);\n\n    ctx.current_instruction = Instruction::AdcSbb {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: if source_operand.eq_local(dest_operand) {\n            InstructionOperand::Other // aliasing\n        }\n        else {\n            source_operand.to_instruction_operand(ctx)\n        },\n    };\n}\n\nfn gen_and8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n    };\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.and_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_8,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n\n    ctx.builder\n        .load_fixed_u8(global_pointers::last_result as u32);\n}\nfn gen_and32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n    };\n\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.and_i32();\n    ctx.builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(ctx.builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n}\n\nfn gen_test(\n    ctx: &mut JitContext,\n    dest_operand: &WasmLocal,\n    source_operand: &LocalOrImmediate,\n    size: i32,\n) {\n    let is_self_test = source_operand.eq_local(dest_operand);\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: size,\n        dest: if is_self_test {\n            local_to_instruction_operand(ctx, dest_operand)\n        }\n        else {\n            InstructionOperandDest::Other\n        },\n    };\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    if is_self_test {\n        ctx.builder.get_local(&dest_operand);\n    }\n    else {\n        ctx.builder.get_local(&dest_operand);\n        source_operand.gen_get(ctx.builder);\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        size,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n}\nfn gen_test8(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) {\n    gen_test(ctx, dest, source, OPSIZE_8)\n}\nfn gen_test16(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) {\n    gen_test(ctx, dest, source, OPSIZE_16)\n}\nfn gen_test32(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) {\n    gen_test(ctx, dest, source, OPSIZE_32)\n}\n\nfn gen_or8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n    };\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.or_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_8,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n\n    ctx.builder\n        .load_fixed_u8(global_pointers::last_result as u32);\n}\nfn gen_or32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n    };\n\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.or_i32();\n    ctx.builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(ctx.builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n}\n\nfn gen_xor8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: OPSIZE_8,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n    };\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.xor_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_8,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n\n    ctx.builder\n        .load_fixed_u8(global_pointers::last_result as u32);\n}\nfn gen_xor32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.current_instruction = Instruction::Bitwise {\n        opsize: OPSIZE_32,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n    };\n\n    if source_operand.eq_local(dest_operand) {\n        ctx.builder.const_i32(0);\n        ctx.builder.set_local(dest_operand);\n    // TODO:\n    // - Set last_result to zero rather than reading from local\n    // - Skip setting opsize (not relevant for SF, ZF, and PF on zero)\n    }\n    else {\n        ctx.builder.get_local(&dest_operand);\n        source_operand.gen_get(ctx.builder);\n        ctx.builder.xor_i32();\n        ctx.builder.set_local(dest_operand);\n    }\n\n    codegen::gen_set_last_result(ctx.builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW & !FLAG_ADJUST,\n    );\n    codegen::gen_clear_flags_bits(ctx.builder, FLAG_CARRY | FLAG_OVERFLOW | FLAG_ADJUST);\n}\n\nfn gen_rol32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.get_local(l);\n            builder.const_i32(31);\n            builder.and_i32();\n        },\n        LocalOrImmediate::Immediate(i) => {\n            builder.const_i32(*i & 31);\n        },\n    }\n    builder.const_i32(31);\n    builder.and_i32();\n    builder.call_fn2_ret(\"rol32\");\n    builder.set_local(dest_operand);\n}\nfn gen_ror32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.get_local(l);\n            builder.const_i32(31);\n            builder.and_i32();\n        },\n        LocalOrImmediate::Immediate(i) => {\n            builder.const_i32(*i & 31);\n        },\n    }\n    builder.const_i32(31);\n    builder.and_i32();\n    builder.call_fn2_ret(\"ror32\");\n    builder.set_local(dest_operand);\n}\n\nfn gen_rcl32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.get_local(l);\n            builder.const_i32(31);\n            builder.and_i32();\n        },\n        LocalOrImmediate::Immediate(i) => {\n            builder.const_i32(*i & 31);\n        },\n    }\n    builder.const_i32(31);\n    builder.and_i32();\n    builder.call_fn2_ret(\"rcl32\");\n    builder.set_local(dest_operand);\n}\nfn gen_rcr32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.get_local(l);\n            builder.const_i32(31);\n            builder.and_i32();\n        },\n        LocalOrImmediate::Immediate(i) => {\n            builder.const_i32(*i & 31);\n        },\n    }\n    builder.const_i32(31);\n    builder.and_i32();\n    builder.call_fn2_ret(\"rcr32\");\n    builder.set_local(dest_operand);\n}\n\nenum ShiftCount {\n    Local(WasmLocal),\n    Immediate(i32),\n}\nimpl ShiftCount {\n    pub fn gen_get(builder: &mut WasmBuilder, count: &ShiftCount) {\n        match &count {\n            ShiftCount::Local(l) => builder.get_local(l),\n            ShiftCount::Immediate(i) => builder.const_i32(*i),\n        }\n    }\n    pub fn gen_get_thirtytwo_minus(builder: &mut WasmBuilder, count: &ShiftCount) {\n        match &count {\n            ShiftCount::Local(l) => {\n                builder.const_i32(32);\n                builder.get_local(l);\n                builder.sub_i32();\n            },\n            ShiftCount::Immediate(i) => builder.const_i32(32 - *i),\n        }\n    }\n    pub fn gen_get_minus_one(builder: &mut WasmBuilder, count: &ShiftCount) {\n        match &count {\n            ShiftCount::Local(l) => {\n                builder.get_local(l);\n                builder.const_i32(1);\n                builder.sub_i32()\n            },\n            ShiftCount::Immediate(i) => builder.const_i32(*i - 1),\n        }\n    }\n}\n\nfn gen_shl32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    if let &LocalOrImmediate::Immediate(1..=31) = source_operand {\n        ctx.current_instruction = Instruction::NonZeroShift {\n            dest: local_to_instruction_operand(ctx, dest_operand),\n            opsize: OPSIZE_32,\n        };\n    }\n    let builder = &mut ctx.builder;\n    let count = match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            let exit = builder.block_void();\n            builder.get_local(l);\n            builder.const_i32(31); // Note: mask can probably be avoided since wasm has the same semantics on shl_i32\n            builder.and_i32();\n            let count = builder.tee_new_local();\n            builder.eqz_i32();\n            builder.br_if(exit);\n            ShiftCount::Local(count)\n        },\n        LocalOrImmediate::Immediate(i) => {\n            if *i & 31 == 0 {\n                return;\n            }\n            ShiftCount::Immediate(*i & 31)\n        },\n    };\n\n    builder.get_local(&dest_operand);\n    ShiftCount::gen_get_thirtytwo_minus(builder, &count);\n    builder.shr_u_i32();\n    builder.const_i32(1);\n    builder.and_i32();\n    let b = builder.set_new_local();\n\n    builder.get_local(dest_operand);\n    ShiftCount::gen_get(builder, &count);\n    builder.shl_i32();\n    builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(builder, dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW,\n    );\n\n    builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(builder);\n    builder.const_i32(!(FLAG_CARRY | FLAG_OVERFLOW));\n    builder.and_i32();\n    builder.get_local(&b);\n    builder.or_i32();\n    {\n        builder.get_local(&b);\n        builder.get_local(&dest_operand);\n        builder.const_i32(31);\n        builder.shr_u_i32();\n        builder.xor_i32();\n        builder.const_i32(11);\n        builder.shl_i32();\n        builder.const_i32(FLAG_OVERFLOW);\n        builder.and_i32();\n        builder.or_i32();\n    }\n    builder.store_aligned_i32(0);\n\n    builder.free_local(b);\n\n    if let ShiftCount::Local(l) = count {\n        builder.block_end();\n        builder.free_local(l);\n    }\n}\nfn gen_shr32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    if let &LocalOrImmediate::Immediate(1..=31) = source_operand {\n        ctx.current_instruction = Instruction::NonZeroShift {\n            dest: local_to_instruction_operand(ctx, dest_operand),\n            opsize: OPSIZE_32,\n        };\n    }\n    let builder = &mut ctx.builder;\n    let count = match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            let exit = builder.block_void();\n            builder.get_local(l);\n            builder.const_i32(31); // Note: mask can probably be avoided since wasm has the same semantics on shl_i32\n            builder.and_i32();\n            let count = builder.tee_new_local();\n            builder.eqz_i32();\n            builder.br_if(exit);\n            ShiftCount::Local(count)\n        },\n        LocalOrImmediate::Immediate(i) => {\n            if *i & 31 == 0 {\n                return;\n            }\n            ShiftCount::Immediate(*i & 31)\n        },\n    };\n\n    builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(builder);\n    builder.const_i32(!(FLAG_CARRY | FLAG_OVERFLOW));\n    builder.and_i32();\n    {\n        builder.get_local(dest_operand);\n        ShiftCount::gen_get_minus_one(builder, &count);\n        builder.shr_u_i32();\n        builder.const_i32(1);\n        builder.and_i32();\n        builder.or_i32()\n    }\n    {\n        builder.get_local(dest_operand);\n        builder.const_i32(20);\n        builder.shr_u_i32();\n        builder.const_i32(FLAG_OVERFLOW);\n        builder.and_i32();\n        builder.or_i32()\n    }\n    builder.store_aligned_i32(0);\n\n    builder.get_local(dest_operand);\n    ShiftCount::gen_get(builder, &count);\n    builder.shr_u_i32();\n    builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(builder, dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW,\n    );\n\n    if let ShiftCount::Local(l) = count {\n        builder.block_end();\n        builder.free_local(l);\n    }\n}\nfn gen_sar32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    if let &LocalOrImmediate::Immediate(1..=31) = source_operand {\n        ctx.current_instruction = Instruction::NonZeroShift {\n            dest: local_to_instruction_operand(ctx, dest_operand),\n            opsize: OPSIZE_32,\n        };\n    }\n    let builder = &mut ctx.builder;\n    let count = match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            let exit = builder.block_void();\n            builder.get_local(l);\n            builder.const_i32(31); // Note: mask can probably be avoided since wasm has the same semantics on shl_i32\n            builder.and_i32();\n            let count = builder.tee_new_local();\n            builder.eqz_i32();\n            builder.br_if(exit);\n            ShiftCount::Local(count)\n        },\n        LocalOrImmediate::Immediate(i) => {\n            if *i & 31 == 0 {\n                return;\n            }\n            ShiftCount::Immediate(*i & 31)\n        },\n    };\n\n    builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(builder);\n    builder.const_i32(!(FLAG_CARRY | FLAG_OVERFLOW));\n    builder.and_i32();\n    {\n        builder.get_local(dest_operand);\n        ShiftCount::gen_get_minus_one(builder, &count);\n        builder.shr_u_i32();\n        builder.const_i32(1);\n        builder.and_i32();\n        builder.or_i32()\n    }\n    builder.store_aligned_i32(0);\n\n    builder.get_local(dest_operand);\n    ShiftCount::gen_get(builder, &count);\n    builder.shr_s_i32();\n    builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(builder, dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        builder,\n        OPSIZE_32,\n        FLAGS_ALL & !FLAG_CARRY & !FLAG_OVERFLOW,\n    );\n\n    if let ShiftCount::Local(l) = count {\n        builder.block_end();\n        builder.free_local(l);\n    }\n}\n\nfn gen_xadd32(ctx: &mut JitContext, dest_operand: &WasmLocal, r: u32) {\n    ctx.builder.get_local(&ctx.register_locals[r as usize]);\n    let tmp = ctx.builder.set_new_local();\n\n    ctx.builder.get_local(&dest_operand);\n    codegen::gen_set_reg32(ctx, r);\n\n    gen_add32(ctx, &dest_operand, &LocalOrImmediate::WasmLocal(&tmp));\n\n    ctx.builder.free_local(tmp);\n}\n\nfn gen_cmpxchg32(ctx: &mut JitContext, r: u32) {\n    let source = ctx.builder.set_new_local();\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.get_local(&source);\n    ctx.builder.sub_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.const_i32(global_pointers::last_op1 as i32);\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.store_aligned_i32(0);\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, OPSIZE_32, FLAGS_ALL | FLAG_SUB);\n\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.get_local(&source);\n    ctx.builder.eq_i32();\n    ctx.builder.if_i32();\n    codegen::gen_get_reg32(ctx, r);\n    ctx.builder.else_();\n    ctx.builder.get_local(&source);\n    codegen::gen_set_reg32(ctx, regs::EAX);\n    ctx.builder.get_local(&source);\n    ctx.builder.block_end();\n\n    ctx.builder.free_local(source);\n}\n\nfn gen_mul32(ctx: &mut JitContext) {\n    ctx.builder.extend_unsigned_i32_to_i64();\n\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.extend_unsigned_i32_to_i64();\n    ctx.builder.mul_i64();\n\n    let result = ctx.builder.tee_new_local_i64();\n    ctx.builder.const_i64(32);\n    ctx.builder.shr_u_i64();\n    ctx.builder.wrap_i64_to_i32();\n    codegen::gen_set_reg32(ctx, regs::EDX);\n\n    ctx.builder.get_local_i64(&result);\n    ctx.builder.free_local_i64(result);\n    ctx.builder.wrap_i64_to_i32();\n    codegen::gen_set_reg32(ctx, regs::EAX);\n\n    codegen::gen_get_reg32(ctx, regs::EDX);\n    ctx.builder.if_void();\n    codegen::gen_set_flags_bits(ctx.builder, 1 | FLAG_OVERFLOW);\n    ctx.builder.else_();\n    codegen::gen_clear_flags_bits(ctx.builder, 1 | FLAG_OVERFLOW);\n    ctx.builder.block_end();\n\n    codegen::gen_set_last_result(ctx.builder, &ctx.register_locals[regs::EAX as usize]);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !1 & !FLAG_OVERFLOW,\n    );\n}\n\nfn gen_imul32(ctx: &mut JitContext) {\n    ctx.builder.extend_signed_i32_to_i64();\n\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.extend_signed_i32_to_i64();\n    ctx.builder.mul_i64();\n\n    let result = ctx.builder.tee_new_local_i64();\n    ctx.builder.const_i64(32);\n    ctx.builder.shr_u_i64();\n    ctx.builder.wrap_i64_to_i32();\n    codegen::gen_set_reg32(ctx, regs::EDX);\n\n    ctx.builder.get_local_i64(&result);\n    ctx.builder.free_local_i64(result);\n    ctx.builder.wrap_i64_to_i32();\n    codegen::gen_set_reg32(ctx, regs::EAX);\n\n    codegen::gen_get_reg32(ctx, regs::EDX);\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.const_i32(31);\n    ctx.builder.shr_s_i32();\n    ctx.builder.eq_i32();\n    ctx.builder.if_void();\n    codegen::gen_clear_flags_bits(ctx.builder, 1 | FLAG_OVERFLOW);\n    ctx.builder.else_();\n    codegen::gen_set_flags_bits(ctx.builder, 1 | FLAG_OVERFLOW);\n    ctx.builder.block_end();\n\n    codegen::gen_set_last_result(ctx.builder, &ctx.register_locals[regs::EAX as usize]);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        ctx.builder,\n        OPSIZE_32,\n        FLAGS_ALL & !1 & !FLAG_OVERFLOW,\n    );\n}\n\nfn gen_imul_reg32(\n    ctx: &mut JitContext,\n    dest_operand: &WasmLocal,\n    source_operand: &LocalOrImmediate,\n) {\n    gen_imul3_reg32(ctx.builder, dest_operand, dest_operand, source_operand);\n}\n\nfn gen_imul3_reg32(\n    builder: &mut WasmBuilder,\n    dest_operand: &WasmLocal,\n    source_operand1: &WasmLocal,\n    source_operand2: &LocalOrImmediate,\n) {\n    builder.get_local(&source_operand1);\n    builder.extend_signed_i32_to_i64();\n    source_operand2.gen_get(builder);\n    builder.extend_signed_i32_to_i64();\n    builder.mul_i64();\n\n    let result = builder.tee_new_local_i64();\n    builder.wrap_i64_to_i32();\n    builder.set_local(&dest_operand);\n\n    codegen::gen_set_last_result(builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(\n        builder,\n        OPSIZE_32,\n        FLAGS_ALL & !1 & !FLAG_OVERFLOW,\n    );\n\n    builder.const_i32(global_pointers::flags as i32);\n    builder.get_local_i64(&result);\n    builder.wrap_i64_to_i32();\n    builder.extend_signed_i32_to_i64();\n    builder.get_local_i64(&result);\n    builder.ne_i64();\n    builder.const_i32(1 | FLAG_OVERFLOW);\n    builder.mul_i32();\n    codegen::gen_get_flags(builder);\n    builder.const_i32(!1 & !FLAG_OVERFLOW);\n    builder.and_i32();\n    builder.or_i32();\n    builder.store_aligned_i32(0);\n\n    builder.free_local_i64(result);\n}\n\nfn gen_div32(ctx: &mut JitContext, source: &WasmLocal) {\n    let done = ctx.builder.block_void();\n    {\n        let exception = ctx.builder.block_void();\n        {\n            ctx.builder.get_local(source);\n            ctx.builder.eqz_i32();\n            ctx.builder.br_if(exception);\n\n            codegen::gen_get_reg32(ctx, regs::EDX);\n            ctx.builder.extend_unsigned_i32_to_i64();\n            ctx.builder.const_i64(32);\n            ctx.builder.shl_i64();\n            codegen::gen_get_reg32(ctx, regs::EAX);\n            ctx.builder.extend_unsigned_i32_to_i64();\n            ctx.builder.or_i64();\n            let dest_operand = ctx.builder.tee_new_local_i64();\n\n            ctx.builder.get_local(source);\n            ctx.builder.extend_unsigned_i32_to_i64();\n            ctx.builder.div_i64();\n            let result = ctx.builder.tee_new_local_i64();\n            ctx.builder.const_i64(0xFFFF_FFFF);\n            ctx.builder.gtu_i64();\n            ctx.builder.br_if(exception);\n\n            ctx.builder.get_local_i64(&dest_operand);\n            ctx.builder.get_local(source);\n            ctx.builder.extend_unsigned_i32_to_i64();\n            ctx.builder.rem_i64();\n            ctx.builder.wrap_i64_to_i32();\n            codegen::gen_set_reg32(ctx, regs::EDX);\n\n            ctx.builder.get_local_i64(&result);\n            ctx.builder.wrap_i64_to_i32();\n            codegen::gen_set_reg32(ctx, regs::EAX);\n            ctx.builder.br(done);\n\n            ctx.builder.free_local_i64(dest_operand);\n            ctx.builder.free_local_i64(result);\n        }\n        ctx.builder.block_end();\n\n        codegen::gen_trigger_de(ctx);\n    }\n    ctx.builder.block_end();\n}\n\nfn gen_bt(\n    builder: &mut WasmBuilder,\n    bit_base: &WasmLocal,\n    bit_offset: &LocalOrImmediate,\n    offset_mask: u32,\n) {\n    builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(builder);\n    builder.const_i32(!1);\n    builder.and_i32();\n    builder.get_local(bit_base);\n    match bit_offset {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.get_local(l);\n            builder.const_i32(offset_mask as i32);\n            builder.and_i32();\n        },\n        LocalOrImmediate::Immediate(imm) => builder.const_i32(imm & offset_mask as i32),\n    }\n    builder.shr_u_i32();\n    builder.const_i32(1);\n    builder.and_i32();\n    builder.or_i32();\n    builder.store_aligned_i32(0);\n\n    codegen::gen_clear_flags_changed_bits(builder, 1);\n}\nfn gen_bts(\n    builder: &mut WasmBuilder,\n    dest_bit_base: &WasmLocal,\n    bit_offset: &LocalOrImmediate,\n    offset_mask: u32,\n) {\n    gen_bt(builder, dest_bit_base, bit_offset, offset_mask);\n\n    builder.get_local(dest_bit_base);\n    match bit_offset {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.const_i32(1);\n            builder.get_local(l);\n            builder.const_i32(offset_mask as i32);\n            builder.and_i32();\n            builder.shl_i32();\n        },\n        LocalOrImmediate::Immediate(imm) => builder.const_i32(1 << (imm & offset_mask as i32)),\n    }\n    builder.or_i32();\n    builder.set_local(dest_bit_base);\n}\nfn gen_btc(\n    builder: &mut WasmBuilder,\n    dest_bit_base: &WasmLocal,\n    bit_offset: &LocalOrImmediate,\n    offset_mask: u32,\n) {\n    gen_bt(builder, dest_bit_base, bit_offset, offset_mask);\n\n    builder.get_local(dest_bit_base);\n    match bit_offset {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.const_i32(1);\n            builder.get_local(l);\n            builder.const_i32(offset_mask as i32);\n            builder.and_i32();\n            builder.shl_i32();\n        },\n        LocalOrImmediate::Immediate(imm) => builder.const_i32(1 << (imm & offset_mask as i32)),\n    }\n    builder.xor_i32();\n    builder.set_local(dest_bit_base);\n}\nfn gen_btr(\n    builder: &mut WasmBuilder,\n    dest_bit_base: &WasmLocal,\n    bit_offset: &LocalOrImmediate,\n    offset_mask: u32,\n) {\n    gen_bt(builder, dest_bit_base, bit_offset, offset_mask);\n\n    builder.get_local(dest_bit_base);\n    match bit_offset {\n        LocalOrImmediate::WasmLocal(l) => {\n            builder.const_i32(1);\n            builder.get_local(l);\n            builder.const_i32(offset_mask as i32);\n            builder.and_i32();\n            builder.shl_i32();\n            builder.const_i32(-1);\n            builder.xor_i32();\n        },\n        LocalOrImmediate::Immediate(imm) => builder.const_i32(!(1 << (imm & offset_mask as i32))),\n    }\n    builder.and_i32();\n    builder.set_local(dest_bit_base);\n}\n\nfn gen_bit_rmw(\n    ctx: &mut JitContext,\n    modrm_byte: ModrmByte,\n    op: &dyn Fn(&mut WasmBuilder, &WasmLocal, &LocalOrImmediate, u32),\n    source_operand: &LocalOrImmediate,\n    opsize: i32,\n) {\n    dbg_assert!(opsize == 16 || opsize == 32);\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    match source_operand {\n        LocalOrImmediate::WasmLocal(l) => {\n            ctx.builder.get_local(l);\n            if opsize == 16 {\n                codegen::sign_extend_i16(ctx.builder);\n            }\n            ctx.builder.const_i32(3);\n            ctx.builder.shr_s_i32();\n            ctx.builder.add_i32();\n        },\n        &LocalOrImmediate::Immediate(imm8) => {\n            let offset = (imm8 as i32 & (opsize - 1)) >> 3;\n            if offset != 0 {\n                ctx.builder.const_i32(offset);\n                ctx.builder.add_i32();\n            }\n        },\n    }\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read_write(ctx, BitSize::BYTE, &address_local, &|ref mut ctx| {\n        let value_local = ctx.builder.set_new_local();\n        op(ctx.builder, &value_local, source_operand, 7);\n        ctx.builder.get_local(&value_local);\n        ctx.builder.free_local(value_local);\n    });\n    ctx.builder.free_local(address_local);\n}\n\nfn gen_bsf32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.call_fn2_ret(\"bsf32\");\n    ctx.builder.set_local(dest_operand);\n}\n\nfn gen_bsr32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) {\n    ctx.builder.get_local(&dest_operand);\n    source_operand.gen_get(ctx.builder);\n    ctx.builder.call_fn2_ret(\"bsr32\");\n    ctx.builder.set_local(dest_operand);\n}\n\nfn gen_bswap(ctx: &mut JitContext, reg: i32) {\n    let l = &ctx.register_locals[reg as usize];\n\n    ctx.builder.get_local(l);\n    ctx.builder.const_i32(8);\n    ctx.builder.rotl_i32();\n    ctx.builder.const_i32(0xFF00FF);\n    ctx.builder.and_i32();\n\n    ctx.builder.get_local(l);\n    ctx.builder.const_i32(24);\n    ctx.builder.rotl_i32();\n    ctx.builder.const_i32(0xFF00FF00u32 as i32);\n    ctx.builder.and_i32();\n\n    ctx.builder.or_i32();\n\n    ctx.builder.set_local(l);\n}\n\ndefine_instruction_read_write_mem8!(gen_add8, instr_00_mem_jit, instr_00_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"add16\", instr16_01_mem_jit, instr16_01_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_add32, instr32_01_mem_jit, instr32_01_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_add8, instr_02_mem_jit, instr_02_reg_jit);\ndefine_instruction_write_reg16!(\"add16\", instr16_03_mem_jit, instr16_03_reg_jit);\ndefine_instruction_write_reg32!(gen_add32, instr32_03_mem_jit, instr32_03_reg_jit);\n\npub fn instr_04_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_add8, imm8); }\npub fn instr16_05_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"add16\", imm16);\n}\npub fn instr32_05_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_add32, imm32);\n}\n\ndefine_instruction_read_write_mem8!(gen_or8, instr_08_mem_jit, instr_08_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"or16\", instr16_09_mem_jit, instr16_09_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_or32, instr32_09_mem_jit, instr32_09_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_or8, instr_0A_mem_jit, instr_0A_reg_jit);\ndefine_instruction_write_reg16!(\"or16\", instr16_0B_mem_jit, instr16_0B_reg_jit);\ndefine_instruction_write_reg32!(gen_or32, instr32_0B_mem_jit, instr32_0B_reg_jit);\n\npub fn instr_0C_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_or8, imm8); }\npub fn instr16_0D_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"or16\", imm16);\n}\npub fn instr32_0D_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_or32, imm32);\n}\n\ndefine_instruction_read_write_mem8!(gen_adc8, instr_10_mem_jit, instr_10_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"adc16\", instr16_11_mem_jit, instr16_11_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_adc32, instr32_11_mem_jit, instr32_11_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_adc8, instr_12_mem_jit, instr_12_reg_jit);\ndefine_instruction_write_reg16!(\"adc16\", instr16_13_mem_jit, instr16_13_reg_jit);\ndefine_instruction_write_reg32!(gen_adc32, instr32_13_mem_jit, instr32_13_reg_jit);\n\npub fn instr_14_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_adc8, imm8); }\npub fn instr16_15_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"adc16\", imm16);\n}\npub fn instr32_15_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_adc32, imm32);\n}\n\ndefine_instruction_read_write_mem8!(gen_sbb8, instr_18_mem_jit, instr_18_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"sbb16\", instr16_19_mem_jit, instr16_19_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_sbb32, instr32_19_mem_jit, instr32_19_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_sbb8, instr_1A_mem_jit, instr_1A_reg_jit);\ndefine_instruction_write_reg16!(\"sbb16\", instr16_1B_mem_jit, instr16_1B_reg_jit);\ndefine_instruction_write_reg32!(gen_sbb32, instr32_1B_mem_jit, instr32_1B_reg_jit);\n\npub fn instr_1C_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_sbb8, imm8); }\npub fn instr16_1D_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"sbb16\", imm16);\n}\npub fn instr32_1D_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_sbb32, imm32);\n}\n\ndefine_instruction_read_write_mem8!(gen_and8, instr_20_mem_jit, instr_20_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"and16\", instr16_21_mem_jit, instr16_21_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_and32, instr32_21_mem_jit, instr32_21_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_and8, instr_22_mem_jit, instr_22_reg_jit);\ndefine_instruction_write_reg16!(\"and16\", instr16_23_mem_jit, instr16_23_reg_jit);\ndefine_instruction_write_reg32!(gen_and32, instr32_23_mem_jit, instr32_23_reg_jit);\n\npub fn instr_24_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_and8, imm8); }\npub fn instr16_25_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"and16\", imm16);\n}\npub fn instr32_25_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_and32, imm32);\n}\n\ndefine_instruction_read_write_mem8!(gen_sub8, instr_28_mem_jit, instr_28_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"sub16\", instr16_29_mem_jit, instr16_29_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_sub32, instr32_29_mem_jit, instr32_29_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_sub8, instr_2A_mem_jit, instr_2A_reg_jit);\ndefine_instruction_write_reg16!(\"sub16\", instr16_2B_mem_jit, instr16_2B_reg_jit);\ndefine_instruction_write_reg32!(gen_sub32, instr32_2B_mem_jit, instr32_2B_reg_jit);\n\npub fn instr_2C_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_sub8, imm8); }\npub fn instr16_2D_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"sub16\", imm16);\n}\npub fn instr32_2D_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_sub32, imm32);\n}\n\ndefine_instruction_read_write_mem8!(gen_xor8, instr_30_mem_jit, instr_30_reg_jit, reg);\ndefine_instruction_read_write_mem16!(\"xor16\", instr16_31_mem_jit, instr16_31_reg_jit, reg);\ndefine_instruction_read_write_mem32!(gen_xor32, instr32_31_mem_jit, instr32_31_reg_jit, reg);\n\ndefine_instruction_write_reg8!(gen_xor8, instr_32_mem_jit, instr_32_reg_jit);\ndefine_instruction_write_reg16!(\"xor16\", instr16_33_mem_jit, instr16_33_reg_jit);\ndefine_instruction_write_reg32!(gen_xor32, instr32_33_mem_jit, instr32_33_reg_jit);\n\npub fn instr_34_jit(ctx: &mut JitContext, imm8: u32) { group_arith_al_imm8(ctx, &gen_xor8, imm8); }\npub fn instr16_35_jit(ctx: &mut JitContext, imm16: u32) {\n    group_arith_ax_imm16(ctx, \"xor16\", imm16);\n}\npub fn instr32_35_jit(ctx: &mut JitContext, imm32: u32) {\n    group_arith_eax_imm32(ctx, &gen_xor32, imm32);\n}\n\ndefine_instruction_read8!(gen_cmp8, instr_38_mem_jit, instr_38_reg_jit);\ndefine_instruction_read16!(gen_cmp16, instr16_39_mem_jit, instr16_39_reg_jit);\ndefine_instruction_read32!(gen_cmp32, instr32_39_mem_jit, instr32_39_reg_jit);\n\npub fn instr_3A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r);\n    codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n    let source_operand = ctx.builder.set_new_local();\n    gen_cmp8(\n        ctx,\n        &dest_operand,\n        &LocalOrImmediate::WasmLocal(&source_operand),\n    );\n    codegen::gen_free_reg8_or_alias(ctx, r, dest_operand);\n    ctx.builder.free_local(source_operand);\n}\n\npub fn instr_3A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    let dest_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r2);\n    let source_operand = codegen::gen_get_reg8_or_alias_to_reg32(ctx, r1);\n    gen_cmp8(\n        ctx,\n        &dest_operand,\n        &LocalOrImmediate::WasmLocal(&source_operand),\n    );\n    codegen::gen_free_reg8_or_alias(ctx, r2, dest_operand);\n    codegen::gen_free_reg8_or_alias(ctx, r1, source_operand);\n}\n\npub fn instr16_3B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    let source_operand = ctx.builder.set_new_local();\n    gen_cmp16(\n        ctx,\n        &ctx.reg(r),\n        &LocalOrImmediate::WasmLocal(&source_operand),\n    );\n    ctx.builder.free_local(source_operand);\n}\n\npub fn instr16_3B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_cmp16(\n        ctx,\n        &ctx.reg(r2),\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r1)),\n    );\n}\n\npub fn instr32_3B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    let source_operand = ctx.builder.set_new_local();\n    gen_cmp32(\n        ctx,\n        &ctx.reg(r),\n        &LocalOrImmediate::WasmLocal(&source_operand),\n    );\n    ctx.builder.free_local(source_operand);\n}\n\npub fn instr32_3B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_cmp32(\n        ctx,\n        &ctx.reg(r2),\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r1)),\n    );\n}\n\npub fn instr_3C_jit(ctx: &mut JitContext, imm8: u32) {\n    gen_cmp8(ctx, &ctx.reg(0), &LocalOrImmediate::Immediate(imm8 as i32));\n}\n\npub fn instr16_3D_jit(ctx: &mut JitContext, imm16: u32) {\n    gen_cmp16(ctx, &ctx.reg(0), &LocalOrImmediate::Immediate(imm16 as i32));\n}\n\npub fn instr32_3D_jit(ctx: &mut JitContext, imm32: u32) {\n    gen_cmp32(ctx, &ctx.reg(0), &LocalOrImmediate::Immediate(imm32 as i32));\n}\n\nfn gen_inc(ctx: &mut JitContext, dest_operand: &WasmLocal, size: i32) {\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(!1);\n    ctx.builder.and_i32();\n    codegen::gen_getcf(ctx, ConditionNegate::False);\n    ctx.builder.or_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.const_i32(global_pointers::last_op1 as i32);\n    ctx.builder.get_local(&dest_operand);\n    if size == OPSIZE_8 || size == OPSIZE_16 {\n        ctx.builder\n            .const_i32(if size == OPSIZE_8 { 0xFF } else { 0xFFFF });\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.get_local(dest_operand);\n    ctx.builder.const_i32(1);\n    ctx.builder.add_i32();\n    if size == OPSIZE_16 {\n        codegen::gen_set_reg16_local(ctx.builder, dest_operand);\n    }\n    else {\n        ctx.builder.set_local(dest_operand);\n    }\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(&dest_operand);\n    if size == OPSIZE_16 {\n        ctx.builder.const_i32(0xFFFF);\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, size, FLAGS_ALL & !1);\n    ctx.current_instruction = Instruction::Add {\n        opsize: size,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: InstructionOperand::Immediate(1),\n        is_inc: true,\n    };\n}\nfn gen_inc16(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    gen_inc(ctx, dest_operand, OPSIZE_16);\n}\nfn gen_inc32(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    gen_inc(ctx, dest_operand, OPSIZE_32);\n}\n\nfn gen_dec(ctx: &mut JitContext, dest_operand: &WasmLocal, size: i32) {\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(!1);\n    ctx.builder.and_i32();\n    codegen::gen_getcf(ctx, ConditionNegate::False);\n    ctx.builder.or_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.const_i32(global_pointers::last_op1 as i32);\n    ctx.builder.get_local(&dest_operand);\n    if size == OPSIZE_8 || size == OPSIZE_16 {\n        ctx.builder\n            .const_i32(if size == OPSIZE_8 { 0xFF } else { 0xFFFF });\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n\n    ctx.builder.get_local(dest_operand);\n    ctx.builder.const_i32(1);\n    ctx.builder.sub_i32();\n    if size == OPSIZE_16 {\n        codegen::gen_set_reg16_local(ctx.builder, dest_operand);\n    }\n    else {\n        ctx.builder.set_local(dest_operand);\n    }\n\n    ctx.builder.const_i32(global_pointers::last_result as i32);\n    ctx.builder.get_local(&dest_operand);\n    if size == OPSIZE_16 {\n        ctx.builder.const_i32(0xFFFF);\n        ctx.builder.and_i32();\n    }\n    ctx.builder.store_aligned_i32(0);\n    codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, size, FLAGS_ALL & !1 | FLAG_SUB);\n    ctx.current_instruction = Instruction::Sub {\n        opsize: size,\n        dest: local_to_instruction_operand(ctx, dest_operand),\n        source: InstructionOperand::Immediate(1),\n        is_dec: true,\n    };\n}\nfn gen_dec16(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    gen_dec(ctx, dest_operand, OPSIZE_16)\n}\nfn gen_dec32(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    gen_dec(ctx, dest_operand, OPSIZE_32)\n}\n\nfn gen_inc16_r(ctx: &mut JitContext, r: u32) { gen_inc16(ctx, &mut ctx.reg(r)) }\nfn gen_inc32_r(ctx: &mut JitContext, r: u32) { gen_inc32(ctx, &mut ctx.reg(r)) }\nfn gen_dec16_r(ctx: &mut JitContext, r: u32) { gen_dec16(ctx, &mut ctx.reg(r)) }\nfn gen_dec32_r(ctx: &mut JitContext, r: u32) { gen_dec32(ctx, &mut ctx.reg(r)) }\n\nfn gen_not16(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    builder.const_i32(-1);\n    builder.xor_i32();\n    codegen::gen_set_reg16_local(builder, dest_operand);\n}\nfn gen_not32(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    builder.const_i32(-1);\n    builder.xor_i32();\n    builder.set_local(dest_operand);\n}\n\nfn gen_neg16(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    let builder = &mut ctx.builder;\n    builder.get_local(dest_operand);\n    builder.call_fn1_ret(\"neg16\");\n    codegen::gen_set_reg16_local(builder, dest_operand);\n}\nfn gen_neg32(ctx: &mut JitContext, dest_operand: &WasmLocal) {\n    let builder = &mut ctx.builder;\n    builder.const_i32(global_pointers::last_op1 as i32);\n    builder.const_i32(0);\n    builder.store_aligned_i32(0);\n\n    builder.const_i32(0);\n    builder.get_local(&dest_operand);\n    builder.sub_i32();\n    builder.set_local(dest_operand);\n\n    codegen::gen_set_last_result(builder, &dest_operand);\n    codegen::gen_set_last_op_size_and_flags_changed(builder, OPSIZE_32, FLAGS_ALL | FLAG_SUB);\n}\n\npub fn instr16_06_jit(ctx: &mut JitContext) {\n    codegen::gen_get_sreg(ctx, regs::ES);\n    let sreg = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &sreg);\n    ctx.builder.free_local(sreg);\n}\npub fn instr32_06_jit(ctx: &mut JitContext) { codegen::gen_push32_sreg(ctx, regs::ES) }\n\npub fn instr16_0E_jit(ctx: &mut JitContext) {\n    codegen::gen_get_sreg(ctx, regs::CS);\n    let sreg = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &sreg);\n    ctx.builder.free_local(sreg);\n}\npub fn instr32_0E_jit(ctx: &mut JitContext) { codegen::gen_push32_sreg(ctx, regs::CS) }\n\npub fn instr16_16_jit(ctx: &mut JitContext) {\n    codegen::gen_get_sreg(ctx, regs::SS);\n    let sreg = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &sreg);\n    ctx.builder.free_local(sreg);\n}\npub fn instr32_16_jit(ctx: &mut JitContext) { codegen::gen_push32_sreg(ctx, regs::SS) }\n\npub fn instr16_1E_jit(ctx: &mut JitContext) {\n    codegen::gen_get_sreg(ctx, regs::DS);\n    let sreg = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &sreg);\n    ctx.builder.free_local(sreg);\n}\npub fn instr32_1E_jit(ctx: &mut JitContext) { codegen::gen_push32_sreg(ctx, regs::DS) }\n\npub fn instr16_40_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, AX); }\npub fn instr32_40_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, EAX); }\npub fn instr16_41_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, CX); }\npub fn instr32_41_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, ECX); }\npub fn instr16_42_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, DX); }\npub fn instr32_42_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, EDX); }\npub fn instr16_43_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, BX); }\npub fn instr32_43_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, EBX); }\npub fn instr16_44_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, SP); }\npub fn instr32_44_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, ESP); }\npub fn instr16_45_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, BP); }\npub fn instr32_45_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, EBP); }\npub fn instr16_46_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, SI); }\npub fn instr32_46_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, ESI); }\npub fn instr16_47_jit(ctx: &mut JitContext) { gen_inc16_r(ctx, DI); }\npub fn instr32_47_jit(ctx: &mut JitContext) { gen_inc32_r(ctx, EDI); }\n\npub fn instr16_48_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, AX); }\npub fn instr32_48_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, EAX); }\npub fn instr16_49_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, CX); }\npub fn instr32_49_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, ECX); }\npub fn instr16_4A_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, DX); }\npub fn instr32_4A_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, EDX); }\npub fn instr16_4B_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, BX); }\npub fn instr32_4B_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, EBX); }\npub fn instr16_4C_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, SP); }\npub fn instr32_4C_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, ESP); }\npub fn instr16_4D_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, BP); }\npub fn instr32_4D_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, EBP); }\npub fn instr16_4E_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, SI); }\npub fn instr32_4E_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, ESI); }\npub fn instr16_4F_jit(ctx: &mut JitContext) { gen_dec16_r(ctx, DI); }\npub fn instr32_4F_jit(ctx: &mut JitContext) { gen_dec32_r(ctx, EDI); }\n\npub fn instr16_50_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, AX); }\npub fn instr32_50_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, EAX); }\npub fn instr16_51_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, CX); }\npub fn instr32_51_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, ECX); }\npub fn instr16_52_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, DX); }\npub fn instr32_52_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, EDX); }\npub fn instr16_53_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, BX); }\npub fn instr32_53_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, EBX); }\npub fn instr16_54_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, SP); }\npub fn instr32_54_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, ESP); }\npub fn instr16_55_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, BP); }\npub fn instr32_55_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, EBP); }\npub fn instr16_56_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, SI); }\npub fn instr32_56_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, ESI); }\npub fn instr16_57_jit(ctx: &mut JitContext) { push16_reg_jit(ctx, DI); }\npub fn instr32_57_jit(ctx: &mut JitContext) { push32_reg_jit(ctx, EDI); }\n\npub fn instr16_58_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, AX); }\npub fn instr32_58_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, EAX); }\npub fn instr16_59_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, CX); }\npub fn instr32_59_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, ECX); }\npub fn instr16_5A_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, DX); }\npub fn instr32_5A_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, EDX); }\npub fn instr16_5B_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, BX); }\npub fn instr32_5B_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, EBX); }\npub fn instr16_5C_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, SP); }\npub fn instr32_5C_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, ESP); }\npub fn instr16_5D_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, BP); }\npub fn instr32_5D_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, EBP); }\npub fn instr16_5E_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, SI); }\npub fn instr32_5E_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, ESI); }\npub fn instr16_5F_jit(ctx: &mut JitContext) { pop16_reg_jit(ctx, DI); }\npub fn instr32_5F_jit(ctx: &mut JitContext) { pop32_reg_jit(ctx, EDI); }\n\npub fn instr16_68_jit(ctx: &mut JitContext, imm16: u32) { push16_imm_jit(ctx, imm16) }\npub fn instr32_68_jit(ctx: &mut JitContext, imm32: u32) { push32_imm_jit(ctx, imm32) }\npub fn instr16_6A_jit(ctx: &mut JitContext, imm16: u32) { push16_imm_jit(ctx, imm16) }\npub fn instr32_6A_jit(ctx: &mut JitContext, imm32: u32) { push32_imm_jit(ctx, imm32) }\n\npub fn instr16_69_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm16: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    ctx.builder.const_i32(imm16 as i32);\n    ctx.builder.call_fn2_ret(\"imul_reg16\");\n    codegen::gen_set_reg16(ctx, r);\n}\npub fn instr16_69_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm16: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    ctx.builder.const_i32(imm16 as i32);\n    ctx.builder.call_fn2_ret(\"imul_reg16\");\n    codegen::gen_set_reg16(ctx, r2);\n}\n\npub fn instr32_69_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm32: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    let value_local = ctx.builder.set_new_local();\n    gen_imul3_reg32(\n        ctx.builder,\n        &ctx.register_locals[r as usize],\n        &value_local,\n        &LocalOrImmediate::Immediate(imm32 as i32),\n    );\n    ctx.builder.free_local(value_local);\n}\npub fn instr32_69_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm32: u32) {\n    gen_imul3_reg32(\n        ctx.builder,\n        &ctx.register_locals[r2 as usize],\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::Immediate(imm32 as i32),\n    );\n}\n\npub fn instr16_6B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8s: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    ctx.builder.const_i32(imm8s as i32);\n    ctx.builder.call_fn2_ret(\"imul_reg16\");\n    codegen::gen_set_reg16(ctx, r);\n}\npub fn instr16_6B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8s: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    ctx.builder.const_i32(imm8s as i32);\n    ctx.builder.call_fn2_ret(\"imul_reg16\");\n    codegen::gen_set_reg16(ctx, r2);\n}\n\npub fn instr32_6B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8s: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    let value_local = ctx.builder.set_new_local();\n    gen_imul3_reg32(\n        ctx.builder,\n        &ctx.register_locals[r as usize],\n        &value_local,\n        &LocalOrImmediate::Immediate(imm8s as i32),\n    );\n    ctx.builder.free_local(value_local);\n}\npub fn instr32_6B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8s: u32) {\n    gen_imul3_reg32(\n        ctx.builder,\n        &ctx.register_locals[r2 as usize],\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::Immediate(imm8s as i32),\n    );\n}\n\n// Code for conditional jumps is generated automatically by the basic block codegen\npub fn instr16_70_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_70_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_71_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_71_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_72_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_72_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_73_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_73_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_74_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_74_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_75_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_75_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_76_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_76_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_77_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_77_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_78_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_78_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_79_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_79_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_7A_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_7A_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_7B_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_7B_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_7C_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_7C_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_7D_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_7D_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_7E_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_7E_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_7F_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_7F_jit(_ctx: &mut JitContext, _imm: u32) {}\n\n// loop/loopz/loopnz/jcxz: Conditional jump is generated in main loop\npub fn instr16_E0_jit(ctx: &mut JitContext, _imm: u32) { codegen::decr_exc_asize(ctx) }\npub fn instr32_E0_jit(ctx: &mut JitContext, _imm: u32) { codegen::decr_exc_asize(ctx) }\npub fn instr16_E1_jit(ctx: &mut JitContext, _imm: u32) { codegen::decr_exc_asize(ctx) }\npub fn instr32_E1_jit(ctx: &mut JitContext, _imm: u32) { codegen::decr_exc_asize(ctx) }\npub fn instr16_E2_jit(ctx: &mut JitContext, _imm: u32) { codegen::decr_exc_asize(ctx) }\npub fn instr32_E2_jit(ctx: &mut JitContext, _imm: u32) { codegen::decr_exc_asize(ctx) }\npub fn instr16_E3_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_E3_jit(_ctx: &mut JitContext, _imm: u32) {}\n\ndefine_instruction_read_write_mem8!(gen_add8, instr_80_0_mem_jit, instr_80_0_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_or8, instr_80_1_mem_jit, instr_80_1_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_adc8, instr_80_2_mem_jit, instr_80_2_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_sbb8, instr_80_3_mem_jit, instr_80_3_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_and8, instr_80_4_mem_jit, instr_80_4_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_sub8, instr_80_5_mem_jit, instr_80_5_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_xor8, instr_80_6_mem_jit, instr_80_6_reg_jit, ximm8);\n\ndefine_instruction_read_write_mem8!(gen_add8, instr_82_0_mem_jit, instr_82_0_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_or8, instr_82_1_mem_jit, instr_82_1_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_adc8, instr_82_2_mem_jit, instr_82_2_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_sbb8, instr_82_3_mem_jit, instr_82_3_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_and8, instr_82_4_mem_jit, instr_82_4_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_sub8, instr_82_5_mem_jit, instr_82_5_reg_jit, ximm8);\ndefine_instruction_read_write_mem8!(gen_xor8, instr_82_6_mem_jit, instr_82_6_reg_jit, ximm8);\n\ndefine_instruction_read_write_mem16!(\"add16\", instr16_81_0_mem_jit, instr16_81_0_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_add32, instr32_81_0_mem_jit, instr32_81_0_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\"or16\", instr16_81_1_mem_jit, instr16_81_1_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_or32, instr32_81_1_mem_jit, instr32_81_1_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\"adc16\", instr16_81_2_mem_jit, instr16_81_2_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_adc32, instr32_81_2_mem_jit, instr32_81_2_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\"sbb16\", instr16_81_3_mem_jit, instr16_81_3_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_sbb32, instr32_81_3_mem_jit, instr32_81_3_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\"and16\", instr16_81_4_mem_jit, instr16_81_4_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_and32, instr32_81_4_mem_jit, instr32_81_4_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\"sub16\", instr16_81_5_mem_jit, instr16_81_5_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_sub32, instr32_81_5_mem_jit, instr32_81_5_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\"xor16\", instr16_81_6_mem_jit, instr16_81_6_reg_jit, imm16);\ndefine_instruction_read_write_mem32!(gen_xor32, instr32_81_6_mem_jit, instr32_81_6_reg_jit, imm32);\n\ndefine_instruction_read_write_mem16!(\n    \"add16\",\n    instr16_83_0_mem_jit,\n    instr16_83_0_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_add32, instr32_83_0_mem_jit, instr32_83_0_reg_jit, imm8s);\n\ndefine_instruction_read_write_mem16!(\n    \"or16\",\n    instr16_83_1_mem_jit,\n    instr16_83_1_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_or32, instr32_83_1_mem_jit, instr32_83_1_reg_jit, imm8s);\n\ndefine_instruction_read_write_mem16!(\n    \"adc16\",\n    instr16_83_2_mem_jit,\n    instr16_83_2_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_adc32, instr32_83_2_mem_jit, instr32_83_2_reg_jit, imm8s);\n\ndefine_instruction_read_write_mem16!(\n    \"sbb16\",\n    instr16_83_3_mem_jit,\n    instr16_83_3_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_sbb32, instr32_83_3_mem_jit, instr32_83_3_reg_jit, imm8s);\n\ndefine_instruction_read_write_mem16!(\n    \"and16\",\n    instr16_83_4_mem_jit,\n    instr16_83_4_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_and32, instr32_83_4_mem_jit, instr32_83_4_reg_jit, imm8s);\n\ndefine_instruction_read_write_mem16!(\n    \"sub16\",\n    instr16_83_5_mem_jit,\n    instr16_83_5_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_sub32, instr32_83_5_mem_jit, instr32_83_5_reg_jit, imm8s);\n\ndefine_instruction_read_write_mem16!(\n    \"xor16\",\n    instr16_83_6_mem_jit,\n    instr16_83_6_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read_write_mem32!(gen_xor32, instr32_83_6_mem_jit, instr32_83_6_reg_jit, imm8s);\n\ndefine_instruction_read8!(gen_cmp8, instr_80_7_mem_jit, instr_80_7_reg_jit, imm8);\ndefine_instruction_read16!(gen_cmp16, instr16_81_7_mem_jit, instr16_81_7_reg_jit, imm16);\ndefine_instruction_read32!(gen_cmp32, instr32_81_7_mem_jit, instr32_81_7_reg_jit, imm32);\n\ndefine_instruction_read8!(gen_cmp8, instr_82_7_mem_jit, instr_82_7_reg_jit, imm8);\n\ndefine_instruction_read16!(\n    gen_cmp16,\n    instr16_83_7_mem_jit,\n    instr16_83_7_reg_jit,\n    imm8s_16bits\n);\ndefine_instruction_read32!(gen_cmp32, instr32_83_7_mem_jit, instr32_83_7_reg_jit, imm8s);\n\ndefine_instruction_read8!(gen_test8, instr_84_mem_jit, instr_84_reg_jit);\ndefine_instruction_read16!(gen_test16, instr16_85_mem_jit, instr16_85_reg_jit);\ndefine_instruction_read32!(gen_test32, instr32_85_mem_jit, instr32_85_reg_jit);\n\npub fn instr_86_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        codegen::gen_safe_read_write(ctx, BitSize::BYTE, &addr, &|ref mut ctx| {\n            codegen::gen_get_reg8(ctx, r);\n            let tmp = ctx.builder.set_new_local();\n            codegen::gen_set_reg8(ctx, r);\n            ctx.builder.get_local(&tmp);\n            ctx.builder.free_local(tmp);\n        });\n    });\n}\npub fn instr_86_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg8(ctx, r2);\n    let tmp = ctx.builder.set_new_local();\n    codegen::gen_get_reg8(ctx, r1);\n    codegen::gen_set_reg8(ctx, r2);\n    ctx.builder.get_local(&tmp);\n    codegen::gen_set_reg8(ctx, r1);\n    ctx.builder.free_local(tmp);\n}\npub fn instr16_87_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        codegen::gen_safe_read_write(ctx, BitSize::WORD, &addr, &|ref mut ctx| {\n            codegen::gen_get_reg16(ctx, r);\n            let tmp = ctx.builder.set_new_local();\n            codegen::gen_set_reg16(ctx, r);\n            ctx.builder.get_local(&tmp);\n            ctx.builder.free_local(tmp);\n        });\n    });\n}\npub fn instr32_87_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        codegen::gen_safe_read_write(ctx, BitSize::DWORD, &addr, &|ref mut ctx| {\n            codegen::gen_get_reg32(ctx, r);\n            let tmp = ctx.builder.set_new_local();\n            codegen::gen_set_reg32(ctx, r);\n            ctx.builder.get_local(&tmp);\n            ctx.builder.free_local(tmp);\n        });\n    });\n}\npub fn instr16_87_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r2);\n    let tmp = ctx.builder.set_new_local();\n    codegen::gen_get_reg16(ctx, r1);\n    codegen::gen_set_reg16(ctx, r2);\n    ctx.builder.get_local(&tmp);\n    codegen::gen_set_reg16(ctx, r1);\n    ctx.builder.free_local(tmp);\n}\npub fn instr32_87_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r2);\n    let tmp = ctx.builder.set_new_local();\n    codegen::gen_get_reg32(ctx, r1);\n    codegen::gen_set_reg32(ctx, r2);\n    ctx.builder.get_local(&tmp);\n    codegen::gen_set_reg32(ctx, r1);\n    ctx.builder.free_local(tmp);\n}\n\npub fn instr_88_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        codegen::gen_get_reg8(ctx, r);\n        let value_local = ctx.builder.set_new_local();\n        codegen::gen_safe_write8(ctx, &addr, &value_local);\n        ctx.builder.free_local(value_local);\n    });\n}\npub fn instr_88_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_reg8_r(ctx, r1, r2);\n}\n\npub fn instr16_89_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        codegen::gen_safe_write16(ctx, addr, &ctx.reg(r));\n    });\n}\npub fn instr16_89_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_reg16_r(ctx, r1, r2);\n}\npub fn instr32_89_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        codegen::gen_safe_write32(ctx, &addr, &ctx.reg(r));\n    });\n}\npub fn instr32_89_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_reg32_r(ctx, r1, r2);\n}\n\npub fn instr_8A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    // Pseudo: reg8[r] = safe_read8(modrm_resolve(modrm_byte));\n    codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n    codegen::gen_set_reg8_unmasked(ctx, r);\n}\npub fn instr_8A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_reg8_r(ctx, r2, r1);\n}\n\npub fn instr16_8B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_set_reg16_unmasked(ctx, r);\n}\npub fn instr16_8B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_reg16_r(ctx, r2, r1);\n}\npub fn instr32_8B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr32_8B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_reg32_r(ctx, r2, r1);\n}\n\npub fn instr16_8C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    if r >= 6 {\n        codegen::gen_trigger_ud(ctx);\n    }\n    else {\n        codegen::gen_get_sreg(ctx, r);\n        let value_local = ctx.builder.set_new_local();\n        codegen::gen_safe_write16(ctx, &address_local, &value_local);\n        ctx.builder.free_local(value_local);\n    }\n    ctx.builder.free_local(address_local);\n}\npub fn instr32_8C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    if r >= 6 {\n        codegen::gen_trigger_ud(ctx);\n    }\n    else {\n        codegen::gen_get_sreg(ctx, r);\n        let value_local = ctx.builder.set_new_local();\n        codegen::gen_safe_write16(ctx, &address_local, &value_local);\n        ctx.builder.free_local(value_local);\n    }\n    ctx.builder.free_local(address_local);\n}\npub fn instr16_8C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    if r2 >= 6 {\n        codegen::gen_trigger_ud(ctx);\n    }\n    else {\n        codegen::gen_get_sreg(ctx, r2);\n        codegen::gen_set_reg16(ctx, r1);\n    }\n}\npub fn instr32_8C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    if r2 >= 6 {\n        codegen::gen_trigger_ud(ctx);\n    }\n    else {\n        codegen::gen_get_sreg(ctx, r2);\n        codegen::gen_set_reg32(ctx, r1);\n    }\n}\n\npub fn instr16_8D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, reg: u32) {\n    ctx.cpu.prefixes |= SEG_PREFIX_ZERO;\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    codegen::gen_set_reg16(ctx, reg);\n}\npub fn instr32_8D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, reg: u32) {\n    if !modrm_byte.is_nop(reg) {\n        ctx.cpu.prefixes |= SEG_PREFIX_ZERO;\n        codegen::gen_modrm_resolve(ctx, modrm_byte);\n        codegen::gen_set_reg32(ctx, reg);\n    }\n}\n\npub fn instr16_8D_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr32_8D_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr16_8F_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_with_esp_offset(ctx, modrm_byte, 2);\n    let address_local = ctx.builder.set_new_local();\n\n    codegen::gen_pop16(ctx);\n    let value_local = ctx.builder.set_new_local();\n\n    // undo the esp change of pop, as safe_write16 can fail\n    codegen::gen_adjust_stack_reg(ctx, (-2i32) as u32);\n\n    codegen::gen_safe_write16(ctx, &address_local, &value_local);\n\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n\n    // finally, actually update esp\n    codegen::gen_adjust_stack_reg(ctx, 2);\n}\npub fn instr16_8F_0_reg_jit(ctx: &mut JitContext, r: u32) { pop16_reg_jit(ctx, r); }\npub fn instr32_8F_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_with_esp_offset(ctx, modrm_byte, 4);\n    let address_local = ctx.builder.set_new_local();\n\n    codegen::gen_pop32s(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_adjust_stack_reg(ctx, (-4i32) as u32);\n\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n\n    codegen::gen_adjust_stack_reg(ctx, 4);\n}\npub fn instr32_8F_0_reg_jit(ctx: &mut JitContext, r: u32) { pop32_reg_jit(ctx, r); }\n\ndefine_instruction_read_write_mem16!(\n    \"rol16\",\n    instr16_C1_0_mem_jit,\n    instr16_C1_0_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_rol32,\n    instr32_C1_0_mem_jit,\n    instr32_C1_0_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"ror16\",\n    instr16_C1_1_mem_jit,\n    instr16_C1_1_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_ror32,\n    instr32_C1_1_mem_jit,\n    instr32_C1_1_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"rcl16\",\n    instr16_C1_2_mem_jit,\n    instr16_C1_2_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_rcl32,\n    instr32_C1_2_mem_jit,\n    instr32_C1_2_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"rcr16\",\n    instr16_C1_3_mem_jit,\n    instr16_C1_3_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_rcr32,\n    instr32_C1_3_mem_jit,\n    instr32_C1_3_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shl16\",\n    instr16_C1_4_mem_jit,\n    instr16_C1_4_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_shl32,\n    instr32_C1_4_mem_jit,\n    instr32_C1_4_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shr16\",\n    instr16_C1_5_mem_jit,\n    instr16_C1_5_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_shr32,\n    instr32_C1_5_mem_jit,\n    instr32_C1_5_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shl16\",\n    instr16_C1_6_mem_jit,\n    instr16_C1_6_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_shl32,\n    instr32_C1_6_mem_jit,\n    instr32_C1_6_reg_jit,\n    imm8_5bits\n);\n\ndefine_instruction_read_write_mem16!(\n    \"sar16\",\n    instr16_C1_7_mem_jit,\n    instr16_C1_7_reg_jit,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    gen_sar32,\n    instr32_C1_7_mem_jit,\n    instr32_C1_7_reg_jit,\n    imm8_5bits\n);\n\npub fn instr16_E8_jit(ctx: &mut JitContext, _imm: u32) {\n    codegen::gen_get_real_eip(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr32_E8_jit(ctx: &mut JitContext, _imm: u32) {\n    codegen::gen_get_real_eip(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push32(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n}\n\npub fn instr16_E9_jit(_ctx: &mut JitContext, _imm: u32) {\n    //\n}\npub fn instr32_E9_jit(_ctx: &mut JitContext, _imm: u32) {\n    //\n}\n\npub fn instr16_C2_jit(ctx: &mut JitContext, imm16: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_pop16(ctx);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n    codegen::gen_adjust_stack_reg(ctx, imm16);\n}\n\npub fn instr32_C2_jit(ctx: &mut JitContext, imm16: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_pop32s(ctx);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n    codegen::gen_adjust_stack_reg(ctx, imm16);\n}\n\npub fn instr16_C3_jit(ctx: &mut JitContext) {\n    ctx.builder.const_i32(0);\n    codegen::gen_pop16(ctx);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\n\npub fn instr32_C3_jit(ctx: &mut JitContext) {\n    ctx.builder.const_i32(0);\n    codegen::gen_pop32s(ctx);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\n\npub fn instr16_C9_jit(ctx: &mut JitContext) { codegen::gen_leave(ctx, false); }\npub fn instr32_C9_jit(ctx: &mut JitContext) { codegen::gen_leave(ctx, true); }\n\npub fn gen_mov_reg8_imm(ctx: &mut JitContext, r: u32, imm: u32) {\n    ctx.builder.const_i32(imm as i32);\n    codegen::gen_set_reg8_unmasked(ctx, r);\n}\n\npub fn instr_B0_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 0, imm) }\npub fn instr_B1_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 1, imm) }\npub fn instr_B2_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 2, imm) }\npub fn instr_B3_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 3, imm) }\npub fn instr_B4_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 4, imm) }\npub fn instr_B5_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 5, imm) }\npub fn instr_B6_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 6, imm) }\npub fn instr_B7_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg8_imm(ctx, 7, imm) }\n\npub fn gen_mov_reg16_imm(ctx: &mut JitContext, r: u32, imm: u32) {\n    ctx.builder.const_i32(imm as i32);\n    codegen::gen_set_reg16_unmasked(ctx, r);\n}\n\npub fn instr16_B8_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 0, imm) }\npub fn instr16_B9_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 1, imm) }\npub fn instr16_BA_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 2, imm) }\npub fn instr16_BB_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 3, imm) }\npub fn instr16_BC_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 4, imm) }\npub fn instr16_BD_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 5, imm) }\npub fn instr16_BE_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 6, imm) }\npub fn instr16_BF_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg16_imm(ctx, 7, imm) }\n\npub fn gen_mov_reg32_imm(ctx: &mut JitContext, r: u32, imm: u32) {\n    ctx.builder.const_i32(imm as i32);\n    codegen::gen_set_reg32(ctx, r);\n}\n\npub fn instr32_B8_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 0, imm) }\npub fn instr32_B9_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 1, imm) }\npub fn instr32_BA_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 2, imm) }\npub fn instr32_BB_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 3, imm) }\npub fn instr32_BC_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 4, imm) }\npub fn instr32_BD_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 5, imm) }\npub fn instr32_BE_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 6, imm) }\npub fn instr32_BF_jit(ctx: &mut JitContext, imm: u32) { gen_mov_reg32_imm(ctx, 7, imm) }\n\ndefine_instruction_read_write_mem8!(\"rol8\", instr_C0_0_mem_jit, instr_C0_0_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"ror8\", instr_C0_1_mem_jit, instr_C0_1_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"rcl8\", instr_C0_2_mem_jit, instr_C0_2_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"rcr8\", instr_C0_3_mem_jit, instr_C0_3_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"shl8\", instr_C0_4_mem_jit, instr_C0_4_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"shr8\", instr_C0_5_mem_jit, instr_C0_5_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"shl8\", instr_C0_6_mem_jit, instr_C0_6_reg_jit, imm8_5bits);\ndefine_instruction_read_write_mem8!(\"sar8\", instr_C0_7_mem_jit, instr_C0_7_reg_jit, imm8_5bits);\n\ndefine_instruction_read_write_mem8!(\"rol8\", instr_D0_0_mem_jit, instr_D0_0_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"ror8\", instr_D0_1_mem_jit, instr_D0_1_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"rcl8\", instr_D0_2_mem_jit, instr_D0_2_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"rcr8\", instr_D0_3_mem_jit, instr_D0_3_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"shl8\", instr_D0_4_mem_jit, instr_D0_4_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"shr8\", instr_D0_5_mem_jit, instr_D0_5_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"shl8\", instr_D0_6_mem_jit, instr_D0_6_reg_jit, constant_one);\ndefine_instruction_read_write_mem8!(\"sar8\", instr_D0_7_mem_jit, instr_D0_7_reg_jit, constant_one);\n\ndefine_instruction_read_write_mem16!(\n    \"rol16\",\n    instr16_D1_0_mem_jit,\n    instr16_D1_0_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_rol32,\n    instr32_D1_0_mem_jit,\n    instr32_D1_0_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"ror16\",\n    instr16_D1_1_mem_jit,\n    instr16_D1_1_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_ror32,\n    instr32_D1_1_mem_jit,\n    instr32_D1_1_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"rcl16\",\n    instr16_D1_2_mem_jit,\n    instr16_D1_2_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_rcl32,\n    instr32_D1_2_mem_jit,\n    instr32_D1_2_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"rcr16\",\n    instr16_D1_3_mem_jit,\n    instr16_D1_3_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_rcr32,\n    instr32_D1_3_mem_jit,\n    instr32_D1_3_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shl16\",\n    instr16_D1_4_mem_jit,\n    instr16_D1_4_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_shl32,\n    instr32_D1_4_mem_jit,\n    instr32_D1_4_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shr16\",\n    instr16_D1_5_mem_jit,\n    instr16_D1_5_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_shr32,\n    instr32_D1_5_mem_jit,\n    instr32_D1_5_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shl16\",\n    instr16_D1_6_mem_jit,\n    instr16_D1_6_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_shl32,\n    instr32_D1_6_mem_jit,\n    instr32_D1_6_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem16!(\n    \"sar16\",\n    instr16_D1_7_mem_jit,\n    instr16_D1_7_reg_jit,\n    constant_one\n);\ndefine_instruction_read_write_mem32!(\n    gen_sar32,\n    instr32_D1_7_mem_jit,\n    instr32_D1_7_reg_jit,\n    constant_one\n);\n\ndefine_instruction_read_write_mem8!(\"rol8\", instr_D2_0_mem_jit, instr_D2_0_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"ror8\", instr_D2_1_mem_jit, instr_D2_1_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"rcl8\", instr_D2_2_mem_jit, instr_D2_2_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"rcr8\", instr_D2_3_mem_jit, instr_D2_3_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"shl8\", instr_D2_4_mem_jit, instr_D2_4_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"shr8\", instr_D2_5_mem_jit, instr_D2_5_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"shl8\", instr_D2_6_mem_jit, instr_D2_6_reg_jit, cl);\ndefine_instruction_read_write_mem8!(\"sar8\", instr_D2_7_mem_jit, instr_D2_7_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"rol16\", instr16_D3_0_mem_jit, instr16_D3_0_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_rol32, instr32_D3_0_mem_jit, instr32_D3_0_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"ror16\", instr16_D3_1_mem_jit, instr16_D3_1_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_ror32, instr32_D3_1_mem_jit, instr32_D3_1_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"rcl16\", instr16_D3_2_mem_jit, instr16_D3_2_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_rcl32, instr32_D3_2_mem_jit, instr32_D3_2_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"rcr16\", instr16_D3_3_mem_jit, instr16_D3_3_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_rcr32, instr32_D3_3_mem_jit, instr32_D3_3_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"shl16\", instr16_D3_4_mem_jit, instr16_D3_4_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_shl32, instr32_D3_4_mem_jit, instr32_D3_4_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"shr16\", instr16_D3_5_mem_jit, instr16_D3_5_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_shr32, instr32_D3_5_mem_jit, instr32_D3_5_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"shl16\", instr16_D3_6_mem_jit, instr16_D3_6_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_shl32, instr32_D3_6_mem_jit, instr32_D3_6_reg_jit, cl);\n\ndefine_instruction_read_write_mem16!(\"sar16\", instr16_D3_7_mem_jit, instr16_D3_7_reg_jit, cl);\ndefine_instruction_read_write_mem32!(gen_sar32, instr32_D3_7_mem_jit, instr32_D3_7_reg_jit, cl);\n\npub fn instr_D7_jit(ctx: &mut JitContext) {\n    if ctx.cpu.asize_32() {\n        codegen::gen_get_reg32(ctx, regs::EBX);\n    }\n    else {\n        codegen::gen_get_reg16(ctx, regs::BX);\n    }\n    codegen::gen_get_reg8(ctx, regs::AL);\n    ctx.builder.add_i32();\n    if !ctx.cpu.asize_32() {\n        ctx.builder.const_i32(0xFFFF);\n        ctx.builder.and_i32();\n    }\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read8(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    codegen::gen_set_reg8(ctx, regs::AL);\n}\n\nfn instr_group_D8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, op: &str) {\n    ctx.builder.const_i32(0);\n    codegen::gen_fpu_load_m32(ctx, modrm_byte);\n    ctx.builder.call_fn3_i32_i64_i32(op)\n}\nfn instr_group_D8_reg_jit(ctx: &mut JitContext, r: u32, op: &str) {\n    ctx.builder.const_i32(0);\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn3_i32_i64_i32(op)\n}\n\npub fn instr_D8_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_D8_mem_jit(ctx, modrm_byte, \"fpu_fadd\")\n}\npub fn instr_D8_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_D8_reg_jit(ctx, r, \"fpu_fadd\")\n}\npub fn instr_D8_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_D8_mem_jit(ctx, modrm_byte, \"fpu_fmul\")\n}\npub fn instr_D8_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_D8_reg_jit(ctx, r, \"fpu_fmul\")\n}\npub fn instr_D8_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_m32(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\")\n}\npub fn instr_D8_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\")\n}\npub fn instr_D8_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_m32(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\")\n}\npub fn instr_D8_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\")\n}\npub fn instr_D8_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_D8_mem_jit(ctx, modrm_byte, \"fpu_fsub\")\n}\npub fn instr_D8_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_D8_reg_jit(ctx, r, \"fpu_fsub\")\n}\npub fn instr_D8_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_D8_mem_jit(ctx, modrm_byte, \"fpu_fsubr\")\n}\npub fn instr_D8_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_D8_reg_jit(ctx, r, \"fpu_fsubr\")\n}\npub fn instr_D8_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_D8_mem_jit(ctx, modrm_byte, \"fpu_fdiv\")\n}\npub fn instr_D8_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_D8_reg_jit(ctx, r, \"fpu_fdiv\")\n}\npub fn instr_D8_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_D8_mem_jit(ctx, modrm_byte, \"fpu_fdivr\")\n}\npub fn instr_D8_7_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_D8_reg_jit(ctx, r, \"fpu_fdivr\")\n}\n\npub fn instr16_D9_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_m32(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_push\");\n}\npub fn instr16_D9_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn2_i64_i32(\"fpu_push\");\n}\npub fn instr32_D9_0_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_0_reg_jit(ctx, r) }\npub fn instr32_D9_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_0_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr16_D9_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1(\"fpu_fxch\");\n}\npub fn instr32_D9_1_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_1_reg_jit(ctx, r) }\npub fn instr32_D9_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_1_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"f80_to_f32\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr16_D9_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    if r != 0 {\n        codegen::gen_trigger_ud(ctx);\n    }\n}\npub fn instr32_D9_2_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_2_reg_jit(ctx, r) }\npub fn instr32_D9_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_2_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"f80_to_f32\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\npub fn instr16_D9_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fstp\", r);\n}\npub fn instr32_D9_3_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_3_reg_jit(ctx, r) }\npub fn instr32_D9_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_3_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n\n    codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n        ctx.builder,\n        ctx.start_of_current_instruction as i32 & 0xFFF,\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"fpu_fldenv32\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n\n    codegen::gen_get_page_fault(ctx.builder);\n    ctx.builder.if_void();\n    codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_label);\n    ctx.builder.block_end();\n}\npub fn instr16_D9_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    match r {\n        0 | 1 | 4 | 5 => {\n            ctx.builder.const_i32(r as i32);\n            ctx.builder.call_fn1(\"instr16_D9_4_reg\");\n        },\n        _ => codegen::gen_trigger_ud(ctx),\n    }\n}\npub fn instr32_D9_4_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_4_reg_jit(ctx, r) }\npub fn instr32_D9_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_4_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    ctx.builder.call_fn1(\"set_control_word\");\n}\npub fn instr16_D9_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    if r == 7 {\n        codegen::gen_trigger_ud(ctx);\n    }\n    else {\n        codegen::gen_fn1_const(ctx.builder, \"instr16_D9_5_reg\", r);\n    }\n}\npub fn instr32_D9_5_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_5_reg_jit(ctx, r) }\npub fn instr32_D9_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_5_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n\n    codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n        ctx.builder,\n        ctx.start_of_current_instruction as i32 & 0xFFF,\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"fpu_fstenv32\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n\n    codegen::gen_get_page_fault(ctx.builder);\n    ctx.builder.if_void();\n    codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_label);\n    ctx.builder.block_end();\n}\npub fn instr16_D9_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr16_D9_6_reg\", r);\n}\npub fn instr32_D9_6_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_6_reg_jit(ctx, r) }\npub fn instr32_D9_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_6_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_D9_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder\n        .const_i32(global_pointers::fpu_control_word as i32);\n    ctx.builder.load_aligned_u16(0);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write16(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr16_D9_7_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr16_D9_7_reg\", r);\n}\npub fn instr32_D9_7_reg_jit(ctx: &mut JitContext, r: u32) { instr16_D9_7_reg_jit(ctx, r) }\npub fn instr32_D9_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_D9_7_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr_DA_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DA_0_reg\", r);\n}\npub fn instr_DA_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DA_1_reg\", r);\n}\npub fn instr_DA_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DA_2_reg\", r);\n}\npub fn instr_DA_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DA_3_reg\", r);\n}\npub fn instr_DA_4_reg_jit(ctx: &mut JitContext, _r: u32) { codegen::gen_trigger_ud(ctx) }\npub fn instr_DA_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    if r == 1 {\n        codegen::gen_fn0_const(ctx.builder, \"fpu_fucompp\")\n    }\n    else {\n        codegen::gen_trigger_ud(ctx)\n    }\n}\npub fn instr_DA_6_reg_jit(ctx: &mut JitContext, _r: u32) { codegen::gen_trigger_ud(ctx) }\npub fn instr_DA_7_reg_jit(ctx: &mut JitContext, _r: u32) { codegen::gen_trigger_ud(ctx) }\n\npub fn instr_group_DA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, op: &str) {\n    ctx.builder.const_i32(0);\n    codegen::gen_fpu_load_i32(ctx, modrm_byte);\n    ctx.builder.call_fn3_i32_i64_i32(op)\n}\npub fn instr_DA_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DA_mem_jit(ctx, modrm_byte, \"fpu_fadd\")\n}\npub fn instr_DA_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DA_mem_jit(ctx, modrm_byte, \"fpu_fmul\")\n}\npub fn instr_DA_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_i32(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\")\n}\npub fn instr_DA_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_i32(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\")\n}\npub fn instr_DA_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DA_mem_jit(ctx, modrm_byte, \"fpu_fsub\")\n}\npub fn instr_DA_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DA_mem_jit(ctx, modrm_byte, \"fpu_fsubr\")\n}\npub fn instr_DA_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DA_mem_jit(ctx, modrm_byte, \"fpu_fdiv\")\n}\npub fn instr_DA_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DA_mem_jit(ctx, modrm_byte, \"fpu_fdivr\")\n}\n\npub fn instr_DB_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_i32(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_push\");\n}\npub fn instr_DB_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DB_0_reg\", r);\n}\n\npub fn instr_DB_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"fpu_truncate_to_i32\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\npub fn instr_DB_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DB_1_reg\", r);\n}\n\npub fn instr_DB_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"fpu_convert_to_i32\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr_DB_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DB_2_reg\", r);\n}\npub fn instr_DB_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"fpu_convert_to_i32\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\npub fn instr_DB_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"instr_DB_3_reg\", r);\n}\n\npub fn instr_DB_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n\n    codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n        ctx.builder,\n        ctx.start_of_current_instruction as i32 & 0xFFF,\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"fpu_fldm80\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n\n    codegen::gen_get_page_fault(ctx.builder);\n    ctx.builder.if_void();\n    codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_label);\n    ctx.builder.block_end();\n}\npub fn instr_DB_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1(\"fpu_fucomi\");\n}\n\npub fn instr_DB_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_DB_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1(\"fpu_fcomi\");\n}\n\nfn instr_group_DC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, op: &str) {\n    ctx.builder.const_i32(0);\n    codegen::gen_fpu_load_m64(ctx, modrm_byte);\n    ctx.builder.call_fn3_i32_i64_i32(op)\n}\nfn instr_group_DC_reg_jit(ctx: &mut JitContext, r: u32, op: &str) {\n    ctx.builder.const_i32(r as i32);\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn3_i32_i64_i32(op)\n}\n\npub fn instr_DC_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DC_mem_jit(ctx, modrm_byte, \"fpu_fadd\")\n}\npub fn instr_DC_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DC_reg_jit(ctx, r, \"fpu_fadd\")\n}\npub fn instr_DC_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DC_mem_jit(ctx, modrm_byte, \"fpu_fmul\")\n}\npub fn instr_DC_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DC_reg_jit(ctx, r, \"fpu_fmul\")\n}\npub fn instr_DC_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_m64(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\")\n}\npub fn instr_DC_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\")\n}\npub fn instr_DC_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_m64(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\")\n}\npub fn instr_DC_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\")\n}\npub fn instr_DC_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DC_mem_jit(ctx, modrm_byte, \"fpu_fsub\")\n}\npub fn instr_DC_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DC_reg_jit(ctx, r, \"fpu_fsub\")\n}\npub fn instr_DC_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DC_mem_jit(ctx, modrm_byte, \"fpu_fsubr\")\n}\npub fn instr_DC_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DC_reg_jit(ctx, r, \"fpu_fsubr\")\n}\npub fn instr_DC_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DC_mem_jit(ctx, modrm_byte, \"fpu_fdiv\")\n}\npub fn instr_DC_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DC_reg_jit(ctx, r, \"fpu_fdiv\")\n}\npub fn instr_DC_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DC_mem_jit(ctx, modrm_byte, \"fpu_fdivr\")\n}\npub fn instr_DC_7_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DC_reg_jit(ctx, r, \"fpu_fdivr\")\n}\n\npub fn instr16_DD_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_m64(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_push\");\n}\npub fn instr16_DD_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_ffree\", r);\n}\npub fn instr32_DD_0_reg_jit(ctx: &mut JitContext, r: u32) { instr16_DD_0_reg_jit(ctx, r) }\npub fn instr32_DD_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_DD_0_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_DD_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret_i64(\"fpu_truncate_to_i64\");\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\npub fn instr16_DD_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fxch\", r);\n}\npub fn instr32_DD_1_reg_jit(ctx: &mut JitContext, r: u32) { instr16_DD_1_reg_jit(ctx, r) }\npub fn instr32_DD_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_DD_1_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_DD_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret_i64(\"f80_to_f64\");\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n}\npub fn instr16_DD_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fst\", r);\n}\npub fn instr32_DD_2_reg_jit(ctx: &mut JitContext, r: u32) { instr16_DD_2_reg_jit(ctx, r) }\npub fn instr32_DD_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_DD_2_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_DD_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret_i64(\"f80_to_f64\");\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n}\npub fn instr16_DD_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fstp\", r);\n}\npub fn instr32_DD_3_reg_jit(ctx: &mut JitContext, r: u32) { instr16_DD_3_reg_jit(ctx, r) }\npub fn instr32_DD_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_DD_3_mem_jit(ctx, modrm_byte)\n}\n\npub fn instr16_DD_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr16_DD_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1(\"fpu_fucomp\");\n}\npub fn instr32_DD_5_reg_jit(ctx: &mut JitContext, r: u32) { instr16_DD_5_reg_jit(ctx, r) }\npub fn instr32_DD_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_DD_5_mem_jit(ctx, modrm_byte)\n}\n\nfn instr_group_DE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, op: &str) {\n    ctx.builder.const_i32(0);\n    codegen::gen_fpu_load_i16(ctx, modrm_byte);\n    ctx.builder.call_fn3_i32_i64_i32(op)\n}\nfn instr_group_DE_reg_jit(ctx: &mut JitContext, r: u32, op: &str) {\n    ctx.builder.const_i32(r as i32);\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn3_i32_i64_i32(op);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\")\n}\n\npub fn instr_DE_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DE_mem_jit(ctx, modrm_byte, \"fpu_fadd\")\n}\npub fn instr_DE_0_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DE_reg_jit(ctx, r, \"fpu_fadd\")\n}\npub fn instr_DE_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DE_mem_jit(ctx, modrm_byte, \"fpu_fmul\")\n}\npub fn instr_DE_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DE_reg_jit(ctx, r, \"fpu_fmul\")\n}\npub fn instr_DE_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_i16(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\")\n}\npub fn instr_DE_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fpu_get_sti(ctx, r);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcom\");\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\")\n}\npub fn instr_DE_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_i16(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\")\n}\npub fn instr_DE_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    if r == 1 {\n        codegen::gen_fpu_get_sti(ctx, r);\n        ctx.builder.call_fn2_i64_i32(\"fpu_fcomp\");\n        codegen::gen_fn0_const(ctx.builder, \"fpu_pop\")\n    }\n    else {\n        codegen::gen_trigger_ud(ctx);\n    }\n}\npub fn instr_DE_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DE_mem_jit(ctx, modrm_byte, \"fpu_fsub\")\n}\npub fn instr_DE_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DE_reg_jit(ctx, r, \"fpu_fsub\")\n}\npub fn instr_DE_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DE_mem_jit(ctx, modrm_byte, \"fpu_fsubr\")\n}\npub fn instr_DE_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DE_reg_jit(ctx, r, \"fpu_fsubr\")\n}\npub fn instr_DE_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DE_mem_jit(ctx, modrm_byte, \"fpu_fdiv\")\n}\npub fn instr_DE_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DE_reg_jit(ctx, r, \"fpu_fdiv\")\n}\npub fn instr_DE_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr_group_DE_mem_jit(ctx, modrm_byte, \"fpu_fdivr\")\n}\npub fn instr_DE_7_reg_jit(ctx: &mut JitContext, r: u32) {\n    instr_group_DE_reg_jit(ctx, r, \"fpu_fdivr\")\n}\n\npub fn instr_DF_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"fpu_truncate_to_i16\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write16(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\npub fn instr_DF_1_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fxch\", r);\n}\n\npub fn instr_DF_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"fpu_convert_to_i16\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write16(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr_DF_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fstp\", r);\n}\npub fn instr_DF_3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret(\"fpu_convert_to_i16\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write16(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\npub fn instr_DF_3_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fstp\", r);\n}\n\npub fn instr_DF_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    dbg_log!(\"fbld\");\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_DF_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    if r == 0 {\n        ctx.builder.call_fn0_ret(\"fpu_load_status_word\");\n        codegen::gen_set_reg16(ctx, regs::AX);\n    }\n    else {\n        codegen::gen_trigger_ud(ctx);\n    };\n}\n\npub fn instr_DF_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_fpu_load_i64(ctx, modrm_byte);\n    ctx.builder.call_fn2_i64_i32(\"fpu_push\");\n}\npub fn instr_DF_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_fn1_const(ctx.builder, \"fpu_fucomip\", r);\n}\n\npub fn instr_DF_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n\n    codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n        ctx.builder,\n        ctx.start_of_current_instruction as i32 & 0xFFF,\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"fpu_fbstp\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n\n    codegen::gen_get_page_fault(ctx.builder);\n    ctx.builder.if_void();\n    codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_label);\n    ctx.builder.block_end();\n}\npub fn instr_DF_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1(\"fpu_fcomip\");\n}\n\npub fn instr_DF_7_reg_jit(ctx: &mut JitContext, _r: u32) { codegen::gen_trigger_ud(ctx); }\npub fn instr_DF_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_fpu_get_sti(ctx, 0);\n    ctx.builder.call_fn2_i64_i32_ret_i64(\"fpu_convert_to_i64\");\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n    codegen::gen_fn0_const(ctx.builder, \"fpu_pop\");\n}\n\npub fn instr16_EB_jit(_ctx: &mut JitContext, _imm8: u32) {\n    //\n}\n\npub fn instr32_EB_jit(_ctx: &mut JitContext, _imm8: u32) {\n    // jmp near\n}\n\ndefine_instruction_read8!(gen_test8, instr_F6_0_mem_jit, instr_F6_0_reg_jit, imm8);\ndefine_instruction_read16!(\n    gen_test16,\n    instr16_F7_0_mem_jit,\n    instr16_F7_0_reg_jit,\n    imm16\n);\ndefine_instruction_read32!(\n    gen_test32,\n    instr32_F7_0_mem_jit,\n    instr32_F7_0_reg_jit,\n    imm32\n);\n\npub fn instr_F6_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n    instr_F6_0_mem_jit(ctx, modrm_byte, imm)\n}\npub fn instr_F6_1_reg_jit(ctx: &mut JitContext, r: u32, imm: u32) {\n    instr_F6_0_reg_jit(ctx, r, imm)\n}\npub fn instr16_F7_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n    instr16_F7_0_mem_jit(ctx, modrm_byte, imm)\n}\npub fn instr16_F7_1_reg_jit(ctx: &mut JitContext, r: u32, imm: u32) {\n    instr16_F7_0_reg_jit(ctx, r, imm)\n}\npub fn instr32_F7_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n    instr32_F7_0_mem_jit(ctx, modrm_byte, imm)\n}\npub fn instr32_F7_1_reg_jit(ctx: &mut JitContext, r: u32, imm: u32) {\n    instr32_F7_0_reg_jit(ctx, r, imm)\n}\n\ndefine_instruction_read_write_mem8!(\"not8\", instr_F6_2_mem_jit, instr_F6_2_reg_jit, none);\ndefine_instruction_read_write_mem8!(\"neg8\", instr_F6_3_mem_jit, instr_F6_3_reg_jit, none);\n\ndefine_instruction_read_write_mem16!(gen_not16, instr16_F7_2_mem_jit, instr16_F7_2_reg_jit, none);\ndefine_instruction_read_write_mem32!(gen_not32, instr32_F7_2_mem_jit, instr32_F7_2_reg_jit, none);\ndefine_instruction_read_write_mem16!(gen_neg16, instr16_F7_3_mem_jit, instr16_F7_3_reg_jit, none);\ndefine_instruction_read_write_mem32!(gen_neg32, instr32_F7_3_mem_jit, instr32_F7_3_reg_jit, none);\n\npub fn instr16_F7_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"mul16\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n}\npub fn instr16_F7_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg16(ctx, r);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"mul16\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n}\npub fn instr32_F7_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    gen_mul32(ctx);\n}\npub fn instr32_F7_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg32(ctx, r);\n    gen_mul32(ctx);\n}\n\npub fn instr16_F7_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"imul16\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n}\npub fn instr16_F7_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg16(ctx, r);\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1(\"imul16\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n}\npub fn instr32_F7_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    gen_imul32(ctx);\n}\npub fn instr32_F7_5_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg32(ctx, r);\n    gen_imul32(ctx);\n}\n\npub fn instr16_F7_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1_ret(\"div16_without_fault\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_de(ctx);\n    ctx.builder.block_end();\n}\npub fn instr16_F7_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg16(ctx, r);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1_ret(\"div16_without_fault\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_de(ctx);\n    ctx.builder.block_end();\n}\n\npub fn instr32_F7_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    if false {\n        codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        ctx.builder.call_fn1_ret(\"div32_without_fault\");\n        codegen::gen_move_registers_from_memory_to_locals(ctx);\n        ctx.builder.eqz_i32();\n        ctx.builder.if_void();\n        codegen::gen_trigger_de(ctx);\n        ctx.builder.block_end();\n    }\n    else {\n        codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n        let source_operand = ctx.builder.set_new_local();\n        gen_div32(ctx, &source_operand);\n        ctx.builder.free_local(source_operand);\n    }\n}\npub fn instr32_F7_6_reg_jit(ctx: &mut JitContext, r: u32) {\n    if false {\n        codegen::gen_get_reg32(ctx, r);\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        ctx.builder.call_fn1_ret(\"div32_without_fault\");\n        codegen::gen_move_registers_from_memory_to_locals(ctx);\n        ctx.builder.eqz_i32();\n        ctx.builder.if_void();\n        codegen::gen_trigger_de(ctx);\n        ctx.builder.block_end();\n    }\n    else {\n        gen_div32(ctx, &ctx.reg(r));\n    }\n}\n\npub fn instr16_F7_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1_ret(\"idiv16_without_fault\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_de(ctx);\n    ctx.builder.block_end();\n}\npub fn instr16_F7_7_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg16(ctx, r);\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1_ret(\"idiv16_without_fault\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_de(ctx);\n    ctx.builder.block_end();\n}\npub fn instr32_F7_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1_ret(\"idiv32_without_fault\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_de(ctx);\n    ctx.builder.block_end();\n}\npub fn instr32_F7_7_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg32(ctx, r);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn1_ret(\"idiv32_without_fault\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_de(ctx);\n    ctx.builder.block_end();\n}\n\npub fn instr_F8_jit(ctx: &mut JitContext) {\n    codegen::gen_clear_flags_changed_bits(ctx.builder, 1);\n    codegen::gen_clear_flags_bits(ctx.builder, 1);\n}\npub fn instr_F9_jit(ctx: &mut JitContext) {\n    codegen::gen_clear_flags_changed_bits(ctx.builder, 1);\n    codegen::gen_set_flags_bits(ctx.builder, 1);\n}\n\npub fn instr_FA_jit(ctx: &mut JitContext) {\n    ctx.builder.call_fn0_ret(\"instr_FA_without_fault\");\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_gp(ctx, 0);\n    ctx.builder.block_end();\n}\n\npub fn instr_FB_jit(ctx: &mut JitContext) {\n    ctx.builder.call_fn0_ret(\"instr_FB_without_fault\");\n    ctx.builder.eqz_i32();\n    ctx.builder.if_void();\n    codegen::gen_trigger_gp(ctx, 0);\n    ctx.builder.block_end();\n    // handle_irqs is specially handled in jit to be called one instruction after this one\n}\n\npub fn instr_FC_jit(ctx: &mut JitContext) {\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(!FLAG_DIRECTION);\n    ctx.builder.and_i32();\n    ctx.builder.store_aligned_i32(0);\n}\n\npub fn instr_FD_jit(ctx: &mut JitContext) {\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(FLAG_DIRECTION);\n    ctx.builder.or_i32();\n    ctx.builder.store_aligned_i32(0);\n}\n\ndefine_instruction_read_write_mem8!(\"inc8\", instr_FE_0_mem_jit, instr_FE_0_reg_jit, none);\ndefine_instruction_read_write_mem8!(\"dec8\", instr_FE_1_mem_jit, instr_FE_1_reg_jit, none);\n\ndefine_instruction_read_write_mem16!(gen_inc16, instr16_FF_0_mem_jit, instr16_FF_0_reg_jit, none);\ndefine_instruction_read_write_mem32!(gen_inc32, instr32_FF_0_mem_jit, instr32_FF_0_reg_jit, none);\n\ndefine_instruction_read_write_mem16!(gen_dec16, instr16_FF_1_mem_jit, instr16_FF_1_reg_jit, none);\ndefine_instruction_read_write_mem32!(gen_dec32, instr32_FF_1_mem_jit, instr32_FF_1_reg_jit, none);\n\npub fn instr16_FF_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_add_cs_offset(ctx);\n    let new_eip = ctx.builder.set_new_local();\n\n    codegen::gen_get_real_eip(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n\n    ctx.builder.const_i32(0);\n    ctx.builder.get_local(&new_eip);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n    ctx.builder.free_local(new_eip);\n}\npub fn instr16_FF_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_real_eip(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n\n    ctx.builder.const_i32(0);\n    codegen::gen_get_reg16(ctx, r);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\npub fn instr32_FF_2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    codegen::gen_add_cs_offset(ctx);\n    let new_eip = ctx.builder.set_new_local();\n\n    codegen::gen_get_real_eip(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push32(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n\n    ctx.builder.const_i32(0);\n    ctx.builder.get_local(&new_eip);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n    ctx.builder.free_local(new_eip);\n}\npub fn instr32_FF_2_reg_jit(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_real_eip(ctx);\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_push32(ctx, &value_local);\n    ctx.builder.free_local(value_local);\n\n    ctx.builder.const_i32(0);\n    codegen::gen_get_reg32(ctx, r);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\n\npub fn instr16_FF_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder.const_i32(0);\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\npub fn instr16_FF_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_get_reg16(ctx, r);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\npub fn instr32_FF_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    ctx.builder.const_i32(0);\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\npub fn instr32_FF_4_reg_jit(ctx: &mut JitContext, r: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_get_reg32(ctx, r);\n    codegen::gen_add_cs_offset(ctx);\n    ctx.builder\n        .store_aligned_i32(global_pointers::instruction_pointer as u32);\n}\n\npub fn instr16_FF_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    push16_mem_jit(ctx, modrm_byte)\n}\npub fn instr16_FF_6_reg_jit(ctx: &mut JitContext, r: u32) { push16_reg_jit(ctx, r) }\npub fn instr32_FF_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    push32_mem_jit(ctx, modrm_byte)\n}\npub fn instr32_FF_6_reg_jit(ctx: &mut JitContext, r: u32) { push32_reg_jit(ctx, r) }\n\n// Code for conditional jumps is generated automatically by the basic block codegen\npub fn instr16_0F80_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F81_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F82_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F83_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F84_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F85_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F86_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F87_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F88_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F89_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F8A_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F8B_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F8C_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F8D_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F8E_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr16_0F8F_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F80_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F81_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F82_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F83_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F84_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F85_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F86_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F87_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F88_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F89_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F8A_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F8B_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F8C_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F8D_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F8E_jit(_ctx: &mut JitContext, _imm: u32) {}\npub fn instr32_0F8F_jit(_ctx: &mut JitContext, _imm: u32) {}\n\npub fn instr_90_jit(_ctx: &mut JitContext) {}\n\nfn gen_xchg_reg16(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg16(ctx, r);\n    let tmp = ctx.builder.set_new_local();\n    codegen::gen_get_reg16(ctx, regs::AX);\n    codegen::gen_set_reg16(ctx, r);\n    ctx.builder.get_local(&tmp);\n    codegen::gen_set_reg16(ctx, regs::AX);\n    ctx.builder.free_local(tmp);\n}\n\nfn gen_xchg_reg32(ctx: &mut JitContext, r: u32) {\n    codegen::gen_get_reg32(ctx, r);\n    let tmp = ctx.builder.set_new_local();\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    codegen::gen_set_reg32(ctx, r);\n    ctx.builder.get_local(&tmp);\n    codegen::gen_set_reg32(ctx, regs::EAX);\n    ctx.builder.free_local(tmp);\n}\n\npub fn instr16_91_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::CX); }\npub fn instr16_92_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::DX); }\npub fn instr16_93_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::BX); }\npub fn instr16_94_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::SP); }\npub fn instr16_95_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::BP); }\npub fn instr16_96_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::SI); }\npub fn instr16_97_jit(ctx: &mut JitContext) { gen_xchg_reg16(ctx, regs::DI); }\n\npub fn instr32_91_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::CX); }\npub fn instr32_92_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::DX); }\npub fn instr32_93_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::BX); }\npub fn instr32_94_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::SP); }\npub fn instr32_95_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::BP); }\npub fn instr32_96_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::SI); }\npub fn instr32_97_jit(ctx: &mut JitContext) { gen_xchg_reg32(ctx, regs::DI); }\n\npub fn instr16_98_jit(ctx: &mut JitContext) {\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    codegen::sign_extend_i8(ctx.builder);\n    codegen::gen_set_reg16(ctx, regs::AX);\n}\npub fn instr32_98_jit(ctx: &mut JitContext) {\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_set_reg32(ctx, regs::EAX);\n}\n\npub fn instr16_99_jit(ctx: &mut JitContext) {\n    codegen::gen_get_reg16(ctx, regs::AX);\n    ctx.builder.const_i32(16);\n    ctx.builder.shl_i32();\n    ctx.builder.const_i32(31);\n    ctx.builder.shr_s_i32();\n    codegen::gen_set_reg16(ctx, regs::DX);\n}\npub fn instr32_99_jit(ctx: &mut JitContext) {\n    codegen::gen_get_reg32(ctx, regs::EAX);\n    ctx.builder.const_i32(31);\n    ctx.builder.shr_s_i32();\n    codegen::gen_set_reg32(ctx, regs::EDX);\n}\n\nfn gen_pushf_popf_check(ctx: &mut JitContext) {\n    // 0 != *flags & FLAG_VM && getiopl() < 3\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(FLAG_VM);\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(FLAG_VM);\n    ctx.builder.eq_i32();\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(FLAG_IOPL);\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(FLAG_IOPL);\n    ctx.builder.ne_i32();\n    ctx.builder.and_i32();\n}\n\npub fn instr16_9C_jit(ctx: &mut JitContext) {\n    gen_pushf_popf_check(ctx);\n    ctx.builder.if_void();\n    codegen::gen_trigger_gp(ctx, 0);\n    ctx.builder.else_();\n    ctx.builder.call_fn0_ret(\"get_eflags\");\n    let value = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &value);\n    ctx.builder.block_end();\n    ctx.builder.free_local(value);\n}\npub fn instr32_9C_jit(ctx: &mut JitContext) {\n    gen_pushf_popf_check(ctx);\n    ctx.builder.if_void();\n    codegen::gen_trigger_gp(ctx, 0);\n    ctx.builder.else_();\n    ctx.builder.call_fn0_ret(\"get_eflags\");\n    ctx.builder.const_i32(0xFCFFFF);\n    ctx.builder.and_i32();\n    let value = ctx.builder.set_new_local();\n    codegen::gen_push32(ctx, &value);\n    ctx.builder.block_end();\n    ctx.builder.free_local(value);\n}\n\nfn gen_popf(ctx: &mut JitContext, is_32: bool) {\n    gen_pushf_popf_check(ctx);\n    ctx.builder.if_void();\n    codegen::gen_trigger_gp(ctx, 0);\n    ctx.builder.else_();\n\n    codegen::gen_get_flags(ctx.builder);\n    let old_eflags = ctx.builder.set_new_local();\n\n    if is_32 {\n        codegen::gen_pop32s(ctx);\n    }\n    else {\n        ctx.builder.get_local(&old_eflags);\n        ctx.builder.const_i32(!0xFFFF);\n        ctx.builder.and_i32();\n        codegen::gen_pop16(ctx);\n        ctx.builder.or_i32();\n    }\n\n    ctx.builder.call_fn1(\"update_eflags\");\n\n    ctx.builder.get_local(&old_eflags);\n    ctx.builder.free_local(old_eflags);\n    ctx.builder.const_i32(FLAG_INTERRUPT);\n    ctx.builder.and_i32();\n    ctx.builder.eqz_i32();\n\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(FLAG_INTERRUPT);\n    ctx.builder.and_i32();\n    ctx.builder.eqz_i32();\n    ctx.builder.eqz_i32();\n\n    ctx.builder.and_i32();\n    ctx.builder.if_void();\n    {\n        codegen::gen_set_eip_to_after_current_instruction(ctx);\n        codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        codegen::gen_fn0_const(ctx.builder, \"handle_irqs\");\n\n        codegen::gen_update_instruction_counter(ctx);\n        ctx.builder.return_();\n    }\n    ctx.builder.block_end();\n\n    ctx.builder.block_end();\n}\n\npub fn instr16_9D_jit(ctx: &mut JitContext) { gen_popf(ctx, false) }\npub fn instr32_9D_jit(ctx: &mut JitContext) { gen_popf(ctx, true) }\n\npub fn instr_9E_jit(ctx: &mut JitContext) {\n    ctx.builder.const_i32(global_pointers::flags as i32);\n    codegen::gen_get_flags(ctx.builder);\n    ctx.builder.const_i32(!0xFF);\n    ctx.builder.and_i32();\n    codegen::gen_get_reg8(ctx, regs::AH);\n    ctx.builder.or_i32();\n    ctx.builder.const_i32(FLAGS_MASK);\n    ctx.builder.and_i32();\n    ctx.builder.const_i32(FLAGS_DEFAULT);\n    ctx.builder.or_i32();\n    ctx.builder.store_aligned_i32(0);\n\n    codegen::gen_clear_flags_changed_bits(ctx.builder, 0xFF);\n}\n\npub fn instr_9F_jit(ctx: &mut JitContext) {\n    ctx.builder.call_fn0_ret(\"get_eflags\");\n    codegen::gen_set_reg8(ctx, regs::AH);\n}\n\npub fn instr_A0_jit(ctx: &mut JitContext, immaddr: u32) {\n    ctx.builder.const_i32(immaddr as i32);\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read8(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    codegen::gen_set_reg8(ctx, regs::AL);\n}\npub fn instr16_A1_jit(ctx: &mut JitContext, immaddr: u32) {\n    ctx.builder.const_i32(immaddr as i32);\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read16(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    codegen::gen_set_reg16(ctx, regs::AX);\n}\npub fn instr32_A1_jit(ctx: &mut JitContext, immaddr: u32) {\n    ctx.builder.const_i32(immaddr as i32);\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read32(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    codegen::gen_set_reg32(ctx, regs::EAX);\n}\n\npub fn instr_A2_jit(ctx: &mut JitContext, immaddr: u32) {\n    ctx.builder.const_i32(immaddr as i32);\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write8(ctx, &address_local, &ctx.reg(regs::EAX));\n    ctx.builder.free_local(address_local);\n}\npub fn instr16_A3_jit(ctx: &mut JitContext, immaddr: u32) {\n    ctx.builder.const_i32(immaddr as i32);\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write16(ctx, &address_local, &ctx.reg(regs::EAX));\n    ctx.builder.free_local(address_local);\n}\npub fn instr32_A3_jit(ctx: &mut JitContext, immaddr: u32) {\n    ctx.builder.const_i32(immaddr as i32);\n    jit_add_seg_offset(ctx, regs::DS);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &ctx.reg(regs::EAX));\n    ctx.builder.free_local(address_local);\n}\n\npub fn instr_A8_jit(ctx: &mut JitContext, imm8: u32) {\n    gen_test8(ctx, &ctx.reg(0), &LocalOrImmediate::Immediate(imm8 as i32));\n}\n\npub fn instr16_A9_jit(ctx: &mut JitContext, imm16: u32) {\n    gen_test16(ctx, &ctx.reg(0), &LocalOrImmediate::Immediate(imm16 as i32));\n}\n\npub fn instr32_A9_jit(ctx: &mut JitContext, imm32: u32) {\n    gen_test32(ctx, &ctx.reg(0), &LocalOrImmediate::Immediate(imm32 as i32));\n}\n\n#[derive(PartialEq)]\nenum String {\n    INS,\n    OUTS,\n    MOVS,\n    CMPS,\n    STOS,\n    LODS,\n    SCAS,\n}\nfn gen_string_ins(ctx: &mut JitContext, ins: String, size: u8, prefix: u8) {\n    dbg_assert!(prefix == 0 || prefix == 0xF2 || prefix == 0xF3);\n    dbg_assert!(size == 8 || size == 16 || size == 32);\n\n    if prefix == 0 {\n        fn get_direction(ctx: &mut JitContext, size: u8) {\n            let bytes: i32 = (size / 8).into();\n            dbg_assert!(bytes == 1 || bytes == 2 || bytes == 4);\n            ctx.builder.const_i32(-bytes);\n            ctx.builder.const_i32(bytes);\n            codegen::gen_get_flags(ctx.builder);\n            ctx.builder.const_i32(FLAG_DIRECTION);\n            ctx.builder.and_i32();\n            ctx.builder.select();\n        }\n\n        match &ins {\n            String::LODS => {\n                if ctx.cpu.asize_32() {\n                    codegen::gen_get_reg32(ctx, regs::ESI);\n                }\n                else {\n                    codegen::gen_get_reg16(ctx, regs::ESI);\n                }\n                jit_add_seg_offset(ctx, regs::DS);\n                let address_local = ctx.builder.set_new_local();\n                if size == 8 {\n                    codegen::gen_safe_read8(ctx, &address_local);\n                    ctx.builder.free_local(address_local);\n                    codegen::gen_set_reg8_unmasked(ctx, regs::AL);\n                }\n                else if size == 16 {\n                    codegen::gen_safe_read16(ctx, &address_local);\n                    ctx.builder.free_local(address_local);\n                    codegen::gen_set_reg16(ctx, regs::AX);\n                }\n                else {\n                    codegen::gen_safe_read32(ctx, &address_local);\n                    ctx.builder.free_local(address_local);\n                    codegen::gen_set_reg32(ctx, regs::EAX);\n                }\n\n                codegen::gen_get_reg32(ctx, regs::ESI);\n                get_direction(ctx, size);\n                ctx.builder.add_i32();\n                if ctx.cpu.asize_32() {\n                    codegen::gen_set_reg32(ctx, regs::ESI);\n                }\n                else {\n                    codegen::gen_set_reg16(ctx, regs::ESI);\n                }\n                return;\n            },\n            String::SCAS => {\n                if ctx.cpu.asize_32() {\n                    codegen::gen_get_reg32(ctx, regs::EDI);\n                }\n                else {\n                    codegen::gen_get_reg16(ctx, regs::EDI);\n                }\n                jit_add_seg_offset_no_override(ctx, regs::ES);\n                let address_local = ctx.builder.set_new_local();\n                if size == 8 {\n                    codegen::gen_safe_read8(ctx, &address_local);\n                    ctx.builder.free_local(address_local);\n                    let value = ctx.builder.set_new_local();\n                    gen_cmp8(\n                        ctx,\n                        &ctx.reg(regs::EAX),\n                        &LocalOrImmediate::WasmLocal(&value),\n                    );\n                    ctx.builder.free_local(value);\n                }\n                else if size == 16 {\n                    codegen::gen_safe_read16(ctx, &address_local);\n                    ctx.builder.free_local(address_local);\n                    let value = ctx.builder.set_new_local();\n                    gen_cmp16(\n                        ctx,\n                        &ctx.reg(regs::EAX),\n                        &LocalOrImmediate::WasmLocal(&value),\n                    );\n                    ctx.builder.free_local(value);\n                }\n                else {\n                    codegen::gen_safe_read32(ctx, &address_local);\n                    ctx.builder.free_local(address_local);\n                    let value = ctx.builder.set_new_local();\n                    gen_cmp32(\n                        ctx,\n                        &ctx.reg(regs::EAX),\n                        &LocalOrImmediate::WasmLocal(&value),\n                    );\n                    ctx.builder.free_local(value);\n                }\n\n                codegen::gen_get_reg32(ctx, regs::EDI);\n                get_direction(ctx, size);\n                ctx.builder.add_i32();\n                if ctx.cpu.asize_32() {\n                    codegen::gen_set_reg32(ctx, regs::EDI);\n                }\n                else {\n                    codegen::gen_set_reg16(ctx, regs::EDI);\n                }\n                return;\n            },\n            String::STOS => {\n                if ctx.cpu.asize_32() {\n                    codegen::gen_get_reg32(ctx, regs::EDI);\n                }\n                else {\n                    codegen::gen_get_reg16(ctx, regs::EDI);\n                }\n                jit_add_seg_offset_no_override(ctx, regs::ES);\n                let address_local = ctx.builder.set_new_local();\n                if size == 8 {\n                    codegen::gen_safe_write8(ctx, &address_local, &ctx.reg(regs::AL));\n                    ctx.builder.free_local(address_local);\n                }\n                else if size == 16 {\n                    codegen::gen_safe_write16(ctx, &address_local, &ctx.reg(regs::AX));\n                    ctx.builder.free_local(address_local);\n                }\n                else {\n                    codegen::gen_safe_write32(ctx, &address_local, &ctx.reg(regs::EAX));\n                    ctx.builder.free_local(address_local);\n                }\n\n                codegen::gen_get_reg32(ctx, regs::EDI);\n                get_direction(ctx, size);\n                ctx.builder.add_i32();\n                if ctx.cpu.asize_32() {\n                    codegen::gen_set_reg32(ctx, regs::EDI);\n                }\n                else {\n                    codegen::gen_set_reg16(ctx, regs::EDI);\n                }\n                return;\n            },\n            String::MOVS => {\n                if ctx.cpu.asize_32() {\n                    codegen::gen_get_reg32(ctx, regs::EDI);\n                }\n                else {\n                    codegen::gen_get_reg16(ctx, regs::EDI);\n                }\n                jit_add_seg_offset_no_override(ctx, regs::ES);\n                let dest_address = ctx.builder.set_new_local();\n\n                if ctx.cpu.asize_32() {\n                    codegen::gen_get_reg32(ctx, regs::ESI);\n                }\n                else {\n                    codegen::gen_get_reg16(ctx, regs::ESI);\n                }\n                jit_add_seg_offset(ctx, regs::DS);\n                let source_address = ctx.builder.set_new_local();\n\n                if size == 8 {\n                    codegen::gen_safe_read8(ctx, &source_address);\n                    ctx.builder.free_local(source_address);\n                    let value = ctx.builder.set_new_local();\n                    codegen::gen_safe_write8(ctx, &dest_address, &value);\n                    ctx.builder.free_local(value);\n                }\n                else if size == 16 {\n                    codegen::gen_safe_read16(ctx, &source_address);\n                    ctx.builder.free_local(source_address);\n                    let value = ctx.builder.set_new_local();\n                    codegen::gen_safe_write16(ctx, &dest_address, &value);\n                    ctx.builder.free_local(value);\n                }\n                else {\n                    codegen::gen_safe_read32(ctx, &source_address);\n                    ctx.builder.free_local(source_address);\n                    let value = ctx.builder.set_new_local();\n                    codegen::gen_safe_write32(ctx, &dest_address, &value);\n                    ctx.builder.free_local(value);\n                }\n\n                ctx.builder.free_local(dest_address);\n\n                codegen::gen_get_reg32(ctx, regs::EDI);\n                get_direction(ctx, size);\n                ctx.builder.add_i32();\n                if ctx.cpu.asize_32() {\n                    codegen::gen_set_reg32(ctx, regs::EDI);\n                }\n                else {\n                    codegen::gen_set_reg16(ctx, regs::EDI);\n                }\n\n                codegen::gen_get_reg32(ctx, regs::ESI);\n                get_direction(ctx, size);\n                ctx.builder.add_i32();\n                if ctx.cpu.asize_32() {\n                    codegen::gen_set_reg32(ctx, regs::ESI);\n                }\n                else {\n                    codegen::gen_set_reg16(ctx, regs::ESI);\n                }\n                return;\n            },\n            _ => {},\n        }\n    }\n\n    let mut args = 0;\n    args += 1;\n    ctx.builder.const_i32(ctx.cpu.asize_32() as i32);\n\n    if ins == String::OUTS || ins == String::CMPS || ins == String::LODS || ins == String::MOVS {\n        // TODO: check es/ds is null (only if rep && count!=0)\n        args += 1;\n        let prefix = ctx.cpu.prefixes & PREFIX_MASK_SEGMENT;\n        dbg_assert!(prefix != SEG_PREFIX_ZERO);\n        let seg = if prefix != 0 { (prefix - 1) as u32 } else { regs::DS };\n        ctx.builder.const_i32(seg as i32);\n    }\n\n    let name = format!(\n        \"{}{}{}\",\n        match ins {\n            String::INS => \"ins\",\n            String::OUTS => \"outs\",\n            String::MOVS => \"movs\",\n            String::CMPS => \"cmps\",\n            String::STOS => \"stos\",\n            String::LODS => \"lods\",\n            String::SCAS => \"scas\",\n        },\n        if size == 8 {\n            \"b\"\n        }\n        else if size == 16 {\n            \"w\"\n        }\n        else {\n            \"d\"\n        },\n        if prefix == 0xF2 || prefix == 0xF3 {\n            match ins {\n                String::CMPS | String::SCAS => {\n                    if prefix == 0xF2 {\n                        \"_repnz\"\n                    }\n                    else {\n                        \"_repz\"\n                    }\n                },\n                _ => \"_rep\",\n            }\n        }\n        else {\n            \"_no_rep\"\n        }\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    if args == 1 {\n        ctx.builder.call_fn1(&name)\n    }\n    else if args == 2 {\n        ctx.builder.call_fn2(&name)\n    }\n    else {\n        dbg_assert!(false);\n    }\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n}\n\npub fn instr_6C_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 8, 0) }\npub fn instr_F26C_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 8, 0xF2) }\npub fn instr_F36C_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 8, 0xF3) }\npub fn instr16_6D_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 16, 0) }\npub fn instr16_F26D_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 16, 0xF2) }\npub fn instr16_F36D_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 16, 0xF3) }\npub fn instr32_6D_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 32, 0) }\npub fn instr32_F26D_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 32, 0xF2) }\npub fn instr32_F36D_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::INS, 32, 0xF3) }\npub fn instr_6E_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 8, 0) }\npub fn instr_F26E_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 8, 0xF2) }\npub fn instr_F36E_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 8, 0xF3) }\npub fn instr16_6F_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 16, 0) }\npub fn instr16_F26F_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 16, 0xF2) }\npub fn instr16_F36F_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 16, 0xF3) }\npub fn instr32_6F_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 32, 0) }\npub fn instr32_F26F_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 32, 0xF2) }\npub fn instr32_F36F_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::OUTS, 32, 0xF3) }\npub fn instr_A4_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 8, 0) }\npub fn instr_F2A4_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 8, 0xF2) }\npub fn instr_F3A4_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 8, 0xF3) }\npub fn instr16_A5_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 16, 0) }\npub fn instr16_F2A5_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 16, 0xF2) }\npub fn instr16_F3A5_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 16, 0xF3) }\npub fn instr32_A5_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 32, 0) }\npub fn instr32_F2A5_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 32, 0xF2) }\npub fn instr32_F3A5_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::MOVS, 32, 0xF3) }\npub fn instr_A6_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 8, 0) }\npub fn instr_F2A6_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 8, 0xF2) }\npub fn instr_F3A6_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 8, 0xF3) }\npub fn instr16_A7_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 16, 0) }\npub fn instr16_F2A7_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 16, 0xF2) }\npub fn instr16_F3A7_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 16, 0xF3) }\npub fn instr32_A7_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 32, 0) }\npub fn instr32_F2A7_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 32, 0xF2) }\npub fn instr32_F3A7_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::CMPS, 32, 0xF3) }\npub fn instr_AA_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 8, 0) }\npub fn instr_F2AA_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 8, 0xF2) }\npub fn instr_F3AA_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 8, 0xF3) }\npub fn instr16_AB_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 16, 0) }\npub fn instr16_F2AB_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 16, 0xF2) }\npub fn instr16_F3AB_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 16, 0xF3) }\npub fn instr32_AB_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 32, 0) }\npub fn instr32_F2AB_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 32, 0xF2) }\npub fn instr32_F3AB_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::STOS, 32, 0xF3) }\npub fn instr_AC_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 8, 0) }\npub fn instr_F2AC_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 8, 0xF2) }\npub fn instr_F3AC_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 8, 0xF3) }\npub fn instr16_AD_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 16, 0) }\npub fn instr16_F2AD_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 16, 0xF2) }\npub fn instr16_F3AD_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 16, 0xF3) }\npub fn instr32_AD_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 32, 0) }\npub fn instr32_F2AD_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 32, 0xF2) }\npub fn instr32_F3AD_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::LODS, 32, 0xF3) }\npub fn instr_AE_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 8, 0) }\npub fn instr_F2AE_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 8, 0xF2) }\npub fn instr_F3AE_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 8, 0xF3) }\npub fn instr16_AF_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 16, 0) }\npub fn instr16_F2AF_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 16, 0xF2) }\npub fn instr16_F3AF_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 16, 0xF3) }\npub fn instr32_AF_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 32, 0) }\npub fn instr32_F2AF_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 32, 0xF2) }\npub fn instr32_F3AF_jit(ctx: &mut JitContext) { gen_string_ins(ctx, String::SCAS, 32, 0xF3) }\n\npub fn instr_0F31_jit(ctx: &mut JitContext) {\n    ctx.builder.load_fixed_u8(global_pointers::cpl as u32);\n    ctx.builder.eqz_i32();\n\n    dbg_assert!(regs::CR4_TSD < 0x100);\n    ctx.builder\n        .load_fixed_u8(global_pointers::get_creg_offset(4));\n    ctx.builder.const_i32(regs::CR4_TSD as i32);\n    ctx.builder.and_i32();\n    ctx.builder.eqz_i32();\n\n    ctx.builder.or_i32();\n    ctx.builder.if_void();\n    ctx.builder.call_fn0_ret_i64(\"read_tsc\");\n\n    let tsc = ctx.builder.tee_new_local_i64();\n    ctx.builder.wrap_i64_to_i32();\n    codegen::gen_set_reg32(ctx, regs::EAX);\n\n    ctx.builder.get_local_i64(&tsc);\n    ctx.builder.const_i64(32);\n    ctx.builder.shr_u_i64();\n    ctx.builder.wrap_i64_to_i32();\n    codegen::gen_set_reg32(ctx, regs::EDX);\n\n    ctx.builder.free_local_i64(tsc);\n    ctx.builder.else_();\n    codegen::gen_trigger_gp(ctx, 0);\n    ctx.builder.block_end();\n}\n\npub fn instr_0F0B_jit(ctx: &mut JitContext) { codegen::gen_trigger_ud(ctx) }\n\npub fn instr_0F18_mem_jit(_ctx: &mut JitContext, _modrm_byte: ModrmByte, _reg: u32) {}\npub fn instr_0F18_reg_jit(_ctx: &mut JitContext, _r1: u32, _r2: u32) {}\n\npub fn instr_0F19_mem_jit(_ctx: &mut JitContext, _modrm_byte: ModrmByte, _reg: u32) {}\npub fn instr_0F19_reg_jit(_ctx: &mut JitContext, _r1: u32, _r2: u32) {}\n\npub fn instr_0F1C_mem_jit(_ctx: &mut JitContext, _modrm_byte: ModrmByte, _reg: u32) {}\npub fn instr_0F1C_reg_jit(_ctx: &mut JitContext, _r1: u32, _r2: u32) {}\npub fn instr_0F1D_mem_jit(_ctx: &mut JitContext, _modrm_byte: ModrmByte, _reg: u32) {}\npub fn instr_0F1D_reg_jit(_ctx: &mut JitContext, _r1: u32, _r2: u32) {}\npub fn instr_0F1E_mem_jit(_ctx: &mut JitContext, _modrm_byte: ModrmByte, _reg: u32) {}\npub fn instr_0F1E_reg_jit(_ctx: &mut JitContext, _r1: u32, _r2: u32) {}\npub fn instr_0F1F_mem_jit(_ctx: &mut JitContext, _modrm_byte: ModrmByte, _reg: u32) {}\npub fn instr_0F1F_reg_jit(_ctx: &mut JitContext, _r1: u32, _r2: u32) {}\n\ndefine_instruction_read_write_mem16!(\n    \"shld16\",\n    instr16_0FA4_mem_jit,\n    instr16_0FA4_reg_jit,\n    reg,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    \"shld32\",\n    instr32_0FA4_mem_jit,\n    instr32_0FA4_reg_jit,\n    reg,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem16!(\n    \"shld16\",\n    instr16_0FA5_mem_jit,\n    instr16_0FA5_reg_jit,\n    reg,\n    cl\n);\ndefine_instruction_read_write_mem32!(\n    \"shld32\",\n    instr32_0FA5_mem_jit,\n    instr32_0FA5_reg_jit,\n    reg,\n    cl\n);\n\ndefine_instruction_read_write_mem16!(\n    \"shrd16\",\n    instr16_0FAC_mem_jit,\n    instr16_0FAC_reg_jit,\n    reg,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem32!(\n    \"shrd32\",\n    instr32_0FAC_mem_jit,\n    instr32_0FAC_reg_jit,\n    reg,\n    imm8_5bits\n);\ndefine_instruction_read_write_mem16!(\n    \"shrd16\",\n    instr16_0FAD_mem_jit,\n    instr16_0FAD_reg_jit,\n    reg,\n    cl\n);\ndefine_instruction_read_write_mem32!(\n    \"shrd32\",\n    instr32_0FAD_mem_jit,\n    instr32_0FAD_reg_jit,\n    reg,\n    cl\n);\n\npub fn instr16_0FB1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    ctx.builder.const_i32(r2 as i32);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn2_ret(\"cmpxchg16\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    codegen::gen_set_reg16(ctx, r1);\n}\npub fn instr16_0FB1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read_write(ctx, BitSize::WORD, &address_local, &|ref mut ctx| {\n        ctx.builder.const_i32(r as i32);\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        ctx.builder.call_fn2_ret(\"cmpxchg16\");\n        codegen::gen_move_registers_from_memory_to_locals(ctx);\n    });\n    ctx.builder.free_local(address_local);\n}\n\npub fn instr32_0FB1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    gen_cmpxchg32(ctx, r2);\n    codegen::gen_set_reg32(ctx, r1);\n}\npub fn instr32_0FB1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read_write(ctx, BitSize::DWORD, &address_local, &|ref mut ctx| {\n        gen_cmpxchg32(ctx, r);\n    });\n    ctx.builder.free_local(address_local);\n}\n\npub fn instr16_0FB6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg8(ctx, r1);\n    codegen::gen_set_reg16_unmasked(ctx, r2);\n}\npub fn instr16_0FB6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n    codegen::gen_set_reg16_unmasked(ctx, r);\n}\n\npub fn instr32_0FB6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg8(ctx, r1);\n    codegen::gen_set_reg32(ctx, r2);\n}\npub fn instr32_0FB6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte);\n    codegen::gen_set_reg32(ctx, r);\n}\n\npub fn instr16_0FB7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_set_reg16(ctx, r);\n}\npub fn instr16_0FB7_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    codegen::gen_set_reg16(ctx, r2);\n}\npub fn instr32_0FB7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr32_0FB7_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    codegen::gen_set_reg32(ctx, r2);\n}\n\npub fn instr16_F30FB8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    ctx.builder.call_fn1_ret(\"popcnt\");\n    codegen::gen_set_reg16(ctx, r);\n}\npub fn instr16_F30FB8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    ctx.builder.call_fn1_ret(\"popcnt\");\n    codegen::gen_set_reg16(ctx, r2);\n}\npub fn instr32_F30FB8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.call_fn1_ret(\"popcnt\");\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr32_F30FB8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder.call_fn1_ret(\"popcnt\");\n    codegen::gen_set_reg32(ctx, r2);\n}\n\ndefine_instruction_write_reg16!(\"bsf16\", instr16_0FBC_mem_jit, instr16_0FBC_reg_jit);\ndefine_instruction_write_reg32!(gen_bsf32, instr32_0FBC_mem_jit, instr32_0FBC_reg_jit);\ndefine_instruction_write_reg16!(\"bsr16\", instr16_0FBD_mem_jit, instr16_0FBD_reg_jit);\ndefine_instruction_write_reg32!(gen_bsr32, instr32_0FBD_mem_jit, instr32_0FBD_reg_jit);\n\npub fn instr16_0FBE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg8(ctx, r1);\n    codegen::sign_extend_i8(ctx.builder);\n    codegen::gen_set_reg16(ctx, r2);\n}\npub fn instr16_0FBE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte); // TODO: Could use sign-extended read\n    codegen::sign_extend_i8(ctx.builder);\n    codegen::gen_set_reg16(ctx, r);\n}\n\npub fn instr32_0FBE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    match r1 {\n        regs::AL | regs::CL | regs::DL | regs::BL => {\n            ctx.builder.get_local(&ctx.register_locals[r1 as usize]);\n            ctx.builder.const_i32(24);\n            ctx.builder.shl_i32();\n        },\n        regs::AH | regs::CH | regs::DH | regs::BH => {\n            ctx.builder\n                .get_local(&ctx.register_locals[(r1 - 4) as usize]);\n            ctx.builder.const_i32(16);\n            ctx.builder.shl_i32();\n        },\n        _ => assert!(false),\n    }\n    ctx.builder.const_i32(24);\n    ctx.builder.shr_s_i32();\n    codegen::gen_set_reg32(ctx, r2);\n}\npub fn instr32_0FBE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read8(ctx, modrm_byte); // TODO: Could use sign-extended read\n    codegen::sign_extend_i8(ctx.builder);\n    codegen::gen_set_reg32(ctx, r);\n}\n\npub fn instr16_0FBF_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    codegen::gen_set_reg16_unmasked(ctx, r2);\n}\npub fn instr16_0FBF_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n    codegen::gen_set_reg16_unmasked(ctx, r);\n}\n\npub fn instr32_0FBF_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_set_reg32(ctx, r2);\n}\npub fn instr32_0FBF_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte); // TODO: Could use sign-extended read\n    codegen::sign_extend_i16(ctx.builder);\n    codegen::gen_set_reg32(ctx, r);\n}\n\npub fn instr16_0FC1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read_write(ctx, BitSize::WORD, &address_local, &|ref mut ctx| {\n        ctx.builder.const_i32(r as i32);\n        codegen::gen_move_registers_from_locals_to_memory(ctx);\n        ctx.builder.call_fn2_ret(\"xadd16\");\n        codegen::gen_move_registers_from_memory_to_locals(ctx);\n    });\n    ctx.builder.free_local(address_local);\n}\npub fn instr16_0FC1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg16(ctx, r1);\n    ctx.builder.const_i32(r2 as i32);\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.call_fn2_ret(\"xadd16\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n    codegen::gen_set_reg16(ctx, r1);\n}\n\npub fn instr32_0FC1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read_write(ctx, BitSize::DWORD, &address_local, &|ref mut ctx| {\n        let dest_operand = ctx.builder.set_new_local();\n        gen_xadd32(ctx, &dest_operand, r);\n        ctx.builder.get_local(&dest_operand);\n        ctx.builder.free_local(dest_operand);\n    });\n    ctx.builder.free_local(address_local);\n}\npub fn instr32_0FC1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    let dest_operand = ctx.builder.set_new_local();\n    gen_xadd32(ctx, &dest_operand, r2);\n    ctx.builder.get_local(&dest_operand);\n    codegen::gen_set_reg32(ctx, r1);\n    ctx.builder.free_local(dest_operand);\n}\n\npub fn instr_0FC3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &ctx.reg(r));\n    ctx.builder.free_local(address_local);\n}\npub fn instr_0FC3_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) { codegen::gen_trigger_ud(ctx) }\n\npub fn instr_0FC4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read16(ctx, &address_local);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3(\"instr_0FC4\");\n    ctx.builder.free_local(address_local);\n}\npub fn instr_0FC4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3(\"instr_0FC4\");\n}\n\npub fn instr_660FC4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read16(ctx, &address_local);\n    ctx.builder\n        .store_aligned_u16(global_pointers::get_reg_xmm_offset(r) + ((imm8 & 7) << 1));\n    ctx.builder.free_local(address_local);\n}\npub fn instr_660FC4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder\n        .store_aligned_u16(global_pointers::get_reg_xmm_offset(r2) + ((imm8 & 7) << 1));\n}\n\npub fn instr_0FC5_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _r: u32, _imm8: u32) {\n    codegen::gen_trigger_ud(ctx)\n}\npub fn instr_0FC5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3(\"instr_0FC5_reg\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n}\n\npub fn instr_660FC5_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _r: u32, _imm8: u32) {\n    codegen::gen_trigger_ud(ctx)\n}\npub fn instr_660FC5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    ctx.builder\n        .load_fixed_u16(global_pointers::get_reg_xmm_offset(r1) + ((imm8 & 7) << 1));\n    codegen::gen_set_reg32(ctx, r2);\n}\n\npub fn instr16_0FC7_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    // cmpxchg8b\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read_write(ctx, BitSize::QWORD, &address_local, &|ref mut ctx| {\n        let dest_operand = ctx.builder.tee_new_local_i64();\n        codegen::gen_get_reg32(ctx, regs::EDX);\n        ctx.builder.extend_unsigned_i32_to_i64();\n        ctx.builder.const_i64(32);\n        ctx.builder.shl_i64();\n        codegen::gen_get_reg32(ctx, regs::EAX);\n        ctx.builder.extend_unsigned_i32_to_i64();\n        ctx.builder.or_i64();\n        ctx.builder.eq_i64();\n        ctx.builder.if_i64();\n        {\n            codegen::gen_set_flags_bits(ctx.builder, FLAG_ZERO);\n            codegen::gen_get_reg32(ctx, regs::ECX);\n            ctx.builder.extend_unsigned_i32_to_i64();\n            ctx.builder.const_i64(32);\n            ctx.builder.shl_i64();\n            codegen::gen_get_reg32(ctx, regs::EBX);\n            ctx.builder.extend_unsigned_i32_to_i64();\n            ctx.builder.or_i64();\n        }\n        ctx.builder.else_();\n        {\n            codegen::gen_clear_flags_bits(ctx.builder, FLAG_ZERO);\n            ctx.builder.get_local_i64(&dest_operand);\n            ctx.builder.wrap_i64_to_i32();\n            codegen::gen_set_reg32(ctx, regs::EAX);\n            ctx.builder.get_local_i64(&dest_operand);\n            ctx.builder.const_i64(32);\n            ctx.builder.shr_u_i64();\n            ctx.builder.wrap_i64_to_i32();\n            codegen::gen_set_reg32(ctx, regs::EDX);\n            ctx.builder.get_local_i64(&dest_operand);\n        }\n        ctx.builder.block_end();\n        codegen::gen_clear_flags_changed_bits(ctx.builder, FLAG_ZERO);\n        ctx.builder.free_local_i64(dest_operand);\n    });\n    ctx.builder.free_local(address_local);\n}\npub fn instr16_0FC7_1_reg_jit(ctx: &mut JitContext, _r: u32) { codegen::gen_trigger_ud(ctx); }\npub fn instr32_0FC7_1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte) {\n    instr16_0FC7_1_mem_jit(ctx, modrm_byte);\n}\npub fn instr32_0FC7_1_reg_jit(ctx: &mut JitContext, _r: u32) { codegen::gen_trigger_ud(ctx); }\n\npub fn instr_0FC2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    sse_read128_xmm_xmm_imm(ctx, \"instr_0FC2\", r1, r2, imm8)\n}\npub fn instr_0FC2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    sse_read128_xmm_mem_imm(ctx, \"instr_0FC2\", modrm_byte, r, imm8)\n}\npub fn instr_660FC2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    sse_read128_xmm_xmm_imm(ctx, \"instr_660FC2\", r1, r2, imm8)\n}\npub fn instr_660FC2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    sse_read128_xmm_mem_imm(ctx, \"instr_660FC2\", modrm_byte, r, imm8)\n}\npub fn instr_F20FC2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3_i64_i32_i32(\"instr_F20FC2\");\n}\npub fn instr_F20FC2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3_i64_i32_i32(\"instr_F20FC2\");\n}\npub fn instr_F30FC2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_i32(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3(\"instr_F30FC2\");\n}\npub fn instr_F30FC2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3(\"instr_F30FC2\");\n}\n\npub fn instr_0FC6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    sse_read128_xmm_xmm_imm(ctx, \"instr_0FC6\", r1, r2, imm8)\n}\npub fn instr_0FC6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    sse_read128_xmm_mem_imm(ctx, \"instr_0FC6\", modrm_byte, r, imm8)\n}\npub fn instr_660FC6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    sse_read128_xmm_xmm_imm(ctx, \"instr_660FC6\", r1, r2, imm8)\n}\npub fn instr_660FC6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    sse_read128_xmm_mem_imm(ctx, \"instr_660FC6\", modrm_byte, r, imm8)\n}\n\npub fn instr_C6_0_reg_jit(ctx: &mut JitContext, r: u32, imm: u32) {\n    // reg8[r] = imm;\n    ctx.builder.const_i32(imm as i32);\n    codegen::gen_set_reg8_unmasked(ctx, r);\n}\n\npub fn instr_C6_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        ctx.builder.const_i32(imm as i32);\n        let value_local = ctx.builder.set_new_local();\n        codegen::gen_safe_write8(ctx, &addr, &value_local);\n        ctx.builder.free_local(value_local);\n    });\n}\n\npub fn instr16_C7_0_reg_jit(ctx: &mut JitContext, r: u32, imm: u32) {\n    // reg16[r] = imm;\n    ctx.builder.const_i32(imm as i32);\n    codegen::gen_set_reg16_unmasked(ctx, r);\n}\n\npub fn instr16_C7_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        ctx.builder.const_i32(imm as i32);\n        let value_local = ctx.builder.set_new_local();\n        codegen::gen_safe_write16(ctx, &addr, &value_local);\n        ctx.builder.free_local(value_local);\n    });\n}\n\npub fn instr32_C7_0_reg_jit(ctx: &mut JitContext, r: u32, imm: u32) {\n    // reg32[r] = imm;\n    ctx.builder.const_i32(imm as i32);\n    codegen::gen_set_reg32(ctx, r);\n}\n\npub fn instr32_C7_0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm: u32) {\n    codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n        ctx.builder.const_i32(imm as i32);\n        let value_local = ctx.builder.set_new_local();\n        codegen::gen_safe_write32(ctx, &addr, &value_local);\n        ctx.builder.free_local(value_local);\n    });\n}\n\npub fn instr_0FC8_jit(ctx: &mut JitContext) { gen_bswap(ctx, 0) }\npub fn instr_0FC9_jit(ctx: &mut JitContext) { gen_bswap(ctx, 1) }\npub fn instr_0FCA_jit(ctx: &mut JitContext) { gen_bswap(ctx, 2) }\npub fn instr_0FCB_jit(ctx: &mut JitContext) { gen_bswap(ctx, 3) }\npub fn instr_0FCC_jit(ctx: &mut JitContext) { gen_bswap(ctx, 4) }\npub fn instr_0FCD_jit(ctx: &mut JitContext) { gen_bswap(ctx, 5) }\npub fn instr_0FCE_jit(ctx: &mut JitContext) { gen_bswap(ctx, 6) }\npub fn instr_0FCF_jit(ctx: &mut JitContext) { gen_bswap(ctx, 7) }\n\ndefine_instruction_write_reg16!(\"imul_reg16\", instr16_0FAF_mem_jit, instr16_0FAF_reg_jit);\ndefine_instruction_write_reg32!(gen_imul_reg32, instr32_0FAF_mem_jit, instr32_0FAF_reg_jit);\n\nmacro_rules! define_cmovcc16(\n    ($cond:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read16(ctx, modrm_byte);\n            let value = ctx.builder.set_new_local();\n            codegen::gen_condition_fn(ctx, $cond);\n            ctx.builder.if_void();\n            ctx.builder.get_local(&value);\n            codegen::gen_set_reg16(ctx, r);\n            ctx.builder.block_end();\n            ctx.builder.free_local(value);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            codegen::gen_condition_fn(ctx, $cond);\n            ctx.builder.if_void();\n            codegen::gen_get_reg16(ctx, r1);\n            codegen::gen_set_reg16(ctx, r2);\n            ctx.builder.block_end();\n        }\n    );\n);\n\nmacro_rules! define_cmovcc32(\n    ($cond:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n            codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n            let value = ctx.builder.set_new_local();\n            codegen::gen_condition_fn(ctx, $cond);\n            ctx.builder.if_void();\n            ctx.builder.get_local(&value);\n            codegen::gen_set_reg32(ctx, r);\n            ctx.builder.block_end();\n            ctx.builder.free_local(value);\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, r2: u32) {\n            codegen::gen_condition_fn(ctx, $cond);\n            ctx.builder.if_void();\n            codegen::gen_get_reg32(ctx, r1);\n            codegen::gen_set_reg32(ctx, r2);\n            ctx.builder.block_end();\n        }\n    );\n);\n\ndefine_cmovcc16!(0x0, instr16_0F40_mem_jit, instr16_0F40_reg_jit);\ndefine_cmovcc16!(0x1, instr16_0F41_mem_jit, instr16_0F41_reg_jit);\ndefine_cmovcc16!(0x2, instr16_0F42_mem_jit, instr16_0F42_reg_jit);\ndefine_cmovcc16!(0x3, instr16_0F43_mem_jit, instr16_0F43_reg_jit);\ndefine_cmovcc16!(0x4, instr16_0F44_mem_jit, instr16_0F44_reg_jit);\ndefine_cmovcc16!(0x5, instr16_0F45_mem_jit, instr16_0F45_reg_jit);\ndefine_cmovcc16!(0x6, instr16_0F46_mem_jit, instr16_0F46_reg_jit);\ndefine_cmovcc16!(0x7, instr16_0F47_mem_jit, instr16_0F47_reg_jit);\n\ndefine_cmovcc16!(0x8, instr16_0F48_mem_jit, instr16_0F48_reg_jit);\ndefine_cmovcc16!(0x9, instr16_0F49_mem_jit, instr16_0F49_reg_jit);\ndefine_cmovcc16!(0xA, instr16_0F4A_mem_jit, instr16_0F4A_reg_jit);\ndefine_cmovcc16!(0xB, instr16_0F4B_mem_jit, instr16_0F4B_reg_jit);\ndefine_cmovcc16!(0xC, instr16_0F4C_mem_jit, instr16_0F4C_reg_jit);\ndefine_cmovcc16!(0xD, instr16_0F4D_mem_jit, instr16_0F4D_reg_jit);\ndefine_cmovcc16!(0xE, instr16_0F4E_mem_jit, instr16_0F4E_reg_jit);\ndefine_cmovcc16!(0xF, instr16_0F4F_mem_jit, instr16_0F4F_reg_jit);\n\ndefine_cmovcc32!(0x0, instr32_0F40_mem_jit, instr32_0F40_reg_jit);\ndefine_cmovcc32!(0x1, instr32_0F41_mem_jit, instr32_0F41_reg_jit);\ndefine_cmovcc32!(0x2, instr32_0F42_mem_jit, instr32_0F42_reg_jit);\ndefine_cmovcc32!(0x3, instr32_0F43_mem_jit, instr32_0F43_reg_jit);\ndefine_cmovcc32!(0x4, instr32_0F44_mem_jit, instr32_0F44_reg_jit);\ndefine_cmovcc32!(0x5, instr32_0F45_mem_jit, instr32_0F45_reg_jit);\ndefine_cmovcc32!(0x6, instr32_0F46_mem_jit, instr32_0F46_reg_jit);\ndefine_cmovcc32!(0x7, instr32_0F47_mem_jit, instr32_0F47_reg_jit);\n\ndefine_cmovcc32!(0x8, instr32_0F48_mem_jit, instr32_0F48_reg_jit);\ndefine_cmovcc32!(0x9, instr32_0F49_mem_jit, instr32_0F49_reg_jit);\ndefine_cmovcc32!(0xA, instr32_0F4A_mem_jit, instr32_0F4A_reg_jit);\ndefine_cmovcc32!(0xB, instr32_0F4B_mem_jit, instr32_0F4B_reg_jit);\ndefine_cmovcc32!(0xC, instr32_0F4C_mem_jit, instr32_0F4C_reg_jit);\ndefine_cmovcc32!(0xD, instr32_0F4D_mem_jit, instr32_0F4D_reg_jit);\ndefine_cmovcc32!(0xE, instr32_0F4E_mem_jit, instr32_0F4E_reg_jit);\ndefine_cmovcc32!(0xF, instr32_0F4F_mem_jit, instr32_0F4F_reg_jit);\n\nmacro_rules! define_setcc(\n    ($cond:expr, $name_mem:ident, $name_reg:ident) => (\n        pub fn $name_mem(ctx: &mut JitContext, modrm_byte: ModrmByte, _r: u32) {\n            codegen::gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {\n                codegen::gen_condition_fn(ctx, $cond);\n                ctx.builder.const_i32(0);\n                ctx.builder.ne_i32();\n                let value_local = ctx.builder.set_new_local();\n                codegen::gen_safe_write8(ctx, &addr, &value_local);\n                ctx.builder.free_local(value_local);\n            });\n        }\n\n        pub fn $name_reg(ctx: &mut JitContext, r1: u32, _r2: u32) {\n            codegen::gen_condition_fn(ctx, $cond);\n            ctx.builder.const_i32(0);\n            ctx.builder.ne_i32();\n            codegen::gen_set_reg8_unmasked(ctx, r1);\n        }\n    );\n);\n\ndefine_setcc!(0x0, instr_0F90_mem_jit, instr_0F90_reg_jit);\ndefine_setcc!(0x1, instr_0F91_mem_jit, instr_0F91_reg_jit);\ndefine_setcc!(0x2, instr_0F92_mem_jit, instr_0F92_reg_jit);\ndefine_setcc!(0x3, instr_0F93_mem_jit, instr_0F93_reg_jit);\ndefine_setcc!(0x4, instr_0F94_mem_jit, instr_0F94_reg_jit);\ndefine_setcc!(0x5, instr_0F95_mem_jit, instr_0F95_reg_jit);\ndefine_setcc!(0x6, instr_0F96_mem_jit, instr_0F96_reg_jit);\ndefine_setcc!(0x7, instr_0F97_mem_jit, instr_0F97_reg_jit);\n\ndefine_setcc!(0x8, instr_0F98_mem_jit, instr_0F98_reg_jit);\ndefine_setcc!(0x9, instr_0F99_mem_jit, instr_0F99_reg_jit);\ndefine_setcc!(0xA, instr_0F9A_mem_jit, instr_0F9A_reg_jit);\ndefine_setcc!(0xB, instr_0F9B_mem_jit, instr_0F9B_reg_jit);\ndefine_setcc!(0xC, instr_0F9C_mem_jit, instr_0F9C_reg_jit);\ndefine_setcc!(0xD, instr_0F9D_mem_jit, instr_0F9D_reg_jit);\ndefine_setcc!(0xE, instr_0F9E_mem_jit, instr_0F9E_reg_jit);\ndefine_setcc!(0xF, instr_0F9F_mem_jit, instr_0F9F_reg_jit);\n\npub fn instr_0F10_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n}\npub fn instr_0F10_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r1, r2) }\npub fn instr_660F10_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n}\npub fn instr_660F10_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r1, r2) }\npub fn instr_F20F10_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_F30F7E_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_F20F10_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i64(global_pointers::get_reg_xmm_offset(r1));\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r2));\n}\npub fn instr_F30F10_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_660F6E_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_F30F10_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r1));\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r2));\n}\n\npub fn instr_0F11_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_0F11_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r2, r1) }\npub fn instr_660F11_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_660F29_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_660F11_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r2, r1) }\npub fn instr_F20F11_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_660FD6_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_F20F11_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i64(global_pointers::get_reg_xmm_offset(r2));\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r1));\n}\npub fn instr_F30F11_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_660F7E_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_F30F11_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r2));\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r1));\n}\n\npub fn instr_0F12_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32);\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.store_aligned_i64(0);\n}\npub fn instr_0F12_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r2) as i32);\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32 + 8);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.store_aligned_i64(0);\n}\npub fn instr_660F12_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32);\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.store_aligned_i64(0);\n}\npub fn instr_660F12_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr_F20F12_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F12\", modrm_byte, r);\n}\npub fn instr_F20F12_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F12\", r1, r2);\n}\npub fn instr_F30F12_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_F30F12\", modrm_byte, r);\n}\npub fn instr_F30F12_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_F30F12\", r1, r2);\n}\n\npub fn instr_0F13_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_660FD6_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_0F13_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F13_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_660FD6_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_660F13_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr_0F14_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_0F14\", modrm_byte, r);\n}\npub fn instr_0F14_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_0F14\", r1, r2);\n}\npub fn instr_660F14_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_660F14\", modrm_byte, r);\n}\npub fn instr_660F14_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_660F14\", r1, r2);\n}\n\npub fn instr_0F15_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F15\", modrm_byte, r);\n}\npub fn instr_0F15_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F15\", r1, r2);\n}\npub fn instr_660F15_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F15\", modrm_byte, r);\n}\npub fn instr_660F15_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F15\", r1, r2);\n}\n\npub fn instr_0F16_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_0F16\", modrm_byte, r);\n}\npub fn instr_0F16_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_0F16\", r1, r2);\n}\npub fn instr_660F16_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_0F16\", modrm_byte, r);\n}\npub fn instr_660F16_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr_F30F16_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_F30F16\", modrm_byte, r);\n}\npub fn instr_F30F16_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_F30F16\", r1, r2);\n}\n\npub fn instr_0F17_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32);\n    ctx.builder.load_aligned_i64(8);\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n}\npub fn instr_0F17_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F17_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F17_mem_jit(ctx, modrm_byte, r);\n}\npub fn instr_660F17_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr_0F28_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n}\npub fn instr_0F28_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r1, r2) }\npub fn instr_660F28_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n}\npub fn instr_660F28_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r1, r2) }\n\npub fn instr_0F29_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    // XXX: Aligned write or #gp\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32);\n    ctx.builder.load_aligned_i64(0);\n    let value_local_low = ctx.builder.set_new_local_i64();\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32 + 8);\n    ctx.builder.load_aligned_i64(0);\n    let value_local_high = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write128(ctx, &address_local, &value_local_low, &value_local_high);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local_low);\n    ctx.builder.free_local_i64(value_local_high);\n}\npub fn instr_0F29_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r2, r1) }\n\npub fn instr_660F29_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r);\n}\npub fn instr_660F29_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r2, r1) }\n\npub fn instr_0F2A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F2A\", modrm_byte, r);\n}\npub fn instr_0F2A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F2A\", r1, r2);\n}\npub fn instr_660F2A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_660F2A\", modrm_byte, r);\n}\npub fn instr_660F2A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_660F2A\", r1, r2);\n}\npub fn instr_F20F2A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2(\"instr_F20F2A\")\n}\npub fn instr_F20F2A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(\"instr_F20F2A\")\n}\npub fn instr_F30F2A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2(\"instr_F30F2A\")\n}\npub fn instr_F30F2A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(\"instr_F30F2A\")\n}\n\npub fn instr_0F2B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_0F2B_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F2B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r)\n}\npub fn instr_660F2B_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr_F20F2C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.reinterpret_i64_as_f64();\n    ctx.builder\n        .call_fn1_f64_ret(\"sse_convert_with_truncation_f64_to_i32\");\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr_F20F2C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_f64(0);\n    ctx.builder\n        .call_fn1_f64_ret(\"sse_convert_with_truncation_f64_to_i32\");\n    codegen::gen_set_reg32(ctx, r2);\n}\npub fn instr_F30F2C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.reinterpret_i32_as_f32();\n    ctx.builder\n        .call_fn1_f32_ret(\"sse_convert_with_truncation_f32_to_i32\");\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr_F30F2C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_f32(0);\n    ctx.builder\n        .call_fn1_f32_ret(\"sse_convert_with_truncation_f32_to_i32\");\n    codegen::gen_set_reg32(ctx, r2);\n}\n\npub fn instr_F20F2D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.reinterpret_i64_as_f64();\n    ctx.builder.call_fn1_f64_ret(\"sse_convert_f64_to_i32\");\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr_F20F2D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_f64(0);\n    ctx.builder.call_fn1_f64_ret(\"sse_convert_f64_to_i32\");\n    codegen::gen_set_reg32(ctx, r2);\n}\npub fn instr_F30F2D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.reinterpret_i32_as_f32();\n    ctx.builder.call_fn1_f32_ret(\"sse_convert_f32_to_i32\");\n    codegen::gen_set_reg32(ctx, r);\n}\npub fn instr_F30F2D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r1) as i32);\n    ctx.builder.load_aligned_f32(0);\n    ctx.builder.call_fn1_f32_ret(\"sse_convert_f32_to_i32\");\n    codegen::gen_set_reg32(ctx, r2);\n}\n\npub fn instr_0F2E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_0F2E\", modrm_byte, r);\n}\npub fn instr_0F2E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_0F2E\", r1, r2);\n}\npub fn instr_660F2E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_660F2E\", modrm_byte, r);\n}\npub fn instr_660F2E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_660F2E\", r1, r2);\n}\n\npub fn instr_0F2F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_0F2F\", modrm_byte, r);\n}\npub fn instr_0F2F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_0F2F\", r1, r2);\n}\npub fn instr_660F2F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_660F2F\", modrm_byte, r);\n}\npub fn instr_660F2F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_660F2F\", r1, r2);\n}\n\npub fn instr_0F51_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F51\", modrm_byte, r);\n}\npub fn instr_0F51_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F51\", r1, r2);\n}\npub fn instr_660F51_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F51\", modrm_byte, r);\n}\npub fn instr_660F51_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F51\", r1, r2);\n}\npub fn instr_F20F51_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F51\", modrm_byte, r);\n}\npub fn instr_F20F51_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F51\", r1, r2);\n}\npub fn instr_F30F51_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F51\", modrm_byte, r);\n}\npub fn instr_F30F51_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F51\", r1, r2);\n}\n\npub fn instr_0F52_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F52\", modrm_byte, r);\n}\npub fn instr_0F52_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F52\", r1, r2);\n}\npub fn instr_F30F52_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F52\", modrm_byte, r);\n}\npub fn instr_F30F52_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F52\", r1, r2);\n}\n\npub fn instr_0F53_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F53\", modrm_byte, r);\n}\npub fn instr_0F53_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F53\", r1, r2);\n}\npub fn instr_F30F53_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F53\", modrm_byte, r);\n}\npub fn instr_F30F53_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F53\", r1, r2);\n}\n\npub fn instr_0F54_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F54\", modrm_byte, r);\n}\npub fn instr_0F54_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F54\", r1, r2);\n}\npub fn instr_660F54_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F54\", modrm_byte, r);\n}\npub fn instr_660F54_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F54\", r1, r2);\n}\n\npub fn instr_0F55_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F55\", modrm_byte, r);\n}\npub fn instr_0F55_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F55\", r1, r2);\n}\npub fn instr_660F55_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F55\", modrm_byte, r);\n}\npub fn instr_660F55_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F55\", r1, r2);\n}\n\npub fn instr_0F56_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F56\", modrm_byte, r);\n}\npub fn instr_0F56_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F56\", r1, r2);\n}\npub fn instr_660F56_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F56\", modrm_byte, r);\n}\npub fn instr_660F56_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F56\", r1, r2);\n}\n\npub fn instr_0F57_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F57\", modrm_byte, r);\n}\npub fn instr_0F57_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F57\", r1, r2);\n}\npub fn instr_660F57_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F57\", modrm_byte, r);\n}\npub fn instr_660F57_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F57\", r1, r2);\n}\n\npub fn instr_0F58_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F58\", modrm_byte, r);\n}\npub fn instr_0F58_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F58\", r1, r2);\n}\npub fn instr_660F58_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F58\", modrm_byte, r);\n}\npub fn instr_660F58_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F58\", r1, r2);\n}\npub fn instr_F20F58_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F58\", modrm_byte, r);\n}\npub fn instr_F20F58_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F58\", r1, r2);\n}\npub fn instr_F30F58_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F58\", modrm_byte, r);\n}\npub fn instr_F30F58_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F58\", r1, r2);\n}\n\npub fn instr_0F59_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F59\", modrm_byte, r);\n}\npub fn instr_0F59_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F59\", r1, r2);\n}\npub fn instr_660F59_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F59\", modrm_byte, r);\n}\npub fn instr_660F59_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F59\", r1, r2);\n}\npub fn instr_F20F59_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F59\", modrm_byte, r);\n}\npub fn instr_F20F59_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F59\", r1, r2);\n}\npub fn instr_F30F59_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F59\", modrm_byte, r);\n}\npub fn instr_F30F59_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F59\", r1, r2);\n}\n\npub fn instr_0F5A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_0F5A\", modrm_byte, r);\n}\npub fn instr_0F5A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_0F5A\", r1, r2);\n}\npub fn instr_660F5A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F5A\", modrm_byte, r);\n}\npub fn instr_660F5A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F5A\", r1, r2);\n}\npub fn instr_F20F5A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F5A\", modrm_byte, r);\n}\npub fn instr_F20F5A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F5A\", r1, r2);\n}\npub fn instr_F30F5A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F5A\", modrm_byte, r);\n}\npub fn instr_F30F5A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F5A\", r1, r2);\n}\n\npub fn instr_0F5B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F5B\", modrm_byte, r);\n}\npub fn instr_0F5B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F5B\", r1, r2);\n}\npub fn instr_660F5B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F5B\", modrm_byte, r);\n}\npub fn instr_660F5B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F5B\", r1, r2);\n}\npub fn instr_F30F5B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_F30F5B\", modrm_byte, r);\n}\npub fn instr_F30F5B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_F30F5B\", r1, r2);\n}\n\npub fn instr_0F5C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F5C\", modrm_byte, r);\n}\npub fn instr_0F5C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F5C\", r1, r2);\n}\npub fn instr_660F5C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F5C\", modrm_byte, r);\n}\npub fn instr_660F5C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F5C\", r1, r2);\n}\npub fn instr_F20F5C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F5C\", modrm_byte, r);\n}\npub fn instr_F20F5C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F5C\", r1, r2);\n}\npub fn instr_F30F5C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F5C\", modrm_byte, r);\n}\npub fn instr_F30F5C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F5C\", r1, r2);\n}\n\npub fn instr_0F5D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F5D\", modrm_byte, r);\n}\npub fn instr_0F5D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F5D\", r1, r2);\n}\npub fn instr_660F5D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F5D\", modrm_byte, r);\n}\npub fn instr_660F5D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F5D\", r1, r2);\n}\npub fn instr_F20F5D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F5D\", modrm_byte, r);\n}\npub fn instr_F20F5D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F5D\", r1, r2);\n}\npub fn instr_F30F5D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F5D\", modrm_byte, r);\n}\npub fn instr_F30F5D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F5D\", r1, r2);\n}\n\npub fn instr_0F5E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F5E\", modrm_byte, r);\n}\npub fn instr_0F5E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F5E\", r1, r2);\n}\npub fn instr_660F5E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F5E\", modrm_byte, r);\n}\npub fn instr_660F5E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F5E\", r1, r2);\n}\npub fn instr_F20F5E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F5E\", modrm_byte, r);\n}\npub fn instr_F20F5E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F5E\", r1, r2);\n}\npub fn instr_F30F5E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F5E\", modrm_byte, r);\n}\npub fn instr_F30F5E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F5E\", r1, r2);\n}\n\npub fn instr_0F5F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_0F5F\", modrm_byte, r);\n}\npub fn instr_0F5F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_0F5F\", r1, r2);\n}\npub fn instr_660F5F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F5F\", modrm_byte, r);\n}\npub fn instr_660F5F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F5F\", r1, r2);\n}\npub fn instr_F20F5F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F20F5F\", modrm_byte, r);\n}\npub fn instr_F20F5F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F20F5F\", r1, r2);\n}\npub fn instr_F30F5F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read_f32_xmm_mem(ctx, \"instr_F30F5F\", modrm_byte, r);\n}\npub fn instr_F30F5F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read_f32_xmm_xmm(ctx, \"instr_F30F5F\", r1, r2);\n}\n\npub fn instr_0F60_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem32(ctx, \"instr_0F60\", modrm_byte, r);\n}\npub fn instr_0F60_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm32(ctx, \"instr_0F60\", r1, r2);\n}\npub fn instr_0F61_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem32(ctx, \"instr_0F61\", modrm_byte, r);\n}\npub fn instr_0F61_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm32(ctx, \"instr_0F61\", r1, r2);\n}\npub fn instr_0F62_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem32(ctx, \"instr_0F62\", modrm_byte, r);\n}\npub fn instr_0F62_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm32(ctx, \"instr_0F62\", r1, r2);\n}\n\npub fn instr_0F63_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F63\", modrm_byte, r);\n}\npub fn instr_0F63_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F63\", r1, r2);\n}\npub fn instr_0F64_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F64\", modrm_byte, r);\n}\npub fn instr_0F64_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F64\", r1, r2);\n}\npub fn instr_0F65_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F65\", modrm_byte, r);\n}\npub fn instr_0F65_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F65\", r1, r2);\n}\npub fn instr_0F66_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F66\", modrm_byte, r);\n}\npub fn instr_0F66_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F66\", r1, r2);\n}\npub fn instr_0F67_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F67\", modrm_byte, r);\n}\npub fn instr_0F67_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F67\", r1, r2);\n}\npub fn instr_0F68_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F68\", modrm_byte, r);\n}\npub fn instr_0F68_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F68\", r1, r2);\n}\npub fn instr_0F69_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F69\", modrm_byte, r);\n}\npub fn instr_0F69_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F69\", r1, r2);\n}\npub fn instr_0F6A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F6A\", modrm_byte, r);\n}\npub fn instr_0F6A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F6A\", r1, r2);\n}\npub fn instr_0F6B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F6B\", modrm_byte, r);\n}\npub fn instr_0F6B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F6B\", r1, r2);\n}\n\npub fn instr_660F60_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    // Note: Only requires 64-bit read, but is allowed to do 128-bit read\n    sse_read128_xmm_mem(ctx, \"instr_660F60\", modrm_byte, r);\n}\npub fn instr_660F60_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F60\", r1, r2);\n}\npub fn instr_660F61_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    // Note: Only requires 64-bit read, but is allowed to do 128-bit read\n    sse_read128_xmm_mem(ctx, \"instr_660F61\", modrm_byte, r);\n}\npub fn instr_660F61_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F61\", r1, r2);\n}\npub fn instr_660F62_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let src = global_pointers::sse_scratch_register as u32;\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, src);\n    ctx.builder.const_i32(0);\n    ctx.builder.load_fixed_i32(src + 4);\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r) + 12);\n\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r) + 4);\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r) + 8);\n\n    ctx.builder.const_i32(0);\n    ctx.builder.load_fixed_i32(src + 0);\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r) + 4);\n}\npub fn instr_660F62_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r1) + 4);\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r2) + 12);\n\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r2) + 4);\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r2) + 8);\n\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r1) + 0);\n    ctx.builder\n        .store_aligned_i32(global_pointers::get_reg_xmm_offset(r2) + 4);\n}\npub fn instr_660F63_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F63\", modrm_byte, r);\n}\npub fn instr_660F63_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F63\", r1, r2);\n}\npub fn instr_660F64_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F64\", modrm_byte, r);\n}\npub fn instr_660F64_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F64\", r1, r2);\n}\npub fn instr_660F65_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F65\", modrm_byte, r);\n}\npub fn instr_660F65_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F65\", r1, r2);\n}\npub fn instr_660F66_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F66\", modrm_byte, r);\n}\npub fn instr_660F66_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F66\", r1, r2);\n}\npub fn instr_660F67_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F67\", modrm_byte, r);\n}\npub fn instr_660F67_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F67\", r1, r2);\n}\npub fn instr_660F68_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F68\", modrm_byte, r);\n}\npub fn instr_660F68_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F68\", r1, r2);\n}\npub fn instr_660F69_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F69\", modrm_byte, r);\n}\npub fn instr_660F69_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F69\", r1, r2);\n}\npub fn instr_660F6A_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F6A\", modrm_byte, r);\n}\npub fn instr_660F6A_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F6A\", r1, r2);\n}\npub fn instr_660F6B_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F6B\", modrm_byte, r);\n}\npub fn instr_660F6B_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F6B\", r1, r2);\n}\npub fn instr_660F6C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F6C\", modrm_byte, r);\n}\npub fn instr_660F6C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F6C\", r1, r2);\n}\npub fn instr_660F6D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F6D\", modrm_byte, r);\n}\npub fn instr_660F6D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F6D\", r1, r2);\n}\n\npub fn instr_0F6E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2(\"instr_0F6E\")\n}\npub fn instr_0F6E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(\"instr_0F6E\")\n}\n\npub fn instr_660F6E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_modrm_resolve_safe_read32(ctx, modrm_byte);\n    ctx.builder.extend_unsigned_i32_to_i64();\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r));\n    ctx.builder.const_i32(0);\n    ctx.builder.const_i64(0);\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r) + 8);\n}\npub fn instr_660F6E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    codegen::gen_get_reg32(ctx, r1);\n    ctx.builder.extend_unsigned_i32_to_i64();\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r2));\n    ctx.builder.const_i32(0);\n    ctx.builder.const_i64(0);\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r2) + 8);\n}\n\npub fn instr_0F6F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    // XXX: Aligned read or #gp\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn2_i64_i32(\"instr_0F6F\")\n}\npub fn instr_0F6F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(\"instr_0F6F_reg\")\n}\n\npub fn instr_660F6F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    // XXX: Aligned read or #gp\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n}\npub fn instr_660F6F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r1, r2) }\npub fn instr_F30F6F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    let dest = global_pointers::get_reg_xmm_offset(r);\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, dest);\n}\npub fn instr_F30F6F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r1, r2) }\n\npub fn instr_0F70_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3_i64_i32_i32(\"instr_0F70\");\n}\npub fn instr_0F70_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_mmx_offset(r1) as i32);\n    ctx.builder.load_aligned_i64(0);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn3_i64_i32_i32(\"instr_0F70\");\n}\npub fn instr_660F70_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    let src = global_pointers::sse_scratch_register as u32;\n    codegen::gen_modrm_resolve_safe_read128(ctx, modrm_byte, src);\n    for i in 0..4 {\n        ctx.builder.const_i32(0);\n        ctx.builder.load_fixed_i32(src + 4 * (imm8 >> 2 * i & 3));\n        ctx.builder\n            .store_aligned_i32(global_pointers::get_reg_xmm_offset(r) + 4 * i);\n    }\n}\npub fn instr_660F70_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    codegen::gen_read_reg_xmm128_into_scratch(ctx, r1);\n    // TODO: perf: copy less (handle aliased src/dst), use 64-bit loads/stores if possible\n    let src = global_pointers::sse_scratch_register as u32;\n    for i in 0..4 {\n        ctx.builder.const_i32(0);\n        ctx.builder.load_fixed_i32(src + 4 * (imm8 >> 2 * i & 3));\n        ctx.builder\n            .store_aligned_i32(global_pointers::get_reg_xmm_offset(r2) + 4 * i);\n    }\n}\npub fn instr_F20F70_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    sse_read128_xmm_mem_imm(ctx, \"instr_F20F70\", modrm_byte, r, imm8)\n}\npub fn instr_F20F70_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    sse_read128_xmm_xmm_imm(ctx, \"instr_F20F70\", r1, r2, imm8)\n}\npub fn instr_F30F70_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32, imm8: u32) {\n    sse_read128_xmm_mem_imm(ctx, \"instr_F30F70\", modrm_byte, r, imm8)\n}\npub fn instr_F30F70_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32, imm8: u32) {\n    sse_read128_xmm_xmm_imm(ctx, \"instr_F30F70\", r1, r2, imm8)\n}\n\npub fn instr_0F71_2_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F71_2_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F71_2_reg\");\n}\npub fn instr_0F71_4_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F71_4_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F71_4_reg\");\n}\npub fn instr_0F71_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F71_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F71_6_reg\");\n}\n\npub fn instr_0F72_2_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F72_2_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F72_2_reg\");\n}\npub fn instr_0F72_4_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F72_4_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F72_4_reg\");\n}\npub fn instr_0F72_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F72_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F72_6_reg\");\n}\n\npub fn instr_0F73_2_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F73_2_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F73_2_reg\");\n}\npub fn instr_0F73_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0F73_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_0F73_6_reg\");\n}\n\npub fn instr_660F71_2_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F71_2_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F71_2_reg\");\n}\npub fn instr_660F71_4_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F71_4_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F71_4_reg\");\n}\npub fn instr_660F71_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F71_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F71_6_reg\");\n}\n\npub fn instr_660F72_2_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F72_2_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F72_2_reg\");\n}\npub fn instr_660F72_4_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F72_4_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F72_4_reg\");\n}\npub fn instr_660F72_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F72_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F72_6_reg\");\n}\n\npub fn instr_660F73_2_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F73_2_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F73_2_reg\");\n}\npub fn instr_660F73_3_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F73_3_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F73_3_reg\");\n}\npub fn instr_660F73_6_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F73_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F73_6_reg\");\n}\npub fn instr_660F73_7_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _imm: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_660F73_7_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.const_i32(imm8 as i32);\n    ctx.builder.call_fn2(\"instr_660F73_7_reg\");\n}\n\npub fn instr_0F74_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F74\", modrm_byte, r);\n}\npub fn instr_0F74_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F74\", r1, r2);\n}\npub fn instr_0F75_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F75\", modrm_byte, r);\n}\npub fn instr_0F75_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F75\", r1, r2);\n}\npub fn instr_0F76_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0F76\", modrm_byte, r);\n}\npub fn instr_0F76_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0F76\", r1, r2);\n}\n\npub fn instr_660F74_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F74\", modrm_byte, r);\n}\npub fn instr_660F74_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F74\", r1, r2);\n}\npub fn instr_660F75_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F75\", modrm_byte, r);\n}\npub fn instr_660F75_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F75\", r1, r2);\n}\npub fn instr_660F76_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F76\", modrm_byte, r);\n}\npub fn instr_660F76_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F76\", r1, r2);\n}\n\npub fn instr_660F7C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F7C\", modrm_byte, r);\n}\npub fn instr_660F7C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F7C\", r1, r2);\n}\npub fn instr_F20F7C_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_F20F7C\", modrm_byte, r);\n}\npub fn instr_F20F7C_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_F20F7C\", r1, r2);\n}\npub fn instr_660F7D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660F7D\", modrm_byte, r);\n}\npub fn instr_660F7D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660F7D\", r1, r2);\n}\npub fn instr_F20F7D_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_F20F7D\", modrm_byte, r);\n}\npub fn instr_F20F7D_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_F20F7D\", r1, r2);\n}\n\npub fn instr_0F7E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1_ret(\"instr_0F7E\");\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr_0F7E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn1_ret(\"instr_0F7E\");\n    codegen::gen_set_reg32(ctx, r1);\n}\n\npub fn instr_660F7E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r));\n    let value_local = ctx.builder.set_new_local();\n    codegen::gen_safe_write32(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local(value_local);\n}\npub fn instr_660F7E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_reg_xmm_offset(r2));\n    codegen::gen_set_reg32(ctx, r1);\n}\n\npub fn instr_0F7F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder.const_i32(r as i32);\n    ctx.builder.call_fn1_ret_i64(\"instr_0F7F\");\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n}\npub fn instr_0F7F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.const_i32(r2 as i32);\n    ctx.builder.call_fn2(\"instr_0F7F_reg\")\n}\n\npub fn instr_F30F7E_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32);\n    codegen::gen_modrm_resolve_safe_read64(ctx, modrm_byte);\n    ctx.builder.store_aligned_i64(0);\n\n    ctx.builder\n        .const_i32(global_pointers::get_reg_xmm_offset(r) as i32 + 8);\n    ctx.builder.const_i64(0);\n    ctx.builder.store_aligned_i64(0);\n}\npub fn instr_F30F7E_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    instr_660FD6_reg_jit(ctx, r2, r1)\n}\n\npub fn instr_660F7F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r);\n}\npub fn instr_660F7F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r2, r1) }\npub fn instr_F30F7F_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r);\n}\npub fn instr_F30F7F_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) { sse_mov_xmm_xmm(ctx, r2, r1) }\n\npub fn instr16_0FA0_jit(ctx: &mut JitContext) {\n    codegen::gen_get_sreg(ctx, regs::FS);\n    let sreg = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &sreg);\n    ctx.builder.free_local(sreg);\n}\npub fn instr32_0FA0_jit(ctx: &mut JitContext) { codegen::gen_push32_sreg(ctx, regs::FS) }\npub fn instr16_0FA8_jit(ctx: &mut JitContext) {\n    codegen::gen_get_sreg(ctx, regs::GS);\n    let sreg = ctx.builder.set_new_local();\n    codegen::gen_push16(ctx, &sreg);\n    ctx.builder.free_local(sreg);\n}\npub fn instr32_0FA8_jit(ctx: &mut JitContext) { codegen::gen_push32_sreg(ctx, regs::GS) }\n\npub fn instr16_0FA3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_bt(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        15,\n    )\n}\npub fn instr16_0FA3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    codegen::gen_get_reg16(ctx, r);\n    codegen::sign_extend_i16(ctx.builder);\n    ctx.builder.const_i32(3);\n    ctx.builder.shr_s_i32();\n    ctx.builder.add_i32();\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read8(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    let value = ctx.builder.set_new_local();\n    gen_bt(\n        &mut ctx.builder,\n        &value,\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r as usize]),\n        7,\n    );\n    ctx.builder.free_local(value);\n}\npub fn instr32_0FA3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_bt(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        31,\n    )\n}\npub fn instr32_0FA3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    codegen::gen_get_reg32(ctx, r);\n    ctx.builder.const_i32(3);\n    ctx.builder.shr_s_i32();\n    ctx.builder.add_i32();\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read8(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    let value = ctx.builder.set_new_local();\n    gen_bt(\n        &mut ctx.builder,\n        &value,\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r as usize]),\n        7,\n    );\n    ctx.builder.free_local(value);\n}\n\npub fn instr16_0FAB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_bts(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        15,\n    )\n}\npub fn instr16_0FAB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_bts,\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n        16,\n    );\n}\npub fn instr32_0FAB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_bts(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        31,\n    )\n}\npub fn instr32_0FAB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_bts,\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n        32,\n    );\n}\n\npub fn instr16_0FB3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_btr(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        15,\n    )\n}\npub fn instr16_0FB3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btr,\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n        16,\n    );\n}\npub fn instr32_0FB3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_btr(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        31,\n    )\n}\npub fn instr32_0FB3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btr,\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n        32,\n    );\n}\n\npub fn instr16_0FBB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_btc(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        15,\n    )\n}\npub fn instr16_0FBB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btc,\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n        16,\n    );\n}\npub fn instr32_0FBB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    gen_btc(\n        &mut ctx.builder,\n        &ctx.register_locals[r1 as usize],\n        &LocalOrImmediate::WasmLocal(&ctx.register_locals[r2 as usize]),\n        31,\n    )\n}\npub fn instr32_0FBB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btc,\n        &LocalOrImmediate::WasmLocal(&ctx.reg(r)),\n        32,\n    );\n}\n\npub fn instr16_0FBA_4_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_bt(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        15,\n    )\n}\npub fn instr16_0FBA_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let offset = (imm8 as i32 & 15) >> 3;\n    if offset != 0 {\n        ctx.builder.const_i32(offset);\n        ctx.builder.add_i32();\n    }\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read8(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    let value = ctx.builder.set_new_local();\n    gen_bt(\n        &mut ctx.builder,\n        &value,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        7,\n    );\n    ctx.builder.free_local(value);\n}\npub fn instr32_0FBA_4_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_bt(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        31,\n    )\n}\npub fn instr32_0FBA_4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let offset = (imm8 as i32 & 31) >> 3;\n    if offset != 0 {\n        ctx.builder.const_i32(offset);\n        ctx.builder.add_i32();\n    }\n    let address_local = ctx.builder.set_new_local();\n    codegen::gen_safe_read8(ctx, &address_local);\n    ctx.builder.free_local(address_local);\n    let value = ctx.builder.set_new_local();\n    gen_bt(\n        &mut ctx.builder,\n        &value,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        7,\n    );\n    ctx.builder.free_local(value);\n}\n\npub fn instr16_0FBA_5_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_bts(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        15,\n    )\n}\npub fn instr16_0FBA_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_bts,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        16,\n    );\n}\npub fn instr32_0FBA_5_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_bts(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        31,\n    )\n}\npub fn instr32_0FBA_5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_bts,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        32,\n    );\n}\n\npub fn instr16_0FBA_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_btr(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        15,\n    )\n}\npub fn instr16_0FBA_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btr,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        16,\n    );\n}\npub fn instr32_0FBA_6_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_btr(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        31,\n    )\n}\npub fn instr32_0FBA_6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btr,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        32,\n    );\n}\n\npub fn instr16_0FBA_7_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_btc(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        15,\n    )\n}\npub fn instr16_0FBA_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btc,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        16,\n    );\n}\npub fn instr32_0FBA_7_reg_jit(ctx: &mut JitContext, r: u32, imm8: u32) {\n    gen_btc(\n        &mut ctx.builder,\n        &ctx.register_locals[r as usize],\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        31,\n    )\n}\npub fn instr32_0FBA_7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, imm8: u32) {\n    gen_bit_rmw(\n        ctx,\n        modrm_byte,\n        &gen_btc,\n        &LocalOrImmediate::Immediate(imm8 as i32),\n        32,\n    );\n}\n\npub fn instr_0FAE_5_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte) {\n    dbg_log!(\"Generating #ud for unimplemented instruction: instr_0FAE_5_mem_jit\");\n    codegen::gen_trigger_ud(ctx);\n}\npub fn instr_0FAE_5_reg_jit(_ctx: &mut JitContext, _r: u32) {\n    // For this instruction, the processor ignores the r/m field of the ModR/M byte.\n}\n\npub fn instr_0FD1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD1\", modrm_byte, r);\n}\npub fn instr_0FD1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD1\", r1, r2);\n}\npub fn instr_0FD2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD2\", modrm_byte, r);\n}\npub fn instr_0FD2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD2\", r1, r2);\n}\npub fn instr_0FD3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD3\", modrm_byte, r);\n}\npub fn instr_0FD3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD3\", r1, r2);\n}\npub fn instr_0FD4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD4\", modrm_byte, r);\n}\npub fn instr_0FD4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD4\", r1, r2);\n}\npub fn instr_0FD5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD5\", modrm_byte, r);\n}\npub fn instr_0FD5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD5\", r1, r2);\n}\n\npub fn instr_0FD7_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _r: u32) {\n    codegen::gen_trigger_ud(ctx)\n}\npub fn instr_0FD7_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.call_fn1_ret(\"instr_0FD7\");\n    codegen::gen_set_reg32(ctx, r2);\n}\n\npub fn instr_0FD8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD8\", modrm_byte, r);\n}\npub fn instr_0FD8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD8\", r1, r2);\n}\npub fn instr_0FD9_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FD9\", modrm_byte, r);\n}\npub fn instr_0FD9_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FD9\", r1, r2);\n}\npub fn instr_0FDA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FDA\", modrm_byte, r);\n}\npub fn instr_0FDA_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FDA\", r1, r2);\n}\npub fn instr_0FDB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FDB\", modrm_byte, r);\n}\npub fn instr_0FDB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FDB\", r1, r2);\n}\npub fn instr_0FDC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FDC\", modrm_byte, r);\n}\npub fn instr_0FDC_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FDC\", r1, r2);\n}\npub fn instr_0FDD_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FDD\", modrm_byte, r);\n}\npub fn instr_0FDD_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FDD\", r1, r2);\n}\npub fn instr_0FDE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FDE\", modrm_byte, r);\n}\npub fn instr_0FDE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FDE\", r1, r2);\n}\npub fn instr_0FDF_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FDF\", modrm_byte, r);\n}\npub fn instr_0FDF_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FDF\", r1, r2);\n}\n\npub fn instr_660FD1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD1\", modrm_byte, r);\n}\npub fn instr_660FD1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD1\", r1, r2);\n}\npub fn instr_660FD2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD2\", modrm_byte, r);\n}\npub fn instr_660FD2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD2\", r1, r2);\n}\npub fn instr_660FD3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD3\", modrm_byte, r);\n}\npub fn instr_660FD3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD3\", r1, r2);\n}\npub fn instr_660FD4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD4\", modrm_byte, r);\n}\npub fn instr_660FD4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD4\", r1, r2);\n}\npub fn instr_660FD5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD5\", modrm_byte, r);\n}\npub fn instr_660FD5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD5\", r1, r2);\n}\n\npub fn instr_660FD6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    codegen::gen_modrm_resolve(ctx, modrm_byte);\n    let address_local = ctx.builder.set_new_local();\n    ctx.builder\n        .load_fixed_i64(global_pointers::get_reg_xmm_offset(r));\n    let value_local = ctx.builder.set_new_local_i64();\n    codegen::gen_safe_write64(ctx, &address_local, &value_local);\n    ctx.builder.free_local(address_local);\n    ctx.builder.free_local_i64(value_local);\n}\npub fn instr_660FD6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(0);\n    ctx.builder\n        .load_fixed_i64(global_pointers::get_reg_xmm_offset(r2));\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r1));\n    ctx.builder.const_i32(0);\n    ctx.builder.const_i64(0);\n    ctx.builder\n        .store_aligned_i64(global_pointers::get_reg_xmm_offset(r1) + 8);\n}\n\npub fn instr_660FD7_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _r: u32) {\n    codegen::gen_trigger_ud(ctx)\n}\npub fn instr_660FD7_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.call_fn1_ret(\"instr_660FD7\");\n    codegen::gen_set_reg32(ctx, r2);\n}\n\npub fn instr_660FD8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD8\", modrm_byte, r);\n}\npub fn instr_660FD8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD8\", r1, r2);\n}\npub fn instr_660FD9_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FD9\", modrm_byte, r);\n}\npub fn instr_660FD9_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FD9\", r1, r2);\n}\npub fn instr_660FDA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FDA\", modrm_byte, r);\n}\npub fn instr_660FDA_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FDA\", r1, r2);\n}\npub fn instr_660FDB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FDB\", modrm_byte, r);\n}\npub fn instr_660FDB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FDB\", r1, r2);\n}\npub fn instr_660FDC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FDC\", modrm_byte, r);\n}\npub fn instr_660FDC_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FDC\", r1, r2);\n}\npub fn instr_660FDD_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FDD\", modrm_byte, r);\n}\npub fn instr_660FDD_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FDD\", r1, r2);\n}\npub fn instr_660FDE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FDE\", modrm_byte, r);\n}\npub fn instr_660FDE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FDE\", r1, r2);\n}\npub fn instr_660FDF_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FDF\", modrm_byte, r);\n}\npub fn instr_660FDF_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FDF\", r1, r2);\n}\n\npub fn instr_0FE0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE0\", modrm_byte, r);\n}\npub fn instr_0FE0_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE0\", r1, r2);\n}\npub fn instr_0FE1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE1\", modrm_byte, r);\n}\npub fn instr_0FE1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE1\", r1, r2);\n}\npub fn instr_0FE2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE2\", modrm_byte, r);\n}\npub fn instr_0FE2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE2\", r1, r2);\n}\npub fn instr_0FE3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE3\", modrm_byte, r);\n}\npub fn instr_0FE3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE3\", r1, r2);\n}\npub fn instr_0FE4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE4\", modrm_byte, r);\n}\npub fn instr_0FE4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE4\", r1, r2);\n}\npub fn instr_0FE5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE5\", modrm_byte, r);\n}\npub fn instr_0FE5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE5\", r1, r2);\n}\n\npub fn instr_0FE8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE8\", modrm_byte, r);\n}\npub fn instr_0FE8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE8\", r1, r2);\n}\npub fn instr_0FE9_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FE9\", modrm_byte, r);\n}\npub fn instr_0FE9_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FE9\", r1, r2);\n}\npub fn instr_0FEA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FEA\", modrm_byte, r);\n}\npub fn instr_0FEA_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FEA\", r1, r2);\n}\npub fn instr_0FEB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FEB\", modrm_byte, r);\n}\npub fn instr_0FEB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FEB\", r1, r2);\n}\npub fn instr_0FEC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FEC\", modrm_byte, r);\n}\npub fn instr_0FEC_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FEC\", r1, r2);\n}\npub fn instr_0FED_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FED\", modrm_byte, r);\n}\npub fn instr_0FED_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FED\", r1, r2);\n}\npub fn instr_0FEE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FEE\", modrm_byte, r);\n}\npub fn instr_0FEE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FEE\", r1, r2);\n}\npub fn instr_0FEF_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FEF\", modrm_byte, r);\n}\npub fn instr_0FEF_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FEF\", r1, r2);\n}\n\npub fn instr_660FE0_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE0\", modrm_byte, r);\n}\npub fn instr_660FE0_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE0\", r1, r2);\n}\npub fn instr_660FE1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE1\", modrm_byte, r);\n}\npub fn instr_660FE1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE1\", r1, r2);\n}\npub fn instr_660FE2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE2\", modrm_byte, r);\n}\npub fn instr_660FE2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE2\", r1, r2);\n}\npub fn instr_660FE3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE3\", modrm_byte, r);\n}\npub fn instr_660FE3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE3\", r1, r2);\n}\npub fn instr_660FE4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE4\", modrm_byte, r);\n}\npub fn instr_660FE4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE4\", r1, r2);\n}\npub fn instr_660FE5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE5\", modrm_byte, r);\n}\npub fn instr_660FE5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE5\", r1, r2);\n}\n\npub fn instr_660FE6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE6\", modrm_byte, r);\n}\npub fn instr_660FE6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE6\", r1, r2);\n}\npub fn instr_F20FE6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_F20FE6\", modrm_byte, r);\n}\npub fn instr_F20FE6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_F20FE6\", r1, r2);\n}\npub fn instr_F30FE6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read64_xmm_mem(ctx, \"instr_F30FE6\", modrm_byte, r);\n}\npub fn instr_F30FE6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read64_xmm_xmm(ctx, \"instr_F30FE6\", r1, r2);\n}\n\npub fn instr_660FE7_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    instr_0F29_mem_jit(ctx, modrm_byte, r);\n}\npub fn instr_660FE7_reg_jit(ctx: &mut JitContext, _r1: u32, _r2: u32) {\n    codegen::gen_trigger_ud(ctx);\n}\n\npub fn instr_660FE8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE8\", modrm_byte, r);\n}\npub fn instr_660FE8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE8\", r1, r2);\n}\npub fn instr_660FE9_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FE9\", modrm_byte, r);\n}\npub fn instr_660FE9_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FE9\", r1, r2);\n}\npub fn instr_660FEA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FEA\", modrm_byte, r);\n}\npub fn instr_660FEA_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FEA\", r1, r2);\n}\npub fn instr_660FEB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FEB\", modrm_byte, r);\n}\npub fn instr_660FEB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FEB\", r1, r2);\n}\npub fn instr_660FEC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FEC\", modrm_byte, r);\n}\npub fn instr_660FEC_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FEC\", r1, r2);\n}\npub fn instr_660FED_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FED\", modrm_byte, r);\n}\npub fn instr_660FED_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FED\", r1, r2);\n}\npub fn instr_660FEE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FEE\", modrm_byte, r);\n}\npub fn instr_660FEE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FEE\", r1, r2);\n}\npub fn instr_660FEF_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FEF\", modrm_byte, r);\n}\npub fn instr_660FEF_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FEF\", r1, r2);\n}\n\npub fn instr_0FF1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF1\", modrm_byte, r);\n}\npub fn instr_0FF1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF1\", r1, r2);\n}\npub fn instr_0FF2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF2\", modrm_byte, r);\n}\npub fn instr_0FF2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF2\", r1, r2);\n}\npub fn instr_0FF3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF3\", modrm_byte, r);\n}\npub fn instr_0FF3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF3\", r1, r2);\n}\npub fn instr_0FF4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF4\", modrm_byte, r);\n}\npub fn instr_0FF4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF4\", r1, r2);\n}\npub fn instr_0FF5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF5\", modrm_byte, r);\n}\npub fn instr_0FF5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF5\", r1, r2);\n}\npub fn instr_0FF6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF6\", modrm_byte, r);\n}\npub fn instr_0FF6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF6\", r1, r2);\n}\n\npub fn instr_0FF7_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _r: u32) {\n    codegen::gen_trigger_ud(ctx)\n}\npub fn instr_0FF7_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n        ctx.builder,\n        ctx.start_of_current_instruction as i32 & 0xFFF,\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.const_i32(r2 as i32);\n    if ctx.cpu.asize_32() {\n        codegen::gen_get_reg32(ctx, regs::EDI);\n    }\n    else {\n        codegen::gen_get_reg16(ctx, regs::DI);\n    }\n    jit_add_seg_offset(ctx, regs::DS);\n    ctx.builder.call_fn3(\"maskmovq\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n\n    codegen::gen_get_page_fault(ctx.builder);\n    ctx.builder.if_void();\n    codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_label);\n    ctx.builder.block_end();\n}\n\npub fn instr_0FF8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF8\", modrm_byte, r);\n}\npub fn instr_0FF8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF8\", r1, r2);\n}\npub fn instr_0FF9_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FF9\", modrm_byte, r);\n}\npub fn instr_0FF9_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FF9\", r1, r2);\n}\npub fn instr_0FFA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FFA\", modrm_byte, r);\n}\npub fn instr_0FFA_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FFA\", r1, r2);\n}\npub fn instr_0FFB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FFB\", modrm_byte, r);\n}\npub fn instr_0FFB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FFB\", r1, r2);\n}\npub fn instr_0FFC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FFC\", modrm_byte, r);\n}\npub fn instr_0FFC_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FFC\", r1, r2);\n}\npub fn instr_0FFD_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FFD\", modrm_byte, r);\n}\npub fn instr_0FFD_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FFD\", r1, r2);\n}\npub fn instr_0FFE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    mmx_read64_mm_mem(ctx, \"instr_0FFE\", modrm_byte, r);\n}\npub fn instr_0FFE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    mmx_read64_mm_mm(ctx, \"instr_0FFE\", r1, r2);\n}\n\npub fn instr_660FF1_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF1\", modrm_byte, r);\n}\npub fn instr_660FF1_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF1\", r1, r2);\n}\npub fn instr_660FF2_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF2\", modrm_byte, r);\n}\npub fn instr_660FF2_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF2\", r1, r2);\n}\npub fn instr_660FF3_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF3\", modrm_byte, r);\n}\npub fn instr_660FF3_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF3\", r1, r2);\n}\npub fn instr_660FF4_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF4\", modrm_byte, r);\n}\npub fn instr_660FF4_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF4\", r1, r2);\n}\npub fn instr_660FF5_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF5\", modrm_byte, r);\n}\npub fn instr_660FF5_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF5\", r1, r2);\n}\npub fn instr_660FF6_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF6\", modrm_byte, r);\n}\npub fn instr_660FF6_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF6\", r1, r2);\n}\n\npub fn instr_660FF7_mem_jit(ctx: &mut JitContext, _modrm_byte: ModrmByte, _r: u32) {\n    codegen::gen_trigger_ud(ctx)\n}\npub fn instr_660FF7_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    codegen::gen_set_previous_eip_offset_from_eip_with_low_bits(\n        ctx.builder,\n        ctx.start_of_current_instruction as i32 & 0xFFF,\n    );\n\n    codegen::gen_move_registers_from_locals_to_memory(ctx);\n    ctx.builder.const_i32(r1 as i32);\n    ctx.builder.const_i32(r2 as i32);\n    if ctx.cpu.asize_32() {\n        codegen::gen_get_reg32(ctx, regs::EDI);\n    }\n    else {\n        codegen::gen_get_reg16(ctx, regs::DI);\n    }\n    jit_add_seg_offset(ctx, regs::DS);\n    ctx.builder.call_fn3(\"maskmovdqu\");\n    codegen::gen_move_registers_from_memory_to_locals(ctx);\n\n    codegen::gen_get_page_fault(ctx.builder);\n    ctx.builder.if_void();\n    codegen::gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);\n    ctx.builder.br(ctx.exit_label);\n    ctx.builder.block_end();\n}\n\npub fn instr_660FF8_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF8\", modrm_byte, r);\n}\npub fn instr_660FF8_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF8\", r1, r2);\n}\npub fn instr_660FF9_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FF9\", modrm_byte, r);\n}\npub fn instr_660FF9_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FF9\", r1, r2);\n}\npub fn instr_660FFA_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FFA\", modrm_byte, r);\n}\npub fn instr_660FFA_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FFA\", r1, r2);\n}\npub fn instr_660FFB_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FFB\", modrm_byte, r);\n}\npub fn instr_660FFB_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FFB\", r1, r2);\n}\npub fn instr_660FFC_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FFC\", modrm_byte, r);\n}\npub fn instr_660FFC_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FFC\", r1, r2);\n}\npub fn instr_660FFD_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FFD\", modrm_byte, r);\n}\npub fn instr_660FFD_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FFD\", r1, r2);\n}\npub fn instr_660FFE_mem_jit(ctx: &mut JitContext, modrm_byte: ModrmByte, r: u32) {\n    sse_read128_xmm_mem(ctx, \"instr_660FFE\", modrm_byte, r);\n}\npub fn instr_660FFE_reg_jit(ctx: &mut JitContext, r1: u32, r2: u32) {\n    sse_read128_xmm_xmm(ctx, \"instr_660FFE\", r1, r2);\n}\n"
  },
  {
    "path": "src/rust/js_api.rs",
    "content": "use crate::cpu::cpu::translate_address_system_read;\n\n#[no_mangle]\npub unsafe fn translate_address_system_read_js(addr: i32) -> u32 {\n    translate_address_system_read(addr).unwrap()\n}\n"
  },
  {
    "path": "src/rust/leb.rs",
    "content": "pub fn write_leb_i32(buf: &mut Vec<u8>, v: i32) { write_leb_i64(buf, v as i64); }\n\npub fn write_leb_i64(buf: &mut Vec<u8>, mut v: i64) {\n    // https://en.wikipedia.org/wiki/LEB128#Encode_signed_integer\n    loop {\n        let mut byte = v as u8 & 0b0111_1111;\n        v >>= 7;\n        let sign = byte & (1 << 6);\n        let done = v == 0 && sign == 0 || v == -1 && sign != 0;\n        if !done {\n            byte |= 0b1000_0000;\n        }\n        buf.push(byte);\n        if done {\n            break;\n        }\n    }\n}\n\npub fn write_leb_u32(buf: &mut Vec<u8>, mut v: u32) {\n    loop {\n        let mut byte = v as u8 & 0b0111_1111;\n        v >>= 7;\n        if v != 0 {\n            byte |= 0b1000_0000;\n        }\n        buf.push(byte);\n        if v == 0 {\n            break;\n        }\n    }\n}\n\npub fn write_fixed_leb16_at_idx(vec: &mut Vec<u8>, idx: usize, x: u16) {\n    dbg_assert!(x < (1 << 14)); // we have 14 bits of available space in 2 bytes for leb\n    vec[idx] = ((x & 0b1111111) | 0b10000000) as u8;\n    vec[idx + 1] = (x >> 7) as u8;\n}\n\npub fn write_fixed_leb32_at_idx(vec: &mut Vec<u8>, idx: usize, x: u32) {\n    dbg_assert!(x < (1 << 28)); // we have 28 bits of available space in 4 bytes for leb\n    vec[idx] = (x & 0b1111111) as u8 | 0b10000000;\n    vec[idx + 1] = (x >> 7 & 0b1111111) as u8 | 0b10000000;\n    vec[idx + 2] = (x >> 14 & 0b1111111) as u8 | 0b10000000;\n    vec[idx + 3] = (x >> 21 & 0b1111111) as u8;\n}\n"
  },
  {
    "path": "src/rust/lib.rs",
    "content": "#[macro_use]\nmod dbg;\n\n#[macro_use]\nmod paging;\n\npub mod cpu;\n\npub mod js_api;\npub mod profiler;\n\nmod analysis;\nmod codegen;\nmod config;\nmod control_flow;\nmod cpu_context;\nmod gen;\nmod jit;\nmod jit_instructions;\nmod leb;\nmod modrm;\nmod opstats;\nmod page;\nmod prefix;\nmod regs;\nmod softfloat;\nmod state_flags;\nmod wasmgen;\nmod zstd;\n"
  },
  {
    "path": "src/rust/modrm.rs",
    "content": "use crate::codegen;\nuse crate::cpu::global_pointers;\nuse crate::cpu_context::CpuContext;\nuse crate::jit::JitContext;\nuse crate::prefix::{PREFIX_MASK_SEGMENT, SEG_PREFIX_ZERO};\nuse crate::profiler;\nuse crate::regs::{BP, BX, DI, SI};\nuse crate::regs::{CS, DS, ES, FS, GS, SS};\nuse crate::regs::{EAX, EBP, EBX, ECX, EDI, EDX, ESI, ESP};\n\npub struct ModrmByte {\n    segment: u32,\n    first_reg: Option<u32>,\n    second_reg: Option<u32>,\n    shift: u8,\n    immediate: i32,\n    is_16: bool,\n}\nimpl ModrmByte {\n    pub fn is_nop(&self, reg: u32) -> bool {\n        self.first_reg == Some(reg)\n            && self.second_reg.is_none()\n            && self.shift == 0\n            && self.immediate == 0\n            && !self.is_16\n    }\n}\n\npub fn decode(ctx: &mut CpuContext, modrm_byte: u8) -> ModrmByte {\n    if ctx.asize_32() {\n        decode32(ctx, modrm_byte)\n    }\n    else {\n        decode16(ctx, modrm_byte)\n    }\n}\n\nfn decode16(ctx: &mut CpuContext, modrm_byte: u8) -> ModrmByte {\n    fn mk16(\n        segment: u32,\n        first_reg: Option<u32>,\n        second_reg: Option<u32>,\n        immediate: i32,\n    ) -> ModrmByte {\n        ModrmByte {\n            segment,\n            first_reg,\n            second_reg,\n            shift: 0,\n            immediate,\n            is_16: true,\n        }\n    }\n\n    match modrm_byte & !0o070 {\n        0o000 => mk16(DS, Some(BX), Some(SI), 0),\n        0o001 => mk16(DS, Some(BX), Some(DI), 0),\n        0o002 => mk16(SS, Some(BP), Some(SI), 0),\n        0o003 => mk16(SS, Some(BP), Some(DI), 0),\n        0o004 => mk16(DS, Some(SI), None, 0),\n        0o005 => mk16(DS, Some(DI), None, 0),\n        0o006 => mk16(DS, None, None, ctx.read_imm16() as i32),\n        0o007 => mk16(DS, Some(BX), None, 0),\n\n        0o100 => mk16(DS, Some(BX), Some(SI), ctx.read_imm8s() as i32),\n        0o101 => mk16(DS, Some(BX), Some(DI), ctx.read_imm8s() as i32),\n        0o102 => mk16(SS, Some(BP), Some(SI), ctx.read_imm8s() as i32),\n        0o103 => mk16(SS, Some(BP), Some(DI), ctx.read_imm8s() as i32),\n        0o104 => mk16(DS, Some(SI), None, ctx.read_imm8s() as i32),\n        0o105 => mk16(DS, Some(DI), None, ctx.read_imm8s() as i32),\n        0o106 => mk16(SS, Some(BP), None, ctx.read_imm8s() as i32),\n        0o107 => mk16(DS, Some(BX), None, ctx.read_imm8s() as i32),\n\n        0o200 => mk16(DS, Some(BX), Some(SI), ctx.read_imm16() as i32),\n        0o201 => mk16(DS, Some(BX), Some(DI), ctx.read_imm16() as i32),\n        0o202 => mk16(SS, Some(BP), Some(SI), ctx.read_imm16() as i32),\n        0o203 => mk16(SS, Some(BP), Some(DI), ctx.read_imm16() as i32),\n        0o204 => mk16(DS, Some(SI), None, ctx.read_imm16() as i32),\n        0o205 => mk16(DS, Some(DI), None, ctx.read_imm16() as i32),\n        0o206 => mk16(SS, Some(BP), None, ctx.read_imm16() as i32),\n        0o207 => mk16(DS, Some(BX), None, ctx.read_imm16() as i32),\n\n        _ => panic!(\"modrm byte >= 0xC0\"),\n    }\n}\n\nfn decode32(ctx: &mut CpuContext, modrm_byte: u8) -> ModrmByte {\n    fn mk32(segment: u32, first_reg: Option<u32>, immediate: i32) -> ModrmByte {\n        ModrmByte {\n            segment,\n            first_reg,\n            second_reg: None,\n            shift: 0,\n            immediate,\n            is_16: false,\n        }\n    }\n\n    match modrm_byte & !0o070 {\n        0o000 => mk32(DS, Some(EAX), 0),\n        0o001 => mk32(DS, Some(ECX), 0),\n        0o002 => mk32(DS, Some(EDX), 0),\n        0o003 => mk32(DS, Some(EBX), 0),\n        0o004 => decode_sib(ctx, Imm32::None),\n        0o005 => mk32(DS, None, ctx.read_imm32() as i32),\n        0o006 => mk32(DS, Some(ESI), 0),\n        0o007 => mk32(DS, Some(EDI), 0),\n\n        0o100 => mk32(DS, Some(EAX), ctx.read_imm8s() as i32),\n        0o101 => mk32(DS, Some(ECX), ctx.read_imm8s() as i32),\n        0o102 => mk32(DS, Some(EDX), ctx.read_imm8s() as i32),\n        0o103 => mk32(DS, Some(EBX), ctx.read_imm8s() as i32),\n        0o104 => decode_sib(ctx, Imm32::Imm8),\n        0o105 => mk32(SS, Some(EBP), ctx.read_imm8s() as i32),\n        0o106 => mk32(DS, Some(ESI), ctx.read_imm8s() as i32),\n        0o107 => mk32(DS, Some(EDI), ctx.read_imm8s() as i32),\n\n        0o200 => mk32(DS, Some(EAX), ctx.read_imm32() as i32),\n        0o201 => mk32(DS, Some(ECX), ctx.read_imm32() as i32),\n        0o202 => mk32(DS, Some(EDX), ctx.read_imm32() as i32),\n        0o203 => mk32(DS, Some(EBX), ctx.read_imm32() as i32),\n        0o204 => decode_sib(ctx, Imm32::Imm32),\n        0o205 => mk32(SS, Some(EBP), ctx.read_imm32() as i32),\n        0o206 => mk32(DS, Some(ESI), ctx.read_imm32() as i32),\n        0o207 => mk32(DS, Some(EDI), ctx.read_imm32() as i32),\n\n        _ => panic!(\"modrm byte >= 0xC0\"),\n    }\n}\n\nfn decode_sib(ctx: &mut CpuContext, immediate: Imm32) -> ModrmByte {\n    let sib_byte = ctx.read_imm8();\n    let r = sib_byte & 7;\n    let m = sib_byte >> 3 & 7;\n    let shift = sib_byte >> 6 & 3;\n\n    let second_reg = if m == 4 { None } else { Some(m as u32) };\n\n    let segment;\n    let reg;\n\n    if r == 4 {\n        segment = SS;\n        reg = ESP;\n    }\n    else if r == 5 {\n        if immediate == Imm32::None {\n            return ModrmByte {\n                segment: DS,\n                first_reg: None,\n                second_reg,\n                shift,\n                immediate: ctx.read_imm32() as i32,\n                is_16: false,\n            };\n        }\n        else {\n            segment = SS;\n            reg = EBP;\n        }\n    }\n    else {\n        segment = DS;\n        reg = r as u32;\n    }\n\n    let immediate = match immediate {\n        Imm32::None => 0,\n        Imm32::Imm8 => ctx.read_imm8s() as i32,\n        Imm32::Imm32 => ctx.read_imm32() as i32,\n    };\n\n    ModrmByte {\n        segment,\n        first_reg: Some(reg),\n        second_reg,\n        shift,\n        immediate,\n        is_16: false,\n    }\n}\n\npub fn gen(ctx: &mut JitContext, modrm_byte: ModrmByte, esp_offset: i32) {\n    codegen::gen_profiler_stat_increment(\n        ctx.builder,\n        match modrm_byte {\n            ModrmByte {\n                first_reg: None,\n                second_reg: None,\n                ..\n            } => profiler::stat::MODRM_SIMPLE_CONST_OFFSET,\n            ModrmByte {\n                first_reg: Some(_),\n                second_reg: None,\n                ..\n            }\n            | ModrmByte {\n                first_reg: None,\n                second_reg: Some(_),\n                shift: 0,\n                ..\n            } => {\n                if modrm_byte.immediate == 0 {\n                    profiler::stat::MODRM_SIMPLE_REG\n                }\n                else {\n                    profiler::stat::MODRM_SIMPLE_REG_WITH_OFFSET\n                }\n            },\n            _ => profiler::stat::MODRM_COMPLEX,\n        },\n    );\n\n    let mut have_something_on_stack = false;\n\n    if let Some(reg) = modrm_byte.first_reg {\n        codegen::gen_get_reg32(ctx, reg);\n        if reg == ESP && esp_offset != 0 {\n            ctx.builder.const_i32(esp_offset);\n            ctx.builder.add_i32();\n        }\n        have_something_on_stack = true;\n    }\n\n    if let Some(reg) = modrm_byte.second_reg {\n        codegen::gen_get_reg32(ctx, reg);\n        dbg_assert!(reg != ESP); // second reg cannot be esp, no need to handle esp_offset\n        if modrm_byte.shift != 0 {\n            ctx.builder.const_i32(modrm_byte.shift.into());\n            ctx.builder.shl_i32();\n        }\n        if have_something_on_stack {\n            ctx.builder.add_i32();\n        }\n        have_something_on_stack = true;\n    }\n\n    if modrm_byte.immediate != 0 || !have_something_on_stack {\n        ctx.builder.const_i32(modrm_byte.immediate);\n        if have_something_on_stack {\n            ctx.builder.add_i32();\n        }\n    }\n\n    if modrm_byte.is_16 {\n        ctx.builder.const_i32(0xFFFF);\n        ctx.builder.and_i32();\n    }\n    jit_add_seg_offset(ctx, modrm_byte.segment);\n}\n\npub fn get_as_reg_index_if_possible(ctx: &mut JitContext, modrm_byte: &ModrmByte) -> Option<u32> {\n    let prefix = ctx.cpu.prefixes & PREFIX_MASK_SEGMENT;\n    let seg = if prefix != 0 { (prefix - 1) as u32 } else { modrm_byte.segment };\n    if can_optimize_get_seg(ctx, seg)\n        && modrm_byte.second_reg.is_none()\n        && modrm_byte.immediate == 0\n        && !modrm_byte.is_16\n        && modrm_byte.shift == 0\n    {\n        modrm_byte.first_reg\n    }\n    else {\n        None\n    }\n}\n\npub fn skip(ctx: &mut CpuContext, modrm_byte: u8) { let _ = decode(ctx, modrm_byte); }\n\n#[derive(PartialEq)]\nenum Imm32 {\n    None,\n    Imm8,\n    Imm32,\n}\n\nfn can_optimize_get_seg(ctx: &mut JitContext, segment: u32) -> bool {\n    (segment == DS || segment == SS || segment == CS) && ctx.cpu.has_flat_segmentation()\n}\n\npub fn jit_add_seg_offset(ctx: &mut JitContext, default_segment: u32) {\n    let prefix = ctx.cpu.prefixes & PREFIX_MASK_SEGMENT;\n\n    if prefix == SEG_PREFIX_ZERO {\n        return;\n    }\n\n    let seg = if prefix != 0 { (prefix - 1) as u32 } else { default_segment };\n    jit_add_seg_offset_no_override(ctx, seg);\n}\n\npub fn jit_add_seg_offset_no_override(ctx: &mut JitContext, seg: u32) {\n    if can_optimize_get_seg(ctx, seg) {\n        codegen::gen_profiler_stat_increment(ctx.builder, profiler::stat::SEG_OFFSET_OPTIMISED);\n        return;\n    }\n    codegen::gen_profiler_stat_increment(ctx.builder, profiler::stat::SEG_OFFSET_NOT_OPTIMISED);\n    codegen::gen_profiler_stat_increment(\n        ctx.builder,\n        if seg == ES {\n            profiler::stat::SEG_OFFSET_NOT_OPTIMISED_ES\n        }\n        else if seg == FS {\n            profiler::stat::SEG_OFFSET_NOT_OPTIMISED_FS\n        }\n        else if seg == GS {\n            profiler::stat::SEG_OFFSET_NOT_OPTIMISED_GS\n        }\n        else {\n            profiler::stat::SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT\n        },\n    );\n\n    if seg != CS && seg != SS {\n        if cfg!(feature = \"profiler\") {\n            ctx.builder.const_i32(seg as i32);\n            ctx.builder.call_fn1(\"log_segment_null\");\n        }\n\n        ctx.builder\n            .load_fixed_u8(global_pointers::get_segment_is_null_offset(seg));\n        ctx.builder.if_void();\n        codegen::gen_trigger_gp(ctx, 0);\n        ctx.builder.block_end();\n    }\n\n    ctx.builder\n        .load_fixed_i32(global_pointers::get_seg_offset(seg));\n    ctx.builder.add_i32();\n}\n"
  },
  {
    "path": "src/rust/opstats.rs",
    "content": "use crate::wasmgen::wasm_builder::WasmBuilder;\n\nconst SIZE: usize = if cfg!(feature = \"profiler\") { 8192 } else { 0 };\n\n#[allow(non_upper_case_globals)]\npub static mut opstats_buffer: [u64; SIZE] = [0; SIZE];\n#[allow(non_upper_case_globals)]\npub static mut opstats_compiled_buffer: [u64; SIZE] = [0; SIZE];\n#[allow(non_upper_case_globals)]\npub static mut opstats_jit_exit_buffer: [u64; SIZE] = [0; SIZE];\n#[allow(non_upper_case_globals)]\npub static mut opstats_unguarded_register_buffer: [u64; SIZE] = [0; SIZE];\n#[allow(non_upper_case_globals)]\npub static mut opstats_wasm_size: [u64; SIZE] = [0; SIZE];\n\npub struct Instruction {\n    pub prefixes: Vec<u8>,\n    pub opcode: u8,\n    pub fixed_g: u8,\n    pub is_mem: bool,\n    pub is_0f: bool,\n}\n\npub fn decode(mut instruction: u32) -> Instruction {\n    let mut is_0f = false;\n    let mut prefixes = vec![];\n    let mut final_opcode = 0;\n\n    for _ in 0..4 {\n        let opcode = (instruction & 0xFF) as u8;\n        instruction >>= 8;\n\n        // TODO:\n        // - If the instruction uses 4 or more prefixes, only the prefixes will be counted\n\n        if is_0f {\n            final_opcode = opcode;\n            break;\n        }\n        else {\n            if opcode == 0x0F {\n                is_0f = true;\n            }\n            else if opcode == 0x26\n                || opcode == 0x2E\n                || opcode == 0x36\n                || opcode == 0x3E\n                || opcode == 0x64\n                || opcode == 0x65\n                || opcode == 0x66\n                || opcode == 0x67\n                || opcode == 0xF0\n                || opcode == 0xF2\n                || opcode == 0xF3\n            {\n                prefixes.push(opcode);\n            }\n            else {\n                final_opcode = opcode;\n                break;\n            }\n        }\n    }\n\n    let has_modrm_byte = if is_0f {\n        match final_opcode {\n            0x0 | 0x1 | 0x2 | 0x3 | 0x10 | 0x11 | 0x12 | 0x13 | 0x14 | 0x15 | 0x16 | 0x17\n            | 0x18 | 0x19 | 0x20 | 0x21 | 0x22 | 0x23 | 0x28 | 0x29 | 0x40 | 0x41 | 0x42 | 0x43\n            | 0x44 | 0x45 | 0x46 | 0x47 | 0x48 | 0x49 | 0x50 | 0x51 | 0x52 | 0x53 | 0x54 | 0x55\n            | 0x56 | 0x57 | 0x58 | 0x59 | 0x60 | 0x61 | 0x62 | 0x63 | 0x64 | 0x65 | 0x66 | 0x67\n            | 0x68 | 0x69 | 0x70 | 0x71 | 0x72 | 0x73 | 0x74 | 0x75 | 0x76 | 0x90 | 0x91 | 0x92\n            | 0x93 | 0x94 | 0x95 | 0x96 | 0x97 | 0x98 | 0x99 | 0x1c | 0x1d | 0x1e | 0x1f | 0x2a\n            | 0x2b | 0x2c | 0x2d | 0x2e | 0x2f | 0x4a | 0x4b | 0x4c | 0x4d | 0x4e | 0x4f | 0x5a\n            | 0x5b | 0x5c | 0x5d | 0x5e | 0x5f | 0x6a | 0x6b | 0x6c | 0x6d | 0x6e | 0x6f | 0x7e\n            | 0x7f | 0x9a | 0x9b | 0x9c | 0x9d | 0x9e | 0x9f | 0xa3 | 0xa4 | 0xa5 | 0xab | 0xac\n            | 0xad | 0xae | 0xaf | 0xb0 | 0xb1 | 0xb2 | 0xb3 | 0xb4 | 0xb5 | 0xb6 | 0xb7 | 0xb8\n            | 0xba | 0xbb | 0xbc | 0xbd | 0xbe | 0xbf | 0xc0 | 0xc1 | 0xc2 | 0xc3 | 0xc4 | 0xc5\n            | 0xc6 | 0xc7 | 0xd1 | 0xd2 | 0xd3 | 0xd4 | 0xd5 | 0xd6 | 0xd7 | 0xd8 | 0xd9 | 0xda\n            | 0xdb | 0xdc | 0xdd | 0xde | 0xdf | 0xe0 | 0xe1 | 0xe2 | 0xe3 | 0xe4 | 0xe5 | 0xe6\n            | 0xe7 | 0xe8 | 0xe9 | 0xea | 0xeb | 0xec | 0xed | 0xee | 0xef | 0xf1 | 0xf2 | 0xf3\n            | 0xf4 | 0xf5 | 0xf6 | 0xf7 | 0xf8 | 0xf9 | 0xfa | 0xfb | 0xfc | 0xfd | 0xfe => true,\n            _ => false,\n        }\n    }\n    else {\n        match final_opcode {\n            0x0 | 0x1 | 0x2 | 0x3 | 0x8 | 0x9 | 0x10 | 0x11 | 0x12 | 0x13 | 0x18 | 0x19 | 0x20\n            | 0x21 | 0x22 | 0x23 | 0x28 | 0x29 | 0x30 | 0x31 | 0x32 | 0x33 | 0x38 | 0x39 | 0x62\n            | 0x63 | 0x69 | 0x80 | 0x81 | 0x82 | 0x83 | 0x84 | 0x85 | 0x86 | 0x87 | 0x88 | 0x89\n            | 0xa | 0xb | 0x1a | 0x1b | 0x2a | 0x2b | 0x3a | 0x3b | 0x6b | 0x8a | 0x8b | 0x8c\n            | 0x8d | 0x8e | 0x8f | 0xc0 | 0xc1 | 0xc4 | 0xc5 | 0xc6 | 0xc7 | 0xd0 | 0xd1 | 0xd2\n            | 0xd3 | 0xd8 | 0xd9 | 0xda | 0xdb | 0xdc | 0xdd | 0xde | 0xdf | 0xf6 | 0xf7 | 0xfe\n            | 0xff => true,\n            _ => false,\n        }\n    };\n\n    let has_fixed_g = if is_0f {\n        final_opcode == 0x71\n            || final_opcode == 0x72\n            || final_opcode == 0x73\n            || final_opcode == 0xAE\n            || final_opcode == 0xBA\n            || final_opcode == 0xC7\n    }\n    else {\n        final_opcode >= 0x80 && final_opcode < 0x84\n            || final_opcode >= 0xC0 && final_opcode < 0xC2\n            || final_opcode >= 0xD0 && final_opcode < 0xD4\n            || final_opcode >= 0xD8 && final_opcode < 0xE0\n            || final_opcode >= 0xF6 && final_opcode < 0xF8\n            || final_opcode == 0xFE\n            || final_opcode == 0xFF\n    };\n\n    let mut is_mem = false;\n    let mut fixed_g = 0;\n\n    if has_fixed_g {\n        dbg_assert!(has_modrm_byte);\n        let modrm_byte = (instruction & 0xFF) as u8;\n        fixed_g = modrm_byte >> 3 & 7;\n        is_mem = modrm_byte < 0xC0\n    }\n    if has_modrm_byte {\n        let modrm_byte = (instruction & 0xFF) as u8;\n        is_mem = modrm_byte < 0xC0\n    }\n\n    Instruction {\n        prefixes,\n        opcode: final_opcode,\n        is_mem,\n        fixed_g,\n        is_0f,\n    }\n}\n\npub fn gen_opstats(builder: &mut WasmBuilder, opcode: u32) {\n    if !cfg!(feature = \"profiler\") {\n        return;\n    }\n\n    let instruction = decode(opcode);\n\n    for prefix in instruction.prefixes {\n        let index = (prefix as u32) << 4;\n        builder.increment_fixed_i64(\n            unsafe { &mut opstats_buffer[index as usize] as *mut _ } as u32,\n            1,\n        );\n    }\n\n    let index = (instruction.is_0f as u32) << 12\n        | (instruction.opcode as u32) << 4\n        | (instruction.is_mem as u32) << 3\n        | instruction.fixed_g as u32;\n\n    builder.increment_fixed_i64(\n        unsafe { &mut opstats_buffer[index as usize] as *mut _ } as u32,\n        1,\n    );\n}\n\npub fn record_opstat_compiled(opcode: u32) {\n    if !cfg!(feature = \"profiler\") {\n        return;\n    }\n\n    let instruction = decode(opcode);\n\n    for prefix in instruction.prefixes {\n        let index = (prefix as u32) << 4;\n        unsafe { opstats_compiled_buffer[index as usize] += 1 }\n    }\n\n    let index = (instruction.is_0f as u32) << 12\n        | (instruction.opcode as u32) << 4\n        | (instruction.is_mem as u32) << 3\n        | instruction.fixed_g as u32;\n\n    unsafe { opstats_compiled_buffer[index as usize] += 1 }\n}\n\npub fn record_opstat_jit_exit(opcode: u32) {\n    if !cfg!(feature = \"profiler\") {\n        return;\n    }\n\n    let instruction = decode(opcode);\n\n    for prefix in instruction.prefixes {\n        let index = (prefix as u32) << 4;\n        unsafe { opstats_jit_exit_buffer[index as usize] += 1 }\n    }\n\n    let index = (instruction.is_0f as u32) << 12\n        | (instruction.opcode as u32) << 4\n        | (instruction.is_mem as u32) << 3\n        | instruction.fixed_g as u32;\n\n    unsafe { opstats_jit_exit_buffer[index as usize] += 1 }\n}\n\npub fn gen_opstat_unguarded_register(builder: &mut WasmBuilder, opcode: u32) {\n    if !cfg!(feature = \"profiler\") {\n        return;\n    }\n\n    let instruction = decode(opcode);\n\n    for prefix in instruction.prefixes {\n        let index = (prefix as u32) << 4;\n        builder.increment_fixed_i64(\n            unsafe { &mut opstats_unguarded_register_buffer[index as usize] as *mut _ } as u32,\n            1,\n        );\n    }\n\n    let index = (instruction.is_0f as u32) << 12\n        | (instruction.opcode as u32) << 4\n        | (instruction.is_mem as u32) << 3\n        | instruction.fixed_g as u32;\n\n    builder.increment_fixed_i64(\n        unsafe { &mut opstats_unguarded_register_buffer[index as usize] as *mut _ } as u32,\n        1,\n    );\n}\n\npub fn record_opstat_size_wasm(opcode: u32, size: u64) {\n    if !cfg!(feature = \"profiler\") {\n        return;\n    }\n\n    let instruction = decode(opcode);\n\n    for prefix in instruction.prefixes {\n        let index = (prefix as u32) << 4;\n        unsafe { opstats_wasm_size[index as usize] += size }\n    }\n\n    let index = (instruction.is_0f as u32) << 12\n        | (instruction.opcode as u32) << 4\n        | (instruction.is_mem as u32) << 3\n        | instruction.fixed_g as u32;\n\n    unsafe { opstats_wasm_size[index as usize] += size }\n}\n"
  },
  {
    "path": "src/rust/page.rs",
    "content": "use std::ops::RangeInclusive;\n\n#[derive(Copy, Clone, Eq, Hash, PartialEq)]\npub struct Page(u32);\nimpl Page {\n    pub fn page_of(address: u32) -> Page { Page(address >> 12) }\n    pub fn to_address(self) -> u32 { self.0 << 12 }\n\n    pub fn to_u32(self) -> u32 { self.0 }\n    pub fn of_u32(page: u32) -> Page { Page(page) }\n\n    pub fn address_range(self) -> RangeInclusive<u32> {\n        self.to_address()..=self.to_address() + 4095\n    }\n}\n"
  },
  {
    "path": "src/rust/paging.rs",
    "content": "pub type OrPageFault<T> = Result<T, ()>;\n\nmacro_rules! return_on_pagefault {\n    ($expr:expr) => {\n        match $expr {\n            Ok(v) => v,\n            Err(()) => return,\n        }\n    };\n    ($expr:expr, $ret:expr) => {\n        match $expr {\n            Ok(v) => v,\n            Err(()) => return $ret,\n        }\n    };\n}\n\nmacro_rules! break_on_pagefault {\n    ($expr:expr) => {\n        match $expr {\n            Ok(v) => v,\n            Err(()) => break,\n        }\n    };\n}\n"
  },
  {
    "path": "src/rust/prefix.rs",
    "content": "pub const PREFIX_REPZ: u8 = 0b01000;\npub const PREFIX_REPNZ: u8 = 0b10000;\npub const PREFIX_MASK_REP: u8 = PREFIX_REPZ | PREFIX_REPNZ;\n\npub const PREFIX_MASK_OPSIZE: u8 = 0b100000;\npub const PREFIX_MASK_ADDRSIZE: u8 = 0b1000000;\n\npub const PREFIX_66: u8 = PREFIX_MASK_OPSIZE;\npub const PREFIX_67: u8 = PREFIX_MASK_ADDRSIZE;\npub const PREFIX_F2: u8 = PREFIX_REPNZ;\npub const PREFIX_F3: u8 = PREFIX_REPZ;\n\npub const SEG_PREFIX_ZERO: u8 = 7;\n\npub const PREFIX_MASK_SEGMENT: u8 = 0b111;\n"
  },
  {
    "path": "src/rust/profiler.rs",
    "content": "#[allow(non_camel_case_types)]\npub enum stat {\n    COMPILE,\n    COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS,\n    COMPILE_WRONG_ADDRESS_SPACE,\n    COMPILE_CUT_OFF_AT_END_OF_PAGE,\n    COMPILE_WITH_LOOP_SAFETY,\n    COMPILE_PAGE,\n    COMPILE_BASIC_BLOCK,\n    COMPILE_DUPLICATED_BASIC_BLOCK,\n    COMPILE_WASM_BLOCK,\n    COMPILE_WASM_LOOP,\n    COMPILE_DISPATCHER,\n    COMPILE_ENTRY_POINT,\n    COMPILE_WASM_TOTAL_BYTES,\n\n    RUN_INTERPRETED,\n    RUN_INTERPRETED_NEW_PAGE,\n    RUN_INTERPRETED_PAGE_HAS_CODE,\n    RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK,\n    RUN_INTERPRETED_NEAR_END_OF_PAGE,\n    RUN_INTERPRETED_DIFFERENT_STATE,\n    RUN_INTERPRETED_DIFFERENT_STATE_CPL3,\n    RUN_INTERPRETED_DIFFERENT_STATE_FLAT,\n    RUN_INTERPRETED_DIFFERENT_STATE_IS32,\n    RUN_INTERPRETED_DIFFERENT_STATE_SS32,\n    RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED,\n    RUN_INTERPRETED_STEPS,\n\n    RUN_FROM_CACHE,\n    RUN_FROM_CACHE_STEPS,\n\n    DIRECT_EXIT,\n    INDIRECT_JUMP,\n    INDIRECT_JUMP_NO_ENTRY,\n    NORMAL_PAGE_CHANGE,\n    NORMAL_FALLTHRU,\n    NORMAL_FALLTHRU_WITH_TARGET_BLOCK,\n    NORMAL_BRANCH,\n    NORMAL_BRANCH_WITH_TARGET_BLOCK,\n    CONDITIONAL_JUMP,\n    CONDITIONAL_JUMP_PAGE_CHANGE,\n    CONDITIONAL_JUMP_EXIT,\n    CONDITIONAL_JUMP_FALLTHRU,\n    CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK,\n    CONDITIONAL_JUMP_BRANCH,\n    CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK,\n    DISPATCHER_SMALL,\n    DISPATCHER_LARGE,\n    LOOP,\n\n    LOOP_SAFETY,\n\n    CONDITION_OPTIMISED,\n    CONDITION_UNOPTIMISED,\n    CONDITION_UNOPTIMISED_PF,\n    CONDITION_UNOPTIMISED_UNHANDLED_L,\n    CONDITION_UNOPTIMISED_UNHANDLED_LE,\n\n    FAILED_PAGE_CHANGE,\n\n    SAFE_READ_FAST,\n    SAFE_READ_SLOW_PAGE_CROSSED,\n    SAFE_READ_SLOW_NOT_VALID,\n    SAFE_READ_SLOW_NOT_USER,\n    SAFE_READ_SLOW_IN_MAPPED_RANGE,\n\n    SAFE_WRITE_FAST,\n    SAFE_WRITE_SLOW_PAGE_CROSSED,\n    SAFE_WRITE_SLOW_NOT_VALID,\n    SAFE_WRITE_SLOW_NOT_USER,\n    SAFE_WRITE_SLOW_IN_MAPPED_RANGE,\n    SAFE_WRITE_SLOW_READ_ONLY,\n    SAFE_WRITE_SLOW_HAS_CODE,\n\n    SAFE_READ_WRITE_FAST,\n    SAFE_READ_WRITE_SLOW_PAGE_CROSSED,\n    SAFE_READ_WRITE_SLOW_NOT_VALID,\n    SAFE_READ_WRITE_SLOW_NOT_USER,\n    SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE,\n    SAFE_READ_WRITE_SLOW_READ_ONLY,\n    SAFE_READ_WRITE_SLOW_HAS_CODE,\n\n    PAGE_FAULT,\n    TLB_MISS,\n\n    MAIN_LOOP,\n    MAIN_LOOP_IDLE,\n    DO_MANY_CYCLES,\n    CYCLE_INTERNAL,\n\n    INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES,\n    INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED,\n    INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE,\n    INVALIDATE_MODULE_DIRTY_PAGE,\n\n    INVALIDATE_PAGE_HAD_CODE,\n    INVALIDATE_PAGE_HAD_ENTRY_POINTS,\n    DIRTY_PAGE_DID_NOT_HAVE_CODE,\n\n    RUN_FROM_CACHE_EXIT_SAME_PAGE,\n    RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE,\n    RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE,\n\n    CLEAR_TLB,\n    FULL_CLEAR_TLB,\n    TLB_FULL,\n    TLB_GLOBAL_FULL,\n\n    MODRM_SIMPLE_REG,\n    MODRM_SIMPLE_REG_WITH_OFFSET,\n    MODRM_SIMPLE_CONST_OFFSET,\n    MODRM_COMPLEX,\n\n    SEG_OFFSET_OPTIMISED,\n    SEG_OFFSET_NOT_OPTIMISED,\n    SEG_OFFSET_NOT_OPTIMISED_ES,\n    SEG_OFFSET_NOT_OPTIMISED_FS,\n    SEG_OFFSET_NOT_OPTIMISED_GS,\n    SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT,\n}\n\n#[allow(non_upper_case_globals)]\npub static mut stat_array: [u64; 500] = [0; 500];\n\npub fn stat_increment(stat: stat) { stat_increment_by(stat, 1); }\n\npub fn stat_increment_by(stat: stat, by: u64) {\n    if cfg!(feature = \"profiler\") {\n        unsafe { stat_array[stat as usize] += by }\n    }\n}\n\n#[no_mangle]\npub fn profiler_init() {\n    unsafe {\n        #[allow(static_mut_refs)]\n        for x in stat_array.iter_mut() {\n            *x = 0\n        }\n    }\n}\n\n#[no_mangle]\npub fn profiler_stat_get(stat: stat) -> f64 {\n    if cfg!(feature = \"profiler\") {\n        unsafe { stat_array[stat as usize] as f64 }\n    }\n    else {\n        0.0\n    }\n}\n\n#[no_mangle]\npub fn profiler_is_enabled() -> bool { cfg!(feature = \"profiler\") }\n"
  },
  {
    "path": "src/rust/regs.rs",
    "content": "pub const ES: u32 = 0;\npub const CS: u32 = 1;\npub const SS: u32 = 2;\npub const DS: u32 = 3;\npub const FS: u32 = 4;\npub const GS: u32 = 5;\n\npub const EAX: u32 = 0;\npub const ECX: u32 = 1;\npub const EDX: u32 = 2;\npub const EBX: u32 = 3;\npub const ESP: u32 = 4;\npub const EBP: u32 = 5;\npub const ESI: u32 = 6;\npub const EDI: u32 = 7;\n\npub const AX: u32 = 0;\npub const CX: u32 = 1;\npub const DX: u32 = 2;\npub const BX: u32 = 3;\npub const SP: u32 = 4;\npub const BP: u32 = 5;\npub const SI: u32 = 6;\npub const DI: u32 = 7;\n\npub const AL: u32 = 0;\npub const CL: u32 = 1;\npub const DL: u32 = 2;\npub const BL: u32 = 3;\npub const AH: u32 = 4;\npub const CH: u32 = 5;\npub const DH: u32 = 6;\npub const BH: u32 = 7;\n\npub const CR0_EM: u32 = 1 << 2;\npub const CR0_TS: u32 = 1 << 3;\n\npub const CR4_TSD: u32 = 1 << 2;\n"
  },
  {
    "path": "src/rust/softfloat.rs",
    "content": "extern \"C\" {\n    fn extF80M_add(x: *const F80, y: *const F80, ptr: *mut F80);\n    fn extF80M_sub(x: *const F80, y: *const F80, ptr: *mut F80);\n    fn extF80M_mul(x: *const F80, y: *const F80, ptr: *mut F80);\n    fn extF80M_div(x: *const F80, y: *const F80, ptr: *mut F80);\n    //fn extF80M_rem(x: *const F80, y: *const F80, ptr: *mut F80);\n    fn extF80M_sqrt(x: *const F80, ptr: *mut F80);\n\n    fn extF80M_roundToInt(x: *const F80, rounding_mode: u8, raise_inexact: bool, dst: *mut F80);\n\n    fn extF80M_eq(x: *const F80, y: *const F80) -> bool;\n    //fn extF80M_eq_signaling(x: *const F80, y: *const F80) -> bool;\n\n    //fn extF80M_le(x: *const F80, y: *const F80) -> bool;\n    //fn extF80M_le_quiet(x: *const F80, y: *const F80) -> bool;\n    fn extF80M_lt(x: *const F80, y: *const F80) -> bool;\n    fn extF80M_lt_quiet(x: *const F80, y: *const F80) -> bool;\n\n    fn extF80M_to_i32(src: *const F80, rounding_mode: u8, raise_inexact: bool) -> i32;\n    fn extF80M_to_i64(src: *const F80, rounding_mode: u8, raise_inexact: bool) -> i64;\n    fn i32_to_extF80M(src: i32, dst: *mut F80);\n    fn i64_to_extF80M(src: i64, dst: *mut F80);\n\n    fn f32_to_extF80M(src: i32, dst: *mut F80);\n    fn f64_to_extF80M(src: u64, dst: *mut F80);\n    fn extF80M_to_f32(src: *const F80) -> i32;\n    fn extF80M_to_f64(src: *const F80) -> u64;\n\n    static mut softfloat_roundingMode: u8;\n    static mut extF80_roundingPrecision: u8;\n    static mut softfloat_exceptionFlags: u8;\n}\n\npub enum RoundingMode {\n    NearEven,\n    Trunc,\n    Floor,\n    Ceil,\n}\npub enum Precision {\n    P80,\n    P64,\n    P32,\n}\n\n#[repr(C)]\n#[derive(Copy, Clone)]\npub struct F80 {\n    pub mantissa: u64,\n    pub sign_exponent: u16,\n}\nimpl F80 {\n    pub const ZERO: F80 = F80 {\n        mantissa: 0,\n        sign_exponent: 0,\n    };\n    pub const ONE: F80 = F80 {\n        mantissa: 0x8000000000000000,\n        sign_exponent: 0x3FFF,\n    };\n    pub const LN_10: F80 = F80 {\n        mantissa: 0x935D8DDDAAA8B000,\n        sign_exponent: 0x4000,\n    };\n    pub const LN_2: F80 = F80 {\n        mantissa: 0xB17217F7D1CF7800,\n        sign_exponent: 0x3FFE,\n    };\n    pub const PI: F80 = F80 {\n        mantissa: 0xC90FDAA22168C000,\n        sign_exponent: 0x4000,\n    };\n    pub const LOG2_E: F80 = F80 {\n        mantissa: 0xB8AA3B295C17F000,\n        sign_exponent: 0x3FFF,\n    };\n    pub const INDEFINITE_NAN: F80 = F80 {\n        mantissa: 0xC000000000000000,\n        sign_exponent: 0x7FFF,\n    };\n    pub const POS_INFINITY: F80 = F80 {\n        mantissa: 0x8000000000000000,\n        sign_exponent: 0x7FFF,\n    };\n    pub const NEG_INFINITY: F80 = F80 {\n        mantissa: 0x8000000000000000,\n        sign_exponent: 0xFFFF,\n    };\n\n    pub fn sign(&self) -> bool { (self.sign_exponent >> 15) == 1 }\n    pub fn exponent(&self) -> i16 { (self.sign_exponent as i16 & 0x7FFF) - 0x3FFF }\n\n    pub fn of_i32(src: i32) -> F80 {\n        let mut x = F80::ZERO;\n        unsafe { i32_to_extF80M(src, &mut x) };\n        x\n    }\n    pub fn of_i64(src: i64) -> F80 {\n        let mut x = F80::ZERO;\n        unsafe { i64_to_extF80M(src, &mut x) };\n        x\n    }\n\n    pub fn of_f32(src: i32) -> F80 {\n        let mut x = F80::ZERO;\n        unsafe { f32_to_extF80M(src, &mut x) };\n        x\n    }\n\n    pub fn of_f64(src: u64) -> F80 {\n        let mut x = F80::ZERO;\n        unsafe { f64_to_extF80M(src, &mut x) };\n        x\n    }\n    fn of_f64x(src: f64) -> F80 { F80::of_f64(f64::to_bits(src)) }\n\n    pub fn to_f32(&self) -> i32 { unsafe { extF80M_to_f32(self) } }\n    pub fn to_f64(&self) -> u64 { unsafe { extF80M_to_f64(self) } }\n    fn to_f64x(&self) -> f64 { f64::from_bits(self.to_f64()) }\n\n    pub fn to_i32(&self) -> i32 { unsafe { extF80M_to_i32(self, softfloat_roundingMode, false) } }\n    pub fn to_i64(&self) -> i64 { unsafe { extF80M_to_i64(self, softfloat_roundingMode, false) } }\n\n    pub fn truncate_to_i32(&self) -> i32 { unsafe { extF80M_to_i32(self, 1, false) } }\n    pub fn truncate_to_i64(&self) -> i64 { unsafe { extF80M_to_i64(self, 1, false) } }\n\n    pub fn cos(self) -> F80 { F80::of_f64x(self.to_f64x().cos()) }\n    pub fn sin(self) -> F80 { F80::of_f64x(self.to_f64x().sin()) }\n    pub fn tan(self) -> F80 { F80::of_f64x(self.to_f64x().tan()) }\n    pub fn atan(self) -> F80 { F80::of_f64x(self.to_f64x().atan()) }\n    pub fn atan2(self, other: F80) -> F80 { F80::of_f64x(self.to_f64x().atan2(other.to_f64x())) }\n\n    pub fn log2(self) -> F80 { F80::of_f64x(self.to_f64x().log2()) }\n    pub fn ln(self) -> F80 { F80::of_f64x(self.to_f64x().ln()) }\n\n    pub fn abs(self) -> F80 {\n        F80 {\n            mantissa: self.mantissa,\n            sign_exponent: self.sign_exponent & !0x8000,\n        }\n    }\n    pub fn two_pow(self) -> F80 { F80::of_f64x(2.0f64.powf(self.to_f64x())) }\n    pub fn round(self) -> F80 {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_roundToInt(&self, softfloat_roundingMode, false, &mut result) };\n        result\n    }\n    pub fn trunc(self) -> F80 {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_roundToInt(&self, 1, false, &mut result) };\n        result\n    }\n\n    pub fn sqrt(self) -> F80 {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_sqrt(&self, &mut result) };\n        result\n    }\n\n    pub fn is_finite(self) -> bool {\n        // TODO: Can probably be done more efficiently\n        self != F80::POS_INFINITY && self != F80::NEG_INFINITY\n    }\n    pub fn is_nan(self) -> bool {\n        // TODO: Can probably be done more efficiently\n        self != self\n    }\n\n    pub fn set_rounding_mode(mode: RoundingMode) {\n        unsafe {\n            softfloat_roundingMode = match mode {\n                RoundingMode::NearEven => 0,\n                RoundingMode::Trunc => 1,\n                RoundingMode::Floor => 2,\n                RoundingMode::Ceil => 3,\n            }\n        };\n    }\n    pub fn set_precision(precision: Precision) {\n        unsafe {\n            extF80_roundingPrecision = match precision {\n                Precision::P80 => 80,\n                Precision::P64 => 64,\n                Precision::P32 => 32,\n            }\n        };\n    }\n\n    pub fn get_exception_flags() -> u8 {\n        let f = unsafe { softfloat_exceptionFlags };\n        // translate softfloat's flags to x87 status flags\n        f >> 4 & 1 | f >> 1 & 4 | f << 3 & 16\n    }\n    pub fn clear_exception_flags() { unsafe { softfloat_exceptionFlags = 0 } }\n\n    pub fn partial_cmp_quiet(&self, other: &Self) -> Option<std::cmp::Ordering> {\n        // TODO: Can probably be done more efficiently\n        if unsafe { extF80M_lt_quiet(self, other) } {\n            Some(std::cmp::Ordering::Less)\n        }\n        else if unsafe { extF80M_lt_quiet(other, self) } {\n            Some(std::cmp::Ordering::Greater)\n        }\n        else if self == other {\n            Some(std::cmp::Ordering::Equal)\n        }\n        else {\n            None\n        }\n    }\n}\n\nimpl std::ops::Add for F80 {\n    type Output = F80;\n    fn add(self, other: Self) -> Self {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_add(&self, &other, &mut result) };\n        result\n    }\n}\nimpl std::ops::Sub for F80 {\n    type Output = F80;\n    fn sub(self, other: Self) -> Self {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_sub(&self, &other, &mut result) };\n        result\n    }\n}\nimpl std::ops::Neg for F80 {\n    type Output = F80;\n    fn neg(self) -> Self {\n        let mut result = self;\n        result.sign_exponent ^= 1 << 15;\n        result\n    }\n}\nimpl std::ops::Mul for F80 {\n    type Output = F80;\n    fn mul(self, other: Self) -> Self {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_mul(&self, &other, &mut result) };\n        result\n    }\n}\nimpl std::ops::Div for F80 {\n    type Output = F80;\n    fn div(self, other: Self) -> Self {\n        let mut result = F80::ZERO;\n        unsafe { extF80M_div(&self, &other, &mut result) };\n        result\n    }\n}\nimpl std::ops::Rem for F80 {\n    type Output = F80;\n    fn rem(self, other: Self) -> Self {\n        let quot = (self / other).trunc();\n        self - quot * other\n        // Uses round-to-nearest instead of truncation\n        //let mut result = F80::ZERO;\n        //unsafe {\n        //    extF80M_rem(&self, &other, &mut result)\n        //};\n        //result\n    }\n}\n\nimpl PartialEq for F80 {\n    fn eq(&self, other: &Self) -> bool { unsafe { extF80M_eq(self, other) } }\n}\nimpl PartialOrd for F80 {\n    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {\n        // TODO: Can probably be done more efficiently\n        if unsafe { extF80M_lt(self, other) } {\n            Some(std::cmp::Ordering::Less)\n        }\n        else if unsafe { extF80M_lt(other, self) } {\n            Some(std::cmp::Ordering::Greater)\n        }\n        else if self == other {\n            Some(std::cmp::Ordering::Equal)\n        }\n        else {\n            None\n        }\n    }\n}\n"
  },
  {
    "path": "src/rust/state_flags.rs",
    "content": "#[derive(Copy, Clone, PartialEq, Eq)]\n#[repr(transparent)]\npub struct CachedStateFlags(u8);\n\nimpl CachedStateFlags {\n    const MASK_IS_32: u8 = 1 << 0;\n    const MASK_SS32: u8 = 1 << 1;\n    const MASK_CPL3: u8 = 1 << 2;\n    const MASK_FLAT_SEGS: u8 = 1 << 3;\n\n    pub const EMPTY: CachedStateFlags = CachedStateFlags(0);\n\n    pub fn of_u32(f: u32) -> CachedStateFlags {\n        dbg_assert!(\n            f as u8\n                & !(Self::MASK_IS_32 | Self::MASK_SS32 | Self::MASK_CPL3 | Self::MASK_FLAT_SEGS)\n                == 0\n        );\n        CachedStateFlags(f as u8)\n    }\n    pub fn to_u32(&self) -> u32 { self.0 as u32 }\n\n    pub fn cpl3(&self) -> bool { self.0 & CachedStateFlags::MASK_CPL3 != 0 }\n    pub fn has_flat_segmentation(&self) -> bool { self.0 & CachedStateFlags::MASK_FLAT_SEGS != 0 }\n    pub fn is_32(&self) -> bool { self.0 & CachedStateFlags::MASK_IS_32 != 0 }\n    pub fn ssize_32(&self) -> bool { self.0 & CachedStateFlags::MASK_SS32 != 0 }\n}\n"
  },
  {
    "path": "src/rust/wasmgen/mod.rs",
    "content": "pub mod wasm_builder;\nmod wasm_opcodes;\n"
  },
  {
    "path": "src/rust/wasmgen/wasm_builder.rs",
    "content": "use std::collections::HashMap;\nuse std::mem::transmute;\n\nuse crate::leb::{\n    write_fixed_leb16_at_idx, write_fixed_leb32_at_idx, write_leb_i32, write_leb_i64, write_leb_u32,\n};\nuse crate::wasmgen::wasm_opcodes as op;\n\npub trait SafeToU8 {\n    fn safe_to_u8(self) -> u8;\n}\nimpl SafeToU8 for usize {\n    fn safe_to_u8(self) -> u8 {\n        dbg_assert!(self <= ::std::u8::MAX as usize);\n        self as u8\n    }\n}\n\npub trait SafeToU16 {\n    fn safe_to_u16(self) -> u16;\n}\nimpl SafeToU16 for usize {\n    fn safe_to_u16(self) -> u16 {\n        dbg_assert!(self <= ::std::u16::MAX as usize);\n        self as u16\n    }\n}\n\n#[derive(PartialEq)]\n#[allow(non_camel_case_types)]\nenum FunctionType {\n    FN0,\n    FN1,\n    FN2,\n    FN3,\n\n    FN0_RET,\n    FN0_RET_I64,\n    FN1_RET,\n    FN2_RET,\n\n    FN1_RET_I64,\n    FN1_F32_RET,\n    FN1_F64_RET,\n\n    FN2_I32_I64,\n    FN2_I64_I32,\n    FN2_I64_I32_RET,\n    FN2_I64_I32_RET_I64,\n    FN2_F32_I32,\n\n    FN3_RET,\n\n    FN3_I64_I32_I32,\n    FN3_I32_I64_I32,\n    FN3_I32_I64_I32_RET,\n    FN4_I32_I64_I64_I32_RET,\n    // When adding at the end, update LAST below\n}\n\nimpl FunctionType {\n    pub fn of_u8(x: u8) -> FunctionType {\n        dbg_assert!(x <= FunctionType::LAST as u8);\n        unsafe { transmute(x) }\n    }\n    pub fn to_u8(self: FunctionType) -> u8 { self as u8 }\n    pub const LAST: FunctionType = FunctionType::FN4_I32_I64_I64_I32_RET;\n}\n\npub const WASM_MODULE_ARGUMENT_COUNT: u8 = 1;\n\npub struct WasmBuilder {\n    output: Vec<u8>,\n    instruction_body: Vec<u8>,\n\n    idx_import_table_size: usize, // for rewriting once finished\n    idx_import_count: usize,      // for rewriting once finished\n    idx_import_entries: usize,    // for searching the imports\n\n    import_table_size: usize, // the current import table size (to avoid reading 2 byte leb)\n    import_count: u16,        // same as above\n\n    initial_static_size: usize, // size of module after initialization, rest is drained on reset\n\n    // label for referencing block/if/loop constructs directly via branch instructions\n    next_label: Label,\n    label_stack: Vec<Label>,\n    label_to_depth: HashMap<Label, usize>,\n\n    free_locals_i32: Vec<WasmLocal>,\n    free_locals_i64: Vec<WasmLocalI64>,\n    local_count: u8,\n    pub arg_local_initial_state: WasmLocal,\n}\n\n#[derive(Eq, PartialEq)]\npub struct WasmLocal(u8);\nimpl WasmLocal {\n    pub fn idx(&self) -> u8 { self.0 }\n    /// Unsafe: Can result in multiple free's. Should only be used for locals that are used during\n    /// the whole module (for example, registers)\n    pub fn unsafe_clone(&self) -> WasmLocal { WasmLocal(self.0) }\n}\n\npub struct WasmLocalI64(u8);\nimpl WasmLocalI64 {\n    pub fn idx(&self) -> u8 { self.0 }\n}\n\n#[derive(Copy, Clone, Eq, Hash, PartialEq)]\npub struct Label(u32);\nimpl Label {\n    const ZERO: Label = Label(0);\n    fn next(&self) -> Label { Label(self.0.wrapping_add(1)) }\n}\n\nimpl WasmBuilder {\n    pub fn new() -> Self {\n        let mut b = WasmBuilder {\n            output: Vec::with_capacity(256),\n            instruction_body: Vec::with_capacity(256),\n\n            idx_import_table_size: 0,\n            idx_import_count: 0,\n            idx_import_entries: 0,\n\n            import_table_size: 2,\n            import_count: 0,\n\n            initial_static_size: 0,\n\n            label_to_depth: HashMap::new(),\n            label_stack: Vec::new(),\n            next_label: Label::ZERO,\n\n            free_locals_i32: Vec::with_capacity(8),\n            free_locals_i64: Vec::with_capacity(8),\n            local_count: 0,\n            arg_local_initial_state: WasmLocal(0),\n        };\n        b.init();\n        b\n    }\n\n    fn init(&mut self) {\n        self.output.extend(\"\\0asm\".as_bytes());\n\n        // wasm version in leb128, 4 bytes\n        self.output.push(op::WASM_VERSION);\n        self.output.push(0);\n        self.output.push(0);\n        self.output.push(0);\n\n        self.write_type_section();\n        self.write_import_section_preamble();\n\n        // store state of current pointers etc. so we can reset them later\n        self.initial_static_size = self.output.len();\n    }\n\n    pub fn reset(&mut self) {\n        self.output.drain(self.initial_static_size..);\n        self.set_import_table_size(2);\n        self.set_import_count(0);\n        self.instruction_body.clear();\n        self.free_locals_i32.clear();\n        self.free_locals_i64.clear();\n        self.local_count = 0;\n\n        dbg_assert!(self.label_to_depth.is_empty());\n        dbg_assert!(self.label_stack.is_empty());\n        self.next_label = Label::ZERO;\n    }\n\n    pub fn finish(&mut self) -> usize {\n        dbg_assert!(self.label_to_depth.is_empty());\n        dbg_assert!(self.label_stack.is_empty());\n\n        self.write_memory_import();\n        self.write_function_section();\n        self.write_export_section();\n\n        // write code section preamble\n        self.output.push(op::SC_CODE);\n\n        let idx_code_section_size = self.output.len(); // we will write to this location later\n        self.output.push(0);\n        self.output.push(0); // write temp val for now using 4 bytes\n        self.output.push(0);\n        self.output.push(0);\n\n        self.output.push(1); // number of function bodies: just 1\n\n        // same as above but for body size of the function\n        let idx_fn_body_size = self.output.len();\n        self.output.push(0);\n        self.output.push(0);\n        self.output.push(0);\n        self.output.push(0);\n\n        dbg_assert!(\n            self.local_count as usize == self.free_locals_i32.len() + self.free_locals_i64.len(),\n            \"All locals should have been freed\"\n        );\n\n        let free_locals_i32 = &self.free_locals_i32;\n        let free_locals_i64 = &self.free_locals_i64;\n\n        let locals = (0..self.local_count).map(|i| {\n            let local_index = WASM_MODULE_ARGUMENT_COUNT + i;\n            if free_locals_i64.iter().any(|v| v.idx() == local_index) {\n                op::TYPE_I64\n            }\n            else {\n                dbg_assert!(free_locals_i32.iter().any(|v| v.idx() == local_index));\n                op::TYPE_I32\n            }\n        });\n        let mut groups = vec![];\n        for local_type in locals {\n            if let Some(last) = groups.last_mut() {\n                let (last_type, last_count) = *last;\n                if last_type == local_type {\n                    *last = (local_type, last_count + 1);\n                    continue;\n                }\n            }\n            groups.push((local_type, 1));\n        }\n        dbg_assert!(groups.len() < 128);\n        self.output.push(groups.len().safe_to_u8());\n        for (local_type, count) in groups {\n            dbg_assert!(count < 128);\n            self.output.push(count);\n            self.output.push(local_type);\n        }\n\n        self.output.append(&mut self.instruction_body);\n\n        self.output.push(op::OP_END);\n\n        // write the actual sizes to the pointer locations stored above. We subtract 4 from the actual\n        // value because the ptr itself points to four bytes\n        let fn_body_size = (self.output.len() - idx_fn_body_size - 4) as u32;\n        write_fixed_leb32_at_idx(&mut self.output, idx_fn_body_size, fn_body_size);\n\n        let code_section_size = (self.output.len() - idx_code_section_size - 4) as u32;\n        write_fixed_leb32_at_idx(&mut self.output, idx_code_section_size, code_section_size);\n\n        self.output.len()\n    }\n\n    pub fn write_type_section(&mut self) {\n        self.output.push(op::SC_TYPE);\n\n        let idx_section_size = self.output.len();\n        self.output.push(0);\n        self.output.push(0);\n\n        let nr_of_function_types = FunctionType::to_u8(FunctionType::LAST) + 1;\n        dbg_assert!(nr_of_function_types < 128);\n        self.output.push(nr_of_function_types);\n\n        for i in 0..(nr_of_function_types) {\n            match FunctionType::of_u8(i) {\n                FunctionType::FN0 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(0); // no args\n                    self.output.push(0); // no return val\n                },\n                FunctionType::FN1 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN2 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN3 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(3);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN0_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(0);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN0_RET_I64 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(0);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I64);\n                },\n                FunctionType::FN1_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN2_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN1_RET_I64 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I64);\n                },\n                FunctionType::FN1_F32_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_F32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN1_F64_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_F64);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN2_I32_I64 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(0);\n                },\n                FunctionType::FN2_I64_I32 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN2_I64_I32_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN2_I64_I32_RET_I64 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I64);\n                },\n                FunctionType::FN2_F32_I32 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(2);\n                    self.output.push(op::TYPE_F32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN3_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(3);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN3_I64_I32_I32 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(3);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN3_I32_I64_I32 => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(3);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(0);\n                },\n                FunctionType::FN3_I32_I64_I32_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(3);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n                FunctionType::FN4_I32_I64_I64_I32_RET => {\n                    self.output.push(op::TYPE_FUNC);\n                    self.output.push(4);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I64);\n                    self.output.push(op::TYPE_I32);\n                    self.output.push(1);\n                    self.output.push(op::TYPE_I32);\n                },\n            }\n        }\n\n        let new_len = self.output.len();\n        let size = (new_len - 2) - idx_section_size;\n        write_fixed_leb16_at_idx(&mut self.output, idx_section_size, size.safe_to_u16());\n    }\n\n    /// Goes over the import block to find index of an import entry by function name\n    pub fn get_import_index(&self, fn_name: &str) -> Option<u16> {\n        let mut offset = self.idx_import_entries;\n        for i in 0..self.import_count {\n            offset += 1; // skip length of module name\n            offset += 1; // skip module name itself\n            let len = self.output[offset] as usize;\n            offset += 1;\n            let name = self\n                .output\n                .get(offset..(offset + len))\n                .expect(\"get function name\");\n            if name == fn_name.as_bytes() {\n                return Some(i);\n            }\n            offset += len; // skip the string\n            offset += 1; // skip import kind\n            offset += 1; // skip type index\n        }\n        None\n    }\n\n    pub fn set_import_count(&mut self, count: u16) {\n        dbg_assert!(count < 0x4000);\n        self.import_count = count;\n        let idx_import_count = self.idx_import_count;\n        write_fixed_leb16_at_idx(&mut self.output, idx_import_count, count);\n    }\n\n    pub fn set_import_table_size(&mut self, size: usize) {\n        dbg_assert!(size < 0x4000);\n        self.import_table_size = size;\n        let idx_import_table_size = self.idx_import_table_size;\n        write_fixed_leb16_at_idx(&mut self.output, idx_import_table_size, size.safe_to_u16());\n    }\n\n    pub fn write_import_section_preamble(&mut self) {\n        self.output.push(op::SC_IMPORT);\n\n        self.idx_import_table_size = self.output.len();\n        self.output.push(1 | 0b10000000);\n        self.output.push(2); // 2 in 2 byte leb\n\n        self.idx_import_count = self.output.len();\n        self.output.push(1 | 0b10000000);\n        self.output.push(0); // 0 in 2 byte leb\n\n        // here after starts the actual list of imports\n        self.idx_import_entries = self.output.len();\n    }\n\n    pub fn write_memory_import(&mut self) {\n        self.output.push(1);\n        self.output.push('e' as u8);\n        self.output.push(1);\n        self.output.push('m' as u8);\n\n        self.output.push(op::EXT_MEMORY);\n\n        self.output.push(0); // memory flag, 0 for no maximum memory limit present\n        write_leb_u32(&mut self.output, 64); // initial memory length of 64 pages, takes 1 bytes in leb128\n\n        let new_import_count = self.import_count + 1;\n        self.set_import_count(new_import_count);\n\n        let new_table_size = self.import_table_size + 7;\n        self.set_import_table_size(new_table_size);\n    }\n\n    fn write_import_entry(&mut self, fn_name: &str, type_index: FunctionType) -> u16 {\n        self.output.push(1); // length of module name\n        self.output.push('e' as u8); // module name\n        self.output.push(fn_name.len().safe_to_u8());\n        self.output.extend(fn_name.as_bytes());\n        self.output.push(op::EXT_FUNCTION);\n        self.output.push(type_index.to_u8());\n\n        let new_import_count = self.import_count + 1;\n        self.set_import_count(new_import_count);\n\n        let new_table_size = self.import_table_size + 1 + 1 + 1 + fn_name.len() + 1 + 1;\n        self.set_import_table_size(new_table_size);\n\n        self.import_count - 1\n    }\n\n    pub fn write_function_section(&mut self) {\n        self.output.push(op::SC_FUNCTION);\n        self.output.push(2); // length of this section\n        self.output.push(1); // count of signature indices\n        self.output.push(FunctionType::FN1.to_u8());\n    }\n\n    pub fn write_export_section(&mut self) {\n        self.output.push(op::SC_EXPORT);\n        self.output.push(1 + 1 + 1 + 1 + 2); // size of this section\n        self.output.push(1); // count of table: just one function exported\n\n        self.output.push(1); // length of exported function name\n        self.output.push('f' as u8); // function name\n        self.output.push(op::EXT_FUNCTION);\n\n        // index of the exported function\n        // function space starts with imports. index of last import is import count - 1\n        // the last import however is a memory, so we subtract one from that\n        let next_op_idx = self.output.len();\n        self.output.push(0);\n        self.output.push(0); // add 2 bytes for writing 16 byte val\n        write_fixed_leb16_at_idx(&mut self.output, next_op_idx, self.import_count - 1);\n    }\n\n    fn get_fn_idx(&mut self, fn_name: &str, type_index: FunctionType) -> u16 {\n        match self.get_import_index(fn_name) {\n            Some(idx) => idx,\n            None => {\n                let idx = self.write_import_entry(fn_name, type_index);\n                idx\n            },\n        }\n    }\n\n    pub fn get_output_ptr(&self) -> *const u8 { self.output.as_ptr() }\n    pub fn get_output_len(&self) -> u32 { self.output.len() as u32 }\n\n    fn open_block(&mut self) -> Label {\n        let label = self.next_label;\n        self.next_label = self.next_label.next();\n        self.label_to_depth\n            .insert(label, self.label_stack.len() + 1);\n        self.label_stack.push(label);\n        label\n    }\n    fn close_block(&mut self) {\n        let label = self.label_stack.pop().unwrap();\n        let old_depth = self.label_to_depth.remove(&label).unwrap();\n        dbg_assert!(self.label_to_depth.len() + 1 == old_depth);\n    }\n\n    #[must_use = \"local allocated but not used\"]\n    fn alloc_local(&mut self) -> WasmLocal {\n        match self.free_locals_i32.pop() {\n            Some(local) => local,\n            None => {\n                let new_idx = self.local_count + WASM_MODULE_ARGUMENT_COUNT;\n                self.local_count = self.local_count.checked_add(1).unwrap();\n                WasmLocal(new_idx)\n            },\n        }\n    }\n    pub fn free_local(&mut self, local: WasmLocal) {\n        dbg_assert!(\n            (WASM_MODULE_ARGUMENT_COUNT..self.local_count + WASM_MODULE_ARGUMENT_COUNT)\n                .contains(&local.0)\n        );\n        self.free_locals_i32.push(local)\n    }\n\n    #[must_use = \"local allocated but not used\"]\n    pub fn set_new_local(&mut self) -> WasmLocal {\n        let local = self.alloc_local();\n        self.instruction_body.push(op::OP_SETLOCAL);\n        self.instruction_body.push(local.idx());\n        local\n    }\n    #[must_use = \"local allocated but not used\"]\n    pub fn tee_new_local(&mut self) -> WasmLocal {\n        let local = self.alloc_local();\n        self.instruction_body.push(op::OP_TEELOCAL);\n        self.instruction_body.push(local.idx());\n        local\n    }\n    pub fn set_local(&mut self, local: &WasmLocal) {\n        self.instruction_body.push(op::OP_SETLOCAL);\n        self.instruction_body.push(local.idx());\n    }\n    pub fn tee_local(&mut self, local: &WasmLocal) {\n        self.instruction_body.push(op::OP_TEELOCAL);\n        self.instruction_body.push(local.idx());\n    }\n    pub fn get_local(&mut self, local: &WasmLocal) {\n        self.instruction_body.push(op::OP_GETLOCAL);\n        self.instruction_body.push(local.idx());\n    }\n\n    #[must_use = \"local allocated but not used\"]\n    fn alloc_local_i64(&mut self) -> WasmLocalI64 {\n        match self.free_locals_i64.pop() {\n            Some(local) => local,\n            None => {\n                let new_idx = self.local_count + WASM_MODULE_ARGUMENT_COUNT;\n                self.local_count += 1;\n                WasmLocalI64(new_idx)\n            },\n        }\n    }\n    pub fn free_local_i64(&mut self, local: WasmLocalI64) {\n        dbg_assert!(\n            (WASM_MODULE_ARGUMENT_COUNT..self.local_count + WASM_MODULE_ARGUMENT_COUNT)\n                .contains(&local.0)\n        );\n        self.free_locals_i64.push(local)\n    }\n    #[must_use = \"local allocated but not used\"]\n    pub fn set_new_local_i64(&mut self) -> WasmLocalI64 {\n        let local = self.alloc_local_i64();\n        self.instruction_body.push(op::OP_SETLOCAL);\n        self.instruction_body.push(local.idx());\n        local\n    }\n    #[must_use = \"local allocated but not used\"]\n    pub fn tee_new_local_i64(&mut self) -> WasmLocalI64 {\n        let local = self.alloc_local_i64();\n        self.instruction_body.push(op::OP_TEELOCAL);\n        self.instruction_body.push(local.idx());\n        local\n    }\n    pub fn get_local_i64(&mut self, local: &WasmLocalI64) {\n        self.instruction_body.push(op::OP_GETLOCAL);\n        self.instruction_body.push(local.idx());\n    }\n\n    pub fn const_i32(&mut self, v: i32) {\n        self.instruction_body.push(op::OP_I32CONST);\n        write_leb_i32(&mut self.instruction_body, v);\n    }\n    pub fn const_i64(&mut self, v: i64) {\n        self.instruction_body.push(op::OP_I64CONST);\n        write_leb_i64(&mut self.instruction_body, v);\n    }\n\n    pub fn load_fixed_u8(&mut self, addr: u32) {\n        self.const_i32(addr as i32);\n        self.load_u8(0);\n    }\n    pub fn load_fixed_u16(&mut self, addr: u32) {\n        // doesn't cause a failure in the generated code, but it will be much slower\n        dbg_assert!((addr & 1) == 0);\n\n        self.const_i32(addr as i32);\n        self.instruction_body.push(op::OP_I32LOAD16U);\n        self.instruction_body.push(op::MEM_ALIGN16);\n        self.instruction_body.push(0); // immediate offset\n    }\n    pub fn load_fixed_i32(&mut self, addr: u32) {\n        // doesn't cause a failure in the generated code, but it will be much slower\n        dbg_assert!((addr & 3) == 0);\n\n        self.const_i32(addr as i32);\n        self.load_aligned_i32(0);\n    }\n    pub fn load_fixed_i64(&mut self, addr: u32) {\n        // doesn't cause a failure in the generated code, but it will be much slower\n        dbg_assert!((addr & 7) == 0);\n\n        self.const_i32(addr as i32);\n        self.load_aligned_i64(0);\n    }\n\n    pub fn load_u8(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32LOAD8U);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_unaligned_i64(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I64LOAD);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_unaligned_i32(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32LOAD);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_unaligned_u16(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32LOAD16U);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_aligned_f64(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_F64LOAD);\n        self.instruction_body.push(op::MEM_ALIGN64);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_aligned_i64(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I64LOAD);\n        self.instruction_body.push(op::MEM_ALIGN64);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_aligned_f32(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_F32LOAD);\n        self.instruction_body.push(op::MEM_ALIGN32);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_aligned_i32(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32LOAD);\n        self.instruction_body.push(op::MEM_ALIGN32);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn load_aligned_u16(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32LOAD16U);\n        self.instruction_body.push(op::MEM_ALIGN16);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_u8(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32STORE8);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_aligned_u16(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32STORE16);\n        self.instruction_body.push(op::MEM_ALIGN16);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_aligned_i32(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32STORE);\n        self.instruction_body.push(op::MEM_ALIGN32);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_aligned_i64(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I64STORE);\n        self.instruction_body.push(op::MEM_ALIGN64);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_unaligned_u16(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32STORE16);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_unaligned_i32(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I32STORE);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn store_unaligned_i64(&mut self, byte_offset: u32) {\n        self.instruction_body.push(op::OP_I64STORE);\n        self.instruction_body.push(op::MEM_NO_ALIGN);\n        write_leb_u32(&mut self.instruction_body, byte_offset);\n    }\n\n    pub fn increment_fixed_i64(&mut self, byte_offset: u32, n: i64) {\n        self.const_i32(byte_offset as i32);\n        self.load_fixed_i64(byte_offset);\n        self.const_i64(n);\n        self.add_i64();\n        self.store_aligned_i64(0);\n    }\n\n    pub fn add_i32(&mut self) { self.instruction_body.push(op::OP_I32ADD); }\n    pub fn add_i64(&mut self) { self.instruction_body.push(op::OP_I64ADD); }\n    pub fn sub_i32(&mut self) { self.instruction_body.push(op::OP_I32SUB); }\n    pub fn and_i32(&mut self) { self.instruction_body.push(op::OP_I32AND); }\n    pub fn or_i32(&mut self) { self.instruction_body.push(op::OP_I32OR); }\n    pub fn or_i64(&mut self) { self.instruction_body.push(op::OP_I64OR); }\n    pub fn xor_i32(&mut self) { self.instruction_body.push(op::OP_I32XOR); }\n    pub fn mul_i32(&mut self) { self.instruction_body.push(op::OP_I32MUL); }\n    pub fn mul_i64(&mut self) { self.instruction_body.push(op::OP_I64MUL); }\n    pub fn div_i64(&mut self) { self.instruction_body.push(op::OP_I64DIVU); }\n    pub fn rem_i64(&mut self) { self.instruction_body.push(op::OP_I64REMU); }\n\n    pub fn rotl_i32(&mut self) { self.instruction_body.push(op::OP_I32ROTL); }\n\n    pub fn shl_i32(&mut self) { self.instruction_body.push(op::OP_I32SHL); }\n    pub fn shl_i64(&mut self) { self.instruction_body.push(op::OP_I64SHL); }\n    pub fn shr_u_i32(&mut self) { self.instruction_body.push(op::OP_I32SHRU); }\n    pub fn shr_u_i64(&mut self) { self.instruction_body.push(op::OP_I64SHRU); }\n    pub fn shr_s_i32(&mut self) { self.instruction_body.push(op::OP_I32SHRS); }\n\n    pub fn eq_i32(&mut self) { self.instruction_body.push(op::OP_I32EQ); }\n    pub fn eq_i64(&mut self) { self.instruction_body.push(op::OP_I64EQ); }\n    pub fn ne_i32(&mut self) { self.instruction_body.push(op::OP_I32NE); }\n    pub fn ne_i64(&mut self) { self.instruction_body.push(op::OP_I64NE); }\n\n    pub fn le_i32(&mut self) { self.instruction_body.push(op::OP_I32LES); }\n    pub fn lt_i32(&mut self) { self.instruction_body.push(op::OP_I32LTS); }\n    pub fn ge_i32(&mut self) { self.instruction_body.push(op::OP_I32GES); }\n    pub fn gt_i32(&mut self) { self.instruction_body.push(op::OP_I32GTS); }\n\n    pub fn gtu_i32(&mut self) { self.instruction_body.push(op::OP_I32GTU); }\n    pub fn geu_i32(&mut self) { self.instruction_body.push(op::OP_I32GEU); }\n    pub fn ltu_i32(&mut self) { self.instruction_body.push(op::OP_I32LTU); }\n    pub fn leu_i32(&mut self) { self.instruction_body.push(op::OP_I32LEU); }\n\n    pub fn gtu_i64(&mut self) { self.instruction_body.push(op::OP_I64GTU); }\n\n    pub fn reinterpret_i32_as_f32(&mut self) {\n        self.instruction_body.push(op::OP_F32REINTERPRETI32);\n    }\n    //pub fn reinterpret_f32_as_i32(&mut self) {\n    //    self.instruction_body.push(op::OP_I32REINTERPRETF32);\n    //}\n    pub fn reinterpret_i64_as_f64(&mut self) {\n        self.instruction_body.push(op::OP_F64REINTERPRETI64);\n    }\n    //pub fn reinterpret_f64_as_i64(&mut self) {\n    //    self.instruction_body.push(op::OP_I64REINTERPRETF64);\n    //}\n    //pub fn promote_f32_to_f64(&mut self) { self.instruction_body.push(op::OP_F64PROMOTEF32); }\n    //pub fn demote_f64_to_f32(&mut self) { self.instruction_body.push(op::OP_F32DEMOTEF64); }\n    //pub fn convert_i32_to_f64(&mut self) { self.instruction_body.push(op::OP_F64CONVERTSI32); }\n    //pub fn convert_i64_to_f64(&mut self) { self.instruction_body.push(op::OP_F64CONVERTSI64); }\n    pub fn extend_unsigned_i32_to_i64(&mut self) {\n        self.instruction_body.push(op::OP_I64EXTENDUI32);\n    }\n    pub fn extend_signed_i32_to_i64(&mut self) { self.instruction_body.push(op::OP_I64EXTENDSI32); }\n    pub fn wrap_i64_to_i32(&mut self) { self.instruction_body.push(op::OP_I32WRAPI64); }\n\n    pub fn eqz_i32(&mut self) { self.instruction_body.push(op::OP_I32EQZ); }\n\n    pub fn select(&mut self) { self.instruction_body.push(op::OP_SELECT); }\n\n    pub fn if_i32(&mut self) {\n        self.open_block();\n        self.instruction_body.push(op::OP_IF);\n        self.instruction_body.push(op::TYPE_I32);\n    }\n    #[allow(dead_code)]\n    pub fn if_i64(&mut self) {\n        self.open_block();\n        self.instruction_body.push(op::OP_IF);\n        self.instruction_body.push(op::TYPE_I64);\n    }\n    #[allow(dead_code)]\n    pub fn block_i32(&mut self) {\n        self.open_block();\n        self.instruction_body.push(op::OP_BLOCK);\n        self.instruction_body.push(op::TYPE_I32);\n    }\n\n    pub fn if_void(&mut self) {\n        self.open_block();\n        self.instruction_body.push(op::OP_IF);\n        self.instruction_body.push(op::TYPE_VOID_BLOCK);\n    }\n\n    pub fn else_(&mut self) {\n        dbg_assert!(!self.label_stack.is_empty());\n        self.instruction_body.push(op::OP_ELSE);\n    }\n\n    pub fn loop_void(&mut self) -> Label {\n        self.instruction_body.push(op::OP_LOOP);\n        self.instruction_body.push(op::TYPE_VOID_BLOCK);\n        self.open_block()\n    }\n\n    pub fn block_void(&mut self) -> Label {\n        self.instruction_body.push(op::OP_BLOCK);\n        self.instruction_body.push(op::TYPE_VOID_BLOCK);\n        self.open_block()\n    }\n\n    pub fn block_end(&mut self) {\n        self.close_block();\n        self.instruction_body.push(op::OP_END);\n    }\n\n    pub fn return_(&mut self) { self.instruction_body.push(op::OP_RETURN); }\n\n    #[allow(dead_code)]\n    pub fn drop_(&mut self) { self.instruction_body.push(op::OP_DROP); }\n\n    pub fn brtable(\n        &mut self,\n        default_case: Label,\n        cases: &mut dyn std::iter::ExactSizeIterator<Item = &Label>,\n    ) {\n        self.instruction_body.push(op::OP_BRTABLE);\n        write_leb_u32(&mut self.instruction_body, cases.len() as u32);\n        for case in cases {\n            self.write_label(*case);\n        }\n        self.write_label(default_case);\n    }\n\n    pub fn br(&mut self, label: Label) {\n        self.instruction_body.push(op::OP_BR);\n        self.write_label(label);\n    }\n    pub fn br_if(&mut self, label: Label) {\n        self.instruction_body.push(op::OP_BRIF);\n        self.write_label(label);\n    }\n\n    fn write_label(&mut self, label: Label) {\n        let depth = *self.label_to_depth.get(&label).unwrap();\n        dbg_assert!(depth <= self.label_stack.len());\n        write_leb_u32(\n            &mut self.instruction_body,\n            (self.label_stack.len() - depth) as u32,\n        );\n    }\n\n    fn call_fn(&mut self, name: &str, function: FunctionType) {\n        let i = self.get_fn_idx(name, function);\n        self.instruction_body.push(op::OP_CALL);\n        write_leb_u32(&mut self.instruction_body, i as u32);\n    }\n\n    pub fn call_fn0(&mut self, name: &str) { self.call_fn(name, FunctionType::FN0) }\n    pub fn call_fn0_ret(&mut self, name: &str) { self.call_fn(name, FunctionType::FN0_RET) }\n    pub fn call_fn0_ret_i64(&mut self, name: &str) { self.call_fn(name, FunctionType::FN0_RET_I64) }\n    pub fn call_fn1(&mut self, name: &str) { self.call_fn(name, FunctionType::FN1) }\n    pub fn call_fn1_ret(&mut self, name: &str) { self.call_fn(name, FunctionType::FN1_RET) }\n    pub fn call_fn1_ret_i64(&mut self, name: &str) { self.call_fn(name, FunctionType::FN1_RET_I64) }\n    pub fn call_fn1_f32_ret(&mut self, name: &str) { self.call_fn(name, FunctionType::FN1_F32_RET) }\n    pub fn call_fn1_f64_ret(&mut self, name: &str) { self.call_fn(name, FunctionType::FN1_F64_RET) }\n    pub fn call_fn2(&mut self, name: &str) { self.call_fn(name, FunctionType::FN2) }\n    pub fn call_fn2_i32_i64(&mut self, name: &str) { self.call_fn(name, FunctionType::FN2_I32_I64) }\n    pub fn call_fn2_i64_i32(&mut self, name: &str) { self.call_fn(name, FunctionType::FN2_I64_I32) }\n    pub fn call_fn2_i64_i32_ret(&mut self, name: &str) {\n        self.call_fn(name, FunctionType::FN2_I64_I32_RET)\n    }\n    pub fn call_fn2_i64_i32_ret_i64(&mut self, name: &str) {\n        self.call_fn(name, FunctionType::FN2_I64_I32_RET_I64)\n    }\n    pub fn call_fn2_f32_i32(&mut self, name: &str) { self.call_fn(name, FunctionType::FN2_F32_I32) }\n    pub fn call_fn2_ret(&mut self, name: &str) { self.call_fn(name, FunctionType::FN2_RET) }\n    pub fn call_fn3(&mut self, name: &str) { self.call_fn(name, FunctionType::FN3) }\n    pub fn call_fn3_ret(&mut self, name: &str) { self.call_fn(name, FunctionType::FN3_RET) }\n    pub fn call_fn3_i64_i32_i32(&mut self, name: &str) {\n        self.call_fn(name, FunctionType::FN3_I64_I32_I32)\n    }\n    pub fn call_fn3_i32_i64_i32(&mut self, name: &str) {\n        self.call_fn(name, FunctionType::FN3_I32_I64_I32)\n    }\n    pub fn call_fn3_i32_i64_i32_ret(&mut self, name: &str) {\n        self.call_fn(name, FunctionType::FN3_I32_I64_I32_RET)\n    }\n    pub fn call_fn4_i32_i64_i64_i32_ret(&mut self, name: &str) {\n        self.call_fn(name, FunctionType::FN4_I32_I64_I64_I32_RET)\n    }\n\n    pub fn unreachable(&mut self) { self.instruction_body.push(op::OP_UNREACHABLE) }\n\n    pub fn instruction_body_length(&self) -> u32 { self.instruction_body.len() as u32 }\n}\n\n#[cfg(test)]\nmod tests {\n    use super::{FunctionType, WasmBuilder, WASM_MODULE_ARGUMENT_COUNT};\n    use std::fs::File;\n    use std::io::Write;\n\n    #[test]\n    fn import_table_management() {\n        let mut w = WasmBuilder::new();\n\n        assert_eq!(0, w.get_fn_idx(\"foo\", FunctionType::FN0));\n        assert_eq!(1, w.get_fn_idx(\"bar\", FunctionType::FN1));\n        assert_eq!(0, w.get_fn_idx(\"foo\", FunctionType::FN0));\n        assert_eq!(2, w.get_fn_idx(\"baz\", FunctionType::FN2));\n    }\n\n    #[test]\n    fn builder_test() {\n        let mut m = WasmBuilder::new();\n\n        m.call_fn(\"foo\", FunctionType::FN0);\n        m.call_fn(\"bar\", FunctionType::FN0);\n\n        let local0 = m.alloc_local(); // for ensuring that reset clears previous locals\n        m.free_local(local0);\n\n        m.finish();\n        m.reset();\n\n        m.const_i32(2);\n\n        m.call_fn(\"baz\", FunctionType::FN1_RET);\n        m.call_fn(\"foo\", FunctionType::FN1);\n\n        m.const_i32(10);\n        let local1 = m.alloc_local();\n        m.tee_local(&local1); // local1 = 10\n\n        m.const_i32(20);\n        m.add_i32();\n        let local2 = m.alloc_local();\n        m.tee_local(&local2); // local2 = 30\n\n        m.free_local(local1);\n\n        let local3 = m.alloc_local();\n        assert_eq!(local3.idx(), WASM_MODULE_ARGUMENT_COUNT);\n\n        m.free_local(local2);\n        m.free_local(local3);\n\n        m.const_i32(30);\n        m.ne_i32();\n        m.if_void();\n        m.unreachable();\n        m.block_end();\n\n        m.finish();\n\n        let op_ptr = m.get_output_ptr();\n        let op_len = m.get_output_len();\n        dbg_log!(\"op_ptr: {:?}, op_len: {:?}\", op_ptr, op_len);\n\n        let mut f = File::create(\"build/dummy_output.wasm\").expect(\"creating dummy_output.wasm\");\n        f.write_all(&m.output).expect(\"write dummy_output.wasm\");\n    }\n}\n"
  },
  {
    "path": "src/rust/wasmgen/wasm_opcodes.rs",
    "content": "macro_rules! c {\n    ($x:ident, $y:expr) => {\n        #[allow(dead_code)]\n        pub const $x: u8 = $y;\n    };\n}\n\n// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#high-level-structure\nc!(WASM_VERSION, 0x1);\n\n// Section codes\nc!(SC_TYPE, 1);\nc!(SC_IMPORT, 2);\nc!(SC_FUNCTION, 3);\nc!(SC_TABLE, 4);\nc!(SC_MEMORY, 5);\nc!(SC_GLOBAL, 6);\nc!(SC_EXPORT, 7);\nc!(SC_START, 8);\nc!(SC_ELEMENT, 9);\nc!(SC_CODE, 10);\nc!(SC_DATA, 11);\n\n// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#language-types\nc!(TYPE_I32, 0x7f);\nc!(TYPE_I64, 0x7e);\nc!(TYPE_F32, 0x7d);\nc!(TYPE_F64, 0x7c);\nc!(TYPE_ANYFUNC, 0x70);\nc!(TYPE_FUNC, 0x60);\nc!(TYPE_VOID_BLOCK, 0x40);\n\n// https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#external_kind\nc!(EXT_FUNCTION, 0);\nc!(EXT_TABLE, 1);\nc!(EXT_MEMORY, 2);\nc!(EXT_GLOBAL, 3);\n\n// Taken from wasm2ast's source code and modified with vim magic\nc!(OP_UNREACHABLE, 0x00);\nc!(OP_NOP, 0x01);\nc!(OP_BLOCK, 0x02);\nc!(OP_LOOP, 0x03);\nc!(OP_IF, 0x04);\nc!(OP_ELSE, 0x05);\nc!(OP_TRY, 0x06);\nc!(OP_CATCH, 0x07);\nc!(OP_THROW, 0x08);\nc!(OP_RETHROW, 0x09);\nc!(OP_CATCHALL, 0x0a);\nc!(OP_END, 0x0b);\nc!(OP_BR, 0x0c);\nc!(OP_BRIF, 0x0d);\nc!(OP_BRTABLE, 0x0e);\nc!(OP_RETURN, 0x0f);\nc!(OP_CALL, 0x10);\nc!(OP_CALLINDIRECT, 0x11);\nc!(OP_DROP, 0x1a);\nc!(OP_SELECT, 0x1b);\nc!(OP_GETLOCAL, 0x20);\nc!(OP_SETLOCAL, 0x21);\nc!(OP_TEELOCAL, 0x22);\nc!(OP_GETGLOBAL, 0x23);\nc!(OP_SETGLOBAL, 0x24);\nc!(OP_I32LOAD, 0x28);\nc!(OP_I64LOAD, 0x29);\nc!(OP_F32LOAD, 0x2a);\nc!(OP_F64LOAD, 0x2b);\nc!(OP_I32LOAD8S, 0x2c);\nc!(OP_I32LOAD8U, 0x2d);\nc!(OP_I32LOAD16S, 0x2e);\nc!(OP_I32LOAD16U, 0x2f);\nc!(OP_I64LOAD8S, 0x30);\nc!(OP_I64LOAD8U, 0x31);\nc!(OP_I64LOAD16S, 0x32);\nc!(OP_I64LOAD16U, 0x33);\nc!(OP_I64LOAD32S, 0x34);\nc!(OP_I64LOAD32U, 0x35);\nc!(OP_I32STORE, 0x36);\nc!(OP_I64STORE, 0x37);\nc!(OP_F32STORE, 0x38);\nc!(OP_F64STORE, 0x39);\nc!(OP_I32STORE8, 0x3a);\nc!(OP_I32STORE16, 0x3b);\nc!(OP_I64STORE8, 0x3c);\nc!(OP_I64STORE16, 0x3d);\nc!(OP_I64STORE32, 0x3e);\nc!(OP_CURRENTMEMORY, 0x3f);\nc!(OP_GROWMEMORY, 0x40);\nc!(OP_I32CONST, 0x41);\nc!(OP_I64CONST, 0x42);\nc!(OP_F32CONST, 0x43);\nc!(OP_F64CONST, 0x44);\nc!(OP_I32EQZ, 0x45);\nc!(OP_I32EQ, 0x46);\nc!(OP_I32NE, 0x47);\nc!(OP_I32LTS, 0x48);\nc!(OP_I32LTU, 0x49);\nc!(OP_I32GTS, 0x4a);\nc!(OP_I32GTU, 0x4b);\nc!(OP_I32LES, 0x4c);\nc!(OP_I32LEU, 0x4d);\nc!(OP_I32GES, 0x4e);\nc!(OP_I32GEU, 0x4f);\nc!(OP_I64EQZ, 0x50);\nc!(OP_I64EQ, 0x51);\nc!(OP_I64NE, 0x52);\nc!(OP_I64LTS, 0x53);\nc!(OP_I64LTU, 0x54);\nc!(OP_I64GTS, 0x55);\nc!(OP_I64GTU, 0x56);\nc!(OP_I64LES, 0x57);\nc!(OP_I64LEU, 0x58);\nc!(OP_I64GES, 0x59);\nc!(OP_I64GEU, 0x5a);\nc!(OP_F32EQ, 0x5b);\nc!(OP_F32NE, 0x5c);\nc!(OP_F32LT, 0x5d);\nc!(OP_F32GT, 0x5e);\nc!(OP_F32LE, 0x5f);\nc!(OP_F32GE, 0x60);\nc!(OP_F64EQ, 0x61);\nc!(OP_F64NE, 0x62);\nc!(OP_F64LT, 0x63);\nc!(OP_F64GT, 0x64);\nc!(OP_F64LE, 0x65);\nc!(OP_F64GE, 0x66);\nc!(OP_I32CLZ, 0x67);\nc!(OP_I32CTZ, 0x68);\nc!(OP_I32POPCNT, 0x69);\nc!(OP_I32ADD, 0x6a);\nc!(OP_I32SUB, 0x6b);\nc!(OP_I32MUL, 0x6c);\nc!(OP_I32DIVS, 0x6d);\nc!(OP_I32DIVU, 0x6e);\nc!(OP_I32REMS, 0x6f);\nc!(OP_I32REMU, 0x70);\nc!(OP_I32AND, 0x71);\nc!(OP_I32OR, 0x72);\nc!(OP_I32XOR, 0x73);\nc!(OP_I32SHL, 0x74);\nc!(OP_I32SHRS, 0x75);\nc!(OP_I32SHRU, 0x76);\nc!(OP_I32ROTL, 0x77);\nc!(OP_I32ROTR, 0x78);\nc!(OP_I64CLZ, 0x79);\nc!(OP_I64CTZ, 0x7a);\nc!(OP_I64POPCNT, 0x7b);\nc!(OP_I64ADD, 0x7c);\nc!(OP_I64SUB, 0x7d);\nc!(OP_I64MUL, 0x7e);\nc!(OP_I64DIVS, 0x7f);\nc!(OP_I64DIVU, 0x80);\nc!(OP_I64REMS, 0x81);\nc!(OP_I64REMU, 0x82);\nc!(OP_I64AND, 0x83);\nc!(OP_I64OR, 0x84);\nc!(OP_I64XOR, 0x85);\nc!(OP_I64SHL, 0x86);\nc!(OP_I64SHRS, 0x87);\nc!(OP_I64SHRU, 0x88);\nc!(OP_I64ROTL, 0x89);\nc!(OP_I64ROTR, 0x8a);\nc!(OP_F32ABS, 0x8b);\nc!(OP_F32NEG, 0x8c);\nc!(OP_F32CEIL, 0x8d);\nc!(OP_F32FLOOR, 0x8e);\nc!(OP_F32TRUNC, 0x8f);\nc!(OP_F32NEAREST, 0x90);\nc!(OP_F32SQRT, 0x91);\nc!(OP_F32ADD, 0x92);\nc!(OP_F32SUB, 0x93);\nc!(OP_F32MUL, 0x94);\nc!(OP_F32DIV, 0x95);\nc!(OP_F32MIN, 0x96);\nc!(OP_F32MAX, 0x97);\nc!(OP_F32COPYSIGN, 0x98);\nc!(OP_F64ABS, 0x99);\nc!(OP_F64NEG, 0x9a);\nc!(OP_F64CEIL, 0x9b);\nc!(OP_F64FLOOR, 0x9c);\nc!(OP_F64TRUNC, 0x9d);\nc!(OP_F64NEAREST, 0x9e);\nc!(OP_F64SQRT, 0x9f);\nc!(OP_F64ADD, 0xa0);\nc!(OP_F64SUB, 0xa1);\nc!(OP_F64MUL, 0xa2);\nc!(OP_F64DIV, 0xa3);\nc!(OP_F64MIN, 0xa4);\nc!(OP_F64MAX, 0xa5);\nc!(OP_F64COPYSIGN, 0xa6);\nc!(OP_I32WRAPI64, 0xa7);\nc!(OP_I32TRUNCSF32, 0xa8);\nc!(OP_I32TRUNCUF32, 0xa9);\nc!(OP_I32TRUNCSF64, 0xaa);\nc!(OP_I32TRUNCUF64, 0xab);\nc!(OP_I64EXTENDSI32, 0xac);\nc!(OP_I64EXTENDUI32, 0xad);\nc!(OP_I64TRUNCSF32, 0xae);\nc!(OP_I64TRUNCUF32, 0xaf);\nc!(OP_I64TRUNCSF64, 0xb0);\nc!(OP_I64TRUNCUF64, 0xb1);\nc!(OP_F32CONVERTSI32, 0xb2);\nc!(OP_F32CONVERTUI32, 0xb3);\nc!(OP_F32CONVERTSI64, 0xb4);\nc!(OP_F32CONVERTUI64, 0xb5);\nc!(OP_F32DEMOTEF64, 0xb6);\nc!(OP_F64CONVERTSI32, 0xb7);\nc!(OP_F64CONVERTUI32, 0xb8);\nc!(OP_F64CONVERTSI64, 0xb9);\nc!(OP_F64CONVERTUI64, 0xba);\nc!(OP_F64PROMOTEF32, 0xbb);\nc!(OP_I32REINTERPRETF32, 0xbc);\nc!(OP_I64REINTERPRETF64, 0xbd);\nc!(OP_F32REINTERPRETI32, 0xbe);\nc!(OP_F64REINTERPRETI64, 0xbf);\n\nc!(MEM_NO_ALIGN, 0);\nc!(MEM_ALIGN16, 1);\nc!(MEM_ALIGN32, 2);\nc!(MEM_ALIGN64, 3);\n"
  },
  {
    "path": "src/rust/zstd.rs",
    "content": "use std::alloc;\n\nextern \"C\" {\n    fn ZSTD_createDStream() -> u32;\n    fn ZSTD_freeDStream(ctx: u32) -> i32;\n    fn ZSTD_decompressStream_simpleArgs(\n        ctx: u32,\n        dst: *mut u8,\n        dstCapacity: u32,\n        dstPos: *mut u32,\n        src: *const u8,\n        srcSize: u32,\n        srcPos: *mut u32,\n    ) -> i32;\n\n    fn ZSTD_isError(err: i32) -> bool;\n}\n\nconst MALLOC_ALIGN: usize = 16;\n\n// malloc and free are needed by the zstd library\n#[no_mangle]\npub unsafe fn v86_malloc(size: u32) -> u32 {\n    let layout = alloc::Layout::from_size_align(size as usize + 4, MALLOC_ALIGN).unwrap();\n    let addr = alloc::alloc(layout);\n    *(addr as *mut u32) = size as u32;\n    addr as u32 + 4\n}\n#[no_mangle]\npub unsafe fn v86_free(addr: u32) {\n    let size = *((addr - 4) as *mut u32);\n    let layout = alloc::Layout::from_size_align(size as usize + 4, MALLOC_ALIGN).unwrap();\n    alloc::dealloc((addr - 4) as *mut u8, layout)\n}\n\npub struct ZstdContext {\n    ctx: u32,\n    src: *mut u8,\n    src_size: u32,\n    src_pos: u32,\n}\n\n#[no_mangle]\npub unsafe fn zstd_create_ctx(src_size: u32) -> *mut ZstdContext {\n    let src = alloc::alloc(alloc::Layout::from_size_align(src_size as usize, 1).unwrap());\n    let ctx = ZSTD_createDStream();\n    let result = alloc::alloc(alloc::Layout::new::<ZstdContext>()) as *mut ZstdContext;\n    *result = ZstdContext {\n        ctx,\n        src,\n        src_size,\n        src_pos: 0,\n    };\n    result\n}\n\n#[no_mangle]\npub unsafe fn zstd_get_src_ptr(ctx: *mut ZstdContext) -> *mut u8 { (*ctx).src }\n\n#[no_mangle]\npub unsafe fn zstd_free_ctx(ctx: *mut ZstdContext) {\n    alloc::dealloc(\n        (*ctx).src,\n        alloc::Layout::from_size_align((*ctx).src_size as usize, 1).unwrap(),\n    );\n    ZSTD_freeDStream((*ctx).ctx);\n    std::ptr::drop_in_place(ctx);\n}\n\n#[no_mangle]\npub unsafe fn zstd_read(ctx: *mut ZstdContext, length: u32) -> *mut u8 {\n    let dst = alloc::alloc(alloc::Layout::from_size_align(length as usize, 1).unwrap());\n    let mut dst_pos = 0;\n    let result = ZSTD_decompressStream_simpleArgs(\n        (*ctx).ctx,\n        dst,\n        length,\n        &mut dst_pos,\n        (*ctx).src,\n        (*ctx).src_size,\n        &mut (*ctx).src_pos,\n    );\n    if ZSTD_isError(result) {\n        dbg_log!(\n            \"ZSTD_decompressStream_simpleArgs returned error: {}\",\n            result\n        );\n        dbg_assert!(false);\n        zstd_read_free(dst, length);\n        return std::ptr::null_mut::<u8>();\n    }\n    if dst_pos != length {\n        dbg_assert!(false, \"ZSTD: Partial read\");\n        zstd_read_free(dst, length);\n        return std::ptr::null_mut::<u8>();\n    }\n    dst\n}\n#[no_mangle]\npub unsafe fn zstd_read_free(ptr: *mut u8, length: u32) {\n    alloc::dealloc(\n        ptr,\n        alloc::Layout::from_size_align(length as usize, 1).unwrap(),\n    );\n}\n"
  },
  {
    "path": "src/sb16.js",
    "content": "import {\n    LOG_SB16,\n    MIXER_CHANNEL_BOTH, MIXER_CHANNEL_LEFT, MIXER_CHANNEL_RIGHT,\n    MIXER_SRC_PCSPEAKER, MIXER_SRC_DAC, MIXER_SRC_MASTER,\n} from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_log } from \"./log.js\";\nimport { SyncBuffer } from \"./buffer.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { DMA } from \"./dma.js\";\nimport { IO } from \"./io.js\";\nimport { BusConnector } from \"./bus.js\";\nimport { ByteQueue, FloatQueue } from \"./lib.js\";\n\n// Useful documentation, articles, and source codes for reference:\n// ===============================================================\n//\n// Official Hardware Programming Guide\n// -> https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf\n//\n// Official Yamaha YMF262 Manual\n// -> http://map.grauw.nl/resources/sound/yamaha_ymf262.pdf\n//\n// OPL3 Programming Guide\n// -> http://www.fit.vutbr.cz/~arnost/opl/opl3.html\n//\n// DOSBox\n// -> https://sourceforge.net/p/dosbox/code-0/HEAD/tree/dosbox/branches/mamesound/src/hardware/sblaster.cpp\n// -> https://github.com/duganchen/dosbox/blob/master/src/hardware/sblaster.cpp\n// -> https://github.com/joncampbell123/dosbox-x/blob/master/src/hardware/sblaster.cpp\n//\n// QEMU\n// -> https://github.com/qemu/qemu/blob/master/hw/audio/sb16.c\n// -> https://github.com/hackndev/qemu/blob/master/hw/sb16.c\n//\n// VirtualBox\n// -> https://www.virtualbox.org/svn/vbox/trunk/src/VBox/Devices/Audio/DevSB16.cpp\n// -> https://github.com/mdaniel/virtualbox-org-svn-vbox-trunk/blob/master/src/VBox/Devices/Audio/DevSB16.cpp\n\nconst\n    // Used for drivers to identify device (DSP command 0xE3).\n    DSP_COPYRIGHT = \"COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.\",\n\n    // Value of the current DSP command that indicates that the\n    // next command/data write in port 2xC should be interpreted\n    // as a command number.\n    DSP_NO_COMMAND = 0,\n\n    // Size (bytes) of the DSP write/read buffers\n    DSP_BUFSIZE = 64,\n\n    // Size (bytes) of the buffers containing floating point linear PCM audio.\n    DSP_DACSIZE = 65536,\n\n    // Size (bytes) of the buffer in which DMA transfers are temporarily\n    // stored before being processed.\n    SB_DMA_BUFSIZE = 65536,\n\n    // Number of samples to attempt to retrieve per transfer.\n    SB_DMA_BLOCK_SAMPLES = 1024,\n\n    // Usable DMA channels.\n    SB_DMA0 = 0,\n    SB_DMA1 = 1,\n    SB_DMA3 = 3,\n    SB_DMA5 = 5,\n    SB_DMA6 = 6,\n    SB_DMA7 = 7,\n\n    // Default DMA channels.\n    SB_DMA_CHANNEL_8BIT = SB_DMA1,\n    SB_DMA_CHANNEL_16BIT = SB_DMA5,\n\n    // Usable IRQ channels.\n    SB_IRQ2 = 2,\n    SB_IRQ5 = 5,\n    SB_IRQ7 = 7,\n    SB_IRQ10 = 10,\n\n    // Default IRQ channel.\n    SB_IRQ = SB_IRQ5,\n\n    // Indices to the irq_triggered register.\n    SB_IRQ_8BIT = 0x1,\n    SB_IRQ_16BIT = 0x2,\n    SB_IRQ_MIDI = 0x1,\n    SB_IRQ_MPU = 0x4;\n\n\n// Probably less efficient, but it's more maintainable, instead\n// of having a single large unorganised and decoupled table.\nvar DSP_COMMAND_SIZES = new Uint8Array(256);\nvar DSP_COMMAND_HANDLERS = [];\nvar MIXER_READ_HANDLERS = [];\nvar MIXER_WRITE_HANDLERS = [];\nvar MIXER_REGISTER_IS_LEGACY = new Uint8Array(256);\nvar FM_HANDLERS = [];\n\n\n/**\n * Sound Blaster 16 Emulator, or so it seems.\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n */\nexport function SB16(cpu, bus)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n\n    // I/O Buffers.\n    this.write_buffer = new ByteQueue(DSP_BUFSIZE);\n    this.read_buffer = new ByteQueue(DSP_BUFSIZE);\n    this.read_buffer_lastvalue = 0;\n\n    // Current DSP command info.\n    this.command = DSP_NO_COMMAND;\n    this.command_size = 0;\n\n    // Mixer.\n    this.mixer_current_address = 0;\n    this.mixer_registers = new Uint8Array(256);\n    this.mixer_reset();\n\n    // Dummy status and test registers.\n    this.dummy_speaker_enabled = false;\n    this.test_register = 0;\n\n    // DSP state.\n    this.dsp_highspeed = false;\n    this.dsp_stereo = false;\n    this.dsp_16bit = false;\n    this.dsp_signed = false;\n\n    // DAC buffer.\n    // The final destination for audio data before being sent off\n    // to Web Audio APIs.\n    // Format:\n    // Floating precision linear PCM, nominal between -1 and 1.\n    this.dac_buffers = [\n      new FloatQueue(DSP_DACSIZE),\n      new FloatQueue(DSP_DACSIZE),\n    ];\n\n    // Direct Memory Access transfer info.\n    this.dma = cpu.devices.dma;\n    this.dma_sample_count = 0;\n    this.dma_bytes_count = 0;\n    this.dma_bytes_left = 0;\n    this.dma_bytes_block = 0;\n    this.dma_irq = 0;\n    this.dma_channel = 0;\n    this.dma_channel_8bit = SB_DMA_CHANNEL_8BIT;\n    this.dma_channel_16bit = SB_DMA_CHANNEL_16BIT;\n    this.dma_autoinit = false;\n    this.dma_buffer = new ArrayBuffer(SB_DMA_BUFSIZE);\n    this.dma_buffer_int8 = new Int8Array(this.dma_buffer);\n    this.dma_buffer_uint8 = new Uint8Array(this.dma_buffer);\n    this.dma_buffer_int16 = new Int16Array(this.dma_buffer);\n    this.dma_buffer_uint16 = new Uint16Array(this.dma_buffer);\n    this.dma_syncbuffer = new SyncBuffer(this.dma_buffer);\n    this.dma_waiting_transfer = false;\n    this.dma_paused = false;\n    this.sampling_rate = 22050;\n    bus.send(\"dac-tell-sampling-rate\", this.sampling_rate);\n    this.bytes_per_sample = 1;\n\n    // DMA identification data.\n    this.e2_value = 0xAA;\n    this.e2_count = 0;\n\n    // ASP data: not understood by me.\n    this.asp_registers = new Uint8Array(256);\n\n    // MPU.\n    this.mpu_read_buffer = new ByteQueue(DSP_BUFSIZE);\n    this.mpu_read_buffer_lastvalue = 0;\n\n    // FM Synthesizer.\n    this.fm_current_address0 = 0;\n    this.fm_current_address1 = 0;\n    this.fm_waveform_select_enable = false;\n\n    // Interrupts.\n    this.irq = SB_IRQ;\n    this.irq_triggered = new Uint8Array(0x10);\n\n    // IO Ports.\n    // http://homepages.cae.wisc.edu/~brodskye/sb16doc/sb16doc.html#DSPPorts\n    // https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf\n\n    cpu.io.register_read_consecutive(0x220, this,\n        this.port2x0_read, this.port2x1_read, this.port2x2_read, this.port2x3_read);\n    cpu.io.register_read_consecutive(0x388, this,\n        this.port2x0_read, this.port2x1_read);\n\n    cpu.io.register_read_consecutive(0x224, this,\n        this.port2x4_read, this.port2x5_read);\n\n    cpu.io.register_read(0x226, this, this.port2x6_read);\n    cpu.io.register_read(0x227, this, this.port2x7_read);\n    cpu.io.register_read(0x228, this, this.port2x8_read);\n    cpu.io.register_read(0x229, this, this.port2x9_read);\n\n    cpu.io.register_read(0x22A, this, this.port2xA_read);\n    cpu.io.register_read(0x22B, this, this.port2xB_read);\n    cpu.io.register_read(0x22C, this, this.port2xC_read);\n    cpu.io.register_read(0x22D, this, this.port2xD_read);\n\n    cpu.io.register_read_consecutive(0x22E, this,\n        this.port2xE_read, this.port2xF_read);\n\n    cpu.io.register_write_consecutive(0x220, this,\n        this.port2x0_write, this.port2x1_write, this.port2x2_write, this.port2x3_write);\n    cpu.io.register_write_consecutive(0x388, this,\n        this.port2x0_write, this.port2x1_write);\n\n    cpu.io.register_write_consecutive(0x224, this,\n        this.port2x4_write, this.port2x5_write);\n\n    cpu.io.register_write(0x226, this, this.port2x6_write);\n    cpu.io.register_write(0x227, this, this.port2x7_write);\n\n    cpu.io.register_write_consecutive(0x228, this,\n        this.port2x8_write, this.port2x9_write);\n\n    cpu.io.register_write(0x22A, this, this.port2xA_write);\n    cpu.io.register_write(0x22B, this, this.port2xB_write);\n    cpu.io.register_write(0x22C, this, this.port2xC_write);\n    cpu.io.register_write(0x22D, this, this.port2xD_write);\n    cpu.io.register_write(0x22E, this, this.port2xE_write);\n    cpu.io.register_write(0x22F, this, this.port2xF_write);\n\n    cpu.io.register_read_consecutive(0x330, this, this.port3x0_read, this.port3x1_read);\n    cpu.io.register_write_consecutive(0x330, this, this.port3x0_write, this.port3x1_write);\n\n    this.dma.on_unmask(this.dma_on_unmask, this);\n\n    bus.register(\"dac-request-data\", function()\n    {\n        this.dac_handle_request();\n    }, this);\n    bus.register(\"speaker-has-initialized\", function()\n    {\n        this.mixer_reset();\n    }, this);\n    bus.send(\"speaker-confirm-initialized\");\n\n    this.dsp_reset();\n}\n\n//\n// General\n//\n\nSB16.prototype.dsp_reset = function()\n{\n    this.write_buffer.clear();\n    this.read_buffer.clear();\n\n    this.command = DSP_NO_COMMAND;\n    this.command_size = 0;\n\n    this.dummy_speaker_enabled = false;\n    this.test_register = 0;\n\n    this.dsp_highspeed = false;\n    this.dsp_stereo = false;\n    this.dsp_16bit = false;\n    this.dsp_signed = false;\n\n    this.dac_buffers[0].clear();\n    this.dac_buffers[1].clear();\n\n    this.dma_sample_count = 0;\n    this.dma_bytes_count = 0;\n    this.dma_bytes_left = 0;\n    this.dma_bytes_block = 0;\n    this.dma_irq = 0;\n    this.dma_channel = 0;\n    this.dma_autoinit = false;\n    this.dma_buffer_uint8.fill(0);\n    this.dma_waiting_transfer = false;\n    this.dma_paused = false;\n\n    this.e2_value = 0xAA;\n    this.e2_count = 0;\n\n    this.sampling_rate = 22050;\n    this.bytes_per_sample = 1;\n\n    this.lower_irq(SB_IRQ_8BIT);\n    this.irq_triggered.fill(0);\n\n    this.asp_registers.fill(0);\n    this.asp_registers[5] = 0x01;\n    this.asp_registers[9] = 0xF8;\n};\n\nSB16.prototype.get_state = function()\n{\n    var state = [];\n\n    // state[0] = this.write_buffer;\n    // state[1] = this.read_buffer;\n    state[2] = this.read_buffer_lastvalue;\n\n    state[3] = this.command;\n    state[4] = this.command_size;\n\n    state[5] = this.mixer_current_address;\n    state[6] = this.mixer_registers;\n\n    state[7] = this.dummy_speaker_enabled;\n    state[8] = this.test_register;\n\n    state[9] = this.dsp_highspeed;\n    state[10] = this.dsp_stereo;\n    state[11] = this.dsp_16bit;\n    state[12] = this.dsp_signed;\n\n    // state[13] = this.dac_buffers;\n    //state[14]\n\n    state[15] = this.dma_sample_count;\n    state[16] = this.dma_bytes_count;\n    state[17] = this.dma_bytes_left;\n    state[18] = this.dma_bytes_block;\n    state[19] = this.dma_irq;\n    state[20] = this.dma_channel;\n    state[21] = this.dma_channel_8bit;\n    state[22] = this.dma_channel_16bit;\n    state[23] = this.dma_autoinit;\n    state[24] = this.dma_buffer_uint8;\n    state[25] = this.dma_waiting_transfer;\n    state[26] = this.dma_paused;\n    state[27] = this.sampling_rate;\n    state[28] = this.bytes_per_sample;\n\n    state[29] = this.e2_value;\n    state[30] = this.e2_count;\n\n    state[31] = this.asp_registers;\n\n    // state[32] = this.mpu_read_buffer;\n    state[33] = this.mpu_read_buffer_last_value;\n\n    state[34] = this.irq;\n    state[35] = this.irq_triggered;\n    //state[36]\n\n    return state;\n};\n\nSB16.prototype.set_state = function(state)\n{\n    // this.write_buffer = state[0];\n    // this.read_buffer = state[1];\n    this.read_buffer_lastvalue = state[2];\n\n    this.command = state[3];\n    this.command_size = state[4];\n\n    this.mixer_current_address = state[5];\n    this.mixer_registers = state[6];\n    this.mixer_full_update();\n\n    this.dummy_speaker_enabled = state[7];\n    this.test_register = state[8];\n\n    this.dsp_highspeed = state[9];\n    this.dsp_stereo = state[10];\n    this.dsp_16bit = state[11];\n    this.dsp_signed = state[12];\n\n    // this.dac_buffers = state[13];\n    //state[14]\n\n    this.dma_sample_count = state[15];\n    this.dma_bytes_count = state[16];\n    this.dma_bytes_left = state[17];\n    this.dma_bytes_block = state[18];\n    this.dma_irq = state[19];\n    this.dma_channel = state[20];\n    this.dma_channel_8bit = state[21];\n    this.dma_channel_16bit = state[22];\n    this.dma_autoinit = state[23];\n    this.dma_buffer_uint8 = state[24];\n    this.dma_waiting_transfer = state[25];\n    this.dma_paused = state[26];\n    this.sampling_rate = state[27];\n    this.bytes_per_sample = state[28];\n\n    this.e2_value = state[29];\n    this.e2_count = state[30];\n\n    this.asp_registers = state[31];\n\n    // this.mpu_read_buffer = state[32];\n    this.mpu_read_buffer_last_value = state[33];\n\n    this.irq = state[34];\n    this.irq_triggered = state[35];\n    //state[36];\n\n    this.dma_buffer = this.dma_buffer_uint8.buffer;\n    this.dma_buffer_int8 = new Int8Array(this.dma_buffer);\n    this.dma_buffer_int16 = new Int16Array(this.dma_buffer);\n    this.dma_buffer_uint16 = new Uint16Array(this.dma_buffer);\n    this.dma_syncbuffer = new SyncBuffer(this.dma_buffer);\n\n    if(this.dma_paused)\n    {\n        this.bus.send(\"dac-disable\");\n    }\n    else\n    {\n        this.bus.send(\"dac-enable\");\n    }\n};\n\n//\n// I/O handlers\n//\n\nSB16.prototype.port2x0_read = function()\n{\n    dbg_log(\"220 read: fm music status port (unimplemented)\", LOG_SB16);\n    return 0xFF;\n};\n\nSB16.prototype.port2x1_read = function()\n{\n    dbg_log(\"221 read: fm music data port (write only)\", LOG_SB16);\n    return 0xFF;\n};\n\nSB16.prototype.port2x2_read = function()\n{\n    dbg_log(\"222 read: advanced fm music status port (unimplemented)\", LOG_SB16);\n    return 0xFF;\n};\n\nSB16.prototype.port2x3_read = function()\n{\n    dbg_log(\"223 read: advanced music data port (write only)\", LOG_SB16);\n    return 0xFF;\n};\n\n// Mixer Address Port.\nSB16.prototype.port2x4_read = function()\n{\n    dbg_log(\"224 read: mixer address port\", LOG_SB16);\n    return this.mixer_current_address;\n};\n\n// Mixer Data Port.\nSB16.prototype.port2x5_read = function()\n{\n    dbg_log(\"225 read: mixer data port\", LOG_SB16);\n    return this.mixer_read(this.mixer_current_address);\n};\n\nSB16.prototype.port2x6_read = function()\n{\n    dbg_log(\"226 read: (write only)\", LOG_SB16);\n    return 0xFF;\n};\n\nSB16.prototype.port2x7_read = function()\n{\n    dbg_log(\"227 read: undocumented\", LOG_SB16);\n    return 0xFF;\n};\n\nSB16.prototype.port2x8_read = function()\n{\n    dbg_log(\"228 read: fm music status port (unimplemented)\", LOG_SB16);\n    return 0xFF;\n};\n\nSB16.prototype.port2x9_read = function()\n{\n    dbg_log(\"229 read: fm music data port (write only)\", LOG_SB16);\n    return 0xFF;\n};\n\n// Read Data.\n// Used to access in-bound DSP data.\nSB16.prototype.port2xA_read = function()\n{\n    dbg_log(\"22A read: read data\", LOG_SB16);\n    if(this.read_buffer.length)\n    {\n        this.read_buffer_lastvalue = this.read_buffer.shift();\n    }\n    dbg_log(\" <- \" + this.read_buffer_lastvalue + \" \" + h(this.read_buffer_lastvalue) + \" '\" + String.fromCharCode(this.read_buffer_lastvalue) + \"'\", LOG_SB16);\n    return this.read_buffer_lastvalue;\n};\n\nSB16.prototype.port2xB_read = function()\n{\n    dbg_log(\"22B read: undocumented\", LOG_SB16);\n    return 0xFF;\n};\n\n// Write-Buffer Status.\n// Indicates whether the DSP is ready to accept commands or data.\nSB16.prototype.port2xC_read = function()\n{\n    dbg_log(\"22C read: write-buffer status\", LOG_SB16);\n    // Always return ready (bit-7 set to low)\n    return 0x7F;\n};\n\nSB16.prototype.port2xD_read = function()\n{\n    dbg_log(\"22D read: undocumented\", LOG_SB16);\n    return 0xFF;\n};\n\n// Read-Buffer Status.\n// Indicates whether there is any in-bound data available for reading.\n// Also used to acknowledge DSP 8-bit interrupt.\nSB16.prototype.port2xE_read = function()\n{\n    dbg_log(\"22E read: read-buffer status / irq 8bit ack.\", LOG_SB16);\n    if(this.irq_triggered[SB_IRQ_8BIT])\n    {\n        this.lower_irq(SB_IRQ_8BIT);\n    }\n    var ready = this.read_buffer.length && !this.dsp_highspeed;\n    return (ready << 7) | 0x7F;\n};\n\n// DSP 16-bit interrupt acknowledgement.\nSB16.prototype.port2xF_read = function()\n{\n    dbg_log(\"22F read: irq 16bit ack\", LOG_SB16);\n    this.lower_irq(SB_IRQ_16BIT);\n    return 0;\n};\n\n\n// FM Address Port - primary register.\nSB16.prototype.port2x0_write = function(value)\n{\n    dbg_log(\"220 write: (unimplemented) fm register 0 address = \" + h(value), LOG_SB16);\n    this.fm_current_address0 = 0;\n};\n\n// FM Data Port - primary register.\nSB16.prototype.port2x1_write = function(value)\n{\n    dbg_log(\"221 write: (unimplemented) fm register 0 data = \" + h(value), LOG_SB16);\n    var handler = FM_HANDLERS[this.fm_current_address0];\n    if(!handler)\n    {\n        handler = this.fm_default_write;\n    }\n    handler.call(this, value, 0, this.fm_current_address0);\n};\n\n// FM Address Port - secondary register.\nSB16.prototype.port2x2_write = function(value)\n{\n    dbg_log(\"222 write: (unimplemented) fm register 1 address = \" + h(value), LOG_SB16);\n    this.fm_current_address1 = 0;\n};\n\n// FM Data Port - secondary register.\nSB16.prototype.port2x3_write = function(value)\n{\n    dbg_log(\"223 write: (unimplemented) fm register 1 data =\" + h(value), LOG_SB16);\n    var handler = FM_HANDLERS[this.fm_current_address1];\n    if(!handler)\n    {\n        handler = this.fm_default_write;\n    }\n    handler.call(this, value, 1, this.fm_current_address1);\n};\n\n// Mixer Address Port.\nSB16.prototype.port2x4_write = function(value)\n{\n    dbg_log(\"224 write: mixer address = \" + h(value), LOG_SB16);\n    this.mixer_current_address = value;\n};\n\n// Mixer Data Port.\nSB16.prototype.port2x5_write = function(value)\n{\n    dbg_log(\"225 write: mixer data = \" + h(value), LOG_SB16);\n    this.mixer_write(this.mixer_current_address, value);\n};\n\n// Reset.\n// Used to reset the DSP to its default state and to exit highspeed mode.\nSB16.prototype.port2x6_write = function(yesplease)\n{\n    dbg_log(\"226 write: reset = \" + h(yesplease), LOG_SB16);\n\n    if(this.dsp_highspeed)\n    {\n        dbg_log(\" -> exit highspeed\", LOG_SB16);\n        this.dsp_highspeed = false;\n    }\n    else if(yesplease)\n    {\n        dbg_log(\" -> reset\", LOG_SB16);\n        this.dsp_reset();\n    }\n\n    // Signal completion.\n    this.read_buffer.clear();\n    this.read_buffer.push(0xAA);\n};\n\nSB16.prototype.port2x7_write = function(value)\n{\n    dbg_log(\"227 write: undocumented\", LOG_SB16);\n};\n\nSB16.prototype.port2x8_write = function(value)\n{\n    dbg_log(\"228 write: fm music register port (unimplemented)\", LOG_SB16);\n};\n\nSB16.prototype.port2x9_write = function(value)\n{\n    dbg_log(\"229 write: fm music data port (unimplemented)\", LOG_SB16);\n};\n\nSB16.prototype.port2xA_write = function(value)\n{\n    dbg_log(\"22A write: dsp read data port (read only)\", LOG_SB16);\n};\n\nSB16.prototype.port2xB_write = function(value)\n{\n    dbg_log(\"22B write: undocumented\", LOG_SB16);\n};\n\n// Write Command/Data.\n// Used to send commands or data to the DSP.\nSB16.prototype.port2xC_write = function(value)\n{\n    dbg_log(\"22C write: write command/data\", LOG_SB16);\n\n    if(this.command === DSP_NO_COMMAND)\n    {\n        // New command.\n        dbg_log(\"22C write: command = \" + h(value), LOG_SB16);\n        this.command = value;\n        this.write_buffer.clear();\n        this.command_size = DSP_COMMAND_SIZES[value];\n    }\n    else\n    {\n        // More data for current command.\n        dbg_log(\"22C write: data: \" + h(value), LOG_SB16);\n        this.write_buffer.push(value);\n    }\n\n    // Perform command when we have all the needed data.\n    if(this.write_buffer.length >= this.command_size)\n    {\n        this.command_do();\n    }\n};\n\nSB16.prototype.port2xD_write = function(value)\n{\n    dbg_log(\"22D write: undocumented\", LOG_SB16);\n};\n\nSB16.prototype.port2xE_write = function(value)\n{\n    dbg_log(\"22E write: dsp read buffer status (read only)\", LOG_SB16);\n};\n\nSB16.prototype.port2xF_write = function(value)\n{\n    dbg_log(\"22F write: undocumented\", LOG_SB16);\n};\n\n\n// MPU UART Mode - Data Port\nSB16.prototype.port3x0_read = function()\n{\n    dbg_log(\"330 read: mpu data\", LOG_SB16);\n\n    if(this.mpu_read_buffer.length)\n    {\n        this.mpu_read_buffer_lastvalue = this.mpu_read_buffer.shift();\n    }\n    dbg_log(\" <- \" + h(this.mpu_read_buffer_lastvalue), LOG_SB16);\n\n    return this.mpu_read_buffer_lastvalue;\n};\nSB16.prototype.port3x0_write = function(value)\n{\n    dbg_log(\"330 write: mpu data (unimplemented) : \" + h(value), LOG_SB16);\n};\n\n// MPU UART Mode - Status Port\nSB16.prototype.port3x1_read = function()\n{\n    dbg_log(\"331 read: mpu status\", LOG_SB16);\n\n    var status = 0;\n    status |= 0x40 * 0; // Output Ready\n    status |= 0x80 * !this.mpu_read_buffer.length; // Input Ready\n\n    return status;\n};\n\n// MPU UART Mode - Command Port\nSB16.prototype.port3x1_write = function(value)\n{\n    dbg_log(\"331 write: mpu command: \" + h(value), LOG_SB16);\n    if(value === 0xFF)\n    {\n        // Command acknowledge.\n        this.mpu_read_buffer.clear();\n        this.mpu_read_buffer.push(0xFE);\n    }\n};\n\n//\n// DSP command handlers\n//\n\nSB16.prototype.command_do = function()\n{\n    var handler = DSP_COMMAND_HANDLERS[this.command];\n    if(!handler)\n    {\n        handler = this.dsp_default_handler;\n    }\n    handler.call(this);\n\n    // Reset Inputs.\n    this.command = DSP_NO_COMMAND;\n    this.command_size = 0;\n    this.write_buffer.clear();\n};\n\nSB16.prototype.dsp_default_handler = function()\n{\n    dbg_log(\"Unhandled command: \" + h(this.command), LOG_SB16);\n};\n\n/**\n * @param {Array} commands\n * @param {number} size\n * @param {function()=} handler\n */\nfunction register_dsp_command(commands, size, handler)\n{\n    if(!handler)\n    {\n        handler = SB16.prototype.dsp_default_handler;\n    }\n    for(var i = 0; i < commands.length; i++)\n    {\n        DSP_COMMAND_SIZES[commands[i]] = size;\n        DSP_COMMAND_HANDLERS[commands[i]] = handler;\n    }\n}\n\nfunction any_first_digit(base)\n{\n    var commands = [];\n    for(var i = 0; i < 16; i++)\n    {\n        commands.push(base + i);\n    }\n    return commands;\n}\n\n// ASP set register\nregister_dsp_command([0x0E], 2, function()\n{\n    this.asp_registers[this.write_buffer.shift()] = this.write_buffer.shift();\n});\n\n// ASP get register\nregister_dsp_command([0x0F], 1, function()\n{\n    this.read_buffer.clear();\n    this.read_buffer.push(this.asp_registers[this.write_buffer.shift()]);\n});\n\n// 8-bit direct mode single byte digitized sound output.\nregister_dsp_command([0x10], 1, function()\n{\n    var value = audio_normalize(this.write_buffer.shift(), 127.5, -1);\n\n    this.dac_buffers[0].push(value);\n    this.dac_buffers[1].push(value);\n    this.bus.send(\"dac-enable\");\n});\n\n// 8-bit single-cycle DMA mode digitized sound output.\nregister_dsp_command([0x14, 0x15], 2, function()\n{\n    this.dma_irq = SB_IRQ_8BIT;\n    this.dma_channel = this.dma_channel_8bit;\n    this.dma_autoinit = false;\n    this.dsp_signed = false;\n    this.dsp_16bit = false;\n    this.dsp_highspeed = false;\n    this.dma_transfer_size_set();\n    this.dma_transfer_start();\n});\n\n// Creative 8-bit to 2-bit ADPCM single-cycle DMA mode digitized sound output.\nregister_dsp_command([0x16], 2);\n\n// Creative 8-bit to 2-bit ADPCM single-cycle DMA mode digitzed sound output\n// with reference byte.\nregister_dsp_command([0x17], 2);\n\n// 8-bit auto-init DMA mode digitized sound output.\nregister_dsp_command([0x1C], 0, function()\n{\n    this.dma_irq = SB_IRQ_8BIT;\n    this.dma_channel = this.dma_channel_8bit;\n    this.dma_autoinit = true;\n    this.dsp_signed = false;\n    this.dsp_16bit = false;\n    this.dsp_highspeed = false;\n    this.dma_transfer_start();\n});\n\n// Creative 8-bit to 2-bit ADPCM auto-init DMA mode digitized sound output\n// with reference byte.\nregister_dsp_command([0x1F], 0);\n\n// 8-bit direct mode single byte digitized sound input.\nregister_dsp_command([0x20], 0, function()\n{\n    // Fake silent input.\n    this.read_buffer.clear();\n    this.read_buffer.push(0x7f);\n});\n\n// 8-bit single-cycle DMA mode digitized sound input.\nregister_dsp_command([0x24], 2);\n\n// 8-bit auto-init DMA mode digitized sound input.\nregister_dsp_command([0x2C], 0);\n\n// Polling mode MIDI input.\nregister_dsp_command([0x30], 0);\n\n// Interrupt mode MIDI input.\nregister_dsp_command([0x31], 0);\n\n// UART polling mode MIDI I/O.\nregister_dsp_command([0x34], 0);\n\n// UART interrupt mode MIDI I/O.\nregister_dsp_command([0x35], 0);\n\n// UART polling mode MIDI I/O with time stamping.\nregister_dsp_command([0x36], 0);\n\n// UART interrupt mode MIDI I/O with time stamping.\nregister_dsp_command([0x37], 0);\n\n// MIDI output.\nregister_dsp_command([0x38], 0);\n\n// Set digitized sound transfer Time Constant.\nregister_dsp_command([0x40], 1, function()\n{\n    // Note: bTimeConstant = 256 * time constant\n    this.sampling_rate_change(\n        1000000 / (256 - this.write_buffer.shift()) / this.get_channel_count()\n    );\n});\n\n// Set digitized sound output sampling rate.\n// Set digitized sound input sampling rate.\nregister_dsp_command([0x41, 0x42], 2, function()\n{\n    this.sampling_rate_change((this.write_buffer.shift() << 8) | this.write_buffer.shift());\n});\n\n// Set DSP block transfer size.\nregister_dsp_command([0x48], 2, function()\n{\n    // TODO: should be in bytes, but if this is only used\n    // for 8 bit transfers, then this number is the same\n    // as number of samples?\n    // Wrong: e.g. stereo requires two bytes per sample.\n    this.dma_transfer_size_set();\n});\n\n// Creative 8-bit to 4-bit ADPCM single-cycle DMA mode digitized sound output.\nregister_dsp_command([0x74], 2);\n\n// Creative 8-bit to 4-bit ADPCM single-cycle DMA mode digitized sound output\n// with referene byte.\nregister_dsp_command([0x75], 2);\n\n// Creative 8-bit to 3-bit ADPCM single-cycle DMA mode digitized sound output.\nregister_dsp_command([0x76], 2);\n\n// Creative 8-bit to 3-bit ADPCM single-cycle DMA mode digitized sound output\n// with referene byte.\nregister_dsp_command([0x77], 2);\n\n// Creative 8-bit to 4-bit ADPCM auto-init DMA mode digitized sound output\n// with reference byte.\nregister_dsp_command([0x7D], 0);\n\n// Creative 8-bit to 3-bit ADPCM auto-init DMA mode digitized sound output\n// with reference byte.\nregister_dsp_command([0x7F], 0);\n\n// Pause DAC for a duration.\nregister_dsp_command([0x80], 2);\n\n// 8-bit high-speed auto-init DMA mode digitized sound output.\nregister_dsp_command([0x90], 0, function()\n{\n    this.dma_irq = SB_IRQ_8BIT;\n    this.dma_channel = this.dma_channel_8bit;\n    this.dma_autoinit = true;\n    this.dsp_signed = false;\n    this.dsp_highspeed = true;\n    this.dsp_16bit = false;\n    this.dma_transfer_start();\n});\n\n// 8-bit high-speed single-cycle DMA mode digitized sound input.\nregister_dsp_command([0x91], 0);\n\n// 8-bit high-speed auto-init DMA mode digitized sound input.\nregister_dsp_command([0x98], 0);\n\n// 8-bit high-speed single-cycle DMA mode digitized sound input.\nregister_dsp_command([0x99], 0);\n\n// Set input mode to mono.\nregister_dsp_command([0xA0], 0);\n\n// Set input mode to stereo.\nregister_dsp_command([0xA8], 0);\n\n// Program 16-bit DMA mode digitized sound I/O.\nregister_dsp_command(any_first_digit(0xB0), 3, function()\n{\n    if(this.command & (1 << 3))\n    {\n        // Analogue to digital not implemented.\n        this.dsp_default_handler();\n        return;\n    }\n    var mode = this.write_buffer.shift();\n    this.dma_irq = SB_IRQ_16BIT;\n    this.dma_channel = this.dma_channel_16bit;\n    this.dma_autoinit = !!(this.command & (1 << 2));\n    this.dsp_signed = !!(mode & (1 << 4));\n    this.dsp_stereo = !!(mode & (1 << 5));\n    this.dsp_16bit = true;\n    this.dma_transfer_size_set();\n    this.dma_transfer_start();\n});\n\n// Program 8-bit DMA mode digitized sound I/O.\nregister_dsp_command(any_first_digit(0xC0), 3, function()\n{\n    if(this.command & (1 << 3))\n    {\n        // Analogue to digital not implemented.\n        this.dsp_default_handler();\n        return;\n    }\n    var mode = this.write_buffer.shift();\n    this.dma_irq = SB_IRQ_8BIT;\n    this.dma_channel = this.dma_channel_8bit;\n    this.dma_autoinit = !!(this.command & (1 << 2));\n    this.dsp_signed = !!(mode & (1 << 4));\n    this.dsp_stereo = !!(mode & (1 << 5));\n    this.dsp_16bit = false;\n    this.dma_transfer_size_set();\n    this.dma_transfer_start();\n});\n\n// Pause 8-bit DMA mode digitized sound I/O.\nregister_dsp_command([0xD0], 0, function()\n{\n    this.dma_paused = true;\n    this.bus.send(\"dac-disable\");\n});\n\n// Turn on speaker.\n// Documented to have no effect on SB16.\nregister_dsp_command([0xD1], 0, function()\n{\n    this.dummy_speaker_enabled = true;\n});\n\n// Turn off speaker.\n// Documented to have no effect on SB16.\nregister_dsp_command([0xD3], 0, function()\n{\n    this.dummy_speaker_enabled = false;\n});\n\n// Continue 8-bit DMA mode digitized sound I/O.\nregister_dsp_command([0xD4], 0, function()\n{\n    this.dma_paused = false;\n    this.bus.send(\"dac-enable\");\n});\n\n// Pause 16-bit DMA mode digitized sound I/O.\nregister_dsp_command([0xD5], 0, function()\n{\n    this.dma_paused = true;\n    this.bus.send(\"dac-disable\");\n});\n\n// Continue 16-bit DMA mode digitized sound I/O.\nregister_dsp_command([0xD6], 0, function()\n{\n    this.dma_paused = false;\n    this.bus.send(\"dac-enable\");\n});\n\n// Get speaker status.\nregister_dsp_command([0xD8], 0, function()\n{\n    this.read_buffer.clear();\n    this.read_buffer.push(this.dummy_speaker_enabled * 0xFF);\n});\n\n// Exit 16-bit auto-init DMA mode digitized sound I/O.\n// Exit 8-bit auto-init mode digitized sound I/O.\nregister_dsp_command([0xD9, 0xDA], 0, function()\n{\n    this.dma_autoinit = false;\n});\n\n// DSP identification\nregister_dsp_command([0xE0], 1, function()\n{\n    this.read_buffer.clear();\n    this.read_buffer.push(~this.write_buffer.shift());\n});\n\n// Get DSP version number.\nregister_dsp_command([0xE1], 0, function()\n{\n    this.read_buffer.clear();\n    this.read_buffer.push(4);\n    this.read_buffer.push(5);\n});\n\n// DMA identification.\nregister_dsp_command([0xE2], 1);\n\n// Get DSP copyright.\nregister_dsp_command([0xE3], 0, function()\n{\n    this.read_buffer.clear();\n    for(var i = 0; i < DSP_COPYRIGHT.length; i++)\n    {\n        this.read_buffer.push(DSP_COPYRIGHT.charCodeAt(i));\n    }\n    // Null terminator.\n    this.read_buffer.push(0);\n});\n\n// Write test register.\nregister_dsp_command([0xE4], 1, function()\n{\n    this.test_register = this.write_buffer.shift();\n});\n\n// Read test register.\nregister_dsp_command([0xE8], 0, function()\n{\n    this.read_buffer.clear();\n    this.read_buffer.push(this.test_register);\n});\n\n// Trigger IRQ\nregister_dsp_command([0xF2, 0xF3], 0, function()\n{\n    this.raise_irq();\n});\n\n// ASP - unknown function\nvar SB_F9 = new Uint8Array(256);\nSB_F9[0x0E] = 0xFF;\nSB_F9[0x0F] = 0x07;\nSB_F9[0x37] = 0x38;\nregister_dsp_command([0xF9], 1, function()\n{\n    var input = this.write_buffer.shift();\n    dbg_log(\"dsp 0xf9: unknown function. input: \" + input, LOG_SB16);\n\n    this.read_buffer.clear();\n    this.read_buffer.push(SB_F9[input]);\n});\n\n//\n// Mixer Handlers (CT1745)\n//\n\nSB16.prototype.mixer_read = function(address)\n{\n    var handler = MIXER_READ_HANDLERS[address];\n    var data;\n    if(handler)\n    {\n        data = handler.call(this);\n    }\n    else\n    {\n        data = this.mixer_registers[address];\n        dbg_log(\"unhandled mixer register read. addr:\" + h(address) + \" data:\" + h(data), LOG_SB16);\n    }\n    return data;\n};\n\nSB16.prototype.mixer_write = function(address, data)\n{\n    var handler = MIXER_WRITE_HANDLERS[address];\n    if(handler)\n    {\n        handler.call(this, data);\n    }\n    else\n    {\n        dbg_log(\"unhandled mixer register write. addr:\" + h(address) + \" data:\" + h(data), LOG_SB16);\n    }\n};\n\nSB16.prototype.mixer_default_read = function()\n{\n    dbg_log(\"mixer register read. addr:\" + h(this.mixer_current_address), LOG_SB16);\n    return this.mixer_registers[this.mixer_current_address];\n};\n\nSB16.prototype.mixer_default_write = function(data)\n{\n    dbg_log(\"mixer register write. addr:\" + h(this.mixer_current_address) + \" data:\" + h(data), LOG_SB16);\n    this.mixer_registers[this.mixer_current_address] = data;\n};\n\nSB16.prototype.mixer_reset = function()\n{\n    // Values intentionally in decimal.\n    // Default values available at\n    // https://pdos.csail.mit.edu/6.828/2011/readings/hardware/SoundBlaster.pdf\n    this.mixer_registers[0x04] = 12 << 4 | 12;\n    this.mixer_registers[0x22] = 12 << 4 | 12;\n    this.mixer_registers[0x26] = 12 << 4 | 12;\n    this.mixer_registers[0x28] = 0;\n    this.mixer_registers[0x2E] = 0;\n    this.mixer_registers[0x0A] = 0;\n    this.mixer_registers[0x30] = 24 << 3;\n    this.mixer_registers[0x31] = 24 << 3;\n    this.mixer_registers[0x32] = 24 << 3;\n    this.mixer_registers[0x33] = 24 << 3;\n    this.mixer_registers[0x34] = 24 << 3;\n    this.mixer_registers[0x35] = 24 << 3;\n    this.mixer_registers[0x36] = 0;\n    this.mixer_registers[0x37] = 0;\n    this.mixer_registers[0x38] = 0;\n    this.mixer_registers[0x39] = 0;\n    this.mixer_registers[0x3B] = 0;\n    this.mixer_registers[0x3C] = 0x1F;\n    this.mixer_registers[0x3D] = 0x15;\n    this.mixer_registers[0x3E] = 0x0B;\n    this.mixer_registers[0x3F] = 0;\n    this.mixer_registers[0x40] = 0;\n    this.mixer_registers[0x41] = 0;\n    this.mixer_registers[0x42] = 0;\n    this.mixer_registers[0x43] = 0;\n    this.mixer_registers[0x44] = 8 << 4;\n    this.mixer_registers[0x45] = 8 << 4;\n    this.mixer_registers[0x46] = 8 << 4;\n    this.mixer_registers[0x47] = 8 << 4;\n\n    this.mixer_full_update();\n};\n\nSB16.prototype.mixer_full_update = function()\n{\n    // Start at 1. Don't re-reset.\n    for(var i = 1; i < this.mixer_registers.length; i++)\n    {\n        if(MIXER_REGISTER_IS_LEGACY[i])\n        {\n            // Legacy registers are actually mapped to other register locations. Update\n            // using the new registers rather than the legacy registers.\n            continue;\n        }\n        this.mixer_write(i, this.mixer_registers[i]);\n    }\n};\n\n/**\n * @param{number} address\n * @param{function():number=} handler\n */\nfunction register_mixer_read(address, handler)\n{\n    if(!handler)\n    {\n        handler = SB16.prototype.mixer_default_read;\n    }\n    MIXER_READ_HANDLERS[address] = handler;\n}\n\n/**\n * @param{number} address\n * @param{function(number)=} handler\n */\nfunction register_mixer_write(address, handler)\n{\n    if(!handler)\n    {\n        handler = SB16.prototype.mixer_default_write;\n    }\n    MIXER_WRITE_HANDLERS[address] = handler;\n}\n\n// Legacy registers map each nibble to the last 4 bits of the new registers\nfunction register_mixer_legacy(address_old, address_new_left, address_new_right)\n{\n    MIXER_REGISTER_IS_LEGACY[address_old] = 1;\n\n    /** @this {SB16} */\n    MIXER_READ_HANDLERS[address_old] = function()\n    {\n        var left = this.mixer_registers[address_new_left] & 0xF0;\n        var right = this.mixer_registers[address_new_right] >>> 4;\n        return left | right;\n    };\n\n    /** @this {SB16} */\n    MIXER_WRITE_HANDLERS[address_old] = function(data)\n    {\n        this.mixer_registers[address_old] = data;\n        var prev_left = this.mixer_registers[address_new_left];\n        var prev_right = this.mixer_registers[address_new_right];\n        var left = (data & 0xF0) | (prev_left & 0x0F);\n        var right = (data << 4 & 0xF0) | (prev_right & 0x0F);\n\n        this.mixer_write(address_new_left, left);\n        this.mixer_write(address_new_right, right);\n    };\n}\n\n/**\n * @param {number} address\n * @param {number} mixer_source\n * @param {number} channel\n */\nfunction register_mixer_volume(address, mixer_source, channel)\n{\n    MIXER_READ_HANDLERS[address] = SB16.prototype.mixer_default_read;\n\n    /** @this {SB16} */\n    MIXER_WRITE_HANDLERS[address] = function(data)\n    {\n        this.mixer_registers[address] = data;\n        this.bus.send(\"mixer-volume\",\n        [\n            mixer_source,\n            channel,\n            (data >>> 2) - 62\n        ]);\n    };\n}\n\n// Reset.\nregister_mixer_read(0x00, function()\n{\n    this.mixer_reset();\n    return 0;\n});\nregister_mixer_write(0x00);\n\n// Legacy Voice Volume Left/Right.\nregister_mixer_legacy(0x04, 0x32, 0x33);\n\n// Legacy Mic Volume. TODO.\n//register_mixer_read(0x0A);\n//register_mixer_write(0x0A, function(data)\n//{\n//    this.mixer_registers[0x0A] = data;\n//    var prev = this.mixer_registers[0x3A];\n//    this.mixer_write(0x3A, data << 5 | (prev & 0x0F));\n//});\n\n// Legacy Master Volume Left/Right.\nregister_mixer_legacy(0x22, 0x30, 0x31);\n// Legacy Midi Volume Left/Right.\nregister_mixer_legacy(0x26, 0x34, 0x35);\n// Legacy CD Volume Left/Right.\nregister_mixer_legacy(0x28, 0x36, 0x37);\n// Legacy Line Volume Left/Right.\nregister_mixer_legacy(0x2E, 0x38, 0x39);\n\n// Master Volume Left.\nregister_mixer_volume(0x30, MIXER_SRC_MASTER, MIXER_CHANNEL_LEFT);\n// Master Volume Right.\nregister_mixer_volume(0x31, MIXER_SRC_MASTER, MIXER_CHANNEL_RIGHT);\n// Voice Volume Left.\nregister_mixer_volume(0x32, MIXER_SRC_DAC, MIXER_CHANNEL_LEFT);\n// Voice Volume Right.\nregister_mixer_volume(0x33, MIXER_SRC_DAC, MIXER_CHANNEL_RIGHT);\n// MIDI Volume Left. TODO.\n//register_mixer_volume(0x34, MIXER_SRC_SYNTH, MIXER_CHANNEL_LEFT);\n// MIDI Volume Right. TODO.\n//register_mixer_volume(0x35, MIXER_SRC_SYNTH, MIXER_CHANNEL_RIGHT);\n// CD Volume Left. TODO.\n//register_mixer_volume(0x36, MIXER_SRC_CD, MIXER_CHANNEL_LEFT);\n// CD Volume Right. TODO.\n//register_mixer_volume(0x37, MIXER_SRC_CD, MIXER_CHANNEL_RIGHT);\n// Line Volume Left. TODO.\n//register_mixer_volume(0x38, MIXER_SRC_LINE, MIXER_CHANNEL_LEFT);\n// Line Volume Right. TODO.\n//register_mixer_volume(0x39, MIXER_SRC_LINE, MIXER_CHANNEL_RIGHT);\n// Mic Volume. TODO.\n//register_mixer_volume(0x3A, MIXER_SRC_MIC, MIXER_CHANNEL_BOTH);\n\n// PC Speaker Volume.\nregister_mixer_read(0x3B);\nregister_mixer_write(0x3B, function(data)\n{\n    this.mixer_registers[0x3B] = data;\n    this.bus.send(\"mixer-volume\", [MIXER_SRC_PCSPEAKER, MIXER_CHANNEL_BOTH, (data >>> 6) * 6 - 18]);\n});\n\n// Output Mixer Switches. TODO.\n//register_mixer_read(0x3C);\n//register_mixer_write(0x3C, function(data)\n//{\n//    this.mixer_registers[0x3C] = data;\n//\n//    if(data & 0x01) this.bus.send(\"mixer-connect\", [MIXER_SRC_MIC, MIXER_CHANNEL_BOTH]);\n//    else this.bus.send(\"mixer-disconnect\", [MIXER_SRC_MIC, MIXER_CHANNEL_BOTH]);\n//\n//    if(data & 0x02) this.bus.send(\"mixer-connect\", [MIXER_SRC_CD, MIXER_CHANNEL_RIGHT]);\n//    else this.bus.send(\"mixer-disconnect\", [MIXER_SRC_CD, MIXER_CHANNEL_RIGHT]);\n//\n//    if(data & 0x04) this.bus.send(\"mixer-connect\", [MIXER_SRC_CD, MIXER_CHANNEL_LEFT]);\n//    else this.bus.send(\"mixer-disconnect\", [MIXER_SRC_CD, MIXER_CHANNEL_LEFT]);\n//\n//    if(data & 0x08) this.bus.send(\"mixer-connect\", [MIXER_SRC_LINE, MIXER_CHANNEL_RIGHT]);\n//    else this.bus.send(\"mixer-disconnect\", [MIXER_SRC_LINE, MIXER_CHANNEL_RIGHT]);\n//\n//    if(data & 0x10) this.bus.send(\"mixer-connect\", [MIXER_SRC_LINE, MIXER_CHANNEL_LEFT]);\n//    else this.bus.send(\"mixer-disconnect\", [MIXER_SRC_LINE, MIXER_CHANNEL_LEFT]);\n//});\n\n// Input Mixer Left Switches. TODO.\n//register_mixer_read(0x3D);\n//register_mixer_write(0x3D);\n\n// Input Mixer Right Switches. TODO.\n//register_mixer_read(0x3E);\n//register_mixer_write(0x3E);\n\n// Input Gain Left. TODO.\n//register_mixer_read(0x3F);\n//register_mixer_write(0x3F);\n\n// Input Gain Right. TODO.\n//register_mixer_read(0x40);\n//register_mixer_write(0x40);\n\n// Output Gain Left.\nregister_mixer_read(0x41);\nregister_mixer_write(0x41, function(data)\n{\n    this.mixer_registers[0x41] = data;\n    this.bus.send(\"mixer-gain-left\", (data >>> 6) * 6);\n});\n\n// Output Gain Right.\nregister_mixer_read(0x42);\nregister_mixer_write(0x42, function(data)\n{\n    this.mixer_registers[0x42] = data;\n    this.bus.send(\"mixer-gain-right\", (data >>> 6) * 6);\n});\n\n// Mic AGC. TODO.\n//register_mixer_read(0x43);\n//register_mixer_write(0x43);\n\n// Treble Left.\nregister_mixer_read(0x44);\nregister_mixer_write(0x44, function(data)\n{\n    this.mixer_registers[0x44] = data;\n    data >>>= 3;\n    this.bus.send(\"mixer-treble-left\", data - (data < 16 ? 14 : 16));\n});\n\n// Treble Right.\nregister_mixer_read(0x45);\nregister_mixer_write(0x45, function(data)\n{\n    this.mixer_registers[0x45] = data;\n    data >>>= 3;\n    this.bus.send(\"mixer-treble-right\", data - (data < 16 ? 14 : 16));\n});\n\n// Bass Left.\nregister_mixer_read(0x46);\nregister_mixer_write(0x46, function(data)\n{\n    this.mixer_registers[0x46] = data;\n    data >>>= 3;\n    this.bus.send(\"mixer-bass-right\", data - (data < 16 ? 14 : 16));\n});\n\n// Bass Right.\nregister_mixer_read(0x47);\nregister_mixer_write(0x47, function(data)\n{\n    this.mixer_registers[0x47] = data;\n    data >>>= 3;\n    this.bus.send(\"mixer-bass-right\", data - (data < 16 ? 14 : 16));\n});\n\n// IRQ Select.\nregister_mixer_read(0x80, function()\n{\n    switch(this.irq)\n    {\n        case SB_IRQ2: return 0x1;\n        case SB_IRQ5: return 0x2;\n        case SB_IRQ7: return 0x4;\n        case SB_IRQ10: return 0x8;\n        default: return 0x0;\n    }\n});\nregister_mixer_write(0x80, function(bits)\n{\n    if(bits & 0x1) this.irq = SB_IRQ2;\n    if(bits & 0x2) this.irq = SB_IRQ5;\n    if(bits & 0x4) this.irq = SB_IRQ7;\n    if(bits & 0x8) this.irq = SB_IRQ10;\n});\n\n// DMA Select.\nregister_mixer_read(0x81, function()\n{\n    var ret = 0;\n    switch(this.dma_channel_8bit)\n    {\n        case SB_DMA0: ret |= 0x1; break;\n        case SB_DMA1: ret |= 0x2; break;\n        // Channel 2 is hardwired to floppy disk.\n        case SB_DMA3: ret |= 0x8; break;\n    }\n    switch(this.dma_channel_16bit)\n    {\n        // Channel 4 cannot be used.\n        case SB_DMA5: ret |= 0x20; break;\n        case SB_DMA6: ret |= 0x40; break;\n        case SB_DMA7: ret |= 0x80; break;\n    }\n    return ret;\n});\nregister_mixer_write(0x81, function(bits)\n{\n    if(bits & 0x1) this.dma_channel_8bit = SB_DMA0;\n    if(bits & 0x2) this.dma_channel_8bit = SB_DMA1;\n    if(bits & 0x8) this.dma_channel_8bit = SB_DMA3;\n    if(bits & 0x20) this.dma_channel_16bit = SB_DMA5;\n    if(bits & 0x40) this.dma_channel_16bit = SB_DMA6;\n    if(bits & 0x80) this.dma_channel_16bit = SB_DMA7;\n});\n\n// IRQ Status.\nregister_mixer_read(0x82, function()\n{\n    var ret = 0x20;\n    for(var i = 0; i < 16; i++)\n    {\n        ret |= i * this.irq_triggered[i];\n    }\n    return ret;\n});\n\n//\n// FM Handlers\n//\n\nSB16.prototype.fm_default_write = function(data, register, address)\n{\n    dbg_log(\"unhandled fm register write. addr:\" + register + \"|\" + h(address) + \" data:\" + h(data), LOG_SB16);\n    // No need to save into a dummy register as the registers are write-only.\n};\n\n/**\n * @param{Array} addresses\n * @param{function(number, number, number)=} handler\n */\nfunction register_fm_write(addresses, handler)\n{\n    if(!handler)\n    {\n        handler = SB16.prototype.fm_default_write;\n    }\n    for(var i = 0; i < addresses.length; i++)\n    {\n        FM_HANDLERS[addresses[i]] = handler;\n    }\n}\n\nfunction between(start, end)\n{\n    var a = [];\n    for(var i = start; i <= end; i++)\n    {\n        a.push(i);\n    }\n    return a;\n}\n\nconst SB_FM_OPERATORS_BY_OFFSET = new Uint8Array(32);\nSB_FM_OPERATORS_BY_OFFSET[0x00] = 0;\nSB_FM_OPERATORS_BY_OFFSET[0x01] = 1;\nSB_FM_OPERATORS_BY_OFFSET[0x02] = 2;\nSB_FM_OPERATORS_BY_OFFSET[0x03] = 3;\nSB_FM_OPERATORS_BY_OFFSET[0x04] = 4;\nSB_FM_OPERATORS_BY_OFFSET[0x05] = 5;\nSB_FM_OPERATORS_BY_OFFSET[0x08] = 6;\nSB_FM_OPERATORS_BY_OFFSET[0x09] = 7;\nSB_FM_OPERATORS_BY_OFFSET[0x0A] = 8;\nSB_FM_OPERATORS_BY_OFFSET[0x0B] = 9;\nSB_FM_OPERATORS_BY_OFFSET[0x0C] = 10;\nSB_FM_OPERATORS_BY_OFFSET[0x0D] = 11;\nSB_FM_OPERATORS_BY_OFFSET[0x10] = 12;\nSB_FM_OPERATORS_BY_OFFSET[0x11] = 13;\nSB_FM_OPERATORS_BY_OFFSET[0x12] = 14;\nSB_FM_OPERATORS_BY_OFFSET[0x13] = 15;\nSB_FM_OPERATORS_BY_OFFSET[0x14] = 16;\nSB_FM_OPERATORS_BY_OFFSET[0x15] = 17;\n\nfunction get_fm_operator(register, offset)\n{\n    return register * 18 + SB_FM_OPERATORS_BY_OFFSET[offset];\n}\n\nregister_fm_write([0x01], function(bits, register, address)\n{\n    this.fm_waveform_select_enable[register] = bits & 0x20 > 0;\n    this.fm_update_waveforms();\n});\n\n// Timer 1 Count.\nregister_fm_write([0x02]);\n\n// Timer 2 Count.\nregister_fm_write([0x03]);\n\nregister_fm_write([0x04], function(bits, register, address)\n{\n    switch(register)\n    {\n        case 0:\n            // if(bits & 0x80)\n            // {\n            //     // IQR Reset\n            // }\n            // else\n            // {\n            //     // Timer masks and on/off\n            // }\n            break;\n        case 1:\n            // Four-operator enable\n            break;\n    }\n});\n\nregister_fm_write([0x05], function(bits, register, address)\n{\n    if(register === 0)\n    {\n        // No registers documented here.\n        this.fm_default_write(bits, register, address);\n    }\n    else\n    {\n        // OPL3 Mode Enable\n    }\n});\n\nregister_fm_write([0x08], function(bits, register, address)\n{\n    // Composite sine wave on/off\n    // Note select (keyboard split selection method)\n});\n\nregister_fm_write(between(0x20, 0x35), function(bits, register, address)\n{\n    var operator = get_fm_operator(register, address - 0x20);\n    // Tremolo\n    // Vibrato\n    // Sustain\n    // KSR Envelope Scaling\n    // Frequency Multiplication Factor\n});\n\nregister_fm_write(between(0x40, 0x55), function(bits, register, address)\n{\n    var operator = get_fm_operator(register, address - 0x40);\n    // Key Scale Level\n    // Output Level\n});\n\nregister_fm_write(between(0x60, 0x75), function(bits, register, address)\n{\n    var operator = get_fm_operator(register, address - 0x60);\n    // Attack Rate\n    // Decay Rate\n});\n\nregister_fm_write(between(0x80, 0x95), function(bits, register, address)\n{\n    var operator = get_fm_operator(register, address - 0x80);\n    // Sustain Level\n    // Release Rate\n});\n\nregister_fm_write(between(0xA0, 0xA8), function(bits, register, address)\n{\n    var channel = address - 0xA0;\n    // Frequency Number (Lower 8 bits)\n});\n\nregister_fm_write(between(0xB0, 0xB8), function(bits, register, address)\n{\n    // Key-On\n    // Block Number\n    // Frequency Number (Higher 2 bits)\n});\n\nregister_fm_write([0xBD], function(bits, register, address)\n{\n    // Tremelo Depth\n    // Vibrato Depth\n    // Percussion Mode\n    // Bass Drum Key-On\n    // Snare Drum Key-On\n    // Tom-Tom Key-On\n    // Cymbal Key-On\n    // Hi-Hat Key-On\n});\n\nregister_fm_write(between(0xC0, 0xC8), function(bits, register, address)\n{\n    // Right Speaker Enable\n    // Left Speaker Enable\n    // Feedback Modulation Factor\n    // Synthesis Type\n});\n\nregister_fm_write(between(0xE0, 0xF5), function(bits, register, address)\n{\n    var operator = get_fm_operator(register, address - 0xE0);\n    // Waveform Select\n});\n\n//\n// FM behaviours\n//\n\nSB16.prototype.fm_update_waveforms = function()\n{\n    // To be implemented.\n};\n\n//\n// General behaviours\n//\n\nSB16.prototype.sampling_rate_change = function(rate)\n{\n    this.sampling_rate = rate;\n    this.bus.send(\"dac-tell-sampling-rate\", rate);\n};\n\nSB16.prototype.get_channel_count = function()\n{\n    return this.dsp_stereo ? 2 : 1;\n};\n\nSB16.prototype.dma_transfer_size_set = function()\n{\n    this.dma_sample_count = 1 + (this.write_buffer.shift() << 0) + (this.write_buffer.shift() << 8);\n};\n\nSB16.prototype.dma_transfer_start = function()\n{\n    dbg_log(\"begin dma transfer\", LOG_SB16);\n\n    // (1) Setup appropriate settings.\n\n    this.bytes_per_sample = 1;\n    if(this.dsp_16bit) this.bytes_per_sample *= 2;\n\n    // Don't count stereo interleaved bits apparently.\n    // Disabling this line is needed for sounds to work correctly,\n    // especially double buffering autoinit mode.\n    // Learnt the hard way.\n    // if(this.dsp_stereo) this.bytes_per_sample *= 2;\n\n    this.dma_bytes_count = this.dma_sample_count * this.bytes_per_sample;\n    this.dma_bytes_block = SB_DMA_BLOCK_SAMPLES * this.bytes_per_sample;\n\n    // Ensure block size is small enough but not too small, and is divisible by 4\n    var max_bytes_block = Math.max(this.dma_bytes_count >> 2 & ~0x3, 32);\n    this.dma_bytes_block = Math.min(max_bytes_block, this.dma_bytes_block);\n\n    // (2) Wait until channel is unmasked (if not already)\n    this.dma_waiting_transfer = true;\n    if(!this.dma.channel_mask[this.dma_channel])\n    {\n        this.dma_on_unmask(this.dma_channel);\n    }\n};\n\nSB16.prototype.dma_on_unmask = function(channel)\n{\n    if(channel !== this.dma_channel || !this.dma_waiting_transfer)\n    {\n        return;\n    }\n\n    // (3) Configure amount of bytes left to transfer and tell speaker adapter\n    // to start requesting transfers\n    this.dma_waiting_transfer = false;\n    this.dma_bytes_left = this.dma_bytes_count;\n    this.dma_paused = false;\n    this.bus.send(\"dac-enable\");\n};\n\nSB16.prototype.dma_transfer_next = function()\n{\n    dbg_log(\"dma transfering next block\", LOG_SB16);\n\n    var size = Math.min(this.dma_bytes_left, this.dma_bytes_block);\n    var samples = Math.floor(size / this.bytes_per_sample);\n\n    this.dma.do_write(this.dma_syncbuffer, 0, size, this.dma_channel, (error) =>\n    {\n        dbg_log(\"dma block transfer \" + (error ? \"unsuccessful\" : \"successful\"), LOG_SB16);\n        if(error) return;\n\n        this.dma_to_dac(samples);\n        this.dma_bytes_left -= size;\n\n        if(!this.dma_bytes_left)\n        {\n            // Completed requested transfer of given size.\n            this.raise_irq(this.dma_irq);\n\n            if(this.dma_autoinit)\n            {\n                // Restart the transfer.\n                this.dma_bytes_left = this.dma_bytes_count;\n            }\n        }\n    });\n};\n\nSB16.prototype.dma_to_dac = function(sample_count)\n{\n    var amplitude = this.dsp_16bit ? 32767.5 : 127.5;\n    var offset = this.dsp_signed ? 0 : -1;\n    var repeats = this.dsp_stereo ? 1 : 2;\n\n    var buffer;\n    if(this.dsp_16bit)\n    {\n        buffer = this.dsp_signed ? this.dma_buffer_int16 : this.dma_buffer_uint16;\n    }\n    else\n    {\n        buffer = this.dsp_signed ? this.dma_buffer_int8 : this.dma_buffer_uint8;\n    }\n\n    var channel = 0;\n    for(var i = 0; i < sample_count; i++)\n    {\n        var sample = audio_normalize(buffer[i], amplitude, offset);\n        for(var j = 0; j < repeats; j++)\n        {\n            this.dac_buffers[channel].push(sample);\n            channel ^= 1;\n        }\n    }\n\n    this.dac_send();\n};\n\nSB16.prototype.dac_handle_request = function()\n{\n    if(!this.dma_bytes_left || this.dma_paused)\n    {\n        // No more data to transfer or is paused. Send whatever is in the buffers.\n        this.dac_send();\n    }\n    else\n    {\n        this.dma_transfer_next();\n    }\n};\n\nSB16.prototype.dac_send = function()\n{\n    if(!this.dac_buffers[0].length)\n    {\n        return;\n    }\n\n    var out0 = this.dac_buffers[0].shift_block(this.dac_buffers[0].length);\n    var out1 = this.dac_buffers[1].shift_block(this.dac_buffers[1].length);\n    this.bus.send(\"dac-send-data\", [out0, out1], [out0.buffer, out1.buffer]);\n};\n\nSB16.prototype.raise_irq = function(type)\n{\n    dbg_log(\"raise irq\", LOG_SB16);\n    this.irq_triggered[type] = 1;\n    this.cpu.device_raise_irq(this.irq);\n};\n\nSB16.prototype.lower_irq = function(type)\n{\n    dbg_log(\"lower irq\", LOG_SB16);\n    this.irq_triggered[type] = 0;\n    this.cpu.device_lower_irq(this.irq);\n};\n\n//\n// Helpers\n//\n\nfunction audio_normalize(value, amplitude, offset)\n{\n    return audio_clip(value / amplitude + offset, -1, 1);\n}\n\nfunction audio_clip(value, low, high)\n{\n    return (value < low) * low + (value > high) * high + (low <= value && value <= high) * value;\n}\n"
  },
  {
    "path": "src/state.js",
    "content": "import { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\nimport { CPU } from \"./cpu.js\";\n\nconst STATE_VERSION = 6;\nconst STATE_MAGIC = 0x86768676|0;\nconst STATE_INDEX_MAGIC = 0;\nconst STATE_INDEX_VERSION = 1;\nconst STATE_INDEX_TOTAL_LEN = 2;\nconst STATE_INDEX_INFO_LEN = 3;\nconst STATE_INFO_BLOCK_START = 16;\n\nconst ZSTD_MAGIC = 0xFD2FB528;\n\n/** @constructor */\nfunction StateLoadError(msg)\n{\n    this.message = msg;\n}\nStateLoadError.prototype = new Error;\n\nconst CONSTRUCTOR_TABLE = {\n    \"Map\": Map,\n    \"Uint8Array\": Uint8Array,\n    \"Int8Array\": Int8Array,\n    \"Uint16Array\": Uint16Array,\n    \"Int16Array\": Int16Array,\n    \"Uint32Array\": Uint32Array,\n    \"Int32Array\": Int32Array,\n    \"Float32Array\": Float32Array,\n    \"Float64Array\": Float64Array,\n};\n\nfunction save_object(obj, saved_buffers)\n{\n    if(typeof obj !== \"object\" || obj === null)\n    {\n        dbg_assert(typeof obj !== \"function\");\n        return obj;\n    }\n\n    if(Array.isArray(obj))\n    {\n        return obj.map(x => save_object(x, saved_buffers));\n    }\n\n    if(obj instanceof Map)\n    {\n        return {\n            \"__state_type__\": \"Map\",\n            \"args\": Array.from(obj.entries()).map(([k, v]) => [\n                save_object(k, saved_buffers),\n                save_object(v, saved_buffers),\n            ]),\n        };\n    }\n\n    if(obj.constructor === Object)\n    {\n        console.log(obj);\n        dbg_assert(obj.constructor !== Object, \"Expected non-object\");\n    }\n\n    if(obj.BYTES_PER_ELEMENT)\n    {\n        // Uint8Array, etc.\n        var buffer = new Uint8Array(obj.buffer, obj.byteOffset, obj.length * obj.BYTES_PER_ELEMENT);\n\n        const constructor = obj.constructor.name.replace(\"bound \", \"\");\n\n        dbg_assert(CONSTRUCTOR_TABLE[constructor]);\n\n        return {\n            \"__state_type__\": constructor,\n            \"buffer_id\": saved_buffers.push(buffer) - 1,\n        };\n    }\n\n    if(DEBUG && !obj.get_state)\n    {\n        console.log(\"Object without get_state: \", obj);\n    }\n\n    var state = obj.get_state();\n    var result = [];\n\n    for(var i = 0; i < state.length; i++)\n    {\n        var value = state[i];\n\n        dbg_assert(typeof value !== \"function\");\n\n        result[i] = save_object(value, saved_buffers);\n    }\n\n    return result;\n}\n\nfunction restore_buffers(obj, buffers)\n{\n    if(typeof obj !== \"object\" || obj === null)\n    {\n        dbg_assert(typeof obj !== \"function\");\n        return obj;\n    }\n\n    if(Array.isArray(obj))\n    {\n        for(let i = 0; i < obj.length; i++)\n        {\n            obj[i] = restore_buffers(obj[i], buffers);\n        }\n\n        return obj;\n    }\n\n    const type = obj[\"__state_type__\"];\n    dbg_assert(type !== undefined);\n\n    const constructor = CONSTRUCTOR_TABLE[type];\n    dbg_assert(constructor, \"Unkown type: \" + type);\n\n    if(obj[\"args\"] !== undefined) {\n        return new constructor(obj[\"args\"]);\n    }\n\n    const buffer = buffers[obj[\"buffer_id\"]];\n    return new constructor(buffer);\n}\n\n/* @param {CPU} cpu */\nexport function save_state(cpu)\n{\n    var saved_buffers = [];\n    var state = save_object(cpu, saved_buffers);\n\n    var buffer_infos = [];\n    var total_buffer_size = 0;\n\n    for(var i = 0; i < saved_buffers.length; i++)\n    {\n        var len = saved_buffers[i].byteLength;\n\n        buffer_infos[i] = {\n            offset: total_buffer_size,\n            length: len,\n        };\n\n        total_buffer_size += len;\n\n        // align\n        total_buffer_size = total_buffer_size + 3 & ~3;\n    }\n\n    var info_object = JSON.stringify({\n        \"buffer_infos\": buffer_infos,\n        \"state\": state,\n    });\n    var info_block = new TextEncoder().encode(info_object);\n\n    var buffer_block_start = STATE_INFO_BLOCK_START + info_block.length;\n    buffer_block_start = buffer_block_start + 3 & ~3;\n    var total_size = buffer_block_start + total_buffer_size;\n\n    //console.log(\"State: json_size=\" + Math.ceil(buffer_block_start / 1024 / 1024) + \"MB \" +\n    //               \"buffer_size=\" + Math.ceil(total_buffer_size / 1024 / 1024) + \"MB\");\n\n    var result = new ArrayBuffer(total_size);\n\n    var header_block = new Int32Array(\n        result,\n        0,\n        STATE_INFO_BLOCK_START / 4\n    );\n    new Uint8Array(result, STATE_INFO_BLOCK_START, info_block.length).set(info_block);\n    var buffer_block = new Uint8Array(\n        result,\n        buffer_block_start\n    );\n\n    header_block[STATE_INDEX_MAGIC] = STATE_MAGIC;\n    header_block[STATE_INDEX_VERSION] = STATE_VERSION;\n    header_block[STATE_INDEX_TOTAL_LEN] = total_size;\n    header_block[STATE_INDEX_INFO_LEN] = info_block.length;\n\n    for(var i = 0; i < saved_buffers.length; i++)\n    {\n        var buffer = saved_buffers[i];\n        dbg_assert(buffer.constructor === Uint8Array);\n        buffer_block.set(buffer, buffer_infos[i].offset);\n    }\n\n    dbg_log(\"State: json size \" + (info_block.byteLength >> 10) + \"k\");\n    dbg_log(\"State: Total buffers size \" + (buffer_block.byteLength >> 10) + \"k\");\n\n    return result;\n}\n\n/* @param {CPU} cpu */\nexport function restore_state(cpu, state)\n{\n    state = new Uint8Array(state);\n\n    function read_state_header(state, check_length)\n    {\n        const len = state.length;\n\n        if(len < STATE_INFO_BLOCK_START)\n        {\n            throw new StateLoadError(\"Invalid length: \" + len);\n        }\n\n        const header_block = new Int32Array(state.buffer, state.byteOffset, 4);\n\n        if(header_block[STATE_INDEX_MAGIC] !== STATE_MAGIC)\n        {\n            throw new StateLoadError(\"Invalid header: \" + h(header_block[STATE_INDEX_MAGIC] >>> 0));\n        }\n\n        if(header_block[STATE_INDEX_VERSION] !== STATE_VERSION)\n        {\n            throw new StateLoadError(\n                    \"Version mismatch: dump=\" + header_block[STATE_INDEX_VERSION] +\n                    \" we=\" + STATE_VERSION);\n        }\n\n        if(check_length && header_block[STATE_INDEX_TOTAL_LEN] !== len)\n        {\n            throw new StateLoadError(\n                    \"Length doesn't match header: \" +\n                    \"real=\" + len + \" header=\" + header_block[STATE_INDEX_TOTAL_LEN]);\n        }\n\n        return header_block[STATE_INDEX_INFO_LEN];\n    }\n\n    function read_info_block(info_block_buffer)\n    {\n        const info_block = new TextDecoder().decode(info_block_buffer);\n        return JSON.parse(info_block);\n    }\n\n    if(new Uint32Array(state.buffer, 0, 1)[0] === ZSTD_MAGIC)\n    {\n        const ctx = cpu.zstd_create_ctx(state.length);\n\n        new Uint8Array(cpu.wasm_memory.buffer, cpu.zstd_get_src_ptr(ctx) >>> 0, state.length).set(state);\n\n        let ptr = cpu.zstd_read(ctx, 16);\n        const header_block = new Uint8Array(cpu.wasm_memory.buffer, ptr >>> 0, 16);\n        const info_block_len = read_state_header(header_block, false);\n        cpu.zstd_read_free(ptr, 16);\n\n        ptr = cpu.zstd_read(ctx, info_block_len);\n        const info_block_buffer = new Uint8Array(cpu.wasm_memory.buffer, ptr >>> 0, info_block_len);\n        const info_block_obj = read_info_block(info_block_buffer);\n        cpu.zstd_read_free(ptr, info_block_len);\n\n        let state_object = info_block_obj[\"state\"];\n        const buffer_infos = info_block_obj[\"buffer_infos\"];\n        const buffers = [];\n\n        let position = STATE_INFO_BLOCK_START + info_block_len;\n\n        for(const buffer_info of buffer_infos)\n        {\n            const front_padding = (position + 3 & ~3) - position;\n            const CHUNK_SIZE = 1 * 1024 * 1024;\n\n            if(buffer_info.length > CHUNK_SIZE)\n            {\n                const ptr = cpu.zstd_read(ctx, front_padding) >>> 0;\n                cpu.zstd_read_free(ptr, front_padding);\n\n                const buffer = new Uint8Array(buffer_info.length);\n                buffers.push(buffer.buffer);\n\n                let have = 0;\n                while(have < buffer_info.length)\n                {\n                    const remaining = buffer_info.length - have;\n                    dbg_assert(remaining >= 0);\n                    const to_read = Math.min(remaining, CHUNK_SIZE);\n\n                    const ptr = cpu.zstd_read(ctx, to_read);\n                    buffer.set(new Uint8Array(cpu.wasm_memory.buffer, ptr >>> 0, to_read), have);\n                    cpu.zstd_read_free(ptr, to_read);\n\n                    have += to_read;\n                }\n            }\n            else\n            {\n                const ptr = cpu.zstd_read(ctx, front_padding + buffer_info.length);\n                const offset = (ptr >>> 0) + front_padding;\n                buffers.push(cpu.wasm_memory.buffer.slice(offset, offset + buffer_info.length));\n                cpu.zstd_read_free(ptr, front_padding + buffer_info.length);\n            }\n\n            position += front_padding + buffer_info.length;\n        }\n\n        state_object = restore_buffers(state_object, buffers);\n        cpu.set_state(state_object);\n\n        cpu.zstd_free_ctx(ctx);\n    }\n    else\n    {\n        const info_block_len = read_state_header(state, true);\n\n        if(info_block_len < 0 || info_block_len + 12 >= state.length)\n        {\n            throw new StateLoadError(\"Invalid info block length: \" + info_block_len);\n        }\n\n        const info_block_buffer = state.subarray(STATE_INFO_BLOCK_START, STATE_INFO_BLOCK_START + info_block_len);\n        const info_block_obj = read_info_block(info_block_buffer);\n        let state_object = info_block_obj[\"state\"];\n        const buffer_infos = info_block_obj[\"buffer_infos\"];\n        let buffer_block_start = STATE_INFO_BLOCK_START + info_block_len;\n        buffer_block_start = buffer_block_start + 3 & ~3;\n\n        const buffers = buffer_infos.map(buffer_info => {\n            const offset = buffer_block_start + buffer_info.offset;\n            return state.buffer.slice(offset, offset + buffer_info.length);\n        });\n\n        state_object = restore_buffers(state_object, buffers);\n        cpu.set_state(state_object);\n    }\n}\n"
  },
  {
    "path": "src/uart.js",
    "content": "import { LOG_SERIAL } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { BusConnector } from \"./bus.js\";\n\n/*\n * Serial ports\n * http://wiki.osdev.org/UART\n * https://github.com/s-macke/jor1k/blob/master/js/worker/dev/uart.js\n * https://www.freebsd.org/doc/en/articles/serial-uart/\n */\n\nconst DLAB = 0x80;\n\nconst UART_IER_MSI  = 0x08; /* Modem Status Changed int. */\nconst UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */\nconst UART_IER_RDI = 0x01; /* Enable receiver data interrupt */\n\nconst UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority) */\nconst UART_IIR_NO_INT = 0x01;\nconst UART_IIR_THRI = 0x02; /* Transmitter holding register empty */\nconst UART_IIR_RDI = 0x04; /* Receiver data interrupt */\nconst UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.) */\nconst UART_IIR_CTI = 0x0c; /* Character timeout */\n\n// Modem control register\nconst UART_MCR_LOOPBACK = 0x10;\n\nconst UART_LSR_DATA_READY        = 0x1;  // data available\nconst UART_LSR_TX_EMPTY        = 0x20; // TX (THR) buffer is empty\nconst UART_LSR_TRANSMITTER_EMPTY = 0x40; // TX empty and line is idle\n\n// Modem status register\nconst UART_MSR_DCD = 0x7; // Data Carrier Detect\nconst UART_MSR_RI = 0x6; // Ring Indicator\nconst UART_MSR_DSR = 0x5; // Data Set Ready\nconst UART_MSR_CTS = 0x4; // Clear To Send\n// Delta bits\nconst UART_MSR_DDCD = 0x3; // Delta DCD\nconst UART_MSR_TERI = 0x2; // Trailing Edge RI\nconst UART_MSR_DDSR = 0x1; // Delta DSR\nconst UART_MSR_DCTS = 0x0; // Delta CTS\n\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {number} port\n * @param {BusConnector} bus\n */\nexport function UART(cpu, port, bus)\n{\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    this.ints = 1 << UART_IIR_THRI;\n\n    this.baud_rate = 0;\n\n    this.line_control = 0;\n\n    // line status register\n    this.lsr = UART_LSR_TRANSMITTER_EMPTY | UART_LSR_TX_EMPTY;\n\n    this.fifo_control = 0;\n\n    // interrupts enable\n    this.ier = 0;\n\n    // interrupt identification register\n    this.iir = UART_IIR_NO_INT;\n\n    this.modem_control = 0;\n    this.modem_status = 0;\n\n    this.scratch_register = 0;\n\n    this.irq = 0;\n\n    this.input = [];\n\n    this.current_line = \"\";\n\n    switch(port)\n    {\n        case 0x3F8:\n            this.com = 0;\n            this.irq = 4;\n            break;\n        case 0x2F8:\n            this.com = 1;\n            this.irq = 3;\n            break;\n        case 0x3E8:\n            this.com = 2;\n            this.irq = 4;\n            break;\n        case 0x2E8:\n            this.com = 3;\n            this.irq = 3;\n            break;\n        default:\n            dbg_log(\"Invalid serial port: \" + h(port), LOG_SERIAL);\n            this.com = 0;\n            this.irq = 4;\n    }\n\n    this.bus.register(\"serial\" + this.com + \"-input\", function(data)\n    {\n        this.data_received(data);\n    }, this);\n\n    this.bus.register(\"serial\" + this.com + \"-modem-status-input\", function(data)\n    {\n        this.set_modem_status(data);\n    }, this);\n\n    // Set individual modem status bits\n\n    this.bus.register(\"serial\" + this.com + \"-carrier-detect-input\", function(data)\n    {\n        const status = data ?\n            this.modem_status | (1 << UART_MSR_DCD) | (1 << UART_MSR_DDCD) :\n            this.modem_status & ~(1 << UART_MSR_DCD) & ~(1 << UART_MSR_DDCD);\n        this.set_modem_status(status);\n    }, this);\n\n    this.bus.register(\"serial\" + this.com + \"-ring-indicator-input\", function(data)\n    {\n        const status = data ?\n            this.modem_status | (1 << UART_MSR_RI) | (1 << UART_MSR_TERI) :\n            this.modem_status & ~(1 << UART_MSR_RI) & ~(1 << UART_MSR_TERI);\n        this.set_modem_status(status);\n    }, this);\n\n    this.bus.register(\"serial\" + this.com + \"-data-set-ready-input\", function(data)\n    {\n        const status = data ?\n            this.modem_status | (1 << UART_MSR_DSR) | (1 << UART_MSR_DDSR) :\n            this.modem_status & ~(1 << UART_MSR_DSR) & ~(1 << UART_MSR_DDSR);\n        this.set_modem_status(status);\n    }, this);\n\n    this.bus.register(\"serial\" + this.com + \"-clear-to-send-input\", function(data)\n    {\n        const status = data ?\n            this.modem_status | (1 << UART_MSR_CTS) | (1 << UART_MSR_DCTS) :\n            this.modem_status & ~(1 << UART_MSR_CTS) & ~(1 << UART_MSR_DCTS);\n        this.set_modem_status(status);\n    }, this);\n\n    var io = cpu.io;\n\n    io.register_write(port, this, function(out_byte)\n    {\n        this.write_data(out_byte);\n    }, function(out_word)\n    {\n        this.write_data(out_word & 0xFF);\n        this.write_data(out_word >> 8);\n    });\n\n    io.register_write(port | 1, this, function(out_byte)\n    {\n        if(this.line_control & DLAB)\n        {\n            this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;\n            dbg_log(\"baud rate: \" + h(this.baud_rate), LOG_SERIAL);\n        }\n        else\n        {\n            if((this.ier & UART_IIR_THRI) === 0 && (out_byte & UART_IIR_THRI))\n            {\n                // re-throw THRI if it was masked\n                this.ThrowInterrupt(UART_IIR_THRI);\n            }\n\n            this.ier = out_byte & 0xF;\n            dbg_log(\"interrupt enable: \" + h(out_byte), LOG_SERIAL);\n            this.CheckInterrupt();\n        }\n    });\n\n    io.register_read(port, this, function()\n    {\n        if(this.line_control & DLAB)\n        {\n            return this.baud_rate & 0xFF;\n        }\n        else\n        {\n            let data = 0;\n\n            if(this.input.length === 0)\n            {\n                dbg_log(\"Read input empty\", LOG_SERIAL);\n            }\n            else\n            {\n                data = this.input.shift();\n                dbg_log(\"Read input: \" + h(data), LOG_SERIAL);\n            }\n\n            if(this.input.length === 0)\n            {\n                this.lsr &= ~UART_LSR_DATA_READY;\n                this.ClearInterrupt(UART_IIR_CTI);\n                this.ClearInterrupt(UART_IIR_RDI);\n            }\n\n            return data;\n        }\n    });\n\n    io.register_read(port | 1, this, function()\n    {\n        if(this.line_control & DLAB)\n        {\n            return this.baud_rate >> 8;\n        }\n        else\n        {\n            return this.ier & 0xF;\n        }\n    });\n\n    io.register_read(port | 2, this, function()\n    {\n        var ret = this.iir & 0xF;\n        dbg_log(\"read interrupt identification: \" + h(this.iir), LOG_SERIAL);\n\n        if(this.iir === UART_IIR_THRI) {\n            this.ClearInterrupt(UART_IIR_THRI);\n        }\n\n        if(this.fifo_control & 1) ret |= 0xC0;\n\n        return ret;\n    });\n    io.register_write(port | 2, this, function(out_byte)\n    {\n        dbg_log(\"fifo control: \" + h(out_byte), LOG_SERIAL);\n        this.fifo_control = out_byte;\n    });\n\n    io.register_read(port | 3, this, function()\n    {\n        dbg_log(\"read line control: \" + h(this.line_control), LOG_SERIAL);\n        return this.line_control;\n    });\n    io.register_write(port | 3, this, function(out_byte)\n    {\n        dbg_log(\"line control: \" + h(out_byte), LOG_SERIAL);\n        this.line_control = out_byte;\n    });\n\n\n    io.register_read(port | 4, this, function()\n    {\n        return this.modem_control;\n    });\n    io.register_write(port | 4, this, function(out_byte)\n    {\n        dbg_log(\"modem control: \" + h(out_byte), LOG_SERIAL);\n        this.modem_control = out_byte;\n    });\n\n    io.register_read(port | 5, this, function()\n    {\n        dbg_log(\"read line status: \" + h(this.lsr), LOG_SERIAL);\n        return this.lsr;\n    });\n    io.register_write(port | 5, this, function(out_byte)\n    {\n        dbg_log(\"Factory test write\", LOG_SERIAL);\n    });\n\n    io.register_read(port | 6, this, function()\n    {\n        dbg_log(\"read modem status: \" + h(this.modem_status), LOG_SERIAL);\n        // Clear delta bits\n        this.modem_status &= 0xF0;\n        return this.modem_status;\n    });\n    io.register_write(port | 6, this, function(out_byte)\n    {\n        dbg_log(\"write modem status: \" + h(out_byte), LOG_SERIAL);\n        this.set_modem_status(out_byte);\n    });\n\n    io.register_read(port | 7, this, function()\n    {\n        return this.scratch_register;\n    });\n    io.register_write(port | 7, this, function(out_byte)\n    {\n        this.scratch_register = out_byte;\n    });\n}\n\nUART.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.ints;\n    state[1] = this.baud_rate;\n    state[2] = this.line_control;\n    state[3] = this.lsr;\n    state[4] = this.fifo_control;\n    state[5] = this.ier;\n    state[6] = this.iir;\n    state[7] = this.modem_control;\n    state[8] = this.modem_status;\n    state[9] = this.scratch_register;\n    state[10] = this.irq;\n\n    return state;\n};\n\nUART.prototype.set_state = function(state)\n{\n    this.ints = state[0];\n    this.baud_rate = state[1];\n    this.line_control = state[2];\n    this.lsr = state[3];\n    this.fifo_control = state[4];\n    this.ier = state[5];\n    this.iir = state[6];\n    this.modem_control = state[7];\n    this.modem_status = state[8];\n    this.scratch_register = state[9];\n    this.irq = state[10];\n};\n\nUART.prototype.CheckInterrupt = function() {\n    if((this.ints & (1 << UART_IIR_CTI))  && (this.ier & UART_IER_RDI)) {\n        this.iir = UART_IIR_CTI;\n        this.cpu.device_raise_irq(this.irq);\n    } else\n    if((this.ints & (1 << UART_IIR_RDI))  && (this.ier & UART_IER_RDI)) {\n        this.iir = UART_IIR_RDI;\n        this.cpu.device_raise_irq(this.irq);\n    } else\n    if((this.ints & (1 << UART_IIR_THRI)) && (this.ier & UART_IER_THRI)) {\n        this.iir = UART_IIR_THRI;\n        this.cpu.device_raise_irq(this.irq);\n    } else\n    if((this.ints & (1 << UART_IIR_MSI))  && (this.ier & UART_IER_MSI)) {\n        this.iir = UART_IIR_MSI;\n        this.cpu.device_raise_irq(this.irq);\n    } else {\n        this.iir = UART_IIR_NO_INT;\n        this.cpu.device_lower_irq(this.irq);\n    }\n};\n\nUART.prototype.ThrowInterrupt = function(line) {\n    this.ints |= (1 << line);\n    this.CheckInterrupt();\n};\n\nUART.prototype.ClearInterrupt = function(line) {\n    this.ints &= ~(1 << line);\n    this.CheckInterrupt();\n};\n\n/**\n * @param {number} data\n */\nUART.prototype.data_received = function(data)\n{\n    dbg_log(\"input: \" + h(data), LOG_SERIAL);\n    this.input.push(data);\n\n    this.lsr |= UART_LSR_DATA_READY;\n\n    if(this.fifo_control & 1)\n    {\n        this.ThrowInterrupt(UART_IIR_CTI);\n    }\n    else\n    {\n        this.ThrowInterrupt(UART_IIR_RDI);\n    }\n};\n\nUART.prototype.write_data = function(out_byte)\n{\n    if(this.line_control & DLAB)\n    {\n        this.baud_rate = this.baud_rate & ~0xFF | out_byte;\n        return;\n    }\n\n    dbg_log(\"data: \" + h(out_byte), LOG_SERIAL);\n\n    this.ThrowInterrupt(UART_IIR_THRI);\n\n    if(this.modem_control & UART_MCR_LOOPBACK) {\n        this.data_received(out_byte);\n    } else {\n        this.bus.send(\"serial\" + this.com + \"-output-byte\", out_byte);\n    }\n\n    if(DEBUG)\n    {\n        var char = String.fromCharCode(out_byte);\n        this.current_line += char;\n\n        if(char === \"\\n\")\n        {\n            const line = this.current_line.trimRight().replace(/[\\x00-\\x08\\x0b-\\x1f\\x7f\\x80-\\xff]/g, \"\");\n            dbg_log(\"SERIAL: \" + line);\n            this.current_line = \"\";\n        }\n    }\n};\n\nUART.prototype.set_modem_status = function(status)\n{\n    dbg_log(\"modem status: \" + h(status), LOG_SERIAL);\n    const prev_delta_bits = this.modem_status & 0x0F;\n    // compare the bits that have changed and shift them into the delta bits\n    let delta = (this.modem_status ^ status) >> 4;\n    // The delta should stay set if they were previously set\n    delta |= prev_delta_bits;\n\n    // update the current modem status\n    this.modem_status = status;\n    // update the delta bits based on the changes and previous\n    // values, but also leave the delta bits set if they were\n    // passed in as part of the status\n    this.modem_status |= delta;\n};\n"
  },
  {
    "path": "src/vga.js",
    "content": "import { LOG_VGA } from \"./const.js\";\nimport { h } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { ScreenAdapter } from \"./browser/screen.js\";\nimport { BusConnector } from \"./bus.js\";\nimport { DummyScreenAdapter } from \"./browser/dummy_screen.js\";\nimport { round_up_to_next_power_of_2, view } from \"./lib.js\";\n\n// Always 64k\nconst VGA_BANK_SIZE = 64 * 1024;\n\nconst MAX_XRES = 2560;\nconst MAX_YRES = 1600;\nconst MAX_BPP = 32;\n\n//const VGA_LFB_ADDRESS = 0xFE000000; // set by seabios\nconst VGA_LFB_ADDRESS = 0xE0000000;\n\n/**\n * Equals the maximum number of pixels for non svga.\n * 8 pixels per byte.\n */\nconst VGA_PIXEL_BUFFER_SIZE = 8 * VGA_BANK_SIZE;\n\nconst VGA_MIN_MEMORY_SIZE = 4 * VGA_BANK_SIZE;\n\n/**\n * Avoid wrapping past VGA_LFB_ADDRESS\n */\nconst VGA_MAX_MEMORY_SIZE = 256 * 1024 * 1024;\n\n/**\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm#06}\n */\nconst VGA_HOST_MEMORY_SPACE_START = Uint32Array.from([\n    0xA0000,\n    0xA0000,\n    0xB0000,\n    0xB8000,\n]);\n\n/**\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm#06}\n */\nconst VGA_HOST_MEMORY_SPACE_SIZE = Uint32Array.from([\n    0x20000, // 128K\n    0x10000, // 64K\n    0x8000, // 32K\n    0x8000, // 32K\n]);\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n * @param {ScreenAdapter|DummyScreenAdapter} screen\n * @param {number} vga_memory_size\n */\nexport function VGAScreen(cpu, bus, screen, vga_memory_size)\n{\n    this.cpu = cpu;\n\n    /** @const */\n    this.bus = bus;\n\n    /** @const */\n    this.screen = screen;\n\n    this.vga_memory_size = vga_memory_size;\n\n    /** @type {number} */\n    this.cursor_address = 0;\n\n    /** @type {number} */\n    this.cursor_scanline_start = 0xE;\n\n    /** @type {number} */\n    this.cursor_scanline_end = 0xF;\n\n    /**\n     * Number of columns in text mode\n     * @type {number}\n     */\n    this.max_cols = 80;\n\n    /**\n     * Number of rows in text mode\n     * @type {number}\n     */\n    this.max_rows = 25;\n\n    /**\n     * Width in pixels in graphical mode\n     * @type {number}\n     */\n    this.screen_width = 0;\n\n    /**\n     * Height in pixels in graphical mode\n     * @type {number}\n     */\n    this.screen_height = 0;\n\n    /**\n     * Logical width in pixels of virtual buffer available for panning\n     * @type {number}\n     */\n    this.virtual_width = 0;\n\n    /**\n     * Logical height in pixels of virtual buffer available for panning\n     * @type {number}\n     */\n    this.virtual_height = 0;\n\n    /**\n     * The rectangular fragments of the image buffer, and their destination\n     * locations, to be drawn every screen_fill_buffer during VGA modes.\n     * @type {Array<Object<string, number>>}\n     */\n    this.layers = [];\n\n    /**\n     * video memory start address\n     * @type {number}\n     */\n    this.start_address = 0;\n\n    /**\n     * Start address - a copy of start_address that only gets updated\n     * during VSync, used for panning and page flipping\n     * @type {number}\n     */\n    this.start_address_latched = 0;\n\n    /**\n     * Unimplemented CRTC registers go here\n     */\n    this.crtc = new Uint8Array(0x19);\n\n    // Implemented CRTC registers:\n\n    /** @type {number} */\n    this.crtc_mode = 0;\n\n    /** @type {number} */\n    this.horizontal_display_enable_end = 0;\n\n    /** @type {number} */\n    this.horizontal_blank_start = 0;\n\n    /** @type {number} */\n    this.vertical_display_enable_end = 0;\n\n    /** @type {number} */\n    this.vertical_blank_start = 0;\n\n    /** @type {number} */\n    this.underline_location_register = 0;\n\n    /** @type {number} */\n    this.preset_row_scan = 0;\n\n    /** @type {number} */\n    this.offset_register = 0;\n\n    /** @type {number} */\n    this.line_compare = 0;\n\n    // End of CRTC registers\n\n    /** @type {boolean} */\n    this.graphical_mode = false;\n\n    /*\n     * VGA palette containing 256 colors for video mode 13, svga 8bpp, etc.\n     * Needs to be initialised by the BIOS\n     */\n    this.vga256_palette = new Int32Array(256);\n\n    /**\n     * VGA read latches\n     * @type{number}\n     */\n    this.latch_dword = 0;\n\n    /** @type {number} */\n    this.svga_version = 0xB0C5;\n\n    /** @type {number} */\n    this.svga_width = 0;\n\n    /** @type {number} */\n    this.svga_height = 0;\n\n    this.svga_enabled = false;\n\n    /** @type {number} */\n    this.svga_bpp = 32;\n\n    /** @type {number} */\n    this.svga_bank_offset = 0;\n\n    /**\n     * The video buffer offset created by VBE_DISPI_INDEX_Y_OFFSET\n     * In bytes\n     * @type {number}\n     */\n    this.svga_offset = 0;\n    this.svga_offset_x = 0;\n    this.svga_offset_y = 0;\n\n    if(this.vga_memory_size === undefined || this.vga_memory_size < VGA_MIN_MEMORY_SIZE)\n    {\n        this.vga_memory_size = VGA_MIN_MEMORY_SIZE;\n    }\n    else if(this.vga_memory_size > VGA_MAX_MEMORY_SIZE)\n    {\n        this.vga_memory_size = VGA_MAX_MEMORY_SIZE;\n    }\n    else\n    {\n        // required for pci code\n        this.vga_memory_size = round_up_to_next_power_of_2(this.vga_memory_size);\n    }\n    dbg_log(\"effective vga memory size: \" + this.vga_memory_size, LOG_VGA);\n\n    const pci_revision = 0; // set to 2 for qemu extended registers\n\n    // Experimental, could probably need some changes\n    // 01:00.0 VGA compatible controller: NVIDIA Corporation GT216 [GeForce GT 220] (rev a2)\n    this.pci_space = [\n        0x34, 0x12, 0x11, 0x11, 0x03, 0x01, 0x00, 0x00, pci_revision, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,\n        0x08, VGA_LFB_ADDRESS >>> 8, VGA_LFB_ADDRESS >>> 16, VGA_LFB_ADDRESS >>> 24,\n                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xfe, 0x00, 0x00, 0x00, 0x00,\n        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x1a, 0x00, 0x11,\n        0x00, 0x00, 0xbe, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n    ];\n    this.pci_id = 0x12 << 3;\n    this.pci_bars = [\n        {\n            size: this.vga_memory_size,\n        },\n    ];\n\n    // TODO: Should be matched with vga bios size and mapping address\n    // Seabios config for this device:\n    // CONFIG_VGA_PCI=y\n    // CONFIG_OVERRIDE_PCI_ID=y\n    // CONFIG_VGA_VID=0x10de\n    // CONFIG_VGA_DID=0x0a20\n\n    this.pci_rom_size = 0x10000;\n    this.pci_rom_address = 0xFEB00000;\n\n    this.name = \"vga\";\n\n    this.index_crtc = 0;\n\n    // index for setting colors through port 3C9h\n    this.dac_color_index_write = 0;\n    this.dac_color_index_read = 0;\n    this.dac_state = 0;\n\n    this.dac_mask = 0xFF;\n\n    this.dac_map = new Uint8Array(0x10);\n\n    this.attribute_controller_index = -1;\n    this.palette_source = 0x20;\n    this.attribute_mode = 0;\n    this.color_plane_enable = 0;\n    this.horizontal_panning = 0;\n    this.color_select = 0;\n\n    this.sequencer_index = -1;\n\n    // bitmap of planes 0-3\n    this.plane_write_bm = 0xF;\n    this.sequencer_memory_mode = 0;\n    this.clocking_mode = 0;\n    this.graphics_index = -1;\n    this.character_map_select = 0;\n\n    this.plane_read = 0; // value 0-3, which plane to read\n    this.planar_mode = 0;\n    this.planar_rotate_reg = 0;\n    this.planar_bitmap = 0xFF;\n    this.planar_setreset = 0;\n    this.planar_setreset_enable = 0;\n    this.miscellaneous_graphics_register = 0;\n\n    this.color_compare = 0;\n    this.color_dont_care = 0;\n\n    this.max_scan_line = 0;\n\n    this.miscellaneous_output_register = 0xff;\n    this.port_3DA_value = 0xFF;\n\n    this.font_page_ab_enabled = false;\n\n    var io = cpu.io;\n\n    io.register_write(0x3C0, this, this.port3C0_write);\n    io.register_read(0x3C0, this, this.port3C0_read, this.port3C0_read16);\n\n    io.register_read(0x3C1, this, this.port3C1_read);\n    io.register_write(0x3C2, this, this.port3C2_write);\n\n    io.register_write_consecutive(0x3C4, this, this.port3C4_write, this.port3C5_write);\n\n    io.register_read(0x3C4, this, this.port3C4_read);\n    io.register_read(0x3C5, this, this.port3C5_read);\n\n    io.register_write_consecutive(0x3CE, this, this.port3CE_write, this.port3CF_write);\n\n    io.register_read(0x3CE, this, this.port3CE_read);\n    io.register_read(0x3CF, this, this.port3CF_read);\n\n    io.register_read(0x3C6, this, this.port3C6_read);\n    io.register_write(0x3C6, this, this.port3C6_write);\n    io.register_write(0x3C7, this, this.port3C7_write);\n    io.register_read(0x3C7, this, this.port3C7_read);\n    io.register_write(0x3C8, this, this.port3C8_write);\n    io.register_read(0x3C8, this, this.port3C8_read);\n    io.register_write(0x3C9, this, this.port3C9_write);\n    io.register_read(0x3C9, this, this.port3C9_read);\n\n    io.register_read(0x3CC, this, this.port3CC_read);\n\n    io.register_write(0x3D4, this, this.port3D4_write, this.port3D4_write16);\n    io.register_write(0x3D5, this, this.port3D5_write, this.port3D5_write16);\n\n    io.register_read(0x3D4, this, this.port3D4_read);\n    io.register_read(0x3D5, this, this.port3D5_read, this.port3D5_read16);\n\n    // use same handlers for monochrome text-mode's alternate port addresses 0x3B4/0x3B5 as for the regular addresses (0x3D4/0x3D5)\n    io.register_write(0x3B4, this, this.port3D4_write, this.port3D4_write16);\n    io.register_write(0x3B5, this, this.port3D5_write, this.port3D5_write16);\n\n    io.register_read(0x3B4, this, this.port3D4_read);\n    io.register_read(0x3B5, this, this.port3D5_read, this.port3D5_read16);\n\n    io.register_read(0x3CA, this, function() { dbg_log(\"3CA read\", LOG_VGA); return 0; });\n\n    // use same handler for monochrome text-mode's alternate port address 0x3BA as for its regular address (0x3DA)\n    io.register_read(0x3DA, this, this.port3DA_read);\n    io.register_read(0x3BA, this, this.port3DA_read);\n\n\n    // Bochs VBE Extensions\n    // http://wiki.osdev.org/Bochs_VBE_Extensions\n    this.dispi_index = -1;\n    this.dispi_enable_value = 0;\n\n    io.register_write(0x1CE, this, undefined, this.port1CE_write);\n\n    io.register_write(0x1CF, this, undefined, this.port1CF_write);\n    io.register_read(0x1CF, this, undefined, this.port1CF_read);\n\n\n    const vga_offset = cpu.svga_allocate_memory(this.vga_memory_size) >>> 0;\n    this.svga_memory = view(Uint8Array, cpu.wasm_memory, vga_offset, this.vga_memory_size);\n\n    this.diff_addr_min = this.vga_memory_size;\n    this.diff_addr_max = 0;\n    this.diff_plot_min = this.vga_memory_size;\n    this.diff_plot_max = 0;\n\n    this.image_data = null;\n\n    this.vga_memory = new Uint8Array(4 * VGA_BANK_SIZE);\n    this.plane0 = new Uint8Array(this.vga_memory.buffer, 0 * VGA_BANK_SIZE, VGA_BANK_SIZE);\n    this.plane1 = new Uint8Array(this.vga_memory.buffer, 1 * VGA_BANK_SIZE, VGA_BANK_SIZE);\n    this.plane2 = new Uint8Array(this.vga_memory.buffer, 2 * VGA_BANK_SIZE, VGA_BANK_SIZE);\n    this.plane3 = new Uint8Array(this.vga_memory.buffer, 3 * VGA_BANK_SIZE, VGA_BANK_SIZE);\n    this.pixel_buffer = new Uint8Array(VGA_PIXEL_BUFFER_SIZE);\n\n    io.mmap_register(0xA0000, 0x20000,\n        addr => this.vga_memory_read(addr),\n        (addr, value) => this.vga_memory_write(addr, value),\n    );\n\n    cpu.devices.pci.register_device(this);\n}\n\nVGAScreen.prototype.get_state = function()\n{\n    var state = [];\n\n    state[0] = this.vga_memory_size;\n    state[1] = this.cursor_address;\n    state[2] = this.cursor_scanline_start;\n    state[3] = this.cursor_scanline_end;\n    state[4] = this.max_cols;\n    state[5] = this.max_rows;\n    state[6] = this.vga_memory;\n    state[7] = this.dac_state;\n    state[8] = this.start_address;\n    state[9] = this.graphical_mode;\n    state[10] = this.vga256_palette;\n    state[11] = this.latch_dword;\n    state[12] = this.color_compare;\n    state[13] = this.color_dont_care;\n    state[14] = this.miscellaneous_graphics_register;\n    state[15] = this.svga_width;\n    state[16] = this.svga_height;\n    state[17] = this.crtc_mode;\n    state[18] = this.svga_enabled;\n    state[19] = this.svga_bpp;\n    state[20] = this.svga_bank_offset;\n    state[21] = this.svga_offset;\n    state[22] = this.index_crtc;\n    state[23] = this.dac_color_index_write;\n    state[24] = this.dac_color_index_read;\n    state[25] = this.dac_map;\n    state[26] = this.sequencer_index;\n    state[27] = this.plane_write_bm;\n    state[28] = this.sequencer_memory_mode;\n    state[29] = this.graphics_index;\n    state[30] = this.plane_read;\n    state[31] = this.planar_mode;\n    state[32] = this.planar_rotate_reg;\n    state[33] = this.planar_bitmap;\n    state[34] = this.max_scan_line;\n    state[35] = this.miscellaneous_output_register;\n    state[36] = this.port_3DA_value;\n    state[37] = this.dispi_index;\n    state[38] = this.dispi_enable_value;\n    state[39] = this.svga_memory;\n    // this.graphical_mode_is_linear\n    state[41] = this.attribute_controller_index;\n    state[42] = this.offset_register;\n    state[43] = this.planar_setreset;\n    state[44] = this.planar_setreset_enable;\n    state[45] = this.start_address_latched;\n    state[46] = this.crtc;\n    state[47] = this.horizontal_display_enable_end;\n    state[48] = this.horizontal_blank_start;\n    state[49] = this.vertical_display_enable_end;\n    state[50] = this.vertical_blank_start;\n    state[51] = this.underline_location_register;\n    state[52] = this.preset_row_scan;\n    state[53] = this.offset_register;\n    state[54] = this.palette_source;\n    state[55] = this.attribute_mode;\n    state[56] = this.color_plane_enable;\n    state[57] = this.horizontal_panning;\n    state[58] = this.color_select;\n    state[59] = this.clocking_mode;\n    state[60] = this.line_compare;\n    state[61] = this.pixel_buffer;\n    state[62] = this.dac_mask;\n    state[63] = this.character_map_select;\n    state[64] = this.font_page_ab_enabled;\n\n    return state;\n};\n\nVGAScreen.prototype.set_state = function(state)\n{\n    this.vga_memory_size = state[0];\n    this.cursor_address = state[1];\n    this.cursor_scanline_start = state[2];\n    this.cursor_scanline_end = state[3];\n    this.max_cols = state[4];\n    this.max_rows = state[5];\n    state[6] && this.vga_memory.set(state[6]);\n    this.dac_state = state[7];\n    this.start_address = state[8];\n    this.graphical_mode = state[9];\n    this.vga256_palette = state[10];\n    this.latch_dword = state[11];\n    this.color_compare = state[12];\n    this.color_dont_care = state[13];\n    this.miscellaneous_graphics_register = state[14];\n    this.svga_width = state[15];\n    this.svga_height = state[16];\n    this.crtc_mode = state[17];\n    this.svga_enabled = state[18];\n    this.svga_bpp = state[19];\n    this.svga_bank_offset = state[20];\n    this.svga_offset = state[21];\n    this.index_crtc = state[22];\n    this.dac_color_index_write = state[23];\n    this.dac_color_index_read = state[24];\n    this.dac_map = state[25];\n    this.sequencer_index = state[26];\n    this.plane_write_bm = state[27];\n    this.sequencer_memory_mode = state[28];\n    this.graphics_index = state[29];\n    this.plane_read = state[30];\n    this.planar_mode = state[31];\n    this.planar_rotate_reg = state[32];\n    this.planar_bitmap = state[33];\n    this.max_scan_line = state[34];\n    this.miscellaneous_output_register = state[35];\n    this.port_3DA_value = state[36];\n    this.dispi_index = state[37];\n    this.dispi_enable_value = state[38];\n    this.svga_memory.set(state[39]);\n    // state[40];\n    this.attribute_controller_index = state[41];\n    this.offset_register = state[42];\n    this.planar_setreset = state[43];\n    this.planar_setreset_enable = state[44];\n    this.start_address_latched = state[45];\n    this.crtc.set(state[46]);\n    this.horizontal_display_enable_end = state[47];\n    this.horizontal_blank_start = state[48];\n    this.vertical_display_enable_end = state[49];\n    this.vertical_blank_start = state[50];\n    this.underline_location_register = state[51];\n    this.preset_row_scan = state[52];\n    this.offset_register = state[53];\n    this.palette_source = state[54];\n    this.attribute_mode = state[55];\n    this.color_plane_enable = state[56];\n    this.horizontal_panning = state[57];\n    this.color_select = state[58];\n    this.clocking_mode = state[59];\n    this.line_compare = state[60];\n    state[61] && this.pixel_buffer.set(state[61]);\n    this.dac_mask = state[62] === undefined ? 0xFF : state[62];\n    this.character_map_select = state[63] === undefined ? 0 : state[63];\n    this.font_page_ab_enabled = state[64] === undefined ? 0 : state[64];\n\n    this.screen.set_mode(this.graphical_mode);\n\n    if(this.graphical_mode)\n    {\n        // Ensure set_size_graphical will update\n        this.screen_width = 0;\n        this.screen_height = 0;\n\n        if(this.svga_enabled)\n        {\n            this.set_size_graphical(this.svga_width, this.svga_height, this.svga_width, this.svga_height, this.svga_bpp);\n            this.update_layers();\n        }\n        else\n        {\n            this.update_vga_size();\n            this.update_layers();\n            this.complete_replot();\n        }\n    }\n    else\n    {\n        this.set_font_bitmap(true);\n        this.set_size_text(this.max_cols, this.max_rows);\n        this.set_font_page();\n        this.update_cursor_scanline();\n        this.update_cursor();\n    }\n    this.complete_redraw();\n};\n\nVGAScreen.prototype.vga_memory_read = function(addr)\n{\n    if(this.svga_enabled)\n    {\n        // vbe banked mode (accessing svga memory through the regular vga memory range)\n        return this.cpu.read8((addr - 0xA0000 | this.svga_bank_offset) + VGA_LFB_ADDRESS | 0);\n    }\n\n    var memory_space_select = this.miscellaneous_graphics_register >> 2 & 0x3;\n    addr -= VGA_HOST_MEMORY_SPACE_START[memory_space_select];\n\n    // VGA chip only decodes addresses within the selected memory space.\n    if(addr < 0 || addr >= VGA_HOST_MEMORY_SPACE_SIZE[memory_space_select])\n    {\n        dbg_log(\"vga read outside memory space: addr:\" + h(addr >>> 0), LOG_VGA);\n        return 0;\n    }\n\n    this.latch_dword = this.plane0[addr];\n    this.latch_dword |= this.plane1[addr] << 8;\n    this.latch_dword |= this.plane2[addr] << 16;\n    this.latch_dword |= this.plane3[addr] << 24;\n\n    if(this.planar_mode & 0x08)\n    {\n        // read mode 1\n        var reading = 0xFF;\n\n        if(this.color_dont_care & 0x1)\n        {\n            reading &= this.plane0[addr] ^ ~(this.color_compare & 0x1 ? 0xFF : 0x00);\n        }\n        if(this.color_dont_care & 0x2)\n        {\n            reading &= this.plane1[addr] ^ ~(this.color_compare & 0x2 ? 0xFF : 0x00);\n        }\n        if(this.color_dont_care & 0x4)\n        {\n            reading &= this.plane2[addr] ^ ~(this.color_compare & 0x4 ? 0xFF : 0x00);\n        }\n        if(this.color_dont_care & 0x8)\n        {\n            reading &= this.plane3[addr] ^ ~(this.color_compare & 0x8 ? 0xFF : 0x00);\n        }\n\n        return reading;\n    }\n    else\n    {\n        // read mode 0\n\n        var plane = this.plane_read;\n        if(!this.graphical_mode)\n        {\n            // We store all text data linearly and font data in plane 2.\n            // TODO: works well for planes 0 and 2, but what about plane 1?\n            plane &= 0x3;\n        }\n        else if(this.sequencer_memory_mode & 0x8)\n        {\n            // Chain 4\n            plane = addr & 0x3;\n            addr &= ~0x3;\n        }\n        else if(this.planar_mode & 0x10)\n        {\n            // Odd/Even host read\n            plane = addr & 0x1;\n            addr &= ~0x1;\n        }\n        return this.vga_memory[plane << 16 | addr];\n    }\n};\n\nVGAScreen.prototype.vga_memory_write = function(addr, value)\n{\n    if(this.svga_enabled)\n    {\n        // vbe banked mode (accessing svga memory through the regular vga memory range)\n        this.cpu.write8((addr - 0xA0000 | this.svga_bank_offset) + VGA_LFB_ADDRESS | 0, value);\n        return;\n    }\n\n    var memory_space_select = this.miscellaneous_graphics_register >> 2 & 0x3;\n    addr -= VGA_HOST_MEMORY_SPACE_START[memory_space_select];\n\n    if(addr < 0 || addr >= VGA_HOST_MEMORY_SPACE_SIZE[memory_space_select])\n    {\n        dbg_log(\"vga write outside memory space: addr:\" + h(addr >>> 0) + \", value:\" + h(value), LOG_VGA);\n        return;\n    }\n\n    if(this.graphical_mode)\n    {\n        this.vga_memory_write_graphical(addr, value);\n    }\n    else if(!(this.plane_write_bm & 0x3))\n    {\n        if(this.plane_write_bm & 0x4)\n        {\n            // write to plane 2 (font-bitmap)\n            this.plane2[addr] = value;\n        }\n    }\n    else\n    {\n        this.vga_memory_write_text_mode(addr, value);\n    }\n};\n\nVGAScreen.prototype.vga_memory_write_graphical = function(addr, value)\n{\n    var plane_dword;\n    var write_mode = this.planar_mode & 3;\n    var bitmask = this.apply_feed(this.planar_bitmap);\n    var setreset_dword = this.apply_expand(this.planar_setreset);\n    var setreset_enable_dword = this.apply_expand(this.planar_setreset_enable);\n\n    // Write modes - see http://www.osdever.net/FreeVGA/vga/graphreg.htm#05\n    switch(write_mode)\n    {\n        case 0:\n            value = this.apply_rotate(value);\n            plane_dword = this.apply_feed(value);\n            plane_dword = this.apply_setreset(plane_dword, setreset_enable_dword);\n            plane_dword = this.apply_logical(plane_dword, this.latch_dword);\n            plane_dword = this.apply_bitmask(plane_dword, bitmask);\n            break;\n        case 1:\n            plane_dword = this.latch_dword;\n            break;\n        case 2:\n            plane_dword = this.apply_expand(value);\n            plane_dword = this.apply_logical(plane_dword, this.latch_dword);\n            plane_dword = this.apply_bitmask(plane_dword, bitmask);\n            break;\n        case 3:\n            value = this.apply_rotate(value);\n            bitmask &= this.apply_feed(value);\n            plane_dword = setreset_dword;\n            plane_dword = this.apply_bitmask(plane_dword, bitmask);\n            break;\n    }\n\n    var plane_select = 0xF;\n\n    switch(this.sequencer_memory_mode & 0xC)\n    {\n        // Odd/Even (aka chain 2)\n        case 0x0:\n            plane_select = 0x5 << (addr & 0x1);\n            addr &= ~0x1;\n            break;\n\n        // Chain 4\n        // Note: FreeVGA may have mistakenly stated that this bit field is\n        // for system read only, yet the IBM Open Source Graphics Programmer's\n        // Reference Manual explicitly states \"both read and write\".\n        case 0x8:\n        case 0xC:\n            plane_select = 1 << (addr & 0x3);\n            addr &= ~0x3;\n            break;\n    }\n\n    // Plane masks take precedence\n    // See: http://www.osdever.net/FreeVGA/vga/seqreg.htm#02\n    plane_select &= this.plane_write_bm;\n\n    if(plane_select & 0x1) this.plane0[addr] = (plane_dword >> 0) & 0xFF;\n    if(plane_select & 0x2) this.plane1[addr] = (plane_dword >> 8) & 0xFF;\n    if(plane_select & 0x4) this.plane2[addr] = (plane_dword >> 16) & 0xFF;\n    if(plane_select & 0x8) this.plane3[addr] = (plane_dword >> 24) & 0xFF;\n\n    var pixel_addr = this.vga_addr_to_pixel(addr);\n    this.partial_replot(pixel_addr, pixel_addr + 7);\n};\n\n/**\n * Copies data_byte into the four planes, with each plane\n * represented by an 8-bit field inside the dword.\n * @param {number} data_byte\n * @return {number} 32-bit number representing the bytes for each plane.\n */\nVGAScreen.prototype.apply_feed = function(data_byte)\n{\n    var dword = data_byte;\n    dword |= data_byte << 8;\n    dword |= data_byte << 16;\n    dword |= data_byte << 24;\n    return dword;\n};\n\n/**\n * Expands bits 0 to 3 to ocupy bits 0 to 31. Each\n * bit is expanded to 0xFF if set or 0x00 if clear.\n * @param {number} data_byte\n * @return {number} 32-bit number representing the bytes for each plane.\n */\nVGAScreen.prototype.apply_expand = function(data_byte)\n{\n    var dword = data_byte & 0x1 ? 0xFF : 0x00;\n    dword |= (data_byte & 0x2 ? 0xFF : 0x00) << 8;\n    dword |= (data_byte & 0x4 ? 0xFF : 0x00) << 16;\n    dword |= (data_byte & 0x8 ? 0xFF : 0x00) << 24;\n    return dword;\n};\n\n/**\n * Planar Write - Barrel Shifter\n * @param {number} data_byte\n * @return {number}\n * @see {@link http://www.phatcode.net/res/224/files/html/ch25/25-01.html#Heading3}\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm#03}\n */\nVGAScreen.prototype.apply_rotate = function(data_byte)\n{\n    var wrapped = data_byte | (data_byte << 8);\n    var count = this.planar_rotate_reg & 0x7;\n    var shifted = wrapped >>> count;\n    return shifted & 0xFF;\n};\n\n/**\n * Planar Write - Set / Reset Circuitry\n * @param {number} data_dword\n * @param {number} enable_dword\n * @return {number}\n * @see {@link http://www.phatcode.net/res/224/files/html/ch25/25-03.html#Heading5}\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm#00}\n */\nVGAScreen.prototype.apply_setreset = function(data_dword, enable_dword)\n{\n    var setreset_dword = this.apply_expand(this.planar_setreset);\n    data_dword |= enable_dword & setreset_dword;\n    data_dword &= ~enable_dword | setreset_dword;\n    return data_dword;\n};\n\n/**\n * Planar Write - ALU Unit\n * @param {number} data_dword\n * @param {number} latch_dword\n * @return {number}\n * @see {@link http://www.phatcode.net/res/224/files/html/ch24/24-01.html#Heading3}\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm#03}\n */\nVGAScreen.prototype.apply_logical = function(data_dword, latch_dword)\n{\n    switch(this.planar_rotate_reg & 0x18)\n    {\n        case 0x08:\n            return data_dword & latch_dword;\n        case 0x10:\n            return data_dword | latch_dword;\n        case 0x18:\n            return data_dword ^ latch_dword;\n    }\n    return data_dword;\n};\n\n/**\n * Planar Write - Bitmask Unit\n * @param {number} data_dword\n * @param {number} bitmask_dword\n * @return {number}\n * @see {@link http://www.phatcode.net/res/224/files/html/ch25/25-01.html#Heading2}\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm#08}\n */\nVGAScreen.prototype.apply_bitmask = function(data_dword, bitmask_dword)\n{\n    var plane_dword = bitmask_dword & data_dword;\n    plane_dword |= ~bitmask_dword & this.latch_dword;\n    return plane_dword;\n};\n\nVGAScreen.prototype.text_mode_redraw = function()\n{\n    const split_screen_row = this.scan_line_to_screen_row(this.line_compare);\n    const row_offset = Math.max(0, (this.offset_register * 2 - this.max_cols) * 2);\n    const blink_enabled = this.attribute_mode & 1 << 3;\n    const fg_color_mask = this.font_page_ab_enabled ? 7 : 0xF;\n    const bg_color_mask = blink_enabled ? 7 : 0xF;\n    const FLAG_BLINKING = this.screen.FLAG_BLINKING;\n    const FLAG_FONT_PAGE_B = this.screen.FLAG_FONT_PAGE_B;\n\n    let addr = this.start_address << 1;\n\n    for(let row = 0; row < this.max_rows; row++)\n    {\n        if(row === split_screen_row)\n        {\n            addr = 0;\n        }\n\n        for(let col = 0; col < this.max_cols; col++)\n        {\n            const chr = this.vga_memory[addr];\n            const color = this.vga_memory[addr | 1];\n            const blinking = blink_enabled && (color & 1 << 7);\n            const font_page_b = this.font_page_ab_enabled && !(color & 1 << 3);\n            const flags = (blinking ? FLAG_BLINKING : 0) | (font_page_b ? FLAG_FONT_PAGE_B : 0);\n\n            this.bus.send(\"screen-put-char\", [row, col, chr]);\n\n            this.screen.put_char(row, col, chr, flags,\n                this.vga256_palette[this.dac_mask & this.dac_map[color >> 4 & bg_color_mask]],\n                this.vga256_palette[this.dac_mask & this.dac_map[color & fg_color_mask]]);\n\n            addr += 2;\n        }\n\n        addr += row_offset;\n    }\n};\n\nVGAScreen.prototype.vga_memory_write_text_mode = function(addr, value)\n{\n    this.vga_memory[addr] = value;\n\n    const max_cols = Math.max(this.max_cols, this.offset_register * 2);\n    let row;\n    let col;\n\n    if((addr >> 1) >= this.start_address)\n    {\n        const memory_start = (addr >> 1) - this.start_address;\n        row = memory_start / max_cols | 0;\n        col = memory_start % max_cols;\n    }\n    else\n    {\n        const memory_start = addr >> 1;\n        row = (memory_start / max_cols | 0) + this.scan_line_to_screen_row(this.line_compare);\n        col = memory_start % max_cols;\n    }\n\n    dbg_assert(row >= 0 && col >= 0);\n\n    if(col >= this.max_cols || row >= this.max_rows)\n    {\n        return;\n    }\n\n    let chr;\n    let color;\n\n    // XXX: Should handle 16 bit write if possible\n    if(addr & 1)\n    {\n        color = value;\n        chr = this.vga_memory[addr & ~1];\n    }\n    else\n    {\n        chr = value;\n        color = this.vga_memory[addr | 1];\n    }\n    const blink_enabled = this.attribute_mode & 1 << 3;\n    const blinking = blink_enabled && (color & 1 << 7);\n    const font_page_b = this.font_page_ab_enabled && !(color & 1 << 3);\n    const flags = (blinking ? this.screen.FLAG_BLINKING : 0) | (font_page_b ? this.screen.FLAG_FONT_PAGE_B : 0);\n    const fg_color_mask = this.font_page_ab_enabled ? 7 : 0xF;\n    const bg_color_mask = blink_enabled ? 7 : 0xF;\n\n    this.bus.send(\"screen-put-char\", [row, col, chr]);\n\n    this.screen.put_char(row, col, chr, flags,\n        this.vga256_palette[this.dac_mask & this.dac_map[color >> 4 & bg_color_mask]],\n        this.vga256_palette[this.dac_mask & this.dac_map[color & fg_color_mask]]);\n};\n\nVGAScreen.prototype.update_cursor = function()\n{\n    const max_cols = Math.max(this.max_cols, this.offset_register * 2);\n    let row;\n    let col;\n\n    if(this.cursor_address >= this.start_address)\n    {\n        row = (this.cursor_address - this.start_address) / max_cols | 0;\n        col = (this.cursor_address - this.start_address) % max_cols;\n    }\n    else\n    {\n        row = (this.cursor_address / max_cols | 0) + this.scan_line_to_screen_row(this.line_compare);\n        col = this.cursor_address % max_cols;\n    }\n\n    dbg_assert(row >= 0 && col >= 0);\n\n    // NOTE: is allowed to be out of bounds\n    this.screen.update_cursor(row, col);\n};\n\nVGAScreen.prototype.complete_redraw = function()\n{\n    dbg_log(\"complete redraw\", LOG_VGA);\n\n    if(this.graphical_mode)\n    {\n        if(this.svga_enabled)\n        {\n            this.cpu.svga_mark_dirty();\n        }\n        else\n        {\n            this.diff_addr_min = 0;\n            this.diff_addr_max = VGA_PIXEL_BUFFER_SIZE;\n        }\n    }\n    else\n    {\n        this.text_mode_redraw();\n    }\n};\n\nVGAScreen.prototype.complete_replot = function()\n{\n    dbg_log(\"complete replot\", LOG_VGA);\n\n    if(!this.graphical_mode || this.svga_enabled)\n    {\n        return;\n    }\n\n    this.diff_plot_min = 0;\n    this.diff_plot_max = VGA_PIXEL_BUFFER_SIZE;\n\n    this.complete_redraw();\n};\n\nVGAScreen.prototype.partial_redraw = function(min, max)\n{\n    if(min < this.diff_addr_min) this.diff_addr_min = min;\n    if(max > this.diff_addr_max) this.diff_addr_max = max;\n};\n\nVGAScreen.prototype.partial_replot = function(min, max)\n{\n    if(min < this.diff_plot_min) this.diff_plot_min = min;\n    if(max > this.diff_plot_max) this.diff_plot_max = max;\n\n    this.partial_redraw(min, max);\n};\n\nVGAScreen.prototype.reset_diffs = function()\n{\n    this.diff_addr_min = this.vga_memory_size;\n    this.diff_addr_max = 0;\n    this.diff_plot_min = this.vga_memory_size;\n    this.diff_plot_max = 0;\n};\n\nVGAScreen.prototype.destroy = function()\n{\n\n};\n\nVGAScreen.prototype.vga_bytes_per_line = function()\n{\n    var bytes_per_line = this.offset_register << 2;\n    if(this.underline_location_register & 0x40) bytes_per_line <<= 1;\n    else if(this.crtc_mode & 0x40) bytes_per_line >>>= 1;\n    return bytes_per_line;\n};\n\nVGAScreen.prototype.vga_addr_shift_count = function()\n{\n    // Count in multiples of 0x40 for convenience\n    // Left shift 2 for word mode - 2 bytes per dot clock\n    var shift_count = 0x80;\n\n    // Left shift 3 for byte mode - 1 byte per dot clock\n    shift_count += ~this.underline_location_register & this.crtc_mode & 0x40;\n\n    // Left shift 1 for doubleword mode - 4 bytes per dot clock\n    shift_count -= this.underline_location_register & 0x40;\n\n    // But shift one less if PEL width mode - 2 dot clocks per pixel\n    shift_count -= this.attribute_mode & 0x40;\n\n    return shift_count >>> 6;\n};\n\nVGAScreen.prototype.vga_addr_to_pixel = function(addr)\n{\n    var shift_count = this.vga_addr_shift_count();\n\n    // Undo effects of substituted bits 13 and 14\n    // Assumptions:\n    //  - max_scan_line register is set to the values shown below\n    //  - Each scan line stays within the offset alignment\n    //  - No panning and no page flipping after drawing\n    if(~this.crtc_mode & 0x3)\n    {\n        var pixel_addr = addr - this.start_address;\n\n        // Remove substituted bits\n        pixel_addr &= this.crtc_mode << 13 | ~0x6000;\n\n        // Convert to 1 pixel per address\n        pixel_addr <<= shift_count;\n\n        // Decompose address\n        var row = pixel_addr / this.virtual_width | 0;\n        var col = pixel_addr % this.virtual_width;\n\n        switch(this.crtc_mode & 0x3)\n        {\n            case 0x2:\n                // Alternating rows using bit 13\n                // Assumes max scan line = 1\n                row = row << 1 | (addr >> 13 & 0x1);\n                break;\n            case 0x1:\n                // Alternating rows using bit 14\n                // Assumes max scan line = 3\n                row = row << 1 | (addr >> 14 & 0x1);\n                break;\n            case 0x0:\n                // Cycling through rows using bit 13 and 14\n                // Assumes max scan line = 3\n                row = row << 2 | (addr >> 13 & 0x3);\n                break;\n        }\n\n        // Reassemble address\n        return row * this.virtual_width + col + (this.start_address << shift_count);\n    }\n    else\n    {\n        // Convert to 1 pixel per address\n        return addr << shift_count;\n    }\n};\n\nVGAScreen.prototype.scan_line_to_screen_row = function(scan_line)\n{\n    // Double scanning. The clock to the row scan counter is halved\n    // so it is not affected by the memory address bit substitutions below\n    if(this.max_scan_line & 0x80)\n    {\n        scan_line >>>= 1;\n    }\n\n    // Maximum scan line, aka scan lines per character row\n    // This is the number of repeats - 1 for graphic modes\n    var repeat_factor = 1 + (this.max_scan_line & 0x1F);\n    scan_line = Math.ceil(scan_line / repeat_factor);\n\n    // Odd and Even Row Scan Counter\n    // Despite repeated address counter values, because bit 13 of the shifted\n    // address is substituted with bit 0 of the row scan counter, a different\n    // display buffer address is generated instead of repeated\n    // Assumes maximum scan line register is set to 2 or 4.\n    // Note: can't assert this as register values may not be fully programmed.\n    if(!(this.crtc_mode & 0x1))\n    {\n        scan_line <<= 1;\n    }\n\n    // Undo effects of substituted bit 14\n    // Assumes maximum scan line register is set to 2 or 4\n    // Note: can't assert this as register values may not be fully programmed.\n    // Other maximum scan line register values would result in weird addressing\n    // anyway\n    if(!(this.crtc_mode & 0x2))\n    {\n        scan_line <<= 1;\n    }\n\n    return scan_line;\n};\n\n/**\n * @param {number} cols_count\n * @param {number} rows_count\n */\nVGAScreen.prototype.set_size_text = function(cols_count, rows_count)\n{\n    dbg_assert(!this.graphical_mode);\n    this.max_cols = cols_count;\n    this.max_rows = rows_count;\n\n    this.screen.set_size_text(cols_count, rows_count);\n    this.bus.send(\"screen-set-size\", [cols_count, rows_count, 0]);\n};\n\nVGAScreen.prototype.set_size_graphical = function(width, height, virtual_width, virtual_height, bpp)\n{\n    dbg_assert(this.graphical_mode);\n\n    virtual_width = Math.max(virtual_width, 1);\n    virtual_height = Math.max(virtual_height, 1);\n\n    const needs_update =\n        this.screen_width !== width ||\n        this.screen_height !== height ||\n        this.virtual_width !== virtual_width ||\n        this.virtual_height !== virtual_height;\n\n    if(needs_update)\n    {\n        this.screen_width = width;\n        this.screen_height = height;\n        this.virtual_width = virtual_width;\n        this.virtual_height = virtual_height;\n\n        if(typeof ImageData !== \"undefined\")\n        {\n            const size = virtual_width * virtual_height;\n            const offset = this.cpu.svga_allocate_dest_buffer(size) >>> 0;\n\n            this.dest_buffet_offset = offset;\n            this.image_data = new ImageData(new Uint8ClampedArray(this.cpu.wasm_memory.buffer, offset, 4 * size), virtual_width, virtual_height);\n\n            this.cpu.svga_mark_dirty();\n        }\n        else\n        {\n            // TODO: nodejs\n        }\n\n        this.screen.set_size_graphical(width, height, virtual_width, virtual_height);\n        this.bus.send(\"screen-set-size\", [width, height, bpp]);\n    }\n};\n\nVGAScreen.prototype.update_vga_size = function()\n{\n    if(this.svga_enabled)\n    {\n        return;\n    }\n\n    var horizontal_characters = Math.min(1 + this.horizontal_display_enable_end,\n        this.horizontal_blank_start);\n    var vertical_scans = Math.min(1 + this.vertical_display_enable_end,\n        this.vertical_blank_start);\n\n    if(!horizontal_characters || !vertical_scans)\n    {\n        // Don't update if width or height is zero.\n        // These happen when registers are not fully configured yet.\n        return;\n    }\n\n    if(this.graphical_mode)\n    {\n        var screen_width = horizontal_characters << 3;\n\n        // Offset is half the number of bytes/words/dwords (depending on clocking mode)\n        // of display memory that each logical line occupies.\n        // However, the number of pixels latched, regardless of addressing mode,\n        // should always 8 pixels per character clock (except for 8 bit PEL width, in which\n        // case 4 pixels).\n        var virtual_width = this.offset_register << 4;\n        var bpp = 4;\n\n        // Pixel Width / PEL Width / Clock Select\n        if(this.attribute_mode & 0x40)\n        {\n            screen_width >>>= 1;\n            virtual_width >>>= 1;\n            bpp = 8;\n        }\n        else if(this.attribute_mode & 0x2)\n        {\n            bpp = 1;\n        }\n\n        var screen_height = this.scan_line_to_screen_row(vertical_scans);\n\n        // The virtual buffer height is however many rows of data that can fit.\n        // Previously drawn graphics outside of current memory address space can\n        // still be drawn by setting start_address. The address at\n        // VGA_HOST_MEMORY_SPACE_START[memory_space_select] is mapped to the first\n        // byte of the frame buffer. Verified on some hardware.\n        // Depended on by: Windows 98 start screen\n        var available_bytes = VGA_HOST_MEMORY_SPACE_SIZE[0];\n\n        const bytes_per_line = this.vga_bytes_per_line();\n        const virtual_height = bytes_per_line ? Math.ceil(available_bytes / bytes_per_line) : screen_height;\n\n        this.set_size_graphical(screen_width, screen_height, virtual_width, virtual_height, bpp);\n\n        this.update_vertical_retrace();\n        this.update_layers();\n    }\n    else\n    {\n        if(this.max_scan_line & 0x80)\n        {\n            // Double scanning means that half of those scan lines\n            // are just repeats\n            vertical_scans >>>= 1;\n        }\n\n        var height = vertical_scans / (1 + (this.max_scan_line & 0x1F)) | 0;\n\n        if(horizontal_characters && height)\n        {\n            this.set_size_text(horizontal_characters, height);\n        }\n    }\n};\n\nVGAScreen.prototype.update_layers = function()\n{\n    if(!this.graphical_mode)\n    {\n        this.text_mode_redraw();\n    }\n\n    if(this.svga_enabled)\n    {\n        this.layers = [];\n        return;\n    }\n\n    if(!this.virtual_width || !this.screen_width)\n    {\n        // Avoid division by zero\n        return;\n    }\n\n    if(!this.palette_source || (this.clocking_mode & 0x20))\n    {\n        // Palette source and screen disable bits = draw nothing\n        // See http://www.phatcode.net/res/224/files/html/ch29/29-05.html#Heading6\n        // and http://www.osdever.net/FreeVGA/vga/seqreg.htm#01\n        this.layers = [];\n        this.screen.clear_screen();\n        return;\n    }\n\n    var start_addr = this.start_address_latched;\n\n    var pixel_panning = this.horizontal_panning;\n    if(this.attribute_mode & 0x40)\n    {\n        pixel_panning >>>= 1;\n    }\n\n    var byte_panning = this.preset_row_scan >> 5 & 0x3;\n    var pixel_addr_start = this.vga_addr_to_pixel(start_addr + byte_panning);\n\n    var start_buffer_row = pixel_addr_start / this.virtual_width | 0;\n    var start_buffer_col = pixel_addr_start % this.virtual_width + pixel_panning;\n\n    var split_screen_row = this.scan_line_to_screen_row(1 + this.line_compare);\n    split_screen_row = Math.min(split_screen_row, this.screen_height);\n\n    var split_buffer_height = this.screen_height - split_screen_row;\n\n    this.layers = [];\n\n    for(var x = -start_buffer_col, y = 0; x < this.screen_width; x += this.virtual_width, y++)\n    {\n        this.layers.push({\n            image_data: this.image_data,\n            screen_x: x,\n            screen_y: 0,\n            buffer_x: 0,\n            buffer_y: start_buffer_row + y,\n            buffer_width: this.virtual_width,\n            buffer_height: split_screen_row,\n        });\n    }\n\n    var start_split_col = 0;\n    if(!(this.attribute_mode & 0x20))\n    {\n        // Pixel panning mode. Allow panning for the lower split screen\n        start_split_col = this.vga_addr_to_pixel(byte_panning) + pixel_panning;\n    }\n\n    for(var x = -start_split_col, y = 0; x < this.screen_width; x += this.virtual_width, y++)\n    {\n        this.layers.push({\n            image_data: this.image_data,\n            screen_x: x,\n            screen_y: split_screen_row,\n            buffer_x: 0,\n            buffer_y: y,\n            buffer_width: this.virtual_width,\n            buffer_height: split_buffer_height,\n        });\n    }\n};\n\nVGAScreen.prototype.update_vertical_retrace = function()\n{\n    // Emulate behaviour during VSync/VRetrace\n    this.port_3DA_value |= 0x8;\n    if(this.start_address_latched !== this.start_address)\n    {\n        this.start_address_latched = this.start_address;\n        this.update_layers();\n    }\n};\n\nVGAScreen.prototype.update_cursor_scanline = function()\n{\n    const disabled = this.cursor_scanline_start & 0x20;\n    const max = this.max_scan_line & 0x1F;\n    const start = Math.min(max, this.cursor_scanline_start & 0x1F);\n    const end = Math.min(max, this.cursor_scanline_end & 0x1F);\n    const visible = !disabled && start < end;\n    this.screen.update_cursor_scanline(start, end, visible);\n};\n\n/**\n * Attribute controller register / index write\n * @see {@link http://www.osdever.net/FreeVGA/vga/attrreg.htm}\n * @see {@link http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf} page 89\n * @see {@link https://01.org/sites/default/files/documentation/intel-gfx-prm-osrc-hsw-display_0.pdf} page 48\n */\nVGAScreen.prototype.port3C0_write = function(value)\n{\n    if(this.attribute_controller_index === -1)\n    {\n        dbg_log(\"attribute controller index register: \" + h(value), LOG_VGA);\n        this.attribute_controller_index = value & 0x1F;\n        dbg_log(\"attribute actual index: \" + h(this.attribute_controller_index), LOG_VGA);\n\n        if(this.palette_source !== (value & 0x20))\n        {\n            // A method of blanking the screen.\n            // See http://www.phatcode.net/res/224/files/html/ch29/29-05.html#Heading6\n            this.palette_source = value & 0x20;\n            this.update_layers();\n        }\n    }\n    else\n    {\n        if(this.attribute_controller_index < 0x10)\n        {\n            dbg_log(\"internal palette: \" + h(this.attribute_controller_index) + \" -> \" + h(value), LOG_VGA);\n            this.dac_map[this.attribute_controller_index] = value;\n\n            if(!(this.attribute_mode & 0x40))\n            {\n                this.complete_redraw();\n            }\n        }\n        else\n        switch(this.attribute_controller_index)\n        {\n            case 0x10:\n                dbg_log(\"3C0 / attribute mode control: \" + h(value), LOG_VGA);\n                if(this.attribute_mode !== value)\n                {\n                    var previous_mode = this.attribute_mode;\n                    this.attribute_mode = value;\n\n                    const is_graphical = (value & 0x1) !== 0;\n                    if(!this.svga_enabled && this.graphical_mode !== is_graphical)\n                    {\n                        this.graphical_mode = is_graphical;\n                        this.screen.set_mode(this.graphical_mode);\n                    }\n\n                    if((previous_mode ^ value) & 0x40)\n                    {\n                        // PEL width changed. Pixel Buffer now invalidated\n                        this.complete_replot();\n                    }\n\n                    this.update_vga_size();\n\n                    // Data stored in image buffer are invalidated\n                    this.complete_redraw();\n\n                    this.set_font_bitmap(false);\n                }\n                break;\n            case 0x12:\n                dbg_log(\"3C0 / color plane enable: \" + h(value), LOG_VGA);\n                if(this.color_plane_enable !== value)\n                {\n                    this.color_plane_enable = value;\n\n                    // Data stored in image buffer are invalidated\n                    this.complete_redraw();\n                }\n                break;\n            case 0x13:\n                dbg_log(\"3C0 / horizontal panning: \" + h(value), LOG_VGA);\n                if(this.horizontal_panning !== value)\n                {\n                    this.horizontal_panning = value & 0xF;\n                    this.update_layers();\n                }\n                break;\n            case 0x14:\n                dbg_log(\"3C0 / color select: \" + h(value), LOG_VGA);\n                if(this.color_select !== value)\n                {\n                    this.color_select = value;\n\n                    // Data stored in image buffer are invalidated\n                    this.complete_redraw();\n                }\n                break;\n            default:\n                dbg_log(\"3C0 / attribute controller write \" + h(this.attribute_controller_index) + \": \" + h(value), LOG_VGA);\n        }\n\n        this.attribute_controller_index = -1;\n    }\n};\n\nVGAScreen.prototype.port3C0_read = function()\n{\n    dbg_log(\"3C0 read\", LOG_VGA);\n    return (this.attribute_controller_index | this.palette_source) & 0xFF;\n};\n\nVGAScreen.prototype.port3C0_read16 = function()\n{\n    dbg_log(\"3C0 read16\", LOG_VGA);\n    return this.port3C0_read() | this.port3C1_read() << 8 & 0xFF00;\n};\n\nVGAScreen.prototype.port3C1_read = function()\n{\n    if(this.attribute_controller_index < 0x10)\n    {\n        dbg_log(\"3C1 / internal palette read: \" + h(this.attribute_controller_index) +\n            \" -> \" + h(this.dac_map[this.attribute_controller_index]), LOG_VGA);\n        return this.dac_map[this.attribute_controller_index] & 0xFF;\n    }\n\n    switch(this.attribute_controller_index)\n    {\n        case 0x10:\n            dbg_log(\"3C1 / attribute mode read: \" + h(this.attribute_mode), LOG_VGA);\n            return this.attribute_mode;\n        case 0x12:\n            dbg_log(\"3C1 / color plane enable read: \" + h(this.color_plane_enable), LOG_VGA);\n            return this.color_plane_enable;\n        case 0x13:\n            dbg_log(\"3C1 / horizontal panning read: \" + h(this.horizontal_panning), LOG_VGA);\n            return this.horizontal_panning;\n        case 0x14:\n            dbg_log(\"3C1 / color select read: \" + h(this.color_select), LOG_VGA);\n            return this.color_select;\n        default:\n            dbg_log(\"3C1 / attribute controller read \" + h(this.attribute_controller_index), LOG_VGA);\n    }\n    return 0xFF;\n\n};\n\nVGAScreen.prototype.port3C2_write = function(value)\n{\n    dbg_log(\"3C2 / miscellaneous output register = \" + h(value), LOG_VGA);\n    this.miscellaneous_output_register = value;\n};\n\nVGAScreen.prototype.port3C4_write = function(value)\n{\n    this.sequencer_index = value;\n};\n\nVGAScreen.prototype.port3C4_read = function()\n{\n    return this.sequencer_index;\n};\n\n/**\n * Sequencer register writes\n * @see {@link http://www.osdever.net/FreeVGA/vga/seqreg.htm}\n * @see {@link http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf} page 47\n * @see {@link https://01.org/sites/default/files/documentation/intel-gfx-prm-osrc-hsw-display_0.pdf} page 19\n */\nVGAScreen.prototype.port3C5_write = function(value)\n{\n    switch(this.sequencer_index)\n    {\n        case 0x01:\n            dbg_log(\"clocking mode: \" + h(value), LOG_VGA);\n            var previous_clocking_mode = this.clocking_mode;\n            this.clocking_mode = value;\n            if((previous_clocking_mode ^ value) & 0x20)\n            {\n                // Screen disable bit modified\n                this.update_layers();\n            }\n            this.set_font_bitmap(false);\n            break;\n        case 0x02:\n            dbg_log(\"plane write mask: \" + h(value), LOG_VGA);\n            var previous_plane_write_bm = this.plane_write_bm;\n            this.plane_write_bm = value;\n            if(!this.graphical_mode && previous_plane_write_bm & 0x4 && !(this.plane_write_bm & 0x4))\n            {\n                // End of font plane 2 write access\n                this.set_font_bitmap(true);\n            }\n            break;\n        case 0x03:\n            dbg_log(\"character map select: \" + h(value), LOG_VGA);\n            var previous_character_map_select = this.character_map_select;\n            this.character_map_select = value;\n            if(!this.graphical_mode && previous_character_map_select !== value)\n            {\n                this.set_font_page();\n            }\n            break;\n        case 0x04:\n            dbg_log(\"sequencer memory mode: \" + h(value), LOG_VGA);\n            this.sequencer_memory_mode = value;\n            break;\n        default:\n            dbg_log(\"3C5 / sequencer write \" + h(this.sequencer_index) + \": \" + h(value), LOG_VGA);\n    }\n};\n\nVGAScreen.prototype.port3C5_read = function()\n{\n    dbg_log(\"3C5 / sequencer read \" + h(this.sequencer_index), LOG_VGA);\n\n    switch(this.sequencer_index)\n    {\n        case 0x01:\n            return this.clocking_mode;\n        case 0x02:\n            return this.plane_write_bm;\n        case 0x03:\n            return this.character_map_select;\n        case 0x04:\n            return this.sequencer_memory_mode;\n        case 0x06:\n            return 0x12;\n        default:\n    }\n    return 0;\n};\n\nVGAScreen.prototype.port3C6_write = function(data)\n{\n    if(this.dac_mask !== data)\n    {\n        this.dac_mask = data;\n        this.complete_redraw();\n    }\n};\n\nVGAScreen.prototype.port3C6_read = function()\n{\n    return this.dac_mask;\n};\n\nVGAScreen.prototype.port3C7_write = function(index)\n{\n    // index for reading the DAC\n    dbg_log(\"3C7 write: \" + h(index), LOG_VGA);\n    this.dac_color_index_read = index * 3;\n    this.dac_state &= 0x0;\n};\n\nVGAScreen.prototype.port3C7_read = function()\n{\n    // prepared to accept reads or writes\n    return this.dac_state;\n};\n\nVGAScreen.prototype.port3C8_write = function(index)\n{\n    this.dac_color_index_write = index * 3;\n    this.dac_state |= 0x3;\n};\n\nVGAScreen.prototype.port3C8_read = function()\n{\n    return this.dac_color_index_write / 3 & 0xFF;\n};\n\n/**\n * DAC color palette register writes\n * @see {@link http://www.osdever.net/FreeVGA/vga/colorreg.htm}\n * @see {@link http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf} page 104\n * @see {@link https://01.org/sites/default/files/documentation/intel-gfx-prm-osrc-hsw-display_0.pdf} page 57\n */\nVGAScreen.prototype.port3C9_write = function(color_byte)\n{\n    var index = this.dac_color_index_write / 3 | 0,\n        offset = this.dac_color_index_write % 3,\n        color = this.vga256_palette[index];\n\n    if((this.dispi_enable_value & 0x20) === 0)\n    {\n        color_byte &= 0x3F;\n        const b = color_byte & 1;\n        color_byte = color_byte << 2 | b << 1 | b;\n    }\n\n    if(offset === 0)\n    {\n        color = color & ~0xFF0000 | color_byte << 16;\n    }\n    else if(offset === 1)\n    {\n        color = color & ~0xFF00 | color_byte << 8;\n    }\n    else\n    {\n        color = color & ~0xFF | color_byte;\n        dbg_log(\"dac set color, index=\" + h(index) + \" value=\" + h(color), LOG_VGA);\n    }\n\n    if(this.vga256_palette[index] !== color)\n    {\n        this.vga256_palette[index] = color;\n        this.complete_redraw();\n    }\n    this.dac_color_index_write++;\n};\n\nVGAScreen.prototype.port3C9_read = function()\n{\n    dbg_log(\"3C9 read\", LOG_VGA);\n\n    var index = this.dac_color_index_read / 3 | 0;\n    var offset = this.dac_color_index_read % 3;\n    var color = this.vga256_palette[index];\n    var color8 = color >> (2 - offset) * 8 & 0xFF;\n\n    this.dac_color_index_read++;\n\n    if(this.dispi_enable_value & 0x20)\n    {\n        return color8;\n    }\n    else\n    {\n        return color8 >> 2;\n    }\n};\n\nVGAScreen.prototype.port3CC_read = function()\n{\n    dbg_log(\"3CC read\", LOG_VGA);\n    return this.miscellaneous_output_register;\n};\n\nVGAScreen.prototype.port3CE_write = function(value)\n{\n    this.graphics_index = value;\n};\n\nVGAScreen.prototype.port3CE_read = function()\n{\n    return this.graphics_index;\n};\n\n/**\n * Graphics controller register writes\n * @see {@link http://www.osdever.net/FreeVGA/vga/graphreg.htm}\n * @see {@link http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf} page 78\n * @see {@link https://01.org/sites/default/files/documentation/intel-gfx-prm-osrc-hsw-display_0.pdf} page 29\n */\nVGAScreen.prototype.port3CF_write = function(value)\n{\n    switch(this.graphics_index)\n    {\n        case 0:\n            this.planar_setreset = value;\n            dbg_log(\"plane set/reset: \" + h(value), LOG_VGA);\n            break;\n        case 1:\n            this.planar_setreset_enable = value;\n            dbg_log(\"plane set/reset enable: \" + h(value), LOG_VGA);\n            break;\n        case 2:\n            this.color_compare = value;\n            dbg_log(\"color compare: \" + h(value), LOG_VGA);\n            break;\n        case 3:\n            this.planar_rotate_reg = value;\n            dbg_log(\"plane rotate: \" + h(value), LOG_VGA);\n            break;\n        case 4:\n            this.plane_read = value;\n            dbg_log(\"plane read: \" + h(value), LOG_VGA);\n            break;\n        case 5:\n            var previous_planar_mode = this.planar_mode;\n            this.planar_mode = value;\n            dbg_log(\"planar mode: \" + h(value), LOG_VGA);\n            if((previous_planar_mode ^ value) & 0x60)\n            {\n                // Shift mode modified. Pixel buffer invalidated\n                this.complete_replot();\n            }\n            break;\n        case 6:\n            dbg_log(\"miscellaneous graphics register: \" + h(value), LOG_VGA);\n            if(this.miscellaneous_graphics_register !== value)\n            {\n                this.miscellaneous_graphics_register = value;\n                this.update_vga_size();\n            }\n            break;\n        case 7:\n            this.color_dont_care = value;\n            dbg_log(\"color don't care: \" + h(value), LOG_VGA);\n            break;\n        case 8:\n            this.planar_bitmap = value;\n            dbg_log(\"planar bitmap: \" + h(value), LOG_VGA);\n            break;\n        default:\n            dbg_log(\"3CF / graphics write \" + h(this.graphics_index) + \": \" + h(value), LOG_VGA);\n    }\n};\n\nVGAScreen.prototype.port3CF_read = function()\n{\n    dbg_log(\"3CF / graphics read \" + h(this.graphics_index), LOG_VGA);\n\n    switch(this.graphics_index)\n    {\n        case 0:\n            return this.planar_setreset;\n        case 1:\n            return this.planar_setreset_enable;\n        case 2:\n            return this.color_compare;\n        case 3:\n            return this.planar_rotate_reg;\n        case 4:\n            return this.plane_read;\n        case 5:\n            return this.planar_mode;\n        case 6:\n            return this.miscellaneous_graphics_register;\n        case 7:\n            return this.color_dont_care;\n        case 8:\n            return this.planar_bitmap;\n        default:\n    }\n    return 0;\n};\n\nVGAScreen.prototype.port3D4_write = function(register)\n{\n    dbg_log(\"3D4 / crtc index: \" + register, LOG_VGA);\n    this.index_crtc = register;\n};\n\nVGAScreen.prototype.port3D4_write16 = function(register)\n{\n    this.port3D4_write(register & 0xFF);\n    this.port3D5_write(register >> 8 & 0xFF);\n};\n\nVGAScreen.prototype.port3D4_read = function()\n{\n    dbg_log(\"3D4 read / crtc index: \" + this.index_crtc, LOG_VGA);\n    return this.index_crtc;\n};\n\n/**\n * CRT controller register writes\n * @see {@link http://www.osdever.net/FreeVGA/vga/crtcreg.htm}\n * @see {@link http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf} page 55\n * @see {@link https://01.org/sites/default/files/documentation/intel-gfx-prm-osrc-hsw-display_0.pdf} page 63\n */\nVGAScreen.prototype.port3D5_write = function(value)\n{\n    switch(this.index_crtc)\n    {\n        case 0x1:\n            dbg_log(\"3D5 / hdisp enable end write: \" + h(value), LOG_VGA);\n            if(this.horizontal_display_enable_end !== value)\n            {\n                this.horizontal_display_enable_end = value;\n                this.update_vga_size();\n            }\n            break;\n        case 0x2:\n            if(this.horizontal_blank_start !== value)\n            {\n                this.horizontal_blank_start = value;\n                this.update_vga_size();\n            }\n            break;\n        case 0x7:\n            dbg_log(\"3D5 / overflow register write: \" + h(value), LOG_VGA);\n            var previous_vertical_display_enable_end = this.vertical_display_enable_end;\n            this.vertical_display_enable_end &= 0xFF;\n            this.vertical_display_enable_end |= (value << 3 & 0x200) | (value << 7 & 0x100);\n            if(previous_vertical_display_enable_end !== this.vertical_display_enable_end)\n            {\n                this.update_vga_size();\n            }\n            this.line_compare = (this.line_compare & 0x2FF) | (value << 4 & 0x100);\n\n            var previous_vertical_blank_start = this.vertical_blank_start;\n            this.vertical_blank_start = (this.vertical_blank_start & 0x2FF) | (value << 5 & 0x100);\n            if(previous_vertical_blank_start !== this.vertical_blank_start)\n            {\n                this.update_vga_size();\n            }\n            this.update_layers();\n            break;\n        case 0x8:\n            dbg_log(\"3D5 / preset row scan write: \" + h(value), LOG_VGA);\n            this.preset_row_scan = value;\n            this.update_layers();\n            break;\n        case 0x9:\n            dbg_log(\"3D5 / max scan line write: \" + h(value), LOG_VGA);\n            var previous_max_scan_line = this.max_scan_line;\n            this.max_scan_line = value;\n            this.line_compare = (this.line_compare & 0x1FF) | (value << 3 & 0x200);\n\n            var previous_vertical_blank_start = this.vertical_blank_start;\n            this.vertical_blank_start = (this.vertical_blank_start & 0x1FF) | (value << 4 & 0x200);\n            if(((previous_max_scan_line ^ this.max_scan_line) & 0x9F) || previous_vertical_blank_start !== this.vertical_blank_start)\n            {\n                this.update_vga_size();\n            }\n\n            this.update_cursor_scanline();\n            this.update_layers();\n\n            this.set_font_bitmap(false);\n            break;\n        case 0xA:\n            dbg_log(\"3D5 / cursor scanline start write: \" + h(value), LOG_VGA);\n            this.cursor_scanline_start = value;\n            this.update_cursor_scanline();\n            break;\n        case 0xB:\n            dbg_log(\"3D5 / cursor scanline end write: \" + h(value), LOG_VGA);\n            this.cursor_scanline_end = value;\n            this.update_cursor_scanline();\n            break;\n        case 0xC:\n            if((this.start_address >> 8 & 0xFF) !== value)\n            {\n                this.start_address = this.start_address & 0xff | value << 8;\n                this.update_layers();\n                if(~this.crtc_mode &  0x3)\n                {\n                    // Address substitution implementation depends on the\n                    // starting row and column, so the pixel buffer is invalidated.\n                    this.complete_replot();\n                }\n            }\n            dbg_log(\"3D5 / start addr hi write: \" + h(value) + \" -> \" + h(this.start_address, 4), LOG_VGA);\n            break;\n        case 0xD:\n            if((this.start_address & 0xFF) !== value)\n            {\n                this.start_address = this.start_address & 0xff00 | value;\n                this.update_layers();\n                if(~this.crtc_mode &  0x3)\n                {\n                    // Address substitution implementation depends on the\n                    // starting row and column, so the pixel buffer is invalidated.\n                    this.complete_replot();\n                }\n            }\n            dbg_log(\"3D5 / start addr lo write: \" + h(value) + \" -> \" + h(this.start_address, 4), LOG_VGA);\n            break;\n        case 0xE:\n            dbg_log(\"3D5 / cursor address hi write: \" + h(value), LOG_VGA);\n            this.cursor_address = this.cursor_address & 0xFF | value << 8;\n            this.update_cursor();\n            break;\n        case 0xF:\n            dbg_log(\"3D5 / cursor address lo write: \" + h(value), LOG_VGA);\n            this.cursor_address = this.cursor_address & 0xFF00 | value;\n            this.update_cursor();\n            break;\n        case 0x12:\n            dbg_log(\"3D5 / vdisp enable end write: \" + h(value), LOG_VGA);\n            if((this.vertical_display_enable_end & 0xFF) !== value)\n            {\n                this.vertical_display_enable_end = (this.vertical_display_enable_end & 0x300) | value;\n                this.update_vga_size();\n            }\n            break;\n        case 0x13:\n            dbg_log(\"3D5 / offset register write: \" + h(value), LOG_VGA);\n            if(this.offset_register !== value)\n            {\n                this.offset_register = value;\n                this.update_vga_size();\n\n                if(~this.crtc_mode & 0x3)\n                {\n                    // Address substitution implementation depends on the\n                    // virtual width, so the pixel buffer is invalidated.\n                    this.complete_replot();\n                }\n            }\n            break;\n        case 0x14:\n            dbg_log(\"3D5 / underline location write: \" + h(value), LOG_VGA);\n            if(this.underline_location_register !== value)\n            {\n                var previous_underline = this.underline_location_register;\n\n                this.underline_location_register = value;\n                this.update_vga_size();\n\n                if((previous_underline ^ value) & 0x40)\n                {\n                    // Doubleword addressing changed. Pixel buffer invalidated.\n                    this.complete_replot();\n                }\n            }\n            break;\n        case 0x15:\n            dbg_log(\"3D5 / vertical blank start write: \" + h(value), LOG_VGA);\n            if((this.vertical_blank_start & 0xFF) !== value)\n            {\n                this.vertical_blank_start = (this.vertical_blank_start & 0x300) | value;\n                this.update_vga_size();\n            }\n            break;\n        case 0x17:\n            dbg_log(\"3D5 / crtc mode write: \" + h(value), LOG_VGA);\n            if(this.crtc_mode !== value)\n            {\n                var previous_mode = this.crtc_mode;\n\n                this.crtc_mode = value;\n                this.update_vga_size();\n\n                if((previous_mode ^ value) & 0x43)\n                {\n                    // Word/byte addressing changed or address substitution changed.\n                    // Pixel buffer invalidated.\n                    this.complete_replot();\n                }\n            }\n            break;\n        case 0x18:\n            dbg_log(\"3D5 / line compare write: \" + h(value), LOG_VGA);\n            this.line_compare = (this.line_compare & 0x300) | value;\n            this.update_layers();\n            break;\n        default:\n            if(this.index_crtc < this.crtc.length)\n            {\n                this.crtc[this.index_crtc] = value;\n            }\n            dbg_log(\"3D5 / CRTC write \" + h(this.index_crtc) + \": \" + h(value), LOG_VGA);\n    }\n\n};\n\nVGAScreen.prototype.port3D5_write16 = function(register)\n{\n    dbg_log(\"16-bit write to 3D5: \" + h(register, 4), LOG_VGA);\n    this.port3D5_write(register & 0xFF);\n};\n\nVGAScreen.prototype.port3D5_read = function()\n{\n    dbg_log(\"3D5 read \" + h(this.index_crtc), LOG_VGA);\n\n    switch(this.index_crtc)\n    {\n        case 0x1:\n            return this.horizontal_display_enable_end;\n        case 0x2:\n            return this.horizontal_blank_start;\n        case 0x7:\n            return (this.vertical_display_enable_end >> 7 & 0x2) |\n                (this.vertical_blank_start >> 5 & 0x8) |\n                (this.line_compare >> 4 & 0x10) |\n                (this.vertical_display_enable_end >> 3 & 0x40);\n        case 0x8:\n            return this.preset_row_scan;\n        case 0x9:\n            return this.max_scan_line;\n        case 0xA:\n            return this.cursor_scanline_start;\n        case 0xB:\n            return this.cursor_scanline_end;\n        case 0xC:\n            return this.start_address & 0xFF;\n        case 0xD:\n            return this.start_address >> 8;\n        case 0xE:\n            return this.cursor_address >> 8;\n        case 0xF:\n            return this.cursor_address & 0xFF;\n        case 0x12:\n            return this.vertical_display_enable_end & 0xFF;\n        case 0x13:\n            return this.offset_register;\n        case 0x14:\n            return this.underline_location_register;\n        case 0x15:\n            return this.vertical_blank_start & 0xFF;\n        case 0x17:\n            return this.crtc_mode;\n        case 0x18:\n            return this.line_compare & 0xFF;\n    }\n\n    if(this.index_crtc < this.crtc.length)\n    {\n        return this.crtc[this.index_crtc];\n    }\n    else\n    {\n        return 0;\n    }\n};\n\nVGAScreen.prototype.port3D5_read16 = function()\n{\n    dbg_log(\"Warning: 16-bit read from 3D5\", LOG_VGA);\n    return this.port3D5_read();\n};\n\nVGAScreen.prototype.port3DA_read = function()\n{\n    dbg_log(\"3DA read - status 1 and clear attr index\", LOG_VGA);\n\n    var value = this.port_3DA_value;\n\n    // Status register, bit 3 set by update_vertical_retrace\n    // during screen-fill-buffer\n    if(!this.graphical_mode)\n    {\n        // But screen-fill-buffer may not get triggered in text mode\n        // so toggle it manually here\n        if(this.port_3DA_value & 1)\n        {\n            this.port_3DA_value ^= 8;\n        }\n        this.port_3DA_value ^= 1;\n    }\n    else\n    {\n        this.port_3DA_value ^= 1;\n        this.port_3DA_value &= 1;\n    }\n    this.attribute_controller_index = -1;\n    return value;\n};\n\nVGAScreen.prototype.port1CE_write = function(value)\n{\n    this.dispi_index = value;\n};\n\nVGAScreen.prototype.port1CF_write = function(value)\n{\n    dbg_log(\"1CF / dispi write \" + h(this.dispi_index) + \": \" + h(value), LOG_VGA);\n\n    const was_enabled = this.svga_enabled;\n\n    switch(this.dispi_index)\n    {\n        case 0:\n            if(value >= 0xB0C0 && value <= 0xB0C5)\n            {\n                this.svga_version = value;\n            }\n            else\n            {\n                dbg_log(\"Invalid version value: \" + h(value), LOG_VGA);\n            }\n            break;\n        case 1:\n            this.svga_width = value;\n            if(this.svga_width > MAX_XRES)\n            {\n                dbg_log(\"svga_width reduced from \" + this.svga_width + \" to \" + MAX_XRES, LOG_VGA);\n                this.svga_width = MAX_XRES;\n            }\n            break;\n        case 2:\n            this.svga_height = value;\n            if(this.svga_height > MAX_YRES)\n            {\n                dbg_log(\"svga_height reduced from \" + this.svga_height + \" to \" + MAX_YRES, LOG_VGA);\n                this.svga_height = MAX_YRES;\n            }\n            break;\n        case 3:\n            this.svga_bpp = value;\n            break;\n        case 4:\n            // enable, options\n            this.svga_enabled = (value & 1) === 1;\n            if(this.svga_enabled && (value & 0x80) === 0)\n            {\n                this.svga_memory.fill(0);\n            }\n            this.dispi_enable_value = value;\n            break;\n        case 5:\n            dbg_log(\"SVGA bank offset: \" + h(value << 16), LOG_VGA);\n            this.svga_bank_offset = value << 16;\n            break;\n        case 8:\n            // x offset\n            dbg_log(\"SVGA X offset: \" + h(value), LOG_VGA);\n            if(this.svga_offset_x !== value)\n            {\n                this.svga_offset_x = value;\n                this.svga_offset = this.svga_offset_y * this.svga_width + this.svga_offset_x;\n                this.complete_redraw();\n            }\n            break;\n        case 9:\n            // y offset\n            dbg_log(\"SVGA Y offset: \" + h(value * this.svga_width) + \" y=\" + h(value), LOG_VGA);\n            if(this.svga_offset_y !== value)\n            {\n                this.svga_offset_y = value;\n                this.svga_offset = this.svga_offset_y * this.svga_width + this.svga_offset_x;\n                this.complete_redraw();\n            }\n            break;\n        default:\n            dbg_log(\"Unimplemented dispi write index: \" + h(this.dispi_index), LOG_VGA);\n    }\n\n    if(this.svga_enabled && (!this.svga_width || !this.svga_height))\n    {\n        dbg_log(\"SVGA: disabled because of invalid width/height: \" + this.svga_width + \"x\" + this.svga_height, LOG_VGA);\n        this.svga_enabled = false;\n    }\n\n    dbg_assert(this.svga_bpp !== 4, \"unimplemented svga bpp: 4\");\n    dbg_assert(this.svga_bpp === 4 || this.svga_bpp === 8 ||\n               this.svga_bpp === 15 || this.svga_bpp === 16 ||\n               this.svga_bpp === 24 || this.svga_bpp === 32,\n               \"unexpected svga bpp: \" + this.svga_bpp);\n\n    if(this.svga_enabled)\n    {\n        dbg_log(\"SVGA: enabled, \" + this.svga_width + \"x\" + this.svga_height + \"x\" + this.svga_bpp, LOG_VGA);\n    }\n    else\n    {\n        dbg_log(\"SVGA: disabled\", LOG_VGA);\n    }\n\n    if(this.svga_enabled && !was_enabled)\n    {\n        this.svga_offset = 0;\n        this.svga_offset_x = 0;\n        this.svga_offset_y = 0;\n\n        this.graphical_mode = true;\n        this.screen.set_mode(this.graphical_mode);\n        this.set_size_graphical(this.svga_width, this.svga_height, this.svga_width, this.svga_height, this.svga_bpp);\n    }\n\n    if(was_enabled && !this.svga_enabled)\n    {\n        const is_graphical = (this.attribute_mode & 0x1) !== 0;\n        this.graphical_mode = is_graphical;\n        this.screen.set_mode(is_graphical);\n        this.update_vga_size();\n        this.set_font_bitmap(false);\n        this.complete_redraw();\n    }\n\n    if(!this.svga_enabled)\n    {\n        this.svga_bank_offset = 0;\n    }\n\n    this.update_layers();\n};\n\nVGAScreen.prototype.port1CF_read = function()\n{\n    dbg_log(\"1CF / dispi read \" + h(this.dispi_index), LOG_VGA);\n    return this.svga_register_read(this.dispi_index);\n};\n\nVGAScreen.prototype.svga_register_read = function(n)\n{\n    switch(n)\n    {\n        case 0:\n            return this.svga_version;\n        case 1:\n            return this.dispi_enable_value & 2 ? MAX_XRES : this.svga_width;\n        case 2:\n            return this.dispi_enable_value & 2 ? MAX_YRES : this.svga_height;\n        case 3:\n            return this.dispi_enable_value & 2 ? MAX_BPP : this.svga_bpp;\n        case 4:\n            return this.dispi_enable_value;\n        case 5:\n            return this.svga_bank_offset >>> 16;\n        case 6:\n            // virtual width\n            if(this.screen_width)\n            {\n                return this.screen_width;\n            }\n            else\n            {\n                return 1; // seabios/windows98 divide exception\n            }\n            break;\n\n        case 8:\n            // x offset\n            return this.svga_offset_x;\n        case 9:\n            return this.svga_offset_y;\n        case 0x0A:\n            // memory size in 64 kilobyte banks\n            return this.vga_memory_size / VGA_BANK_SIZE | 0;\n        default:\n            dbg_log(\"Unimplemented dispi read index: \" + h(this.dispi_index), LOG_VGA);\n    }\n\n    return 0xFF;\n};\n\n/**\n * Transfers graphics from VGA Planes to the Pixel Buffer\n * VGA Planes represent data stored on actual hardware.\n * Pixel Buffer caches the 4-bit or 8-bit color indices for each pixel.\n */\nVGAScreen.prototype.vga_replot = function()\n{\n    // Round to multiple of 8 towards extreme\n    var start = this.diff_plot_min & ~0xF;\n    var end = Math.min((this.diff_plot_max | 0xF), VGA_PIXEL_BUFFER_SIZE - 1);\n\n    var addr_shift = this.vga_addr_shift_count();\n    var addr_substitution = ~this.crtc_mode & 0x3;\n\n    var shift_mode = this.planar_mode & 0x60;\n    var pel_width = this.attribute_mode & 0x40;\n\n    for(var pixel_addr = start; pixel_addr <= end;)\n    {\n        var addr = pixel_addr >>> addr_shift;\n        if(addr_substitution)\n        {\n            var row = pixel_addr / this.virtual_width | 0;\n            var col = pixel_addr - this.virtual_width * row;\n\n            switch(addr_substitution)\n            {\n                case 0x1:\n                    // Alternating rows using bit 13\n                    // Assumes max scan line = 1\n                    addr = (row & 0x1) << 13;\n                    row >>>= 1;\n                    break;\n                case 0x2:\n                    // Alternating rows using bit 14\n                    // Assumes max scan line = 3\n                    addr = (row & 0x1) << 14;\n                    row >>>= 1;\n                    break;\n                case 0x3:\n                    // Cycling through rows using bit 13 and 14\n                    // Assumes max scan line = 3\n                    addr = (row & 0x3) << 13;\n                    row >>>= 2;\n                    break;\n            }\n\n            addr |= (row * this.virtual_width + col >>> addr_shift) + this.start_address;\n        }\n\n        var byte0 = this.plane0[addr];\n        var byte1 = this.plane1[addr];\n        var byte2 = this.plane2[addr];\n        var byte3 = this.plane3[addr];\n\n        var shift_loads = new Uint8Array(8);\n        switch(shift_mode)\n        {\n            // Planar Shift Mode\n            // See http://www.osdever.net/FreeVGA/vga/vgaseq.htm\n            case 0x00:\n                // Shift these, so that the bits for the color are in\n                // the correct position in the for loop\n                byte0 <<= 0;\n                byte1 <<= 1;\n                byte2 <<= 2;\n                byte3 <<= 3;\n\n                for(var i = 7; i >= 0; i--)\n                {\n                    shift_loads[7 - i] =\n                            byte0 >> i & 1 |\n                            byte1 >> i & 2 |\n                            byte2 >> i & 4 |\n                            byte3 >> i & 8;\n                }\n                break;\n\n            // Packed Shift Mode, aka Interleaved Shift Mode\n            // Video Modes 4h and 5h\n            case 0x20:\n                shift_loads[0] = (byte0 >> 6 & 0x3) | (byte2 >> 4 & 0xC);\n                shift_loads[1] = (byte0 >> 4 & 0x3) | (byte2 >> 2 & 0xC);\n                shift_loads[2] = (byte0 >> 2 & 0x3) | (byte2 >> 0 & 0xC);\n                shift_loads[3] = (byte0 >> 0 & 0x3) | (byte2 << 2 & 0xC);\n\n                shift_loads[4] = (byte1 >> 6 & 0x3) | (byte3 >> 4 & 0xC);\n                shift_loads[5] = (byte1 >> 4 & 0x3) | (byte3 >> 2 & 0xC);\n                shift_loads[6] = (byte1 >> 2 & 0x3) | (byte3 >> 0 & 0xC);\n                shift_loads[7] = (byte1 >> 0 & 0x3) | (byte3 << 2 & 0xC);\n                break;\n\n            // 256-Color Shift Mode\n            // Video Modes 13h and unchained 256 color\n            case 0x40:\n            case 0x60:\n                shift_loads[0] = byte0 >> 4 & 0xF;\n                shift_loads[1] = byte0 >> 0 & 0xF;\n                shift_loads[2] = byte1 >> 4 & 0xF;\n                shift_loads[3] = byte1 >> 0 & 0xF;\n                shift_loads[4] = byte2 >> 4 & 0xF;\n                shift_loads[5] = byte2 >> 0 & 0xF;\n                shift_loads[6] = byte3 >> 4 & 0xF;\n                shift_loads[7] = byte3 >> 0 & 0xF;\n                break;\n        }\n\n        if(pel_width)\n        {\n            // Assemble from two sets of 4 bits.\n            for(var i = 0, j = 0; i < 4; i++, pixel_addr++, j += 2)\n            {\n                this.pixel_buffer[pixel_addr] = (shift_loads[j] << 4) | shift_loads[j + 1];\n            }\n        }\n        else\n        {\n            for(var i = 0; i < 8; i++, pixel_addr++)\n            {\n                this.pixel_buffer[pixel_addr] = shift_loads[i];\n            }\n        }\n    }\n};\n\n/**\n * Transfers graphics from Pixel Buffer to Destination Image Buffer.\n * The 4-bit/8-bit color indices in the Pixel Buffer are passed through\n * the internal palette (dac_map) and the DAC palette (vga256_palette) to\n * obtain the final 32 bit color that the Canvas API uses.\n */\nVGAScreen.prototype.vga_redraw = function()\n{\n    var start = this.diff_addr_min;\n    var end = Math.min(this.diff_addr_max, VGA_PIXEL_BUFFER_SIZE - 1);\n    const buffer = new Int32Array(this.cpu.wasm_memory.buffer, this.dest_buffet_offset, this.virtual_width * this.virtual_height);\n\n    var mask = 0xFF;\n    var colorset = 0x00;\n    if(this.attribute_mode & 0x80)\n    {\n        // Palette bits 5/4 select\n        mask &= 0xCF;\n        colorset |= this.color_select << 4 & 0x30;\n    }\n\n    if(this.attribute_mode & 0x40)\n    {\n        // 8 bit mode\n\n        for(var pixel_addr = start; pixel_addr <= end; pixel_addr++)\n        {\n            var color256 = (this.pixel_buffer[pixel_addr] & mask) | colorset;\n            var color = this.vga256_palette[color256];\n\n            buffer[pixel_addr] = color & 0xFF00 | color << 16 | color >> 16 | 0xFF000000;\n        }\n    }\n    else\n    {\n        // 4 bit mode\n\n        // Palette bits 7/6 select\n        mask &= 0x3F;\n        colorset |= this.color_select << 4 & 0xC0;\n\n        for(var pixel_addr = start; pixel_addr <= end; pixel_addr++)\n        {\n            var color16 = this.pixel_buffer[pixel_addr] & this.color_plane_enable;\n            var color256 = (this.dac_map[color16] & mask) | colorset;\n            var color = this.vga256_palette[color256];\n\n            buffer[pixel_addr] = color & 0xFF00 | color << 16 | color >> 16 | 0xFF000000;\n        }\n    }\n};\n\nVGAScreen.prototype.screen_fill_buffer = function()\n{\n    if(!this.graphical_mode)\n    {\n        // text mode\n        // Update retrace behaviour anyway - programs waiting for signal before\n        // changing to graphical mode\n        this.update_vertical_retrace();\n        return;\n    }\n\n    if(this.image_data.data.byteLength === 0)\n    {\n        // wasm memory resized\n        const buffer = new Uint8ClampedArray(this.cpu.wasm_memory.buffer, this.dest_buffet_offset, 4 * this.virtual_width * this.virtual_height);\n        this.image_data = new ImageData(buffer, this.virtual_width, this.virtual_height);\n        this.update_layers();\n    }\n\n    if(this.svga_enabled)\n    {\n        let min_y = 0;\n        let max_y = this.svga_height;\n\n        if(this.svga_bpp === 8)\n        {\n            // XXX: Slow, should be ported to rust, but it doesn't have access to vga256_palette\n            // XXX: Doesn't take svga_offset into account\n            const buffer = new Int32Array(this.cpu.wasm_memory.buffer, this.dest_buffet_offset, this.screen_width * this.screen_height);\n            const svga_memory = new Uint8Array(this.cpu.wasm_memory.buffer, this.svga_memory.byteOffset, this.vga_memory_size);\n\n            for(var i = 0; i < buffer.length; i++)\n            {\n                var color = this.vga256_palette[svga_memory[i]];\n                buffer[i] = color & 0xFF00 | color << 16 | color >> 16 | 0xFF000000;\n            }\n        }\n        else\n        {\n            this.cpu.svga_fill_pixel_buffer(this.svga_bpp, this.svga_offset);\n\n            const bytes_per_pixel = this.svga_bpp === 15 ? 2 : this.svga_bpp / 8;\n            min_y = (((this.cpu.svga_dirty_bitmap_min_offset[0] / bytes_per_pixel | 0) - this.svga_offset) / this.svga_width | 0);\n            max_y = (((this.cpu.svga_dirty_bitmap_max_offset[0] / bytes_per_pixel | 0) - this.svga_offset) / this.svga_width | 0) + 1;\n        }\n\n        if(min_y < max_y)\n        {\n            min_y = Math.max(min_y, 0);\n            max_y = Math.min(max_y, this.svga_height);\n\n            this.screen.update_buffer([{\n                image_data: this.image_data,\n                screen_x: 0, screen_y: min_y,\n                buffer_x: 0, buffer_y: min_y,\n                buffer_width: this.svga_width,\n                buffer_height: max_y - min_y,\n            }]);\n        }\n    }\n    else\n    {\n        this.vga_replot();\n        this.vga_redraw();\n        this.screen.update_buffer(this.layers);\n    }\n\n    this.reset_diffs();\n    this.update_vertical_retrace();\n};\n\nVGAScreen.prototype.set_font_bitmap = function(font_plane_dirty)\n{\n    const height = this.max_scan_line & 0x1f;\n    if(height && !this.graphical_mode)\n    {\n        const width_dbl = !!(this.clocking_mode & 0x08);\n        const width_9px = !width_dbl && !(this.clocking_mode & 0x01);\n        const copy_8th_col = !!(this.attribute_mode & 0x04);\n        this.screen.set_font_bitmap(\n            height + 1,         // int height, font height 1..32px\n            width_9px,          // bool width_9px, True: font width 9px, else 8px\n            width_dbl,          // bool width_dbl, True: font width 16px (overrides width_9px)\n            copy_8th_col,       // bool copy_8th_col, True: duplicate 8th into 9th column in ASCII chars 0xC0-0xDF\n            this.plane2,        // Uint8Array font_bitmap[64k], static\n            font_plane_dirty    // bool bitmap_changed, True: content of this.plane2 has changed\n        );\n    }\n};\n\nVGAScreen.prototype.set_font_page = function()\n{\n    // bits 2, 3 and 5 (LSB to MSB): VGA font page index of font A\n    // bits 0, 1 and 4: VGA font page index of font B\n    // linear_index_map[] maps VGA's non-liner font page index to linear index\n    const linear_index_map = [0, 2, 4, 6, 1, 3, 5, 7];\n    const vga_index_A = ((this.character_map_select & 0b1100) >> 2) | ((this.character_map_select & 0b100000) >> 3);\n    const vga_index_B = (this.character_map_select & 0b11) | ((this.character_map_select & 0b10000) >> 2);\n    this.font_page_ab_enabled = vga_index_A !== vga_index_B;\n    this.screen.set_font_page(linear_index_map[vga_index_A], linear_index_map[vga_index_B]);\n    this.complete_redraw();\n};\n"
  },
  {
    "path": "src/virtio.js",
    "content": "import { LOG_VIRTIO } from \"./const.js\";\nimport { h, int_log2 } from \"./lib.js\";\nimport { dbg_assert, dbg_log } from \"./log.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { PCI } from \"./pci.js\";\n\n// http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html\n\nconst VIRTIO_PCI_VENDOR_ID = 0x1AF4;\n// Identifies vendor-specific PCI capability.\nconst VIRTIO_PCI_CAP_VENDOR = 0x09;\n// Length (bytes) of VIRTIO_PCI_CAP linked list entry.\nconst VIRTIO_PCI_CAP_LENGTH = 16;\n\n// Capability types.\n\nconst VIRTIO_PCI_CAP_COMMON_CFG = 1;\nconst VIRTIO_PCI_CAP_NOTIFY_CFG = 2;\nconst VIRTIO_PCI_CAP_ISR_CFG = 3;\nconst VIRTIO_PCI_CAP_DEVICE_CFG = 4;\nconst VIRTIO_PCI_CAP_PCI_CFG = 5;\n\n// Status bits (device_status values).\n\nconst VIRTIO_STATUS_ACKNOWLEDGE = 1;\nconst VIRTIO_STATUS_DRIVER = 2;\nconst VIRTIO_STATUS_DRIVER_OK = 4;\nconst VIRTIO_STATUS_FEATURES_OK = 8;\nconst VIRTIO_STATUS_DEVICE_NEEDS_RESET = 64;\nconst VIRTIO_STATUS_FAILED = 128;\n\n// ISR bits (isr_status values).\n\nconst VIRTIO_ISR_QUEUE = 1;\nconst VIRTIO_ISR_DEVICE_CFG = 2;\n\n// Feature bits (bit positions).\n\nexport const VIRTIO_F_RING_INDIRECT_DESC = 28;\nexport const VIRTIO_F_RING_EVENT_IDX = 29;\nexport const VIRTIO_F_VERSION_1 = 32;\n\n// Queue struct sizes.\n\n// Size (bytes) of the virtq_desc struct per queue size.\nconst VIRTQ_DESC_ENTRYSIZE = 16;\n// Size (bytes) of the virtq_avail struct ignoring ring entries.\nconst VIRTQ_AVAIL_BASESIZE = 6;\n// Size (bytes) of the virtq_avail struct per queue size.\nconst VIRTQ_AVAIL_ENTRYSIZE = 2;\n// Size (bytes) of the virtq_used struct ignoring ring entries.\nconst VIRTQ_USED_BASESIZE = 6;\n// Size (bytes) of the virtq_desc struct per queue size.\nconst VIRTQ_USED_ENTRYSIZE = 8;\n// Mask for wrapping the idx field of the virtq_used struct so that the value\n// naturally overflows after 65535 (idx is a word).\nconst VIRTQ_IDX_MASK = 0xFFFF;\n\n// Queue flags.\n\nconst VIRTQ_DESC_F_NEXT = 1;\nconst VIRTQ_DESC_F_WRITE = 2;\nconst VIRTQ_DESC_F_INDIRECT = 4;\nconst VIRTQ_AVAIL_F_NO_INTERRUPT = 1;\nconst VIRTQ_USED_F_NO_NOTIFY = 1;\n\n// Closure Compiler Types.\n\n/**\n * @typedef {!Array<{\n *     bytes: number,\n *     name: string,\n *     read: function():number,\n *     write: function(number)\n * }>}\n */\nvar VirtIO_CapabilityStruct;\n\n/**\n * @typedef {\n * {\n *     type: number,\n *     bar: number,\n *     port: number,\n *     use_mmio: boolean,\n *     offset: number,\n *     extra: Uint8Array,\n *     struct: VirtIO_CapabilityStruct,\n * }}\n */\nvar VirtIO_CapabilityInfo;\n\n/**\n * @typedef {\n * {\n *     size_supported: number,\n *     notify_offset: number,\n * }}\n */\nvar VirtQueue_Options;\n\n/**\n * @typedef {\n * {\n *     initial_port: number,\n *     queues: !Array<VirtQueue_Options>,\n *     features: !Array<number>,\n *     on_driver_ok: function(),\n * }}\n */\nvar VirtIO_CommonCapabilityOptions;\n\n/**\n * @typedef {\n * {\n *     initial_port: number,\n *     single_handler: boolean,\n *     handlers: !Array<function()>,\n * }}\n */\nvar VirtIO_NotificationCapabilityOptions;\n\n/**\n * @typedef {\n * {\n *     initial_port: number,\n * }}\n */\nvar VirtIO_ISRCapabilityOptions;\n\n/**\n * @typedef {\n * {\n *     initial_port: number,\n *     struct: VirtIO_CapabilityStruct,\n * }}\n */\nvar VirtIO_DeviceSpecificCapabilityOptions;\n\n/**\n * @typedef {\n * {\n *     name: string,\n *     pci_id: number,\n *     device_id: number,\n *     subsystem_device_id: number,\n *     common: VirtIO_CommonCapabilityOptions,\n *     notification: VirtIO_NotificationCapabilityOptions,\n *     isr_status: VirtIO_ISRCapabilityOptions,\n *     device_specific: (undefined | VirtIO_DeviceSpecificCapabilityOptions),\n * }}\n */\nvar VirtIO_Options;\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {VirtIO_Options} options\n */\nexport function VirtIO(cpu, options)\n{\n    const io = cpu.io;\n\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    /** @const @type {PCI} */\n    this.pci = cpu.devices.pci;\n\n    this.device_id = options.device_id;\n\n    this.pci_space =\n    [\n        // Vendor ID\n        VIRTIO_PCI_VENDOR_ID & 0xFF, VIRTIO_PCI_VENDOR_ID >> 8,\n        // Device ID\n        options.device_id & 0xFF, options.device_id >> 8,\n        // Command\n        0x07, 0x05,\n        // Status - enable capabilities list\n        0x10, 0x00,\n        // Revision ID\n        0x01,\n        // Prof IF, Subclass, Class code\n        0x00, 0x02, 0x00,\n        // Cache line size\n        0x00,\n        // Latency Timer\n        0x00,\n        // Header Type\n        0x00,\n        // Built-in self test\n        0x00,\n        // BAR0\n        0x01, 0xa8, 0x00, 0x00,\n        // BAR1\n        0x00, 0x10, 0xbf, 0xfe,\n        // BAR2\n        0x00, 0x00, 0x00, 0x00,\n        // BAR3\n        0x00, 0x00, 0x00, 0x00,\n        // BAR4\n        0x00, 0x00, 0x00, 0x00,\n        // BAR5\n        0x00, 0x00, 0x00, 0x00,\n        // CardBus CIS pointer\n        0x00, 0x00, 0x00, 0x00,\n        // Subsystem vendor ID\n        VIRTIO_PCI_VENDOR_ID & 0xFF, VIRTIO_PCI_VENDOR_ID >> 8,\n        // Subsystem ID\n        options.subsystem_device_id & 0xFF, options.subsystem_device_id >> 8,\n        // Expansion ROM base address\n        0x00, 0x00, 0x00, 0x00,\n        // Capabilities pointer\n        0x40,\n        // Reserved\n        0x00, 0x00, 0x00,\n        // Reserved\n        0x00, 0x00, 0x00, 0x00,\n        // Interrupt line\n        0x00,\n        // Interrupt pin\n        0x01,\n        // Min grant\n        0x00,\n        // Max latency\n        0x00,\n    ];\n\n    // Prevent sparse arrays by preallocating.\n    this.pci_space = this.pci_space.concat(Array(256 - this.pci_space.length).fill(0));\n    // Remaining PCI space is appended by capabilities further below.\n\n    this.pci_id = options.pci_id;\n\n    // PCI bars gets filled in by capabilities further below.\n    this.pci_bars = [];\n\n    this.name = options.name;\n\n    // Feature bits grouped in dwords, dword selected by decive_feature_select.\n    this.device_feature_select = 0;\n    this.driver_feature_select = 0;\n\n    // Unspecified upper bound. Assume 4*32=128 bits.\n    this.device_feature = new Uint32Array(4);\n    this.driver_feature = new Uint32Array(4);\n    for(const f of options.common.features)\n    {\n        dbg_assert(f >= 0,\n            \"VirtIO device<\" + this.name + \"> feature bit numbers must be non-negative\");\n        dbg_assert(f < 128,\n            \"VirtIO device<\" + this.name + \"> feature bit numbers assumed less than 128 in implementation\");\n\n        // Feature bits are grouped in 32 bits.\n        this.device_feature[f >>> 5] |= 1 << (f & 0x1F);\n        this.driver_feature[f >>> 5] |= 1 << (f & 0x1F);\n    }\n\n    dbg_assert(options.common.features.includes(VIRTIO_F_VERSION_1),\n        \"VirtIO device<\" + this.name + \"> only non-transitional devices are supported\");\n\n    // Indicates whether driver_feature bits is subset of device_feature bits.\n    this.features_ok = true;\n\n    this.device_status = 0;\n\n    this.config_has_changed = false;\n    this.config_generation = 0;\n\n    /** @type {!Array<VirtQueue>} */\n    this.queues = [];\n    for(const queue_options of options.common.queues)\n    {\n        this.queues.push(new VirtQueue(cpu, this, queue_options));\n    }\n    this.queue_select = 0;\n    this.queue_selected = this.queues[0];\n\n    this.isr_status = 0;\n\n    // Verify notification options.\n    if(DEBUG)\n    {\n        const offsets = new Set();\n        for(const offset of this.queues.map(q => q.notify_offset))\n        {\n            const effective_offset = options.notification.single_handler ? 0 : offset;\n            offsets.add(effective_offset);\n            dbg_assert(options.notification.handlers[effective_offset],\n                \"VirtIO device<\" + this.name + \"> every queue's notifier must exist\");\n        }\n        for(const [index, handler] of options.notification.handlers.entries())\n        {\n            dbg_assert(!handler || offsets.has(index),\n                \"VirtIO device<\" + this.name +\"> no defined notify handler should be unused\");\n        }\n    }\n\n    /** @type {!Array<VirtIO_CapabilityInfo>} */\n    const capabilities = [];\n    capabilities.push(this.create_common_capability(options.common));\n    capabilities.push(this.create_notification_capability(options.notification));\n    capabilities.push(this.create_isr_capability(options.isr_status));\n    if(options.device_specific)\n    {\n        capabilities.push(this.create_device_specific_capability(options.device_specific));\n    }\n    this.init_capabilities(capabilities);\n\n    cpu.devices.pci.register_device(this);\n    this.reset();\n}\n\n/**\n * @param {VirtIO_CommonCapabilityOptions} options\n * @return {VirtIO_CapabilityInfo}\n */\nVirtIO.prototype.create_common_capability = function(options)\n{\n    return {\n        type: VIRTIO_PCI_CAP_COMMON_CFG,\n        bar: 0,\n        port: options.initial_port,\n        use_mmio: false,\n        offset: 0,\n        extra: new Uint8Array(0),\n        struct:\n        [\n            {\n                bytes: 4,\n                name: \"device_feature_select\",\n                read: () => this.device_feature_select,\n                write: data =>\n                {\n                    this.device_feature_select = data;\n                },\n            },\n            {\n                bytes: 4,\n                name: \"device_feature\",\n                read: () => this.device_feature[this.device_feature_select] || 0,\n                write: data => { /* read only */ },\n            },\n            {\n                bytes: 4,\n                name: \"driver_feature_select\",\n                read: () => this.driver_feature_select,\n                write: data =>\n                {\n                    this.driver_feature_select = data;\n                },\n            },\n            {\n                bytes: 4,\n                name: \"driver_feature\",\n                read: () => this.driver_feature[this.driver_feature_select] || 0,\n                write: data =>\n                {\n                    const supported_feature = this.device_feature[this.driver_feature_select];\n\n                    if(this.driver_feature_select < this.driver_feature.length)\n                    {\n                        // Note: only set subset of device_features is set.\n                        // Required in our implementation for is_feature_negotiated().\n                        this.driver_feature[this.driver_feature_select] = data & supported_feature;\n                    }\n\n                    // Check that driver features is an inclusive subset of device features.\n                    const invalid_bits = data & ~supported_feature;\n                    this.features_ok = this.features_ok && !invalid_bits;\n                },\n            },\n            {\n                bytes: 2,\n                name: \"msix_config\",\n                read: () =>\n                {\n                    dbg_log(\"No msi-x capability supported.\", LOG_VIRTIO);\n                    return 0xFFFF;\n                },\n                write: data =>\n                {\n                    dbg_log(\"No msi-x capability supported.\", LOG_VIRTIO);\n                },\n            },\n            {\n                bytes: 2,\n                name: \"num_queues\",\n                read: () => this.queues.length,\n                write: data => { /* read only */ },\n            },\n            {\n                bytes: 1,\n                name: \"device_status\",\n                read: () => this.device_status,\n                write: data =>\n                {\n                    if(data === 0)\n                    {\n                        dbg_log(\"Reset device<\" + this.name + \">\", LOG_VIRTIO);\n                        this.reset();\n                    }\n                    else if(data & VIRTIO_STATUS_FAILED)\n                    {\n                        dbg_log(\"Warning: Device<\" + this.name + \"> status failed\", LOG_VIRTIO);\n                    }\n                    else\n                    {\n                        dbg_log(\"Device<\" + this.name +\"> status: \" +\n                                ((data & VIRTIO_STATUS_ACKNOWLEDGE) ? \"ACKNOWLEDGE \" : \"\") +\n                                ((data & VIRTIO_STATUS_DRIVER) ? \"DRIVER \" : \"\") +\n                                ((data & VIRTIO_STATUS_DRIVER_OK) ? \"DRIVER_OK\" : \"\") +\n                                ((data & VIRTIO_STATUS_FEATURES_OK) ? \"FEATURES_OK \" : \"\") +\n                                ((data & VIRTIO_STATUS_DEVICE_NEEDS_RESET) ? \"DEVICE_NEEDS_RESET\" : \"\"),\n                                LOG_VIRTIO);\n                    }\n\n                    if((data & ~this.device_status & VIRTIO_STATUS_DRIVER_OK) &&\n                        (this.device_status & VIRTIO_STATUS_DEVICE_NEEDS_RESET))\n                    {\n                        // We couldn't notify NEEDS_RESET earlier because DRIVER_OK was not set.\n                        // Now it has been set, notify now.\n                        this.notify_config_changes();\n                    }\n\n                    // Don't set FEATURES_OK if our device doesn't support requested features.\n                    if(!this.features_ok)\n                    {\n                        if(DEBUG && (data & VIRTIO_STATUS_FEATURES_OK))\n                        {\n                            dbg_log(\"Removing FEATURES_OK\", LOG_VIRTIO);\n                        }\n                        data &= ~VIRTIO_STATUS_FEATURES_OK;\n                    }\n\n                    this.device_status = data;\n\n                    if(data & ~this.device_status & VIRTIO_STATUS_DRIVER_OK)\n                    {\n                        options.on_driver_ok();\n                    }\n                },\n            },\n            {\n                bytes: 1,\n                name: \"config_generation\",\n                read: () => this.config_generation,\n                write: data => { /* read only */ },\n            },\n            {\n                bytes: 2,\n                name: \"queue_select\",\n                read: () => this.queue_select,\n                write: data =>\n                {\n                    this.queue_select = data;\n\n                    if(this.queue_select < this.queues.length)\n                    {\n                        this.queue_selected = this.queues[this.queue_select];\n                    }\n                    else\n                    {\n                        // Allow queue_select >= num_queues.\n                        this.queue_selected = null;\n                        // Drivers can then detect that the queue is not available\n                        // using the below fields.\n                    }\n                },\n            },\n            {\n                bytes: 2,\n                name: \"queue_size\",\n                read: () => this.queue_selected ? this.queue_selected.size : 0,\n                write: data =>\n                {\n                    if(!this.queue_selected)\n                    {\n                        return;\n                    }\n                    if(data & data - 1)\n                    {\n                        dbg_log(\"Warning: dev<\" + this.name +\"> \" +\n                                \"Given queue size was not a power of 2. \" +\n                                \"Rounding up to next power of 2.\", LOG_VIRTIO);\n                        data = 1 << (int_log2(data - 1) + 1);\n                    }\n                    if(data > this.queue_selected.size_supported)\n                    {\n                        dbg_log(\"Warning: dev<\" + this.name +\"> \" +\n                                \"Trying to set queue size greater than supported. \" +\n                                \"Clamping to supported size.\", LOG_VIRTIO);\n                        data = this.queue_selected.size_supported;\n                    }\n                    this.queue_selected.set_size(data);\n                },\n            },\n            {\n                bytes: 2,\n                name: \"queue_msix_vector\",\n                read: () =>\n                {\n                    dbg_log(\"No msi-x capability supported.\", LOG_VIRTIO);\n                    return 0xFFFF;\n                },\n                write: data =>\n                {\n                    dbg_log(\"No msi-x capability supported.\", LOG_VIRTIO);\n                },\n            },\n            {\n                bytes: 2,\n                name: \"queue_enable\",\n                read: () => this.queue_selected ? this.queue_selected.enabled | 0 : 0,\n                write: data =>\n                {\n                    if(!this.queue_selected)\n                    {\n                        return;\n                    }\n                    if(data === 1)\n                    {\n                        if(this.queue_selected.is_configured())\n                        {\n                            this.queue_selected.enable();\n                        }\n                        else\n                        {\n                            dbg_log(\"Driver bug: tried enabling unconfigured queue\", LOG_VIRTIO);\n                        }\n                    }\n                    else if(data === 0)\n                    {\n                        dbg_log(\"Driver bug: tried writing 0 to queue_enable\", LOG_VIRTIO);\n                    }\n                },\n            },\n            {\n                bytes: 2,\n                name: \"queue_notify_off\",\n                read: () => this.queue_selected ? this.queue_selected.notify_offset : 0,\n                write: data => { /* read only */ },\n            },\n            {\n                bytes: 4,\n                name: \"queue_desc (low dword)\",\n                read: () => this.queue_selected ? this.queue_selected.desc_addr : 0,\n                write: data =>\n                {\n                    if(this.queue_selected) this.queue_selected.desc_addr = data;\n                },\n            },\n            {\n                bytes: 4,\n                name: \"queue_desc (high dword)\",\n                read: () => 0,\n                write: data =>\n                {\n                    if(data !== 0) dbg_log(\"Warning: High dword of 64 bit queue_desc ignored:\" + data, LOG_VIRTIO);\n                },\n            },\n            {\n                bytes: 4,\n                name: \"queue_avail (low dword)\",\n                read: () => this.queue_selected ? this.queue_selected.avail_addr : 0,\n                write: data =>\n                {\n                    if(this.queue_selected) this.queue_selected.avail_addr = data;\n                },\n            },\n            {\n                bytes: 4,\n                name: \"queue_avail (high dword)\",\n                read: () => 0,\n                write: data =>\n                {\n                    if(data !== 0) dbg_log(\"Warning: High dword of 64 bit queue_avail ignored:\" + data, LOG_VIRTIO);\n                },\n            },\n            {\n                bytes: 4,\n                name: \"queue_used (low dword)\",\n                read: () => this.queue_selected ? this.queue_selected.used_addr : 0,\n                write: data =>\n                {\n                    if(this.queue_selected) this.queue_selected.used_addr = data;\n                },\n            },\n            {\n                bytes: 4,\n                name: \"queue_used (high dword)\",\n                read: () => 0,\n                write: data =>\n                {\n                    if(data !== 0) dbg_log(\"Warning: High dword of 64 bit queue_used ignored:\" + data, LOG_VIRTIO);\n                },\n            },\n        ],\n    };\n};\n\n/**\n * @param {VirtIO_NotificationCapabilityOptions} options\n * @return {VirtIO_CapabilityInfo}\n */\nVirtIO.prototype.create_notification_capability = function(options)\n{\n    const notify_struct = [];\n    let notify_off_multiplier;\n\n    if(options.single_handler)\n    {\n        dbg_assert(options.handlers.length === 1,\n            \"VirtIO device<\" + this.name + \"> too many notify handlers specified: expected single handler\");\n\n        // Forces all queues to use the same address for notifying.\n        notify_off_multiplier = 0;\n    }\n    else\n    {\n        notify_off_multiplier = 2;\n    }\n\n    for(const [i, handler] of options.handlers.entries())\n    {\n        notify_struct.push(\n        {\n            bytes: 2,\n            name: \"notify\" + i,\n            read: () => 0xFFFF,\n            write: handler || (data => {}),\n        });\n    }\n\n    return {\n        type: VIRTIO_PCI_CAP_NOTIFY_CFG,\n        bar: 1,\n        port: options.initial_port,\n        use_mmio: false,\n        offset: 0,\n        extra: new Uint8Array(\n        [\n            notify_off_multiplier & 0xFF,\n            (notify_off_multiplier >> 8) & 0xFF,\n            (notify_off_multiplier >> 16) & 0xFF,\n            notify_off_multiplier >> 24,\n        ]),\n        struct: notify_struct,\n    };\n};\n\n/**\n * @param {VirtIO_ISRCapabilityOptions} options\n * @return {VirtIO_CapabilityInfo}\n */\nVirtIO.prototype.create_isr_capability = function(options)\n{\n    return {\n        type: VIRTIO_PCI_CAP_ISR_CFG,\n        bar: 2,\n        port: options.initial_port,\n        use_mmio: false,\n        offset: 0,\n        extra: new Uint8Array(0),\n        struct:\n        [\n            {\n                bytes: 1,\n                name: \"isr_status\",\n                read: () =>\n                {\n                    const isr_status = this.isr_status;\n                    this.lower_irq();\n                    return isr_status;\n                },\n                write: data => { /* read only */ },\n            },\n        ],\n    };\n};\n\n/**\n * @param {VirtIO_DeviceSpecificCapabilityOptions} options\n * @return {VirtIO_CapabilityInfo}\n */\nVirtIO.prototype.create_device_specific_capability = function(options)\n{\n    dbg_assert(~options.offset & 0x3,\n            \"VirtIO device<\" + this.name + \"> device specific cap offset must be 4-byte aligned\");\n\n    return {\n        type: VIRTIO_PCI_CAP_DEVICE_CFG,\n        bar: 3,\n        port: options.initial_port,\n        use_mmio: false,\n        offset: 0,\n        extra: new Uint8Array(0),\n        struct: options.struct,\n    };\n};\n\n/**\n * Writes capabilities into pci_space and hook up IO/MMIO handlers.\n * Call only within constructor.\n * @param {!Array<VirtIO_CapabilityInfo>} capabilities\n */\nVirtIO.prototype.init_capabilities = function(capabilities)\n{\n    // Next available offset for capabilities linked list.\n    let cap_next = this.pci_space[0x34] = 0x40;\n\n    // Current offset.\n    let cap_ptr = cap_next;\n\n    for(const cap of capabilities)\n    {\n        const cap_len = VIRTIO_PCI_CAP_LENGTH + cap.extra.length;\n\n        cap_ptr = cap_next;\n        cap_next = cap_ptr + cap_len;\n\n        dbg_assert(cap_next <= 256,\n            \"VirtIO device<\" + this.name + \"> can't fit all capabilities into 256byte configspace\");\n\n        dbg_assert(0 <= cap.bar && cap.bar < 6,\n            \"VirtIO device<\" + this.name + \"> capability invalid bar number\");\n\n        let bar_size = cap.struct.reduce((bytes, field) => bytes + field.bytes, 0);\n        bar_size += cap.offset;\n\n        // Round up to next power of 2,\n        // Minimum 16 bytes for its size to be detectable in general (esp. mmio).\n        bar_size = bar_size < 16 ? 16 : 1 << (int_log2(bar_size - 1) + 1);\n\n        dbg_assert((cap.port & (bar_size - 1)) === 0,\n            \"VirtIO device<\" + this.name + \"> capability port should be aligned to pci bar size\");\n\n        this.pci_bars[cap.bar] =\n        {\n            size: bar_size,\n        };\n\n        this.pci_space[cap_ptr] = VIRTIO_PCI_CAP_VENDOR;\n        this.pci_space[cap_ptr + 1] = cap_next;\n        this.pci_space[cap_ptr + 2] = cap_len;\n        this.pci_space[cap_ptr + 3] = cap.type;\n        this.pci_space[cap_ptr + 4] = cap.bar;\n\n        this.pci_space[cap_ptr + 5] = 0; // Padding.\n        this.pci_space[cap_ptr + 6] = 0; // Padding.\n        this.pci_space[cap_ptr + 7] = 0; // Padding.\n\n        this.pci_space[cap_ptr + 8] = cap.offset & 0xFF;\n        this.pci_space[cap_ptr + 9] = (cap.offset >>> 8) & 0xFF;\n        this.pci_space[cap_ptr + 10] = (cap.offset >>> 16) & 0xFF;\n        this.pci_space[cap_ptr + 11] = cap.offset >>> 24;\n\n        this.pci_space[cap_ptr + 12] = bar_size & 0xFF;\n        this.pci_space[cap_ptr + 13] = (bar_size >>> 8) & 0xFF;\n        this.pci_space[cap_ptr + 14] = (bar_size >>> 16) & 0xFF;\n        this.pci_space[cap_ptr + 15] = bar_size >>> 24;\n\n        for(const [i, extra_byte] of cap.extra.entries())\n        {\n            this.pci_space[cap_ptr + 16 + i] = extra_byte;\n        }\n\n        const bar_offset = 0x10 + 4 * cap.bar;\n        this.pci_space[bar_offset] = (cap.port & 0xFE) | !cap.use_mmio;\n        this.pci_space[bar_offset + 1] = (cap.port >>> 8) & 0xFF;\n        this.pci_space[bar_offset + 2] = (cap.port >>> 16) & 0xFF;\n        this.pci_space[bar_offset + 3] = (cap.port >>> 24) & 0xFF;\n\n        let port = cap.port + cap.offset;\n\n        for(const field of cap.struct)\n        {\n            let read = field.read;\n            let write = field.write;\n\n            if(DEBUG)\n            {\n                read = () =>\n                {\n                    const val = field.read();\n\n                    dbg_log(\"Device<\" + this.name + \"> \" +\n                            \"cap[\" + cap.type + \"] \" +\n                            \"read[\" + field.name + \"] \" +\n                            \"=> \" + h(val, field.bytes * 8),\n                        LOG_VIRTIO);\n\n                    return val;\n                };\n                write = data =>\n                {\n                    dbg_log(\"Device<\" + this.name + \"> \" +\n                            \"cap[\" + cap.type + \"] \" +\n                            \"write[\" + field.name + \"] \" +\n                            \"<= \" + h(data, field.bytes * 8),\n                        LOG_VIRTIO);\n\n                    field.write(data);\n                };\n            }\n\n            if(cap.use_mmio)\n            {\n                dbg_assert(false, \"VirtIO device <\" + this.name + \"> mmio capability not implemented.\");\n            }\n            else\n            {\n                // DSL (2.4 kernel) does these reads\n                const shim_read8_on_16 = function(addr)\n                {\n                    dbg_log(\"Warning: 8-bit read from 16-bit virtio port\", LOG_VIRTIO);\n                    return read(addr & ~1) >> ((addr & 1) << 3) & 0xFF;\n                };\n                const shim_read8_on_32 = function(addr)\n                {\n                    dbg_log(\"Warning: 8-bit read from 32-bit virtio port\", LOG_VIRTIO);\n                    return read(addr & ~3) >> ((addr & 3) << 3) & 0xFF;\n                };\n\n                // archhurd does these reads\n                const shim_read32_on_16 = function(addr)\n                {\n                    dbg_log(\"Warning: 32-bit read from 16-bit virtio port\", LOG_VIRTIO);\n                    return read(addr);\n                };\n\n                switch(field.bytes)\n                {\n                    case 4:\n                        this.cpu.io.register_read(port, this, shim_read8_on_32, undefined, read);\n                        this.cpu.io.register_read(port + 1, this, shim_read8_on_32);\n                        this.cpu.io.register_read(port + 2, this, shim_read8_on_32);\n                        this.cpu.io.register_read(port + 3, this, shim_read8_on_32);\n                        this.cpu.io.register_write(port, this, undefined, undefined, write);\n                        break;\n                    case 2:\n                        this.cpu.io.register_read(port, this, shim_read8_on_16, read, shim_read32_on_16);\n                        this.cpu.io.register_read(port + 1, this, shim_read8_on_16);\n                        this.cpu.io.register_write(port, this, undefined, write);\n                        break;\n                    case 1:\n                        this.cpu.io.register_read(port, this, read);\n                        this.cpu.io.register_write(port, this, write);\n                        break;\n                    default:\n                        dbg_assert(false,\n                            \"VirtIO device <\" + this.name + \"> invalid capability field width of \" +\n                            field.bytes + \" bytes\");\n                        break;\n                }\n            }\n\n            port += field.bytes;\n        }\n    }\n\n    // Terminate linked list with the pci config access capability.\n\n    const cap_len = VIRTIO_PCI_CAP_LENGTH + 4;\n    dbg_assert(cap_next + cap_len <= 256,\n        \"VirtIO device<\" + this.name + \"> can't fit all capabilities into 256byte configspace\");\n    this.pci_space[cap_next] = VIRTIO_PCI_CAP_VENDOR;\n    this.pci_space[cap_next + 1] = 0; // cap next (null terminator)\n    this.pci_space[cap_next + 2] = cap_len;\n    this.pci_space[cap_next + 3] = VIRTIO_PCI_CAP_PCI_CFG; // cap type\n    this.pci_space[cap_next + 4] = 0; // bar (written by device)\n    this.pci_space[cap_next + 5] = 0; // Padding.\n    this.pci_space[cap_next + 6] = 0; // Padding.\n    this.pci_space[cap_next + 7] = 0; // Padding.\n\n    // Remaining fields are configured by driver when needed.\n\n    // offset\n    this.pci_space[cap_next + 8] = 0;\n    this.pci_space[cap_next + 9] = 0;\n    this.pci_space[cap_next + 10] = 0;\n    this.pci_space[cap_next + 11] = 0;\n\n    // bar size\n    this.pci_space[cap_next + 12] = 0;\n    this.pci_space[cap_next + 13] = 0;\n    this.pci_space[cap_next + 14] = 0;\n    this.pci_space[cap_next + 15] = 0;\n\n    // cfg_data\n    this.pci_space[cap_next + 16] = 0;\n    this.pci_space[cap_next + 17] = 0;\n    this.pci_space[cap_next + 18] = 0;\n    this.pci_space[cap_next + 19] = 0;\n\n    //\n    // TODO\n    // The pci config access capability is required by spec, but so far, devices\n    // seem to work well without it.\n    // This capability provides a cfg_data field (at cap_next + 16 for 4 bytes)\n    // that acts like a window to the previous bars. The driver writes the bar number,\n    // offset, and length values in this capability, and the cfg_data field should\n    // mirror the data referred by the bar, offset and length. Here, length can be\n    // 1, 2, or 4.\n    //\n    // This requires some sort of pci devicespace read and write handlers.\n};\n\nVirtIO.prototype.get_state = function()\n{\n    let state = [];\n\n    state[0] = this.device_feature_select;\n    state[1] = this.driver_feature_select;\n    state[2] = this.device_feature;\n    state[3] = this.driver_feature;\n    state[4] = this.features_ok;\n    state[5] = this.device_status;\n    state[6] = this.config_has_changed;\n    state[7] = this.config_generation;\n    state[8] = this.isr_status;\n    state[9] = this.queue_select;\n    state = state.concat(this.queues);\n\n    return state;\n};\n\nVirtIO.prototype.set_state = function(state)\n{\n    this.device_feature_select = state[0];\n    this.driver_feature_select = state[1];\n    this.device_feature = state[2];\n    this.driver_feature = state[3];\n    this.features_ok = state[4];\n    this.device_status = state[5];\n    this.config_has_changed = state[6];\n    this.config_generation = state[7];\n    this.isr_status = state[8];\n    this.queue_select = state[9];\n    let i = 0;\n    for(const queue of state.slice(10))\n    {\n        this.queues[i].set_state(queue);\n        i++;\n    }\n    this.queue_selected = this.queues[this.queue_select] || null;\n};\n\nVirtIO.prototype.reset = function()\n{\n    this.device_feature_select = 0;\n    this.driver_feature_select = 0;\n    this.driver_feature.set(this.device_feature);\n\n    this.features_ok = true;\n    this.device_status = 0;\n\n    this.queue_select = 0;\n    this.queue_selected = this.queues[0];\n\n    for(const queue of this.queues)\n    {\n        queue.reset();\n    }\n\n    this.config_has_changed = false;\n    this.config_generation = 0;\n\n    this.lower_irq();\n};\n\n/**\n * Call this when device-specific configuration state changes.\n * Also called when status DEVICE_NEEDS_RESET is set.\n */\nVirtIO.prototype.notify_config_changes = function()\n{\n    this.config_has_changed = true;\n\n    if(this.device_status & VIRTIO_STATUS_DRIVER_OK)\n    {\n        this.raise_irq(VIRTIO_ISR_DEVICE_CFG);\n    }\n    else\n    {\n        dbg_assert(false,\n            \"VirtIO device<\" + this.name + \"> attempted to notify driver before DRIVER_OK\");\n    }\n};\n\n/**\n * To be called after reading any field whose write can trigger notify_config_changes().\n */\nVirtIO.prototype.update_config_generation = function()\n{\n    if(this.config_has_changed)\n    {\n        this.config_generation++;\n        this.config_generation &= 0xFF;\n        this.config_has_changed = false;\n    }\n};\n\nVirtIO.prototype.is_feature_negotiated = function(feature)\n{\n    // Feature bits are grouped in 32 bits.\n    // Note: earlier we chose not to set invalid features into driver_feature.\n    return (this.driver_feature[feature >>> 5] & (1 << (feature & 0x1F))) > 0;\n};\n\n/**\n * Call this if an irrecoverable error has been occured.\n * Notifies driver if DRIVER_OK, or when DRIVER_OK gets set.\n */\nVirtIO.prototype.needs_reset = function()\n{\n    dbg_log(\"Device<\" + this.name + \"> experienced error - requires reset\", LOG_VIRTIO);\n    this.device_status |= VIRTIO_STATUS_DEVICE_NEEDS_RESET;\n\n    if(this.device_status & VIRTIO_STATUS_DRIVER_OK)\n    {\n        this.notify_config_changes();\n    }\n};\n\nVirtIO.prototype.raise_irq = function(type)\n{\n    dbg_log(\"Raise irq \" + h(type), LOG_VIRTIO);\n    this.isr_status |= type;\n    this.pci.raise_irq(this.pci_id);\n};\n\nVirtIO.prototype.lower_irq = function()\n{\n    dbg_log(\"Lower irq \", LOG_VIRTIO);\n    this.isr_status = 0;\n    this.pci.lower_irq(this.pci_id);\n};\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {VirtQueue_Options} options\n */\nfunction VirtQueue(cpu, virtio, options)\n{\n    /** @const @type {CPU} */\n    this.cpu = cpu;\n\n    /** @const @type {VirtIO} */\n    this.virtio = virtio;\n\n    // Number of entries.\n    this.size = options.size_supported;\n    this.size_supported = options.size_supported;\n    this.mask = this.size - 1;\n    this.enabled = false;\n    this.notify_offset = options.notify_offset;\n\n    this.desc_addr = 0;\n\n    this.avail_addr = 0;\n    this.avail_last_idx = 0;\n\n    this.used_addr = 0;\n    this.num_staged_replies = 0;\n\n    this.reset();\n}\n\nVirtQueue.prototype.get_state = function()\n{\n    const state = [];\n\n    state[0] = this.size;\n    state[1] = this.size_supported;\n    state[2] = this.enabled;\n    state[3] = this.notify_offset;\n    state[4] = this.desc_addr;\n    state[5] = this.avail_addr;\n    state[6] = this.avail_last_idx;\n    state[7] = this.used_addr;\n    state[8] = this.num_staged_replies;\n    state[9] = 1;\n\n    return state;\n};\n\nVirtQueue.prototype.set_state = function(state)\n{\n    this.size = state[0];\n    this.size_supported = state[1];\n    this.enabled = state[2];\n    this.notify_offset = state[3];\n    this.desc_addr = state[4];\n    this.avail_addr = state[5];\n    this.avail_last_idx = state[6];\n    this.used_addr = state[7];\n    this.num_staged_replies = state[8];\n\n    this.mask = this.size - 1;\n    this.fix_wrapping = state[9] !== 1;\n};\n\nVirtQueue.prototype.reset = function()\n{\n    this.enabled = false;\n    this.desc_addr = 0;\n    this.avail_addr = 0;\n    this.avail_last_idx = 0;\n    this.used_addr = 0;\n    this.num_staged_replies = 0;\n    this.set_size(this.size_supported);\n};\n\nVirtQueue.prototype.is_configured = function()\n{\n    return this.desc_addr && this.avail_addr && this.used_addr;\n};\n\nVirtQueue.prototype.enable = function()\n{\n    dbg_assert(this.is_configured(), \"VirtQueue must be configured before enabled\");\n    this.enabled = true;\n};\n\nVirtQueue.prototype.set_size = function(size)\n{\n    dbg_assert((size & size - 1) === 0, \"VirtQueue size must be power of 2 or zero\");\n    dbg_assert(size <= this.size_supported, \"VirtQueue size must be within supported size\");\n    this.size = size;\n    this.mask = size - 1;\n};\n\n/**\n * @return {number}\n */\nVirtQueue.prototype.count_requests = function()\n{\n    dbg_assert(this.avail_addr, \"VirtQueue addresses must be configured before use\");\n    if(this.fix_wrapping) {\n        this.fix_wrapping = false;\n        this.avail_last_idx = (this.avail_get_idx() & ~this.mask) + (this.avail_last_idx & this.mask);\n    }\n    return (this.avail_get_idx() - this.avail_last_idx) & 0xFFFF;\n};\n\n/**\n * @return {boolean}\n */\nVirtQueue.prototype.has_request = function()\n{\n    dbg_assert(this.avail_addr, \"VirtQueue addresses must be configured before use\");\n    return this.count_requests() !== 0;\n};\n\n/**\n * @return {VirtQueueBufferChain}\n */\nVirtQueue.prototype.pop_request = function()\n{\n    dbg_assert(this.avail_addr, \"VirtQueue addresses must be configured before use\");\n    dbg_assert(this.has_request(), \"VirtQueue must not pop nonexistent request\");\n\n    const desc_idx = this.avail_get_entry(this.avail_last_idx);\n    dbg_log(\"Pop request: avail_last_idx=\" + this.avail_last_idx +\n        \" desc_idx=\" + desc_idx, LOG_VIRTIO);\n\n    const bufchain = new VirtQueueBufferChain(this, desc_idx);\n\n    this.avail_last_idx = (this.avail_last_idx + 1) & 0xFFFF;\n\n    return bufchain;\n};\n\n/**\n * Stage a buffer chain into the used ring.\n * Can call push_reply many times before flushing to batch replies together.\n * Note: this reply is not visible to driver until flush_replies is called.\n * @param {VirtQueueBufferChain} bufchain\n */\nVirtQueue.prototype.push_reply = function(bufchain)\n{\n    dbg_assert(this.used_addr, \"VirtQueue addresses must be configured before use\");\n    dbg_assert(this.num_staged_replies < this.size, \"VirtQueue replies must not exceed queue size\");\n\n    const used_idx = this.used_get_idx() + this.num_staged_replies & this.mask;\n    dbg_log(\"Push reply: used_idx=\" + used_idx +\n        \" desc_idx=\" + bufchain.head_idx, LOG_VIRTIO);\n\n    this.used_set_entry(used_idx, bufchain.head_idx, bufchain.length_written);\n    this.num_staged_replies++;\n};\n\n/**\n * Makes replies visible to driver by updating the used ring idx and\n * firing appropriate interrupt if needed.\n */\nVirtQueue.prototype.flush_replies = function()\n{\n    dbg_assert(this.used_addr, \"VirtQueue addresses must be configured before use\");\n\n    if(this.num_staged_replies === 0)\n    {\n        dbg_log(\"flush_replies: Nothing to flush\", LOG_VIRTIO);\n        return;\n    }\n\n    dbg_log(\"Flushing \" + this.num_staged_replies + \" replies\", LOG_VIRTIO);\n    const old_idx = this.used_get_idx();\n    const new_idx = old_idx + this.num_staged_replies & VIRTQ_IDX_MASK;\n    this.used_set_idx(new_idx);\n\n    this.num_staged_replies = 0;\n\n    if(this.virtio.is_feature_negotiated(VIRTIO_F_RING_EVENT_IDX))\n    {\n        const used_event = this.avail_get_used_event();\n\n        // Fire irq when idx values associated with the pushed reply buffers\n        // has reached or gone past used_event.\n        let has_passed = old_idx <= used_event && used_event < new_idx;\n\n        // Has overflowed? Assumes num_staged_replies > 0.\n        if(new_idx <= old_idx)\n        {\n            has_passed = used_event < new_idx || old_idx <= used_event;\n        }\n\n        // Commented out: Workaround for sometimes loading from the filesystem hangs and the emulator stays idle\n        //if(has_passed)\n        {\n            this.virtio.raise_irq(VIRTIO_ISR_QUEUE);\n        }\n    }\n    else\n    {\n        if(~this.avail_get_flags() & VIRTQ_AVAIL_F_NO_INTERRUPT)\n        {\n            this.virtio.raise_irq(VIRTIO_ISR_QUEUE);\n        }\n    }\n};\n\n/**\n * If using VIRTIO_F_RING_EVENT_IDX, device must tell driver when\n * to get notifications or else driver won't notify regularly.\n * If not using VIRTIO_F_RING_EVENT_IDX, driver will ignore avail_event\n * and notify every request regardless unless NO_NOTIFY is set (TODO implement when needed).\n * @param {number} num_skipped_requests Zero = get notified in the next request.\n */\nVirtQueue.prototype.notify_me_after = function(num_skipped_requests)\n{\n    dbg_assert(num_skipped_requests >= 0, \"Must skip a non-negative number of requests\");\n\n    // The 16 bit idx field wraps around after 2^16.\n    const avail_event = this.avail_get_idx() + num_skipped_requests & 0xFFFF;\n    this.used_set_avail_event(avail_event);\n};\n\n/**\n * @param {number} table_address The physical address of the start of the desc table.\n * @param {number} i\n */\nVirtQueue.prototype.get_descriptor = function(table_address, i)\n{\n    return {\n        addr_low: this.cpu.read32s(table_address + i * VIRTQ_DESC_ENTRYSIZE),\n        addr_high: this.cpu.read32s(table_address + i * VIRTQ_DESC_ENTRYSIZE + 4),\n        len: this.cpu.read32s(table_address + i * VIRTQ_DESC_ENTRYSIZE + 8),\n        flags: this.cpu.read16(table_address + i * VIRTQ_DESC_ENTRYSIZE + 12),\n        next: this.cpu.read16(table_address + i * VIRTQ_DESC_ENTRYSIZE + 14),\n    };\n};\n\n// Avail ring fields\n\nVirtQueue.prototype.avail_get_flags = function()\n{\n    return this.cpu.read16(this.avail_addr);\n};\n\nVirtQueue.prototype.avail_get_idx = function()\n{\n    return this.cpu.read16(this.avail_addr + 2);\n};\n\nVirtQueue.prototype.avail_get_entry = function(i)\n{\n    return this.cpu.read16(this.avail_addr + 4 + VIRTQ_AVAIL_ENTRYSIZE * (i & this.mask));\n};\n\nVirtQueue.prototype.avail_get_used_event = function()\n{\n    return this.cpu.read16(this.avail_addr + 4 + VIRTQ_AVAIL_ENTRYSIZE * this.size);\n};\n\n// Used ring fields\n\nVirtQueue.prototype.used_get_flags = function()\n{\n    return this.cpu.read16(this.used_addr);\n};\n\nVirtQueue.prototype.used_set_flags = function(value)\n{\n    this.cpu.write16(this.used_addr, value);\n};\n\nVirtQueue.prototype.used_get_idx = function()\n{\n    return this.cpu.read16(this.used_addr + 2);\n};\n\nVirtQueue.prototype.used_set_idx = function(value)\n{\n    this.cpu.write16(this.used_addr + 2, value);\n};\n\nVirtQueue.prototype.used_set_entry = function(i, desc_idx, length_written)\n{\n    this.cpu.write32(this.used_addr + 4 + VIRTQ_USED_ENTRYSIZE * i, desc_idx);\n    this.cpu.write32(this.used_addr + 8 + VIRTQ_USED_ENTRYSIZE * i, length_written);\n};\n\nVirtQueue.prototype.used_set_avail_event = function(value)\n{\n    this.cpu.write16(this.used_addr + 4 + VIRTQ_USED_ENTRYSIZE * this.size, value);\n};\n\n/**\n * Traverses through descriptor chain starting at head_id.\n * Provides means to read/write to buffers represented by the descriptors.\n * @constructor\n * @param {VirtQueue} virtqueue\n * @param {number} head_idx\n */\nfunction VirtQueueBufferChain(virtqueue, head_idx)\n{\n    /** @const @type {CPU} */\n    this.cpu = virtqueue.cpu;\n\n    /** @const @type {VirtIO} */\n    this.virtio = virtqueue.virtio;\n\n    this.head_idx = head_idx;\n\n    this.read_buffers = [];\n    // Pointers for sequential consumption via get_next_blob.\n    this.read_buffer_idx = 0;\n    this.read_buffer_offset = 0;\n    this.length_readable = 0;\n\n    this.write_buffers = [];\n    // Pointers for sequential write via set_next_blob.\n    this.write_buffer_idx = 0;\n    this.write_buffer_offset = 0;\n    this.length_written = 0;\n    this.length_writable = 0;\n\n    // Traverse chain to discover buffers.\n    // - There shouldn't be an excessive amount of descriptor elements.\n    let table_address = virtqueue.desc_addr;\n    let desc_idx = head_idx;\n    let chain_length = 0;\n    let chain_max = virtqueue.size;\n    let writable_region = false;\n    const has_indirect_feature = this.virtio.is_feature_negotiated(VIRTIO_F_RING_INDIRECT_DESC);\n    dbg_log(\"<<< Descriptor chain start\", LOG_VIRTIO);\n    do\n    {\n        const desc = virtqueue.get_descriptor(table_address, desc_idx);\n\n        dbg_log(\"descriptor: idx=\" + desc_idx + \" addr=\" + h(desc.addr_high, 8) + \":\" + h(desc.addr_low, 8) +\n            \" len=\" + h(desc.len, 8) + \" flags=\" + h(desc.flags, 4) + \" next=\" + h(desc.next, 4), LOG_VIRTIO);\n\n        if(has_indirect_feature && (desc.flags & VIRTQ_DESC_F_INDIRECT))\n        {\n            if(DEBUG && (desc.flags & VIRTQ_DESC_F_NEXT))\n            {\n                dbg_log(\"Driver bug: has set VIRTQ_DESC_F_NEXT flag in an indirect table descriptor\", LOG_VIRTIO);\n            }\n\n            // Carry on using indirect table, starting at first entry.\n            table_address = desc.addr_low;\n            desc_idx = 0;\n            chain_length = 0;\n            chain_max = desc.len / VIRTQ_DESC_ENTRYSIZE;\n            dbg_log(\"start indirect\", LOG_VIRTIO);\n            continue;\n        }\n\n        if(desc.flags & VIRTQ_DESC_F_WRITE)\n        {\n            writable_region = true;\n            this.write_buffers.push(desc);\n            this.length_writable += desc.len;\n        }\n        else\n        {\n            if(writable_region)\n            {\n                dbg_log(\"Driver bug: readonly buffer after writeonly buffer within chain\", LOG_VIRTIO);\n                break;\n            }\n            this.read_buffers.push(desc);\n            this.length_readable += desc.len;\n        }\n\n        chain_length++;\n        if(chain_length > chain_max)\n        {\n            dbg_log(\"Driver bug: descriptor chain cycle detected\", LOG_VIRTIO);\n            break;\n        }\n\n        if(desc.flags & VIRTQ_DESC_F_NEXT)\n        {\n            desc_idx = desc.next;\n        }\n        else\n        {\n            break;\n        }\n    }\n    while(true);\n    dbg_log(\"Descriptor chain end >>>\", LOG_VIRTIO);\n}\n\n/**\n * Reads the next blob of memory represented by the buffer chain into dest_buffer.\n * @param {Uint8Array} dest_buffer\n * @return {number} Number of bytes successfully read.\n */\nVirtQueueBufferChain.prototype.get_next_blob = function(dest_buffer)\n{\n    let dest_offset = 0;\n    let remaining = dest_buffer.length;\n\n    while(remaining)\n    {\n        if(this.read_buffer_idx === this.read_buffers.length)\n        {\n            dbg_log(\"Device<\" + this.virtio.name + \"> Read more than device-readable buffers has\", LOG_VIRTIO);\n            break;\n        }\n\n        const buf = this.read_buffers[this.read_buffer_idx];\n        const read_address = buf.addr_low + this.read_buffer_offset;\n        let read_length = buf.len - this.read_buffer_offset;\n\n        if(read_length > remaining)\n        {\n            read_length = remaining;\n            this.read_buffer_offset += remaining;\n        }\n        else\n        {\n            this.read_buffer_idx++;\n            this.read_buffer_offset = 0;\n        }\n\n        dest_buffer.set(this.cpu.read_blob(read_address, read_length), dest_offset);\n\n        dest_offset += read_length;\n        remaining -= read_length;\n    }\n\n    return dest_offset;\n};\n\n/**\n * Appends contents of src_buffer into the memory represented by the buffer chain.\n * @param {Uint8Array} src_buffer\n * @return {number} Number of bytes successfully written.\n */\nVirtQueueBufferChain.prototype.set_next_blob = function(src_buffer)\n{\n    let src_offset = 0;\n    let remaining = src_buffer.length;\n\n    while(remaining)\n    {\n        if(this.write_buffer_idx === this.write_buffers.length)\n        {\n            dbg_log(\"Device<\" + this.virtio.name + \"> Write more than device-writable capacity\", LOG_VIRTIO);\n            break;\n        }\n\n        const buf = this.write_buffers[this.write_buffer_idx];\n        const write_address = buf.addr_low + this.write_buffer_offset;\n        let write_length = buf.len - this.write_buffer_offset;\n\n        if(write_length > remaining)\n        {\n            write_length = remaining;\n            this.write_buffer_offset += remaining;\n        }\n        else\n        {\n            this.write_buffer_idx++;\n            this.write_buffer_offset = 0;\n        }\n\n        const src_end = src_offset + write_length;\n        this.cpu.write_blob(src_buffer.subarray(src_offset, src_end), write_address);\n\n        src_offset += write_length;\n        remaining -= write_length;\n    }\n\n    this.length_written += src_offset;\n    return src_offset;\n};\n"
  },
  {
    "path": "src/virtio_balloon.js",
    "content": "// https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-2900003\n\nimport { LOG_PCI } from \"./const.js\";\nimport { dbg_log } from \"./log.js\";\nimport { VirtIO, VIRTIO_F_VERSION_1 } from \"./virtio.js\";\nimport * as marshall from \"../lib/marshall.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { BusConnector } from \"./bus.js\";\n\nconst VIRTIO_BALLOON_F_MUST_TELL_HOST = 0;\nconst VIRTIO_BALLOON_F_STATS_VQ = 1;\nconst VIRTIO_BALLOON_F_DEFLATE_ON_OOM = 2;\nconst VIRTIO_BALLOON_F_FREE_PAGE_HINT = 3;\n\nconst STAT_NAMES = [\n    \"SWAP_IN\",\n    \"SWAP_OUT\",\n    \"MAJFLT\",\n    \"MINFLT\",\n    \"MEMFREE\",\n    \"MEMTOT\",\n    \"AVAIL\",\n    \"CACHES\",\n    \"HTLB_PGALLOC\",\n    \"HTLB_PGFAIL\",\n];\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n */\nexport function VirtioBalloon(cpu, bus)\n{\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n    this.num_pages = 0;\n    this.actual = 0;\n    this.fp_cmd = 0;\n    this.zeroed = 0;\n\n    const queues = [\n        {size_supported: 32, notify_offset: 0},\n        {size_supported: 32, notify_offset: 0},\n        {size_supported: 2, notify_offset: 1},\n        {size_supported: 64, notify_offset: 2},\n    ];\n\n    //setInterval(() => this.GetStats(console.log.bind(console, \"STATS\")), 10000);\n\n    /** @type {VirtIO} */\n    this.virtio = new VirtIO(cpu,\n    {\n        name: \"virtio-balloon\",\n        pci_id: 0x0B << 3,\n        device_id: 0x1045,\n        subsystem_device_id: 5,\n        common:\n        {\n            initial_port: 0xD800,\n            queues: queues,\n            features:\n            [\n                VIRTIO_BALLOON_F_STATS_VQ,\n                VIRTIO_BALLOON_F_FREE_PAGE_HINT,\n                VIRTIO_F_VERSION_1,\n            ],\n            on_driver_ok: () => {\n                dbg_log(\"Balloon setup\", LOG_PCI);\n            },\n        },\n        notification:\n        {\n            initial_port: 0xD900,\n            single_handler: false,\n            handlers:\n            [\n                (queue_id) =>\n                {\n                    const queue = this.virtio.queues[queue_id];\n                    while(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        const buffer = new Uint8Array(bufchain.length_readable);\n                        bufchain.get_next_blob(buffer);\n                        this.virtio.queues[queue_id].push_reply(bufchain);\n                        let n = buffer.byteLength / 4;\n                        this.actual += (queue_id === 0 ? n : -n);\n                        //console.log(queue_id === 0 ? \"Inflate\" : \"Deflate\", this.num_pages, this.actual, bufchain.read_buffers);\n                    }\n                    this.virtio.queues[queue_id].flush_replies();\n                },\n                (queue_id) =>\n                {\n                    const queue = this.virtio.queues[queue_id];\n                    if(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        const buffer = new Uint8Array(bufchain.length_readable);\n                        bufchain.get_next_blob(buffer);\n                        let result = {};\n                        for(let i = 0; i < bufchain.length_readable; i += 10) {\n                            let [cat, value] = marshall.Unmarshall([\"h\", \"d\"], buffer, { offset : i });\n                            result[STAT_NAMES[cat]] = value;\n                        }\n                        this.virtio.queues[queue_id].push_reply(bufchain);\n                        if(this.stats_cb) this.stats_cb(result);\n                    }\n                },\n                (queue_id) =>\n                {\n                    const queue = this.virtio.queues[queue_id];\n                    while(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        if(bufchain.length_readable > 0) {\n                            const buffer = new Uint8Array(bufchain.length_readable);\n                            bufchain.get_next_blob(buffer);\n                            let [cmd] = marshall.Unmarshall([\"w\"], buffer, { offset : 0 });\n                            if(cmd === 0) {\n                                if(this.free_cb) this.free_cb(this.zeroed);\n                                if(this.fp_cmd > 1) this.fp_cmd = 1; // Signal done\n                                this.virtio.notify_config_changes();\n                            }\n                        }\n                        if(bufchain.length_writable > 0) {\n                            // console.log(\"Free pages hinted\", bufchain.read_buffers, bufchain.write_buffers);\n                            let zeros = new Uint8Array(0);\n                            for(let i = 0; i < bufchain.write_buffers.length; ++i) {\n                                let b = bufchain.write_buffers[i];\n                                this.zeroed += b.len;\n                                this.virtio.cpu.zero_memory(b.addr_low, b.len);\n                            }\n                        }\n                        this.virtio.queues[queue_id].push_reply(bufchain);\n                    }\n                    this.virtio.queues[queue_id].flush_replies();\n                },\n            ],\n        },\n        isr_status:\n        {\n            initial_port: 0xD700,\n        },\n        device_specific:\n        {\n            initial_port: 0xD600,\n            struct:\n            [\n                {\n                    bytes: 4,\n                    name: \"num_pages\",\n                    read: () => this.num_pages,\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 4,\n                    name: \"actual\",\n                    read: () => {\n                        return this.actual;\n                    },\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 4,\n                    name: \"free_page_hint_cmd_id\",\n                    read: () => this.fp_cmd,\n                    write: data => { /* read only */ },\n                }\n           ]\n        },\n    });\n}\n\nVirtioBalloon.prototype.Inflate = function(amount) {\n    this.num_pages += amount;\n    this.virtio.notify_config_changes();\n};\n\nVirtioBalloon.prototype.Deflate = function(amount) {\n    this.num_pages -= amount;\n    this.virtio.notify_config_changes();\n};\n\nVirtioBalloon.prototype.Cleanup = function(cb) {\n    this.fp_cmd = 2;\n    this.free_cb = cb;\n    this.zeroed = 0;\n    this.virtio.notify_config_changes();\n};\n\n\nVirtioBalloon.prototype.get_state = function()\n{\n    const state = [];\n    state[0] = this.virtio;\n    state[1] = this.num_pages;\n    state[2] = this.actual;\n    return state;\n};\n\nVirtioBalloon.prototype.set_state = function(state)\n{\n    this.virtio.set_state(state[0]);\n    this.num_pages = state[1];\n    this.actual = state[2];\n};\n\nVirtioBalloon.prototype.GetStats = function(data)\n{\n    this.stats_cb = data;\n    const queue = this.virtio.queues[2];\n    while(queue.has_request())\n    {\n        const bufchain = queue.pop_request();\n        this.virtio.queues[2].push_reply(bufchain);\n    }\n    this.virtio.queues[2].flush_replies();\n};\n\nVirtioBalloon.prototype.Reset = function() {\n\n};\n"
  },
  {
    "path": "src/virtio_console.js",
    "content": "import { dbg_assert } from \"./log.js\";\nimport { VirtIO, VIRTIO_F_VERSION_1 } from \"./virtio.js\";\nimport * as marshall from \"../lib/marshall.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { BusConnector } from \"./bus.js\";\n\n// https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-2900003\n\nconst VIRTIO_CONSOLE_DEVICE_READY     = 0;\nconst VIRTIO_CONSOLE_DEVICE_ADD       = 1;\nconst VIRTIO_CONSOLE_DEVICE_REMOVE    = 2;\nconst VIRTIO_CONSOLE_PORT_READY       = 3;\nconst VIRTIO_CONSOLE_CONSOLE_PORT     = 4;\nconst VIRTIO_CONSOLE_RESIZE           = 5;\nconst VIRTIO_CONSOLE_PORT_OPEN        = 6;\nconst VIRTIO_CONSOLE_PORT_NAME        = 7;\n\nconst VIRTIO_CONSOLE_F_SIZE           = 0;\nconst VIRTIO_CONSOLE_F_MULTIPORT      = 1;\nconst VIRTIO_CONSOLE_F_EMERG_WRITE    = 2;\n\n/**\n * @constructor\n *\n * @param {CPU} cpu\n */\nexport function VirtioConsole(cpu, bus)\n{\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n    this.rows = 25;\n    this.cols = 80;\n    this.ports = 4;\n\n    const queues = [\n        {\n            size_supported: 16,\n            notify_offset: 0,\n        },\n        {\n            size_supported: 16,\n            notify_offset: 1,\n        },\n        {\n            size_supported: 16,\n            notify_offset: 2,\n        },\n        {\n            size_supported: 16,\n            notify_offset: 3,\n        },\n    ];\n\n    for(let i = 1; i < this.ports; ++i)\n    {\n        queues.push({size_supported: 16, notify_offset: 0});\n        queues.push({size_supported: 8, notify_offset: 1});\n    }\n\n    /** @type {VirtIO} */\n    this.virtio = new VirtIO(cpu,\n    {\n        name: \"virtio-console\",\n        pci_id: 0x0C << 3,\n        device_id: 0x1043,\n        subsystem_device_id: 3,\n        common:\n        {\n            initial_port: 0xB800,\n            queues: queues,\n            features:\n            [\n                VIRTIO_CONSOLE_F_SIZE,\n                VIRTIO_CONSOLE_F_MULTIPORT,\n                VIRTIO_F_VERSION_1,\n            ],\n            on_driver_ok: () => {},\n        },\n        notification:\n        {\n            initial_port: 0xB900,\n            single_handler: false,\n            handlers:\n            [\n                (queue_id) =>\n                {\n\n                },\n                (queue_id) =>\n                {\n                    const queue = this.virtio.queues[queue_id];\n                    const port = queue_id > 3 ? (queue_id-3 >> 1) : 0;\n                    while(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        const buffer = new Uint8Array(bufchain.length_readable);\n                        bufchain.get_next_blob(buffer);\n                        this.bus.send(\"virtio-console\" + port + \"-output-bytes\", buffer);\n                        this.Ack(queue_id, bufchain);\n                    }\n                },\n                (queue_id) =>\n                {\n                    if(queue_id !== 2)\n                    {\n                        dbg_assert(false, \"VirtioConsole Notified for wrong queue: \" + queue_id +\n                            \" (expected queue_id of 2)\");\n\n                    }\n\n                },\n                (queue_id) =>\n                {\n                    if(queue_id !== 3)\n                    {\n                        dbg_assert(false, \"VirtioConsole Notified for wrong queue: \" + queue_id +\n                            \" (expected queue_id of 3)\");\n                        return;\n                    }\n                    const queue = this.virtio.queues[queue_id];\n\n                    while(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        const buffer = new Uint8Array(bufchain.length_readable);\n                        bufchain.get_next_blob(buffer);\n\n\n                        const parts = marshall.Unmarshall([\"w\", \"h\", \"h\"], buffer, { offset : 0 });\n                        const port = parts[0];\n                        const event = parts[1];\n                        const value = parts[2];\n\n\n                        this.Ack(queue_id, bufchain);\n\n                        switch(event) {\n                            case VIRTIO_CONSOLE_DEVICE_READY:\n                                for(let i = 0; i < this.ports; ++i) {\n                                    this.SendEvent(i, VIRTIO_CONSOLE_DEVICE_ADD, 0);\n                                }\n                                break;\n                            case VIRTIO_CONSOLE_PORT_READY:\n                                this.Ack(queue_id, bufchain);\n                                this.SendEvent(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);\n                                this.SendName(port, \"virtio-\" + port);\n                                this.SendEvent(port, VIRTIO_CONSOLE_PORT_OPEN, 1);\n\n                                break;\n                            case VIRTIO_CONSOLE_PORT_OPEN:\n                                this.Ack(queue_id, bufchain);\n                                if(port === 0) {\n                                    this.SendWindowSize(port);\n                                }\n                                break;\n                            default:\n                                dbg_assert(false,\" VirtioConsole received unknown event: \" + event[1]);\n                                return;\n\n                        }\n                    }\n                },\n            ],\n        },\n        isr_status:\n        {\n            initial_port: 0xB700,\n        },\n        device_specific:\n        {\n            initial_port: 0xB600,\n            struct:\n            [\n                {\n                    bytes: 2,\n                    name: \"cols\",\n                    read: () => this.cols,\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 2,\n                    name: \"rows\",\n                    read: () => this.rows,\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 4,\n                    name: \"max_nr_ports\",\n                    read: () => this.ports,\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 4,\n                    name: \"emerg_wr\",\n                    read: () => 0,\n                    write: data => {\n                        dbg_assert(false, \"Emergency write!\");\n                    },\n                },\n           ]\n        },\n    });\n\n    for(let port = 0; port < this.ports; ++port) {\n        const queue_id = port === 0 ? 0 : port * 2 + 2;\n        this.bus.register(\"virtio-console\" + port + \"-input-bytes\", function(data) {\n            const queue = this.virtio.queues[queue_id];\n            if(queue.has_request()) {\n                const bufchain = queue.pop_request();\n                this.Send(queue_id, bufchain, new Uint8Array(data));\n            } else {\n                //TODO: Buffer\n            }\n        }, this);\n\n        this.bus.register(\"virtio-console\" + port + \"-resize\", function(size) {\n            if(port === 0) {\n                this.cols = size[0];\n                this.rows = size[1];\n            }\n\n            if(this.virtio.queues[2].is_configured() && this.virtio.queues[2].has_request()) {\n                this.SendWindowSize(port, size[0], size[1]);\n            }\n        }, this);\n    }\n}\n\nVirtioConsole.prototype.SendWindowSize = function(port, cols = undefined, rows = undefined)\n{\n    rows = rows || this.rows;\n    cols = cols || this.cols;\n    const bufchain = this.virtio.queues[2].pop_request();\n    const buf = new Uint8Array(12);\n    marshall.Marshall([\"w\", \"h\", \"h\", \"h\", \"h\"], [port, VIRTIO_CONSOLE_RESIZE, 0, rows, cols], buf, 0);\n    this.Send(2, bufchain, buf);\n};\n\nVirtioConsole.prototype.SendName = function(port, name)\n{\n    const bufchain = this.virtio.queues[2].pop_request();\n    const namex = new TextEncoder().encode(name);\n    const buf = new Uint8Array(8 + namex.length + 1);\n    marshall.Marshall([\"w\", \"h\", \"h\"], [port, VIRTIO_CONSOLE_PORT_NAME, 1], buf, 0);\n    for( let i = 0; i < namex.length; ++i ) {\n        buf[i+8] = namex[i];\n    }\n    buf[8 + namex.length] = 0;\n    this.Send(2, bufchain, buf);\n};\n\n\nVirtioConsole.prototype.get_state = function()\n{\n    const state = [];\n\n    state[0] = this.virtio;\n    state[1] = this.rows;\n    state[2] = this.cols;\n    state[3] = this.ports;\n\n    return state;\n};\n\nVirtioConsole.prototype.set_state = function(state)\n{\n    this.virtio.set_state(state[0]);\n    this.rows = state[1];\n    this.cols = state[2];\n    this.ports = state[3];\n};\n\nVirtioConsole.prototype.reset = function() {\n    this.virtio.reset();\n};\n\nVirtioConsole.prototype.SendEvent = function(port, event, value)\n{\n    const queue = this.virtio.queues[2];\n    const bufchain = queue.pop_request();\n\n    const buf = new Uint8Array(8);\n    marshall.Marshall([\"w\",\"h\",\"h\"], [port, event, value], buf, 0);\n    this.Send(2, bufchain, buf);\n};\n\nVirtioConsole.prototype.Send = function (queue_id, bufchain, blob)\n{\n    bufchain.set_next_blob(blob);\n    this.virtio.queues[queue_id].push_reply(bufchain);\n    this.virtio.queues[queue_id].flush_replies();\n};\n\nVirtioConsole.prototype.Ack = function (queue_id, bufchain)\n{\n    bufchain.set_next_blob(new Uint8Array(0));\n    this.virtio.queues[queue_id].push_reply(bufchain);\n    this.virtio.queues[queue_id].flush_replies();\n};\n"
  },
  {
    "path": "src/virtio_net.js",
    "content": "// https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-2900003\n\nimport { dbg_assert } from \"./log.js\";\nimport { VirtIO, VIRTIO_F_VERSION_1 } from \"./virtio.js\";\nimport { format_mac } from \"./ne2k.js\";\nimport * as marshall from \"../lib/marshall.js\";\n\n// For Types Only\nimport { CPU } from \"./cpu.js\";\nimport { BusConnector } from \"./bus.js\";\n\nconst MTU_DEFAULT = 1500;\n\nconst VIRTIO_NET_F_MAC = 5;\nconst VIRTIO_NET_F_CTRL_VQ = 17;\nconst VIRTIO_NET_F_STATUS = 16;\nconst VIRTIO_NET_F_MQ = 22;\nconst VIRTIO_NET_F_CTRL_MAC_ADDR = 23;\nconst VIRTIO_NET_F_MTU = 3;\n\nconst VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET = 0;\nconst VIRTIO_NET_CTRL_MAC_ADDR_SET = 1;\n\n/**\n * @constructor\n * @param {CPU} cpu\n * @param {BusConnector} bus\n * @param {Boolean} preserve_mac_from_state_image\n * @param {number} mtu\n */\nexport function VirtioNet(cpu, bus, preserve_mac_from_state_image, mtu = MTU_DEFAULT)\n{\n    /** @const @type {BusConnector} */\n    this.bus = bus;\n    this.id = cpu.devices.net ? 1 : 0;\n    this.pairs = 1;\n    this.status = 1;\n    this.preserve_mac_from_state_image = preserve_mac_from_state_image;\n    this.mac = new Uint8Array([\n        0x00, 0x22, 0x15,\n        Math.random() * 255 | 0,\n        Math.random() * 255 | 0,\n        Math.random() * 255 | 0,\n    ]);\n\n    this.bus.send(\"net\" + this.id + \"-mac\", format_mac(this.mac));\n\n    const queues = [];\n\n    for(let i = 0; i < this.pairs; ++i)\n    {\n        queues.push({size_supported: 1024, notify_offset: 0});\n        queues.push({size_supported: 1024, notify_offset: 1});\n    }\n    queues.push({\n        size_supported: 16,\n        notify_offset: 2,\n    });\n\n    /** @type {VirtIO} */\n    this.virtio = new VirtIO(cpu,\n    {\n        name: \"virtio-net\",\n        pci_id: 0x0A << 3,\n        device_id: 0x1041,\n        subsystem_device_id: 1,\n        common:\n        {\n            initial_port: 0xC800,\n            queues: queues,\n            features:\n            [\n                VIRTIO_NET_F_MAC,\n                VIRTIO_NET_F_STATUS,\n                VIRTIO_NET_F_MQ,\n                VIRTIO_NET_F_MTU,\n                VIRTIO_NET_F_CTRL_VQ,\n                VIRTIO_NET_F_CTRL_MAC_ADDR,\n                VIRTIO_F_VERSION_1,\n            ],\n            on_driver_ok: () => {},\n        },\n        notification:\n        {\n            initial_port: 0xC900,\n            single_handler: false,\n            handlers:\n            [\n                (queue_id) =>\n                {\n\n                },\n                (queue_id) =>\n                {\n                    const queue = this.virtio.queues[queue_id];\n\n                    while(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        const buffer = new Uint8Array(bufchain.length_readable);\n                        bufchain.get_next_blob(buffer);\n                        this.bus.send(\"net\" + this.id + \"-send\", buffer.subarray(12));\n                        this.bus.send(\"eth-transmit-end\", [buffer.length - 12]);\n                        this.virtio.queues[queue_id].push_reply(bufchain);\n                    }\n                    this.virtio.queues[queue_id].flush_replies();\n                },\n                (queue_id) =>\n                {\n                    if(queue_id !== this.pairs * 2)\n                    {\n                        dbg_assert(false, \"VirtioNet Notified for wrong queue: \" + queue_id +\n                            \" (expected queue_id of 3)\");\n                        return;\n                    }\n                    const queue = this.virtio.queues[queue_id];\n\n                    while(queue.has_request())\n                    {\n                        const bufchain = queue.pop_request();\n                        const buffer = new Uint8Array(bufchain.length_readable);\n                        bufchain.get_next_blob(buffer);\n\n\n                        const parts = marshall.Unmarshall([\"b\", \"b\"], buffer, { offset : 0 });\n                        const xclass = parts[0];\n                        const command = parts[1];\n\n\n                        //this.Ack(queue_id, bufchain);\n\n                        switch(xclass << 8 | command) {\n                            case 4 << 8 | VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET:\n                                const data =  marshall.Unmarshall([\"h\"], buffer, { offset : 2 });\n                                dbg_assert(data[0] === 1);\n                                this.Send(queue_id, bufchain, new Uint8Array([0]));\n                                break;\n                            case 1 << 8 | VIRTIO_NET_CTRL_MAC_ADDR_SET:\n                                this.mac = buffer.subarray(2, 8);\n                                this.Send(queue_id, bufchain, new Uint8Array([0]));\n                                this.bus.send(\"net\" + this.id + \"-mac\", format_mac(this.mac));\n                                break;\n                            default:\n                                dbg_assert(false,\" VirtioNet received unknown command: \" + xclass + \":\" + command);\n                                this.Send(queue_id, bufchain, new Uint8Array([1]));\n                                return;\n\n                        }\n                    }\n                },\n            ],\n        },\n        isr_status:\n        {\n            initial_port: 0xC700,\n        },\n        device_specific:\n        {\n            initial_port: 0xC600,\n            struct:\n            [0,1,2,3,4,5].map((v,k) => ({\n                bytes: 1,\n                name: \"mac_\" + k,\n                read: () => this.mac[k],\n                write: data => { /* read only */ },\n            })).concat(\n            [\n                {\n                    bytes: 2,\n                    name: \"status\",\n                    read: () => this.status,\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 2,\n                    name: \"max_pairs\",\n                    read: () => this.pairs,\n                    write: data => { /* read only */ },\n                },\n                {\n                    bytes: 2,\n                    name: \"mtu\",\n                    read: () => mtu,\n                    write: data => {},\n                }\n           ])\n        },\n    });\n\n    this.bus.register(\"net\" + this.id + \"-receive\", data => {\n        this.bus.send(\"eth-receive-end\", [data.length]);\n        const with_header = new Uint8Array(12 + data.byteLength);\n        const view = new DataView(with_header.buffer, with_header.byteOffset, with_header.byteLength);\n        view.setInt16(10, 1);\n        with_header.set(data, 12);\n\n        const queue = this.virtio.queues[0];\n        if(queue.has_request()) {\n            const bufchain = queue.pop_request();\n            bufchain.set_next_blob(with_header);\n            this.virtio.queues[0].push_reply(bufchain);\n            this.virtio.queues[0].flush_replies();\n        } else {\n            console.log(\"No buffer to write into!\");\n        }\n    }, this);\n\n}\n\n\nVirtioNet.prototype.get_state = function()\n{\n    const state = [];\n    state[0] = this.virtio;\n    state[1] = this.id;\n    state[2] = this.mac;\n    return state;\n};\n\nVirtioNet.prototype.set_state = function(state)\n{\n    this.virtio.set_state(state[0]);\n    this.id = state[1];\n    if(this.preserve_mac_from_state_image)\n    {\n        this.mac = state[2];\n        this.bus.send(\"net\" + this.id + \"-mac\", format_mac(this.mac));\n    }\n};\n\nVirtioNet.prototype.reset = function() {\n    this.virtio.reset();\n};\n\nVirtioNet.prototype.Send = function (queue_id, bufchain, blob)\n{\n    bufchain.set_next_blob(blob);\n    this.virtio.queues[queue_id].push_reply(bufchain);\n    this.virtio.queues[queue_id].flush_replies();\n};\n\nVirtioNet.prototype.Ack = function (queue_id, bufchain)\n{\n    //bufchain.set_next_blob(new Uint8Array(0));\n    this.virtio.queues[queue_id].push_reply(bufchain);\n    this.virtio.queues[queue_id].flush_replies();\n};\n"
  },
  {
    "path": "tests/Readme.md",
    "content": "Use the corresponding `make` target in the root directory to run a test. The\nfollowing list is roughtly sorted from most interesting/useful to least.\n\n- [nasm](nasm/): Small unit tests written in assembly, which are run using gdb\n  on the host.\n- [qemu](qemu/): Based on tests from qemu. Builds a Linux binary, which tests\n  many CPU features, which are then compared to a run on qemu.\n- [kvm-unit-test](kvm-unit-test/): Based on tests from the KVM project, tests\n  various CPU features.\n- [full](full/): Starts several OSes and checks if they boot correctly.\n- [jit-paging](jit-paging/): Tests jit and paging interaction.\n- [api](api/): Tests for several API functions of v86.\n- [devices](devices/): Device tests.\n- [rust](rust/): Rust unit test helpers.\n- [expect](expect/): Expect tests for the jit output. Contains a set of\n  asm+wasm files, where the jit is expected to produce the wasm file given the\n  asm file.\n\nThe following environmental variables are respected by most tests if applicable:\n\n- `TEST_RELEASE_BUILD=1`: Test the release build (libv86.js, v86.wasm) instead of the\n  debug build (source files with v86-debug.wasm)\n- `MAX_PARALLEL_TESTS=n`: Maximum number of tests to run in parallel. Defaults\n  to the number of cores in your system or less.\n- `TEST_NAME=\"…\"`: Run only the specified test (only expect, full, nasm)\n"
  },
  {
    "path": "tests/api/2g-mem.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\nimport fs from \"node:fs\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst config = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    bzimage: { url: __dirname + \"/../../images/buildroot-bzimage68.bin\" },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 2 * 1024 * 1024 * 1024,\n    filesystem: {},\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n};\n\nconst emulator = new V86(config);\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n\n    emulator.create_file(\"test.lua\", Buffer.from(`\nlocal t = {}\nlocal m = 1\nwhile collectgarbage(\"count\") < 1.8 * 1024 * 1024 do\n    t[m] = string.rep(\"A\", 4096)\n    m = m + 1\n    if m % 10000 == 0 then\n        print(m, \" \", collectgarbage(\"count\"))\n    end\nend\nprint(\"memory usage (kB) \", collectgarbage(\"count\"))\nprint(\"page count \", m)\nlocal ref = string.rep(\"A\", 4096)\nfor i = 1, m - 1 do\n    assert(t[i] == ref)\nend\nprint(\"ok\")\n`));\n});\n\nlet ran_command = false;\nlet line = \"\";\n\nemulator.add_listener(\"serial0-output-byte\", async function(byte)\n{\n    const chr = String.fromCharCode(byte);\n\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(chr === \"\\n\")\n    {\n        var new_line = line;\n        console.error(\"Serial: %s\", line);\n        line = \"\";\n    }\n    else if(chr >= \" \" && chr <= \"~\")\n    {\n        line += chr;\n    }\n\n    if(!ran_command && line.endsWith(\"~% \"))\n    {\n        ran_command = true;\n\n        emulator.serial0_send(\"free -m\\n\");\n        emulator.serial0_send(\"time -v lua /mnt/test.lua\\n\");\n        emulator.serial0_send(\"echo test fini''shed\\n\");\n    }\n\n    if(chr === \"\\n\" && new_line.startsWith(\"test finished\"))\n    {\n        emulator.destroy();\n    }\n});\n"
  },
  {
    "path": "tests/api/cdrom-insert-eject.js",
    "content": "#!/usr/bin/env node\n\nimport { setTimeout as pause } from \"timers/promises\";\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    hda: { url: __dirname + \"/../../images/msdos622.img\" },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n});\n\n//const interval = setInterval(() => {\n//    console.warn(emulator.screen_adapter.get_text_screen());\n//}, 1000);\n\nconst timeout = setTimeout(() => {\n    console.warn(emulator.screen_adapter.get_text_screen());\n    throw new Error(\"Timeout\");\n}, 60 * 1000);\n\nsetTimeout(async () =>\n{\n    await emulator.wait_until_vga_screen_contains(\"C:\\\\> \");\n    console.log(\"Got C:\\\\>\");\n    await pause(1000);\n    emulator.keyboard_send_text(\"dir D:\\n\");\n    await emulator.wait_until_vga_screen_contains(\"Abort, Retry, Fail?\");\n    console.log(\"Got Abort, Retry, Fail?\");\n    await pause(1000);\n    emulator.keyboard_send_text(\"a\");\n    emulator.set_cdrom({ url: __dirname + \"/../../images/linux4.iso\" });\n    await pause(1000);\n    emulator.keyboard_send_text(\"dir D:\\n\");\n    await emulator.wait_until_vga_screen_contains(\"BOOT         <DIR>\");\n    console.log(\"Got BOOT\");\n    emulator.destroy();\n    clearTimeout(timeout);\n    //clearInterval(interval);\n}, 1000);\n"
  },
  {
    "path": "tests/api/clean-shutdown.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\n// This test checks that calling emulator.destroy() will remove all event\n// listeners, so that the nodejs process cleanly and automatically exits.\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst config = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\", async: true },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n};\n\nconst emulator = new V86(config);\n\nsetTimeout(function()\n    {\n        console.error(\"Calling stop()\");\n        emulator.destroy();\n        console.error(\"Called stop()\");\n    }, 3000);\n"
  },
  {
    "path": "tests/api/floppy.js",
    "content": "#!/usr/bin/env node\n\nimport { setTimeout as pause } from \"timers/promises\";\nimport url from \"node:url\";\nimport fs from \"node:fs\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nfunction regexp_escape(text)\n{\n    // TODO: The official RegExp.escape() would be prefarrable to this,\n    // but currently (Aug 2025) the May 2025 Baseline is not yet available\n    // at github CI.\n    return text.replace(/[/\\-\\\\^$*+?.()|[\\]{}]/g, \"\\\\$&\");\n}\n\nasync function exec_test(test_name, v86_config, timeout_sec, test_function)\n{\n    console.log(\"Starting: \" + test_name);\n    const tm_start = performance.now();\n    const emulator = new V86(v86_config);\n    const timeout = setTimeout(async () => {\n        console.warn(emulator.screen_adapter.get_text_screen());\n        await emulator.destroy();\n        throw new Error(\"Timeout error in test \" + test_name);\n    }, timeout_sec * 1000);\n    await new Promise(resolve => emulator.bus.register(\"emulator-started\", () => resolve()));\n    try\n    {\n        await test_function(emulator);\n    }\n    catch(err)\n    {\n        console.warn(emulator.screen_adapter.get_text_screen());\n        throw new Error(\"Error in test \" + test_name, { cause: err });\n    }\n    clearTimeout(timeout);\n    await emulator.destroy();\n    console.log(\"Done: \" + test_name + \" (\" + ((performance.now() - tm_start) / 1000).toFixed(2) + \" sec)\");\n}\n\n/**\n * Execute given CLI command and wait for its completion.\n *\n * Injects command into the guest's keyboard buffer, then waits for both the\n * command and the expected response lines to show at the bottom of the VGA\n * screen.\n *\n * If command is empty then no command is executed and only the expected\n * response lines are waited for. If command contains only whitespace and/or\n * newline characters then it is send to the guest, but it does not become\n * part of the expected response.\n *\n * Allowed character set for command and expected is the printable subset\n * of 7-bit ASCII, use newline character \"\\n\" to encode ENTER key.\n *\n * Throws an Error if the given timeout elapsed before the expected response\n * could be detected.\n *\n * @param {V86} emulator\n * @param {string} command\n * @param {Array<string|RegExp>} expected\n * @param {number} timeout_msec\n */\nasync function expect(emulator, command, expected, timeout_msec)\n{\n    if(command)\n    {\n        for(const ch of command)\n        {\n            emulator.keyboard_send_text(ch);\n            await pause(10);\n        }\n        expected = [new RegExp(regexp_escape(command.trimRight()) + \"$\"), ...expected];\n        await pause(100);\n    }\n    if(!await emulator.wait_until_vga_screen_contains(expected, {timeout_msec: timeout_msec}))\n    {\n        throw new Error(\"Timeout of \" + timeout_msec + \" msec expired\");\n    }\n}\n\nconst CONFIG_MSDOS622_HD = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    hda: { url: __dirname + \"/../../images/msdos622.img\" },\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT\n};\n\nconst CONFIG_TINYCORE_CD = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/TinyCore-11.0.iso\" },\n    autostart: true,\n    memory_size: 128 * 1024 * 1024,\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT\n};\n\nawait exec_test(\"floppy-insert-eject\", CONFIG_MSDOS622_HD, 60, async emulator =>\n{\n    console.log(\"Waiting for C:\\\\>\");\n    await expect(emulator, \"\", [\"C:\\\\>\"], 10000);\n\n    console.log(\"Reading A:\");\n    await expect(emulator, \"dir A:\\n\", [\"\", \"\", \"General failure reading drive A\", \"Abort, Retry, Fail?\"], 5000);\n    await expect(emulator, \"A\", [\"\", \"C:\\\\>\"], 1000);\n\n    console.log(\"Inserting disk freedos722.img into drive fda\");\n    await emulator.set_fda({ url: __dirname + \"/../../images/freedos722.img\" });\n\n    console.log(\"Reading A:X86TEST.ASM\");\n    await expect(emulator, \"dir /B A:X86TEST.ASM\\n\", [\"X86TEST.ASM\", \"\", \"C:\\\\>\"], 3000);\n\n    console.log(\"Ejecting disk from drive A:\");\n    emulator.eject_fda();\n\n    console.log(\"Reading A:\");\n    await expect(emulator, \"dir A:\\n\", [\"\", \" Volume in drive A is FREEDOS\", \"\", \"General failure reading drive A\", \"Abort, Retry, Fail?\"], 5000);\n});\n\nawait exec_test(\"floppy-insert-fdb\", CONFIG_MSDOS622_HD, 60, async emulator =>\n{\n    console.log(\"Waiting for C:\\\\>\");\n    await expect(emulator, \"\", [\"C:\\\\>\"], 10000);\n\n    console.log(\"Inserting disk freedos722.img into drive fdb\");\n    await emulator.set_fdb({ url: __dirname + \"/../../images/freedos722.img\" });\n\n    console.log(\"Reading B:X86TEST.ASM\");\n    await expect(emulator, \"dir /B B:X86TEST.ASM\\n\", [\"X86TEST.ASM\", \"\", \"C:\\\\>\"], 3000);\n\n    console.log(\"Formatting B:\");\n    await expect(emulator, \"format /V:V86 /U B:\\n\", [\"Insert new diskette for drive B:\", \"and press ENTER when ready...\"], 3000);\n    await expect(emulator, \"\\n\", [/Volume Serial Number is [0-9A-F-]+/, \"\", \"Format another (Y/N)?\"], 3000);\n    await expect(emulator, \"N\\n\", [\"\", \"\", \"C:\\\\>\"], 3000);\n});\n\nawait exec_test(\"floppy-tinycore-linux\", CONFIG_TINYCORE_CD, 60, async emulator =>\n{\n    console.log(\"Waiting for boot menu\");\n    await expect(emulator, \"\", [/BIOS default device boot in \\d+ seconds\\.\\.\\./], 10000);\n\n    // press arrow down key 3 times\n    for(let i = 0; i < 3; i++)\n    {\n        emulator.keyboard_send_scancodes([\n            0xe0, 0x50,        // press\n            0xe0, 0x50 | 0x80, // release\n        ]);\n        await pause(600);\n    }\n\n    console.log(\"Waiting for tc@box:~$\");\n    await expect(emulator, \"\\n\", [\"tc@box:~$\"], 30000);\n\n    console.log(\"Inserting disk windows101.img into drive fda\");\n    await emulator.set_fda({ url: __dirname + \"/../../images/windows101.img\" });\n\n    console.log(\"Mounting /dev/fd0 into /mnt/fd0\");\n    await expect(emulator, \"mkdir /mnt/fd0\\n\", [\"tc@box:~$\"], 3000);\n    await expect(emulator, \"sudo mount /dev/fd0 /mnt/fd0\\n\", [\"tc@box:~$\"], 3000);\n\n    console.log(\"Reading /mnt/fd0/COMMAND.COM\");\n    await expect(emulator, \"ls /mnt/fd0/COMMAND.COM\\n\", [\"/mnt/fd0/COMMAND.COM\", \"tc@box:~$\"], 3000);\n\n    console.log(\"Unmounting fda\");\n    await expect(emulator, \"sudo umount /dev/fd0\\n\", [\"tc@box:~$\"], 3000);\n\n    console.log(\"Formatting /dev/fd0\");\n    await expect(emulator, \"sudo mke2fs -q /dev/fd0\\n\", [\"/dev/fd0 contains a vfat file system labelled 'WIN101'\", \"Proceed anyway? (y,N)\"], 3000);\n    await expect(emulator, \"y\\n\", [\"tc@box:~$\"], 5000);\n\n    console.log(\"Reading /mnt/fd0\");\n    await expect(emulator, \"sudo mount /dev/fd0 /mnt/fd0\\n\", [\"tc@box:~$\"], 3000);\n    await expect(emulator, \"ls /mnt/fd0\\n\", [\"lost+found/\", \"tc@box:~$\"], 3000);\n});\n\nawait exec_test(\"floppy-state-snapshot\", CONFIG_MSDOS622_HD, 60, async emulator =>\n{\n    console.log(\"Waiting for C:\\\\>\");\n    await expect(emulator, \"\", [\"C:\\\\>\"], 10000);\n\n    console.log(\"Inserting disk freedos722.img into drive fda\");\n    await emulator.set_fda({ url: __dirname + \"/../../images/freedos722.img\" });\n\n    console.log(\"Saving initial state\");\n    const initial_state = await emulator.save_state();\n\n    console.log(\"Creating file A:V86TEST.TXT\");\n    await expect(emulator, \"echo v86test > A:V86TEST.TXT\\n\", [\"\", \"C:\\\\>\"], 3000);\n\n    console.log(\"Saving modified state\");\n    const modified_state = await emulator.save_state();\n\n    console.log(\"Restoring initial state\");\n    await emulator.restore_state(initial_state);\n\n    console.log(\"Reading A:V86TEST.TXT\");\n    await expect(emulator, \"dir /B A:V86TEST.TXT\\n\", [\"\", \"C:\\\\>\"], 3000);\n\n    console.log(\"Restoring modified state\");\n    await emulator.restore_state(modified_state);\n\n    console.log(\"Reading A:V86TEST.TXT\");\n    await expect(emulator, \"dir /B A:V86TEST.TXT\\n\", [\"V86TEST.TXT\", \"\", \"C:\\\\>\"], 3000);\n});\n"
  },
  {
    "path": "tests/api/pic.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\nimport fs from \"node:fs\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst root_path = __dirname + \"/../..\";\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nif(!fs.existsSync(root_path + \"/images/fs.json\"))\n{\n    console.log(\"Missing images/fs.json, test skipped\");\n    process.exit(0);\n}\n\nconst config = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    bzimage_initrd_from_filesystem: true,\n    cmdline: [\n        \"rw apm=off vga=0x344 video=vesafb:ypan,vremap:8\",\n        \"root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose mitigations=off\",\n        \"audit=0 init=/usr/bin/init-openrc net.ifnames=0 biosdevname=0\",\n    ].join(\" \"),\n    filesystem: {\n        basefs: root_path + \"/images/fs.json\",\n        baseurl: root_path + \"/images/arch/\",\n    },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 512 * 1024 * 1024,\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n};\n\nconst emulator = new V86(config);\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n\n    // Trigger a lot of interrupts\n    // There have been bugs in the pic in the past, e.g. #1203\n    const interval = setInterval(() =>\n    {\n        emulator.bus.send(\"mouse-delta\", [1, 0]);\n    }, 0);\n\n    const timeout = setTimeout(() => {\n        console.warn(emulator.screen_adapter.get_text_screen());\n        throw new Error(\"Timeout\");\n    }, 120 * 1000);\n\n    let line = \"\";\n    emulator.add_listener(\"serial0-output-byte\", async function(byte)\n    {\n        const chr = String.fromCharCode(byte);\n\n        if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n        {\n            return;\n        }\n\n        if(chr === \"\\n\")\n        {\n            console.error(\"Serial: %s\", line);\n\n            if(line.startsWith(\"localhost login:\"))\n            {\n                console.log(\"Test passed\");\n                clearTimeout(timeout);\n                clearInterval(interval);\n                emulator.destroy();\n            }\n\n            line = \"\";\n        }\n        else\n        {\n            line += chr;\n        }\n    });\n});\n"
  },
  {
    "path": "tests/api/reboot.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst config = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\", async: true },\n    net_device: {\n        relay_url: \"fetch\",\n        type: \"virtio\",\n    },\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    virtio_console: true,\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n};\n\nconst emulator = new V86(config);\n\nlet did_reboot = false;\nlet serial_text = \"\";\n\nconst timeout = setTimeout(() => {\n    console.log(serial_text);\n    throw new Error(\"Timeout\");\n}, 120 * 1000);\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    serial_text += chr;\n\n    if(did_reboot)\n    {\n        if(serial_text.endsWith(\"Files send via emulator appear in /mnt/\"))\n        {\n            console.log(\"Ok\");\n            emulator.destroy();\n            clearTimeout(timeout);\n        }\n    }\n    else\n    {\n        if(serial_text.endsWith(\"~% \"))\n        {\n            console.log(\"rebooting\");\n            emulator.serial0_send(\"reboot\\n\");\n            serial_text = \"\";\n            did_reboot = true;\n        }\n    }\n});\n"
  },
  {
    "path": "tests/api/reset.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\n// This test checks that reset works\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst config = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\", async: true },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n};\n\nconst emulator = new V86(config);\n\nlet did_restart = false;\nlet serial_text = \"\";\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    serial_text += chr;\n\n    if(serial_text.includes(\"Files send via emulator appear in /mnt/\")) {\n        serial_text = \"\";\n        if(did_restart) {\n            console.log(\"Ok\");\n            emulator.destroy();\n        }\n        else {\n            console.log(\"Calling restart()\");\n            emulator.restart();\n            did_restart = true;\n        }\n    }\n});\n"
  },
  {
    "path": "tests/api/serial.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\nimport assert from \"node:assert/strict\";\nimport crypto from \"node:crypto\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst config = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux.iso\" },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    log_level: 0,\n    disable_jit: +process.env.DISABLE_JIT,\n};\n\nconst emulator = new V86(config);\n\nlet serial_data = [];\n\nsetTimeout(async () =>\n{\n    await emulator.wait_until_vga_screen_contains(\"/root% \");\n    console.log(\"Booted, sending file to ttyS0\");\n    emulator.keyboard_send_text(\"cat /bin/busybox > /dev/ttyS0\\n\");\n}, 1000);\n\nconst timeout = setTimeout(() => {\n    console.log(serial_data);\n    throw new Error(\"Timeout\");\n}, 60 * 1000);\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    serial_data.push(byte);\n\n    if(serial_data.length === 510277)\n    {\n        const hash = crypto.createHash(\"sha256\");\n        hash.update(new Uint8Array(serial_data));\n        assert(\"da1fb5b421123c58080a59832675632505b8c139a8d7ecd1c31591ca5c65cea6\" === hash.digest(\"hex\"));\n        console.log(\"ok\");\n        clearTimeout(timeout);\n        emulator.destroy();\n    }\n});\n"
  },
  {
    "path": "tests/api/state.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst config_async_cdrom = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\", async: true },\n    autostart: true,\n    memory_size: 64 * 1024 * 1024,\n    filesystem: {},\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n};\n\nconst config_sync_cdrom = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\", async: false },\n    autostart: true,\n    memory_size: 64 * 1024 * 1024,\n    filesystem: {},\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n};\n\nconst config_filesystem = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    autostart: true,\n    memory_size: 64 * 1024 * 1024,\n    filesystem: {},\n    bzimage: { url: __dirname + \"/../../images/buildroot-bzimage68.bin\" },\n    cmdline: \"tsc=reliable mitigations=off random.trust_cpu=on\",\n    network_relay_url: \"<UNUSED>\",\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n};\n\nconst config_large_memory = {\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\", async: true },\n    autostart: true,\n    memory_size: 2048 * 1024 * 1024,\n    vga_memory_size: 512 * 1024 * 1024,\n    network_relay_url: \"<UNUSED>\",\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n};\n\nasync function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }\n\nasync function run_test(name, config, done)\n{\n    const emulator = new V86(config);\n\n    await sleep(2000);\n\n    console.log(\"Saving: %s\", name);\n    const state = await emulator.save_state();\n\n    await sleep(1000);\n\n    console.log(\"Restoring: %s\", name);\n    await emulator.restore_state(state);\n\n    await emulator.wait_until_vga_screen_contains(\"~% \");\n    await sleep(1000);\n\n    emulator.keyboard_send_text(\"echo -n test; echo passed\\n\");\n    await sleep(1000);\n\n    const lines = emulator.screen_adapter.get_text_screen();\n    if(!lines.some(line => line.startsWith(\"testpassed\")))\n    {\n        console.warn(\"Failed: \" + name);\n        console.warn(lines.map(line => line.replace(/\\x00/g, \" \")));\n        process.exit(1);\n    }\n\n    console.log(\"Done: %s\", name);\n    emulator.destroy();\n}\n\n(async function() {\n    await run_test(\"async cdrom\", config_async_cdrom);\n    await run_test(\"sync cdrom\", config_sync_cdrom);\n    await run_test(\"filesystem\", config_filesystem);\n    await run_test(\"large memory size\", config_large_memory);\n})();\n"
  },
  {
    "path": "tests/api/test.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    fda: { url: __dirname + \"/../../images/freedos722.img\", async: true },\n    network_relay_url: \"<UNUSED>\",\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    log_level: -641,\n    disable_jit: +process.env.DISABLE_JIT,\n});\n\nsetInterval(() => {\n    console.log(emulator.screen_adapter.get_text_screen());\n}, 500);\n"
  },
  {
    "path": "tests/benchmark/arch-bytemark.js",
    "content": "#!/usr/bin/env node\n\nimport path from \"node:path\";\nimport url from \"node:url\";\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;\nconst { V86 } = await import(BENCH_COLLECT_STATS ? \"../../src/main.js\" : \"../../build/libv86.mjs\");\n\nconst V86_ROOT = path.join(__dirname, \"../..\");\n\nconst emulator = new V86({\n    bios: { url: path.join(V86_ROOT, \"/bios/seabios.bin\") },\n    vga_bios: { url: path.join(V86_ROOT, \"/bios/vgabios.bin\") },\n    autostart: true,\n    memory_size: 512 * 1024 * 1024,\n    vga_memory_size: 8 * 1024 * 1024,\n    net_device: {\n        type: \"virtio\",\n        relay_url: \"<UNUSED>\",\n    },\n    initial_state: { url: path.join(V86_ROOT, \"/images/arch_state-v2.bin.zst\") },\n    filesystem: { baseurl: path.join(V86_ROOT, \"/images/arch/\") },\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n});\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    let exclude_tests = [];\n\n    if(process.argv.length > 2)\n    {\n        exclude_tests = [\n            \"DONUMSORT\",\n            \"DOSTRINGSORT\",\n            \"DOBITFIELD\",\n            \"DOEMF\",\n            \"DOFOUR\",\n            \"DOASSIGN\",\n            \"DOIDEA\",\n            \"DOHUFF\",\n            \"DONNET\",\n            \"DOLU\",\n        ].filter(name => !process.argv.includes(name));\n    }\n\n    setTimeout(() => {\n        const set = exclude_tests.map(name => `echo ${name}=0 >> CMD`).join(\" && \");\n        emulator.serial0_send(`echo 0 > /sys/class/graphics/fbcon/cursor_blink && cd nbench && touch CMD && ${set || \"echo\"} && ./nbench -cCMD\\n`);\n    }, 1000);\n});\n\nvar line = \"\";\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(chr === \"\\n\")\n    {\n        console.log(\"%s\", line);\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n\n    if(line === \"* Trademarks are property of their respective holder.\")\n    {\n        emulator.destroy();\n\n        if(BENCH_COLLECT_STATS)\n        {\n            console.log(emulator.get_instruction_stats());\n        }\n    }\n});\n"
  },
  {
    "path": "tests/benchmark/arch-python.js",
    "content": "#!/usr/bin/env node\n\nimport path from \"node:path\";\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;\nconst { V86 } = await import(BENCH_COLLECT_STATS ? \"../../src/main.js\" : \"../../build/libv86.mjs\");\n\nconst V86_ROOT = path.join(__dirname, \"../..\");\n\nconst emulator = new V86({\n    bios: { url: path.join(V86_ROOT, \"/bios/seabios.bin\") },\n    vga_bios: { url: path.join(V86_ROOT, \"/bios/vgabios.bin\") },\n    autostart: true,\n    memory_size: 512 * 1024 * 1024,\n    vga_memory_size: 8 * 1024 * 1024,\n    net_device: {\n        type: \"virtio\",\n        relay_url: \"<UNUSED>\",\n    },\n    initial_state: { url: path.join(V86_ROOT, \"/images/arch_state-v2.bin.zst\") },\n    filesystem: { baseurl: path.join(V86_ROOT, \"/images/arch/\") },\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n});\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    emulator.create_file(\"/bench.py\", Buffer.from(`\ndef fib(n):\n    if n < 2:\n        return n\n    return fib(n-2) + fib(n-1)\n\nn = 30\nprint(\"fib(\", n, \")= \", fib(n))\n`));\n\n    setTimeout(() => {\n        emulator.serial0_send(`python3 /bench.py > /dev/null && python /bench.py > /dev/null && time python /bench.py\\n`);\n    }, 1000);\n});\n\nvar line = \"\";\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(chr === \"\\n\")\n    {\n        console.log(\"%s\", line);\n\n        if(line.startsWith(\"sys\"))\n        {\n            emulator.destroy();\n\n            if(BENCH_COLLECT_STATS)\n            {\n                console.log(emulator.get_instruction_stats());\n            }\n        }\n\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n});\n"
  },
  {
    "path": "tests/benchmark/fetch-download.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\nimport { createServer } from \"node:http\";\nimport { Worker, isMainThread, parentPort, workerData } from \"node:worker_threads\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nconst __filename = url.fileURLToPath(import.meta.url);\n\nconst USE_VIRTIO = !!process.env.USE_VIRTIO;\nconst BENCHFILE_SIZE = (parseInt(process.env.BENCHFILE_SIZE_MB, 10) || 32) * 1024 * 1024;\n\nconst { V86 } = await import(\"../../build/libv86.mjs\");\n\nconst LOG_SERIAL = true;\n\nif(isMainThread)\n{\n    const emulator = new V86({\n        bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n        vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n        bzimage: { url: __dirname + \"/../../images/buildroot-bzimage68.bin\" },\n        autostart: false,\n        memory_size: 64 * 1024 * 1024,\n        net_device: {\n            relay_url: \"fetch\",\n            type: USE_VIRTIO ? \"virtio\" : \"ne2k\",\n        }\n    });\n\n    const server = new Worker(__filename, { workerData: BENCHFILE_SIZE });\n    server.on(\"error\", (e) => { throw new Error(\"server: \" + e); });\n    server.on(\"message\", function(msg) {\n        SERVER_PORT = msg;\n        console.log(\"Server started on port \" + SERVER_PORT);\n        emulator.run();\n    });\n\n    let SERVER_PORT = 0;\n    let serial_text = \"\";\n    let booted = false;\n\n    emulator.bus.register(\"emulator-started\", function()\n    {\n        console.log(\"Booting now, please stand by\");\n    });\n\n    emulator.add_listener(\"serial0-output-byte\", function(byte)\n    {\n        var chr = String.fromCharCode(byte);\n\n        if(LOG_SERIAL) process.stdout.write(chr);\n\n        serial_text += chr;\n\n        if(!booted && serial_text.endsWith(\"~% \"))\n        {\n            booted = true;\n            emulator.serial0_send(`udhcpc;curl --fail --connect-timeout 10 -s -o /dev/null -w '<%{exitcode}><%{speed_download}>\\\\t<DONE>' http://${SERVER_PORT}.external\\n`);\n        }\n\n        if(serial_text.endsWith(\"\\t<DONE>\"))\n        {\n            console.log(\"\\n---\\n\");\n            emulator.destroy();\n            server.terminate();\n            parse_console(serial_text);\n        }\n    });\n}\nelse\n{\n    const benchsize = workerData;\n    const benchfile = Buffer.alloc(benchsize);\n\n    const server = createServer(function(_, response) {\n        response.setHeader(\"content-type\", \"application/octet-stream\");\n        response.setHeader(\"content-length\", benchsize.toString(10));\n        response.write(benchfile);\n        response.end();\n    });\n\n    server.listen(0, () => parentPort.postMessage(server.address().port));\n}\n\nfunction parse_console(output) {\n    const regex = /<(\\d+)><(\\d+)>\\t<DONE>/.exec(output);\n\n    if(!regex)\n    {\n        console.error(\"Can't parse console log\");\n        process.exit(1);\n    }\n\n    const exitcode = parseInt(regex[1], 10);\n    const speed = parseInt(regex[2], 10); // in bytes\n\n    if(exitcode !== 0)\n    {\n        console.error(\"Bench failed, curl returned non-zero exit code %s\", exitcode);\n        process.exit(exitcode);\n    }\n\n    console.log(\"Average download speed: %s kB/s\", (speed / 1024).toFixed(2));\n}\n"
  },
  {
    "path": "tests/benchmark/linux-boot.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;\nconst { V86 } = await import(BENCH_COLLECT_STATS ? \"../../src/main.js\" : \"../../build/libv86.mjs\");\n\nconst V86_ROOT = path.join(__dirname, \"../..\");\n\nconst LOG_SERIAL = true;\n\nif(true)\n{\n    var emulator = new V86({\n        bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n        vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n        cdrom: { url: __dirname + \"/../../images/linux3.iso\" },\n        autostart: true,\n        memory_size: 32 * 1024 * 1024,\n        disable_jit: +process.env.DISABLE_JIT,\n        log_level: 0,\n    });\n}\nelse\n{\n    var emulator = new V86({\n        bios: { url: path.join(V86_ROOT, \"/bios/seabios.bin\") },\n        vga_bios: { url: path.join(V86_ROOT, \"/bios/vgabios.bin\") },\n        autostart: true,\n        memory_size: 512 * 1024 * 1024,\n        vga_memory_size: 8 * 1024 * 1024,\n        network_relay_url: \"<UNUSED>\",\n        bzimage_initrd_from_filesystem: true,\n        cmdline: \"rw console=ttyS0 apm=off root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose mitigations=off audit=0 tsc=reliable nowatchdog init=/usr/bin/init-openrc\",\n        filesystem: {\n            basefs: {\n                url: path.join(V86_ROOT, \"/images/fs.json\"),\n            },\n            baseurl: path.join(V86_ROOT, \"/images/arch/\"),\n        },\n        disable_jit: +process.env.DISABLE_JIT,\n        log_level: 0,\n    });\n}\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n    start_time = Date.now();\n});\n\nvar serial_text = \"\";\nvar start_time;\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(LOG_SERIAL) process.stdout.write(chr);\n\n    serial_text += chr;\n\n    if(serial_text.endsWith(\"~% \") || serial_text.endsWith(\"root@localhost:~# \"))\n    {\n        const end_time = Date.now();\n        const elapsed = end_time - start_time;\n        console.log(\"Done in %dms\", elapsed);\n        emulator.destroy();\n\n        if(BENCH_COLLECT_STATS)\n        {\n            console.log(emulator.get_instruction_stats());\n        }\n    }\n});\n"
  },
  {
    "path": "tests/benchmark/snapshot.js",
    "content": "#!/usr/bin/env node\n\nimport path from \"node:path\";\nimport url from \"node:url\";\n\nconst BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;\nconst { V86 } = await import(BENCH_COLLECT_STATS ? \"../../src/main.js\" : \"../../build/libv86.mjs\");\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nconst V86_ROOT = path.join(__dirname, \"../..\");\n\nconst LOG_SERIAL = true;\n\n\nvar emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux3.iso\" },\n    autostart: true,\n    memory_size: 1024 * 1024 * 1024,\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n});\n\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n    start_time = Date.now();\n});\n\nvar serial_text = \"\";\nvar start_time;\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(LOG_SERIAL) process.stdout.write(chr);\n\n    serial_text += chr;\n\n    if(serial_text.endsWith(\"~% \") || serial_text.endsWith(\"root@localhost:~# \"))\n    {\n        console.log(\"Creating snapshots\");\n        const start_time = Date.now();\n        for(var i = 0; i < 10; ++i) emulator.save_state();\n        const end_time = Date.now();\n        const elapsed = end_time - start_time;\n        console.log(\"Done in %dms\", elapsed);\n        emulator.destroy();\n\n        if(BENCH_COLLECT_STATS)\n        {\n            console.log(emulator.get_instruction_stats());\n        }\n    }\n});\n"
  },
  {
    "path": "tests/devices/fetch_network.js",
    "content": "#!/usr/bin/env node\n\nimport assert from \"assert/strict\";\nimport url from \"node:url\";\nimport { Worker, isMainThread, parentPort } from \"node:worker_threads\";\nimport { createServer } from \"node:http\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nconst __filename = url.fileURLToPath(import.meta.url);\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst USE_VIRTIO = !!process.env.USE_VIRTIO;\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\n\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\nconst SHOW_LOGS = false;\n\nfunction wait(time) {\n    return new Promise((res) => setTimeout(res, time));\n}\n\nif(isMainThread)\n{\n    let SERVER_PORT = 0;\n\n    const tests =\n    [\n        {\n            name: \"DHCP\",\n            start: () =>\n            {\n                emulator.serial0_send(\"udhcpc\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tudhcpc\\n\");\n            },\n            end_trigger: \"done\\tudhcpc\",\n            end: (capture) =>\n            {\n                assert(/lease of 192.168.86.100 obtained/.test(capture), \"lease of 192.168.86.100 obtained\");\n            },\n        },\n        {\n            name: \"lspci\",\n            timeout: 60,\n            start: () =>\n            {\n                emulator.serial0_send(\"lspci -k\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tlspci\\n\");\n            },\n            end_trigger: \"done\\tlspci\",\n            end: (capture) =>\n            {\n                if(!USE_VIRTIO) {\n                    assert(/ne2k/.test(capture), \"ne2k missing from lspci\");\n                } else {\n                    assert(!/ne2k/.test(capture), \"ne2k in lspci\");\n                }\n            },\n        },\n        {\n            name: \"ifconfig\",\n            start: () =>\n            {\n                emulator.serial0_send(\"ifconfig\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tifconfig\\n\");\n            },\n            end_trigger: \"done\\tifconfig\",\n            end: (capture) =>\n            {\n                assert(/192.168.86.100/.test(capture), \"192.168.86.100\");\n            },\n        },\n        {\n            name: \"route\",\n            start: () =>\n            {\n                emulator.serial0_send(\"ip route\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\troute\\n\");\n            },\n            end_trigger: \"done\\troute\",\n            end: (capture) =>\n            {\n                assert(/192.168.86.1/.test(capture), \"192.168.86.100\");\n            },\n        },\n        {\n            name: \"ping 1.2.3.4\",\n            start: () =>\n            {\n                emulator.serial0_send(\"ping -c 2 1.2.3.4\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tping\\n\");\n            },\n            end_trigger: \"done\\tping\",\n            end: (capture) =>\n            {\n                assert(/2 packets transmitted, 2 (packets )?received, 0% packet loss/.test(capture), \"2 packets transmitted, 2 packets received, 0% packet loss\");\n                assert(/from 1\\.2\\.3\\.4:/.test(capture), \"got correct source ip\");\n            },\n        },\n        {\n            name: \"arp -a\",\n            start: () =>\n            {\n                emulator.serial0_send(\"arp -a\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tarp\\n\");\n            },\n            end_trigger: \"done\\tarp\",\n            end: (capture) =>\n            {\n                assert(/.192.168.86.1. at 52:54:00:01:02:03 \\[ether\\] {2}on eth0/.test(capture), \"(192.168.86.1) at 52:54:00:01:02:03 [ether]  on eth0\");\n            },\n        },\n        {\n            name: \"Accept incoming connection\",\n            timeout: 60,\n            allow_failure: true,\n            start: async () =>\n            {\n                let open = await emulator.network_adapter.tcp_probe(80);\n                assert(!open, \"Probe shows port not open\");\n                emulator.serial0_send(\"echo -n hello | socat TCP4-LISTEN:80 - && echo -e done\\\\\\\\tlisten\\n\");\n                await wait(1000);\n                open = await emulator.network_adapter.tcp_probe(80);\n                assert(open, \"Probe shows port open, but does not show as a connection\");\n                await wait(1000);\n                let h = emulator.network_adapter.connect(80);\n                h.on(\"connect\", () => {\n                    h.write(new TextEncoder().encode(\"From VM: \"));\n                    h.on(\"data\", (d) => {\n                        d.reverse();\n                        h.write(d);\n                        h.write(new TextEncoder().encode(\"\\n\"));\n                        h.close();\n                    });\n                });\n            },\n            end_trigger: \"done\\tlisten\",\n            end: (capture) =>\n            {\n                assert(/From VM: olleh/.test(capture), \"got From VM\");\n            },\n        },\n        {\n            name: \"GET mocked.example.org\",\n            allow_failure: true,\n            start: () =>\n            {\n                emulator.serial0_send(\"wget -T 10 -O - mocked.example.org\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tmocked.example.org\\n\");\n            },\n            end_trigger: \"done\\tmocked.example.org\",\n            end: (capture) =>\n            {\n                assert(/This text is from the mock/.test(capture), \"got mocked.example.org text\");\n            },\n        },\n        {\n            name: \"GET example.org\",\n            allow_failure: true,\n            start: () =>\n            {\n                emulator.serial0_send(\"wget -T 10 -O - example.org\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\texample.org\\n\");\n            },\n            end_trigger: \"done\\texample.org\",\n            end: (capture) =>\n            {\n                assert(/This domain is for use in illustrative examples in documents/.test(capture), \"got example.org text\");\n            },\n        },\n        {\n            name: \"GET local server\",\n            allow_failure: true,\n            start: () =>\n            {\n                emulator.serial0_send(`wget -T 10 -O - ${SERVER_PORT}.external\\n`);\n                emulator.serial0_send(\"echo -e done\\\\\\\\tlocal server\\n\");\n            },\n            end_trigger: \"done\\tlocal server\",\n            end: (capture) =>\n            {\n                assert(/This text is from the local server/.test(capture), \"got local server text\");\n            },\n        },\n        {\n            name: \"GET local server with custom header\",\n            allow_failure: true,\n            start: () =>\n            {\n                emulator.serial0_send(`wget -S -T 10 --header='x-client-test: hello' -O - ${SERVER_PORT}.external/header\\n`);\n                emulator.serial0_send(\"echo -e done\\\\\\\\tlocal server custom header\\n\");\n            },\n            end_trigger: \"done\\tlocal server custom header\",\n            end: (capture) =>\n            {\n                assert(/x-server-test: {1,}h_e_l_l_o/.test(capture), \"got local server custom header\");\n            },\n        },\n        {\n            name: \"GET local server with redirect\",\n            allow_failure: true,\n            start: () =>\n            {\n                emulator.serial0_send(`curl -m 10 -L -v ${SERVER_PORT}.external/redirect\\n`);\n                emulator.serial0_send(\"echo -e done\\\\\\\\tlocal server redirect\\n\");\n            },\n            end_trigger: \"done\\tlocal server redirect\",\n            end: (capture) =>\n            {\n                assert(/x-was-fetch-redirected: {1,}true/.test(capture), \"got local server redirect header\");\n                assert(/This text is from the local server/.test(capture), \"got actual redirect\");\n            },\n        },\n        {\n            name: \"Forbidden character in header name\",\n            start: () =>\n            {\n                emulator.serial0_send(\"wget --header='test.v86: 123' -T 10 -O - test.domain\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tincorrect header name\\n\");\n            },\n            end_trigger: \"done\\tincorrect header name\",\n            end: (capture) =>\n            {\n                assert(/400 Bad Request/.test(capture), \"got error 400\");\n            },\n        },\n        {\n            name: \"Empty header value\",\n            start: () =>\n            {\n                emulator.serial0_send(\"wget --header='test:' -T 10 -O - test.domain\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\tempty header value\\n\");\n            },\n            end_trigger: \"done\\tempty header value\",\n            end: (capture) =>\n            {\n                assert(/400 Bad Request/.test(capture), \"got error 400\");\n            },\n        },\n        {\n            name: \"Header without separator\",\n            start: () =>\n            {\n                emulator.serial0_send(\"wget --header='testheader' -T 10 -O - test.domain\\n\");\n                emulator.serial0_send(\"echo -e done\\\\\\\\theader without colon\\n\");\n            },\n            end_trigger: \"done\\theader without colon\",\n            end: (capture) =>\n            {\n                assert(/400 Bad Request/.test(capture), \"got error 400\");\n            },\n        },\n    ];\n\n\n    const emulator = new V86({\n        bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n        vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n        bzimage: { url: __dirname + \"/../../images/buildroot-bzimage68.bin\" },\n        autostart: true,\n        memory_size: 64 * 1024 * 1024,\n        disable_jit: +process.env.DISABLE_JIT,\n        net_device: {\n            relay_url: \"fetch\",\n            type: USE_VIRTIO ? \"virtio\" : \"ne2k\",\n        },\n        log_level: SHOW_LOGS ? 0x400000 : 0,\n    });\n\n    const server = new Worker(__filename);\n    server.on(\"error\", (e) => { throw new Error(\"server: \" + e); });\n    server.on(\"message\", function(msg) {\n        SERVER_PORT = msg;\n        console.log(\"Server started on port \" + SERVER_PORT);\n    });\n\n    emulator.add_listener(\"emulator-ready\", function () {\n        let network_adapter = emulator.network_adapter;\n        let original_fetch = network_adapter.fetch;\n        network_adapter.fetch = (url, opts) => {\n            if(/^http:\\/\\/mocked.example.org\\/?/.test(url)) {\n                let contents = new TextEncoder().encode(\"This text is from the mock\");\n                let headers = new Headers();\n                headers.append(\"Content-Type\", \"text/plain\");\n                headers.append(\"Content-Length\", contents.length);\n                return new Promise(res => setTimeout(() => res(new Response(contents, {\n                    headers\n                })), 50));\n            }\n            return original_fetch(url, opts);\n        };\n    });\n\n    let test_num = 0;\n    let booted = false;\n    let line = \"\";\n    let capture = \"\";\n    let end_trigger;\n\n    emulator.bus.register(\"emulator-started\", function()\n    {\n        console.log(\"Booting now, please stand by\");\n    });\n\n    emulator.add_listener(\"serial0-output-byte\", function(byte)\n    {\n        const chr = String.fromCharCode(byte);\n        if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n        {\n            return;\n        }\n\n        let new_line = \"\";\n        if(chr === \"\\n\")\n        {\n            console.log(\"    Captured: %s\", line);\n            new_line = line;\n            capture += line + \"\\n\";\n            line = \"\";\n        }\n        else\n        {\n            line += chr;\n        }\n\n        if(new_line === end_trigger)\n        {\n            let test_has_failed = false;\n\n            try {\n                tests[test_num].end(capture);\n            } catch(e) {\n                console.log(e);\n                test_has_failed = true;\n            }\n\n            if(!test_has_failed)\n            {\n                console.log(\"[+] Test #%d passed: %s\", test_num, tests[test_num].name);\n            }\n            else\n            {\n                if(tests[test_num].allow_failure)\n                {\n                    console.warn(\"[!] Test #%d failed: %s (failure allowed)\", test_num, tests[test_num].name);\n                }\n                else\n                {\n                    console.error(\"[-] Test #%d failed: %s\", test_num, tests[test_num].name);\n                    server.terminate();\n                    process.exit(1);\n                }\n            }\n\n            test_num++;\n\n        }\n\n        if(!booted && line.endsWith(\"~% \") || new_line === end_trigger)\n        {\n            booted = true;\n\n            if(test_num >= tests.length)\n            {\n                emulator.destroy();\n                server.terminate();\n                console.log(\"Tests finished.\");\n            }\n            else\n            {\n                console.log(\"Starting test #%d: %s\", test_num, tests[test_num].name);\n\n                capture = \"\";\n                end_trigger = tests[test_num].end_trigger;\n\n                tests[test_num].start();\n            }\n        }\n    });\n}\nelse\n{\n    const server = createServer(function(request, response) {\n        switch(request.method) {\n            case \"GET\":\n                if(request.url === \"/\") {\n                    response.end(\"This text is from the local server\");\n                } else if(request.url === \"/header\") {\n                     response.writeHead(200, { \"x-server-test\": request.headers[\"x-client-test\"].split(\"\").join(\"_\") || \"none\" });\n                     response.end();\n                } else if(request.url === \"/redirect\") {\n                     response.writeHead(307, { \"location\": \"/\" });\n                     response.end();\n                } else {\n                     response.writeHead(404);\n                     response.end(\"Unknown endpoint\");\n                }\n                break;\n            default:\n                response.writeHead(405);\n                response.end(\"Unknown method\");\n                break;\n        }\n    });\n\n    server.listen(0, () => parentPort.postMessage(server.address().port));\n}\n"
  },
  {
    "path": "tests/devices/testfs/5d70f436aa013f4f1d5af4a5e8149b479c813ab4ceea0bcf8b01f78eac84fd25",
    "content": "foobaz\n"
  },
  {
    "path": "tests/devices/testfs/7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730",
    "content": "bar\n"
  },
  {
    "path": "tests/devices/testfs.json",
    "content": "{\"fsroot\":[[\"foo\",4,1531432001,33188,1000,1000,\"7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730\"],[\"dir\",4096,1532940393,16877,1000,1000,[[\"bar\",7,1532940393,33188,1000,1000,\"5d70f436aa013f4f1d5af4a5e8149b479c813ab4ceea0bcf8b01f78eac84fd25\"]]]],\"version\":3,\"size\":4107}\n"
  },
  {
    "path": "tests/devices/virtio_9p.js",
    "content": "#!/usr/bin/env node\n\nimport url from \"node:url\";\nimport fs from \"node:fs\";\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst testfsjson = JSON.parse(fs.readFileSync(__dirname + \"/testfs.json\", \"utf-8\"));\nconst SHOW_LOGS = false;\nconst STOP_ON_FIRST_FAILURE = false;\n\nfunction assert_equal(actual, expected, message)\n{\n    if(actual !== expected)\n    {\n        console.warn(\"Failed assert equal (Test: %s). %s\", tests[test_num].name, message || \"\");\n        console.warn(\"Expected:\\n\" + expected);\n        console.warn(\"Actual:\\n\" + actual);\n        test_fail();\n    }\n}\n\nfunction assert_not_equal(actual, expected, message)\n{\n    if(actual === expected)\n    {\n        console.warn(\"Failed assert not equal (Test: %s). %s\", tests[test_num].name, message || \"\");\n        console.warn(\"Expected something different than:\\n\" + expected);\n        test_fail();\n    }\n}\n\n// Random printable characters.\nconst test_file = new Uint8Array(512).map(v => 0x20 + Math.random() * 0x5e);\nconst test_file_string = Buffer.from(test_file).toString();\nconst test_file_small = new Uint8Array(16).map(v => 0x20 + Math.random() * 0x5e);\nconst test_file_small_string = Buffer.from(test_file_small).toString();\n\nconst tests =\n[\n    {\n        name: \"Read Existing\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"cp /etc/profile /mnt/read-existing\\n\");\n            emulator.serial0_send(\"echo start-capture; cat /etc/profile; echo done-read-existing\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-read-existing\",\n        end: async (capture, done) =>\n        {\n            const data = await emulator.read_file(\"read-existing\");\n            assert_equal(capture, Buffer.from(data).toString());\n            done();\n        },\n    },\n    {\n        name: \"Read New\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"dd if=/dev/zero of=/mnt/read-new bs=1k count=512\\n\");\n            emulator.serial0_send(\"echo done-read-new\\n\");\n        },\n        end_trigger: \"done-read-new\",\n        end: async (capture, done) =>\n        {\n            const data = await emulator.read_file(\"read-new\");\n            assert_equal(data.length, 512 * 1024);\n            if(data.find(v => v !== 0))\n            {\n                console.warn(\"Fail: Incorrect data. Expected all zeros.\");\n                test_fail();\n            }\n            done();\n        },\n    },\n    {\n        name: \"Read Async\",\n        use_fsjson: true,\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo start-capture;\");\n\n            // \"foo\" is from ./testfs/foo\n            emulator.serial0_send(\"cat /mnt/foo;\");\n\n            emulator.serial0_send(\"echo done-read-async\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-read-async\",\n        end: async (capture, done) =>\n        {\n            assert_equal(capture, \"bar\\n\");\n            const data = await emulator.read_file(\"foo\");\n            assert_equal(Buffer.from(data).toString(), \"bar\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Write New\",\n        timeout: 60,\n        files:\n        [\n            {\n                file: \"write-new\",\n                data: test_file,\n            },\n        ],\n        start: () =>\n        {\n            emulator.serial0_send(\"echo start-capture; cat /mnt/write-new; echo; echo done-write-new\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-write-new\",\n        end: (capture, done)  =>\n        {\n            // Handle word wrapping.\n            const lines = capture.split(\"\\n\");\n            let pos = 0;\n            for(const line of lines)\n            {\n                assert_equal(line, test_file_string.slice(pos, line.length));\n                pos += line.length;\n            }\n            done();\n        },\n    },\n    {\n        name: \"New file time\",\n        timeout: 10,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo start-capture; echo foo > /mnt/bar; ls  -l --full-time --color=never /mnt/bar; echo; echo done-write-new\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-write-new\",\n        end: (capture, done)  =>\n        {\n            const outputs = capture.split(\"\\n\").map(output => output.split(/\\s+/));\n\n            // atime: Should be fresh\n            const [year, month, day] = outputs[0][5].split(\"-\");\n            assert_not_equal(year, \"1970\");\n\n            done();\n        },\n    },\n    {\n        name: \"Move\",\n        timeout: 60,\n        files:\n        [\n            {\n                file: \"test-file\",\n                data: test_file,\n            },\n        ],\n        start: () =>\n        {\n            emulator.serial0_send(\"echo start-capture;\");\n            emulator.serial0_send(\"cat /mnt/test-file;\");\n            emulator.serial0_send(\"find /mnt;\");\n\n            // Rename. Verify updated directory.\n            emulator.serial0_send(\"mv /mnt/test-file /mnt/renamed;\");\n            emulator.serial0_send(\"cat /mnt/renamed;\");\n            emulator.serial0_send(\"find /mnt;\");\n\n            // Move between folders. Verify directories.\n            emulator.serial0_send(\"mkdir /mnt/somedir;\");\n            emulator.serial0_send(\"mv /mnt/renamed /mnt/somedir/file;\");\n            emulator.serial0_send(\"cat /mnt/somedir/file;\");\n            emulator.serial0_send(\"find /mnt;\");\n\n            // Rename folder.\n            emulator.serial0_send(\"mv /mnt/somedir /mnt/otherdir;\");\n            emulator.serial0_send(\"cat /mnt/otherdir/file;\");\n            emulator.serial0_send(\"find /mnt;\");\n\n            // Move folder.\n            emulator.serial0_send(\"mkdir /mnt/thirddir;\");\n            emulator.serial0_send(\"mv /mnt/otherdir /mnt/thirddir;\");\n            emulator.serial0_send(\"cat /mnt/thirddir/otherdir/file;\");\n            emulator.serial0_send(\"find /mnt;\");\n\n            // Move folder outside /mnt. Should be removed from 9p filesystem.\n            emulator.serial0_send(\"mv /mnt/thirddir/otherdir /root/movedoutside;\");\n            emulator.serial0_send(\"cat /root/movedoutside/file;\");\n            emulator.serial0_send(\"find /mnt;\");\n\n            // Cleanup.\n            emulator.serial0_send(\"rm -rf /root/movedoutside;\");\n            emulator.serial0_send(\"echo done-move\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-move\",\n        end: (capture, done)  =>\n        {\n            assert_equal(capture,\n                test_file_string +\n                \"/mnt\\n\" +\n                \"/mnt/test-file\\n\" +\n                test_file_string +\n                \"/mnt\\n\" +\n                \"/mnt/renamed\\n\" +\n                test_file_string +\n                \"/mnt\\n\" +\n                \"/mnt/somedir\\n\" +\n                \"/mnt/somedir/file\\n\" +\n                test_file_string +\n                \"/mnt\\n\" +\n                \"/mnt/otherdir\\n\" +\n                \"/mnt/otherdir/file\\n\" +\n                test_file_string +\n                \"/mnt\\n\" +\n                \"/mnt/thirddir\\n\" +\n                \"/mnt/thirddir/otherdir\\n\" +\n                \"/mnt/thirddir/otherdir/file\\n\" +\n                test_file_string +\n                \"/mnt\\n\" +\n                \"/mnt/thirddir\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Unlink\",\n        timeout: 60,\n        files:\n        [\n            {\n                file: \"existing-file\",\n                data: test_file,\n            },\n        ],\n        start: () =>\n        {\n            emulator.serial0_send(\"touch /mnt/new-file\\n\");\n            emulator.serial0_send(\"mkdir /mnt/new-dir\\n\");\n            emulator.serial0_send(\"touch /mnt/new-dir/file\\n\");\n\n            emulator.serial0_send(\"echo start-capture;\");\n\n            emulator.serial0_send(\"rm /mnt/new-file;\");\n            emulator.serial0_send(\"test ! -e /mnt/new-file && echo new-file-unlinked;\");\n            emulator.serial0_send(\"cat /mnt/new-file 2>/dev/null || echo read-failed;\");\n\n            emulator.serial0_send(\"rm /mnt/existing-file;\");\n            emulator.serial0_send(\"test ! -e /mnt/existing-file && echo existing-file-unlinked;\");\n            emulator.serial0_send(\"cat /mnt/existing-file 2>/dev/null || echo read-failed;\");\n\n            emulator.serial0_send(\"rmdir /mnt/new-dir 2>/dev/null || echo rmdir-failed;\");\n            emulator.serial0_send(\"test -e /mnt/new-dir && echo new-dir-exist;\");\n\n            emulator.serial0_send(\"rm /mnt/new-dir/file;\");\n            emulator.serial0_send(\"rmdir /mnt/new-dir;\");\n            emulator.serial0_send(\"test ! -e /mnt/new-dir/file && echo new-dir-file-unlinked;\");\n            emulator.serial0_send(\"test ! -e /mnt/new-dir && echo new-dir-unlinked;\");\n            emulator.serial0_send(\"ls /mnt/new-dir 2>/dev/null || echo read-failed;\");\n\n            emulator.serial0_send(\"echo done-unlink\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-unlink\",\n        end: (capture, done)  =>\n        {\n            assert_equal(capture,\n                \"new-file-unlinked\\n\" +\n                \"read-failed\\n\" +\n                \"existing-file-unlinked\\n\" +\n                \"read-failed\\n\" +\n                \"rmdir-failed\\n\" +\n                \"new-dir-exist\\n\" +\n                \"new-dir-file-unlinked\\n\" +\n                \"new-dir-unlinked\\n\" +\n                \"read-failed\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Hard Links\",\n        timeout: 60,\n        files:\n        [\n            {\n                file: \"target\",\n                data: test_file_small,\n            },\n        ],\n        start: () =>\n        {\n            // Helper that prints filename followed by nlinks.\n            emulator.serial0_send(\"nlinks() {\\n\");\n            emulator.serial0_send(`  ls -dli $@ | awk '{ print \"'$@' \"$3 }'\\n`);\n            emulator.serial0_send(\"}\\n\");\n\n            // Check nlinks before mkdir.\n            emulator.serial0_send(\"nlinks /mnt | tee -a /mnt/target\\n\");\n\n            emulator.serial0_send(\"mkdir /mnt/dir\\n\");\n            emulator.serial0_send(\"echo other > /mnt/target2\\n\");\n\n            // Check nlinks after mkdir.\n            emulator.serial0_send(\"nlinks /mnt | tee -a /mnt/target\\n\");\n            emulator.serial0_send(\"nlinks /mnt/dir | tee -a /mnt/target\\n\");\n            emulator.serial0_send(\"nlinks /mnt/target | tee -a /mnt/target\\n\");\n\n            // Create hard links.\n            emulator.serial0_send(\"ln /mnt/target /mnt/link1\\n\");\n            emulator.serial0_send(\"ln /mnt/link1 /mnt/dir/link2\\n\");\n            emulator.serial0_send(\"ln /mnt/dir/link2 /mnt/dir/link3\\n\");\n            emulator.serial0_send(\"ln /mnt/target2 /mnt/link-other\\n\");\n\n            // Test inode numbers.\n            emulator.serial0_send(\"{ test /mnt/target -ef /mnt/link1 && \\n\");\n            emulator.serial0_send(\"  test /mnt/link1 -ef /mnt/dir/link2 && \\n\");\n            emulator.serial0_send(\"  test /mnt/target -ef /mnt/dir/link3 && \\n\");\n            emulator.serial0_send(\"  echo same inode | tee -a /mnt/target; }\\n\");\n            emulator.serial0_send(\"{ test /mnt/link-other -ef /mnt/dir/link3 || \\n\");\n            emulator.serial0_send(\"  echo different inode | tee -a /mnt/link1; }\\n\");\n\n            // Check nlinks after hard links.\n            emulator.serial0_send(\"nlinks /mnt | tee -a /mnt/dir/link2\\n\");\n            emulator.serial0_send(\"nlinks /mnt/dir | tee -a /mnt/dir/link2\\n\");\n            emulator.serial0_send(\"nlinks /mnt/target | tee -a /mnt/dir/link2\\n\");\n            emulator.serial0_send(\"nlinks /mnt/dir/link2 | tee -a /mnt/dir/link2\\n\");\n            emulator.serial0_send(\"nlinks /mnt/target2 | tee -a /mnt/dir/link2\\n\");\n            emulator.serial0_send(\"nlinks /mnt/link-other | tee -a /mnt/dir/link2\\n\");\n\n            // Movement and unlink.\n            emulator.serial0_send(\"mv /mnt/link1 /mnt/link1-renamed\\n\");\n            emulator.serial0_send(\"echo renamed | tee -a /mnt/link1-renamed\\n\");\n            emulator.serial0_send(\"mv /mnt/dir/link2 /mnt/link2-moved\\n\");\n            emulator.serial0_send(\"echo moved | tee -a /mnt/link2-moved\\n\");\n            emulator.serial0_send(\"rm /mnt/target\\n\");\n            emulator.serial0_send(\"echo unlinked original | tee -a /mnt/dir/link3\\n\");\n\n            // Test inode numbers after movement and unlinking.\n            emulator.serial0_send(\"{ test /mnt/link1-renamed -ef /mnt/link2-moved && \\n\");\n            emulator.serial0_send(\"  test /mnt/link2-moved -ef /mnt/dir/link3 && \\n\");\n            emulator.serial0_send(\"  echo same inode after mv | tee -a /mnt/link1-renamed; }\\n\");\n\n            // Check nlinks after movement and unlinking.\n            emulator.serial0_send(\"nlinks /mnt | tee -a /mnt/link2-moved\\n\");\n            emulator.serial0_send(\"nlinks /mnt/dir | tee -a /mnt/link2-moved\\n\");\n            emulator.serial0_send(\"nlinks /mnt/link1-renamed | tee -a /mnt/link2-moved\\n\");\n\n            emulator.serial0_send(\"echo start-capture;\\\\\\n\");\n\n            // Unlink the rest and output the above messages.\n            emulator.serial0_send(\"rm /mnt/link1-renamed;\\\\\\n\");\n            emulator.serial0_send(\"echo unlinked link1 >> /mnt/link2-moved;\\\\\\n\");\n            emulator.serial0_send(\"nlinks /mnt/link2-moved >> /mnt/link2-moved;\\\\\\n\");\n            emulator.serial0_send(\"rm /mnt/link2-moved;\\\\\\n\");\n            emulator.serial0_send(\"echo unlinked link2 >> /mnt/dir/link3;\\\\\\n\");\n            emulator.serial0_send(\"nlinks /mnt/dir/link3 >> /mnt/dir/link3;\\\\\\n\");\n            emulator.serial0_send(\"cat /mnt/dir/link3;\\\\\\n\");\n            emulator.serial0_send(\"rm /mnt/dir/link3;\\\\\\n\");\n\n            // Verify nlinks of directories after unlinking hardlinks.\n            emulator.serial0_send(\"nlinks /mnt;\\\\\\n\");\n            emulator.serial0_send(\"nlinks /mnt/dir;\\\\\\n\");\n\n            // Verify nlinks of root directory after subdirectory is unlinked.\n            emulator.serial0_send(\"rmdir /mnt/dir;\\\\\\n\");\n            emulator.serial0_send(\"nlinks /mnt;\\\\\\n\");\n\n            emulator.serial0_send(\"echo done-hard-links\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-hard-links\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture,\n                test_file_small_string +\n                \"/mnt 2\\n\" +\n                \"/mnt 3\\n\" +\n                \"/mnt/dir 2\\n\" +\n                \"/mnt/target 1\\n\" +\n                \"same inode\\n\" +\n                \"different inode\\n\" +\n                \"/mnt 3\\n\" +\n                \"/mnt/dir 2\\n\" +\n                \"/mnt/target 4\\n\" +\n                \"/mnt/dir/link2 4\\n\" +\n                \"/mnt/target2 2\\n\" +\n                \"/mnt/link-other 2\\n\" +\n                \"renamed\\n\" +\n                \"moved\\n\" +\n                \"unlinked original\\n\" +\n                \"same inode after mv\\n\" +\n                \"/mnt 3\\n\" +\n                \"/mnt/dir 2\\n\" +\n                \"/mnt/link1-renamed 3\\n\" +\n                \"unlinked link1\\n\" +\n                \"/mnt/link2-moved 2\\n\" +\n                \"unlinked link2\\n\" +\n                \"/mnt/dir/link3 1\\n\" +\n                \"/mnt 3\\n\" +\n                \"/mnt/dir 2\\n\" +\n                \"/mnt 2\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Symlinks\",\n        timeout: 60,\n        files:\n        [\n            {\n                file: \"target\",\n                data: test_file_small,\n            },\n        ],\n        start: () =>\n        {\n            emulator.serial0_send(\"echo otherdata > /mnt/target2\\n\");\n            emulator.serial0_send(\"ln -s /mnt/target /mnt/symlink\\n\");\n            emulator.serial0_send(\"echo appended >> /mnt/symlink\\n\");\n\n            emulator.serial0_send(\"echo start-capture;\");\n\n            // Should output same file data.\n            emulator.serial0_send(\"cat /mnt/target;\");\n            emulator.serial0_send(\"cat /mnt/symlink;\");\n\n            // Swap target with the other file.\n            emulator.serial0_send(\"rm /mnt/target;\");\n            emulator.serial0_send(\"mv /mnt/target2 /mnt/target;\");\n\n            // Symlink should now read from that file.\n            emulator.serial0_send(\"cat /mnt/symlink;\");\n\n            emulator.serial0_send(\"rm /mnt/target;\");\n            emulator.serial0_send(\"rm /mnt/symlink;\");\n            emulator.serial0_send(\"echo done-symlinks\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-symlinks\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture,\n                test_file_small_string + \"appended\\n\" +\n                test_file_small_string + \"appended\\n\" +\n                \"otherdata\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Mknod - fifo\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"mkfifo /mnt/testfifo\\n\");\n            emulator.serial0_send('(cat /mnt/testfifo > /mnt/testfifo-output;echo \"\\ndone-fifo\") &\\n');\n            emulator.serial0_send(\"echo fifomessage > /mnt/testfifo\\n\");\n        },\n        end_trigger: \"done-fifo\",\n        end: async (capture, done) =>\n        {\n            const data = await emulator.read_file(\"testfifo-output\");\n            assert_equal(Buffer.from(data).toString(), \"fifomessage\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Readlink\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"touch /mnt/target\\n\");\n            emulator.serial0_send(\"ln -s /mnt/target /mnt/link\\n\");\n            emulator.serial0_send(\"echo start-capture;\");\n            emulator.serial0_send(\"readlink /mnt/link;\");\n            emulator.serial0_send(\"echo done-readlink\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-readlink\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture, \"/mnt/target\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Mkdir\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo notfoobar > /mnt/e-file\\n\");\n            emulator.serial0_send(\"mkdir /mnt/a-dir\\n\");\n            emulator.serial0_send(\"mkdir /mnt/a-dir/b-dir\\n\");\n            emulator.serial0_send(\"mkdir /mnt/a-dir/c-dir\\n\");\n            emulator.serial0_send(\"touch /mnt/a-dir/d-file\\n\");\n            emulator.serial0_send(\"echo mkdirfoobar > /mnt/a-dir/e-file\\n\");\n            emulator.serial0_send(\"echo done-mkdir\\n\");\n        },\n        end_trigger: \"done-mkdir\",\n        end: async (capture, done) =>\n        {\n            const data = await emulator.read_file(\"a-dir/e-file\");\n            assert_equal(Buffer.from(data).toString(), \"mkdirfoobar\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Walk\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"mkdir -p /mnt/walk/a/aa/aaa/aaaa\\n\");\n            emulator.serial0_send(\"mkdir -p /mnt/walk/a/aa/aaa/aaaa\\n\");\n            emulator.serial0_send(\"mkdir -p /mnt/walk/b/ba\\n\");\n            emulator.serial0_send(\"mkdir -p /mnt/walk/a/aa/aab\\n\");\n            emulator.serial0_send(\"mkdir -p /mnt/walk/a/aa/aac\\n\");\n            emulator.serial0_send(\"touch /mnt/walk/a/aa/aab/aabfile\\n\");\n            emulator.serial0_send(\"touch /mnt/walk/b/bfile\\n\");\n            emulator.serial0_send(\"echo start-capture;\");\n            emulator.serial0_send(\"find /mnt/walk | sort;\"); // order agnostic\n            emulator.serial0_send(\"echo done-walk\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-walk\",\n        end: (capture, done) =>\n        {\n            const actual = capture;\n            const expected =\n                \"/mnt/walk\\n\" +\n                \"/mnt/walk/a\\n\" +\n                \"/mnt/walk/a/aa\\n\" +\n                \"/mnt/walk/a/aa/aaa\\n\" +\n                \"/mnt/walk/a/aa/aaa/aaaa\\n\" +\n                \"/mnt/walk/a/aa/aab\\n\" +\n                \"/mnt/walk/a/aa/aab/aabfile\\n\" +\n                \"/mnt/walk/a/aa/aac\\n\" +\n                \"/mnt/walk/b\\n\" +\n                \"/mnt/walk/b/ba\\n\" +\n                \"/mnt/walk/b/bfile\\n\";\n            assert_equal(actual, expected);\n            done();\n        },\n    },\n    {\n        name: \"Statfs\",\n        timeout: 60,\n        allow_failure: true,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo start-capture;\");\n            emulator.serial0_send(\"touch /mnt/file;\");\n            emulator.serial0_send(\"df -PTk /mnt | tail -n 1;\");\n\n            // Grow file and verify space usage.\n            emulator.serial0_send(\"dd if=/dev/zero of=/mnt/file bs=1k count=4 status=none;\");\n            emulator.serial0_send(\"df -PTk /mnt | tail -n 1;\");\n\n            // Shrink file and verify space usage.\n            emulator.serial0_send(\"truncate -s 0 /mnt/file;\");\n            emulator.serial0_send(\"df -PTk /mnt | tail -n 1;\");\n\n            emulator.serial0_send(\"echo done-statfs\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-statfs\",\n        end: (capture, done) =>\n        {\n            const outputs = capture.split(\"\\n\").map(output => output.split(/\\s+/));\n            if(outputs.length < 3)\n            {\n                console.warn(\"Wrong format: %s\", capture);\n                test_fail();\n                done();\n                return;\n            }\n\n            const before = outputs[0];\n            const after_add = outputs[1];\n            const after_rm = outputs[2];\n\n            // mount tag\n            assert_equal(before[0], \"host9p\");\n\n            // fs type\n            assert_equal(before[1], \"9p\");\n\n            // total size in 1024 blocks\n            assert_equal(after_add[2], before[2]);\n            assert_equal(after_rm[2], before[2]);\n\n            // used size in 1024 blocks\n            assert_equal(+after_add[3], (+before[3]) + 4);\n            assert_equal(after_rm[3], before[3]);\n\n            // free size in 1024 blocks\n            assert_equal(+after_add[4], (+before[4]) - 4);\n            assert_equal(after_rm[4], before[4]);\n\n            // Entry [5] is percentage used.\n\n            // mount path\n            assert_equal(before[6], \"/mnt\");\n\n            done();\n        },\n    },\n    {\n        name: \"File Attributes\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo start-capture;\");\n\n            emulator.serial0_send(\"dd if=/dev/zero of=/mnt/file bs=1 count=137 status=none;\");\n            emulator.serial0_send(\"touch -t 200002022222 /mnt/file;\");\n            emulator.serial0_send(\"chmod =rw /mnt/file;\");\n            emulator.serial0_send(\"ls -l --full-time --color=never /mnt/file;\");\n\n            emulator.serial0_send(\"chmod +x /mnt/file;\");\n            emulator.serial0_send(\"chmod -w /mnt/file;\");\n            emulator.serial0_send(\"ln /mnt/file /mnt/file-link;\");\n            emulator.serial0_send(\"ls -l --full-time --color=never /mnt/file;\");\n\n            emulator.serial0_send(\"chmod -x /mnt/file;\");\n            emulator.serial0_send(\"truncate -s 100 /mnt/file;\");\n            emulator.serial0_send(\"touch -t 201011220344 /mnt/file;\");\n            emulator.serial0_send(\"rm /mnt/file-link;\");\n            emulator.serial0_send(\"ls -l --full-time --color=never /mnt/file;\");\n\n            emulator.serial0_send(\"echo done-file-attr\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-file-attr\",\n        end: (capture, done) =>\n        {\n            const outputs = capture.split(\"\\n\").map(output => output.split(/\\s+/));\n\n            if(outputs.length < 3)\n            {\n                console.warn(\"Wrong format (expected 3 rows): %s\", capture);\n                test_fail();\n                done();\n                return;\n            }\n\n            // mode\n            assert_equal(outputs[0][0], \"-rw-r--r--\");\n            // nlinks\n            assert_equal(outputs[0][1], \"1\");\n            // user\n            assert_equal(outputs[0][2], \"root\");\n            // group\n            assert_equal(outputs[0][3], \"root\");\n            // size\n            assert_equal(outputs[0][4], \"137\");\n            // atime\n            assert_equal(outputs[0][5], \"2000-02-02\");\n            assert_equal(outputs[0][6], \"22:22:00\");\n            assert_equal(outputs[0][7], \"+0000\");\n            // pathname\n            assert_equal(outputs[0][8], \"/mnt/file\");\n\n            // mode\n            assert_equal(outputs[1][0], \"-r-xr-xr-x\");\n            // nlinks\n            assert_equal(outputs[1][1], \"2\");\n            // user\n            assert_equal(outputs[1][2], \"root\");\n            // group\n            assert_equal(outputs[1][3], \"root\");\n            // size\n            assert_equal(outputs[1][4], \"137\");\n            // atime\n            assert_equal(outputs[1][5], \"2000-02-02\");\n            assert_equal(outputs[1][6], \"22:22:00\");\n            assert_equal(outputs[1][7], \"+0000\");\n            // pathname\n            assert_equal(outputs[1][8], \"/mnt/file\");\n\n            // mode\n            assert_equal(outputs[2][0], \"-r--r--r--\");\n            // nlinks\n            assert_equal(outputs[2][1], \"1\");\n            // user\n            assert_equal(outputs[2][2], \"root\");\n            // group\n            assert_equal(outputs[2][3], \"root\");\n            // size\n            assert_equal(outputs[2][4], \"100\");\n            // atime\n            assert_equal(outputs[2][5], \"2010-11-22\");\n            assert_equal(outputs[2][6], \"03:44:00\");\n            assert_equal(outputs[2][7], \"+0000\");\n            // pathname\n            assert_equal(outputs[2][8], \"/mnt/file\");\n\n            done();\n        },\n    },\n    {\n        name: \"Xattrwalk and Listxattr\",\n        timeout: 60,\n        allow_failure: true,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo originalvalue > /mnt/file\\n\");\n            emulator.serial0_send(\"echo start-capture;\");\n\n            emulator.serial0_send('setfattr --name=user.attr1 --value=\"val1\" /mnt/file;');\n            emulator.serial0_send('setfattr --name=user.attr2 --value=\"val2\" /mnt/file;');\n            emulator.serial0_send('setfattr --name=user.mime_type --value=\"text/plain\" /mnt/file;');\n            emulator.serial0_send('setfattr --name=user.nested.attr --value=\"foobar\" /mnt/file;');\n\n            // Unrecognized attribute name under other namespaces should be allowed.\n            emulator.serial0_send('setfattr --name=security.not_an_attr --value=\"val3\" /mnt/file;');\n\n            // Remove the caps attribute we've automatically put in. Tested later.\n            emulator.serial0_send(\"setfattr --remove=security.capability /mnt/file;\");\n\n            emulator.serial0_send(\"getfattr --encoding=text --absolute-names --dump /mnt/file | sort;\");\n            emulator.serial0_send(\"getfattr --encoding=text --absolute-names --name=user.nested.attr /mnt/file;\");\n            emulator.serial0_send(\"getfattr --encoding=text --absolute-names --name=security.not_an_attr /mnt/file;\");\n            emulator.serial0_send(\"getfattr --encoding=text --absolute-names --name=user.attr2 /mnt/file;\");\n            emulator.serial0_send(\"echo done-listxattr\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-listxattr\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture,\n                \"# file: /mnt/file\\n\" +\n                'security.not_an_attr=\"val3\"\\n' +\n                'user.attr1=\"val1\"\\n' +\n                'user.attr2=\"val2\"\\n' +\n                'user.mime_type=\"text/plain\"\\n' +\n                'user.nested.attr=\"foobar\"\\n' +\n                \"\\n\" +\n                \"# file: /mnt/file\\n\" +\n                'user.nested.attr=\"foobar\"\\n' +\n                \"\\n\" +\n                \"# file: /mnt/file\\n\" +\n                'security.not_an_attr=\"val3\"\\n' +\n                \"\\n\" +\n                \"# file: /mnt/file\\n\" +\n                'user.attr2=\"val2\"\\n');\n            done();\n        },\n    },\n    {\n        name: \"Xattrcreate\",\n        timeout: 60,\n        allow_failure: true,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo originalvalue > /mnt/file\\n\");\n            // Remove the caps attribute we've automatically put in. Tested later.\n            emulator.serial0_send(\"setfattr --remove=security.capability /mnt/file\\n\");\n\n            emulator.serial0_send(\"echo start-capture;\");\n\n            // Creation of new xattr using xattrcreate.\n            emulator.serial0_send(\"setfattr --name=user.foo --value=bar /mnt/file;\");\n            // File contents should not be overriden.\n            emulator.serial0_send(\"cat /mnt/file;\");\n            emulator.serial0_send(\"getfattr --encoding=hex --absolute-names --name=user.foo /mnt/file;\");\n\n            // Overwriting of xattr using xattrcreate.\n            emulator.serial0_send(\"setfattr --name=user.foo --value=baz /mnt/file;\");\n            // File contents should not be overriden.\n            emulator.serial0_send(\"cat /mnt/file;\");\n            emulator.serial0_send(\"getfattr --encoding=hex --absolute-names --name=user.foo /mnt/file;\");\n\n            emulator.serial0_send(\"echo done-xattrcreate\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-xattrcreate\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture,\n                \"originalvalue\\n\" +\n                \"# file: /mnt/file\\n\" +\n                'user.foo=\"bar\"\\n' +\n                \"\\n\" +\n                \"originalvalue\\n\" +\n                \"# file: /mnt/file\\n\" +\n                'user.foo=\"baz\"\\n' +\n                \"\\n\");\n            done();\n        },\n    },\n    {\n        name: \"Report All Security Capabilities\",\n        timeout: 60,\n        allow_failure: true,\n        start: () =>\n        {\n            emulator.serial0_send(\"touch /mnt/file\\n\");\n            emulator.serial0_send(\"echo start-capture;\");\n            emulator.serial0_send(\"getfattr --encoding=hex --absolute-names --name=security.capability /mnt/file;\");\n            emulator.serial0_send(\"echo done-xattr\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-xattr\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture,\n                \"# file: /mnt/file\\n\" +\n                \"security.capability=0x\" +\n                // magic and revision number\n                \"00000002\" +\n                // lower permitted\n                \"ffffffff\" +\n                // lower inheritable\n                \"ffffffff\" +\n                // higher permitted\n                \"3f000000\" +\n                // higher inheritable\n                \"3f000000\" +\n                \"\\n\\n\");\n            done();\n        },\n    },\n    {\n        name: \"File Locks\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"touch /mnt/file\\n\");\n            emulator.serial0_send(\"touch /mnt/logs\\n\");\n            emulator.serial0_send(\"mkfifo /mnt/fifo1\\n\");\n            emulator.serial0_send(\"mkfifo /mnt/fifo2\\n\");\n\n            emulator.serial0_send(\"flock -s /mnt/file -c 'cat /mnt/fifo1 >> /mnt/file' &\\n\");\n            emulator.serial0_send(\"flock -s /mnt/file -c 'echo lock-shared-2 >> /mnt/file' \\n\");\n            emulator.serial0_send(\"flock -xn /mnt/file -c 'echo lock unblocked! >> /mnt/logs' \\n\");\n            emulator.serial0_send(\"echo lock-shared-1 > /mnt/fifo1\\n\");\n\n            emulator.serial0_send(\"flock -x /mnt/file -c 'cat /mnt/fifo1 >> /mnt/file' &\\n\");\n            emulator.serial0_send(\"flock -x /mnt/file -c 'echo lock-exclusive-2 >> /mnt/file' &\\n\");\n            emulator.serial0_send(\"flock -sn /mnt/file -c 'echo lock unblocked! >> /mnt/logs' \\n\");\n            emulator.serial0_send(\"echo lock-exclusive-1 > /mnt/fifo1\\n\");\n\n            emulator.serial0_send(\"flock -sn /mnt/file -c 'cat /mnt/fifo1 >> /mnt/file' &\\n\");\n            emulator.serial0_send(\"flock -s /mnt/file -c 'cat /mnt/fifo2 >> /mnt/file' &\\n\");\n            emulator.serial0_send(\"flock -x /mnt/file -c 'echo lock-exclusive-3 >> /mnt/file' &\\n\");\n            emulator.serial0_send(\"echo lock-shared-4 > /mnt/fifo2\\n\");\n            emulator.serial0_send(\"sleep 0.1\\n\");\n            emulator.serial0_send(\"echo lock-shared-3 > /mnt/fifo1\\n\");\n\n            emulator.serial0_send(\"echo start-capture;\\\\\\n\");\n            emulator.serial0_send(\"cat /mnt/file;\\\\\\n\");\n            emulator.serial0_send(\"cat /mnt/logs;\\\\\\n\");\n            emulator.serial0_send(\"echo done-locks\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-locks\",\n        end: (capture, done) =>\n        {\n            assert_equal(capture,\n                \"lock-shared-2\\n\" +\n                \"lock-shared-1\\n\" +\n                \"lock-exclusive-1\\n\" +\n                \"lock-exclusive-2\\n\" +\n                \"lock-shared-4\\n\" +\n                \"lock-shared-3\\n\" +\n                \"lock-exclusive-3\\n\");\n\n            const idx = emulator.fs9p.Search(0, \"file\");\n\n            const P9_LOCK_TYPE_RDLCK = 0;\n            const P9_LOCK_TYPE_WRLCK = 1;\n            const P9_LOCK_TYPE_UNLCK = 2;\n            const P9_LOCK_SUCCESS = 0;\n            const P9_LOCK_BLOCKED = 1;\n            const CLIENT_ID = \"under test\";\n\n            function test_getlock(num, type, pos, proc_id, locked)\n            {\n                const lock = emulator.fs9p.DescribeLock(type, pos, 1, proc_id, CLIENT_ID);\n                const ret = emulator.fs9p.GetLock(idx, lock, 0);\n                assert_equal(ret !== null, locked,\n                    `getlock ${num}: type=${type}, pos=${pos}, proc_id=${proc_id}. Wrong state:`);\n            }\n\n            function test_lock(num, type, start, length, proc_id, status, lock_state)\n            {\n                console.log(`    Lock ${num}: type=${type}, start=${start}, length=${length} ` +\n                    ` proc_id=${proc_id}, expected state=${lock_state}`);\n\n                const lock = emulator.fs9p.DescribeLock(type, start, length, proc_id, CLIENT_ID);\n                assert_equal(emulator.fs9p.Lock(idx, lock, 0), status, \"Wrong status:\");\n\n                for(const [i, state] of [...lock_state].entries())\n                {\n                    switch(state)\n                    {\n                        case \"1\":\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, false);\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, false);\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, true);\n                            break;\n                        case \"2\":\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, false);\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, false);\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, true);\n                            break;\n                        case \"3\":\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, false);\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, true);\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, false);\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, true);\n                            break;\n                        case \"e\":\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, false);\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, true);\n                            break;\n                        case \"E\":\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 1, true);\n                            test_getlock(num, P9_LOCK_TYPE_RDLCK, i, 2, false);\n                            break;\n                        case \"-\":\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 1, false);\n                            test_getlock(num, P9_LOCK_TYPE_WRLCK, i, 2, false);\n                            break;\n                    }\n                }\n            }\n\n            // Key:\n            // 1/2/3 = shared lock by process 1/2/both\n            // e/E   = exclusive lock by process 1/2\n            // -     = no locks\n            const I = Infinity;\n            test_lock(0, P9_LOCK_TYPE_RDLCK, 0, 1, 1, P9_LOCK_SUCCESS, \"1-------\"); // First lock.\n            test_lock(1, P9_LOCK_TYPE_RDLCK, 0, 2, 1, P9_LOCK_SUCCESS, \"11------\"); // Replace.\n            test_lock(2, P9_LOCK_TYPE_RDLCK, 1, 1, 2, P9_LOCK_SUCCESS, \"13------\");\n            test_lock(3, P9_LOCK_TYPE_RDLCK, 2, 2, 1, P9_LOCK_SUCCESS, \"1311----\"); // Skip. Merge before.\n            test_lock(4, P9_LOCK_TYPE_WRLCK, 0, 1, 1, P9_LOCK_SUCCESS, \"e311----\"); // Shrink left.\n            test_lock(5, P9_LOCK_TYPE_WRLCK, 1, 1, 1, P9_LOCK_BLOCKED, \"e311----\");\n            test_lock(6, P9_LOCK_TYPE_UNLCK, 0, 4, 1, P9_LOCK_SUCCESS, \"-2------\"); // Delete.\n            test_lock(7, P9_LOCK_TYPE_WRLCK, 1, 2, 1, P9_LOCK_BLOCKED, \"-2------\");\n            test_lock(8, P9_LOCK_TYPE_UNLCK, 1, 3, 2, P9_LOCK_SUCCESS, \"--------\"); // Delete.\n            test_lock(9, P9_LOCK_TYPE_WRLCK, 1, 1, 1, P9_LOCK_SUCCESS, \"-e------\");\n            test_lock(10, P9_LOCK_TYPE_RDLCK, 3, 3, 1, P9_LOCK_SUCCESS, \"-e-111--\"); // Skip.\n            test_lock(11, P9_LOCK_TYPE_RDLCK, 2, 1, 2, P9_LOCK_SUCCESS, \"-e2111--\"); // Skip past.\n            test_lock(12, P9_LOCK_TYPE_UNLCK, 2, 1, 2, P9_LOCK_SUCCESS, \"-e-111--\"); // Delete.\n            test_lock(13, P9_LOCK_TYPE_WRLCK, 0, 1, 1, P9_LOCK_SUCCESS, \"ee-111--\");\n            test_lock(14, P9_LOCK_TYPE_WRLCK, 1, 4, 1, P9_LOCK_SUCCESS, \"eeeee1--\"); // Merge before. Shrink both ways.\n            test_lock(15, P9_LOCK_TYPE_WRLCK, 1, 2, 2, P9_LOCK_BLOCKED, \"eeeee1--\");\n            test_lock(16, P9_LOCK_TYPE_RDLCK, 4, 5, 2, P9_LOCK_BLOCKED, \"eeeee1--\");\n            test_lock(17, P9_LOCK_TYPE_RDLCK, 5, I, 2, P9_LOCK_SUCCESS, \"eeeee322\");\n            test_lock(18, P9_LOCK_TYPE_UNLCK, 0, I, 1, P9_LOCK_SUCCESS, \"-----222\"); // Replace.\n            test_lock(19, P9_LOCK_TYPE_RDLCK, 4, I, 2, P9_LOCK_SUCCESS, \"----2222\"); // Replace.\n            test_lock(20, P9_LOCK_TYPE_WRLCK, 2, I, 2, P9_LOCK_SUCCESS, \"--EEEEEE\"); // Replace.\n            test_lock(21, P9_LOCK_TYPE_WRLCK, 0, 1, 2, P9_LOCK_SUCCESS, \"E-EEEEEE\");\n            test_lock(22, P9_LOCK_TYPE_WRLCK, 1, 3, 2, P9_LOCK_SUCCESS, \"EEEEEEEE\"); // Merge both. Shrink left.\n            test_lock(23, P9_LOCK_TYPE_RDLCK, 3, 4, 2, P9_LOCK_SUCCESS, \"EEE2222E\"); // Split.\n            test_lock(24, P9_LOCK_TYPE_RDLCK, 1, 2, 2, P9_LOCK_SUCCESS, \"E222222E\"); // Merge after. Shrink right.\n            test_lock(25, P9_LOCK_TYPE_RDLCK, 2, 3, 2, P9_LOCK_SUCCESS, \"E222222E\"); // No-op.\n\n            done();\n        },\n    },\n    {\n        name: \"Stress Files\",\n        timeout: 360,\n        start: () =>\n        {\n            emulator.serial0_send(\"mkdir /mnt/stress-files\\n\");\n\n            emulator.serial0_send('cat << \"EOF\" | sh\\n');\n\n            // Create files.\n            // Ensure directory inode data exceeds maximum message size for 9p.\n            emulator.serial0_send(\"for f in $(seq -w 0 999)\\n\");\n            emulator.serial0_send(\"do\\n\");\n            emulator.serial0_send('    echo \"$f\" > \"/mnt/stress-files/file-$f\"\\n');\n            emulator.serial0_send(\"done\\n\");\n\n            emulator.serial0_send(\"echo start-capture\\n\");\n\n            // Read some of them.\n            emulator.serial0_send(\"for f in $(seq -w 0 31 999)\\n\");\n            emulator.serial0_send(\"do\\n\");\n            emulator.serial0_send('    cat \"/mnt/stress-files/file-$f\"\\n');\n            emulator.serial0_send(\"done\\n\");\n\n            // Walk.\n            emulator.serial0_send(\"find /mnt/stress-files | sort\\n\");\n\n            // Delete and verify.\n            // Using glob checks readdir.\n            emulator.serial0_send(\"rm /mnt/stress-files/file-*\\n\");\n            emulator.serial0_send('test -z \"$(ls /mnt/stress-files)\" && echo delete-success\\n');\n\n            emulator.serial0_send(\"echo done-stress-files\\n\");\n            emulator.serial0_send(\"EOF\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-stress-files\",\n        end: (capture, done) =>\n        {\n            let expected = \"\";\n            for(let i = 0; i < 1000; i += 31)\n            {\n                expected += i.toString().padStart(3, \"0\") + \"\\n\";\n            }\n            expected += \"/mnt/stress-files\\n\";\n            for(let i = 0; i < 1000; i ++)\n            {\n                expected += \"/mnt/stress-files/file-\" + i.toString().padStart(3, \"0\") + \"\\n\";\n            }\n            expected += \"delete-success\\n\";\n            assert_equal(capture, expected);\n            done();\n        },\n    },\n    {\n        name: \"Stress Directories\",\n        timeout: 360,\n        start: () =>\n        {\n            emulator.serial0_send('cat << \"EOF\" | sh\\n');\n\n            emulator.serial0_send(\"p=/mnt/stress-dirs\\n\");\n            emulator.serial0_send('mkdir \"$p\"\\n');\n\n            // Create deep folder structure\n            emulator.serial0_send(\"for i in $(seq 0 99)\\n\");\n            emulator.serial0_send(\"do\\n\");\n            emulator.serial0_send('    p=\"$p/$i\"\\n');\n            emulator.serial0_send('    mkdir \"$p\"\\n');\n            emulator.serial0_send('    echo \"$i\" > \"$p/file\"\\n');\n            emulator.serial0_send(\"done\\n\");\n\n            // Try accessing deep files\n            emulator.serial0_send(\"p=/mnt/stress-dirs\\n\");\n            emulator.serial0_send(\"echo start-capture\\n\");\n            // Skip first 80 - otherwise too slow\n            emulator.serial0_send(\"for i in $(seq 0 79)\\n\");\n            emulator.serial0_send(\"do\\n\");\n            emulator.serial0_send('    p=\"$p/$i\"\\n');\n            emulator.serial0_send(\"done\\n\");\n            emulator.serial0_send(\"for i in $(seq 80 99)\\n\");\n            emulator.serial0_send(\"do\\n\");\n            emulator.serial0_send('    p=\"$p/$i\"\\n');\n            emulator.serial0_send('    cat \"$p/file\"\\n');\n            emulator.serial0_send(\"done\\n\");\n\n            // Delete and verify\n            emulator.serial0_send(\"rm -rf /mnt/stress-dirs/0\\n\");\n            emulator.serial0_send('test -z \"$(ls /mnt/stress-dirs)\" && echo delete-success\\n');\n\n            emulator.serial0_send(\"echo done-stress-dirs\\n\");\n            emulator.serial0_send(\"EOF\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-stress-dirs\",\n        end: (capture, done) =>\n        {\n            const outputs = capture.split(\"\\n\");\n            for(let i = 0; i < 20; i++)\n            {\n                assert_equal(outputs[i], `${i + 80}`);\n            }\n            assert_equal(outputs[20], \"delete-success\");\n            done();\n        },\n    },\n    {\n        name: \"Read Past Available\",\n        timeout: 60,\n        start: () =>\n        {\n            emulator.serial0_send(\"echo a > /mnt/small-file\\n\");\n            emulator.serial0_send(\"echo start-capture;\");\n\n            // Reading from offsets > size of file should not read anything.\n            emulator.serial0_send(\"dd if=/mnt/small-file bs=1 count=1 skip=10;\");\n            emulator.serial0_send(\"dd if=/mnt/small-file bs=1 count=1 skip=100;\");\n            emulator.serial0_send(\"dd if=/mnt/small-file bs=1 count=1 skip=1000;\");\n\n            emulator.serial0_send(\"echo done-read-exceed\\n\");\n        },\n        capture_trigger: \"start-capture\",\n        end_trigger: \"done-read-exceed\",\n        end: (capture, done) =>\n        {\n            const outputs = capture.split(\"\\n\");\n            assert_equal(outputs[0], \"0+0 records in\");\n            assert_equal(outputs[1], \"0+0 records out\");\n            assert_equal(outputs[2], \"0+0 records in\");\n            assert_equal(outputs[3], \"0+0 records out\");\n            assert_equal(outputs[4], \"0+0 records in\");\n            assert_equal(outputs[5], \"0+0 records out\");\n            done();\n        },\n    },\n];\n\nlet test_num = 0;\nlet test_timeout = 0;\nlet test_has_failed = false;\nconst failed_tests = [];\n\nfunction test_fail()\n{\n    if(!test_has_failed)\n    {\n        test_has_failed = true;\n        failed_tests.push(test_num);\n    }\n}\n\nconst emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\" },\n    autostart: true,\n    memory_size: 64 * 1024 * 1024,\n    filesystem: {\n        baseurl: __dirname + \"/testfs/\",\n    },\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: SHOW_LOGS ? 0x400000 : 0,\n});\n\nlet ran_command = false;\nlet line = \"\";\nlet capturing = false;\nlet capture = \"\";\nlet next_trigger;\nlet next_trigger_handler;\n\nasync function prepare_test()\n{\n    console.log(\"\\nPreparing test #%d: %s\", test_num, tests[test_num].name);\n\n    if(tests[test_num].timeout)\n    {\n        test_timeout = setTimeout(() =>\n        {\n            console.error(\"[-] Test #%d (%s) took longer than %s sec. Timing out and terminating.\", test_num, tests[test_num].name, tests[test_num].timeout);\n            process.exit(1);\n        }, tests[test_num].timeout * 1000);\n    }\n\n    console.log(\"    Nuking /mnt\");\n    emulator.fs9p.RecursiveDelete(\"\");\n\n    if(tests[test_num].use_fsjson)\n    {\n        console.log(\"    Reloading files from json\");\n        emulator.fs9p.load_from_json(testfsjson);\n    }\n\n    console.log(\"    Loading additional files\");\n    if(tests[test_num].files)\n    {\n        let remaining = tests[test_num].files.length;\n        for(const f of tests[test_num].files)\n        {\n            await emulator.create_file(f.file, f.data);\n        }\n    }\n\n    console.log(\"Starting test #%d: %s\", test_num, tests[test_num].name);\n\n    capture = \"\";\n\n    tests[test_num].start();\n\n    if(tests[test_num].capture_trigger)\n    {\n        next_trigger = tests[test_num].capture_trigger;\n        next_trigger_handler = start_capture;\n    }\n    else\n    {\n        next_trigger = tests[test_num].end_trigger;\n        next_trigger_handler = end_test;\n    }\n}\n\nfunction start_capture()\n{\n    console.log(\"Capturing...\");\n    capture = \"\";\n    capturing = true;\n\n    next_trigger = tests[test_num].end_trigger;\n    next_trigger_handler = end_test;\n}\n\nfunction end_test()\n{\n    capturing = false;\n\n    if(tests[test_num].timeout)\n    {\n        clearTimeout(test_timeout);\n    }\n\n    tests[test_num].end(capture, report_test);\n}\n\nfunction report_test()\n{\n    if(!test_has_failed)\n    {\n        console.log(\"[+] Test #%d passed: %s\", test_num, tests[test_num].name);\n    }\n    else\n    {\n        if(tests[test_num].allow_failure)\n        {\n            console.warn(\"Test #%d failed: %s (failure allowed)\", test_num, tests[test_num].name);\n        }\n        else\n        {\n            console.error(\"[-] Test #%d failed: %s\", test_num, tests[test_num].name);\n\n            if(STOP_ON_FIRST_FAILURE)\n            {\n                finish_tests();\n            }\n        }\n        test_has_failed = false;\n    }\n\n    test_num++;\n\n    if(test_num < tests.length)\n    {\n        prepare_test();\n    }\n    else\n    {\n        finish_tests();\n    }\n}\n\nfunction finish_tests()\n{\n    emulator.stop();\n\n    console.log(\"\\nTests finished.\");\n    if(failed_tests.length === 0)\n    {\n        console.log(\"All tests passed\");\n    }\n    else\n    {\n        let unallowed_failure = false;\n\n        console.error(\"[-] Failed %d out of %d tests:\", failed_tests.length, tests.length);\n        for(const num of failed_tests)\n        {\n            if(tests[num].allow_failure)\n            {\n                console.warn(\"#%d %s (failure allowed)\", num, tests[num].name);\n            }\n            else\n            {\n                unallowed_failure = true;\n                console.error(\"[-] #%d %s\", num, tests[num].name);\n            }\n        }\n        if(unallowed_failure)\n        {\n            process.exit(1);\n        }\n    }\n}\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n});\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    let new_line = \"\";\n    let is_new_line = false;\n    if(chr === \"\\n\")\n    {\n        is_new_line = true;\n        new_line = line;\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n\n    if(!ran_command && line.endsWith(\"~% \"))\n    {\n        ran_command = true;\n        prepare_test();\n    }\n    else if(new_line === next_trigger)\n    {\n        next_trigger_handler();\n    }\n    else if(is_new_line && capturing)\n    {\n        capture += new_line + \"\\n\";\n        console.log(\"    Captured: %s\", new_line);\n    }\n    else if(is_new_line)\n    {\n        console.log(\"    Serial: %s\", new_line);\n    }\n});\n"
  },
  {
    "path": "tests/devices/virtio_balloon.js",
    "content": "#!/usr/bin/env node\n\nimport assert from \"assert/strict\";\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst SHOW_LOGS = false;\n\nfunction wait(time) {\n    return new Promise((res) => setTimeout(res, time));\n}\n\nlet s1;\nconst tests =\n[\n    {\n        name: \"Use space\",\n        start: async () =>\n        {\n            emulator.serial0_send(\"udhcpc\\n\");\n            emulator.serial0_send(\"free -h\\n\");\n            emulator.serial0_send(\"lspci -k\\n\");\n            emulator.serial0_send(\"dd if=/dev/urandom of=/tmp/file bs=1M count=64\\n\");\n            emulator.serial0_send(\"du -h /tmp/file\\n\");\n            emulator.serial0_send(\"free -h\\n\");\n            emulator.serial0_send(\"rm /tmp/file\\n\");\n            emulator.serial0_send(\"free -h\\n\");\n            emulator.serial0_send(\"echo done\\n\");\n        },\n        end_trigger: \"done\",\n        end: (capture) =>\n        {\n        },\n    },\n    {\n        name: \"Delete space\",\n        start: async () =>\n        {\n            emulator.serial0_send(\"echo 3 > /proc/sys/vm/drop_caches\\n\");\n            emulator.serial0_send(\"df -h; echo done\\n\");\n        },\n        end_trigger: \"done\",\n        end: (capture) =>\n        {\n        },\n    },\n    {\n        name: \"Reclaim space\",\n        start: async () =>\n        {\n            s1 = await emulator.save_state();\n            emulator.v86.cpu.devices.virtio_balloon.GetStats(s => { console.log(s); });\n            emulator.v86.cpu.devices.virtio_balloon.Cleanup(async (zeroed) => {\n                console.log(\"Zeroed \", zeroed / 1024 / 1024, \"MB of pages\");\n                await wait(2000);\n                emulator.serial0_send(\"echo done\\n\");\n            });\n        },\n        end_trigger: \"done\",\n        end: async (capture) =>\n        {\n            let s2 = await emulator.save_state();\n            let saved = s1.byteLength - s2.byteLength;\n            console.log(`Saved ${saved>>20}MB of ${s1.byteLength>>20}MB (${saved/s1.byteLength*100}%)`);\n            assert(saved > 1000000, \"not enough space saved\");\n            emulator.serial0_send(\"echo done\\n\");\n        },\n    },\n    {\n        name: \"Wait\",\n        start: async () =>\n        {\n        },\n        end_trigger: \"done\",\n        end: async (capture) =>\n        {\n        },\n    },\n    {\n        name: \"Get Memory\",\n        start: async () =>\n        {\n            emulator.serial0_send(\"free -m; echo done\\n\");\n        },\n        end_trigger: \"done\",\n        end: async (capture) =>\n        {\n        },\n    },\n    {\n        name: \"Inflate Balloon\",\n        start: async () =>\n        {\n            emulator.v86.cpu.devices.virtio_balloon.Inflate(30000);\n            await wait(1000);\n            emulator.serial0_send(\"free -m; echo done\\n\");\n        },\n        end_trigger: \"done\",\n        end: async (capture) =>\n        {\n            assert(emulator.v86.cpu.devices.virtio_balloon.actual === 30000, \"Got 30000 pages from vm\");\n        },\n    },\n];\n\n\nconst emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    bzimage: { url: __dirname + \"/../../images/buildroot-bzimage68.bin\" },\n    autostart: true,\n    memory_size: 200 * 1024 * 1024,\n    disable_jit: +process.env.DISABLE_JIT,\n    virtio_console: true,\n    virtio_balloon: true,\n    uart0: true,\n    net_device: {\n        relay_url: \"fetch\",\n        type: \"virtio\",\n    },\n    log_level: SHOW_LOGS ? 0x400000 : 0,\n    cmdline: \"console=/dev/ttyS0\"\n});\n\nlet test_num = 0;\nlet booted = false;\nlet line = \"\";\nlet capture = \"\";\nlet end_trigger;\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n});\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    const chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    let new_line = \"\";\n    if(chr === \"\\n\")\n    {\n        console.log(\"    Captured: %s\", line);\n        new_line = line;\n        capture += line + \"\\n\";\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n\n    if(new_line === end_trigger)\n    {\n        let test_has_failed = false;\n\n        try {\n            tests[test_num].end(capture);\n        } catch(e) {\n            console.log(e);\n            test_has_failed = true;\n        }\n\n        if(!test_has_failed)\n        {\n            console.log(\"[+] Test #%d passed: %s\", test_num, tests[test_num].name);\n        }\n        else\n        {\n            if(tests[test_num].allow_failure)\n            {\n                console.warn(\"[!] Test #%d failed: %s (failure allowed)\", test_num, tests[test_num].name);\n            }\n            else\n            {\n                console.error(\"[-] Test #%d failed: %s\", test_num, tests[test_num].name);\n                process.exit(1);\n            }\n        }\n\n        test_num++;\n\n    }\n\n    if(!booted && line.endsWith(\"~% \") || new_line === end_trigger)\n    {\n        booted = true;\n\n        if(test_num >= tests.length)\n        {\n            emulator.destroy();\n\n            console.log(\"Tests finished.\");\n        }\n        else\n        {\n            console.log(\"Starting test #%d: %s\", test_num, tests[test_num].name);\n\n            capture = \"\";\n            end_trigger = tests[test_num].end_trigger;\n\n            tests[test_num].start();\n        }\n    }\n});\n"
  },
  {
    "path": "tests/devices/virtio_console.js",
    "content": "#!/usr/bin/env node\n\nimport assert from \"assert/strict\";\nimport fs from \"node:fs\";\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst SHOW_LOGS = false;\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    bzimage: { url: __dirname + \"/../../images/buildroot-bzimage68.bin\" },\n    autostart: true,\n    memory_size: 512 * 1024 * 1024,\n    acpi: true,\n    cmdline: [\n        \"console=ttyS0\",\n        \"audit=0\",\n    ].join(\" \"),\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: SHOW_LOGS ? 0x400000 : 0,\n    virtio_console: true,\n});\n\nlet line = \"\";\nlet sent_command = false;\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    var chr = String.fromCharCode(byte);\n\n    process.stdout.write(chr);\n\n    if(chr === \"\\n\")\n    {\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n\n    // TODO: use better prompt detection once it's configured to not print colours\n    if(!sent_command && line.endsWith(\"~%\"))\n    {\n        sent_command = true;\n        emulator.serial0_send(\"lspci -vv; echo ping > /dev/hvc0\\n\");\n    }\n\n    if(line.endsWith(\"pong\"))\n    {\n        console.log(\"\\nTest passed\");\n        emulator.destroy();\n    }\n});\n\nlet got_output = false;\n\nemulator.add_listener(\"virtio-console0-output-bytes\", function(bytes)\n{\n    if(!got_output)\n    {\n        got_output = true;\n        console.log(\"From virtio console:\", String.fromCharCode.apply(String, bytes));\n        emulator.serial0_send(\"cat /dev/hvc0\\n\");\n        setTimeout(() => {\n            emulator.bus.send(\"virtio-console0-input-bytes\", Uint8Array.from(Buffer.from(\"pong\\n\")));\n        }, 5000);\n    }\n});\n"
  },
  {
    "path": "tests/devices/wisp_network.js",
    "content": "#!/usr/bin/env -S node --experimental-websocket\n\nimport assert from \"assert/strict\";\nimport url from \"node:url\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst SHOW_LOGS = false;\n\nconst tests =\n[\n    {\n        name: \"DHCP\",\n        start: () =>\n        {\n            emulator.serial0_send(\"udhcpc\\n\");\n            emulator.serial0_send(\"echo -e done\\\\\\\\tudhcpc\\n\");\n        },\n        end_trigger: \"done\\tudhcpc\",\n        end: (capture) =>\n        {\n            assert(/lease of 192.168.86.100 obtained/.test(capture), \"lease of 192.168.86.100 obtained\");\n        },\n    },\n    {\n        name: \"ifconfig\",\n        start: () =>\n        {\n            emulator.serial0_send(\"ifconfig\\n\");\n            emulator.serial0_send(\"echo -e done\\\\\\\\tifconfig\\n\");\n        },\n        end_trigger: \"done\\tifconfig\",\n        end: (capture) =>\n        {\n            assert(/192.168.86.100/.test(capture), \"192.168.86.100\");\n        },\n    },\n    {\n        name: \"route\",\n        start: () =>\n        {\n            emulator.serial0_send(\"ip route\\n\");\n            emulator.serial0_send(\"echo -e done\\\\\\\\troute\\n\");\n        },\n        end_trigger: \"done\\troute\",\n        end: (capture) =>\n        {\n            assert(/192.168.86.1/.test(capture), \"192.168.86.100\");\n        },\n    },\n    //{\n    //    name: \"arp -a\",\n    //    start: () =>\n    //    {\n    //        emulator.serial0_send(\"arp -a\\n\");\n    //        emulator.serial0_send(\"echo -e done\\\\\\\\tarp\\n\");\n    //    },\n    //    end_trigger: \"done\\tarp\",\n    //    end: (capture) =>\n    //    {\n    //        assert(/.192.168.86.1. at 52:54:00:01:02:03 \\[ether\\] {2}on eth0/.test(capture), \"(192.168.86.1) at 52:54:00:01:02:03 [ether]  on eth0\");\n    //    },\n    //},\n    {\n        name: \"Curl example.org\",\n        allow_failure: true,\n        start: () =>\n        {\n            emulator.serial0_send(\"wget -T 10 -O - example.org\\n\");\n            emulator.serial0_send(\"echo -e done\\\\\\\\texample.org\\n\");\n        },\n        end_trigger: \"done\\texample.org\",\n        end: (capture) =>\n        {\n            assert(/This domain is for use in illustrative examples in documents/.test(capture), \"got example.org text\");\n        },\n    },\n\n];\n\nconst emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\" },\n    autostart: true,\n    memory_size: 64 * 1024 * 1024,\n    disable_jit: +process.env.DISABLE_JIT,\n    network_relay_url: \"wisps://wisp.mercurywork.shop/\",\n    log_level: SHOW_LOGS ? 0x400000 : 0,\n});\n\nlet test_num = 0;\nlet booted = false;\nlet line = \"\";\nlet capture = \"\";\nlet end_trigger;\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n});\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    const chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    let new_line = \"\";\n    if(chr === \"\\n\")\n    {\n        console.log(\"    Captured: %s\", line);\n        new_line = line;\n        capture += line + \"\\n\";\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n\n    if(new_line === end_trigger)\n    {\n        let test_has_failed = false;\n\n        try {\n            tests[test_num].end(capture);\n        } catch(e) {\n            console.log(e);\n            test_has_failed = true;\n        }\n\n        if(!test_has_failed)\n        {\n            console.log(\"[+] Test #%d passed: %s\", test_num, tests[test_num].name);\n        }\n        else\n        {\n            if(tests[test_num].allow_failure)\n            {\n                console.warn(\"[!] Test #%d failed: %s (failure allowed)\", test_num, tests[test_num].name);\n            }\n            else\n            {\n                console.error(\"[-] Test #%d failed: %s\", test_num, tests[test_num].name);\n                process.exit(1);\n            }\n        }\n\n        test_num++;\n\n    }\n\n    if(!booted && line.endsWith(\"~% \") || new_line === end_trigger)\n    {\n        booted = true;\n\n        if(test_num >= tests.length)\n        {\n            emulator.destroy();\n\n            console.log(\"Tests finished.\");\n        }\n        else\n        {\n            console.log(\"Starting test #%d: %s\", test_num, tests[test_num].name);\n\n            capture = \"\";\n            end_trigger = tests[test_num].end_trigger;\n\n            tests[test_num].start();\n        }\n    }\n});\n"
  },
  {
    "path": "tests/expect/readme.md",
    "content": "Expect tests\n------------\n\nThese so-called \"expect tests\" test the code generation, i.e. the translation\nof x86 assembly to Web Assembly. Use the following workflow:\n\n1. Hack on the code generator\n2. Run make `expect-tests`\n3. For each failing test:\n    - Manually verify that the generated code changes are as expected by the diff\n    - If so, accept the new code by copying the .actual.wast file over the .wast file\n      and checking the new .wast file into git\n\nIn order to add a new expect test:\n\n1. Create a new .asm file in tests/\n2. Run make `expect-tests`\n3. Verify the generated code and use the printed cp command to accept the test\n\nNote that .asm files are translated to flat binaries, not elf files, so a .data\nsection may be meaningless.\n\n\nFor more information, see https://blog.janestreet.com/testing-with-expectations/\n"
  },
  {
    "path": "tests/expect/run.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport assert from \"node:assert/strict\";\nimport url from \"node:url\";\nimport { spawnSync } from \"node:child_process\";\nimport wabt from \"../../build/libwabt.cjs\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst libwabt = wabt();\n\nconst TEST_NAME = process.env.TEST_NAME;\n\nconst LOG_LEVEL = 0;\nconst MIN_MEMORY_OFFSET = 4096;\n\nconst GIT_DIFF_FLAGS = [\"--no-index\", \"--patience\", \"--color=always\"];\n\nconst TEST_DIR = path.join(__dirname, \"tests\");\nconst BUILD_DIR = path.join(TEST_DIR, \"build\");\n\nfunction run_all()\n{\n    const asm_files = fs.readdirSync(TEST_DIR).filter(filename => filename.endsWith(\".asm\"));\n\n    const files = asm_files.map(asm_file => {\n        const name = asm_file.slice(0, -4);\n        return {\n            name,\n            expect_file: path.relative(\".\", path.join(TEST_DIR, name + \".wast\")),\n            actual_file: path.relative(\".\", path.join(BUILD_DIR, name + \".actual.wast\")),\n            actual_wasm: path.relative(\".\", path.join(BUILD_DIR, name + \".wasm\")),\n            asm_file: path.join(TEST_DIR, name + \".asm\"),\n            executable_file: path.join(BUILD_DIR, name + \".bin\"),\n        };\n    }).filter(({ name }) => !TEST_NAME || name === TEST_NAME);\n\n    next_test(0);\n\n    function next_test(i)\n    {\n        if(files[i])\n        {\n            run_test(files[i], () => next_test(i + 1));\n        }\n    }\n}\n\n// Remove parts that may not be stable between multiple runs\nfunction normalise_wast(wast)\n{\n    return wast.replace(/offset=(\\d+)/g, function(match, offset)\n        {\n            offset = Number(offset);\n\n            if(offset >= MIN_MEMORY_OFFSET)\n            {\n                return \"offset={normalised output}\";\n            }\n            else\n            {\n                return match;\n            }\n        }).replace(/memory \\$[\\w\\.]+ \\d+/g, \"memory {normalised output}\");\n}\n\nfunction run_test({ name, executable_file, expect_file, actual_file, actual_wasm, asm_file }, onfinished)\n{\n    const emulator = new V86({\n        autostart: false,\n        memory_size: 2 * 1024 * 1024,\n        log_level: LOG_LEVEL,\n    });\n\n    const executable = fs.readFileSync(executable_file);\n    const asm = fs.readFileSync(asm_file);\n\n    const is_32 = asm.includes(\"BITS 32\\n\");\n\n    emulator.add_listener(\"emulator-loaded\", function()\n        {\n            const cpu = emulator.v86.cpu;\n\n            const hook_not_called_timeout = setTimeout(() => {\n                throw new Error(\"Hook for code generation not called\");\n            }, 1000);\n\n            cpu.test_hook_did_generate_wasm = function(wasm)\n            {\n                const wast = normalise_wast(disassemble_wasm(wasm));\n\n                clearTimeout(hook_not_called_timeout);\n                fs.writeFileSync(actual_file, wast);\n                fs.writeFileSync(actual_wasm, wasm);\n\n                cpu.test_hook_did_generate_wasm = function()\n                {\n                    cpu.test_hook_did_generate_wasm = function() {};\n                    throw new Error(\"Hook for wasm generation called multiple times\");\n                };\n\n                if(!fs.existsSync(expect_file))\n                {\n                    // enhanced workflow: If file doesn't exist yet print full diff\n                    var expect_file_for_diff = \"/dev/null\";\n                }\n                else\n                {\n                    expect_file_for_diff = expect_file;\n                }\n\n                const result = spawnSync(\"git\",\n                    [].concat(\n                        \"diff\",\n                        GIT_DIFF_FLAGS,\n                        expect_file_for_diff,\n                        actual_file\n                    ),\n                    { encoding: \"utf8\" });\n\n                if(result.status)\n                {\n                    console.log(result.stdout);\n                    console.log(result.stderr);\n\n                    if(process.argv.includes(\"--accept-all\"))\n                    {\n                        console.log(`Running: cp ${actual_file} ${expect_file}`);\n                        fs.copyFileSync(actual_file, expect_file);\n                    }\n                    else\n                    {\n                        const failure_message = `${name}.asm failed:\nThe code generator produced different code. If you believe this change is intentional,\nverify the diff above and run the following command to accept the change:\n\n    cp ${actual_file} ${expect_file}\n\nWhen done, re-run this test to confirm that all expect-tests pass.\n\nHint: Use tests/expect/run.js --accept-all to accept all changes (use git diff to verify).\n`;\n\n                        console.log(failure_message);\n\n                        process.exit(1);\n                    }\n                }\n                else\n                {\n                    console.log(\"%s ok\", name);\n                    assert(!result.stdout);\n                    assert(!result.stderr);\n                }\n\n                onfinished();\n            };\n\n            if(is_32)\n            {\n                cpu.is_32[0] = true;\n                cpu.stack_size_32[0] = true;\n            }\n\n            const START_ADDRESS = 0x1000;\n\n            cpu.mem8.set(executable, START_ADDRESS);\n            cpu.update_state_flags();\n            cpu.jit_force_generate(START_ADDRESS);\n        });\n}\n\nfunction disassemble_wasm(wasm)\n{\n    // Need to make a small copy otherwise libwabt goes nuts trying to copy\n    // the whole underlying buffer\n    wasm = wasm.slice();\n\n    try\n    {\n        var module = libwabt.readWasm(wasm, { readDebugNames: false });\n        module.generateNames();\n        module.applyNames();\n        return module.toText({ foldExprs: true, inlineExport: true });\n    }\n    catch(e)\n    {\n        console.error(\"Error while running libwabt: \" + e.toString());\n        console.error(\"Did you forget an ending hlt instruction?\\n\");\n        throw e;\n    }\n    finally\n    {\n        module && module.destroy();\n    }\n}\n\nrun_all();\n"
  },
  {
    "path": "tests/expect/tests/Makefile",
    "content": "build_dir := build\nsource_files := $(wildcard *.asm)\nexecutable_files := $(patsubst %.asm,$(build_dir)/%.bin,$(source_files))\n\n.PHONY: all\nall: $(build_dir) $(executable_files)\n\n$(build_dir):\n\tmkdir -p $@\n\n$(build_dir)/%.bin: %.asm\n\tnasm $< -o $@\n\n.PHONY: clean\nclean:\n\trm $(build_dir)/*\n"
  },
  {
    "path": "tests/expect/tests/add.asm",
    "content": "BITS 32\n    add ebx, eax\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/add.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.store\n              (i32.const 104)\n              (get_local $l3))\n            (set_local $l3\n              (i32.add\n                (get_local $l3)\n                (get_local $l0)))\n            (i32.store\n              (i32.const 112)\n              (get_local $l3))\n            (i64.store\n              (i32.const 96)\n              (i64.const 9710921056287))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 2)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 3)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/call-ret.asm",
    "content": "BITS 32\n    call test\n    hlt\n\ntest:\n    inc eax\n    ret\n"
  },
  {
    "path": "tests/expect/tests/call-ret.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"safe_write32_slow_jit\" (func $e.safe_write32_slow_jit (type $t16)))\n  (import \"e\" \"safe_read32s_slow_jit\" (func $e.safe_read32s_slow_jit (type $t7)))\n  (import \"e\" \"jit_find_cache_entry_in_page\" (func $e.jit_find_cache_entry_in_page (type $t16)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32) (local $l11 i32) (local $l12 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n              (block $B5\n                (br_if $B4\n                  (i32.eq\n                    (get_local $p0)\n                    (i32.const 0))))\n              (set_local $l8\n                (i32.add\n                  (get_local $l8)\n                  (i32.const 1)))\n              (i32.store\n                (i32.const 560)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 5)))\n              (i32.store\n                (i32.const 556)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 6)))\n              (i32.store\n                (i32.const 64)\n                (get_local $l0))\n              (i32.store\n                (i32.const 68)\n                (get_local $l1))\n              (i32.store\n                (i32.const 72)\n                (get_local $l2))\n              (i32.store\n                (i32.const 76)\n                (get_local $l3))\n              (i32.store\n                (i32.const 80)\n                (get_local $l4))\n              (i32.store\n                (i32.const 84)\n                (get_local $l5))\n              (i32.store\n                (i32.const 88)\n                (get_local $l6))\n              (i32.store\n                (i32.const 92)\n                (get_local $l7))\n              (call $e.instr_F4)\n              (set_local $l0\n                (i32.load\n                  (i32.const 64)))\n              (set_local $l1\n                (i32.load\n                  (i32.const 68)))\n              (set_local $l2\n                (i32.load\n                  (i32.const 72)))\n              (set_local $l3\n                (i32.load\n                  (i32.const 76)))\n              (set_local $l4\n                (i32.load\n                  (i32.const 80)))\n              (set_local $l5\n                (i32.load\n                  (i32.const 84)))\n              (set_local $l6\n                (i32.load\n                  (i32.const 88)))\n              (set_local $l7\n                (i32.load\n                  (i32.const 92)))\n              (br $B0))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 1)))\n            (set_local $l9\n              (i32.sub\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 5))\n                (i32.load\n                  (i32.const 740))))\n            (set_local $l11\n              (i32.add\n                (tee_local $l10\n                  (i32.sub\n                    (get_local $l4)\n                    (i32.const 4)))\n                (i32.load\n                  (i32.const 744))))\n            (block $B6\n              (br_if $B6\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l12\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l11)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4075))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l11)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l12\n                    (call $e.safe_write32_slow_jit\n                      (get_local $l11)\n                      (get_local $l9)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (i32.store align=1\n              (i32.xor\n                (i32.and\n                  (get_local $l12)\n                  (i32.const -4096))\n                (get_local $l11))\n              (get_local $l9))\n            (set_local $l4\n              (get_local $l10))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.store\n              (i32.const 120)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 120))\n                  (i32.const -2))\n                (if $I7 (result i32)\n                  (i32.and\n                    (tee_local $l9\n                      (i32.load\n                        (i32.const 100)))\n                    (i32.const 1))\n                  (then\n                    (set_local $l9\n                      (i32.shr_s\n                        (get_local $l9)\n                        (i32.const 31)))\n                    (i32.lt_u\n                      (i32.xor\n                        (i32.load\n                          (i32.const 112))\n                        (get_local $l9))\n                      (i32.xor\n                        (i32.load\n                          (i32.const 104))\n                        (get_local $l9))))\n                  (else\n                    (i32.and\n                      (i32.load\n                        (i32.const 120))\n                      (i32.const 1))))))\n            (i32.store\n              (i32.const 104)\n              (get_local $l0))\n            (set_local $l0\n              (i32.add\n                (get_local $l0)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 112)\n              (get_local $l0))\n            (i64.store\n              (i32.const 96)\n              (i64.const 9706626088991))\n            (i32.const 0)\n            (set_local $l9\n              (i32.add\n                (get_local $l4)\n                (i32.load\n                  (i32.const 744))))\n            (block $B8\n              (br_if $B8\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l10\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l9)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4041))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l9)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_read32s_slow_jit\n                      (get_local $l9)\n                      (i32.const 7)))\n                  (i32.const 1))))\n            (i32.load align=1\n              (i32.xor\n                (i32.and\n                  (get_local $l10)\n                  (i32.const -4096))\n                (get_local $l9)))\n            (set_local $l4\n              (i32.add\n                (get_local $l4)\n                (i32.const 4)))\n            (i32.load\n              (i32.const 740))\n            (i32.add)\n            (i32.store offset=556)\n            (br_if $L2\n              (i32.ge_s\n                (tee_local $p0\n                  (call $e.jit_find_cache_entry_in_page\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const 899)\n                    (i32.const 3)))\n                (i32.const 0)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/do-while.asm",
    "content": "BITS 32\n\nstart:\n    inc ebx\n    cmp eax, 10\n    jnz start\n\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/do-while.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (block $B5\n              (loop $L6\n                (i32.store\n                  (i32.const 556)\n                  (i32.or\n                    (i32.and\n                      (i32.load\n                        (i32.const 556))\n                      (i32.const -4096))\n                    (i32.const 0)))\n                (br_if $B0\n                  (i32.ge_u\n                    (get_local $l8)\n                    (i32.const 100003)))\n                (set_local $l8\n                  (i32.add\n                    (get_local $l8)\n                    (i32.const 3)))\n                (i32.store\n                  (i32.const 120)\n                  (i32.or\n                    (i32.and\n                      (i32.load\n                        (i32.const 120))\n                      (i32.const -2))\n                    (if $I7 (result i32)\n                      (i32.and\n                        (tee_local $l9\n                          (i32.load\n                            (i32.const 100)))\n                        (i32.const 1))\n                      (then\n                        (set_local $l9\n                          (i32.shr_s\n                            (get_local $l9)\n                            (i32.const 31)))\n                        (i32.lt_u\n                          (i32.xor\n                            (i32.load\n                              (i32.const 112))\n                            (get_local $l9))\n                          (i32.xor\n                            (i32.load\n                              (i32.const 104))\n                            (get_local $l9))))\n                      (else\n                        (i32.and\n                          (i32.load\n                            (i32.const 120))\n                          (i32.const 1))))))\n                (i32.store\n                  (i32.const 104)\n                  (get_local $l3))\n                (set_local $l3\n                  (i32.add\n                    (get_local $l3)\n                    (i32.const 1)))\n                (i32.store\n                  (i32.const 112)\n                  (get_local $l3))\n                (i64.store\n                  (i32.const 96)\n                  (i64.const 9706626088991))\n                (i32.store\n                  (i32.const 112)\n                  (i32.sub\n                    (get_local $l0)\n                    (i32.const 10)))\n                (i32.store\n                  (i32.const 104)\n                  (get_local $l0))\n                (i64.store\n                  (i32.const 96)\n                  (i64.const -9223362325933719521))\n                (br_if $L6\n                  (i32.ne\n                    (get_local $l0)\n                    (i32.const 10)))))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 6)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 7)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/if.asm",
    "content": "BITS 32\n    cmp eax, 5\n    jg else\n    inc ecx\n\nelse:\n    inc ebx\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/if.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (block $B5\n              (set_local $l8\n                (i32.add\n                  (get_local $l8)\n                  (i32.const 2)))\n              (i32.store\n                (i32.const 112)\n                (i32.sub\n                  (get_local $l0)\n                  (i32.const 5)))\n              (i32.store\n                (i32.const 104)\n                (get_local $l0))\n              (i64.store\n                (i32.const 96)\n                (i64.const -9223362325933719521))\n              (br_if $B5\n                (i32.gt_s\n                  (get_local $l0)\n                  (i32.const 5)))\n              (set_local $l8\n                (i32.add\n                  (get_local $l8)\n                  (i32.const 1)))\n              (i32.store\n                (i32.const 120)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 120))\n                    (i32.const -2))\n                  (if $I6 (result i32)\n                    (i32.and\n                      (tee_local $l9\n                        (i32.load\n                          (i32.const 100)))\n                      (i32.const 1))\n                    (then\n                      (set_local $l9\n                        (i32.shr_s\n                          (get_local $l9)\n                          (i32.const 31)))\n                      (i32.lt_u\n                        (i32.xor\n                          (i32.load\n                            (i32.const 112))\n                          (get_local $l9))\n                        (i32.xor\n                          (i32.load\n                            (i32.const 104))\n                          (get_local $l9))))\n                    (else\n                      (i32.and\n                        (i32.load\n                          (i32.const 120))\n                        (i32.const 1))))))\n              (i32.store\n                (i32.const 104)\n                (get_local $l1))\n              (set_local $l1\n                (i32.add\n                  (get_local $l1)\n                  (i32.const 1)))\n              (i32.store\n                (i32.const 112)\n                (get_local $l1))\n              (i64.store\n                (i32.const 96)\n                (i64.const 9706626088991)))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.store\n              (i32.const 120)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 120))\n                  (i32.const -2))\n                (if $I7 (result i32)\n                  (i32.and\n                    (tee_local $l9\n                      (i32.load\n                        (i32.const 100)))\n                    (i32.const 1))\n                  (then\n                    (set_local $l9\n                      (i32.shr_s\n                        (get_local $l9)\n                        (i32.const 31)))\n                    (i32.lt_u\n                      (i32.xor\n                        (i32.load\n                          (i32.const 112))\n                        (get_local $l9))\n                      (i32.xor\n                        (i32.load\n                          (i32.const 104))\n                        (get_local $l9))))\n                  (else\n                    (i32.and\n                      (i32.load\n                        (i32.const 120))\n                      (i32.const 1))))))\n            (i32.store\n              (i32.const 104)\n              (get_local $l3))\n            (set_local $l3\n              (i32.add\n                (get_local $l3)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 112)\n              (get_local $l3))\n            (i64.store\n              (i32.const 96)\n              (i64.const 9706626088991))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 7)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 8)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/inc.asm",
    "content": "BITS 32\n    inc eax\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/inc.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.store\n              (i32.const 120)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 120))\n                  (i32.const -2))\n                (if $I5 (result i32)\n                  (i32.and\n                    (tee_local $l9\n                      (i32.load\n                        (i32.const 100)))\n                    (i32.const 1))\n                  (then\n                    (set_local $l9\n                      (i32.shr_s\n                        (get_local $l9)\n                        (i32.const 31)))\n                    (i32.lt_u\n                      (i32.xor\n                        (i32.load\n                          (i32.const 112))\n                        (get_local $l9))\n                      (i32.xor\n                        (i32.load\n                          (i32.const 104))\n                        (get_local $l9))))\n                  (else\n                    (i32.and\n                      (i32.load\n                        (i32.const 120))\n                      (i32.const 1))))))\n            (i32.store\n              (i32.const 104)\n              (get_local $l0))\n            (set_local $l0\n              (i32.add\n                (get_local $l0)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 112)\n              (get_local $l0))\n            (i64.store\n              (i32.const 96)\n              (i64.const 9706626088991))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 1)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 2)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/indirect-call.asm",
    "content": "BITS 32\n    call [eax]\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/indirect-call.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_gp_jit\" (func $e.trigger_gp_jit (type $t2)))\n  (import \"e\" \"safe_read32s_slow_jit\" (func $e.safe_read32s_slow_jit (type $t7)))\n  (import \"e\" \"safe_write32_slow_jit\" (func $e.safe_write32_slow_jit (type $t16)))\n  (import \"e\" \"jit_find_cache_entry_in_page\" (func $e.jit_find_cache_entry_in_page (type $t16)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32) (local $l11 i32) (local $l12 i32) (local $l13 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n              (block $B5\n                (br_if $B4\n                  (i32.eq\n                    (get_local $p0)\n                    (i32.const 0))))\n              (set_local $l8\n                (i32.add\n                  (get_local $l8)\n                  (i32.const 1)))\n              (i32.store\n                (i32.const 560)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 2)))\n              (i32.store\n                (i32.const 556)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 3)))\n              (i32.store\n                (i32.const 64)\n                (get_local $l0))\n              (i32.store\n                (i32.const 68)\n                (get_local $l1))\n              (i32.store\n                (i32.const 72)\n                (get_local $l2))\n              (i32.store\n                (i32.const 76)\n                (get_local $l3))\n              (i32.store\n                (i32.const 80)\n                (get_local $l4))\n              (i32.store\n                (i32.const 84)\n                (get_local $l5))\n              (i32.store\n                (i32.const 88)\n                (get_local $l6))\n              (i32.store\n                (i32.const 92)\n                (get_local $l7))\n              (call $e.instr_F4)\n              (set_local $l0\n                (i32.load\n                  (i32.const 64)))\n              (set_local $l1\n                (i32.load\n                  (i32.const 68)))\n              (set_local $l2\n                (i32.load\n                  (i32.const 72)))\n              (set_local $l3\n                (i32.load\n                  (i32.const 76)))\n              (set_local $l4\n                (i32.load\n                  (i32.const 80)))\n              (set_local $l5\n                (i32.load\n                  (i32.const 84)))\n              (set_local $l6\n                (i32.load\n                  (i32.const 88)))\n              (set_local $l7\n                (i32.load\n                  (i32.const 92)))\n              (br $B0))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 1)))\n            (get_local $l0)\n            (if $I6\n              (i32.load8_u\n                (i32.const 727))\n              (then\n                (call $e.trigger_gp_jit\n                  (i32.const 0)\n                  (i32.const 0))\n                (br $B1)))\n            (i32.load\n              (i32.const 748))\n            (i32.add)\n            (set_local $l9)\n            (block $B7\n              (br_if $B7\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l10\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l9)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4041))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l9)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_read32s_slow_jit\n                      (get_local $l9)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (set_local $l9\n              (i32.add\n                (i32.load align=1\n                  (i32.xor\n                    (i32.and\n                      (get_local $l10)\n                      (i32.const -4096))\n                    (get_local $l9)))\n                (i32.load\n                  (i32.const 740))))\n            (set_local $l10\n              (i32.sub\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 2))\n                (i32.load\n                  (i32.const 740))))\n            (set_local $l12\n              (i32.add\n                (tee_local $l11\n                  (i32.sub\n                    (get_local $l4)\n                    (i32.const 4)))\n                (i32.load\n                  (i32.const 744))))\n            (block $B8\n              (br_if $B8\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l13\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l12)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4075))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l12)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l13\n                    (call $e.safe_write32_slow_jit\n                      (get_local $l12)\n                      (get_local $l10)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (i32.store align=1\n              (i32.xor\n                (i32.and\n                  (get_local $l13)\n                  (i32.const -4096))\n                (get_local $l12))\n              (get_local $l10))\n            (set_local $l4\n              (get_local $l11))\n            (i32.store offset=556\n              (i32.const 0)\n              (get_local $l9))\n            (br_if $L2\n              (i32.ge_s\n                (tee_local $p0\n                  (call $e.jit_find_cache_entry_in_page\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const 899)\n                    (i32.const 3)))\n                (i32.const 0)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/loop.asm",
    "content": "BITS 32\n\nstart:\n    loop start\n    hlt\n\n"
  },
  {
    "path": "tests/expect/tests/loop.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (block $B5\n              (loop $L6\n                (i32.store\n                  (i32.const 556)\n                  (i32.or\n                    (i32.and\n                      (i32.load\n                        (i32.const 556))\n                      (i32.const -4096))\n                    (i32.const 0)))\n                (br_if $B0\n                  (i32.ge_u\n                    (get_local $l8)\n                    (i32.const 100003)))\n                (set_local $l8\n                  (i32.add\n                    (get_local $l8)\n                    (i32.const 1)))\n                (set_local $l1\n                  (i32.sub\n                    (get_local $l1)\n                    (i32.const 1)))\n                (br_if $L6\n                  (get_local $l1))))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 2)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 3)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/mem32r.asm",
    "content": "BITS 32\n    mov eax, [ebx + 123456789]\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/mem32r.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"trigger_gp_jit\" (func $e.trigger_gp_jit (type $t2)))\n  (import \"e\" \"safe_read32s_slow_jit\" (func $e.safe_read32s_slow_jit (type $t7)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.add\n              (get_local $l3)\n              (i32.const 123456789))\n            (if $I5\n              (i32.load8_u\n                (i32.const 727))\n              (then\n                (call $e.trigger_gp_jit\n                  (i32.const 0)\n                  (i32.const 0))\n                (br $B1)))\n            (i32.load\n              (i32.const 748))\n            (i32.add)\n            (set_local $l9)\n            (block $B6\n              (br_if $B6\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l10\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l9)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4041))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l9)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_read32s_slow_jit\n                      (get_local $l9)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (set_local $l0\n              (i32.load align=1\n                (i32.xor\n                  (i32.and\n                    (get_local $l10)\n                    (i32.const -4096))\n                  (get_local $l9))))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 6)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 7)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/mem32rmw.asm",
    "content": "BITS 32\n    inc dword [eax + 123456789]\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/mem32rmw.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"trigger_gp_jit\" (func $e.trigger_gp_jit (type $t2)))\n  (import \"e\" \"safe_read_write32s_slow_jit\" (func $e.safe_read_write32s_slow_jit (type $t7)))\n  (import \"e\" \"safe_write32_slow_jit\" (func $e.safe_write32_slow_jit (type $t16)))\n  (import \"e\" \"bug_gen_safe_read_write_page_fault\" (func $e.bug_gen_safe_read_write_page_fault (type $t2)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32) (local $l11 i32) (local $l12 i32) (local $l13 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.add\n              (get_local $l0)\n              (i32.const 123456789))\n            (if $I5\n              (i32.load8_u\n                (i32.const 727))\n              (then\n                (call $e.trigger_gp_jit\n                  (i32.const 0)\n                  (i32.const 0))\n                (br $B1)))\n            (i32.load\n              (i32.const 748))\n            (i32.add)\n            (set_local $l9)\n            (block $B6\n              (br_if $B6\n                (tee_local $l11\n                  (i32.and\n                    (i32.eq\n                      (i32.and\n                        (tee_local $l10\n                          (i32.load offset={normalised output}\n                            (i32.shl\n                              (i32.shr_u\n                                (get_local $l9)\n                                (i32.const 12))\n                              (i32.const 2))))\n                        (i32.const 4075))\n                      (i32.const 1))\n                    (i32.le_s\n                      (i32.and\n                        (get_local $l9)\n                        (i32.const 4095))\n                      (i32.const 4092)))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_read_write32s_slow_jit\n                      (get_local $l9)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (set_local $l12\n              (i32.load align=1\n                (tee_local $l10\n                  (i32.xor\n                    (i32.and\n                      (get_local $l10)\n                      (i32.const -4096))\n                    (get_local $l9)))))\n            (i32.store\n              (i32.const 120)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 120))\n                  (i32.const -2))\n                (if $I7 (result i32)\n                  (i32.and\n                    (tee_local $l13\n                      (i32.load\n                        (i32.const 100)))\n                    (i32.const 1))\n                  (then\n                    (set_local $l13\n                      (i32.shr_s\n                        (get_local $l13)\n                        (i32.const 31)))\n                    (i32.lt_u\n                      (i32.xor\n                        (i32.load\n                          (i32.const 112))\n                        (get_local $l13))\n                      (i32.xor\n                        (i32.load\n                          (i32.const 104))\n                        (get_local $l13))))\n                  (else\n                    (i32.and\n                      (i32.load\n                        (i32.const 120))\n                      (i32.const 1))))))\n            (i32.store\n              (i32.const 104)\n              (get_local $l12))\n            (set_local $l12\n              (i32.add\n                (get_local $l12)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 112)\n              (get_local $l12))\n            (i64.store\n              (i32.const 96)\n              (i64.const 9706626088991))\n            (set_local $l12\n              (get_local $l12))\n            (if $I8\n              (i32.eqz\n                (get_local $l11))\n              (then\n                (if $I9\n                  (i32.and\n                    (call $e.safe_write32_slow_jit\n                      (get_local $l9)\n                      (get_local $l12)\n                      (i32.const 0))\n                    (i32.const 1))\n                  (then\n                    (call $e.bug_gen_safe_read_write_page_fault\n                      (i32.const 32)\n                      (get_local $l9))))))\n            (i32.store align=1\n              (get_local $l10)\n              (get_local $l12))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 6)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 7)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/mem32w.asm",
    "content": "BITS 32\n    mov [ebx + 123456789], eax\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/mem32w.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"trigger_gp_jit\" (func $e.trigger_gp_jit (type $t2)))\n  (import \"e\" \"safe_write32_slow_jit\" (func $e.safe_write32_slow_jit (type $t16)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.add\n              (get_local $l3)\n              (i32.const 123456789))\n            (if $I5\n              (i32.load8_u\n                (i32.const 727))\n              (then\n                (call $e.trigger_gp_jit\n                  (i32.const 0)\n                  (i32.const 0))\n                (br $B1)))\n            (i32.load\n              (i32.const 748))\n            (i32.add)\n            (set_local $l9)\n            (block $B6\n              (br_if $B6\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l10\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l9)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4075))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l9)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_write32_slow_jit\n                      (get_local $l9)\n                      (get_local $l0)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (i32.store align=1\n              (i32.xor\n                (i32.and\n                  (get_local $l10)\n                  (i32.const -4096))\n                (get_local $l9))\n              (get_local $l0))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 6)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 7)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/mov-immoffs.asm",
    "content": "BITS 32\n    mov eax, [0xcafe]\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/mov-immoffs.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"trigger_gp_jit\" (func $e.trigger_gp_jit (type $t2)))\n  (import \"e\" \"safe_read32s_slow_jit\" (func $e.safe_read32s_slow_jit (type $t7)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (i32.const 51966)\n            (if $I5\n              (i32.load8_u\n                (i32.const 727))\n              (then\n                (call $e.trigger_gp_jit\n                  (i32.const 0)\n                  (i32.const 0))\n                (br $B1)))\n            (i32.load\n              (i32.const 748))\n            (i32.add)\n            (set_local $l9)\n            (block $B6\n              (br_if $B6\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l10\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l9)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4041))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l9)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_read32s_slow_jit\n                      (get_local $l9)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (set_local $l0\n              (i32.load align=1\n                (i32.xor\n                  (i32.and\n                    (get_local $l10)\n                    (i32.const -4096))\n                  (get_local $l9))))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 5)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 6)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/pop.asm",
    "content": "BITS 32\n    pop eax\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/pop.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"safe_read32s_slow_jit\" (func $e.safe_read32s_slow_jit (type $t7)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (set_local $l9\n              (i32.add\n                (get_local $l4)\n                (i32.load\n                  (i32.const 744))))\n            (block $B5\n              (br_if $B5\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l10\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l9)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4041))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l9)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l10\n                    (call $e.safe_read32s_slow_jit\n                      (get_local $l9)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (i32.load align=1\n              (i32.xor\n                (i32.and\n                  (get_local $l10)\n                  (i32.const -4096))\n                (get_local $l9)))\n            (set_local $l4\n              (i32.add\n                (get_local $l4)\n                (i32.const 4)))\n            (set_local $l0)\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 1)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 2)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/push.asm",
    "content": "BITS 32\n    push eax\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/push.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"safe_write32_slow_jit\" (func $e.safe_write32_slow_jit (type $t16)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32) (local $l9 i32) (local $l10 i32) (local $l11 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (set_local $l10\n              (i32.add\n                (tee_local $l9\n                  (i32.sub\n                    (get_local $l4)\n                    (i32.const 4)))\n                (i32.load\n                  (i32.const 744))))\n            (block $B5\n              (br_if $B5\n                (i32.and\n                  (i32.eq\n                    (i32.and\n                      (tee_local $l11\n                        (i32.load offset={normalised output}\n                          (i32.shl\n                            (i32.shr_u\n                              (get_local $l10)\n                              (i32.const 12))\n                            (i32.const 2))))\n                      (i32.const 4075))\n                    (i32.const 1))\n                  (i32.le_s\n                    (i32.and\n                      (get_local $l10)\n                      (i32.const 4095))\n                    (i32.const 4092))))\n              (br_if $B1\n                (i32.and\n                  (tee_local $l11\n                    (call $e.safe_write32_slow_jit\n                      (get_local $l10)\n                      (get_local $l0)\n                      (i32.const 0)))\n                  (i32.const 1))))\n            (i32.store align=1\n              (i32.xor\n                (i32.and\n                  (get_local $l11)\n                  (i32.const -4096))\n                (get_local $l10))\n              (get_local $l0))\n            (set_local $l4\n              (get_local $l9))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 1)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 2)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/sti.asm",
    "content": "BITS 32\n    sti\n    mov eax, 42424242\n    ; handle_irqs check should be inserted here\n    mov eax, 53535353\n\n    hlt\n\n"
  },
  {
    "path": "tests/expect/tests/sti.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"instr_FB_without_fault\" (func $e.instr_FB_without_fault (type $t4)))\n  (import \"e\" \"trigger_gp_jit\" (func $e.trigger_gp_jit (type $t2)))\n  (import \"e\" \"handle_irqs\" (func $e.handle_irqs (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n              (block $B5\n                (br_if $B4\n                  (i32.eq\n                    (get_local $p0)\n                    (i32.const 0))))\n              (set_local $l8\n                (i32.add\n                  (get_local $l8)\n                  (i32.const 2)))\n              (set_local $l0\n                (i32.const 53535353))\n              (i32.store\n                (i32.const 560)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 11)))\n              (i32.store\n                (i32.const 556)\n                (i32.or\n                  (i32.and\n                    (i32.load\n                      (i32.const 556))\n                    (i32.const -4096))\n                  (i32.const 12)))\n              (i32.store\n                (i32.const 64)\n                (get_local $l0))\n              (i32.store\n                (i32.const 68)\n                (get_local $l1))\n              (i32.store\n                (i32.const 72)\n                (get_local $l2))\n              (i32.store\n                (i32.const 76)\n                (get_local $l3))\n              (i32.store\n                (i32.const 80)\n                (get_local $l4))\n              (i32.store\n                (i32.const 84)\n                (get_local $l5))\n              (i32.store\n                (i32.const 88)\n                (get_local $l6))\n              (i32.store\n                (i32.const 92)\n                (get_local $l7))\n              (call $e.instr_F4)\n              (set_local $l0\n                (i32.load\n                  (i32.const 64)))\n              (set_local $l1\n                (i32.load\n                  (i32.const 68)))\n              (set_local $l2\n                (i32.load\n                  (i32.const 72)))\n              (set_local $l3\n                (i32.load\n                  (i32.const 76)))\n              (set_local $l4\n                (i32.load\n                  (i32.const 80)))\n              (set_local $l5\n                (i32.load\n                  (i32.const 84)))\n              (set_local $l6\n                (i32.load\n                  (i32.const 88)))\n              (set_local $l7\n                (i32.load\n                  (i32.const 92)))\n              (br $B0))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (if $I6\n              (i32.eqz\n                (call $e.instr_FB_without_fault))\n              (then\n                (call $e.trigger_gp_jit\n                  (i32.const 0)\n                  (i32.const 0))\n                (br $B1)))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 1)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 6)))\n            (set_local $l0\n              (i32.const 42424242))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.handle_irqs)\n            (i32.store\n              (i32.const 664)\n              (i32.add\n                (i32.load\n                  (i32.const 664))\n                (get_local $l8)))\n            (return))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/task_switch_test.asm",
    "content": "BITS 32\n    fadd\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/task_switch_test.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"task_switch_test_jit\" (func $e.task_switch_test_jit (type $t1)))\n  (import \"e\" \"fpu_get_sti_jit\" (func $e.fpu_get_sti_jit (type $t2)))\n  (import \"e\" \"fpu_fadd\" (func $e.fpu_fadd (type $t18)))\n  (import \"e\" \"fpu_pop\" (func $e.fpu_pop (type $t0)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (if $I5\n              (i32.and\n                (i32.load8_u\n                  (i32.const 580))\n                (i32.const 12))\n              (then\n                (call $e.task_switch_test_jit\n                  (i32.const 0))\n                (br $B1)))\n            (i32.const 1)\n            (call $e.fpu_get_sti_jit\n              (i32.const 1136)\n              (i32.const 1))\n            (i64.load\n              (i32.const 1136))\n            (i32.load16_u\n              (i32.const 1144))\n            (call $e.fpu_fadd)\n            (call $e.fpu_pop)\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 2)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 3)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/task_switch_test_sse.asm",
    "content": "BITS 32\n    andpd xmm0, xmm1\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/task_switch_test_sse.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"task_switch_test_mmx_jit\" (func $e.task_switch_test_mmx_jit (type $t1)))\n  (import \"e\" \"instr_660F54\" (func $e.instr_660F54 (type $t2)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 2)))\n            (if $I5\n              (i32.and\n                (i32.load8_u\n                  (i32.const 580))\n                (i32.const 12))\n              (then\n                (call $e.task_switch_test_mmx_jit\n                  (i32.const 0))\n                (br $B1)))\n            (i64.store\n              (i32.const 1136)\n              (i64.load\n                (i32.const 848)))\n            (i64.store\n              (i32.const 1144)\n              (i64.load\n                (i32.const 856)))\n            (call $e.instr_660F54\n              (i32.const 1136)\n              (i32.const 0))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 4)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 5)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/expect/tests/while-do.asm",
    "content": "BITS 32\n\nstart:\n    cmp eax, 10\n    jz end\n    add ebx, 1\n    jmp start\n\nend:\n    hlt\n"
  },
  {
    "path": "tests/expect/tests/while-do.wast",
    "content": "(module\n  (type $t0 (func))\n  (type $t1 (func (param i32)))\n  (type $t2 (func (param i32 i32)))\n  (type $t3 (func (param i32 i32 i32)))\n  (type $t4 (func (result i32)))\n  (type $t5 (func (result i64)))\n  (type $t6 (func (param i32) (result i32)))\n  (type $t7 (func (param i32 i32) (result i32)))\n  (type $t8 (func (param i32) (result i64)))\n  (type $t9 (func (param f32) (result i32)))\n  (type $t10 (func (param f64) (result i32)))\n  (type $t11 (func (param i32 i64)))\n  (type $t12 (func (param i64 i32)))\n  (type $t13 (func (param i64 i32) (result i32)))\n  (type $t14 (func (param i64 i32) (result i64)))\n  (type $t15 (func (param f32 i32)))\n  (type $t16 (func (param i32 i32 i32) (result i32)))\n  (type $t17 (func (param i64 i32 i32)))\n  (type $t18 (func (param i32 i64 i32)))\n  (type $t19 (func (param i32 i64 i32) (result i32)))\n  (type $t20 (func (param i32 i64 i64 i32) (result i32)))\n  (import \"e\" \"instr_F4\" (func $e.instr_F4 (type $t0)))\n  (import \"e\" \"trigger_fault_end_jit\" (func $e.trigger_fault_end_jit (type $t0)))\n  (import \"e\" \"m\" (memory {normalised output}))\n  (func $f (export \"f\") (type $t1) (param $p0 i32)\n    (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) (local $l7 i32) (local $l8 i32)\n    (set_local $l0\n      (i32.load\n        (i32.const 64)))\n    (set_local $l1\n      (i32.load\n        (i32.const 68)))\n    (set_local $l2\n      (i32.load\n        (i32.const 72)))\n    (set_local $l3\n      (i32.load\n        (i32.const 76)))\n    (set_local $l4\n      (i32.load\n        (i32.const 80)))\n    (set_local $l5\n      (i32.load\n        (i32.const 84)))\n    (set_local $l6\n      (i32.load\n        (i32.const 88)))\n    (set_local $l7\n      (i32.load\n        (i32.const 92)))\n    (set_local $l8\n      (i32.const 0))\n    (block $B0\n      (block $B1\n        (loop $L2\n          (br_if $B0\n            (i32.ge_u\n              (get_local $l8)\n              (i32.const 100003)))\n          (block $B3\n            (block $B4\n            )\n            (block $B5\n              (loop $L6\n                (i32.store\n                  (i32.const 556)\n                  (i32.or\n                    (i32.and\n                      (i32.load\n                        (i32.const 556))\n                      (i32.const -4096))\n                    (i32.const 0)))\n                (br_if $B0\n                  (i32.ge_u\n                    (get_local $l8)\n                    (i32.const 100003)))\n                (set_local $l8\n                  (i32.add\n                    (get_local $l8)\n                    (i32.const 2)))\n                (i32.store\n                  (i32.const 112)\n                  (i32.sub\n                    (get_local $l0)\n                    (i32.const 10)))\n                (i32.store\n                  (i32.const 104)\n                  (get_local $l0))\n                (i64.store\n                  (i32.const 96)\n                  (i64.const -9223362325933719521))\n                (br_if $B5\n                  (i32.eq\n                    (get_local $l0)\n                    (i32.const 10)))\n                (set_local $l8\n                  (i32.add\n                    (get_local $l8)\n                    (i32.const 2)))\n                (i32.store\n                  (i32.const 104)\n                  (get_local $l3))\n                (set_local $l3\n                  (i32.add\n                    (get_local $l3)\n                    (i32.const 1)))\n                (i32.store\n                  (i32.const 112)\n                  (get_local $l3))\n                (i64.store\n                  (i32.const 96)\n                  (i64.const 9710921056287))\n                (br $L6)))\n            (set_local $l8\n              (i32.add\n                (get_local $l8)\n                (i32.const 1)))\n            (i32.store\n              (i32.const 560)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 10)))\n            (i32.store\n              (i32.const 556)\n              (i32.or\n                (i32.and\n                  (i32.load\n                    (i32.const 556))\n                  (i32.const -4096))\n                (i32.const 11)))\n            (i32.store\n              (i32.const 64)\n              (get_local $l0))\n            (i32.store\n              (i32.const 68)\n              (get_local $l1))\n            (i32.store\n              (i32.const 72)\n              (get_local $l2))\n            (i32.store\n              (i32.const 76)\n              (get_local $l3))\n            (i32.store\n              (i32.const 80)\n              (get_local $l4))\n            (i32.store\n              (i32.const 84)\n              (get_local $l5))\n            (i32.store\n              (i32.const 88)\n              (get_local $l6))\n            (i32.store\n              (i32.const 92)\n              (get_local $l7))\n            (call $e.instr_F4)\n            (set_local $l0\n              (i32.load\n                (i32.const 64)))\n            (set_local $l1\n              (i32.load\n                (i32.const 68)))\n            (set_local $l2\n              (i32.load\n                (i32.const 72)))\n            (set_local $l3\n              (i32.load\n                (i32.const 76)))\n            (set_local $l4\n              (i32.load\n                (i32.const 80)))\n            (set_local $l5\n              (i32.load\n                (i32.const 84)))\n            (set_local $l6\n              (i32.load\n                (i32.const 88)))\n            (set_local $l7\n              (i32.load\n                (i32.const 92)))\n            (br $B0))\n          (unreachable)))\n      (i32.store\n        (i32.const 64)\n        (get_local $l0))\n      (i32.store\n        (i32.const 68)\n        (get_local $l1))\n      (i32.store\n        (i32.const 72)\n        (get_local $l2))\n      (i32.store\n        (i32.const 76)\n        (get_local $l3))\n      (i32.store\n        (i32.const 80)\n        (get_local $l4))\n      (i32.store\n        (i32.const 84)\n        (get_local $l5))\n      (i32.store\n        (i32.const 88)\n        (get_local $l6))\n      (i32.store\n        (i32.const 92)\n        (get_local $l7))\n      (call $e.trigger_fault_end_jit)\n      (i32.store\n        (i32.const 664)\n        (i32.add\n          (i32.load\n            (i32.const 664))\n          (get_local $l8)))\n      (return))\n    (i32.store\n      (i32.const 64)\n      (get_local $l0))\n    (i32.store\n      (i32.const 68)\n      (get_local $l1))\n    (i32.store\n      (i32.const 72)\n      (get_local $l2))\n    (i32.store\n      (i32.const 76)\n      (get_local $l3))\n    (i32.store\n      (i32.const 80)\n      (get_local $l4))\n    (i32.store\n      (i32.const 84)\n      (get_local $l5))\n    (i32.store\n      (i32.const 88)\n      (get_local $l6))\n    (i32.store\n      (i32.const 92)\n      (get_local $l7))\n    (i32.store\n      (i32.const 664)\n      (i32.add\n        (i32.load\n          (i32.const 664))\n        (get_local $l8)))))\n"
  },
  {
    "path": "tests/full/run.js",
    "content": "#!/usr/bin/env node\n\nimport assert from \"node:assert/strict\";\nimport cluster from \"node:cluster\";\nimport os from \"node:os\";\nimport fs from \"node:fs\";\nimport url from \"node:url\";\n\n// config variables\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst TIMEOUT_EXTRA_FACTOR = +process.env.TIMEOUT_EXTRA_FACTOR || 1;\nconst MAX_PARALLEL_TESTS = +process.env.MAX_PARALLEL_TESTS || 4;\nconst TEST_NAME = process.env.TEST_NAME;\nconst RUN_SLOW_TESTS = +process.env.RUN_SLOW_TESTS;\nconst LOG_LEVEL = +process.env.LOG_LEVEL || 0;\nconst DISABLE_JIT = +process.env.DISABLE_JIT;\nconst TEST_ACPI = +process.env.TEST_ACPI;\n\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst VERBOSE = false;\nconst LOG_SCREEN = false;\n\n\nvar root_path = __dirname + \"/../..\";\n\nvar SCREEN_WIDTH = 80;\n\nfunction get_line(screen, y)\n{\n    return screen.subarray(y * SCREEN_WIDTH, (y + 1) * SCREEN_WIDTH);\n}\n\nfunction line_to_text(screen, y)\n{\n    return bytearray_to_string(get_line(screen, y));\n}\n\nfunction string_to_bytearray(str)\n{\n    return new Uint8Array(str.split(\"\").map(chr => chr.charCodeAt(0)));\n}\n\nfunction bytearray_to_string(arr)\n{\n    return String.fromCharCode.apply(String, arr).replace(/[\\x00-\\x08\\x0b-\\x1f\\x7f\\x80-\\xff]/g, \" \");\n}\n\nfunction screen_to_text(s)\n{\n    var result = [];\n    result.push(\"+==================================== SCREEN ====================================+\");\n\n    for(var i = 0; i < 25; i++)\n    {\n        var line = line_to_text(s, i);\n        result.push(\"|\" + line + \"|\");\n    }\n\n    result.push(\"+================================================================================+\");\n\n    return result.join(\"\\n\");\n}\n\nfunction send_work_to_worker(worker, message)\n{\n    if(current_test < tests.length)\n    {\n        worker.send(tests[current_test]);\n        current_test++;\n    }\n    else\n    {\n        worker.disconnect();\n    }\n}\n\nif(cluster.isPrimary)\n{\n    var tests = [\n        {\n            name: \"FreeDOS boot\",\n            fda: root_path + \"/images/freedos722.img\",\n            timeout: 20,\n            expected_texts: [\n                \"Welcome to FreeDOS\",\n            ],\n        },\n        {\n            name: \"FreeDOS boot with Bochs BIOS\",\n            fda: root_path + \"/images/freedos722.img\",\n            timeout: 20,\n            alternative_bios: true,\n            expected_texts: [\n                \"Welcome to FreeDOS\",\n            ],\n        },\n        {\n            name: \"Windows 1.01 boot\",\n            fda: root_path + \"/images/windows101.img\",\n            timeout: 10,\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Sol OS\",\n            fda: root_path + \"/images/os8.img\",\n            timeout: 20,\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            actions: [\n                {\n                    on_text: \" or press\",\n                    run: \"\\n\"\n                },\n            ],\n        },\n        {\n            name: \"Snowdrop\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/images/snowdrop.img\",\n            timeout: 30,\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            actions: [\n                {\n                    on_text: \"[Snowdrop OS snowshell]:\",\n                    run: \"desktop\\n\"\n                },\n            ],\n        },\n        {\n            name: \"Linux\",\n            cdrom: root_path + \"/images/linux.iso\",\n            timeout: 90,\n            expected_texts: [\n                \"/root%\",\n                \"test passed\",\n            ],\n            actions: [\n                {\n                    on_text: \"/root%\",\n                    run: \"cd tests; ./test-i386 > emu.test; diff emu.test reference.test && echo test pas''sed || echo failed\\n\",\n                },\n            ],\n        },\n        {\n            name: \"Windows Vista installer\",\n            skip_if_disk_image_missing: true,\n            cdrom: root_path + \"/images/experimental/en_windows_vista_sp2_x86_dvd_342266.iso\",\n            memory_size: 512 * 1024 * 1024,\n            timeout: 300,\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n            acpi: true,\n        },\n        {\n            name: \"Windows XP CD\",\n            skip_if_disk_image_missing: true,\n            cdrom: root_path + \"/images/experimental/VirtualXP.iso\",\n            memory_size: 512 * 1024 * 1024,\n            timeout: 600,\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n            acpi: false, // XXX: fails with acpi on\n        },\n        {\n            name: \"Windows XP HD\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/experimental/copy_winxp_lite-from-pixelsuft.img\",\n            memory_size: 512 * 1024 * 1024,\n            timeout: 300,\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Windows 2000\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/windows2k.img\",\n            memory_size: 512 * 1024 * 1024,\n            timeout: 300,\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Windows NT 4.0\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/winnt4_noacpi.img\",\n            memory_size: 512 * 1024 * 1024,\n            timeout: 60,\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n            cpuid_level: 2,\n        },\n        {\n            name: \"Windows NT 3.1\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/winnt31.img\",\n            memory_size: 256 * 1024 * 1024,\n            timeout: 60,\n            expect_graphical_mode: true,\n            expect_graphical_size: [640, 480],\n            expect_mouse_registered: true,\n        },\n        //{\n        //    name: \"Windows 98\",\n        //    skip_if_disk_image_missing: true,\n        //    hda: root_path + \"/images/windows98.img\",\n        //    timeout: 60,\n        //    expect_graphical_mode: true,\n        //    expect_graphical_size: [800, 600],\n        //    expect_mouse_registered: true,\n        //    failure_allowed: true,\n        //},\n        {\n            name: \"Windows 95\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/w95.img\",\n            timeout: 120,\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n            failure_allowed: true,\n        },\n        {\n            name: \"Oberon\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/oberon.img\",\n            timeout: 30,\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Linux 3\",\n            skip_if_disk_image_missing: true,\n            cdrom: root_path + \"/images/linux3.iso\",\n            timeout: 200,\n            expected_texts: [\n                \"test passed\",\n            ],\n            actions: [\n                {\n                    on_text: \"~%\",\n                    run: \"head -c 10000 /dev/urandom > rand; echo test pas''sed\\n\",\n                    after: 1000,\n                },\n            ],\n        },\n        {\n            name: \"Linux 3 reboot\",\n            cdrom: root_path + \"/images/linux3.iso\",\n            timeout: 90,\n            expected_texts: [\n                \"~%\",\n                \"SeaBIOS \",\n                \"~%\",\n            ],\n            actions: [\n                {\n                    on_text: \"~%\",\n                    run: \"reboot\\n\",\n                },\n            ],\n        },\n        {\n            name: \"KolibriOS\",\n            fda: root_path + \"/images/kolibri.img\",\n            timeout: 120,\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Linux with Bochs BIOS\",\n            cdrom: root_path + \"/images/linux.iso\",\n            timeout: 90,\n            expected_texts: [\n                \"/root%\",\n                \"test passed\",\n            ],\n            alternative_bios: true,\n            actions: [\n                {\n                    on_text: \"/root%\",\n                    run: \"cd tests; ./test-i386 > emu.test; diff emu.test reference.test && echo test pas''sed || echo failed\\n\",\n                },\n            ],\n        },\n        {\n            name: \"MS-DOS\",\n            hda: root_path + \"/images/msdos.img\",\n            timeout: 90,\n            expected_texts: [\n                \"C:\\\\>\",\n            ],\n        },\n        {\n            name: \"MS-DOS (hard disk + floppy disk)\",\n            hda: root_path + \"/images/msdos.img\",\n            fda: root_path + \"/images/kolibri.img\",\n            boot_order: 0x132,\n            timeout: 90,\n            actions: [\n                { on_text: \"C:\\\\>\", run: \"a:\\n\" },\n            ],\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"Linux 4\",\n            skip_if_disk_image_missing: true,\n            cdrom: root_path + \"/images/linux4.iso\",\n            timeout: 200,\n            expected_texts: [\n                \"~%\",\n            ],\n            expected_serial_text: [\n                \"Files send via emulator appear in\",\n            ],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Linux bzImage\",\n            bzimage: root_path + \"/images/buildroot-bzimage68.bin\",\n            cmdline: \"auto\",\n            timeout: 200,\n            expected_texts: [\n                \"~%\",\n            ],\n            expected_serial_text: [\n                \"Files send via emulator appear in\",\n            ],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Linux with bzImage from filesystem\",\n            bzimage_initrd_from_filesystem: true,\n            filesystem: {\n                basefs: root_path + \"/build/integration-test-fs/fs.json\",\n                baseurl: root_path + \"/build/integration-test-fs/flat/\",\n            },\n            cmdline: \"auto\",\n            timeout: 200,\n            expected_texts: [\n                \"~%\",\n            ],\n            expected_serial_text: [\n                \"Files send via emulator appear in\",\n            ],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"QNX\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/images/qnx-demo-network-4.05.img\",\n            timeout: 300,\n            expect_mouse_registered: true,\n            expect_graphical_mode: true,\n            expect_graphical_size: [640, 480],\n            actions: [\n                { run: \" \", after: 30 * 1000 },\n                { run: \" \", after: 15 * 1000 },\n                { run: \" \", after: 15 * 1000 },\n                { run: \" \", after: 15 * 1000 },\n                { run: \" \", after: 15 * 1000 },\n                { run: \" \", after: 15 * 1000 },\n                { run: \" \", after: 15 * 1000 },\n            ],\n        },\n        {\n            name: \"OpenBSD Floppy\",\n            fda: root_path + \"/images/openbsd-floppy.img\",\n            timeout: 180,\n            expected_texts: [\"(I)nstall, (U)pgrade or (S)hell\"],\n        },\n        {\n            name: \"OpenBSD\",\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/openbsd.img\",\n            timeout: 300,\n            actions: [\n                {\n                    on_text: \"boot>\",\n                    run: \"boot -c\\n\",\n                },\n                {\n                    on_text: \"UKC>\",\n                    run: \"disable mpbios\\nexit\\n\",\n                },\n                {\n                    on_text: \"login:\",\n                    run: \"root\\n\",\n                },\n                {\n                    on_text: \"Password:\",\n                    run: \"root\\n\",\n                },\n            ],\n            expected_texts: [\"nyu# \"],\n        },\n        {\n            name: \"OpenBSD state image\",\n            timeout: 60,\n            memory_size: 256 * 1024 * 1024,\n            skip_if_disk_image_missing: true,\n            hda: root_path + \"/images/openbsd.img\",\n            state: root_path + \"/images/openbsd_state-v2.bin.zst\",\n            actions: [\n                {\n                    after: 1 * 1000,\n                    run: `echo 'main(){printf(\"it\");puts(\" works\");}' > a.c; clang a.c; ./a.out\\n`,\n                }\n            ],\n            expected_texts: [\n                \"it works\",\n            ],\n        },\n        {\n            name: \"Windows 3.0\",\n            slow: 1,\n            skip_if_disk_image_missing: true,\n            timeout: 10 * 60,\n            cdrom: root_path + \"/images/Win30.iso\",\n            expected_texts: [\n                \"Press any key to continue\",\n                \"              **************************************************\",\n            ],\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            actions: [\n                {\n                    on_text: \"Press any key to continue . . .\",\n                    after: 1000,\n                    run: \"x\",\n                },\n                {\n                    on_text: \"              **************************************************\",\n                    after: 1000,\n                    run: \"x\",\n                },\n                {\n                    on_text: \"C> \",\n                    after: 1000,\n                    run: \"win\\n\",\n                },\n            ],\n        },\n        {\n            name: \"Windows 3.1\",\n            skip_if_disk_image_missing: true,\n            timeout: 2 * 60,\n            hda: root_path + \"/images/win31.img\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n            expected_texts: [\n                \"MODE prepare code page function completed\",\n            ],\n        },\n        {\n            name: \"FreeBSD\",\n            skip_if_disk_image_missing: true,\n            timeout: 15 * 60,\n            hda: root_path + \"/images/freebsd.img\",\n            expected_texts: [\n                \"FreeBSD/i386 (nyu) (ttyv0)\",\n                \"root@nyu:~ #\",\n            ],\n            actions: [\n                {\n                    on_text: \"   Autoboot in\",\n                    run: \"\\n\",\n                },\n                {\n                    // workaround for freebsd not accepting key inputs just before the boot prompt\n                    // (probably needs delay between keydown and keyup)\n                    on_text: \"FreeBSD/i386 (nyu) (ttyv0)\",\n                    run: \"\\x08\", // backspace to avoid messing with login prompt\n                },\n                {\n                    on_text: \"login:\",\n                    after: 1000,\n                    run: \"root\\n\",\n                },\n                {\n                    on_text: \"Password:\",\n                    after: 1000,\n                    run: \"\\n\",\n                },\n            ],\n        },\n        {\n            name: \"FreeBSD cdrom\",\n            skip_if_disk_image_missing: true,\n            slow: 1,\n            timeout: 10 * 60,\n            cdrom: root_path + \"/images/experimental/os/FreeBSD-11.0-RELEASE-i386-bootonly.iso\",\n            expected_texts: [\"Welcome to FreeBSD!\"],\n            actions: [\n                {\n                    on_text: \"   Autoboot in \",\n                    run: \"\\n\",\n                }\n            ],\n        },\n        {\n            name: \"Arch Linux\",\n            skip_if_disk_image_missing: true,\n            timeout: 20 * 60,\n            bzimage_initrd_from_filesystem: true,\n            memory_size: 512 * 1024 * 1024,\n            cmdline: [\n                \"rw apm=off vga=0x344 video=vesafb:ypan,vremap:8\",\n                \"root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose mitigations=off\",\n                \"audit=0 init=/usr/bin/init-openrc net.ifnames=0 biosdevname=0\",\n            ].join(\" \"),\n            filesystem: {\n                basefs: \"images/fs.json\",\n                baseurl: \"images/arch/\",\n            },\n            expected_texts: [\n                \"root@localhost\",\n                \"aaaaaaaaaaaaaaaaaaaa\",\n                \"Hello, world\",\n                \"Hello from JS\",\n                \"Hello from OCaml\",\n                \"Compress okay\",\n                \"v86-in-v86 okay\",\n            ],\n            actions: [\n                {\n                    on_text: \"root@localhost\",\n                    run: `python -c 'print(100 * \"a\")'\\n`,\n                },\n                {\n                    on_text: \"aaaaaaaaaaaaaaaaaaaa\",\n                    run: `gcc hello.c && ./a.out\\n`,\n                },\n                {\n                    on_text: \"Hello, world\",\n                    run: `echo 'console.log(\"Hello from JS\")' | node\\n`,\n                },\n                {\n                    on_text: \"Hello from JS\",\n                    run: `echo 'print_endline \"Hello from OCaml\"' > hello.ml && ocamlopt hello.ml && ./a.out\\n`,\n                },\n                {\n                    on_text: \"Hello from OCaml\",\n                    run:\n                        \"zstd hello.c && gzip -k hello.c && bzip2 -k hello.c && xz -k hello.c && lzma -k hello.c && \" +\n                        \"zstdcat hello.c.zst && zcat hello.c.gz && bzcat hello.c.bz2 && xzcat hello.c.xz && lzmadec hello.c.lzma && \" +\n                        \"echo Compress okay\\n\",\n                },\n                {\n                    on_text: \"Compress okay\",\n                    run:\n                        RUN_SLOW_TESTS ?\n                            \"./v86-in-v86.js | tee /dev/stderr | grep -m1 'Files send via emulator appear in' ; sleep 2; echo; echo v86-in-v86 okay\\n\"\n                        :\n                            \"./v86-in-v86.js | tee /dev/stderr | grep -m1 'Kernel command line:' ; sleep 2; echo; echo v86-in-v86 okay\\n\",\n                },\n                {\n                    on_text: \"v86-in-v86 okay\",\n                    run: \"./startx.sh\\n\",\n                },\n            ],\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Arch Linux state image\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            memory_size: 512 * 1024 * 1024,\n            filesystem: {\n                basefs: \"images/fs.json\",\n                baseurl: \"images/arch/\",\n            },\n            state: \"images/arch_state-v3.bin.zst\",\n            net_device: { type: \"virtio\" },\n            actions: [\n                { after: 1000, run: \"ls --color=never /dev/ /usr/bin/ > /dev/ttyS0\\n\" },\n                { after: 2000, run: `python -c 'print(100 * \"a\")' > /dev/ttyS0\\n` },\n            ],\n            expected_serial_text: [\n                \"ttyS0\",\n                \"syslinux-install_update\",\n                \"aaaaaaaaaaaaaaaaaaaa\",\n            ],\n        },\n        {\n            name: \"Arch Linux (with fda, cdrom, hda and hdb)\",\n            skip_if_disk_image_missing: true,\n            timeout: 5 * 60,\n            bzimage_initrd_from_filesystem: true,\n            memory_size: 512 * 1024 * 1024,\n            cmdline: [\n                \"rw apm=off vga=0x344 video=vesafb:ypan,vremap:8\",\n                \"root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose mitigations=off\",\n                \"audit=0 init=/usr/bin/init-openrc net.ifnames=0 biosdevname=0\",\n            ].join(\" \"),\n            filesystem: {\n                basefs: \"images/fs.json\",\n                baseurl: \"images/arch/\",\n            },\n            hda: root_path + \"/images/w95.img\",\n            hdb: root_path + \"/images/FiwixOS-3.4-i386.img\",\n            cdrom: root_path + \"/images/dsl-4.11.rc2.iso\",\n            fda: root_path + \"/images/freedos722.img\",\n            actions: [\n                {\n                    on_text: \"root@localhost\",\n                    run: \"modprobe floppy && mkdir /mnt/{a,b,c,f} && mount /dev/sda1 /mnt/a && mount /dev/sdb2 /mnt/b && mount /dev/sr0 /mnt/c && mount /dev/fd0 /mnt/f && ls /mnt/*\\n\",\n                },\n            ],\n            expected_texts: [\n                \"bin   dev  home\",                          // fiwix\n                \" AUTOEXEC.BAT   CONFIG.WIN   MSDOS.SYS\",   // w95\n                \"KNOPPIX  boot  index.html\",                // DSL\n                \"FDOS          README      debug.com\",      // freedos\n            ],\n        },\n        {\n            name: \"FreeGEM\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            hda: root_path + \"/images/freegem.bin\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            actions: [\n                {\n                    on_text: \"   Select from Menu\",\n                    run: \"3\",\n                }\n            ],\n        },\n        {\n            name: \"Haiku\",\n            skip_if_disk_image_missing: true,\n            timeout: 15 * 60,\n            memory_size: 512 * 1024 * 1024,\n            hda: root_path + \"/images/haiku-r1beta2-hrev54154_111-x86_gcc2h-anyboot.iso\",\n            expected_serial_text: [\n                \"init_hardware()\",\n                \"Running post install script /boot/system/boot/post-install/sshd_keymaker.sh\",\n                // After pressing enter in the boot dialog:\n                \"Running first login script /boot/system/boot/first-login/default_deskbar_items.sh\",\n            ],\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n            actions: [\n                { after: 1 * 60 * 1000, run: \"\\n\" },\n                { after: 2 * 60 * 1000, run: \"\\n\" },\n                { after: 3 * 60 * 1000, run: \"\\n\" },\n                { after: 4 * 60 * 1000, run: \"\\n\" },\n                { after: 5 * 60 * 1000, run: \"\\n\" },\n                { after: 6 * 60 * 1000, run: \"\\n\" },\n                { after: 7 * 60 * 1000, run: \"\\n\" },\n                { after: 8 * 60 * 1000, run: \"\\n\" },\n            ],\n            acpi: true,\n        },\n        {\n            name: \"Haiku state image\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            memory_size: 512 * 1024 * 1024,\n            hda: root_path + \"/images/haiku-v5.img\",\n            state: root_path + \"/images/haiku_state-v5.bin.zst\",\n            actions: [\n                {\n                    after: 2 * 1000,\n                    run: `echo 'let rec f=function 0|1->1|x->f(x-1)+f(x-2)in Printf.printf\"%d\\n\"(f 25)' | ocaml -stdin > /dev/ports/pc_serial0\\n`\n                },\n            ],\n            expected_serial_text: [\n                \"121393\",\n            ],\n            acpi: true,\n        },\n        {\n            name: \"9front\",\n            use_small_bios: true, // has issues with 256k bios\n            skip_if_disk_image_missing: true,\n            acpi: true,\n            timeout: 5 * 60,\n            hda: root_path + \"/images/9front-7781.38dcaeaa222c.386.iso\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n            actions: [\n                { after: 60 * 1000, run: \"\\n\" },\n                { after: 70 * 1000, run: \"\\n\" },\n                { after: 80 * 1000, run: \"\\n\" },\n                { after: 90 * 1000, run: \"\\n\" },\n                { after: 100 * 1000, run: \"\\n\" },\n                { after: 110 * 1000, run: \"\\n\" },\n                { after: 120 * 1000, run: \"\\n\" },\n                { after: 130 * 1000, run: \"\\n\" },\n                { after: 140 * 1000, run: \"\\n\" },\n                { after: 150 * 1000, run: \"\\n\" },\n                { after: 160 * 1000, run: \"\\n\" },\n                { after: 170 * 1000, run: \"\\n\" },\n                { after: 180 * 1000, run: \"\\n\" },\n            ],\n        },\n        {\n            name: \"ReactOS\",\n            skip_if_disk_image_missing: true,\n            timeout: 10 * 60,\n            hda: root_path + \"/images/reactos-livecd-0.4.15-dev-73-g03c09c9-x86-gcc-lin-dbg.iso\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n            actions: [\n                { after: 1 * 60 * 1000, run: \"\\n\" },\n                { after: 2 * 60 * 1000, run: \"\\n\" },\n                { after: 3 * 60 * 1000, run: \"\\n\" },\n                { after: 4 * 60 * 1000, run: \"\\n\" },\n                { after: 5 * 60 * 1000, run: \"\\n\" },\n                { after: 6 * 60 * 1000, run: \"\\n\" },\n                { after: 7 * 60 * 1000, run: \"\\n\" },\n                { after: 8 * 60 * 1000, run: \"\\n\" },\n            ],\n            expected_serial_text: [\n                \"DnsIntCacheInitialize()\",\n                // when desktop is rendered:\n                \"err: Attempted to close thread desktop\",\n            ],\n        },\n        {\n            name: \"ReactOS state image\",\n            skip_if_disk_image_missing: true,\n            memory_size: 512 * 1024 * 1024,\n            acpi: true,\n            net_device: { type: \"virtio\" },\n            timeout: 60,\n            hda: root_path + \"/images/reactos-v3.img\",\n            state: root_path + \"/images/reactos_state-v3.bin.zst\",\n            actions: [\n                { after: 5 * 1000, run: [0xE0, 0x5B, 0x13, 0x93, 0xE0, 0xDB] }, // meta+r\n                { after: 10 * 1000, run: \"cmd\\n\" },\n                { after: 15 * 1000, run: \"echo it works > COM1\\n\" },\n            ],\n            expected_serial_text: [\n                \"it works\",\n            ],\n        },\n        {\n            name: \"ReactOS CD\",\n            skip_if_disk_image_missing: true,\n            timeout: 10 * 60,\n            cdrom: root_path + \"/images/reactos-livecd-0.4.15-dev-73-g03c09c9-x86-gcc-lin-dbg.iso\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n            expected_serial_text: [\"DnsIntCacheInitialize()\"],\n        },\n        {\n            name: \"HelenOS\",\n            skip_if_disk_image_missing: true,\n            timeout: 3 * 60,\n            cdrom: root_path + \"/images/HelenOS-0.11.2-ia32.iso\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            expected_serial_text: [\"init: Spawning\"],\n        },\n        {\n            name: \"Minix\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            hda: root_path + \"/images/experimental/os/minix2hd.img\",\n            actions: [\n                {\n                    on_text: \"    =  Start Minix\",\n                    run: \"=\",\n                },\n                {\n                    on_text: \"noname login:\",\n                    run: \"root\\n\",\n                },\n            ],\n            expected_texts: [\"noname login:\", \"# \"],\n        },\n        {\n            name: \"Minix CD\",\n            skip_if_disk_image_missing: true,\n            timeout: 3 * 60,\n            cdrom: root_path + \"/images/minix-3.3.0.iso\",\n            actions: [\n                {\n                    on_text: \"login:\",\n                    run: \"root\\n\",\n                },\n            ],\n            expected_texts: [\"login:\", \"We'd like your feedback\", \"# \"],\n        },\n        {\n            name: \"Mojo OS\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            cdrom: root_path + \"/images/mojo-0.2.2.iso\",\n            actions: [\n                {\n                    on_text: \"/> \",\n                    run: \"help\\n\",\n                },\n            ],\n            expected_texts: [\"Mojo test shell\", \"See manual pages for more information\"],\n            expected_serial_text: [\" ===> Shell loaded\"],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Vanadium OS\",\n            skip_if_disk_image_missing: true,\n            timeout: 180,\n            cdrom: root_path + \"/images/vanadiumos.iso\",\n            actions: [\n                { after: 5000, run: \" \" },\n                { after: 5100, run: \" \" },\n                { after: 5200, run: \" \" },\n                { after: 5300, run: \" \" },\n                { after: 5400, run: \" \" },\n                { after: 5500, run: \" \" },\n                { after: 5600, run: \" \" },\n                { after: 5700, run: \" \" },\n                { after: 5800, run: \"c\" },\n            ],\n            expect_mouse_registered: true,\n            expect_graphical_mode: true,\n        },\n        {\n            name: \"Asuro\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            cdrom: root_path + \"/images/asuro.iso\",\n            expect_mouse_registered: true,\n            expect_graphical_mode: true,\n            expected_serial_text: [\"Asuro Booted Correctly!\"],\n        },\n        {\n            name: \"Mobius\",\n            skip_if_disk_image_missing: true,\n            timeout: 2 * 60,\n            fda: root_path + \"/images/mobius-fd-release5.img\",\n            expect_graphical_mode: true,\n            actions: [\n                {\n                    on_text: \"   The highlighted entry will be booted automatically\",\n                    run: \"\\n\",\n                },\n            ],\n            acpi: false, // segfaults with acpi on (also in other emulators)\n        },\n        {\n            name: \"FreeNOS\",\n            skip_if_disk_image_missing: true,\n            timeout: 2 * 60,\n            cdrom: root_path + \"/images/FreeNOS-1.0.3.iso\",\n            acpi: true,\n            actions: [\n                {\n                    on_text: \"login:\",\n                    run: \"root\\n\",\n                },\n            ],\n            expected_texts: [\"login:\", \"(localhost)\"],\n            expected_serial_text: [\"FreeNOS 1.0.3\"],\n        },\n        {\n            name: \"SerenityOS\",\n            skip_if_disk_image_missing: true,\n            timeout: 2 * 60,\n            hda: root_path + \"/images/serenity.img\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Redox\",\n            skip_if_disk_image_missing: true,\n            timeout: 5 * 60,\n            memory_size: 512 * 1024 * 1024,\n            acpi: true,\n            hda: root_path + \"/images/redox_demo_i686_2022-11-26_643_harddrive.img\",\n            actions: [\n                { on_text: \"Arrow keys and enter select mode\", run: \"\\n\" },\n            ],\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            expected_serial_text: [\"# Login with the following:\"],\n        },\n        {\n            name: \"Android 1.6\",\n            skip_if_disk_image_missing: true,\n            timeout: 2 * 60,\n            cdrom: root_path + \"/images/android-x86-1.6-r2.iso\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Android 4.4\",\n            skip_if_disk_image_missing: true,\n            timeout: 5 * 60,\n            hda: root_path + \"/images/android_x86_nonsse3_4.4r1_20140904.iso\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [800, 600],\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Syllable\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            memory_size: 512 * 1024 * 1024,\n            hda: root_path + \"/images/syllable-destop-0.6.7.img\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"Mu\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            memory_size: 256 * 1024 * 1024,\n            hda: root_path + \"/images/mu-shell.img\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n        },\n        {\n            name: \"FreeDOS boot floppy 160K\", // source: https://github.com/codercowboy/freedosbootdisks/tree/master/bootdisks\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/images/experimental/freedos-fds/freedos.boot.disk.160K.img\",\n            timeout: 10,\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"FreeDOS boot floppy 180K\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/image/experimentals/freedos-fds/freedos.boot.disk.180K.img\",\n            timeout: 10,\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"FreeDOS boot floppy 320K\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/image/experimentals/freedos-fds/freedos.boot.disk.320K.img\",\n            timeout: 10,\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"FreeDOS boot floppy 360K\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/image/experimentals/freedos-fds/freedos.boot.disk.360K.img\",\n            timeout: 10,\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"FreeDOS boot floppy 640K\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/image/experimentals/freedos-fds/freedos.boot.disk.640K.img\",\n            timeout: 10,\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"FreeDOS boot floppy 1200K\",\n            skip_if_disk_image_missing: true,\n            fda: root_path + \"/image/experimentals/freedos-fds/freedos.boot.disk.1200K.img\",\n            timeout: 10,\n            expected_texts: [\n                \"A:\\\\>\",\n            ],\n        },\n        {\n            name: \"ASM Space Invaders\",\n            skip_if_disk_image_missing: true,\n            timeout: 10,\n            fda: root_path + \"/images/space-invaders.img\", // non-standard floppy disk size, reads past end of original image\n            expected_texts: [\n                \"                             #   SPACE INVADERS   # \",\n            ],\n        },\n        {\n            name: \"NetBSD multiboot\",\n            skip_if_disk_image_missing: true,\n            timeout: 15,\n            memory_size: 256 * 1024 * 1024,\n            multiboot: root_path + \"/images/netbsd9.3-kernel-multiboot.img\",\n            expected_texts: [\n                // NOTE: doesn't success booting yet, just testing the multiboot boot\n                \"[   1.0000000] multiboot:\",\n            ],\n        },\n        {\n            name: \"Crazierl\",\n            skip_if_disk_image_missing: true,\n            timeout: 60,\n            memory_size: 256 * 1024 * 1024,\n            multiboot: root_path + \"/images/crazierl-elf.img\",\n            initrd: root_path + \"/images/crazierl-initrd.img\",\n            cmdline: \"kernel /libexec/ld-elf32.so.1\",\n            acpi: true,\n            expected_serial_text: [\n                \"Welcome to Crazierl:\",\n            ],\n        },\n        {\n            name: \"Fiwix\",\n            skip_if_disk_image_missing: true,\n            timeout: 2 * 60,\n            memory_size: 512 * 1024 * 1024,\n            hda: root_path + \"/images/FiwixOS-3.4-i386.img\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            expected_texts: [\n                \"(root):~#\",\n            ],\n            actions: [\n                { on_text: \"(root):~#\", run: \"/usr/games/lsdoom\\n\" },\n            ],\n        },\n        {\n            name: \"9legacy\",\n            use_small_bios: true, // has issues with 256k bios\n            skip_if_disk_image_missing: true,\n            net_device: { type: \"none\" }, // if netdevice is found, waits for dhcp before starting desktop\n            timeout: 5 * 60,\n            memory_size: 512 * 1024 * 1024,\n            hda: root_path + \"/images/9legacy.img\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            expected_texts: [\n                \"Selection:\",\n            ],\n            actions: [\n                { on_text: \"Selection:\", run: \"1\\n\" },\n            ],\n            expected_serial_text: [\n                \"init: starting\",\n            ],\n        },\n        {\n            name: \"BSD/OS 3\",\n            skip_if_disk_image_missing: true,\n            net_device: { type: \"none\" }, // executes 16-bit io instructions\n            timeout: 5 * 60,\n            memory_size: 512 * 1024 * 1024,\n            cdrom: root_path + \"/images/experimental/bsdos-3.0-binary.iso\",\n            fda: root_path + \"/images/experimental/bsdos3-install-floppy.img\",\n            expected_texts: [\"\\xc9\\xcd BSD/OS Installation\"],\n            boot_order: 0x321,\n        },\n        {\n            name: \"BSD/OS 4\",\n            skip_if_disk_image_missing: true,\n            net_device: { type: \"none\" }, // executes 16-bit io instructions\n            timeout: 5 * 60,\n            memory_size: 512 * 1024 * 1024,\n            cdrom: root_path + \"/images/experimental/bsdos-4.3-x86-binary.iso\",\n            expected_texts: [\"\\xc9\\xcd BSD/OS Installation\"],\n        },\n        {\n            name: \"Arch Hurd\",\n            skip_if_disk_image_missing: true,\n            net_device: { type: \"none\" }, // executes 16-bit io instructions\n            timeout: 5 * 60,\n            memory_size: 512 * 1024 * 1024,\n            hda: root_path + \"/images/archhurd-2018.09.28.img\",\n            expected_texts: [\"sh-4.4# \"],\n        },\n        {\n            name: \"Linux with Postgres\",\n            skip_if_disk_image_missing: true,\n            timeout: 5 * 60,\n            memory_size: 512 * 1024 * 1024,\n            cdrom: root_path + \"/images/experimental/linux-postgres.iso\",\n            expected_texts: [\n                \"performing post-bootstrap initialization\",\n                \"syncing data to disk\",\n                \"Success. You can now start the database server using\",\n            ],\n        },\n        {\n            name: \"Tiny Core 11 CD\",\n            skip_if_disk_image_missing: 1,\n            timeout: 10 * 60,\n            cdrom: root_path + \"/images/TinyCore-11.0.iso\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            actions: [{ on_text: \"                   BIOS default device boot in\", run: \"\\n\", after: 5000 }],\n        },\n        {\n            name: \"Tiny Core 11 HD\",\n            skip_if_disk_image_missing: 1,\n            timeout: 10 * 60,\n            hda: root_path + \"/images/TinyCore-11.0.iso\",\n            expect_graphical_mode: true,\n            expect_mouse_registered: true,\n            actions: [{ on_text: \"                   BIOS default device boot in\", run: \"\\n\", after: 5000 }],\n        },\n        {\n            name: \"Core 9 (with hard disk)\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-9.0.iso\",\n            fda: root_path + \"/images/freedos722.img\",\n            boot_order: 0x213,\n            actions: [\n                { on_text: \"boot:\", run: \"\\n\" },\n                { on_text: \"tc@box\", run: \"sudo mount /dev/fd0 /mnt && ls /mnt\\n\" },\n            ],\n            expected_texts: [\"AUTOEXEC.BAT\"],\n        },\n        {\n            name: \"Core 9 (with hard disk)\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-9.0.iso\",\n            hda: root_path + \"/images/TinyCore-11.0.iso\",\n            boot_order: 0x213,\n            actions: [\n                { on_text: \"boot:\", run: \"\\n\" },\n                { on_text: \"tc@box\", run: \"sudo mount /dev/sda1 /mnt && ls /mnt\\n\" },\n            ],\n            expected_texts: [\"boot/ cde/\"],\n        },\n        {\n            name: \"Core 8\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-8.0.iso\",\n            expected_texts: [\"tc@box\"],\n            actions: [{ on_text: \"boot:\", run: \"\\n\" }],\n        },\n        {\n            name: \"Core 7\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-7.2.iso\",\n            expected_texts: [\"tc@box\"],\n            actions: [{ on_text: \"boot:\", run: \"\\n\" }],\n        },\n        {\n            name: \"Core 6\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-6.4.1.iso\",\n            expected_texts: [\"tc@box\"],\n            actions: [{ on_text: \"boot:\", run: \"\\n\" }],\n        },\n        {\n            name: \"Core 5\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-5.4.iso\",\n            expected_texts: [\"tc@box\"],\n            actions: [{ on_text: \"boot:\", run: \"\\n\" }],\n        },\n        {\n            name: \"Core 4\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/experimental/os/Core-4.7.7.iso\",\n            expected_texts: [\"tc@box\"],\n            actions: [{ on_text: \"boot:\", run: \"\\n\" }],\n        },\n        {\n            name: \"Damn Small Linux\",\n            skip_if_disk_image_missing: 1,\n            timeout: 5 * 60,\n            cdrom: root_path + \"/images/dsl-4.11.rc2.iso\",\n            expect_graphical_mode: true,\n            expect_graphical_size: [1024, 768],\n            expect_mouse_registered: true,\n        },\n    ];\n\n    if(TEST_NAME)\n    {\n        tests = tests.filter(test => test.name === TEST_NAME);\n    }\n\n    var nr_of_cpus = Math.min(Math.round(os.cpus().length / 2) || 1, tests.length, MAX_PARALLEL_TESTS);\n    console.log(\"Using %d cpus\", nr_of_cpus);\n\n    var current_test = 0;\n\n    for(var i = 0; i < nr_of_cpus; i++)\n    {\n        var worker = cluster.fork();\n\n        worker.on(\"message\", send_work_to_worker.bind(null, worker));\n\n        worker.on(\"exit\", function(code, signal)\n        {\n            if(signal)\n            {\n                console.warn(\"Worker killed by signal \" + signal);\n                process.exit(1);\n            }\n            else if(code !== 0)\n            {\n                process.exit(code);\n            }\n        });\n\n        worker.on(\"error\", function(error)\n        {\n            console.error(\"Worker error: \", error.toString(), error);\n            process.exit(1);\n        });\n    }\n}\nelse\n{\n    cluster.worker.on(\"message\", function(test_case)\n    {\n        run_test(test_case, function()\n        {\n            process.send(\"I'm done\");\n        });\n    });\n    process.send(\"up\");\n}\n\nfunction bytearray_starts_with(arr, search)\n{\n    for(var i = 0; i < search.length; i++)\n    {\n        if(arr[i] !== search[i])\n        {\n            return false;\n        }\n    }\n    return true;\n}\n\nfunction run_test(test, done)\n{\n    console.log(\"Starting test: %s\", test.name);\n\n    const images = [test.fda, test.hda, test.cdrom, test.bzimage, test.multiboot, test.filesystem && test.filesystem.basefs].filter(x => x);\n    assert(images.length, \"Bootable drive expected\");\n\n    const missing_images = images.filter(i => !fs.existsSync(i));\n    if(missing_images.length)\n    {\n        if(test.skip_if_disk_image_missing)\n        {\n            console.warn(\"Missing disk image: \" + missing_images.join(\", \") + \", test skipped\");\n            console.warn();\n\n            done();\n            return;\n        }\n        else\n        {\n            console.warn(\"Missing disk image: \" + missing_images.join(\", \"));\n            process.exit(1);\n        }\n    }\n\n    if(test.slow && !RUN_SLOW_TESTS)\n    {\n        console.warn(\"Slow test: \" + test.name + \", skipped\");\n        console.warn();\n        done();\n        return;\n    }\n\n    if(test.alternative_bios)\n    {\n        var bios = root_path + \"/bios/bochs-bios.bin\";\n        var vga_bios = root_path + \"/bios/bochs-vgabios.bin\";\n    }\n    else if(test.use_small_bios || TEST_RELEASE_BUILD)\n    {\n        var bios = root_path + \"/bios/seabios.bin\";\n        var vga_bios = root_path + \"/bios/vgabios.bin\";\n    }\n    else\n    {\n        var bios = root_path + \"/bios/seabios-debug.bin\";\n        var vga_bios = root_path + \"/bios/vgabios-debug.bin\";\n    }\n\n    var settings = {\n        bios: { url: bios },\n        vga_bios: { url: vga_bios },\n        autostart: true,\n        memory_size: test.memory_size || 128 * 1024 * 1024,\n        log_level: LOG_LEVEL,\n    };\n\n    if(test.cdrom)\n    {\n        settings.cdrom = { url: test.cdrom, async: true };\n    }\n    if(test.fda)\n    {\n        settings.fda = { url: test.fda };\n    }\n    if(test.hda)\n    {\n        settings.hda = { url: test.hda, async: true };\n    }\n    if(test.hdb)\n    {\n        settings.hdb = { url: test.hdb, async: true };\n    }\n    if(test.state)\n    {\n        settings.initial_state = { url: test.state };\n    }\n    if(test.bzimage)\n    {\n        settings.bzimage = { url: test.bzimage };\n    }\n    if(test.multiboot)\n    {\n        settings.multiboot = { url: test.multiboot };\n    }\n    if(test.initrd)\n    {\n        settings.initrd = { url: test.initrd };\n    }\n    if(test.filesystem)\n    {\n        settings.filesystem = test.filesystem;\n    }\n    settings.cmdline = test.cmdline;\n    settings.bzimage_initrd_from_filesystem = test.bzimage_initrd_from_filesystem;\n    settings.acpi = test.acpi === undefined && !test.state ? TEST_ACPI : test.acpi;\n    settings.boot_order = test.boot_order;\n    settings.cpuid_level = test.cpuid_level;\n    settings.net_device = test.net_device;\n    settings.disable_jit = DISABLE_JIT;\n\n    if(test.expected_texts)\n    {\n        test.expected_texts = test.expected_texts.map(string_to_bytearray);\n    }\n    else\n    {\n        test.expected_texts = [];\n    }\n\n    if(!test.expected_serial_text)\n    {\n        test.expected_serial_text = [];\n    }\n\n    var emulator = new V86(settings);\n    var screen = new Uint8Array(SCREEN_WIDTH * 25);\n\n    function check_text_test_done()\n    {\n        return test.expected_texts.length === 0;\n    }\n\n    function check_serial_test_done()\n    {\n        return test.expected_serial_text.length === 0;\n    }\n\n    var mouse_test_done = false;\n    function check_mouse_test_done()\n    {\n        return !test.expect_mouse_registered || mouse_test_done;\n    }\n\n    var graphical_test_done = false;\n    var size_test_done = false;\n    function check_graphical_test_done()\n    {\n        return !test.expect_graphical_mode || (graphical_test_done && (!test.expect_graphical_size || size_test_done));\n    }\n\n    var test_start = Date.now();\n\n    var timeout_seconds = test.timeout * TIMEOUT_EXTRA_FACTOR;\n    var timeout = setTimeout(check_test_done, (timeout_seconds + 1) * 1000);\n    var timeouts = [timeout];\n\n    var on_text = [];\n    var stopped = false;\n\n    var screen_interval = null;\n\n    function check_test_done()\n    {\n        if(stopped)\n        {\n            return;\n        }\n\n        if(check_text_test_done() &&\n            check_mouse_test_done() &&\n            check_graphical_test_done() &&\n            check_serial_test_done())\n        {\n            var end = Date.now();\n\n            for(let timeout of timeouts) clearTimeout(timeout);\n            stopped = true;\n\n            emulator.stop();\n\n            if(screen_interval !== null)\n            {\n                clearInterval(screen_interval);\n            }\n\n            console.warn(\"Passed test: %s (took %ds)\", test.name, (end - test_start) / 1000);\n            console.warn();\n\n            done();\n        }\n        else if(Date.now() >= test_start + timeout_seconds * 1000)\n        {\n            for(let timeout of timeouts) clearTimeout(timeout);\n            stopped = true;\n\n            if(screen_interval !== null)\n            {\n                clearInterval(screen_interval);\n            }\n\n            emulator.destroy();\n\n            if(test.failure_allowed)\n            {\n                console.warn(\"Test failed: %s (failure allowed)\\n\", test.name);\n            }\n            else\n            {\n                console.warn(screen_to_text(screen));\n                console.warn(\"Test failed: %s\\n\", test.name);\n            }\n\n            if(!check_text_test_done())\n            {\n                console.warn('Expected text \"%s\" after %d seconds.', bytearray_to_string(test.expected_texts[0]), timeout_seconds);\n            }\n\n            if(!check_graphical_test_done())\n            {\n                console.warn(\"Expected graphical mode after %d seconds.\", timeout_seconds);\n            }\n\n            if(!check_mouse_test_done())\n            {\n                console.warn(\"Expected mouse activation after %d seconds.\", timeout_seconds);\n            }\n\n            if(!check_serial_test_done())\n            {\n                console.warn('Expected serial text \"%s\" after %d seconds.', test.expected_serial_text, timeout_seconds);\n            }\n\n            if(on_text.length)\n            {\n                console.warn(`Note: Expected text \"${bytearray_to_string(on_text[0].text)}\" to run \"${on_text[0].run}\"`);\n            }\n\n            if(!test.failure_allowed)\n            {\n                process.exit(1);\n            }\n            else\n            {\n                done();\n            }\n        }\n    }\n\n    emulator.add_listener(\"mouse-enable\", function()\n    {\n        mouse_test_done = true;\n        check_test_done();\n    });\n\n    emulator.add_listener(\"screen-set-size\", function(args)\n    {\n        const [w, h, bpp] = args;\n        graphical_test_done = bpp !== 0;\n\n        if(test.expect_graphical_size)\n        {\n            size_test_done = w === test.expect_graphical_size[0] && h === test.expect_graphical_size[1];\n        }\n\n        check_test_done();\n    });\n\n    emulator.add_listener(\"screen-put-char\", function(chr)\n    {\n        var y = chr[0];\n        var x = chr[1];\n        var code = chr[2];\n        screen[x + SCREEN_WIDTH * y] = code;\n\n        var line = get_line(screen, y);\n\n        if(!check_text_test_done())\n        {\n            let expected = test.expected_texts[0];\n            if(x < expected.length && bytearray_starts_with(line, expected))\n            {\n                test.expected_texts.shift();\n                if(VERBOSE) console.log(`Passed: \"${bytearray_to_string(expected)}\" on screen (${test.name})`);\n                check_test_done();\n            }\n        }\n\n        if(on_text.length)\n        {\n            let expected = on_text[0].text;\n\n            if(x < expected.length && bytearray_starts_with(line, expected))\n            {\n                var action = on_text.shift();\n\n                timeouts.push(\n                    setTimeout(() => {\n                        if(VERBOSE) console.error(\"Sending '%s'\", action.run);\n                        if(typeof action.run[0] === \"string\") emulator.keyboard_send_text(action.run, 10);\n                        else emulator.keyboard_send_scancodes(action.run, 10);\n                    }, action.after || 0)\n                );\n            }\n        }\n    });\n\n    if(LOG_SCREEN)\n    {\n        screen_interval = setInterval(() => {\n            console.warn(screen_to_text(screen));\n        }, 10000);\n    }\n\n    let serial_line = \"\";\n    emulator.add_listener(\"serial0-output-byte\", function(byte)\n        {\n            var c = String.fromCharCode(byte);\n            if(c === \"\\n\")\n            {\n                if(VERBOSE)\n                {\n                    console.log(`Serial (${test.name}):`, serial_line);\n                }\n\n                if(test.expected_serial_text.length)\n                {\n                    const expected = test.expected_serial_text[0];\n                    if(serial_line.includes(expected))\n                    {\n                        test.expected_serial_text.shift();\n                        if(VERBOSE) console.log(`Passed: \"${expected}\" on serial (${test.name})`);\n                        check_test_done();\n                    }\n                }\n\n                serial_line = \"\";\n            }\n            else if(c >= \" \" && c <= \"~\")\n            {\n                serial_line += c;\n            }\n        });\n\n    test.actions && test.actions.forEach(function(action)\n    {\n        if(action.on_text)\n        {\n            on_text.push({ text: string_to_bytearray(action.on_text), run: action.run, after: action.after });\n        }\n        else\n        {\n            timeouts.push(\n                setTimeout(() => {\n                    if(VERBOSE) console.error(\"Sending '%s'\", action.run);\n                    if(typeof action.run[0] === \"string\") emulator.keyboard_send_text(action.run, 10);\n                    else emulator.keyboard_send_scancodes(action.run, 10);\n                }, action.after || 0)\n            );\n        }\n    });\n}\n"
  },
  {
    "path": "tests/jit-paging/Makefile",
    "content": "CC=gcc\nCC_I386=$(CC) -m32\nCFLAGS=-Wall -g -fno-strict-aliasing -fPIC -static\nLDFLAGS=\n\n\ntest-jit: test-jit.c\n\t$(CC_I386) $(CFLAGS) $(LDFLAGS) -o $@ $(<D)/test-jit.c\n\nclean:\n\trm -f test-jit\n"
  },
  {
    "path": "tests/jit-paging/README.md",
    "content": "# About\n\nThese tests map 2 adjacent pages to the exact same physical frame. Code is\nwritten to one page and executed from the other, then overwritten and executed\nagain, in order to trigger cache activity. Unlike `/tests/jit/`, this folder is\nmeant to test the JIT in protected mode with paging setup, not in real-mode.\n\n# Run\n\n- Obtain the `linux3.iso` image (see [Readme.md](../../Readme.md))\n- Run `make jitpagingtests` in the root of the project\n"
  },
  {
    "path": "tests/jit-paging/run.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport url from \"node:url\";\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nvar test_executable = new Uint8Array(fs.readFileSync(__dirname + \"/test-jit\"));\n\nvar emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\" },\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: 0,\n});\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    console.log(\"Booting now, please stand by\");\n    emulator.create_file(\"test-jit\", test_executable);\n});\n\nvar ran_command = false;\nvar line = \"\";\n\nemulator.add_listener(\"serial0-output-byte\", async function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(chr === \"\\n\")\n    {\n        var new_line = line;\n        console.error(\"Serial: %s\", line);\n        line = \"\";\n    }\n    else\n    {\n        line += chr;\n    }\n\n    if(!ran_command && line.endsWith(\"~% \"))\n    {\n        ran_command = true;\n        emulator.serial0_send(\"chmod +x /mnt/test-jit\\n\");\n        emulator.serial0_send(\"/mnt/test-jit 2>&1 | tee /mnt/result\\n\");\n        emulator.serial0_send(\"echo test fini''shed\\n\");\n    }\n\n    if(new_line && new_line.includes(\"test finished\"))\n    {\n        console.error(\"Done. Reading result ...\");\n\n        const data = await emulator.read_file(\"/result\");\n\n        emulator.destroy();\n\n        let result = Buffer.from(data).toString();\n        if(result !== \"test_shared passed\\ntest_consecutive_written passed\\n\")\n        {\n            console.error(\"[!] Error. Result was:\\n\" + result);\n            process.exit(1);\n        }\n        else\n        {\n            console.log(\"[+] Test passed\");\n        }\n    }\n});\n"
  },
  {
    "path": "tests/jit-paging/test-jit.c",
    "content": "#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <sys/mman.h>\n#include <sys/user.h>\n#include <unistd.h>\n\n\nint fib(int n)\n{\n    int first = 0, second = 1, next = 0, i = 0;\n    while(i <= n)\n    {\n        if(i < 2)\n        {\n            next = i;\n        }\n        else\n        {\n            next = first + second;\n            first = second;\n            second = next;\n        }\n        i++;\n    }\n    return next;\n}\n\nint pass_test()\n{\n    return 0x42;\n}\n\nvoid fatal(char *msg)\n{\n    fprintf(stderr, \"*** FATAL ERROR: %s\\n\", (msg ? msg : \"no message\"));\n    fflush(stderr);\n    abort();\n}\n\nvoid test_shared()\n{\n    static char filename[] = \"/tmp/DoubleMapXXXXXX\";\n    int fd = mkstemp(filename);\n    if(fd == -1)\n    {\n        fatal(\"mkstemp\");\n    }\n    if(ftruncate(fd, PAGE_SIZE) == -1)\n    {\n        fatal(\"ftruncate\");\n    }\n\n    uint8_t *const write_addr = mmap(0, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);\n    uint8_t *const exec_addr = mmap(write_addr+PAGE_SIZE, PAGE_SIZE,\n            PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED, fd, 0);\n\n    if(write_addr == MAP_FAILED || exec_addr == MAP_FAILED)\n    {\n        fatal(\"mmap\");\n    }\n\n    size_t size = PAGE_SIZE;\n    memcpy(write_addr, fib, size);\n\n    int (*fun_pointer)() = (void*)exec_addr;\n\n    // Give the JIT something to potentially cache\n    for(int i = 0; i < 15000; i++)\n    {\n        if(fun_pointer(20) != 6765)\n        {\n            fatal(\"fibonacci\");\n        }\n    }\n\n    memcpy(write_addr, pass_test, size);\n    if(fun_pointer() == 0x42)\n    {\n        printf(\"test_shared passed\\n\");\n    }\n\n    munmap(write_addr, 2 * size);\n    munmap(exec_addr, size);\n}\n\n\nvoid test_consecutive()\n{\n    uint8_t *const page0 = mmap(NULL,\n            2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n\n    // throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames\n    uint8_t *const throwaway = mmap(NULL,\n            PAGE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n\n    uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,\n            PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);\n\n    if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)\n    {\n        fatal(\"mmap\");\n    }\n\n    // Attempt to influence virtual to physical mapping - we want page0->page1 to not be contiguous\n    // physically\n    page0[0] = 0;\n    throwaway[0] = 0;\n    page1[0] = 0;\n\n    for(int32_t i = 0; i < 100; i++)\n    {\n        uint8_t* start = (uint8_t*)(page1 - i - 1);\n        memcpy(start, fib, PAGE_SIZE);\n        int (*fun_pointer)() = (void*)start;\n\n        for(int j = 0; j < 15000; j++)\n        {\n            if(fun_pointer(20) != 6765)\n            {\n                fatal(\"fibonacci\");\n            }\n        }\n    }\n\n    printf(\"test_consecutive passed\\n\");\n}\n\nvoid test_consecutive_written()\n{\n    uint8_t *const page0 = mmap(NULL,\n            2 * PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n\n    // throwaway mmap to reduce likelhood of page0 and page1 mapping to consecutive physical frames\n    uint8_t *const throwaway = mmap(NULL,\n            PAGE_SIZE, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n\n    uint8_t *const page1 = mmap(page0 + PAGE_SIZE, PAGE_SIZE,\n            PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);\n\n    if(page0 == MAP_FAILED || throwaway == MAP_FAILED || page1 == MAP_FAILED)\n    {\n        fatal(\"mmap\");\n    }\n\n    // Attempt to influence virtual to physical mapping - we want page0->page1 to not be contiguous\n    // physically\n    page0[0] = 0;\n    throwaway[0] = 0;\n    page1[0] = 0;\n\n    uint8_t* start = page1 - 8;\n    uint8_t* ptr = start;\n    const int32_t INC_COUNT = 16;\n\n    // xor eax, eax\n    *ptr++ = 0x31;\n    *ptr++ = 0xc0;\n\n    for(int i = 0; i < INC_COUNT; i++)\n    {\n        // inc eax\n        *ptr++ = 0x40;\n    }\n\n    // ret\n    *ptr++ = 0xC3;\n\n    int (*fun_pointer)() = (void*)start;\n\n    for(int i = 0; i < 15000; i++)\n    {\n        int32_t result = fun_pointer();\n\n        if(result != INC_COUNT)\n        {\n            fatal(\"test_consecutive_written\");\n        }\n    }\n\n    // overwrite one INC at the start of the second page with a NOP\n    *page1 = 0x90;\n\n    int32_t result = fun_pointer();\n\n    if(result != INC_COUNT - 1)\n    {\n        fatal(\"test_consecutive_written after overwrite\");\n    }\n\n    printf(\"test_consecutive_written passed\\n\");\n}\n\nint main()\n{\n    test_shared();\n\n    // disabled for now, takes long and not sure if it actually catches bugs\n    //test_consecutive();\n\n    test_consecutive_written();\n\n    return 0;\n}\n"
  },
  {
    "path": "tests/manual/gc.html",
    "content": "<!doctype html>\n<title>Test that emulator is GCed properly</title>\n\n<script src=\"../../build/libv86.js\"></script>\n<script>\n\"use strict\";\n\nwindow.onload = function()\n{\n    var emulator = new V86({\n        wasm_path: \"../../build/v86.wasm\",\n        memory_size: 32 * 1024 * 1024,\n        vga_memory_size: 2 * 1024 * 1024,\n        screen_container: document.getElementById(\"screen_container\"),\n        bios: { url: \"../../bios/seabios.bin\" },\n        vga_bios: { url: \"../../bios/vgabios.bin\" },\n        cdrom: { url: \"../../images/linux.iso\" },\n        autostart: true,\n    });\n\n    setTimeout(() => {\n        emulator.destroy();\n        console.log(\"Emulator freed. Check using devtools (in chromium: Memory -> Heap Snapshot -> click collect garbage -> take snapshot).\");\n    }, 3 * 1000);\n}\n</script>\n\n<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->\n<div id=\"screen_container\">\n    <div style=\"white-space: pre; font: 14px monospace; line-height: 14px\"></div>\n    <canvas style=\"display: none\"></canvas>\n</div>\n"
  },
  {
    "path": "tests/nasm/Readme.md",
    "content": "# About\n\nThe tests in this folder are not comprehensive by any means at the\nmoment.\n\n# Automated Testing\n\n- Run `make nasmtests` in the root of the project\n"
  },
  {
    "path": "tests/nasm/arith-optimisations.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    xor eax, eax\n    pushf\n    and dword [esp], 8ffh\n\n    test ebx, ebx\n    pushf\n    and dword [esp], 8ffh\n\n    cmp ecx, 0\n    pushf\n    and dword [esp], 8ffh\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/btc-imm.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], 0x03479aef\n    mov dword [esp+4], 0x03479aef\n    mov dword [esp+8], 0x03479aef\n    mov dword [esp+12], 0x03479aef\n    mov dword [esp+16], 0x03479aef\n    mov dword [esp+20], 0x03479aef\n    mov dword [esp+24], 0x03479aef\n    mov dword [esp+28], 0x03479aef\n\n    btc word [esp], 0\n    btc word [esp], 4\n    btc word [esp], 9\n    btc word [esp], 16\n    btc word [esp], 31\n    btc word [esp], 32\n    btc word [esp], 55\n    btc word [esp], 200\n\n    btc dword [esp], 1\n    btc dword [esp], 5\n    btc dword [esp], 10\n    btc dword [esp], 17\n    btc dword [esp], 30\n    btc dword [esp], 33\n    btc dword [esp], 56\n    btc dword [esp], 201\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/btc-reg16.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], 0x03479aef\n    mov dword [esp+4], 0x03479aef\n    mov dword [esp+8], 0x03479aef\n    mov dword [esp+12], 0x03479aef\n    mov dword [esp+16], 0x03479aef\n    mov dword [esp+20], 0x03479aef\n    mov dword [esp+24], 0x03479aef\n    mov dword [esp+28], 0x03479aef\n\n    mov eax, 0\n    btc word [esp], ax\n    mov eax, -5\n    btc word [esp + 4], ax\n    mov eax, 1\n    btc word [esp], ax\n    mov eax, 31\n    btc word [esp], ax\n    mov eax, 32\n    btc word [esp], ax\n    mov eax, 63\n    btc word [esp], ax\n    mov eax, 99\n    btc word [esp], ax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/btc-reg32.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], 0x03479aef\n    mov dword [esp+4], 0x03479aef\n    mov dword [esp+8], 0x03479aef\n    mov dword [esp+12], 0x03479aef\n    mov dword [esp+16], 0x03479aef\n    mov dword [esp+20], 0x03479aef\n    mov dword [esp+24], 0x03479aef\n    mov dword [esp+28], 0x03479aef\n\n    mov eax, 0\n    btc dword [esp], eax\n    mov eax, -5\n    btc dword [esp + 4], eax\n    mov eax, 1\n    btc dword [esp], eax\n    mov eax, 31\n    btc dword [esp], eax\n    mov eax, 32\n    btc dword [esp], eax\n    mov eax, 63\n    btc dword [esp], eax\n    mov eax, 99\n    btc dword [esp], eax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/btr-imm.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], -1\n    mov dword [esp+4], -1\n    mov dword [esp+8], -1\n    mov dword [esp+12], -1\n    mov dword [esp+16], -1\n    mov dword [esp+20], -1\n    mov dword [esp+24], -1\n    mov dword [esp+28], -1\n\n    btr word [esp], 0\n    btr word [esp], 4\n    btr word [esp], 9\n    btr word [esp], 16\n    btr word [esp], 31\n    btr word [esp], 32\n    btr word [esp], 55\n    btr word [esp], 200\n\n    btr dword [esp], 1\n    btr dword [esp], 5\n    btr dword [esp], 10\n    btr dword [esp], 17\n    btr dword [esp], 30\n    btr dword [esp], 33\n    btr dword [esp], 56\n    btr dword [esp], 201\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/btr-reg16.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], -1\n    mov dword [esp+4], -1\n    mov dword [esp+8], -1\n    mov dword [esp+12], -1\n    mov dword [esp+16], -1\n    mov dword [esp+20], -1\n    mov dword [esp+24], -1\n    mov dword [esp+28], -1\n\n    mov eax, 0\n    btr word [esp], ax\n    mov eax, -5\n    btr word [esp + 4], ax\n    mov eax, 1\n    btr word [esp], ax\n    mov eax, 31\n    btr word [esp], ax\n    mov eax, 32\n    btr word [esp], ax\n    mov eax, 63\n    btr word [esp], ax\n    mov eax, 99\n    btr word [esp], ax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/btr-reg32.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], -1\n    mov dword [esp+4], -1\n    mov dword [esp+8], -1\n    mov dword [esp+12], -1\n    mov dword [esp+16], -1\n    mov dword [esp+20], -1\n    mov dword [esp+24], -1\n    mov dword [esp+28], -1\n\n    mov eax, 0\n    btr dword [esp], eax\n    mov eax, -5\n    btr dword [esp + 4], eax\n    mov eax, 1\n    btr dword [esp], eax\n    mov eax, 31\n    btr dword [esp], eax\n    mov eax, 32\n    btr dword [esp], eax\n    mov eax, 63\n    btr dword [esp], eax\n    mov eax, 99\n    btr dword [esp], eax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/bts-imm.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], 0\n    mov dword [esp+4], 0\n    mov dword [esp+8], 0\n    mov dword [esp+12], 0\n    mov dword [esp+16], 0\n    mov dword [esp+20], 0\n    mov dword [esp+24], 0\n    mov dword [esp+28], 0\n\n    bts word [esp], 0\n    bts word [esp], 4\n    bts word [esp], 9\n    bts word [esp], 16\n    bts word [esp], 31\n    bts word [esp], 32\n    bts word [esp], 55\n    bts word [esp], 200\n\n    bts dword [esp], 1\n    bts dword [esp], 5\n    bts dword [esp], 10\n    bts dword [esp], 17\n    bts dword [esp], 30\n    bts dword [esp], 33\n    bts dword [esp], 56\n    bts dword [esp], 201\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/bts-reg16.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], 0\n    mov dword [esp+4], 0\n    mov dword [esp+8], 0\n    mov dword [esp+12], 0\n    mov dword [esp+16], 0\n    mov dword [esp+20], 0\n    mov dword [esp+24], 0\n    mov dword [esp+28], 0\n\n    mov eax, 0\n    bts word [esp], ax\n    mov eax, -5\n    bts word [esp + 4], ax\n    mov eax, 1\n    bts word [esp], ax\n    mov eax, 31\n    bts word [esp], ax\n    mov eax, 32\n    bts word [esp], ax\n    mov eax, 63\n    bts word [esp], ax\n    mov eax, 99\n    bts word [esp], ax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/bts-reg32.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp], 0\n    mov dword [esp+4], 0\n    mov dword [esp+8], 0\n    mov dword [esp+12], 0\n    mov dword [esp+16], 0\n    mov dword [esp+20], 0\n    mov dword [esp+24], 0\n    mov dword [esp+28], 0\n\n    mov eax, 0\n    bts dword [esp], eax\n    mov eax, -5\n    bts dword [esp + 4], eax\n    mov eax, 1\n    bts dword [esp], eax\n    mov eax, 31\n    bts dword [esp], eax\n    mov eax, 32\n    bts dword [esp], eax\n    mov eax, 63\n    bts dword [esp], eax\n    mov eax, 99\n    bts dword [esp], eax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/cmp16-setbe.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ax, -1\n    cmp ax, -3\n    setbe bl\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/cmpxchg-setbe.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov eax, 1\n    mov edx, 2\n    cmpxchg eax, edx\n    setbe  cl\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/cmpxchg.asm",
    "content": "global _start\n\nsection .data\n\n%include \"header.inc\"\n\n    mov eax, 123456789\n    mov ebx, 123456789\n    mov edx, 123456789\n\n    cmpxchg edx, ebx\n    push eax\n    push edx\n    push ebx\n    pushf\n    and dword [esp], 8ffh\n\n    cmpxchg ax, bx\n    push eax\n    push edx\n    push ebx\n    pushf\n    and dword [esp], 8ffh\n\n    cmpxchg al, bh\n    push eax\n    push edx\n    push ebx\n    pushf\n    and dword [esp], 8ffh\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/cmpxchg8b.asm",
    "content": "global _start\n\nsection .data\n\n%include \"header.inc\"\n\n    mov eax, 123456789\n    mov edx, 987654321\n\n    mov dword [esp], 123456789\n    mov dword [esp+4], 987654321\n\n    cmpxchg8b [esp]\n    push eax\n    push ecx\n    push ebx\n    push edx\n    pushf\n    and dword [esp], 8ffh\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/comiss.asm",
    "content": "global _start\n\nsection .data\n\talign 16\ncontrolword:\n\tdw\t0\ndwordmxcsr:\n\tdw\t0\ndword0:\n\tdd\t1000.0\ndword1:\n\tdd\t5.0\ndword2:\n\tdd\t3000.0\ndwSNaN:\n\tdd\t__SNaN__\ndwQNaN:\n\tdd\t__QNaN__\n\n; Moves EFLAGS into specified register\n%macro moveflags 1\n\tpushf\n\tand\t\t\tdword [esp], 0x45\n\tpop\t\t\teax\n\tmovd\t\t%1, eax\n%endmacro\n\n%include \"header.inc\"\n\n\tmovd\t\txmm0, [dword0]\n\t; Equal\n\tcomiss\t\txmm0, [dword0]\n\tmoveflags\tmm0\t\t\t\t; [ZF] = 100000\n\t; Less than\n\tcomiss\t\txmm0, [dword1]\n\tmoveflags\tmm1\t\t\t\t; [CF] = 000001\n\t; Greater than\n\tcomiss\t\txmm0, [dword2]\n\tmoveflags\tmm2\t\t\t\t; [] = 000000\n\n\t; Unordered: Quiet NaN\n\tmovd\t\txmm1, [dwQNaN]\n\tucomiss\t\txmm0, xmm1\n\tmoveflags\tmm3\t\t\t\t; [ZF][PF][CF] = 100101\n\t; Check #I exception\n\tstmxcsr\t\t[dwordmxcsr]\n\tmovd\t\tmm4, [dwordmxcsr]\n\n\t; Unordered: Signaling NaN\n\tmovd\t\txmm1,[dwSNaN]\n\tucomiss\t\txmm0, xmm1\n\tmoveflags\tmm5\t\t\t\t; [ZF][PF][CF] = 100101\n\t; Check #I exception\n\tstmxcsr\t\t[dwordmxcsr]\n\tmovd\t\tmm6, [dwordmxcsr]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/create_tests.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport fse from \"node:fs/promises\";\nimport path from \"node:path\";\nimport assert from \"node:assert/strict\";\nimport util from \"node:util\";\nimport url from \"node:url\";\nimport { execFile as execFileAsync } from \"node:child_process\";\n\nimport encodings from \"../../gen/x86_table.js\";\nimport Rand from \"./rand.js\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\n// number of tests per instruction\nconst NUMBER_TESTS = 5;\n// arithmetic tests\nconst NUMBER_ARITH_TESTS = 100;\n\nconst MAX_PARALLEL_PROCS = +process.env.MAX_PARALLEL_PROCS || 32;\n\nconst FLAGS_IGNORE = 0xFFFF3200;\nconst CF = 1 << 0;\nconst PF = 1 << 2;\nconst AF = 1 << 4;\nconst ZF = 1 << 6;\nconst SF = 1 << 7;\nconst OF = 1 << 11;\n\nconst BUILD_DIR = __dirname + \"/build/\";\nconst LOG_VERBOSE = false;\n\nconst execFile = util.promisify(execFileAsync);\n\nconst header = fs.readFileSync(path.join(__dirname, \"header.inc\"));\nconst footer = fs.readFileSync(path.join(__dirname, \"footer.inc\"));\n\nmain();\n\nasync function main()\n{\n    try\n    {\n        fs.mkdirSync(BUILD_DIR);\n    }\n    catch(e)\n    {\n        if(e.code !== \"EEXIST\")\n        {\n            throw e;\n        }\n    }\n\n    const tests = create_tests().reverse();\n\n    const workers = [];\n    for(let i = 0; i < MAX_PARALLEL_PROCS; i++)\n    {\n        workers.push(worker(make_test, tests));\n    }\n\n    await Promise.all(workers);\n}\n\nasync function worker(f, work)\n{\n    while(work.length)\n    {\n        await f(work.pop());\n    }\n}\n\nasync function make_test(test)\n{\n    LOG_VERBOSE && console.log(\"Start\", test.name || test.file);\n    let asm_file;\n    let img_file;\n    let tmp_file;\n\n    assert((test.asm && test.name) || test.file);\n    if(test.asm)\n    {\n        asm_file = BUILD_DIR + test.name + \".asm\";\n        img_file = BUILD_DIR + test.name + \".img\";\n        tmp_file = \"/tmp/\" + test.name + \".o\";\n\n        let old_code = undefined;\n\n        try\n        {\n            old_code = await fse.readFile(asm_file, { encoding: \"ascii\" });\n        }\n        catch(e)\n        {\n        }\n\n        if(old_code === test.asm)\n        {\n            LOG_VERBOSE && console.log(\"Skip\", test.name || test.file);\n            return;\n        }\n\n        await fse.writeFile(asm_file, test.asm);\n    }\n    else\n    {\n        asm_file = path.join(__dirname, test.file);\n        img_file = BUILD_DIR + test.file.replace(/\\.asm$/, \".img\");\n        tmp_file = \"/tmp/\" + test.file + \".o\";\n\n        try\n        {\n            if((await fse.stat(asm_file)).mtime < (await fse.stat(img_file)).mtime)\n            {\n                return;\n            }\n        }\n        catch(e)\n        {\n            if(e.code !== \"ENOENT\") throw e;\n        }\n    }\n\n    const options = {\n        cwd: __dirname,\n    };\n\n    LOG_VERBOSE && console.log(\"nasm\", [\"-w+error\", \"-felf32\", \"-o\", tmp_file, asm_file].join(\" \"));\n    await execFile(\"nasm\", [\"-w+error\", \"-felf32\", \"-o\", tmp_file, asm_file], options);\n    LOG_VERBOSE && console.log(\"ld\", [\"-g\", tmp_file, \"-m\", \"elf_i386\", \"--section-start=.bss=0x100000\", \"--section-start=.text=0x80000\", \"--section-start=.multiboot=0x20000\", \"-o\", img_file].join(\" \"));\n    await execFile(\"ld\", [\"-g\", tmp_file, \"-m\", \"elf_i386\", \"--section-start=.bss=0x100000\", \"--section-start=.text=0x80000\", \"--section-start=.multiboot=0x20000\", \"-o\", img_file], options);\n    await fse.unlink(tmp_file);\n\n    console.log(test.name || test.file);\n}\n\nfunction create_tests()\n{\n    const tests = [];\n\n    const asm_files = fs.readdirSync(__dirname).filter(f => f.endsWith(\".asm\"));\n    tests.push.apply(tests, asm_files.map(file => ({ file })));\n\n    for(const op of encodings)\n    {\n        const configurations = [\n            { mem: 0, size: 16, },\n            { mem: 0, size: 32, },\n            { mem: 1, size: 16, },\n            { mem: 1, size: 32, },\n        ];\n\n        let i = 0;\n\n        for(const config of configurations)\n        {\n            for(let nth_test = 0; nth_test < NUMBER_TESTS; nth_test++)\n            {\n                if(nth_test > 0 && op.opcode === 0x8D)\n                {\n                    // is already tested exhaustively in first run\n                    continue;\n                }\n\n                for(const asm of create_instruction_test(op, config, nth_test))\n                {\n                    tests.push({\n                        name: \"gen_\" + format_opcode(op.opcode) + \"_\" + (op.fixed_g || 0) + \"_\" + i,\n                        asm,\n                    });\n\n                    i++;\n                }\n            }\n        }\n    }\n\n    for(let i = 0; i < NUMBER_ARITH_TESTS; i++)\n    {\n        tests.push(create_arith_test(i));\n    }\n\n    return tests;\n}\n\nfunction format_opcode(n)\n{\n    let x = n.toString(16);\n    return (x.length === 1 || x.length === 3) ? \"0\" + x : x;\n}\n\nfunction create_nasm_modrm_combinations_16()\n{\n    let result = [];\n\n    for(let modrm = 0; modrm < 0xC0; modrm++)\n    {\n        let mod = modrm >> 6;\n        let rm = modrm & 7;\n\n        let has_imm8 = mod === 1;\n        let has_imm16 = mod === 2 || rm === 6 && mod === 0;\n\n        assert(!has_imm8 || !has_imm16);\n\n        let line = [\"db \" + modrm];\n        if(has_imm8) line.push(\"db 9ah\");\n        if(has_imm16) line.push(\"dw 9a1fh\");\n        result.push(line);\n    }\n\n    return result;\n}\n\nfunction create_nasm_modrm_combinations_32()\n{\n    let result = [];\n\n    let sample_sib_bytes = [0x05, 0x65, 0xAD, 0xCD, 0x20, 0xFF];\n    let exhaustive_sib_bytes = [];\n    for(let sib = 0; sib < 0x100; sib++) exhaustive_sib_bytes.push(sib);\n\n    for(let modrm = 0; modrm < 0xC0; modrm++)\n    {\n        let mod = modrm >> 6;\n        let reg = modrm >> 3 & 7;\n        let rm = modrm & 7;\n\n        let has_imm8 = mod === 1;\n        let has_imm32 = mod === 2 || rm === 5 && mod === 0;\n        let has_sib = rm === 4;\n\n        assert(!has_imm8 || !has_imm32);\n\n        if(has_sib)\n        {\n            // avoid generating an excessive number of tests\n            let sib_bytes = reg === 0 ? exhaustive_sib_bytes : sample_sib_bytes;\n\n            for(let sib of sib_bytes)\n            {\n                let line = [\"db \" + modrm, \"db \" + sib];\n                if(has_imm8) line.push(\"db 9ah\");\n                if(has_imm32 || mod === 0 && (sib & 7) === 5) line.push(\"dd 9a1fbcdeh\");\n                result.push(line);\n            }\n        }\n        else\n        {\n            let line = [\"db \" + modrm];\n            if(has_imm8) line.push(\"db 9ah\");\n            if(has_imm32) line.push(\"dd 9a1fbcdeh\");\n            result.push(line);\n        }\n    }\n\n    return result;\n}\n\nfunction rand_reg_but_not_esp(rng)\n{\n    let r = rng.int32() & 7;\n    return r === 4 ? rand_reg_but_not_esp(rng) : r;\n}\n\nfunction interesting_immediate(rng)\n{\n    if(rng.int32() & 1)\n    {\n        return rng.int32();\n    }\n    else\n    {\n        return rng.int32() << (rng.int32() & 31) >> (rng.int32() & 31);\n    }\n}\n\nfunction create_instruction_test(op, config, nth_test)\n{\n    if(op.prefix || op.skip)\n    {\n        return [];\n    }\n\n    if(config.mem ? op.skip_mem : op.skip_reg)\n    {\n        // Not supported by test\n        return [];\n    }\n\n    if(!op.e)\n    {\n        if(config.mem)\n        {\n            // doesn't use memory, don't test both\n            return [];\n        }\n    }\n\n    if(!op.os)\n    {\n        if(config.size === 16)\n        {\n            // equivalent to 32-bit version, don't test both\n            return [];\n        }\n    }\n\n    const rng = new Rand(1283698341 ^ op.opcode + nth_test * 0x10000);\n\n    const size = (op.os || op.opcode % 2 === 1) ? config.size : 8;\n    const is_modrm = op.e || op.fixed_g !== undefined;\n\n    const codes = [];\n\n    for(let reg of [\"eax\", \"ecx\", \"edx\", \"ebx\", \"ebp\", \"esi\", \"edi\"])\n    {\n        let rand = rng.int32();\n        codes.push(\"mov \" + reg + \", \" + rand);\n    }\n\n    if(!op.is_fpu) // generate random mmx registers\n    {\n        codes.push(\"sub esp, 8\");\n        for(let i = 0; i < 8; i++)\n        {\n            codes.push(\"mov dword [esp], \" + rng.int32());\n            codes.push(\"mov dword [esp + 4], \" + rng.int32());\n            codes.push(\"movq mm\" + i + \", [esp]\");\n        }\n        codes.push(\"add esp, 8\");\n    }\n    else // generate random fpu registers\n    {\n        codes.push(\"finit\");\n        codes.push(\"sub esp, 8\");\n\n        for(let i = 0; i < 8; i++)\n        {\n            codes.push(\"mov dword [esp], \" + rng.int32());\n            codes.push(\"mov dword [esp + 4], \" + rng.int32());\n            codes.push(\"fld qword [esp]\");\n        }\n\n        for(let i = 0; i < 4; i++) // half full stack\n        {\n            codes.push(\"fstp qword [esp]\");\n        }\n\n        codes.push(\"add esp, 8\");\n    }\n\n    if(true) // generate random xmm registers\n    {\n        codes.push(\"sub esp, 16\");\n        for(let i = 0; i < 8; i++)\n        {\n            codes.push(\"mov dword [esp], \" + rng.int32());\n            codes.push(\"mov dword [esp + 4], \" + rng.int32());\n            codes.push(\"mov dword [esp + 8], \" + rng.int32());\n            codes.push(\"mov dword [esp + 12], \" + rng.int32());\n            codes.push(\"movdqu xmm\" + i + \", [esp]\");\n        }\n        codes.push(\"add esp, 16\");\n    }\n\n    if(true) // generate random stack memory\n    {\n        for(let i = 0; i < 8; i++)\n        {\n            codes.push(\"sub esp, 4\");\n            codes.push(\"mov dword [esp], \" + rng.int32());\n        }\n    }\n\n    codes.push(\"push dword \" + (rng.int32() & ~(1 << 8 | 1 << 9)));\n    codes.push(\"popf\");\n\n    if(rng.int32() & 1)\n    {\n        // generate random flags using arithmetic instruction\n        // not well-distributed, but can trigger bugs in lazy flag calculation\n        if(rng.int32() & 1)\n        {\n            // rarely sets zero flag, other flags mostly well-distributed\n            codes.push(\"add al, ah\");\n        }\n        else\n        {\n            // always sets zero flag\n            codes.push(\"sub al, al\");\n        }\n    }\n\n    if(op.is_string)\n    {\n        codes.push(\"mov ecx, 3\");\n        codes.push(\"mov edi, (102000h-16)\");\n        codes.push(\"mov esi, (102000h-20)\");\n    }\n\n    if(size === 16)\n    {\n        codes.push(\"db 66h ; 16 bit\");\n    }\n\n    let opcode = op.opcode;\n\n    if([0x0FA5, 0x0FAD].includes(op.opcode) && size === 16)\n    {\n        // shld/shrd: immediates larger than opsize are undefined behaviour,\n        // but it's anded with 31 automatically, so only bit 4 needs to be cleared\n        codes.push(\"and cl, ~16\");\n    }\n\n    if(opcode === 0x8D)\n    {\n        // special case: lea: generate 16-bit addressing and all modrm combinations\n        assert(is_modrm);\n\n        codes.push([].concat(\n            create_nasm_modrm_combinations_16().map(lines => [\"db 67h\", \"db 8dh\"].concat(lines).join(\"\\n\")),\n            create_nasm_modrm_combinations_32().map(lines => [\"db 8dh\"].concat(lines).join(\"\\n\"))\n        ));\n    }\n    else\n    {\n        assert(opcode < 0x1000000);\n        if(opcode >= 0x10000)\n        {\n            let c = opcode >> 16;\n            assert(c === 0x66 || c === 0xF3 || c === 0xF2);\n            codes.push(\"db \" + c);\n            opcode &= ~0xFF0000;\n        }\n        if(opcode >= 0x100)\n        {\n            let c = opcode >> 8;\n            assert(c === 0x0F || c === 0xF2 || c === 0xF3, \"Expected 0F, F2, or F3 prefix, got \" + c.toString(16));\n            codes.push(\"db \" + c);\n            opcode &= ~0xFF00;\n        }\n        codes.push(\"db \" + opcode);\n\n        if(is_modrm)\n        {\n            let g = rand_reg_but_not_esp(rng);\n\n            if(op.fixed_g !== undefined)\n            {\n                g = op.fixed_g;\n            }\n\n            if(config.mem)\n            {\n                const e = 0x04; // [esp]\n                const sib = 0x24;\n\n                codes.push(\"db \" + (e | g << 3));\n                codes.push(\"db \" + sib);\n            }\n            else\n            {\n                const es =\n                    op.is_fpu ? [0, 1, 2, 3, 4, 5, 6, 7] : [\n                        rand_reg_but_not_esp(rng)\n                    ];\n                const modrm_bytes = es.map(e => \"db \" + (0xC0 | g << 3 | e));\n                codes.push(modrm_bytes);\n            }\n        }\n    }\n\n    if(op.opcode === 0xC8) // special case: enter\n    {\n        codes.push(\"dw 8h\");\n        codes.push(\"db 0h\");\n    }\n    else if(op.imm8 || op.imm8s || op.imm16 || op.imm1632 || op.imm32 || op.immaddr)\n    {\n        if(op.imm8 || op.imm8s)\n        {\n            if([0x0FA4, 0x0FAC].includes(op.opcode))\n            {\n                // shld/shrd: immediates larger than opsize are undefined behaviour\n                codes.push(\"db \" + (rng.int32() & (size === 16 ? 15 : 31)));\n            }\n            else\n            {\n                codes.push(\"db \" + (rng.int32() & 0xFF));\n            }\n        }\n        else\n        {\n            if(op.immaddr)\n            {\n                // immaddr: depends on address size\n                // generate valid pointer into bss section\n                codes.push(\"dd (102000h-16)\");\n            }\n            else\n            {\n                assert(op.imm1632 || op.imm16 || op.imm32);\n\n                if(op.imm1632 && size === 16 || op.imm16)\n                {\n                    codes.push(\"dw \" + (rng.int32() & 0xFFFF));\n                }\n                else\n                {\n                    assert(op.imm1632 && size === 32 || op.imm32);\n                    codes.push(\"dd \" + rng.int32());\n                }\n            }\n        }\n    }\n\n    if(op.mask_flags)\n    {\n        codes.push(\n            \"pushf\",\n            \"and dword [esp], ~\" + (op.mask_flags | FLAGS_IGNORE),\n            \"popf\",\n            \"mov dword [esp-4], 0\",\n        );\n    }\n\n    if(op.opcode === 0x06 || op.opcode === 0x0E || op.opcode === 0x16 || op.opcode === 0x1E ||\n        op.opcode === 0x0FA0 || op.opcode === 0x0FA8)\n    {\n        // push sreg: mask result\n        if(size === 16)\n        {\n            codes.push(\"mov word [esp], 0\");\n        }\n        else\n        {\n            // NOTE: upper word is undefined behaviour (unchanged on Intel, zero on AMD)\n            codes.push(\"mov dword [esp], 0\");\n        }\n    }\n\n    return all_combinations(codes).map(c => {\n        return header + c.join(\"\\n\") + \"\\n\" + footer;\n    });\n}\n\nfunction create_arith_test(i)\n{\n    const rng = new Rand(916237867 ^ i);\n\n    const registers_by_size = {\n        8: [\"al\", \"ah\", \"cl\", \"ch\", \"dl\", \"dh\", \"bl\", \"bh\"],\n        16: [\"ax\", \"cx\", \"dx\", \"bx\", \"sp\", \"bp\", \"si\", \"di\"],\n        32: [\"eax\", \"ecx\", \"edx\", \"ebx\", \"esp\", \"ebp\", \"esi\", \"edi\"],\n    };\n    const mask_by_size = {\n        8: 0xFF,\n        16: 0xFFFF,\n        32: -1,\n    };\n    const word_by_size = {\n        8: \"byte\",\n        16: \"word\",\n        32: \"dword\",\n    };\n    const two_operand_instructions = [\"add\", \"sub\", \"adc\", \"sbb\", \"and\", \"or\", \"xor\", \"cmp\", \"test\"];\n    const one_operand_instructions = [\n        \"inc\", \"dec\", \"neg\",\n        \"mul\", //\"idiv\", \"div\", // technically also eax:edx, but are implied by assembler\n        \"imul\", // handled specifically below to also generate 2-/3-operand form\n    ];\n    const shift_instructions = [\"shl\", \"shr\", \"sar\", \"rol\", \"ror\", \"rcl\", \"rcr\"];\n    // TODO: cmpxchg, xadd, bsf, bsr, shrd/shld, popcnt, bt*\n    const instructions = [two_operand_instructions, one_operand_instructions, shift_instructions].flat();\n    const conditions = [\n        // suffix flag\n        [\"o\", OF],\n        [\"c\", CF],\n        [\"z\", ZF],\n        [\"p\", PF],\n        [\"s\", SF],\n        [\"be\", CF | ZF],\n        [\"l\", SF | OF],\n        [\"le\", SF | OF | ZF],\n    ];\n\n    let c = [];\n    let address = 0x100000;\n\n    for(let reg of registers_by_size[32])\n    {\n        if(reg !== \"esp\")\n        {\n            c.push(`mov ${reg}, ${interesting_immediate(rng)}`);\n        }\n    }\n\n    let undefined_flags = 0;\n\n    for(let i = 0; i < 2000; i++)\n    {\n        const ins = instructions[rng.uint32() % instructions.length];\n        const size = [8, 16, 32][rng.uint32() % 3];\n        const size_word = word_by_size[size];\n        const dst_is_mem = rng.int32() & 1;\n        const dst = dst_is_mem ?\n            `${size_word} [${nasm_hex(address)}]` :\n            registers_by_size[size][rand_reg_but_not_esp(rng)];\n        let src_is_mem = false;\n        if(ins === \"imul\" && (rng.int32() & 1)) // other encodings handled in one_operand_instructions\n        {\n            // dst must be reg, no 8-bit\n            const size_imul = [16, 32][rng.int32() & 1];\n            const dst_imul = registers_by_size[size_imul][rand_reg_but_not_esp(rng)];\n            const src1 = dst_is_mem ?\n                `${word_by_size[size_imul]} [${nasm_hex(address)}]` :\n                registers_by_size[size_imul][rand_reg_but_not_esp(rng)];\n            if(rng.int32() & 1)\n            {\n                c.push(`${ins} ${dst_imul}, ${src1}`);\n            }\n            else\n            {\n                const src2 = nasm_hex(interesting_immediate(rng) & mask_by_size[size_imul]);\n                c.push(`${ins} ${dst_imul}, ${src1}, ${src2}`);\n            }\n        }\n        else if(one_operand_instructions.includes(ins))\n        {\n            c.push(`${ins} ${dst}`);\n        }\n        else if(two_operand_instructions.includes(ins))\n        {\n            src_is_mem = !dst_is_mem && (rng.int32() & 1);\n            const src = src_is_mem ?\n                `${size_word} [${nasm_hex(address)}]` :\n                (rng.int32() & 1) ?\n                registers_by_size[size][rand_reg_but_not_esp(rng)] :\n                nasm_hex(interesting_immediate(rng) & mask_by_size[size]);\n            c.push(`${ins} ${dst}, ${src}`);\n        }\n        else if(shift_instructions.includes(ins))\n        {\n            if(rng.int32() & 1)\n            {\n                // unknown CL\n                undefined_flags |= AF | OF;\n                c.push(`${ins} ${dst}, cl`);\n            }\n            else\n            {\n                const shift = interesting_immediate(rng) & 0xFF;\n                // TODO: shift mod {8,9,16,17,32,33} depending on bitsize/rotate/with-carry, shifts can clear undefined_flags if shift is not zero\n                undefined_flags |= shift === 1 ? AF : AF | OF;\n                if(rng.int32() & 1)\n                {\n                    // known CL\n                    c.push(`mov cl, ${nasm_hex(shift)}`);\n                    c.push(`${ins} ${dst}, cl`);\n                }\n                else\n                {\n                    // immediate\n                    c.push(`${ins} ${dst}, ${nasm_hex(shift)}`);\n                }\n            }\n        }\n\n        if(dst_is_mem || src_is_mem)\n        {\n            if(rng.int32() & 1)\n            {\n                address += size / 8;\n                // initialise next word\n                c.push(`mov dword [${nasm_hex(address)}], ${nasm_hex(interesting_immediate(rng) & 0xFF)}`);\n            }\n        }\n\n        if(ins === \"imul\" || ins === \"mul\" || ins === \"idiv\" || ins === \"div\")\n        {\n            undefined_flags = SF | ZF | AF | PF;\n        }\n        else if(!shift_instructions.includes(ins))\n        {\n            // adc/sbb/inc/dec read CF, but CF is never undefined\n            undefined_flags = 0;\n        }\n\n        if(rng.int32() & 1)\n        {\n            // setcc\n            const cond = random_pick(conditions.filter(([_, flag]) => 0 === (flag & undefined_flags)).map(([suffix]) => suffix), rng);\n            assert(cond);\n            const invert = (rng.int32() & 1) ? \"n\" : \"\";\n            const ins2 = `set${invert}${cond}`;\n            const dst2 = (rng.int32() & 1) ? `byte [${nasm_hex(address++)}]` : registers_by_size[8][rng.int32() & 7];\n            c.push(`${ins2} ${dst2}`);\n        }\n        else if(rng.int32() & 1)\n        {\n            // cmovcc\n            const cond = random_pick(conditions.filter(([_, flag]) => 0 === (flag & undefined_flags)).map(([suffix]) => suffix), rng);\n            assert(cond);\n            const invert = (rng.int32() & 1) ? \"n\" : \"\";\n            const ins2 = `cmov${invert}${cond}`;\n            const size = (rng.int32() & 1) ? 16 : 32;\n            const src2 = registers_by_size[size][rng.int32() & 7];\n            const dst2 = registers_by_size[size][rand_reg_but_not_esp(rng)];\n            c.push(`${ins2} ${dst2}, ${src2}`);\n        }\n        else if(rng.int32() & 1)\n        {\n            c.push(\"pushf\");\n            c.push(\"and dword [esp], ~\" + nasm_hex(FLAGS_IGNORE | undefined_flags));\n            c.push(`pop ${registers_by_size[32][rand_reg_but_not_esp(rng)]}`);\n        }\n        else\n        {\n            // intentionally left blank\n        }\n\n        // TODO:\n        // cmovcc\n        // other random instructions (mov, etc.)\n    }\n\n    c.push(\"pushf\");\n    c.push(\"and dword [esp], ~\" + nasm_hex(FLAGS_IGNORE | undefined_flags));\n    c.push(\"popf\");\n\n    assert(address < 0x102000);\n\n    const name = `arith_${i}`;\n    const asm = header + c.join(\"\\n\") + \"\\n\" + footer;\n\n    return { name, asm };\n}\n\nfunction all_combinations(xs)\n{\n    let result = [xs];\n\n    for(let i = 0; i < xs.length; i++)\n    {\n        let x = xs[i];\n\n        if(x instanceof Array)\n        {\n            let new_result = [];\n\n            for(let r of result)\n            {\n                for(let x_ of x)\n                {\n                    r = r.slice();\n                    r[i] = x_;\n                    new_result.push(r);\n                }\n            }\n\n            result = new_result;\n        }\n    }\n\n    return result;\n}\n\nfunction nasm_hex(x)\n{\n    return `0${(x >>> 0).toString(16).toUpperCase()}h`;\n}\n\nfunction random_pick(xs, rng)\n{\n    return xs[rng.uint32() % xs.length];\n}\n"
  },
  {
    "path": "tests/nasm/cvtpi2ps.asm",
    "content": "global _start\n\nsection .data\n    align 16\nquad0low:\n    dd\t1\nquad0high:\n    dd\t2\nquad1low:\n    dd\t-1234567\nquad1high:\n    dd\t0\nmyaddress:\n    dd\t0xdeadbeef\n%include \"header.inc\"\n\n\tcvtpi2ps\txmm0, [quad0low]\n\t; fill xmm1 in order to ensure that the high quadword remain inchanged\n\tpshufd\t\txmm1, xmm0, 0\n\tcvtpi2ps\txmm1, [quad1low]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/cvtps2pi.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nfloat0low:\n\tdd\t12345.678\nfloat0high:\n\tdd\t1234.5\nfloat1low:\n\tdd\t0x80000001.0\nfloat1high:\n\tdd\t-2147483130.0\nmxcsr:\n\tdd\t0\n\n; Set mxcsr regiter rouding bits\n%macro  setRoundingBits 1\n\tstmxcsr\t\t[mxcsr]\n\tmov\t\t\teax, [mxcsr]\n\tand \t\tax, 0x9F80\n\tor\t\t\tah, %1\n\tmov\t\t\t[mxcsr], eax\n\tldmxcsr\t\t[mxcsr]\n%endmacro\n\n%include \"header.inc\"\n\n\tsetRoundingBits 0x00 ; Round to nearest\n\tcvtps2pi\tmm0, [float0low]\n\tcvtps2pi\tmm4, [float1low]\n\tsetRoundingBits 0x20 ; Round down\n\tcvtps2pi\tmm1, [float0low]\n\tcvtps2pi\tmm5, [float1low]\n\tsetRoundingBits 0x40 ; Round up\n\tcvtps2pi\tmm2, [float0low]\n\tcvtps2pi\tmm6, [float1low]\n\tsetRoundingBits 0x60 ; Round toward zero\n\tcvtps2pi\tmm3, [float0low]\n\tcvtps2pi\tmm7, [float1low]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/cvttps2pi.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nfloat0low:\n\tdd\t2147483647.0\nfloat0high:\n\tdd\t-2147483648.0\nfloat1low:\n\tdd\t1235.678\nfloat1high:\n\tdd\t1325400064\nfloat2low:\n\tdd\t-54.321\nfloat2high:\n\tdd\t-12345.6\nfloat3low:\n\tdd\t123.456\nfloat3high:\n\tdd\t1234.5678\nmyaddress:\n\tdd\t0xdeadbeef\n%include \"header.inc\"\n\n    movaps\t    xmm0, [float0low]\n    cvttps2pi\tmm0, xmm0\n\tcvttps2pi\tmm1, [float1low]\n\tcvttps2pi\tmm2, [float2low]\n\tcvttps2pi\tmm3, [float3low]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/emms.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyquad:\n\tdq\t0xad0000ceadad00ff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\temms\n        ; TODO: Check tag word\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/f2xm1.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldz\n    f2xm1\n\n    fld1\n    f2xm1\n\n    fld1\n    fchs\n    f2xm1\n\n    fldln2\n    f2xm1\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fchs.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldz\n    fchs\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fdecstp.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fdecstp\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fdiv-zero.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    push 1234\n    fild dword [esp]\n    push 0\n    fild dword [esp]\n    fdiv\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fdiv.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldz\n    fldz\n    fdiv\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fdivr.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldz\n    fldz\n    fdivr\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fincstp.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fincstp\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/footer.inc",
    "content": "\nloop:\n       hlt\n       jmp     loop\n"
  },
  {
    "path": "tests/nasm/fprem.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fld1\n    fldz\n    fprem\n\n    fld1\n    fldpi\n    fprem\n\n    fld1\n    fldl2t\n    fprem\n\n    fldz\n    fldz\n    fprem\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fprem1.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fld1\n    fldz\n    fprem1\n\n    fld1\n    fldpi\n    fprem1\n\n    fld1\n    fldl2t\n    fprem1\n\n    fldz\n    fldz\n    fprem1\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fptan.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldpi\n    fptan\n\n    fldpi\n    fldpi\n    fpatan\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fpu_m80.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    push 1\n    push 0\n    push 0\n    fld tword [esp-8]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/frndint.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldz\n    frndint\n\n    fldpi\n    frndint\n\n    fldl2t\n    frndint\n\n    fldln2\n    frndint\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fsave_frstor.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    sub esp, 128\n    fldz\n    fld1\n    fsave [esp]\n    frstor [esp]\n    mov dword [esp + 12], 0 ; fpu eip (currently not emulated)\n    mov dword [esp + 16], 0 ; fpu cs/opcode (currently not emulated)\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fscale.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldpi\n    fldpi\n    fscale\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fsincos.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldpi\n    fsincos\n    fldz\n    fsincos\n\n    fldpi\n    fsin\n    fldz\n    fsin\n\n    fldpi\n    fcos\n    fldz\n    fcos\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fsqrt.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fldpi\n    fsqrt\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fstenv.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    fstenv [esp]\n\n    ; zero undefined fields\n    mov word [esp + 0 + 2], 0\n    mov word [esp + 4 + 2], 0\n    mov word [esp + 8 + 2], 0\n    mov word [esp + 24 + 2], 0\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fstm80.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fstcw [esp]\n    and word [esp], ~0x300\n    or word [esp], 0x200\n    fldcw [esp]\n\n    fldpi\n    fld1\n    fmul\n    fstp tword [esp]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fstsw.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov eax, 12345678h\n    fstsw ax\n    push 87654321h\n    fstsw [esp]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fxtract-zero.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    push 0\n    fild dword [esp]\n    fxtract\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fxtract.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    push 12345\n    fild dword [esp]\n    fxtract\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fyl2x-zero.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    push 1234\n    fild dword [esp]\n    push 0\n    fild dword [esp]\n    fyl2x\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fyl2x.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fld1\n    fldpi\n    fyl2x\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/fyl2xp1.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    fld1\n    fldpi\n    fyl2xp1\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/gdb-extract-def",
    "content": "# Invocation: gdb -x gdb-extract-def\n\n# extract-state /path/to/foo.bin /path/to/foo.fixture\ndefine extract-state\n  file $arg0\n\n  # Disables logging to stdout - only log to file\n  set logging redirect on\n\n  set logging file $arg1\n  set logging overwrite on\n  set logging on\n\n  run\n\n  printf \"---BEGIN JSON---\\n\"\n  printf \"[\\n\"\n  printf \"    %d,\\n\", $eax\n  printf \"    %d,\\n\", $ecx\n  printf \"    %d,\\n\", $edx\n  printf \"    %d,\\n\", $ebx\n  printf \"    %d,\\n\", $esp\n  printf \"    %d,\\n\", $ebp\n  printf \"    %d,\\n\", $esi\n  printf \"    %d,\\n\", $edi\n  printf \"\\n\"\n\n  printf \"    %d,\\n\", $eip\n  printf \"\\n\"\n\n  # For fpu registers, check the tag register first. If the tag index is\n  # invalid and you try to access to corresponding register, gdb exits with an\n  # error.\n  set $top = $fstat >> 11 & 7\n  if ($ftag >> (2 * (($top + 0) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st0\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 1) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st1\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 2) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st2\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 3) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st3\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 4) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st4\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 5) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st5\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 6) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st6\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  if ($ftag >> (2 * (($top + 7) & 7)) & 3) != 2\n    printf \"    %.100e,\\n\", $st7\n  else\n    printf \"    \\\"invalid\\\",\\n\"\n  end\n  printf \"\\n\"\n\n  printf \"    %d,\\n\", $mm0.v2_int32[0]\n  printf \"    %d,\\n\", $mm0.v2_int32[1]\n  printf \"    %d,\\n\", $mm1.v2_int32[0]\n  printf \"    %d,\\n\", $mm1.v2_int32[1]\n  printf \"    %d,\\n\", $mm2.v2_int32[0]\n  printf \"    %d,\\n\", $mm2.v2_int32[1]\n  printf \"    %d,\\n\", $mm3.v2_int32[0]\n  printf \"    %d,\\n\", $mm3.v2_int32[1]\n  printf \"    %d,\\n\", $mm4.v2_int32[0]\n  printf \"    %d,\\n\", $mm4.v2_int32[1]\n  printf \"    %d,\\n\", $mm5.v2_int32[0]\n  printf \"    %d,\\n\", $mm5.v2_int32[1]\n  printf \"    %d,\\n\", $mm6.v2_int32[0]\n  printf \"    %d,\\n\", $mm6.v2_int32[1]\n  printf \"    %d,\\n\", $mm7.v2_int32[0]\n  printf \"    %d,\\n\", $mm7.v2_int32[1]\n  printf \"\\n\"\n\n  printf \"    %d,\\n\", $xmm0.v4_int32[0]\n  printf \"    %d,\\n\", $xmm0.v4_int32[1]\n  printf \"    %d,\\n\", $xmm0.v4_int32[2]\n  printf \"    %d,\\n\", $xmm0.v4_int32[3]\n  printf \"    %d,\\n\", $xmm1.v4_int32[0]\n  printf \"    %d,\\n\", $xmm1.v4_int32[1]\n  printf \"    %d,\\n\", $xmm1.v4_int32[2]\n  printf \"    %d,\\n\", $xmm1.v4_int32[3]\n  printf \"    %d,\\n\", $xmm2.v4_int32[0]\n  printf \"    %d,\\n\", $xmm2.v4_int32[1]\n  printf \"    %d,\\n\", $xmm2.v4_int32[2]\n  printf \"    %d,\\n\", $xmm2.v4_int32[3]\n  printf \"    %d,\\n\", $xmm3.v4_int32[0]\n  printf \"    %d,\\n\", $xmm3.v4_int32[1]\n  printf \"    %d,\\n\", $xmm3.v4_int32[2]\n  printf \"    %d,\\n\", $xmm3.v4_int32[3]\n  printf \"    %d,\\n\", $xmm4.v4_int32[0]\n  printf \"    %d,\\n\", $xmm4.v4_int32[1]\n  printf \"    %d,\\n\", $xmm4.v4_int32[2]\n  printf \"    %d,\\n\", $xmm4.v4_int32[3]\n  printf \"    %d,\\n\", $xmm5.v4_int32[0]\n  printf \"    %d,\\n\", $xmm5.v4_int32[1]\n  printf \"    %d,\\n\", $xmm5.v4_int32[2]\n  printf \"    %d,\\n\", $xmm5.v4_int32[3]\n  printf \"    %d,\\n\", $xmm6.v4_int32[0]\n  printf \"    %d,\\n\", $xmm6.v4_int32[1]\n  printf \"    %d,\\n\", $xmm6.v4_int32[2]\n  printf \"    %d,\\n\", $xmm6.v4_int32[3]\n  printf \"    %d,\\n\", $xmm7.v4_int32[0]\n  printf \"    %d,\\n\", $xmm7.v4_int32[1]\n  printf \"    %d,\\n\", $xmm7.v4_int32[2]\n  printf \"    %d,\\n\", $xmm7.v4_int32[3]\n  printf \"\\n\"\n\n\n  set $addr=0x100000\n  while($addr < 0x102000)\n     printf \"    %d, %d, %d, %d, %d, %d, %d, %d,\\n\", *(int*)($addr+0), *(int*)($addr+4), *(int*)($addr+8), *(int*)($addr+12), *(int*)($addr+16), *(int*)($addr+20), *(int*)($addr+24), *(int*)($addr+28)\n     set $addr=$addr+32\n  end\n  printf \"\\n\"\n\n  printf \"    %d,\\n\", $eflags\n  printf \"    %d,\\n\", $ftag\n  printf \"    %d\\n\", $fstat\n\n  printf \"]\\n\"\n  printf \"---END JSON---\\n\"\n\n  set logging off\n\nend\n"
  },
  {
    "path": "tests/nasm/gen_fixtures.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\nimport assert from \"node:assert/strict\";\nimport os from \"node:os\";\nimport { spawn, spawnSync } from \"node:child_process\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst DEBUG = process.env.DEBUG || false;\n// Maximum number of gdb processes to spawn in parallel\nconst MAX_PARALLEL_PROCS = +process.env.MAX_PARALLEL_PROCS || 32;\n// Default to true for now. It's slower, but async execution occasionally gets stuck\nconst SYNC_GDB_EXECUTION = process.env.SYNC_GDB_EXECUTION || true;\n\n// Usage: console.log(CYAN_FMT, \"This shows up in cyan!\")\nconst CYAN_FMT = \"\\x1b[36m%s\\x1b[0m\";\nconst YELLOW_FMT = \"\\x1b[33m%s\\x1b[0m\";\n\nconst TEST_DIR = __dirname + \"/\";\nconst BUILD_DIR = path.join(TEST_DIR, \"build\");\n\nconst GDB_DEFAULT_ARGS = [\n    \"-batch\",\n    \"--eval-command=set disable-randomization off\", // allow execution on docker\n    `--command=${TEST_DIR}gdb-extract-def`,\n    // Set a breakpoint \"in the future\", which all the test binaries can then share\n    \"--eval-command=set breakpoint pending on\",\n    \"--eval-command=break loop\",\n    \"--eval-command=catch signal SIGFPE\",\n    \"--eval-command=catch signal SIGILL\",\n    \"--eval-command=catch signal SIGSEGV\",\n    \"--eval-command=catch signal SIGBUS\",\n];\n\n/* Split up an array into semi-evenly sized chunks */\nfunction chunk(source, num_chunks)\n{\n    const arr = source.slice();\n    const ret = [];\n\n    let rem_chunks = num_chunks;\n    while(rem_chunks > 0)\n    {\n        // We guarantee that the entire array is processed because when rem_chunk=1 -> len/1 = len\n        ret.push(arr.splice(0, Math.floor(arr.length / rem_chunks)));\n        rem_chunks--;\n    }\n    return ret;\n}\nassert(\n    JSON.stringify(chunk(\"0 0 1 1 2 2 2 3 3 3\".split(\" \"), 4)) ===\n        JSON.stringify([[\"0\", \"0\"],\n                        [\"1\", \"1\"],\n                        [\"2\", \"2\", \"2\"],\n                        [\"3\", \"3\", \"3\"]]),\n    \"Chunk\"\n);\n\nconst dir_files = fs.readdirSync(BUILD_DIR);\nconst test_files = dir_files.filter(name => {\n    return name.endsWith(\".img\");\n}).map(name => {\n    return name.slice(0, -4);\n}).filter(name => {\n    const bin_file = path.join(BUILD_DIR, `${name}.img`);\n    const fixture_file = path.join(BUILD_DIR, `${name}.fixture`);\n    if(!fs.existsSync(fixture_file))\n    {\n        return true;\n    }\n    return fs.statSync(bin_file).mtime > fs.statSync(fixture_file).mtime;\n});\n\nconst nr_of_cpus = Math.min(\n    os.cpus().length || 1,\n    test_files.length,\n    MAX_PARALLEL_PROCS\n);\n\nif(SYNC_GDB_EXECUTION)\n{\n    console.log(\"[+] Generating %d fixtures\", test_files.length);\n}\nelse\n{\n    console.log(\"[+] Using %d cpus to generate %d fixtures\", nr_of_cpus, test_files.length);\n}\n\nconst workloads = chunk(test_files, nr_of_cpus);\n\nfunction test_arg_formatter(workload)\n{\n    return workload.map(test => {\n        const test_path = path.join(BUILD_DIR, test);\n        return `--eval-command=extract-state ${test_path}.img ${test_path}.fixture`;\n    });\n}\n\nfunction set_proc_handlers(proc, n)\n{\n    proc.on(\"close\", (code) => on_proc_close(code, n));\n\n    if(DEBUG)\n    {\n        proc.stdout.on(\"data\", (data) => {\n            console.log(CYAN_FMT, \"stdout\", `${n}: ${data}`);\n        });\n\n        proc.stderr.on(\"data\", (data) => {\n            console.log(YELLOW_FMT, \"stderr\", `${n}: ${data}`);\n        });\n    }\n}\n\nfunction on_proc_close(code, n)\n{\n    console.log(`[+] child process ${n} exited with code ${code}`);\n    if(code !== 0)\n    {\n        process.exit(code);\n    }\n}\n\nfor(let i = 0; i < nr_of_cpus; i++)\n{\n    const gdb_args = GDB_DEFAULT_ARGS.concat(test_arg_formatter(workloads[i]));\n\n    if(DEBUG)\n    {\n        console.log(CYAN_FMT, \"[DEBUG]\", \"gdb\", gdb_args.join(\" \"));\n    }\n\n    if(SYNC_GDB_EXECUTION || nr_of_cpus === 1)\n    {\n        const { status: code } = spawnSync(\"gdb\", gdb_args);\n        on_proc_close(code, i);\n    }\n    else\n    {\n        const gdb = spawn(\"gdb\", gdb_args);\n        set_proc_handlers(gdb, i);\n    }\n}\n"
  },
  {
    "path": "tests/nasm/header.inc",
    "content": "MBALIGN     equ  1<<0                   ; align loaded modules on page boundaries\nMEMINFO     equ  1<<1                   ; provide memory map\nFLAGS       equ  0                      ; this is the Multiboot 'flag' field\nMAGIC       equ  0x1BADB002             ; 'magic number' lets bootloader find the header\nCHECKSUM    equ -(MAGIC + FLAGS)        ; checksum of above, to prove we are multiboot\nsection .multiboot\nalign 4\n    dd MAGIC\n    dd FLAGS\n    dd CHECKSUM\n\nsection .bss\n    resb 2*4096   ; 0x2000\n\nstack_top:\n\nsection .text\n\n_start:\nmain:\n        xor eax, eax\n        xor ecx, ecx\n        xor edx, edx\n        xor ebx, ebx\n        mov esp, stack_top\n        xor ebp, ebp\n        xor esi, esi\n        xor edi, edi\n\n        pxor xmm0, xmm0\n\n        ; make space for memory operations\n        sub esp, 32\n\n        push 0\n        popf\n"
  },
  {
    "path": "tests/nasm/idiv16-overflow.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ax, 0000h\n    mov dx, 8000h\n    mov bx, -1\n    idiv bx\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/idiv32-overflow.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov eax, 00000000h\n    mov edx, 80000000h\n    mov ebx, -1\n    idiv ebx\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/idiv8-overflow.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ax, 8000h\n    mov bl, -1\n    idiv bl\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/indirect-call.asm",
    "content": "global _start\n\nsection .data\n\n%include \"header.inc\"\n\n    mov eax, foo\n    call eax\nfoo:\n    xor eax, eax\n    ; clear stack (pushed eip is not the same between vm and gdb execution)\n    mov dword [esp], 0\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/indirect-jump.asm",
    "content": "global _start\n\nsection .data\n\n%include \"header.inc\"\n\n    mov eax, foo\n    jmp eax\nfoo:\n    xor eax, eax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/jcxz.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ecx, 0x10000\n    jecxz cont1\n    or eax, 1\ncont1:\n\n    mov ecx, 0\n    jecxz cont2\n    or eax, 2\ncont2:\n\n    mov ecx, 0x1\n    jcxz cont3\n    or eax, 4\ncont3:\n\n    mov ecx, 0x10000\n    jcxz cont4\n    or eax, 8\ncont4:\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/jump.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    mov eax, 0\n    mov ebx, 0\n    mov ecx, 0\n    mov edx, 0\n    mov esi, 0\n    mov edi, 0\n\n    ; skip\n    jmp .target1\n    inc eax\n.target1:\n\n    ; conditional jump up\n.target2:\n    inc ebx\n    inc ecx\n    cmp ebx, 2\n    jne .target2\n\n    ; conditional jump down\n.target3:\n    cmp ebx, 4\n    je .target4\n    inc ebx\n    inc edx\n    jmp .target3\n\n.target4:\n    call .fun\n    call .not_returning_fun\n.after_call:\n    jmp .after_fun\n\n.fun:\n    inc esi\n    ret\n\n.not_returning_fun:\n    inc esi\n    jmp .after_call\n    inc esi\n    ret\n\n.after_fun:\n    push .target5\n    ret\n.target5:\n\n    ; clear stack (pushed eip is not the same between vm and gdb execution)\n    mov dword [esp], 0\n    mov dword [esp-4], 0\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/lea-nop.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    ; nops\n    lea edx, [edx]\n    db 8Dh, 40h, 00h\n    db 8Dh, 0B6h, 00h, 00h, 00h, 00h\n    db 8Dh, 0BCh, 27h, 00h, 00h, 00h, 00h\n\n    ; non-nops, but similar encodings\n    lea eax, [bx+si]\n    lea cx, [bx+di]\n    lea edx, [edx+42]\n    lea ebp, [ebp*2]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/leave16.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    lea ebp, [esp+10h]\n    mov dword [ebp], 123456789\n    db 0x66\n    leave\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/leave32.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    lea ebp, [esp+10h]\n    mov dword [ebp], 123456789\n    leave\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/loop.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ecx, 0x10042\n    mov eax, 0\nstart1:\n    inc eax\n    loop start1\n\n    mov ecx, 0x10005\n    mov ebx, 0\nstart2:\n    inc ebx\n    db 67h\n    loop start2\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/loopnz.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ecx, 0x10042\n    mov eax, 42\nstart1:\n    dec eax\n    loopz start1\n\n    mov ecx, 0x10005\n    mov ebx, 51\nstart2:\n    dec ebx\n    db 67h\n    loopz start2\n\n    mov ecx, 0x10005\nstart3:\n    or edx, 1\n    db 67h\n    loopz start3\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/loopz.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov ecx, 0x10042\n    mov eax, -1\nstart1:\n    inc eax\n    loopz start1\n\n    mov ecx, 0x10005\n    mov ebx, -1\nstart2:\n    inc ebx\n    db 67h\n    loopz start2\n\n    mov ecx, 0x10005\nstart3:\n    xor edx, edx\n    db 67h\n    loopz start3\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/maskmovdqu.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\ndquad0:\n\tdq\t0x1234567890abcdef\n\tdq\t0xfedcba0987654321\ndquad1:\n\tdq\t0xffffffffffffffff\n\tdq\t0xffffffffffffffff\nmask0:\n\tdq\t0x8081828384858687\n\tdq\t0x88898a8b8c8d8e8f\nmask1:\n\tdq\t0x10203080405080ff\n\tdq\t0x1234567890abcdef\n\n%include \"header.inc\"\n\n\tmovdqu\txmm0, [dquad0]\n\tmovdqu\txmm1, [dquad1]\n\tmovdqu\txmm2, [dquad0]\n\tmovdqu\txmm3, [dquad1]\n\tmovdqu\txmm6, [mask0]\n\tmovdqu\txmm7, [mask1]\n\n\t;; Look out for size of extracted memory region\n\tmov esp, stack_top - 16\n\n\tmov\t\tedi, esp\n\tmaskmovdqu\txmm0, xmm6\n\tsub\t\tesp, 16\n\n\tmov\t\tedi, esp\n\tmaskmovdqu\txmm1, xmm6\n\tsub\t\tesp, 16\n\n\tmov\t\tedi, esp\n\tmaskmovdqu\txmm2, xmm7\n\tsub\t\tesp, 16\n\n\tmov\t\tedi, esp\n\tmaskmovdqu\txmm3, xmm7\n\tsub\t\tesp, 16\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/maskmovq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\nquad0:\n\tdq\t0x1234567890abcdef\nquad1:\n\tdq\t0xffffffffffffffff\nmask0:\n\tdq\t0x8080808080808080\nmask1:\n\tdq\t0x10203080405080ff\n\n%include \"header.inc\"\n\n\tmovq\tmm0, [quad0]\n\tmovq\tmm1, [quad1]\n\tmovq\tmm2, [quad0]\n\tmovq\tmm3, [quad1]\n\tmovq\tmm6, [mask0]\n\tmovq\tmm7, [mask1]\n\n\t;; Look out for size of extracted memory region\n\tmov esp, stack_top - 16\n\n\tmov\t\tedi, esp\n\tmaskmovq\tmm0, mm6\n\tsub\t\tesp, 8\n\n\tmov\t\tedi, esp\n\tmaskmovq\tmm1, mm6\n\tsub\t\tesp, 8\n\n\tmov\t\tedi, esp\n\tmaskmovq\tmm2, mm7\n\tsub\t\tesp, 8\n\n\tmov\t\tedi, esp\n\tmaskmovq\tmm3, mm7\n\tsub\t\tesp, 8\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/mov16.asm",
    "content": ";;; Test JIT optimization of opcodes 0x89 and 0x8b\n\nglobal _start\n\nsection .data\n    align 16\nmydword:\n    dd  0xcafebabe\nmyaddress:\n    dd  0xdeadbeef\n\n%include \"header.inc\"\n\n    ;; Load 32-bit values to confirm that the 16-bit movs do not overwrite existing values here\n    mov eax, 0xcafeb055\n    mov esi, 0x1bada551\n\n    mov ecx, [mydword]\n    mov edx, [myaddress]\n\n    mov [myaddress], cx\n    mov [mydword], dx\n\n    ;; The following db's are used since mov reg, reg can be accomplished with several opcodes but\n    ;; we want to test these specific ones\n\n    ;; mov cx, si\n    db 0x66\n    db 0x89\n    db 0xf1\n    ;; mov dx, di\n    db 0x66\n    db 0x89\n    db 0xfa\n\n    ;; mov dx, ax\n    db 0x66\n    db 0x8b\n    db 0xd0\n    ;; mov ax, cx\n    db 0x66\n    db 0x8b\n    db 0xc1\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/mov_sreg.asm",
    "content": "global _start\n\nsection .data\n    align 16\nmydword:\n    dd  0\n\n%include \"header.inc\"\n\n    ; 32-bit register move should set higher bits to zero\n    mov eax, -1\n    mov eax, ss\n    and eax, 0xffff0000\n\n    mov ebx, -1\n    db 66h\n    mov ebx, ss\n    and ebx, 0xffff0000\n\n    ; 32-bit memory move should preserver higher bits\n    mov dword [mydword], 0xdeadbeef\n    mov [mydword], ss\n    mov ecx, [mydword]\n    and ecx, 0xffff0000\n\n    mov dword [mydword+4], 0xdeadbeef\n    db 66h\n    mov [mydword+4], ss\n    mov edx, [mydword+4]\n    and edx, 0xffff0000\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/mov_sreg_ud1.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    db 8eh\n    db 3fh\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/mov_sreg_ud2.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    db 8ch\n    db 3fh\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/movaps.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyfloat0:\n\tdd\t1.234567e20\nmyfloat1:\n\tdd\t2.345678e20\nmyfloat2:\n\tdd\t3.456789e20\nmyfloat3:\n\tdd\t4.567891e20\nmyaddress:\n\tdd\t0xdeadbeef\n%include \"header.inc\"\n\n\tmovaps\txmm0, [myfloat0]\n    movaps\t[myaddress], xmm0\n    movaps\txmm1, [myaddress]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/movd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyquad:\n\tdq\t0x1234567890abcdef\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdd\t0xdeadbeef\n\n%include \"header.inc\"\n\n\n\tmovd\tmm0, [mydword]\n\tmovd\t[myaddress], mm0\n\tmovd\tmm1, [myaddress]\n\tmovd\teax, mm0\n\tmovd\tmm4, eax\n\tmov     eax, 0x42\n\tmovd    mm6, eax\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/movntpd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyfloat0:\n\tdd\t1.234567e20\nmyfloat1:\n\tdd\t2.345678e20\nmyfloat2:\n\tdd\t3.456789e20\nmyfloat3:\n\tdd\t4.567891e20\nmyaddress:\n\tdd\t0xdeadbeef\n%include \"header.inc\"\n\n\tmovapd\txmm0, [myfloat0]\n    movntpd\t[myaddress], xmm0\n    movapd\txmm1, [myaddress]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/movntps.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyfloat0:\n\tdd\t1.234567e20\nmyfloat1:\n\tdd\t2.345678e20\nmyfloat2:\n\tdd\t3.456789e20\nmyfloat3:\n\tdd\t4.567891e20\nmyaddress:\n\tdd\t0xdeadbeef\n%include \"header.inc\"\n\n\tmovapd\txmm0, [myfloat0]\n    movntps\t[myaddress], xmm0\n    movapd\txmm1, [myaddress]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/movq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyquad:\n\tdq\t0x1234567890abcdef\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdd\t0xdeadbeef\n\n%include \"header.inc\"\n\n\tmovq\tmm0, [myquad]\n\tmovq\t[myaddress], mm0\n\tmovq\tmm1, [myaddress]\n\tmovq\tmm4, mm0\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/packssdw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0xffffffff0fffffff\nquad2:\n\tdq\t0x0000abcd80000000\nquad3:\n\tdq\t0xaaaaaaaaffffff00\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpackssdw\tmm0, mm2\n\tpackssdw\tmm0, [quad1]\n\tpackssdw\tmm1, [quad3]\n\tpackssdw\tmm2, [quad1]\n\tpackssdw\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/packsswb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x00ad00adad007fff\nquad2:\n\tdq\t0x7fff00428000ffff\nquad3:\n\tdq\t0x01008080f0f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpacksswb\tmm0, [quad3]\n\tpacksswb\tmm0, [quad1]\n\tpacksswb\tmm1, [quad3]\n\tpacksswb\tmm2, [quad1]\n\tpacksswb\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/packuswb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0xad0000ceadad00ff\nquad2:\n\tdq\t0xffffffffffffffff\nquad3:\n\tdq\t0x0000000000000000\nquad4:\n\tdq\t0x7fff8000ffff0808\nmyquad:\n\tdq\t0x00ad00adad007fff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\tmovq\t\tmm5, [quad2]\n\tmovq\t\tmm6, [quad3]\n\tmovq\t\tmm7, [quad4]\n\n\tpackuswb\tmm4, mm0\n\tpackuswb\tmm5, mm1\n\tpackuswb\tmm6, mm2\n\tpackuswb\tmm7, mm3\n\tpackuswb\tmm0, [quad2]\n\tpackuswb\tmm1, [quad3]\n\tpackuswb\tmm2, [quad4]\n\tpackuswb\tmm3, [quad1]\n\tpackuswb\tmm0, mm5\n\tpackuswb\tmm1, mm6\n\tpackuswb\tmm2, mm7\n\tpackuswb\tmm3, mm4\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffbaef\nquad2:\n\tdq\t0x71ae010f0f000dbe\nquad3:\n\tdq\t0xf100808080f0af42\nquad4:\n\tdq\t0xffffffffffffffff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\tmovq\t\tmm4, [quad4]\n\n\tpaddb\t\tmm0, [quad2]\n\tpaddb\t\tmm0, [quad1]\n\tpaddb\t\tmm1, mm2\n\tpaddb\t\tmm2, [quad1]\n\tpaddb\t\tmm3, [quad1]\n\tpaddb\t\tmm4, [quad4]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x00ad80ad0fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nquad4:\n\tdq\t0xffffffffffffffff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\tmovq\t\tmm4, [quad4]\n\n\tpaddd\t\tmm0, [quad2]\n\tpaddd\t\tmm0, [quad1]\n\tpaddd\t\tmm1, mm2\n\tpaddd\t\tmm2, [quad1]\n\tpaddd\t\tmm3, [quad1]\n\tpaddd\t\tmm4, [quad4]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddsb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpaddsb\t\tmm0, [quad2]\n\tpaddsb\t\tmm0, [quad1]\n\tpaddsb\t\tmm1, mm2\n\tpaddsb\t\tmm2, [quad1]\n\tpaddsb\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddsw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpaddsw\t\tmm0, [quad2]\n\tpaddsw\t\tmm0, [quad1]\n\tpaddsw\t\tmm1, mm2\n\tpaddsw\t\tmm2, [quad1]\n\tpaddsw\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddusb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpaddusb\tmm0, [quad2]\n\tpaddusb\tmm0, [quad1]\n\tpaddusb\tmm1, mm2\n\tpaddusb\tmm2, [quad1]\n\tpaddusb\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddusw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpaddusw\tmm0, [quad2]\n\tpaddusw\tmm0, [quad1]\n\tpaddusw\tmm1, mm2\n\tpaddusw\tmm2, [quad1]\n\tpaddusw\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/paddw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyquad:\n\tdq\t0xad0000ceadad00ff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [myquad]\n\tpaddw\t\tmm0, [myaddress]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pand.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0xad0000ceadad00ff\nquad2:\n\tdq\t0xffffffffffffffff\nquad3:\n\tdq\t0x0000000000000000\nquad4:\n\tdq\t0x7fff8000ffff0808\nmydword:\n\tdd\t0xcafebabe\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\n\tpand\t\tmm1, [quad2]\n\tpand\t\tmm1, mm2\n\tpand\t\tmm2, [quad1]\n\tpand\t\tmm2, mm3\n\tpand\t\tmm3, [quad3]\n\tpand\t\tmm3, mm4\n\tpand\t\tmm4, [quad1]\n\tpand\t\tmm4, mm1\n\tpand\t\tmm4, mm3\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pandn.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpandn\t\tmm0, [quad2]\n\tpandn\t\tmm0, [quad1]\n\tpandn\t\tmm1, mm2\n\tpandn\t\tmm2, [quad1]\n\tpandn\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pcmpeqb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x00ad00adad007fff\nquad2:\n\tdq\t0x7fff00428000ffff\nquad3:\n\tdq\t0x01008080f0f0ff42\nquad4:\n\tdq\t0x0000000000000000\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\n\tpcmpeqb\tmm1, [quad2]\n\tpcmpeqb\tmm1, mm2\n\tpcmpeqb\tmm2, [quad1]\n\tpcmpeqb\tmm2, mm3\n\tpcmpeqb\tmm3, [quad3]\n\tpcmpeqb\tmm3, mm4\n\tpcmpeqb\tmm4, [quad1]\n\tpcmpeqb\tmm4, mm1\n\tpcmpeqb\tmm4, mm3\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pcmpeqd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x70ad80ad7fffffff\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpcmpeqd\tmm0, [quad2]\n\tpcmpeqd\tmm0, [quad1]\n\tpcmpeqd\tmm1, mm2\n\tpcmpeqd\tmm2, [quad1]\n\tpcmpeqd\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pcmpeqw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80adad007fff\nquad2:\n\tdq\t0x7fff00428000ffff\nquad3:\n\tdq\t0x01008080f0f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpcmpeqw\tmm0, [quad2]\n\tpcmpeqw\tmm0, [quad1]\n\tpcmpeqw\tmm1, mm2\n\tpcmpeqw\tmm2, [quad1]\n\tpcmpeqw\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pcmpgtb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x00ad00adad007fff\nquad2:\n\tdq\t0x7fff00428000ffff\nquad3:\n\tdq\t0x01008080f0f0ff42\nquad4:\n\tdq\t0x0000000000000000\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\tmovq\t\tmm5, [quad2]\n\tmovq\t\tmm6, [quad3]\n\tmovq\t\tmm7, [quad4]\n\n\tpcmpgtb\tmm4, mm0\n\tpcmpgtb\tmm5, mm1\n\tpcmpgtb\tmm6, mm2\n\tpcmpgtb\tmm7, mm3\n\tpcmpgtb\tmm0, [quad2]\n\tpcmpgtb\tmm1, [quad3]\n\tpcmpgtb\tmm2, [quad4]\n\tpcmpgtb\tmm3, [quad1]\n\tpcmpgtb\tmm0, mm5\n\tpcmpgtb\tmm1, mm6\n\tpcmpgtb\tmm2, mm7\n\tpcmpgtb\tmm3, mm4\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pcmpgtd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x7fff00428000ffff\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpcmpgtd\tmm0, [quad2]\n\tpcmpgtd\tmm0, [quad1]\n\tpcmpgtd\tmm1, mm2\n\tpcmpgtd\tmm2, [quad1]\n\tpcmpgtd\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n\n"
  },
  {
    "path": "tests/nasm/pcmpgtw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80adad007fff\nquad2:\n\tdq\t0x7fff00428000ffff\nquad3:\n\tdq\t0x01008080f0f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpcmpgtw\tmm0, [quad2]\n\tpcmpgtw\tmm0, [quad1]\n\tpcmpgtw\tmm1, mm2\n\tpcmpgtw\tmm2, [quad1]\n\tpcmpgtw\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pmaddwd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nquad4:\n\tdq\t0x8000800080008000\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\tmovq\t\tmm4, [quad4]\n\n\tpmaddwd\tmm0, [quad2]\n\tpmaddwd\tmm0, [quad1]\n\tpmaddwd\tmm1, mm2\n\tpmaddwd\tmm2, [quad1]\n\tpmaddwd\tmm3, [quad1]\n\tpmaddwd\tmm4, [quad4]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pmulhw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpmulhw\t\tmm0, [quad2]\n\tpmulhw\t\tmm0, [quad1]\n\tpmulhw\t\tmm1, mm2\n\tpmulhw\t\tmm2, [quad1]\n\tpmulhw\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pmullw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpmullw\t\tmm0, [quad2]\n\tpmullw\t\tmm0, [quad1]\n\tpmullw\t\tmm1, mm2\n\tpmullw\t\tmm2, [quad1]\n\tpmullw\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pmuludq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\ndword1:\n\tdd\t0x00000002\ndword2:\n\tdd\t0xFFFFFF11\ndword3:\n\tdd\t0xFFF00000\ndword4:\n\tdd\t0x0000FFFF\ndword5:\n\tdd\t0xFFFFFFFF\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\nqword1:\n\tdq\t0xffffffff00000001\n%include \"header.inc\"\n\n\tmovd\t\tmm0, [dword1]\n\tmovd\t\tmm1, [dword2]\n\tmovd\t\tmm2, [dword1]\n\tmovd\t\tmm3, [dword2]\n\tmovd\t\tmm4, [dword4]\n\tmovd\t\tmm5, [dword5]\n\t\n\tpmuludq\tmm0, [mydword]\n\tpmuludq\tmm2, mm1\n\tpmuludq\tmm3, [dword3]\n\tpmuludq\tmm4, [dword3]\n\tpmuludq\tmm5, [dword5]\n\n\tmovd\t   xmm1, [dword5]\n\tpshufd     xmm1, xmm1, 0\n\tpmuludq    xmm1, xmm1\n\n\tmovd\t   xmm2, [dword4]\n\tpmuludq    xmm2, xmm1\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pop_esp.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    mov esp, stack_top-16\n    mov dword [esp], 55aaaa55h\n    pop dword [esp-12]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pop_esp2.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\n%include \"header.inc\"\n\n    ; pop esp encoded using 8F\n    mov esp, stack_top-16\n    db 8Fh, 0C4h\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/por.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpor\t\tmm0, [quad2]\n\tpor\t\tmm0, [quad1]\n\tpor\t\tmm1, mm2\n\tpor\t\tmm2, [quad1]\n\tpor\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pshufw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmyquad:\n\tdq\t0xad0000ceadad00ff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [myquad]\n\tpshufw\t\tmm0, [myaddress], 0xAB\n\tpshufw\t\tmm1, [myaddress], 0xFE\n\tpshufw\t\tmm2, [myquad], 0xFF\n\tpshufw\t\tmm6, [myaddress], 0x19\n\tpshufw\t\tmm7, [myaddress], 0xB5\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pslld.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmydq0:\n\tdq\t0xad0000ceadad00ff\n\tdq\t0xff00dadaec0000da\nmydq1:\n\tdq\t0x0102030405060708\n\tdq\t0x090a0b0c0d0e0f10\nmydword:\n\tdd\t0xcafebac0\nshift0:\n\tdq\t0x07\nshift1:\n\tdq\t-0x22\n\nalign 16\nshift2:\n\tdq\t0x07\n\tdq\t0\nshift3:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [mydq0]\n\tmovq\t\tmm1, [mydq0]\n\tmovq\t\tmm2, [mydq1]\n\tmovq\t\tmm6, [mydq1]\n\n\tmovq\t\txmm0, [mydq0]\n\tmovq\t\txmm1, [mydq0]\n\tmovq\t\txmm2, [mydq1]\n\tmovq\t\txmm6, [mydq1]\n\n\tpslld\t\tmm0, [shift0]\n\tpslld\t\tmm1, 30\n\tpslld\t\tmm2, [shift1]\n\tpslld\t\tmm6, 0x5\n\n\tpslld\t\txmm0, [shift2]\n\tpslld\t\txmm1, 30\n\tpslld\t\txmm2, [shift3]\n\tpslld\t\txmm6, 0x5\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psllq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\ndq0:\n\tdq\t0x0102030405060708\n\tdq\t0xffffaaaabbbbcccc\ndq1:\n\tdq\t0x8d0000ceadad00ff\n\tdq\t0x0123456789abcdef\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift1:\n\tdq\t0x07\n\tdq\t0\nshift2:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [dq0]\n\tmovq\t\tmm1, [dq0]\n\tmovq\t\tmm2, [dq0]\n\tmovq\t\tmm3, [dq1]\n\tmovq\t\tmm4, [dq1]\n\tmovq\t\tmm6, [dq1]\n\n\tmovq\t\txmm0, [dq0]\n\tmovq\t\txmm1, [dq0]\n\tmovq\t\txmm2, [dq0]\n\tmovq\t\txmm3, [dq1]\n\tmovq\t\txmm4, [dq1]\n\tmovq\t\txmm6, [dq1]\n\n\tpsllq\t\tmm0, [shift1]\n\tpsllq\t\tmm1, [shift2]\n\tpsllq\t\tmm2, 50\n\tpsllq\t\tmm3, 28\n\tpsllq\t\tmm4, 68\n\tpsllq\t\tmm6, 0x5\n\n\tpsllq\t\txmm0, [shift1]\n\tpsllq\t\txmm1, [shift2]\n\tpsllq\t\txmm2, 50\n\tpsllq\t\txmm3, 28\n\tpsllq\t\txmm4, 68\n\tpsllq\t\txmm6, 0x5\n\n%include \"footer.inc\"\n\n"
  },
  {
    "path": "tests/nasm/psllw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\ndq1:\n\tdq\t0xad0000ceadad00ff\n\tdq\t0xad0000ceadad00ff\ndq2:\n\tdq\t0x42ff88ff11aabbcc\n\tdq\t0x42ff88ff11aabbcc\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift1:\n\tdq\t0x07\n\tdq\t0\nshift2:\n\tdq\t-0x22\n\tdq\t0\nshift3:\n\tdq\t8\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [dq1]\n\tmovq\t\tmm1, [dq1]\n\tmovq\t\tmm2, [dq1]\n\tmovq\t\tmm3, [dq2]\n\tmovq\t\tmm4, [dq2]\n\tmovq\t\tmm6, [dq2]\n\n\tmovq\t\txmm0, [dq1]\n\tmovq\t\txmm1, [dq1]\n\tmovq\t\txmm2, [dq1]\n\tmovq\t\txmm3, [dq2]\n\tmovq\t\txmm4, [dq2]\n\tmovq\t\txmm6, [dq2]\n\n\tpsllw\t\tmm0, [shift1]\n\tpsllw\t\tmm1, 0xff\n\tpsllw\t\tmm2, [shift2]\n\tpsllw\t\tmm3, 12\n\tpsllw\t\tmm4, [shift3]\n\tpsllw\t\tmm6, 0x5\n\n\tpsllw\t\txmm0, [shift1]\n\tpsllw\t\txmm1, 0xff\n\tpsllw\t\txmm2, [shift2]\n\tpsllw\t\txmm3, 12\n\tpsllw\t\txmm4, [shift3]\n\tpsllw\t\txmm6, 0x5\n\n%include \"footer.inc\"\n\n"
  },
  {
    "path": "tests/nasm/psrad.asm",
    "content": "global _start\n\nsection .data\n\talign 16\ndq0:\n\tdq\t0x0102030405060708\n\tdq\t0xffffaaaabbbbcccc\ndq1:\n\tdq\t0x8d0000ceadad00ff\n\tdq\t0x0123456789abcdef\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift1:\n\tdq\t0x07\n\tdq\t0\nshift2:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [dq0]\n\tmovq\t\tmm1, [dq0]\n\tmovq\t\tmm2, [dq1]\n\tmovq\t\tmm6, [dq1]\n\n\tmovq\t\txmm0, [dq0]\n\tmovq\t\txmm1, [dq0]\n\tmovq\t\txmm2, [dq1]\n\tmovq\t\txmm6, [dq1]\n\n\tpsrad\t\tmm0, [shift1]\n\tpsrad\t\tmm1, 0x18\n\tpsrad\t\tmm2, [shift2]\n\tpsrad\t\tmm6, 0x5\n\n\tpsrad\t\txmm0, [shift1]\n\tpsrad\t\txmm1, 0x18\n\tpsrad\t\txmm2, [shift2]\n\tpsrad\t\txmm6, 0x5\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psraw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmydq0:\n\tdq\t0xad0000ceadad00ff\n\tdq\t0xff00dadaec0000da\nmydq1:\n\tdq\t0x0102030405060708\n\tdq\t0x090a0b0c0d0e0f10\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift0:\n\tdq\t0x07\n\tdq\t0\nshift1:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [mydq0]\n\tmovq\t\tmm1, [mydq0]\n\tmovq\t\tmm2, [mydq1]\n\tmovq\t\tmm6, [mydq1]\n\n\tmovq\t\txmm0, [mydq0]\n\tmovq\t\txmm1, [mydq0]\n\tmovq\t\txmm2, [mydq1]\n\tmovq\t\txmm6, [mydq1]\n\n\tpsraw\t\tmm0, [shift1]\n\tpsraw\t\tmm1, 18\n\tpsraw\t\tmm2, [shift0]\n\tpsraw\t\tmm6, 0x5\n\n\tpsraw\t\txmm0, [shift1]\n\tpsraw\t\txmm1, 18\n\tpsraw\t\txmm2, [shift0]\n\tpsraw\t\txmm6, 0x5\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psrld.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmydq0:\n\tdq\t0xad0000ceadad00ff\n\tdq\t0xff00dadaec0000da\nmydq1:\n\tdq\t0x0102030405060708\n\tdq\t0x090a0b0c0d0e0f10\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift0:\n\tdq\t0x07\n\tdq\t0\nshift1:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [mydq0]\n\tmovq\t\tmm1, [mydq0]\n\tmovq\t\tmm2, [mydq1]\n\tmovq\t\tmm6, [mydq1]\n\n\tmovq\t\txmm0, [mydq0]\n\tmovq\t\txmm1, [mydq0]\n\tmovq\t\txmm2, [mydq1]\n\tmovq\t\txmm6, [mydq1]\n\n\tpsrld\t\tmm0, [shift1]\n\tpsrld\t\tmm1, 0xff\n\tpsrld\t\tmm2, [shift0]\n\tpsrld\t\tmm6, 0x5\n\n\tpsrld\t\txmm0, [shift1]\n\tpsrld\t\txmm1, 0xff\n\tpsrld\t\txmm2, [shift0]\n\tpsrld\t\txmm6, 0x5\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psrlq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmydq0:\n\tdq\t0xad0000ceadad00ff\n\tdq\t0xff00dadaec0000da\nmydq1:\n\tdq\t0x0102030405060708\n\tdq\t0x090a0b0c0d0e0f10\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift0:\n\tdq\t0x07\n\tdq\t0\nshift1:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [mydq0]\n\tmovq\t\tmm1, [mydq0]\n\tmovq\t\tmm2, [mydq0]\n\tmovq\t\tmm3, [mydq1]\n\tmovq\t\tmm4, [mydq1]\n\tmovq\t\tmm6, [mydq1]\n\n\tmovq\t\txmm0, [mydq0]\n\tmovq\t\txmm1, [mydq0]\n\tmovq\t\txmm2, [mydq0]\n\tmovq\t\txmm3, [mydq1]\n\tmovq\t\txmm4, [mydq1]\n\tmovq\t\txmm6, [mydq1]\n\n\tpsrlq\t\tmm0, [shift1]\n\tpsrlq\t\tmm1, [shift0]\n\tpsrlq\t\tmm2, 0x65\n\tpsrlq\t\tmm3, 0x25\n\tpsrlq\t\tmm4, 0x1F\n\tpsrlq\t\tmm6, 0x5\n\n\tmovq\t\txmm0, [mydq0]\n\tmovq\t\txmm1, [mydq0]\n\tmovq\t\txmm2, [mydq1]\n\tmovq\t\txmm3, [mydq1]\n\tmovq\t\txmm6, [mydq1]\n\n\tpsrlq\t\txmm0, [shift1]\n\tpsrlq\t\txmm1, [shift0]\n\tpsrlq\t\txmm2, 0x65\n\tpsrlq\t\txmm3, 0x25\n\tpsrlq\t\txmm4, 0x1F\n\tpsrlq\t\txmm6, 0x5\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psrlw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nmydq0:\n\tdq\t0xad0000ceadad00ff\n\tdq\t0xff00dadaec0000da\nmydq1:\n\tdq\t0x0102030405060708\n\tdq\t0x090a0b0c0d0e0f10\nmydword:\n\tdd\t0xcafebac0\n\nalign 16\nshift0:\n\tdq\t0x07\n\tdq\t0\nshift1:\n\tdq\t-0x22\n\tdq\t0\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [mydq0]\n\tmovq\t\tmm1, [mydq0]\n\tmovq\t\tmm2, [mydq1]\n\tmovq\t\tmm3, [mydq1]\n\tmovq\t\tmm6, [mydq1]\n\n\tmovq\t\txmm0, [mydq0]\n\tmovq\t\txmm1, [mydq0]\n\tmovq\t\txmm2, [mydq1]\n\tmovq\t\txmm3, [mydq1]\n\tmovq\t\txmm6, [mydq1]\n\n\tpsrlw\t\tmm0, [shift1]\n\tpsrlw\t\tmm1, 30\n\tpsrlw\t\tmm2, [shift0]\n\tpsrlw\t\tmm3, 12\n\tpsrlw\t\tmm6, 0x5\n\n\tpsrlw\t\txmm0, [shift1]\n\tpsrlw\t\txmm1, 30\n\tpsrlw\t\txmm2, [shift0]\n\tpsrlw\t\txmm3, 12\n\tpsrlw\t\txmm6, 0x5\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nquad4:\n\tdq\t0x8000800080008000\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\tmovq\t\tmm4, [quad4]\n\n\tpsubb\t\tmm0, [quad2]\n\tpsubb\t\tmm0, [quad1]\n\tpsubb\t\tmm1, mm2\n\tpsubb\t\tmm2, [quad1]\n\tpsubb\t\tmm3, [quad1]\n\tpsubb\t\tmm4, [quad4]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nquad4:\n\tdq\t0xffffffffffffffff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\tmovq\t\tmm4, [quad4]\n\n\tpsubd\t\tmm0, [quad2]\n\tpsubd\t\tmm0, [quad1]\n\tpsubd\t\tmm1, mm2\n\tpsubd\t\tmm2, [quad1]\n\tpsubd\t\tmm3, [quad1]\n\tpsubd\t\tmm4, [quad4]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubsb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpsubsb\t\tmm0, [quad2]\n\tpsubsb\t\tmm0, [quad1]\n\tpsubsb\t\tmm1, mm2\n\tpsubsb\t\tmm2, [quad1]\n\tpsubsb\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubsw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpsubsw\t\tmm0, [quad2]\n\tpsubsw\t\tmm0, [quad1]\n\tpsubsw\t\tmm1, mm2\n\tpsubsw\t\tmm2, [quad1]\n\tpsubsw\t\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubusb.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpsubusb\tmm0, [quad2]\n\tpsubusb\tmm0, [quad1]\n\tpsubusb\tmm1, mm2\n\tpsubusb\tmm2, [quad1]\n\tpsubusb\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubusw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x70ad80ad7fffffff\nquad2:\n\tdq\t0x71ae01ff0f00ffbe\nquad3:\n\tdq\t0xf100808080f0ff42\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad2]\n\n\tpsubusw\tmm0, [quad2]\n\tpsubusw\tmm0, [quad1]\n\tpsubusw\tmm1, mm2\n\tpsubusw\tmm2, [quad1]\n\tpsubusw\tmm3, [quad1]\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/psubw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0xad0000ceadad00ff\nquad2:\n\tdq\t0xffffffffffffffff\nquad3:\n\tdq\t0x0000000000000000\nquad4:\n\tdq\t0x7fff8000ffff0808\nmyquad:\n\tdq\t0xad0000ceadad00ff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\n\tpsubw\t\tmm1, [quad2]\n\tpsubw\t\tmm1, mm2\n\tpsubw\t\tmm2, [quad1]\n\tpsubw\t\tmm2, mm3\n\tpsubw\t\tmm3, [quad3]\n\tpsubw\t\tmm3, mm4\n\tpsubw\t\tmm4, [quad1]\n\tpsubw\t\tmm4, mm1\n\tpsubw\t\tmm4, mm3\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/punpckhbw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\nquad1:\n\tdq\t0xccddccddad007fff\nquad2:\n\tdq\t0xaabbaabbad007fff\nquad3:\n\tdq\t0x00ff00ffad007fff\n\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad3]\n\tmovq\t\tmm2, [quad2]\n\tmovq\t\tmm3, [quad3]\n\n\tpunpckhbw\tmm0, [myaddress]\n\tpunpckhbw\tmm0, mm1\n\tpunpckhbw\tmm1, [quad2]\n\tpunpckhbw\tmm2, [quad1]\n\tpunpckhbw\tmm3, [quad1]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/punpckhdq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\nquad1:\n\tdq\t0xccddccddad007fff\nquad2:\n\tdq\t0xaabbaabbad007fff\nquad3:\n\tdq\t0x00ff00ffad007fff\n\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad3]\n\tmovq\t\tmm2, [quad2]\n\tmovq\t\tmm3, [quad3]\n\n\tpunpckhdq\tmm0, [myaddress]\n\tpunpckhdq\tmm0, mm1\n\tpunpckhdq\tmm1, [quad2]\n\tpunpckhdq\tmm2, [quad1]\n\tpunpckhdq\tmm3, [quad1]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/punpckhwd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\n\nquad1:\n\tdq\t0xccddccddad007fff\nquad2:\n\tdq\t0xaabbaabbad007fff\nquad3:\n\tdq\t0x00ff00ffad007fff\n\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad3]\n\tmovq\t\tmm2, [quad2]\n\tmovq\t\tmm3, [quad3]\n\n\tpunpckhwd\tmm0, [myaddress]\n\tpunpckhwd\tmm0, mm1\n\tpunpckhwd\tmm1, [quad2]\n\tpunpckhwd\tmm2, [quad1]\n\tpunpckhwd\tmm3, [quad1]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/punpcklbw.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0xad0000ceadad00ff\nquad2:\n\tdq\t0xffffffffffffffff\nquad3:\n\tdq\t0x0000000000000000\nquad4:\n\tdq\t0x7fff8000ffff0808\nmyquad:\n\tdq\t0x00ad00adad007fff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\tmovq\t\tmm5, [quad2]\n\tmovq\t\tmm6, [quad3]\n\tmovq\t\tmm7, [quad4]\n\n\tpunpcklbw\tmm4, mm0\n\tpunpcklbw\tmm5, mm1\n\tpunpcklbw\tmm6, mm2\n\tpunpcklbw\tmm7, mm3\n\tpunpcklbw\tmm0, [quad2]\n\tpunpcklbw\tmm1, [quad3]\n\tpunpcklbw\tmm2, [quad4]\n\tpunpcklbw\tmm3, [quad1]\n\tpunpcklbw\tmm0, mm5\n\tpunpcklbw\tmm1, mm6\n\tpunpcklbw\tmm2, mm7\n\tpunpcklbw\tmm3, mm4\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/punpckldq.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x00ad00adad007fff\nquad2:\n\tdq\t0xac4b1b9de29df0ff\nquad3:\n\tdq\t0x01008080f0f0ff42\nquad4:\n\tdq\t0x0123456789abcdef\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\tmovq\t\tmm5, [quad2]\n\tmovq\t\tmm6, [quad3]\n\tmovq\t\tmm7, [quad4]\n\n\tpunpckldq\tmm4, mm0\n\tpunpckldq\tmm5, mm1\n\tpunpckldq\tmm6, mm2\n\tpunpckldq\tmm7, mm3\n\tpunpckldq\tmm0, [quad2]\n\tpunpckldq\tmm1, [quad3]\n\tpunpckldq\tmm2, [quad4]\n\tpunpckldq\tmm3, [quad1]\n\tpunpckldq\tmm0, mm5\n\tpunpckldq\tmm1, mm6\n\tpunpckldq\tmm2, mm7\n\tpunpckldq\tmm3, mm4\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/punpcklwd.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0x00ad00adad007fff\nquad2:\n\tdq\t0xac4b1b9de29df0ff\nquad3:\n\tdq\t0x01008080f0f0ff42\nquad4:\n\tdq\t0x0123456789abcdef\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\tmovq\t\tmm5, [quad2]\n\tmovq\t\tmm6, [quad3]\n\tmovq\t\tmm7, [quad4]\n\n\tpunpcklwd\tmm4, mm0\n\tpunpcklwd\tmm5, mm1\n\tpunpcklwd\tmm6, mm2\n\tpunpcklwd\tmm7, mm3\n\tpunpcklwd\tmm0, [quad2]\n\tpunpcklwd\tmm1, [quad3]\n\tpunpcklwd\tmm2, [quad4]\n\tpunpcklwd\tmm3, [quad1]\n\tpunpcklwd\tmm0, mm5\n\tpunpcklwd\tmm1, mm6\n\tpunpcklwd\tmm2, mm7\n\tpunpcklwd\tmm3, mm4\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/push.asm",
    "content": "global _start\n\nsection .data\n    align 16\nmyaddress:\n    dd  0xdeadbeef\n\n%include \"header.inc\"\n\n    ;; push r/m - push edx\n    db      0xff\n    db      0xf2\n\n    ;; push r/m - push bx\n    db      0x66\n    db      0xff\n    db      0xf3\n\n    ;; push imm\n    push    0xdeadbeef\n    push    WORD 0xd00d\n\n    ;; push r/m - mem\n    push    DWORD [myaddress]\n    lea     eax, [myaddress]\n    push    WORD [eax]\n\n    ;; push reg\n    mov     ecx, 0xcafe\n    push    cx\n    push    ecx\n\n    xor     eax, eax\n\n    pop     ax\n    pop     eax\n    pop     cx\n    pop     ecx\n    pop     dx\n    pop     ebx\n    pop     si\n    pop     di\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pushf.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    pushf\n    and dword [esp], ~0x0200 ; if\n\n    db 66h\n    pushf\n    and dword [esp], ~0x0200\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/pxor.asm",
    "content": "global _start\n\nsection .data\n\talign 16\nquad1:\n\tdq\t0xad0000ceadad00ff\nquad2:\n\tdq\t0xffffffffffffffff\nquad3:\n\tdq\t0x0000000000000000\nquad4:\n\tdq\t0x7fff8000ffff0808\nmyquad:\n\tdq\t0xad0000ceadad00ff\nmydword:\n\tdd\t0xcafebabe\nmyaddress:\n\tdq\t0x00adbeefc0de00ce\n\n%include \"header.inc\"\n\n\tmovq\t\tmm0, [quad1]\n\tmovq\t\tmm1, [quad2]\n\tmovq\t\tmm2, [quad3]\n\tmovq\t\tmm3, [quad4]\n\tmovq\t\tmm4, [quad1]\n\tmovq\t\tmm5, [quad2]\n\tmovq\t\tmm6, [quad3]\n\tmovq\t\tmm7, [quad4]\n\n\tpxor\tmm4, mm0\n\tpxor\tmm5, mm1\n\tpxor\tmm6, mm2\n\tpxor\tmm7, mm3\n\tpxor\tmm0, [quad2]\n\tpxor\tmm1, [quad3]\n\tpxor\tmm2, [quad4]\n\tpxor\tmm3, [quad1]\n\tpxor\tmm0, mm5\n\tpxor\tmm1, mm6\n\tpxor\tmm2, mm7\n\tpxor\tmm3, mm4\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/rand.js",
    "content": "// From http://baagoe.com/en/RandomMusings/javascript/\n// Johannes Baagøe <baagoe@baagoe.com>, 2010\nfunction Mash() {\n  var n = 0xefc8249d;\n\n  var mash = function(data) {\n    data = data.toString();\n    for(var i = 0; i < data.length; i++) {\n      n += data.charCodeAt(i);\n      var h = 0.02519603282416938 * n;\n      n = h >>> 0;\n      h -= n;\n      h *= n;\n      n = h >>> 0;\n      h -= n;\n      n += h * 0x100000000; // 2^32\n    }\n    return (n >>> 0) * 2.3283064365386963e-10; // 2^-32\n  };\n\n  return mash;\n}\n\n// From http://baagoe.com/en/RandomMusings/javascript/\nexport default function KISS07() {\n  return (function(args) {\n    // George Marsaglia, 2007-06-23\n    //http://groups.google.com/group/comp.lang.fortran/msg/6edb8ad6ec5421a5\n    var x = 123456789;\n    var y = 362436069;\n    var z =  21288629;\n    var w =  14921776;\n    var c = 0;\n\n    if(args.length === 0) {\n      args = [+new Date];\n    }\n    var mash = Mash();\n    for(var i = 0; i < args.length; i++) {\n      x ^= mash(args[i]) * 0x100000000; // 2^32\n      y ^= mash(args[i]) * 0x100000000;\n      z ^= mash(args[i]) * 0x100000000;\n      w ^= mash(args[i]) * 0x100000000;\n    }\n    if(y === 0) {\n      y = 1;\n    }\n    c ^= z >>> 31;\n    z &= 0x7fffffff;\n    if((z % 7559) === 0) {\n      z++;\n    }\n    w &= 0x7fffffff;\n    if((w % 7559) === 0) {\n      w++;\n    }\n    mash = null;\n\n    var int32 = function() {\n      var t;\n\n      x += 545925293;\n      x >>>= 0;\n\n      y ^= y << 13;\n      y ^= y >>> 17;\n      y ^= y << 5;\n\n      t = z + w + c;\n      z = w;\n      c = t >>> 31;\n      w = t & 0x7fffffff;\n\n      return x + y + w | 0;\n    };\n    var uint32 = function() {\n      var t;\n\n      x += 545925293;\n      x >>>= 0;\n\n      y ^= y << 13;\n      y ^= y >>> 17;\n      y ^= y << 5;\n\n      t = z + w + c;\n      z = w;\n      c = t >>> 31;\n      w = t & 0x7fffffff;\n\n      return x + y + w >>> 0;\n    };\n\n    return {\n        int32,\n        uint32,\n    };\n  } (Array.prototype.slice.call(arguments)));\n}\n"
  },
  {
    "path": "tests/nasm/ret-imm.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    jmp start\nfoo:\n    mov eax, esp\n    ret 123\n\nstart:\n    call foo\n    mov dword [eax], 0 ; clear the address pushed by the call instruction\n\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/run.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\nimport assert from \"node:assert/strict\";\nimport os from \"node:os\";\nimport cluster from \"node:cluster\";\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\n// Mapping between signals and x86 exceptions:\n// \"Program received signal SIGILL, Illegal instruction.\" -> #UD (6)\n// \"Program received signal SIGFPE, Arithmetic exception.\" -> #DE (0)\n// to be determined -> #GP\n// to be determined -> #NM\n// to be determined -> #TS\n// to be determined -> #NP\n// to be determined -> #SS\n// to be determined -> #PF\n\n// A #UD might indicate a bug in the test generation\n\nconst MAX_PARALLEL_TESTS = +process.env.MAX_PARALLEL_TESTS || 99;\nconst TEST_NAME = new RegExp(process.env.TEST_NAME || \"\", \"i\");\nconst SINGLE_TEST_TIMEOUT = 10000;\n\nconst TEST_DIR = __dirname + \"/build/\";\nconst DONE_MSG = \"DONE\";\nconst TERMINATE_MSG = \"DONE\";\nconst READY_MSG = \"READY\";\n\nconst BSS = 0x100000;\nconst STACK_TOP = 0x102000;\n\nconst FORCE_JIT = process.argv.includes(\"--force-jit\");\n\n// alternative representation for infinity for json\nconst JSON_POS_INFINITY = \"+INFINITY\";\nconst JSON_NEG_INFINITY = \"-INFINITY\";\nconst JSON_POS_NAN = \"+NAN\";\nconst JSON_NEG_NAN = \"-NAN\";\n\nconst MASK_ARITH = 1 | 1 << 2 | 1 << 4 | 1 << 6 | 1 << 7 | 1 << 11;\nconst FPU_TAG_ALL_INVALID = 0xAAAA;\nconst FPU_STATUS_MASK = 0xFFFF & ~(1 << 9 | 1 << 5 | 1 << 3 | 1 << 1); // bits that are not correctly implemented by v86\nconst FP_COMPARISON_SIGNIFICANT_DIGITS = 7;\n\nfunction float_equal(x, y)\n{\n    assert(typeof x === \"number\");\n    assert(typeof y === \"number\");\n\n    if(x === Infinity && y === Infinity || x === -Infinity && y === -Infinity || isNaN(x) && isNaN(y))\n    {\n        return true;\n    }\n\n    const epsilon = Math.pow(10, -FP_COMPARISON_SIGNIFICANT_DIGITS);\n    return Math.abs(x - y) < epsilon;\n}\n\nfunction format_value(v)\n{\n    if(typeof v === \"number\")\n    {\n        if((v >>> 0) !== v && (v | 0) !== v)\n        {\n            return String(v);\n        }\n        else\n        {\n            return \"0x\" + (v >>> 0).toString(16);\n        }\n    }\n    else\n    {\n        return String(v);\n    }\n}\n\nif(cluster.isMaster)\n{\n    function extract_json(name, fixture_text)\n    {\n        let exception;\n\n        if(fixture_text.includes(\"(signal SIGFPE)\"))\n        {\n            exception = \"DE\";\n        }\n\n        if(fixture_text.includes(\"(signal SIGILL)\"))\n        {\n            exception = \"UD\";\n        }\n\n        if(fixture_text.includes(\"(signal SIGSEGV)\"))\n        {\n            exception = \"GP\";\n        }\n\n        if(fixture_text.includes(\"(signal SIGBUS)\"))\n        {\n            exception = \"PF\";\n        }\n\n        if(!exception && fixture_text.includes(\"Program received signal\"))\n        {\n            throw new Error(\"Test was killed during execution by gdb: \" + name + \"\\n\" + fixture_text);\n        }\n\n        fixture_text = fixture_text.toString()\n            .replace(/-inf\\b/g, JSON.stringify(JSON_NEG_INFINITY))\n            .replace(/\\binf\\b/g, JSON.stringify(JSON_POS_INFINITY))\n            .replace(/-nan\\b/g, JSON.stringify(JSON_NEG_NAN))\n            .replace(/\\bnan\\b/g, JSON.stringify(JSON_POS_NAN));\n\n        const json_regex = /---BEGIN JSON---([\\s\\[\\]\\.\\+\\w\":\\-,]*)---END JSON---/;\n        const regex_match = json_regex.exec(fixture_text);\n        if(!regex_match || regex_match.length < 2) {\n            throw new Error(\"Could not find JSON in fixture text: \" + fixture_text + \"\\nTest: \" + name);\n        }\n\n        let array = JSON.parse(regex_match[1]);\n        return {\n            array: array,\n            exception,\n        };\n    }\n\n\n    function send_work_to_worker(worker, message) {\n        if(current_test < tests.length) {\n            const test = tests[current_test];\n            worker.send(test);\n            current_test++;\n        }\n        else {\n            worker.send(TERMINATE_MSG);\n            worker.disconnect();\n\n            setTimeout(() => {\n                // The emulator currently doesn't cleanly exit, so this is necessary\n                console.log(\"Worker killed\");\n                worker.kill();\n            }, 100);\n\n            finished_workers++;\n            if(finished_workers === nr_of_cpus)\n            {\n                test_finished();\n            }\n        }\n    }\n\n    const dir_files = fs.readdirSync(TEST_DIR);\n    const files = dir_files.filter((name) => {\n        return name.endsWith(\".img\");\n    }).map(name => {\n        return name.slice(0, -4);\n    }).filter(name => {\n        return TEST_NAME.test(name + \".img\");\n    });\n\n    const tests = files.map(name => {\n        let fixture_name = name + \".fixture\";\n        let img_name = name + \".img\";\n        let fixture_text = fs.readFileSync(TEST_DIR + fixture_name);\n        let fixture = extract_json(name, fixture_text);\n\n        return {\n            img_name: img_name,\n            fixture: fixture,\n        };\n    });\n\n    const nr_of_cpus = Math.min(\n        os.cpus().length || 1,\n        tests.length,\n        MAX_PARALLEL_TESTS\n    );\n    console.log(\"Using %d cpus\", nr_of_cpus);\n\n    let current_test = 0;\n\n    let failed_tests = [];\n    let finished_workers = 0;\n\n    for(let i = 0; i < nr_of_cpus; i++)\n    {\n        let worker = cluster.fork();\n\n        worker.on(\"message\", function(message) {\n            if(message !== DONE_MSG && message !== READY_MSG) {\n                failed_tests.push(message);\n            }\n            send_work_to_worker(this);\n        });\n\n        worker.on(\"exit\", function(code, signal) {\n            if(code !== 0 &&  code !== null) {\n                console.log(\"Worker error code:\", code);\n                process.exit(code);\n            }\n        });\n\n        worker.on(\"error\", function(error) {\n            console.error(\"Worker error: \", error.toString(), error);\n            process.exit(1);\n        });\n    }\n\n    function test_finished()\n    {\n        console.log(\n            \"\\n[+] Passed %d/%d tests.\",\n            tests.length - failed_tests.length,\n            tests.length\n        );\n        if(failed_tests.length > 0) {\n            console.log(\"[-] Failed %d test(s).\", failed_tests.length);\n            failed_tests.forEach(function(test_failure) {\n\n                console.error(\"\\n[-] %s:\", test_failure.img_name);\n\n                test_failure.failures.forEach(function(failure) {\n                    console.error(\"\\n\\t\" + failure.name);\n                    console.error(\"\\tActual:   \" + failure.actual);\n                    console.error(\"\\tExpected: \" + failure.expected);\n                });\n            });\n            process.exit(1);\n        }\n    }\n}\nelse {\n    function run_test(test)\n    {\n        if(!loaded)\n        {\n            first_test = test;\n            return;\n        }\n\n        waiting_to_receive_next_test = false;\n        current_test = test;\n        console.info(\"Testing\", test.img_name);\n\n        var cpu = emulator.v86.cpu;\n\n        assert(!emulator.running);\n\n        cpu.reboot_internal();\n        cpu.reset_memory();\n        cpu.load_multiboot(fs.readFileSync(TEST_DIR + current_test.img_name).buffer);\n\n        test_timeout = setTimeout(() => {\n            console.error(\"Test \" + test.img_name + \" timed out after \" + (SINGLE_TEST_TIMEOUT / 1000) + \" seconds.\");\n            process.exit(2);\n        }, SINGLE_TEST_TIMEOUT);\n\n        if(FORCE_JIT)\n        {\n            let eip = cpu.instruction_pointer[0];\n\n            cpu.test_hook_did_finalize_wasm = function()\n            {\n                eip += 4096;\n                const last_word = cpu.mem32s[eip - 4 >> 2];\n\n                if(last_word === 0 || last_word === undefined)\n                {\n                    cpu.test_hook_did_finalize_wasm = null;\n\n                    // don't synchronously call into the emulator from this callback\n                    setTimeout(() => {\n                        emulator.run();\n                    }, 0);\n                }\n                else\n                {\n                    cpu.jit_force_generate(eip);\n                }\n            };\n\n            cpu.jit_force_generate(eip);\n        }\n        else\n        {\n            emulator.run();\n        }\n    }\n\n    let loaded = false;\n    let current_test = undefined;\n    let first_test = undefined;\n    let waiting_to_receive_next_test = false;\n    let recorded_exceptions = [];\n    let test_timeout;\n\n    let emulator = new V86({\n        autostart: false,\n        memory_size: 2 * 1024 * 1024,\n        disable_jit: +process.env.DISABLE_JIT,\n        log_level: 0,\n    });\n\n    emulator.add_listener(\"emulator-loaded\", function()\n        {\n            loaded = true;\n\n            if(first_test)\n            {\n                run_test(first_test);\n            }\n        });\n\n    emulator.cpu_exception_hook = function(n)\n    {\n        emulator.v86.cpu.instruction_counter[0] += 100000; // always make progress\n\n        if(waiting_to_receive_next_test)\n        {\n            return true;\n        }\n\n        const exceptions = {\n            0: \"DE\",\n            6: \"UD\",\n            13: \"GP\",\n        };\n\n        const exception = exceptions[n];\n\n        if(exception === undefined)\n        {\n            console.error(\"Unexpected CPU exception: \" + n);\n            process.exit(1);\n        }\n\n        const eip = emulator.v86.cpu.instruction_pointer[0];\n        emulator.v86.cpu.write32(emulator.v86.cpu.translate_address_system_read(eip), 0xF4F4F4F4); // hlt\n\n        // XXX: On gdb execution is stopped at this point. On v86 we\n        // currently don't have this ability, so we record the exception\n        // and continue execution\n        recorded_exceptions.push({ exception, eip });\n        finish_test();\n        return true;\n    };\n\n    emulator.bus.register(\"cpu-event-halt\", function() {\n        finish_test();\n    });\n\n    function finish_test()\n    {\n        if(waiting_to_receive_next_test)\n        {\n            return;\n        }\n\n        waiting_to_receive_next_test = true;\n        clearTimeout(test_timeout);\n\n        emulator.stop();\n        var cpu = emulator.v86.cpu;\n\n        const evaluated_fpu_regs = new Float64Array(8).map((_, i) => cpu.fpu_get_sti_f64(i));\n        const evaluated_mmxs = new Int32Array(16).map((_, i) => cpu.fpu_st[(i & ~1) << 1 | (i & 1)]);\n        const evaluated_xmms = cpu.reg_xmm32s;\n        const evaluated_memory = new Int32Array(cpu.mem8.buffer, cpu.mem8.byteOffset + BSS, STACK_TOP - BSS >> 2);\n        const evaluated_fpu_tag = cpu.fpu_load_tag_word();\n        const evaluated_fpu_status = cpu.fpu_load_status_word() & FPU_STATUS_MASK;\n\n        let individual_failures = [];\n\n        assert(current_test.fixture.array);\n\n        const FLOAT_TRANSLATION = {\n            [JSON_POS_INFINITY]: Infinity,\n            [JSON_NEG_INFINITY]: -Infinity,\n            [JSON_POS_NAN]: NaN,\n            [JSON_NEG_NAN]: NaN, // XXX: Ignore sign of NaN\n        };\n\n        let offset = 0;\n        const expected_reg32 = current_test.fixture.array.slice(offset, offset += 8);\n        const expected_eip = current_test.fixture.array[offset++];\n        const expected_fpu_regs =\n            current_test.fixture.array.slice(offset, offset += 8) .map(x => x in FLOAT_TRANSLATION ? FLOAT_TRANSLATION[x] : x);\n        const expected_mmx_registers = current_test.fixture.array.slice(offset, offset += 16);\n        const expected_xmm_registers = current_test.fixture.array.slice(offset, offset += 32);\n        const expected_memory = current_test.fixture.array.slice(offset, offset += 8192 / 4);\n        const expected_eflags = current_test.fixture.array[offset++] & MASK_ARITH;\n        const fpu_tag = current_test.fixture.array[offset++];\n        const fpu_status = current_test.fixture.array[offset++] & FPU_STATUS_MASK;\n\n        if(offset !== current_test.fixture.array.length)\n        {\n            throw new Error(\"Bad fixture length in test \" + current_test.img_name);\n        }\n\n        if(!current_test.fixture.exception)\n        {\n            for(let i = 0; i < cpu.reg32.length; i++) {\n                let reg = cpu.reg32[i];\n                if(reg !== expected_reg32[i]) {\n                    individual_failures.push({\n                        name: \"cpu.reg32[\" + i + \"]\",\n                        expected: expected_reg32[i],\n                        actual: reg,\n                    });\n                }\n            }\n\n            if(fpu_tag !== FPU_TAG_ALL_INVALID)\n            {\n                for(let i = 0; i < evaluated_fpu_regs.length; i++) {\n                    if(expected_fpu_regs[i] !== \"invalid\" &&\n                            !float_equal(evaluated_fpu_regs[i], expected_fpu_regs[i])) {\n                        individual_failures.push({\n                            name: \"st\" + i,\n                            expected: expected_fpu_regs[i],\n                            actual: evaluated_fpu_regs[i],\n                        });\n                    }\n                }\n\n                if(fpu_status !== evaluated_fpu_status)\n                {\n                    individual_failures.push({\n                        name: \"fpu status word\",\n                        expected: fpu_status,\n                        actual: evaluated_fpu_status,\n                    });\n                }\n            }\n            else\n            {\n                for(let i = 0; i < evaluated_mmxs.length; i++) {\n                    if(evaluated_mmxs[i] !== expected_mmx_registers[i]) {\n                        individual_failures.push({\n                            name: \"mm\" + (i >> 1) + \".int32[\" + (i & 1) + \"]\",\n                            expected: expected_mmx_registers[i],\n                            actual: evaluated_mmxs[i],\n                        });\n                    }\n                }\n            }\n\n            for(let i = 0; i < evaluated_xmms.length; i++) {\n                if(evaluated_xmms[i] !== expected_xmm_registers[i]) {\n                    individual_failures.push({\n                        name: \"xmm\" + (i >> 2) + \".int32[\" + (i & 3) + \"] (cpu.reg_xmm[\" + i + \"])\",\n                        expected: expected_xmm_registers[i],\n                        actual: evaluated_xmms[i],\n                    });\n                }\n            }\n\n            for(let i = 0; i < evaluated_memory.length; i++) {\n                if(evaluated_memory[i] !== expected_memory[i]) {\n                    individual_failures.push({\n                        name: \"mem[\" + (BSS + 4 * i).toString(16).toUpperCase() + \"]\",\n                        expected: expected_memory[i],\n                        actual: evaluated_memory[i],\n                    });\n                }\n            }\n\n            const seen_eflags = cpu.get_eflags() & MASK_ARITH;\n            if(seen_eflags !== expected_eflags)\n            {\n                individual_failures.push({\n                    name: \"eflags\",\n                    expected: expected_eflags,\n                    actual: seen_eflags,\n                });\n            }\n        }\n\n        if(current_test.fixture.exception)\n        {\n            const seen_eip = (recorded_exceptions[0] || {}).eip;\n            if(seen_eip !== expected_eip)\n            {\n                individual_failures.push({\n                    name: \"exception eip\",\n                    expected: expected_eip,\n                    actual: seen_eip === undefined ? \"(none)\" : seen_eip,\n                });\n            }\n        }\n\n        const seen_exception = (recorded_exceptions[0] || {}).exception;\n        if(current_test.fixture.exception !== seen_exception)\n        {\n            individual_failures.push({\n                name: \"Exception\",\n                actual: seen_exception || \"(none)\",\n                expected: current_test.fixture.exception,\n            });\n        }\n\n        individual_failures = individual_failures.map(({ name, actual, expected }) => {\n            return {\n                name,\n                actual: format_value(actual),\n                expected: format_value(expected),\n            };\n        });\n\n        recorded_exceptions = [];\n\n        if(individual_failures.length > 0) {\n            process.send({\n                failures: individual_failures,\n                img_name: current_test.img_name\n            });\n        }\n        else {\n            process.send(DONE_MSG);\n        }\n    }\n\n    cluster.worker.on(\"message\", function(message) {\n        if(message === TERMINATE_MSG)\n        {\n            emulator.stop();\n            emulator = null;\n        }\n        else\n        {\n            run_test(message);\n        }\n    });\n\n    process.send(READY_MSG);\n}\n"
  },
  {
    "path": "tests/nasm/shufps-edgecase.asm",
    "content": "global _start\n\n%include \"header.inc\"\n\n    mov dword [esp+0], 1\n    mov dword [esp+4], 2\n    mov dword [esp+8], 3\n    mov dword [esp+12], 4\n    movdqu xmm3, [esp]\n    shufps xmm3, xmm3, 0x32\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/nasm/ucomiss.asm",
    "content": "global _start\n\nsection .data\n\talign 16\ncontrolword:\n\tdw 0\ndwordmxcsr:\n\tdw 0\ndword0:\n\tdd\t1000.0\ndword1:\n\tdd\t5.0\ndword2:\n\tdd\t3000.0\ndwSNaN:\n\tdd\t__SNaN__\ndwQNaN:\n\tdd\t__QNaN__\n\n; Moves EFLAGS into specified register\n%macro moveflags 1\n\tpushf\n\tand\t\t\tdword [esp], 0x45\n\tpop\t\t\teax\n\tmovd\t\t%1, eax\n%endmacro\n\n%include \"header.inc\"\n\n\tmovd\t\txmm0, [dword0]\n\t; Equal\n\tucomiss\t\txmm0, [dword0]\n\tmoveflags\tmm0\t\t\t\t; [ZF] = 100000\n\t; Less than\n\tucomiss\t\txmm0, [dword1]\n\tmoveflags\tmm1\t\t\t\t; [CF] = 000001\n\t; Greater than\n\tucomiss\t\txmm0, [dword2]\n\tmoveflags\tmm2\t\t\t\t; [] = 000000\n\n\t; Unordered: Quiet NaN\n\tmovd\t\txmm1, [dwQNaN]\n\tucomiss\t\txmm0, xmm1\n\tmoveflags\tmm3\t\t\t\t; [ZF][PF][CF] = 100101\n\t; Check #I exception\n\tstmxcsr\t\t[dwordmxcsr]\n\tmovd\t\tmm4, [dwordmxcsr]\n\n\t; Unordered: Signaling NaN\n\tmovd\t\txmm1, [dwSNaN]\n\tucomiss\t\txmm0, xmm1\n\tmoveflags\tmm5\t\t\t\t; [ZF][PF][CF] = 100101\n\t; Check #I exception\n\tstmxcsr\t\t[dwordmxcsr]\n\tmovd\t\tmm6, [dwordmxcsr]\n\n%include \"footer.inc\"\n"
  },
  {
    "path": "tests/qemu/LICENSE",
    "content": "x86 CPU test\n\nCopyright (c) 2003 Fabrice Bellard\n\nThis program is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation; either version 2 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program; if not, see <http://www.gnu.org/licenses/>.\n"
  },
  {
    "path": "tests/qemu/Makefile",
    "content": "CC=gcc\nCC_I386=$(CC) -m32\nCFLAGS=-Wall -O2 -g -fno-strict-aliasing -static -mmmx -msse\nLDFLAGS=\n\n\n# i386/x86_64 emulation test (test various opcodes) */\ntest-i386: test-i386.c test-i386-code16.S test-i386-vm86.S \\\n           test-i386.h test-i386-shift.h test-i386-muldiv.h\n\t$(CC_I386) $(CFLAGS) $(LDFLAGS) -o $@ \\\n              $(<D)/test-i386.c $(<D)/test-i386-code16.S $(<D)/test-i386-vm86.S -lm\n\nclean:\n\trm -f test-i386\n"
  },
  {
    "path": "tests/qemu/Readme.md",
    "content": "How to run:\n\n- Obtain the `linux3.iso` image (see [Readme.md](../../Readme.md))\n- Run `make test-i386`\n- Get the result on the host: `./test-386 > reference`\n- Get the result from the VM: `./run.js > result`\n- The difference should be empty: `diff reference result`\n"
  },
  {
    "path": "tests/qemu/compiler.h",
    "content": "/* compiler.h: macros to abstract away compiler specifics\n *\n * This work is licensed under the terms of the GNU GPL, version 2 or later.\n * See the COPYING file in the top-level directory.\n */\n\n#ifndef COMPILER_H\n#define COMPILER_H\n\n#if defined __clang_analyzer__ || defined __COVERITY__\n#define QEMU_STATIC_ANALYSIS 1\n#endif\n\n/*----------------------------------------------------------------------------\n| The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler.\n| The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h.\n*----------------------------------------------------------------------------*/\n#if defined(__GNUC__) && defined(__GNUC_MINOR__)\n# define QEMU_GNUC_PREREQ(maj, min) \\\n         ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))\n#else\n# define QEMU_GNUC_PREREQ(maj, min) 0\n#endif\n\n#define QEMU_NORETURN __attribute__ ((__noreturn__))\n\n#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n\n#define QEMU_SENTINEL __attribute__((sentinel))\n\n#if QEMU_GNUC_PREREQ(4, 3)\n#define QEMU_ARTIFICIAL __attribute__((always_inline, artificial))\n#else\n#define QEMU_ARTIFICIAL\n#endif\n\n#if defined(_WIN32)\n# define QEMU_PACKED __attribute__((gcc_struct, packed))\n#else\n# define QEMU_PACKED __attribute__((packed))\n#endif\n\n#define QEMU_ALIGNED(X) __attribute__((aligned(X)))\n\n#ifndef glue\n#define xglue(x, y) x ## y\n#define glue(x, y) xglue(x, y)\n#define stringify(s)\ttostring(s)\n#define tostring(s)\t#s\n#endif\n\n#ifndef likely\n#if __GNUC__ < 3\n#define __builtin_expect(x, n) (x)\n#endif\n\n#define likely(x)   __builtin_expect(!!(x), 1)\n#define unlikely(x)   __builtin_expect(!!(x), 0)\n#endif\n\n#ifndef container_of\n#define container_of(ptr, type, member) ({                      \\\n        const typeof(((type *) 0)->member) *__mptr = (ptr);     \\\n        (type *) ((char *) __mptr - offsetof(type, member));})\n#endif\n\n/* Convert from a base type to a parent type, with compile time checking.  */\n#ifdef __GNUC__\n#define DO_UPCAST(type, field, dev) ( __extension__ ( { \\\n    char __attribute__((unused)) offset_must_be_zero[ \\\n        -offsetof(type, field)]; \\\n    container_of(dev, type, field);}))\n#else\n#define DO_UPCAST(type, field, dev) container_of(dev, type, field)\n#endif\n\n#define typeof_field(type, field) typeof(((type *)0)->field)\n#define type_check(t1,t2) ((t1*)0 - (t2*)0)\n\n#define QEMU_BUILD_BUG_ON_STRUCT(x) \\\n    struct { \\\n        int:(x) ? -1 : 1; \\\n    }\n\n#if defined(CONFIG_STATIC_ASSERT)\n#define QEMU_BUILD_BUG_ON(x) _Static_assert(!(x), \"not expecting: \" #x)\n#elif defined(__COUNTER__)\n#define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \\\n    glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused))\n#else\n#define QEMU_BUILD_BUG_ON(x)\n#endif\n\n#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \\\n                                   sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)))\n\n#if defined __GNUC__\n# if !QEMU_GNUC_PREREQ(4, 4)\n   /* gcc versions before 4.4.x don't support gnu_printf, so use printf. */\n#  define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))\n# else\n   /* Use gnu_printf when supported (qemu uses standard format strings). */\n#  define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))\n#  if defined(_WIN32)\n    /* Map __printf__ to __gnu_printf__ because we want standard format strings\n     * even when MinGW or GLib include files use __printf__. */\n#   define __printf__ __gnu_printf__\n#  endif\n# endif\n#else\n#define GCC_FMT_ATTR(n, m)\n#endif\n\n#endif /* COMPILER_H */\n"
  },
  {
    "path": "tests/qemu/config-host.h",
    "content": "/* Automatically generated by create_config - do not modify */\n#define CONFIG_QEMU_CONFDIR \"/usr/local/etc/qemu\"\n#define CONFIG_QEMU_DATADIR \"/usr/local/share/qemu\"\n#define CONFIG_QEMU_DOCDIR \"/usr/local/share/doc/qemu\"\n#define CONFIG_QEMU_LOCALSTATEDIR \"/usr/local/var\"\n#define CONFIG_QEMU_HELPERDIR \"/usr/local/libexec\"\n#define HOST_I386 1\n#define CONFIG_POSIX 1\n#define CONFIG_LINUX 1\n#define CONFIG_SLIRP 1\n#define CONFIG_SMBD_COMMAND \"/usr/sbin/smbd\"\n#define CONFIG_AC97 1\n#define CONFIG_ES1370 1\n#define CONFIG_SB16 1\n#define CONFIG_HDA 1\n#define CONFIG_AUDIO_DRIVERS \\\n    &oss_audio_driver,\\\n\n#define CONFIG_OSS 1\n#define CONFIG_BDRV_WHITELIST \\\n    NULL\n#define CONFIG_VNC 1\n#define CONFIG_VNC_TLS 1\n#define CONFIG_VNC_PNG 1\n#define CONFIG_FNMATCH 1\n#define QEMU_VERSION \"1.3.1\"\n#define QEMU_PKGVERSION \"\"\n#define CONFIG_SDL 1\n#define CONFIG_CURSES 1\n#define CONFIG_ATFILE 1\n#define CONFIG_UTIMENSAT 1\n#define CONFIG_PIPE2 1\n#define CONFIG_ACCEPT4 1\n#define CONFIG_SPLICE 1\n#define CONFIG_EVENTFD 1\n#define CONFIG_FALLOCATE 1\n#define CONFIG_SYNC_FILE_RANGE 1\n#define CONFIG_FIEMAP 1\n#define CONFIG_DUP3 1\n#define CONFIG_EPOLL 1\n#define CONFIG_EPOLL_CREATE1 1\n#define CONFIG_EPOLL_PWAIT 1\n#define CONFIG_INOTIFY 1\n#define CONFIG_INOTIFY1 1\n#define CONFIG_BYTESWAP_H 1\n#define CONFIG_CURL 1\n#define CONFIG_ATTR 1\n#define CONFIG_IOVEC 1\n#define CONFIG_PREADV 1\n#define CONFIG_SIGNALFD 1\n#define CONFIG_FDATASYNC 1\n#define CONFIG_MADVISE 1\n#define CONFIG_POSIX_MADVISE 1\n#define CONFIG_SIGEV_THREAD_ID 1\n#define CONFIG_SMARTCARD 1\n#define CONFIG_OPENGL 1\n#define CONFIG_UNAME_RELEASE \"\"\n#define CONFIG_ZERO_MALLOC 1\n#define CONFIG_UCONTEXT_COROUTINE 1\n#define CONFIG_OPEN_BY_HANDLE 1\n#define CONFIG_LINUX_MAGIC_H 1\n#define CONFIG_PRAGMA_DISABLE_UNUSED_BUT_SET 1\n#define CONFIG_HAS_ENVIRON 1\n#define CONFIG_TRACE_NOP 1\n#define CONFIG_TRACE_FILE trace\n#define CONFIG_TRACE_DEFAULT 1\n"
  },
  {
    "path": "tests/qemu/run-qemu.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\nimport assert from \"node:assert/strict\";\nimport { spawn, spawnSync } from \"node:child_process\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst QEMU = \"qemu-system-x86_64\";\n\nconst share_dir_9p = fs.mkdtempSync(\"/tmp/v86-test-qemu-9p\");\n\nfs.copyFileSync(path.join(__dirname, \"/test-i386\"), path.join(share_dir_9p, \"/test-i386\"));\n\nconst qemu_version = spawnSync(QEMU, [\"--version\"]);\nassert(qemu_version.status === 0, \"QEMU not found, return code: \" + qemu_version.status);\nconsole.error(\"Using QEMU:\");\nconsole.error(qemu_version.stdout.toString(\"utf8\"));\n\nconst qemu = spawn(QEMU,\n    [\n        \"-serial\", \"stdio\",\n        \"-cdrom\", path.join(__dirname, \"/../../images/linux4.iso\"),\n        \"-device\", \"virtio-9p-pci,fsdev=fs9p,mount_tag=host9p\",\n        \"-fsdev\", `local,id=fs9p,path=${share_dir_9p},security_model=none`,\n        \"-display\", \"none\",\n        \"-cpu\", \"Westmere\", // default doesn't support popcnt\n\n        //\"-monitor\", \"telnet:127.0.0.1:1235,server,nowait\",\n    ]\n);\n\nlet qemu_output = \"\";\nlet ran_command = false;\nlet finished = false;\n\nqemu.stdout.on(\"data\", data => {\n    process.stderr.write(data);\n    qemu_output += data.toString().replace(/\\r/, \"\");\n\n    if(!ran_command && qemu_output.endsWith(\"~% \"))\n    {\n        ran_command = true;\n        qemu.stdin.write(\"chmod +x /mnt/test-i386\\n\");\n        qemu.stdin.write(\"/mnt/test-i386 > /mnt/result\\n\");\n        qemu.stdin.write(\"echo test fini''shed\\n\");\n    }\n\n    if(ran_command && !finished && qemu_output.includes(\"test finished\"))\n    {\n        const result_file = path.join(share_dir_9p, \"result\");\n\n        finished = true;\n        console.error(\"Finished\");\n        process.stdout.write(fs.readFileSync(result_file));\n\n        fs.unlinkSync(result_file);\n        fs.unlinkSync(path.join(share_dir_9p, \"test-i386\"));\n        fs.rmdirSync(share_dir_9p);\n\n        qemu.kill();\n    }\n});\n\nqemu.stderr.on(\"data\", data => {\n    process.stderr.write(data);\n});\n"
  },
  {
    "path": "tests/qemu/run.js",
    "content": "#!/usr/bin/env node\n\nimport { fileURLToPath } from \"url\";\nimport path from \"path\";\nimport fs from \"fs\";\nconst __dirname = path.dirname(fileURLToPath(import.meta.url));\n\nconst TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;\nconst { V86 } = await import(TEST_RELEASE_BUILD ? \"../../build/libv86.mjs\" : \"../../src/main.js\");\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nvar test_executable = new Uint8Array(fs.readFileSync(__dirname + \"/test-i386\"));\n\nvar emulator = new V86({\n    bios: { url: __dirname + \"/../../bios/seabios.bin\" },\n    vga_bios: { url: __dirname + \"/../../bios/vgabios.bin\" },\n    cdrom: { url: __dirname + \"/../../images/linux4.iso\" },\n    autostart: true,\n    memory_size: 32 * 1024 * 1024,\n    filesystem: {},\n    disable_jit: +process.env.DISABLE_JIT,\n    log_level: +process.env.LOG_LEVEL || 0,\n});\n\nemulator.bus.register(\"emulator-started\", function()\n{\n    emulator.create_file(\"test-i386\", test_executable);\n});\n\nvar ran_command = false;\nvar line = \"\";\n\nlet outfile = process.stdout;\nif(process.argv[2])\n{\n    outfile = await fs.promises.open(process.argv[2], \"w\");\n}\n\nemulator.add_listener(\"serial0-output-byte\", async function(byte)\n{\n    var chr = String.fromCharCode(byte);\n    if(chr < \" \" && chr !== \"\\n\" && chr !== \"\\t\" || chr > \"~\")\n    {\n        return;\n    }\n\n    if(chr === \"\\n\")\n    {\n        var new_line = line;\n        console.error(\"Serial: %s\", line);\n        line = \"\";\n    }\n    else if(chr >= \" \" && chr <= \"~\")\n    {\n        line += chr;\n    }\n\n    if(!ran_command && line.endsWith(\"~% \"))\n    {\n        ran_command = true;\n        emulator.serial0_send(\"chmod +x /mnt/test-i386\\n\");\n        emulator.serial0_send(\"/mnt/test-i386 > /mnt/result\\n\");\n        emulator.serial0_send(\"echo test fini''shed\\n\");\n    }\n\n    if(new_line && new_line.includes(\"test finished\"))\n    {\n        console.error(\"Done. Reading result ...\");\n\n        const data = await emulator.read_file(\"/result\");\n        console.error(\"Got result, writing to stdout\");\n\n        outfile.write(Buffer.from(data));\n        emulator.destroy();\n    }\n});\n"
  },
  {
    "path": "tests/qemu/test-i386-code16.S",
    "content": "        .code16\n        .globl code16_start\n        .globl code16_end\n\nCS_SEG = 0xf\n\ncode16_start:\n\n        .globl code16_func1\n\n        /* basic test */\ncode16_func1 = . - code16_start\n        mov $1, %eax\n        data32 lret\n\n/* test push/pop in 16 bit mode */\n        .globl code16_func2\ncode16_func2 = . - code16_start\n        xor %eax, %eax\n        mov $0x12345678, %ebx\n        movl %esp, %ecx\n        push %bx\n        subl %esp, %ecx\n        pop %ax\n        data32 lret\n\n/* test various jmp opcodes */\n        .globl code16_func3\ncode16_func3 = . - code16_start\n        jmp 1f\n        nop\n1:\n        mov $4, %eax\n        mov $0x12345678, %ebx\n        xor %bx, %bx\n        jz 2f\n        add $2, %ax\n2:\n\n        call myfunc\n\n        lcall $CS_SEG, $(myfunc2 - code16_start)\n\n        ljmp $CS_SEG, $(myjmp1 - code16_start)\nmyjmp1_next:\n\n        cs lcall *myfunc2_addr - code16_start\n\n        cs ljmp *myjmp2_addr - code16_start\nmyjmp2_next:\n\n        data32 lret\n\nmyfunc2_addr:\n        .short myfunc2 - code16_start\n        .short CS_SEG\n\nmyjmp2_addr:\n        .short myjmp2 - code16_start\n        .short CS_SEG\n\nmyjmp1:\n        add $8, %ax\n        jmp myjmp1_next\n\nmyjmp2:\n        add $16, %ax\n        jmp myjmp2_next\n\nmyfunc:\n        add $1, %ax\n        ret\n\nmyfunc2:\n        add $4, %ax\n        lret\n\n\ncode16_end:\n"
  },
  {
    "path": "tests/qemu/test-i386-muldiv.h",
    "content": "\nvoid glue(glue(test_, OP), b)(long op0, long op1)\n{\n    long res, s1, s0, flags;\n    s0 = op0;\n    s1 = op1;\n    res = s0;\n    flags = 0;\n    asm (\"push %4\\n\\t\"\n         \"popf\\n\\t\"\n         stringify(OP)\"b %b2\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=a\" (res), \"=g\" (flags)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags));\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CC=%04lx\\n\",\n           stringify(OP) \"b\", s0, s1, res, flags & CC_MASK);\n}\n\nvoid glue(glue(test_, OP), w)(long op0h, long op0, long op1)\n{\n    long res, s1, flags, resh;\n    s1 = op1;\n    resh = op0h;\n    res = op0;\n    flags = 0;\n    asm (\"push %5\\n\\t\"\n         \"popf\\n\\t\"\n         stringify(OP) \"w %w3\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=a\" (res), \"=g\" (flags), \"=d\" (resh)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags), \"2\" (resh));\n    printf(\"%-10s AH=\" FMTLX \" AL=\" FMTLX \" B=\" FMTLX \" RH=\" FMTLX \" RL=\" FMTLX \" CC=%04lx\\n\",\n           stringify(OP) \"w\", op0h, op0, s1, resh, res, flags & CC_MASK);\n}\n\nvoid glue(glue(test_, OP), l)(long op0h, long op0, long op1)\n{\n    long res, s1, flags, resh;\n    s1 = op1;\n    resh = op0h;\n    res = op0;\n    flags = 0;\n    asm (\"push %5\\n\\t\"\n         \"popf\\n\\t\"\n         stringify(OP) \"l %k3\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=a\" (res), \"=g\" (flags), \"=d\" (resh)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags), \"2\" (resh));\n    printf(\"%-10s AH=\" FMTLX \" AL=\" FMTLX \" B=\" FMTLX \" RH=\" FMTLX \" RL=\" FMTLX \" CC=%04lx\\n\",\n           stringify(OP) \"l\", op0h, op0, s1, resh, res, flags & CC_MASK);\n}\n\n#if defined(__x86_64__)\nvoid glue(glue(test_, OP), q)(long op0h, long op0, long op1)\n{\n    long res, s1, flags, resh;\n    s1 = op1;\n    resh = op0h;\n    res = op0;\n    flags = 0;\n    asm (\"push %5\\n\\t\"\n         \"popf\\n\\t\"\n         stringify(OP) \"q %3\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=a\" (res), \"=g\" (flags), \"=d\" (resh)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags), \"2\" (resh));\n    printf(\"%-10s AH=\" FMTLX \" AL=\" FMTLX \" B=\" FMTLX \" RH=\" FMTLX \" RL=\" FMTLX \" CC=%04lx\\n\",\n           stringify(OP) \"q\", op0h, op0, s1, resh, res, flags & CC_MASK);\n}\n#endif\n\n#undef OP\n"
  },
  {
    "path": "tests/qemu/test-i386-shift.h",
    "content": "\n#define exec_op glue(exec_, OP)\n#define exec_opq glue(glue(exec_, OP), q)\n#define exec_opl glue(glue(exec_, OP), l)\n#define exec_opw glue(glue(exec_, OP), w)\n#define exec_opb glue(glue(exec_, OP), b)\n\n#ifndef OP_SHIFTD\n\n#ifdef OP_NOBYTE\n#define EXECSHIFT(size, rsize, res, s1, s2, flags) \\\n    asm (\"push %4\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         stringify(OP) size \" %\" rsize \"2, %\" rsize \"0\\n\\t\" \\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n         : \"=g\" (res), \"=g\" (flags)\\\n         : \"r\" (s1), \"0\" (res), \"1\" (flags));\n#else\n#define EXECSHIFT(size, rsize, res, s1, s2, flags) \\\n    asm (\"push %4\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         stringify(OP) size \" %%cl, %\" rsize \"0\\n\\t\" \\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n         : \"=q\" (res), \"=g\" (flags)\\\n         : \"c\" (s1), \"0\" (res), \"1\" (flags));\n#endif\n\n#if defined(__x86_64__)\nvoid exec_opq(long s2, long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"q\", \"\", res, s1, s2, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"q\", s0, s1, res, iflags, flags & CC_MASK);\n}\n#endif\n\nvoid exec_opl(long s2, long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"l\", \"k\", res, s1, s2, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"l\", s0, s1, res, iflags, flags & CC_MASK);\n}\n\nvoid exec_opw(long s2, long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"w\", \"w\", res, s1, s2, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"w\", s0, s1, res, iflags, flags & CC_MASK);\n}\n\n#else\n#define EXECSHIFT(size, rsize, res, s1, s2, flags) \\\n    asm (\"push %4\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         stringify(OP) size \" %%cl, %\" rsize \"5, %\" rsize \"0\\n\\t\" \\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n         : \"=g\" (res), \"=g\" (flags)\\\n         : \"c\" (s1), \"0\" (res), \"1\" (flags), \"r\" (s2));\n\n#if defined(__x86_64__)\nvoid exec_opq(long s2, long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"q\", \"\", res, s1, s2, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" C=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"q\", s0, s2, s1, res, iflags, flags & CC_MASK);\n}\n#endif\n\nvoid exec_opl(long s2, long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"l\", \"k\", res, s1, s2, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" C=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"l\", s0, s2, s1, res, iflags, flags & CC_MASK);\n}\n\nvoid exec_opw(long s2, long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"w\", \"w\", res, s1, s2, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" C=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"w\", s0, s2, s1, res, iflags, flags & CC_MASK);\n}\n\n#endif\n\n#ifndef OP_NOBYTE\nvoid exec_opb(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECSHIFT(\"b\", \"b\", res, s1, 0, flags);\n    /* overflow is undefined if count != 1 */\n    if (s1 != 1)\n      flags &= ~CC_O;\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\",\n           stringify(OP) \"b\", s0, s1, res, iflags, flags & CC_MASK);\n}\n#endif\n\nvoid exec_op(long s2, long s0, long s1)\n{\n    s2 = i2l(s2);\n    s0 = i2l(s0);\n#if defined(__x86_64__)\n    exec_opq(s2, s0, s1, 0);\n#endif\n    exec_opl(s2, s0, s1, 0);\n#ifdef OP_SHIFTD\n    exec_opw(s2, s0, s1, 0);\n#else\n    exec_opw(s2, s0, s1, 0);\n#endif\n#ifndef OP_NOBYTE\n    exec_opb(s0, s1, 0);\n#endif\n#ifdef OP_CC\n#if defined(__x86_64__)\n    exec_opq(s2, s0, s1, CC_C);\n#endif\n    exec_opl(s2, s0, s1, CC_C);\n    exec_opw(s2, s0, s1, CC_C);\n    exec_opb(s0, s1, CC_C);\n#endif\n}\n\nvoid glue(test_, OP)(void)\n{\n    int i, n;\n#if defined(__x86_64__)\n    n = 64;\n#else\n    n = 32;\n#endif\n    for(i = 0; i < n; i++)\n        exec_op(0x21ad3d34, 0x12345678, i);\n    for(i = 0; i < n; i++)\n        exec_op(0x813f3421, 0x82345679, i);\n    for(i = 0; i < n; i++)\n        exec_op(0x21ad3d34, 0x82345679, i);\n    for(i = 0; i < n; i++)\n        exec_op(0x813f3421, 0x12345678, i);\n    for(i = 0; i < n; i++)\n        exec_op(0x80008481, 0x80008688, i);\n    for(i = 0; i < n; i++)\n        exec_op(0xffffffff, 0xffffffff, i);\n    for(i = 0; i < n; i++)\n        exec_op(0xfffe0080, 0xfffe0080, i);\n}\n\nvoid *glue(_test_, OP) __init_call = glue(test_, OP);\n\n#undef OP\n#undef OP_CC\n#undef OP_SHIFTD\n#undef OP_NOBYTE\n#undef EXECSHIFT\n"
  },
  {
    "path": "tests/qemu/test-i386-vm86.S",
    "content": "        .code16\n        .globl vm86_code_start\n        .globl vm86_code_end\n\n#define GET_OFFSET(x) ((x) - vm86_code_start + 0x100)\n\nvm86_code_start:\n        movw $GET_OFFSET(hello_world), %dx\n        movb $0x09, %ah\n        int $0x21\n\n        /* prepare int 0x90 vector */\n        xorw %ax, %ax\n        movw %ax, %es\n        es movw $GET_OFFSET(int90_test), 0x90 * 4\n        es movw %cs, 0x90 * 4 + 2\n\n        /* launch int 0x90 */\n\n        int $0x90\n\n        /* test IF support */\n        movw $GET_OFFSET(IF_msg), %dx\n        movb $0x09, %ah\n        int $0x21\n\n        pushf\n        popw %dx\n        movb $0xff, %ah\n        int $0x21\n\n        cli\n        pushf\n        popw %dx\n        movb $0xff, %ah\n        int $0x21\n\n        sti\n        pushfl\n        popl %edx\n        movb $0xff, %ah\n        int $0x21\n\n#if 0\n        movw $GET_OFFSET(IF_msg1), %dx\n        movb $0x09, %ah\n        int $0x21\n\n        pushf\n        movw %sp, %bx\n        andw $~0x200, (%bx)\n        popf\n#else\n        cli\n#endif\n\n        pushf\n        popw %dx\n        movb $0xff, %ah\n        int $0x21\n\n        pushfl\n        movw %sp, %bx\n        orw $0x200, (%bx)\n        popfl\n\n        pushfl\n        popl %edx\n        movb $0xff, %ah\n        int $0x21\n\n        movb $0x00, %ah\n        int $0x21\n\nint90_test:\n        pushf\n        pop %dx\n        movb $0xff, %ah\n        int $0x21\n\n        movw %sp, %bx\n        movw 4(%bx), %dx\n        movb $0xff, %ah\n        int $0x21\n\n        movw $GET_OFFSET(int90_msg), %dx\n        movb $0x09, %ah\n        int $0x21\n        iret\n\nint90_msg:\n        .string \"INT90 started\\n$\"\n\nhello_world:\n        .string \"Hello VM86 world\\n$\"\n\nIF_msg:\n        .string \"VM86 IF test\\n$\"\n\nIF_msg1:\n        .string \"If you see a diff here, your Linux kernel is buggy, please update to 2.4.20 kernel\\n$\"\n\nvm86_code_end:\n"
  },
  {
    "path": "tests/qemu/test-i386.c",
    "content": "/*\n *  x86 CPU test\n *\n *  Copyright (c) 2003 Fabrice Bellard\n *\n *  This program is free software; you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation; either version 2 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program; if not, see <http://www.gnu.org/licenses/>.\n */\n#define _GNU_SOURCE\n#include \"compiler.h\"\n#include <assert.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <inttypes.h>\n#include <math.h>\n#include <signal.h>\n#include <setjmp.h>\n#include <errno.h>\n#include <sys/ucontext.h>\n#include <sys/mman.h>\n#include <sys/user.h>\n\n#if !defined(__x86_64__)\n#define TEST_VM86\n#define TEST_SEGS\n#endif\n//#define LINUX_VM86_IOPL_FIX\n//#define TEST_P4_FLAGS\n\n//#ifdef __SSE__\n#if 1\n#define TEST_SSE\n#define TEST_CMOV  1\n#define TEST_FCOMI 1\n#else\n#undef TEST_SSE\n#define TEST_CMOV  1\n#define TEST_FCOMI 1\n#endif\n\n#if defined(__x86_64__)\n#define FMT64X \"%016lx\"\n#define FMTLX \"%016lx\"\n#define X86_64_ONLY(x) x\n#else\n#define FMT64X \"%016\" PRIx64\n#define FMTLX \"%08lx\"\n#define X86_64_ONLY(x)\n#endif\n\n#ifdef TEST_VM86\n#include <asm/vm86.h>\n#endif\n\n#define xglue(x, y) x ## y\n#define glue(x, y) xglue(x, y)\n#define stringify(s)\ttostring(s)\n#define tostring(s)\t#s\n#define UNUSED(s)\t(void)(s)\n\n#define CC_C   \t0x0001\n#define CC_P \t0x0004\n#define CC_A\t0x0010\n#define CC_Z\t0x0040\n#define CC_S    0x0080\n#define CC_O    0x0800\n\n#define __init_call\t__attribute__ ((unused,__section__ (\"initcall\")))\n\n#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)\n\n#if defined(__x86_64__)\nstatic inline long i2l(long v)\n{\n    return v | ((v ^ 0xabcd) << 32);\n}\n#else\nstatic inline long i2l(long v)\n{\n    return v;\n}\n#endif\n\n#define OP add\n#include \"test-i386.h\"\n\n#define OP sub\n#include \"test-i386.h\"\n\n#define OP xor\n#include \"test-i386.h\"\n\n#define OP and\n#include \"test-i386.h\"\n\n#define OP or\n#include \"test-i386.h\"\n\n#define OP cmp\n#include \"test-i386.h\"\n\n#define OP adc\n#define OP_CC\n#include \"test-i386.h\"\n\n#define OP sbb\n#define OP_CC\n#include \"test-i386.h\"\n\n#define OP inc\n#define OP_CC\n#define OP1\n#include \"test-i386.h\"\n\n#define OP dec\n#define OP_CC\n#define OP1\n#include \"test-i386.h\"\n\n#define OP neg\n#define OP_CC\n#define OP1\n#include \"test-i386.h\"\n\n#define OP not\n#define OP_CC\n#define OP1\n#include \"test-i386.h\"\n\n#undef CC_MASK\n#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)\n\n#define OP shl\n#include \"test-i386-shift.h\"\n\n#define OP shr\n#include \"test-i386-shift.h\"\n\n#define OP sar\n#include \"test-i386-shift.h\"\n\n#define OP rol\n#include \"test-i386-shift.h\"\n\n#define OP ror\n#include \"test-i386-shift.h\"\n\n#define OP rcr\n#define OP_CC\n#include \"test-i386-shift.h\"\n\n#define OP rcl\n#define OP_CC\n#include \"test-i386-shift.h\"\n\n#define OP shld\n#define OP_SHIFTD\n#define OP_NOBYTE\n#include \"test-i386-shift.h\"\n\n#define OP shrd\n#define OP_SHIFTD\n#define OP_NOBYTE\n#include \"test-i386-shift.h\"\n\n/* XXX: should be more precise ? */\n#undef CC_MASK\n#define CC_MASK (CC_C)\n\n#define OP bt\n#define OP_NOBYTE\n#include \"test-i386-shift.h\"\n\n#define OP bts\n#define OP_NOBYTE\n#include \"test-i386-shift.h\"\n\n#define OP btr\n#define OP_NOBYTE\n#include \"test-i386-shift.h\"\n\n#define OP btc\n#define OP_NOBYTE\n#include \"test-i386-shift.h\"\n\n/* lea test (modrm support) */\n#define TEST_LEAQ(STR)\\\n{\\\n    asm(\"lea \" STR \", %0\"\\\n        : \"=r\" (res)\\\n        : \"a\" (eax), \"b\" (ebx), \"c\" (ecx), \"d\" (edx), \"S\" (esi), \"D\" (edi));\\\n    printf(\"lea %s = \" FMTLX \"\\n\", STR, res);\\\n}\n\n#define TEST_LEA(STR)\\\n{\\\n    asm(\"lea \" STR \", %0\"\\\n        : \"=r\" (res)\\\n        : \"a\" (eax), \"b\" (ebx), \"c\" (ecx), \"d\" (edx), \"S\" (esi), \"D\" (edi));\\\n    printf(\"lea %s = \" FMTLX \"\\n\", STR, res);\\\n}\n\n#define TEST_LEA16(STR)\\\n{\\\n    asm(\".code16 ; .byte 0x67 ; leal \" STR \", %0 ; .code32\"\\\n        : \"=r\" (res)\\\n        : \"a\" (eax), \"b\" (ebx), \"c\" (ecx), \"d\" (edx), \"S\" (esi), \"D\" (edi));\\\n    printf(\"lea %s = %08lx\\n\", STR, res);\\\n}\n\n\nvoid test_lea(void)\n{\n    long eax, ebx, ecx, edx, esi, edi, res;\n    eax = i2l(0x0001);\n    ebx = i2l(0x0002);\n    ecx = i2l(0x0004);\n    edx = i2l(0x0008);\n    esi = i2l(0x0010);\n    edi = i2l(0x0020);\n\n    TEST_LEA(\"0x4000\");\n\n    TEST_LEA(\"(%%eax)\");\n    TEST_LEA(\"(%%ebx)\");\n    TEST_LEA(\"(%%ecx)\");\n    TEST_LEA(\"(%%edx)\");\n    TEST_LEA(\"(%%esi)\");\n    TEST_LEA(\"(%%edi)\");\n\n    TEST_LEA(\"0x40(%%eax)\");\n    TEST_LEA(\"0x40(%%ebx)\");\n    TEST_LEA(\"0x40(%%ecx)\");\n    TEST_LEA(\"0x40(%%edx)\");\n    TEST_LEA(\"0x40(%%esi)\");\n    TEST_LEA(\"0x40(%%edi)\");\n\n    TEST_LEA(\"0x4000(%%eax)\");\n    TEST_LEA(\"0x4000(%%ebx)\");\n    TEST_LEA(\"0x4000(%%ecx)\");\n    TEST_LEA(\"0x4000(%%edx)\");\n    TEST_LEA(\"0x4000(%%esi)\");\n    TEST_LEA(\"0x4000(%%edi)\");\n\n    TEST_LEA(\"(%%eax, %%ecx)\");\n    TEST_LEA(\"(%%ebx, %%edx)\");\n    TEST_LEA(\"(%%ecx, %%ecx)\");\n    TEST_LEA(\"(%%edx, %%ecx)\");\n    TEST_LEA(\"(%%esi, %%ecx)\");\n    TEST_LEA(\"(%%edi, %%ecx)\");\n\n    TEST_LEA(\"0x40(%%eax, %%ecx)\");\n    TEST_LEA(\"0x4000(%%ebx, %%edx)\");\n\n    TEST_LEA(\"(%%ecx, %%ecx, 2)\");\n    TEST_LEA(\"(%%edx, %%ecx, 4)\");\n    TEST_LEA(\"(%%esi, %%ecx, 8)\");\n\n    TEST_LEA(\"(,%%eax, 2)\");\n    TEST_LEA(\"(,%%ebx, 4)\");\n    TEST_LEA(\"(,%%ecx, 8)\");\n\n    TEST_LEA(\"0x40(,%%eax, 2)\");\n    TEST_LEA(\"0x40(,%%ebx, 4)\");\n    TEST_LEA(\"0x40(,%%ecx, 8)\");\n\n\n    TEST_LEA(\"-10(%%ecx, %%ecx, 2)\");\n    TEST_LEA(\"-10(%%edx, %%ecx, 4)\");\n    TEST_LEA(\"-10(%%esi, %%ecx, 8)\");\n\n    TEST_LEA(\"0x4000(%%ecx, %%ecx, 2)\");\n    TEST_LEA(\"0x4000(%%edx, %%ecx, 4)\");\n    TEST_LEA(\"0x4000(%%esi, %%ecx, 8)\");\n\n#if defined(__x86_64__)\n    TEST_LEAQ(\"0x4000\");\n    TEST_LEAQ(\"0x4000(%%rip)\");\n\n    TEST_LEAQ(\"(%%rax)\");\n    TEST_LEAQ(\"(%%rbx)\");\n    TEST_LEAQ(\"(%%rcx)\");\n    TEST_LEAQ(\"(%%rdx)\");\n    TEST_LEAQ(\"(%%rsi)\");\n    TEST_LEAQ(\"(%%rdi)\");\n\n    TEST_LEAQ(\"0x40(%%rax)\");\n    TEST_LEAQ(\"0x40(%%rbx)\");\n    TEST_LEAQ(\"0x40(%%rcx)\");\n    TEST_LEAQ(\"0x40(%%rdx)\");\n    TEST_LEAQ(\"0x40(%%rsi)\");\n    TEST_LEAQ(\"0x40(%%rdi)\");\n\n    TEST_LEAQ(\"0x4000(%%rax)\");\n    TEST_LEAQ(\"0x4000(%%rbx)\");\n    TEST_LEAQ(\"0x4000(%%rcx)\");\n    TEST_LEAQ(\"0x4000(%%rdx)\");\n    TEST_LEAQ(\"0x4000(%%rsi)\");\n    TEST_LEAQ(\"0x4000(%%rdi)\");\n\n    TEST_LEAQ(\"(%%rax, %%rcx)\");\n    TEST_LEAQ(\"(%%rbx, %%rdx)\");\n    TEST_LEAQ(\"(%%rcx, %%rcx)\");\n    TEST_LEAQ(\"(%%rdx, %%rcx)\");\n    TEST_LEAQ(\"(%%rsi, %%rcx)\");\n    TEST_LEAQ(\"(%%rdi, %%rcx)\");\n\n    TEST_LEAQ(\"0x40(%%rax, %%rcx)\");\n    TEST_LEAQ(\"0x4000(%%rbx, %%rdx)\");\n\n    TEST_LEAQ(\"(%%rcx, %%rcx, 2)\");\n    TEST_LEAQ(\"(%%rdx, %%rcx, 4)\");\n    TEST_LEAQ(\"(%%rsi, %%rcx, 8)\");\n\n    TEST_LEAQ(\"(,%%rax, 2)\");\n    TEST_LEAQ(\"(,%%rbx, 4)\");\n    TEST_LEAQ(\"(,%%rcx, 8)\");\n\n    TEST_LEAQ(\"0x40(,%%rax, 2)\");\n    TEST_LEAQ(\"0x40(,%%rbx, 4)\");\n    TEST_LEAQ(\"0x40(,%%rcx, 8)\");\n\n\n    TEST_LEAQ(\"-10(%%rcx, %%rcx, 2)\");\n    TEST_LEAQ(\"-10(%%rdx, %%rcx, 4)\");\n    TEST_LEAQ(\"-10(%%rsi, %%rcx, 8)\");\n\n    TEST_LEAQ(\"0x4000(%%rcx, %%rcx, 2)\");\n    TEST_LEAQ(\"0x4000(%%rdx, %%rcx, 4)\");\n    TEST_LEAQ(\"0x4000(%%rsi, %%rcx, 8)\");\n#else\n    /* limited 16 bit addressing test */\n    TEST_LEA16(\"0x4000\");\n    TEST_LEA16(\"(%%bx)\");\n    TEST_LEA16(\"(%%si)\");\n    TEST_LEA16(\"(%%di)\");\n    TEST_LEA16(\"0x40(%%bx)\");\n    TEST_LEA16(\"0x40(%%si)\");\n    TEST_LEA16(\"0x40(%%di)\");\n    TEST_LEA16(\"0x4000(%%bx)\");\n    TEST_LEA16(\"0x4000(%%si)\");\n    TEST_LEA16(\"(%%bx,%%si)\");\n    TEST_LEA16(\"(%%bx,%%di)\");\n    TEST_LEA16(\"0x40(%%bx,%%si)\");\n    TEST_LEA16(\"0x40(%%bx,%%di)\");\n    TEST_LEA16(\"0x4000(%%bx,%%si)\");\n    TEST_LEA16(\"0x4000(%%bx,%%di)\");\n#endif\n}\n\n#define TEST_JCC(JCC, v1, v2)\\\n{\\\n    int res;\\\n    asm(\"movl $1, %0\\n\\t\"\\\n        \"cmpl %2, %1\\n\\t\"\\\n        \"j\" JCC \" 1f\\n\\t\"\\\n        \"movl $0, %0\\n\\t\"\\\n        \"1:\\n\\t\"\\\n        : \"=r\" (res)\\\n        : \"r\" (v1), \"r\" (v2));\\\n    printf(\"%-10s %d\\n\", \"j\" JCC, res);\\\n\\\n    asm(\"movl $0, %0\\n\\t\"\\\n        \"cmpl %2, %1\\n\\t\"\\\n        \"set\" JCC \" %b0\\n\\t\"\\\n        : \"=r\" (res)\\\n        : \"r\" (v1), \"r\" (v2));\\\n    printf(\"%-10s %d\\n\", \"set\" JCC, res);\\\n if (TEST_CMOV) {\\\n    long val = i2l(1);\\\n    long res = i2l(0x12345678);\\\nX86_64_ONLY(\\\n    asm(\"cmpl %2, %1\\n\\t\"\\\n        \"cmov\" JCC \"q %3, %0\\n\\t\"\\\n        : \"=r\" (res)\\\n        : \"r\" (v1), \"r\" (v2), \"m\" (val), \"0\" (res));\\\n        printf(\"%-10s R=\" FMTLX \"\\n\", \"cmov\" JCC \"q\", res);)\\\n    asm(\"cmpl %2, %1\\n\\t\"\\\n        \"cmov\" JCC \"l %k3, %k0\\n\\t\"\\\n        : \"=r\" (res)\\\n        : \"r\" (v1), \"r\" (v2), \"m\" (val), \"0\" (res));\\\n        printf(\"%-10s R=\" FMTLX \"\\n\", \"cmov\" JCC \"l\", res);\\\n    asm(\"cmpl %2, %1\\n\\t\"\\\n        \"cmov\" JCC \"w %w3, %w0\\n\\t\"\\\n        : \"=r\" (res)\\\n        : \"r\" (v1), \"r\" (v2), \"r\" (1), \"0\" (res));\\\n        printf(\"%-10s R=\" FMTLX \"\\n\", \"cmov\" JCC \"w\", res);\\\n } \\\n}\n\n/* various jump tests */\nvoid test_jcc(void)\n{\n    TEST_JCC(\"ne\", 1, 1);\n    TEST_JCC(\"ne\", 1, 0);\n\n    TEST_JCC(\"e\", 1, 1);\n    TEST_JCC(\"e\", 1, 0);\n\n    TEST_JCC(\"l\", 1, 1);\n    TEST_JCC(\"l\", 1, 0);\n    TEST_JCC(\"l\", 1, -1);\n\n    TEST_JCC(\"le\", 1, 1);\n    TEST_JCC(\"le\", 1, 0);\n    TEST_JCC(\"le\", 1, -1);\n\n    TEST_JCC(\"ge\", 1, 1);\n    TEST_JCC(\"ge\", 1, 0);\n    TEST_JCC(\"ge\", -1, 1);\n\n    TEST_JCC(\"g\", 1, 1);\n    TEST_JCC(\"g\", 1, 0);\n    TEST_JCC(\"g\", 1, -1);\n\n    TEST_JCC(\"b\", 1, 1);\n    TEST_JCC(\"b\", 1, 0);\n    TEST_JCC(\"b\", 1, -1);\n\n    TEST_JCC(\"be\", 1, 1);\n    TEST_JCC(\"be\", 1, 0);\n    TEST_JCC(\"be\", 1, -1);\n\n    TEST_JCC(\"ae\", 1, 1);\n    TEST_JCC(\"ae\", 1, 0);\n    TEST_JCC(\"ae\", 1, -1);\n\n    TEST_JCC(\"a\", 1, 1);\n    TEST_JCC(\"a\", 1, 0);\n    TEST_JCC(\"a\", 1, -1);\n\n\n    TEST_JCC(\"p\", 1, 1);\n    TEST_JCC(\"p\", 1, 0);\n\n    TEST_JCC(\"np\", 1, 1);\n    TEST_JCC(\"np\", 1, 0);\n\n    TEST_JCC(\"o\", 0x7fffffff, 0);\n    TEST_JCC(\"o\", 0x7fffffff, -1);\n\n    TEST_JCC(\"no\", 0x7fffffff, 0);\n    TEST_JCC(\"no\", 0x7fffffff, -1);\n\n    TEST_JCC(\"s\", 0, 1);\n    TEST_JCC(\"s\", 0, -1);\n    TEST_JCC(\"s\", 0, 0);\n\n    TEST_JCC(\"ns\", 0, 1);\n    TEST_JCC(\"ns\", 0, -1);\n    TEST_JCC(\"ns\", 0, 0);\n}\n\n#define TEST_LOOP(insn) \\\n{\\\n    for(i = 0; i < sizeof(ecx_vals) / sizeof(long); i++) {\\\n        ecx = ecx_vals[i];\\\n        for(zf = 0; zf < 2; zf++) {\\\n    asm(\"test %2, %2\\n\\t\"\\\n        \"movl $1, %0\\n\\t\"\\\n          insn \" 1f\\n\\t\" \\\n        \"movl $0, %0\\n\\t\"\\\n        \"1:\\n\\t\"\\\n        : \"=a\" (res)\\\n        : \"c\" (ecx), \"b\" (!zf)); \\\n    printf(\"%-10s ECX=\" FMTLX \" ZF=%ld r=%d\\n\", insn, ecx, zf, res);      \\\n        }\\\n   }\\\n}\n\nvoid test_loop(void)\n{\n    long ecx, zf;\n    const long ecx_vals[] = {\n        0,\n        1,\n        0x10000,\n        0x10001,\n#if defined(__x86_64__)\n        0x100000000L,\n        0x100000001L,\n#endif\n    };\n    int i, res;\n\n#if !defined(__x86_64__)\n    TEST_LOOP(\"jcxz\");\n    TEST_LOOP(\"loopw\");\n    TEST_LOOP(\"loopzw\");\n    TEST_LOOP(\"loopnzw\");\n#endif\n\n    TEST_LOOP(\"jecxz\");\n    TEST_LOOP(\"loopl\");\n    TEST_LOOP(\"loopzl\");\n    TEST_LOOP(\"loopnzl\");\n}\n\n#undef CC_MASK\n#ifdef TEST_P4_FLAGS\n#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)\n#else\n#define CC_MASK (CC_O | CC_C)\n#endif\n\n#define OP mul\n#include \"test-i386-muldiv.h\"\n\n#define OP imul\n#include \"test-i386-muldiv.h\"\n\nvoid test_imulw2(long op0, long op1)\n{\n    long res, s1, s0, flags;\n    s0 = op0;\n    s1 = op1;\n    res = s0;\n    flags = 0;\n    asm volatile (\"push %4\\n\\t\"\n         \"popf\\n\\t\"\n         \"imulw %w2, %w0\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=q\" (res), \"=g\" (flags)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags));\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CC=%04lx\\n\",\n           \"imulw\", s0, s1, res, flags & CC_MASK);\n}\n\nvoid test_imull2(long op0, long op1)\n{\n    long res, s1, s0, flags;\n    s0 = op0;\n    s1 = op1;\n    res = s0;\n    flags = 0;\n    asm volatile (\"push %4\\n\\t\"\n         \"popf\\n\\t\"\n         \"imull %k2, %k0\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=q\" (res), \"=g\" (flags)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags));\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CC=%04lx\\n\",\n           \"imull\", s0, s1, res, flags & CC_MASK);\n}\n\n#if defined(__x86_64__)\nvoid test_imulq2(long op0, long op1)\n{\n    long res, s1, s0, flags;\n    s0 = op0;\n    s1 = op1;\n    res = s0;\n    flags = 0;\n    asm volatile (\"push %4\\n\\t\"\n         \"popf\\n\\t\"\n         \"imulq %2, %0\\n\\t\"\n         \"pushf\\n\\t\"\n         \"pop %1\\n\\t\"\n         : \"=q\" (res), \"=g\" (flags)\n         : \"q\" (s1), \"0\" (res), \"1\" (flags));\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CC=%04lx\\n\",\n           \"imulq\", s0, s1, res, flags & CC_MASK);\n}\n#endif\n\n#define TEST_IMUL_IM(size, rsize, op0, op1)\\\n{\\\n    long res, flags, s1;\\\n    flags = 0;\\\n    res = 0;\\\n    s1 = op1;\\\n    asm volatile (\"push %3\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         \"imul\" size \" $\" #op0 \", %\" rsize \"2, %\" rsize \"0\\n\\t\" \\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n         : \"=r\" (res), \"=g\" (flags)\\\n         : \"r\" (s1), \"1\" (flags), \"0\" (res));\\\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CC=%04lx\\n\",\\\n           \"imul\" size \" im\", (long)op0, (long)op1, res, flags & CC_MASK);\\\n}\n\n\n#undef CC_MASK\n#define CC_MASK (0)\n\n#define OP div\n#include \"test-i386-muldiv.h\"\n\n#define OP idiv\n#include \"test-i386-muldiv.h\"\n\nvoid test_mul(void)\n{\n    test_imulb(0x1234561d, 4);\n    test_imulb(3, -4);\n    test_imulb(0x80, 0x80);\n    test_imulb(0x10, 0x10);\n\n    test_imulw(0, 0x1234001d, 45);\n    test_imulw(0, 23, -45);\n    test_imulw(0, 0x8000, 0x8000);\n    test_imulw(0, 0x100, 0x100);\n\n    test_imull(0, 0x1234001d, 45);\n    test_imull(0, 23, -45);\n    test_imull(0, 0x80000000, 0x80000000);\n    test_imull(0, 0x10000, 0x10000);\n\n    test_mulb(0x1234561d, 4);\n    test_mulb(3, -4);\n    test_mulb(0x80, 0x80);\n    test_mulb(0x10, 0x10);\n\n    test_mulw(0, 0x1234001d, 45);\n    test_mulw(0, 23, -45);\n    test_mulw(0, 0x8000, 0x8000);\n    test_mulw(0, 0x100, 0x100);\n\n    test_mull(0, 0x1234001d, 45);\n    test_mull(0, 23, -45);\n    test_mull(0, 0x80000000, 0x80000000);\n    test_mull(0, 0x10000, 0x10000);\n\n    test_mull(0, 0xffffffff, 0xffffffff);\n    test_mull(0, 0xfffffffe, 0xffffffff);\n    test_mull(0, 0xffffffff, 0xfffffffe);\n\n    test_mull(0, 0xffffffff, 0);\n    test_mull(0, 0xffffffff, 1);\n    test_mull(0, 0xffffffff, 2);\n    test_mull(0, 0xffffffff, 3);\n\n    test_mull(0, 0, 0xffffffff);\n    test_mull(0, 1, 0xffffffff);\n    test_mull(0, 2, 0xffffffff);\n    test_mull(0, 3, 0xffffffff);\n\n\n    test_imulw2(0x1234001d, 45);\n    test_imulw2(23, -45);\n    test_imulw2(0x8000, 0x8000);\n    test_imulw2(0x100, 0x100);\n\n    test_imull2(0x1234001d, 45);\n    test_imull2(23, -45);\n    test_imull2(0x80000000, 0x80000000);\n    test_imull2(0x10000, 0x10000);\n\n    TEST_IMUL_IM(\"w\", \"w\", 45, 0x1234);\n    TEST_IMUL_IM(\"w\", \"w\", -45, 23);\n    TEST_IMUL_IM(\"w\", \"w\", 0x8000, 0x80000000);\n    TEST_IMUL_IM(\"w\", \"w\", 0x7fff, 0x1000);\n\n    TEST_IMUL_IM(\"l\", \"k\", 45, 0x1234);\n    TEST_IMUL_IM(\"l\", \"k\", -45, 23);\n    TEST_IMUL_IM(\"l\", \"k\", 0x8000, 0x80000000);\n    TEST_IMUL_IM(\"l\", \"k\", 0x7fff, 0x1000);\n\n    test_idivb(0x12341678, 0x127e);\n    test_idivb(0x43210123, -5);\n    test_idivb(0x12340004, -1);\n    test_idivb(-20, 3);\n    test_idivb(20, -3);\n    test_idivb(-20, -3);\n\n    test_idivw(0, 0x12345678, 12347);\n    test_idivw(0, -23223, -45);\n    test_idivw(0, 0x12348000, -1);\n    test_idivw(0x12343, 0x12345678, 0x81238567);\n    test_idivw(-20, 0, 300);\n    test_idivw(20,  0, -300);\n    test_idivw(-20, 0, -300);\n\n    test_idivl(0, 0x12345678, 12347);\n    test_idivl(0, -233223, -45);\n    test_idivl(0, 0x80000000, -1);\n    test_idivl(0x12343, 0x12345678, 0x81234567);\n\n    test_divb(0x12341678, 0x127e);\n    test_divb(0x43210123, -5);\n    test_divb(0x12340004, -1);\n\n    test_divw(0, 0x12345678, 12347);\n    test_divw(0, -23223, -45);\n    test_divw(0, 0x12348000, -1);\n    test_divw(0x12343, 0x12345678, 0x81238567);\n\n    test_divl(0, 0x12345678, 12347);\n    test_divl(0, -233223, -45);\n    test_divl(0, 0x80000000, -1);\n    test_divl(0x12343, 0x12345678, 0x81234567);\n\n    test_divl(0xfffffffe, 0xffffffff, 0xffffffff);\n    test_divl(0xffffffe, 0xffffffff, 0xfffffff);\n    test_divl(0xfffffe, 0xffffffff, 0xffffff);\n    test_divl(0xffffe, 0xffffffff, 0xfffff);\n    test_divl(0xfffe, 0xffffffff, 0xffff);\n    test_divl(0xffe, 0xffffffff, 0xfff);\n    test_divl(0xfe, 0xffffffff, 0xff);\n    test_divl(0xe, 0xffffffff, 0xf);\n\n    test_divl(0x7ffffffe, 0xffffffff, 0x7fffffff);\n    test_divl(0x7fffffe, 0xffffffff, 0x7ffffff);\n    test_divl(0x7ffffe, 0xffffffff, 0x7fffff);\n    test_divl(0x7fffe, 0xffffffff, 0x7ffff);\n    test_divl(0x7ffe, 0xffffffff, 0x7fff);\n    test_divl(0x7fe, 0xffffffff, 0x7ff);\n    test_divl(0x7e, 0xffffffff, 0x7f);\n\n    test_divl(0x3ffffffe, 0xffffffff, 0x3fffffff);\n    test_divl(0x3fffffe, 0xffffffff, 0x3ffffff);\n    test_divl(0x3ffffe, 0xffffffff, 0x3fffff);\n    test_divl(0x3fffe, 0xffffffff, 0x3ffff);\n    test_divl(0x3ffe, 0xffffffff, 0x3fff);\n    test_divl(0x3fe, 0xffffffff, 0x3ff);\n    test_divl(0x3e, 0xffffffff, 0x3f);\n\n    test_divl(0x1ffffffe, 0xffffffff, 0x1fffffff);\n    test_divl(0x1fffffe, 0xffffffff, 0x1ffffff);\n    test_divl(0x1ffffe, 0xffffffff, 0x1fffff);\n    test_divl(0x1fffe, 0xffffffff, 0x1ffff);\n    test_divl(0x1ffe, 0xffffffff, 0x1fff);\n    test_divl(0x1fe, 0xffffffff, 0x1ff);\n    test_divl(0x1e, 0xffffffff, 0x1f);\n\n    int i;\n    for(i = 0; i < 16; i++)\n    {\n        test_divl(0, 0xfffffffe, i + 1);\n        test_divl(0, 0xffffffff, i + 1);\n        test_divl(1, 0xfffffffe, i + 2);\n        test_divl(1, 0xffffffff, i + 2);\n        test_divl(2, 0xfffffffe, i + 3);\n        test_divl(2, 0xffffffff, i + 3);\n        test_divl(3, 0xfffffffe, i + 4);\n        test_divl(3, 0xffffffff, i + 4);\n        test_divl(4, 0xfffffffe, i + 5);\n        test_divl(4, 0xffffffff, i + 5);\n\n        test_divl(0xfffffffd, 0x00000000 + i, 0xfffffffe);\n        test_divl(0xfffffffd, 0xfffffff0 + i, 0xfffffffe);\n\n        test_divl(0xfffffffe, 0x00000000 + i, 0xffffffff);\n        test_divl(0xfffffffe, 0xfffffff0 + i, 0xffffffff);\n\n        test_divl(0, i, 0xfffffffa);\n        test_divl(0, i, 0xfffffffb);\n        test_divl(0, i, 0xfffffffc);\n        test_divl(0, i, 0xfffffffd);\n        test_divl(0, i, 0xfffffffe);\n        test_divl(0, i, 0xffffffff);\n\n        test_idivl(0, 1,  i + 1);\n        test_idivl(-1, -1, i + 1);\n        test_idivl(0, 1,  -(i + 1));\n        test_idivl(-1, -1, -(i + 1));\n\n        test_idivl(0,  0x7fffffff, i + 1);\n        test_idivl(-1, 0x80000001, i + 1);\n        test_idivl(0,  0x7fffffff, -(i + 1));\n        test_idivl(-1, 0x80000001, -(i + 1));\n    }\n\n#if defined(__x86_64__)\n    test_imulq(0, 0x1234001d1234001d, 45);\n    test_imulq(0, 23, -45);\n    test_imulq(0, 0x8000000000000000, 0x8000000000000000);\n    test_imulq(0, 0x100000000, 0x100000000);\n\n    test_mulq(0, 0x1234001d1234001d, 45);\n    test_mulq(0, 23, -45);\n    test_mulq(0, 0x8000000000000000, 0x8000000000000000);\n    test_mulq(0, 0x100000000, 0x100000000);\n\n    test_imulq2(0x1234001d1234001d, 45);\n    test_imulq2(23, -45);\n    test_imulq2(0x8000000000000000, 0x8000000000000000);\n    test_imulq2(0x100000000, 0x100000000);\n\n    TEST_IMUL_IM(\"q\", \"\", 45, 0x12341234);\n    TEST_IMUL_IM(\"q\", \"\", -45, 23);\n    TEST_IMUL_IM(\"q\", \"\", 0x8000, 0x8000000000000000);\n    TEST_IMUL_IM(\"q\", \"\", 0x7fff, 0x10000000);\n\n    test_idivq(0, 0x12345678abcdef, 12347);\n    test_idivq(0, -233223, -45);\n    test_idivq(0, 0x8000000000000000, -1);\n    test_idivq(0x12343, 0x12345678, 0x81234567);\n\n    test_divq(0, 0x12345678abcdef, 12347);\n    test_divq(0, -233223, -45);\n    test_divq(0, 0x8000000000000000, -1);\n    test_divq(0x12343, 0x12345678, 0x81234567);\n#endif\n}\n\n#define TEST_BSX(op, size, op0)\\\n{\\\n    long res, val, resz;\\\n    val = op0;\\\n    asm(\"xor %1, %1\\n\"\\\n        \"mov $0x12345678, %0\\n\"\\\n        #op \" %\" size \"2, %\" size \"0 ; setz %b1\" \\\n        : \"=&r\" (res), \"=&q\" (resz)\\\n        : \"r\" (val));\\\n    printf(\"%-10s A=\" FMTLX \" R=\" FMTLX \" %ld\\n\", #op, val, res, resz);\\\n}\n\nvoid test_bsx(void)\n{\n    TEST_BSX(bsrw, \"w\", 0);\n    TEST_BSX(bsrw, \"w\", 0x12340128);\n    TEST_BSX(bsrw, \"w\", 0xffffffff);\n    TEST_BSX(bsrw, \"w\", 0xffff7fff);\n\n    TEST_BSX(bsfw, \"w\", 0);\n    TEST_BSX(bsfw, \"w\", 0x12340128);\n    TEST_BSX(bsfw, \"w\", 0xffffffff);\n    TEST_BSX(bsfw, \"w\", 0xfffffff7);\n\n    TEST_BSX(bsrl, \"k\", 0);\n    TEST_BSX(bsrl, \"k\", 0x00340128);\n    TEST_BSX(bsrl, \"k\", 0xffffffff);\n    TEST_BSX(bsrl, \"k\", 0x7fffffff);\n\n    TEST_BSX(bsfl, \"k\", 0);\n    TEST_BSX(bsfl, \"k\", 0x00340128);\n    TEST_BSX(bsfl, \"k\", 0xffffffff);\n    TEST_BSX(bsfl, \"k\", 0xfffffff7);\n\n#if defined(__x86_64__)\n    TEST_BSX(bsrq, \"\", 0);\n    TEST_BSX(bsrq, \"\", 0x003401281234);\n    TEST_BSX(bsfq, \"\", 0);\n    TEST_BSX(bsfq, \"\", 0x003401281234);\n#endif\n}\n\n#define TEST_POPCNT(size, op0)\\\n{\\\n    long res, val, resz;\\\n    val = op0;\\\n    asm(\"xor %1, %1\\n\"\\\n        \"mov $0x12345678, %0\\n\"\\\n        \"popcnt %\" size \"2, %\" size \"0 ; pushf; pop %1;\" \\\n        : \"=&r\" (res), \"=&q\" (resz)\\\n        : \"r\" (val));\\\n    printf(\"popcnt A=\" FMTLX \" R=\" FMTLX \" flags=%lx\\n\", val, res, resz);\\\n}\n\nvoid test_popcnt(void)\n{\n    TEST_POPCNT(\"w\", 0);\n}\n\n/**********************************************/\n\nunion float64u {\n    double d;\n    uint64_t l;\n};\n\nunion float64u q_nan = { .l = 0xFFF8000000000000LL };\nunion float64u s_nan = { .l = 0xFFF0000000000000LL };\n\nvoid test_fops(double a, double b)\n{\n    int ib = (int)b;\n    int dest = 0;\n\n    printf(\"a=%f b=%f a+b=%f\\n\", a, b, a + b);\n    printf(\"a=%f b=%f a-b=%f\\n\", a, b, a - b);\n    printf(\"a=%f b=%f a*b=%f\\n\", a, b, a * b);\n    printf(\"a=%f b=%f a/b=%f\\n\", a, b, a / b);\n    printf(\"a=%f b=%f =%f\\n\", a, b, a + a + a + 3 * b / a * (a * a * a / b / b / (a + 1.0) - 3.5 + a * b / (3.7 * a / (a - b * b) + 6.5 * a / (b * b * a / -b - a * b) + 5.5 * (b - a))));\n    //printf(\"a=%f b=%f fmod(a, b)=%f\\n\", a, b, fmod(a, b)); // difference in sign bit on zero and nan\n    printf(\"a=%f fma(a,b,a)=%f\\n\", a, fma(a, b, a));\n    printf(\"a=%f fdim(a,b)=%f\\n\", a, fdim(a, b));\n    printf(\"a=%f copysign(a,b)=%f\\n\", a, copysign(a, b));\n    printf(\"a=%f sqrt(a)=%f\\n\", a, sqrt(a));\n    printf(\"a=%f sin(a)=%f\\n\", a, sin(a));\n    printf(\"a=%f cos(a)=%f\\n\", a, cos(a));\n    printf(\"a=%f tan(a)=%f\\n\", a, tan(a));\n    if(a >= 0)\n    {\n        printf(\"a=%f log(a)=%f\\n\", a, log(a));\n        printf(\"a=%f log10(a)=%f\\n\", a, log10(a));\n        printf(\"a=%f log1p(a)=%f\\n\", a, log1p(a));\n        printf(\"a=%f log2(a)=%f\\n\", a, log2(a));\n    }\n    printf(\"a=%f logb(a)=%f\\n\", a, logb(a));\n    printf(\"a=%f ilogb(a)=%d\\n\", a, ilogb(a));\n    printf(\"a=%f exp(a)=%f\\n\", a, exp(a));\n    printf(\"a=%f exp2(a)=%f\\n\", a, exp2(a));\n    printf(\"a=%f frexp(a)=%f, %d\\n\", a, frexp(a, &dest), dest);\n    printf(\"a=%f ldexp(a,b)=%f\\n\", a, ldexp(a, ib));\n    printf(\"a=%f scalbn(a,b)=%f\\n\", a, scalbn(a, ib));\n    printf(\"a=%f sinh(a)=%f\\n\", a, sinh(a));\n    printf(\"a=%f cosh(a)=%f\\n\", a, cosh(a));\n    printf(\"a=%f tanh(a)=%f\\n\", a, tanh(a));\n    printf(\"a=%f fabs(a)=%f\\n\", a, fabs(a));\n    printf(\"a=%f pow(a,b)=%f\\n\", a, pow(a,b));\n    printf(\"a=%f b=%f atan2(a, b)=%f\\n\", a, b, atan2(a, b));\n    /* just to test some op combining */\n    printf(\"a=%f asin(sin(a))=%f\\n\", a, asin(sin(a)));\n    printf(\"a=%f acos(cos(a))=%f\\n\", a, acos(cos(a)));\n    printf(\"a=%f atan(tan(a))=%f\\n\", a, atan(tan(a)));\n\n}\n\nvoid fpu_clear_exceptions(void)\n{\n    struct QEMU_PACKED {\n        uint16_t fpuc;\n        uint16_t dummy1;\n        uint16_t fpus;\n        uint16_t dummy2;\n        uint16_t fptag;\n        uint16_t dummy3;\n        uint32_t ignored[4];\n        long double fpregs[8];\n    } float_env32;\n\n    asm volatile (\"fnstenv %0\\n\" : \"=m\" (float_env32));\n    float_env32.fpus &= ~0x7f;\n    asm volatile (\"fldenv %0\\n\" : : \"m\" (float_env32));\n}\n\n/* XXX: display exception bits when supported */\n#define FPUS_EMASK 0x007f\n\nvoid test_fcmp(double a, double b)\n{\n    long eflags, fpus;\n\n    fpu_clear_exceptions();\n    asm(\"fcom %2\\n\"\n        \"fstsw %%ax\\n\"\n        : \"=a\" (fpus)\n        : \"t\" (a), \"u\" (b));\n    printf(\"fcom(%f %f)=%04lx\\n\",\n           a, b, fpus & (0x4500 | FPUS_EMASK & ~1));\n    fpu_clear_exceptions();\n    asm(\"fucom %2\\n\"\n        \"fstsw %%ax\\n\"\n        : \"=a\" (fpus)\n        : \"t\" (a), \"u\" (b));\n    printf(\"fucom(%f %f)=%04lx\\n\",\n           a, b, fpus & (0x4500 | FPUS_EMASK));\n    if (TEST_FCOMI) {\n        /* test f(u)comi instruction */\n        fpu_clear_exceptions();\n        asm(\"fcomi %3, %2\\n\"\n            \"fstsw %%ax\\n\"\n            \"pushf\\n\"\n            \"pop %0\\n\"\n            : \"=r\" (eflags), \"=a\" (fpus)\n            : \"t\" (a), \"u\" (b));\n        printf(\"fcomi(%f %f)=%04lx %02lx\\n\",\n               a, b, fpus & FPUS_EMASK & ~1, eflags & (CC_Z | CC_P | CC_C));\n        fpu_clear_exceptions();\n        asm(\"fucomi %3, %2\\n\"\n            \"fstsw %%ax\\n\"\n            \"pushf\\n\"\n            \"pop %0\\n\"\n            : \"=r\" (eflags), \"=a\" (fpus)\n            : \"t\" (a), \"u\" (b));\n        printf(\"fucomi(%f %f)=%04lx %02lx\\n\",\n               a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));\n    }\n    fpu_clear_exceptions();\n    asm volatile(\"fxam\\n\"\n                 \"fstsw %%ax\\n\"\n                 : \"=a\" (fpus)\n                 : \"t\" (a));\n    printf(\"fxam(%f)=%04lx\\n\", a, fpus & 0x4700);\n    fpu_clear_exceptions();\n}\n\nvoid test_fcvt(double a)\n{\n    float fa;\n    long double la;\n    int16_t fpuc;\n    int i;\n    int64_t lla;\n    int ia;\n    int16_t wa;\n    double ra;\n\n    fa = a;\n    la = a;\n    printf(\"(float)%f = %f\\n\", a, fa);\n    printf(\"(long double)%f = %Lf\\n\", a, la);\n    printf(\"a=\" FMT64X \"\\n\", *(uint64_t *)&a);\n    printf(\"la=\" FMT64X \" %04x\\n\", *(uint64_t *)&la,\n           *(unsigned short *)((char *)(&la) + 8));\n\n    /* test all roundings */\n    asm volatile (\"fstcw %0\" : \"=m\" (fpuc));\n    for(i=0;i<4;i++) {\n        uint16_t val16;\n        val16 = (fpuc & ~0x0c00) | (i << 10);\n        asm volatile (\"fldcw %0\" : : \"m\" (val16));\n        asm volatile (\"fist %0\" : \"=m\" (wa) : \"t\" (a));\n        asm volatile (\"fistl %0\" : \"=m\" (ia) : \"t\" (a));\n        asm volatile (\"fistpll %0\" : \"=m\" (lla) : \"t\" (a) : \"st\");\n        asm volatile (\"frndint ; fstl %0\" : \"=m\" (ra) : \"t\" (a));\n        asm volatile (\"fldcw %0\" : : \"m\" (fpuc));\n        printf(\"(short)a = %d\\n\", wa);\n        printf(\"(int)a = %d\\n\", ia);\n        printf(\"(int64_t)a = \" FMT64X \"\\n\", lla);\n        printf(\"rint(a) = %f\\n\", ra);\n    }\n}\n\n#define TEST(N) \\\n    asm(\"fld\" #N : \"=t\" (a)); \\\n    printf(\"fld\" #N \"= %f\\n\", a);\n\nvoid test_fconst(void)\n{\n    double a;\n    TEST(1);\n    TEST(l2t);\n    TEST(l2e);\n    TEST(pi);\n    TEST(lg2);\n    TEST(ln2);\n    TEST(z);\n}\n\nvoid test_fbcd(double a)\n{\n    unsigned short bcd[5];\n    double b;\n\n    asm(\"fbstp %0\" : \"=m\" (bcd[0]) : \"t\" (a) : \"st\");\n    //asm(\"fbld %1\" : \"=t\" (b) : \"m\" (bcd[0]));\n    printf(\"a=%f bcd=%04x%04x%04x%04x%04x b=%f\\n\",\n           a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);\n}\n\n#define TEST_ENV(env, save, restore)\\\n{\\\n    memset((env), 0xaa, sizeof(*(env)));\\\n    for(i=0;i<5;i++)\\\n        asm volatile (\"fldl %0\" : : \"m\" (dtab[i]));\\\n    asm volatile (save \" %0\\n\" : : \"m\" (*(env)));\\\n    asm volatile (restore \" %0\\n\": : \"m\" (*(env)));\\\n    for(i=0;i<5;i++)\\\n        asm volatile (\"fstpl %0\" : \"=m\" (rtab[i]));\\\n    for(i=0;i<5;i++)\\\n        printf(\"res[%d]=%f\\n\", i, rtab[i]);\\\n    printf(\"fpuc=%04x fpus=%04x fptag=%04x\\n\",\\\n           (env)->fpuc,\\\n           (env)->fpus & 0xff00,\\\n           (env)->fptag);\\\n}\n\nvoid test_fenv(void)\n{\n    struct __attribute__((__packed__)) {\n        uint16_t fpuc;\n        uint16_t dummy1;\n        uint16_t fpus;\n        uint16_t dummy2;\n        uint16_t fptag;\n        uint16_t dummy3;\n        uint32_t ignored[4];\n        long double fpregs[8];\n    } float_env32;\n    struct __attribute__((__packed__)) {\n        uint16_t fpuc;\n        uint16_t fpus;\n        uint16_t fptag;\n        uint16_t ignored[4];\n        long double fpregs[8];\n    } float_env16;\n    double dtab[8];\n    double rtab[8];\n    int i;\n\n    for(i=0;i<8;i++)\n        dtab[i] = i + 1;\n\n    asm volatile (\"fninit\");\n    //TEST_ENV(&float_env16, \"data16 fnstenv\", \"data16 fldenv\");\n    //TEST_ENV(&float_env16, \"data16 fnsave\", \"data16 frstor\");\n    TEST_ENV(&float_env32, \"fnstenv\", \"fldenv\");\n    TEST_ENV(&float_env32, \"fnsave\", \"frstor\");\n\n    /* test for ffree */\n    for(i=0;i<5;i++)\n        asm volatile (\"fldl %0\" : : \"m\" (dtab[i]));\n    asm volatile(\"ffree %st(2)\");\n    asm volatile (\"fnstenv %0\\n\" : : \"m\" (float_env32));\n    asm volatile (\"fninit\");\n    printf(\"fptag=%04x\\n\", float_env32.fptag);\n}\n\n\n#define TEST_FCMOV(a, b, eflags, CC)\\\n{\\\n    double res;\\\n    asm(\"push %3\\n\"\\\n        \"popf\\n\"\\\n        \"fcmov\" CC \" %2, %0\\n\"\\\n        : \"=t\" (res)\\\n        : \"0\" (a), \"u\" (b), \"g\" (eflags));\\\n    printf(\"fcmov%s eflags=0x%04lx-> %f\\n\", \\\n           CC, (long)eflags, res);\\\n}\n\nvoid test_fcmov(void)\n{\n    double a, b;\n    long eflags, i;\n\n    a = 1.0;\n    b = 2.0;\n    for(i = 0; i < 4; i++) {\n        eflags = 0;\n        if (i & 1)\n            eflags |= CC_C;\n        if (i & 2)\n            eflags |= CC_Z;\n        TEST_FCMOV(a, b, eflags, \"b\");\n        TEST_FCMOV(a, b, eflags, \"e\");\n        TEST_FCMOV(a, b, eflags, \"be\");\n        TEST_FCMOV(a, b, eflags, \"nb\");\n        TEST_FCMOV(a, b, eflags, \"ne\");\n        TEST_FCMOV(a, b, eflags, \"nbe\");\n    }\n    TEST_FCMOV(a, b, 0, \"u\");\n    TEST_FCMOV(a, b, CC_P, \"u\");\n    TEST_FCMOV(a, b, 0, \"nu\");\n    TEST_FCMOV(a, b, CC_P, \"nu\");\n}\n\nvoid test_floats(void)\n{\n    test_fops(2, 3);\n    test_fops(1.4, -5);\n    test_fops(-20.5, 128);\n    test_fops(-0.5, -4);\n    test_fcmp(2, -1);\n    test_fcmp(2, 2);\n    test_fcmp(2, 3);\n    test_fcmp(2, q_nan.d);\n    test_fcmp(q_nan.d, -1);\n    test_fcmp(-1.0/0.0, -1);\n    test_fcmp(1.0/0.0, -1);\n    test_fcvt(0.5);\n    test_fcvt(-0.5);\n    test_fcvt(1.0/7.0);\n    test_fcvt(-1.0/9.0);\n    test_fcvt(32768);\n    // largest and smallest, odd and even numbers that have one bit left for the fractional part (2**52-1)\n    test_fcvt(4503599627370494.5);\n    test_fcvt(4503599627370495.5);\n    test_fcvt(-4503599627370494.5);\n    test_fcvt(-4503599627370495.5);\n    test_fcvt(-1e20);\n    test_fcvt(-1.0/0.0);\n    test_fcvt(1.0/0.0);\n    test_fcvt(q_nan.d);\n    test_fconst();\n    test_fbcd(0.0);\n    test_fbcd(-0.0);\n    test_fbcd(1.0);\n    test_fbcd(-1.0);\n    test_fbcd(1234567890123456.0);\n    test_fbcd(-123451234567890.0);\n    test_fbcd(341234567890123456.0);\n    test_fbcd(-345123451234567890.0);\n    test_fbcd(999999999999999900.0);\n    test_fbcd(-999999999999999900.0);\n    test_fbcd(1000000000000000000.0);\n    test_fbcd(-1000000000000000000.0);\n    test_fbcd(1000000000000000000000.0);\n    test_fbcd(-1000000000000000000000.0);\n    test_fenv();\n    if (TEST_CMOV) {\n        test_fcmov();\n    }\n}\n\n/**********************************************/\n#if !defined(__x86_64__)\n\n#define TEST_BCD(op, op0, cc_in, cc_mask)\\\n{\\\n    int res, flags;\\\n    res = op0;\\\n    flags = cc_in;\\\n    asm (\"push %3\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         #op \"\\n\\t\"\\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n        : \"=a\" (res), \"=g\" (flags)\\\n        : \"0\" (res), \"1\" (flags));\\\n    printf(\"%-10s A=%08x R=%08x CCIN=%04x CC=%04x\\n\",\\\n           #op, op0, res, cc_in, flags & cc_mask);\\\n}\n\nvoid test_bcd(void)\n{\n    TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n\n    TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n    TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));\n\n    TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));\n    TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));\n\n    TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));\n    TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));\n    TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));\n    TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));\n    TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));\n    TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));\n    TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));\n    TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));\n\n    TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\n    TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\n}\n#endif\n\n#define TEST_XCHG(op, size, opconst)\\\n{\\\n    long op0, op1;\\\n    op0 = i2l(0x12345678);\\\n    op1 = i2l(0xfbca7654);\\\n    asm(#op \" %\" size \"0, %\" size \"1\" \\\n        : \"=q\" (op0), opconst (op1) \\\n        : \"0\" (op0));\\\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \"\\n\",\\\n           #op, op0, op1);\\\n}\n\n#define TEST_CMPXCHG(op, size, opconst, eax)\\\n{\\\n    long op0, op1, op2;\\\n    op0 = i2l(0x12345678);\\\n    op1 = i2l(0xfbca7654);\\\n    op2 = i2l(eax);\\\n    asm(#op \" %\" size \"0, %\" size \"1\" \\\n        : \"=q\" (op0), opconst (op1) \\\n        : \"0\" (op0), \"a\" (op2));\\\n    printf(\"%-10s EAX=\" FMTLX \" A=\" FMTLX \" C=\" FMTLX \"\\n\",\\\n           #op, op2, op0, op1);\\\n}\n\nvoid test_xchg(void)\n{\n#if defined(__x86_64__)\n    TEST_XCHG(xchgq, \"\", \"+q\");\n#endif\n    TEST_XCHG(xchgl, \"k\", \"+q\");\n    TEST_XCHG(xchgw, \"w\", \"+q\");\n    TEST_XCHG(xchgb, \"b\", \"+q\");\n\n#if defined(__x86_64__)\n    TEST_XCHG(xchgq, \"\", \"=m\");\n#endif\n    TEST_XCHG(xchgl, \"k\", \"+m\");\n    TEST_XCHG(xchgw, \"w\", \"+m\");\n    TEST_XCHG(xchgb, \"b\", \"+m\");\n\n#if defined(__x86_64__)\n    TEST_XCHG(xaddq, \"\", \"+q\");\n#endif\n    TEST_XCHG(xaddl, \"k\", \"+q\");\n    TEST_XCHG(xaddw, \"w\", \"+q\");\n    TEST_XCHG(xaddb, \"b\", \"+q\");\n\n    {\n        int res;\n        res = 0x12345678;\n        asm(\"xaddl %1, %0\" : \"=r\" (res) : \"0\" (res));\n        printf(\"xaddl same res=%08x\\n\", res);\n    }\n\n#if defined(__x86_64__)\n    TEST_XCHG(xaddq, \"\", \"+m\");\n#endif\n    TEST_XCHG(xaddl, \"k\", \"+m\");\n    TEST_XCHG(xaddw, \"w\", \"+m\");\n    TEST_XCHG(xaddb, \"b\", \"+m\");\n\n#if defined(__x86_64__)\n    TEST_CMPXCHG(cmpxchgq, \"\", \"+q\", 0xfbca7654);\n#endif\n    TEST_CMPXCHG(cmpxchgl, \"k\", \"+q\", 0xfbca7654);\n    TEST_CMPXCHG(cmpxchgw, \"w\", \"+q\", 0xfbca7654);\n    TEST_CMPXCHG(cmpxchgb, \"b\", \"+q\", 0xfbca7654);\n\n#if defined(__x86_64__)\n    TEST_CMPXCHG(cmpxchgq, \"\", \"+q\", 0xfffefdfc);\n#endif\n    TEST_CMPXCHG(cmpxchgl, \"k\", \"+q\", 0xfffefdfc);\n    TEST_CMPXCHG(cmpxchgw, \"w\", \"+q\", 0xfffefdfc);\n    TEST_CMPXCHG(cmpxchgb, \"b\", \"+q\", 0xfffefdfc);\n\n#if defined(__x86_64__)\n    TEST_CMPXCHG(cmpxchgq, \"\", \"+m\", 0xfbca7654);\n#endif\n    TEST_CMPXCHG(cmpxchgl, \"k\", \"+m\", 0xfbca7654);\n    TEST_CMPXCHG(cmpxchgw, \"w\", \"+m\", 0xfbca7654);\n    TEST_CMPXCHG(cmpxchgb, \"b\", \"+m\", 0xfbca7654);\n\n#if defined(__x86_64__)\n    TEST_CMPXCHG(cmpxchgq, \"\", \"+m\", 0xfffefdfc);\n#endif\n    TEST_CMPXCHG(cmpxchgl, \"k\", \"+m\", 0xfffefdfc);\n    TEST_CMPXCHG(cmpxchgw, \"w\", \"+m\", 0xfffefdfc);\n    TEST_CMPXCHG(cmpxchgb, \"b\", \"+m\", 0xfffefdfc);\n\n    {\n        uint64_t op0, op1, op2;\n        long eax, edx;\n        long i, eflags;\n\n        for(i = 0; i < 2; i++) {\n            op0 = 0x123456789abcdLL;\n            eax = i2l(op0 & 0xffffffff);\n            edx = i2l(op0 >> 32);\n            if (i == 0)\n                op1 = 0xfbca765423456LL;\n            else\n                op1 = op0;\n            op2 = 0x6532432432434LL;\n            asm(\"cmpxchg8b %2\\n\"\n                \"pushf\\n\"\n                \"pop %3\\n\"\n                : \"=a\" (eax), \"=d\" (edx), \"=m\" (op1), \"=g\" (eflags)\n                : \"0\" (eax), \"1\" (edx), \"m\" (op1), \"b\" ((int)op2), \"c\" ((int)(op2 >> 32)));\n            printf(\"cmpxchg8b: eax=\" FMTLX \" edx=\" FMTLX \" op1=\" FMT64X \" CC=%02lx\\n\",\n                   eax, edx, op1, eflags & CC_Z);\n        }\n    }\n}\n\n#ifdef TEST_SEGS\n/**********************************************/\n/* segmentation tests */\n\n#include <sys/syscall.h>\n#include <unistd.h>\n#include <asm/ldt.h>\n#include <linux/version.h>\n\nstatic inline int modify_ldt(int func, void * ptr, unsigned long bytecount)\n{\n    int result = syscall(__NR_modify_ldt, func, ptr, bytecount);\n    if(result == -1)\n    {\n        fprintf(stderr, \"Error: modify_ldt not available on this kernel. Check MODIFY_LDT_SYSCALL in /proc/config.gz.\\n\");\n        exit(1);\n    }\n    return result;\n}\n\n#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)\n#define modify_ldt_ldt_s user_desc\n#endif\n\n#define MK_SEL(n) (((n) << 3) | 7)\n\nuint8_t seg_data1[4096];\nuint8_t seg_data2[4096];\n\n#define TEST_LR(op, size, seg, mask)\\\n{\\\n    int res, res2;\\\n    uint16_t mseg = seg;\\\n    res = 0x12345678;\\\n    asm (op \" %\" size \"2, %\" size \"0\\n\" \\\n         \"movl $0, %1\\n\"\\\n         \"jnz 1f\\n\"\\\n         \"movl $1, %1\\n\"\\\n         \"1:\\n\"\\\n         : \"=r\" (res), \"=r\" (res2) : \"m\" (mseg), \"0\" (res));\\\n    printf(op \": Z=%d %08x\\n\", res2, res & ~(mask));\\\n}\n\n#define TEST_ARPL(op, size, op1, op2)\\\n{\\\n    long a, b, c;                               \\\n    a = (op1);                                  \\\n    b = (op2);                                  \\\n    asm volatile(op \" %\" size \"3, %\" size \"0\\n\"\\\n                 \"movl $0,%1\\n\"\\\n                 \"jnz 1f\\n\"\\\n                 \"movl $1,%1\\n\"\\\n                 \"1:\\n\"\\\n                 : \"=r\" (a), \"=r\" (c) : \"0\" (a), \"r\" (b));    \\\n    printf(op size \" A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" z=%ld\\n\",\\\n           (long)(op1), (long)(op2), a, c);\\\n}\n\n/* NOTE: we use Linux modify_ldt syscall */\nvoid test_segs(void)\n{\n    struct modify_ldt_ldt_s ldt;\n    long long ldt_table[3];\n    int res, res2;\n    char tmp;\n    struct {\n        uint32_t offset;\n        uint16_t seg;\n    } __attribute__((__packed__)) segoff;\n\n    ldt.entry_number = 1;\n    ldt.base_addr = (unsigned long)&seg_data1;\n    ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;\n    ldt.seg_32bit = 1;\n    ldt.contents = MODIFY_LDT_CONTENTS_DATA;\n    ldt.read_exec_only = 0;\n    ldt.limit_in_pages = 1;\n    ldt.seg_not_present = 0;\n    ldt.useable = 1;\n    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */\n\n    ldt.entry_number = 2;\n    ldt.base_addr = (unsigned long)&seg_data2;\n    ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;\n    ldt.seg_32bit = 1;\n    ldt.contents = MODIFY_LDT_CONTENTS_DATA;\n    ldt.read_exec_only = 0;\n    ldt.limit_in_pages = 1;\n    ldt.seg_not_present = 0;\n    ldt.useable = 1;\n    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */\n\n    modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */\n    {\n        int i;\n        for(i=0;i<3;i++)\n            printf(\"%d: %016Lx\\n\", i, ldt_table[i]);\n    }\n    /* do some tests with fs or gs */\n    asm volatile (\"movl %0, %%fs\" : : \"r\" (MK_SEL(1)));\n\n    seg_data1[1] = 0xaa;\n    seg_data2[1] = 0x55;\n\n    asm volatile (\"fs movzbl 0x1, %0\" : \"=r\" (res));\n    printf(\"FS[1] = %02x\\n\", res);\n\n    asm volatile (\"pushl %%gs\\n\"\n                  \"movl %1, %%gs\\n\"\n                  \"gs movzbl 0x1, %0\\n\"\n                  \"popl %%gs\\n\"\n                  : \"=r\" (res)\n                  : \"r\" (MK_SEL(2)));\n    printf(\"GS[1] = %02x\\n\", res);\n\n    /* tests with ds/ss (implicit segment case) */\n    tmp = 0xa5;\n    asm volatile (\"pushl %%ebp\\n\\t\"\n                  \"pushl %%ds\\n\\t\"\n                  \"movl %2, %%ds\\n\\t\"\n                  \"movl %3, %%ebp\\n\\t\"\n                  \"movzbl 0x1, %0\\n\\t\"\n                  \"movzbl (%%ebp), %1\\n\\t\"\n                  \"popl %%ds\\n\\t\"\n                  \"popl %%ebp\\n\\t\"\n                  : \"=r\" (res), \"=r\" (res2)\n                  : \"r\" (MK_SEL(1)), \"r\" (&tmp));\n    printf(\"DS[1] = %02x\\n\", res);\n    printf(\"SS[tmp] = %02x\\n\", res2);\n\n    segoff.seg = MK_SEL(2);\n    segoff.offset = 0xabcdef12;\n    asm volatile(\"lfs %2, %0\\n\\t\"\n                 \"movl %%fs, %1\\n\\t\"\n                 : \"=r\" (res), \"=g\" (res2)\n                 : \"m\" (segoff));\n    printf(\"FS:reg = %04x:%08x\\n\", res2, res);\n\n    TEST_LR(\"larw\", \"w\", MK_SEL(2), 0x0100);\n    TEST_LR(\"larl\", \"\", MK_SEL(2), 0x0100);\n    TEST_LR(\"lslw\", \"w\", MK_SEL(2), 0);\n    TEST_LR(\"lsll\", \"\", MK_SEL(2), 0);\n\n    TEST_LR(\"larw\", \"w\", 0xfff8, 0);\n    TEST_LR(\"larl\", \"\", 0xfff8, 0);\n    TEST_LR(\"lslw\", \"w\", 0xfff8, 0);\n    TEST_LR(\"lsll\", \"\", 0xfff8, 0);\n\n    TEST_ARPL(\"arpl\", \"w\", 0x12345678 | 3, 0x762123c | 1);\n    TEST_ARPL(\"arpl\", \"w\", 0x12345678 | 1, 0x762123c | 3);\n    TEST_ARPL(\"arpl\", \"w\", 0x12345678 | 1, 0x762123c | 1);\n}\n\n/* 16 bit code test */\nextern char code16_start, code16_end;\nextern char code16_func1;\nextern char code16_func2;\nextern char code16_func3;\n\nvoid test_code16(void)\n{\n    struct modify_ldt_ldt_s ldt;\n    int res, res2;\n\n    /* build a code segment */\n    ldt.entry_number = 1;\n    ldt.base_addr = (unsigned long)&code16_start;\n    ldt.limit = &code16_end - &code16_start;\n    ldt.seg_32bit = 0;\n    ldt.contents = MODIFY_LDT_CONTENTS_CODE;\n    ldt.read_exec_only = 0;\n    ldt.limit_in_pages = 0;\n    ldt.seg_not_present = 0;\n    ldt.useable = 1;\n    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */\n\n    /* call the first function */\n    // XXX: Temporarily disabled: Fails to compile on newer gcc, wait for upstream fix\n#if 0\n    asm volatile (\"lcall %1, %2\"\n                  : \"=a\" (res)\n                  : \"i\" (MK_SEL(1)), \"i\" (&code16_func1): \"memory\", \"cc\");\n    printf(\"func1() = 0x%08x\\n\", res);\n    asm volatile (\"lcall %2, %3\"\n                  : \"=a\" (res), \"=c\" (res2)\n                  : \"i\" (MK_SEL(1)), \"i\" (&code16_func2): \"memory\", \"cc\");\n    printf(\"func2() = 0x%08x spdec=%d\\n\", res, res2);\n    asm volatile (\"lcall %1, %2\"\n                  : \"=a\" (res)\n                  : \"i\" (MK_SEL(1)), \"i\" (&code16_func3): \"memory\", \"cc\");\n    printf(\"func3() = 0x%08x\\n\", res);\n#endif\n}\n#endif\n\n#if defined(__x86_64__)\nasm(\".globl func_lret\\n\"\n    \"func_lret:\\n\"\n    \"movl $0x87654641, %eax\\n\"\n    \"lretq\\n\");\n#else\nasm(\".globl func_lret\\n\"\n    \"func_lret:\\n\"\n    \"movl $0x87654321, %eax\\n\"\n    \"lret\\n\"\n\n    \".globl func_iret\\n\"\n    \"func_iret:\\n\"\n    \"movl $0xabcd4321, %eax\\n\"\n    \"iret\\n\");\n#endif\n\nextern char func_lret;\nextern char func_iret;\n\nvoid test_misc(void)\n{\n    char table[256];\n    long res, i;\n\n    for(i=0;i<256;i++) table[i] = 256 - i;\n    res = 0x12345678;\n    asm (\"xlat\" : \"=a\" (res) : \"b\" (table), \"0\" (res));\n    printf(\"xlat: EAX=\" FMTLX \"\\n\", res);\n\n#if defined(__x86_64__)\n#if 0\n    {\n        /* XXX: see if Intel Core2 and AMD64 behavior really\n           differ. Here we implemented the Intel way which is not\n           compatible yet with QEMU. */\n        static struct QEMU_PACKED {\n            uint64_t offset;\n            uint16_t seg;\n        } desc;\n        long cs_sel;\n\n        asm volatile (\"mov %%cs, %0\" : \"=r\" (cs_sel));\n\n        asm volatile (\"push %1\\n\"\n                      \"call func_lret\\n\"\n                      : \"=a\" (res)\n                      : \"r\" (cs_sel) : \"memory\", \"cc\");\n        printf(\"func_lret=\" FMTLX \"\\n\", res);\n\n        desc.offset = (long)&func_lret;\n        desc.seg = cs_sel;\n\n        asm volatile (\"xor %%rax, %%rax\\n\"\n                      \"rex64 lcall *(%%rcx)\\n\"\n                      : \"=a\" (res)\n                      : \"c\" (&desc)\n                      : \"memory\", \"cc\");\n        printf(\"func_lret2=\" FMTLX \"\\n\", res);\n\n        asm volatile (\"push %2\\n\"\n                      \"mov $ 1f, %%rax\\n\"\n                      \"push %%rax\\n\"\n                      \"rex64 ljmp *(%%rcx)\\n\"\n                      \"1:\\n\"\n                      : \"=a\" (res)\n                      : \"c\" (&desc), \"b\" (cs_sel)\n                      : \"memory\", \"cc\");\n        printf(\"func_lret3=\" FMTLX \"\\n\", res);\n    }\n#endif\n#else\n\n    // XXX: Temporarily disabled: Fails to compile on newer gcc, wait for upstream fix\n#if 0\n    asm volatile (\"push %%cs ; call %1\"\n                  : \"=a\" (res)\n                  : \"m\" (func_lret): \"memory\", \"cc\");\n    printf(\"func_lret=\" FMTLX \"\\n\", res);\n\n    asm volatile (\"pushf ; push %%cs ; call %1\"\n                  : \"=a\" (res)\n                  : \"m\" (func_iret): \"memory\", \"cc\");\n    printf(\"func_iret=\" FMTLX \"\\n\", res);\n#endif\n#endif\n\n#if defined(__x86_64__)\n    /* specific popl test */\n    asm volatile (\"push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0\"\n                  : \"=g\" (res));\n    printf(\"popl esp=\" FMTLX \"\\n\", res);\n#else\n    /* specific popl test */\n    asm volatile (\"pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0\"\n                  : \"=g\" (res));\n    printf(\"popl esp=\" FMTLX \"\\n\", res);\n\n    /* specific popw test */\n    asm volatile (\"pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0\"\n                  : \"=g\" (res));\n    printf(\"popw esp=\" FMTLX \"\\n\", res);\n#endif\n}\n\nvoid byte_read(uint8_t* buffer, uint16_t offset, size_t num_bytes);\n\n// 8 pages in every direction\n#define STR_BUFFER_SIZE (4096 * 16)\nuint8_t __attribute__((aligned (4096))) str_buffer[STR_BUFFER_SIZE];\n\n#define TEST_STRING1(OP, size_bytes, size, DF, REP, count, offset1, offset2)\\\n{\\\n    long esi, edi, eax, ecx, eflags, i;\\\n\\\n    for(i = 0; i < (count + 1) * size_bytes; i++) {\\\n        str_buffer[sizeof(str_buffer)/2 + offset1 + i] = i + 0x56;\\\n        str_buffer[sizeof(str_buffer)/2 + offset1 - i - 1] = i + 0x97;\\\n        str_buffer[sizeof(str_buffer)/2 + offset2 + i] = i + 0xa5;\\\n        str_buffer[sizeof(str_buffer)/2 + offset2 - i - 1] = i + 0x3e;\\\n    }\\\n    esi = (long)(str_buffer + sizeof(str_buffer)/2 + offset1);\\\n    edi = (long)(str_buffer + sizeof(str_buffer)/2 + offset2);\\\n    eax = i2l(0x12345678);\\\n    ecx = count;\\\n\\\n    asm volatile (\"push $0\\n\\t\"\\\n                  \"popf\\n\\t\"\\\n                  DF \"\\n\\t\"\\\n                  REP #OP size \"\\n\\t\"\\\n                  \"cld\\n\\t\"\\\n                  \"pushf\\n\\t\"\\\n                  \"pop %4\\n\\t\"\\\n                  : \"=S\" (esi), \"=D\" (edi), \"=a\" (eax), \"=c\" (ecx), \"=g\" (eflags)\\\n                  : \"0\" (esi), \"1\" (edi), \"2\" (eax), \"3\" (ecx));\\\n    printf(\"%-10s ESI=\" FMTLX \" EDI=\" FMTLX \" EAX=\" FMTLX \" ECX=\" FMTLX \" EFL=%04x\\n\",\\\n           REP #OP size, esi, edi, eax, ecx,\\\n           (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\\\n    byte_read(str_buffer, offset1, 16); \\\n    if(count > 16) byte_read(str_buffer, offset1 + count * size_bytes - 16, 16);\\\n    if(count > 16) byte_read(str_buffer, offset1 - count * size_bytes, 16);\\\n    byte_read(str_buffer, offset2, 16); \\\n    if(count > 16) byte_read(str_buffer, offset2 + count * size_bytes - 16, 16);\\\n    if(count > 16) byte_read(str_buffer, offset2 - count * size_bytes, 16);\\\n}\n\n#define TEST_STRING(OP, REP, count, offset1, offset2)\\\n    TEST_STRING1(OP, 1, \"b\", \"\", REP, count, offset1, offset2);\\\n    TEST_STRING1(OP, 2, \"w\", \"\", REP, count, offset1, offset2);\\\n    TEST_STRING1(OP, 4, \"l\", \"\", REP, count, offset1, offset2);\\\n    TEST_STRING1(OP, 1, \"b\", \"std\", REP, count, offset1, offset2);\\\n    TEST_STRING1(OP, 2, \"w\", \"std\", REP, count, offset1, offset2);\\\n    TEST_STRING1(OP, 4, \"l\", \"std\", REP, count, offset1, offset2);\n\nvoid test_string(void)\n{\n   TEST_STRING(stos, \"\", 17, 4096, 4096 + 64);\n   TEST_STRING(stos, \"rep \", 17, 4096, 4096 + 64);\n\n   TEST_STRING(lods, \"\", 17, 4096, 4096 + 64);\n   TEST_STRING(lods, \"rep \", 17, 4096, 4096 + 64);\n\n   TEST_STRING(movs, \"\", 17, 4096, 4096 + 64);\n   TEST_STRING(movs, \"rep \", 17, 4096, 4096 + 64);\n\n   /* XXX: better tests */\n   TEST_STRING(scas, \"\", 17, 4096, 4096 + 64);\n   TEST_STRING(scas, \"repz \", 17, 4096, 4096 + 64);\n   TEST_STRING(scas, \"repnz \", 17, 4096, 4096 + 64);\n\n   TEST_STRING(cmps, \"\", 17, 4096, 4096 + 64);\n   TEST_STRING(cmps, \"repz \", 17, 4096, 4096 + 64);\n   TEST_STRING(cmps, \"repnz \", 17, 4096, 4096 + 64);\n\n   int counts[] = { 0, 1, 2, 3, 4095, 4096, 4097, 2047, 2048, 2049, 1023, 1024, 1025 };\n   int offsets[] = { 0, 1, 2, 3, 4095, 4096, 4097, 2047, 2048, 2049, 1023, 1024, 1025 };\n\n   for(int count = 0; count < sizeof(counts) / sizeof(int); count++)\n   {\n       for(int offset1 = 0; offset1 < sizeof(offsets) / sizeof(int); offset1++)\n       {\n           TEST_STRING(stos, \"rep \", counts[count], offsets[offset1], offsets[offset1]);\n\n           for(int offset2 = 0; offset2 < sizeof(offsets) / sizeof(int); offset2++)\n           {\n               TEST_STRING(movs, \"rep \", counts[count], offsets[offset1], offsets[offset2]);\n           }\n       }\n   }\n}\n\n#ifdef TEST_VM86\n/* VM86 test */\n\nstatic inline void set_bit(uint8_t *a, unsigned int bit)\n{\n    a[bit / 8] |= (1 << (bit % 8));\n}\n\nstatic inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)\n{\n    return (uint8_t *)((seg << 4) + (reg & 0xffff));\n}\n\nstatic inline void pushw(struct vm86_regs *r, int val)\n{\n    r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);\n    *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;\n}\n\nstatic inline int vm86(int func, struct vm86plus_struct *v86)\n{\n    return syscall(__NR_vm86, func, v86);\n}\n\nextern char vm86_code_start;\nextern char vm86_code_end;\n\n#define VM86_CODE_CS 0x100\n#define VM86_CODE_IP 0x100\n\nvoid test_vm86(void)\n{\n    struct vm86plus_struct ctx;\n    struct vm86_regs *r;\n    uint8_t *vm86_mem;\n    int seg, ret;\n\n    vm86_mem = mmap((void *)0x00000000, 0x110000,\n                    PROT_WRITE | PROT_READ | PROT_EXEC,\n                    MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);\n    if (vm86_mem == MAP_FAILED) {\n        printf(\"ERROR: could not map vm86 memory\");\n        return;\n    }\n    memset(&ctx, 0, sizeof(ctx));\n\n    /* init basic registers */\n    r = &ctx.regs;\n    r->eip = VM86_CODE_IP;\n    r->esp = 0xfffe;\n    seg = VM86_CODE_CS;\n    r->cs = seg;\n    r->ss = seg;\n    r->ds = seg;\n    r->es = seg;\n    r->fs = seg;\n    r->gs = seg;\n    //r->eflags = VIF_MASK;\n\n    /* move code to proper address. We use the same layout as a .com\n       dos program. */\n    memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,\n           &vm86_code_start, &vm86_code_end - &vm86_code_start);\n\n    /* mark int 0x21 as being emulated */\n    set_bit((uint8_t *)&ctx.int_revectored, 0x21);\n\n    for(;;) {\n        ret = vm86(VM86_ENTER, &ctx);\n        switch(VM86_TYPE(ret)) {\n        case VM86_INTx:\n            {\n                int int_num, ah, v;\n\n                int_num = VM86_ARG(ret);\n                if (int_num != 0x21)\n                    goto unknown_int;\n                ah = (r->eax >> 8) & 0xff;\n                switch(ah) {\n                case 0x00: /* exit */\n                    goto the_end;\n                case 0x02: /* write char */\n                    {\n                        uint8_t c = r->edx;\n                        putchar(c);\n                    }\n                    break;\n                case 0x09: /* write string */\n                    {\n                        uint8_t c, *ptr;\n                        ptr = seg_to_linear(r->ds, r->edx);\n                        for(;;) {\n                            c = *ptr++;\n                            if (c == '$')\n                                break;\n                            putchar(c);\n                        }\n                        r->eax = (r->eax & ~0xff) | '$';\n                    }\n                    break;\n                case 0xff: /* extension: write eflags number in edx */\n                    v = (int)r->edx;\n#ifndef LINUX_VM86_IOPL_FIX\n                    v &= ~0x3000;\n#endif\n                    printf(\"%08x\\n\", v);\n                    break;\n                default:\n                unknown_int:\n                    printf(\"unsupported int 0x%02x\\n\", int_num);\n                    goto the_end;\n                }\n            }\n            break;\n        case VM86_SIGNAL:\n            /* a signal came, we just ignore that */\n            break;\n        case VM86_STI:\n            break;\n        default:\n            printf(\"ERROR: unhandled vm86 return code (0x%x)\\n\", ret);\n            goto the_end;\n        }\n    }\n the_end:\n    printf(\"VM86 end\\n\");\n    munmap(vm86_mem, 0x110000);\n}\n#endif\n\n/* exception tests */\n#if defined(__i386__) && !defined(REG_EAX)\n#define REG_EAX EAX\n#define REG_EBX EBX\n#define REG_ECX ECX\n#define REG_EDX EDX\n#define REG_ESI ESI\n#define REG_EDI EDI\n#define REG_EBP EBP\n#define REG_ESP ESP\n#define REG_EIP EIP\n#define REG_EFL EFL\n#define REG_TRAPNO TRAPNO\n#define REG_ERR ERR\n#endif\n\n#if defined(__x86_64__)\n#define REG_EIP REG_RIP\n#endif\n\njmp_buf jmp_env;\nint v1;\nint tab[2];\n\nvoid sig_handler(int sig, siginfo_t *info, void *puc)\n{\n    ucontext_t *uc = puc;\n\n    printf(\"si_signo=%d si_errno=%d si_code=%d\",\n           info->si_signo, info->si_errno, info->si_code);\n    printf(\" si_addr=0x%08lx\",\n           (unsigned long)info->si_addr);\n    printf(\"\\n\");\n\n    printf(\"trapno=\" FMTLX \" err=\" FMTLX,\n           (long)uc->uc_mcontext.gregs[REG_TRAPNO],\n           (long)uc->uc_mcontext.gregs[REG_ERR]);\n    printf(\" EIP=\" FMTLX, (long)uc->uc_mcontext.gregs[REG_EIP]);\n    printf(\"\\n\");\n    longjmp(jmp_env, 1);\n}\n\nvoid test_exceptions(void)\n{\n    struct sigaction act;\n    volatile int val;\n\n    act.sa_sigaction = sig_handler;\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = SA_SIGINFO | SA_NODEFER;\n    sigaction(SIGFPE, &act, NULL);\n    sigaction(SIGILL, &act, NULL);\n    sigaction(SIGSEGV, &act, NULL);\n    sigaction(SIGBUS, &act, NULL);\n    sigaction(SIGTRAP, &act, NULL);\n\n    /* test division by zero reporting */\n    printf(\"DIVZ exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        /* now divide by zero */\n        v1 = 0;\n        v1 = 2 / v1;\n    }\n\n#if 0\n#if !defined(__x86_64__)\n    printf(\"BOUND exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        /* bound exception */\n        tab[0] = 1;\n        tab[1] = 10;\n        asm volatile (\"bound %0, %1\" : : \"r\" (11), \"m\" (tab[0]));\n    }\n#endif\n#endif\n\n#ifdef TEST_SEGS\n    printf(\"segment exceptions:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        /* load an invalid segment */\n        asm volatile (\"movl %0, %%fs\" : : \"r\" ((0x1234 << 3) | 1));\n    }\n    if (setjmp(jmp_env) == 0) {\n        /* null data segment is valid */\n        asm volatile (\"movl %0, %%fs\" : : \"r\" (3));\n        /* null stack segment */\n        asm volatile (\"movl %0, %%ss\" : : \"r\" (3));\n    }\n\n    {\n        struct modify_ldt_ldt_s ldt;\n        ldt.entry_number = 1;\n        ldt.base_addr = (unsigned long)&seg_data1;\n        ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;\n        ldt.seg_32bit = 1;\n        ldt.contents = MODIFY_LDT_CONTENTS_DATA;\n        ldt.read_exec_only = 0;\n        ldt.limit_in_pages = 1;\n        ldt.seg_not_present = 1;\n        ldt.useable = 1;\n        modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */\n\n        if (setjmp(jmp_env) == 0) {\n            /* segment not present */\n            asm volatile (\"movl %0, %%fs\" : : \"r\" (MK_SEL(1)));\n        }\n    }\n#endif\n\n    /* test SEGV reporting */\n    printf(\"PF exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        val = 1;\n        /* we add a nop to test a weird PC retrieval case */\n        asm volatile (\"nop\");\n        /* now store in an invalid address */\n        *(char *)0x1234 = 1;\n    }\n\n    /* test SEGV reporting */\n    printf(\"PF exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        val = 1;\n        /* read from an invalid address */\n        v1 = *(char *)0x1234;\n    }\n\n    /* test illegal instruction reporting */\n    printf(\"UD2 exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        /* now execute an invalid instruction */\n        asm volatile(\"ud2\");\n    }\n#if 0\n    printf(\"lock nop exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        /* now execute an invalid instruction */\n        asm volatile(\".byte 0xf0, 0x90\");\n    }\n#endif\n\n    printf(\"INT exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"int $0xfd\");\n    }\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"int $0x01\");\n    }\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\".byte 0xcd, 0x03\");\n    }\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"int $0x04\");\n    }\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"int $0x05\");\n    }\n\n    printf(\"INT3 exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"int3\");\n    }\n\n    printf(\"CLI exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"cli\");\n    }\n\n    printf(\"STI exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"cli\");\n    }\n\n#if !defined(__x86_64__)\n    printf(\"INTO exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        /* overflow exception */\n        asm volatile (\"addl $1, %0 ; into\" : : \"r\" (0x7fffffff));\n    }\n#endif\n\n    printf(\"OUTB exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"outb %%al, %%dx\" : : \"d\" (0x4321), \"a\" (0));\n    }\n\n    printf(\"INB exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"inb %%dx, %%al\" : \"=a\" (val) : \"d\" (0x4321));\n    }\n\n    printf(\"REP OUTSB exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"rep outsb\" : : \"d\" (0x4321), \"S\" (tab), \"c\" (1));\n    }\n\n    printf(\"REP INSB exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"rep insb\" : : \"d\" (0x4321), \"D\" (tab), \"c\" (1));\n    }\n\n    printf(\"HLT exception:\\n\");\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"hlt\");\n    }\n\n#if 0\n    printf(\"single step exception:\\n\");\n    val = 0;\n    if (setjmp(jmp_env) == 0) {\n        asm volatile (\"pushf\\n\"\n                      \"orl $0x00100, (%%esp)\\n\"\n                      \"popf\\n\"\n                      \"movl $0xabcd, %0\\n\"\n                      \"movl $0x0, %0\\n\" : \"=m\" (val) : : \"cc\", \"memory\");\n    }\n    printf(\"val=0x%x\\n\", val);\n#endif\n}\n\n#if !defined(__x86_64__)\n/* specific precise single step test */\nvoid sig_trap_handler(int sig, siginfo_t *info, void *puc)\n{\n    ucontext_t *uc = puc;\n    printf(\"EIP=\" FMTLX \"\\n\", (long)uc->uc_mcontext.gregs[REG_EIP]);\n}\n\nconst uint8_t sstep_buf1[4] = { 1, 2, 3, 4};\nuint8_t sstep_buf2[4];\n\nvoid test_single_step(void)\n{\n    struct sigaction act;\n    volatile int val;\n    int i;\n\n    val = 0;\n    act.sa_sigaction = sig_trap_handler;\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = SA_SIGINFO;\n    sigaction(SIGTRAP, &act, NULL);\n    asm volatile (\"pushf\\n\"\n                  \"orl $0x00100, (%%esp)\\n\"\n                  \"popf\\n\"\n                  \"movl $0xabcd, %0\\n\"\n\n                  /* jmp test */\n                  \"movl $3, %%ecx\\n\"\n                  \"1:\\n\"\n                  \"addl $1, %0\\n\"\n                  \"decl %%ecx\\n\"\n                  \"jnz 1b\\n\"\n\n                  /* movsb: the single step should stop at each movsb iteration */\n                  \"movl $sstep_buf1, %%esi\\n\"\n                  \"movl $sstep_buf2, %%edi\\n\"\n                  \"movl $0, %%ecx\\n\"\n                  \"rep movsb\\n\"\n                  \"movl $3, %%ecx\\n\"\n                  \"rep movsb\\n\"\n                  \"movl $1, %%ecx\\n\"\n                  \"rep movsb\\n\"\n\n                  /* cmpsb: the single step should stop at each cmpsb iteration */\n                  \"movl $sstep_buf1, %%esi\\n\"\n                  \"movl $sstep_buf2, %%edi\\n\"\n                  \"movl $0, %%ecx\\n\"\n                  \"rep cmpsb\\n\"\n                  \"movl $4, %%ecx\\n\"\n                  \"rep cmpsb\\n\"\n\n                  /* getpid() syscall: single step should skip one\n                     instruction */\n                  \"movl $20, %%eax\\n\"\n                  \"int $0x80\\n\"\n                  \"movl $0, %%eax\\n\"\n\n                  /* when modifying SS, trace is not done on the next\n                     instruction */\n                  \"movl %%ss, %%ecx\\n\"\n                  \"movl %%ecx, %%ss\\n\"\n                  \"addl $1, %0\\n\"\n                  \"movl $1, %%eax\\n\"\n                  \"movl %%ecx, %%ss\\n\"\n                  \"jmp 1f\\n\"\n                  \"addl $1, %0\\n\"\n                  \"1:\\n\"\n                  \"movl $1, %%eax\\n\"\n                  \"pushl %%ecx\\n\"\n                  \"popl %%ss\\n\"\n                  \"addl $1, %0\\n\"\n                  \"movl $1, %%eax\\n\"\n\n                  \"pushf\\n\"\n                  \"andl $~0x00100, (%%esp)\\n\"\n                  \"popf\\n\"\n                  : \"=m\" (val)\n                  :\n                  : \"cc\", \"memory\", \"eax\", \"ecx\", \"esi\", \"edi\");\n    printf(\"val=%d\\n\", val);\n    for(i = 0; i < 4; i++)\n        printf(\"sstep_buf2[%d] = %d\\n\", i, sstep_buf2[i]);\n}\n\n/* self modifying code test */\nuint8_t code[] = {\n    0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */\n    0xc3, /* ret */\n};\n\nasm(\".section \\\".data\\\"\\n\"\n    \"smc_code2:\\n\"\n    \"movl 4(%esp), %eax\\n\"\n    \"movl %eax, smc_patch_addr2 + 1\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"nop\\n\"\n    \"smc_patch_addr2:\\n\"\n    \"movl $1, %eax\\n\"\n    \"ret\\n\"\n    \".previous\\n\"\n    );\n\ntypedef int FuncType(void);\nextern int smc_code2(int);\nvoid test_self_modifying_code(void)\n{\n    int i;\n    printf(\"self modifying code:\\n\");\n    printf(\"func1 = 0x%x\\n\", ((FuncType *)code)());\n    for(i = 2; i <= 4; i++) {\n        code[1] = i;\n        printf(\"func%d = 0x%x\\n\", i, ((FuncType *)code)());\n    }\n\n    /* more difficult test : the modified code is just after the\n       modifying instruction. It is forbidden in Intel specs, but it\n       is used by old DOS programs */\n    for(i = 2; i <= 4; i++) {\n        printf(\"smc_code2(%d) = %d\\n\", i, smc_code2(i));\n    }\n}\n#endif\n\nlong enter_stack[4096];\n\n#if defined(__x86_64__)\n#define RSP \"%%rsp\"\n#define RBP \"%%rbp\"\n#else\n#define RSP \"%%esp\"\n#define RBP \"%%ebp\"\n#endif\n\n#if !defined(__x86_64__)\n/* causes an infinite loop, disable it for now.  */\n#define TEST_ENTER(size, stack_type, level)\n#else\n#define TEST_ENTER(size, stack_type, level)\\\n{\\\n    long esp_save, esp_val, ebp_val, ebp_save, i;\\\n    stack_type *ptr, *stack_end, *stack_ptr;\\\n    memset(enter_stack, 0, sizeof(enter_stack));\\\n    stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\\\n    ebp_val = (long)stack_ptr;\\\n    for(i=1;i<=32;i++)\\\n       *--stack_ptr = i;\\\n    esp_val = (long)stack_ptr;\\\n    asm(\"mov \" RSP \", %[esp_save]\\n\"\\\n        \"mov \" RBP \", %[ebp_save]\\n\"\\\n        \"mov %[esp_val], \" RSP \"\\n\"\\\n        \"mov %[ebp_val], \" RBP \"\\n\"\\\n        \"enter\" size \" $8, $\" #level \"\\n\"\\\n        \"mov \" RSP \", %[esp_val]\\n\"\\\n        \"mov \" RBP \", %[ebp_val]\\n\"\\\n        \"mov %[esp_save], \" RSP \"\\n\"\\\n        \"mov %[ebp_save], \" RBP \"\\n\"\\\n        : [esp_save] \"=r\" (esp_save),\\\n        [ebp_save] \"=r\" (ebp_save),\\\n        [esp_val] \"=r\" (esp_val),\\\n        [ebp_val] \"=r\" (ebp_val)\\\n        :  \"[esp_val]\" (esp_val),\\\n        \"[ebp_val]\" (ebp_val));\\\n    printf(\"level=%d:\\n\", level);\\\n    printf(\"esp_val=\" FMTLX \"\\n\", esp_val - (long)stack_end);\\\n    printf(\"ebp_val=\" FMTLX \"\\n\", ebp_val - (long)stack_end);\\\n    for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\\\n        printf(FMTLX \"\\n\", (long)ptr[0]);\\\n}\n#endif\n\nstatic void test_enter(void)\n{\n#if defined(__x86_64__)\n    TEST_ENTER(\"q\", uint64_t, 0);\n    TEST_ENTER(\"q\", uint64_t, 1);\n    TEST_ENTER(\"q\", uint64_t, 2);\n    TEST_ENTER(\"q\", uint64_t, 31);\n#else\n    TEST_ENTER(\"l\", uint32_t, 0);\n    TEST_ENTER(\"l\", uint32_t, 1);\n    TEST_ENTER(\"l\", uint32_t, 2);\n    TEST_ENTER(\"l\", uint32_t, 31);\n#endif\n\n    TEST_ENTER(\"w\", uint16_t, 0);\n    TEST_ENTER(\"w\", uint16_t, 1);\n    TEST_ENTER(\"w\", uint16_t, 2);\n    TEST_ENTER(\"w\", uint16_t, 31);\n}\n\n#ifdef TEST_SSE\n\ntypedef int __m64 __attribute__ ((vector_size(8)));\ntypedef float __m128 __attribute__ ((vector_size(16)));\n\ntypedef union {\n    double d[2];\n    float s[4];\n    uint32_t l[4];\n    uint64_t q[2];\n    __m128 dq;\n} XMMReg;\n\nstatic uint64_t __attribute__((aligned(16))) test_values[4][2] = {\n    { 0x456723c698694873, 0xdc515cff944a58ec },\n    { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },\n    { 0x007c62c2085427f8, 0x231be9e8cde7438d },\n    { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },\n};\n\n#define SSE_OP(op)\\\n{\\\n    asm volatile (#op \" %2, %0\" : \"=x\" (r.dq) : \"0\" (a.dq), \"x\" (b.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" b=\" FMT64X \"\" FMT64X \" r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           b.q[1], b.q[0],\\\n           r.q[1], r.q[0]);\\\n}\n\n#define SSE_OP2(op)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    a.q[1] = test_values[2*i][1];\\\n    b.q[0] = test_values[2*i+1][0];\\\n    b.q[1] = test_values[2*i+1][1];\\\n    SSE_OP(op);\\\n    }\\\n}\n\n#define MMX_OP2(op)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    b.q[0] = test_values[2*i+1][0];\\\n    asm volatile (#op \" %2, %0\" : \"=y\" (r.q[0]) : \"0\" (a.q[0]), \"y\" (b.q[0]));\\\n    printf(\"%-9s: a=\" FMT64X \" b=\" FMT64X \" r=\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[0],\\\n           b.q[0],\\\n           r.q[0]);\\\n    }\\\n    SSE_OP2(op);\\\n}\n\n\n#define SHUF_OP_MMX(op, ib)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    b.q[0] = test_values[2*i+1][0];\\\n    asm volatile (#op \" $\" #ib \", %2, %0\" : \"=y\" (r.q[0]) : \"0\" (a.q[0]), \"y\" (b.q[0])); \\\n    printf(\"%-9s: a=\" FMT64X \" b=\" FMT64X \" ib=%02x r=\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[0],\\\n           b.q[0],\\\n           ib,\\\n           r.q[0]);\\\n    }\\\n}\n\n#define SHUF_OP(op, ib)\\\n{\\\n    a.q[0] = test_values[0][0];\\\n    a.q[1] = test_values[0][1];\\\n    b.q[0] = test_values[1][0];\\\n    b.q[1] = test_values[1][1];\\\n    asm volatile (#op \" $\" #ib \", %2, %0\" : \"=x\" (r.dq) : \"0\" (a.dq), \"x\" (b.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" b=\" FMT64X \"\" FMT64X \" ib=%02x r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           b.q[1], b.q[0],\\\n           ib,\\\n           r.q[1], r.q[0]);\\\n}\n\n#define PSHUF_OP(op, ib)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    a.q[1] = test_values[2*i][1];\\\n    asm volatile (#op \" $\" #ib \", %1, %0\" : \"=x\" (r.dq) : \"x\" (a.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" ib=%02x r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           ib,\\\n           r.q[1], r.q[0]);\\\n    }\\\n}\n\n// To use mm0-7 registers instead of xmm registers\n#define SHIFT_IM_MMX(op, ib)                        \\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    asm volatile (#op \" $\" #ib \", %0\" : \"=y\" (r.q[0]) : \"0\" (a.q[0]));\\\n    printf(\"%-9s: a=\" FMT64X \" ib=%02x r=\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[0],\\\n           ib,\\\n           r.q[0]);\\\n    }\\\n}\n\n#define SHIFT_IM(op, ib)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    a.q[1] = test_values[2*i][1];\\\n    asm volatile (#op \" $\" #ib \", %0\" : \"=x\" (r.dq) : \"0\" (a.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" ib=%02x r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           ib,\\\n           r.q[1], r.q[0]);\\\n    }\\\n}\n\n#define SHIFT_REG_MMX(op, ib)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    b.q[0] = ib;\\\n    asm volatile (#op \" %2, %0\" : \"=y\" (r.q[0]) : \"0\" (a.q[0]), \"y\" (b.q[0]));\\\n    printf(\"%-9s: a=\" FMT64X \" b=\" FMT64X \" ib=%02llx r=\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[0],\\\n           b.q[0],\\\n           (uint64_t)ib,\\\n           r.q[0]);\\\n    }\\\n}\n\n// To use mm0-7 registers instead of xmm registers\n#define SHIFT_OP_MMX(op, ib)\\\n{\\\n    SHIFT_IM_MMX(op, ib);\\\n    SHIFT_REG_MMX(op, ib);\\\n}\n\n#define SHIFT_REG(op, ib)\\\n{\\\n    int i;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    a.q[1] = test_values[2*i][1];\\\n    b.q[0] = ib;\\\n    b.q[1] = 0;\\\n    asm volatile (#op \" %2, %0\" : \"=x\" (r.dq) : \"0\" (a.dq), \"x\" (b.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" b=\" FMT64X \"\" FMT64X \" r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           b.q[1], b.q[0],\\\n           r.q[1], r.q[0]);\\\n    }\\\n}\n\n\n#define SHIFT_OP(op, ib)\\\n{\\\n    SHIFT_OP_MMX(op, ib)\\\n    SHIFT_IM(op, ib);\\\n    SHIFT_REG(op, ib);\\\n}\n\n#define MOVMSK(op)\\\n{\\\n    int i, reg;\\\n    for(i=0;i<sizeof(test_values)/sizeof(uint64_t)/4;i++) {\\\n    a.q[0] = test_values[2*i][0];\\\n    a.q[1] = test_values[2*i][1];\\\n    asm volatile (#op \" %1, %0\" : \"=r\" (reg) : \"x\" (a.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" r=%08x\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           reg);\\\n    }\\\n}\n\n#define SSE_OPS(a) \\\nSSE_OP(a ## ps);\\\nSSE_OP(a ## ss);\n\n#define SSE_OPD(a) \\\nSSE_OP(a ## pd);\\\nSSE_OP(a ## sd);\n\n#define SSE_COMI(op, field)\\\n{\\\n    unsigned long eflags;\\\n    XMMReg a, b;\\\n    a.field[0] = a1;\\\n    b.field[0] = b1;\\\n    asm volatile (#op \" %2, %1\\n\"\\\n        \"pushf\\n\"\\\n        \"pop %0\\n\"\\\n        : \"=rm\" (eflags)\\\n        : \"x\" (a.dq), \"x\" (b.dq));\\\n    printf(\"%-9s: a=%f b=%f cc=%04lx\\n\",\\\n           #op, a1, b1,\\\n           eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\\\n}\n\nvoid test_sse_comi(double a1, double b1)\n{\n    SSE_COMI(ucomiss, s);\n    SSE_COMI(ucomisd, d);\n    SSE_COMI(comiss, s);\n    SSE_COMI(comisd, d);\n}\n\n#define CVT_OP_XMM(op)\\\n{\\\n    asm volatile (#op \" %1, %0\" : \"+x\" (r.dq) : \"x\" (a.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           r.q[1], r.q[0]);\\\n}\n\n/* Force %xmm0 usage to avoid the case where both register index are 0\n   to test instruction decoding more extensively */\n#define CVT_OP_XMM2MMX(op)\\\n{\\\n    asm volatile (#op \" %1, %0\" : \"=y\" (r.q[0]) : \"x\" (a.dq) \\\n                  : \"%xmm0\"); \\\n    asm volatile(\"emms\\n\"); \\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" r=\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           r.q[0]);\\\n}\n\n#define CVT_OP_MMX2XMM(op)\\\n{\\\n    asm volatile (#op \" %1, %0\" : \"=x\" (r.dq) : \"y\" (a.q[0]));\\\n    asm volatile(\"emms\\n\"); \\\n    printf(\"%-9s: a=\" FMT64X \" r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.q[0],\\\n           r.q[1], r.q[0]);\\\n}\n\n#define CVT_OP_REG2XMM(op)\\\n{\\\n    asm volatile (#op \" %1, %0\" : \"+x\" (r.dq) : \"r\" (a.l[0]));\\\n    printf(\"%-9s: a=%08x r=\" FMT64X \"\" FMT64X \"\\n\",\\\n           #op,\\\n           a.l[0],\\\n           r.q[1], r.q[0]);\\\n}\n\n#define CVT_OP_XMM2REG(op)\\\n{\\\n    asm volatile (#op \" %1, %0\" : \"=r\" (r.l[0]) : \"x\" (a.dq));\\\n    printf(\"%-9s: a=\" FMT64X \"\" FMT64X \" r=%08x\\n\",\\\n           #op,\\\n           a.q[1], a.q[0],\\\n           r.l[0]);\\\n}\n\nstruct fpxstate {\n    uint16_t fpuc;\n    uint16_t fpus;\n    uint16_t fptag;\n    uint16_t fop;\n    uint32_t fpuip;\n    uint16_t cs_sel;\n    uint16_t dummy0;\n    uint32_t fpudp;\n    uint16_t ds_sel;\n    uint16_t dummy1;\n    uint32_t mxcsr;\n    uint32_t mxcsr_mask;\n    uint8_t fpregs1[8 * 16];\n    uint8_t xmm_regs[8 * 16];\n    uint8_t dummy2[224];\n};\n\nstatic struct fpxstate fpx_state __attribute__((aligned(16)));\nstatic struct fpxstate fpx_state2 __attribute__((aligned(16)));\n\nvoid test_fxsave(void)\n{\n    struct fpxstate *fp = &fpx_state;\n    struct fpxstate *fp2 = &fpx_state2;\n    int i, nb_xmm;\n    XMMReg a, b;\n    a.q[0] = test_values[0][0];\n    a.q[1] = test_values[0][1];\n    b.q[0] = test_values[1][0];\n    b.q[1] = test_values[1][1];\n\n    asm(\"movdqa %2, %%xmm0\\n\"\n        \"movdqa %3, %%xmm7\\n\"\n#if defined(__x86_64__)\n        \"movdqa %2, %%xmm15\\n\"\n#endif\n        \" fninit\\n\"\n        \" fld1\\n\"\n        \" fld1\\n\"\n        \" fldz\\n\"\n        \" fxsave %0\\n\"\n        \" fxrstor %0\\n\"\n        \" fxsave %1\\n\"\n        \" fninit\\n\"\n        : \"=m\" (*(uint32_t *)fp2), \"=m\" (*(uint32_t *)fp)\n        : \"m\" (a), \"m\" (b));\n    printf(\"fpuc=%04x\\n\", fp->fpuc);\n    printf(\"fpus=%04x\\n\", fp->fpus);\n    printf(\"fptag=%04x\\n\", fp->fptag);\n    for(i = 0; i < 3; i++) {\n        printf(\"ST%d: \" FMT64X \" %04x\\n\",\n               i,\n               *(uint64_t *)&fp->fpregs1[i * 16],\n               *(uint16_t *)&fp->fpregs1[i * 16 + 8]);\n    }\n    printf(\"mxcsr=%08x\\n\", fp->mxcsr & 0x1f80);\n#if defined(__x86_64__)\n    nb_xmm = 16;\n#else\n    nb_xmm = 8;\n#endif\n    for(i = 0; i < nb_xmm; i++) {\n        printf(\"xmm%d: \" FMT64X \"\" FMT64X \"\\n\",\n               i,\n               *(uint64_t *)&fp->xmm_regs[i * 16],\n               *(uint64_t *)&fp->xmm_regs[i * 16 + 8]);\n    }\n}\n\nvoid test_sse(void)\n{\n    XMMReg r, a, b;\n    int i;\n\n    MMX_OP2(punpcklbw);\n    MMX_OP2(punpcklwd);\n    MMX_OP2(punpckldq);\n    MMX_OP2(packsswb);\n    MMX_OP2(pcmpgtb);\n    MMX_OP2(pcmpgtw);\n    MMX_OP2(pcmpgtd);\n    MMX_OP2(packuswb);\n    MMX_OP2(punpckhbw);\n    MMX_OP2(punpckhwd);\n    MMX_OP2(punpckhdq);\n    MMX_OP2(packssdw);\n    MMX_OP2(pcmpeqb);\n    MMX_OP2(pcmpeqw);\n    MMX_OP2(pcmpeqd);\n\n    MMX_OP2(paddq);\n    MMX_OP2(pmullw);\n    MMX_OP2(psubusb);\n    MMX_OP2(psubusw);\n    MMX_OP2(pminub);\n    MMX_OP2(pand);\n    MMX_OP2(paddusb);\n    MMX_OP2(paddusw);\n    MMX_OP2(pmaxub);\n    MMX_OP2(pandn);\n\n    MMX_OP2(pmulhuw);\n    MMX_OP2(pmulhw);\n\n    MMX_OP2(psubsb);\n    MMX_OP2(psubsw);\n    MMX_OP2(pminsw);\n    MMX_OP2(por);\n    MMX_OP2(paddsb);\n    MMX_OP2(paddsw);\n    MMX_OP2(pmaxsw);\n    MMX_OP2(pxor);\n    MMX_OP2(pmuludq);\n    MMX_OP2(pmaddwd);\n    MMX_OP2(psadbw);\n    MMX_OP2(psubb);\n    MMX_OP2(psubw);\n    MMX_OP2(psubd);\n    MMX_OP2(psubq);\n    MMX_OP2(paddb);\n    MMX_OP2(paddw);\n    MMX_OP2(psrlw);\n    MMX_OP2(paddd);\n\n    MMX_OP2(pavgb);\n    MMX_OP2(pavgw);\n\n    asm volatile (\"pinsrw $1, %1, %0\" : \"+y\" (r.q[0]) : \"r\" (0x12345678));\n    printf(\"%-9s: r=\" FMT64X \"\\n\", \"pinsrw\", r.q[0]);\n\n    asm volatile (\"pinsrw $5, %1, %0\" : \"+x\" (r.dq) : \"r\" (0x12345678));\n    printf(\"%-9s: r=\" FMT64X \"\" FMT64X \"\\n\", \"pinsrw\", r.q[1], r.q[0]);\n\n    a.q[0] = test_values[0][0];\n    a.q[1] = test_values[0][1];\n    asm volatile (\"pextrw $1, %1, %0\" : \"=r\" (r.l[0]) : \"y\" (a.q[0]));\n    printf(\"%-9s: r=%08x\\n\", \"pextrw\", r.l[0]);\n\n    asm volatile (\"pextrw $5, %1, %0\" : \"=r\" (r.l[0]) : \"x\" (a.dq));\n    printf(\"%-9s: r=%08x\\n\", \"pextrw\", r.l[0]);\n\n    asm volatile (\"pmovmskb %1, %0\" : \"=r\" (r.l[0]) : \"y\" (a.q[0]));\n    printf(\"%-9s: r=%08x\\n\", \"pmovmskb\", r.l[0]);\n\n    asm volatile (\"pmovmskb %1, %0\" : \"=r\" (r.l[0]) : \"x\" (a.dq));\n    printf(\"%-9s: r=%08x\\n\", \"pmovmskb\", r.l[0]);\n\n    {\n        r.q[0] = -1;\n        r.q[1] = -1;\n\n        a.q[0] = test_values[0][0];\n        a.q[1] = test_values[0][1];\n        b.q[0] = test_values[1][0];\n        b.q[1] = test_values[1][1];\n        asm volatile(\"maskmovq %1, %0\" :\n                     : \"y\" (a.q[0]), \"y\" (b.q[0]), \"D\" (&r)\n                     : \"memory\");\n        printf(\"%-9s: r=\" FMT64X \" a=\" FMT64X \" b=\" FMT64X \"\\n\",\n               \"maskmov\",\n               r.q[0],\n               a.q[0],\n               b.q[0]);\n        asm volatile(\"maskmovdqu %1, %0\" :\n                     : \"x\" (a.dq), \"x\" (b.dq), \"D\" (&r)\n                     : \"memory\");\n        printf(\"%-9s: r=\" FMT64X \"\" FMT64X \" a=\" FMT64X \"\" FMT64X \" b=\" FMT64X \"\" FMT64X \"\\n\",\n               \"maskmov\",\n               r.q[1], r.q[0],\n               a.q[1], a.q[0],\n               b.q[1], b.q[0]);\n    }\n\n    asm volatile (\"emms\");\n\n    SSE_OP2(punpcklqdq);\n    SSE_OP2(punpckhqdq);\n    SSE_OP2(andps);\n    SSE_OP2(andpd);\n    SSE_OP2(andnps);\n    SSE_OP2(andnpd);\n    SSE_OP2(orps);\n    SSE_OP2(orpd);\n    SSE_OP2(xorps);\n    SSE_OP2(xorpd);\n\n    SSE_OP2(unpcklps);\n    SSE_OP2(unpcklpd);\n    SSE_OP2(unpckhps);\n    SSE_OP2(unpckhpd);\n\n    SHUF_OP(shufps, 0x78);\n    SHUF_OP(shufpd, 0x02);\n    SHUF_OP_MMX(pshufw, 0x78);\n    SHUF_OP_MMX(pshufw, 0x02);\n\n    PSHUF_OP(pshufd, 0x78);\n    PSHUF_OP(pshuflw, 0x78);\n    PSHUF_OP(pshufhw, 0x78);\n\n    SHIFT_OP(psrlw, 0);\n    SHIFT_OP(psrlw, 7);\n    SHIFT_OP(psrlw, 15);\n    SHIFT_OP(psrlw, 16);\n    SHIFT_REG(psrlw, 0x100000000);\n    SHIFT_REG_MMX(psrlw, 0x100000000);\n\n    SHIFT_OP(psraw, 0);\n    SHIFT_OP(psraw, 7);\n    SHIFT_OP(psraw, 15);\n    SHIFT_OP(psraw, 16);\n    SHIFT_REG(psraw, 0x100000000);\n    SHIFT_REG_MMX(psraw, 0x100000000);\n\n    SHIFT_OP(psllw, 0);\n    SHIFT_OP(psllw, 7);\n    SHIFT_OP(psllw, 15);\n    SHIFT_OP(psllw, 16);\n    SHIFT_REG(psllw, 0x100000000);\n    SHIFT_REG_MMX(psllw, 0x100000000);\n\n    SHIFT_OP(psrld, 0);\n    SHIFT_OP(psrld, 7);\n    SHIFT_OP(psrld, 31);\n    SHIFT_OP(psrld, 32);\n    SHIFT_REG(psrld, 0x100000000);\n    SHIFT_REG_MMX(psrld, 0x100000000);\n\n    SHIFT_OP(psrad, 0);\n    SHIFT_OP(psrad, 7);\n    SHIFT_OP(psrad, 31);\n    SHIFT_OP(psrad, 32);\n    SHIFT_REG(psrad, 0x100000000);\n    SHIFT_REG_MMX(psrad, 0x100000000);\n\n    SHIFT_OP(pslld, 0);\n    SHIFT_OP(pslld, 7);\n    SHIFT_OP(pslld, 31);\n    SHIFT_OP(pslld, 32);\n    SHIFT_REG(pslld, 0x100000000);\n    SHIFT_REG_MMX(pslld, 0x100000000);\n\n    SHIFT_OP(psrlq, 0);\n    SHIFT_OP(psrlq, 7);\n    SHIFT_OP(psrlq, 32);\n    SHIFT_OP(psrlq, 63);\n    SHIFT_OP(psrlq, 64);\n    SHIFT_REG(psrlq, 0x100000000);\n    SHIFT_REG_MMX(psrlq, 0x100000000);\n\n    SHIFT_OP(psllq, 0);\n    SHIFT_OP(psllq, 7);\n    SHIFT_OP(psllq, 32);\n    SHIFT_OP(psllq, 63);\n    SHIFT_OP(psllq, 64);\n    SHIFT_REG(psllq, 0x100000000);\n    SHIFT_REG_MMX(psllq, 0x100000000);\n\n    // byte-wise shifts\n    SHIFT_IM(psrldq, 0);\n    SHIFT_IM(psrldq, 1);\n    SHIFT_IM(psrldq, 7);\n    SHIFT_IM(psrldq, 8);\n    SHIFT_IM(psrldq, 11);\n    SHIFT_IM(psrldq, 15);\n    SHIFT_IM(psrldq, 16);\n\n    SHIFT_IM(pslldq, 0);\n    SHIFT_IM(pslldq, 1);\n    SHIFT_IM(pslldq, 7);\n    SHIFT_IM(pslldq, 8);\n    SHIFT_IM(pslldq, 11);\n    SHIFT_IM(pslldq, 15);\n    SHIFT_IM(pslldq, 16);\n\n    MOVMSK(movmskps);\n    MOVMSK(movmskpd);\n\n    /* FPU specific ops */\n    {\n        uint32_t mxcsr;\n        asm volatile(\"stmxcsr %0\" : \"=m\" (mxcsr));\n        printf(\"mxcsr=%08x\\n\", mxcsr & 0x1f80);\n        asm volatile(\"ldmxcsr %0\" : : \"m\" (mxcsr));\n    }\n\n    asm volatile (\"emms\");\n\n    test_sse_comi(2, -1);\n    test_sse_comi(2, 2);\n    test_sse_comi(2, 3);\n    test_sse_comi(2, q_nan.d);\n    test_sse_comi(q_nan.d, -1);\n\n    for(i = 0; i < 2; i++) {\n        a.s[0] = 2.7;\n        a.s[1] = 3.4;\n        a.s[2] = 4;\n        a.s[3] = -6.3;\n        b.s[0] = 45.7;\n        b.s[1] = 353.4;\n        b.s[2] = 4;\n        b.s[3] = 56.3;\n        if (i == 1) {\n            a.s[0] = q_nan.d;\n            b.s[3] = q_nan.d;\n        }\n\n        SSE_OPS(add);\n        SSE_OPS(mul);\n        SSE_OPS(sub);\n        SSE_OPS(min);\n        SSE_OPS(div);\n        SSE_OPS(max);\n        SSE_OPS(sqrt);\n        SSE_OPS(cmpeq);\n        SSE_OPS(cmplt);\n        SSE_OPS(cmple);\n        SSE_OPS(cmpunord);\n        SSE_OPS(cmpneq);\n        SSE_OPS(cmpnlt);\n        SSE_OPS(cmpnle);\n        SSE_OPS(cmpord);\n\n\n        a.d[0] = 2.7;\n        a.d[1] = -3.4;\n        b.d[0] = 45.7;\n        b.d[1] = -53.4;\n        if (i == 1) {\n            a.d[0] = q_nan.d;\n            b.d[1] = q_nan.d;\n        }\n        SSE_OPD(add);\n        SSE_OPD(mul);\n        SSE_OPD(sub);\n        SSE_OPD(min);\n        SSE_OPD(div);\n        SSE_OPD(max);\n        SSE_OPD(sqrt);\n        SSE_OPD(cmpeq);\n        SSE_OPD(cmplt);\n        SSE_OPD(cmple);\n        SSE_OPD(cmpunord);\n        SSE_OPD(cmpneq);\n        SSE_OPD(cmpnlt);\n        SSE_OPD(cmpnle);\n        SSE_OPD(cmpord);\n    }\n\n    // approximating instructions: Pick some nice round values\n    a.s[0] = 1024.0;\n    a.s[1] = 1.0 / 256.0;\n    b.s[0] = 1024.0;\n    b.s[1] = 1.0 / 256.0;\n    SSE_OPS(rsqrt);\n    SSE_OPS(rcp);\n\n    /* float to float/int */\n    a.s[0] = 2.7;\n    a.s[1] = 3.4;\n    a.s[2] = 4;\n    a.s[3] = -6.3;\n    CVT_OP_XMM(cvtps2pd);\n    CVT_OP_XMM(cvtss2sd);\n    CVT_OP_XMM2MMX(cvtps2pi);\n    CVT_OP_XMM2MMX(cvttps2pi);\n    CVT_OP_XMM2REG(cvtss2si);\n    CVT_OP_XMM2REG(cvttss2si);\n    CVT_OP_XMM(cvtps2dq);\n    CVT_OP_XMM(cvttps2dq);\n\n    a.d[0] = 2.6;\n    a.d[1] = -3.4;\n    CVT_OP_XMM(cvtpd2ps);\n    CVT_OP_XMM(cvtsd2ss);\n    CVT_OP_XMM2MMX(cvtpd2pi);\n    CVT_OP_XMM2MMX(cvttpd2pi);\n    CVT_OP_XMM2REG(cvtsd2si);\n    CVT_OP_XMM2REG(cvttsd2si);\n    CVT_OP_XMM(cvtpd2dq);\n    CVT_OP_XMM(cvttpd2dq);\n\n    /* sse/mmx moves */\n    CVT_OP_XMM2MMX(movdq2q);\n    CVT_OP_MMX2XMM(movq2dq);\n\n    /* int to float */\n    a.l[0] = -6;\n    a.l[1] = 2;\n    a.l[2] = 100;\n    a.l[3] = -60000;\n    CVT_OP_MMX2XMM(cvtpi2ps);\n    CVT_OP_MMX2XMM(cvtpi2pd);\n    CVT_OP_REG2XMM(cvtsi2ss);\n    CVT_OP_REG2XMM(cvtsi2sd);\n    CVT_OP_XMM(cvtdq2ps);\n    CVT_OP_XMM(cvtdq2pd);\n    /* XXX: test PNI insns */\n#if 0\n    SSE_OP2(movshdup);\n#endif\n    asm volatile (\"emms\");\n}\n\n#endif\n\n#define TEST_CONV_RAX(op)\\\n{\\\n    unsigned long a, r;\\\n    a = i2l(0x8234a6f8);\\\n    r = a;\\\n    asm volatile(#op : \"=a\" (r) : \"0\" (r));\\\n    printf(\"%-10s A=\" FMTLX \" R=\" FMTLX \"\\n\", #op, a, r);\\\n}\n\n#define TEST_CONV_RAX_RDX(op)\\\n{\\\n    unsigned long a, d, r, rh;                   \\\n    a = i2l(0x8234a6f8);\\\n    d = i2l(0x8345a1f2);\\\n    r = a;\\\n    rh = d;\\\n    asm volatile(#op : \"=a\" (r), \"=d\" (rh) : \"0\" (r), \"1\" (rh));   \\\n    printf(\"%-10s A=\" FMTLX \" R=\" FMTLX \":\" FMTLX \"\\n\", #op, a, r, rh);  \\\n}\n\nvoid test_conv(void)\n{\n    TEST_CONV_RAX(cbw);\n    TEST_CONV_RAX(cwde);\n#if defined(__x86_64__)\n    TEST_CONV_RAX(cdqe);\n#endif\n\n    TEST_CONV_RAX_RDX(cwd);\n    TEST_CONV_RAX_RDX(cdq);\n#if defined(__x86_64__)\n    TEST_CONV_RAX_RDX(cqo);\n#endif\n\n    {\n        unsigned long a, r;\n        a = i2l(0x12345678);\n        asm volatile(\"bswapl %k0\" : \"=r\" (r) : \"0\" (a));\n        printf(\"%-10s: A=\" FMTLX \" R=\" FMTLX \"\\n\", \"bswapl\", a, r);\n    }\n#if defined(__x86_64__)\n    {\n        unsigned long a, r;\n        a = i2l(0x12345678);\n        asm volatile(\"bswapq %0\" : \"=r\" (r) : \"0\" (a));\n        printf(\"%-10s: A=\" FMTLX \" R=\" FMTLX \"\\n\", \"bswapq\", a, r);\n    }\n#endif\n}\n\n\nvoid byte_read(uint8_t* buffer, uint16_t offset, size_t num_bytes)\n{\n    uint64_t v1 = 0;\n    for(size_t i = 0; i < num_bytes && i < 8; i++)\n    {\n        if(setjmp(jmp_env) == 0)\n        {\n            v1 |= (uint64_t)buffer[offset + i] << (i * 8);\n        }\n    }\n\n    uint64_t v2 = 0;\n    for(size_t i = 8; i < num_bytes; i++)\n    {\n        if(setjmp(jmp_env) == 0)\n        {\n            v2 |= (uint64_t)buffer[offset + i] << ((i - 8) * 8);\n        }\n\n    }\n\n    if(num_bytes > 8)\n    {\n        printf(\"%-12s: offset=%x value=%08llx%08llx\\n\", \"byte_r\", offset, v2, v1);\n    }\n    else\n    {\n        printf(\"%-12s: offset=%x value=%llx\\n\", \"byte_r\", offset, v1);\n    }\n}\n\nuint64_t seq_counter = 0x8070605040302010;\nuint64_t get_seq64()\n{\n    seq_counter += 0x0101010101010101;\n    return seq_counter;\n}\n\nvoid byte_write_seq(uint8_t* target, uint16_t offset, size_t num_bytes)\n{\n    uint64_t v = get_seq64();\n    if(num_bytes < 8) v &= (1LL << (num_bytes * 8)) - 1;\n\n    for(size_t i = 0; i < num_bytes; i++)\n    {\n        if(setjmp(jmp_env) == 0)\n        {\n            target[offset + i] = (v >> (i * 8 % 64)) & 0xFF;\n        }\n    }\n\n    if(num_bytes > 8)\n    {\n        printf(\"%-12s: offset=%x value=%08llx%08llx\\n\", \"byte_w\", offset, v, v);\n    }\n    else\n    {\n        printf(\"%-12s: offset=%x value=%llx\\n\", \"byte_w\", offset, v);\n    }\n}\n\n#define GENERATE_CHUNK_READ(INSTR, BITS, CONSTR)                    \\\n    void chunk_read ## BITS(uint8_t* addr, uint16_t offset)         \\\n    {                                                               \\\n        uint ## BITS ## _t chunk = 0;                               \\\n        if(setjmp(jmp_env) == 0) {                                  \\\n            asm volatile(INSTR \" %1, %0\" :                          \\\n                         \"=\" CONSTR (chunk) :                       \\\n                         \"m\" (*(addr + offset)), \"0\" (chunk));      \\\n        }                                                           \\\n        printf(\"%-12s: offset=%x value=%\" PRIx ## BITS \"\\n\",        \\\n               \"chunk\" #BITS \"_r\",                                  \\\n               offset,                                              \\\n               chunk);                                              \\\n    }\n\n#define GENERATE_CHUNK_WRITE(INSTR, BITS, CONSTR)                   \\\n    void chunk_write ## BITS(uint8_t* addr, uint16_t offset)        \\\n    {                                                               \\\n        uint ## BITS ## _t chunk = get_seq64();                     \\\n        if(setjmp(jmp_env) == 0) {                                  \\\n            asm volatile(INSTR \" %0, %1\" :                          \\\n                         \"=\" CONSTR (chunk) :                       \\\n                         \"m\" (*(addr + offset)), \"0\" (chunk));      \\\n        }                                                           \\\n        printf(\"%-12s: offset=%x value=%\" PRIx ## BITS \"\\n\",        \\\n               \"chunk\" #BITS \"_w\",                                  \\\n               offset,                                              \\\n               chunk);                                              \\\n    }\n\n#define GENERATE_CHUNK_FNS(INSTR, BITS, CONSTR)                   \\\n    GENERATE_CHUNK_READ(INSTR, BITS, CONSTR)                      \\\n    GENERATE_CHUNK_WRITE(INSTR, BITS, CONSTR)\n\n#define TEST_CHUNK_READ(BITS, ADDR, OFFSET)         \\\n    byte_write_seq(ADDR, OFFSET, (BITS) >> 3);      \\\n    chunk_read ## BITS(ADDR, OFFSET);\n\n#define TEST_CHUNK_WRITE(BITS, ADDR, OFFSET)        \\\n    if(!skip_write_test) {                          \\\n        byte_write_seq(ADDR, OFFSET, (BITS) >> 3);  \\\n        mask_pf_address = 1;                        \\\n        chunk_write ## BITS(ADDR, OFFSET);          \\\n        mask_pf_address = 0;                        \\\n        byte_read(ADDR, OFFSET, (BITS) >> 3);       \\\n    }\n\n#define TEST_CHUNK_READ_WRITE(BITS, ADDR, OFFSET)         \\\n    if(BITS <= 32) {                                      \\\n        byte_write_seq(ADDR, OFFSET, (BITS) >> 3);        \\\n        mask_pf_error = 1;                                \\\n        mask_pf_address = 1;                              \\\n        chunk_read_write ## BITS(ADDR, OFFSET);           \\\n        mask_pf_address = 0;                              \\\n        mask_pf_error = 0;                                \\\n        byte_read(ADDR, OFFSET, (BITS) >> 3);             \\\n    }\n\n// Based on BITS, we calculate the offset where cross-page reads/writes would begin\n#define TEST_CROSS_PAGE(BITS, ADDR)                     \\\n    for(size_t offset = (PAGE_SIZE + 1 - (BITS >> 3));  \\\n        offset < PAGE_SIZE; offset++)                   \\\n    {                                                   \\\n        TEST_CHUNK_READ(BITS, ADDR, offset);            \\\n        TEST_CHUNK_WRITE(BITS, ADDR, offset);           \\\n        TEST_CHUNK_READ_WRITE(BITS, ADDR, offset);      \\\n    }\n\nGENERATE_CHUNK_FNS(\"movw\", 16, \"r\");\nGENERATE_CHUNK_FNS(\"mov\", 32, \"r\");\n\n#ifdef TEST_SSE\nGENERATE_CHUNK_FNS(\"movq\", 64, \"y\");\n\nvoid chunk_read_write16(uint8_t* addr, uint16_t offset)\n{\n    uint16_t chunk = get_seq64();\n    if(setjmp(jmp_env) == 0)\n    {\n        asm volatile(\"addw %0, %1\" :\n                     \"=r\" (chunk) :\n                     \"m\" (*(addr + offset)), \"0\" (chunk));\n    }\n    printf(\"%-12s: offset=%x value=%\" PRIx16 \"\\n\",\n           \"chunk16_rw\",\n           offset,\n           chunk);\n}\n\nvoid chunk_read_write32(uint8_t* addr, uint16_t offset)\n{\n    uint32_t chunk = get_seq64();\n    if(setjmp(jmp_env) == 0)\n    {\n        asm volatile(\"add %0, %1\" :\n                     \"=r\" (chunk) :\n                     \"m\" (*(addr + offset)), \"0\" (chunk));\n    }\n    printf(\"%-12s: offset=%x value=%\" PRIx32 \"\\n\",\n           \"chunk32_rw\",\n           offset,\n           chunk);\n}\n\n// No 64 or 128-bit read-write x86 instructions support a memory address as the destination\nvoid chunk_read_write64(uint8_t* addr, uint16_t offset)\n{\n    UNUSED(addr);\n    UNUSED(offset);\n}\n\nvoid chunk_read_write128(uint8_t* addr, uint16_t offset)\n{\n    UNUSED(addr);\n    UNUSED(offset);\n}\n\n\nvoid chunk_read128(uint8_t* addr, uint16_t offset)\n{\n    XMMReg chunk;\n    chunk.q[0] = chunk.q[1] = 0.0;\n    if(setjmp(jmp_env) == 0)\n    {\n        asm volatile(\"movdqu %1, %0\" :\n                     \"=x\" (chunk.dq) :\n                     \"m\" (*(addr + offset)), \"0\" (chunk.dq)\n            );\n    }\n    printf(\"%-12s: offset=%x value=\" FMT64X FMT64X \"\\n\",\n           \"chunk128_r\",\n           offset,\n           chunk.q[1],\n           chunk.q[0]);\n}\n\nvoid chunk_write128(uint8_t* addr, uint16_t offset)\n{\n    XMMReg chunk;\n    chunk.q[0] = get_seq64();\n    chunk.q[1] = get_seq64();\n    if(setjmp(jmp_env) == 0)\n    {\n        asm volatile(\"movdqu %0, %1\" :\n                     \"=x\" (chunk.dq) :\n                     \"m\" (*(addr + offset)), \"0\" (chunk.dq)\n            );\n    }\n    printf(\"%-12s: offset=%x value=\" FMT64X FMT64X \"\\n\",\n           \"chunk128_w\",\n           offset,\n           chunk.q[1],\n           chunk.q[0]);\n}\n#endif\n\nvoid* const TEST_ADDRESS = (void *)0x70000000;\nuint8_t* first_page = NULL;\nuint8_t* second_page = NULL;\nuint8_t* throwaway_page = NULL;\n\nvoid setup_pages(int first_page_type, int second_page_type)\n{\n    const int prot = PROT_READ | PROT_WRITE;\n\n    if(first_page_type)\n    {\n        // mmap 2 consecutive pages\n        first_page = mmap(TEST_ADDRESS, 2 * PAGE_SIZE, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n        assert(first_page == TEST_ADDRESS);\n    }\n    else\n    {\n        first_page = NULL;\n    }\n\n    // throwaway mmap to reduce likelhood of first_page and second_page mapping to consecutive physical frames\n    throwaway_page = mmap(NULL, PAGE_SIZE, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n    assert(throwaway_page != MAP_FAILED && throwaway_page != TEST_ADDRESS && throwaway_page != TEST_ADDRESS + PAGE_SIZE);\n\n    if(second_page_type)\n    {\n        second_page = mmap(TEST_ADDRESS + PAGE_SIZE, PAGE_SIZE, prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);\n        assert(second_page == TEST_ADDRESS + PAGE_SIZE);\n    }\n    else\n    {\n        munmap(TEST_ADDRESS + PAGE_SIZE, PAGE_SIZE);\n        second_page = NULL;\n    }\n\n    // Trigger page-faults causing virtual pages to be allocated to physical frames\n    if(first_page != NULL) memset(first_page, 0x42, PAGE_SIZE);\n    memset(throwaway_page, 0x42, PAGE_SIZE);\n    if(second_page != NULL) memset(second_page, 0x42, PAGE_SIZE);\n\n    if(first_page_type == PROT_READ)\n    {\n        mprotect(first_page, PAGE_SIZE, PROT_READ);\n    }\n\n    if(second_page_type == PROT_READ)\n    {\n        mprotect(second_page, PAGE_SIZE, PROT_READ);\n    }\n}\n\nvoid free_pages()\n{\n    munmap(TEST_ADDRESS, PAGE_SIZE);\n    munmap(TEST_ADDRESS + PAGE_SIZE, PAGE_SIZE);\n    munmap(throwaway_page, PAGE_SIZE);\n}\n\n// XXX: Workarounds for qemu bugs: Can be removed when running tests in kvm mode\nint mask_pf_error = 0;\nint mask_pf_address = 0;\nint skip_write_test = 0;\n\nvoid pagefault_handler(int sig, siginfo_t *info, void *puc)\n{\n    ucontext_t *uc = puc;\n\n    printf(\"page fault: addr=0x%08lx err=0x%lx eip=0x%08lx\\n\",\n            (unsigned long)info->si_addr & (mask_pf_address ? ~0xfff : ~0),\n            (long)uc->uc_mcontext.gregs[REG_ERR] & (mask_pf_error ? ~2 : ~0),\n            (long)uc->uc_mcontext.gregs[REG_EIP]);\n\n    assert(info->si_addr >= TEST_ADDRESS && info->si_addr < TEST_ADDRESS + 2 * PAGE_SIZE);\n\n    longjmp(jmp_env, 1);\n}\n\nvoid test_page_boundaries()\n{\n    const int prot_rw = PROT_READ | PROT_WRITE;\n    const int prot_ronly = PROT_READ;\n\n    setup_pages(prot_rw, prot_rw);\n\n    TEST_CROSS_PAGE(16, TEST_ADDRESS);\n    TEST_CROSS_PAGE(32, TEST_ADDRESS);\n#ifdef TEST_SSE\n    TEST_CROSS_PAGE(64, TEST_ADDRESS);\n    TEST_CROSS_PAGE(128, TEST_ADDRESS);\n#endif\n\n    struct sigaction act;\n    act.sa_sigaction = pagefault_handler;\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = SA_SIGINFO | SA_NODEFER;\n    sigaction(SIGSEGV, &act, NULL);\n\n    free_pages();\n    printf(\"With non-present page faults in first page:\\n\");\n    setup_pages(0, prot_rw);\n\n    TEST_CROSS_PAGE(16, TEST_ADDRESS);\n    TEST_CROSS_PAGE(32, TEST_ADDRESS);\n#ifdef TEST_SSE\n    TEST_CROSS_PAGE(64, TEST_ADDRESS);\n    TEST_CROSS_PAGE(128, TEST_ADDRESS);\n#endif\n\n    free_pages();\n    printf(\"With read-only page faults in first page:\\n\");\n    setup_pages(prot_ronly, prot_rw);\n\n    TEST_CROSS_PAGE(16, TEST_ADDRESS);\n    TEST_CROSS_PAGE(32, TEST_ADDRESS);\n#ifdef TEST_SSE\n    TEST_CROSS_PAGE(64, TEST_ADDRESS);\n    TEST_CROSS_PAGE(128, TEST_ADDRESS);\n#endif\n\n    free_pages();\n    printf(\"With non-present page faults in second page:\\n\");\n    setup_pages(prot_rw, 0);\n\n    TEST_CROSS_PAGE(16, TEST_ADDRESS);\n    TEST_CROSS_PAGE(32, TEST_ADDRESS);\n#ifdef TEST_SSE\n    TEST_CROSS_PAGE(64, TEST_ADDRESS);\n    skip_write_test = 1;\n    TEST_CROSS_PAGE(128, TEST_ADDRESS);\n    skip_write_test = 0;\n#endif\n\n    free_pages();\n    printf(\"With read-only page faults in second page:\\n\");\n    setup_pages(prot_rw, prot_ronly);\n\n    TEST_CROSS_PAGE(16, TEST_ADDRESS);\n    TEST_CROSS_PAGE(32, TEST_ADDRESS);\n#ifdef TEST_SSE\n    TEST_CROSS_PAGE(64, TEST_ADDRESS);\n    skip_write_test = 1;\n    TEST_CROSS_PAGE(128, TEST_ADDRESS);\n    skip_write_test = 0;\n#endif\n}\n\nextern void *__start_initcall;\nextern void *__stop_initcall;\n\nint main(int argc, char **argv)\n{\n    // Uncomment to disable buffering, useful for debugging segfaults\n    //setvbuf(stdout, NULL, _IONBF, 0);\n\n    void **ptr;\n    void (*func)(void);\n\n    ptr = &__start_initcall;\n    while (ptr != &__stop_initcall) {\n        func = *ptr++;\n        func();\n    }\n    test_bsx();\n    test_popcnt();\n    test_mul();\n    test_jcc();\n    test_loop();\n    test_floats();\n#if !defined(__x86_64__)\n    test_bcd();\n#endif\n    test_xchg();\n    test_string();\n    test_misc();\n    test_lea();\n#ifdef TEST_SEGS\n    test_segs();\n    test_code16();\n#endif\n#ifdef TEST_VM86\n    test_vm86();\n#endif\n#if !defined(__x86_64__)\n    test_exceptions();\n    test_self_modifying_code();\n    //test_single_step();\n#endif\n    test_enter();\n    test_conv();\n#ifdef TEST_SSE\n    test_sse();\n    test_fxsave();\n#endif\n    test_page_boundaries();\n    return 0;\n}\n"
  },
  {
    "path": "tests/qemu/test-i386.h",
    "content": "\n#define exec_op glue(exec_, OP)\n#define exec_opq glue(glue(exec_, OP), q)\n#define exec_opl glue(glue(exec_, OP), l)\n#define exec_opw glue(glue(exec_, OP), w)\n#define exec_opb glue(glue(exec_, OP), b)\n\n#define EXECOP2(size, rsize, res, s1, flags) \\\n    asm (\"push %4\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         stringify(OP) size \" %\" rsize \"2, %\" rsize \"0\\n\\t\" \\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n         : \"=q\" (res), \"=g\" (flags)\\\n         : \"q\" (s1), \"0\" (res), \"1\" (flags)); \\\n    printf(\"%-10s A=\" FMTLX \" B=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\", \\\n           stringify(OP) size, s0, s1, res, iflags, flags & CC_MASK);\n\n#define EXECOP1(size, rsize, res, flags) \\\n    asm (\"push %3\\n\\t\"\\\n         \"popf\\n\\t\"\\\n         stringify(OP) size \" %\" rsize \"0\\n\\t\" \\\n         \"pushf\\n\\t\"\\\n         \"pop %1\\n\\t\"\\\n         : \"=q\" (res), \"=g\" (flags)\\\n         : \"0\" (res), \"1\" (flags)); \\\n    printf(\"%-10s A=\" FMTLX \" R=\" FMTLX \" CCIN=%04lx CC=%04lx\\n\", \\\n           stringify(OP) size, s0, res, iflags, flags & CC_MASK);\n\n#ifdef OP1\n#if defined(__x86_64__)\nvoid exec_opq(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP1(\"q\", \"\", res, flags);\n}\n#endif\n\nvoid exec_opl(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP1(\"l\", \"k\", res, flags);\n}\n\nvoid exec_opw(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP1(\"w\", \"w\", res, flags);\n}\n\nvoid exec_opb(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP1(\"b\", \"b\", res, flags);\n}\n#else\n#if defined(__x86_64__)\nvoid exec_opq(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP2(\"q\", \"\", res, s1, flags);\n}\n#endif\n\nvoid exec_opl(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP2(\"l\", \"k\", res, s1, flags);\n}\n\nvoid exec_opw(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP2(\"w\", \"w\", res, s1, flags);\n}\n\nvoid exec_opb(long s0, long s1, long iflags)\n{\n    long res, flags;\n    res = s0;\n    flags = iflags;\n    EXECOP2(\"b\", \"b\", res, s1, flags);\n}\n#endif\n\nvoid exec_op(long s0, long s1)\n{\n    s0 = i2l(s0);\n    s1 = i2l(s1);\n#if defined(__x86_64__)\n    exec_opq(s0, s1, 0);\n#endif\n    exec_opl(s0, s1, 0);\n    exec_opw(s0, s1, 0);\n    exec_opb(s0, s1, 0);\n#ifdef OP_CC\n#if defined(__x86_64__)\n    exec_opq(s0, s1, CC_C);\n#endif\n    exec_opl(s0, s1, CC_C);\n    exec_opw(s0, s1, CC_C);\n    exec_opb(s0, s1, CC_C);\n#endif\n}\n\nvoid glue(test_, OP)(void)\n{\n    exec_op(0x12345678, 0x812FADA);\n    exec_op(0x12341, 0x12341);\n    exec_op(0x12341, -0x12341);\n    exec_op(0xffffffff, 0);\n    exec_op(0xffffffff, -1);\n    exec_op(0xffffffff, 1);\n    exec_op(0xffffffff, 2);\n    exec_op(0x7fffffff, 0);\n    exec_op(0x7fffffff, 1);\n    exec_op(0x7fffffff, -1);\n    exec_op(0x80000000, -1);\n    exec_op(0x80000000, 1);\n    exec_op(0x80000000, -2);\n    exec_op(0x12347fff, 0);\n    exec_op(0x12347fff, 1);\n    exec_op(0x12347fff, -1);\n    exec_op(0x12348000, -1);\n    exec_op(0x12348000, 1);\n    exec_op(0x12348000, -2);\n    exec_op(0x12347f7f, 0);\n    exec_op(0x12347f7f, 1);\n    exec_op(0x12347f7f, -1);\n    exec_op(0x12348080, -1);\n    exec_op(0x12348080, 1);\n    exec_op(0x12348080, -2);\n\n    exec_op(0xfffe0080, -1);\n    exec_op(0xfffe0080, 1);\n    exec_op(0xfffe0080, 0);\n    exec_op(0xfffe0080, 0xfffe0080);\n    exec_op(0xfffe0080, 0x80);\n    exec_op(0xfffe0080, 0x81);\n    exec_op(0xfffe0080, 0x10000);\n    exec_op(0xfffe0080, 0x20000);\n    exec_op(0xfffe0080, 0x1ff7f);\n    exec_op(0xfffe0080, 0x1ff80);\n    exec_op(0xfffe0080, 0x1ff81);\n    exec_op(0xfffe0080, 0x1ffff);\n\n\n    exec_op(0, 0);\n}\n\nvoid *glue(_test_, OP) __init_call = glue(test_, OP);\n\n#undef OP\n#undef OP_CC\n"
  },
  {
    "path": "tests/rust/verify-wasmgen-dummy-output.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport url from \"node:url\";\nimport assert from \"node:assert/strict\";\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nprocess.on(\"unhandledRejection\", exn => { throw exn; });\n\nconst DUMMY_MODULE_PATH = path.resolve(__dirname, \"../../build/dummy_output.wasm\");\nconst dummy_module = fs.readFileSync(DUMMY_MODULE_PATH);\n\nconst wm = new WebAssembly.Module(dummy_module);\nconst mem = new WebAssembly.Memory({ initial: 256 });\n\n// These tests have to be kept in sync with src/rust/wasmgen/module_init.rs' tests\n// XXX: make the test more complex, involving locals, conditionals and stuff\n\nlet baz_recd_arg;\nfunction baz(arg) {\n    baz_recd_arg = arg;\n    return 456;\n}\n\nlet foo_recd_arg;\nfunction foo(arg) {\n    foo_recd_arg = arg;\n}\n\nconst i = new WebAssembly.Instance(wm, { \"e\": { m: mem, baz, foo } });\ni.exports.f();\n\nassert(baz_recd_arg === 2, `baz returned: \"${baz_recd_arg}\"`);\nassert(foo_recd_arg === 456, `foo returned: \"${foo_recd_arg}\"`);\n"
  },
  {
    "path": "tools/copy-to-sha256.py",
    "content": "#!/usr/bin/env python3\n\nimport os\nimport logging\nimport stat\nimport argparse\nimport hashlib\nimport shutil\nimport tarfile\nimport sys\nimport io\n\nHASH_LENGTH = 8\n\ndef hash_file(filename) -> str:\n    with open(filename, \"rb\", buffering=0) as f:\n        return hash_fileobj(f)\n\ndef hash_fileobj(f) -> str:\n    h = hashlib.sha256()\n    for b in iter(lambda: f.read(128*1024), b\"\"):\n        h.update(b)\n    return h.hexdigest()\n\ndef main():\n    logging.basicConfig(format=\"%(message)s\")\n    logger = logging.getLogger(\"copy\")\n    logger.setLevel(logging.DEBUG)\n\n    args = argparse.ArgumentParser(description=\"...\",\n                                   formatter_class=argparse.RawTextHelpFormatter)\n    args.add_argument(\"from_path\", metavar=\"from\", help=\"from\")\n    args.add_argument(\"to_path\", metavar=\"to\", help=\"to\")\n    args.add_argument(\"--zstd\", action=\"store_true\", help=\"Use Zstandard compression\")\n\n    args = args.parse_args()\n\n    from_path = os.path.normpath(args.from_path)\n    to_path = os.path.normpath(args.to_path)\n\n    # Import zstd only if compression is requested\n    zstd_module = None\n    if args.zstd:\n        if sys.version_info >= (3, 14):\n            from compression import zstd\n            zstd_module = zstd\n        else:\n            try:\n                import zstandard as zstd\n                zstd_module = zstd\n            except ImportError:\n                print(\"Error: zstandard module required when using --zstd flag\")\n                print(\"Install with: pip install zstandard\")\n                sys.exit(1)\n\n    if os.path.isfile(from_path):\n        tar = tarfile.open(from_path, \"r\")\n    else:\n        tar = None\n\n    if tar:\n        handle_tar(logger, tar, to_path, args.zstd, zstd_module)\n    else:\n        handle_dir(logger, from_path, to_path, args.zstd, zstd_module)\n\ndef handle_dir(logger, from_path: str, to_path: str, use_compression: bool, zstd_module):\n    def onerror(oserror):\n        logger.warning(oserror)\n\n    files = os.walk(from_path, onerror=onerror)\n\n    for f in files:\n        dirpath, dirnames, filenames = f\n\n        for filename in filenames:\n            absname = os.path.join(dirpath, filename)\n            st = os.lstat(absname)\n            mode = st.st_mode\n\n            assert not stat.S_ISDIR(mode)\n            if stat.S_ISLNK(mode) or stat.S_ISCHR(mode) or stat.S_ISBLK(mode) or stat.S_ISFIFO(mode) or stat.S_ISSOCK(mode):\n                continue\n\n            file_hash = hash_file(absname)\n            filename = file_hash[0:HASH_LENGTH] + (\".bin.zst\" if use_compression else \".bin\")\n            to_abs = os.path.join(to_path, filename)\n\n            if os.path.exists(to_abs):\n                logger.info(\"Exists, skipped {} ({})\".format(to_abs, absname))\n            else:\n                if use_compression:\n                    logger.info(\"Compressing {} {}\".format(absname, to_abs))\n                    with open(absname, 'rb') as src_file:\n                        with open(to_abs, 'wb') as dst_file:\n                            zstd_module.ZstdCompressor(level=19).copy_stream(src_file, dst_file)\n                else:\n                    logger.info(\"cp {} {}\".format(absname, to_abs))\n                    shutil.copyfile(absname, to_abs)\n\ndef handle_tar(logger, tar, to_path: str, use_compression: bool, zstd_module):\n    for member in tar.getmembers():\n        if member.isfile() or member.islnk():\n            f = tar.extractfile(member)\n            file_hash = hash_fileobj(f)\n            filename = file_hash[0:HASH_LENGTH] + (\".bin.zst\" if use_compression else \".bin\")\n            to_abs = os.path.join(to_path, filename)\n\n            if os.path.exists(to_abs):\n                logger.info(\"Exists, skipped {} ({})\".format(to_abs, member.name))\n            else:\n                if use_compression:\n                    logger.info(\"Extracted and compressing {} ({})\".format(to_abs, member.name))\n                    f.seek(0)\n                    with open(to_abs, 'wb') as dst_file:\n                        zstd_module.ZstdCompressor(level=19).copy_stream(f, dst_file)\n                else:\n                    logger.info(\"Extracted {} ({})\".format(to_abs, member.name))\n                    to_file = open(to_abs, \"wb\")\n                    f.seek(0)\n                    shutil.copyfileobj(f, to_file)\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "tools/docker/README.md",
    "content": "# Dockerfiles\n\nThis directory contains Dockerfile to generate images for various purposes.\n\n## debian\n\nTo create a Docker image to run Debian inside v86.\n\n## exec\n\nTo create a Docker image to build and host v86 system and expose in a port (default 8000).\n\n## test-image\n\nTo create a Docker image that runs v86 tests.\n"
  },
  {
    "path": "tools/docker/alpine/Dockerfile",
    "content": "FROM docker.io/i386/alpine:3.21.0\n\nENV KERNEL=virt\nENV ADDPKGS=nodejs\n\nRUN apk add openrc alpine-base agetty alpine-conf linux-$KERNEL linux-firmware-none $ADDPKGS\n\nRUN sed -i 's/getty 38400 tty1/agetty --autologin root tty1 linux/' /etc/inittab\nRUN echo 'ttyS0::respawn:/sbin/agetty --autologin root -s ttyS0 115200 vt100' >> /etc/inittab\nRUN echo \"root:\" | chpasswd\n\nRUN setup-hostname localhost\n\n# Adding networking.sh script\nRUN echo -e \"rmmod ne2k-pci && modprobe ne2k-pci\\nrmmod virtio-net && modprobe virtio-net\\nhwclock -s\\nsetup-interfaces -a -r\" > /root/networking.sh && chmod +x /root/networking.sh\n\nRUN echo 'console.log(\"Hello, world!\");' > /root/hello.js\n\n# https://wiki.alpinelinux.org/wiki/Alpine_Linux_in_a_chroot#Preparing_init_services\nRUN for i in devfs dmesg mdev hwdrivers; do rc-update add $i sysinit; done\nRUN for i in hwclock modules sysctl hostname syslog bootmisc; do rc-update add $i boot; done\nRUN rc-update add killprocs shutdown\n\n# Generate initramfs with 9p modules\nRUN mkinitfs -F \"base virtio 9p\" $(cat /usr/share/kernel/$KERNEL/kernel.release)\n"
  },
  {
    "path": "tools/docker/alpine/Readme.md",
    "content": "You can build a Alpine Linux 9p image using Docker:\n\n1. As needed, kernel flavor (`virt` is smaller than `lts`) and set of additional packages (community repo is enabled by default) can be edited in `Dockerfile`\n2. Check and run `./build.sh` with started dockerd (podman works)\n3. Run local webserver (e.g. `make run`) and open `examples/alpine.html`\n4. (optional) Run `./build-state.js` and add `initial_state: { url: \"../images/alpine-state.bin\" }` to `alpine.html`\n"
  },
  {
    "path": "tools/docker/alpine/build-state.js",
    "content": "#!/usr/bin/env node\n\nimport path from \"node:path\";\nimport fs from \"node:fs\";\nimport url from \"node:url\";\nimport { V86 } from \"../../../build/libv86.mjs\";\n\nconsole.log(\"Don't forget to run `make all` before running this script\");\n\nconst __dirname = url.fileURLToPath(new URL(\".\", import.meta.url));\n\nconst V86_ROOT = path.join(__dirname, \"../../..\");\nconst OUTPUT_FILE = path.join(V86_ROOT, \"images/alpine-state.bin\");\n\nvar emulator = new V86({\n    bios: { url: path.join(V86_ROOT, \"bios/seabios.bin\") },\n    vga_bios: { url: path.join(V86_ROOT, \"bios/vgabios.bin\") },\n    autostart: true,\n    memory_size: 512 * 1024 * 1024,\n    vga_memory_size: 8 * 1024 * 1024,\n    network_relay_url: \"<UNUSED>\",\n    bzimage_initrd_from_filesystem: true,\n    cmdline: \"rw root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose modules=virtio_pci tsc=reliable init_on_free=on\",\n    filesystem: {\n        baseurl: path.join(V86_ROOT, \"images/alpine-rootfs-flat\"),\n        basefs: path.join(V86_ROOT, \"images/alpine-fs.json\"),\n    },\n});\n\nconsole.log(\"Now booting, please stand by ...\");\n\nlet serial_text = \"\";\nlet booted = false;\n\nemulator.add_listener(\"serial0-output-byte\", function(byte)\n{\n    const c = String.fromCharCode(byte);\n    //process.stdout.write(c);\n\n    serial_text += c;\n\n    if(!booted && serial_text.endsWith(\"localhost:~# \"))\n    {\n        booted = true;\n\n        emulator.serial0_send(\"sync;echo 3 >/proc/sys/vm/drop_caches\\n\");\n\n        setTimeout(async function ()\n            {\n                const s = await emulator.save_state();\n\n                fs.writeFile(OUTPUT_FILE, new Uint8Array(s), function(e)\n                    {\n                        if(e) throw e;\n                        console.log(\"Saved as \" + OUTPUT_FILE);\n                        emulator.destroy();\n                    });\n            }, 10 * 1000);\n    }\n});\n"
  },
  {
    "path": "tools/docker/alpine/build.sh",
    "content": "#!/usr/bin/env bash\nset -euo pipefail\n\ncd \"$(dirname \"$0\")\"\n\nIMAGES=../../../images\nOUT_ROOTFS_TAR=\"$IMAGES\"/alpine-rootfs.tar\nOUT_ROOTFS_FLAT=\"$IMAGES\"/alpine-rootfs-flat\nOUT_FSJSON=\"$IMAGES\"/alpine-fs.json\nCONTAINER_NAME=alpine-v86\nIMAGE_NAME=i386/alpine-v86\n\nmkdir -p \"$IMAGES\"\ndocker build . --platform linux/386 --rm --tag \"$IMAGE_NAME\"\ndocker rm \"$CONTAINER_NAME\" || true\ndocker create --platform linux/386 -t -i --name \"$CONTAINER_NAME\" \"$IMAGE_NAME\"\n\ndocker export \"$CONTAINER_NAME\" -o \"$OUT_ROOTFS_TAR\"\n\n# https://github.com/iximiuz/docker-to-linux/issues/19#issuecomment-1242809707\ntar -f \"$OUT_ROOTFS_TAR\" --delete \".dockerenv\" || true\n\n../../../tools/fs2json.py --zstd --out \"$OUT_FSJSON\" \"$OUT_ROOTFS_TAR\"\n\n# Note: Not deleting old files here\nmkdir -p \"$OUT_ROOTFS_FLAT\"\n../../../tools/copy-to-sha256.py --zstd \"$OUT_ROOTFS_TAR\" \"$OUT_ROOTFS_FLAT\"\n\necho \"$OUT_ROOTFS_TAR\", \"$OUT_ROOTFS_FLAT\" and \"$OUT_FSJSON\" created.\n"
  },
  {
    "path": "tools/docker/exec/Dockerfile",
    "content": "FROM alpine:3.19 as v86-builder\nWORKDIR /v86\n\nRUN apk add --update curl clang make openjdk8-jre-base npm python3\n\nRUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && PATH=\"${HOME}/.cargo/bin:${PATH}\" rustup target add wasm32-unknown-unknown\n\nCOPY . .\n\nRUN PATH=\"${HOME}/.cargo/bin:${PATH}\" make all && rm -rf closure-compiler gen lib src tools .cargo cargo.toml Makefile\n\nFROM python:3.10.13-alpine3.19\nWORKDIR /v86\n\nCOPY --from=v86-builder v86 .\n\nARG PORT=8000\nCMD python3 -m http.server ${PORT}\n\nEXPOSE ${PORT}\n"
  },
  {
    "path": "tools/docker/exec/build.sh",
    "content": "#!/usr/bin/env bash\nset -euo pipefail\ncd \"$(dirname \"$0\")\"\ntar -cf - ../../../{*.html,*.js,*.css,gen,lib,src,bios,.cargo,Cargo.toml,Makefile,tools} | \\\n    docker build -t v86:alpine-3.14 -f tools/docker/exec/Dockerfile -\n"
  },
  {
    "path": "tools/docker/test-image/Dockerfile",
    "content": "FROM ubuntu:21.04\n\nRUN \\\n        export DEBIAN_FRONTEND=noninteractive && \\\n        dpkg --add-architecture i386 && \\\n        apt-get update -qq && \\\n        apt-get install -y nodejs nasm gdb unzip p7zip-full openjdk-8-jre wget python python3 qemu-system-x86 git-core build-essential libc6-dev-i386-cross libc6-dev-i386 clang curl time && \\\n        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \\\n        export PATH=\"$HOME/.cargo/bin:$PATH\" && \\\n        rustup toolchain install stable && \\\n        rustup target add wasm32-unknown-unknown && \\\n        rustup component add rustfmt-preview\n"
  },
  {
    "path": "tools/fetch_network_harness.mjs",
    "content": "import dgram from \"node:dgram\";\nimport { V86, FetchNetworkAdapter } from \"../nodejs-loader.mjs\";\n\n//  qemu-system-i386 -m 2G -nographic -hda ~/disk.qcow2 -netdev dgram,id=net0,remote.type=inet,remote.host=127.0.0.1,remote.port=6677,local.host=127.0.0.1,local.port=7744,local.type=inet -device e1000,netdev=net0\n\nconst server = dgram.createSocket(\"udp4\");\nconst events = {};\n\nconst bus = {\n    register: (name, fn, bind) => events[name] = fn.bind(bind),\n    send: (name, data) => events[name](data)\n};\n\nconst a = new FetchNetworkAdapter(bus);\n\nserver.on(\"error\", (err) => {\n  console.error(`server error:\\n${err.stack}`);\n  server.close();\n});\n\nserver.on(\"message\", (msg, rinfo) => {\n  //console.log(`server got: ${msg.toString(\"hex\")} from ${rinfo.address}:${rinfo.port}`);\n  events[\"net0-receive\"] = (data) => {\n    //console.log(`sending: ${Buffer.from(data).toString(\"hex\")} to ${rinfo.address}:${rinfo.port}`);\n    server.send(Buffer.from(data), rinfo.port);\n  };\n  bus.send(\"net0-send\", new Uint8Array(msg));\n});\n\nserver.on(\"listening\", () => {\n  const address = server.address();\n\n  console.log(`server listening ${address.address}:${address.port}`);\n});\n\nserver.bind(6677);\n"
  },
  {
    "path": "tools/fs2json.py",
    "content": "#!/usr/bin/env python3\n\n# Note:\n# - Hardlinks are copied\n# - The size of symlinks and directories is meaningless, it depends on whatever\n#   the filesystem/tar file reports\n\nimport argparse\nimport json\nimport os\nimport stat\nimport sys\nimport itertools\nimport logging\nimport hashlib\nimport tarfile\n\nVERSION = 3\n\nIDX_NAME = 0\nIDX_SIZE = 1\nIDX_MTIME = 2\nIDX_MODE = 3\nIDX_UID = 4\nIDX_GID = 5\n\n# target for symbolic links\n# child nodes for directories\n# filename for files\nIDX_TARGET = 6\nIDX_FILENAME = 6\n\nHASH_LENGTH = 8\n\nS_IFLNK = 0xA000\nS_IFREG = 0x8000\nS_IFDIR = 0x4000\n\ndef hash_file(filename) -> str:\n    with open(filename, \"rb\", buffering=0) as f:\n        return hash_fileobj(f)\n\ndef hash_fileobj(f) -> str:\n    h = hashlib.sha256()\n    for b in iter(lambda: f.read(128*1024), b\"\"):\n        h.update(b)\n    return h.hexdigest()\n\ndef main():\n    logging.basicConfig(format=\"%(message)s\")\n    logger = logging.getLogger(\"fs2json\")\n    logger.setLevel(logging.DEBUG)\n\n    args = argparse.ArgumentParser(description=\"Create filesystem JSON. Example:\\n\"\n                                               \"    ./fs2json.py --exclude /boot/ --out fs.json /mnt/\",\n                                   formatter_class=argparse.RawTextHelpFormatter\n                                  )\n    args.add_argument(\"--exclude\",\n                      action=\"append\",\n                      metavar=\"path\",\n                      help=\"Path to exclude (relative to base path). Can be specified multiple times.\")\n    args.add_argument(\"--out\",\n                      metavar=\"out\",\n                      nargs=\"?\",\n                      type=argparse.FileType(\"w\"),\n                      help=\"File to write to (defaults to stdout)\",\n                      default=sys.stdout)\n    args.add_argument(\"path\",\n                      metavar=\"path-or-tar\",\n                      help=\"Base path or tar file to include in JSON\")\n    args.add_argument(\"--zstd\", action=\"store_true\",\n                      help=\"Use Zstandard compression\")\n\n    args = args.parse_args()\n\n    path = os.path.normpath(args.path)\n\n    if os.path.isfile(path):\n        tar = tarfile.open(path, \"r\")\n    else:\n        tar = None\n\n    if tar:\n        (root, total_size) = handle_tar(logger, tar, args.zstd)\n    else:\n        (root, total_size) = handle_dir(logger, path, args.exclude, args.zstd)\n\n    if False:\n        # normalize the order of children, useful to debug differences between\n        # the tar and filesystem reader\n        def sort_children(children):\n            for c in children:\n                if isinstance(c[IDX_TARGET], list):\n                    sort_children(c[IDX_TARGET])\n            children.sort()\n\n        sort_children(root)\n\n    result = {\n        \"fsroot\": root,\n        \"version\": VERSION,\n        \"size\": total_size,\n    }\n\n    logger.info(\"Creating json ...\")\n    json.dump(result, args.out, check_circular=False, separators=(',', ':'))\n\ndef handle_dir(logger, path, exclude, use_compression):\n    path = path + \"/\"\n    exclude = exclude or []\n    exclude = [os.path.join(\"/\", os.path.normpath(p)) for p in exclude]\n    exclude = set(exclude)\n\n    def onerror(oserror):\n        logger.warning(oserror)\n\n    rootdepth = path.count(\"/\")\n    files = os.walk(path, onerror=onerror)\n    prevpath = []\n\n    mainroot = []\n    filename_to_hash = {}\n    total_size = 0\n    rootstack = [mainroot]\n\n    def make_node(st, name):\n        obj = [None] * 7\n\n        obj[IDX_NAME] = name\n        obj[IDX_SIZE] = st.st_size\n        obj[IDX_MTIME] = int(st.st_mtime)\n        obj[IDX_MODE] = int(st.st_mode)\n\n        obj[IDX_UID] = st.st_uid\n        obj[IDX_GID] = st.st_gid\n\n        nonlocal total_size\n        total_size += st.st_size\n\n        # Missing:\n        #     int(st.st_atime),\n        #     int(st.st_ctime),\n\n        return obj\n\n    logger.info(\"Creating file tree ...\")\n\n    for f in files:\n        dirpath, dirnames, filenames = f\n        pathparts = dirpath.split(\"/\")\n        pathparts = pathparts[rootdepth:]\n        fullpath = os.path.join(\"/\", *pathparts)\n\n        if fullpath in exclude:\n            dirnames[:] = []\n            continue\n\n        depth = 0\n        for this, prev in zip(pathparts, prevpath):\n            if this != prev:\n                break\n            depth += 1\n\n        for _name in prevpath[depth:]:\n            rootstack.pop()\n\n        oldroot = rootstack[-1]\n\n        assert len(pathparts[depth:]) == 1\n        openname = pathparts[-1]\n\n        if openname == \"\":\n            root = mainroot\n        else:\n            root = []\n            st = os.stat(dirpath)\n            rootobj = make_node(st, openname)\n            rootobj[IDX_TARGET] = root\n            oldroot.append(rootobj)\n\n        rootstack.append(root)\n\n        for filename in itertools.chain(filenames, dirnames):\n            absname = os.path.join(dirpath, filename)\n\n            st = os.lstat(absname)\n            isdir = stat.S_ISDIR(st.st_mode)\n            islink = stat.S_ISLNK(st.st_mode)\n\n            isfile = stat.S_ISREG(st.st_mode)\n\n            if isdir and not islink:\n                continue\n\n            obj = make_node(st, filename)\n\n            if islink:\n                target = os.readlink(absname)\n                obj[IDX_TARGET] = target\n            elif isfile:\n                file_hash = hash_file(absname)\n                filename = file_hash[0:HASH_LENGTH] + (\".bin.zst\" if use_compression else \".bin\")\n                existing = filename_to_hash.get(filename)\n                assert existing is None or existing == file_hash, \"Collision in short hash (%s and %s)\" % (existing, file_hash)\n                filename_to_hash[filename] = file_hash\n                obj[IDX_FILENAME] = filename\n\n            while obj[-1] is None:\n                obj.pop()\n\n            root.append(obj)\n\n        prevpath = pathparts\n\n    return (mainroot, total_size)\n\ndef handle_tar(logger, tar, use_compression):\n    mainroot = []\n    filename_to_hash = {}\n    total_size = 0\n\n    for member in tar.getmembers():\n        parts = member.name.split(\"/\")\n        name = parts.pop()\n\n        dir = mainroot\n\n        for p in parts:\n            for c in dir:\n                if c[IDX_NAME] == p:\n                    dir = c[IDX_TARGET]\n\n        obj = [None] * 7\n        obj[IDX_NAME] = name\n        obj[IDX_SIZE] = member.size\n        obj[IDX_MTIME] = member.mtime\n        obj[IDX_MODE] = member.mode\n        obj[IDX_UID] = member.uid\n        obj[IDX_GID] = member.gid\n\n        if member.isfile() or member.islnk():\n            obj[IDX_MODE] |= S_IFREG\n            f = tar.extractfile(member)\n            file_hash = hash_fileobj(f)\n            filename = file_hash[0:HASH_LENGTH] + (\".bin.zst\" if use_compression else \".bin\")\n            existing = filename_to_hash.get(filename)\n            assert existing is None or existing == file_hash, \"Collision in short hash (%s and %s)\" % (existing, file_hash)\n            filename_to_hash[filename] = file_hash\n            obj[IDX_FILENAME] = filename\n            if member.islnk():\n                # fix size for hard links\n                f.seek(0, os.SEEK_END)\n                obj[IDX_SIZE] = int(f.tell())\n        elif member.isdir():\n            obj[IDX_MODE] |= S_IFDIR\n            obj[IDX_TARGET] = []\n        elif member.issym():\n            obj[IDX_MODE] |= S_IFLNK\n            obj[IDX_TARGET] = member.linkname\n        else:\n            logger.error(\"Unsupported type: {} ({})\".format(member.type, name))\n\n        total_size += obj[IDX_SIZE]\n\n        while obj[-1] is None:\n            obj.pop()\n\n        dir.append(obj)\n\n    return mainroot, total_size\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "tools/mkisofs.js",
    "content": "#!/usr/bin/env node\n\nimport fs from \"node:fs\";\nimport cp from \"node:child_process\";\n\nimport * as iso9660 from \"../src/iso9660.js\";\n\n// this tool is for testing, v86 in browser already has this built-in\n// usage: mkisofs.js [files...]\n\nconst files = process.argv.slice(2);\nconst iso = iso9660.generate(files.map(name => ({ name, contents: fs.readFileSync(name) })));\n\nfs.writeFileSync(\"test.iso\", iso);\nconsole.log(\"test.iso written\");\n\n//cp.spawnSync(\"mkisofs\", [\"-o\", \"reference.iso\"].concat(files), { stdio: \"inherit\" });\ncp.spawnSync(\"7z\", [\"l\", \"test.iso\"], { stdio: \"inherit\" });\n//cp.spawnSync(\"diff\", [\"<(hexdump reference.iso)\", \"<(hexdump test.iso)\"], { stdio: \"inherit\", shell: true });\n"
  },
  {
    "path": "tools/rust-lld-wrapper",
    "content": "#!/usr/bin/env python3\n\n# A wrapper for rust-lld that removes certain arguments inserted by rustc that\n# we'd like to override\n\nimport sys\nimport subprocess\nimport re\nfrom os import path\n\ndef main():\n    args = sys.argv[1:]\n\n    strip_debug = \"--v86-strip-debug\" in args\n\n    # filter out args inserted by rustc\n    TO_REMOVE = {\n        \"--export-table\",\n        \"--stack-first\",\n        \"--strip-debug\",\n        \"--v86-strip-debug\",\n    }\n    args = list(filter(lambda arg: arg not in TO_REMOVE, args))\n\n    if strip_debug:\n        args += [\"--strip-debug\"]\n\n    lld = find_rust_lld()\n\n    result = subprocess.run([lld] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n\n    print(result.stderr, file=sys.stderr)\n    print(result.stdout)\n\n    result.check_returncode()\n\ndef find_host_triplet():\n    rustc = subprocess.run([\"rustc\", \"--version\", \"--verbose\"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n    rustc.check_returncode()\n\n    rustc_details = rustc.stdout.decode(\"utf8\")\n    host = re.search(r\"host: (.*)\", rustc_details)\n    if host is None:\n        raise ValueError(\"unexpected rustc output\")\n    return host.group(1)\n\ndef find_rust_lld():\n    try:\n        which = subprocess.run([\"rustup\", \"which\", \"rustc\"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n    except FileNotFoundError:\n        return \"lld\"\n    which.check_returncode()\n\n    rustc_path = which.stdout.decode(\"utf8\").strip()\n    assert path.basename(rustc_path) == \"rustc\"\n\n    bin_path = path.dirname(rustc_path)\n    triplet = find_host_triplet()\n\n    rust_lld_path = path.join(bin_path, \"../lib/rustlib\", triplet, \"bin/rust-lld\")\n    assert path.isfile(rust_lld_path)\n\n    return rust_lld_path\n\nmain()\n"
  },
  {
    "path": "tools/split-image.py",
    "content": "#!/usr/bin/env python3\n\nimport sys\nimport subprocess\nimport os.path\n\nargs = sys.argv\n\nzstd = \"--zstd\" in args\ngzip = \"--gzip\" in args\nif zstd: args.remove(\"--zstd\")\nif gzip: args.remove(\"--gzip\")\n\nif len(args) != 4:\n    print(\"Usage: split-image.py [--zstd|--gzip] partsize filename-in filename-out-with-%d-%d\")\n    exit(1)\n\n(_, partsize_raw, infile, outfile) = sys.argv\npartsize_raw = partsize_raw.lower()\nif partsize_raw.endswith(\"m\") or partsize_raw.endswith(\"mb\"):\n    partsize_base = 1024 * 1024\n    partsize_raw = partsize_raw.removesuffix(\"mb\").removesuffix(\"m\")\nelif partsize_raw.endswith(\"k\") or partsize_raw.endswith(\"k\"):\n    partsize_base = 1024\n    partsize_raw = partsize_raw.removesuffix(\"k\").removesuffix(\"k\")\nelse:\n    partsize_base = 1\npartsize = partsize_base * int(partsize_raw)\n\nwith open(infile, \"rb\") as f:\n    readf = f.read()\n\nsize = len(readf)\nif len(readf) % partsize != 0:\n    print(\"Warning: size % partsize != 0\")\n\nunit = \"B\"\nif size % 1024 == 0: size //= 1024; unit = \"kB\"\nif size % 1024 == 0: size //= 1024; unit = \"mB\"\n\nprint(\"Size: %d %s, creating %d chunks\" % (size, unit, len(readf) / partsize))\n\ntry:\n    os.mkdir(os.path.dirname(outfile))\nexcept FileExistsError:\n    pass\n\nfor i in range(0, len(readf), partsize):\n    part_name = outfile % (i, i + partsize)\n    with open(part_name, \"wb\") as f:\n        chunk = readf[i:i + partsize]\n        f.write(chunk)\n        if len(chunk) < partsize:\n            # last chunk\n            f.write(bytes(partsize - len(chunk)))\n    if zstd: subprocess.run([\"zstd\", \"-19\", \"-f\", \"--rm\", part_name], check=True)\n    elif gzip: subprocess.run([\"gzip\", \"-9\", \"-f\", part_name], check=True)\n"
  },
  {
    "path": "v86.css",
    "content": "#log, #runtime_infos, #serial, #filesystem_panel, #debug_panel {\n    font-family: DejaVu Sans Mono, monospace;\n    font-size: 13px;\n    border: 1px solid #333;\n    background-color: #000;\n    padding: 4px;\n    color: #fff;\n}\n#screen_container:fullscreen {\n    display: flex !important;\n    align-items: center;\n    justify-content: center;\n}\n#screen_container:fullscreen #vga {\n    width: auto !important;\n    height: 100% !important;\n}\n#runtime_infos, #filesystem_panel {\n    float: left;\n    width: 250px;\n    margin-bottom: 7px;\n    margin-right: 7px;\n    padding-bottom: 7px;\n}\n#log {\n    height: 100px;\n    width: 550px;\n}\n#debug_panel {\n    white-space: pre;\n    float: left;\n}\n#serial {\n    margin: 0;\n}\n#serial:focus {\n    outline: 1px solid rgb(229, 151, 0);\n}\n#screen {\n    white-space: pre;\n    position: relative;\n    font-family: Liberation Mono, DejaVu Sans Mono, Courier New, monospace;\n    font-weight: bold;\n    font-size: 15px;\n    line-height: normal;\n}\n#screen, #vga {\n    -webkit-transform-origin: top left;\n    -moz-transform-origin: top left;\n    transform-origin: top left;\n}\n#vga {\n    background-color: #000;\n    touch-action: none;\n}\n#runtime_options {\n    padding-bottom: 1.5em;\n}\nbody {\n    background-color: #111;\n    color: #fff;\n    line-height: 1.5;\n    padding: 10px;\n    font-family: sans-serif;\n}\na {\n    color: wheat;\n    text-decoration: none;\n    cursor: pointer;\n}\n.phone_keyboard {\n    width: 0;\n    height: 0;\n    resize: none;\n    position: absolute;\n    opacity: 0;\n    left: -9999em;\n    top: 0;\n    z-index: -10;\n    white-space: nowrap;\n    overflow: hidden;\n}\nh4 {\n    margin: 0 0 9px 0;\n}\n#start_emulation {\n    padding: 6px 18px;\n    font-size: 16px;\n    font-weight: bold;\n}\n#setup_error {\n    color: red;\n    font-weight: bold;\n    background-color: #222;\n    padding: 8px 18px;\n}\n#config_link {\n    font-weight: bold;\n    background-color: #222;\n    padding: 8px 18px;\n}\n#screen_container {\n    float: left;\n    margin-right: 10px;\n    margin-bottom: 10px;\n    outline: 1px solid #555;\n}\n#boot_options td, #boot_options th {\n    padding: 1px 7px;\n}\n\n#oses a {\n    color: #fff;\n}\n#oses a span:nth-child(1) {\n    font-weight: bold;\n    color: wheat;\n}\n#oses .th {\n    font-weight: bold;\n}\n#oses a.tr:hover {\n    background-color: #311;\n}\n#oses a span:nth-child(2) {\n    text-align: right;\n}\n\n.table { display: table }\n.th { display: table-header-group }\n.tr { display: table-row }\n.tr > span { display: table-cell; padding: 2px 5px }\n\n/* This is the best I managed to do with my little css experience.\n   If you can do better, please send a PR. */\n@media (max-width: 1250px) {\n    #oses span:nth-child(9), #oses span:nth-child(9) {\n        display: none;\n    }\n}\n@media (max-width: 1150px) {\n    #oses span:nth-child(8), #oses span:nth-child(7), #oses span:nth-child(8), #oses span:nth-child(7) {\n        display: none;\n    }\n}\n@media (max-width: 1050px) {\n    #oses span:nth-child(5), #oses span:nth-child(6), #oses span:nth-child(5), #oses span:nth-child(6) {\n        display: none;\n    }\n}\n@media (max-width: 850px) {\n    #oses span:nth-child(4), #oses span:nth-child(4) {\n        display: none;\n    }\n}\n@media (max-width: 750px) {\n    #oses .th span:nth-child(10) {\n        display: none;\n    }\n    #oses span:nth-child(1), #oses span:nth-child(2), #oses span:nth-child(3), #oses span:nth-child(3) div {\n        display: inline;\n    }\n    #oses span:nth-child(2), #oses span:nth-child(3) {\n        font-size: smaller;\n    }\n    #oses span:nth-child(10) {\n        display: block;\n        padding-bottom: 10px;\n    }\n}\n\nlabel {\n    user-select: none;\n    white-space: nowrap;\n}\ninput[type=checkbox] {\n    vertical-align: middle;\n    position: relative;\n    bottom: 1px;\n    margin: 0;\n}\ninput[type=number] {\n    width: 75px;\n}\n#filter label {\n    background-color: #444;\n    padding: 2px 4px;\n    border-radius: 4px;\n}\n\n#terminal {\n    max-width: 1024px;\n}\n.blink {\n    animation: blink 0.6s step-start infinite;\n}\n@keyframes blink {\n    50% {\n        color: transparent;\n    }\n}\n.blinking-cursor {\n    animation: blinking-cursor 0.6s step-start infinite;\n}\n@keyframes blinking-cursor {\n    50% {\n        background-color: transparent;\n    }\n}\n\n.gui_icon {\n\theight: 1em;\n\tcontent: url('data:image/svg+xml,<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 56 50\"><path d=\"M65.6 102.7h15.3v5.1H65.6z\" style=\"fill:white;fill-opacity:1;stroke:none\" transform=\"translate(-64.1 -84.7)\"/><path d=\"M80.9 92.4H118V118H81ZM65 103.6v25.6h38V118H80.8v-14.4z\" style=\"fill:none;stroke:white;stroke-width:2;stroke-linecap:square\" transform=\"translate(-64.1 -84.7)\"/><path d=\"M80.9 92.4h37.9v5.1H80.9zm8.7 21.1v-12.3l7.6 7.6-2.7 1.5 1.9 3.2-1.8 1-2-3.4z\" style=\"fill:white;fill-opacity:1;stroke:none\" transform=\"translate(-64.1 -84.7)\"/></svg>');\n}\n.tui_icon {\n\theight: 1em;\n\tcontent: url('data:image/svg+xml,<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 55 48\"><g style=\"fill:white\"><path d=\"M91 127h26v5H91z\" transform=\"translate(-64 -85)\"/><path d=\"M118 17h26v5h-26z\" transform=\"rotate(45 70 -120)\"/><path d=\"M-43 139h26v5h-26z\" transform=\"rotate(-45 -134 35)\"/></g></svg>');\n}\n\n.theatre_screen_container {\n    position: fixed;\n    transform: translate(-50%, -50%);\n    top: 50dvh;\n    left: 50vw;\n    z-index: 2;\n    outline: none !important;\n    margin: 0 !important;\n    float: none;\n}\n.theatre_runtime_options {\n    position: fixed;\n    width: 100vw;\n    top: 0;\n    left: 0;\n    padding: 5px !important;\n    z-index: 3;\n    background-color: black;\n    border-bottom: 2px solid #333;\n    transform: translate(0%, -50%);\n}\n.theatre_runtime_options:hover {\n    transform: translate(0%, 0%);\n}\n.theatre_runtime_infos {\n    position: fixed;\n    bottom: 0;\n    z-index: 4;\n    margin: 0 !important;\n    transform: translate(-10px, calc(100% - 65px));\n    opacity: 40%;\n}\n.theatre_runtime_infos:hover {\n    transform: translate(-10px, -5px);\n}\n.theatre_filesystem_panel {\n    position: fixed;\n    bottom: 0;\n    z-index: 4;\n    margin: 0 !important;\n    transform: translate(0%, 75%);\n    left: 280px;\n}\n.theatre_filesystem_panel:hover {\n    transform: translate(0%, -5px);\n}\n#theatre_background {\n    position: fixed;\n    width: 100vw;\n    height: 100dvh;\n    top: 0;\n    left: 0;\n    background-color: black;\n    display: none;\n    z-index: 1;\n}\n#toggle_ui {\n    display: none;\n    position: fixed;\n    bottom: 0;\n    right: 0;\n    z-index: 5;\n}\n.theatre_filesystem_panel, .theatre_filesystem_panel {\n    opacity: 40%;\n}\n#toggle_ui, .theatre_runtime_options {\n    opacity: 25%;\n}\n#toggle_ui,\n.theatre_runtime_options,\n.theatre_runtime_infos,\n.theatre_filesystem_panel {\n    transition: 0.25s;\n}\n#toggle_ui:hover,\n.theatre_runtime_options:hover,\n.theatre_runtime_infos:hover,\n.theatre_filesystem_panel:hover {\n    opacity: 100%;\n}\n\n/* the code below was copied from xterm.css */\n\n.xterm {\n    cursor: text;\n    position: relative;\n    user-select: none;\n    -ms-user-select: none;\n    -webkit-user-select: none;\n}\n\n.xterm.focus,\n.xterm:focus {\n    outline: none;\n}\n\n.xterm .xterm-helpers {\n    position: absolute;\n    top: 0;\n    /**\n     * The z-index of the helpers must be higher than the canvases in order for\n     * IMEs to appear on top.\n     */\n    z-index: 5;\n}\n\n.xterm .xterm-helper-textarea {\n    padding: 0;\n    border: 0;\n    margin: 0;\n    /* Move textarea out of the screen to the far left, so that the cursor is not visible */\n    position: absolute;\n    opacity: 0;\n    left: -9999em;\n    top: 0;\n    width: 0;\n    height: 0;\n    z-index: -5;\n    /** Prevent wrapping so the IME appears against the textarea at the correct position */\n    white-space: nowrap;\n    overflow: hidden;\n    resize: none;\n}\n\n.xterm .composition-view {\n    /* TODO: Composition position got messed up somewhere */\n    background: #000;\n    color: #FFF;\n    display: none;\n    position: absolute;\n    white-space: nowrap;\n    z-index: 1;\n}\n\n.xterm .composition-view.active {\n    display: block;\n}\n\n.xterm .xterm-viewport {\n    /* On OS X this is required in order for the scroll bar to appear fully opaque */\n    background-color: #000;\n    overflow-y: scroll;\n    cursor: default;\n    position: absolute;\n    right: 0;\n    left: 0;\n    top: 0;\n    bottom: 0;\n}\n\n.xterm .xterm-screen {\n    position: relative;\n}\n\n.xterm .xterm-screen canvas {\n    position: absolute;\n    left: 0;\n    top: 0;\n}\n\n.xterm .xterm-scroll-area {\n    visibility: hidden;\n}\n\n.xterm-char-measure-element {\n    display: inline-block;\n    visibility: hidden;\n    position: absolute;\n    top: 0;\n    left: -9999em;\n    line-height: normal;\n}\n\n.xterm.enable-mouse-events {\n    /* When mouse events are enabled (eg. tmux), revert to the standard pointer cursor */\n    cursor: default;\n}\n\n.xterm.xterm-cursor-pointer,\n.xterm .xterm-cursor-pointer {\n    cursor: pointer;\n}\n\n.xterm.column-select.focus {\n    /* Column selection mode */\n    cursor: crosshair;\n}\n\n.xterm .xterm-accessibility,\n.xterm .xterm-message {\n    position: absolute;\n    left: 0;\n    top: 0;\n    bottom: 0;\n    right: 0;\n    z-index: 10;\n    color: transparent;\n    pointer-events: none;\n}\n\n.xterm .live-region {\n    position: absolute;\n    left: -9999px;\n    width: 1px;\n    height: 1px;\n    overflow: hidden;\n}\n\n.xterm-dim {\n    /* Dim should not apply to background, so the opacity of the foreground color is applied\n     * explicitly in the generated class and reset to 1 here */\n    opacity: 1 !important;\n}\n\n.xterm-underline-1 { text-decoration: underline; }\n.xterm-underline-2 { text-decoration: double underline; }\n.xterm-underline-3 { text-decoration: wavy underline; }\n.xterm-underline-4 { text-decoration: dotted underline; }\n.xterm-underline-5 { text-decoration: dashed underline; }\n\n.xterm-overline {\n    text-decoration: overline;\n}\n\n.xterm-overline.xterm-underline-1 { text-decoration: overline underline; }\n.xterm-overline.xterm-underline-2 { text-decoration: overline double underline; }\n.xterm-overline.xterm-underline-3 { text-decoration: overline wavy underline; }\n.xterm-overline.xterm-underline-4 { text-decoration: overline dotted underline; }\n.xterm-overline.xterm-underline-5 { text-decoration: overline dashed underline; }\n\n.xterm-strikethrough {\n    text-decoration: line-through;\n}\n\n.xterm-screen .xterm-decoration-container .xterm-decoration {\n\tz-index: 6;\n\tposition: absolute;\n}\n\n.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer {\n\tz-index: 7;\n}\n\n.xterm-decoration-overview-ruler {\n    z-index: 8;\n    position: absolute;\n    top: 0;\n    right: 0;\n    pointer-events: none;\n}\n\n.xterm-decoration-top {\n    z-index: 2;\n    position: relative;\n}\n"
  },
  {
    "path": "v86.d.ts",
    "content": "// warning: experimental, incomplete and likely to change in the future\n// if you find problems, please send a pull request\n\n/**\n * The type of disk/bios/state images\n *\n * `async` defaults to false, but this is subject to change. If true, the file\n * is downloaded completely, otherwise in chunks (see below for the chunking\n * method). BIOS, multiboot, bzimage and state files are always downloaded\n * completely, as they're required before emulation can start.\n *\n * Files with `async: true` and `use_parts: false` are downloaded using HTTP\n * Range requests. Note that not all web servers support this header correctly,\n * and it inherently disables HTTP compression. The `url` fields points\n * directly to the disk images.\n *\n * If you specify `use_parts: true`, v86 expects the image to be split in files\n * of `fixed_chunk_size` bytes. You can use `tools/split-image.py` in the\n * repository to split an image. V86 appends `-<start-byte>-<end-byte>` to the\n * url.\n *\n * state images and fixed-size chunks (but not other image types) that end with\n * .zst are automatically decompressed using a built-in zstd decompressor. This\n * has a performance overhead compared to HTTP compression, but will result in\n * better compression ration.\n */\nexport type V86Image =\n    | {\n          url: string;\n          async?: boolean;\n          size: number;\n          use_parts?: boolean;\n          fixed_chunk_size?: number;\n      }\n    //| { buffer: File; async?: boolean; }; // only in browsers: https://developer.mozilla.org/en-US/docs/Web/API/File\n    | { buffer: ArrayBuffer };\n\n/**\n * Console types:\n * `textarea` - using TextArea HTML element, doesn't support ESC codes\n * `xtermjs` - using XtermJS-compatible terminal\n *\n * `xterm_lib` - XtermJS constructor, useful for ESM users. When not set,\n * `window[\"Terminal\"]` is used\n *\n * `container` - HTML container for console\n */\nexport type ConsoleConfig =\n    {\n        type: \"textarea\" | \"xtermjs\" | \"none\";\n        xterm_lib?: Function;\n        container?: HTMLElement | HTMLTextAreaElement;\n    };\n\nexport enum LogLevel {\n    LOG_ALL = -1,\n    LOG_NONE = 0,\n    LOG_OTHER = 0x000001,\n    LOG_CPU = 0x000002,\n    LOG_FPU = 0x000004,\n    LOG_MEM = 0x000008,\n    LOG_DMA = 0x000010,\n    LOG_IO = 0x000020,\n    LOG_PS2 = 0x000040,\n    LOG_PIC = 0x000080,\n    LOG_VGA = 0x000100,\n    LOG_PIT = 0x000200,\n    LOG_MOUSE = 0x000400,\n    LOG_PCI = 0x000800,\n    LOG_BIOS = 0x001000,\n    LOG_FLOPPY = 0x002000,\n    LOG_SERIAL = 0x004000,\n    LOG_DISK = 0x008000,\n    LOG_RTC = 0x010000,\n    LOG_HPET = 0x020000,\n    LOG_ACPI = 0x040000,\n    LOG_APIC = 0x080000,\n    LOG_NET = 0x100000,\n    LOG_VIRTIO = 0x200000,\n    LOG_9P = 0x400000,\n    LOG_SB16 = 0x800000,\n}\n\nexport enum BootOrder {\n    AUTO = 0,\n    CD_FLOPPY_HARDDISK = 0x213,\n    CD_HARDDISK_FLOPPY = 0x123,\n    FLOPPY_CD_HARDDISK = 0x231,\n    FLOPPY_HARDDISK_CD = 0x321,\n    HARDDISK_CD_FLOPPY = 0x132,\n}\n\nexport type Event =\n    | \"9p-attach\"\n    | \"9p-read-end\"\n    | \"9p-read-start\"\n    | \"9p-write-end\"\n    | \"download-error\"\n    | \"download-progress\"\n    | \"emulator-loaded\"\n    | \"emulator-ready\"\n    | \"emulator-started\"\n    | \"emulator-stopped\"\n    | \"eth-receive-end\"\n    | \"eth-transmit-end\"\n    | \"ide-read-end\"\n    | \"ide-read-start\"\n    | \"ide-write-end\"\n    | \"mouse-enable\"\n    | \"net0-send\"\n    | \"screen-put-char\"\n    | \"screen-set-size\"\n    | \"serial0-output-byte\"\n    | \"virtio-console0-output-bytes\";\n\n/**\n * emulator instance constructor options.\n */\nexport interface V86Options {\n    /**\n     * Reference to the v86 wasm exorted function.\n     */\n    wasm_fn?: (options: WebAssembly.Imports) => Promise<WebAssembly.Exports>;\n\n    /**\n     * Path to v86 wasm artifact\n     * @default \"build/v86.wasm\" or \"build/v86-debug.wasm\" when debug mode enabled\n     */\n    wasm_path?: string;\n\n    /**\n     * The memory size in bytes, should be a power of 2.\n     * @example 16 * 1024 * 1024\n     * @default 64 * 1024 * 1024\n     */\n    memory_size?: number;\n\n    /**\n     * VGA memory size in bytes.\n     * @example 8 * 1024 * 1024\n     * @default 8 * 1024 * 1024\n     */\n    vga_memory_size?: number;\n\n    /**\n     * If emulation should be started when emulator is ready.\n     * @default false\n     */\n    autostart?: boolean;\n\n    /**\n     * If keyboard should be disabled.\n     */\n    disable_keyboard?: boolean;\n\n    /**\n     * If mouse should be disabled.\n     */\n    disable_mouse?: boolean;\n\n    /**\n     * If speaker should be disabled.\n     */\n    disable_speaker?: boolean;\n\n    /**\n     * Either a url pointing to a bios or an ArrayBuffer\n     */\n    bios?: V86Image;\n\n    /**\n     * VGA bios\n     */\n    vga_bios?: V86Image;\n\n    /**\n     * First hard disk\n     */\n    hda?: V86Image;\n\n    /**\n     * Second hard disk\n     */\n    hdb?: V86Image;\n\n    /**\n     * First floppy disk\n     */\n    fda?: V86Image;\n\n    /**\n     * Second floppy disk\n     */\n    fdb?: V86Image;\n\n    /**\n     * CD-ROM\n     * By default, an ejected CD-ROM drive is emulated\n     */\n    cdrom?: V86Image;\n\n    /**\n     * A Linux kernel image to boot (only bzimage format)\n     */\n    bzimage?: V86Image;\n\n    /**\n     * Kernel boot cmdline\n     */\n    cmdline?: string;\n\n    /**\n     * A Linux ramdisk image\n     */\n    initrd?: V86Image;\n\n    /**\n     * Automatically fetch bzimage and initrd from the 9p filesystem\n     */\n    bzimage_initrd_from_filesystem?: boolean;\n\n    /**\n     * multiboot image\n     */\n    multiboot?: V86Image;\n\n    /**\n     * An initial state to load\n     * See `save_state`\n     */\n    initial_state?: V86Image;\n\n    /**\n     * Should the MAC address be preserved from the state image, for operating systems that don't allow you to reload the network card driver\n     */\n    preserve_mac_from_state_image?: boolean;\n\n    /**\n     * A 9p filesystem is supported by the emulator, using a virtio transport. Using it, files can be exchanged with the guest OS\n     * If `basefs` and `baseurl` are omitted, an empty 9p filesystem is created.\n     */\n    filesystem?: {\n        /**\n         * json file created using [fs2json](https://github.com/copy/v86/blob/master/tools/fs2json.py).\n         */\n        baseurl?: string;\n\n        /**\n         * A directory of 9p files, as created by [copy-to-sha256.py](https://github.com/copy/v86/blob/master/tools/copy-to-sha256.py).\n         * For more details, see docs/filesystem.md\n         */\n        basefs?: string;\n\n        /**\n         * A function that will be called for each 9p request.\n         * If specified, this will back Virtio9p instead of a filesystem.\n         * Use this to build or connect to a custom 9p server.\n         */\n        handle9p?: (reqbuf: Uint8Array, reply: (replybuf: Uint8Array) => void) => void;\n\n        /**\n         * A URL to a websocket proxy for 9p.\n         * If specified, this will back Virtio9p instead of a filesystem.\n         * Use this to connect to a custom 9p server over websocket.\n         */\n        proxy_url?: string;\n    };\n\n    /**\n     * A textarea that will receive and send data to the emulated serial terminal.\n     * Alternatively the serial terminal can also be accessed programatically, see [serial.html](../examples/serial.html).\n     * Deprecated in favor of the serial_console config below\n     */\n    serial_container?: HTMLTextAreaElement;\n\n    /**\n     * Xtermjs serial terminal container. When set, serial_container option is ignored.\n     * Deprecated in favor of the serial_console config below\n     */\n    serial_container_xtermjs?: HTMLElement;\n\n    /**\n     * Console adapter for serial console\n     */\n    serial_console: ConsoleConfig;\n\n    /**\n     * Console adapter for virtio console.\n     * Setting to true, creates virtio console device without adapter\n     */\n    virtio_console: ConsoleConfig;\n\n    /**\n     * An HTMLElement. This should have a certain structure, see [basic.html](../examples/basic.html).\n     */\n    screen_container?: HTMLElement | null;\n\n    /**\n     * Enable ACPI (also enables APIC). Experimental and only partially implemented.\n     * @default false\n     */\n    acpi?: boolean;\n\n    /**\n     * log level\n     * @default LogLevel.LOG_NONE\n     */\n    log_level?: LogLevel;\n\n    /**\n     * boot order\n     * @default BootOrder.AUTO\n     */\n    boot_order?: BootOrder;\n\n    /**\n     * fast boot, skips boot menu in bochs bios\n     */\n    fastboot?: boolean;\n\n    /**\n     * create a virtio balloon device\n     * @default false\n     */\n    virtio_balloon?: boolean;\n\n    /**\n     * override the maximum supported cpuid level\n     * used for some versions of Windows, see docs/windows-nt.md\n     */\n    cpuid_level?: number;\n\n    /**\n     * turn off the x86-to-wasm jit\n     * @default false\n     */\n    disable_jit?: boolean;\n\n    /**\n     * The url of a server running websockproxy\n     * Deprecated in favor of the net_device config below\n     */\n    network_relay_url?: string;\n\n    /**\n     * Network device configuration, see docs/networking.md for more infos\n     */\n    net_device?: {\n        type?: \"ne2k\" | \"virtio\";\n        relay_url?: string;\n        id?: number;\n        router_mac?: string;\n        router_ip?: string;\n        vm_ip?: string;\n        masquerade?: boolean;\n        dns_method?: \"static\" | \"doh\";\n        doh_server?: string;\n        cors_proxy?: string;\n        mtu?: number;\n    };\n}\n\nexport class V86 {\n    constructor(options: V86Options);\n\n    /**\n     * Start emulation. Do nothing if emulator is running already. Can be asynchronous.\n     */\n    run(): void;\n\n    /**\n     * Stop emulation. Do nothing if emulator is not running. Can be asynchronous.\n     */\n    stop(): Promise<void>;\n\n    /**\n     * Free resources associated with this instance\n     */\n    destroy(): Promise<void>;\n\n    /**\n     * Restart (force a reboot).\n     */\n    restart(): void;\n\n    /**\n     * Add an event listener (the emulator is an event emitter).\n     *\n     * The callback function gets a single argument which depends on the event.\n     *\n     * @param event Name of the event.\n     * @param listener The callback function.\n     */\n    add_listener(event: Event, listener: Function): void;\n\n    /**\n     * Remove an event listener.\n     *\n     * @param event\n     * @param listener\n     */\n    remove_listener(event: Event, listener: Function): void;\n\n    /**\n     * Restore the emulator state from the given state, which must be an\n     * ArrayBuffer returned by\n     * [`save_state`](#save_statefunctionobject-arraybuffer-callback).\n     *\n     * Note that the state can only be restored correctly if this constructor has\n     * been created with the same options as the original instance (e.g., same disk\n     * images, memory size, etc.).\n     *\n     * Different versions of the emulator might use a different format for the\n     * state buffer.\n     *\n     * @param state\n     */\n    restore_state(state: ArrayBuffer): Promise<void>;\n\n    /**\n     * Asynchronously save the current state of the emulator.\n     */\n    save_state(): Promise<ArrayBuffer>;\n\n    get_instruction_counter(): number;\n    is_running(): boolean;\n\n    /**\n     * Set the image inserted in the floppy drive. Can be changed at runtime, as\n     * when physically changing the floppy disk.\n     */\n    set_fda(image: V86Image): Promise<void>;\n\n    /**\n     * Eject the floppy drive.\n     */\n    eject_fda(): void;\n\n    /**\n     * Set the image inserted in the CD-ROM drive. Can be changed at runtime, as\n     * when physically changing the CD-ROM.\n     */\n    set_cdrom(image: V86Image): Promise<void>;\n\n    /**\n     * Eject the CD-ROM.\n     */\n    eject_cdrom(): void;\n\n    /**\n     * Send a sequence of scan codes to the emulated PS2 controller. A list of\n     * codes can be found at http://stanislavs.org/helppc/make_codes.html.\n     * Do nothing if there is no keyboard controller.\n     *\n     * @param codes\n     */\n    keyboard_send_scancodes(codes: number[]): void;\n\n    /**\n     * Send translated keys\n     */\n    keyboard_send_keys(codes: number[]): void;\n\n    /**\n     * Send text, assuming the guest OS uses a US keyboard layout\n     */\n    keyboard_send_text(string: string): void;\n\n    /**\n     * Download a screenshot (returns an <img> element, only works in browsers)\n     */\n    screen_make_screenshot(): HTMLElement;\n\n    /**\n     * Set the scaling level of the emulated screen.\n     *\n     * @param {number} sx\n     * @param {number} sy\n     */\n    screen_set_scale(sx: number, sy: number): void;\n\n    /**\n     * Go fullscreen (only browsers)\n     */\n    screen_go_fullscreen(): void;\n\n    /**\n     * Lock the mouse cursor: It becomes invisble and is not moved out of the browser window (only browsers)\n     */\n    lock_mouse(): void;\n\n    /**\n     * Enable or disable sending mouse events to the emulated PS2 controller.\n     */\n    mouse_set_enabled(enabled: boolean): void;\n\n    /**\n     * Enable or disable sending keyboard events to the emulated PS2 controller.\n     */\n    keyboard_set_enabled(enabled: boolean): void;\n\n    /**\n     * Send a string to the first emulated serial terminal.\n     *\n     * @param data\n     */\n    serial0_send(data: string): void;\n\n    /**\n     * Send bytes to a serial port (to be received by the emulated PC).\n     *\n     * @param serial the index of the serial port\n     * @param data\n     */\n    serial_send_bytes(serial: number, data: Uint8Array): void;\n\n    /**\n     * Write to a file in the 9p filesystem. Nothing happens if no filesystem has\n     * been initialized.\n     *\n     * @param file\n     * @param data\n     * @param callback\n     */\n    create_file(file: string, data: Uint8Array): Promise<void>;\n\n    /**\n     * Read a file in the 9p filesystem.\n     *\n     * @param {string} file\n     */\n    read_file(file: string): Promise<Uint8Array>;\n}\n"
  }
]