| 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 |
|
| 7 |
// Generate a block of inline assembly that loads three user-defined registers |
| 8 |
// A, B, C from memory and deinterleaves them, post-incrementing the src |
| 9 |
// pointer. The register set should be sequential. |
| 10 |
#define LOAD(A, B, C) \ |
| 11 |
"ld3 {"A".16b, "B".16b, "C".16b}, [%[src]], #48 \n\t" |
| 12 |
|
| 13 |
// Generate a block of inline assembly that takes three deinterleaved registers |
| 14 |
// and shuffles the bytes. The output is in temporary registers t0..t3. |
| 15 |
#define SHUF(A, B, C) \ |
| 16 |
"ushr %[t0].16b, "A".16b, #2 \n\t" \ |
| 17 |
"ushr %[t1].16b, "B".16b, #4 \n\t" \ |
| 18 |
"ushr %[t2].16b, "C".16b, #6 \n\t" \ |
| 19 |
"sli %[t1].16b, "A".16b, #4 \n\t" \ |
| 20 |
"sli %[t2].16b, "B".16b, #2 \n\t" \ |
| 21 |
"and %[t1].16b, %[t1].16b, %[n63].16b \n\t" \ |
| 22 |
"and %[t2].16b, %[t2].16b, %[n63].16b \n\t" \ |
| 23 |
"and %[t3].16b, "C".16b, %[n63].16b \n\t" |
| 24 |
|
| 25 |
// Generate a block of inline assembly that takes temporary registers t0..t3 |
| 26 |
// and translates them to the base64 alphabet, using a table loaded into |
| 27 |
// v8..v11. The output is in user-defined registers A..D. |
| 28 |
#define TRAN(A, B, C, D) \ |
| 29 |
"tbl "A".16b, {v8.16b-v11.16b}, %[t0].16b \n\t" \ |
| 30 |
"tbl "B".16b, {v8.16b-v11.16b}, %[t1].16b \n\t" \ |
| 31 |
"tbl "C".16b, {v8.16b-v11.16b}, %[t2].16b \n\t" \ |
| 32 |
"tbl "D".16b, {v8.16b-v11.16b}, %[t3].16b \n\t" |
| 33 |
|
| 34 |
// Generate a block of inline assembly that interleaves four registers and |
| 35 |
// stores them, post-incrementing the destination pointer. |
| 36 |
#define STOR(A, B, C, D) \ |
| 37 |
"st4 {"A".16b, "B".16b, "C".16b, "D".16b}, [%[dst]], #64 \n\t" |
| 38 |
|
| 39 |
// Generate a block of inline assembly that generates a single self-contained |
| 40 |
// encoder round: fetch the data, process it, and store the result. |
| 41 |
#define ROUND() \ |
| 42 |
LOAD("v12", "v13", "v14") \ |
| 43 |
SHUF("v12", "v13", "v14") \ |
| 44 |
TRAN("v12", "v13", "v14", "v15") \ |
| 45 |
STOR("v12", "v13", "v14", "v15") |
| 46 |
|
| 47 |
// Generate a block of assembly that generates a type A interleaved encoder |
| 48 |
// round. It uses registers that were loaded by the previous type B round, and |
| 49 |
// in turn loads registers for the next type B round. |
| 50 |
#define ROUND_A() \ |
| 51 |
SHUF("v2", "v3", "v4") \ |
| 52 |
LOAD("v12", "v13", "v14") \ |
| 53 |
TRAN("v2", "v3", "v4", "v5") \ |
| 54 |
STOR("v2", "v3", "v4", "v5") |
| 55 |
|
| 56 |
// Type B interleaved encoder round. Same as type A, but register sets swapped. |
| 57 |
#define ROUND_B() \ |
| 58 |
SHUF("v12", "v13", "v14") \ |
| 59 |
LOAD("v2", "v3", "v4") \ |
| 60 |
TRAN("v12", "v13", "v14", "v15") \ |
| 61 |
STOR("v12", "v13", "v14", "v15") |
| 62 |
|
| 63 |
// The first type A round needs to load its own registers. |
| 64 |
#define ROUND_A_FIRST() \ |
| 65 |
LOAD("v2", "v3", "v4") \ |
| 66 |
ROUND_A() |
| 67 |
|
| 68 |
// The last type B round omits the load for the next step. |
| 69 |
#define ROUND_B_LAST() \ |
| 70 |
SHUF("v12", "v13", "v14") \ |
| 71 |
TRAN("v12", "v13", "v14", "v15") \ |
| 72 |
STOR("v12", "v13", "v14", "v15") |
| 73 |
|
| 74 |
// Suppress clang's warning that the literal string in the asm statement is |
| 75 |
// overlong (longer than the ISO-mandated minimum size of 4095 bytes for C99 |
| 76 |
// compilers). It may be true, but the goal here is not C99 portability. |
| 77 |
#pragma GCC diagnostic push |
| 78 |
#pragma GCC diagnostic ignored "-Woverlength-strings" |
| 79 |
|
| 80 |
static inline void |
| 81 |
enc_loop_neon64 (const uint8_t **s, size_t *slen, uint8_t **o, size_t *olen) |
| 82 |
{ |
| 83 |
size_t rounds = *slen / 48; |
| 84 |
|
| 85 |
if (rounds == 0) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
*slen -= rounds * 48; // 48 bytes consumed per round. |
| 90 |
*olen += rounds * 64; // 64 bytes produced per round. |
| 91 |
|
| 92 |
// Number of times to go through the 8x loop. |
| 93 |
size_t loops = rounds / 8; |
| 94 |
|
| 95 |
// Number of rounds remaining after the 8x loop. |
| 96 |
rounds %= 8; |
| 97 |
|
| 98 |
// Temporary registers, used as scratch space. |
| 99 |
uint8x16_t tmp0, tmp1, tmp2, tmp3; |
| 100 |
|
| 101 |
__asm__ volatile ( |
| 102 |
|
| 103 |
// Load the encoding table into v8..v11. |
| 104 |
" ld1 {v8.16b-v11.16b}, [%[tbl]] \n\t" |
| 105 |
|
| 106 |
// If there are eight rounds or more, enter an 8x unrolled loop |
| 107 |
// of interleaved encoding rounds. The rounds interleave memory |
| 108 |
// operations (load/store) with data operations to maximize |
| 109 |
// pipeline throughput. |
| 110 |
" cbz %[loops], 4f \n\t" |
| 111 |
|
| 112 |
// The SIMD instructions do not touch the flags. |
| 113 |
"88: subs %[loops], %[loops], #1 \n\t" |
| 114 |
" " ROUND_A_FIRST() |
| 115 |
" " ROUND_B() |
| 116 |
" " ROUND_A() |
| 117 |
" " ROUND_B() |
| 118 |
" " ROUND_A() |
| 119 |
" " ROUND_B() |
| 120 |
" " ROUND_A() |
| 121 |
" " ROUND_B_LAST() |
| 122 |
" b.ne 88b \n\t" |
| 123 |
|
| 124 |
// Enter a 4x unrolled loop for rounds of 4 or more. |
| 125 |
"4: cmp %[rounds], #4 \n\t" |
| 126 |
" b.lt 30f \n\t" |
| 127 |
" " ROUND_A_FIRST() |
| 128 |
" " ROUND_B() |
| 129 |
" " ROUND_A() |
| 130 |
" " ROUND_B_LAST() |
| 131 |
" sub %[rounds], %[rounds], #4 \n\t" |
| 132 |
|
| 133 |
// Dispatch the remaining rounds 0..3. |
| 134 |
"30: cbz %[rounds], 0f \n\t" |
| 135 |
" cmp %[rounds], #2 \n\t" |
| 136 |
" b.eq 2f \n\t" |
| 137 |
" b.lt 1f \n\t" |
| 138 |
|
| 139 |
// Block of non-interlaced encoding rounds, which can each |
| 140 |
// individually be jumped to. Rounds fall through to the next. |
| 141 |
"3: " ROUND() |
| 142 |
"2: " ROUND() |
| 143 |
"1: " ROUND() |
| 144 |
"0: \n\t" |
| 145 |
|
| 146 |
// Outputs (modified). |
| 147 |
: [loops] "+r" (loops), |
| 148 |
[src] "+r" (*s), |
| 149 |
[dst] "+r" (*o), |
| 150 |
[t0] "=&w" (tmp0), |
| 151 |
[t1] "=&w" (tmp1), |
| 152 |
[t2] "=&w" (tmp2), |
| 153 |
[t3] "=&w" (tmp3) |
| 154 |
|
| 155 |
// Inputs (not modified). |
| 156 |
: [rounds] "r" (rounds), |
| 157 |
[tbl] "r" (base64_table_enc_6bit), |
| 158 |
[n63] "w" (vdupq_n_u8(63)) |
| 159 |
|
| 160 |
// Clobbers. |
| 161 |
: "v2", "v3", "v4", "v5", |
| 162 |
"v8", "v9", "v10", "v11", |
| 163 |
"v12", "v13", "v14", "v15", |
| 164 |
"cc", "memory" |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
#pragma GCC diagnostic pop |