227 Commits

Author SHA1 Message Date
Andrew Kelley
f466667888 stage2: fix crash on comptime lazy @ctz and @clz 2022-11-29 23:30:38 -07:00
Andrew Kelley
ceb0a632cf std.mem.Allocator: allow shrink to fail
closes #13535
2022-11-29 23:30:38 -07:00
Jakub Konka
648579b330 libstd: skip problematic tests on aarch64-windows 2022-11-30 00:26:40 +01:00
Jacob Young
d9a51648e6 std.big.int.Mutable: fix set(@as(DoubleLimb, 0))
Previously, this would set len to 1 but fail to initialize any limbs.
2022-11-28 07:40:09 -05:00
Stevie Hryciw
5f6f38ff31 std.math.big.int: implement popCount() for Const 2022-11-18 14:31:30 +02:00
Nick Cernis
8a5818535b
Make invalidFmtError public and use in place of compileErrors for bad format strings (#13526)
* Export invalidFmtErr

To allow consistent use of "invalid format string" compile error
response for badly formatted format strings.

See https://github.com/ziglang/zig/pull/13489#issuecomment-1311759340.

* Replace format compile errors with invalidFmtErr

- Provides more consistent compile errors.
- Gives user info about the type of the badly formated value.

* Rename invalidFmtErr as invalidFmtError

For consistency. Zig seems to use “Error” more often than “Err”.

* std: add invalid format string checks to remaining custom formatters

* pass reference-trace to comp when building build file; fix checkobjectstep
2022-11-12 21:03:24 +02:00
Cody Tapscott
3295fee911 stage2: Use mem.readPackedInt etc. for packed bitcasts
Packed memory has a well-defined layout that doesn't require
conversion from an integer to read from. Let's use it :-)

This change means that for bitcasting to/from a packed value that
is N layers deep, we no longer have to create N temporary big-ints
and perform N copies.

Other miscellaneous improvements:
  - Adds support for casting to packed enums and vectors
  - Fixes bitcasting to/from vectors outside of a packed struct
  - Adds a fast path for bitcasting <= u/i64
  - Fixes bug when bitcasting f80 which would clear following fields

This also changes the bitcast memory layout of exotic integers on
big-endian systems to match what's empirically observed on our targets.
Technically, this layout is not guaranteed by LLVM so we should probably
ban bitcasts that reveal these padding bits, but for now this is an
improvement.
2022-10-28 08:41:04 -07:00
Andrew Kelley
66d6183001 Merge branch 'amdgpu-improvements' of https://github.com/Snektron/zig into Snektron-amdgpu-improvements 2022-10-15 10:36:10 -07:00
Jacob Young
02d7292a8c build.zig: Forward LLVM lib/include dirs from CMake
Previously, you might obtain `-lLLVM-15` from the CMake configuration,
but we might not be able to locate the library if it's not in your
system library path.
2022-10-12 18:11:46 -04:00
Robin Voetter
5859d8458f
big int: make Mutable.normalize const 2022-10-12 20:34:41 +02:00
Jacob Young
e78d7704a4 math.big.int: document the purpose of limb_len in scalar methods
Ideally this duplicated code could be factored out into a function, but
there doesn't seem to be any way in the Zig type system to represent an
argument to a function called at comptime that is only needed if it is
comptime-known.  Instead, we document what is going on in an adjacent
comment in case it gets copy-pasted into new methods in the future.
2022-10-12 08:56:13 -04:00
Jacob Young
38ee512a25 math.big.int: add calcLimbLen doc comment note
When trying to allocate memory for functions like `Managed.init` and
`Managed.set` on the stack, a comptime-known allocation size is desired.
The doc comments for these functions indicate that `calcLimbLen` can be
used to determine how many limbs to allocate, but if `value` is not
comptime-known, then neither is `calcLimbLen(value)`.  However, an upper
bound on the allocation size is still computable at comptime in this
case, so this note documents an expression that can be used, rather than
trying to add it to every doc comment that mentions `calcLimbLen`.
2022-10-12 08:18:47 -04:00
Jacob Young
2fe5bdb9ed big.int: rewrite confusing code in an equivalent but less confusing way 2022-10-11 19:57:13 -04:00
Jacob Young
5c49341f09 big.int: add support for non-comptime scalars 2022-10-11 19:51:03 -04:00
GethDW
01b9fa2c25
std: fix memory leak on OutOfMemory error in math.big.int and math.big.rationa 2022-10-11 20:12:03 +03:00
Cody Tapscott
879fb0c57c Manually construct denormal constants in tests
Constructing these at runtime can cause them to be flushed to zero,
which was triggering a CI failure for Windows.
2022-10-09 20:29:30 -07:00
Cody Tapscott
eac1e613be compiler_rt: Re-implement ldexp/ilogb using bit-ops
This re-write was needed to fix deficiencies in the existing ldexp,
which was failing to compute correct results for both f16 and f80.

It would be nice to add a fast multiplication-based fallback in the
future for targets that have a hardware FPU, but this implementation
should be much faster than the existing for targets without one.
2022-10-09 11:09:02 -07:00
Chris Boesch
9c99a88796
std.math: add "Greatest common divisor" (gcd) 2022-09-29 21:42:56 +03:00
Andrew Kelley
d3d24874c9 std: remove deprecated API for the upcoming release
See #3811
2022-09-16 14:46:53 -04:00
Yujiri
ae3a5ff7f9 Fix #12440: std.math.big.Rational order/orderAbs 2022-09-04 18:45:20 +03:00
Andrew Kelley
2a96209c40
Merge pull request #12574 from Vexu/remove-bit-op-type-param
stage2+stage1: remove type parameter from bit builtins
2022-08-24 15:57:44 -04:00
Veikka Tuominen
62ff8871ed stage2+stage1: remove type parameter from bit builtins
Closes #12529
Closes #12511
Closes #6835
2022-08-22 11:19:20 +03:00
Veikka Tuominen
b55a5007fa Sema: fix parameter of type 'T' must be comptime error
Closes #12519
Closes #12505
2022-08-22 11:16:36 +03:00
Hiroaki Nakamura
3e667fd292 Use Managed.len in sub, divFloor, and divTrunc too 2022-07-16 11:46:13 +09:00
Hiroaki Nakamura
0cee8372cf Use Managed.len() instead of Managed.toConst().limbs.len 2022-07-16 08:14:16 +09:00
Hiroaki Nakamura
d63604b116 Fix std.math.big.int.Managed capacity after mul and sqr 2022-07-16 00:08:55 +09:00
Andrew Kelley
92bc3cbe27 stage2: fix comptime bitcast involving f80
* Sema: implement comptime bitcast of f80 with integer-like types
  bitwise rather than taking a round trip through memory layout.
* Type: introduce `isAbiInt`.
* Value: comptime memory write of f80 writes 0 bytes for padding
  instead of leaving the memory uninitialized.
* Value: floatReadFromMemory has a more general implementation, checking
  the endianness rather than checking for specific architectures.

This fixes behavior test failures occurring on MIPS.
2022-07-13 13:14:37 -07:00
Andrew Kelley
efe34243c6 std.math: add inline to some functions
These functions semantically benefit from being inline; it makes sense
that `isInf(x)` where `x` is comptime-known should have a
comptime-known result.
2022-07-13 11:14:46 -07:00
Andrew Kelley
6bc6e47b15 stage2: lower float negation explicitly
Rather than lowering float negation as `0.0 - x`.

 * Add AIR instruction for float negation.
 * Add compiler-rt functions for f128, f80 negation

closes #11853
2022-06-30 00:02:00 -07:00
Andrew Kelley
54454fd010 std.math.big.int: breaking API changes to prevent UAF
Many of the Managed methods accepted by-val parameters which could
reference Limb slices that became invalid memory after any
ensureCapacity calls. Now, Managed methods accept `*const Managed`
parameters so that if the function allows aliasing and the
ensure-capacity call resizes the Limb slice, it also affects the
aliased parameters, avoiding use-after-free bugs.

This is a breaking change that reduces the requirement for callsites to
manually make the ensure-capacity changes prior to calling many of the
Managed methods.

Closes #11897
2022-06-29 22:06:27 -04:00
Mikael Berthe
47c4d44502
std.math.big.int: update Managed.toString() to use provided allocator (#11839) 2022-06-13 17:19:37 +02:00
Veikka Tuominen
6b36774adc std: disable failing tests, add zig2 build test-std to CI 2022-06-12 10:43:28 +03:00
Cody Tapscott
70dc910086 std.math: Add O(log N) implementation of log2(x) for comptime_int
Since Zig provides @clz and not @ffs (find-first-set), log2 for comptime
integers needs to be computed algorithmically. To avoid hitting the
backward branch quota, this updates log2(x) to use a simple O(log N)
algorithm.
2022-06-07 20:07:40 -04:00
Veikka Tuominen
413577c881 std: adjust for stage2 semantics 2022-06-07 21:27:07 +03:00
Veikka Tuominen
9431100736 Sema: apply previous changes to validateUnionInit 2022-06-01 13:01:39 +03:00
Ali Chraghi
0e6285c8fc math: make cast return optional instead of an error 2022-05-27 16:43:33 -04:00
alice
951ab802a3
std.math: simpler error handling 2022-05-17 22:04:12 +01:00
alice
70b6b98e91
Simplify Copysign 2022-05-17 21:55:22 +01:00
alice
ceeec8d19f
Simplify signbit 2022-05-17 21:41:57 +01:00
leesongun
1de7b8d26c
std.math.powi: use standard definition of underflow/overflow, implement u0, i0, i1 edge case (#11499) 2022-05-16 18:28:20 -04:00
Andrew Kelley
0ffe82e624 std: use float builtins instead of std.math 2022-04-27 19:35:28 -07:00
Andrew Kelley
09f1d62bdf add new builtin function @tan
The reason for having `@tan` is that we already have `@sin` and `@cos`
because some targets have machine code instructions for them, but in the
case that the implementation needs to go into compiler-rt, sin, cos, and
tan all share a common dependency which includes a table of data. To
avoid duplicating this table of data, we promote tan to become a builtin
alongside sin and cos.

ZIR: The tag enum is at capacity so this commit moves
`field_call_bind_named` to be `extended`. I measured this as one of
the least used tags in the zig codebase.

Fix libc math suffix for `f32` being wrong in both stage1 and stage2.
stage1: add missing libc prefix for float functions.
2022-04-27 16:45:23 -07:00
Andrew Kelley
087aedfa38 stage2: fix recent LLVM backend code
* std.math.snan: fix compilation error. Also make it and nan inline.
 * LLVM: use a proper enum type for float op instead of enum literal.
   Also various cleanups.
 * LLVM: use LLVMBuildVectorSplat for vector splat AIR instruction.
   - also the bindings had parameter order wrong
 * LLVM: additionally handle f16 lowering. For now all targets report OK
   but I think we will need to add some exceptions to this list.
2022-04-27 14:18:34 -07:00
Andrew Kelley
41dd2beaac compiler-rt: math functions reorg
* unify the logic for exporting math functions from compiler-rt,
   with the appropriate suffixes and prefixes.
   - add all missing f128 and f80 exports. Functions with missing
     implementations call other functions and have TODO comments.
   - also add f16 functions
 * move math functions from freestanding libc to compiler-rt (#7265)
 * enable all the f128 and f80 code in the stage2 compiler and behavior
   tests (#11161).
 * update std lib to use builtins rather than `std.math`.
2022-04-27 12:20:44 -07:00
Cody Tapscott
f5540778b5 stdlib: Fix hex-float printing for f80 2022-04-25 17:21:09 -07:00
vi
4ad1480298
std.math.fabs: enable f80 testing 2022-04-15 16:52:02 -06:00
vi
2ef9a0d9ae
std.math: enable f80 tests on freebsd 2022-04-15 16:50:00 -06:00
Veikka Tuominen
845a30624f std: add workaround for stage2 bug 2022-04-15 11:17:19 +03:00
vi
6ad9ac59e7 std.math.float: fix f80-backed c_longdouble consts 2022-04-14 02:55:46 -04:00
Cody Tapscott
319555a669 Add floatFractionalBits to replace floatMantissaDigits 2022-04-12 12:33:16 -07:00