88 Commits

Author SHA1 Message Date
Andrew Kelley
902df103c6 std lib API deprecations for the upcoming 0.9.0 release
See #3811
2021-11-30 00:13:07 -07:00
Andrew Kelley
d6067db062 stage2: implement @popCount for non-vectors 2021-10-29 17:49:02 -07:00
Robin Voetter
df167af0b6
Revert 83bdbb2 and a587dd0 (#10028) 2021-10-25 14:00:10 +02:00
Robin Voetter
c563521d44 big ints: tighten some more division memory requirements 2021-10-24 02:56:48 +02:00
Robin Voetter
83bdbb2abb big ints: Make calcLimbLen always work at comptime, even if parameter is runtime 2021-10-24 02:56:48 +02:00
Robin Voetter
c905ceb23c big ints: fix divFloor 2021-10-24 02:56:48 +02:00
Robin Voetter
87b7b31557 big ints: improve division 2021-10-24 01:21:33 +02:00
Andrew Kelley
7f70c27e9d stage2: more division support
AIR:
 * div is renamed to div_trunc.
 * Add div_float, div_floor, div_exact.
   - Implemented in Sema and LLVM codegen. C backend has a stub.

Improvements to std.math.big.Int:
 * Add `eqZero` function to `Mutable`.
 * Fix incorrect results for `divFloor`.

Compiler-rt:
 * Add muloti4 to the stage2 section.
2021-10-21 19:05:26 -07:00
Robin Voetter
a3c9bfef30 stage2: truncation
* Also fixes a related case where big int truncate would assume that the
input fits in the output limbs buffer
2021-10-21 20:10:27 -04:00
Robin Voetter
6a3659c4e0 big.int: 2s-complement binary wrapping not 2021-10-17 20:33:04 +02:00
Robin Voetter
1e09157b53 big ints: Fix set(signed int minimum) panic 2021-10-16 11:32:05 +02:00
Robin Voetter
efa4f76c8b big ints: Saturating left shift + tests 2021-10-16 11:32:05 +02:00
Andrew Kelley
78902db68b stage2: fix comptime @bitCast
Before, Sema for comptime `@bitCast` would return the same Value but
change the Type. This gave invalid results because, for example, an
integer Value when the Type is a float would be interpreted numerically,
but `@bitCast` needs it to reinterpret how they would be stored in
memory.

This requires a mechanism to serialize a Value to a byte buffer and
deserialize a Value from a byte buffer.

Not done yet, but needs to happen: comptime dereferencing a pointer
to a Decl needs to perform a comptime bitcast on the loaded value.
Currently the value is silently wrong in the same way that `@bitCast`
was silently wrong before this commit.

The logic in Value for handling readFromMemory for large integers is
only correct for small integers. It needs to be fleshed out for proper
big integers.

As part of this change:
 * std.math.big.Int: initial implementations of readTwosComplement and
   writeTwosComplement. They only support bit_count <= 128 so far and
   panic otherwise.
 * compiler-rt: move the compareXf2 exports over to the stage2 section.
   Even with the improvements in this commit, I'm still seeing test
   failures in the widening behavior tests; more investigation is
   needed.
2021-10-04 23:30:04 -07:00
Robin Voetter
a1e02f33ad fmt 2021-10-04 11:25:29 +02:00
Robin Voetter
3dc47b8161 Apply new big int wrap/saturate to Value.zig 2021-10-04 11:25:29 +02:00
Robin Voetter
b352564b36 big ints: Some extra comments 2021-10-04 11:25:29 +02:00
Robin Voetter
bbd50248f2 big ints: saturate() function 2021-10-04 11:25:29 +02:00
Robin Voetter
fdf13fb819 big ints: Wrapping multiplication 2021-10-04 11:25:29 +02:00
Robin Voetter
41e9c1bac1 big ints: Allow llmulaccum to wrap 2021-10-04 11:25:29 +02:00
Robin Voetter
5907b3e383 big ints: Improve karatsuba multiplication 2021-10-04 11:25:29 +02:00
Robin Voetter
bb53f4f15a big ints: implement normal/wrapping/saturating subtraction in terms of addition 2021-10-04 11:25:29 +02:00
Robin Voetter
16991f920b big ints: saturating addition 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
dc1f698545 big ints: unify add/sub with their wrapping variants 2021-10-04 11:25:29 +02:00
Robin Voetter
a36ef84deb big ints: Basic wrapping multiplication 2021-10-04 11:25:29 +02:00
Robin Voetter
69be6ba8ee Comptime wrapping addition/subtraction 2021-10-04 11:25:29 +02:00
Robin Voetter
fdb37743fa big ints: addWrap, subWrap + fix Managed.truncate allocation size 2021-10-04 11:25:29 +02:00
Robin Voetter
b58cf6dab6 big ints: 2s complement truncate 2021-10-04 11:25:29 +02:00
Robin Voetter
616b23c815 big ints: split lladd/llsub into carry variants
lladd is now implemented in terms of lladdcarry, which returns the carry limb.
Similarly, llsub is implemented using llsubcarry, which returns the borrow limb.
2021-10-04 11:25:29 +02:00
Robin Voetter
cd3dcc225b big ints: only write xor overflow if required 2021-09-23 06:28:09 +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
4b2d7a9c67 stage2: implement comptime bitwise nand 2021-09-20 15:44:09 -07:00
Andrew Kelley
c05a20fc8c std: reorganization that allows new usingnamespace semantics
The proposal #9629 is now accepted, usingnamespace stays but no longer
puts identifiers in scope.
2021-09-01 17:54:06 -07: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
Jacob G-W
9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Jacob G-W
641ecc260f std, src, doc, test: remove unused variables 2021-06-21 17:03:03 -07:00
Asherah Connor
e56ba4cee1 bigint: add ensureAdd(Scalar)Capacity, note aliasing requirements 2021-06-11 16:18:10 +03:00
Asherah Connor
b15e205438 bigint: use a stack local here to prevent aliasing issues 2021-06-11 16:18:10 +03:00
daurnimator
916b645fc1 Have std.fmt functions take case as an enum 2021-06-10 08:33:42 +03:00
Benjamin Graf
c70832bc41 replace ArrayList.shrinkAndFree by ArrayList.shrinkRetainingCapacity 2021-02-21 11:56:14 +02: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
LemonBoy
134f5fd3d6 std: Update test "" to test where it makes sense 2021-01-22 15:46:58 +01:00
Alex Cameron
89286376c6 std: Rename ArrayList shrink => shrinkAndFree 2021-01-06 00:55:51 +11:00
Frank Denis
6c2e0c2046 Year++ 2020-12-31 15:45:24 -08:00
Tadeo Kondrak
25ec2dbc1e Add builtin.Signedness, use it instead of is_signed 2020-11-19 18:59:21 +02:00
Jan Prudil
aadccc4206 Make std.meta.Int accept a signedness parameter 2020-10-17 14:09:59 +02: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