From 2e911139688fde9138fa4ee84e999bcc78c56931 Mon Sep 17 00:00:00 2001 From: clamchowder Date: Thu, 8 Aug 2024 12:52:37 -0700 Subject: [PATCH] save x87 state --- AsmGen/UarchTest.cs | 1 + AsmGen/UarchTestHelpers.cs | 39 +++----------------------------------- AsmGen/tests/MmxRfTest.cs | 9 +++++++-- 3 files changed, 11 insertions(+), 38 deletions(-) diff --git a/AsmGen/UarchTest.cs b/AsmGen/UarchTest.cs index 18c11ba..3530b34 100644 --- a/AsmGen/UarchTest.cs +++ b/AsmGen/UarchTest.cs @@ -93,6 +93,7 @@ public void GenerateTestBlock(StringBuilder sb, IUarchTest.ISA isa) sb.AppendLine("#endif"); sb.AppendLine(" gettimeofday(&endTv, &endTz);"); sb.AppendLine(" time_diff_ms = 1000 * (endTv.tv_sec - startTv.tv_sec) + ((endTv.tv_usec - startTv.tv_usec) / 1000);"); + //sb.AppendLine(" fprintf(stderr, \"%lu ms elapsed, %lu iter\\n\", time_diff_ms, structIterations);"); if (DivideTimeByCount) sb.AppendLine(" latency = 1e6 * (float)time_diff_ms / (float)(iterations);"); else diff --git a/AsmGen/UarchTestHelpers.cs b/AsmGen/UarchTestHelpers.cs index 16ef70c..2dbe569 100644 --- a/AsmGen/UarchTestHelpers.cs +++ b/AsmGen/UarchTestHelpers.cs @@ -47,41 +47,6 @@ public static void GenerateVsExternLines(StringBuilder sb, UarchTest test) sb.AppendLine("extern \"C\" uint64_t " + test.Prefix + counts[i] + $"({test.FunctionDefinitionParameters});"); } - public static void GenerateTestBlock(StringBuilder sb, UarchTest test) - { - sb.AppendLine(" if (argc > 1 && strncmp(test_name, \"" + test.Prefix + "\", " + test.Prefix.Length + ") == 0) {"); - sb.AppendLine(" printf(\"" + test.Description + ":\\n\");"); - - int[] counts = test.Counts; - for (int i = 0; i < counts.Length; i++) - { - // use more iterations (iterations = structIterations * 100) and divide iteration count by tested-thing count - // for certain tests like call stack depth - if (test.DivideTimeByCount) - { - sb.AppendLine(" tmp = structIterations;"); - sb.AppendLine(" structIterations = iterations / " + counts[i] + ";"); - } - - sb.AppendLine(" gettimeofday(&startTv, &startTz);"); - sb.AppendLine(" " + test.Prefix + counts[i] + $"({test.GetFunctionCallParameters});"); - sb.AppendLine(" gettimeofday(&endTv, &endTz);"); - sb.AppendLine(" time_diff_ms = 1000 * (endTv.tv_sec - startTv.tv_sec) + ((endTv.tv_usec - startTv.tv_usec) / 1000);"); - if (test.DivideTimeByCount) - sb.AppendLine(" latency = 1e6 * (float)time_diff_ms / (float)(iterations);"); - else - sb.AppendLine(" latency = 1e6 * (float)time_diff_ms / (float)(structIterations);"); - sb.AppendLine(" printf(\"" + counts[i] + ",%f\\n\", latency);\n"); - - if (test.DivideTimeByCount) - { - sb.AppendLine(" structIterations = tmp;"); - } - } - - sb.AppendLine(" }\n"); - } - /// /// Generates test functions in assembly, with filler instructions between two divs /// Args are put into rcx, rdx, r8 (in that order) to match Windows calling convention @@ -324,7 +289,8 @@ public static void GenerateX86AsmStructureTestFuncs(StringBuilder sb, string initInstrs = null, string postLoadInstrs1 = null, string postLoadInstrs2 = null, - bool lfence = true) + bool lfence = true, + string cleanupInstrs = null) { for (int i = 0; i < counts.Length; i++) { @@ -383,6 +349,7 @@ public static void GenerateX86AsmStructureTestFuncs(StringBuilder sb, sb.AppendLine(" dec %rcx"); sb.AppendLine(" jne " + funcName + "start"); + if (cleanupInstrs != null) sb.AppendLine(cleanupInstrs); sb.AppendLine(" pop %rdx"); sb.AppendLine(" pop %rcx"); sb.AppendLine(" pop %r8"); diff --git a/AsmGen/tests/MmxRfTest.cs b/AsmGen/tests/MmxRfTest.cs index 47a5995..65e4fa6 100644 --- a/AsmGen/tests/MmxRfTest.cs +++ b/AsmGen/tests/MmxRfTest.cs @@ -26,19 +26,24 @@ public override void GenerateAsm(StringBuilder sb, IUarchTest.ISA isa) public void GenerateX86GccAsm(StringBuilder sb) { - string initInstrs = " movq (%rdx), %mm0\n" + + string initInstrs = + " fsave (%r8)\n" + + " movq (%rdx), %mm0\n" + " movq 8(%rdx), %mm1\n" + " movq 16(%rdx), %mm2\n" + " movq 24(%rdx), %mm3\n" + " movq 32(%rdx), %mm4\n"; + string cleanupInstrs = " frstor (%r8)"; + string[] unrolledAdds = new string[4]; unrolledAdds[0] = " paddw %mm0, %mm1"; unrolledAdds[1] = " paddw %mm0, %mm2"; unrolledAdds[2] = " paddw %mm0, %mm3"; unrolledAdds[3] = " paddw %mm0, %mm4"; - UarchTestHelpers.GenerateX86AsmStructureTestFuncs(sb, this.Counts, this.Prefix, unrolledAdds, unrolledAdds, initInstrs: initInstrs); + UarchTestHelpers.GenerateX86AsmStructureTestFuncs( + sb, this.Counts, this.Prefix, unrolledAdds, unrolledAdds, initInstrs: initInstrs, cleanupInstrs: cleanupInstrs); } } } \ No newline at end of file