Commit graph

314 commits

Author SHA1 Message Date
Andrew Kelley
934573fc5d Revert "std.fmt: require specifier for unwrapping ?T and E!T."
This reverts commit 7cbd586ace.

This is causing a fail to build from source:

```
./lib/std/fmt.zig:492:17: error: cannot format optional without a specifier (i.e. {?} or {any})
                @compileError("cannot format optional without a specifier (i.e. {?} or {any})");
                ^
./src/link/MachO/Atom.zig:544:26: note: called from here
                log.debug("  RELA({s}) @ {x} => %{d} in object({d})", .{
                         ^
```

I looked at the code to fix it but none of those args are optionals.
2022-07-24 11:50:10 -07:00
InKryption
7cbd586ace
std.fmt: require specifier for unwrapping ?T and E!T.
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-07-24 12:01:56 +03:00
Yujiri
b8bf5de75a Fix #9184: fmtIntSizeDec/fmtIntSizeBin support FormatOptions 2022-07-21 18:41:21 +03:00
Duncan Holm
4bbc95b219
std.fmt: clarify the description of placeholders in Format Strings
Those 6 sets of square brackets are just a typographical aid used in this doc-comment, and must not actually be written by the user in their own format string... except for in one case where they must

* Avoid the implication that the numeric index is 1-based rather than 0-based
2022-07-11 11:16:28 +03:00
Cody Tapscott
680419c407 compiler_rt: Update Windows ABI for float<->int conversion routines
Starting with LLVM 14, the Libcalls to these functions are now lowered
using a Vec(2, u64) instead of the standard ABI for i128 integers, so
our compiler-rt implementation needs to be updated to expose the same
ABI on Windows.
2022-07-10 20:51:34 -07:00
Andrew Kelley
f9bf488926 two more regressed test cases; same cause as last two commits 2022-07-09 17:35:14 -07:00
Andrew Kelley
321fec1aa6 disable regressed float formatting test case
See #12063
2022-07-09 15:34:34 -07:00
Andrew Kelley
683ace7472 Merge remote-tracking branch 'origin/master' into llvm14 2022-07-05 21:56:55 -07:00
Andrew Kelley
6279a1d684 std: align some function pointers 2022-07-05 21:54:24 -07:00
Andrew Kelley
cbdd21cd9a std: disable tests regressed from LLVM 14 2022-07-03 15:39:39 -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
Veikka Tuominen
d0d5052b39 std.fmt: update test to stage2 fn pointer semantics 2022-06-20 16:12:40 +03:00
Veikka Tuominen
6b36774adc std: disable failing tests, add zig2 build test-std to CI 2022-06-12 10:43:28 +03:00
Veikka Tuominen
35c7e376b8 stage2: improve anon name strategy for local variables 2022-06-11 23:49:33 +03:00
Veikka Tuominen
002df65b6e Sema: handle tuple and anon_struct in resolveTypeFully 2022-06-11 11:02:56 +03:00
Veikka Tuominen
6d44c0a16c std: update tests to stage2 semantics 2022-06-03 20:21:20 +03:00
Veikka Tuominen
1a7b4ddeae std: disable tests that crash stage2 2022-06-03 20:21:20 +03:00
Ali Chraghi
0e6285c8fc math: make cast return optional instead of an error 2022-05-27 16:43:33 -04:00
Dominic Tarr
8171972cbb
document bufPrint, and format now uses writer not output 2022-05-23 11:58:13 +03:00
Marc Tiehuis
5fbda2c579 temporary fix for stage2/stage1 f128 rounding discrepency
This is only to get tests running again. The root issue should be fixed
in stage1 so rounding is consistent between stages.
2022-05-03 17:14:01 +12:00
Marc Tiehuis
2085a4af56 add new float-parser based on eisel-lemire algorithm
The previous float-parsing method was lacking in a lot of areas. This
commit introduces a state-of-the art implementation that is both
accurate and fast to std.

Code is derived from working repo https://github.com/tiehuis/zig-parsefloat.
This includes more test-cases and performance numbers that are present
in this commit.

* Accuracy

The primary testing regime has been using test-data found at
https://github.com/tiehuis/parse-number-fxx-test-data. This is a fork of
upstream with support for f128 test-cases added. This data has been
verified against other independent implementations and represents
accurate round-to-even IEEE-754 floating point semantics.

* Performance

Compared to the existing parseFloat implementation there is ~5-10x
performance improvement using the above corpus. (f128 parsing excluded
in below measurements).

** Old

    $ time ./test_all_fxx_data
    3520298/5296694 succeeded (1776396 fail)

    ________________________________________________________
    Executed in   28.68 secs    fish           external
       usr time   28.48 secs    0.00 micros   28.48 secs
       sys time    0.08 secs  694.00 micros    0.08 secs

** This Implementation

    $ time ./test_all_fxx_data
    5296693/5296694 succeeded (1 fail)

    ________________________________________________________
    Executed in    4.54 secs    fish           external
       usr time    4.37 secs  515.00 micros    4.37 secs
       sys time    0.10 secs  171.00 micros    0.10 secs

Further performance numbers can be seen using the
https://github.com/tiehuis/simple_fastfloat_benchmark/ repository, which
compares against some other well-known string-to-float conversion
functions. A breakdown can be found here:

0d9f020f1a/PERFORMANCE.md (commit-b15406a0d2e18b50a4b62fceb5a6a3bb60ca5706)

In summary, we are within 20% of the C++ reference implementation and
have about ~600-700MB/s throughput on a Intel I5-6500 3.5Ghz.

* F128 Support

Finally, f128 is now completely supported with full accuracy. This does
use a slower path which is possible to improve in future.

* Behavioural Changes

There are a few behavioural changes to note.

 - `parseHexFloat` is now redundant and these are now supported directly
   in `parseFloat`.
 - We implement round-to-even in all parsing routines. This is as
   specified by IEEE-754. Previous code used different rounding
   mechanisms (standard was round-to-zero, hex-parsing looked to use
   round-up) so there may be subtle differences.

Closes #2207.
Fixes #11169.
2022-05-03 16:46:40 +12:00
Isaac Freund
6f4343b61a std: replace usage of std.meta.bitCount() with @bitSizeOf() 2022-04-27 11:10:52 +02:00
Cody Tapscott
f5540778b5 stdlib: Fix hex-float printing for f80 2022-04-25 17:21:09 -07:00
r00ster91
62d717e2ff Add std.unicode.replacement_character 2022-04-15 11:20:11 +03:00
Andrew Kelley
2587474717 stage2: progress towards stage3
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly
   doing element casting for slice element types. This required an
   enhancement both to stage1 and stage2.
 * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that
   abused this is fixed.
 * Make some parameters comptime to support functions in switch
   expressions (as opposed to making them function pointers).
 * Avoid relying on local temporaries being mutable.
 * Workarounds for when stage1 and stage2 disagree on function pointer
   types.
 * Workaround recursive formatting bug with a `@panic("TODO")`.
 * Remove unreachable `else` prongs for some inferred error sets.

All in effort towards #89.
2022-04-14 10:12:45 -07:00
viri
e46c612503
use math/float.zig everywhere 2022-04-07 05:04:38 -06:00
Evan Haas
b4bf3bdf7e std.fmt: Fix incorrect behavior with large floating point integers.
I consider this an interim workaround/hack until #1299 is finished.

There is a bug in the original C implementation of the errol3 (and errol4)
algorithm that can result in undefined behavior or an obviously incorrect
result (leading ':' in the output)

This change checks for those two problems and uses a slower fallback
path if they occur. I can't guarantee that this will always produce
the correct result, but since the workaround is only used if the original
algorithm is guaranteed to fail, it should never turn a previously-correct
result into an incorrect one.

Fixes #11283
2022-04-04 16:04:35 -04:00
Meghan
b73cf97c93
replace other uses of std.meta.Vector with @Vector (#11346) 2022-03-30 14:12:14 -04:00
Veikka Tuominen
0577069af5 stage2 llvm: fix lowerDeclRefValue for function aliases 2022-03-21 15:12:32 -04:00
Robin Voetter
5c3325588e stage1: make type names more unique 2022-03-19 19:40:46 -04:00
Andrew Kelley
8841a71aa6 AstGen: evaluate comptime var init expressions in a comptime context 2022-02-19 15:11:18 -07:00
iddev5
77e5b042a0 fmt: allow uppercase prefix to be parsed in std.fmt.parseInt()
std.fmt.parseHexFloat allow both 0x and 0X as prefix, so in order to
keep things consistent. This commit modifies std.fmt.parseWithSign to
check for the prefix case insensitively in order to allow both upper
case and lower case prefix.

This change now allows: 0O, 0B and 0X as prefixes for parsing.
2022-02-16 11:27:10 +01:00
John Schmidt
adea9a1765
std.fmt: fix out-of-bounds array write in float printing
This commit fixes an out of bounds write that can occur when
formatting certain float values. The write messes up the stack and
causes incorrect results, segfaults, or nothing at all, depending on the
optimization mode used.

The `errol` function writes the digits of the float into `buffer`
starting from index 1, leaving index 0 untouched, and returns `buffer[1..]`
and the exponent. This is because `roundToPrecision` relies on index 0 being
unused in case the rounding adds a digit (e.g rounding 999.99
to 1000.00). When this happens, pointer arithmetic is used
[here](0e6d2184ca/lib/std/fmt/errol.zig (L61-L65))
to access index 0 and put the ones digit in the right place.

However, `errol3u` contains two special cases: `errolInt` and `errolFixed`,
which return from the function early. For these two special cases
index 0 was never reserved, and the return value contains `buffer`
instead of `buffer[1..]`. This causes the pointer arithmetic in
`roundToPrecision` to write out of bounds, which in the case of
`std.fmt.formatFloatDecimal` messes up the stack and causes undefined behavior.

The fix is to move the slicing of `buffer` to `buffer[1..]` from `errol3u`
to `errol` so that both the default and the special cases operate on the sliced
buffer.
2022-01-29 12:25:25 +01:00
John Schmidt
40b3c9a592 Handle FormatOptions in fmtDuration/fmtDurationSigned 2022-01-24 20:30:36 +02:00
Jimmi Holst Christensen
a41ad639a8 fmt: Refactor parsing of placeholders into its own function
This saves on comptime format string parsing, as the compiler caches
comptime calls. The catch here, is that parsePlaceHolder cannot take the
placeholder string as a slice. It must take it as an array by value for
the caching to occure.

There is also some logic in here that ensures that the specifier_arg is
always them same slice when the items they contain are the same. This
makes the compiler stamp out less copies of formatType.
2022-01-01 15:40:24 -05:00
r00ster
b100e2ec2a std.fmt: improve @compileError message 2022-01-01 10:29:36 +02:00
Tom Manner
2dd7255713 Fixed typo in deprecation error for E format specifier where it would instead complain about a specifier of X. 2021-12-29 01:02:04 -05:00
Lee Cannon
85de022c56
allocgate: std Allocator interface refactor 2021-11-30 23:32:47 +00:00
Andrew Kelley
902df103c6 std lib API deprecations for the upcoming 0.9.0 release
See #3811
2021-11-30 00:13:07 -07:00
LemonBoy
51be575745 std: Fix endless loop in fmt impl
Apparently there's a stage1 bug that may sometimes lead to an endless
loop being generated when unrolling the fmt impl.

Closes #9961
2021-10-17 16:06:40 -04:00
Andrew Kelley
f3ebfcae38 Merge remote-tracking branch 'origin/master' into llvm13
Conflicts:

 * cmake/Findclang.cmake
 * cmake/Findlld.cmake
 * cmake/Findllvm.cmake

In master branch, more search paths were added to these files with "12"
in the path. In this commit I updated them to "13".

 * src/stage1/codegen.cpp
 * src/zig_llvm.cpp
 * src/zig_llvm.h

In master branch, ZigLLVMBuildCmpXchg is improved to add
`is_single_threaded`. However, the LLVM 13 C API has this already, and
in the llvm13 branch, ZigLLVMBuildCmpXchg is deleted in favor of the C
API. In this commit I updated stage2 to use the LLVM 13 C API rather
than depending on an improved ZigLLVMBuildCmpXchg.

Additionally, src/target.zig largestAtomicBits needed to be updated to
include the new m68k ISA.
2021-09-15 14:51:08 -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
303e1a062c re-enable mips behavior tests for vectors
closes #3317
2021-09-01 15:41:29 -07:00
Boo
ef39725055
Fix float formatting for 0.0 when precision is 0 (#9642) 2021-08-30 13:39:05 -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
Dmitry Matveyev
b2e970d157
std.json: Add support for recursive objects to std.json.parse (#9307)
* Add support for recursive objects to std.json.parse

* Remove previously defined error set

* Try with function which returns an error set

* Don't analyze already inferred types

* Add comptime to inferred_type parameter

* Make ParseInternalError to accept only a single argument

* Add public `ParseError` for `parse` function

* Use error.Foo syntax for errors instead of a named error set

* Better formatting

* Update to latest code changes
2021-08-20 14:52:48 +03:00
Meghan
7e7d67d8ee
std.fmt: add support for printing slices strings (#9562) 2021-08-19 14:12:11 +03:00
Meghan
fcf2ce0ffe std.fmt: add name of type in unsupport format string compile error 2021-08-14 11:07:01 +03:00
Andrew Kelley
5103053977 compile errors test harness: support unknown file/line/column
This gets us 2 more passing compile error test cases.
2021-07-02 13:28:31 -07:00
Isaac Freund
f398ac3ee4 std/fmt: add fmtDurationSigned
When working with durations it often makes sense to use signed integers
and allow negative durations, and there is currently no nice way to
format these in std.fmt. This patch adds a simple wrapper for the
existing fmtDurtion to fit this need.
2021-06-27 12:54:31 +03:00