Commit Graph

873 Commits

Author SHA1 Message Date
Yoan Lecoq 52c87dd410 Remove 2018-01-05 10:01:29 +01:00
Yoan Lecoq 5a6997c1c8 Add RealNum trait 2018-01-05 09:06:23 +01:00
svartalf 53ab360d94 std::fmt::Display implemented for ParseFloatError 2018-01-04 16:46:02 +03:00
Fabian Schuiki 21dfae004c Add checked shifts
Add traits `CheckedShl` and `CheckedShr` that correspond to the standard
library's `checked_shl` and `checked_shr` functions. Implement the trait
on all primitive integer types by default, akin to what the standard
library does.

The stdlib is somewhat inconsistent when it comes to the type of the
shift amount. The `checked_*` functions have a `u32` shift amount, but
the `std::ops::{Shl,Shr}` traits are generic over the shift amount. Also
the stdlib implements these traits for all primitive integer types as
right-hand sides. Our implementation mimics this behaviour.
2018-01-03 16:25:13 +01:00
Eduardo Pinho af693fef48 Add safety notice in AsPrimitive docs 2017-12-21 21:03:25 +00:00
Eduardo Pinho 773a222237 Add conversions from `bool` in `AsPrimitive` 2017-12-20 21:41:45 +00:00
Eduardo Pinho 741e1f4e09 Add AsPrimitive trait, impls and tests 2017-12-20 00:05:36 +00:00
Josh Stone 42a610d323 Move num-traits to its own repo
All the prior `num` history is kept, so old `num-traits` tags are still
valid, but future development here will be just for `num-traits`.
2017-12-18 17:35:41 -08:00
bors[bot] f172ef3a6b Merge #350
350: Avoid large intermediate product in LCM r=cuviper a=mhogrefe

Changed the implementation of BigUint LCM from
`((self * other) / self.gcd(other))`
to
`self / self.gcd(other) * other`

The division is exact in both cases, so the result is the same, but the new code avoids the potentially-large intermediate product, speeding things up and using less memory.

I also removed the unnecessary parentheses, because I think it's clear what order everything will be executed in. But if others think differently I can add them back.
2017-12-14 08:07:15 +00:00
bors[bot] a0c431e270 Merge #351
351: Remove num-macros r=cuviper a=cuviper

The first commit gives a final deprecation bump to `num-macros`, and
the second removes it from the repo altogether.
2017-12-14 07:54:24 +00:00
Josh Stone d90ae0ae8b Remove num-macros 2017-12-13 23:44:49 -08:00
Josh Stone bd1701ded4 macros: bump to 0.1.40, deprecated 2017-12-13 23:41:50 -08:00
Mikhail Hogrefe 56a029b20f Avoid large intermediate product in LCM 2017-12-13 21:13:57 -05:00
Josh Stone c24f76781b num: bump to 0.1.41
Syncing the metacrate to the latest of all subcrates.
2017-12-01 13:31:33 -08:00
Josh Stone 98cb815183 bigint: bump to 0.1.41
- Now uses Toom-3 multiplication for large inputs.
- `BigInt`/`BigUint` parsing now accepts `_` separating digits.
- `BigInt`/`BigUint::assign_from_slice` reinitializes the value, keeping
  the same internal buffer.
- `BigUint` now implements many `*Assign` ops.
- `BigUint::modpow(exp, mod)` performs efficient modular exponentiation.
2017-12-01 13:15:22 -08:00
Josh Stone 18b48f335e complex: bump to 0.1.41
`Complex` now implements `Num`, `Rem`, and `RemAssign`.

(Complex remainders don't have a clear mathematical basis, but we choose
to round toward zero to a gaussian integer.)
2017-12-01 13:02:13 -08:00
Josh Stone 0e1c4c8b65 traits: bump to 0.1.41
minor warning fixes
2017-12-01 12:54:48 -08:00
bors[bot] 0e31f3c6b3 Merge #345
345: Downgrade libc for Rust 1.8 CI r=cuviper a=cuviper
2017-12-01 01:30:41 +00:00
Josh Stone a7464b2b42 Roll back libc in the ci scripts too 2017-11-30 17:01:09 -08:00
Josh Stone d354559365 Downgrade libc for Rust 1.8 CI 2017-11-30 16:15:45 -08:00
Josh Stone cc3be86781 rational: bump to 0.1.40 2017-11-08 14:40:10 -08:00
bors[bot] ca7e438d77 Merge #342
342: rational: check for NaN when approximating floats r=cuviper a=cuviper

We had a test for NaN already, but thanks to undefined casts (#119) it
was only passing by luck -- on armv7hl it failed:

https://bugzilla.redhat.com/show_bug.cgi?id=1511187

Now we check for NaN explicitly.
2017-11-08 22:29:38 +00:00
Josh Stone b67f1bd6d6 rational: check for NaN when approximating floats
We had a test for NaN already, but thanks to undefined casts (#119) it
was only passing by luck -- on armv7hl it failed:

     https://bugzilla.redhat.com/show_bug.cgi?id=1511187

Now we check for NaN explicitly.
2017-11-08 14:09:05 -08:00
bors[bot] a203e9f9fc Merge #339
339: Implement modpow() for BigUint backed by Montgomery Multiplication r=cuviper a=str4d

Based on this Gist: https://gist.github.com/yshui/027eecdf95248ea69606

Also adds support to `BigUint.from_str_radix()` for using `_` as a visual separator.

Closes #136
2017-11-02 19:37:52 +00:00
Josh Stone ed10d617b5 bigint: Add a modpow fallback for even modulus 2017-10-22 16:44:05 -07:00
Josh Stone 35b7187e83 bigint::monty: use infallible conversions in tests 2017-10-22 15:49:51 -07:00
Josh Stone bb0c9324b2 bigint::monty: deduplicate mr.n and mr.p 2017-10-22 15:45:01 -07:00
Josh Stone b380880ed3 bigint::monty: simplify modpow zero test 2017-10-22 15:38:50 -07:00
Josh Stone 96c4a26624 bigint::monty: simplify modpow parameter init 2017-10-22 15:37:48 -07:00
Josh Stone 7fa27b6007 bigint::monty: simplify redc return value 2017-10-22 15:36:33 -07:00
Josh Stone 5708db0f67 bigint::monty: simplify redc masks 2017-10-22 15:34:46 -07:00
Josh Stone 5a0de140c9 bigint::monty: use mac_digit 2017-10-22 15:30:17 -07:00
Josh Stone 4d35815426 bigint::monty: simplify work space allocation 2017-10-22 15:28:59 -07:00
Josh Stone aea5f85216 bigint::monty: store the inverse as u32 2017-10-22 15:15:02 -07:00
Josh Stone c2fba06787 bigint: less pub in monty 2017-10-22 15:05:16 -07:00
Josh Stone 2a1fe6e7ef bigint: fix parsing leading _ and test more 2017-10-22 14:57:52 -07:00
bors[bot] fc39e1beaa Merge #340
340: Fix documentation formatting with commonmark enabled r=cuviper a=mbrubeck

This makes formatting correct with the new pulldown-cmark Markdown parser (rust-lang/rust#44229).
2017-10-22 21:12:28 +00:00
Matt Brubeck 531c2a754f Fix documentation formatting with commonmark enabled
This makes formatting correct with the new pulldown-cmark Markdown
parser (rust-lang/rust#44229).
2017-10-17 10:16:01 -07:00
str4d 720893f67b
Add support to BigUint.from_str_radix() for using _ as a visual separator 2017-10-09 16:11:18 +01:00
str4d f523b9c359
Implement modpow() for BigUint backed by Montgomery Multiplication
Based on this Gist: https://gist.github.com/yshui/027eecdf95248ea69606

Closes #136
2017-10-09 16:09:49 +01:00
bors[bot] 741a5a6207 Merge #335
335: Clean up some warnings r=cuviper a=cuviper
2017-09-22 00:38:06 +00:00
Josh Stone 2f8f952d1d clean up unused macros 2017-09-21 17:36:21 -07:00
Josh Stone 2a9750ada4 bigint: remove an unused mut 2017-09-21 17:28:37 -07:00
Josh Stone 7679cb86fb Remove `#[must_use]` on `__add2`
It doesn't actually work on functions yet, and nightly now warns that it
is experimental, behind `#[feature(fn_must_use)]`.
2017-09-21 17:26:20 -07:00
bors[bot] 4896746fec Merge #328
328: Optimizing BigUint and Bigint multiplication with the Toom-3 algorithm r=cuviper a=kompass

Hi !

I finally implemented the Toom-3 algorithm ! I first tried to minimize the memory allocations by allocating the `Vec<BigDigit>` myself, as was done for Toom-2, but Toom-3 needs more complex calculations, with negative numbers. So I gave up this method, to use `BigInt` directly, and it's already faster ! I also chose a better threshold for the Toom-2 algorithm.

Before any modification :
```
running 4 tests
test multiply_0        ... bench:         257 ns/iter (+/- 25)
test multiply_1        ... bench:      30,240 ns/iter (+/- 1,651)
test multiply_2        ... bench:   2,752,360 ns/iter (+/- 52,102)
test multiply_3        ... bench:  11,618,575 ns/iter (+/- 266,286)
```

With a better Toom-2 threshold (16 instead of 4) :
```
running 4 tests
test multiply_0        ... bench:         130 ns/iter (+/- 8)
test multiply_1        ... bench:      19,772 ns/iter (+/- 1,083)
test multiply_2        ... bench:   1,340,644 ns/iter (+/- 17,987)
test multiply_3        ... bench:   7,302,854 ns/iter (+/- 82,060)
```

With the Toom-3 algorithm (with a threshold of 300):
```
running 4 tests
test multiply_0        ... bench:         123 ns/iter (+/- 3)
test multiply_1        ... bench:      19,689 ns/iter (+/- 837)
test multiply_2        ... bench:   1,189,589 ns/iter (+/- 29,101)
test multiply_3        ... bench:   3,014,225 ns/iter (+/- 61,222)
```

I think this could be optimized, but it's a first step !
2017-09-20 20:53:40 +00:00
Josh Stone 1ddbee7f37 bigint mac3: tweak thresholds between algorithms
It's not too rigorous, but thresholds 32 and 256 give me better results.

Before:

     test multiply_0        ... bench:          87 ns/iter (+/- 0)
     test multiply_1        ... bench:      11,926 ns/iter (+/- 19)
     test multiply_2        ... bench:     772,178 ns/iter (+/- 3,068)
     test multiply_3        ... bench:   2,034,237 ns/iter (+/- 9,618)

After:

     test multiply_0        ... bench:          87 ns/iter (+/- 0)
     test multiply_1        ... bench:      11,927 ns/iter (+/- 64)
     test multiply_2        ... bench:     672,440 ns/iter (+/- 3,570)
     test multiply_3        ... bench:   1,577,065 ns/iter (+/- 11,137)
2017-09-20 13:19:00 -07:00
Josh Stone 28d84ca3ac Toom-3: operate more on values where possible 2017-09-20 13:17:06 -07:00
Josh Stone 2c2e46c8df Add comments about multiplication strategy 2017-09-20 13:15:44 -07:00
Josh Stone 05dc87c041 Improve mac_digit bounds checking
By starting with `split_at_mut`, the hot multiplication loop runs with
no bounds checking at all!  The remaining carry loop has a slightly
simpler check for when the remaining iterator runs dry.
2017-09-20 11:41:59 -07:00
bors[bot] 8646be5a95 Merge #330
330: Implement *Assign for BigUint r=cuviper a=Darksonn

Not only does this change increase convenience of use, it also allows adding a `&BigUint` to a `&mut BigUint` without allocating (if not necessary) or tricks such as:

    fn add(a: &mut BigUint, b: &BigUint) {
        let aa = mem::replace(a, BigUint::from_slice(&[])); // BigUint::from_slice(&[]) does not allocate
        *a = aa + b;
    }

With this change:

    fn add(a: &mut BigUint, b: &BigUint) {
        *a += b;
    }

It would make sense to add the same functionality to `BigInt`, but it uses some macros to handle the signs, and I'm not sure how to change the macros in order to perform this change.
2017-09-20 01:11:28 +00:00