Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Unified Diff: src/builtins/builtins-math.cc

Issue 2402363002: [Math] implement Math.random as TFJ builtin. (Closed)
Patch Set: fix golden file Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/builtins/builtins-math.cc
diff --git a/src/builtins/builtins-math.cc b/src/builtins/builtins-math.cc
index 6058f12265a36fd3380f02ebf3e07865c0a905e6..29c87b4be2ab225849a046902e44e1f4ce8c9b20 100644
--- a/src/builtins/builtins-math.cc
+++ b/src/builtins/builtins-math.cc
@@ -452,6 +452,46 @@ void Builtins::Generate_MathPow(CodeStubAssembler* assembler) {
assembler->Return(result);
}
+// ES6 section 20.2.2.27 Math.random ( )
+void Builtins::Generate_MathRandom(CodeStubAssembler* assembler) {
+ using compiler::Node;
+
+ Node* context = assembler->Parameter(3);
+ Node* native_context = assembler->LoadNativeContext(context);
+
+ // Load cache index.
+ CodeStubAssembler::Variable smi_index(assembler,
+ MachineRepresentation::kTagged);
+ smi_index.Bind(assembler->LoadContextElement(
+ native_context, Context::MATH_RANDOM_INDEX_INDEX));
+
+ // Cached random numbers are exhausted if index is 0. Go to slow path.
+ CodeStubAssembler::Label if_cached(assembler);
+ assembler->GotoIf(assembler->SmiAbove(smi_index.value(),
+ assembler->SmiConstant(Smi::kZero)),
+ &if_cached);
+
+ // Cache exhausted, populate the cache. Return value is the new index.
+ smi_index.Bind(
+ assembler->CallRuntime(Runtime::kGenerateRandomNumbers, context));
+ assembler->Goto(&if_cached);
+
+ // Compute next index by decrement.
+ assembler->Bind(&if_cached);
+ Node* new_smi_index = assembler->SmiSub(
+ smi_index.value(), assembler->SmiConstant(Smi::FromInt(1)));
+ assembler->StoreContextElement(
+ native_context, Context::MATH_RANDOM_INDEX_INDEX, new_smi_index);
+
+ // Load and return next cached random number.
+ Node* array = assembler->LoadContextElement(native_context,
+ Context::MATH_RANDOM_CACHE_INDEX);
+ Node* random = assembler->LoadFixedDoubleArrayElement(
+ array, new_smi_index, MachineType::Float64(), 0,
+ CodeStubAssembler::SMI_PARAMETERS);
+ assembler->Return(assembler->ChangeFloat64ToTagged(random));
+}
+
// ES6 section 20.2.2.28 Math.round ( x )
void Builtins::Generate_MathRound(CodeStubAssembler* assembler) {
Generate_MathRoundingOperation(assembler, &CodeStubAssembler::Float64Round);
« no previous file with comments | « src/builtins/builtins.h ('k') | src/code-stub-assembler.h » ('j') | test/mjsunit/debug-script.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698