Commit Graph

67 Commits

Author SHA1 Message Date
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
LemonBoy
6f220bb7ca std: explicitly cast indices to usize
This is needed for platforms where usize=u16, eg. MSP430.
2020-12-09 16:48:41 +01:00
Tadeo Kondrak
25ec2dbc1e Add builtin.Signedness, use it instead of is_signed 2020-11-19 18:59:21 +02:00
xackus
5c8f7f81cd change debug.assert to testing.expect in tests 2020-11-06 22:48:54 +01:00
LemonBoy
ff14451b4a std: Implement more useful approxEq semantics
Comparisons with absolute epsilons are usually useful when comparing
numbers to zero, for non-zero numbers it's advised to switch to relative
epsilons instead to obtain meaningful results (check [1] for more
details).

The new API introduces approxEqAbs and approxEqRel, where the former
aliases and deprecated the old `approxEq`, allowing the user to pick the
right tool for the job.

The documentation is meant to guide the user in the choice of the
correct alternative.

[1] https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
2020-11-05 16:08:49 +01: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
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
Vincent Rischmann
533bfc68bf big int: fix Managed.dump() 2020-09-07 20:44:01 +03:00
Vexu
1df0f3ac24
update uses of deprecated type field access 2020-09-03 18:10:40 +03:00
Frank Denis
ad18078d53 forceEval() -> doNotOptimizeAway() 2020-08-26 10:50:34 +02:00
Andrew Kelley
b498eebfd4 std.math.big: fix use-after-free
When there is parameter aliasing, the ensureCapacity calls can cause the
Const parameters to become dangling pointers.

See #6167
2020-08-25 19:49:40 -07: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
Vexu
e85fe13e44
run zig fmt on std lib and self hosted 2020-07-11 20:41:19 +03:00
joachimschmidt557
0ae1157e45 std.mem.dupe is deprecated, move all references in std
Replaced all occurences of std.mem.dupe in stdlib with
Allocator.dupe/std.mem.dupeZ -> Allocator.dupeZ
2020-07-04 21:40:06 +03:00
antlilja
eb7fad28f8 Improve f128 standard library support
* Add functions: floor128, ceil128, trunc128 and round128
* Add corresponding tests
2020-06-17 18:18:45 +02:00
Jonathan Marler
12051b02f1 fix memory errors 2020-06-09 00:17:22 -04:00
Andrew Kelley
ec6ef86219 fix off-by-one error in sizeInBaseUpperBound 2020-05-01 13:33:46 -04:00
Andrew Kelley
4044a77621 update std.meta.IntType => std.meta.Int 2020-05-01 06:49:30 -04: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
Andrew Kelley
28729efe29 ZIR: implement return instruction 2020-05-01 06:47:20 -04:00
Andrew Kelley
6b0f7de247 ZIR: add cmp and condbr instructions 2020-05-01 06:47:20 -04:00
Tadeo Kondrak
350b2adacd
std.meta.IntType -> std.meta.Int 2020-04-28 19:11:31 -06:00
Nick Appleton
28c31a8429
Fix f64 variants of math.cosh and math.sinh to accept negative inputs. (#5172)
* add tests for negative inputs to cosh32 and cosh64. fix bug in cosh64 for negative inputs.

* fix problem with negative input with f64 sinh and add tests
2020-04-26 14:03:19 -04:00
Andrew Kelley
1eda2ada9a std.math.big.Int: don't rely on the allocator when printing 2020-04-22 03:49:50 -04:00
Andrew Kelley
2cdbb5f472 ir: analyze int casting 2020-04-21 19:48:59 -04:00
joachimschmidt557
3fd38429e4 Enable formatting in std.big.Int.format 2020-04-15 12:51:43 -04:00
Vexu
ca3bf6e6ad
translate-c cleanup and zig fmt 2020-04-15 15:15:32 +03:00
joachimschmidt557
1ee59c5c31 move big.rational.gcd to big.int.gcd 2020-04-07 13:43:15 -04:00
xackus
7a28c644aa new ArrayList API: fix everything else 2020-04-02 16:12:08 +02:00
Andrew Kelley
93c7fa105f
Merge remote-tracking branch 'origin/llvm10'
LLVM 10 was released today
2020-03-24 09:57:09 -04:00
Joachim Schmidt
5acc8afb5f
Use math.Order for comparing bigints instead of i8 (#4791) 2020-03-23 23:16:57 -04:00
Andrew Kelley
13d04f9963
Merge pull request #4741 from momumi/master
allow `_` separators in number literals (stage 1)
2020-03-23 00:54:54 -04:00
momumi
8de45e5143 update parsing of int literals in self-hosted
* update std.math.big.Int.setString() to ignore underscores and make it
  case insensitive
* fix issue in ir.zig with leading zeroes in integer literals
2020-03-22 13:59:14 +10:00
Andrew Kelley
53b5aa812b
Merge remote-tracking branch 'origin/master' into llvm10 2020-03-19 22:19:24 -04:00
Andrew Kelley
dbde5df568
clean up some self-hosted bitrot + don't assume libstdc++
closes #4682

The self-hosted compiler is still bit rotted and still not compiling
successfully yet. I have a more serious rework of the code in a
different branch.
2020-03-17 23:03:45 -04:00
Andrew Kelley
656ba530d8
Merge remote-tracking branch 'origin/master' into llvm10 2020-03-13 15:17:53 -04:00
Benjamin Feng
4aae55b4cc Replace fmt with new fmtstream 2020-03-12 10:41:09 -05:00
Benjamin Feng
c11d1055b8 Integrated outstreams with new formatter 2020-03-12 10:26:28 -05:00
Andrew Kelley
d1cb16aace
Merge remote-tracking branch 'origin/master' into llvm10 2020-03-03 09:44:13 -05:00
xackus
00be934569 short std.builtin enum literals in std lib 2020-03-01 13:57:41 -05:00
Andrew Kelley
87b9e744dd
update std lib to new Target API 2020-02-28 14:51:54 -05:00
Andrew Kelley
f33bf48af7
Merge remote-tracking branch 'origin/master' into llvm10 2020-02-25 16:30:40 -05:00
Vexu
538d9a5dd8
remove uses of @ArgType and @IntType 2020-02-24 23:39:03 +02:00
Vexu
45da72c5b6
remove usages of @typeId, @memberCount, @memberName and @memberType 2020-02-24 23:09:01 +02:00
Andrew Kelley
a8b36fbe34
Merge remote-tracking branch 'origin/master' into llvm10 2020-02-14 10:27:44 -05:00