Skip to content

Instantly share code, notes, and snippets.

@ilyakurdyukov
Created November 22, 2021 09:47
Show Gist options
  • Save ilyakurdyukov/b0987a2faee4938aa21cb4d62f1e62af to your computer and use it in GitHub Desktop.
Save ilyakurdyukov/b0987a2faee4938aa21cb4d62f1e62af to your computer and use it in GitHub Desktop.
SSE2 assembly to intrinsics convertor. Unfinished, but still useful for porting.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int is_mem(const char *s, int n) {
int a, i;
for (i = 0; i < n; i++) {
a = s[i];
if (a == '[' || a == ']') return 1;
}
return 0;
}
// TODO:
// xor,sub same reg = _mm_setzero_si128();
// cmpeq same reg = _mm_set1_?(-1);
int main() {
static const char *repl[][2] = {
{ "movq", "\0_mm_loadl_epi64\0_mm_storel_epi64" },
{ "movdqa", "\0_mm_load_si128\0_mm_store_si128" },
{ "movdqu", "\0_mm_loadu_si128\0_mm_storeu_si128" },
{ "pand", "_mm_and_si128" },
{ "pandn", "_mm_andnot_si128" },
{ "pxor", "_mm_xor_si128" },
{ "por", "_mm_or_si128" },
{ "paddb", "_mm_add_epi8" },
{ "paddw", "_mm_add_epi16" },
{ "paddd", "_mm_add_epi32" },
{ "psubb", "_mm_sub_epi8" },
{ "psubw", "_mm_sub_epi16" },
{ "psubd", "_mm_sub_epi32" },
{ "paddsb", "_mm_adds_epi8" },
{ "paddsw", "_mm_adds_epi16" },
{ "psubsb", "_mm_subs_epi8" },
{ "psubsw", "_mm_subs_epi16" },
{ "paddusb", "_mm_adds_epu8" },
{ "paddusw", "_mm_adds_epu16" },
{ "psubusb", "_mm_subs_epu8" },
{ "psubusw", "_mm_subs_epu16" },
{ "psllw", "_mm_slli_epi16" },
{ "pslld", "_mm_slli_epi32" },
{ "psllq", "_mm_slli_epi64" },
{ "psrlw", "_mm_srli_epi16" },
{ "psrld", "_mm_srli_epi32" },
{ "psrlq", "_mm_srli_epi64" },
{ "psraw", "_mm_srai_epi16" },
{ "psrad", "_mm_srai_epi32" },
{ "pslldq", "_mm_bslli_si128" },
{ "psrldq", "_mm_bsrli_si128" },
{ "packsswb", "_mm_packs_epi16" },
{ "packssdw", "_mm_packs_epi32" },
{ "packuswb", "_mm_packus_epi16" },
{ "packusdw", "_mm_packus_epi32" },
{ "punpcklbw", "_mm_unpacklo_epi8" },
{ "punpckhbw", "_mm_unpackhi_epi8" },
{ "punpcklwd", "_mm_unpacklo_epi16" },
{ "punpckhwd", "_mm_unpackhi_epi16" },
{ "punpckldq", "_mm_unpacklo_epi32" },
{ "punpckhdq", "_mm_unpackhi_epi32" },
{ "punpcklqdq", "_mm_unpacklo_epi64" },
{ "punpckhqdq", "_mm_unpackhi_epi64" },
{ "pcmpeqb", "_mm_cmpeq_epi8" },
{ "pcmpeqw", "_mm_cmpeq_epi16" },
{ "pcmpeqd", "_mm_cmpeq_epi32" },
{ "pcmpgtb", "_mm_cmpgt_epi8" },
{ "pcmpgtw", "_mm_cmpgt_epi16" },
{ "pcmpgtd", "_mm_cmpgt_epi32" },
{ "pshufb", "_mm_shuffle_epi8" },
{ "pshufd", "_mm_shuffle_epi32" },
{ "pshuflw", "_mm_shufflelo_epi16" },
{ "pshufhw", "_mm_shufflehi_epi16" },
{ "pmullw", "_mm_mullo_epi16" },
{ "pmulhw", "_mm_mulhi_epi16" },
{ "pmulhuw", "_mm_mulhi_epu16" },
{ "pminub", "_mm_min_epu8" },
{ "pmaxub", "_mm_max_epu8" },
{ "pminsw", "_mm_min_epi16" },
{ "pmaxsw", "_mm_max_epi16" },
{ "paddsb", "_mm_adds_epi8" },
{ "paddsw", "_mm_adds_epi16" },
{ "pmaddubsw", "_mm_maddubs_epi16" },
{ "pmaddwd", "_mm_madd_epi16" }
};
char buf[256];
for (;;) {
int a, e, i0 = 0, i1, i2, i3, i4, i5, i6, i7, i8, k, n = 0, found;
do {
e = getchar();
buf[n++] = e;
} while (n < sizeof(buf) && e != '\n' && e != '\r' && e != ';' && e != EOF);
buf[--n] = 0;
#define SKIPWS(i, i0) \
for (i = i0; ; i++) { \
a = buf[i]; if (a != ' ' && a != '\t') break; \
}
#define SKIPWORD(i, i0) \
for (i = i0; ; i++) { \
a = buf[i]; if (!a || a == ' ' || a == '\t' || a == ',') break; \
}
#define SKIPARG(j, i, i0) \
for (i = j = i0; ; j++) { \
a = buf[j]; \
if (!a || a == ',') break; \
if (a != ' ' && a != '\t') i = j + 1; \
}
#define FINDREPL(r) \
for (k = 0; k < sizeof(r) / sizeof(*r); k++) \
if (buf[i1] == **r[k]) { \
int l = strlen(r[k][0]); \
if (i2 - i1 == l && !memcmp(buf + i1, *r[k], l)) break; \
} \
found = k < sizeof(r) / sizeof(*r);
do {
SKIPWS(i1, 0) if (!a) break;
SKIPWORD(i2, i1) if (!a || a == ',') break;
FINDREPL(repl)
if (!found) break;
if (!*repl[k][1]) {
SKIPWS(i3, i2) if (!a) break;
SKIPARG(i5, i4, i3) if (!a) break;
if (a != ',') break;
i5++;
SKIPWS(i5, i5) if (!a) break;
SKIPARG(i7, i6, i5)
if (is_mem(buf + i3, i4 - i3)) {
printf("\t%s(%.*s, %.*s);",
repl[k][1] + strlen(repl[k][1] + 1) + 2, i4 - i3, buf + i3, i6 - i5, buf + i5);
} else if (is_mem(buf + i5, i6 - i5)) {
printf("\t%.*s = %s(%.*s);",
i4 - i3, buf + i3, repl[k][1] + 1, i6 - i5, buf + i5);
} else {
printf("\t%.*s = %.*s;",
i4 - i3, buf + i3, i6 - i5, buf + i5);
}
i0 = i6;
break;
}
SKIPWS(i3, i2) if (!a) break;
SKIPARG(i5, i4, i3) if (!a) break;
if (a != ',') break;
i5++;
SKIPWS(i5, i5) if (!a) break;
SKIPARG(i7, i6, i5)
if (a != ',') {
printf("\t%.*s = %s(%.*s, %.*s);",
i4 - i3, buf + i3, repl[k][1],
i4 - i3, buf + i3, i6 - i5, buf + i5);
i0 = i6;
} else {
i7++;
SKIPWS(i7, i7) if (!a) break;
SKIPWORD(i8, i7)
printf("\t%.*s = %s(%.*s, %.*s);",
i4 - i3, buf + i3, repl[k][1],
i6 - i5, buf + i5, i8 - i7, buf + i7);
i0 = i8;
}
} while (0);
printf("%s", buf + i0);
if (e == EOF) break;
if (e == ';') printf("//");
else putchar(e);
while (e != '\n' && e != '\r') {
e = getchar();
if (e == EOF) return 0;
putchar(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment