ref: 2869a292caf121ee26d4fc5f0594c051210e9e2f
parent: 16a2cc57f9cd49f84db08e9c46478d8d0acfdf45
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Jul 14 12:13:09 EDT 2025
add missing file
--- /dev/null
+++ b/libc.c
@@ -1,0 +1,158 @@
+#include <u.h>
+#include <libc.h>
+
+#include "bench.h"
+
+char *dst;
+char *src;
+
+enum {
+ Same,
+ Start,
+ Mid,
+ End,
+};
+
+void
+benchmemcpy(B *b, int sz)
+{
+ int i;
+
+ for(i = 0; i < b->N; i++)
+ memcpy(dst, src, sz);
+}
+
+void
+benchmemmovedisjoint(B *b, int sz)
+{
+ int i;
+
+ for(i = 0; i < b->N; i++)
+ memmove(dst, src, sz);
+}
+
+void
+benchmemmoveoverlap(B *b, int sz)
+{
+ int i;
+
+ for(i = 0; i < b->N; i++)
+ memmove(dst, src+1, sz);
+}
+
+void
+benchmemcmp(B *b, int sz, int where)
+{
+ int i;
+
+ memset(src, 0xea, sz);
+ memset(dst, 0xea, sz);
+ switch(where){
+ case Start:
+ dst[0] = 1;
+ break;
+ case Mid:
+ dst[sz/2] = 1;
+ break;
+ case End:
+ dst[sz-1] = 1;
+ break;
+ }
+ for(i = 0; i < b->N; i++)
+ memcmp(dst, src, sz);
+}
+
+
+
+void benchmemcpy_0b(B *b) { benchmemcpy(b, 0); }
+void benchmemmovedisjoint_0b(B *b) { benchmemmovedisjoint(b, 0); }
+void benchmemmoveoverlap_0b(B *b) { benchmemmoveoverlap(b, 0); }
+void benchmemcmp_16k(B *b) { benchmemcmp(b, 0, Same); }
+
+void benchmemcpy_16b(B *b) { benchmemcpy(b, 16); }
+void benchmemmoveoverlap_16b(B *b) { benchmemmovedisjoint(b, 16); }
+void benchmemmovedisjoint_16b(B *b) { benchmemmoveoverlap(b, 16); }
+void benchmemcmpsame_16b(B *b) { benchmemcmp(b, 16, Same); }
+void benchmemcmpdiffhead_16b(B *b) { benchmemcmp(b, 16, Start); }
+void benchmemcmpdiffmid_16b(B *b) { benchmemcmp(b, 16, Mid); }
+void benchmemcmpdifftail_16b(B *b) { benchmemcmp(b, 16, End); }
+
+void benchmemcpy_16k(B *b) { benchmemcpy(b, 16*KiB); }
+void benchmemmoveoverlap_16k(B *b) { benchmemmovedisjoint(b, 16*KiB); }
+void benchmemmovedisjoint_16k(B *b) { benchmemmoveoverlap(b, 16*KiB); }
+void benchmemcmpsame_16k(B *b) { benchmemcmp(b, 16*KiB, Same); }
+void benchmemcmpdiffhead_16k(B *b) { benchmemcmp(b, 16*KiB, Start); }
+void benchmemcmpdiffmid_16k(B *b) { benchmemcmp(b, 16*KiB, Mid); }
+void benchmemcmpdifftail_16k(B *b) { benchmemcmp(b, 16*KiB, End); }
+
+void benchmemcpy_16m(B *b) { benchmemcpy(b, 16*MiB); }
+void benchmemmoveoverlap_16m(B *b) { benchmemmovedisjoint(b, 16*MiB); }
+void benchmemmovedisjoint_16m(B *b) { benchmemmoveoverlap(b, 16*MiB); }
+void benchmemcmpsame_16m(B *b) { benchmemcmp(b, 16*MiB, Same); }
+void benchmemcmpdiffhead_16m(B *b) { benchmemcmp(b, 16*MiB, Start); }
+void benchmemcmpdiffmid_16m(B *b) { benchmemcmp(b, 16*MiB, Mid); }
+void benchmemcmpdifftail_16m(B *b) { benchmemcmp(b, 16*MiB, End); }
+
+void benchmemcpy_1g(B *b) { benchmemcpy(b, 1*GiB); }
+void benchmemmoveoverlap_16g(B *b) { benchmemmovedisjoint(b, 1*GiB); }
+void benchmemmovedisjoint_16g(B *b) { benchmemmoveoverlap(b, 1*GiB); }
+void benchmemcmpsame_1g(B *b) { benchmemcmp(b, 1*GiB, Same); }
+void benchmemcmpdiffhead_1g(B *b) { benchmemcmp(b, 1*GiB, Start); }
+void benchmemcmpdiffmid_1g(B *b) { benchmemcmp(b, 1*GiB, Mid); }
+void benchmemcmpdifftail_1g(B *b) { benchmemcmp(b, 1*GiB, End); }
+
+void
+main(int argc, char **argv)
+{
+ benchinit(argc, argv);
+
+ if((src = malloc(1*GiB + KiB)) == nil)
+ sysfatal("malloc: %r");
+ if((dst = malloc(1*GiB + KiB)) == nil)
+ sysfatal("malloc: %r");
+
+ print("== small nops (0 b) ==\n");
+ BM(benchmemcpy_0b);
+ BM(benchmemmovedisjoint_16b);
+ BM(benchmemmoveoverlap_16b);
+ BM(benchmemcmp_16k);
+
+ print("== small memory ops (16b) ==\n");
+ BM(benchmemcpy_16b);
+ BM(benchmemmoveoverlap_16b);
+ BM(benchmemmovedisjoint_16b);
+ BM(benchmemcmpsame_16b);
+ BM(benchmemcmpdiffhead_16b);
+ BM(benchmemcmpdiffmid_16b);
+ BM(benchmemcmpdifftail_16b);
+
+ print("== midsize memory ops (16k) ==\n");
+ BM(benchmemcpy_16k);
+ BM(benchmemmoveoverlap_16k);
+ BM(benchmemmovedisjoint_16k);
+ BM(benchmemcmpsame_16k);
+ BM(benchmemcmpdiffhead_16k);
+ BM(benchmemcmpdiffmid_16k);
+ BM(benchmemcmpdifftail_16k);
+
+ print("== large memory ops (16m) ==\n");
+ BM(benchmemcpy_16m);
+ BM(benchmemmoveoverlap_16m);
+ BM(benchmemmovedisjoint_16m);
+ BM(benchmemcmpsame_16m);
+ BM(benchmemcmpdiffhead_16m);
+ BM(benchmemcmpdiffmid_16m);
+ BM(benchmemcmpdifftail_16m);
+
+ print("== huge memory ops (1g) ==\n");
+ BM(benchmemcpy_1g);
+ BM(benchmemmoveoverlap_16g);
+ BM(benchmemmovedisjoint_16g);
+ BM(benchmemcmpsame_1g);
+ BM(benchmemcmpdiffhead_1g);
+ BM(benchmemcmpdiffmid_1g);
+ BM(benchmemcmpdifftail_1g);
+
+ exits(nil);
+}
+
--
⑨