2018-02-26 11:07:16 -06:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2017-10-22 22:01:00 -05:00
|
|
|
|
2020-09-02 05:18:07 -05:00
|
|
|
use std::mem::MaybeUninit;
|
2017-10-22 22:01:00 -05:00
|
|
|
use std::str;
|
|
|
|
|
|
|
|
use core::num::flt2dec::strategy::grisu::format_exact_opt;
|
|
|
|
use core::num::flt2dec::strategy::grisu::format_shortest_opt;
|
2019-12-06 22:18:12 -06:00
|
|
|
use core::num::flt2dec::MAX_SIG_DIGITS;
|
|
|
|
use core::num::flt2dec::{decode, DecodableFloat, Decoded, FullDecoded};
|
2017-10-22 22:01:00 -05:00
|
|
|
|
2018-08-04 17:24:39 -05:00
|
|
|
use rand::distributions::{Distribution, Uniform};
|
2019-12-06 22:18:12 -06:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::SeedableRng;
|
2018-02-26 11:07:16 -06:00
|
|
|
|
2017-10-22 22:01:00 -05:00
|
|
|
pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
|
|
|
|
match decode(v).1 {
|
|
|
|
FullDecoded::Finite(decoded) => decoded,
|
2022-02-12 13:16:17 -06:00
|
|
|
full_decoded => panic!("expected finite, got {full_decoded:?} instead"),
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iterate<F, G, V>(func: &str, k: usize, n: usize, mut f: F, mut g: G, mut v: V) -> (usize, usize)
|
2019-12-06 22:18:12 -06:00
|
|
|
where
|
2020-09-02 05:18:07 -05:00
|
|
|
F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
|
|
|
|
G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
|
2019-12-06 22:18:12 -06:00
|
|
|
V: FnMut(usize) -> Decoded,
|
|
|
|
{
|
2017-10-22 22:01:00 -05:00
|
|
|
assert!(k <= 1024);
|
|
|
|
|
|
|
|
let mut npassed = 0; // f(x) = Some(g(x))
|
|
|
|
let mut nignored = 0; // f(x) = None
|
|
|
|
|
|
|
|
for i in 0..n {
|
|
|
|
if (i & 0xfffff) == 0 {
|
2019-12-06 22:18:12 -06:00
|
|
|
println!(
|
|
|
|
"in progress, {:x}/{:x} (ignored={} passed={} failed={})",
|
|
|
|
i,
|
|
|
|
n,
|
|
|
|
nignored,
|
|
|
|
npassed,
|
|
|
|
i - nignored - npassed
|
|
|
|
);
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let decoded = v(i);
|
2020-09-02 05:18:07 -05:00
|
|
|
let mut buf1 = [MaybeUninit::new(0); 1024];
|
|
|
|
if let Some((buf1, e1)) = f(&decoded, &mut buf1[..k]) {
|
|
|
|
let mut buf2 = [MaybeUninit::new(0); 1024];
|
|
|
|
let (buf2, e2) = g(&decoded, &mut buf2[..k]);
|
|
|
|
if e1 == e2 && buf1 == buf2 {
|
2017-10-22 22:01:00 -05:00
|
|
|
npassed += 1;
|
|
|
|
} else {
|
2019-12-06 22:18:12 -06:00
|
|
|
println!(
|
|
|
|
"equivalence test failed, {:x}/{:x}: {:?} f(i)={}e{} g(i)={}e{}",
|
|
|
|
i,
|
|
|
|
n,
|
|
|
|
decoded,
|
2020-09-02 05:18:07 -05:00
|
|
|
str::from_utf8(buf1).unwrap(),
|
2019-12-06 22:18:12 -06:00
|
|
|
e1,
|
2020-09-02 05:18:07 -05:00
|
|
|
str::from_utf8(buf2).unwrap(),
|
2019-12-06 22:18:12 -06:00
|
|
|
e2
|
|
|
|
);
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nignored += 1;
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 22:18:12 -06:00
|
|
|
println!(
|
|
|
|
"{}({}): done, ignored={} passed={} failed={}",
|
|
|
|
func,
|
|
|
|
k,
|
|
|
|
nignored,
|
|
|
|
npassed,
|
|
|
|
n - nignored - npassed
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
nignored + npassed == n,
|
|
|
|
"{}({}): {} out of {} values returns an incorrect value!",
|
|
|
|
func,
|
|
|
|
k,
|
|
|
|
n - nignored - npassed,
|
|
|
|
n
|
|
|
|
);
|
2017-10-22 22:01:00 -05:00
|
|
|
(npassed, nignored)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn f32_random_equivalence_test<F, G>(f: F, g: G, k: usize, n: usize)
|
2019-12-06 22:18:12 -06:00
|
|
|
where
|
2020-09-02 05:18:07 -05:00
|
|
|
F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
|
|
|
|
G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
|
2019-12-06 22:18:12 -06:00
|
|
|
{
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 23:52:50 -06:00
|
|
|
if cfg!(target_os = "emscripten") {
|
2019-12-06 22:18:12 -06:00
|
|
|
return; // using rng pulls in i128 support, which doesn't work
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 23:52:50 -06:00
|
|
|
}
|
2019-08-04 07:27:48 -05:00
|
|
|
let mut rng = StdRng::from_entropy();
|
2018-08-04 17:24:39 -05:00
|
|
|
let f32_range = Uniform::new(0x0000_0001u32, 0x7f80_0000);
|
2017-10-22 22:01:00 -05:00
|
|
|
iterate("f32_random_equivalence_test", k, n, f, g, |_| {
|
2018-08-04 17:24:39 -05:00
|
|
|
let x = f32::from_bits(f32_range.sample(&mut rng));
|
2017-10-22 22:01:00 -05:00
|
|
|
decode_finite(x)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn f64_random_equivalence_test<F, G>(f: F, g: G, k: usize, n: usize)
|
2019-12-06 22:18:12 -06:00
|
|
|
where
|
2020-09-02 05:18:07 -05:00
|
|
|
F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
|
|
|
|
G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
|
2019-12-06 22:18:12 -06:00
|
|
|
{
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 23:52:50 -06:00
|
|
|
if cfg!(target_os = "emscripten") {
|
2019-12-06 22:18:12 -06:00
|
|
|
return; // using rng pulls in i128 support, which doesn't work
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-19 23:52:50 -06:00
|
|
|
}
|
2019-08-04 07:27:48 -05:00
|
|
|
let mut rng = StdRng::from_entropy();
|
2018-08-04 17:24:39 -05:00
|
|
|
let f64_range = Uniform::new(0x0000_0000_0000_0001u64, 0x7ff0_0000_0000_0000);
|
2017-10-22 22:01:00 -05:00
|
|
|
iterate("f64_random_equivalence_test", k, n, f, g, |_| {
|
2018-08-04 17:24:39 -05:00
|
|
|
let x = f64::from_bits(f64_range.sample(&mut rng));
|
2017-10-22 22:01:00 -05:00
|
|
|
decode_finite(x)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn f32_exhaustive_equivalence_test<F, G>(f: F, g: G, k: usize)
|
2019-12-06 22:18:12 -06:00
|
|
|
where
|
2020-09-02 05:18:07 -05:00
|
|
|
F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
|
|
|
|
G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
|
2019-12-06 22:18:12 -06:00
|
|
|
{
|
2017-10-22 22:01:00 -05:00
|
|
|
// we have only 2^23 * (2^8 - 1) - 1 = 2,139,095,039 positive finite f32 values,
|
|
|
|
// so why not simply testing all of them?
|
|
|
|
//
|
|
|
|
// this is of course very stressful (and thus should be behind an `#[ignore]` attribute),
|
|
|
|
// but with `-C opt-level=3 -C lto` this only takes about an hour or so.
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// iterate from 0x0000_0001 to 0x7f7f_ffff, i.e., all finite ranges
|
2019-12-06 22:18:12 -06:00
|
|
|
let (npassed, nignored) =
|
|
|
|
iterate("f32_exhaustive_equivalence_test", k, 0x7f7f_ffff, f, g, |i: usize| {
|
|
|
|
let x = f32::from_bits(i as u32 + 1);
|
|
|
|
decode_finite(x)
|
|
|
|
});
|
2017-10-22 22:01:00 -05:00
|
|
|
assert_eq!((npassed, nignored), (2121451881, 17643158));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shortest_random_equivalence_test() {
|
|
|
|
use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
|
2020-04-23 12:48:35 -05:00
|
|
|
// Miri is too slow
|
|
|
|
let n = if cfg!(miri) { 10 } else { 10_000 };
|
2019-08-09 03:07:59 -05:00
|
|
|
|
2020-04-23 12:48:35 -05:00
|
|
|
f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, n);
|
|
|
|
f32_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, n);
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
#[test]
|
|
|
|
#[ignore] // it is too expensive
|
2017-10-22 22:01:00 -05:00
|
|
|
fn shortest_f32_exhaustive_equivalence_test() {
|
|
|
|
// it is hard to directly test the optimality of the output, but we can at least test if
|
|
|
|
// two different algorithms agree to each other.
|
|
|
|
//
|
|
|
|
// this reports the progress and the number of f32 values returned `None`.
|
|
|
|
// with `--nocapture` (and plenty of time and appropriate rustc flags), this should print:
|
|
|
|
// `done, ignored=17643158 passed=2121451881 failed=0`.
|
|
|
|
|
|
|
|
use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
|
|
|
|
f32_exhaustive_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS);
|
|
|
|
}
|
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
#[test]
|
|
|
|
#[ignore] // it is too expensive
|
2017-10-22 22:01:00 -05:00
|
|
|
fn shortest_f64_hard_random_equivalence_test() {
|
|
|
|
// this again probably has to use appropriate rustc flags.
|
|
|
|
|
|
|
|
use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
|
2019-12-06 22:18:12 -06:00
|
|
|
f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, 100_000_000);
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exact_f32_random_equivalence_test() {
|
|
|
|
use core::num::flt2dec::strategy::dragon::format_exact as fallback;
|
2020-04-23 12:48:35 -05:00
|
|
|
// Miri is too slow
|
|
|
|
let n = if cfg!(miri) { 3 } else { 1_000 };
|
2019-08-09 03:07:59 -05:00
|
|
|
|
2017-10-22 22:01:00 -05:00
|
|
|
for k in 1..21 {
|
2019-12-06 22:18:12 -06:00
|
|
|
f32_random_equivalence_test(
|
|
|
|
|d, buf| format_exact_opt(d, buf, i16::MIN),
|
|
|
|
|d, buf| fallback(d, buf, i16::MIN),
|
|
|
|
k,
|
2020-04-23 12:48:35 -05:00
|
|
|
n,
|
2019-12-06 22:18:12 -06:00
|
|
|
);
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exact_f64_random_equivalence_test() {
|
|
|
|
use core::num::flt2dec::strategy::dragon::format_exact as fallback;
|
2020-04-23 12:48:35 -05:00
|
|
|
// Miri is too slow
|
2020-07-31 04:52:53 -05:00
|
|
|
let n = if cfg!(miri) { 2 } else { 1_000 };
|
2019-08-09 03:07:59 -05:00
|
|
|
|
2017-10-22 22:01:00 -05:00
|
|
|
for k in 1..21 {
|
2019-12-06 22:18:12 -06:00
|
|
|
f64_random_equivalence_test(
|
|
|
|
|d, buf| format_exact_opt(d, buf, i16::MIN),
|
|
|
|
|d, buf| fallback(d, buf, i16::MIN),
|
|
|
|
k,
|
2020-04-23 12:48:35 -05:00
|
|
|
n,
|
2019-12-06 22:18:12 -06:00
|
|
|
);
|
2017-10-22 22:01:00 -05:00
|
|
|
}
|
|
|
|
}
|