514 Commits

Author SHA1 Message Date
quaternic
bfbe919adf Simplify and optimize fdim (#442)
The cases with NaN arguments can be handled by the same x - y
expression, and this generates much better code: https://godbolt.org/z/f3rnT8jx4.
2025-01-14 01:55:26 +00:00
Trevor Gross
bcd9d8a5c3 Reduce indentation in run.sh using early return 2025-01-13 23:01:48 +00:00
Trevor Gross
fd7a45f7f6 Don't set codegen-units=1 by default in CI
We can set this only for the release profile, there isn't any reason to
have it set for debug tests.
2025-01-13 23:01:44 +00:00
Trevor Gross
13b5bf3959 Add fdimf16 and fdimf128
Use the generic algorithms to provide implementations for these
routines.
2025-01-13 14:04:54 +00:00
Trevor Gross
0f285df716 Add a generic version of fdim 2025-01-13 13:49:46 +00:00
Trevor Gross
23dfe62a9d Format the MPFR manual implementation list 2025-01-13 06:22:25 -05:00
Trevor Gross
23c9f74b58 Disable util and libm-macros for optimized tests
These crates take time building in CI, especially with the release
profile having LTO enabled, but there isn't really any reason to test
them with different features or in release mode. Disable this to save
some CI runtime.
2025-01-13 05:54:43 -05:00
Trevor Gross
b558b365d3 Add truncf16 and truncf128
Use the generic algorithms to provide implementations for these
routines.
2025-01-13 10:12:09 +00:00
Trevor Gross
6ac06a97e5 Add a generic version of trunc
The algorithm is identical for both types, so this is a straightforward
routine to port.
2025-01-13 09:59:20 +00:00
Trevor Gross
dfa694a8e4 Add a utility crate for quick evaluation
Introduce a simple binary that can run arbitrary input against any of
the available implementations (musl, MPFR, our libm). This provides an
easy way to check results, or run specific cases against a debugger.

Examples:

    $ cargo run -p util -- eval libm pow 1.6 2.4
    3.089498284311124
    $ cargo run -p util -- eval mpfr pow 1.6 2.4
    3.089498284311124
    $ cargo run -p util -- eval musl tgamma 1.2344597839132
    0.9097442657960874
    $ cargo run -p util -- eval mpfr tgamma 1.2344597839132
    0.9097442657960874
    $ cargo run -p util -- eval libm tgamma 1.2344597839132
    0.9097442657960871
    $ cargo run -p util -- eval musl sincos 3.1415926535
    (8.979318433952318e-11, -1.0)
2025-01-13 04:43:48 -05:00
Trevor Gross
3de783c40f Enable build-mpfr and build-musl by default
Most users who are developing this crate are likely running on a Unix
system, since there isn't much to test against otherwise. For
convenience, enable the features required to run these tests by default.
2025-01-13 03:53:46 +00:00
Trevor Gross
504616d5a0 Rename the test-multiprecision feature to build-mpfr
Currently the features that control what we test against are
`build-musl` and `test-multiprecision`. I didn't name them very
consistently and there isn't really any reason for that.

Rename `test-multiprecision` to `build-mpfr` to better reflect what it
actually does and to be more consistent with `build-musl`.
2025-01-13 03:25:19 +00:00
Hanna Kruppe
87cc064e35 Introduce arch::aarch64 and use it for rint{,f} 2025-01-12 11:26:09 +01:00
Hanna Kruppe
7defd9b429 Use wasm32 arch intrinsics for rint{,f} 2025-01-12 11:25:51 +01:00
Trevor Gross
b50ea809ff Add a new precision adjustment for i586 exp2f
There was a recent failure from the random tests:

     ---- mp_random_exp2f stdout ----
    Random Mpfr exp2f arg 1/1: 10000 iterations (10000 total) using `LIBM_SEED=fqgMuzs6eqH1VZSEmQpLnThnaIyRUOWe`

    thread 'mp_random_exp2f' panicked at crates/libm-test/tests/multiprecision.rs:41:49:
    called `Result::unwrap()` on an `Err` value:
        input:    (127.97238,) (0x42fff1dc,)
        expected: 3.3383009e38           0x7f7b2556
        actual:   inf                    0x7f800000

    Caused by:
        mismatched infinities

Add an xfail for mismatched infinities on i586.
2025-01-12 03:48:13 -05:00
Trevor Gross
228c716a43 Add a new precision adjustment for i586 rint
`rint` had a couple recent failures from the random tests:

    ---- mp_random_rint stdout ----
    Random Mpfr rint arg 1/1: 10000 iterations (10000 total) using `LIBM_SEED=Fl1f69DaJnwkHN2FeuCXaBFRvJYsPvEY`

    thread 'mp_random_rint' panicked at crates/libm-test/tests/multiprecision.rs:41:49:
    called `Result::unwrap()` on an `Err` value:
        input:    (-849751480.5001163,) (0xc1c95316dc4003d0,)
        expected: -849751481.0           0xc1c95316dc800000
        actual:   -849751480.0           0xc1c95316dc000000

    Caused by:
        ulp 8388608 > 100000

And:

    ---- mp_random_rint stdout ----
    Random Mpfr rint arg 1/1: 10000 iterations (10000 total) using `LIBM_SEED=XN7VCGhX3Wu6Mzn8COvJPITyZlGP7gN7`

    thread 'mp_random_rint' panicked at crates/libm-test/tests/multiprecision.rs:41:49:
    called `Result::unwrap()` on an `Err` value:
        input:    (-12493089.499809155,) (0xc167d4242ffe6fc5,)
        expected: -12493089.0            0xc167d42420000000
        actual:   -12493090.0            0xc167d42440000000

    Caused by:
        ulp 536870912 > 100000

It seems we just implement an incorrect rounding mode. Replace the
existing `rint` override with an xfail if the difference is 0.0 <= ε <=
1.0.
2025-01-12 03:48:13 -05:00
Trevor Gross
5b1c1fca03 Expose C versions of libm functions in the cb crate
`compiler_builtins` exposes an `extern "C"` version of `libm` routines,
so add the same here. There really isn't much to test here (unless we
later add tests against C `libm` suites), but one nice benefit is this
gives us a library with unmangled names that is easy to `objdump`. In
accordance with that, also update `cb` to be a `staticlib`.

Unfortunately this also means we have to remove it from the workspace,
since Cargo doesn't allow setting `panic = "abort"` for a single crate.
2025-01-12 03:29:40 -05:00
Trevor Gross
2912076e50 Always use the same seed for benchmarking
It would be preferable to switch to a different generator, or at least
set the seed within the benchmark, but this is the most straightforward
way to make things simple.
2025-01-11 23:36:11 -05:00
Trevor Gross
e1749bdb6d Add biteq and exp_unbiased to Float
These are two convenience methods. Additionally, add tests for the trait
methods, and an `assert_biteq!` macro to check and print the output.
2025-01-11 22:58:00 -05:00
Trevor Gross
5c94cce6b2 Add a release-checked profile with debug and overflow assertions
A failing debug assertion or overflow without correctly wrapping or
saturating is a bug, but the `debug` profile that has these enabled does
not run enough test cases to hit edge cases that may trigger these. Add
a new `release-checked` profile that enables debug assertions and
overflow checks. This seems to only extend per-function test time by a
few seconds (or around a minute on longer extensive tests), so enable
this as the default on CI.

In order to ensure `no_panic` still gets checked, add a build-only step
to CI.
2025-01-11 20:35:30 -05:00
Trevor Gross
721960c172 Remove ExpInt from Float, always use i32 instead
`ExpInt` is likely to only have performance benefits on 16-bit
platforms, but makes working with the exponent more difficult. It seems
like a worthwhile tradeoff to instead just use `i32`, so do that here.
2025-01-11 19:22:01 -05:00
Trevor Gross
5e13eeca01 Reorder tests in run.sh
I do not believe Cargo separately caches crates with different sets of
features enabled. So, ensuring that tests run with `unstable-intrinsics`
are always grouped should slightly reduce runtime.

As an added benefit, all the debug mode tests run first so initial
feedback is available faster.
2025-01-11 18:48:08 -05:00
Trevor Gross
beb34db83d Split cast into cast and cast_lossy
There is a difference in intent between wishing to cast and truncate the
value, and expecting the input to be within range. To make this clear,
add separate `cast_lossy` and `cast_from_lossy` to indicate what that
truncation is intended, leaving `cast` and `cast_from` to only be casts
that expected not to truncate.

Actually enforcing this at runtime is likely to have a cost, so just
`debug_assert!` that `cast` doesn't truncate.
2025-01-11 18:28:45 -05:00
Trevor Gross
ab1038a32b Use core::arch::wasm functions rather than intrinsics
These wasm functions are available in `core::arch::wasm32` since [1], so
we can use them while avoiding the possibly-recursive `intrinsics::*`
calls (in practice none of those should always lower to libcalls on
wasm, but that is up to LLVM).

Since these require an unstable feature, they are still gated under
`unstable-intrinsics`.

[1]: https://github.com/rust-lang/stdarch/pull/1677
2025-01-11 17:04:29 -05:00
Trevor Gross
a0c03b2537 Add tests against MPFR for remquo and remquof
Rug does not yet expose this function, but it is possible to use the
MPFR bindings directly.
2025-01-10 16:04:16 -05:00
Trevor Gross
2b99475df3 Account for optimization levels other than numbers
The build script currently panics with `opt-level=z` or `opt-level=s`.
Account for this here.
2025-01-07 17:58:54 -05:00
beetrees
76714a5657 Make extensive tests exhaustive if there are enough iterations available 2025-01-07 17:44:06 -05:00
Trevor Gross
0359db23c7 Increase the allowed ULP for tgammaf
Extensive tests report that the precision isn't actually 0:

    ---- mp_extensive_tgammaf ----

        input:    (-0.00063536887,) (0xba268ee2,)
        expected: -1574.4668             0xc4c4cef0
        actual:   -1574.4667             0xc4c4ceef

    Caused by:
        ulp 1 > 0

Update ULP to reflect this. After this change, `tgammaf` extensive tests
pass.
2025-01-06 22:19:49 -05:00
Trevor Gross
f3ad123a09 Replace "intrinsic" config with "arch" config
WASM is the only architecture we use `intrinsics::` for. We probably
don't want to do this for any other architectures since it is better to
use assembly, or work toward getting the functions available in `core`.

To more accurately reflect the relationship between arch and intrinsics,
make wasm32 an `arch` module and call the intrinsics from there.
2025-01-06 20:17:01 -05:00
Trevor Gross
c574145577 Don't use intrinsics abs for f16 and f128 on wasm32
This configuration was duplicated from `fabs` and `fabsf`, but wasm is
unlikely to have an intrinsic lowering for these float types. So, just
always use the generic.
2025-01-06 18:33:24 -05:00
Trevor Gross
b5a217b11c Remove an unused feature = "force-soft-floats" gate 2025-01-06 18:06:38 -05:00
Trevor Gross
ac9fec04ef Switch from using unstable-intrinsics to intrinsics_enabled
Unlike `unstable-intrinsics`, `intrinsics_enabled` gets disabled with
`force-soft-floats` which is what we want here.
2025-01-06 17:28:01 -05:00
Trevor Gross
05b87887f4 Increase the allowed precision for failing tests on i586
These will need to be fixed, for now just xfail them so this doesn't
block better test coverage.
2025-01-06 15:41:39 -05:00
Trevor Gross
a9407010b4 Enable MPFR tests on i586
MPFR does build and run correctly without SSE, but requires
`force-cross` be enabled.
2025-01-06 15:41:39 -05:00
Trevor Gross
a882639e7f ci: Only update the github ref for pull requests
On master, this fetch fails with:

    fatal: refusing to fetch into branch 'refs/heads/master' checked out at '/home/runner/work/libm/libm'

Just skip the command when this shouldn't be needed.
2025-01-06 15:37:16 -05:00
Trevor Gross
449d41a24f Loosen precision on i586 based on new tests 2025-01-06 11:46:01 +00:00
Trevor Gross
4b014bff40 Add an override for failing ceil/floor tests on i586 2025-01-06 11:44:28 +00:00
Trevor Gross
a3363785ea Add domain and edge case tests to musl
This provides an increase in test coverage on platforms that cannot test
against MPFR.
2025-01-06 09:30:41 +00:00
Trevor Gross
6b5e8b20f0 Add test infrastructure for f16 and f128
Update test traits to support `f16` and `f128`, as applicable. Add the
new routines (`fabs` and `copysign` for `f16` and `f128`) to the list of
all operations.
2025-01-06 04:10:51 -05:00
Trevor Gross
42c7ace5ba Add fabsf16, fabsf128, copysignf16, and copysignf128
Use the generic implementations to provide these simple methods.
2025-01-06 04:10:51 -05:00
Trevor Gross
aabb7d9dcc Enable f16 and f128 when creating the API change list
Additionally, read glob output as absoulte paths. This enables the
script to work properly even when invoked from a different directory.
2025-01-06 04:10:51 -05:00
Trevor Gross
f8a184e6af Run extensive tests in CI when relevant files change
Add a CI job with a dynamically calculated matrix that runs extensive
jobs on changed files. This makes use of the new
`function-definitions.json` file to determine which changed files
require full tests for a routine to run.
2025-01-06 08:56:22 +00:00
Trevor Gross
05a1f4a982 Update precision based on failures from extensive tests 2025-01-06 08:22:13 +00:00
Trevor Gross
1bbf8b12da Add extensive and exhaustive tests
Add a generator that will test all inputs for input spaces `u32::MAX` or
smaller (e.g. single-argument `f32` routines).  For anything larger,
still run approximately `u32::MAX` tests, but distribute inputs evenly
across the function domain.

Since we often only want to run one of these tests at a time, this
implementation parallelizes within each test using `rayon`. A custom
test runner is used so a progress bar is possible.

Specific tests must be enabled by setting the `LIBM_EXTENSIVE_TESTS`
environment variable, e.g.

    LIBM_EXTENSIVE_TESTS=all_f16,cos,cosf cargo run ...

Testing on a recent machine, most tests take about two minutes or less.
The Bessel functions are quite slow and take closer to 10 minutes, and
FMA is increased to run for about the same.
2025-01-06 08:22:13 +00:00
Trevor Gross
7c04b1916a Add more detailed definition output for update-api-list.py
Update the script to produce, in addition to the simple text list, a
JSON file listing routine names, the types they work with, and the
source files that contain a function with the routine name. This gets
consumed by another script and will be used to determine which extensive
CI jobs to run.
2025-01-06 02:25:59 -05:00
Trevor Gross
f453f5e5b5 Add tests against MPFR for ilogb and ilogbf 2025-01-06 01:29:22 -05:00
Trevor Gross
206bf326d5 Increase the precision for jn and jnf
New random seeds seem to indicate that this test does have some more
failures, this is a recent failure on i586:

    ---- musl_random_jnf stdout ----
    Random Musl jnf arg 1/2: 100 iterations (10000 total) using `LIBM_SEED=nLfzQ3U1OBVvqWaMBcto84UTMsC5FIaC`
    Random Musl jnf arg 2/2: 100 iterations (10000 total) using `LIBM_SEED=nLfzQ3U1OBVvqWaMBcto84UTMsC5FIaC`

    thread 'musl_random_jnf' panicked at crates/libm-test/tests/compare_built_musl.rs:43:51:
    called `Result::unwrap()` on an `Err` value:
        input:    (205, 5497.891) (0x000000cd, 0x45abcf21)
        expected: 7.3291517e-6           0x36f5ecef
        actual:   7.331668e-6            0x36f6028c

    Caused by:
        ulp 5533 > 4000

It seems unlikely that `jn` would somehow have better precision than
`j0`/`j1`, so just use the same precision.
2025-01-05 23:37:12 -05:00
Trevor Gross
ea21b6045f Rename unstable-test-support to unstable-public-internals
The `support` module that this feature makes public will be useful for
implementations in `compiler-builtins`, not only for testing. Give this
feature a more accurate name.
2025-01-05 22:13:11 -05:00
Trevor Gross
cc6a84bf43 Update precision based on new test results 2025-01-06 01:51:37 +00:00
Trevor Gross
37dbc534cb Rewrite the random test generator
Currently, all inputs are generated and then cached. This works
reasonably well but it isn't very configurable or extensible (adding
`f16` and `f128` is awkward).

Replace this with a trait for generating random sequences of tuples.
This also removes possible storage limitations of caching all inputs.
2025-01-06 00:32:21 +00:00