Commit Graph

876 Commits

Author SHA1 Message Date
Simon Sapin
a213ff8299 Move numeric From and TryFrom impls to libcore/convert/num.rs
This makes `libcore/num/mod.rs` slightly smaller. It’s still 4911 lines and not easy to navigate. This doesn’t change any public API.
2019-12-06 14:00:45 +01:00
Simon Sapin
cba479f75c Add {f32,f64}::approx_unchecked_to<Int> unsafe methods
As discussed in https://github.com/rust-lang/rust/issues/10184

Currently, casting a floating point number to an integer with `as` is Undefined Behavior if the value is out of range. `-Z saturating-float-casts` fixes this soundness hole by making `as` “saturate” to the maximum or minimum value of the integer type (or zero for `NaN`), but has measurable negative performance impact in some benchmarks. There is some consensus in that thread for enabling saturation by default anyway, but provide an `unsafe fn` alternative for users who know through some other mean that their values are in range.
2019-12-06 14:01:05 +01:00
David Tolnay
d353a4c267 Make dec2flt_table compatible with rustfmt 2019-11-29 20:17:09 -08:00
Ohad Ravid
85a468b8cf Clarify {f32,f64}::EPSILON docs 2019-11-28 13:49:58 +01:00
Tyler Mandry
4c51d58de0 Rollup merge of #66769 - fusion-engineering-forks:tau-constant, r=dtolnay
Add core::{f32,f64}::consts::TAU.

### **`τ`**
2019-11-27 15:28:47 -06:00
David Tolnay
95e00bfed8 Format libcore with rustfmt
This commit applies rustfmt with default settings to files in
src/libcore *that are not involved in any currently open PR* to minimize
merge conflicts. The list of files involved in open PRs was determined
by querying GitHub's GraphQL API with this script:
https://gist.github.com/dtolnay/aa9c34993dc051a4f344d1b10e4487e8

With the list of files from the script in `outstanding_files`, the
relevant commands were:

    $ find src/libcore -name '*.rs' | xargs rustfmt --edition=2018
    $ rg libcore outstanding_files | xargs git checkout --

Repeating this process several months apart should get us coverage of
most of the rest of libcore.
2019-11-26 23:02:11 -08:00
Mara Bos
d220ed4fd7 Add tracking issue number. 2019-11-26 16:32:46 +01:00
Mara Bos
fd9b986bfe Add core::{f32,f64}::consts::TAU. 2019-11-26 16:32:46 +01:00
Mazdak Farrokhzad
089229a193 Redefine core::convert::Infallible as !. 2019-11-21 14:55:33 +01:00
Mazdak Farrokhzad
15c30ddd69 Stabilize the never_type, written !. 2019-11-21 14:55:32 +01:00
Alexander Nye
619f057dae remove vestigial comments referring to defunct numeric trait hierarchy
see also https://github.com/rust-lang/rust/pull/23104/files
2019-11-08 13:51:07 -08:00
Oliver Scherer
02f9167f94 Have tidy ensure that we document all unsafe blocks in libcore 2019-11-06 11:04:42 +01:00
Lzu Tao
7b72c2831c Stabilize float_to_from_bytes feature 2019-10-31 16:13:28 +00:00
Mateusz Mikuła
dffc1b3282 Apply clippy::useless_let_if_seq suggestion 2019-10-22 19:14:22 +02:00
Mazdak Farrokhzad
a160258927 Rollup merge of #65092 - tspiteri:const-is-pow2, r=oli-obk
make is_power_of_two a const function

This makes `is_power_of_two` a const function by using `&` instead of short-circuiting `&&`; Rust supports bitwise `&` for `bool` and short-circuiting is not required in the existing expression.

I don't think this needs a const-hack label as I don't find the changed code less readable, if anything I prefer that it is clearer that short circuiting is not used.

@oli-obk
2019-10-21 22:00:47 +02:00
Trevor Spiteri
d689c709ea improve readability of is_power_of_two 2019-10-21 15:35:54 +02:00
Tyler Mandry
edc42a91fd Rollup merge of #65549 - t-rapp:tr-wrapping-rotate-docs, r=jonas-schievink
Fix left/right shift typo in wrapping rotate docs

This makes the note similar to the one found on rotate functions for primitive types like i32/u32.
2019-10-18 13:48:37 -07:00
Tobias Rapp
769e75b40c Fix left/right shift typo in wrapping rotate docs
This makes the note similar to the one found on rotate functions for
primitive types like i32/u32.
2019-10-18 08:46:30 +02:00
Trevor Spiteri
83e97c6ac1 properly document panics in div_euclid and rem_euclid 2019-10-17 13:53:57 +02:00
Trevor Spiteri
57aae75ce3 improve performance of signed saturating_mul
Reciprocal throughput is improved from 2.3 to 1.7.
https://godbolt.org/z/ROMiX6
2019-10-11 17:13:19 +02:00
Trevor Spiteri
a12788a0d7 make is_power_of_two a const function 2019-10-04 11:01:17 +02:00
Mazdak Farrokhzad
b131230c3b Rollup merge of #64941 - lzutao:inline-max_min_value, r=nnethercote
Inline `{min,max}_value` even in debug builds

I think it is worth to inline `{min,max}_value` even in debug builds.
See this godbolt link: https://godbolt.org/z/-COkVS
2019-10-03 08:04:33 +02:00
Lzu Tao
eb4ca2120e Add lower bound doctests for saturating_{add,sub} signed ints 2019-10-01 05:46:50 +00:00
Lzu Tao
3b49ab6e48 Inline {min,max}_value even in debug builds 2019-10-01 04:10:51 +00:00
Mazdak Farrokhzad
1e8dd37561 Rollup merge of #64764 - Mark-Simulacrum:snap, r=Centril
Master is now 1.40

r? @pietroalbini
2019-09-25 16:26:22 +02:00
Mazdak Farrokhzad
b30238e312 Rollup merge of #64386 - tspiteri:const-abs2, r=oli-obk
use `sign` variable in abs and wrapping_abs methods

This also makes the code easier to understand by hinting at the significance of `self >> ($BITS - 1)`.

Also, now `overflowing_abs` simply uses `wrapping_abs`, which is clearer and avoids a potential performance regression in the LLVM IR.

This PR follows from the discussion from #63786.

r? @eddyb
cc @nikic
2019-09-25 16:26:16 +02:00
Mark Rousskov
f359a94849 Snap cfgs to new beta 2019-09-25 08:42:46 -04:00
Alex Crichton
b3f95f460f Move --cfg bootstrap out of rustc.rs
Instead let's do this via `RUSTFLAGS` in `builder.rs`. Currently
requires a submodule update of `stdarch` to fix a problem with previous
compilers.
2019-09-23 09:34:44 -07:00
Lzu Tao
bec0a76727 doc: Format some primitives examples 2019-09-18 08:24:24 +00:00
Trevor Spiteri
562903a0a6 use sign variable in abs and wrapping_abs methods
This also makes the code easier to understand by hinting at the
significance of `self >> ($BITS - 1)` and by including an explanation
in the comments.

Also, now overflowing_abs simply uses wrapping_abs, which is clearer
and avoids a potential performance regression in the LLVM IR.
2019-09-13 10:20:17 +02:00
Mazdak Farrokhzad
13726cc444 Rollup merge of #63786 - tspiteri:const-abs, r=alexcrichton
Make `abs`, `wrapping_abs`, `overflowing_abs` const functions

This makes `abs`, `wrapping_abs` and `overflowing_abs` const functions like #58044 makes `wrapping_neg` and `overflowing_neg` const functions.

`abs` is made const by returning `(self ^ -1) - -1` = `!self + 1` = `-self` for negative numbers and `(self ^ 0) - 0` = `self` for non-negative numbers. The subexpression `self >> ($BITS - 1)` evaluates to `-1` for negative numbers and `0` otherwise. The subtraction overflows when `self` is `min_value()`, as we would be subtracting `max_value() - -1`; this is when `abs` should overflow.

`wrapping_abs` and `overflowing_abs` make use of `wrapping_sub` and `overflowing_sub` instead of the subtraction operator.
2019-09-10 17:19:19 +02:00
bors
9af17757be Auto merge of #63692 - iluuu1994:issue-49660, r=sfackler
Test that Wrapping arithmetic ops are implemented for all int types

Closes #49660
2019-09-02 15:32:08 +00:00
Tomasz Różański
3b04e91d27 Change code formatting for readability. 2019-08-22 14:27:51 +02:00
Trevor Spiteri
39260d9016 make abs, wrapping_abs, and overflowing_abs const functions 2019-08-21 14:10:40 +02:00
Ilija Tovilo
0c5220dc36 Test that Wrapping arithmetic ops are implemented for all int types 2019-08-20 23:13:08 +02:00
Eduard-Mihai Burtescu
892ef6fa48 rustbuild: work around the stdarch cfg(bootstrap) bug. 2019-08-16 20:12:10 +03:00
Eduard-Mihai Burtescu
0f1da639d4 Rename overflowing_{add,sub,mul} intrinsics to wrapping_{add,sub,mul}. 2019-08-16 20:04:21 +03:00
Lzu Tao
9163990702 doc: Fix typo in float from bytes methods 2019-08-08 01:53:30 +00:00
Mazdak Farrokhzad
a57c4f6297 Rollup merge of #61884 - crlf0710:stablize_euc, r=dtolnay,Centril
Stablize Euclidean Modulo (feature euclidean_division)

Closes #49048
2019-07-25 23:20:53 +02:00
Lzu Tao
df53a3fd55 Add documentation to float conversion methods 2019-07-08 07:34:25 +00:00
Tobias Bucher
ad47f0874a Add float conversions to and from bytes
Use the same API as for integers.

Fixes #57492.
2019-07-08 07:34:25 +00:00
CrLF0710
72ac8ce9aa Stablize Euclidean Modulo (feature euclidean_division) 2019-07-07 12:16:13 +08:00
Mark Rousskov
8a7dded1a2 Switch master to 1.38 2019-07-04 11:26:57 -04:00
Aaron Kutch
363940bbe1 Change ... to ..= where applicable 2019-06-14 12:24:38 -05:00
Dylan MacKenzie
bd899d02e9 Make i*::signum a const fn.
This uses a well-known branchless implementation of `signum`.
Its `const`-ness is unstable and requires `#![feature(const_int_sign)]`.
2019-06-07 15:37:03 -07:00
varkor
0e5edc9f16 Add intrinsics for floating-point min and max 2019-06-06 21:27:23 +01:00
Mark Rousskov
bea2e55efa Utilize cfg(bootstrap) over cfg(stage0)
Also removes stage1, stage2 cfgs being passed to rustc to ensure that
stage1 and stage2 are only differentiated as a group (i.e., only through
not bootstrap).
2019-06-05 17:57:58 -06:00
Lzu Tao
1c26bbf628 Stabilize reverse_bits feature 2019-05-31 04:43:53 +00:00
Mazdak Farrokhzad
92f1cfd039 Rollup merge of #61134 - nvzqz:reverse_bits-must_use, r=varkor
Annotate each `reverse_bits` with `#[must_use]`

Because the name sounds like an in-place mutation like `[T]::reverse(&mut self)`, it may be confused for one.

This change was requested at https://github.com/rust-lang/rust/issues/48763#issuecomment-493743741.
2019-05-25 04:55:43 +02:00
Nikolai Vazquez
035f651df7 Annotate each reverse_bits with #[must_use]
Because the name sounds like an in-place mutation like
`[T]::reverse(&mut self)`, it may be confused for one.
2019-05-24 22:26:34 +02:00