f64.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 f64 f64) (result f64)
  8. (f64.min (get_local 0) (get_local 1))
  9. )
  10. (func (export "max") (param f64 f64) (result f64)
  11. (f64.max (get_local 0) (get_local 1))
  12. )
  13. )`));
  14. var {min, max} = new WebAssembly.Instance(mod).exports;
  15. function test(fn, expectedRes, ...args) {
  16. const res = fn(...args);
  17. if (isNaN(expectedRes)) {
  18. if (!isNaN(res)) {
  19. console.log(`Failed: ${fn}(${args.join(", ")}) expected NaN. Got ${res}`);
  20. }
  21. } else if (Object.is(expectedRes, -0)) {
  22. if (!Object.is(res, -0)) {
  23. console.log(`Failed: ${fn}(${args.join(", ")}) expected -0. Got ${res}`);
  24. }
  25. } else if (res !== expectedRes) {
  26. console.log(`Failed: ${fn}(${args.join(", ")}) expected ${Math.fround(expectedRes)}. Got ${res}`);
  27. }
  28. }
  29. function testMin(low, high) {
  30. test(min, low, low, high);
  31. test(min, low, high, low);
  32. }
  33. function testMax(low, high) {
  34. test(max, high, low, high);
  35. test(max, high, high, low);
  36. }
  37. function testMinMax(low, high) {
  38. testMin(low, high);
  39. testMax(low, high);
  40. }
  41. testMinMax(11, 11.01);
  42. testMinMax(11.01, 1/0);
  43. testMinMax(-0, 0);
  44. testMin(NaN, 11.01);
  45. testMin(NaN, 11);
  46. testMax(11.01, NaN);
  47. testMax(-NaN, NaN);
  48. console.log("PASSED");