v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bigint-internal.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BIGINT_BIGINT_INTERNAL_H_
6#define V8_BIGINT_BIGINT_INTERNAL_H_
7
8#include <memory>
9
10#include "src/bigint/bigint.h"
11
12namespace v8 {
13namespace bigint {
14
15constexpr int kKaratsubaThreshold = 34;
16constexpr int kToomThreshold = 193;
17constexpr int kFftThreshold = 1500;
18constexpr int kFftInnerThreshold = 200;
19
20constexpr int kBurnikelThreshold = 57;
21constexpr int kNewtonInversionThreshold = 50;
22// kBarrettThreshold is defined in bigint.h.
23
24constexpr int kToStringFastThreshold = 43;
25constexpr int kFromStringLargeThreshold = 300;
26
27class ProcessorImpl : public Processor {
28 public:
29 explicit ProcessorImpl(Platform* platform);
31
33
34 void Multiply(RWDigits Z, Digits X, Digits Y);
37
39 void KaratsubaStart(RWDigits Z, Digits X, Digits Y, RWDigits scratch, int k);
40 void KaratsubaChunk(RWDigits Z, Digits X, Digits Y, RWDigits scratch);
41 void KaratsubaMain(RWDigits Z, Digits X, Digits Y, RWDigits scratch, int n);
42
43 void Divide(RWDigits Q, Digits A, Digits B);
47
48 void Modulo(RWDigits R, Digits A, Digits B);
49
50#if V8_ADVANCED_BIGINT_ALGORITHMS
51 void MultiplyToomCook(RWDigits Z, Digits X, Digits Y);
52 void Toom3Main(RWDigits Z, Digits X, Digits Y);
53
54 void MultiplyFFT(RWDigits Z, Digits X, Digits Y);
55
56 void DivideBarrett(RWDigits Q, RWDigits R, Digits A, Digits B);
57 void DivideBarrett(RWDigits Q, RWDigits R, Digits A, Digits B, Digits I,
58 RWDigits scratch);
59
60 void Invert(RWDigits Z, Digits V, RWDigits scratch);
61 void InvertBasecase(RWDigits Z, Digits V, RWDigits scratch);
62 void InvertNewton(RWDigits Z, Digits V, RWDigits scratch);
63#endif // V8_ADVANCED_BIGINT_ALGORITHMS
64
65 // {out_length} initially contains the allocated capacity of {out}, and
66 // upon return will be set to the actual length of the result string.
67 void ToString(char* out, uint32_t* out_length, Digits X, int radix,
68 bool sign);
69 void ToStringImpl(char* out, uint32_t* out_length, Digits X, int radix,
70 bool sign, bool use_fast_algorithm);
71
72 void FromString(RWDigits Z, FromStringAccumulator* accumulator);
76
78
79 // Each unit is supposed to represent approximately one CPU {mul} instruction.
80 // Doesn't need to be accurate; we just want to make sure to check for
81 // interrupt requests every now and then (roughly every 10-100 ms; often
82 // enough not to appear stuck, rarely enough not to cause noticeable
83 // overhead).
84 static const uintptr_t kWorkEstimateThreshold = 5000000;
85
86 void AddWorkEstimate(uintptr_t estimate) {
87 work_estimate_ += estimate;
92 }
93 }
94 }
95
96 private:
97 uintptr_t work_estimate_{0};
100};
101
102// These constants are primarily needed for Barrett division in div-barrett.cc,
103// and they're also needed by fast to-string conversion in tostring.cc.
104constexpr int DivideBarrettScratchSpace(int n) { return n + 2; }
105// Local values S and W need "n plus a few" digits; U needs 2*n "plus a few".
106// In all tested cases the "few" were either 2 or 3, so give 5 to be safe.
107// S and W are not live at the same time.
108constexpr int kInvertNewtonExtraSpace = 5;
109constexpr int InvertNewtonScratchSpace(int n) {
110 return 3 * n + 2 * kInvertNewtonExtraSpace;
111}
112constexpr int InvertScratchSpace(int n) {
114}
115
116#define CHECK(cond) \
117 if (!(cond)) { \
118 std::cerr << __FILE__ << ":" << __LINE__ << ": "; \
119 std::cerr << "Assertion failed: " #cond "\n"; \
120 abort(); \
121 }
122
123#ifdef DEBUG
124#define DCHECK(cond) CHECK(cond)
125#else
126#define DCHECK(cond) (void(0))
127#endif
128
129#define USE(var) ((void)var)
130
131// RAII memory for a Digits array.
132class Storage {
133 public:
134 explicit Storage(int count) : ptr_(new digit_t[count]) {}
135
136 digit_t* get() { return ptr_.get(); }
137
138 private:
139 std::unique_ptr<digit_t[]> ptr_;
140};
141
142// A writable Digits array with attached storage.
143class ScratchDigits : public RWDigits {
144 public:
145 explicit ScratchDigits(int len) : RWDigits(nullptr, len), storage_(len) {
146 digits_ = storage_.get();
147 }
148
149 private:
151};
152
153} // namespace bigint
154} // namespace v8
155
156#endif // V8_BIGINT_BIGINT_INTERNAL_H_
#define V(Name)
digit_t * digits_
Definition bigint.h:118
virtual bool InterruptRequested()
Definition bigint.h:203
void KaratsubaMain(RWDigits Z, Digits X, Digits Y, RWDigits scratch, int n)
void DivideBurnikelZiegler(RWDigits Q, RWDigits R, Digits A, Digits B)
void FromString(RWDigits Z, FromStringAccumulator *accumulator)
void MultiplySingle(RWDigits Z, Digits X, digit_t y)
void ToString(char *out, uint32_t *out_length, Digits X, int radix, bool sign)
Definition tostring.cc:558
void MultiplySchoolbook(RWDigits Z, Digits X, Digits Y)
void Modulo(RWDigits R, Digits A, Digits B)
void FromStringClassic(RWDigits Z, FromStringAccumulator *accumulator)
Definition fromstring.cc:13
static const uintptr_t kWorkEstimateThreshold
void MultiplyKaratsuba(RWDigits Z, Digits X, Digits Y)
void DivideSingle(RWDigits Q, digit_t *remainder, Digits A, digit_t b)
void Multiply(RWDigits Z, Digits X, Digits Y)
void FromStringBasePowerOfTwo(RWDigits Z, FromStringAccumulator *accumulator)
void AddWorkEstimate(uintptr_t estimate)
void KaratsubaStart(RWDigits Z, Digits X, Digits Y, RWDigits scratch, int k)
void DivideSchoolbook(RWDigits Q, RWDigits R, Digits A, Digits B)
void KaratsubaChunk(RWDigits Z, Digits X, Digits Y, RWDigits scratch)
void FromStringLarge(RWDigits Z, FromStringAccumulator *accumulator)
Definition fromstring.cc:89
void Divide(RWDigits Q, Digits A, Digits B)
void ToStringImpl(char *out, uint32_t *out_length, Digits X, int radix, bool sign, bool use_fast_algorithm)
Definition tostring.cc:565
ProcessorImpl(Platform *platform)
std::unique_ptr< digit_t[]> ptr_
too high values may cause the compiler to set high thresholds for inlining to as much as possible avoid inlined allocation of objects that cannot escape trace load stores from virtual maglev objects use TurboFan fast string builder analyze liveness of environment slots and zap dead values trace TurboFan load elimination emit data about basic block usage in builtins to this enable builtin reordering when run mksnapshot flag for emit warnings when applying builtin profile data verify register allocation in TurboFan randomly schedule instructions to stress dependency tracking enable store store elimination in TurboFan rewrite far to near simulate GC compiler thread race related to allow float parameters to be passed in simulator mode JS Wasm Run additional turbo_optimize_inlined_js_wasm_wrappers enable experimental feedback collection in generic lowering enable Turboshaft s WasmLoadElimination enable Turboshaft s low level load elimination for JS enable Turboshaft s escape analysis for string concatenation use enable Turbolev features that we want to ship in the not too far future trace individual Turboshaft reduction steps trace intermediate Turboshaft reduction steps invocation count threshold for early optimization Enables optimizations which favor memory size over execution speed Enables sampling allocation profiler with X as a sample interval min size of a semi the new space consists of two semi spaces max size of the Collect garbage after Collect garbage after keeps maps alive for< n > old space garbage collections print one detailed trace line in allocation gc speed threshold for starting incremental marking via a task in percent of available threshold for starting incremental marking immediately in percent of available Use a single schedule for determining a marking schedule between JS and C objects schedules the minor GC task with kUserVisible priority max worker number of concurrent for NumberOfWorkerThreads start background threads that allocate memory concurrent_array_buffer_sweeping use parallel threads to clear weak refs in the atomic pause trace progress of the incremental marking trace object counts and memory usage report a tick only when allocated zone memory changes by this amount TracingFlags::gc_stats TracingFlags::gc_stats track native contexts that are expected to be garbage collected verify heap pointers before and after GC memory reducer runs GC with ReduceMemoryFootprint flag Maximum number of memory reducer GCs scheduled Old gen GC speed is computed directly from gc tracer counters Perform compaction on full GCs based on V8 s default heuristics Perform compaction on every full GC Perform code space compaction when finalizing a full GC with stack Stress GC compaction to flush out bugs with moving objects flush of baseline code when it has not been executed recently Use time base code flushing instead of age Use a progress bar to scan large objects in increments when incremental marking is active force incremental marking for small heaps and run it more often force marking at random points between and X(inclusive) percent " "of the regular marking start limit") DEFINE_INT(stress_scavenge
double remainder
int y
int n
Definition mul-fft.cc:296
constexpr int kFftThreshold
constexpr int kToomThreshold
constexpr int InvertNewtonScratchSpace(int n)
constexpr int kFftInnerThreshold
constexpr int kInvertNewtonExtraSpace
constexpr int kNewtonInversionThreshold
constexpr int kBurnikelThreshold
constexpr int InvertScratchSpace(int n)
constexpr int DivideBarrettScratchSpace(int n)
constexpr int kToStringFastThreshold
uintptr_t digit_t
Definition bigint.h:34
constexpr int kKaratsubaThreshold
constexpr int kFromStringLargeThreshold
#define I(name, number_of_args, result_size)
Definition runtime.cc:36