testConversions.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function equal(a, b) {
  6. if (a == b)
  7. {
  8. print("Correct");
  9. }
  10. else
  11. {
  12. print(">> Fail!");
  13. }
  14. }
  15. function testInt32x4BitConversion() {
  16. print("Float32x4 Int32x4 bit conversion");
  17. var m = SIMD.Int32x4(0x3F800000, 0x40000000, 0x40400000, 0x40800000);
  18. var n = SIMD.Float32x4.fromInt32x4Bits(m);
  19. equal(1.0, SIMD.Float32x4.extractLane(n, 0));
  20. equal(2.0, SIMD.Float32x4.extractLane(n, 1));
  21. equal(3.0, SIMD.Float32x4.extractLane(n, 2));
  22. equal(4.0, SIMD.Float32x4.extractLane(n, 3));
  23. n = SIMD.Float32x4(5.0, 6.0, 7.0, 8.0);
  24. m = SIMD.Int32x4.fromFloat32x4Bits(n);
  25. equal(0x40A00000, SIMD.Int32x4.extractLane(m, 0));
  26. equal(0x40C00000, SIMD.Int32x4.extractLane(m, 1));
  27. equal(0x40E00000, SIMD.Int32x4.extractLane(m, 2));
  28. equal(0x41000000, SIMD.Int32x4.extractLane(m, 3));
  29. // Flip sign using bit-wise operators.
  30. n = SIMD.Float32x4(9.0, 10.0, 11.0, 12.0);
  31. m = SIMD.Int32x4(0x80000000, 0x80000000, 0x80000000, 0x80000000);
  32. var nMask = SIMD.Int32x4.fromFloat32x4Bits(n);
  33. nMask = SIMD.Int32x4.xor(nMask, m); // flip sign.
  34. n = SIMD.Float32x4.fromInt32x4Bits(nMask);
  35. equal(-9.0, SIMD.Float32x4.extractLane(n, 0));
  36. equal(-10.0, SIMD.Float32x4.extractLane(n, 1));
  37. equal(-11.0, SIMD.Float32x4.extractLane(n, 2));
  38. equal(-12.0, SIMD.Float32x4.extractLane(n, 3));
  39. nMask = SIMD.Int32x4.fromFloat32x4Bits(n);
  40. nMask = SIMD.Int32x4.xor(nMask, m); // flip sign.
  41. n = SIMD.Float32x4.fromInt32x4Bits(nMask);
  42. equal(9.0, SIMD.Float32x4.extractLane(n, 0));
  43. equal(10.0, SIMD.Float32x4.extractLane(n, 1));
  44. equal(11.0, SIMD.Float32x4.extractLane(n, 2));
  45. equal(12.0, SIMD.Float32x4.extractLane(n, 3));
  46. // Should stay unmodified across bit conversions
  47. m = SIMD.Int32x4(0xFFFFFFFF, 0xFFFF0000, 0x80000000, 0x0);
  48. var m2 = SIMD.Int32x4.fromFloat32x4Bits(SIMD.Float32x4.fromInt32x4Bits(m));
  49. equal(SIMD.Int32x4.extractLane(m, 0), SIMD.Int32x4.extractLane(m2, 0));
  50. equal(SIMD.Int32x4.extractLane(m, 1), SIMD.Int32x4.extractLane(m2, 1));
  51. equal(SIMD.Int32x4.extractLane(m, 2), SIMD.Int32x4.extractLane(m2, 2));
  52. equal(SIMD.Int32x4.extractLane(m, 3), SIMD.Int32x4.extractLane(m2, 3));
  53. }
  54. testInt32x4BitConversion();
  55. testInt32x4BitConversion();
  56. testInt32x4BitConversion();
  57. testInt32x4BitConversion();
  58. testInt32x4BitConversion();
  59. testInt32x4BitConversion();
  60. testInt32x4BitConversion();