74 Commits

Author SHA1 Message Date
Robin Voetter
95fe86e3db big ints: Fix tests for 32-bit architectures 2021-10-04 11:25:29 +02:00
Robin Voetter
a62ce87f1f big ints: mulWrap tests 2021-10-04 11:25:29 +02:00
Robin Voetter
ebcdfebaa6 big ints: saturate() tests 2021-10-04 11:25:29 +02:00
Robin Voetter
15351f206f big.int: truncate tests 2021-10-04 11:25:29 +02:00
Robin Voetter
692827baa7 big ints: [add|sub]Sat tests 2021-10-04 11:25:29 +02:00
Robin Voetter
52721d3a7e big ints: [add|sub]Wrap tests 2021-10-04 11:25:29 +02:00
Robin Voetter
f1b3a90ef6 big ints: setTwosCompIntLimit
This function can be used to initialize a big integer to either the upper
or lower limit of a 2s-complement integer. Note that the result is still
in sign-magnitude representation, though in order to convert it into twos
complement all one has to do is take the absolute value.
2021-10-04 11:25:29 +02:00
Robin Voetter
351e4f07ce big ints: 2s complement signed and + or fixes 2021-09-23 06:19:08 +02:00
Robin Voetter
f5c27dd11a big ints: 2s complement signed or 2021-09-23 04:08:27 +02:00
Robin Voetter
4afe4bdfe7 big ints: 2s complement signed xor 2021-09-22 02:21:55 -04:00
Andrew Kelley
d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00
Asherah Connor
e56ba4cee1 bigint: add ensureAdd(Scalar)Capacity, note aliasing requirements 2021-06-11 16:18:10 +03:00
Asherah Connor
1db6018140 bigint: add failing tests for bigint carry 2021-06-11 16:18:06 +03:00
daurnimator
916b645fc1 Have std.fmt functions take case as an enum 2021-06-10 08:33:42 +03:00
Veikka Tuominen
fd77f2cfed std: update usage of std.testing 2021-05-08 15:15:30 +03:00
Frank Denis
a03f9548d3 std/math/big/int: normalize after a right shift
After a right shift, top limbs may be all zero. However, without
normalization, the number of limbs is not going to change.

In order to check if a big number is zero, we used to assume that the
number of limbs is 1. Which may not be the case after right shifts,
even if the actual value is zero.

- Normalize after a right shift
- Add a test for that issue
- Check all the limbs in `eqlZero()`. It may not be necessary if
callers always remember to normalize before calling the function.
But checking all the limbs is very cheap and makes the function less
bug-prone.
2021-02-01 12:10:01 -08:00
Frank Denis
6c2e0c2046 Year++ 2020-12-31 15:45:24 -08:00
LemonBoy
a31b70c4b8 std: Add/Fix/Change parts of big.int
* Add an optimized squaring routine under the `sqr` name.
  Algorithms for squaring bigger numbers efficiently will come in a
  PR later.
* Fix a bug where a multiplication was done twice if the threshold for
  the use of Karatsuba algorithm was crossed. Add a test to make sure
  this won't happen again.
* Streamline `pow` method, take a `Const` parameter.
* Minor tweaks to `pow`, avoid bit-reversing the exponent.
2020-10-09 22:16:48 -04:00
LemonBoy
dbc11be038 std: Fix two bugs in bigint pow
* Correctly scan all the exponent bits, this caused the incorrect result
  to be computed for exponents being powers of two.
* Allocate enough limbs to make llmulacc stop whining.
2020-10-05 22:16:26 -04:00
LemonBoy
538d485782 std: Add pow(a,b) for big ints
Implemented following Knuth's "Evaluation of Powers" chapter in TAOCP,
some extra complexity is needed to make sure there's no aliasing and
avoid allocating too many limbs.

A brief example to illustrate why the last point is important:
consider 10^123, since 10 is well within the limits of a single limb we
can safely say that the result will surely fit in:

⌈log2(10)⌉ bit * 123 = 492 bits = 7 limbs

A naive calculation using only the number of limbs yields:

1 limb * 123 = 123 limbs

The space savings are noticeable.
2020-10-04 02:24:40 -04:00
Vexu
1df0f3ac24
update uses of deprecated type field access 2020-09-03 18:10:40 +03:00
Andrew Kelley
4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00
joachimschmidt557
616355807f Fix bug in big.int.Mutable.toManaged() and add tests
Fixes #5918
2020-07-27 07:16:44 +00:00
Andrew Kelley
8766821157 rework std.math.big.Int
Now there are 3 types:
 * std.math.big.int.Const
   - the memory is immutable, only stores limbs and is_positive
   - all methods operating on constant data go here
 * std.math.big.int.Mutable
   - the memory is mutable, stores capacity in addition to limbs and
     is_positive
   - methods here have some Mutable parameters and some Const
     parameters. These methods expect callers to pre-calculate the
     amount of resources required, and asserts that the resources are
     available.
 * std.math.big.int.Managed
   - the memory is mutable and additionally stores an allocator.
   - methods here perform the resource calculations for the programmer.
   - this is the high level abstraction from before

Each of these 3 types can be converted to the other ones.

You can see the use case for this in the self-hosted compiler, where we
only store limbs, and construct the big ints as needed.

This gets rid of the hack where the allocator was optional and the
notion of "fixed" versions of the struct. Such things are now modeled
with the `big.int.Const` type.
2020-05-01 06:47:56 -04:00