Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Math.f16round #146

Open
littledivy opened this issue Jul 28, 2024 · 0 comments
Open

Implement Math.f16round #146

littledivy opened this issue Jul 28, 2024 · 0 comments
Labels
A-builtins area: builtins C-tc39/stage3 conformance: tc39 proposal (stage 3) S-large size: large T-feature type: feature

Comments

@littledivy
Copy link
Contributor

Math.f16round can be implemented with the WebAssembly half-precision proposal which adds f16 type and operations.

This hasn't landed in V8 yet. I haven't tested this patch but it should work with this CL

diff --git a/compiler/builtins.js b/compiler/builtins.js
index 2645859..4272046 100644
--- a/compiler/builtins.js
+++ b/compiler/builtins.js
@@ -362,6 +362,21 @@ export const BuiltinFuncs = function() {
     ]
   };

+  this.__Math_f16round = {
+    floatOnly: true,
+    params: [ valtypeBinary ],
+    locals: [],
+    returns: [ valtypeBinary ],
+    returnType: TYPES.number,
+    wasm: [
+      [ Opcodes.local_get, 0 ],
+      // f32x4.demote_f64x2
+      [ Opcodes.f32_demote_f64 ],
+      // f16x8.demote_f32x4_zero
+      [ Opcodes.f16_demote_f32_s ],
+    ]
+  };
+
   // todo: this does not overflow correctly
   this.__Math_imul = {
     floatOnly: true,
@@ -1113,4 +1128,4 @@ export const BuiltinFuncs = function() {
   };

   PrecompiledBuiltins.BuiltinFuncs.call(this);
-};
\ No newline at end of file
+};
diff --git a/compiler/wasmSpec.js b/compiler/wasmSpec.js
index cd1d598..e712375 100644
--- a/compiler/wasmSpec.js
+++ b/compiler/wasmSpec.js
@@ -197,6 +197,8 @@ export const Opcodes = {
   i32_trunc_sat_f64_s: [ 0xfc, 0x02 ],
   i32_trunc_sat_f64_u: [ 0xfc, 0x03 ],

+  f16_demote_f32_s: [0xfd, 0x14d],
+
   memory_init: [ 0xfc, 0x08 ],
   data_drop: [ 0xfc, 0x09 ],
   memory_copy: [ 0xfc, 0x0a ],
@@ -235,4 +237,4 @@ export const ValtypeSize = {
   // special
   i8: 1,
   i16: 2
-};
\ No newline at end of file
+};
@CanadaHonk CanadaHonk added A-builtins area: builtins S-large size: large T-feature type: feature C-tc39/stage3 conformance: tc39 proposal (stage 3) labels Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-builtins area: builtins C-tc39/stage3 conformance: tc39 proposal (stage 3) S-large size: large T-feature type: feature
Projects
None yet
Development

No branches or pull requests

2 participants