SimdUtils.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "RuntimeLanguagePch.h"
  6. namespace Js
  7. {
  8. #if _M_IX86 || _M_AMD64
  9. SIMDValue SIMDUtils::FromSimdBits(const SIMDValue value)
  10. {
  11. X86SIMDValue x86Result;
  12. X86SIMDValue v = X86SIMDValue::ToX86SIMDValue(value);
  13. _mm_store_ps(x86Result.f32, v.m128_value);
  14. return X86SIMDValue::ToSIMDValue(x86Result);
  15. }
  16. #else
  17. SIMDValue SIMDUtils::FromSimdBits(const SIMDValue value)
  18. {
  19. SIMDValue result;
  20. result.i32[SIMD_X] = value.i32[SIMD_X];
  21. result.i32[SIMD_Y] = value.i32[SIMD_Y];
  22. result.i32[SIMD_Z] = value.i32[SIMD_Z];
  23. result.i32[SIMD_W] = value.i32[SIMD_W];
  24. return result;
  25. }
  26. #endif
  27. SIMDValue SIMDUtils::SIMD128InnerShuffle(const SIMDValue src1, const SIMDValue src2, uint32 laneCount, const uint32* lanes)
  28. {
  29. SIMDValue result = { 0 };
  30. Assert(laneCount == 16 || laneCount == 8 || laneCount == 4);
  31. Assert(lanes != nullptr);
  32. switch (laneCount)
  33. {
  34. case 4:
  35. for (uint i = 0; i < laneCount; ++i)
  36. {
  37. result.i32[i] = lanes[i] < laneCount ? src1.i32[lanes[i]] : src2.i32[lanes[i] - laneCount];
  38. }
  39. break;
  40. case 8:
  41. for (uint i = 0; i < laneCount; ++i)
  42. {
  43. result.i16[i] = lanes[i] < laneCount ? src1.i16[lanes[i]] : src2.i16[lanes[i] - laneCount];
  44. }
  45. break;
  46. case 16:
  47. for (uint i = 0; i < laneCount; ++i)
  48. {
  49. result.i8[i] = lanes[i] < laneCount ? src1.i8[lanes[i]] : src2.i8[lanes[i] - laneCount];
  50. }
  51. break;
  52. default:
  53. Assert(UNREACHED);
  54. }
  55. return result;
  56. }
  57. SIMDValue SIMDUtils::SIMDLdData(const SIMDValue *data, uint8 dataWidth)
  58. {
  59. SIMDValue result = { 0, 0, 0, 0 };
  60. // bitwise copy. Always use integer fields to avoid wrong copy of NaNs.
  61. switch (dataWidth)
  62. {
  63. case 16:
  64. result.i32[SIMD_W] = data->i32[SIMD_W];
  65. // fall through
  66. case 12:
  67. result.i32[SIMD_Z] = data->i32[SIMD_Z];
  68. // fall through
  69. case 8:
  70. result.i32[SIMD_Y] = data->i32[SIMD_Y];
  71. // fall through
  72. case 4:
  73. result.i32[SIMD_X] = data->i32[SIMD_X];
  74. break;
  75. default:
  76. Assert(UNREACHED);
  77. }
  78. return result;
  79. }
  80. void SIMDUtils::SIMDStData(SIMDValue *data, const SIMDValue simdValue, uint8 dataWidth)
  81. {
  82. // bitwise copy. Always use integer fields to avoid wrong copy of NaNs.
  83. switch (dataWidth)
  84. {
  85. case 16:
  86. data->i32[SIMD_W] = simdValue.i32[SIMD_W];
  87. // fall through
  88. case 12:
  89. data->i32[SIMD_Z] = simdValue.i32[SIMD_Z];
  90. // fall through
  91. case 8:
  92. data->i32[SIMD_Y] = simdValue.i32[SIMD_Y];
  93. // fall through
  94. case 4:
  95. data->i32[SIMD_X] = simdValue.i32[SIMD_X];
  96. break;
  97. default:
  98. Assert(UNREACHED);
  99. }
  100. }
  101. #if ENABLE_NATIVE_CODEGEN
  102. // Maps Simd opcodes which are non-contiguous to a zero-based linear space. Used to index a table using a Simd opcode.
  103. uint32 SIMDUtils::SimdOpcodeAsIndex(Js::OpCode op)
  104. {
  105. if (op <= Js::OpCode::Simd128_End)
  106. {
  107. return (uint32)((Js::OpCode)op - Js::OpCode::Simd128_Start);
  108. }
  109. else
  110. {
  111. Assert(op >= Js::OpCode::Simd128_Start_Extend && op <= Js::OpCode::Simd128_End_Extend);
  112. return (uint32)((Js::OpCode)op - Js::OpCode::Simd128_Start_Extend) + (uint32)(Js::OpCode::Simd128_End - Js::OpCode::Simd128_Start) + 1;
  113. }
  114. }
  115. #endif
  116. }