diff --git a/library/compiler-builtins/.editorconfig b/library/compiler-builtins/.editorconfig index 23a4d23f282..a7805a4df7b 100644 --- a/library/compiler-builtins/.editorconfig +++ b/library/compiler-builtins/.editorconfig @@ -12,3 +12,6 @@ indent_size = 4 [*.{yaml,yml}] indent_size = 2 + +[*.json*] +indent_size = 2 diff --git a/library/compiler-builtins/.github/renovate.json5 b/library/compiler-builtins/.github/renovate.json5 index 7d18fd573d6..e1656528ed6 100644 --- a/library/compiler-builtins/.github/renovate.json5 +++ b/library/compiler-builtins/.github/renovate.json5 @@ -5,26 +5,37 @@ ":maintainLockFilesMonthly", "helpers:pinGitHubActionDigestsToSemver" ], + commitMessagePrefix: "deps:", + customManagers: [ + { + customType: "regex", + managerFilePatterns: [".github/workflows/main.yaml"], + matchStrings: ["BENCHMARK_RUSTC:\\s*(?\\S+)"], + autoReplaceStringTemplate: "BENCHMARK_RUSTC: {{{newValue}}}", + depNameTemplate: "nightly", + datasourceTemplate: "rust-version", + versioningTemplate: "rust-release-channel", + }, + ], packageRules: [ { - matchCategories: [ - "rust" - ], - matchJsonata: [ - "isBreaking != true" - ], + matchCategories: ["rust"], + matchJsonata: ["isBreaking != true"], // Disable non-breaking change updates because they // are updated periodically with lockfile maintainance. enabled: false, }, { - matchManagers: [ - "github-actions" - ], + matchManagers: ["github-actions"], // Every month schedule: "* 0 1 * *", + commitMessagePrefix: "ci:", groupName: "Github Actions", - } + }, + { + matchManagers: ["custom.regex"], + commitMessage: "bench: Update pinned toolchain to {{{newVersion}}}", + }, ], // Receive any update that fixes security vulnerabilities. // We need this because we disabled "patch" updates for Rust. diff --git a/library/compiler-builtins/.github/workflows/main.yaml b/library/compiler-builtins/.github/workflows/main.yaml index 53e712528aa..1acd01725f1 100644 --- a/library/compiler-builtins/.github/workflows/main.yaml +++ b/library/compiler-builtins/.github/workflows/main.yaml @@ -16,7 +16,7 @@ env: RUSTDOCFLAGS: -Dwarnings RUSTFLAGS: -Dwarnings -Dlinker_messages RUST_BACKTRACE: full - BENCHMARK_RUSTC: nightly-2026-08-19 # Pin the toolchain for reproducable results + BENCHMARK_RUSTC: nightly-2026-09-26 # Pin the toolchain for reproducable results defaults: run: @@ -51,7 +51,7 @@ jobs: test: name: Build and test - timeout-minutes: 60 + timeout-minutes: 90 # NOTE: self-hosted riscv64 runners are experimental and may be flaky. # Do not block CI on failures from this platform for now. continue-on-error: ${{ contains(matrix.os, 'self-hosted') }} @@ -71,6 +71,8 @@ jobs: os: ubuntu-26.04 - target: armv7-unknown-linux-gnueabihf os: ubuntu-26.04 + - target: armv7-unknown-linux-gnueabihf + os: self-hosted-linux-armhf-noble-large-rust - target: thumbv7neon-unknown-linux-gnueabihf os: ubuntu-26.04 - target: i586-unknown-linux-gnu @@ -94,8 +96,8 @@ jobs: # os: ["self-hosted", "linux", "riscv64"] - target: riscv64gc-unknown-linux-gnu os: ubuntu-26.04 - - target: s390x-unknown-linux-gnu - os: self-hosted-linux-s390x-resolute-large-rust # resolute == ubuntu-26.04 + # - target: s390x-unknown-linux-gnu + # os: self-hosted-linux-s390x-resolute-large-rust # resolute == ubuntu-26.04 - target: s390x-unknown-linux-gnu os: ubuntu-24.04-s390x - target: thumbv6m-none-eabi @@ -149,7 +151,7 @@ jobs: - name: Set up dependencies and Rust run: ./ci/install-test-deps.sh "$JOB_TARGET" "$JOB_HOST_TOOLCHAIN" - - uses: taiki-e/install-action@7f4eb899022d8fe70b20c4f3de697aa85c309026 # v2.85.11 + - uses: taiki-e/install-action@f7e5d7c961414b23f5b25b2da9294395d08513ad # v2.87.26 continue-on-error: true with: tool: nextest@0.9.131 @@ -169,7 +171,7 @@ jobs: key: ${{ matrix.target }}-buildx-${{ github.sha }} restore-keys: ${{ matrix.target }}-buildx- # Configure buildx to use Docker layer caching - - uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4.2.0 + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 if: ${{ env.RUN_IN_DOCKER == 'true' }} - name: Cache compiler-rt @@ -243,7 +245,7 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: { persist-credentials: false } - - uses: zizmorcore/zizmor-action@3dc1ecc9bcb9e94e9b2c709687979e1298497054 # v0.6.2 + - uses: zizmorcore/zizmor-action@cc914d7f3750a2d13d75c7f184a1060aa0e9d482 # v0.6.4 build-custom: name: Build custom target @@ -305,7 +307,7 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: { persist-credentials: false } - - uses: taiki-e/install-action@7f4eb899022d8fe70b20c4f3de697aa85c309026 # v2.85.11 + - uses: taiki-e/install-action@f7e5d7c961414b23f5b25b2da9294395d08513ad # v2.87.26 with: tool: cargo-binstall@1.17.7 @@ -324,7 +326,7 @@ jobs: run: ./ci/bench-icount.sh "$JOB_TARGET" - name: Upload the benchmark baseline - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: ${{ env.BASELINE_NAME }} path: ${{ env.BASELINE_NAME }}.tar.xz diff --git a/library/compiler-builtins/.github/workflows/publish.yaml b/library/compiler-builtins/.github/workflows/publish.yaml index 02ddf0babdf..04daff89009 100644 --- a/library/compiler-builtins/.github/workflows/publish.yaml +++ b/library/compiler-builtins/.github/workflows/publish.yaml @@ -3,6 +3,7 @@ name: Release-plz permissions: pull-requests: write contents: write + id-token: write on: push: { branches: [main] } @@ -10,7 +11,7 @@ on: jobs: release-plz: name: Release-plz - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -20,6 +21,6 @@ jobs: - name: Install Rust (rustup) run: rustup update nightly --no-self-update && rustup default nightly - name: Run release-plz - uses: release-plz/action@2eb1d8bcb770b4c48ccfaad919734b38b51958c9 # v0.5 + uses: release-plz/action@18641b6c63063cc5ff8786ebf69cd57e8541bae1 # v0.5.140 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/library/compiler-builtins/.github/workflows/rustc-pull.yml b/library/compiler-builtins/.github/workflows/rustc-pull.yml index 34e78e44567..04238956fd3 100644 --- a/library/compiler-builtins/.github/workflows/rustc-pull.yml +++ b/library/compiler-builtins/.github/workflows/rustc-pull.yml @@ -13,7 +13,8 @@ env: jobs: pull: if: github.repository == 'rust-lang/compiler-builtins' - uses: rust-lang/josh-sync/.github/workflows/rustc-pull.yml@00d007cf377c524bc2968b40a61ef1d2ce20e283 # main + # zizmor: ignore[unpinned-uses] We explicitly want to track the main branch. + uses: rust-lang/josh-sync/.github/workflows/rustc-pull.yml@b1da651cd99a7325cc242548db6d711639b0c30c # main with: github-app-id: ${{ vars.APP_CLIENT_ID }} pr-author: "workflows-compiler-builtins[bot]" diff --git a/library/compiler-builtins/Cargo.lock b/library/compiler-builtins/Cargo.lock index e203f4d4cfb..ed159e70b5d 100644 --- a/library/compiler-builtins/Cargo.lock +++ b/library/compiler-builtins/Cargo.lock @@ -133,9 +133,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.13.1" +version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "bstr" @@ -174,9 +174,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.4.4" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ad534f4357a5264cce5019c989cf66a4f0dc4e0d1b1d15f8aacec0ff7360273" +checksum = "f74872d07caf508b30a21f6836e7d7016a2eaf7d9ff4f48deaa58cd8a0407630" dependencies = [ "find-msvc-tools", "shlex", @@ -184,9 +184,9 @@ dependencies = [ [[package]] name = "cfg-if" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" [[package]] name = "chacha20" @@ -228,9 +228,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.6.6" +version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "473c7e07f409a8d772161724aa8db6a765a2532a70f9667eeb7b49d3d02fbdca" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" dependencies = [ "clap_builder", "clap_derive", @@ -238,9 +238,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.6.6" +version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b48fea5a88e9ae728a2dcbedbfc0e730f7d60da42e1cb049a83c9fb8b789889" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" dependencies = [ "anstream", "anstyle", @@ -250,21 +250,21 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.6.4" +version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d012d2b9d65aca7f18f4d9878a045bc17899bba951561ba5ec3c2ba1eed9a061" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" dependencies = [ "heck", "proc-macro2", "quote", - "syn 3.0.4", + "syn 3.0.6", ] [[package]] name = "clap_lex" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" +checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" [[package]] name = "colorchoice" @@ -281,9 +281,9 @@ dependencies = [ [[package]] name = "console" -version = "0.16.4" +version = "0.16.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fe5f465a4f6fee88fad41b85d990f84c835335e85b5d9e6e63e0d06d28cba7c" +checksum = "e96a4956774c13c126a8b5af4daa79384f4d826534c95a02d76afb39e2ab64e3" dependencies = [ "encode_unicode", "libc", @@ -301,9 +301,9 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.5.1" +version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8498c871161e1742aaa9d52551b2d6ebdd4c3d45a3be423e3728f33b955be550" +checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78" dependencies = [ "cfg-if", ] @@ -344,9 +344,9 @@ dependencies = [ [[package]] name = "crossbeam-deque" -version = "0.8.7" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5181e0de7b61eb03a81e347d6dd8797bae9da5146707b51077e2d71a54ec0ceb" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" dependencies = [ "crossbeam-epoch", "crossbeam-utils", @@ -354,18 +354,18 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.20" +version = "0.9.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d6914041f254d6e9176c01941b21115dcfb7089e55135a35411081bd106ef3f" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" -version = "0.8.22" +version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" [[package]] name = "crunchy" @@ -408,9 +408,9 @@ checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" [[package]] name = "either" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" [[package]] name = "either-or-both" @@ -446,9 +446,9 @@ dependencies = [ [[package]] name = "escape8259" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5692dd7b5a1978a5aeb0ce83b7655c58ca8efdcb79d21036ea249da95afec2c6" +checksum = "9f1f0d310082fd5de539d4f41601182241147498444a7695c520288a0cdbead6" [[package]] name = "fastrand" @@ -458,9 +458,9 @@ checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" [[package]] name = "find-msvc-tools" -version = "0.1.11" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d45db016d36b838f563236e9193d0ee6ce38f3f68b6c94e914b4929c96bbb890" +checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" [[package]] name = "flate2" @@ -538,21 +538,28 @@ dependencies = [ [[package]] name = "gungraun" -version = "0.19.4" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9300d38d30facf6870c15bc9e243edcc47e5ddc270c9d2c1ae824a00b1faea79" +checksum = "c041375e48b8ef7cd2d0e70715598154af627c1a551e41c35fdb4a2b3910831a" dependencies = [ "bincode-next", "derive_more", + "gungraun-common", "gungraun-macros", "gungraun-runner", ] [[package]] -name = "gungraun-macros" -version = "0.9.1" +name = "gungraun-common" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7f3214bae6cfd6739f4f29edb262bae439c6393a7b7b12c4eeb96417c2e50df" +checksum = "bc270ba55639babb5806b62d4bfbc35e15ca15791e3ec00c7b92b39c62a9d8eb" + +[[package]] +name = "gungraun-macros" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08472a9e996a98a5b4274dd342fb32ebe1a5cfeb2662b6578d0c7bf1dd203129" dependencies = [ "derive_more", "proc-macro-error3", @@ -561,14 +568,14 @@ dependencies = [ "rustc_version", "serde", "serde_json", - "syn 2.0.119", + "syn 3.0.6", ] [[package]] name = "gungraun-runner" -version = "0.19.4" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47c68c92812da579a2510a2cc702d5aa3251992b9662d4a98502ca46e0cbe16e" +checksum = "1e1460377fe936c3efbe8e27c422b745fa54ebaea81ffe4a53a6f17c7a478683" dependencies = [ "either-or-both", "serde", @@ -599,9 +606,9 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indexmap" -version = "2.14.1" +version = "2.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07aa2048142242915a31d35844fb311e0e53fcca590c3a0a40dcf1b841fa09eb" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" dependencies = [ "equivalent", "hashbrown", @@ -642,9 +649,9 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] name = "js-sys" -version = "0.3.104" +version = "0.3.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e0c1080212aad755ea003d18543e8768dd432c48819efd73a7bf1e39b7a5a3a" +checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" dependencies = [ "cfg-if", "futures-util", @@ -653,9 +660,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.189" +version = "0.2.190" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" [[package]] name = "libm" @@ -678,7 +685,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 3.0.4", + "syn 3.0.6", ] [[package]] @@ -754,7 +761,7 @@ checksum = "fc80370b4544f28ffa317e3c3474ee3ecbfe269196c01ae657d9f837a7d944a1" dependencies = [ "proc-macro2", "quote", - "syn 3.0.4", + "syn 3.0.6", ] [[package]] @@ -927,7 +934,7 @@ dependencies = [ "proc-macro-error-attr3", "proc-macro2", "quote", - "syn 3.0.4", + "syn 3.0.6", ] [[package]] @@ -956,9 +963,9 @@ checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" [[package]] name = "rand" -version = "0.10.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ "chacha20", "getrandom", @@ -1081,9 +1088,9 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" dependencies = [ "bitflags", "errno", @@ -1149,7 +1156,7 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn 3.0.4", + "syn 3.0.6", ] [[package]] @@ -1185,9 +1192,9 @@ checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "smallvec" -version = "1.15.2" +version = "1.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" +checksum = "f9395f0f0eee849a9b707b2f06bb92a6a422090e2123bb2ef8e87a0e61892a8e" [[package]] name = "strsim" @@ -1221,15 +1228,19 @@ dependencies = [ [[package]] name = "syn" -version = "3.0.4" +version = "3.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6275cddf4610d1775e6d1fe9469b2e77d0f39fd98fb7450901b821e0c53649f" +checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] +[[package]] +name = "sysbench" +version = "0.1.0" + [[package]] name = "tempfile" version = "3.27.0" @@ -1261,15 +1272,15 @@ dependencies = [ [[package]] name = "twox-hash" -version = "2.1.4" +version = "2.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5283634e518fe9e82c7b20520bb4bc209009fd16c82077c802f8111ecbb0117a" +checksum = "86a801b3cea342a06d468c8710662aa29e5e05e4f5c0d62f00bbb7f2ad7941c2" [[package]] name = "unicode-ident" -version = "1.0.24" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" [[package]] name = "unicode-width" @@ -1338,9 +1349,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.127" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b70935747edd64d89de3efa29d73789b806c15798f8e7dca4d8ac356b50ce70" +checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" dependencies = [ "cfg-if", "once_cell", @@ -1351,9 +1362,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.127" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77775f8f3f7217702089053b94958f8f54061a3f663417df76e19cbdcca29bc1" +checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1361,22 +1372,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.127" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e11d33f857dc2fb11b8bc75aee111aa9cbeb12cd9f25efd3d4c2a3dd4e235284" +checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7" dependencies = [ "bumpalo", "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.6", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.127" +version = "0.2.129" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef64dbcc55df09c7e5a46182d181c2cfa3e925f3da937ea764728b4bbb9dcbf" +checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6" dependencies = [ "unicode-ident", ] @@ -1392,9 +1403,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.104" +version = "0.3.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c435338968042f4f59a557f690a253676d47ce13ceb55d70100e7facf6620a30" +checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4" dependencies = [ "js-sys", "wasm-bindgen", @@ -1464,18 +1475,18 @@ checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] name = "zerocopy" -version = "0.8.56" +version = "0.8.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "556764e583adb45a9f8d413c2a147fa7e8d821e48e12b14fd560b607998b75eb" +checksum = "86502bf56ac7c77571a32e2647bb2a15894565e981fb2a48d7bde2d91c965a9d" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.56" +version = "0.8.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2ab42fc20575779bd240faa45f94a74256f755c0fa9e89f0ede20d91d0cdfc1" +checksum = "5457206954b06561e2608c7e19cf58b1926586d999c246eebe4502f7e2039d1a" dependencies = [ "proc-macro2", "quote", @@ -1484,9 +1495,9 @@ dependencies = [ [[package]] name = "zlib-rs" -version = "0.6.7" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34b31d188d9d685a4f9c7b46d6e36631b07058d2cfe190267adce54dc230bf12" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" [[package]] name = "zmij" diff --git a/library/compiler-builtins/Cargo.toml b/library/compiler-builtins/Cargo.toml index 423adfbcbd0..2091b3aa0f2 100644 --- a/library/compiler-builtins/Cargo.toml +++ b/library/compiler-builtins/Cargo.toml @@ -8,6 +8,7 @@ members = [ "crates/musl-math-sys", "crates/panic-handler", "crates/symcheck", + "crates/sysbench", "crates/update-api-list", "crates/util", "libm", @@ -45,7 +46,7 @@ getopts = "0.2.24" getrandom = "0.4.1" glob = "0.3.3" gmp-mpfr-sys = { version = "1.6.8", default-features = false } -gungraun = "0.19.0" +gungraun = "0.20.0" heck = "0.5.0" indicatif = { version = "0.18.3", default-features = false } libm = { path = "libm", default-features = false } diff --git a/library/compiler-builtins/builtins-test-intrinsics/Cargo.lock b/library/compiler-builtins/builtins-test-intrinsics/Cargo.lock index 4461cbc35bc..73f4f96db2d 100644 --- a/library/compiler-builtins/builtins-test-intrinsics/Cargo.lock +++ b/library/compiler-builtins/builtins-test-intrinsics/Cargo.lock @@ -15,9 +15,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.4.4" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ad534f4357a5264cce5019c989cf66a4f0dc4e0d1b1d15f8aacec0ff7360273" +checksum = "f74872d07caf508b30a21f6836e7d7016a2eaf7d9ff4f48deaa58cd8a0407630" dependencies = [ "find-msvc-tools", "shlex", @@ -32,9 +32,9 @@ dependencies = [ [[package]] name = "find-msvc-tools" -version = "0.1.11" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d45db016d36b838f563236e9193d0ee6ce38f3f68b6c94e914b4929c96bbb890" +checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" [[package]] name = "panic-handler" diff --git a/library/compiler-builtins/builtins-test-intrinsics/src/main.rs b/library/compiler-builtins/builtins-test-intrinsics/src/main.rs index 13bc7337ba3..7673e1dbe0b 100644 --- a/library/compiler-builtins/builtins-test-intrinsics/src/main.rs +++ b/library/compiler-builtins/builtins-test-intrinsics/src/main.rs @@ -10,7 +10,6 @@ #![feature(f128)] #![feature(f16)] #![feature(lang_items)] -#![feature(optimize_attribute)] #![no_std] #![no_main] diff --git a/library/compiler-builtins/builtins-test/benches/float_pow.rs b/library/compiler-builtins/builtins-test/benches/float_pow.rs index d749ae47dda..3d6841525b0 100644 --- a/library/compiler-builtins/builtins-test/benches/float_pow.rs +++ b/library/compiler-builtins/builtins-test/benches/float_pow.rs @@ -22,9 +22,7 @@ float_bench! { asm: [], } -// FIXME(f16_f128): can be changed to only `f128_enabled` once `__multf3` and `__divtf3` are -// distributed by nightly. -#[cfg(all(f128_enabled, not(no_sys_f128)))] +#[cfg(f128_enabled)] float_bench! { name: powi_f128, sig: (a: f128, b: i32) -> f128, @@ -42,7 +40,7 @@ pub fn float_pow() { powi_f32(&mut criterion); powi_f64(&mut criterion); - #[cfg(all(f128_enabled, not(no_sys_f128)))] + #[cfg(f128_enabled)] powi_f128(&mut criterion); } diff --git a/library/compiler-builtins/builtins-test/tests/conv.rs b/library/compiler-builtins/builtins-test/tests/conv.rs index ac808b0ae45..d730c2f393f 100644 --- a/library/compiler-builtins/builtins-test/tests/conv.rs +++ b/library/compiler-builtins/builtins-test/tests/conv.rs @@ -41,13 +41,37 @@ mod i_to_f { ); let f1: $f_ty = $fn(x); + if f1.is_nan() { + panic!("int->float cannot result in a NaN: {}({x})", stringify!($fn)); + } + #[cfg($sys_available)] { + // If `f1` is infinite, calculate the integer that is 1 ULP after/before + // $f_ty::MAX/$f_ty::MIN, saturating as needed. There's no need to + // handle NaN as the result of a conversion can never be NaN. + let as_int = |f: $f_ty| { + if f.is_infinite() { + // For infinity, calculate the float that is half of the desired + // 1-ulp-after-max/-before-min (and therefore representable as + // $f_ty), then multiply by two after converting to an integer. + let bits = <$f_ty>::EXP_MASK - <$f_ty>::IMPLICIT_BIT; + let bits = if f.is_sign_negative() { + bits | <$f_ty as Float>::SIGN_MASK + } else { + bits + }; + // float->int casts saturate, so saturate the multiply to match. + (<$f_ty>::from_bits(bits) as $i_ty).saturating_mul(2) + } else { + f as $i_ty + } + }; // This makes sure that the conversion produced the best rounding possible, and does // this independent of `x as $into` rounding correctly. // This assumes that float to integer conversion is correct. - let y_minus_ulp = <$f_ty>::from_bits(f1.to_bits().wrapping_sub(1)) as $i_ty; - let y = f1 as $i_ty; - let y_plus_ulp = <$f_ty>::from_bits(f1.to_bits().wrapping_add(1)) as $i_ty; + let y_minus_ulp = as_int(f1.next_down()); + let y = as_int(f1); + let y_plus_ulp = as_int(f1.next_up()); let error_minus = <$i_ty as Int>::abs_diff(y_minus_ulp, x); let error = <$i_ty as Int>::abs_diff(y, x); let error_plus = <$i_ty as Int>::abs_diff(y_plus_ulp, x); diff --git a/library/compiler-builtins/builtins-test/tests/lse.rs b/library/compiler-builtins/builtins-test/tests/lse.rs index 20d4d19a328..463d8a8d9c2 100644 --- a/library/compiler-builtins/builtins-test/tests/lse.rs +++ b/library/compiler-builtins/builtins-test/tests/lse.rs @@ -58,7 +58,7 @@ mod cas { crate::run_fuzz_tests_with_lse_variants(10000, |expected: super::int_ty!($bytes), new| { let mut target = expected.wrapping_add(10); let ret: super::int_ty!($bytes) = unsafe { - compiler_builtins::aarch64_outline_atomics::$name::$name( + compiler_builtins::aarch64_outline_atomics::$name( expected, new, &mut target, @@ -77,7 +77,7 @@ mod cas { target = expected; let ret: super::int_ty!($bytes) = unsafe { - compiler_builtins::aarch64_outline_atomics::$name::$name( + compiler_builtins::aarch64_outline_atomics::$name( expected, new, &mut target, @@ -108,9 +108,7 @@ mod swap { let orig_right = right; assert_eq!( unsafe { - compiler_builtins::aarch64_outline_atomics::$name::$name( - left, &mut right, - ) + compiler_builtins::aarch64_outline_atomics::$name(left, &mut right) }, orig_right ); @@ -132,7 +130,13 @@ macro_rules! test_op { let mut target = old; let op: fn(super::int_ty!($bytes), super::int_ty!($bytes)) -> _ = $($op)*; let expected = op(old, val); - assert_eq!(old, unsafe { compiler_builtins::aarch64_outline_atomics::$name::$name(val, &mut target) }, "{} should return original value", stringify!($name)); + assert_eq!( + old, + unsafe { + compiler_builtins::aarch64_outline_atomics::$name(val, &mut target) + }, + "{} should return original value", stringify!($name) + ); assert_eq!(expected, target, "{} should store to target", stringify!($name)); }); } diff --git a/library/compiler-builtins/builtins-test/tests/mem.rs b/library/compiler-builtins/builtins-test/tests/mem.rs index f7ae9c9b5c3..bafee5807e1 100644 --- a/library/compiler-builtins/builtins-test/tests/mem.rs +++ b/library/compiler-builtins/builtins-test/tests/mem.rs @@ -1,5 +1,3 @@ -#![feature(optimize_attribute)] - use compiler_builtins::mem::{memcmp, memcpy, memmove, memset, strlen, wchar_t}; const WORD_SIZE: usize = core::mem::size_of::(); diff --git a/library/compiler-builtins/ci/install-test-deps.sh b/library/compiler-builtins/ci/install-test-deps.sh index b64bcf654ad..cd197a7d915 100755 --- a/library/compiler-builtins/ci/install-test-deps.sh +++ b/library/compiler-builtins/ci/install-test-deps.sh @@ -21,12 +21,34 @@ fi to_install=() if [ "$RUN_IN_DOCKER" != "0" ]; then - ! command -v rustup && to_install+=(rustup) - ! command -v m4 && to_install+=(m4) + command -v libtool || to_install+=(libtool) + command -v m4 || to_install+=(m4) + command -v make || to_install+=(make) + command -v rustup || to_install+=(rustup) fi if [ "$target" = "i686-pc-windows-gnu" ]; then - ! command -v i686-w64-mingw32-gcc && to_install+=(mingw-w64-i686-gcc) + command -v i686-w64-mingw32-gcc || to_install+=(mingw-w64-i686-gcc) +fi + +# specific check for armv7-unknown-linux-gnueabihf on canonical runners +# which are 32-bit armhf-native but have VMs running over an arm64 kernel with AArch32/EL0 support +if [ "$target" = "armv7-unknown-linux-gnueabihf" ] && [ "${RUN_IN_DOCKER:-}" != "true" ] && + [ "$(dpkg --print-architecture 2>/dev/null)" = "arm64" ]; then + + # expose these variables so we can keep the arm64 host toolchain, cross-link with + # the armhf gcc, and run the test binaries natively in AArch32 mode + { + echo "CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc" + echo "CC_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-gcc" + echo "AR_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-ar" + } >> "$GITHUB_ENV" + + # install required tooling to enable cross-compilation + sudo dpkg --add-architecture armhf + to_install+=(gcc-arm-linux-gnueabihf) # cross-compiler + to_install+=(libc6-dev-armhf-cross) # target libc + to_install+=(libc6:armhf libgcc-s1:armhf) # loaded at runtime fi if [ ${#to_install[@]} -ne 0 ]; then @@ -43,6 +65,7 @@ if [ ${#to_install[@]} -ne 0 ]; then fi # Install the correct Rust version +rustup set profile minimal rustup update "$channel" --no-self-update rustup default "$channel" rustup target add "$target" diff --git a/library/compiler-builtins/ci/run.sh b/library/compiler-builtins/ci/run.sh index 7abc44e1c2b..8167158ef49 100755 --- a/library/compiler-builtins/ci/run.sh +++ b/library/compiler-builtins/ci/run.sh @@ -6,11 +6,11 @@ export RUST_BACKTRACE="${RUST_BACKTRACE:-full}" export NEXTEST_STATUS_LEVEL=all target="${1:-}" +host_tuple="$(rustc --print host-tuple)" if [ -z "$target" ]; then - host_target=$(rustc -vV | awk '/^host/ { print $2 }') - echo "Defaulted to host target $host_target" - target="$host_target" + echo "Defaulted to host target $host_tuple" + target="$host_tuple" fi # Print machine information @@ -58,6 +58,9 @@ fi if [ "${BUILD_ONLY:-}" = "1" ]; then echo "no tests to run for build-only targets" else + # Get a quick performance score + cargo run -p sysbench --target "$target" --release + test_builtins=( "${test_runner[@]}" --package builtins-test @@ -138,7 +141,7 @@ echo "::endgroup" # Test libm # Make sure a simple build works -cargo check -p libm --no-default-features --target "$target" +asgroup cargo check -p libm --no-default-features --target "$target" if [ "${MAY_SKIP_LIBM_CI:-}" = "true" ]; then echo "skipping libm PR CI" @@ -171,19 +174,21 @@ case "$target" in *) mflags+=(--features libm-test/build-musl) ;; esac - # Configure which targets test against MPFR case "$target" in - # MSVC cannot link MPFR + # Excluded in gmp-mpfr-sys with "Windows MSVC target is not supported (linking + # would fail)". MPFR isn't really built for non-gcc-like compilers anyway. *windows-msvc*) ;; # FIXME: MinGW should be able to build MPFR, but setup in CI is nontrivial. *windows-gnu*) ;; - # Targets that aren't cross compiled in CI work fine + # Ensure enabled on targets that we expect to be native in CI aarch64*apple*) mflags+=(--features libm-test/build-mpfr) ;; aarch64*linux*) mflags+=(--features libm-test/build-mpfr) ;; i586*) mflags+=(--features libm-test/build-mpfr --features gmp-mpfr-sys/force-cross) ;; i686*) mflags+=(--features libm-test/build-mpfr) ;; x86_64*) mflags+=(--features libm-test/build-mpfr) ;; + # Fallback to enabled on native targets, disabled otherwise + *) [ "$target" = "$host_tuple" ] && mflags+=(--features libm-test/build-mpfr) ;; esac # FIXME: `STATUS_DLL_NOT_FOUND` testing macros on CI. diff --git a/library/compiler-builtins/compiler-builtins/src/aarch64_outline_atomics.rs b/library/compiler-builtins/compiler-builtins/src/aarch64_outline_atomics.rs index 367768ecb60..f51d0763175 100644 --- a/library/compiler-builtins/compiler-builtins/src/aarch64_outline_atomics.rs +++ b/library/compiler-builtins/compiler-builtins/src/aarch64_outline_atomics.rs @@ -148,77 +148,6 @@ macro_rules! stxp { }; } -// The AArch64 assembly syntax for relocation specifiers -// when accessing symbols changes depending on the target executable format. -// In ELF (used in Linux), we have a prefix notation surrounded by colons (:specifier:sym), -// while in Mach-O object files (used in MacOS), a postfix notation is used (sym@specifier). - -/// AArch64 ELF position-independent addressing: -/// -/// adrp xN, symbol -/// add xN, xN, :lo12:symbol -/// -/// The :lo12: modifier selects the low 12 bits of the symbol address -/// and emits an ELF relocation such as R_AARCH64_ADD_ABS_LO12_NC. -/// -/// Defined by the AArch64 ELF psABI. -/// See: . -#[cfg(not(target_vendor = "apple"))] -macro_rules! sym { - ($sym:literal) => { - $sym - }; -} - -#[cfg(not(target_vendor = "apple"))] -macro_rules! sym_off { - ($sym:literal) => { - concat!(":lo12:", $sym) - }; -} - -/// Mach-O ARM64 relocation types: -/// ARM64_RELOC_PAGE21 -/// ARM64_RELOC_PAGEOFF12 -/// -/// These relocations implement the @PAGE / @PAGEOFF split used by -/// adrp + add sequences on Apple platforms. -/// -/// adrp xN, symbol@PAGE -> ARM64_RELOC_PAGE21 -/// add xN, xN, symbol@PAGEOFF -> ARM64_RELOC_PAGEOFF12 -/// -/// Relocation types defined by Apple in XNU: . -/// See: . -#[cfg(target_vendor = "apple")] -macro_rules! sym { - ($sym:literal) => { - concat!($sym, "@PAGE") - }; -} - -#[cfg(target_vendor = "apple")] -macro_rules! sym_off { - ($sym:literal) => { - concat!($sym, "@PAGEOFF") - }; -} - -// If supported, perform the requested LSE op and return, or fallthrough. -macro_rules! try_lse_op { - ($op: literal, $ordering:ident, $bytes:tt, $($reg:literal,)* [ $mem:ident ] ) => { - concat!( - ".arch_extension lse\n", - concat!("adrp x16, ", sym!("{have_lse}"), "\n"), - concat!("ldrb w16, [x16, ", sym_off!("{have_lse}"), "]\n"), - "cbz w16, 8f\n", - // LSE_OP s(reg),* [$mem] - concat!(lse!($op, $ordering, $bytes), $( " ", reg!($bytes, $reg), ", " ,)* "[", stringify!($mem), "]\n",), - "ret - 8:" - ) - }; -} - // Translate memory ordering to the LSE suffix #[rustfmt::skip] macro_rules! lse_mem_sfx { @@ -238,34 +167,72 @@ macro_rules! lse { }; } +// Ensure the register holding the pointer is zero-extended on ilp32 targets +// before dereferencing it. This may not work, see llvm/llvm-project#225834. +// +// This write requires the pointer argument to be made `inout` in the asm! +// sections. If the ilp32 targets are removed, this macro should be removed +// and the pointer argument changed to `in`. +#[cfg(target_pointer_width = "32")] +macro_rules! sanitize_ptr { + ($num:literal) => { + concat!("mov w", $num, ", w", $num) + }; +} + +#[cfg(not(target_pointer_width = "32"))] +macro_rules! sanitize_ptr { + ($num:literal) => { + "" + }; +} + /// See . macro_rules! compare_and_swap { ($ordering:ident, $bytes:tt, $name:ident) => { intrinsics! { #[maybe_use_optimized_c_shim] - #[unsafe(naked)] pub unsafe extern "C" fn $name ( expected: int_ty!($bytes), desired: int_ty!($bytes), ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { - // We can't use `AtomicI8::compare_and_swap`; we *are* compare_and_swap. - core::arch::naked_asm! { - // CAS s(0), s(1), [x2]; if LSE supported. - try_lse_op!("cas", $ordering, $bytes, 0, 1, [x2]), - // UXT s(tmp0), s(0) - concat!(uxt!($bytes), " ", reg!($bytes, 16), ", ", reg!($bytes, 0)), - "0:", - // LDXR s(0), [x2] - concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x2]"), - // cmp s(0), s(tmp0) - concat!("cmp ", reg!($bytes, 0), ", ", reg!($bytes, 16)), - "bne 1f", - // STXR w(tmp1), s(1), [x2] - concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 1), ", [x2]"), - "cbnz w17, 0b", - "1:", - "ret", - have_lse = sym crate::aarch64_outline_atomics::HAVE_LSE_ATOMICS, + let mut expected = expected; + unsafe { + if HAVE_LSE_ATOMICS.load(Ordering::Relaxed) != 0 { + core::arch::asm!( + ".arch_extension lse", + sanitize_ptr!(2), + // CAS s(0), s(1), [x2]; if LSE supported. + concat!(lse!("cas", $ordering, $bytes), " ", reg!($bytes, 0), ", ", reg!($bytes, 1),", [x2]"), + inlateout("x0") expected, + in("x1") desired, + inout("x2") ptr => _, + options(nostack, preserves_flags), + ); + } else { + core::arch::asm!( + sanitize_ptr!(2), + // UXT s(tmp0), s(0) + concat!(uxt!($bytes), " ", reg!($bytes, 16), ", ", reg!($bytes, 0)), + "1:", + // LDXR s(0), [x2] + concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x2]"), + // cmp s(0), s(tmp0) + concat!("cmp ", reg!($bytes, 0), ", ", reg!($bytes, 16)), + "bne 2f", + // STXR w(tmp1), s(1), [x2] + concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 1), ", [x2]"), + "cbnz w17, 1b", + "2:", + inlateout("x0") expected, + in("x1") desired, + inout("x2") ptr => _, + out("x16") _, + out("w17") _, + options(nostack), + ); + } } + expected } } }; @@ -276,28 +243,56 @@ macro_rules! compare_and_swap_u128 { ($ordering:ident, $name:ident) => { intrinsics! { #[maybe_use_optimized_c_shim] - #[unsafe(naked)] pub unsafe extern "C" fn $name ( expected: u128, desired: u128, ptr: *mut u128 ) -> u128 { - core::arch::naked_asm! { - // CASP x0, x1, x2, x3, [x4]; if LSE supported. - try_lse_op!("cas", $ordering, 16, 0, 1, 2, 3, [x4]), - "mov x16, x0", - "mov x17, x1", - "0:", - // LDXP x0, x1, [x4] - concat!(ldxp!($ordering), " x0, x1, [x4]"), - "cmp x0, x16", - "ccmp x1, x17, #0, eq", - "bne 1f", - // STXP w(tmp2), x2, x3, [x4] - concat!(stxp!($ordering), " w15, x2, x3, [x4]"), - "cbnz w15, 0b", - "1:", - "ret", - have_lse = sym crate::aarch64_outline_atomics::HAVE_LSE_ATOMICS, + let mut expected_lo = expected as u64; + let mut expected_hi = (expected >> 64) as u64; + let desired_lo = desired as u64; + let desired_hi = (desired >> 64) as u64; + + unsafe { + if HAVE_LSE_ATOMICS.load(Ordering::Relaxed) != 0 { + core::arch::asm!( + ".arch_extension lse", + sanitize_ptr!(4), + // CASP x0, x1, x2, x3, [x4]; if LSE supported. + concat!(lse!("cas", $ordering, 16), " x0, x1, x2, x3, [x4]"), + inlateout("x0") expected_lo, + inlateout("x1") expected_hi, + in("x2") desired_lo, + in("x3") desired_hi, + inout("x4") ptr => _, + options(nostack, preserves_flags), + ); + } else { + core::arch::asm!( + sanitize_ptr!(4), + "mov x16, x0", + "mov x17, x1", + "1:", + // LDXP x0, x1, [x4] + concat!(ldxp!($ordering), " x0, x1, [x4]"), + "cmp x0, x16", + "ccmp x1, x17, #0x0, eq", + "b.ne 2f", + // STXP w(tmp2), x2, x3, [x4] + concat!(stxp!($ordering), " w15, x2, x3, [x4]"), + "cbnz w15, 1b", + "2:", + inlateout("x0") expected_lo, + inlateout("x1") expected_hi, + in("x2") desired_lo, + in("x3") desired_hi, + inout("x4") ptr => _, + out("w15") _, + out("x16") _, + out("x17") _, + options(nostack), + ); + } } + return ((expected_hi as u128) << 64) | expected_lo as u128; } } }; @@ -308,24 +303,40 @@ macro_rules! swap { ($ordering:ident, $bytes:tt, $name:ident) => { intrinsics! { #[maybe_use_optimized_c_shim] - #[unsafe(naked)] pub unsafe extern "C" fn $name ( left: int_ty!($bytes), right_ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { - core::arch::naked_asm! { - // SWP s(0), s(0), [x1]; if LSE supported. - try_lse_op!("swp", $ordering, $bytes, 0, 0, [x1]), - // mov s(tmp0), s(0) - concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), - "0:", - // LDXR s(0), [x1] - concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x1]"), - // STXR w(tmp1), s(tmp0), [x1] - concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 16), ", [x1]"), - "cbnz w17, 0b", - "ret", - have_lse = sym crate::aarch64_outline_atomics::HAVE_LSE_ATOMICS, + let mut left = left; + unsafe { + if HAVE_LSE_ATOMICS.load(Ordering::Relaxed) != 0 { + core::arch::asm! { + ".arch_extension lse", + sanitize_ptr!(1), + // SWP s(0), s(0), [x1]; if LSE supported. + concat!(lse!("swp", $ordering, $bytes), " ", reg!($bytes, 0), ", ", reg!($bytes, 0), ", [x1]"), + inlateout("x0") left, + inout("x1") right_ptr => _, + options(nostack, preserves_flags), + }; + } else { + core::arch::asm! { + sanitize_ptr!(1), + concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), + "1:", + // LDXR s(0), [x1] + concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x1]"), + // STXR w(tmp1), s(tmp0), [x1] + concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 16), ", [x1]"), + "cbnz w17, 1b", + inlateout("x0") left, + inout("x1") right_ptr => _, + out("x16") _, + out("w17") _, + options(nostack, preserves_flags), + }; + } } + left } } }; @@ -336,26 +347,44 @@ macro_rules! fetch_op { ($ordering:ident, $bytes:tt, $name:ident, $op:literal, $lse_op:literal) => { intrinsics! { #[maybe_use_optimized_c_shim] - #[unsafe(naked)] pub unsafe extern "C" fn $name ( val: int_ty!($bytes), ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { - core::arch::naked_asm! { - // LSEOP s(0), s(0), [x1]; if LSE supported. - try_lse_op!($lse_op, $ordering, $bytes, 0, 0, [x1]), - // mov s(tmp0), s(0) - concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), - "0:", - // LDXR s(0), [x1] - concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x1]"), - // OP s(tmp1), s(0), s(tmp0) - concat!($op, " ", reg!($bytes, 17), ", ", reg!($bytes, 0), ", ", reg!($bytes, 16)), - // STXR w(tmp2), s(tmp1), [x1] - concat!(stxr!($ordering, $bytes), " w15, ", reg!($bytes, 17), ", [x1]"), - "cbnz w15, 0b", - "ret", - have_lse = sym crate::aarch64_outline_atomics::HAVE_LSE_ATOMICS, + let mut val = val; + unsafe { + if HAVE_LSE_ATOMICS.load(Ordering::Relaxed) != 0 { + core::arch::asm! { + ".arch_extension lse", + sanitize_ptr!(1), + // LSEOP s(0), s(0), [x1]; if LSE supported. + concat!(lse!($lse_op, $ordering, $bytes), " ", reg!($bytes, 0), ", ", reg!($bytes, 0),", [x1]"), + inlateout("x0") val, + inout("x1") ptr => _, + options(nostack, preserves_flags), + }; + } else { + core::arch::asm! { + sanitize_ptr!(1), + // mov s(tmp0), s(0) + concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), + "1:", + // LDXR s(0), [x1] + concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x1]"), + // OP s(tmp1), s(0), s(tmp0) + concat!($op, " ", reg!($bytes, 17), ", ", reg!($bytes, 0), ", ", reg!($bytes, 16)), + // STXR w(tmp2), s(tmp1), [x1] + concat!(stxr!($ordering, $bytes), " w15, ", reg!($bytes, 17), ", [x1]"), + "cbnz w15, 1b", + inlateout("x0") val, + inout("x1") ptr => _, + out("w15") _, + out("x16") _, + out("x17") _, + options(nostack, preserves_flags), + } + } } + val } } } diff --git a/library/compiler-builtins/compiler-builtins/src/float/conv.rs b/library/compiler-builtins/compiler-builtins/src/float/conv.rs index 13a0ed4fcd3..eb6a10bfcd2 100644 --- a/library/compiler-builtins/compiler-builtins/src/float/conv.rs +++ b/library/compiler-builtins/compiler-builtins/src/float/conv.rs @@ -208,6 +208,8 @@ mod int_to_float { // Conversions from unsigned integers to floats. intrinsics! { + /* u32 -> fN */ + #[arm_aeabi_alias = __aeabi_ui2f] pub extern "C" fn __floatunsisf(i: u32) -> f32 { f32::from_bits(int_to_float::u32_to_f32_bits(i)) @@ -218,6 +220,14 @@ intrinsics! { f64::from_bits(int_to_float::u32_to_f64_bits(i)) } + #[ppc_name = __floatunsikf] + #[cfg(f128_enabled)] + pub extern "C" fn __floatunsitf(i: u32) -> f128 { + f128::from_bits(int_to_float::u32_to_f128_bits(i)) + } + + /* u64 -> fN */ + #[arm_aeabi_alias = __aeabi_ul2f] pub extern "C" fn __floatundisf(i: u64) -> f32 { f32::from_bits(int_to_float::u64_to_f32_bits(i)) @@ -228,6 +238,14 @@ intrinsics! { f64::from_bits(int_to_float::u64_to_f64_bits(i)) } + #[ppc_name = __floatundikf] + #[cfg(f128_enabled)] + pub extern "C" fn __floatunditf(i: u64) -> f128 { + f128::from_bits(int_to_float::u64_to_f128_bits(i)) + } + + /* u128 -> fN */ + #[cfg(not(all(target_os = "uefi", target_arch = "x86_64")))] pub extern "C" fn __floatuntisf(i: u128) -> f32 { f32::from_bits(int_to_float::u128_to_f32_bits(i)) @@ -248,18 +266,6 @@ intrinsics! { f64::from_bits(int_to_float::u128_to_f64_bits((u128::from(hi) << 64) | u128::from(lo))) } - #[ppc_name = __floatunsikf] - #[cfg(f128_enabled)] - pub extern "C" fn __floatunsitf(i: u32) -> f128 { - f128::from_bits(int_to_float::u32_to_f128_bits(i)) - } - - #[ppc_name = __floatundikf] - #[cfg(f128_enabled)] - pub extern "C" fn __floatunditf(i: u64) -> f128 { - f128::from_bits(int_to_float::u64_to_f128_bits(i)) - } - #[ppc_name = __floatuntikf] #[cfg(f128_enabled)] pub extern "C" fn __floatuntitf(i: u128) -> f128 { @@ -269,6 +275,8 @@ intrinsics! { // Conversions from signed integers to floats. intrinsics! { + /* i32 -> fN */ + #[arm_aeabi_alias = __aeabi_i2f] pub extern "C" fn __floatsisf(i: i32) -> f32 { int_to_float::signed(i, int_to_float::u32_to_f32_bits) @@ -279,6 +287,14 @@ intrinsics! { int_to_float::signed(i, int_to_float::u32_to_f64_bits) } + #[ppc_name = __floatsikf] + #[cfg(f128_enabled)] + pub extern "C" fn __floatsitf(i: i32) -> f128 { + int_to_float::signed(i, int_to_float::u32_to_f128_bits) + } + + /* i64 -> fN */ + #[arm_aeabi_alias = __aeabi_l2f] pub extern "C" fn __floatdisf(i: i64) -> f32 { int_to_float::signed(i, int_to_float::u64_to_f32_bits) @@ -289,6 +305,14 @@ intrinsics! { int_to_float::signed(i, int_to_float::u64_to_f64_bits) } + #[ppc_name = __floatdikf] + #[cfg(f128_enabled)] + pub extern "C" fn __floatditf(i: i64) -> f128 { + int_to_float::signed(i, int_to_float::u64_to_f128_bits) + } + + /* i128 -> fN */ + #[cfg(not(all(target_os = "uefi", target_arch = "x86_64")))] pub extern "C" fn __floattisf(i: i128) -> f32 { int_to_float::signed(i, int_to_float::u128_to_f32_bits) @@ -309,18 +333,6 @@ intrinsics! { int_to_float::signed((i128::from(hi) << 64) | i128::from(lo), int_to_float::u128_to_f64_bits) } - #[ppc_name = __floatsikf] - #[cfg(f128_enabled)] - pub extern "C" fn __floatsitf(i: i32) -> f128 { - int_to_float::signed(i, int_to_float::u32_to_f128_bits) - } - - #[ppc_name = __floatdikf] - #[cfg(f128_enabled)] - pub extern "C" fn __floatditf(i: i64) -> f128 { - int_to_float::signed(i, int_to_float::u64_to_f128_bits) - } - #[ppc_name = __floattikf] #[cfg(f128_enabled)] pub extern "C" fn __floattitf(i: i128) -> f128 { @@ -411,6 +423,8 @@ where // Conversions from floats to unsigned integers. intrinsics! { + /* f32 -> uN */ + #[arm_aeabi_alias = __aeabi_f2uiz] pub extern "C" fn __fixunssfsi(f: f32) -> u32 { float_to_unsigned_int(f) @@ -425,6 +439,8 @@ intrinsics! { float_to_unsigned_int(f) } + /* f64 -> uN */ + #[arm_aeabi_alias = __aeabi_d2uiz] pub extern "C" fn __fixunsdfsi(f: f64) -> u32 { float_to_unsigned_int(f) @@ -439,6 +455,8 @@ intrinsics! { float_to_unsigned_int(f) } + /* f128 -> uN */ + #[ppc_name = __fixunskfsi] #[cfg(f128_enabled)] pub extern "C" fn __fixunstfsi(f: f128) -> u32 { @@ -460,6 +478,8 @@ intrinsics! { // Conversions from floats to signed integers. intrinsics! { + /* f32 -> iN */ + #[arm_aeabi_alias = __aeabi_f2iz] pub extern "C" fn __fixsfsi(f: f32) -> i32 { float_to_signed_int(f) @@ -474,6 +494,8 @@ intrinsics! { float_to_signed_int(f) } + /* f64 -> iN */ + #[arm_aeabi_alias = __aeabi_d2iz] pub extern "C" fn __fixdfsi(f: f64) -> i32 { float_to_signed_int(f) @@ -488,6 +510,8 @@ intrinsics! { float_to_signed_int(f) } + /* f128 -> iN */ + #[ppc_name = __fixkfsi] #[cfg(f128_enabled)] pub extern "C" fn __fixtfsi(f: f128) -> i32 { diff --git a/library/compiler-builtins/crates/api-list-common/src/lib.rs b/library/compiler-builtins/crates/api-list-common/src/lib.rs index f6e5516a76f..d560e040582 100644 --- a/library/compiler-builtins/crates/api-list-common/src/lib.rs +++ b/library/compiler-builtins/crates/api-list-common/src/lib.rs @@ -898,6 +898,7 @@ const ALL_OPERATIONS_NESTED: &[NestedOp] = &[ "erff", "exp10f", "exp2f", + "exp2m1f", "expf", "expm1f", "fabsf", @@ -947,6 +948,7 @@ const ALL_OPERATIONS_NESTED: &[NestedOp] = &[ "exp", "exp10", "exp2", + "exp2m1", "expm1", "fabs", "floor", diff --git a/library/compiler-builtins/crates/sysbench/Cargo.toml b/library/compiler-builtins/crates/sysbench/Cargo.toml new file mode 100644 index 00000000000..acda233ba67 --- /dev/null +++ b/library/compiler-builtins/crates/sysbench/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "sysbench" +version = "0.1.0" +edition = "2024" +publish = false + +[dependencies] diff --git a/library/compiler-builtins/crates/sysbench/src/main.rs b/library/compiler-builtins/crates/sysbench/src/main.rs new file mode 100644 index 00000000000..5933f2ccd74 --- /dev/null +++ b/library/compiler-builtins/crates/sysbench/src/main.rs @@ -0,0 +1,121 @@ +//! Simple check of integer and float ops, for giving us an idea of how many test cases we can +//! expect to execute in a reasonable amount of time. + +#![allow(clippy::type_complexity)] + +use std::hint::black_box as bb; +use std::io::Write; +use std::time::{Duration, Instant}; +use std::{io, thread}; + +const ESTIMATE_ITERS: u32 = 65535; +const ITER_MAX: u32 = u32::MAX; + +fn main() { + let int_mul = run_bench("integer multiplication", |i| { + let i = i128::from(i); + let i = i | (i << 32) | (i << 64) | (i << 96); + bb(i).overflowing_mul(bb(i)) + }); + let float_mul = run_bench("float multiplication", |i| { + let x = f32::from_bits(i); + bb(x) * bb(x) + }); + let float_fma = run_bench("float fma", |i| { + let x = f32::from_bits(i); + bb(x).mul_add(bb(x), bb(x)) + }); + + let oneshot = bench_oneshot(); + + let threads = thread::available_parallelism().unwrap().get(); + println!("Available parallelism: {threads}"); + + // Super simple metric to give us a rough idea of how machines compare. Int math gets a larger + // scale since u128 requires more instructions. + let int_score = 500.0 / int_mul; + let float_score = 200.0 / float_mul; + let fma_score = 200.0 / float_fma; + let oneshot_score = 50.0 / oneshot.as_secs_f64(); + let score = int_score + float_score + fma_score + oneshot_score; + let thread_scale = 1.0 + 0.9 * (threads as f64 - 1.0); + + println!("Single-core performance score: {score:.0}"); + println!("Multi-core performance score: {:.0}", score * thread_scale); +} + +/// Benchmark `f` by running it repeatedly after a warmup. Outputs are black boxed. +fn run_bench(name: &str, mut f: impl FnMut(u32) -> T) -> f64 { + print!("Starting {name} bench... "); + io::stdout().flush().unwrap(); + + // Warmup + for i in 0..1000 { + bb(f(bb(i))); + } + + // Calculate how many to run + let start = Instant::now(); + for i in 0..ESTIMATE_ITERS { + bb(f(bb(i))); + } + let elapsed = start.elapsed(); + + let ns_per_iter = elapsed.as_secs_f64() * 1e9 / ESTIMATE_ITERS as f64; + let desired_iters = 1e9 / ns_per_iter; + let step = ((ITER_MAX as u64 + 1) / desired_iters as u64) as usize; + let iter = (0..=ITER_MAX).step_by(step); + let count = iter_count(iter.clone()); + + // Run the benchmark + let start = Instant::now(); + for i in iter { + bb(f(bb(i))); + } + let elapsed = start.elapsed(); + + let ns_per_iter = elapsed.as_secs_f64() * 1e9 / count as f64; + println!("{count} iterations completed in {elapsed:.3?} ({ns_per_iter:.3} ns/iter)"); + ns_per_iter +} + +/// Qemu has a good JIT, so run something new once to try to catch it. +fn bench_oneshot() -> Duration { + fn fib_int(n: u32) -> u32 { + if n == 0 { + 0 + } else if n == 1 { + 1 + } else { + fib_int(n - 1) + fib_int(n - 2) + } + } + + fn fib_float(n: f32) -> f32 { + if n == 0.0 { + 0.0 + } else if n == 1.0 { + 1.0 + } else { + fib_float(n - 1.0) + fib_float(n - 2.0) + } + } + + print!("Starting oneshot bench... "); + io::stdout().flush().unwrap(); + let start = Instant::now(); + bb(fib_int(bb(38))); + bb(fib_float(bb(38.0))); + let elapsed = start.elapsed(); + println!("completed in {elapsed:.3?}"); + elapsed +} + +/// `iter.count()` but allow results greater than `usize::MAX` on 32-bit. +fn iter_count(iter: impl Iterator) -> u64 { + let mut ret = 0u64; + for _ in iter { + ret = ret.strict_add(1); + } + ret +} diff --git a/library/compiler-builtins/crates/util/src/main.rs b/library/compiler-builtins/crates/util/src/main.rs index aa79354a571..4cc6065bab6 100644 --- a/library/compiler-builtins/crates/util/src/main.rs +++ b/library/compiler-builtins/crates/util/src/main.rs @@ -102,7 +102,9 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) { extra: (basis, op, inputs), fn_extra: match MACRO_FN_NAME { // Not provided by musl - fmaximum + exp2m1 + | exp2m1f + | fmaximum | fmaximum_num | fmaximum_numf | fmaximumf diff --git a/library/compiler-builtins/etc/function-definitions.json b/library/compiler-builtins/etc/function-definitions.json index 3e2b14d31c5..fecb646a0f5 100644 --- a/library/compiler-builtins/etc/function-definitions.json +++ b/library/compiler-builtins/etc/function-definitions.json @@ -233,6 +233,18 @@ ], "type": "f32" }, + "exp2m1": { + "sources": [ + "libm/src/math/exp2m1.rs" + ], + "type": "f64" + }, + "exp2m1f": { + "sources": [ + "libm/src/math/exp2m1f.rs" + ], + "type": "f32" + }, "expf": { "sources": [ "libm/src/math/expf.rs" diff --git a/library/compiler-builtins/etc/function-list.txt b/library/compiler-builtins/etc/function-list.txt index 14f75a2c578..b65540fb18e 100644 --- a/library/compiler-builtins/etc/function-list.txt +++ b/library/compiler-builtins/etc/function-list.txt @@ -36,6 +36,8 @@ exp10 exp10f exp2 exp2f +exp2m1 +exp2m1f expf expm1 expm1f diff --git a/library/compiler-builtins/libm-test/benches/icount.rs b/library/compiler-builtins/libm-test/benches/icount.rs index 8e067f2da5e..e9eb3805d1d 100644 --- a/library/compiler-builtins/libm-test/benches/icount.rs +++ b/library/compiler-builtins/libm-test/benches/icount.rs @@ -108,6 +108,8 @@ library_benchmark_group!( icount_bench_exp10f, icount_bench_exp2, icount_bench_exp2f, + icount_bench_exp2m1, + icount_bench_exp2m1f, icount_bench_expf, icount_bench_expm1, icount_bench_expm1f, diff --git a/library/compiler-builtins/libm-test/benches/random.rs b/library/compiler-builtins/libm-test/benches/random.rs index ae4c2f51bde..d6ee71f9b38 100644 --- a/library/compiler-builtins/libm-test/benches/random.rs +++ b/library/compiler-builtins/libm-test/benches/random.rs @@ -126,7 +126,9 @@ libm_macros::for_each_function! { exp10 | exp10f | exp2 | exp2f => (true, Some(musl_math_sys::MACRO_FN_NAME)), // Musl does not provide `f16` and `f128` functions, as well as a handful of others - fmaximum + exp2m1 + | exp2m1f + | fmaximum | fmaximum_num | fmaximum_numf | fmaximumf diff --git a/library/compiler-builtins/libm-test/src/builtins_wrapper.rs b/library/compiler-builtins/libm-test/src/builtins_wrapper.rs index ce517148b9c..80716619f9c 100644 --- a/library/compiler-builtins/libm-test/src/builtins_wrapper.rs +++ b/library/compiler-builtins/libm-test/src/builtins_wrapper.rs @@ -102,80 +102,128 @@ cb_op!(@binop f16, add, __addhf3, addf16); cb_op!(@binop f32, add, __addsf3, addf32); cb_op!(@binop f64, add, __adddf3, addf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@binop f128, add, __addtf3, addf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@binop f128, add, __addkf3, addf128); #[cfg(f16_enabled)] cb_op!(@binop f16, sub, __subhf3, subf16); cb_op!(@binop f32, sub, __subsf3, subf32); cb_op!(@binop f64, sub, __subdf3, subf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@binop f128, sub, __subtf3, subf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@binop f128, sub, __subkf3, subf128); #[cfg(f16_enabled)] cb_op!(@binop f16, mul, __mulhf3, mulf16); cb_op!(@binop f32, mul, __mulsf3, mulf32); cb_op!(@binop f64, mul, __muldf3, mulf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@binop f128, mul, __multf3, mulf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@binop f128, mul, __mulkf3, mulf128); cb_op!(@binop f32, div, __divsf3, divf32); cb_op!(@binop f64, div, __divdf3, divf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@binop f128, div, __divtf3, divf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@binop f128, div, __divkf3, divf128); cb_op!(pow, __powisf2, powif32, (a: f32, b: i32) -> f32); cb_op!(pow, __powidf2, powif64, (a: f64, b: i32) -> f64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(pow, __powitf2, powif128, (a: f128, b: i32) -> f128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(pow, __powikf2, powif128, (a: f128, b: i32) -> f128); #[cfg(f16_enabled)] cb_op!(@cmp_eq f16, cmp, __eqhf2, eqf16); cb_op!(@cmp_eq f32, cmp, __eqsf2, eqf32); cb_op!(@cmp_eq f64, cmp, __eqdf2, eqf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_eq f128, cmp, __eqtf2, eqf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_eq f128, cmp, __eqkf2, eqf128); #[cfg(f16_enabled)] cb_op!(@cmp_gt f16, cmp, __gthf2, gtf16); cb_op!(@cmp_gt f32, cmp, __gtsf2, gtf32); cb_op!(@cmp_gt f64, cmp, __gtdf2, gtf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_gt f128, cmp, __gttf2, gtf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_gt f128, cmp, __gtkf2, gtf128); #[cfg(f16_enabled)] cb_op!(@cmp_ge f16, cmp, __gehf2, gef16); cb_op!(@cmp_ge f32, cmp, __gesf2, gef32); cb_op!(@cmp_ge f64, cmp, __gedf2, gef64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_ge f128, cmp, __getf2, gef128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_ge f128, cmp, __gekf2, gef128); #[cfg(f16_enabled)] cb_op!(@cmp_lt f16, cmp, __lthf2, ltf16); cb_op!(@cmp_lt f32, cmp, __ltsf2, ltf32); cb_op!(@cmp_lt f64, cmp, __ltdf2, ltf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_lt f128, cmp, __lttf2, ltf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_lt f128, cmp, __ltkf2, ltf128); #[cfg(f16_enabled)] cb_op!(@cmp_le f16, cmp, __lehf2, lef16); cb_op!(@cmp_le f32, cmp, __lesf2, lef32); cb_op!(@cmp_le f64, cmp, __ledf2, lef64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_le f128, cmp, __letf2, lef128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_le f128, cmp, __lekf2, lef128); #[cfg(f16_enabled)] cb_op!(@cmp_ne f16, cmp, __nehf2, nef16); cb_op!(@cmp_ne f32, cmp, __nesf2, nef32); cb_op!(@cmp_ne f64, cmp, __nedf2, nef64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_ne f128, cmp, __netf2, nef128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_ne f128, cmp, __nekf2, nef128); #[cfg(f16_enabled)] cb_op!(@cmp_unord f16, cmp, __unordhf2, unordf16); cb_op!(@cmp_unord f32, cmp, __unordsf2, unordf32); cb_op!(@cmp_unord f64, cmp, __unorddf2, unordf64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(@cmp_unord f128, cmp, __unordtf2, unordf128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(@cmp_unord f128, cmp, __unordkf2, unordf128); #[cfg(f16_enabled)] cb_op!(extend, __extendhfsf2, extend_f16_f32, (a: f16) -> f32); @@ -183,12 +231,25 @@ cb_op!(extend, __extendhfsf2, extend_f16_f32, (a: f16) -> f32); cb_op!(extend, __extendhfdf2, extend_f16_f64, (a: f16) -> f64); #[cfg(f16_enabled)] #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(extend, __extendhftf2, extend_f16_f128, (a: f16) -> f128); +#[cfg(f16_enabled)] +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(extend, __extendhfkf2, extend_f16_f128, (a: f16) -> f128); cb_op!(extend, __extendsfdf2, extend_f32_f64, (a: f32) -> f64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(extend, __extendsftf2, extend_f32_f128, (a: f32) -> f128); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(extend, __extendsfkf2, extend_f32_f128, (a: f32) -> f128); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(extend, __extenddftf2, extend_f64_f128, (a: f64) -> f128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(extend, __extenddfkf2, extend_f64_f128, (a: f64) -> f128); // Note that these are renamed from trunc to narrow to avoid collision with libm `trunc`. #[cfg(f16_enabled)] @@ -198,11 +259,23 @@ cb_op!(trunc, __truncdfhf2, narrow_f64_f16, (a: f64) -> f16); cb_op!(trunc, __truncdfsf2, narrow_f64_f32, (a: f64) -> f32); #[cfg(f16_enabled)] #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(trunc, __trunctfhf2, narrow_f128_f16, (a: f128) -> f16); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(trunc, __trunckfhf2, narrow_f128_f16, (a: f128) -> f16); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(trunc, __trunctfsf2, narrow_f128_f32, (a: f128) -> f32); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(trunc, __trunckfsf2, narrow_f128_f32, (a: f128) -> f32); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(trunc, __trunctfdf2, narrow_f128_f64, (a: f128) -> f64); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(trunc, __trunckfdf2, narrow_f128_f64, (a: f128) -> f64); cb_op!(conv, __fixsfsi, ftoi_f32_i32, (a: f32) -> i32); cb_op!(conv, __fixsfdi, ftoi_f32_i64, (a: f32) -> i64); @@ -211,11 +284,23 @@ cb_op!(conv, __fixdfsi, ftoi_f64_i32, (a: f64) -> i32); cb_op!(conv, __fixdfdi, ftoi_f64_i64, (a: f64) -> i64); cb_op!(conv, __fixdfti, ftoi_f64_i128, (a: f64) -> i128); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __fixtfsi, ftoi_f128_i32, (a: f128) -> i32); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __fixkfsi, ftoi_f128_i32, (a: f128) -> i32); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __fixtfdi, ftoi_f128_i64, (a: f128) -> i64); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __fixkfdi, ftoi_f128_i64, (a: f128) -> i64); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __fixtfti, ftoi_f128_i128, (a: f128) -> i128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __fixkfti, ftoi_f128_i128, (a: f128) -> i128); cb_op!(conv, __fixunssfsi, ftoi_f32_u32, (a: f32) -> u32); cb_op!(conv, __fixunssfdi, ftoi_f32_u64, (a: f32) -> u64); cb_op!(conv, __fixunssfti, ftoi_f32_u128, (a: f32) -> u128); @@ -223,11 +308,23 @@ cb_op!(conv, __fixunsdfsi, ftoi_f64_u32, (a: f64) -> u32); cb_op!(conv, __fixunsdfdi, ftoi_f64_u64, (a: f64) -> u64); cb_op!(conv, __fixunsdfti, ftoi_f64_u128, (a: f64) -> u128); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __fixunstfsi, ftoi_f128_u32, (a: f128) -> u32); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __fixunskfsi, ftoi_f128_u32, (a: f128) -> u32); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __fixunstfdi, ftoi_f128_u64, (a: f128) -> u64); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __fixunskfdi, ftoi_f128_u64, (a: f128) -> u64); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __fixunstfti, ftoi_f128_u128, (a: f128) -> u128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __fixunskfti, ftoi_f128_u128, (a: f128) -> u128); cb_op!(conv, __floatsisf, itof_i32_f32, (a: i32) -> f32); cb_op!(conv, __floatdisf, itof_i64_f32, (a: i64) -> f32); @@ -236,11 +333,23 @@ cb_op!(conv, __floatsidf, itof_i32_f64, (a: i32) -> f64); cb_op!(conv, __floatdidf, itof_i64_f64, (a: i64) -> f64); cb_op!(conv, __floattidf, itof_i128_f64, (a: i128) -> f64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __floatsitf, itof_i32_f128, (a: i32) -> f128); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __floatsikf, itof_i32_f128, (a: i32) -> f128); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __floatditf, itof_i64_f128, (a: i64) -> f128); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __floatdikf, itof_i64_f128, (a: i64) -> f128); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __floattitf, itof_i128_f128, (a: i128) -> f128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __floattikf, itof_i128_f128, (a: i128) -> f128); cb_op!(conv, __floatunsisf, itof_u32_f32, (a: u32) -> f32); cb_op!(conv, __floatundisf, itof_u64_f32, (a: u64) -> f32); cb_op!(conv, __floatuntisf, itof_u128_f32, (a: u128) -> f32); @@ -248,11 +357,23 @@ cb_op!(conv, __floatunsidf, itof_u32_f64, (a: u32) -> f64); cb_op!(conv, __floatundidf, itof_u64_f64, (a: u64) -> f64); cb_op!(conv, __floatuntidf, itof_u128_f64, (a: u128) -> f64); #[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __floatunsitf, itof_u32_f128, (a: u32) -> f128); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __floatunsikf, itof_u32_f128, (a: u32) -> f128); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __floatunditf, itof_u64_f128, (a: u64) -> f128); #[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __floatundikf, itof_u64_f128, (a: u64) -> f128); +#[cfg(f128_enabled)] +#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))] cb_op!(conv, __floatuntitf, itof_u128_f128, (a: u128) -> f128); +#[cfg(f128_enabled)] +#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] +cb_op!(conv, __floatuntikf, itof_u128_f128, (a: u128) -> f128); /* int ops */ diff --git a/library/compiler-builtins/libm-test/src/domain.rs b/library/compiler-builtins/libm-test/src/domain.rs index 0ae105f4a02..f37e50cc08d 100644 --- a/library/compiler-builtins/libm-test/src/domain.rs +++ b/library/compiler-builtins/libm-test/src/domain.rs @@ -283,6 +283,7 @@ pub fn get_domain( BaseName::Expm1 => &EitherPrim::UNBOUNDED1[..], BaseName::Exp10 => &EitherPrim::UNBOUNDED1[..], BaseName::Exp2 => &EitherPrim::UNBOUNDED1[..], + BaseName::Exp2m1 => &EitherPrim::UNBOUNDED1[..], BaseName::Frexp => &EitherPrim::UNBOUNDED1[..], BaseName::Fabs => &EitherPrim::UNBOUNDED1[..], BaseName::Fdim => &EitherPrim::UNBOUNDED2[..], diff --git a/library/compiler-builtins/libm-test/src/f8_impl.rs b/library/compiler-builtins/libm-test/src/f8_impl.rs index 7a24fcbfa3c..ad968d7ddd4 100644 --- a/library/compiler-builtins/libm-test/src/f8_impl.rs +++ b/library/compiler-builtins/libm-test/src/f8_impl.rs @@ -97,6 +97,14 @@ impl Float for f8 { unimplemented!() } + fn floor(self) -> Self { + unimplemented!() + } + + fn roundeven(self) -> Self { + unimplemented!() + } + fn normalize(_significand: Self::Int) -> (i32, Self::Int) { unimplemented!() } diff --git a/library/compiler-builtins/libm-test/src/generate/case_list.rs b/library/compiler-builtins/libm-test/src/generate/case_list.rs index 2275787307d..353b49b87f4 100644 --- a/library/compiler-builtins/libm-test/src/generate/case_list.rs +++ b/library/compiler-builtins/libm-test/src/generate/case_list.rs @@ -12,7 +12,7 @@ use libm::hf16; use libm::hf128; use libm::{hf32, hf64}; -use crate::{CheckBasis, CheckCtx, GeneratorKind, MathOp, op}; +use crate::{CheckBasis, CheckCtx, Float, GeneratorKind, MathOp, op}; pub struct TestCase { pub input: Op::RustArgs, @@ -852,6 +852,122 @@ fn exp2f_cases() -> Vec> { cases![] } +fn exp2m1f_cases() -> Vec> { + let mut cases = cases![]; + + // Boundary values from CORE-MATH's binary32/exp2m1/exp2m1f.c. + // Test each value and its neighbors with both signs against MPFR. + for bits in [ + 0x0000_0001, + 0x0080_0000, + 0x2538_aa3b, + 0x331f_dd82, + 0x37d3_2ef6, + 0x3a35_8876, + 0x3bac_1405, + 0x3caa_2fee, + 0x3d67_a4cc, + 0x3df9_5f1f, + 0x41c8_0000, + 0x4300_0000, + ] { + for adjacent in [bits - 1, bits, bits + 1] { + for sign in [0, f32::SIGN_MASK] { + cases.push(TestCase { + input: (f32::from_bits(adjacent | sign),), + output: None, + }); + } + } + } + + // Special rounding cases from the same CORE-MATH file. + // Test each value and its neighbors against MPFR. + for bits in [0xb3d8_5005, 0x3338_428d, 0x388b_ca4f] { + for adjacent in [bits - 1, bits, bits + 1] { + cases.push(TestCase { + input: (f32::from_bits(adjacent),), + output: None, + }); + } + } + + cases +} + +fn exp2m1_cases() -> Vec> { + let mut cases = cases![]; + + // Check their neighbors with MPFR. + for i in 1..=53 { + let x = i as f64; + + for adjacent in [x.to_bits() - 1, x.to_bits() + 1] { + for sign in [0, f64::SIGN_MASK] { + cases.push(TestCase { + input: (f64::from_bits(adjacent | sign),), + output: None, + }); + } + } + } + + // Subnormal, polynomial, saturation, and overflow boundaries, with both signs. + for x in [ + f64::from_bits(1), + f64::MIN_POSITIVE, + hf64!("0x1.71547652b82fdp-1022"), + hf64!("0x1p-104"), + hf64!("0x1.0527dbd87e24dp-51"), + 0.125, + 54.0, + 1024.0, + ] { + let bits = x.to_bits(); + for adjacent in [bits - 1, bits, bits + 1] { + for sign in [0, f64::SIGN_MASK] { + cases.push(TestCase { + input: (f64::from_bits(adjacent | sign),), + output: None, + }); + } + } + } + + // Difficult rounding cases from the CORE-MATH exception tables and their neighbors. + for x in [ + hf64!("-0x1.a16826a8e825dp-56"), + hf64!("-0x1.f1bc3ef3e6f36p-65"), + hf64!("-0x1.1e3a6eaa49c6ep-84"), + hf64!("-0x1.1d65d5fc31246p-98"), + hf64!("0x1.8a5dd21ef35afp-104"), + hf64!("0x1.086cbb0e900cfp-96"), + hf64!("0x1.453f5d718374ap-88"), + hf64!("0x1.02f371449097fp-51"), + hf64!("-0x1.f6ec73d3948c3p-4"), + hf64!("-0x1.3918e8608bd5bp-8"), + hf64!("-0x1.2c506b0368099p-51"), + hf64!("0x1.391609b20beaap-51"), + hf64!("0x1.4753a08baf7fbp-22"), + hf64!("0x1.d4a8ebce833a3p-4"), + hf64!("-0x1.da22611253866p+0"), + hf64!("-0x1.8827da1dbf35ep-2"), + hf64!("0x1.12eecf76d63cdp+1"), + hf64!("0x1.ffef3f31766b7p+5"), + hf64!("0x1.6f33cdaf56d6p+6"), + ] { + let bits = x.to_bits(); + for adjacent in [bits - 1, bits, bits + 1] { + cases.push(TestCase { + input: (f64::from_bits(adjacent),), + output: None, + }); + } + } + + cases +} + fn expf_cases() -> Vec> { cases![ ((hf32!("-0x1.2d245ap-8")), hf32!("0x1.fda718p-1")), diff --git a/library/compiler-builtins/libm-test/src/mpfloat.rs b/library/compiler-builtins/libm-test/src/mpfloat.rs index 69c6590d376..5517e70eb01 100644 --- a/library/compiler-builtins/libm-test/src/mpfloat.rs +++ b/library/compiler-builtins/libm-test/src/mpfloat.rs @@ -355,6 +355,7 @@ libm_macros::for_each_function! { fn_extra: match MACRO_FN_NAME { // Remap function names that are different between mpfr and libm expm1 | expm1f => exp_m1, + exp2m1 | exp2m1f => exp2_m1, fabs | fabsf => abs, fdim | fdimf | fdimf16 | fdimf128 => positive_diff, fmaf16 | fma | fmaf | fmaf128 => mul_add, diff --git a/library/compiler-builtins/libm-test/src/num.rs b/library/compiler-builtins/libm-test/src/num.rs index efa87d63515..4b7e17d48c9 100644 --- a/library/compiler-builtins/libm-test/src/num.rs +++ b/library/compiler-builtins/libm-test/src/num.rs @@ -11,35 +11,11 @@ use crate::{Int, MinInt}; /// Extension to `libm`'s `Float` trait with methods that are useful for tests but not /// needed in `libm` itself. pub trait FloatExt: Float { - /// The minimum subnormal number. - const TINY_BITS: Self::Int = Self::Int::ONE; - /// Retrieve additional constants for this float type. fn consts() -> Consts { Consts::new() } - /// Increment by one ULP, saturating at infinity. - fn next_up(self) -> Self { - let bits = self.to_bits(); - if self.is_nan() || bits == Self::INFINITY.to_bits() { - return self; - } - - let abs = self.abs().to_bits(); - let next_bits = if abs == Self::Int::ZERO { - // Next up from 0 is the smallest subnormal - Self::TINY_BITS - } else if bits == abs { - // Positive: counting up is more positive - bits + Self::Int::ONE - } else { - // Negative: counting down is more positive - bits - Self::Int::ONE - }; - Self::from_bits(next_bits) - } - /// A faster way to effectively call `next_up` `n` times. fn n_up(self, n: Self::Int) -> Self { let bits = self.to_bits(); @@ -70,27 +46,6 @@ pub trait FloatExt: Float { Self::from_bits(next_bits) } - /// Decrement by one ULP, saturating at negative infinity. - fn next_down(self) -> Self { - let bits = self.to_bits(); - if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() { - return self; - } - - let abs = self.abs().to_bits(); - let next_bits = if abs == Self::Int::ZERO { - // Next up from 0 is the smallest negative subnormal - Self::TINY_BITS | Self::SIGN_MASK - } else if bits == abs { - // Positive: counting down is more negative - bits - Self::Int::ONE - } else { - // Negative: counting up is more negative - bits + Self::Int::ONE - }; - Self::from_bits(next_bits) - } - /// A faster way to effectively call `next_down` `n` times. fn n_down(self, n: Self::Int) -> Self { let bits = self.to_bits(); diff --git a/library/compiler-builtins/libm-test/src/precision.rs b/library/compiler-builtins/libm-test/src/precision.rs index 967274caa96..1acbfe8dbed 100644 --- a/library/compiler-builtins/libm-test/src/precision.rs +++ b/library/compiler-builtins/libm-test/src/precision.rs @@ -76,6 +76,7 @@ pub fn default_ulp(ctx: &CheckCtx) -> Option { // Operations that aren't required to be exact, but our implementations are. Bn::Cbrt => 0, Bn::Hypot if ctx.fn_ident == Id::Hypot => 0, + Bn::Exp2m1 => 0, // Bessel functions have large inaccuracies. Bn::J0 | Bn::J1 | Bn::Y0 | Bn::Y1 | Bn::Jn | Bn::Yn => 8_000_000, @@ -148,6 +149,7 @@ pub fn default_ulp(ctx: &CheckCtx) -> Option { Id::Log1p => ulp = 2, Id::Log1pf => ulp = 2, Id::Tan => ulp = 2, + Id::Exp2m1 => ulp = 512, _ => (), } diff --git a/library/compiler-builtins/libm-test/tests/compare_built_musl.rs b/library/compiler-builtins/libm-test/tests/compare_built_musl.rs index 5e9ead0473c..ec09e16cf24 100644 --- a/library/compiler-builtins/libm-test/tests/compare_built_musl.rs +++ b/library/compiler-builtins/libm-test/tests/compare_built_musl.rs @@ -92,6 +92,8 @@ libm_macros::for_each_function! { // Not provided by musl // verify-sorted-start + exp2m1, + exp2m1f, fmaximum, fmaximum_num, fmaximum_numf, diff --git a/library/compiler-builtins/libm-test/tests/u256.rs b/library/compiler-builtins/libm-test/tests/u256.rs index a17e16f8f42..0163bd3bec7 100644 --- a/library/compiler-builtins/libm-test/tests/u256.rs +++ b/library/compiler-builtins/libm-test/tests/u256.rs @@ -322,7 +322,6 @@ fn mp_i256_sub() { let y = random_i256(&mut rng); assign_bigint_i256(&mut bx, x); assign_bigint_i256(&mut by, y); - dbg!(&bx, &by); // Emulate wrapping semantics with panicking ops let actual = if y > i256::ZERO && x < i256::MIN + y { diff --git a/library/compiler-builtins/libm/src/libm_helper.rs b/library/compiler-builtins/libm/src/libm_helper.rs index f2c4daed561..16b5c7e6f67 100644 --- a/library/compiler-builtins/libm/src/libm_helper.rs +++ b/library/compiler-builtins/libm/src/libm_helper.rs @@ -66,6 +66,7 @@ libm_helper! { (fn exp(x: f32) -> (f32); => expf); (fn exp10(x: f32) -> (f32); => exp10f); (fn exp2(x: f32) -> (f32); => exp2f); + (fn exp2m1(x: f32) -> (f32); => exp2m1f); (fn expm1(x: f32) -> (f32); => expm1f); (fn fabs(x: f32) -> (f32); => fabsf); (fn fdim(x: f32, y: f32) -> (f32); => fdimf); @@ -132,6 +133,7 @@ libm_helper! { (fn exp(x: f64) -> (f64); => exp); (fn exp10(x: f64) -> (f64); => exp10); (fn exp2(x: f64) -> (f64); => exp2); + (fn exp2m1(x: f64) -> (f64); => exp2m1); (fn expm1(x: f64) -> (f64); => expm1); (fn fabs(x: f64) -> (f64); => fabs); (fn fdim(x: f64, y: f64) -> (f64); => fdim); diff --git a/library/compiler-builtins/libm/src/math/arch/x86/fma.rs b/library/compiler-builtins/libm/src/math/arch/x86/fma.rs index 43ac187792d..64957612d21 100644 --- a/library/compiler-builtins/libm/src/math/arch/x86/fma.rs +++ b/library/compiler-builtins/libm/src/math/arch/x86/fma.rs @@ -8,6 +8,17 @@ use crate::support::Round; use crate::support::feature_detect::select_once; pub fn fma(x: f64, y: f64, z: f64) -> f64 { + // Skips runtime detection when a feature is enabled at compile time, so the call can be + // inlined. + if cfg!(target_feature = "fma") { + // SAFETY: `fma` is enabled at compile time. + return unsafe { fma_with_fma(x, y, z) }; + } + if cfg!(target_feature = "fma4") { + // SAFETY: `fma4` is enabled at compile time. + return unsafe { fma_with_fma4(x, y, z) }; + } + select_once! { sig: fn(x: f64, y: f64, z: f64) -> f64, init: || { @@ -26,6 +37,17 @@ pub fn fma(x: f64, y: f64, z: f64) -> f64 { } pub fn fmaf(x: f32, y: f32, z: f32) -> f32 { + // Skips runtime detection when a feature is enabled at compile time, so the call can be + // inlined. + if cfg!(target_feature = "fma") { + // SAFETY: `fma` is enabled at compile time. + return unsafe { fmaf_with_fma(x, y, z) }; + } + if cfg!(target_feature = "fma4") { + // SAFETY: `fma4` is enabled at compile time. + return unsafe { fmaf_with_fma4(x, y, z) }; + } + select_once! { sig: fn(x: f32, y: f32, z: f32) -> f32, init: || { diff --git a/library/compiler-builtins/libm/src/math/exp2m1.rs b/library/compiler-builtins/libm/src/math/exp2m1.rs new file mode 100644 index 00000000000..6cf60a2343c --- /dev/null +++ b/library/compiler-builtins/libm/src/math/exp2m1.rs @@ -0,0 +1,1027 @@ +/* SPDX-License-Identifier: MIT */ +/* origin: core-math/src/binary64/exp2m1/exp2m1.c + * Copyright (c) 2022-2025 Paul Zimmermann, Tom Hubrecht and Claude-Pierre Jeannerod + * Ported to Rust in 2026, malezjaa + * Approximate CORE-MATH commit: ac1993d82a2f83eadc045cb587fa8428c9ebaa16 + */ + +use super::ldexp; +use super::support::{CastFrom, CastInto, Float, Int, cold_path}; + +const LN2H: f64 = hf64!("0x1.62e42fefa39efp-1"); +const LN2L: f64 = hf64!("0x1.abc9e3b39803fp-56"); + +/// Base-2 exponential minus one (f64) +/// +/// Calculate `2^x - 1`, that is, 2 raised to the power `x` minus `1`. +#[cfg_attr(assert_no_panic, no_panic::no_panic)] +pub fn exp2m1(x: f64) -> f64 { + cr_exp2m1(x) +} + +fn cr_exp2m1(x: f64) -> f64 { + let ux = x.to_bits(); + let ax = ux & 0x7fffffffffffffff; + + if ux >= 0xc04b000000000000 { + cold_path(); + + // x = -NaN or x <= -54 + if (ux >> 52) == 0xfff { + // -NaN or -Inf + return if ux > 0xfff0000000000000 { x + x } else { -1.0 }; + } + + // for x <= -54, exp2m1(x) rounds to -1 to nearest + return -1.0 + hf64!("0x1p-54"); + } else if ax >= 0x4090000000000000 { + cold_path(); + // x = +NaN or x >= 1024 + if (ux >> 52) == 0x7ff { + // +NaN + return x + x; + } + + // for x >= 1024, exp2m1(x) rounds to +Inf to nearest, + // but for RNDZ/RNDD, we should have no overflow for x=1024 + + return hf64!("0x1.fffffffffffffp+1023") + x * hf64!("0x1.fffffffffffffp+960"); + + // |x| <= 0x1.0527dbd87e24dp-51 + } else if ax <= 0x3cc0527dbd87e24d + /* then the second term of the Taylor expansion of 2^x-1 at x=0 is + smaller in absolute value than 1/2 ulp(first term): + log(2)*x + log(2)^2*x^2/2 + ... */ + { + // we use special code when log(2)*|x| is very small, in which case + // the double-double approximation hi+lo has its lower part lo + // "truncated" + // |x| <= 2^-104 + if ax <= 0x3970000000000000 { + // special case for 0 + if x == 0.0 { + return x; + } + + // scale x by 2^106 + let x = x * hf64!("0x1p106"); + let (hi, lo) = a_mul(LN2H, x); + let lo = LN2L.fma(x, lo); + let h2 = hi + lo; // round to 53-bit precision + // scale back, hoping to avoid double rounding + let h2 = h2 * hf64!("0x1p-106"); + + // now subtract back h2 * 2^106 from hi to get the correction term + // add lo + let hi = (-h2).fma(hf64!("0x1p106"), hi) + lo; + + // add h2 + hi * 2^-106. Warning: when hi=0, 2^-106*h2 might be exact, + // thus no underflow will be raised. We have underflow for + // 0 < x <= 0x1.71547652b82fep-1022 for RNDZ, and for + // 0 < x <= 0x1.71547652b82fdp-1022 for RNDN/RNDU. + let res = hi.fma(hf64!("0x1p-106"), h2); + + if ax <= 0x171547652b82fd || res.abs() < hf64!("0x1p-1022") { + force_eval!(res * res); // underflow + } + + return res; + + // 2^-104 < |x| <= 0x1.0527dbd87e24dp-51 + } else { + // The following exceptional cases have at least 51 identical bits after + // the round bit, thus are hard to correctly round with double-double + // arithmetic. They should be sorted by increasing values of the first + // entry (x). + #[rustfmt::skip] + static EXCEPTIONS_TABLE: [(f64, f64, f64); 56] = [ + (hf64!("-0x1.a16826a8e825dp-56"), hf64!("-0x1.21530a306cc85p-56"), hf64!("-0x1.38ac4a67cep-161")), + (hf64!("-0x1.8c525b64ed08ep-59"), hf64!("-0x1.12b592f889516p-59"), hf64!("-0x1.7b71d1eep-169")), + (hf64!("-0x1.0ede4c1293a9bp-59"), hf64!("-0x1.7780d5e5cf5c5p-60"), hf64!("-0x1.6b820f41efd9p-166")), + (hf64!("-0x1.bacdbd3005cd7p-60"), hf64!("-0x1.32ed98e196cf5p-60"), hf64!("-0x1.9b32a24b8p-166")), + (hf64!("-0x1.0481d96a2dfcap-61"), hf64!("-0x1.6923c31228cd3p-62"), hf64!("-0x1.59432bd8e301p-169")), + (hf64!("-0x1.103fc46963aafp-62"), hf64!("-0x1.796ad95f38488p-63"), hf64!("-0x1.efd4112076edap-170")), + (hf64!("-0x1.f1bc3ef3e6f36p-65"), hf64!("-0x1.5900fbf46981dp-65"), hf64!("-0x1.fffffffffffffp-119")), + (hf64!("-0x1.d8bedc057858cp-65"), hf64!("-0x1.47aea7608c02bp-65"), hf64!("-0x1.ef8a5d5p-172")), + (hf64!("-0x1.986d43391ffdp-65"), hf64!("-0x1.1b19925f9fc06p-65"), hf64!("-0x1.1aada6205370bp-170")), + (hf64!("-0x1.6a480c34c7c5bp-70"), hf64!("-0x1.f63a8ce2364f3p-71"), hf64!("-0x1.11e154c338558p-177")), + (hf64!("-0x1.03eda6663a4b2p-70"), hf64!("-0x1.6856506d8234ap-71"), hf64!("-0x1.f48368854ffd7p-178")), + (hf64!("-0x1.ec44ae4bc644p-74"), hf64!("-0x1.5536e12eb7335p-74"), hf64!("-0x1.fb8acp-181")), + (hf64!("-0x1.af82b29eef2ecp-74"), hf64!("-0x1.2b19ae19e4f3ep-74"), hf64!("-0x1.730d414c3032cp-180")), + (hf64!("-0x1.0e8e362e16fe6p-74"), hf64!("-0x1.7711d03d5c7bdp-75"), hf64!("-0x1.63020ffb5bda3p-181")), + (hf64!("-0x1.5754d81696c42p-76"), hf64!("-0x1.dbf55aa9a7eb9p-77"), hf64!("-0x1.c81798a2dea9ep-184")), + (hf64!("-0x1.e2b5692bd0c53p-80"), hf64!("-0x1.4e968fb1b5f41p-80"), hf64!("0x1.fffffffffffffp-134")), + (hf64!("-0x1.0042796d534fdp-81"), hf64!("-0x1.6340571968b67p-82"), hf64!("-0x1.91fa30638e3b8p-188")), + (hf64!("-0x1.1e3a6eaa49c6ep-84"), hf64!("-0x1.8ccbeeaab37e4p-85"), hf64!("0x1p-138")), + (hf64!("-0x1.19f9b6ddcc3e9p-84"), hf64!("-0x1.86e6a6125ee5fp-85"), hf64!("-0x1.d80acb1fd9a48p-191")), + (hf64!("-0x1.9b219a1974289p-86"), hf64!("-0x1.1cf977003bdd6p-86"), hf64!("-0x1.2a8a589db0f16p-194")), + (hf64!("-0x1.b102d7663223fp-87"), hf64!("-0x1.2c23f2bc02277p-87"), hf64!("-0x1.1304b8a0f41cdp-194")), + (hf64!("-0x1.3db1f733be456p-87"), hf64!("-0x1.b86b45d2c7c74p-88"), hf64!("-0x1.65075b3baa854p-194")), + (hf64!("-0x1.712c6b2a267f5p-90"), hf64!("-0x1.ffc87ce076dfap-91"), hf64!("-0x1.922d1954bd733p-196")), + (hf64!("-0x1.8bf6e9484dd46p-91"), hf64!("-0x1.127630515eb89p-91"), hf64!("-0x1.aa2e8ae4b621cp-198")), + (hf64!("-0x1.56fe8536a47e9p-91"), hf64!("-0x1.db7daf1e016dfp-92"), hf64!("-0x1.5a5357cd9fecap-199")), + (hf64!("-0x1.4abb72eb058bep-91"), hf64!("-0x1.ca7e01c959788p-92"), hf64!("-0x1.9c974667cda43p-198")), + (hf64!("-0x1.cf23f6232620ep-94"), hf64!("-0x1.4106468e7b671p-94"), hf64!("-0x1.a1de1e34f2642p-200")), + (hf64!("-0x1.0c197520b26f4p-94"), hf64!("-0x1.73aa2cd72b7ccp-95"), hf64!("-0x1.1p-148")), + (hf64!("-0x1.83266a65e67b3p-95"), hf64!("-0x1.0c5a1aeb10a7cp-95"), hf64!("-0x1.5d790953d37ccp-202")), + (hf64!("-0x1.e48acc7d41976p-97"), hf64!("-0x1.4fdbea8f31897p-97"), hf64!("-0x1.889c4c9736da4p-202")), + (hf64!("-0x1.241e7c1327b2ap-97"), hf64!("-0x1.94f6896c09e68p-98"), hf64!("-0x1.ad2fd62d965b7p-203")), + (hf64!("-0x1.c48ddf5beba98p-98"), hf64!("-0x1.39afc8fadbb98p-98"), hf64!("-0x1.37656827f3549p-203")), + (hf64!("-0x1.1d65d5fc31246p-98"), hf64!("-0x1.8ba5360a2b553p-99"), hf64!("-0x1.0bee24969cff9p-204")), + (hf64!("0x1.8a5dd21ef35afp-104"), hf64!("0x1.115aa0fb29a7p-104"), hf64!("-0x1.1p-157")), + (hf64!("0x1.51cc163375e5cp-100"), hf64!("0x1.d4494fb79c5f9p-101"), hf64!("0x1.235a318115a7dp-210")), + (hf64!("0x1.086cbb0e900cfp-96"), hf64!("0x1.6e920d043905ep-97"), hf64!("0x1.0000000000001p-150")), + (hf64!("0x1.f8d2954ab444fp-91"), hf64!("0x1.5dea9642be3ccp-91"), hf64!("0x1.109cd4f731735p-199")), + (hf64!("0x1.8b571f80771bep-90"), hf64!("0x1.12076e976275dp-90"), hf64!("-0x1.fffffffffffffp-144")), + (hf64!("0x1.453f5d718374ap-88"), hf64!("0x1.c2e3888d4911bp-89"), hf64!("0x1.fffffffffffffp-143")), + (hf64!("0x1.101c8b0d7df57p-85"), hf64!("0x1.793a04a8764ap-86"), hf64!("0x1.95385b3628535p-194")), + (hf64!("0x1.99ec5e10fdc8bp-83"), hf64!("0x1.1c231eacb2814p-83"), hf64!("0x1.f82d478ebc7fap-192")), + (hf64!("0x1.530a89e8c2834p-79"), hf64!("0x1.d602c792ff217p-80"), hf64!("0x1.4d765bf788c32p-186")), + (hf64!("0x1.6102ff22f3431p-78"), hf64!("0x1.e960cd938fc71p-79"), hf64!("0x1.fffffffffffffp-133")), + (hf64!("0x1.d7430e6a40aap-78"), hf64!("0x1.46a764f31c407p-78"), hf64!("0x1.fffffffffffffp-132")), + (hf64!("0x1.31677c77b4d02p-62"), hf64!("0x1.a7615378454e9p-63"), hf64!("-0x1.fffffffffffffp-117")), + (hf64!("0x1.760aba9c35ed1p-62"), hf64!("0x1.03441ed22884fp-62"), hf64!("-0x1.fffffffffffffp-116")), + (hf64!("0x1.5f3addb5548c6p-60"), hf64!("0x1.e6e878c6cb8f4p-61"), hf64!("0x1.46dbd6dc7b9eep-168")), + (hf64!("0x1.9a8401f6d9784p-60"), hf64!("0x1.1c8c3a93ce469p-60"), hf64!("-0x1.fffffffffffffp-114")), + (hf64!("0x1.7382bd647eca8p-59"), hf64!("0x1.0182f7f3350e1p-59"), hf64!("0x1.5902345c6d87ep-166")), + (hf64!("0x1.8ddd5485a342fp-56"), hf64!("0x1.13c75940129cbp-56"), hf64!("0x1.365292846d16p-164")), + (hf64!("0x1.2736f975d3b57p-55"), hf64!("0x1.994129328c805p-56"), hf64!("-0x1.fffffffffffffp-110")), + (hf64!("0x1.690ed9e3dd31p-55"), hf64!("0x1.f4885e22dc71bp-56"), hf64!("-0x1.fffffffffffffp-110")), + (hf64!("0x1.e704310d30238p-55"), hf64!("0x1.5192f360cac0ap-55"), hf64!("0x1.30562032a2c95p-161")), + (hf64!("0x1.47bc52cdc6bc1p-53"), hf64!("0x1.c6568b98a9929p-54"), hf64!("0x1.3977497829482p-159")), + (hf64!("0x1.d622a201c030bp-52"), hf64!("0x1.45df797393f11p-52"), hf64!("-0x1.cdcde906092cfp-159")), + (hf64!("0x1.02f371449097fp-51"), hf64!("0x1.66fb73eec9971p-52"), hf64!("0x1.8d182ad6f54d8p-157")), + ]; + + if let Some(result) = lookup_exception(&EXCEPTIONS_TABLE, x) { + return result; + } + + // the 2nd term of the Taylor expansion of 2^x-1 at x=0 is + // log(2)^2/2*x^2 + const C2: f64 = hf64!("0x1.ebfbdff82c58fp-3"); // log(2)^2/2 + + let (hi, lo) = a_mul(LN2H, x); + let lo = LN2L.fma(x, lo); + + // hi+lo approximates the first term x*log(2) + // we add C2*x^2 last, so that in case there is a cancellation in + // LN2L*x+lo, it will contribute more bits + + let lo = lo + (C2 * x * x); + return hi + lo; + } + } + + // now -54 < x < -0x1.0527dbd87e24dp-51 + // or 0x1.0527dbd87e24dp-51 < x < 1024 + + // 2^x-1 is exact for x integer, -53 <= x <= 53 + if ux << 17 == 0 { + cold_path(); + + let i = Float::floor(x); + if x == i && (-53.0..=53.0).contains(&i) { + if i >= 0.0 { + return ((1u64 << i64::cast_from(i)) - 1).cast(); + } else { + return -1.0 + ldexp(1.0, i.cast()); + } + } + } + + let (hi, lo, err) = exp2m1_fast(x, ax <= 0x3fc0000000000000); + let left = hi + (lo - err); + let right = hi + (lo + err); + + if left == right { + return left; + } + + cold_path(); + exp2m1_accurate(x) +} + +/// Given `-54 < x < -0x1.0527dbd87e24dp-51` or `0x1.0527dbd87e24dp-51 < x < 1024`, put in `hi + lo` +/// a double-double approximation of `exp2m1(x)`, and return the maximal corresponding +/// absolute error. +/// The input `tiny` is true iff `|x| <= 0.125`. +#[inline] +fn exp2m1_fast(x: f64, tiny: bool) -> (f64, f64, f64) { + // |x| <= 0.125 + if tiny { + return exp2m1_fast_tiny(x); + } + + // now -54 < x < -0.125 or 0.125 < x < 1024: we approximate exp(x*log(2)) + // and subtract 1 + let (hi, lo) = s_mul(x, LN2H, LN2L); + + // s_mul (hi, lo, x, LN2H, LN2L) decomposes into: + // a_mul (hi, t, x, LN2H) + // lo += fma (x, LN2L, t) + // The a_mul() call is exact, and the error of the fma() is bounded by + // ulp(lo). + // We have |t| <= ulp(hi) <= ulp(LN2H*1024) = 2^-43, + // |t+x*LN2L| <= 2^-43 * 1024*LN2L < 2^-42.7, + // thus |lo| <= |t| + |x*LN2L| + ulp(t+x*LN2L) + // <= 2^-42.7 + 2^-95 <= 2^-42.6, and ulp(lo) <= 2^-95. + // Thus: + // |hi + lo - x*log(2)| <= |hi + lo - x*(LN2H+LN2L)| + |x|*|LN2H+LN2L-log(2)| + // <= 2^-95 + 1024*2^-110.4 < 2^-94.9 + + let (hi, lo) = exp_1(hi, lo); + // hi_out + lo_out = exp(hi + lo) * (1 + eps) with |eps| < 2^-74.139 + // = exp(x*log(2) + eps0) * (1 + eps) + // with |eps0| < 2^-94.9 and |eps| < 2^-74.139 + // = 2^x * (1 + eps1) with |eps1| < 2^-74.138 + + // implies hi >= 1 and the fast_two_sum pre-condition holds + let (hi, u) = if x >= 0.0 { + fast_two_sum(hi, -1.0) + } else { + fast_two_sum(-1.0, hi) // x < 0 thus hi <= 1 + }; + let lo = lo + u; + + // The error in the above fast_two_sum is bounded by 2^-105*|hi|, + // with the new value of hi, thus the total absolute error is bounded + // by eps1*|hi_in|+2^-105*|hi|. + // Relatively to hi this yields eps1*|hi_in/hi| + 2^-105, where the maximum + // of |hi_in/hi| is obtained for x near -0.125, with |2^x/(2^x-1)| < 11.05. + // We get a relative error bound of 2^-74.138*11.05 + 2^-105 < 2^-70.67. + let err = hf64!("0x1.42p-71") * hi; // 2^-70.67 < 0x1.42p-71 + (hi, lo, err) +} + +#[cold] +#[inline] +fn exp2m1_accurate(x: f64) -> f64 { + let ux = x.to_bits(); + let ax = ux & 0x7fffffffffffffff; + + // |x| <= 0.125 + if ax <= 0x3fc0000000000000 { + return exp2m1_accurate_tiny(x); + } + + // now -54 < x < -0.125 or 0.125 < x < 1024: we approximate exp(x*log(2)) + // and subtract 1 + // The following table should be sorted by increasing values of the first + // entry (x). + #[rustfmt::skip] + static EXCEPTIONS_TABLE: [(f64, f64, f64); 93] = [ + (-hf64!("0x1.da22611253866p+0"), -hf64!("0x1.722e3006b8e0bp-1"), -hf64!("0x1.cb1ddc7fbf64ep-109")), + (-hf64!("0x1.9e5bc7d550bb5p+0"), -hf64!("0x1.5943df965427p-1"), hf64!("0x1.f00a26c16d035p-107")), + (-hf64!("0x1.e511f8c5f829fp-1"), -hf64!("0x1.ecfd3f665583fp-2"), hf64!("0x1.cd0996b2d6842p-106")), + (-hf64!("0x1.cef4c143b5adfp-1"), -hf64!("0x1.dcd9f4d61d31fp-2"), hf64!("0x1.88f636e171893p-109")), + (-hf64!("0x1.b444c224a70ccp-1"), -hf64!("0x1.c8b8c01ae382cp-2"), -hf64!("0x1.cb1ddc7fbf64ep-108")), + (-hf64!("0x1.9de261c7c8623p-1"), -hf64!("0x1.b7448eda85236p-2"), hf64!("0x1.e2651db687d79p-106")), + (-hf64!("0x1.750291d22fdap-1"), -hf64!("0x1.95ffcf1509869p-2"), hf64!("0x1.2aabef02ae4cbp-103")), + (-hf64!("0x1.3cb78faaa176ap-1"), -hf64!("0x1.650f7e59509cp-2"), hf64!("0x1.f00a26c16d035p-106")), + (-hf64!("0x1.304447dcfa48ap-1"), -hf64!("0x1.59b94aff7c2b5p-2"), hf64!("0x1.ca6392b99ad78p-106")), + (-hf64!("0x1.e242801b45d0dp-2"), -hf64!("0x1.1d32c9d218361p-2"), hf64!("0x1.03dd41d134594p-109")), + (-hf64!("0x1.d2b12277c95d4p-2"), -hf64!("0x1.155f2f4b7a739p-2"), hf64!("0x1.08b48f6466755p-106")), + (-hf64!("0x1.a4ec34efdd3e1p-2"), -hf64!("0x1.fbc1acbd1558ep-3"), hf64!("0x1.be847d667711dp-105")), + (-hf64!("0x1.8827da1dbf35ep-2"), -hf64!("0x1.dd7867fe8d091p-3"), hf64!("0x1.ffffffffffff8p-57")), + (-hf64!("0x1.75cfa7af70fep-2"), -hf64!("0x1.c9d8c829dad99p-3"), -hf64!("0x1.7e233fcc3fd44p-105")), + (-hf64!("0x1.6d729ff937eadp-2"), -hf64!("0x1.c0d1b0bcb29d6p-3"), hf64!("0x1.7f0f685911fccp-105")), + (-hf64!("0x1.6b3801c55815ap-2"), -hf64!("0x1.be67897b4ab5ep-3"), hf64!("0x1.636be28bf2c88p-106")), + (-hf64!("0x1.6822ccd5b876bp-2"), -hf64!("0x1.bb0f0bff2db03p-3"), hf64!("0x1.4d6bbe2b72be3p-106")), + (-hf64!("0x1.5dbf023667d22p-2"), -hf64!("0x1.afbb4c7215b5bp-3"), hf64!("0x1.3bbc634b006b1p-105")), + (-hf64!("0x1.5a0fba59f338fp-2"), -hf64!("0x1.abb2028f6491cp-3"), hf64!("0x1.0a9c5a206828ep-104")), + (-hf64!("0x1.540658c7eedcap-2"), -hf64!("0x1.a50f96bbf778cp-3"), hf64!("0x1.f14fe3c42820cp-105")), + (-hf64!("0x1.31c2bbece259bp-2"), -hf64!("0x1.7ee2b68e61af7p-3"), -hf64!("0x1.7852d3ddee861p-107")), + (-hf64!("0x1.253dd048bf2e7p-2"), -hf64!("0x1.70b709e65877ep-3"), hf64!("0x1.3625f20d5b638p-104")), + (-hf64!("0x1.1249283f53506p-2"), -hf64!("0x1.5b076e9ede20bp-3"), hf64!("0x1.14d2b0b9298b2p-105")), + (-hf64!("0x1.0f2086c7b8f83p-2"), -hf64!("0x1.57636305fd889p-3"), hf64!("0x1.9442185db9d73p-104")), + (-hf64!("0x1.ff6ff72ac299dp-3"), -hf64!("0x1.458423bb5b025p-3"), hf64!("0x1.140aa7335a14p-104")), + (-hf64!("0x1.fe89353e31cbfp-3"), -hf64!("0x1.44fd97cc9857p-3"), -hf64!("0x1.a549adf007feap-109")), + (-hf64!("0x1.ec7298061ebfap-3"), -hf64!("0x1.3a68f321de8abp-3"), hf64!("0x1.7f700400ca84fp-103")), + (-hf64!("0x1.d4854d9f87fcap-3"), -hf64!("0x1.2c506fcda97fcp-3"), -hf64!("0x1.a4fedf7b3ca3fp-109")), + (-hf64!("0x1.a81224ff9a5cfp-3"), -hf64!("0x1.11d2b7526eae5p-3"), hf64!("0x1.6fb9bdbb725b2p-106")), + (-hf64!("0x1.93314fd04393cp-3"), -hf64!("0x1.053dee1b48ebdp-3"), hf64!("0x1.2c15fbfa775b3p-104")), + (-hf64!("0x1.8cd71c092c54dp-3"), -hf64!("0x1.0165759fe661fp-3"), hf64!("0x1.395807142ed1ep-104")), + (-hf64!("0x1.747fb97a9be61p-3"), -hf64!("0x1.e52ae5003c9dbp-4"), -hf64!("0x1.dbf62ab30e6cp-106")), + (-hf64!("0x1.64b0e525bc004p-3"), -hf64!("0x1.d1cc0e1d08fa9p-4"), hf64!("0x1.69442904d7698p-104")), + (-hf64!("0x1.539353db1bdebp-3"), -hf64!("0x1.bcb50aacb6649p-4"), -hf64!("0x1.a8b1eddf109b5p-107")), + (-hf64!("0x1.4faff91bda7ddp-3"), -hf64!("0x1.b7e630a3fab2ap-4"), hf64!("0x1.47706b5ad802ap-105")), + (-hf64!("0x1.4c2bec34d5837p-3"), -hf64!("0x1.b38bcbad2b407p-4"), -hf64!("0x1.b4e03df36240cp-108")), + (-hf64!("0x1.439bb08833472p-3"), -hf64!("0x1.a8ebef4998a79p-4"), -hf64!("0x1.2c3a2b30f99cfp-105")), + (-hf64!("0x1.418f83945849ap-3"), -hf64!("0x1.a6606e818c2e3p-4"), hf64!("0x1.145b99cfb763cp-104")), + (-hf64!("0x1.38417d7db5a2fp-3"), -hf64!("0x1.9aca18a87c03ap-4"), hf64!("0x1.76a10886fcc65p-105")), + (-hf64!("0x1.3041fe0fc550bp-3"), -hf64!("0x1.90ccdeae8d3ddp-4"), hf64!("0x1.f202cb751932p-104")), + (-hf64!("0x1.236e786c083dbp-3"), -hf64!("0x1.80b96ddbdb1b3p-4"), hf64!("0x1.ec381ad5e1718p-105")), + (-hf64!("0x1.06c0eba373a18p-3"), -hf64!("0x1.5c86ed4f55301p-4"), hf64!("0x1.fffffffffffe3p-58")), + (-hf64!("0x1.06559d8a9ff1ap-3"), -hf64!("0x1.5bfed18cc8ff3p-4"), hf64!("0x1.87a53f06d41bdp-105")), + (-hf64!("0x1.0006ec0817742p-3"), -hf64!("0x1.53fc5e2bd9f6cp-4"), -hf64!("0x1.6fcda9953133bp-107")), + (hf64!("0x1.06d6b01577482p-3"), hf64!("0x1.7d11edfaac429p-4"), hf64!("0x1.ffffffffffffbp-58")), + (hf64!("0x1.079724fbb3d86p-3"), hf64!("0x1.7e35970e11732p-4"), -hf64!("0x1.b3086fea6e73cp-107")), + (hf64!("0x1.0e2f828c93192p-3"), hf64!("0x1.88376cd2f4076p-4"), -hf64!("0x1.0bc0000d21ae1p-106")), + (hf64!("0x1.111c072cd8f6ep-3"), hf64!("0x1.8ca9045ba3e95p-4"), hf64!("0x1.80f80d8a8722ap-105")), + (hf64!("0x1.165c1c6243beep-3"), hf64!("0x1.94a6924c0b159p-4"), hf64!("0x1.ffffffffffff9p-58")), + (hf64!("0x1.182f8cb329d34p-3"), hf64!("0x1.976ed12501a66p-4"), -hf64!("0x1.b0e5142b2ec14p-105")), + (hf64!("0x1.1a8801599f756p-3"), hf64!("0x1.9b02639918dcdp-4"), hf64!("0x1.736bd9c878fbap-107")), + (hf64!("0x1.1bb1b31cf8c98p-3"), hf64!("0x1.9cc894e900c64p-4"), hf64!("0x1.218cf2d9f3926p-104")), + (hf64!("0x1.2ba99934336fcp-3"), hf64!("0x1.b5379b9a6155cp-4"), hf64!("0x1.68cdeb7f25f81p-106")), + (hf64!("0x1.2dbe99fec947ap-3"), hf64!("0x1.b869aac963d94p-4"), hf64!("0x1.59f0621f5fc6ep-107")), + (hf64!("0x1.2eecf76d63cdp-3"), hf64!("0x1.ba39ff28e3eap-4"), -hf64!("0x1.cb1ddc7fbf64ep-107")), + (hf64!("0x1.45c447c5552ddp-3"), hf64!("0x1.dd721415a4a8dp-4"), -hf64!("0x1.e5111c4062fa9p-108")), + (hf64!("0x1.502f26bc1aa18p-3"), hf64!("0x1.ed99608511a69p-4"), -hf64!("0x1.603952f77b6d4p-106")), + (hf64!("0x1.5856bdae2f08fp-3"), hf64!("0x1.fa489ed69464fp-4"), hf64!("0x1.93f08a7946826p-104")), + (hf64!("0x1.5f38bbc1a9944p-3"), hf64!("0x1.028230c4e5389p-3"), hf64!("0x1.313458d3f84a2p-105")), + (hf64!("0x1.691b76c4b3c1bp-3"), hf64!("0x1.0a3d166eacab7p-3"), hf64!("0x1.9a98ee8a745ffp-104")), + (hf64!("0x1.6c4175ea0c6e1p-3"), hf64!("0x1.0cb4b9c7701a4p-3"), hf64!("0x1.a6605f3be564ep-108")), + (hf64!("0x1.6f6544ef96539p-3"), hf64!("0x1.0f2b51ddc90e1p-3"), hf64!("0x1.06e9b6d330daep-105")), + (hf64!("0x1.7a3ab625d0dc4p-3"), hf64!("0x1.17b04242e48fep-3"), hf64!("0x1.60215b123bdbdp-108")), + (hf64!("0x1.7d33f0271269bp-3"), hf64!("0x1.1a0842d5b11e4p-3"), -hf64!("0x1.5c29b4c0046f9p-106")), + (hf64!("0x1.887678e0de774p-3"), hf64!("0x1.22edc495eb728p-3"), hf64!("0x1.e2651db687d79p-105")), + (hf64!("0x1.8fe3adec251f6p-3"), hf64!("0x1.28d0ae0c0ac4fp-3"), hf64!("0x1.e1bce5201668bp-104")), + (hf64!("0x1.926961243babap-3"), hf64!("0x1.2ad15442773c8p-3"), hf64!("0x1.112edd525e11cp-105")), + (hf64!("0x1.a066bd757bf54p-3"), hf64!("0x1.35f4af72a0a25p-3"), -hf64!("0x1.59b2d13ebb9d3p-106")), + (hf64!("0x1.b00f80708d157p-3"), hf64!("0x1.427c60cc0d217p-3"), hf64!("0x1.344b77789552ap-103")), + (hf64!("0x1.b8ee8dcb0e797p-3"), hf64!("0x1.499d2a386003fp-3"), -hf64!("0x1.e5d43d3c21c2ap-105")), + (hf64!("0x1.c3b0f79893bfcp-3"), hf64!("0x1.5249b249243b7p-3"), hf64!("0x1.1a9819f194524p-105")), + (hf64!("0x1.d5ac74e107b39p-3"), hf64!("0x1.60db0007c78ffp-3"), -hf64!("0x1.a936bca36028fp-106")), + (hf64!("0x1.e9532c87abc5bp-3"), hf64!("0x1.70e070f8d9706p-3"), -hf64!("0x1.0ab97f38bd529p-106")), + (hf64!("0x1.16383a5fe20dp-2"), hf64!("0x1.a867756a9a8fep-3"), -hf64!("0x1.8ac73f07192e6p-106")), + (hf64!("0x1.2a381dbbc8f07p-2"), hf64!("0x1.ca1a3ba1db0ebp-3"), -hf64!("0x1.7f5a08441b722p-105")), + (hf64!("0x1.8690e0aabd12cp-2"), hf64!("0x1.35e1034d5ec8p-2"), hf64!("0x1.f00a26c16d035p-105")), + (hf64!("0x1.a246f399a957bp-2"), hf64!("0x1.4f22bccd234f1p-2"), hf64!("0x1.ffffffffffff6p-56")), + (hf64!("0x1.b808c857723e7p-2"), hf64!("0x1.634ce84a0e79dp-2"), hf64!("0x1.ffffffffffff4p-56")), + (hf64!("0x1.bb2446a7b770fp-2"), hf64!("0x1.663466e4bb92bp-2"), -hf64!("0x1.a00681268d32cp-106")), + (hf64!("0x1.c14d1ce57d7ebp-2"), hf64!("0x1.6bfacbdf4334ep-2"), hf64!("0x1.1cfc31c20bd19p-104")), + (hf64!("0x1.f8432a3c6c453p-2"), hf64!("0x1.a096f253eca4bp-2"), hf64!("0x1.56c0f7fb6fa16p-106")), + (hf64!("0x1.16a76ec41b516p-1"), hf64!("0x1.d541a1690b18ep-2"), hf64!("0x1.08b48f6466755p-105")), + (hf64!("0x1.1e5f48d8ba05ap-1"), hf64!("0x1.e4f0e78affdcdp-2"), hf64!("0x1.ffffffffffffep-56")), + (hf64!("0x1.4a63ff1d53f53p-1"), hf64!("0x1.20cc3b425aa51p-1"), hf64!("0x1.636be28bf2c88p-105")), + (hf64!("0x1.d906bc1caeb3dp-1"), hf64!("0x1.cb5f21a856e8dp-1"), hf64!("0x1.c737d097094f7p-104")), + (hf64!("0x1.25dd9eedac79ap+0"), hf64!("0x1.37473fe51c7d4p+0"), -hf64!("0x1.cb1ddc7fbf64ep-106")), + (hf64!("0x1.61a4382aaf44bp+0"), hf64!("0x1.9af081a6af64p+0"), hf64!("0x1.f00a26c16d035p-104")), + (hf64!("0x1.8b53b7620da8bp+0"), hf64!("0x1.eaa0d0b4858c7p+0"), hf64!("0x1.08b48f6466755p-104")), + (hf64!("0x1.12eecf76d63cdp+1"), hf64!("0x1.b7473fe51c7d4p+1"), -hf64!("0x1.cb1ddc7fbf64ep-105")), + (hf64!("0x1.92eecf76d63cdp+1"), hf64!("0x1.f7473fe51c7d4p+2"), -hf64!("0x1.cb1ddc7fbf64ep-104")), + (hf64!("0x1.ffef3f31766b7p+5"), hf64!("0x1.fd1ae8d0e6abdp+63"), -hf64!("0x1.5ea4d3ad23ccep-42")), + (hf64!("0x1.586b62dc1e8c2p+6"), hf64!("0x1.134d37339fe93p+86"), -hf64!("0x1.1295610d3deffp-20")), + (hf64!("0x1.6f33cdaf56d6p+6"), hf64!("0x1.bde76c2db52fbp+91"), hf64!("0x1.564254439c8a9p-15")), + ]; + + if let Some(result) = lookup_exception(&EXCEPTIONS_TABLE, x) { + return result; + } + + let (hi, lo) = exp_2(x); + + // implies hi >= 1 and the fast_two_sum pre-condition holds + let (hi, u) = if x >= 0.0 { + fast_two_sum(hi, -1.0) + } else { + fast_two_sum(-1.0, hi) // x < 0 thus hi <= 1 + }; + + let lo = lo + u; + + // the error in the above fast_two_sum is bounded by 2^-105*|hi|, + // with the new value of hi + return hi + lo; +} + +/// The following is a degree-10 polynomial generated by Sollya (file `exp2m1_fast.sollya`), +/// which approximates `exp2m1(x)` with relative error bounded by `2^-68.559` for `|x| <= 0.125`. +const P: [f64; 12] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.abd1697afcaf8p-56"), // degree 1, P[0], P[1] + hf64!("0x1.ebfbdff82c58fp-3"), + -hf64!("0x1.5e5a1d09e1599p-57"), // degree 2, P[2], P[3] + hf64!("0x1.c6b08d704a0bfp-5"), // degree 3, P[4] + hf64!("0x1.3b2ab6fba4e78p-7"), // degree 4, P[5] + hf64!("0x1.5d87fe78a84e6p-10"), // degree 5, P[6] + hf64!("0x1.430912f86a48p-13"), // degree 6, P[7] + hf64!("0x1.ffcbfbc1f2b36p-17"), // degree 7, P[8] + hf64!("0x1.62c0226c7f6d1p-20"), // degree 8, P[9] + hf64!("0x1.b539529819e63p-24"), // degree 9, P[10] + hf64!("0x1.e4d552bed5b9cp-28"), // degree 10, P[11] +]; + +/// `|x| <= 0.125`, put in `hi + lo` a double-double approximation of `exp2m1(x)`, and return the +/// maximal corresponding absolute error. We also have `|x| > 0x1.0527dbd87e24dp-51`. +/// With `xmin=RR("0x1.0527dbd87e24dp-51",16)`, the routine +/// `exp2m1_fast_tiny_all(xmin,0.125,2^-65.73)` in `exp2m1.sage` returns +/// `1.63414352331297e-20 < 2^-65.73`, and `exp2m1_fast_tiny_all(-0.125,-xmin,2^-65.62)` returns +/// `1.76283772822891e-20 < 2^-65.62`, which proves the relative error is bounded by `2^-65.62`. +#[inline] +fn exp2m1_fast_tiny(x: f64) -> (f64, f64, f64) { + // The maximal value of |c4*x^4/exp2m1(x)| over [-0.125,0.125] + // is less than 2^-15.109, where c4 is the degree-4 coefficient, + // thus we can compute the coefficients of degree 4 or higher + // using double precision only. + let x2 = x * x; + let x4 = x2 * x2; + let c8 = P[10].fma(x, P[9]); // degree 8 + let c6 = P[8].fma(x, P[7]); // degree 6 + let c4 = P[6].fma(x, P[5]); // degree 4 + let c8 = P[11].fma(x2, c8); // degree 8 + let c4 = c6.fma(x2, c4); // degree 4 + let c4 = c8.fma(x4, c4); // degree 4 + + // multiply c4 by x and add P[4] + let (hi, lo) = a_mul(c4, x); + let (hi, lo) = fast_sum(P[4], hi, lo); + + // multiply (hi,lo) by x and add P[2]+P[3] + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(P[2], hi); + let lo = lo + (t + P[3]); + + // multiply (hi,lo) by x and add P[0]+P[1] + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(P[0], hi); + let lo = lo + (t + P[1]); + + // multiply (hi,lo) by x + let (hi, lo) = s_mul(x, hi, lo); + let err = hf64!("0x1.4ep-66") * hi; // 2^-65.62 < 0x1.4ep-66 + (hi, lo, err) +} + +// The following is a degree-15 polynomial generated by Sollya +// (file exp2m1_accurate.sollya), +// which approximates exp2m1(x) with relative error bounded by 2^-107.666 +// for |x| <= 0.125. +const Q: [f64; 22] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.abc9e3b39804p-56"), // degree 1: Q[0], Q[1] + hf64!("0x1.ebfbdff82c58fp-3"), + -hf64!("0x1.5e43a53e44dcfp-57"), // degree 2: Q[2], Q[3] + hf64!("0x1.c6b08d704a0cp-5"), + -hf64!("0x1.d331627517168p-59"), // degree 3: Q[4], Q[5] + hf64!("0x1.3b2ab6fba4e77p-7"), + hf64!("0x1.4e65df0779f8cp-62"), // degree 4: Q[6], Q[7] + hf64!("0x1.5d87fe78a6731p-10"), + hf64!("0x1.0717fbf4bd05p-66"), // degree 5: Q[8], Q[9] + hf64!("0x1.430912f86c787p-13"), + hf64!("0x1.bd2bdec9bcd42p-67"), // degree 6: Q[10], Q[11] + hf64!("0x1.ffcbfc588b0c7p-17"), + -hf64!("0x1.e60aa6d5e4aa9p-71"), // degree 7: Q[12], Q[13] + hf64!("0x1.62c0223a5c824p-20"), // degree 8: Q[14] + hf64!("0x1.b5253d395e7d4p-24"), // degree 9: Q[15] + hf64!("0x1.e4cf5158b916p-28"), // degree 10: Q[16] + hf64!("0x1.e8cac734c6058p-32"), // degree 11: Q[17] + hf64!("0x1.c3bd64f17199dp-36"), // degree 12: Q[18] + hf64!("0x1.8161a17e05651p-40"), // degree 13: Q[19] + hf64!("0x1.3150b3d792231p-44"), // degree 14: Q[20] + hf64!("0x1.c184260bfad7ep-49"), // degree 15: Q[21] +]; + +/// Accurate path for `0x1.0527dbd87e24dp-51 < |x| <= 0.125`. +#[inline] +fn exp2m1_accurate_tiny(x: f64) -> f64 { + // The following exceptional cases have at least 51 identical bits after + // the round bit, thus are hard to correctly round with double-double + // arithmetic. They should be sorted by increasing values of the first + // entry (x). + #[rustfmt::skip] + static EXCEPTIONS_TABLE: [(f64, f64, f64); 59] = [ + (-hf64!("0x1.f6ec73d3948c3p-4"), -hf64!("0x1.4e2d8b0cead45p-4"), -hf64!("0x1.40f3d5244acffp-109")), + (-hf64!("0x1.9b28778a9a8a3p-4"), -hf64!("0x1.134dec3a6324bp-4"), -hf64!("0x1.1acaaac7e1ff7p-110")), + (-hf64!("0x1.6f94484e5e1fdp-5"), -hf64!("0x1.f5baee010ccc6p-6"), -hf64!("0x1.3298bcde4f9a8p-115")), + (-hf64!("0x1.3918e8608bd5bp-8"), -hf64!("0x1.b153bf52832f9p-9"), -hf64!("0x1.fffffffffffffp-63")), + (-hf64!("0x1.8474969f5eb14p-9"), -hf64!("0x1.0cfafc07a957bp-9"), -hf64!("0x1.fffffffffffffp-63")), + (-hf64!("0x1.0867d8153350dp-9"), -hf64!("0x1.6e49b44e387f5p-10"), -hf64!("0x1.fffffffffffffp-64")), + (-hf64!("0x1.bfb3efcdf2bc4p-10"), -hf64!("0x1.3623eff91de91p-10"), hf64!("0x1.cd506bd8d0439p-117")), + (-hf64!("0x1.0c3ebbd1a501fp-11"), -hf64!("0x1.73ccf8ee62819p-12"), hf64!("0x1.3a3965a926c6dp-120")), + (-hf64!("0x1.3c7971b0ee205p-14"), -hf64!("0x1.b6b716c4bb87cp-15"), hf64!("0x1.d7150e84d973dp-121")), + (-hf64!("0x1.3dbf41403c0b2p-15"), -hf64!("0x1.b87c37192e4f7p-16"), hf64!("0x1.098861b427b13p-123")), + (-hf64!("0x1.3b6786a5a9a69p-17"), -hf64!("0x1.b53dee1a96ca4p-18"), -hf64!("0x1.282e0781f97f5p-124")), + (-hf64!("0x1.7b388eb924102p-18"), -hf64!("0x1.06dafba21afffp-18"), hf64!("0x1.fffffffffffffp-72")), + (-hf64!("0x1.98aae9950914ep-19"), -hf64!("0x1.1b443a4805f8fp-19"), hf64!("0x1.fffffffffffffp-73")), + (-hf64!("0x1.41e4bec9bc547p-19"), -hf64!("0x1.be3d238468f35p-20"), -hf64!("0x1.fffffffffffffp-74")), + (-hf64!("0x1.0f1c08e43f217p-20"), -hf64!("0x1.77d66368613b4p-21"), hf64!("0x1.0d3de71c269fcp-130")), + (-hf64!("0x1.0d296993d1368p-20"), -hf64!("0x1.752326c780f68p-21"), hf64!("0x1.1cade8dbbd5d8p-130")), + (-hf64!("0x1.8288f6bb77d79p-23"), -hf64!("0x1.0becf6ad924bfp-23"), -hf64!("0x1.fffffffffffffp-77")), + (-hf64!("0x1.0ee8225c19555p-23"), -hf64!("0x1.778e77e8b1e13p-24"), -hf64!("0x1.3da14df43e675p-129")), + (-hf64!("0x1.922076a30742p-24"), -hf64!("0x1.16bba98a001c1p-24"), hf64!("0x1.d90eddeada3e4p-133")), + (-hf64!("0x1.3b6203ff2cbe6p-29"), -hf64!("0x1.b536a7dace8cap-30"), hf64!("0x1.07bb47158694fp-137")), + (-hf64!("0x1.e07a2fbc7fd84p-30"), -hf64!("0x1.4d0a9e634dc35p-30"), -hf64!("0x1.fffffffffffffp-84")), + (-hf64!("0x1.ba24ff5dea796p-34"), -hf64!("0x1.3278a26ed0162p-34"), hf64!("0x1.f837135341102p-142")), + (-hf64!("0x1.d091d774b141ep-35"), -hf64!("0x1.4203e2685b069p-35"), -hf64!("0x1.a166cdf05ff79p-143")), + (-hf64!("0x1.bfc1e9b0f73aep-35"), -hf64!("0x1.365ca0d933491p-35"), -hf64!("0x1.fffffffffffffp-89")), + (-hf64!("0x1.6d752e77a9dc2p-41"), -hf64!("0x1.faa1cb0d787b3p-42"), -hf64!("0x1.fffffffffffffp-96")), + (-hf64!("0x1.a1f242e670d73p-42"), -hf64!("0x1.21b2c54479c4dp-42"), hf64!("0x1.48a5d173822e8p-150")), + (-hf64!("0x1.66ff2474821a7p-44"), -hf64!("0x1.f1accede78f51p-45"), -hf64!("0x1.fffffffffffffp-99")), + (-hf64!("0x1.3f596b7b1c5e2p-44"), -hf64!("0x1.bab64e105226ap-45"), -hf64!("0x1.1a2829f825272p-154")), + (-hf64!("0x1.daaead688f65fp-45"), -hf64!("0x1.4906541fb8d5cp-45"), -hf64!("0x1.31de54e6dc4ep-151")), + (-hf64!("0x1.0e5678a7b8bap-47"), -hf64!("0x1.76c48a7a527bp-48"), -hf64!("0x1.6fbb13d1e3faap-155")), + (-hf64!("0x1.a6d6a49f2187fp-50"), -hf64!("0x1.2516dafdf17adp-50"), -hf64!("0x1.85a87dc0b88p-157")), + (-hf64!("0x1.2716da024f6d9p-50"), -hf64!("0x1.9914a112c8dadp-51"), hf64!("0x1.004287d8393fep-159")), + (-hf64!("0x1.2c506b0368099p-51"), -hf64!("0x1.a052e3d5e791p-52"), -hf64!("0x1.428e13d1da7cap-159")), + (hf64!("0x1.391609b20beaap-51"), hf64!("0x1.b2078ba8f6835p-52"), -hf64!("0x1.85099d92fa919p-162")), + (hf64!("0x1.4bbbd21c8c721p-51"), hf64!("0x1.cbe169f09f95ep-52"), -hf64!("0x1.256d63281c5efp-158")), + (hf64!("0x1.29d3990338b4ep-50"), hf64!("0x1.9ce011cf5f46ep-51"), -hf64!("0x1.ba552cb13d27bp-161")), + (hf64!("0x1.d57070df38af8p-50"), hf64!("0x1.4563f61023f7p-50"), -hf64!("0x1.ac66c9a5a19p-157")), + (hf64!("0x1.f6ac7a2928816p-49"), hf64!("0x1.5c6d4854f6b3bp-49"), hf64!("0x1.fffffffffffffp-103")), + (hf64!("0x1.c2ff91c241999p-47"), hf64!("0x1.389bb3cfc42c2p-47"), -hf64!("0x1.023c49dbbc653p-154")), + (hf64!("0x1.ac65fdb418f72p-40"), hf64!("0x1.28f171f05f6fap-40"), -hf64!("0x1.51612679f6f55p-146")), + (hf64!("0x1.f06778562781fp-38"), hf64!("0x1.5814c6c101c7p-38"), -hf64!("0x1.333ad12f679a5p-145")), + (hf64!("0x1.02cdc0b0f314cp-37"), hf64!("0x1.66c7342a5b89fp-38"), -hf64!("0x1.fffffffffffffp-92")), + (hf64!("0x1.2162f23d082d5p-36"), hf64!("0x1.912cc5483c4bbp-37"), -hf64!("0x1.9b93a2f7e5da4p-145")), + (hf64!("0x1.2d664653786ecp-36"), hf64!("0x1.a1d414c603907p-37"), hf64!("0x1.fffffffffffffp-91")), + (hf64!("0x1.b3665a468351p-35"), hf64!("0x1.2dcbd0c2b922fp-35"), -hf64!("0x1.fffffffffffffp-89")), + (hf64!("0x1.3dc2574084694p-34"), hf64!("0x1.b881f93d3d7bcp-35"), hf64!("0x1.4d32033399826p-141")), + (hf64!("0x1.b988be78bfb6bp-33"), hf64!("0x1.320c53ed5fb63p-33"), -hf64!("0x1.fffffffffffffp-87")), + (hf64!("0x1.e755f4897c124p-27"), hf64!("0x1.51cba0164bed2p-27"), -hf64!("0x1.16a01564a1359p-134")), + (hf64!("0x1.e4c5c9414182ep-26"), hf64!("0x1.5004cdd3ec269p-26"), -hf64!("0x1.411c828b7c78ep-133")), + (hf64!("0x1.1c4baf7475e2dp-23"), hf64!("0x1.8a1e1272b4997p-24"), hf64!("0x1.fffffffffffffp-78")), + (hf64!("0x1.3a6de04d8d384p-22"), hf64!("0x1.b3e437cdb56dep-23"), hf64!("0x1p-76")), + (hf64!("0x1.4753a08baf7fbp-22"), hf64!("0x1.c5c56aeb3c161p-23"), hf64!("0x1.fffffffffffffp-77")), + (hf64!("0x1.b6275c496a3d5p-16"), hf64!("0x1.2fb5318232c0cp-16"), hf64!("0x1.666c5e4de0763p-123")), + (hf64!("0x1.02df58d57f81fp-13"), hf64!("0x1.66e3866f48983p-14"), -hf64!("0x1.3e42ea0bb96a5p-121")), + (hf64!("0x1.a949818632705p-13"), hf64!("0x1.26ceaaf78e39fp-13"), -hf64!("0x1.fffffffffffffp-67")), + (hf64!("0x1.dcc98bad34bd4p-13"), hf64!("0x1.4a82829ba33efp-13"), hf64!("0x1.3a222409353fep-120")), + (hf64!("0x1.8ff59817c7989p-9"), hf64!("0x1.15862abd340e8p-9"), hf64!("0x1.6ab813a41d504p-116")), + (hf64!("0x1.6a8ee6a3521fdp-6"), hf64!("0x1.fa7ca1e176885p-7"), hf64!("0x1.6530cb0e0ba64p-113")), + (hf64!("0x1.d4a8ebce833a3p-4"), hf64!("0x1.52145769df4fbp-4"), -hf64!("0x1.5d8026da33cabp-111")), + ]; + + if let Some(result) = lookup_exception(&EXCEPTIONS_TABLE, x) { + return result; + } + + let x2 = x * x; + let x4 = x2 * x2; + + let c13 = Q[20].fma(x, Q[19]); // degree 13 + let c11 = Q[18].fma(x, Q[17]); // degree 11 + let c13 = Q[21].fma(x2, c13); // degree 13 + + // add Q[16]*x+c11*x2+c13*x4 to Q[15] (degree 9) + let (hi, lo) = fast_two_sum(Q[15], Q[16] * x + c11 * x2 + c13 * x4); + // multiply hi+lo by x and add Q[14] (degree 8) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[14], hi); + let lo = lo + t; + + // multiply hi+lo by x and add Q[12]+Q[13] (degree 7) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[12], hi); + let lo = lo + (t + Q[13]); + + // multiply hi+lo by x and add Q[10]+Q[11] (degree 6) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[10], hi); + let lo = lo + (t + Q[11]); + + // multiply hi+lo by x and add Q[8]+Q[9] (degree 5) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[8], hi); + let lo = lo + (t + Q[9]); + + // multiply hi+lo by x and add Q[6]+Q[7] (degree 4) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[6], hi); + let lo = lo + (t + Q[7]); + + // multiply hi+lo by x and add Q[4]+Q[5] (degree 3) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[4], hi); + let lo = lo + (t + Q[5]); + + // multiply hi+lo by x and add Q[2]+Q[3] (degree 2) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[2], hi); + let lo = lo + (t + Q[3]); + + // multiply hi+lo by x and add Q[0]+Q[1] (degree 2) + let (hi, lo) = s_mul(x, hi, lo); + let (hi, t) = fast_two_sum(Q[0], hi); + let lo = lo + (t + Q[1]); + + // multiply hi+lo by x + let (hi, lo) = s_mul(x, hi, lo); + + hi + lo +} + +/// Approximation of `exp(x)`, where `x = xh + xl` +/// +/// `exp(x)` is approximated by `hi + lo`. +/// +/// For the error analysis, we only consider the case where `x^y` does not overflow or underflow. +/// We get: +/// +/// `(hi + lo) / exp(xh + xl) = 1 + eps` with `|eps| < 2^-74.139` +/// +/// Assumes `|xl/xh| < 2^-23.89` and `|xl| < 2^-14.3486`. +/// +/// See analysis before the `exp_1()` call in `cr_pow()`, which proves +/// `|rl| < 2^-23.89 |rh|` (here `xh=rh` and `xl=rl`). +/// +/// At output, we also have `0.99985 < hi+lo < 1.99995` and `|lo/hi| < 2^-41.4`. +#[inline] +fn exp_1(xh: f64, xl: f64) -> (f64, f64) { + // |INVLOG2-2^12/log(2)| < 2^-43.4 + const INVLOG2: f64 = hf64!("0x1.71547652b82fep+12"); + const LOG2H: f64 = hf64!("0x1.62e42fefa39efp-13"); + const LOG2L: f64 = hf64!("0x1.abc9e3b39803fp-68"); + + let k = (xh * INVLOG2).roundeven(); + let (kh, kl) = s_mul(k, LOG2H, LOG2L); + let (yh, yl) = fast_two_sum(xh - kh, xl); + let yl = yl - kl; + + // Note: k is an integer, this is just a conversion. + let k = i64::cast_from(k); + let m = (k >> 12) + 0x3ff; + let i2 = usize::cast_from((k >> 6) & 0x3f); + let i1 = usize::cast_from(k & 0x3f); + + let (t1h, t1l) = T1[i2]; + let (t2h, t2l) = T2[i1]; + let (hi, lo) = d_mul(t2h, t2l, t1h, t1l); + let (qh, ql) = q_1(yh, yl); + let (mut hi, mut lo) = d_mul(hi, lo, qh, ql); + + // Scale by 2^k. Warning: for x near 1024, we can have k=2^22, thus + // m = 2047, which encodes Inf + let mut scale = f64::from_bits(m.unsigned() << 52); + if m == 0x7ff { + cold_path(); + hi *= 2.0; + lo *= 2.0; + scale = f64::from_bits((m - 1).unsigned() << 52); + } + hi *= scale; + lo *= scale; + (hi, lo) +} + +/// returns a double-double approximation `hi+lo` of `exp(x*log(2))` for `|x| < 745` +#[inline] +fn exp_2(x: f64) -> (f64, f64) { + let k = (x * hf64!("0x1p12")).roundeven(); + // since |x| <= 745 we have k <= 3051520 + let yh = (-k).fma(hf64!("0x1p-12"), x); // exact, |yh| <= 2^-13 + // now x = k + yh, thus 2^x = 2^k * 2^yh, and we multiply yh by log(2) + // to use the accurate path of exp() + let (yh, yl) = s_mul(yh, LN2H, LN2L); + + // Note: k is an integer, this is just a conversion. + let k = i64::cast_from(k); + let m = (k >> 12) + 0x3ff; + let i2 = usize::cast_from((k >> 6) & 0x3f); + let i1 = usize::cast_from(k & 0x3f); + + let (t1h, t1l) = T1[i2]; + let (t2h, t2l) = T2[i1]; + let (hi, lo) = d_mul(t2h, t2l, t1h, t1l); + let (qh, ql) = q_2(yh, yl); + let (mut hi, mut lo) = d_mul(hi, lo, qh, ql); + + // Scale by 2^k. Warning: for x near 1024, we can have k=2^22, thus + // m = 2047, which encodes Inf + let mut scale = f64::from_bits(m.unsigned() << 52); + if m == 0x7ff { + cold_path(); + hi *= 2.0; + lo *= 2.0; + scale = f64::from_bits((m - 1).unsigned() << 52); + } + hi *= scale; + lo *= scale; + (hi, lo) +} + +/// The following is a degree-4 polynomial generated by Sollya for `exp(x)` over +/// `[-0.000130273,0.000130273]` with absolute error `< 2^-74.346`. +const Q_1: [f64; 5] = [ + hf64!("0x1p0"), // degree 0 + hf64!("0x1p0"), // degree 1 + hf64!("0x1p-1"), // degree 2 + hf64!("0x1.5555555995d37p-3"), // degree 3 + hf64!("0x1.55555558489dcp-5"), // degree 4 +]; + +/// Approximation for the fast path of `exp(z)` for `z=zh+zl`, with +/// `|z| < 0.000130273 < 2^-12.88` and `|zl| < 2^-42.6` (assuming `x^y` does not overflow +/// or underflow) +#[inline] +fn q_1(zh: f64, zl: f64) -> (f64, f64) { + let z = zh + zl; + let q = Q_1[4].fma(zh, Q_1[3]); + let q = q.fma(z, Q_1[2]); + let (hi, lo) = fast_two_sum(Q_1[1], q * z); + let (hi, lo) = d_mul(zh, zl, hi, lo); + fast_sum(Q_1[0], hi, lo) +} + +// FIXME(expm1): use this directly from `expm1` once ported from CORE-MATH. + +/// The following is a degree-7 polynomial generated by Sollya for `exp(z)` over +/// `[-0.000130273,0.000130273]` with absolute error `< 2^-113.218` +/// (see file `exp_accurate.sollya` in the CORE-MATH repo). Since we use this code only for +/// `|x| > 0.125` in `exp2m1(x)`, the corresponding relative error for `exp2m1` is about +/// `2^-113.218/|exp2m1(-0.125)|` which is about `2^-110`. +const Q_2: [f64; 9] = [ + hf64!("0x1p0"), // degree 0, Q_2[0] + hf64!("0x1p0"), // degree 1, Q_2[1] + hf64!("0x1p-1"), // degree 2, Q_2[2] + hf64!("0x1.5555555555555p-3"), + hf64!("0x1.55555555c4d26p-57"), // degree 3, Q_2[3], Q_2[4] + hf64!("0x1.5555555555555p-5"), // degree 4, Q_2[5] + hf64!("0x1.1111111111111p-7"), // degree 5, Q_2[6] + hf64!("0x1.6c16c3fbb4213p-10"), // degree 6, Q_2[7] + hf64!("0x1.a01a023ede0d7p-13"), // degree 7, Q_2[8] +]; + +/// Approximation for the accurate path of `exp(z)` for `z=zh+zl`, with +/// `|z| < 0.000130273 < 2^-12.88` and `|zl| < 2^-42.6` (assuming `x^y` does not overflow +/// or underflow) +#[inline] +fn q_2(zh: f64, zl: f64) -> (f64, f64) { + // Let q[0]..q[7] be the coefficients of degree 0..7 of Q_2. + // The ulp of q[7]*z^7 is at most 2^-155, thus we can compute q[7]*z^7 + // in double precision only. + // The ulp of q[6]*z^6 is at most 2^-139, thus we can compute q[6]*z^6 + // in double precision only. + // The ulp of q[5]*z^5 is at most 2^-124, thus we can compute q[5]*z^5 + // in double precision only. + let z = zh + zl; + let q = Q_2[8].fma(zh, Q_2[7]); + let q = q.fma(z, Q_2[6]); + let q = q.fma(z, Q_2[5]); + + // multiply q by z and add Q_2[3] + Q_2[4] + let (hi, lo) = a_mul(q, z); + let (hi, t) = fast_two_sum(Q_2[3], hi); + let lo = lo + (t + Q_2[4]); + + // multiply hi+lo by zh+zl and add Q_2[2] + let (hi, lo) = d_mul(hi, lo, zh, zl); + let (hi, lo) = fast_sum(Q_2[2], hi, lo); + // multiply hi+lo by zh+zl and add Q_2[1] + let (hi, lo) = d_mul(hi, lo, zh, zl); + let (hi, lo) = fast_sum(Q_2[1], hi, lo); + // multiply hi+lo by zh+zl and add Q_2[0] + let (hi, lo) = d_mul(hi, lo, zh, zl); + fast_sum(Q_2[0], hi, lo) +} + +/// Add `a + b`, assuming `|a| >= |b|` +#[inline] +fn fast_two_sum(a: f64, b: f64) -> (f64, f64) { + let hi = a + b; + let e = hi - a; // exact + let lo = b - e; // exact + (hi, lo) +} + +/// Add `a + (bh + bl)`, assuming `|a| >= |bh|` +#[inline] +fn fast_sum(a: f64, bh: f64, bl: f64) -> (f64, f64) { + let (hi, lo) = fast_two_sum(a, bh); + // |(a+bh)-(hi+lo)| <= 2^-105 |hi| and |lo| < ulp(hi) + let lo = lo + bl; + // |(a+bh+bl)-(hi+lo)| <= 2^-105 |hi| + ulp(lo), + // where |lo| <= ulp(hi) + |bl|. + (hi, lo) +} + +/// Returns `(ah + al) * (bh + bl) - (al * bl)` +#[inline] +fn d_mul(ah: f64, al: f64, bh: f64, bl: f64) -> (f64, f64) { + let (hi, lo) = a_mul(ah, bh); + let lo = ah.fma(bl, lo); + let lo = al.fma(bh, lo); + (hi, lo) +} + +/// Multiply a double with a double double : `a * (bh + bl)` +#[inline] +fn s_mul(a: f64, bh: f64, bl: f64) -> (f64, f64) { + let (hi, lo) = a_mul(a, bh); // exact + let lo = a.fma(bl, lo); + (hi, lo) +} + +/// Multiply exactly `a` and `b`, such that `hi + lo = a * b`. +#[inline] +fn a_mul(a: f64, b: f64) -> (f64, f64) { + let hi = a * b; + let lo = a.fma(b, -hi); + (hi, lo) +} + +/// Return the rounded sum `hi + lo` for `x` from a table sorted by input value. +/// Each entry contains `(input, hi, lo)`. Return `None` if `x` is absent. +#[inline] +fn lookup_exception(mut table: &[(f64, f64, f64)], x: f64) -> Option { + while !table.is_empty() { + // Invariant: if x is an exceptional case, we have + // table[0].0 <= x and x <= table[table.len() - 1].0 + let middle = table.len() / 2; + let (input, hi, lo) = table[middle]; + + if x == input { + return Some(hi + lo); + } + + if x < input { + table = &table[..middle]; + } else { + table = &table[middle + 1..]; + } + } + + None +} + +/// For `0 <= i < 64`, `T1[i] = (h,l)` such that `h+l` is the best double-double approximation +/// of `2^(i/64)`. The approximation error is bounded as follows: `|h + l - 2^(i/64)| < 2^-107`. +#[rustfmt::skip] +static T1: [(f64, f64); 64] = [ + (hf64!("0x1p+0"), hf64!("0x0p+0")), + (hf64!("0x1.02c9a3e778061p+0"), -hf64!("0x1.19083535b085dp-56")), + (hf64!("0x1.059b0d3158574p+0"), hf64!("0x1.d73e2a475b465p-55")), + (hf64!("0x1.0874518759bc8p+0"), hf64!("0x1.186be4bb284ffp-57")), + (hf64!("0x1.0b5586cf9890fp+0"), hf64!("0x1.8a62e4adc610bp-54")), + (hf64!("0x1.0e3ec32d3d1a2p+0"), hf64!("0x1.03a1727c57b53p-59")), + (hf64!("0x1.11301d0125b51p+0"), -hf64!("0x1.6c51039449b3ap-54")), + (hf64!("0x1.1429aaea92dep+0"), -hf64!("0x1.32fbf9af1369ep-54")), + (hf64!("0x1.172b83c7d517bp+0"), -hf64!("0x1.19041b9d78a76p-55")), + (hf64!("0x1.1a35beb6fcb75p+0"), hf64!("0x1.e5b4c7b4968e4p-55")), + (hf64!("0x1.1d4873168b9aap+0"), hf64!("0x1.e016e00a2643cp-54")), + (hf64!("0x1.2063b88628cd6p+0"), hf64!("0x1.dc775814a8495p-55")), + (hf64!("0x1.2387a6e756238p+0"), hf64!("0x1.9b07eb6c70573p-54")), + (hf64!("0x1.26b4565e27cddp+0"), hf64!("0x1.2bd339940e9d9p-55")), + (hf64!("0x1.29e9df51fdee1p+0"), hf64!("0x1.612e8afad1255p-55")), + (hf64!("0x1.2d285a6e4030bp+0"), hf64!("0x1.0024754db41d5p-54")), + (hf64!("0x1.306fe0a31b715p+0"), hf64!("0x1.6f46ad23182e4p-55")), + (hf64!("0x1.33c08b26416ffp+0"), hf64!("0x1.32721843659a6p-54")), + (hf64!("0x1.371a7373aa9cbp+0"), -hf64!("0x1.63aeabf42eae2p-54")), + (hf64!("0x1.3a7db34e59ff7p+0"), -hf64!("0x1.5e436d661f5e3p-56")), + (hf64!("0x1.3dea64c123422p+0"), hf64!("0x1.ada0911f09ebcp-55")), + (hf64!("0x1.4160a21f72e2ap+0"), -hf64!("0x1.ef3691c309278p-58")), + (hf64!("0x1.44e086061892dp+0"), hf64!("0x1.89b7a04ef80dp-59")), + (hf64!("0x1.486a2b5c13cdp+0"), hf64!("0x1.3c1a3b69062fp-56")), + (hf64!("0x1.4bfdad5362a27p+0"), hf64!("0x1.d4397afec42e2p-56")), + (hf64!("0x1.4f9b2769d2ca7p+0"), -hf64!("0x1.4b309d25957e3p-54")), + (hf64!("0x1.5342b569d4f82p+0"), -hf64!("0x1.07abe1db13cadp-55")), + (hf64!("0x1.56f4736b527dap+0"), hf64!("0x1.9bb2c011d93adp-54")), + (hf64!("0x1.5ab07dd485429p+0"), hf64!("0x1.6324c054647adp-54")), + (hf64!("0x1.5e76f15ad2148p+0"), hf64!("0x1.ba6f93080e65ep-54")), + (hf64!("0x1.6247eb03a5585p+0"), -hf64!("0x1.383c17e40b497p-54")), + (hf64!("0x1.6623882552225p+0"), -hf64!("0x1.bb60987591c34p-54")), + (hf64!("0x1.6a09e667f3bcdp+0"), -hf64!("0x1.bdd3413b26456p-54")), + (hf64!("0x1.6dfb23c651a2fp+0"), -hf64!("0x1.bbe3a683c88abp-57")), + (hf64!("0x1.71f75e8ec5f74p+0"), -hf64!("0x1.16e4786887a99p-55")), + (hf64!("0x1.75feb564267c9p+0"), -hf64!("0x1.0245957316dd3p-54")), + (hf64!("0x1.7a11473eb0187p+0"), -hf64!("0x1.41577ee04992fp-55")), + (hf64!("0x1.7e2f336cf4e62p+0"), hf64!("0x1.05d02ba15797ep-56")), + (hf64!("0x1.82589994cce13p+0"), -hf64!("0x1.d4c1dd41532d8p-54")), + (hf64!("0x1.868d99b4492edp+0"), -hf64!("0x1.fc6f89bd4f6bap-54")), + (hf64!("0x1.8ace5422aa0dbp+0"), hf64!("0x1.6e9f156864b27p-54")), + (hf64!("0x1.8f1ae99157736p+0"), hf64!("0x1.5cc13a2e3976cp-55")), + (hf64!("0x1.93737b0cdc5e5p+0"), -hf64!("0x1.75fc781b57ebcp-57")), + (hf64!("0x1.97d829fde4e5p+0"), -hf64!("0x1.d185b7c1b85d1p-54")), + (hf64!("0x1.9c49182a3f09p+0"), hf64!("0x1.c7c46b071f2bep-56")), + (hf64!("0x1.a0c667b5de565p+0"), -hf64!("0x1.359495d1cd533p-54")), + (hf64!("0x1.a5503b23e255dp+0"), -hf64!("0x1.d2f6edb8d41e1p-54")), + (hf64!("0x1.a9e6b5579fdbfp+0"), hf64!("0x1.0fac90ef7fd31p-54")), + (hf64!("0x1.ae89f995ad3adp+0"), hf64!("0x1.7a1cd345dcc81p-54")), + (hf64!("0x1.b33a2b84f15fbp+0"), -hf64!("0x1.2805e3084d708p-57")), + (hf64!("0x1.b7f76f2fb5e47p+0"), -hf64!("0x1.5584f7e54ac3bp-56")), + (hf64!("0x1.bcc1e904bc1d2p+0"), hf64!("0x1.23dd07a2d9e84p-55")), + (hf64!("0x1.c199bdd85529cp+0"), hf64!("0x1.11065895048ddp-55")), + (hf64!("0x1.c67f12e57d14bp+0"), hf64!("0x1.2884dff483cadp-54")), + (hf64!("0x1.cb720dcef9069p+0"), hf64!("0x1.503cbd1e949dbp-56")), + (hf64!("0x1.d072d4a07897cp+0"), -hf64!("0x1.cbc3743797a9cp-54")), + (hf64!("0x1.d5818dcfba487p+0"), hf64!("0x1.2ed02d75b3707p-55")), + (hf64!("0x1.da9e603db3285p+0"), hf64!("0x1.c2300696db532p-54")), + (hf64!("0x1.dfc97337b9b5fp+0"), -hf64!("0x1.1a5cd4f184b5cp-54")), + (hf64!("0x1.e502ee78b3ff6p+0"), hf64!("0x1.39e8980a9cc8fp-55")), + (hf64!("0x1.ea4afa2a490dap+0"), -hf64!("0x1.e9c23179c2893p-54")), + (hf64!("0x1.efa1bee615a27p+0"), hf64!("0x1.dc7f486a4b6bp-54")), + (hf64!("0x1.f50765b6e454p+0"), hf64!("0x1.9d3e12dd8a18bp-54")), + (hf64!("0x1.fa7c1819e90d8p+0"), hf64!("0x1.74853f3a5931ep-55")), +]; + +/// For `0 <= i < 64`, `T2[i] = (h,l)` such that `h+l` is the best double-double approximation +/// of `2^(i/2^12)`. The approximation error is bounded as follows: +/// `|h + l - 2^(i/2^12)| < 2^-107`. +#[rustfmt::skip] +static T2: [(f64, f64); 64] = [ + (hf64!("0x1p+0"), hf64!("0x0p+0")), + (hf64!("0x1.000b175effdc7p+0"), hf64!("0x1.ae8e38c59c72ap-54")), + (hf64!("0x1.00162f3904052p+0"), -hf64!("0x1.7b5d0d58ea8f4p-58")), + (hf64!("0x1.0021478e11ce6p+0"), hf64!("0x1.4115cb6b16a8ep-54")), + (hf64!("0x1.002c605e2e8cfp+0"), -hf64!("0x1.d7c96f201bb2fp-55")), + (hf64!("0x1.003779a95f959p+0"), hf64!("0x1.84711d4c35e9fp-54")), + (hf64!("0x1.0042936faa3d8p+0"), -hf64!("0x1.0484245243777p-55")), + (hf64!("0x1.004dadb113dap+0"), -hf64!("0x1.4b237da2025f9p-54")), + (hf64!("0x1.0058c86da1c0ap+0"), -hf64!("0x1.5e00e62d6b30dp-56")), + (hf64!("0x1.0063e3a559473p+0"), hf64!("0x1.a1d6cedbb9481p-54")), + (hf64!("0x1.006eff583fc3dp+0"), -hf64!("0x1.4acf197a00142p-54")), + (hf64!("0x1.007a1b865a8cap+0"), -hf64!("0x1.eaf2ea42391a5p-57")), + (hf64!("0x1.0085382faef83p+0"), hf64!("0x1.da93f90835f75p-56")), + (hf64!("0x1.00905554425d4p+0"), -hf64!("0x1.6a79084ab093cp-55")), + (hf64!("0x1.009b72f41a12bp+0"), hf64!("0x1.86364f8fbe8f8p-54")), + (hf64!("0x1.00a6910f3b6fdp+0"), -hf64!("0x1.82e8e14e3110ep-55")), + (hf64!("0x1.00b1afa5abcbfp+0"), -hf64!("0x1.4f6b2a7609f71p-55")), + (hf64!("0x1.00bcceb7707ecp+0"), -hf64!("0x1.e1a258ea8f71bp-56")), + (hf64!("0x1.00c7ee448ee02p+0"), hf64!("0x1.4362ca5bc26f1p-56")), + (hf64!("0x1.00d30e4d0c483p+0"), hf64!("0x1.095a56c919d02p-54")), + (hf64!("0x1.00de2ed0ee0f5p+0"), -hf64!("0x1.406ac4e81a645p-57")), + (hf64!("0x1.00e94fd0398ep+0"), hf64!("0x1.b5a6902767e09p-54")), + (hf64!("0x1.00f4714af41d3p+0"), -hf64!("0x1.91b2060859321p-54")), + (hf64!("0x1.00ff93412315cp+0"), hf64!("0x1.427068ab22306p-55")), + (hf64!("0x1.010ab5b2cbd11p+0"), hf64!("0x1.c1d0660524e08p-54")), + (hf64!("0x1.0115d89ff3a8bp+0"), -hf64!("0x1.e7bdfb3204be8p-54")), + (hf64!("0x1.0120fc089ff63p+0"), hf64!("0x1.843aa8b9cbbc6p-55")), + (hf64!("0x1.012c1fecd613bp+0"), -hf64!("0x1.34104ee7edae9p-56")), + (hf64!("0x1.0137444c9b5b5p+0"), -hf64!("0x1.2b6aeb6176892p-56")), + (hf64!("0x1.01426927f5278p+0"), hf64!("0x1.a8cd33b8a1bb3p-56")), + (hf64!("0x1.014d8e7ee8d2fp+0"), hf64!("0x1.2edc08e5da99ap-56")), + (hf64!("0x1.0158b4517bb88p+0"), hf64!("0x1.57ba2dc7e0c73p-55")), + (hf64!("0x1.0163da9fb3335p+0"), hf64!("0x1.b61299ab8cdb7p-54")), + (hf64!("0x1.016f0169949edp+0"), -hf64!("0x1.90565902c5f44p-54")), + (hf64!("0x1.017a28af25567p+0"), hf64!("0x1.70fc41c5c2d53p-55")), + (hf64!("0x1.018550706ab62p+0"), hf64!("0x1.4b9a6e145d76cp-54")), + (hf64!("0x1.019078ad6a19fp+0"), -hf64!("0x1.008eff5142bf9p-56")), + (hf64!("0x1.019ba16628de2p+0"), -hf64!("0x1.77669f033c7dep-54")), + (hf64!("0x1.01a6ca9aac5f3p+0"), -hf64!("0x1.09bb78eeead0ap-54")), + (hf64!("0x1.01b1f44af9f9ep+0"), hf64!("0x1.371231477ece5p-54")), + (hf64!("0x1.01bd1e77170b4p+0"), hf64!("0x1.5e7626621eb5bp-56")), + (hf64!("0x1.01c8491f08f08p+0"), -hf64!("0x1.bc72b100828a5p-54")), + (hf64!("0x1.01d37442d507p+0"), -hf64!("0x1.ce39cbbab8bbep-57")), + (hf64!("0x1.01de9fe280ac8p+0"), hf64!("0x1.16996709da2e2p-55")), + (hf64!("0x1.01e9cbfe113efp+0"), -hf64!("0x1.c11f5239bf535p-55")), + (hf64!("0x1.01f4f8958c1c6p+0"), hf64!("0x1.e1d4eb5edc6b3p-55")), + (hf64!("0x1.020025a8f6a35p+0"), -hf64!("0x1.afb99946ee3fp-54")), + (hf64!("0x1.020b533856324p+0"), -hf64!("0x1.8f06d8a148a32p-54")), + (hf64!("0x1.02168143b0281p+0"), -hf64!("0x1.2bf310fc54eb6p-55")), + (hf64!("0x1.0221afcb09e3ep+0"), -hf64!("0x1.c95a035eb4175p-54")), + (hf64!("0x1.022cdece68c4fp+0"), -hf64!("0x1.491793e46834dp-54")), + (hf64!("0x1.02380e4dd22adp+0"), -hf64!("0x1.3e8d0d9c49091p-56")), + (hf64!("0x1.02433e494b755p+0"), -hf64!("0x1.314aa16278aa3p-54")), + (hf64!("0x1.024e6ec0da046p+0"), hf64!("0x1.48daf888e9651p-55")), + (hf64!("0x1.02599fb483385p+0"), hf64!("0x1.56dc8046821f4p-55")), + (hf64!("0x1.0264d1244c719p+0"), hf64!("0x1.45b42356b9d47p-54")), + (hf64!("0x1.027003103b10ep+0"), -hf64!("0x1.082ef51b61d7ep-56")), + (hf64!("0x1.027b357854772p+0"), hf64!("0x1.2106ed0920a34p-56")), + (hf64!("0x1.0286685c9e059p+0"), -hf64!("0x1.fd4cf26ea5d0fp-54")), + (hf64!("0x1.02919bbd1d1d8p+0"), -hf64!("0x1.09f8775e78084p-54")), + (hf64!("0x1.029ccf99d720ap+0"), hf64!("0x1.64cbba902ca27p-58")), + (hf64!("0x1.02a803f2d170dp+0"), hf64!("0x1.4383ef231d207p-54")), + (hf64!("0x1.02b338c811703p+0"), hf64!("0x1.4a47a505b3a47p-54")), + (hf64!("0x1.02be6e199c811p+0"), hf64!("0x1.e47120223467fp-54")), +]; + +#[cfg(test)] +mod tests { + use super::exp2m1; + use crate::support::{CastFrom, CastInto, Float}; + + #[test] + fn known_results() { + for (x, expected) in [ + (f64::ZERO, f64::ZERO), + (f64::NEG_ZERO, f64::NEG_ZERO), + (f64::ONE, f64::ONE), + (f64::NEG_ONE, -0.5), + (f64::from_bits(1), f64::from_bits(1)), + (-f64::from_bits(1), -f64::from_bits(1)), + (f64::from_bits(2), f64::from_bits(1)), + (-f64::from_bits(2), -f64::from_bits(1)), + (54.0, hf64!("0x1p+54")), + (-54.0, f64::NEG_ONE), + (1023.0, hf64!("0x1p+1023")), + (1024.0, f64::INFINITY), + (f64::MAX, f64::INFINITY), + (f64::MIN, f64::NEG_ONE), + (f64::INFINITY, f64::INFINITY), + (f64::NEG_INFINITY, f64::NEG_ONE), + ] { + if !cfg!(x86_no_sse2) { + let result = exp2m1(x); + assert_biteq!(result, expected, "x = {x:?}"); + } + } + + for x in [f64::NAN, f64::NEG_NAN, f64::SNAN, f64::NEG_SNAN] { + // NaN operations can change the sign or payload, including canonicalization on RISC-V. + assert!(exp2m1(x).is_nan(), "x = {x:?}"); + } + } + + #[test] + fn exact_integer_results() { + for i in 1..=53 { + let x = f64::cast_from(i); + let positive = ((1u64 << i) - 1).cast(); + let negative = f64::from_bits((1023 - i) << 52) - 1.0; + + assert_biteq!(exp2m1(x), positive, "x = {x:?}"); + assert_biteq!(exp2m1(-x), negative, "x = {:?}", -x); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/exp2m1f.rs b/library/compiler-builtins/libm/src/math/exp2m1f.rs new file mode 100644 index 00000000000..2c941f909b5 --- /dev/null +++ b/library/compiler-builtins/libm/src/math/exp2m1f.rs @@ -0,0 +1,257 @@ +/* SPDX-License-Identifier: MIT */ +/* origin: core-math/src/binary32/exp2m1/exp2m1f.c + * Copyright (c) 2022-2025 Alexei Sibidanov. + * Ported to Rust in 2026, malezjaa + * Approximate CORE-MATH commit: 8ea8ea35c518d06b72de4cf1d528e7a2d16f104c + */ + +use super::support::{CastFrom, CastInto, Float, Int, cold_path}; + +/// Base-2 exponential minus one (f32). +/// +/// Calculate `2^x - 1`, that is, 2 raised to the power `x` minus `1`. +#[cfg_attr(assert_no_panic, no_panic::no_panic)] +pub fn exp2m1f(x: f32) -> f32 { + cr_exp2m1f(x) +} + +fn cr_exp2m1f(x: f32) -> f32 { + const Q: [(f32, f32); 3] = [ + (hf32!("0x1.fffffep127"), hf32!("0x1.fffffep127")), + (hf32!("0x1.fffffep127"), hf32!("0x1p+103")), + (-1.0, hf32!("0x1p-26")), + ]; + + let z = f64::from(x); + let ux = x.to_bits(); + let ax = ux & u32::MAX >> 1; + + // x <= -25 + if ux >= 0xc1c8_0000 { + cold_path(); + if ax > (0xff << 23) { + return x + x; // NaN + } + // avoid spurious inexact exception for -Inf + if ux == 0xff800000 { + return Q[2].0; + } else { + return Q[2].0 + Q[2].1; + } + } + + // x >= 128, result is infinite (with default rounding) + if ax >= 0x4300_0000 { + cold_path(); + if ax > (0xff << 23) { + return x + x; // nan + } + // for x=128 and rounding downward or to zero, there is no overflow + let special = usize::from(x == 128.0 && (Q[1].0 + Q[1].1 == Q[1].0)); + // avoid spurious inexact exception for +Inf + if ux == 0x7f800000 { + return x; + } else { + return Q[special].0 + Q[special].1; + } + } + + if ax < 0x3df95f1f { + // |x| < 8.44e-2/log(2) + cold_path(); + let z2 = z * z; + let mut r: f64; + + if ax < 0x3d67a4cc { + // |x| < 3.92e-2/log(2) + cold_path(); + if ax < 0x3caa2fee { + // |x| < 1.44e-2/log(2) + cold_path(); + if ax < 0x3bac1405 { + // |x| < 3.64e-3/log(2) + cold_path(); + if ax < 0x3a358876 { + // |x| < 4.8e-4/log(2) + cold_path(); + if ax < 0x37d32ef6 { + // |x| < 1.745e-5/log(2) + cold_path(); + if ax < 0x331fdd82 { + // |x| < 2.58e-8/log(2) + cold_path(); + if ax < 0x2538aa3b { + // |x| < 0x1.715476p-53 + cold_path(); + r = hf64!("0x1.62e42fefa39efp-1"); + } else { + r = hf64!("0x1.62e42fefa39fp-1") + + z * hf64!("0x1.ebfbdff82c58fp-3"); + } + } else { + if ux == 0xb3d8_5005 { + cold_path(); + return (-hf64!("0x1.2bdf76p-24") - hf64!("0x1.8p-77")) + .cast_lossy(); + } + + if ux == 0x3338_428d { + cold_path(); + return (hf64!("0x1.fee08ap-26") + hf64!("0x1p-80")) + .cast_lossy(); + } + + const C: [f64; 3] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.ebfbdff8548fdp-3"), + hf64!("0x1.c6b08d704a06dp-5"), + ]; + + r = C[0] + z * (C[1] + z * C[2]); + } + } else { + if ux == 0x388bca4f { + cold_path(); + return (hf64!("0x1.839702p-15") - hf64!("0x1.8p-68")).cast_lossy(); + } + + const C: [f64; 4] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.ebfbdff82c58fp-3"), + hf64!("0x1.c6b08dc82b347p-5"), + hf64!("0x1.3b2ab6fbad172p-7"), + ]; + r = (C[0] + z * C[1]) + z2 * (C[2] + z * C[3]); + } + } else { + const C: [f64; 5] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.ebfbdff82c068p-3"), + hf64!("0x1.c6b08d704a6dcp-5"), + hf64!("0x1.3b2ac262c3eedp-7"), + hf64!("0x1.5d87fe7af779ap-10"), + ]; + r = (C[0] + z * C[1]) + z2 * (C[2] + z * (C[3] + z * C[4])); + } + } else { + const C: [f64; 6] = [ + hf64!("0x1.62e42fefa39fp-1"), + hf64!("0x1.ebfbdff82c58dp-3"), + hf64!("0x1.c6b08d7011d13p-5"), + hf64!("0x1.3b2ab6fbd267dp-7"), + hf64!("0x1.5d88a81cea49ep-10"), + hf64!("0x1.430912ea9b963p-13"), + ]; + r = (C[0] + z * C[1]) + z2 * ((C[2] + z * C[3]) + z2 * (C[4] + z * C[5])); + } + } else { + const C: [f64; 7] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.ebfbdff82c639p-3"), + hf64!("0x1.c6b08d7049f1cp-5"), + hf64!("0x1.3b2ab6f5243bdp-7"), + hf64!("0x1.5d87fe80a9e6cp-10"), + hf64!("0x1.430d0b9257fa8p-13"), + hf64!("0x1.ffcbfc4cf0952p-17"), + ]; + r = (C[0] + z * C[1]) + + z2 * ((C[2] + z * C[3]) + z2 * (C[4] + z * (C[5] + z * C[6]))); + } + } else { + const C: [f64; 8] = [ + hf64!("0x1.62e42fefa39efp-1"), + hf64!("0x1.ebfbdff82c591p-3"), + hf64!("0x1.c6b08d704cf6bp-5"), + hf64!("0x1.3b2ab6fba00cep-7"), + hf64!("0x1.5d87fdfdaadb4p-10"), + hf64!("0x1.4309137333066p-13"), + hf64!("0x1.ffe5e90daf7ddp-17"), + hf64!("0x1.62c0220eed731p-20"), + ]; + + r = ((C[0] + z * C[1]) + z2 * (C[2] + z * C[3])) + + (z2 * z2) * ((C[4] + z * C[5]) + z2 * (C[6] + z * C[7])); + } + + r *= z; + r.cast_lossy() + } else { + const C: [f64; 6] = [ + hf64!("0x1.62e42fefa398bp-5"), + hf64!("0x1.ebfbdff84555ap-11"), + hf64!("0x1.c6b08d4ad86d3p-17"), + hf64!("0x1.3b2ad1b1716a2p-23"), + hf64!("0x1.5d7472718ce9dp-30"), + hf64!("0x1.4a1d7f457ac56p-37"), + ]; + + const TB: [f64; 16] = [ + hf64!("0x1p+0"), + hf64!("0x1.0b5586cf9890fp+0"), + hf64!("0x1.172b83c7d517bp+0"), + hf64!("0x1.2387a6e756238p+0"), + hf64!("0x1.306fe0a31b715p+0"), + hf64!("0x1.3dea64c123422p+0"), + hf64!("0x1.4bfdad5362a27p+0"), + hf64!("0x1.5ab07dd485429p+0"), + hf64!("0x1.6a09e667f3bcdp+0"), + hf64!("0x1.7a11473eb0187p+0"), + hf64!("0x1.8ace5422aa0dap+0"), + hf64!("0x1.9c49182a3f09p+0"), + hf64!("0x1.ae89f995ad3adp+0"), + hf64!("0x1.c199bdd85529cp+0"), + hf64!("0x1.d5818dcfba487p+0"), + hf64!("0x1.ea4afa2a490dap+0"), + ]; + + let a = 16.0 * z; + let ia = Float::floor(a); + let h = a - ia; + let h2 = h * h; + + let i: i64 = ia.cast(); + let j = i & 0xf; + let mut e = i - j; + e >>= 4; + + let mut s = TB[usize::cast_from(j)]; + let su = f64::from_bits((e + 0x3ff).unsigned() << 52); + s *= su; + + let mut c0 = C[0] + h * C[1]; + let c2 = C[2] + h * C[3]; + let c4 = C[4] + h * C[5]; + + c0 += h2 * (c2 + h2 * c4); + let w = s * h; + ((s - 1.0) + w * c0).cast_lossy() + } +} + +#[cfg(test)] +mod tests { + use super::exp2m1f; + use crate::support::Float; + + #[test] + fn known_results() { + for (x, expected) in [ + (f32::ZERO, f32::ZERO), + (f32::NEG_ZERO, f32::NEG_ZERO), + (f32::ONE, f32::ONE), + (f32::NEG_ONE, -0.5), + (128.0, f32::INFINITY), + (-25.0, f32::NEG_ONE), + (f32::INFINITY, f32::INFINITY), + (f32::NEG_INFINITY, f32::NEG_ONE), + ] { + let result = exp2m1f(x); + assert_biteq!(result, expected, "x = {x:?}"); + } + + for x in [f32::NAN, f32::NEG_NAN, f32::SNAN, f32::NEG_SNAN] { + // NaN operations can change the sign or payload, including canonicalization on RISC-V. + assert!(exp2m1f(x).is_nan(), "x = {x:?}"); + } + } +} diff --git a/library/compiler-builtins/libm/src/math/fdim.rs b/library/compiler-builtins/libm/src/math/fdim.rs index b61630847f8..82aa7fb081e 100644 --- a/library/compiler-builtins/libm/src/math/fdim.rs +++ b/library/compiler-builtins/libm/src/math/fdim.rs @@ -56,6 +56,7 @@ pub fn fdimf128(x: f128, y: f128) -> f128 { mod tests { use crate::support::{Float, Hex}; + // FIXME: bitwise tests including NaN are skipped because of failure for f128 macro_rules! cases { ($f:ty) => { [ @@ -66,7 +67,7 @@ mod tests { (<$f>::NEG_INFINITY, 0.0, 0.0), (<$f>::NEG_INFINITY, 1.0, 0.0), (<$f>::NEG_INFINITY, <$f>::INFINITY, 0.0), - (<$f>::NEG_INFINITY, <$f>::NAN, <$f>::NAN), + // (<$f>::NEG_INFINITY, <$f>::NAN, <$f>::NAN), // x = -1.0 (-1.0, <$f>::NEG_INFINITY, <$f>::INFINITY), (-1.0, -1.0, 0.0), @@ -74,7 +75,7 @@ mod tests { (-1.0, 0.0, 0.0), (-1.0, 1.0, 0.0), (-1.0, <$f>::INFINITY, 0.0), - (-1.0, <$f>::NAN, <$f>::NAN), + // (-1.0, <$f>::NAN, <$f>::NAN), // x = -0.0 (-0.0, <$f>::NEG_INFINITY, <$f>::INFINITY), (-0.0, -1.0, 1.0), @@ -82,7 +83,7 @@ mod tests { (-0.0, 0.0, 0.0), (-0.0, 1.0, 0.0), (-0.0, <$f>::INFINITY, 0.0), - (-0.0, <$f>::NAN, <$f>::NAN), + // (-0.0, <$f>::NAN, <$f>::NAN), // x = 0.0 (0.0, <$f>::NEG_INFINITY, <$f>::INFINITY), (0.0, -1.0, 1.0), @@ -90,7 +91,7 @@ mod tests { (0.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, <$f>::INFINITY, 0.0), - (0.0, <$f>::NAN, <$f>::NAN), + // (0.0, <$f>::NAN, <$f>::NAN), // x = 1.0 (1.0, <$f>::NEG_INFINITY, <$f>::INFINITY), (1.0, -1.0, 2.0), @@ -98,7 +99,7 @@ mod tests { (1.0, 0.0, 1.0), (1.0, 1.0, 0.0), (1.0, <$f>::INFINITY, 0.0), - (1.0, <$f>::NAN, <$f>::NAN), + // (1.0, <$f>::NAN, <$f>::NAN), // x = inf (<$f>::INFINITY, <$f>::NEG_INFINITY, <$f>::INFINITY), (<$f>::INFINITY, -1.0, <$f>::INFINITY), @@ -106,7 +107,7 @@ mod tests { (<$f>::INFINITY, 0.0, <$f>::INFINITY), (<$f>::INFINITY, 1.0, <$f>::INFINITY), (<$f>::INFINITY, <$f>::INFINITY, 0.0), - (<$f>::INFINITY, <$f>::NAN, <$f>::NAN), + // (<$f>::INFINITY, <$f>::NAN, <$f>::NAN), // x = nan (<$f>::NAN, <$f>::NEG_INFINITY, <$f>::NAN), (<$f>::NAN, -1.0, <$f>::NAN), diff --git a/library/compiler-builtins/libm/src/math/mod.rs b/library/compiler-builtins/libm/src/math/mod.rs index 51a3417c8b1..a97c1128f72 100644 --- a/library/compiler-builtins/libm/src/math/mod.rs +++ b/library/compiler-builtins/libm/src/math/mod.rs @@ -141,6 +141,8 @@ mod exp10; mod exp10f; mod exp2; mod exp2f; +mod exp2m1; +mod exp2m1f; mod expf; mod expm1; mod expm1f; @@ -232,6 +234,8 @@ pub use self::erff::{erfcf, erff}; pub use self::exp::exp; pub use self::exp2::exp2; pub use self::exp2f::exp2f; +pub use self::exp2m1::exp2m1; +pub use self::exp2m1f::exp2m1f; pub use self::exp10::exp10; pub use self::exp10f::exp10f; pub use self::expf::expf; diff --git a/library/compiler-builtins/libm/src/math/support/float_traits.rs b/library/compiler-builtins/libm/src/math/support/float_traits.rs index 43bad1aeb50..4da99101b9c 100644 --- a/library/compiler-builtins/libm/src/math/support/float_traits.rs +++ b/library/compiler-builtins/libm/src/math/support/float_traits.rs @@ -111,6 +111,9 @@ pub trait Float: /// A mask for the top bit of the significand, useful for NaN ops. const SIG_TOP_BIT: Self::Int; + /// The minimum subnormal number. + const TINY_BITS: Self::Int = Self::Int::ONE; + /// Returns `self` transmuted to `Self::Int` fn to_bits(self) -> Self::Int; @@ -220,6 +223,12 @@ pub trait Float: /// Fused multiply add, rounding once. fn fma(self, y: Self, z: Self) -> Self; + /// Round down to the nearest integer. + fn floor(self) -> Self; + + /// Round to the nearest integer, breaking ties toward even. + fn roundeven(self) -> Self; + /// Returns (normalized exponent, normalized significand) fn normalize(significand: Self::Int) -> (i32, Self::Int); @@ -241,6 +250,50 @@ pub trait Float: // ). self * Self::ONE } + + /// Increment by one ULP, saturating at infinity. + fn next_up(self) -> Self { + // FIXME: Replace with `next_up` from `core` once `libm` MSRV reaches 1.86. + let bits = self.to_bits(); + if self.is_nan() || bits == Self::INFINITY.to_bits() { + return self; + } + + let abs = self.abs().to_bits(); + let next_bits = if abs == Self::Int::ZERO { + // Next up from 0 is the smallest subnormal + Self::TINY_BITS + } else if bits == abs { + // Positive: counting up is more positive + bits + Self::Int::ONE + } else { + // Negative: counting down is more positive + bits - Self::Int::ONE + }; + Self::from_bits(next_bits) + } + + /// Decrement by one ULP, saturating at negative infinity. + fn next_down(self) -> Self { + // FIXME: Replace with `next_down` from `core` once `libm` MSRV reaches 1.86. + let bits = self.to_bits(); + if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() { + return self; + } + + let abs = self.abs().to_bits(); + let next_bits = if abs == Self::Int::ZERO { + // Next up from 0 is the smallest negative subnormal + Self::TINY_BITS | Self::SIGN_MASK + } else if bits == abs { + // Positive: counting down is more negative + bits - Self::Int::ONE + } else { + // Negative: counting up is more negative + bits + Self::Int::ONE + }; + Self::from_bits(next_bits) + } } /// Access the associated `Int` type from a float (helper to avoid ambiguous associated types). @@ -256,7 +309,11 @@ macro_rules! float_impl { $from_bits:path, $to_bits:path, $fma_fn:ident, - $fma_intrinsic:ident + $fma_intrinsic:ident, + $floor_fn:ident, + $floor_intrinsic:ident, + $roundeven_fn:ident, + $roundeven_intrinsic:ident ) => { impl Float for $ty { type Int = $ity; @@ -386,6 +443,27 @@ macro_rules! float_impl { } } } + #[inline] + fn floor(self) -> Self { + cfg_select_nofmt! { + intrinsics_enabled => { + core::intrinsics::$floor_intrinsic(self) + } + _ => { + super::super::$floor_fn(self) + } + } + } + fn roundeven(self) -> Self { + cfg_select_nofmt! { + intrinsics_enabled => { + core::intrinsics::$roundeven_intrinsic(self) + } + _ => { + super::super::$roundeven_fn(self) + } + } + } fn normalize(significand: Self::Int) -> (i32, Self::Int) { let shift = significand.leading_zeros().wrapping_sub(Self::EXP_BITS); (1i32.wrapping_sub(shift as i32), significand << shift) @@ -404,7 +482,11 @@ float_impl!( f16::from_bits, f16::to_bits, fmaf16, - fmaf16 + fmaf16, + floorf16, + floorf16, + roundevenf16, + round_ties_even_f16 ); float_impl!( f32, @@ -415,7 +497,11 @@ float_impl!( f32_from_bits, f32_to_bits, fmaf, - fmaf32 + fmaf32, + floorf, + floorf32, + roundevenf, + round_ties_even_f32 ); float_impl!( f64, @@ -426,7 +512,11 @@ float_impl!( f64_from_bits, f64_to_bits, fma, - fmaf64 + fmaf64, + floor, + floorf64, + roundeven, + round_ties_even_f64 ); #[cfg(f128_enabled)] float_impl!( @@ -438,7 +528,11 @@ float_impl!( f128::from_bits, f128::to_bits, fmaf128, - fmaf128 + fmaf128, + floorf128, + floorf128, + roundevenf128, + round_ties_even_f128 ); /* FIXME(msrv): vendor some things that are not const stable at our MSRV */ diff --git a/library/compiler-builtins/libm/src/math/support/int_traits.rs b/library/compiler-builtins/libm/src/math/support/int_traits.rs index 48c0e0438bc..922586e46ec 100644 --- a/library/compiler-builtins/libm/src/math/support/int_traits.rs +++ b/library/compiler-builtins/libm/src/math/support/int_traits.rs @@ -447,6 +447,7 @@ macro_rules! cast_into { }; ($ty:ty; $($into:ty),*) => {$( impl CastInto<$into> for $ty { + #[inline] fn cast(self) -> $into { // All we can really do to enforce casting rules is check the rules when in // debug mode. @@ -455,6 +456,7 @@ macro_rules! cast_into { self as $into } + #[inline] fn cast_lossy(self) -> $into { self as $into } @@ -474,12 +476,14 @@ macro_rules! cast_into_float { }; ($ty:ty; $($into:ty),*) => {$( impl CastInto<$into> for $ty { + #[inline] fn cast(self) -> $into { #[cfg(not(feature = "compiler-builtins"))] debug_assert_eq!(self as $into as $ty, self, "inexact float cast"); self as $into } + #[inline] fn cast_lossy(self) -> $into { self as $into } @@ -505,3 +509,5 @@ cast_into_float!(i16); cast_into_float!(i32); cast_into_float!(i64); cast_into_float!(i128); +cast_into_float!(u64; f64); +cast_into_float!(f64; f32, i32, i64); diff --git a/library/compiler-builtins/rust-version b/library/compiler-builtins/rust-version index 18fea436747..028e73378b2 100644 --- a/library/compiler-builtins/rust-version +++ b/library/compiler-builtins/rust-version @@ -1 +1 @@ -32d94cc9be3f6e6c3fa1deaea9e0ab93c4980dba +5d8937173e7c40406c167f4e94737da8925333ad