about summary refs log tree commit diff
path: root/library/compiler-builtins/libm
AgeCommit message (Collapse)AuthorLines
2025-09-21Mark float intrinsics with no preconditions as safeltdk-1/+1
2025-08-19Fix some typosStefan Schindler-1/+1
2025-08-05configure: Use `CARGO_CFG_*_{F16,F128}` rather than invoking rustcTrevor Gross-24/+6
Currently we run the `rustc` from the `RUSTC` environment variable to figure out whether or not to enable `f16` and `f128`, based on the `target_has_reliable_{f16,f128}` config. However, this does not know about the codegen backend used, and the backend isn't trivial to check in a build script (usually it gets set via `RUSTFLAGS`). It turns out we don't actually need to run `rustc` here: Cargo unconditionally emits all config from the relevant compiler as `CARGO_CFG_*` variables, regardless of whether or not they are known options. Switch to checking these for setting config rather than invoking `rustc`. As an added advantage, this will work with target.json files without any special handling. Fixes: ed17b95715dd ("Use the compiler to determine whether or not to enable `f16` and `f128`")
2025-07-30Simplify the configuration for no-panicTrevor Gross-174/+174
Currently, attributes for `no-panic` are gated behind both the `test` config and `assert_no_panic`, because `no-panic` is a dev dependency (so only available with test configuration). However, we only emit `assert_no_panic` when the test config is also set anyway, so there isn't any need to gate on both. Replace gates on `all(test, assert_no_panic)` with only `assert_no_panic`. This is simpler, and also has the benefit that attempting to check for panics without `--test` errors.
2025-07-27Implement `floor` and `ceil` in assembly on `i586`Folkert de Vries-30/+55
Fixes: https://github.com/rust-lang/compiler-builtins/issues/837 The assembly is based on - https://github.com/NetBSD/src/blob/20433927938987dd64c8f6aa46904b7aca3fa39e/lib/libm/arch/i387/s_floor.S - https://github.com/NetBSD/src/blob/20433927938987dd64c8f6aa46904b7aca3fa39e/lib/libm/arch/i387/s_ceil.S Which both state /* * Written by J.T. Conklin <jtc@NetBSD.org>. * Public domain. */ Which I believe means we're good in terms of licensing.
2025-07-27Avoid inlining `floor` into `rem_pio2`quaternic-1/+9
Possible workaround for https://github.com/rust-lang/compiler-builtins/pull/976#issuecomment-3085530354 Inline assembly in the body of a function currently causes the compiler to consider that function possibly unwinding, even if said asm originated from inlining an `extern "C"` function. This patch wraps the problematic callsite with `#[inline(never)]`.
2025-07-26libm: Update for new warn-by-default clippy lintsTrevor Gross-1/+4
Silence the approximate constant lint because it is noisy and not always correct. `single_component_path_imports` is also not accurate when built as part of `compiler-builtins`, so that needs to be `allow`ed as well.
2025-07-24Use `x86_no_sse` configuration in more placesTrevor Gross-1/+1
Emit `x86_no_sse` in the compiler-builtins (and builtins-test) build script, and use it to simplify `all(target_arch = "x86", not(target_fefature = "sse))` configuration.
2025-07-24Enable tests that were skipped on PowerPCTrevor Gross-2/+1
Most of these were skipped because of a bug with the platform implementation, or some kind of crash unwinding. Since the upgrade to Ubuntu 25.04, these all seem to be resolved with the exception of a bug in the host `__floatundisf` [1]. [1] https://github.com/rust-lang/compiler-builtins/pull/384#issuecomment-740413334
2025-07-17Allow a new lint failure in nightlyTrevor Gross-0/+2
```text warning: function `f32_to_bits` is never used --> libm/src/math/support/float_traits.rs:367:14 | 367 | pub const fn f32_to_bits(x: f32) -> u32 { | ^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: function `f64_to_bits` is never used --> libm/src/math/support/float_traits.rs:381:14 | 381 | pub const fn f64_to_bits(x: f64) -> u64 { | ^^^^^^^^^^^ warning: `libm` (lib) generated 2 warnings ``` This is a false positive, see RUST-144060.
2025-07-04Test building custom targets and resolve an issue probing `rustc`Trevor Gross-3/+10
The `rustc` probe done in our build scripts needs to pass `--target` to get the correct configuration, which usually comes from the `TARGET` environment variable. However, for targets specified via a `target.json` file, `TARGET` gets set to the file name without an extension or path. `rustc` will check a search path to attempt to locate the file, but this is likely to fail since the directory where Cargo invokes build scripts (and hence where those scripts invoke `rustc`) might not have any relation to the JSON spec file. Resolve this for now by leaving `f16` and `f128` disabled if the `rustc` command fails. Result of the discussion at CARGO-14208 may eventually provide a better solution. A CI test is also added since custom JSON files are an edge case that could fail in other ways. I verified this fails without the fix here. The JSON file is the output for `thumbv7em-none-eabi`, just renamed so `rustc` doesn't identify it.
2025-07-02Use the compiler to determine whether or not to enable `f16` and `f128`Trevor Gross-52/+29
Currently we whether or not to build and test `f16` and `f128` support mostly based on the target triple. This isn't always accurate, however, since support also varies by backend and the backend version. Since recently, `rustc` is aware of this with the unstable config option `target_has_reliable_{f16,f128}`, which better represents when the types are actually expected to be available and usable. Switch our compiler-builtins and libm configuration to use this by probing `rustc` for the target's settings. A few small `cfg` fixes are needed with this.
2025-07-01libm: Improved integer utilities, implement shifts and bug fixes for i256 ↵quaternic-57/+162
and u256 `i256` and `u256` - operators now use the same overflow convention as primitives - implement `<<` and `-` (previously just `>>` and `+`) - implement `Ord` correctly (the previous `PartialOrd` was broken) - correct `i256::SIGNED` to `true` The `Int`-trait is extended with `trailing_zeros`, `carrying_add`, and `borrowing_sub`.
2025-06-13Clean up and sort manifest keysTrevor Gross-6/+4
Use a consistent ordering for top-level manifest keys, and remove those that are now redundant (`homapage` isn't supposed to be the same as `repository`, and `documentation` automatically points to docs.rs now).
2025-06-13fmaximum,fminimum: Fix incorrect result and add testsTrevor Gross-57/+392
After adding tests, the current implementation for fminimum fails when provided a negative zero and NaN as inputs: ---- math::fminimum_fmaximum_num::tests::fmaximum_num_spec_tests_f64 stdout ---- thread 'math::fminimum_fmaximum_num::tests::fmaximum_num_spec_tests_f64' panicked at libm/src/math/fminimum_fmaximum_num.rs:240:13: fmaximum_num(-0x0p+0, NaN) l: NaN (0x7ff8000000000000) r: -0.0 (0x8000000000000000) ---- math::fminimum_fmaximum_num::tests::fmaximum_num_spec_tests_f32 stdout ---- thread 'math::fminimum_fmaximum_num::tests::fmaximum_num_spec_tests_f32' panicked at libm/src/math/fminimum_fmaximum_num.rs:240:13: fmaximum_num(-0x0p+0, NaN) l: NaN (0x7fc00000) r: -0.0 (0x80000000) Add more thorough spec tests for these functions and correct the implementations. Canonicalization is also moved to a trait method to centralize documentation about what it does and doesn't do.
2025-06-02libm-test: Fix unintentional skips in `binop_common`Trevor Gross-1/+9
`binop_common` emits a `SKIP` that is intended to apply only to `copysign`, but is instead applying to all binary operators. Correct the general case but leave the currently-failing `maximum_num` tests as a FIXME, to be resolved separately in [1]. Also simplify skip logic and NaN checking, and add a few more `copysign` checks. [1]: https://github.com/rust-lang/compiler-builtins/pull/939
2025-06-02cleanup: Use `x.biteq(y)` rather than `x.to_bits() == y.to_bits()`Trevor Gross-14/+12
2025-06-01Fix new `dead_code` warnings from recent nightliesTrevor Gross-103/+116
2025-05-29Reuse `libm`'s `Caat` and `CastFrom` in `compiler-builtins`Trevor Gross-0/+5
2025-05-29cleanup: Reuse `MinInt` and `Int` from `libm` in `compiler-builtins`Trevor Gross-0/+9
Since the two crates are now in the same repo, it is easier to share code. Begin some deduplication with the integer traits.
2025-05-28aarch64: Add a note saying why we use `frintx` rather than `frintn`Trevor Gross-0/+6
2025-05-28Typo in README.mdDario Damiani-1/+1
Link to Apache License changed from htps:// to https://
2025-05-28Update `CmpResult` to use a pointer-sized return typeTrevor Gross-0/+2
As seen at [1], LLVM uses `long long` on LLP64 (to get a 64-bit integer matching pointer size) and `long` on everything else, with exceptions for AArch64 and AVR. Our current logic always uses an `i32`. This happens to work because LLVM uses 32-bit instructions to check the output on x86-64, but the GCC checks the full 64-bit register so garbage in the upper half leads to incorrect results. Update our return type to be `isize`, with exceptions for AArch64 and AVR. Fixes: https://github.com/rust-lang/compiler-builtins/issues/919 [1]: https://github.com/llvm/llvm-project/blob/0cf3c437c18ed27d9663d87804a9a15ff6874af2/compiler-rt/lib/builtins/fp_compare_impl.inc#L11-L27
2025-05-22libm: Clean up unused filesTrevor Gross-310/+0
These were deleted during refactoring in 0a2dc5d9 ("Combine the source files for more generic implementations") but got added back by accident in 54bac411 ("refactor: Move the libm crate to a subdirectory"). Remove them again here.
2025-05-13Fix `i256::MAX`Tobias Decking-1/+1
2025-05-06chore: releasegithub-actions[bot]-1/+7
2025-05-06Require `target_has_atomic = "ptr"` for runtime feature detectionTrevor Gross-6/+15
The `feature_detect` module is currently being built on all targets, but the use of `AtomicU32` causes a problem if atomics are not available (such as with `bpfel-unknown-none`). Gate this module behind `target_has_atomic = "ptr"`. The below now completes successfully: cargo build -p compiler_builtins --target=bpfel-unknown-none -Z build-std=core Fixes: https://github.com/rust-lang/compiler-builtins/issues/908
2025-05-05Replace `super::super` with `crate::support` where possibleTrevor Gross-22/+19
Since `crate::support` now works in both `compiler-builtins` and `libm`, we can get rid of some of these unusual paths.
2025-05-04chore: releasegithub-actions[bot]-1/+7
2025-05-03Use runtime feature detection for fma routines on x86Trevor Gross-3/+586
Get performance closer to the glibc implementations by adding assembly fma routines, with runtime feature detection so they are used even if not compiled with `+fma` (as the distributed standard library is often not). Glibc uses ifuncs, this implementation stores a function pointer in an atomic. Results of CPU flags are also cached in order to avoid repeating the startup time in calls to different functions. The feature detection code is a slightly simplified version of `std-detect`. Musl sources were used as a reference [1]. Fixes: https://github.com/rust-lang/rust/issues/140452 once synced [1]: https://github.com/bminor/musl/blob/c47ad25ea3b484e10326f933e927c0bc8cded3da/src/math/x32/fma.c
2025-05-03Rename the i686 module to x86Trevor Gross-2/+2
This module is used for both i686 and x86-64.
2025-04-29Refactor the fma modulesTrevor Gross-174/+178
Move implementations to `generic/` like the other functions. This also allows us to combine the `fma` and `fma_wide` modules.
2025-04-29Move `fma` implementations to `mod generic`Trevor Gross-0/+0
This will not build correctly, the move is done as a separate step from the rest of refactoring so git's history is cleaner.
2025-04-29Resolve `unnecessary_transmutes` lintsTrevor Gross-9/+8
These appeared in a later nightly. In compiler-builtins we can apply the suggestion, but in `libm` we need to ignore them since `fx::from_bits` is not `const` at the MSRV. `clippy::uninlined_format_args` also seems to have gotten stricter, so fix those here.
2025-04-22chore: Release libm v0.2.13Trevor Gross-1/+7
2025-04-22chore: release builtins 0.1.156 and libm 0.2.12github-actions[bot]-1/+37
2025-04-22Reimplement the generic fmodquaternic-66/+55
2025-04-21Update README and CONTRIBUTING for the new repository layoutTrevor Gross-35/+21
2025-04-21Move README.md to compiler-builtins, CONTRIBUTING.md to rootTrevor Gross-82/+0
Do this in a commit so git tracks the move; a new README will be introduced in the root, and CONTRIBUTING will be updated to apply to both crates.
2025-04-19Run `cargo fmt` on all projectsTrevor Gross-95/+490
Apply the same formatting rules to both `libm` and `compiler-builtins`.
2025-04-19Add a .rustfmt.toml with style edition 2024Trevor Gross-5/+0
Use the 2024 style edition for all crates and enable import sorting. 2024 already applies some smaller heuristics that look good in compiler-builtins, I have dropped `use_small_heuristics` that was set in `libm` because it seems to negatively affect the readibility of anything working with numbers (e.g. collapsing multiple small `if` expressions into a single line).
2025-04-19libm: Flatten the `libm/libm` directoryTrevor Gross-0/+0
2025-04-19libm: Reorganize into compiler-builtinsTrevor Gross-12530/+314
Distribute everything from `libm/` to better locations in the repo. `libm/libm/*` has not moved yet to avoid Git seeing the move as an edit to `Cargo.toml`. Files that remain to be merged somehow are in `etc/libm`.
2025-04-19libm: Remove compiler-builtins-smoke-testTrevor Gross-245/+0
Since `libm` is now part of the `compiler-builtins` repo, the crate to test that they work together is no longer needed.
2025-04-19Update .git-blame-ignore-revs after the libm mergeTrevor Gross-5/+0
2025-04-19Update submodules after the `libm` mergeTrevor Gross-4/+0
2025-04-18Migrate all crates except `libm` to edition 2024Trevor Gross-30/+34
Unfortunately this means we lose use of the convenient name `gen`, so this includes a handful of renaming. We can't increase the edition for `libm` yet due to MSRV, but we can enable `unsafe_op_in_unsafe_fn` to help make that change smoother in the future.
2025-04-18refactor: Introduce a virtual manifestTrevor Gross-302/+305
Move the workspace configuration to a virtual manifest. This reorganization makes a more clear separation between package contents and support files that don't get distributed. It will also make it easier to merge this repository with `compiler-builtins` which is planned (builtins had a similar update done in [1]). LICENSE.txt and README.md are symlinkedinto the new directory to ensure they get included in the package. [1]: https://github.com/rust-lang/compiler-builtins/pull/702
2025-04-18refactor: Move the `libm` crate to a subdirectoryTrevor Gross-23/+334
In preparation for switching to a virtual manifest, move the `libm` crate into a subdirectory and update paths to match. Updating `Cargo.toml` is done in the next commit so git tracks the moved file correctly.
2025-04-18Mark generic functions `#[inline]`Trevor Gross-2/+27
Benchmarks for [1] seemed to indicate that repository organization for some reason had an effect on performance, even though the exact same rustc commands were running (though some with a different order). After investigating more, it appears that dependencies may have an affect on inlining thresholds for generic functions. It is surprising that this happens, we more or less expect that public functions will be standalone but everything they call will be inlined. To help ensure this, mark all generic functions `#[inline]` if they should be merged into the public function. Zulip discussion at [2]. [1]: https://github.com/rust-lang/libm/pull/533 [2]: https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Dependencies.20affecting.20codegen/with/513079387