v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bigint-internal.cc
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
6
7namespace v8 {
8namespace bigint {
9
10// Used for checking consistency between library and public header.
11#if DEBUG
12#if V8_ADVANCED_BIGINT_ALGORITHMS
13bool kAdvancedAlgorithmsEnabledInLibrary = true;
14#else
15bool kAdvancedAlgorithmsEnabledInLibrary = false;
16#endif // V8_ADVANCED_BIGINT_ALGORITHMS
17#endif // DEBUG
18
20
22
28
30 ProcessorImpl* impl = new ProcessorImpl(platform);
31 return static_cast<Processor*>(impl);
32}
33
34void Processor::Destroy() { delete static_cast<ProcessorImpl*>(this); }
35
37 X.Normalize();
38 Y.Normalize();
39 if (X.len() == 0 || Y.len() == 0) return Z.Clear();
40 if (X.len() < Y.len()) std::swap(X, Y);
41 if (Y.len() == 1) return MultiplySingle(Z, X, Y[0]);
42 if (Y.len() < kKaratsubaThreshold) return MultiplySchoolbook(Z, X, Y);
43#if !V8_ADVANCED_BIGINT_ALGORITHMS
44 return MultiplyKaratsuba(Z, X, Y);
45#else
46 if (Y.len() < kToomThreshold) return MultiplyKaratsuba(Z, X, Y);
47 if (Y.len() < kFftThreshold) return MultiplyToomCook(Z, X, Y);
48 return MultiplyFFT(Z, X, Y);
49#endif
50}
51
53 A.Normalize();
54 B.Normalize();
55 // While callers are not required to normalize inputs, they must not
56 // provide divisors that normalize to zero.
57 // This must be a Release-mode CHECK because it is load bearing for
58 // security fuzzing: subsequent operations would perform illegal memory
59 // accesses if they attempted to work with zero divisors.
60 CHECK(B.len() > 0);
61 int cmp = Compare(A, B);
62 if (cmp < 0) return Q.Clear();
63 if (cmp == 0) {
64 Q[0] = 1;
65 for (int i = 1; i < Q.len(); i++) Q[i] = 0;
66 return;
67 }
68 if (B.len() == 1) {
70 return DivideSingle(Q, &remainder, A, B[0]);
71 }
72 if (B.len() < kBurnikelThreshold) {
73 return DivideSchoolbook(Q, RWDigits(nullptr, 0), A, B);
74 }
75#if !V8_ADVANCED_BIGINT_ALGORITHMS
76 return DivideBurnikelZiegler(Q, RWDigits(nullptr, 0), A, B);
77#else
78 if (B.len() < kBarrettThreshold || A.len() == B.len()) {
79 DivideBurnikelZiegler(Q, RWDigits(nullptr, 0), A, B);
80 } else {
81 ScratchDigits R(B.len());
82 DivideBarrett(Q, R, A, B);
83 }
84#endif
85}
86
88 A.Normalize();
89 B.Normalize();
90 // While callers are not required to normalize inputs, they must not
91 // provide divisors that normalize to zero.
92 // This must be a Release-mode CHECK because it is load bearing for
93 // security fuzzing: subsequent operations would perform illegal memory
94 // accesses if they attempted to work with zero divisors.
95 CHECK(B.len() > 0);
96 int cmp = Compare(A, B);
97 if (cmp < 0) {
98 for (int i = 0; i < B.len(); i++) R[i] = B[i];
99 for (int i = B.len(); i < R.len(); i++) R[i] = 0;
100 return;
101 }
102 if (cmp == 0) return R.Clear();
103 if (B.len() == 1) {
105 DivideSingle(RWDigits(nullptr, 0), &remainder, A, B[0]);
106 R[0] = remainder;
107 for (int i = 1; i < R.len(); i++) R[i] = 0;
108 return;
109 }
110 if (B.len() < kBurnikelThreshold) {
111 return DivideSchoolbook(RWDigits(nullptr, 0), R, A, B);
112 }
113 int q_len = DivideResultLength(A, B);
114 ScratchDigits Q(q_len);
115#if !V8_ADVANCED_BIGINT_ALGORITHMS
116 return DivideBurnikelZiegler(Q, R, A, B);
117#else
118 if (B.len() < kBarrettThreshold || A.len() == B.len()) {
119 DivideBurnikelZiegler(Q, R, A, B);
120 } else {
121 DivideBarrett(Q, R, A, B);
122 }
123#endif
124}
125
127 ProcessorImpl* impl = static_cast<ProcessorImpl*>(this);
128 impl->Multiply(Z, X, Y);
129 return impl->get_and_clear_status();
130}
131
133 ProcessorImpl* impl = static_cast<ProcessorImpl*>(this);
134 impl->Divide(Q, A, B);
135 return impl->get_and_clear_status();
136}
137
139 ProcessorImpl* impl = static_cast<ProcessorImpl*>(this);
140 impl->Modulo(R, A, B);
141 return impl->get_and_clear_status();
142}
143
144} // namespace bigint
145} // namespace v8
void DivideBurnikelZiegler(RWDigits Q, RWDigits R, Digits A, Digits B)
void MultiplySingle(RWDigits Z, Digits X, digit_t y)
void MultiplySchoolbook(RWDigits Z, Digits X, Digits Y)
void Modulo(RWDigits R, Digits A, Digits B)
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 DivideSchoolbook(RWDigits Q, RWDigits R, Digits A, Digits B)
void Divide(RWDigits Q, Digits A, Digits B)
ProcessorImpl(Platform *platform)
Status Modulo(RWDigits R, Digits A, Digits B)
Status Divide(RWDigits Q, Digits A, Digits B)
static Processor * New(Platform *platform)
Status Multiply(RWDigits Z, Digits X, Digits Y)
v8::Platform * platform_
Definition cpp-heap.cc:193
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
ZoneVector< RpoNumber > & result
constexpr int kFftThreshold
int Compare(Digits A, Digits B)
Definition bigint.h:224
constexpr int kToomThreshold
constexpr int kBarrettThreshold
Definition bigint.h:337
int DivideResultLength(Digits A, Digits B)
Definition bigint.h:338
constexpr int kBurnikelThreshold
uintptr_t digit_t
Definition bigint.h:34
constexpr int kKaratsubaThreshold
#define CHECK(condition)
Definition logging.h:124