|
| 1 | +#include "include/utils.h" |
| 2 | +#include <cstdio> |
| 3 | + |
| 4 | +int main(int argc, char *argv[]) { |
| 5 | + FILE *fp = fopen(argv[1], "w"); |
| 6 | + assert(fp); |
| 7 | +#ifdef HOST_AARCH64 |
| 8 | + int num_patterns = 6; |
| 9 | +#else |
| 10 | + int num_patterns = 4; |
| 11 | +#endif |
| 12 | + int repeat = 1000; |
| 13 | + |
| 14 | + // args: loop count |
| 15 | + fprintf(fp, ".text\n"); |
| 16 | + for (int pattern = 0; pattern < num_patterns; pattern++) { |
| 17 | + // entry |
| 18 | + fprintf(fp, ".global fp_peak_%d\n", pattern); |
| 19 | + fprintf(fp, ".balign 32\n"); |
| 20 | + fprintf(fp, "fp_peak_%d:\n", pattern); |
| 21 | +#if defined(HOST_AARCH64) |
| 22 | + fprintf(fp, "\tsub sp, sp, #0x100\n"); |
| 23 | + fprintf(fp, "\tstp q8, q9, [sp, #0x0]\n"); |
| 24 | + fprintf(fp, "\tstp q10, q11, [sp, #0x20]\n"); |
| 25 | + fprintf(fp, "\tstp q12, q13, [sp, #0x40]\n"); |
| 26 | + fprintf(fp, "\tstp q14, q15, [sp, #0x60]\n"); |
| 27 | + |
| 28 | + if (pattern == 4 || pattern == 5) { |
| 29 | + fprintf(fp, "\t.arch armv9-a+sve\n"); |
| 30 | + fprintf(fp, "\tptrue p0.b\n"); |
| 31 | + } |
| 32 | + |
| 33 | + fprintf(fp, "\t1:\n"); |
| 34 | + for (int i = 0; i < repeat; i++) { |
| 35 | + if (pattern == 0) { |
| 36 | + // single precision 32-bit fma using FMADD |
| 37 | + fprintf(fp, "\tfmadd s%d, s0, s1, s2\n", (i % 16) + 3); |
| 38 | + } else if (pattern == 1) { |
| 39 | + // double precision 64-bit fma using FMADD |
| 40 | + fprintf(fp, "\tfmadd d%d, d0, d1, d2\n", (i % 16) + 3); |
| 41 | + } else if (pattern == 2) { |
| 42 | + // single precision 128-bit fma using ASIMD |
| 43 | + fprintf(fp, "\tfmla v%d.4s, v0.4s, v1.4s\n", (i % 16) + 2); |
| 44 | + } else if (pattern == 3) { |
| 45 | + // double precision 128-bit fma using ASIMD |
| 46 | + fprintf(fp, "\tfmla v%d.2d, v0.2d, v1.2d\n", (i % 16) + 2); |
| 47 | + } else if (pattern == 4) { |
| 48 | + // single precision fma using SVE |
| 49 | + fprintf(fp, "\tfmla z%d.s, p0/m, z0.s, z1.s\n", (i % 16) + 2); |
| 50 | + } else if (pattern == 5) { |
| 51 | + // double precision fma using SVE |
| 52 | + fprintf(fp, "\tfmla z%d.d, p0/m, z0.d, z1.d\n", (i % 16) + 2); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fprintf(fp, "\tsubs x0, x0, #1\n"); |
| 57 | + fprintf(fp, "\tbne 1b\n"); |
| 58 | + |
| 59 | + fprintf(fp, "\tldp q8, q9, [sp, #0x0]\n"); |
| 60 | + fprintf(fp, "\tldp q10, q11, [sp, #0x20]\n"); |
| 61 | + fprintf(fp, "\tldp q12, q13, [sp, #0x40]\n"); |
| 62 | + fprintf(fp, "\tldp q14, q15, [sp, #0x60]\n"); |
| 63 | + fprintf(fp, "\tadd sp, sp, #0x100\n"); |
| 64 | + fprintf(fp, "\tret\n"); |
| 65 | +#endif |
| 66 | +#if defined(HOST_AMD64) |
| 67 | + fprintf(fp, "\t1:\n"); |
| 68 | + for (int i = 0; i < repeat; i++) { |
| 69 | + if (pattern == 0) { |
| 70 | + // single precision 256-bit fma using FMA |
| 71 | + fprintf(fp, "\tvfmadd213ps %%ymm0, %%ymm1, %%ymm%d\n", (i % 14) + 2); |
| 72 | + } else if (pattern == 1) { |
| 73 | + // double precision 256-bit fma using FMA |
| 74 | + fprintf(fp, "\tvfmadd213pd %%ymm0, %%ymm1, %%ymm%d\n", (i % 14) + 2); |
| 75 | + } else if (pattern == 2) { |
| 76 | + // single precision 512-bit fma using AVX512F |
| 77 | + fprintf(fp, "\tvfmadd213ps %%zmm0, %%zmm1, %%zmm%d\n", (i % 14) + 2); |
| 78 | + } else if (pattern == 3) { |
| 79 | + // double precision 512-bit fma using AVX512F |
| 80 | + fprintf(fp, "\tvfmadd213pd %%zmm0, %%zmm1, %%zmm%d\n", (i % 14) + 2); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fprintf(fp, "\tdec %%rdi\n"); |
| 85 | + fprintf(fp, "\tjnz 1b\n"); |
| 86 | + |
| 87 | + fprintf(fp, "\tret\n"); |
| 88 | +#endif |
| 89 | + } |
| 90 | + |
| 91 | + define_gadgets_array(fp, "fp_peak_gadgets"); |
| 92 | + for (int pattern = 0; pattern < num_patterns; pattern++) { |
| 93 | + add_gadget(fp, "fp_peak_%d", pattern); |
| 94 | + } |
| 95 | + return 0; |
| 96 | +} |
0 commit comments