enc_loop_asm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Apologies in advance for combining the preprocessor with inline assembly,
  2. // two notoriously gnarly parts of C, but it was necessary to avoid a lot of
  3. // code repetition. The preprocessor is used to template large sections of
  4. // inline assembly that differ only in the registers used. If the code was
  5. // written out by hand, it would become very large and hard to audit.
  6. // Generate a block of inline assembly that loads register R0 from memory. The
  7. // offset at which the register is loaded is set by the given round.
  8. #define LOAD(R0, ROUND) \
  9. "vlddqu ("#ROUND" * 12)(%[src]), %["R0"] \n\t"
  10. // Generate a block of inline assembly that deinterleaves and shuffles register
  11. // R0 using preloaded constants. Outputs in R0 and R1.
  12. #define SHUF(R0, R1, R2) \
  13. "vpshufb %[lut0], %["R0"], %["R1"] \n\t" \
  14. "vpand %["R1"], %[msk0], %["R2"] \n\t" \
  15. "vpand %["R1"], %[msk2], %["R1"] \n\t" \
  16. "vpmulhuw %["R2"], %[msk1], %["R2"] \n\t" \
  17. "vpmullw %["R1"], %[msk3], %["R1"] \n\t" \
  18. "vpor %["R1"], %["R2"], %["R1"] \n\t"
  19. // Generate a block of inline assembly that takes R0 and R1 and translates
  20. // their contents to the base64 alphabet, using preloaded constants.
  21. #define TRAN(R0, R1, R2) \
  22. "vpsubusb %[n51], %["R1"], %["R0"] \n\t" \
  23. "vpcmpgtb %[n25], %["R1"], %["R2"] \n\t" \
  24. "vpsubb %["R2"], %["R0"], %["R0"] \n\t" \
  25. "vpshufb %["R0"], %[lut1], %["R2"] \n\t" \
  26. "vpaddb %["R1"], %["R2"], %["R0"] \n\t"
  27. // Generate a block of inline assembly that stores the given register R0 at an
  28. // offset set by the given round.
  29. #define STOR(R0, ROUND) \
  30. "vmovdqu %["R0"], ("#ROUND" * 16)(%[dst]) \n\t"
  31. // Generate a block of inline assembly that generates a single self-contained
  32. // encoder round: fetch the data, process it, and store the result. Then update
  33. // the source and destination pointers.
  34. #define ROUND() \
  35. LOAD("a", 0) \
  36. SHUF("a", "b", "c") \
  37. TRAN("a", "b", "c") \
  38. STOR("a", 0) \
  39. "add $12, %[src] \n\t" \
  40. "add $16, %[dst] \n\t"
  41. // Define a macro that initiates a three-way interleaved encoding round by
  42. // preloading registers a, b and c from memory.
  43. // The register graph shows which registers are in use during each step, and
  44. // is a visual aid for choosing registers for that step. Symbol index:
  45. //
  46. // + indicates that a register is loaded by that step.
  47. // | indicates that a register is in use and must not be touched.
  48. // - indicates that a register is decommissioned by that step.
  49. // x indicates that a register is used as a temporary by that step.
  50. // V indicates that a register is an input or output to the macro.
  51. //
  52. #define ROUND_3_INIT() /* a b c d e f */ \
  53. LOAD("a", 0) /* + */ \
  54. SHUF("a", "d", "e") /* | + x */ \
  55. LOAD("b", 1) /* | + | */ \
  56. TRAN("a", "d", "e") /* | | - x */ \
  57. LOAD("c", 2) /* V V V */
  58. // Define a macro that translates, shuffles and stores the input registers A, B
  59. // and C, and preloads registers D, E and F for the next round.
  60. // This macro can be arbitrarily daisy-chained by feeding output registers D, E
  61. // and F back into the next round as input registers A, B and C. The macro
  62. // carefully interleaves memory operations with data operations for optimal
  63. // pipelined performance.
  64. #define ROUND_3(ROUND, A,B,C,D,E,F) /* A B C D E F */ \
  65. LOAD(D, (ROUND + 3)) /* V V V + */ \
  66. SHUF(B, E, F) /* | | | | + x */ \
  67. STOR(A, (ROUND + 0)) /* - | | | | */ \
  68. TRAN(B, E, F) /* | | | - x */ \
  69. LOAD(E, (ROUND + 4)) /* | | | + */ \
  70. SHUF(C, A, F) /* + | | | | x */ \
  71. STOR(B, (ROUND + 1)) /* | - | | | */ \
  72. TRAN(C, A, F) /* - | | | x */ \
  73. LOAD(F, (ROUND + 5)) /* | | | + */ \
  74. SHUF(D, A, B) /* + x | | | | */ \
  75. STOR(C, (ROUND + 2)) /* | - | | | */ \
  76. TRAN(D, A, B) /* - x V V V */
  77. // Define a macro that terminates a ROUND_3 macro by taking pre-loaded
  78. // registers D, E and F, and translating, shuffling and storing them.
  79. #define ROUND_3_END(ROUND, A,B,C,D,E,F) /* A B C D E F */ \
  80. SHUF(E, A, B) /* + x V V V */ \
  81. STOR(D, (ROUND + 3)) /* | - | | */ \
  82. TRAN(E, A, B) /* - x | | */ \
  83. SHUF(F, C, D) /* + x | | */ \
  84. STOR(E, (ROUND + 4)) /* | - | */ \
  85. TRAN(F, C, D) /* - x | */ \
  86. STOR(F, (ROUND + 5)) /* - */
  87. // Define a type A round. Inputs are a, b, and c, outputs are d, e, and f.
  88. #define ROUND_3_A(ROUND) \
  89. ROUND_3(ROUND, "a", "b", "c", "d", "e", "f")
  90. // Define a type B round. Inputs and outputs are swapped with regard to type A.
  91. #define ROUND_3_B(ROUND) \
  92. ROUND_3(ROUND, "d", "e", "f", "a", "b", "c")
  93. // Terminating macro for a type A round.
  94. #define ROUND_3_A_LAST(ROUND) \
  95. ROUND_3_A(ROUND) \
  96. ROUND_3_END(ROUND, "a", "b", "c", "d", "e", "f")
  97. // Terminating macro for a type B round.
  98. #define ROUND_3_B_LAST(ROUND) \
  99. ROUND_3_B(ROUND) \
  100. ROUND_3_END(ROUND, "d", "e", "f", "a", "b", "c")
  101. // Suppress clang's warning that the literal string in the asm statement is
  102. // overlong (longer than the ISO-mandated minimum size of 4095 bytes for C99
  103. // compilers). It may be true, but the goal here is not C99 portability.
  104. #pragma GCC diagnostic push
  105. #pragma GCC diagnostic ignored "-Woverlength-strings"
  106. static inline void
  107. enc_loop_avx (const uint8_t **s, size_t *slen, uint8_t **o, size_t *olen)
  108. {
  109. // For a clearer explanation of the algorithm used by this function,
  110. // please refer to the plain (not inline assembly) implementation. This
  111. // function follows the same basic logic.
  112. if (*slen < 16) {
  113. return;
  114. }
  115. // Process blocks of 12 bytes at a time. Input is read in blocks of 16
  116. // bytes, so "reserve" four bytes from the input buffer to ensure that
  117. // we never read beyond the end of the input buffer.
  118. size_t rounds = (*slen - 4) / 12;
  119. *slen -= rounds * 12; // 12 bytes consumed per round
  120. *olen += rounds * 16; // 16 bytes produced per round
  121. // Number of times to go through the 36x loop.
  122. size_t loops = rounds / 36;
  123. // Number of rounds remaining after the 36x loop.
  124. rounds %= 36;
  125. // Lookup tables.
  126. const __m128i lut0 = _mm_set_epi8(
  127. 10, 11, 9, 10, 7, 8, 6, 7, 4, 5, 3, 4, 1, 2, 0, 1);
  128. const __m128i lut1 = _mm_setr_epi8(
  129. 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0);
  130. // Temporary registers.
  131. __m128i a, b, c, d, e, f;
  132. __asm__ volatile (
  133. // If there are 36 rounds or more, enter a 36x unrolled loop of
  134. // interleaved encoding rounds. The rounds interleave memory
  135. // operations (load/store) with data operations (table lookups,
  136. // etc) to maximize pipeline throughput.
  137. " test %[loops], %[loops] \n\t"
  138. " jz 18f \n\t"
  139. " jmp 36f \n\t"
  140. " \n\t"
  141. ".balign 64 \n\t"
  142. "36: " ROUND_3_INIT()
  143. " " ROUND_3_A( 0)
  144. " " ROUND_3_B( 3)
  145. " " ROUND_3_A( 6)
  146. " " ROUND_3_B( 9)
  147. " " ROUND_3_A(12)
  148. " " ROUND_3_B(15)
  149. " " ROUND_3_A(18)
  150. " " ROUND_3_B(21)
  151. " " ROUND_3_A(24)
  152. " " ROUND_3_B(27)
  153. " " ROUND_3_A_LAST(30)
  154. " add $(12 * 36), %[src] \n\t"
  155. " add $(16 * 36), %[dst] \n\t"
  156. " dec %[loops] \n\t"
  157. " jnz 36b \n\t"
  158. // Enter an 18x unrolled loop for rounds of 18 or more.
  159. "18: cmp $18, %[rounds] \n\t"
  160. " jl 9f \n\t"
  161. " " ROUND_3_INIT()
  162. " " ROUND_3_A(0)
  163. " " ROUND_3_B(3)
  164. " " ROUND_3_A(6)
  165. " " ROUND_3_B(9)
  166. " " ROUND_3_A_LAST(12)
  167. " sub $18, %[rounds] \n\t"
  168. " add $(12 * 18), %[src] \n\t"
  169. " add $(16 * 18), %[dst] \n\t"
  170. // Enter a 9x unrolled loop for rounds of 9 or more.
  171. "9: cmp $9, %[rounds] \n\t"
  172. " jl 6f \n\t"
  173. " " ROUND_3_INIT()
  174. " " ROUND_3_A(0)
  175. " " ROUND_3_B_LAST(3)
  176. " sub $9, %[rounds] \n\t"
  177. " add $(12 * 9), %[src] \n\t"
  178. " add $(16 * 9), %[dst] \n\t"
  179. // Enter a 6x unrolled loop for rounds of 6 or more.
  180. "6: cmp $6, %[rounds] \n\t"
  181. " jl 55f \n\t"
  182. " " ROUND_3_INIT()
  183. " " ROUND_3_A_LAST(0)
  184. " sub $6, %[rounds] \n\t"
  185. " add $(12 * 6), %[src] \n\t"
  186. " add $(16 * 6), %[dst] \n\t"
  187. // Dispatch the remaining rounds 0..5.
  188. "55: cmp $3, %[rounds] \n\t"
  189. " jg 45f \n\t"
  190. " je 3f \n\t"
  191. " cmp $1, %[rounds] \n\t"
  192. " jg 2f \n\t"
  193. " je 1f \n\t"
  194. " jmp 0f \n\t"
  195. "45: cmp $4, %[rounds] \n\t"
  196. " je 4f \n\t"
  197. // Block of non-interlaced encoding rounds, which can each
  198. // individually be jumped to. Rounds fall through to the next.
  199. "5: " ROUND()
  200. "4: " ROUND()
  201. "3: " ROUND()
  202. "2: " ROUND()
  203. "1: " ROUND()
  204. "0: \n\t"
  205. // Outputs (modified).
  206. : [rounds] "+r" (rounds),
  207. [loops] "+r" (loops),
  208. [src] "+r" (*s),
  209. [dst] "+r" (*o),
  210. [a] "=&x" (a),
  211. [b] "=&x" (b),
  212. [c] "=&x" (c),
  213. [d] "=&x" (d),
  214. [e] "=&x" (e),
  215. [f] "=&x" (f)
  216. // Inputs (not modified).
  217. : [lut0] "x" (lut0),
  218. [lut1] "x" (lut1),
  219. [msk0] "x" (_mm_set1_epi32(0x0FC0FC00)),
  220. [msk1] "x" (_mm_set1_epi32(0x04000040)),
  221. [msk2] "x" (_mm_set1_epi32(0x003F03F0)),
  222. [msk3] "x" (_mm_set1_epi32(0x01000010)),
  223. [n51] "x" (_mm_set1_epi8(51)),
  224. [n25] "x" (_mm_set1_epi8(25))
  225. // Clobbers.
  226. : "cc", "memory"
  227. );
  228. }
  229. #pragma GCC diagnostic pop