about summary refs log tree commit diff
path: root/src/libcore/num
AgeCommit message (Collapse)AuthorLines
2019-10-21Rollup merge of #65092 - tspiteri:const-is-pow2, r=oli-obkMazdak Farrokhzad-2/+2
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-21improve readability of is_power_of_twoTrevor Spiteri-1/+1
2019-10-18Rollup merge of #65549 - t-rapp:tr-wrapping-rotate-docs, r=jonas-schievinkTyler Mandry-2/+2
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-18Fix left/right shift typo in wrapping rotate docsTobias Rapp-2/+2
This makes the note similar to the one found on rotate functions for primitive types like i32/u32.
2019-10-17properly document panics in div_euclid and rem_euclidTrevor Spiteri-2/+10
2019-10-11improve performance of signed saturating_mulTrevor Spiteri-1/+1
Reciprocal throughput is improved from 2.3 to 1.7. https://godbolt.org/z/ROMiX6
2019-10-04make is_power_of_two a const functionTrevor Spiteri-2/+2
2019-10-03Rollup merge of #64941 - lzutao:inline-max_min_value, r=nnethercoteMazdak Farrokhzad-4/+4
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-01Add lower bound doctests for `saturating_{add,sub}` signed intsLzu Tao-3/+6
2019-10-01Inline `{min,max}_value` even in debug buildsLzu Tao-4/+4
2019-09-25Rollup merge of #64764 - Mark-Simulacrum:snap, r=CentrilMazdak Farrokhzad-42/+6
Master is now 1.40 r? @pietroalbini
2019-09-25Rollup merge of #64386 - tspiteri:const-abs2, r=oli-obkMazdak Farrokhzad-3/+26
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-25Snap cfgs to new betaMark Rousskov-42/+6
2019-09-23Move `--cfg bootstrap` out of `rustc.rs`Alex Crichton-12/+12
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-18doc: Format some primitives examplesLzu Tao-12/+18
2019-09-13use `sign` variable in abs and wrapping_abs methodsTrevor Spiteri-3/+26
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-10Rollup merge of #63786 - tspiteri:const-abs, r=alexcrichtonMazdak Farrokhzad-21/+9
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-02Auto merge of #63692 - iluuu1994:issue-49660, r=sfacklerbors-0/+8
Test that Wrapping arithmetic ops are implemented for all int types Closes #49660
2019-08-22Change code formatting for readability.Tomasz Różański-2/+2
2019-08-21make abs, wrapping_abs, and overflowing_abs const functionsTrevor Spiteri-21/+9
2019-08-20Test that Wrapping arithmetic ops are implemented for all int typesIlija Tovilo-0/+8
2019-08-16rustbuild: work around the stdarch cfg(bootstrap) bug.Eduard-Mihai Burtescu-12/+12
2019-08-16Rename overflowing_{add,sub,mul} intrinsics to wrapping_{add,sub,mul}.Eduard-Mihai Burtescu-6/+42
2019-08-08doc: Fix typo in float from bytes methodsLzu Tao-4/+4
2019-07-25Rollup merge of #61884 - crlf0710:stablize_euc, r=dtolnay,CentrilMazdak Farrokhzad-32/+16
Stablize Euclidean Modulo (feature euclidean_division) Closes #49048
2019-07-08Add documentation to float conversion methodsLzu Tao-20/+144
2019-07-08Add float conversions to and from bytesTobias Bucher-0/+106
Use the same API as for integers. Fixes #57492.
2019-07-07Stablize Euclidean Modulo (feature euclidean_division)CrLF0710-32/+16
2019-07-04Switch master to 1.38Mark Rousskov-1/+1
2019-06-14Change `...` to `..=` where applicableAaron Kutch-17/+17
2019-06-07Make `i*::signum` a `const fn`.Dylan MacKenzie-6/+3
This uses a well-known branchless implementation of `signum`. Its `const`-ness is unstable and requires `#![feature(const_int_sign)]`.
2019-06-06Add intrinsics for floating-point min and maxvarkor-36/+10
2019-06-05Utilize cfg(bootstrap) over cfg(stage0)Mark Rousskov-1/+1
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-05-31Stabilize reverse_bits featureLzu Tao-9/+3
2019-05-25Rollup merge of #61134 - nvzqz:reverse_bits-must_use, r=varkorMazdak Farrokhzad-0/+3
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-24Annotate each `reverse_bits` with `#[must_use]`Nikolai Vazquez-0/+3
Because the name sounds like an in-place mutation like `[T]::reverse(&mut self)`, it may be confused for one.
2019-05-22Allow null-pointer-optimized enums in FFI if their underlying representation ↵Michael Bradshaw-0/+1
is FFI safe This allows types like Option<NonZeroU8> to be used in FFI without triggering the improper_ctypes lint. This works by changing the is_repr_nullable_ptr function to consider an enum E to be FFI-safe if: - E has no explicit #[repr(...)]. - It only has two variants. - One of those variants is empty (meaning it has no fields). - The other variant has only one field. - That field is one of the following: - &T - &mut T - extern "C" fn - core::num::NonZero* - core::ptr::NonNull<T> - #[repr(transparent)] struct wrapper around one of the types in this list. - The size of E and its field are both known and are both the same size (implying E is participating in the nonnull optimization).
2019-05-05to_xe_bytes for isize and usize returns an array of different sizeStepan Koltsov-30/+83
... on different platforms. Official rustdoc of [`usize::to_le_bytes`](https://doc.rust-lang.org/std/primitive.usize.html#method.to_le_bytes) displays signature ``` pub fn to_ne_bytes(self) -> [u8; 8] ``` which might be misleading: this function returns 4 bytes on 32-bit systems.
2019-04-26Auto merge of #60167 - varkor:tidy-filelength, r=matthewjasperbors-0/+2
Add a tidy check for files with over 3,000 lines Files with a large number of lines can cause issues in GitHub (e.g. https://github.com/rust-lang/rust/issues/60015) and also tend to be indicative of opportunities to refactor into less monolithic structures. This adds a new check to tidy to warn against files that have more than 3,000 lines, as suggested in https://github.com/rust-lang/rust/issues/60015#issuecomment-483868594. (This number was chosen fairly arbitrarily as a reasonable indicator of size.) This check can be ignored with `// ignore-tidy-filelength`. Existing files with greater than 3,000 lines currently ignore the check, but this helps us spot when files are getting too large. (We might try to split up all files larger than this in the future, as in https://github.com/rust-lang/rust/issues/60015).
2019-04-25ignore-tidy-filelength on all files with greater than 3000 linesvarkor-0/+2
2019-04-25Auto merge of #60192 - t-rapp:tr-saturating-funcs, r=alexcrichtonbors-0/+56
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 saturating_abs() and saturating_neg() functions to signed integer typesTobias Rapp-0/+56
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.
2019-04-23Rollup merge of #59839 - KodrAus:must-use-num, r=sfacklerMazdak Farrokhzad-0/+156
Warn on unused results for operation methods on nums From a suggestion by @llogiq Adds a `#[must_use]` attribute to operation methods on integers that take self by value as the first operand and another value as the second. It makes it clear that these methods return the result of the operation instead of mutating `self`, which is the source of a rather embarrassing bug I had in a codebase of mine recently... As an example: ```rust struct Int { value: i64, } impl Int { fn add(&mut self, other: i64) { self.value.wrapping_add(other); } } ``` Will produce a warning like: ``` warning: unused return value of `core::num::<impl i64>::wrapping_add` that must be used --> src/main.rs:7:7 | 7 | self.value.wrapping_add(other); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(unused_must_use)] on by default = note: this returns the result of the operation, without modifying the original ``` If this is something we're on board with, we could do something similar for `f32` and `f64` too. There are probably other methods on integers that make sense.
2019-04-22Remove double trailing newlinesvarkor-1/+0
2019-04-19libcore: deny more...Mazdak Farrokhzad-16/+16
2019-04-18libcore => 2018Taiki Endo-87/+87
2019-04-16Fix the max value of usize on 16-bit platformsJakub Kądziołka-1/+1
2019-04-10warn on unused results for operation methods on numsAshley Mannix-0/+156
2019-03-28Rollup merge of #58717 - hellow554:nonzero_parse, r=oli-obkMazdak Farrokhzad-0/+24
Add FromStr impl for NonZero types This is a WIP implementation because I do have some questions regarding the solution. Somebody should ping the lang team on this I guess. Please see the annotations on the code for more details. Closes #58604
2019-03-27Rollup merge of #59283 - SimonSapin:branchless-ascii-case, r=joshtriplettJosh Stone-135/+24
Make ASCII case conversions more than 4× faster Reformatted output of `./x.py bench src/libcore --test-args ascii` below. The `libcore` benchmark calls `[u8]::make_ascii_lowercase`. `lookup` has code (effectively) identical to that before this PR, and ~~`branchless`~~ `mask_shifted_bool_match_range` after this PR. ~~See [code comments](https://github.com/rust-lang/rust/pull/59283/commits/ce933f77c865a15670855ac5941fe200752b739f#diff-01076f91a26400b2db49663d787c2576R3796) in `u8::to_ascii_uppercase` in `src/libcore/num/mod.rs` for an explanation of the branchless algorithm.~~ **Update:** the algorithm was simplified while keeping the performance. See `branchless` v.s. `mask_shifted_bool_match_range` benchmarks. Credits to @raphlinus for the idea in https://twitter.com/raphlinus/status/1107654782544736261, which extends this algorithm to “fake SIMD” on `u32` to convert four bytes at a time. The `fake_simd_u32` benchmarks implements this with [`let (before, aligned, after) = bytes.align_to_mut::<u32>()`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut). Note however that this is buggy when addition carries/overflows into the next byte (which does not happen if the input is known to be ASCII). This could be fixed (to optimize `[u8]::make_ascii_lowercase` and `[u8]::make_ascii_uppercase` in `src/libcore/slice/mod.rs`) either with some more bitwise trickery that I didn’t quite figure out, or by using “real” SIMD intrinsics for byte-wise addition. I did not pursue this however because the current (incorrect) fake SIMD algorithm is only marginally faster than the one-byte-at-a-time branchless algorithm. This is because LLVM auto-vectorizes the latter, as can be seen on https://rust.godbolt.org/z/anKtbR. Benchmark results on Linux x64 with Intel i7-7700K: (updated from https://github.com/rust-lang/rust/pull/59283#issuecomment-474146863) ```rust 6830 bytes string: alloc_only ... bench: 112 ns/iter (+/- 0) = 62410 MB/s black_box_read_each_byte ... bench: 1,733 ns/iter (+/- 8) = 4033 MB/s lookup_table ... bench: 1,766 ns/iter (+/- 11) = 3958 MB/s branch_and_subtract ... bench: 417 ns/iter (+/- 1) = 16762 MB/s branch_and_mask ... bench: 401 ns/iter (+/- 1) = 17431 MB/s branchless ... bench: 365 ns/iter (+/- 0) = 19150 MB/s libcore ... bench: 367 ns/iter (+/- 1) = 19046 MB/s fake_simd_u32 ... bench: 361 ns/iter (+/- 2) = 19362 MB/s fake_simd_u64 ... bench: 361 ns/iter (+/- 1) = 19362 MB/s mask_mult_bool_branchy_lookup_table ... bench: 6,309 ns/iter (+/- 19) = 1107 MB/s mask_mult_bool_lookup_table ... bench: 4,183 ns/iter (+/- 29) = 1671 MB/s mask_mult_bool_match_range ... bench: 339 ns/iter (+/- 0) = 20619 MB/s mask_shifted_bool_match_range ... bench: 339 ns/iter (+/- 1) = 20619 MB/s 32 bytes string: alloc_only ... bench: 15 ns/iter (+/- 0) = 2133 MB/s black_box_read_each_byte ... bench: 29 ns/iter (+/- 0) = 1103 MB/s lookup_table ... bench: 24 ns/iter (+/- 4) = 1333 MB/s branch_and_subtract ... bench: 16 ns/iter (+/- 0) = 2000 MB/s branch_and_mask ... bench: 16 ns/iter (+/- 0) = 2000 MB/s branchless ... bench: 16 ns/iter (+/- 0) = 2000 MB/s libcore ... bench: 15 ns/iter (+/- 0) = 2133 MB/s fake_simd_u32 ... bench: 17 ns/iter (+/- 0) = 1882 MB/s fake_simd_u64 ... bench: 16 ns/iter (+/- 0) = 2000 MB/s mask_mult_bool_branchy_lookup_table ... bench: 42 ns/iter (+/- 0) = 761 MB/s mask_mult_bool_lookup_table ... bench: 35 ns/iter (+/- 0) = 914 MB/s mask_mult_bool_match_range ... bench: 16 ns/iter (+/- 0) = 2000 MB/s mask_shifted_bool_match_range ... bench: 16 ns/iter (+/- 0) = 2000 MB/s 7 bytes string: alloc_only ... bench: 14 ns/iter (+/- 0) = 500 MB/s black_box_read_each_byte ... bench: 22 ns/iter (+/- 0) = 318 MB/s lookup_table ... bench: 16 ns/iter (+/- 0) = 437 MB/s branch_and_subtract ... bench: 16 ns/iter (+/- 0) = 437 MB/s branch_and_mask ... bench: 16 ns/iter (+/- 0) = 437 MB/s branchless ... bench: 19 ns/iter (+/- 0) = 368 MB/s libcore ... bench: 20 ns/iter (+/- 0) = 350 MB/s fake_simd_u32 ... bench: 18 ns/iter (+/- 0) = 388 MB/s fake_simd_u64 ... bench: 21 ns/iter (+/- 0) = 333 MB/s mask_mult_bool_branchy_lookup_table ... bench: 20 ns/iter (+/- 0) = 350 MB/s mask_mult_bool_lookup_table ... bench: 19 ns/iter (+/- 0) = 368 MB/s mask_mult_bool_match_range ... bench: 19 ns/iter (+/- 0) = 368 MB/s mask_shifted_bool_match_range ... bench: 19 ns/iter (+/- 0) = 368 MB/s ```