Commit graph

377 commits

Author SHA1 Message Date
viri
f9a9e8ba73
std.math.isInf: add tests for +-NaN 2022-04-08 20:13:30 -06:00
viri
e46c612503
use math/float.zig everywhere 2022-04-07 05:04:38 -06:00
viri
5d6a5a1236
std.math.is*Inf: make generic, support f80 2022-04-07 05:04:03 -06:00
viri
c5c6260534
std.math: generalise inf, even simpler isFinite 2022-04-07 02:38:31 -06:00
viri
a2f5f0da5c
std.math.isFinite: make generic, support f80 2022-04-07 02:38:28 -06:00
viri
7b7f45dc2a std.{fmt, math}: derive float constants from std
This also addresses a nit from #10133 where IntT might be a confusing
name because it might imply signed integer (iX, not uX). We settled on
TBits for math/float.zig so I've applied that change here too.

When I originally wrote ldexp() I copied the name from parse_hex_float.
2022-04-06 15:50:36 +02:00
viri
cb019b80ac math.fabs: simplify implementation, add tests 2022-04-06 15:50:36 +02:00
viri
2f6ee4a97d math.isNormal: simplify implementation, add tests 2022-04-06 15:50:36 +02:00
viri
5b8ac9821d derive float constants in a generic way (#10133) 2022-04-06 15:50:36 +02:00
Damien Firmenich
5fafcc2b62
zig fmt: remove trailing whitespace on doc comments
Fixes #11353

The renderer treats comments and doc comments differently since doc
comments are parsed into the Ast. This commit adds a check after getting
the text for the doc comment and trims whitespace at the end before
rendering.

The `a = 0,` in the test is here to avoid a ParseError while parsing the
test.
2022-04-05 18:08:33 +03:00
BlueAlmost
406507c6dc
std.math.Complex: add 'negation' and 'mulitply by i' 2022-03-27 11:54:43 +03:00
Marc Tiehuis
2e0de0b4e2
math/big: correct fix for divmod (#11271)
Fixes #11166.
2022-03-23 19:24:25 +01:00
Andrew Kelley
2a50a4629b freestanding libc: include roundl 2022-03-14 00:11:46 -07:00
Marc Tiehuis
8bab1b405f math: fix big.int div, gcd and bitAnd edge-cases
Fixes #10932.
2022-03-10 13:56:07 -05:00
Andrew Kelley
71b8760d3b stage2: rework @mulAdd
* mul_add AIR instruction: use `pl_op` instead of `ty_pl`. The type is
   always the same as the operand; no need to waste bytes redundantly
   storing the type.
 * AstGen: use coerced_ty for all the operands except for one which we
   use to communicate the type.
 * Sema: use the correct source location for requireRuntimeBlock in
   handling of `@mulAdd`.
 * native backends: handle liveness even for the functions that are
   TODO.
 * C backend: implement `@mulAdd`. It lowers to libc calls.
 * LLVM backend: make `@mulAdd` handle all float types.
   - improved fptrunc and fpext to handle f80 with compiler-rt calls.
 * Value.mulAdd: handle all float types and use the `@mulAdd` builtin.
 * behavior tests: revert the changes to testing `@mulAdd`. These
   changes broke the test coverage, making it only tested at
   compile-time.

Improved f80 support:
 * std.math.fma handles f80
 * move fma functions from freestanding libc to compiler-rt
   - add __fmax and fmal
   - make __fmax and fmaq only exported when they don't alias fmal.
   - make their linkage weak just like the rest of compiler-rt symbols.
 * removed `longDoubleIsF128` and replaced it with `longDoubleIs` which
   takes a type as a parameter. The implementation is now more accurate
   and handles more targets. Similarly, in stage2 the function
   CTypes.sizeInBits is more accurate for long double for more targets.
2022-03-06 16:11:39 -07:00
Cody Tapscott
ef417f19e1 stage2: Implement @bitReverse and @byteSwap
This change implements the above built-ins for Sema and the LLVM
backend. Other backends have had placeholders added for lowering.
2022-02-18 14:28:32 -07:00
Andrew Kelley
736b9dcdd6
Merge pull request #10858 from topolarity/stage2-bitcast
stage2 sema: Fix sign handling of exotic integers in `@bitCast`
2022-02-18 13:49:00 -05:00
J87
b837855317 Replace magic numbers with clearer representation
Replaces two magic numbers with the procedure used to generate them, in order to keep consistent with other implementations for f64 & f128.
2022-02-17 21:20:04 -05:00
Cody Tapscott
c586e3ba1b Add additional tests for @bitCast 2022-02-13 13:28:08 -07:00
Cody Tapscott
7b72fc6bbc Add abi_size parameter to read/writeTwosComplement
Big-int functions were updated to respect the provided abi_size, rather
than inferring a potentially incorrect abi_size implicitly.

In combination with the convention that any required padding bits are
added on the MSB end, this means that exotic integers can potentially
have a well-defined memory layout.
2022-02-13 13:26:59 -07:00
Cody Tapscott
70d7f87be0 Fix up sign handling and add arbitrary-length integer support to @bitCast() 2022-02-11 08:49:19 -07:00
Marc Tiehuis
53e6c719ef std/math: optimize division with divisors less than a half-limb
This adds a new path which avoids using compiler_rt generated div
udivmod instructions in the case that a divisor is less than half the
max usize value. Two half-limb divisions are performed instead which
ensures that non-emulated division instructions are actually used. This
does not improve the udivmod code which should still be reviewed
independently of this issue.

Notably this improves the performance of the toString implementation of
non-power-of-two bases considerably.

Division performance is improved ~1000% based on some coarse testing.

The following test code is used to provide a rough comparison between
the old vs. new method.

```
const std = @import("std");
const Managed = std.math.big.int.Managed;

const allocator = std.heap.c_allocator;

fn fib(a: *Managed, n: usize) !void {
    var b = try Managed.initSet(allocator, 1);
    defer b.deinit();
    var c = try Managed.init(allocator);
    defer c.deinit();

    var i: usize = 0;
    while (i < n) : (i += 1) {
        try c.add(a.toConst(), b.toConst());

        a.swap(&b);
        b.swap(&c);
    }
}

pub fn main() !void {
    var a = try Managed.initSet(allocator, 0);
    defer a.deinit();

    try fib(&a, 1_000_000);

    // Note: Next two lines (and printed digit count) omitted on no-print version.
    const as = try a.toString(allocator, 10, .lower);
    defer allocator.free(as);

    std.debug.print("fib: digit count: {}, limb count: {}\n", .{ as.len, a.limbs.len });
}
```

```
==> time.no-print <==
limb count: 10849

________________________________________________________
Executed in   10.60 secs    fish           external
   usr time   10.44 secs    0.00 millis   10.44 secs
   sys time    0.02 secs    1.12 millis    0.02 secs

==> time.old <==
fib: digit count: 208988, limb count: 10849

________________________________________________________
Executed in   22.78 secs    fish           external
   usr time   22.43 secs    1.01 millis   22.43 secs
   sys time    0.03 secs    0.13 millis    0.03 secs

==> time.optimized <==
fib: digit count: 208988, limb count: 10849

________________________________________________________
Executed in   11.59 secs    fish           external
   usr time   11.56 secs    1.03 millis   11.56 secs
   sys time    0.03 secs    0.12 millis    0.03 secs
```

Perf data for non-optimized and optimized, verifying no udivmod is
generated by the new code.

```
$ perf report -i perf.data.old --stdio
- Total Lost Samples: 0
-
- Samples: 90K of event 'cycles:u'
- Event count (approx.): 71603695208
-
- Overhead  Command  Shared Object     Symbol
- ........  .......  ................  ...........................................
-
    52.97%  t        t                 [.] compiler_rt.udivmod.udivmod
    45.97%  t        t                 [.] std.math.big.int.Mutable.addCarry
     0.83%  t        t                 [.] main
     0.08%  t        libc-2.33.so      [.] __memmove_avx_unaligned_erms
     0.08%  t        t                 [.] __udivti3
     0.03%  t        [unknown]         [k] 0xffffffff9a0010a7
     0.02%  t        t                 [.] std.math.big.int.Managed.ensureCapacity
     0.01%  t        libc-2.33.so      [.] _int_malloc
     0.00%  t        libc-2.33.so      [.] __malloc_usable_size
     0.00%  t        libc-2.33.so      [.] _int_free
     0.00%  t        t                 [.] 0x0000000000004a80
     0.00%  t        t                 [.] std.heap.CAllocator.resize
     0.00%  t        libc-2.33.so      [.] _mid_memalign
     0.00%  t        libc-2.33.so      [.] sysmalloc
     0.00%  t        libc-2.33.so      [.] __posix_memalign
     0.00%  t        t                 [.] std.heap.CAllocator.alloc
     0.00%  t        ld-2.33.so        [.] do_lookup_x

$ perf report -i perf.data.optimized --stdio
- Total Lost Samples: 0
-
- Samples: 46K of event 'cycles:u'
- Event count (approx.): 36790112336
-
- Overhead  Command  Shared Object     Symbol
- ........  .......  ................  ...........................................
-
    79.98%  t        t                 [.] std.math.big.int.Mutable.addCarry
    15.14%  t        t                 [.] main
     4.58%  t        t                 [.] std.math.big.int.Managed.ensureCapacity
     0.21%  t        libc-2.33.so      [.] __memmove_avx_unaligned_erms
     0.05%  t        [unknown]         [k] 0xffffffff9a0010a7
     0.02%  t        libc-2.33.so      [.] _int_malloc
     0.01%  t        t                 [.] std.heap.CAllocator.alloc
     0.01%  t        libc-2.33.so      [.] __malloc_usable_size
     0.00%  t        libc-2.33.so      [.] systrim.constprop.0
     0.00%  t        libc-2.33.so      [.] _mid_memalign
     0.00%  t        t                 [.] 0x0000000000000c7d
     0.00%  t        libc-2.33.so      [.] malloc
     0.00%  t        ld-2.33.so        [.] check_match
```

Closes #10630.
2022-02-06 21:39:34 -05:00
Jonathan S
aca665cebd
Fix overflow in std.math.isNormal when applied to -Inf or a negative NaN 2022-01-29 18:11:49 +02:00
Veikka Tuominen
67d04a988a std: add f80 bits 2022-01-28 11:45:04 -07:00
Robin Voetter
f3d635b668 stage2: @addWithOverflow 2021-12-21 01:41:51 +01:00
Marc Tiehuis
b02384e03d std/math: hide internal cos/tan functions 2021-12-06 01:17:01 +13:00
Marc Tiehuis
d28b445329 std/math: fix __rem_pio2 underflow
Error in translated code. Found by fuzzing.
2021-12-06 01:02:09 +13:00
Marc Tiehuis
149444723f std/math: Add test cases for #9901
Closes #9901.
Closes #9902.
2021-12-06 01:02:09 +13:00
Marc Tiehuis
e24398361d std/math: replace golang sin/cos/tan with musl sin/cos/tan 2021-12-06 01:02:09 +13: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
Andrew Kelley
c42763f8cc AstGen: use reachableExpr for return operand
Related: #9630
2021-11-24 14:47:33 -07:00
Marc Tiehuis
8f1e417757 std/math: add ldexp and make scalbn an alias
We assume we are compiled on a base-2 radix floating point system. This
is a reasonable assumption. musl libc as an example also assumes this.

We implement scalbn as an alias for ldexp, since ldexp is defined as 2
regardless of the float radix. This is opposite to musl which defines
scalbn in terms of ldexp.

Closes #9799.
2021-11-23 14:47:01 -05:00
Lewis Gaul
dfd604834b Fix copy-paste error that results in incorrect results from exp64() 2021-11-15 19:40:03 -05:00
Andrew Kelley
d6067db062 stage2: implement @popCount for non-vectors 2021-10-29 17:49:02 -07:00
Ominitay
c1a5ff34f3 std.rand: Refactor Random interface
These changes have been made to resolve issue #10037. The `Random`
interface was implemented in such a way that causes significant slowdown
when calling the `fill` function of the rng used.

The `Random` interface is no longer stored in a field of the rng, and is
instead returned by the child function `random()` of the rng. This
avoids the performance issues caused by the interface.
2021-10-27 16:07:48 -04:00
Lewis Gaul
f890de6294
Fix bug in exp2() (#9999)
* Fix bug in exp2_64 to handle negative values (bad translation from C)

* Apply fix to exp2_32() as well, and modify comment on musl behaviour

* Use +%= instead of @addWithOverflow()
2021-10-26 18:57:58 -04:00
Robin Voetter
df167af0b6
Revert 83bdbb2 and a587dd0 (#10028) 2021-10-25 14:00:10 +02:00
Jakub Konka
a587dd08f8 std: disable big.rational setFloat targeting wasm32
until we fix the underlying issue resulting in `error.TargetTooSmall`
error.
2021-10-25 01:27:05 +02:00
LemonBoy
2a733051bb libc: Export truncl
With the usual caveat of it being wrong for targets where c_longdouble
is not f128.
2021-10-24 19:17:55 +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
86b9280963 zig libc: export floorl and ceill
needed since self-hosted now contains calls to `@divFloor` and
`@divTrunc` for 128-bit floats.
2021-10-22 10:48:45 -07: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