summary refs log tree commit diff
path: root/src/libcore/num
AgeCommit message (Collapse)AuthorLines
2015-06-09std: Make abs() panic on overflow in debug modeAlex Crichton-3/+12
Debug overflow checks for arithmetic negation landed in #24500, at which time the `abs` method on signed integers was changed to using `wrapping_neg` to ensure that the function never panicked. This implied that `abs` of `INT_MIN` would return `INT_MIN`, another negative value. When this change was back-ported to beta, however, in #24708, the `wrapping_neg` function had not yet been backported, so the implementation was changed in #24785 to `!self + 1`. This change had the unintended side effect of enabling debug overflow checks for the `abs` function. Consequently, the current state of affairs is that the beta branch checks for overflow in debug mode for `abs` and the nightly branch does not. This commit alters the behavior of nightly to have `abs` always check for overflow in debug mode. This change is more consistent with the way the standard library treats overflow as well, and it is also not a breaking change as it's what the beta branch currently does (albeit if by accident). cc #25378
2015-05-09Auto merge of #24612 - lifthrasiir:flt2dec, r=pnkfelixbors-0/+2231
This is a direct port of my prior work on the float formatting. The detailed description is available [here](https://github.com/lifthrasiir/rust-strconv#flt2dec). In brief, * This adds a new hidden module `core::num::flt2dec` for testing from `libcoretest`. Why is it in `core::num` instead of `core::fmt`? Because I envision that the table used by `flt2dec` is directly applicable to `dec2flt` (cf. #24557) as well, which exceeds the realm of "formatting". * This contains both Dragon4 algorithm (exact, complete but slow) and Grisu3 algorithm (exact, fast but incomplete). * The code is accompanied with a large amount of self-tests and some exhaustive tests. In particular, `libcoretest` gets a new dependency on `librand`. For the external interface it relies on the existing test suite. * It is known that, in the best case, the entire formatting code has about 30 KBs of binary overhead (judged from strconv experiments). Not too bad but there might be a potential room for improvements. This is rather large code. I did my best to comment and annotate the code, but you have been warned. For the maximal availability the original code was licensed in CC0, but I've also dual-licensed it in MIT/Apache as well so there should be no licensing concern. This is [breaking-change] as it changes the float output slightly (and it also affects the casing of `inf` and `nan`). I hope this is not a big deal though :) Fixes #7030, #18038 and #24556. Also related to #6220 and #20870. ## Known Issues - [x] I've yet to finish `make check-stage1`. It does pass main test suites including `run-pass` but there might be some unknown edges on the doctests. - [ ] Figure out how this PR affects rustc. - [ ] Determine which internal routine is mapped to the formatting specifier. Depending on the decision, some internal routine can be safely removed (for instance, currently `to_shortest_str` is unused).
2015-05-09Auto merge of #25159 - inrustwetrust:wrapping_inline, r=alexcrichtonbors-0/+1
This was causing function calls to be emitted for bitwise complements, even with optimizations on. Steps to reproduce: ``` $ cat wrapping.rs fn main() { let a = std::num::Wrapping(std::env::args().len() as u32); let b = !a; println!("{}", b.0); } $ rustc -O wrapping.rs --emit=asm,link $ grep Not wrapping.s callq _ZN3num8wrapping23Wrapping$LT$u32$GT$.Not3not20hba4b266232e02b1dHkbE ```
2015-05-06Add missing inline attribute to Not impl for Wrapping<T>inrustwetrust-0/+1
2015-05-06core: use banker's rounding for the exact mode in flt2dec.Kang Seonghoon-1/+5
For the shortest mode the IEEE 754 decoder already provides an exact rounding range accounting for banker's rounding, but it was not the case for the exact mode. This commit alters the exact mode algorithm for Dragon so that any number ending at `...x5000...` with even `x` and infinite zeroes will round to `...x` instead of `...(x+1)` as it was. Grisu is not affected by this change because this halfway case always results in the failure for Grisu.
2015-05-06core: updated for the master changes.Kang Seonghoon-0/+5
The master no longer has `std::num::Float`, so a generic `ldexp` is not readily available. `DecodableFloat::ldexpi` works around this.
2015-05-06core: fixed a slight bug.Kang Seonghoon-6/+6
The bug involves the incorrect logic for `core::num::flt2dec::decoder`. This makes some numbers in the form of 2^n missing one final digits, which breaks the bijectivity criterion. The regression tests have been added, and f32 exhaustive test is rerun to get the updated result.
2015-05-06core: fixed typos and revised comments in flt2dec.Kang Seonghoon-14/+17
2015-05-06core: tweaked flt2dec to match the casing of the older formatting code.Kang Seonghoon-18/+18
2015-05-06core: added core::num::flt2dec for floating-point formatting.Kang Seonghoon-0/+2219
This is a fork of the flt2dec portion of rust-strconv [1] with a necessary relicensing (the original code was licensed CC0-1.0). Each module is accompanied with large unit tests, integrated in this commit as coretest::num::flt2dec. This module is added in order to replace the existing core::fmt::float method. The forked revision of rust-strconv is from 2015-04-20, with a commit ID 9adf6d3571c6764a6f240a740c823024f70dc1c7. [1] https://github.com/lifthrasiir/rust-strconv/
2015-05-01std: Don't use a wrapper for the float error typeAlex Crichton-12/+16
Ensures that the same error type is propagated throughout. Unnecessary leakage of the internals is prevented through the usage of stability attributes. Closes #24748
2015-05-01Rollup merge of #25010 - huonw:inline-int-extremes, r=alexcrichtonManish Goregaokar-0/+2
These compile down to `mov $CONSTANT, register; ret`, but the lack of `#[inline]` meant they have a full `call ...` when used from external crates.
2015-05-01Mark the {min,max}_value functions on integers #[inline].Huon Wilson-0/+2
These compile down to `mov $CONSTANT, register; ret`, but the lack of `#[inline]` meant they have a full `call ...` when used from external crates.
2015-04-29Currently, LLVM lowers a cttz8 on x86_64 to these instructions:Björn Steinbrink-1/+14
```asm movzbl %dil, %eax bsfl %eax, %eax movl $32, %ecx cmovnel %eax, %ecx cmpl $32, %ecx movl $8, %eax cmovnel %ecx, %eax ``` which has some unnecessary overhead, having two conditional moves. To improve the codegen, we can zero extend the 8 bit integer, then set bit 8 and perform a cttz operation on the extended value. That way there's no conditional operation involved at all.
2015-04-21std: Bring back f32::from_str_radix as an unstable APIAlex Crichton-237/+173
This API was exercised in a few tests and mirrors the `from_str_radix` functionality of the integer types.
2015-04-21std: Remove deprecated/unstable num functionalityAlex Crichton-1691/+103
This commit removes all the old casting/generic traits from `std::num` that are no longer in use by the standard library. This additionally removes the old `strconv` module which has not seen much use in quite a long time. All generic functionality has been supplanted with traits in the `num` crate and the `strconv` module is supplanted with the [rust-strconv crate][rust-strconv]. [rust-strconv]: https://github.com/lifthrasiir/rust-strconv This is a breaking change due to the removal of these deprecated crates, and the alternative crates are listed above. [breaking-change]
2015-04-21Model lexer: Fix remaining issuesPiotr Czarnecki-2/+0
2015-04-17side-step potentially panic'ing negate in `fn abs`.Felix S. Klock II-1/+5
2015-04-17Auto merge of #24420 - pnkfelix:oflo-api, r=alexcrichtonbors-1/+156
Fill in missing parts of Integer overflow API See todo list at #22020
2015-04-14Fix bug in `wrapping_div`Felix S. Klock II-1/+1
See discussion, albeit one-sided, in: https://github.com/rust-lang/rfcs/issues/964
2015-04-14Remaining API additions for int overflow:Felix S. Klock II-0/+155
`wrapping_div`, `wrapping_rem`, `wrapping_neg`, `wrapping_shl`, `wrapping_shr`. All marked unstable under `core` feature for now (with expectation of being marked as stable by 1.0 release).
2015-04-13pluralize doc comment verbs and add missing periodsAndrew Paseltiner-43/+43
2015-04-12mod.rs documentary fixIgor Strebezhev-2/+2
Docs meant that Option is returned though the function returns Result.
2015-04-11Rollup merge of #24298 - White-Oak:master, r=alexcrichtonManish Goregaokar-11/+8
Fixes 'fn from_str_radix' documentation where docs meant that Option is returned, though the function returns Result.
2015-04-11Rollup merge of #24291 - xamgore:patch-1, r=steveklabnikManish Goregaokar-6/+6
From [here](http://doc.rust-lang.org/nightly/std/primitive.i8.html): > `fn rotate_right(self, n: u32) -> i8` > Shifts the bits to the right by a specified __amount amount__, n, wrapping the truncated bits to the beginning of the resulting integer.
2015-04-11Rollup merge of #24274 - steveklabnik:fix_pow_docs, r=nikomatsakisManish Goregaokar-4/+3
This is very confusing now that these are inherent methods.
2015-04-10mod.rs docs fix - for floatsOak-5/+4
Same with integers — docs meant that Option is returned though the function returns Result.
2015-04-10mod.rs docs fixOak-6/+4
Docs meant that Option is returned though the function returns Result.
2015-04-10Fix mistake in documentationIgor Strebezhev-6/+6
2015-04-10Auto merge of #24177 - alexcrichton:rustdoc, r=aturonbors-0/+1
This commit series starts out with more official test harness support for rustdoc tests, and then each commit afterwards adds a test (where appropriate). Each commit should also test and finish independently of all others (they're all pretty separable). I've uploaded a [copy of the documentation](http://people.mozilla.org/~acrichton/doc/std/) generated after all these commits were applied, and a double check on issues being closed would be greatly appreciated! I'll also browse the docs a bit and make sure nothing regressed too horribly.
2015-04-10Fix pow docs to not use IntSteve Klabnik-4/+3
This is very confusing now that these are inherent methods.
2015-04-07std: Hide facade extension traits in docsAlex Crichton-0/+1
These traits are currently all just unstable parts of the facade which are implementation details for primitives further up the facade. This may make it more difficult to find what set of methods you get if only linking to libcore, but for now that's also unstable behavior. Closes #22025
2015-04-08Make `sum` and `product` inherent methods on `Iterator`Tobias Bucher-0/+14
In addition to being nicer, this also allows you to use `sum` and `product` for iterators yielding custom types aside from the standard integers. Due to removing the `AdditiveIterator` and `MultiplicativeIterator` trait, this is a breaking change. [breaking-change]
2015-04-03Auto merge of #23832 - petrochenkov:usize, r=aturonbors-4/+4
These constants are small and can fit even in `u8`, but semantically they have type `usize` because they denote sizes and are almost always used in `usize` context. The change of their type to `u32` during the integer audit led only to the large amount of `as usize` noise (see the second commit, which removes this noise). This is a minor [breaking-change] to an unstable interface. r? @aturon
2015-04-01Test fixes and rebase conflicts, round 2Alex Crichton-1/+1
2015-04-01rollup merge of #23860: nikomatsakis/copy-requires-cloneAlex Crichton-1/+1
Conflicts: src/test/compile-fail/coherence-impls-copy.rs
2015-04-01rollup merge of #23945: pnkfelix/gate-u-negateAlex Crichton-4/+50
Feature-gate unsigned unary negate. Discussed in weekly meeting here: https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2015-03-31.md#feature-gate--expr and also in the internals thread here: http://internals.rust-lang.org/t/forbid-unsigned-integer/752
2015-04-01Fix bug in `OverflowOps` impl for unsigned integers.Felix S. Klock II-3/+49
Namely, the special case treatment for `div`/`rem` is only applicable to signed integer values. Clearly RFC 1027 would have saved us here! ;)
2015-04-01fallout from feature-gating unary negation on unsigned integers.Felix S. Klock II-1/+1
2015-04-01rollup merge of #23947: aturon/revise-numAlex Crichton-0/+20
Recent numerics stabilization removed the inherent `min_value` and `max_value` methods from integer types, assuming that the module-level constants would suffice. However, that failed to account for the use case in FFI code when dealing with integer type aliases. This commit reintroduces the methods as `#[stable]`, since this is essential functionality for 1.0. It's unfortunate to freeze these as methods, but when we can provide inherent associated constants these methods can be deprecated. r? @sfackler cc @alexcrichton
2015-04-01Re-add min_value, max_value methodsAaron Turon-0/+20
Recent numerics stabilization removed the inherent `min_value` and `max_value` methods from integer types, assuming that the module-level constants would suffice. However, that failed to account for the use case in FFI code when dealing with integer type aliases. This commit reintroduces the methods as `#[stable]`, since this is essential functionality for 1.0. It's unfortunate to freeze these as methods, but when we can provide inherent associated constants these methods can be deprecated.
2015-04-01Collect the definition of the `Error` trait into `libstd` for now. ThisNiko Matsakis-21/+20
sidesteps a coherence difficulty where `liballoc` had to prove that `&str: !Error`, which didn't involve any local types.
2015-04-01Fallout in public-facing and semi-public-facing libsNiko Matsakis-1/+1
2015-03-31rollup merge of #23863: pnkfelix/arith-oflo-const-evalAlex Crichton-0/+130
const_eval : add overflow-checking for {`+`, `-`, `*`, `/`, `<<`, `>>`}. One tricky detail here: There is some duplication of labor between `rustc::middle::const_eval` and `rustc_trans::trans::consts`. It might be good to explore ways to try to factor out the common structure to the two passes (by abstracting over the particular value-representation used in the compile-time interpreter). ---- Update: Rebased atop #23841 Fix #22531 Fix #23030 Fix #23221 Fix #23235
2015-04-01Added overflowing_{div,rem,shl,shr} method implementations to WrappingOps.Felix S. Klock II-0/+130
2015-03-31Test fixes and rebase conflicts, round 2Alex Crichton-1/+1
2015-03-31Stabilize std::numAaron Turon-200/+394
This commit stabilizes the `std::num` module: * The `Int` and `Float` traits are deprecated in favor of (1) the newly-added inherent methods and (2) the generic traits available in rust-lang/num. * The `Zero` and `One` traits are reintroduced in `std::num`, which together with various other traits allow you to recover the most common forms of generic programming. * The `FromStrRadix` trait, and associated free function, is deprecated in favor of inherent implementations. * A wide range of methods and constants for both integers and floating point numbers are now `#[stable]`, having been adjusted for integer guidelines. * `is_positive` and `is_negative` are renamed to `is_sign_positive` and `is_sign_negative`, in order to address #22985 * The `Wrapping` type is moved to `std::num` and stabilized; `WrappingOps` is deprecated in favor of inherent methods on the integer types, and direct implementation of operations on `Wrapping<X>` for each concrete integer type `X`. Closes #22985 Closes #21069 [breaking-change]
2015-03-30Change the type of constants BYTES/BITS to usizeVadim Petrochenkov-4/+4
2015-03-28Remove IteratorExtSteven Fackler-1/+1
All methods are inlined into Iterator with `Self: Sized` bounds to make sure Iterator is still object safe. [breaking-change]
2015-03-27rollup merge of #23741: alexcrichton/remove-int-uintAlex Crichton-66/+68
Conflicts: src/librustc/middle/ty.rs src/librustc_trans/trans/adt.rs src/librustc_typeck/check/mod.rs src/libserialize/json.rs src/test/run-pass/spawn-fn.rs