about summary refs log tree commit diff
path: root/src/libcore/tests/num
AgeCommit message (Collapse)AuthorLines
2020-07-27mv std libs to library/mark-3477/+0
2020-07-23Rearrange the pipeline of `pow` to gain efficiencyNeutron3529-1/+57
The check of the `exp` parameter seems useless if we execute the while-loop more than once. The original implementation of `pow` function using one more comparison if the `exp==0` and may break the pipeline of the cpu, which may generate a slower code. The performance gap between the old and the new implementation may be small, but IMO, at least the newer one looks more beautiful. --- bench prog: ``` extern crate test; ($a:expr)=>{let time=std::time::Instant::now();{$a;}print!("{:?} ",time.elapsed())}; ($a:expr,$b:literal)=>{let time=std::time::Instant::now();let mut a=0;for _ in 0..$b{a^=$a;}print!("{:?} {} ",time.elapsed(),a)} } pub fn pow_rust(x:i64, mut exp: u32) -> i64 { let mut base = x; let mut acc = 1; while exp > 1 { if (exp & 1) == 1 { acc = acc * base; } exp /= 2; base = base * base; } if exp == 1 { acc = acc * base; } acc } pub fn pow_new(x:i64, mut exp: u32) -> i64 { if exp==0{ 1 }else{ let mut base = x; let mut acc = 1; while exp > 1 { if (exp & 1) == 1 { acc = acc * base; } exp >>= 1; base = base * base; } acc * base } } fn main(){ let a=2i64; let b=1_u32; println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); timing!(test::black_box(a).pow(test::black_box(b)),100000000); timing!(pow_new(test::black_box(a),test::black_box(b)),100000000); timing!(pow_rust(test::black_box(a),test::black_box(b)),100000000); println!(); } ``` bench in my laptop: ``` neutron@Neutron:/me/rust$ rc commit.rs rustc commit.rs && ./commit 3.978419716s 0 4.079765171s 0 3.964630622s 0 3.997127013s 0 4.260304804s 0 3.997638211s 0 3.963195544s 0 4.11657718s 0 4.176054164s 0 3.830128579s 0 3.980396122s 0 3.937258567s 0 3.986055948s 0 4.127804162s 0 4.018943411s 0 4.185568857s 0 4.217512517s 0 3.98313603s 0 3.863018225s 0 4.030447988s 0 3.694878237s 0 4.206987927s 0 4.137608047s 0 4.115564664s 0 neutron@Neutron:/me/rust$ rc commit.rs -O rustc commit.rs -O && ./commit 162.111993ms 0 165.107125ms 0 166.26924ms 0 175.20479ms 0 205.062565ms 0 176.278791ms 0 174.408975ms 0 166.526899ms 0 201.857604ms 0 146.190062ms 0 168.592821ms 0 154.61411ms 0 199.678912ms 0 168.411598ms 0 162.129996ms 0 147.420765ms 0 209.759326ms 0 154.807907ms 0 165.507134ms 0 188.476239ms 0 157.351524ms 0 121.320123ms 0 126.401229ms 0 114.86428ms 0 ``` delete an unnecessary semicolon... Sorry for the typo. delete trailing whitespace Sorry, too.. Sorry for the missing... I checked all the implementations, and finally found that there is one function that does not check whether `exp == 0` add extra tests add extra tests. finished adding the extra tests to prevent further typo add pow(2) to negative exp add whitespace. add whitespace add whitespace delete extra line
2020-06-10Migrate to numeric associated constsLzu Tao-16/+16
2020-04-23libcore: more compact way to adjust test sizes for MiriRalf Jung-21/+13
2020-04-06Use assoc float consts in libcoreLinus Färnstrand-6/+2
2020-04-05Stop importing int/float modules in libcoreLinus Färnstrand-5/+1
2020-02-17Drop unused argument to float functionsMark Rousskov-236/+224
2020-01-12Tests for leading_trailing_onesThom Chiovoloni-0/+54
2019-12-22Format the worldMark Rousskov-576/+709
2019-12-09Rollup merge of #67119 - RalfJung:miri-test-libstd, r=alexcrichtonTyler Mandry-9/+14
libstd miri tests: avoid warnings Ignore tests in a way that all the code still gets compiled, to get rid of all the "unused" warnings that otherwise show up when running the test suite in Miri.
2019-12-07fix warnings with cfg(miri)Ralf Jung-3/+10
2019-12-07libcore: ignore tests in Miri instead of removing them entirelyRalf Jung-6/+4
2019-12-06Format libcore with rustfmt (including tests and benches)David Tolnay-467/+535
2019-11-29Remove unneeded prelude imports in libcore testsDavid Tolnay-3/+0
These three lines are from c82da7a54b9efb1a0ccbe11de66c71f547bf7db9 in 2015. They cause problems when applying rustfmt to the codebase, because reordering wildcard imports can trigger new unused import warnings. As a minimized example, the following program compiles successfully: #![deny(unused_imports)] use std::fmt::Debug; use std::marker::Send; pub mod repro { use std::prelude::v1::*; use super::*; pub type D = dyn Debug; pub type S = dyn Send; } pub type S = dyn Send; but putting it through rustfmt produces a program that fails to compile: #![deny(unused_imports)] use std::fmt::Debug; use std::marker::Send; pub mod repro { use super::*; use std::prelude::v1::*; pub type D = dyn Debug; pub type S = dyn Send; } pub type S = dyn Send; The error is: error: unused import: `std::prelude::v1::*` --> src/main.rs:8:9 | 8 | use std::prelude::v1::*; | ^^^^^^^^^^^^^^^^^^^
2019-11-06Have tidy ensure that we document all `unsafe` blocks in libcoreOliver Scherer-0/+2
2019-08-09Miri is really slowRalf Jung-4/+5
2019-08-09enable flt2dec tests in MiriRalf Jung-7/+41
2019-08-04bump libcore tests to rand 0.7Ralf Jung-4/+4
2019-06-09Miri: disable a slow testRalf Jung-0/+2
2019-04-25Auto merge of #60192 - t-rapp:tr-saturating-funcs, r=alexcrichtonbors-0/+26
Implement saturating_abs() and saturating_neg() functions for signed integer types Similar to wrapping_abs() / wrapping_neg() functions but saturating at the numeric bounds instead of wrapping around. Complements the existing set of functions with saturation mechanics. cc #59983
2019-04-25Add tests for saturating_abs() and saturating_neg functionsTobias Rapp-0/+26
2019-04-22Remove double trailing newlinesvarkor-6/+0
2019-04-18libcore => 2018Taiki Endo-2/+3
2019-03-16Rollup merge of #59206 - sntdevco:master, r=dtolnaykennytm-24/+24
Improved test output
2019-03-15Improved test output for libcore/num/int_macrossntdevco-24/+24
2019-03-10we can now skip should_panic tests with the libtest harnessRalf Jung-14/+0
2019-02-13review or fix remaining miri failures in libcoreRalf Jung-2/+18
2019-02-07disable tests in MiriRalf Jung-0/+2
2018-12-25Remove licensesMark Rousskov-200/+0
2018-12-17fix testsNicole Mazzuca-2/+2
2018-12-17rename div_euc -> div_euclid, and mod_euc -> rem_euclidNicole Mazzuca-15/+15
logic is written up in https://github.com/rust-lang/rust/issues/49048 Also, update the documentation slightly
2018-12-11std: Depend directly on crates.io cratesAlex Crichton-3/+10
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-12-07Various minor/cosmetic improvements to codeAlexander Regueiro-2/+2
2018-11-14core/tests/num: Simplify `test_int_from_str_overflow()` test codeTobias Bieniek-16/+8
This commit changes the test code to compare against easier-to-read, static values instead of relying on the result of `wrapping_add()` which may or may not result in the value that we expect.
2018-09-04Breaking change upgradesMark Rousskov-8/+8
2018-07-27Use str::repeatShotaro Yamada-2/+1
2018-07-03Auto merge of #51564 - SimonSapin:try-int, r=alexcrichtonbors-0/+127
Implement always-fallible TryFrom for usize/isize conversions that are infallible on some platforms This reverts commit 837d6c70233715a0ae8e15c703d40e3046a2f36a "Remove TryFrom impls that might become conditionally-infallible with a portability lint". This fixes #49415 by adding (restoring) missing `TryFrom` impls for integer conversions to or from `usize` or `isize`, by making them always fallible at the type system level (that is, with `Error=TryFromIntError`) even though they happen to be infallible on some platforms (for some values of `size_of::<usize>()`). They had been removed to allow the possibility to conditionally having some of them be infallible `From` impls instead, depending on the platforms, and have the [portability lint](https://github.com/rust-lang/rfcs/pull/1868) warn when they are used in code that is not already opting into non-portability. For example `#[allow(some_lint)] usize::from(x: u64)` would be valid on code that only targets 64-bit platforms. This PR gives up on this possiblity for two reasons: * Based on discussion with @aturon, it seems that the portability lint is not happening any time soon. It’s better to have the conversions be available *at all* than keep blocking them for so long. Portability-lint-gated platform-specific APIs can always be added separately later. * For code that is fine with fallibility, the alternative would force it to opt into "non-portability" even though there would be no real portability issue.
2018-06-26Add unit tests for `.mod_euc()` and `.div_euc()`Fabian Kössel-0/+19
2018-06-06Revert "Remove TryFrom impls that might become conditionally-infallible with ↵Simon Sapin-0/+127
a portability lint" This reverts commit 837d6c70233715a0ae8e15c703d40e3046a2f36a. Fixes https://github.com/rust-lang/rust/issues/49415
2018-06-01Add @ithinuel's tests from #50597Clar Charr-0/+21
2018-05-22Remove the unstable Float traitSimon Sapin-39/+38
Following up to #49896 and #50629. Fixes #32110. E0689 is weird.
2018-05-17Switch to 1.26 bootstrap compilerMark Simulacrum-1/+0
2018-05-09Rollup merge of #50464 - est31:master, r=rkruppekennytm-6/+4
Remove some transmutes
2018-05-06Some final touches to ensure `./x.py test --stage 0 src/lib*` workskennytm-0/+1
2018-05-05Remove some transmutesest31-6/+4
2018-04-24core: Fix overflow in `int::mod_euc` when `self < 0 && rhs == MIN`Daiki Mizukami-0/+5
2018-03-27Remove TryFrom impls that might become conditionally-infallible with a ↵Simon Sapin-127/+0
portability lint https://github.com/rust-lang/rust/pull/49305#issuecomment-376293243
2018-03-11Update Cargo submoduleAlex Crichton-0/+161
Required moving all fulldeps tests depending on `rand` to different locations as now there's multiple `rand` crates that can't be implicitly linked against.
2018-03-06Add reverse_bits to integer typesAmanieu d'Antras-0/+11
2018-02-19Make ".e0" not parse as 0.0varkor-0/+6
This forces floats to have either a digit before the separating point, or after. Thus ".e0" is invalid like ".", when using `parse()`.