Skip to content

Commit

Permalink
[dart2wasm] Use f64.sqrt instead of Math.sqrt import
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Slava Egorov <[email protected]>
  • Loading branch information
mkustermann authored and Commit Queue committed Dec 12, 2024
1 parent 684018e commit 0d72db1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/dart2wasm/lib/intrinsics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions sdk/lib/_internal/wasm/lib/math_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions sdk/lib/_wasm/wasm_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0d72db1

Please sign in to comment.