| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
Mlen = 761 |
| 6 |
if len(sys.argv) > 1: Mlen = int(sys.argv[1]) |
| 7 |
|
| 8 |
m0 = 4591 |
| 9 |
if len(sys.argv) > 2: m0 = int(sys.argv[2]) |
| 10 |
|
| 11 |
m1 = m0 |
| 12 |
if len(sys.argv) > 3: m1 = int(sys.argv[3]) |
| 13 |
# M is Mlen-1 copies of m0, plus 1 copy of m1 |
| 14 |
|
| 15 |
offset = m0//2 |
| 16 |
if len(sys.argv) > 4: offset = int(sys.argv[4]) |
| 17 |
|
| 18 |
div3 = False |
| 19 |
if len(sys.argv) > 5: div3 = sys.argv[5]=='True' |
| 20 |
|
| 21 |
round = False |
| 22 |
if len(sys.argv) > 6: round = sys.argv[6]=='True' |
| 23 |
|
| 24 |
reading = 'R0' |
| 25 |
top = 16384 |
| 26 |
|
| 27 |
print('/* auto-generated; do not edit */') |
| 28 |
print('/* 20250302 djb: port to -Wc++-compat */') |
| 29 |
print('') |
| 30 |
print('#include <immintrin.h>') |
| 31 |
print('#include "crypto_encode.h"') |
| 32 |
print('#include "crypto_int16.h"') |
| 33 |
print('#include "crypto_uint16.h"') |
| 34 |
print('#include "crypto_uint32.h"') |
| 35 |
print('#define int16 crypto_int16') |
| 36 |
print('#define uint16 crypto_uint16') |
| 37 |
print('#define uint32 crypto_uint32') |
| 38 |
print('') |
| 39 |
|
| 40 |
print('void crypto_encode(unsigned char *out,const void *v)') |
| 41 |
print('{') |
| 42 |
print(' const int16 *%s = (const int16 *) v;' % reading) |
| 43 |
|
| 44 |
if Mlen > 1: |
| 45 |
print(' /* XXX: caller could overlap R with input */') |
| 46 |
Rlen = (Mlen+1)//2 |
| 47 |
print(' uint16 R[%d];' % Rlen) |
| 48 |
|
| 49 |
if Mlen > 2**29: |
| 50 |
print(' long long i;') |
| 51 |
else: |
| 52 |
print(' long i;') |
| 53 |
|
| 54 |
print(' const uint16 *reading;') |
| 55 |
print(' uint16 *writing;') |
| 56 |
print(' uint16 r0,r1;') |
| 57 |
print(' uint32 r2;') |
| 58 |
|
| 59 |
def access(reading,pos): |
| 60 |
result = '%s[%s]' % (reading,pos) |
| 61 |
if reading == 'R0': |
| 62 |
if round: |
| 63 |
result = '3*((10923*%s+16384)>>15)'%result |
| 64 |
result = '((%s+%d)&16383)'%(result,offset) |
| 65 |
if div3: |
| 66 |
result = '(%s*10923)>>15'%result |
| 67 |
return result |
| 68 |
|
| 69 |
def printloop(looplen,reading,todo,m0,bytes): |
| 70 |
if looplen <= 0: return |
| 71 |
if looplen == 1: |
| 72 |
print(' r0 = %s;' % access(reading,0)) |
| 73 |
print(' r1 = %s;' % access(reading,1)) |
| 74 |
print(' r2 = r0+r1*(uint32)%d;' % m0) |
| 75 |
for j in range(bytes): |
| 76 |
print(' *out++ = r2; r2 >>= 8;') |
| 77 |
print(' %s[0] = r2;' % todo) |
| 78 |
return |
| 79 |
|
| 80 |
if looplen >= 12 and bytes == 1: |
| 81 |
print(' reading = (uint16 *) %s;' % reading) |
| 82 |
print(' writing = %s;' % todo) |
| 83 |
print(' i = %d;' % ((looplen+7)//8)) |
| 84 |
print(' while (i > 0) {') |
| 85 |
print(' __m256i x,y;') |
| 86 |
print(' --i;') |
| 87 |
if looplen%8: |
| 88 |
print(' if (!i) {') |
| 89 |
print(' reading -= %d;' % (2*(8-(looplen%8)))) |
| 90 |
print(' writing -= %d;' % (8-(looplen%8))) |
| 91 |
print(' out -= %d;' % (8-(looplen%8))) |
| 92 |
print(' }') |
| 93 |
print(' x = _mm256_loadu_si256((__m256i *) reading);') |
| 94 |
if reading == 'R0' and round: |
| 95 |
print(' x = _mm256_mulhrs_epi16(x,_mm256_set1_epi16(10923));') |
| 96 |
if 0: |
| 97 |
print(' x = _mm256_mullo_epi16(x,_mm256_set1_epi16(3));') |
| 98 |
else: |
| 99 |
print(' x = _mm256_add_epi16(x,_mm256_add_epi16(x,x));') |
| 100 |
if reading == 'R0' and offset != 0: |
| 101 |
print(' x = _mm256_add_epi16(x,_mm256_set1_epi16(%d));' % offset) |
| 102 |
if reading == 'R0': |
| 103 |
print(' x &= _mm256_set1_epi16(16383);') |
| 104 |
if reading == 'R0' and div3: |
| 105 |
print(' x = _mm256_mulhi_epi16(x,_mm256_set1_epi16(21846));') |
| 106 |
|
| 107 |
print(' y = x & _mm256_set1_epi32(65535);') |
| 108 |
print(' x = _mm256_srli_epi32(x,16);') |
| 109 |
print(' x = _mm256_mullo_epi32(x,_mm256_set1_epi32(%d));' % m0) |
| 110 |
print(' x = _mm256_add_epi32(y,x);') |
| 111 |
print(' x = _mm256_shuffle_epi8(x,_mm256_set_epi8(') |
| 112 |
print(' 12,8,4,0,12,8,4,0,14,13,10,9,6,5,2,1,') |
| 113 |
print(' 12,8,4,0,12,8,4,0,14,13,10,9,6,5,2,1') |
| 114 |
print(' ));') |
| 115 |
print(' x = _mm256_permute4x64_epi64(x,0xd8);') |
| 116 |
print(' _mm_storeu_si128((__m128i *) writing,_mm256_extractf128_si256(x,0));') |
| 117 |
print(' *((uint32 *) (out+0)) = _mm256_extract_epi32(x,4);') |
| 118 |
print(' *((uint32 *) (out+4)) = _mm256_extract_epi32(x,6);') |
| 119 |
print(' reading += 16;') |
| 120 |
print(' writing += 8;') |
| 121 |
print(' out += 8;') |
| 122 |
print(' }') |
| 123 |
return |
| 124 |
|
| 125 |
if looplen >= 24 and bytes == 2: |
| 126 |
print(' reading = (uint16 *) %s;' % reading) |
| 127 |
print(' writing = %s;' % todo) |
| 128 |
print(' i = %d;' % ((looplen+15)//16)) |
| 129 |
print(' while (i > 0) {') |
| 130 |
print(' __m256i x,x2,y,y2;') |
| 131 |
print(' --i;') |
| 132 |
if looplen%16: |
| 133 |
print(' if (!i) {') |
| 134 |
print(' reading -= %d;' % (2*(16-(looplen%16)))) |
| 135 |
print(' writing -= %d;' % (16-(looplen%16))) |
| 136 |
print(' out -= %d;' % (2*(16-(looplen%16)))) |
| 137 |
print(' }') |
| 138 |
print(' x = _mm256_loadu_si256((__m256i *) (reading+0));') |
| 139 |
print(' x2 = _mm256_loadu_si256((__m256i *) (reading+16));') |
| 140 |
if reading == 'R0' and round: |
| 141 |
print(' x = _mm256_mulhrs_epi16(x,_mm256_set1_epi16(10923));') |
| 142 |
print(' x2 = _mm256_mulhrs_epi16(x2,_mm256_set1_epi16(10923));') |
| 143 |
if 0: |
| 144 |
print(' x = _mm256_mullo_epi16(x,_mm256_set1_epi16(3));') |
| 145 |
print(' x2 = _mm256_mullo_epi16(x2,_mm256_set1_epi16(3));') |
| 146 |
else: |
| 147 |
print(' x = _mm256_add_epi16(x,_mm256_add_epi16(x,x));') |
| 148 |
print(' x2 = _mm256_add_epi16(x2,_mm256_add_epi16(x2,x2));') |
| 149 |
if reading == 'R0' and offset != 0: |
| 150 |
print(' x = _mm256_add_epi16(x,_mm256_set1_epi16(%d));' % offset) |
| 151 |
print(' x2 = _mm256_add_epi16(x2,_mm256_set1_epi16(%d));' % offset) |
| 152 |
if reading == 'R0': |
| 153 |
print(' x &= _mm256_set1_epi16(16383);') |
| 154 |
print(' x2 &= _mm256_set1_epi16(16383);') |
| 155 |
if reading == 'R0' and div3: |
| 156 |
print(' x = _mm256_mulhi_epi16(x,_mm256_set1_epi16(21846));') |
| 157 |
print(' x2 = _mm256_mulhi_epi16(x2,_mm256_set1_epi16(21846));') |
| 158 |
|
| 159 |
print(' y = x & _mm256_set1_epi32(65535);') |
| 160 |
print(' y2 = x2 & _mm256_set1_epi32(65535);') |
| 161 |
print(' x = _mm256_srli_epi32(x,16);') |
| 162 |
print(' x2 = _mm256_srli_epi32(x2,16);') |
| 163 |
print(' x = _mm256_mullo_epi32(x,_mm256_set1_epi32(%d));' % m0) |
| 164 |
print(' x2 = _mm256_mullo_epi32(x2,_mm256_set1_epi32(%d));' % m0) |
| 165 |
print(' x = _mm256_add_epi32(y,x);') |
| 166 |
print(' x2 = _mm256_add_epi32(y2,x2);') |
| 167 |
print(' x = _mm256_shuffle_epi8(x,_mm256_set_epi8(') |
| 168 |
print(' 15,14,11,10,7,6,3,2,13,12,9,8,5,4,1,0,') |
| 169 |
print(' 15,14,11,10,7,6,3,2,13,12,9,8,5,4,1,0') |
| 170 |
print(' ));') |
| 171 |
print(' x2 = _mm256_shuffle_epi8(x2,_mm256_set_epi8(') |
| 172 |
print(' 15,14,11,10,7,6,3,2,13,12,9,8,5,4,1,0,') |
| 173 |
print(' 15,14,11,10,7,6,3,2,13,12,9,8,5,4,1,0') |
| 174 |
print(' ));') |
| 175 |
print(' x = _mm256_permute4x64_epi64(x,0xd8);') |
| 176 |
print(' x2 = _mm256_permute4x64_epi64(x2,0xd8);') |
| 177 |
print(' _mm256_storeu_si256((__m256i *) writing,_mm256_permute2f128_si256(x,x2,0x31));') |
| 178 |
print(' _mm256_storeu_si256((__m256i *) out,_mm256_permute2f128_si256(x,x2,0x20));') |
| 179 |
print(' reading += 32;') |
| 180 |
print(' writing += 16;') |
| 181 |
print(' out += 32;') |
| 182 |
print(' }') |
| 183 |
return |
| 184 |
|
| 185 |
print(' for (i = 0;i < %d;++i) {' % looplen) |
| 186 |
print(' r0 = %s;' % access(reading,'2*i')) |
| 187 |
print(' r1 = %s;' % access(reading,'2*i+1')) |
| 188 |
print(' r2 = r0+r1*(uint32)%d;' % m0) |
| 189 |
for j in range(bytes): |
| 190 |
print(' *out++ = r2; r2 >>= 8;') |
| 191 |
print(' %s[i] = r2;' % todo) |
| 192 |
print(' }') |
| 193 |
|
| 194 |
todo = 'R' |
| 195 |
while Mlen > 1: |
| 196 |
print(' ') |
| 197 |
|
| 198 |
n0 = m0*m0 |
| 199 |
bytes0 = 0 |
| 200 |
while n0 >= top: |
| 201 |
bytes0 += 1 |
| 202 |
n0 = (n0+255)>>8 |
| 203 |
|
| 204 |
if Mlen&1: |
| 205 |
looplen = Mlen//2 |
| 206 |
printloop(looplen,reading,todo,m0,bytes0) |
| 207 |
r0 = access(reading,2*looplen) |
| 208 |
print(' %s[%d] = %s;' % (todo,looplen,r0)) |
| 209 |
n1 = m1 |
| 210 |
|
| 211 |
else: |
| 212 |
n1 = m0*m1 |
| 213 |
bytes1 = 0 |
| 214 |
while n1 >= top: |
| 215 |
bytes1 += 1 |
| 216 |
n1 = (n1+255)>>8 |
| 217 |
|
| 218 |
if bytes1 == bytes0: |
| 219 |
looplen = (Mlen+1)//2 |
| 220 |
printloop(looplen,reading,todo,m0,bytes0) |
| 221 |
else: |
| 222 |
looplen = (Mlen-1)//2 |
| 223 |
printloop(looplen,reading,todo,m0,bytes0) |
| 224 |
print(' r0 = %s;' % access(reading,2*looplen)) |
| 225 |
print(' r1 = %s;' % access(reading,2*looplen+1)) |
| 226 |
print(' r2 = r0+r1*(uint32)%d;' % m0) |
| 227 |
for j in range(bytes1): |
| 228 |
print(' *out++ = r2; r2 >>= 8;') |
| 229 |
print(' %s[%d] = r2;' % (todo,looplen)) |
| 230 |
|
| 231 |
m0,m1,Mlen = n0,n1,(Mlen+1)//2 |
| 232 |
reading = todo |
| 233 |
|
| 234 |
print(' ') |
| 235 |
print(' r0 = %s;' % access(reading,0)) |
| 236 |
while m1 > 1: |
| 237 |
print(' *out++ = r0; r0 >>= 8;') |
| 238 |
m1 = (m1+255)>>8 |
| 239 |
|
| 240 |
print('}') |