f32.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var mod = new WebAssembly.Module(WebAssembly.wabt.convertWast2Wasm(`
  6. (module
  7. (func (export "min") (param f32) (param f32) (result f32)
  8. (return (f32.min (get_local 0) (get_local 1)))
  9. )
  10. (func (export "max") (param f32) (param f32) (result f32)
  11. (return (f32.max (get_local 0) (get_local 1)))
  12. )
  13. (func (export "reinterpret_f2i") (param i32) (result f32)
  14. (return (f32.reinterpret/i32 (get_local 0)))
  15. )
  16. )`));
  17. var {min, max, reinterpret_f2i} = new WebAssembly.Instance(mod).exports;
  18. function test(fn, expectedRes, ...args) {
  19. const res = fn(...args);
  20. if (isNaN(expectedRes)) {
  21. if (!isNaN(res)) {
  22. console.log(`Failed: ${fn}(${args.join(", ")}) expected NaN. Got ${res}`);
  23. }
  24. } else if (Object.is(expectedRes, -0)) {
  25. if (!Object.is(res, -0)) {
  26. console.log(`Failed: ${fn}(${args.join(", ")}) expected -0. Got ${res}`);
  27. }
  28. } else if (res !== Math.fround(expectedRes)) {
  29. console.log(`Failed: ${fn}(${args.join(", ")}) expected ${Math.fround(expectedRes)}. Got ${res}`);
  30. }
  31. }
  32. function testMin(low, high) {
  33. test(min, low, low, high);
  34. test(min, low, high, low);
  35. }
  36. function testMax(low, high) {
  37. test(max, high, low, high);
  38. test(max, high, high, low);
  39. }
  40. function testMinMax(low, high) {
  41. testMin(low, high);
  42. testMax(low, high);
  43. }
  44. testMinMax(11, 11.01);
  45. testMinMax(11.01, 1/0);
  46. testMinMax(-0, 0);
  47. testMin(NaN, 11.01);
  48. testMin(NaN, 11);
  49. testMax(11.01, NaN);
  50. testMax(-NaN, NaN);
  51. function testReinterpret(val, expected) {
  52. test(reinterpret_f2i, expected, val);
  53. }
  54. testReinterpret(0, 0);
  55. testReinterpret(-1, NaN);
  56. testReinterpret(-1082130432, -1);
  57. testReinterpret(-1081800544, -1.0393257141113281);
  58. testReinterpret(2139095040, Infinity);
  59. testReinterpret(-8388608, -Infinity);
  60. testReinterpret(-8388607, NaN);
  61. console.log("PASSED");