aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/big
AgeCommit message (Collapse)Author
2022-09-04Fix #12440: std.math.big.Rational order/orderAbsYujiri
2022-08-22stage2+stage1: remove type parameter from bit builtinsVeikka Tuominen
Closes #12529 Closes #12511 Closes #6835
2022-07-16Use Managed.len in sub, divFloor, and divTrunc tooHiroaki Nakamura
2022-07-16Use Managed.len() instead of Managed.toConst().limbs.lenHiroaki Nakamura
2022-07-16Fix std.math.big.int.Managed capacity after mul and sqrHiroaki Nakamura
2022-06-29std.math.big.int: breaking API changes to prevent UAFAndrew Kelley
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-13std.math.big.int: update Managed.toString() to use provided allocator (#11839)Mikael Berthe
2022-06-07std: adjust for stage2 semanticsVeikka Tuominen
2022-06-01Sema: apply previous changes to `validateUnionInit`Veikka Tuominen
2022-05-27math: make `cast` return optional instead of an errorAli Chraghi
2022-04-15std: add workaround for stage2 bugVeikka Tuominen
2022-04-05zig fmt: remove trailing whitespace on doc commentsDamien Firmenich
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-03-23math/big: correct fix for divmod (#11271)Marc Tiehuis
Fixes #11166.
2022-03-10math: fix big.int div, gcd and bitAnd edge-casesMarc Tiehuis
Fixes #10932.
2022-02-18stage2: Implement `@bitReverse` and `@byteSwap`Cody Tapscott
This change implements the above built-ins for Sema and the LLVM backend. Other backends have had placeholders added for lowering.
2022-02-13Add additional tests for `@bitCast`Cody Tapscott
2022-02-13Add `abi_size` parameter to read/writeTwosComplementCody Tapscott
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-11Fix up sign handling and add arbitrary-length integer support to @bitCast()Cody Tapscott
2022-02-06std/math: optimize division with divisors less than a half-limbMarc Tiehuis
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.
2021-12-21stage2: @addWithOverflowRobin Voetter
2021-11-30allocgate: std Allocator interface refactorLee Cannon
2021-11-30std lib API deprecations for the upcoming 0.9.0 releaseAndrew Kelley
See #3811
2021-10-29stage2: implement `@popCount` for non-vectorsAndrew Kelley
2021-10-27std.rand: Refactor `Random` interfaceOminitay
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-25Revert 83bdbb2 and a587dd0 (#10028)Robin Voetter
2021-10-25std: disable big.rational setFloat targeting wasm32Jakub Konka
until we fix the underlying issue resulting in `error.TargetTooSmall` error.
2021-10-24big ints: tighten some more division memory requirementsRobin Voetter
2021-10-24big ints: Make calcLimbLen always work at comptime, even if parameter is runtimeRobin Voetter
2021-10-24big ints: fix divFloorRobin Voetter
2021-10-24big ints: improve divisionRobin Voetter
2021-10-21stage2: more division supportAndrew Kelley
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-21stage2: truncationRobin Voetter
* Also fixes a related case where big int truncate would assume that the input fits in the output limbs buffer
2021-10-17big.int: 2s-complement binary wrapping notRobin Voetter
2021-10-16big ints: Fix set(signed int minimum) panicRobin Voetter
2021-10-16big ints: Saturating left shift + testsRobin Voetter
2021-10-04stage2: fix comptime `@bitCast`Andrew Kelley
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-04big ints: Fix tests for 32-bit architecturesRobin Voetter
2021-10-04fmtRobin Voetter
2021-10-04Apply new big int wrap/saturate to Value.zigRobin Voetter
2021-10-04big ints: mulWrap testsRobin Voetter
2021-10-04big ints: Some extra commentsRobin Voetter
2021-10-04big ints: saturate() testsRobin Voetter
2021-10-04big ints: saturate() functionRobin Voetter
2021-10-04big ints: Wrapping multiplicationRobin Voetter
2021-10-04big ints: Allow llmulaccum to wrapRobin Voetter
2021-10-04big ints: Improve karatsuba multiplicationRobin Voetter
2021-10-04big.int: truncate testsRobin Voetter
2021-10-04big ints: [add|sub]Sat testsRobin Voetter
2021-10-04big ints: [add|sub]Wrap testsRobin Voetter
2021-10-04big ints: implement normal/wrapping/saturating subtraction in terms of additionRobin Voetter