SimdUint8x16OperationX86X64.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #if _M_IX86 || _M_AMD64
  7. namespace Js
  8. {
  9. // SIMD.UInt8x16 operation wrappers that cover instrinsics for x86/x64 system
  10. SIMDValue SIMDUint8x16Operation::OpUint8x16(uint8 values[])
  11. {
  12. X86SIMDValue x86Result;
  13. // Sets the 16 signed 8-bit integer values, note in revised order: starts with x15 below
  14. x86Result.m128i_value = _mm_set_epi8((int8)values[15], (int8)values[14], (int8)values[13], (int8)values[12],
  15. (int8)values[11], (int8)values[10], (int8)values[9], (int8)values[8],
  16. (int8)values[7], (int8)values[6], (int8)values[5], (int8)values[4],
  17. (int8)values[3], (int8)values[2], (int8)values[1], (int8)values[0]);
  18. return X86SIMDValue::ToSIMDValue(x86Result);
  19. }
  20. SIMDValue SIMDUint8x16Operation::OpMin(const SIMDValue& aValue, const SIMDValue& bValue)
  21. {
  22. X86SIMDValue x86Result;
  23. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(aValue);
  24. X86SIMDValue tmpbValue = X86SIMDValue::ToX86SIMDValue(bValue);
  25. x86Result.m128i_value = _mm_min_epu8(tmpaValue.m128i_value, tmpbValue.m128i_value);
  26. return X86SIMDValue::ToSIMDValue(x86Result);
  27. }
  28. SIMDValue SIMDUint8x16Operation::OpMax(const SIMDValue& aValue, const SIMDValue& bValue)
  29. {
  30. X86SIMDValue x86Result;
  31. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(aValue);
  32. X86SIMDValue tmpbValue = X86SIMDValue::ToX86SIMDValue(bValue);
  33. x86Result.m128i_value = _mm_max_epu8(tmpaValue.m128i_value, tmpbValue.m128i_value);
  34. return X86SIMDValue::ToSIMDValue(x86Result);
  35. }
  36. SIMDValue SIMDUint8x16Operation::OpLessThan(const SIMDValue& aValue, const SIMDValue& bValue)
  37. {
  38. X86SIMDValue x86Result;
  39. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(aValue);
  40. X86SIMDValue tmpbValue = X86SIMDValue::ToX86SIMDValue(bValue);
  41. #pragma warning(push)
  42. #pragma warning(disable:4838) // conversion from 'unsigned int' to 'int32' requires a narrowing conversion
  43. X86SIMDValue signBits = { {0x80808080,0x80808080, 0x80808080, 0x80808080} };
  44. #pragma warning(pop)
  45. // Signed comparison of unsigned ints can be done if the ints have the "sign" bit xored with 1
  46. tmpaValue.m128i_value = _mm_xor_si128(tmpaValue.m128i_value, signBits.m128i_value);
  47. tmpbValue.m128i_value = _mm_xor_si128(tmpbValue.m128i_value, signBits.m128i_value);
  48. x86Result.m128i_value = _mm_cmplt_epi8(tmpaValue.m128i_value, tmpbValue.m128i_value); // compare a < b?
  49. return X86SIMDValue::ToSIMDValue(x86Result);
  50. }
  51. SIMDValue SIMDUint8x16Operation::OpLessThanOrEqual(const SIMDValue& aValue, const SIMDValue& bValue)
  52. {
  53. X86SIMDValue x86Result;
  54. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(aValue);
  55. X86SIMDValue tmpbValue = X86SIMDValue::ToX86SIMDValue(bValue);
  56. #pragma warning(push)
  57. #pragma warning(disable:4838) // conversion from 'unsigned int' to 'int32' requires a narrowing conversion
  58. X86SIMDValue signBits = { { 0x80808080,0x80808080, 0x80808080, 0x80808080 } };
  59. #pragma warning(pop)
  60. // Signed comparison of unsigned ints can be done if the ints have the "sign" bit xored with 1
  61. tmpaValue.m128i_value = _mm_xor_si128(tmpaValue.m128i_value, signBits.m128i_value);
  62. tmpbValue.m128i_value = _mm_xor_si128(tmpbValue.m128i_value, signBits.m128i_value);
  63. x86Result.m128i_value = _mm_cmplt_epi8(tmpaValue.m128i_value, tmpbValue.m128i_value); // compare a < b?
  64. tmpaValue.m128i_value = _mm_cmpeq_epi8(tmpaValue.m128i_value, tmpbValue.m128i_value); // compare a == b?
  65. x86Result.m128i_value = _mm_or_si128(x86Result.m128i_value, tmpaValue.m128i_value); // result = (a<b)|(a==b)
  66. return X86SIMDValue::ToSIMDValue(x86Result);
  67. }
  68. SIMDValue SIMDUint8x16Operation::OpGreaterThanOrEqual(const SIMDValue& aValue, const SIMDValue& bValue)
  69. {
  70. SIMDValue result;
  71. result = SIMDUint8x16Operation::OpLessThan(aValue, bValue);
  72. result = SIMDInt32x4Operation::OpNot(result);
  73. return result;
  74. }
  75. SIMDValue SIMDUint8x16Operation::OpGreaterThan(const SIMDValue& aValue, const SIMDValue& bValue)
  76. {
  77. SIMDValue result;
  78. result = SIMDUint8x16Operation::OpLessThanOrEqual(aValue, bValue);
  79. result = SIMDInt32x4Operation::OpNot(result);
  80. return result;
  81. }
  82. SIMDValue SIMDUint8x16Operation::OpShiftRightByScalar(const SIMDValue& value, int count)
  83. {
  84. X86SIMDValue x86Result = { { 0, 0, 0, 0} };
  85. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(value);
  86. __m128i x86tmp1;
  87. count = count & SIMDUtils::SIMDGetShiftAmountMask(1);
  88. __m128i mask = _mm_set1_epi8((unsigned char)0xff >> count);
  89. x86tmp1 = _mm_srli_epi16(tmpaValue.m128i_value, count);
  90. x86Result.m128i_value = _mm_and_si128(x86tmp1, mask);
  91. return X86SIMDValue::ToSIMDValue(x86Result);
  92. }
  93. SIMDValue SIMDUint8x16Operation::OpAddSaturate(const SIMDValue& aValue, const SIMDValue& bValue)
  94. {
  95. X86SIMDValue x86Result;
  96. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(aValue);
  97. X86SIMDValue tmpbValue = X86SIMDValue::ToX86SIMDValue(bValue);
  98. x86Result.m128i_value = _mm_adds_epu8(tmpaValue.m128i_value, tmpbValue.m128i_value); // a + b saturated
  99. return X86SIMDValue::ToSIMDValue(x86Result);
  100. }
  101. SIMDValue SIMDUint8x16Operation::OpSubSaturate(const SIMDValue& aValue, const SIMDValue& bValue)
  102. {
  103. X86SIMDValue x86Result;
  104. X86SIMDValue tmpaValue = X86SIMDValue::ToX86SIMDValue(aValue);
  105. X86SIMDValue tmpbValue = X86SIMDValue::ToX86SIMDValue(bValue);
  106. x86Result.m128i_value = _mm_subs_epu8(tmpaValue.m128i_value, tmpbValue.m128i_value); // a - b saturated
  107. return X86SIMDValue::ToSIMDValue(x86Result);
  108. }
  109. }
  110. #endif