u4.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #define VEC4_ROT(A, IMM) \
  2. _mm_or_si128(_mm_slli_epi32(A, IMM), _mm_srli_epi32(A, (32 - IMM)))
  3. /* same, but replace 2 of the shift/shift/or "rotation" by byte shuffles (8 &
  4. * 16) (better) */
  5. #define VEC4_QUARTERROUND_SHUFFLE(A, B, C, D) \
  6. x_##A = _mm_add_epi32(x_##A, x_##B); \
  7. t_##A = _mm_xor_si128(x_##D, x_##A); \
  8. x_##D = _mm_shuffle_epi8(t_##A, rot16); \
  9. x_##C = _mm_add_epi32(x_##C, x_##D); \
  10. t_##C = _mm_xor_si128(x_##B, x_##C); \
  11. x_##B = VEC4_ROT(t_##C, 12); \
  12. x_##A = _mm_add_epi32(x_##A, x_##B); \
  13. t_##A = _mm_xor_si128(x_##D, x_##A); \
  14. x_##D = _mm_shuffle_epi8(t_##A, rot8); \
  15. x_##C = _mm_add_epi32(x_##C, x_##D); \
  16. t_##C = _mm_xor_si128(x_##B, x_##C); \
  17. x_##B = VEC4_ROT(t_##C, 7)
  18. #define VEC4_QUARTERROUND(A, B, C, D) VEC4_QUARTERROUND_SHUFFLE(A, B, C, D)
  19. if (bytes >= 256) {
  20. /* constant for shuffling bytes (replacing multiple-of-8 rotates) */
  21. __m128i rot16 =
  22. _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2);
  23. __m128i rot8 =
  24. _mm_set_epi8(14, 13, 12, 15, 10, 9, 8, 11, 6, 5, 4, 7, 2, 1, 0, 3);
  25. __m128i x_0 = _mm_set1_epi32(x[0]);
  26. __m128i x_1 = _mm_set1_epi32(x[1]);
  27. __m128i x_2 = _mm_set1_epi32(x[2]);
  28. __m128i x_3 = _mm_set1_epi32(x[3]);
  29. __m128i x_4 = _mm_set1_epi32(x[4]);
  30. __m128i x_5 = _mm_set1_epi32(x[5]);
  31. __m128i x_6 = _mm_set1_epi32(x[6]);
  32. __m128i x_7 = _mm_set1_epi32(x[7]);
  33. __m128i x_8 = _mm_set1_epi32(x[8]);
  34. __m128i x_9 = _mm_set1_epi32(x[9]);
  35. __m128i x_10 = _mm_set1_epi32(x[10]);
  36. __m128i x_11 = _mm_set1_epi32(x[11]);
  37. __m128i x_12;
  38. __m128i x_13;
  39. __m128i x_14 = _mm_set1_epi32(x[14]);
  40. __m128i x_15 = _mm_set1_epi32(x[15]);
  41. __m128i orig0 = x_0;
  42. __m128i orig1 = x_1;
  43. __m128i orig2 = x_2;
  44. __m128i orig3 = x_3;
  45. __m128i orig4 = x_4;
  46. __m128i orig5 = x_5;
  47. __m128i orig6 = x_6;
  48. __m128i orig7 = x_7;
  49. __m128i orig8 = x_8;
  50. __m128i orig9 = x_9;
  51. __m128i orig10 = x_10;
  52. __m128i orig11 = x_11;
  53. __m128i orig12;
  54. __m128i orig13;
  55. __m128i orig14 = x_14;
  56. __m128i orig15 = x_15;
  57. __m128i t_0, t_1, t_2, t_3, t_4, t_5, t_6, t_7, t_8, t_9, t_10, t_11, t_12,
  58. t_13, t_14, t_15;
  59. uint32_t in12, in13;
  60. int i;
  61. while (bytes >= 256) {
  62. const __m128i addv12 = _mm_set_epi64x(1, 0);
  63. const __m128i addv13 = _mm_set_epi64x(3, 2);
  64. __m128i t12, t13;
  65. uint64_t in1213;
  66. x_0 = orig0;
  67. x_1 = orig1;
  68. x_2 = orig2;
  69. x_3 = orig3;
  70. x_4 = orig4;
  71. x_5 = orig5;
  72. x_6 = orig6;
  73. x_7 = orig7;
  74. x_8 = orig8;
  75. x_9 = orig9;
  76. x_10 = orig10;
  77. x_11 = orig11;
  78. x_14 = orig14;
  79. x_15 = orig15;
  80. in12 = x[12];
  81. in13 = x[13];
  82. in1213 = ((uint64_t) in12) | (((uint64_t) in13) << 32);
  83. t12 = _mm_set1_epi64x(in1213);
  84. t13 = _mm_set1_epi64x(in1213);
  85. x_12 = _mm_add_epi64(addv12, t12);
  86. x_13 = _mm_add_epi64(addv13, t13);
  87. t12 = _mm_unpacklo_epi32(x_12, x_13);
  88. t13 = _mm_unpackhi_epi32(x_12, x_13);
  89. x_12 = _mm_unpacklo_epi32(t12, t13);
  90. x_13 = _mm_unpackhi_epi32(t12, t13);
  91. orig12 = x_12;
  92. orig13 = x_13;
  93. in1213 += 4;
  94. x[12] = in1213 & 0xFFFFFFFF;
  95. x[13] = (in1213 >> 32) & 0xFFFFFFFF;
  96. for (i = 0; i < ROUNDS; i += 2) {
  97. VEC4_QUARTERROUND(0, 4, 8, 12);
  98. VEC4_QUARTERROUND(1, 5, 9, 13);
  99. VEC4_QUARTERROUND(2, 6, 10, 14);
  100. VEC4_QUARTERROUND(3, 7, 11, 15);
  101. VEC4_QUARTERROUND(0, 5, 10, 15);
  102. VEC4_QUARTERROUND(1, 6, 11, 12);
  103. VEC4_QUARTERROUND(2, 7, 8, 13);
  104. VEC4_QUARTERROUND(3, 4, 9, 14);
  105. }
  106. #define ONEQUAD_TRANSPOSE(A, B, C, D) \
  107. { \
  108. __m128i t0, t1, t2, t3; \
  109. \
  110. x_##A = _mm_add_epi32(x_##A, orig##A); \
  111. x_##B = _mm_add_epi32(x_##B, orig##B); \
  112. x_##C = _mm_add_epi32(x_##C, orig##C); \
  113. x_##D = _mm_add_epi32(x_##D, orig##D); \
  114. t_##A = _mm_unpacklo_epi32(x_##A, x_##B); \
  115. t_##B = _mm_unpacklo_epi32(x_##C, x_##D); \
  116. t_##C = _mm_unpackhi_epi32(x_##A, x_##B); \
  117. t_##D = _mm_unpackhi_epi32(x_##C, x_##D); \
  118. x_##A = _mm_unpacklo_epi64(t_##A, t_##B); \
  119. x_##B = _mm_unpackhi_epi64(t_##A, t_##B); \
  120. x_##C = _mm_unpacklo_epi64(t_##C, t_##D); \
  121. x_##D = _mm_unpackhi_epi64(t_##C, t_##D); \
  122. \
  123. t0 = _mm_xor_si128(x_##A, _mm_loadu_si128((const __m128i*) (m + 0))); \
  124. _mm_storeu_si128((__m128i*) (c + 0), t0); \
  125. t1 = _mm_xor_si128(x_##B, _mm_loadu_si128((const __m128i*) (m + 64))); \
  126. _mm_storeu_si128((__m128i*) (c + 64), t1); \
  127. t2 = \
  128. _mm_xor_si128(x_##C, _mm_loadu_si128((const __m128i*) (m + 128))); \
  129. _mm_storeu_si128((__m128i*) (c + 128), t2); \
  130. t3 = \
  131. _mm_xor_si128(x_##D, _mm_loadu_si128((const __m128i*) (m + 192))); \
  132. _mm_storeu_si128((__m128i*) (c + 192), t3); \
  133. }
  134. #define ONEQUAD(A, B, C, D) ONEQUAD_TRANSPOSE(A, B, C, D)
  135. ONEQUAD(0, 1, 2, 3);
  136. m += 16;
  137. c += 16;
  138. ONEQUAD(4, 5, 6, 7);
  139. m += 16;
  140. c += 16;
  141. ONEQUAD(8, 9, 10, 11);
  142. m += 16;
  143. c += 16;
  144. ONEQUAD(12, 13, 14, 15);
  145. m -= 48;
  146. c -= 48;
  147. #undef ONEQUAD
  148. #undef ONEQUAD_TRANSPOSE
  149. bytes -= 256;
  150. c += 256;
  151. m += 256;
  152. }
  153. }
  154. #undef VEC4_ROT
  155. #undef VEC4_QUARTERROUND
  156. #undef VEC4_QUARTERROUND_SHUFFLE