diff --git a/src/arm/codegen-arm.cc b/src/arm/codegen-arm.cc index 97f1034061..98a8abe97a 100644 --- a/src/arm/codegen-arm.cc +++ b/src/arm/codegen-arm.cc @@ -18,23 +18,22 @@ namespace internal { #if defined(USE_SIMULATOR) -byte* fast_exp_arm_machine_code = NULL; -double fast_exp_simulator(double x) { - return Simulator::current(Isolate::Current())->CallFPReturnsDouble( - fast_exp_arm_machine_code, x, 0); +byte* fast_exp_arm_machine_code = nullptr; +double fast_exp_simulator(double x, Isolate* isolate) { + return Simulator::current(isolate) + ->CallFPReturnsDouble(fast_exp_arm_machine_code, x, 0); } #endif -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); { DwVfpRegister input = d0; @@ -67,11 +66,11 @@ UnaryMathFunction CreateExpFunction() { masm.GetCode(&desc); DCHECK(!RelocInfo::RequiresRelocation(desc)); - Assembler::FlushICacheWithoutIsolate(buffer, actual_size); + Assembler::FlushICache(isolate, buffer, actual_size); base::OS::ProtectCode(buffer, actual_size); #if !defined(USE_SIMULATOR) - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); #else fast_exp_arm_machine_code = buffer; return &fast_exp_simulator; diff --git a/src/arm64/codegen-arm64.cc b/src/arm64/codegen-arm64.cc index 8e927bfd90..6a71f67e21 100644 --- a/src/arm64/codegen-arm64.cc +++ b/src/arm64/codegen-arm64.cc @@ -16,9 +16,9 @@ namespace internal { #define __ ACCESS_MASM(masm) #if defined(USE_SIMULATOR) -byte* fast_exp_arm64_machine_code = NULL; -double fast_exp_simulator(double x) { - Simulator * simulator = Simulator::current(Isolate::Current()); +byte* fast_exp_arm64_machine_code = nullptr; +double fast_exp_simulator(double x, Isolate* isolate) { + Simulator * simulator = Simulator::current(isolate); Simulator::CallArgument args[] = { Simulator::CallArgument(x), Simulator::CallArgument::End() @@ -28,19 +28,17 @@ double fast_exp_simulator(double x) { #endif -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; - +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { // Use the Math.exp implemetation in MathExpGenerator::EmitMathExp() to create // an AAPCS64-compliant exp() function. This will be faster than the C // library's exp() function, but probably less accurate. size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); masm.SetStackPointer(csp); // The argument will be in d0 on entry. @@ -64,11 +62,11 @@ UnaryMathFunction CreateExpFunction() { masm.GetCode(&desc); DCHECK(!RelocInfo::RequiresRelocation(desc)); - Assembler::FlushICacheWithoutIsolate(buffer, actual_size); + Assembler::FlushICache(isolate, buffer, actual_size); base::OS::ProtectCode(buffer, actual_size); #if !defined(USE_SIMULATOR) - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); #else fast_exp_arm64_machine_code = buffer; return &fast_exp_simulator; diff --git a/src/codegen.cc b/src/codegen.cc index 1e806d2ae5..47066887e0 100644 --- a/src/codegen.cc +++ b/src/codegen.cc @@ -73,15 +73,32 @@ double fast_##name(double x) { \ return (*fast_##name##_function)(x); \ } -UNARY_MATH_FUNCTION(exp, CreateExpFunction()) UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction()) #undef UNARY_MATH_FUNCTION +static UnaryMathFunctionWithIsolate fast_exp_function = NULL; -void lazily_initialize_fast_exp() { - if (fast_exp_function == NULL) { - init_fast_exp_function(); + +double std_exp(double x, Isolate* isolate) { + return std::exp(x); +} + + +void init_fast_exp_function(Isolate* isolate) { + if (FLAG_fast_math) fast_exp_function = CreateExpFunction(isolate); + if (!fast_exp_function) fast_exp_function = std_exp; +} + + +double fast_exp(double x, Isolate* isolate) { + return (*fast_exp_function)(x, isolate); +} + + +void lazily_initialize_fast_exp(Isolate* isolate) { + if (fast_exp_function == nullptr) { + init_fast_exp_function(isolate); } } diff --git a/src/codegen.h b/src/codegen.h index 7019d3d106..c145c223e2 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -90,20 +90,21 @@ class CodeGenerator { // from the one we use in our generated code. Therefore we use the same // generated code both in runtime and compiled code. typedef double (*UnaryMathFunction)(double x); +typedef double (*UnaryMathFunctionWithIsolate)(double x, Isolate* isolate); -UnaryMathFunction CreateExpFunction(); +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate); UnaryMathFunction CreateSqrtFunction(); double modulo(double x, double y); // Custom implementation of math functions. -double fast_exp(double input); +double fast_exp(double input, Isolate* isolate); double fast_sqrt(double input); #ifdef _WIN64 void init_modulo_function(); #endif -void lazily_initialize_fast_exp(); +void lazily_initialize_fast_exp(Isolate* isolate); void init_fast_sqrt_function(); diff --git a/src/crankshaft/hydrogen-instructions.cc b/src/crankshaft/hydrogen-instructions.cc index a782dae113..df1fb421e9 100644 --- a/src/crankshaft/hydrogen-instructions.cc +++ b/src/crankshaft/hydrogen-instructions.cc @@ -4080,7 +4080,8 @@ HInstruction* HUnaryMathOperation::New(Isolate* isolate, Zone* zone, } switch (op) { case kMathExp: - return H_CONSTANT_DOUBLE(fast_exp(d)); + lazily_initialize_fast_exp(isolate); + return H_CONSTANT_DOUBLE(fast_exp(d, isolate)); case kMathLog: return H_CONSTANT_DOUBLE(std::log(d)); case kMathSqrt: diff --git a/src/ia32/codegen-ia32.cc b/src/ia32/codegen-ia32.cc index 93f4cee636..8e068f6a7e 100644 --- a/src/ia32/codegen-ia32.cc +++ b/src/ia32/codegen-ia32.cc @@ -34,15 +34,14 @@ void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { #define __ masm. -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); // esp[1 * kPointerSize]: raw double input // esp[0 * kPointerSize]: return address { @@ -65,9 +64,9 @@ UnaryMathFunction CreateExpFunction() { masm.GetCode(&desc); DCHECK(!RelocInfo::RequiresRelocation(desc)); - Assembler::FlushICacheWithoutIsolate(buffer, actual_size); + Assembler::FlushICache(isolate, buffer, actual_size); base::OS::ProtectCode(buffer, actual_size); - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); } diff --git a/src/mips/codegen-mips.cc b/src/mips/codegen-mips.cc index 4a1255e1b4..ee7639b4b3 100644 --- a/src/mips/codegen-mips.cc +++ b/src/mips/codegen-mips.cc @@ -18,23 +18,21 @@ namespace internal { #if defined(USE_SIMULATOR) -byte* fast_exp_mips_machine_code = NULL; -double fast_exp_simulator(double x) { - return Simulator::current(Isolate::Current())->CallFP( - fast_exp_mips_machine_code, x, 0); +byte* fast_exp_mips_machine_code = nullptr; +double fast_exp_simulator(double x, Isolate* isolate) { + return Simulator::current(isolate)->CallFP(fast_exp_mips_machine_code, x, 0); } #endif -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); { DoubleRegister input = f12; @@ -63,7 +61,7 @@ UnaryMathFunction CreateExpFunction() { base::OS::ProtectCode(buffer, actual_size); #if !defined(USE_SIMULATOR) - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); #else fast_exp_mips_machine_code = buffer; return &fast_exp_simulator; diff --git a/src/mips64/codegen-mips64.cc b/src/mips64/codegen-mips64.cc index d30bdbb294..666a69d2a5 100644 --- a/src/mips64/codegen-mips64.cc +++ b/src/mips64/codegen-mips64.cc @@ -18,23 +18,21 @@ namespace internal { #if defined(USE_SIMULATOR) -byte* fast_exp_mips_machine_code = NULL; -double fast_exp_simulator(double x) { - return Simulator::current(Isolate::Current())->CallFP( - fast_exp_mips_machine_code, x, 0); +byte* fast_exp_mips_machine_code = nullptr; +double fast_exp_simulator(double x, Isolate* isolate) { + return Simulator::current(isolate)->CallFP(fast_exp_mips_machine_code, x, 0); } #endif -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); { DoubleRegister input = f12; @@ -63,7 +61,7 @@ UnaryMathFunction CreateExpFunction() { base::OS::ProtectCode(buffer, actual_size); #if !defined(USE_SIMULATOR) - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); #else fast_exp_mips_machine_code = buffer; return &fast_exp_simulator; diff --git a/src/ppc/codegen-ppc.cc b/src/ppc/codegen-ppc.cc index b313d11bb3..26914f7585 100644 --- a/src/ppc/codegen-ppc.cc +++ b/src/ppc/codegen-ppc.cc @@ -18,23 +18,22 @@ namespace internal { #if defined(USE_SIMULATOR) -byte* fast_exp_ppc_machine_code = NULL; -double fast_exp_simulator(double x) { - return Simulator::current(Isolate::Current()) +byte* fast_exp_ppc_machine_code = nullptr; +double fast_exp_simulator(double x, Isolate* isolate) { + return Simulator::current(isolate) ->CallFPReturnsDouble(fast_exp_ppc_machine_code, x, 0); } #endif -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); { DoubleRegister input = d1; @@ -62,11 +61,11 @@ UnaryMathFunction CreateExpFunction() { DCHECK(!RelocInfo::RequiresRelocation(desc)); #endif - Assembler::FlushICacheWithoutIsolate(buffer, actual_size); + Assembler::FlushICache(isolate, buffer, actual_size); base::OS::ProtectCode(buffer, actual_size); #if !defined(USE_SIMULATOR) - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); #else fast_exp_ppc_machine_code = buffer; return &fast_exp_simulator; diff --git a/src/runtime/runtime-maths.cc b/src/runtime/runtime-maths.cc index 3d8c8aef7e..5005a850d8 100644 --- a/src/runtime/runtime-maths.cc +++ b/src/runtime/runtime-maths.cc @@ -107,8 +107,8 @@ RUNTIME_FUNCTION(Runtime_MathExpRT) { isolate->counters()->math_exp()->Increment(); CONVERT_DOUBLE_ARG_CHECKED(x, 0); - lazily_initialize_fast_exp(); - return *isolate->factory()->NewNumber(fast_exp(x)); + lazily_initialize_fast_exp(isolate); + return *isolate->factory()->NewNumber(fast_exp(x, isolate)); } diff --git a/src/x64/codegen-x64.cc b/src/x64/codegen-x64.cc index 5c297f1a07..f7064b790b 100644 --- a/src/x64/codegen-x64.cc +++ b/src/x64/codegen-x64.cc @@ -32,15 +32,14 @@ void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { #define __ masm. -UnaryMathFunction CreateExpFunction() { - if (!FLAG_fast_math) return &std::exp; +UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) { size_t actual_size; byte* buffer = static_cast(base::OS::Allocate(1 * KB, &actual_size, true)); - if (buffer == NULL) return &std::exp; + if (buffer == nullptr) return nullptr; ExternalReference::InitializeMathExpData(); - MacroAssembler masm(NULL, buffer, static_cast(actual_size)); + MacroAssembler masm(nullptr, buffer, static_cast(actual_size)); // xmm0: raw double input. XMMRegister input = xmm0; XMMRegister result = xmm1; @@ -58,9 +57,9 @@ UnaryMathFunction CreateExpFunction() { masm.GetCode(&desc); DCHECK(!RelocInfo::RequiresRelocation(desc)); - Assembler::FlushICacheWithoutIsolate(buffer, actual_size); + Assembler::FlushICache(isolate, buffer, actual_size); base::OS::ProtectCode(buffer, actual_size); - return FUNCTION_CAST(buffer); + return FUNCTION_CAST(buffer); } diff --git a/src/x87/codegen-x87.cc b/src/x87/codegen-x87.cc index 7f99fe332b..46abc1cfb0 100644 --- a/src/x87/codegen-x87.cc +++ b/src/x87/codegen-x87.cc @@ -36,7 +36,7 @@ void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const { UnaryMathFunction CreateExpFunction() { // No SSE2 support - return &std::exp; + return nullptr; }