From 0d72db1b8866c647602a02064c8ce948b7744cc8 Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Thu, 12 Dec 2024 02:34:49 -0800 Subject: [PATCH] [dart2wasm] Use `f64.sqrt` instead of `Math.sqrt` import Issue https://g-issues.chromium.org/issues/383373316 Change-Id: I319df732ecfce5811fc46934919962c94cdb12b0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400281 Commit-Queue: Martin Kustermann Reviewed-by: Slava Egorov --- pkg/dart2wasm/lib/intrinsics.dart | 4 ++++ sdk/lib/_internal/wasm/lib/math_patch.dart | 5 ++--- sdk/lib/_wasm/wasm_types.dart | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/dart2wasm/lib/intrinsics.dart b/pkg/dart2wasm/lib/intrinsics.dart index bcf53204bb58..c7dc54a41645 100644 --- a/pkg/dart2wasm/lib/intrinsics.dart +++ b/pkg/dart2wasm/lib/intrinsics.dart @@ -420,6 +420,10 @@ class Intrinsifier { codeGen.translateExpression(receiver, w.NumType.f64); b.i64_trunc_sat_f64_s(); return w.NumType.i64; + case "sqrt": + codeGen.translateExpression(receiver, w.NumType.f64); + b.f64_sqrt(); + return w.NumType.f64; case "copysign": codeGen.translateExpression(receiver, w.NumType.f64); codeGen.translateExpression( diff --git a/sdk/lib/_internal/wasm/lib/math_patch.dart b/sdk/lib/_internal/wasm/lib/math_patch.dart index b13f1f722aac..da3df63c62a5 100644 --- a/sdk/lib/_internal/wasm/lib/math_patch.dart +++ b/sdk/lib/_internal/wasm/lib/math_patch.dart @@ -5,6 +5,7 @@ import "dart:_internal" show mix64, patch; import "dart:_js_types" show JSUint8ArrayImpl; import "dart:js_interop"; +import "dart:_wasm"; /// There are no parts of this patch library. @@ -113,7 +114,7 @@ double asin(num x) => _asin(x.toDouble()); @patch double atan(num x) => _atan(x.toDouble()); @patch -double sqrt(num x) => _sqrt(x.toDouble()); +double sqrt(num x) => x.toDouble().sqrt(); @patch double exp(num x) => _exp(x.toDouble()); @patch @@ -133,8 +134,6 @@ external double _acos(double x); external double _asin(double x); @pragma("wasm:import", "Math.atan") external double _atan(double x); -@pragma("wasm:import", "Math.sqrt") -external double _sqrt(double x); @pragma("wasm:import", "Math.exp") external double _exp(double x); @pragma("wasm:import", "Math.log") diff --git a/sdk/lib/_wasm/wasm_types.dart b/sdk/lib/_wasm/wasm_types.dart index 4c2d69f0e9f7..3dcee10f24dc 100644 --- a/sdk/lib/_wasm/wasm_types.dart +++ b/sdk/lib/_wasm/wasm_types.dart @@ -221,6 +221,9 @@ class WasmF64 extends _WasmBase { /// Wasm `i64.trunc_sat_f64_s` instruction. external WasmI64 truncSatS(); + /// Wasm `f64.sqrt` instruction. + external WasmF64 sqrt(); + /// Wasm `f64.copysign` instruction. external WasmF64 copysign(WasmF64 other); } @@ -437,6 +440,10 @@ extension DoubleWasmInstructions on double { @pragma("wasm:prefer-inline") double copysign(double other) => this.toWasmF64().copysign(other.toWasmF64()).toDouble(); + + /// Wasm `f64.sqrt` instruction. + @pragma("wasm:prefer-inline") + double sqrt() => this.toWasmF64().sqrt().toDouble(); } extension WasmExternRefToJSAny on WasmExternRef {