18 QueryPerformanceFrequency(&f);
19 QueryPerformanceCounter(&c);
20 return (
double)c.QuadPart / (double)
f.QuadPart;
26 clock_gettime(CLOCK_MONOTONIC, &ts);
27 return (
double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
32#define NUM_COMPONENTS 128
42static int pow_mod(
int base,
int exp,
int mod) {
43 long long r = 1, b = base % mod;
45 if (exp & 1) r = (r * b) % mod;
66 uint16_t *H = (uint16_t *)malloc((
size_t)
NUM_COMPONENTS * 3 *
Q *
sizeof(uint16_t));
67 if (!H) { fprintf(stderr,
"alloc failed\n"); exit(1); }
69 int gs1 = (int)(((
long long)gammas[i] * s1v[i]) %
Q);
70 for (
int c = 0; c <
Q; c++) {
71 H[((size_t)i * 3 + 0) *
Q + c] = (uint16_t)(((
long long)s0v[i] * c) %
Q);
72 H[((size_t)i * 3 + 1) *
Q + c] = (uint16_t)(((
long long)s1v[i] * c) %
Q);
73 H[((size_t)i * 3 + 2) *
Q + c] = (uint16_t)(((
long long)gs1 * c) %
Q);
78 const long ITERS = 40000000L;
83 int *ai = (
int *)malloc(NIN *
sizeof(
int));
84 int *c0 = (
int *)malloc(NIN *
sizeof(
int));
85 int *c1 = (
int *)malloc(NIN *
sizeof(
int));
86 for (
int k = 0; k < NIN; k++) {
92 volatile uint32_t sink = 0;
96 for (
long k = 0; k < ITERS; k++) {
97 int j = (int)(k & (NIN - 1));
98 int i = ai[j], sv0 = s0v[i], sv1 = s1v[i], g = gammas[i];
99 int p0 = (int)(((
long long)sv0 * c0[j] + (
long long)g * sv1 * c1[j]) %
Q);
100 int p1 = (int)(((
long long)sv0 * c1[j] + (
long long)sv1 * c0[j]) %
Q);
101 sink += (uint32_t)(p0 + p1);
103 double t_direct =
now_sec() - t0;
107 for (
long k = 0; k < ITERS; k++) {
108 int j = (int)(k & (NIN - 1));
110 const uint16_t *h0 = &H[((size_t)i * 3 + 0) *
Q];
111 const uint16_t *h1 = &H[((size_t)i * 3 + 1) *
Q];
112 const uint16_t *h2 = &H[((size_t)i * 3 + 2) *
Q];
113 int p0 = (h0[c0[j]] + h2[c1[j]]) %
Q;
114 int p1 = (h0[c1[j]] + h1[c0[j]]) %
Q;
115 sink += (uint32_t)(p0 + p1);
117 double t_table =
now_sec() - t0;
119 double lookup_penalty = t_table / t_direct;
125 const long CITERS = 20000000L;
131 for (
long k = 0; k < CITERS; k++) {
132 g_state = g_state * 6364136223846793005ULL + 1442695040888963407ULL;
133 uint32_t r = (uint32_t)(g_state >> 32);
135 int cc0 = (r >> 13) %
Q, cc1 = (r * 2654435761u) %
Q;
136 sink += (uint32_t)(i + cc0 + cc1);
138 double t_gen =
now_sec() - tg0;
142 for (
long k = 0; k < CITERS; k++) {
143 g_state = g_state * 6364136223846793005ULL + 1442695040888963407ULL;
144 uint32_t r = (uint32_t)(g_state >> 32);
146 int cc0 = (r >> 13) %
Q, cc1 = (r * 2654435761u) %
Q;
147 int sv0 = s0v[i], sv1 = s1v[i], gg = gammas[i];
148 int p0 = (int)(((
long long)sv0 * cc0 + (
long long)gg * sv1 * cc1) %
Q);
149 int p1 = (int)(((
long long)sv0 * cc1 + (
long long)sv1 * cc0) %
Q);
150 sink += (uint32_t)(p0 + p1);
152 double t_direct_cold =
now_sec() - tg0;
156 for (
long k = 0; k < CITERS; k++) {
157 g_state = g_state * 6364136223846793005ULL + 1442695040888963407ULL;
158 uint32_t r = (uint32_t)(g_state >> 32);
160 int cc0 = (r >> 13) %
Q, cc1 = (r * 2654435761u) %
Q;
161 const uint16_t *h0 = &H[((size_t)i * 3 + 0) *
Q];
162 const uint16_t *h1 = &H[((size_t)i * 3 + 1) *
Q];
163 const uint16_t *h2 = &H[((size_t)i * 3 + 2) *
Q];
164 int p0 = (h0[cc0] + h2[cc1]) %
Q;
165 int p1 = (h0[cc1] + h1[cc0]) %
Q;
166 sink += (uint32_t)(p0 + p1);
168 double t_table_cold =
now_sec() - tg0;
170 double work_direct = t_direct_cold - t_gen;
171 double work_table = t_table_cold - t_gen;
172 double realistic_penalty = work_table / work_direct;
174 fprintf(js,
" \"primitive\": {\n");
175 fprintf(js,
" \"iterations\": %ld,\n", ITERS);
176 fprintf(js,
" \"table_bytes\": %zu,\n", table_bytes);
177 fprintf(js,
" \"table_MB\": %.2f,\n", table_bytes / 1e6);
178 fprintf(js,
" \"hot_ns_per_direct\": %.3f,\n", t_direct / ITERS * 1e9);
179 fprintf(js,
" \"hot_ns_per_table_lookup\": %.3f,\n", t_table / ITERS * 1e9);
180 fprintf(js,
" \"hot_lookup_penalty_factor\": %.3f,\n", lookup_penalty);
181 fprintf(js,
" \"hot_note\": \"4096 reused inputs -> touched table subset stays L1-hot; lookups beat arithmetic. Shows the overhead is a cache effect, not an instruction-count effect.\",\n");
182 fprintf(js,
" \"cold_iterations\": %ld,\n", CITERS);
183 fprintf(js,
" \"cold_ns_gen\": %.3f,\n", t_gen / CITERS * 1e9);
184 fprintf(js,
" \"cold_ns_direct_work\": %.3f,\n", work_direct / CITERS * 1e9);
185 fprintf(js,
" \"cold_ns_table_work\": %.3f,\n", work_table / CITERS * 1e9);
186 fprintf(js,
" \"cold_realistic_penalty_factor\": %.3f,\n", realistic_penalty);
187 fprintf(js,
" \"cold_note\": \"inputs random across the full table (gen cost subtracted). Construction B: each base-mul reads the per-component h0/h1/h2 tables (7.67 MB total, k=3) at random ciphertext offsets. Desktop L3-resident regime; embedded targets without L3 place the tables in DRAM (see working_set_sweep DRAM penalty).\",\n");
188 fprintf(js,
" \"sink\": %u\n", (
unsigned)sink);
189 fprintf(js,
" },\n");
191 printf(
"BENCH A hot : direct %.3f ns table %.3f ns penalty %.3fx\n",
192 t_direct / ITERS * 1e9, t_table / ITERS * 1e9, lookup_penalty);
193 printf(
"BENCH A cold: gen %.3f direct-work %.3f table-work %.3f realistic penalty %.3fx\n",
194 t_gen / CITERS * 1e9, work_direct / CITERS * 1e9, work_table / CITERS * 1e9, realistic_penalty);
196 free(H); free(ai); free(c0); free(c1);
203 size_t count = bytes /
sizeof(
node_t);
204 if (count < 2) count = 2;
206 if (!a) { fprintf(stderr,
"alloc failed (%zu bytes)\n", bytes); exit(1); }
209 for (
size_t i = 0; i < count; i++) a[i].next = (uint32_t)i;
210 for (
size_t i = count - 1; i > 0; i--) {
217 for (
long w = 0; w < (long)count; w++) idx = a[idx].next;
221 for (
long s = 0; s < steps; s++) idx = a[idx].next;
224 volatile uint32_t sink = idx; (void)sink;
226 return dt / steps * 1e9;
230 struct {
const char *label;
size_t bytes; } sizes[] = {
231 {
"16KB", 16u * 1024},
232 {
"48KB", 48u * 1024},
233 {
"256KB", 256u * 1024},
234 {
"2MB", 2u * 1024 * 1024},
235 {
"7.67MB", 7670000u},
236 {
"16MB", 16u * 1024 * 1024},
237 {
"36MB", 36u * 1024 * 1024},
238 {
"64MB", 64u * 1024 * 1024},
240 int n = (int)(
sizeof(sizes) /
sizeof(sizes[0]));
241 const long STEPS = 30000000L;
244 fprintf(js,
" \"working_set_sweep\": {\n");
245 fprintf(js,
" \"steps\": %ld,\n", STEPS);
246 fprintf(js,
" \"points\": [\n");
247 for (
int i = 0; i < n; i++) {
249 printf(
"BENCH B: %-8s %.2f ns/access\n", sizes[i].label, lat[i]);
250 fprintf(js,
" {\"label\": \"%s\", \"bytes\": %zu, \"ns_per_access\": %.3f}%s\n",
251 sizes[i].label, sizes[i].bytes, lat[i], (i < n - 1) ?
"," :
"");
253 fprintf(js,
" ],\n");
256 double baseline = lat[0];
257 double at_table = lat[4];
258 double at_dram = lat[n - 1];
259 fprintf(js,
" \"baseline_16KB_ns\": %.3f,\n", baseline);
260 fprintf(js,
" \"table_7p67MB_ns\": %.3f,\n", at_table);
261 fprintf(js,
" \"dram_64MB_ns\": %.3f,\n", at_dram);
262 fprintf(js,
" \"penalty_table_vs_L1\": %.3f,\n", at_table / baseline);
263 fprintf(js,
" \"penalty_dram_vs_L1\": %.3f,\n", at_dram / baseline);
264 fprintf(js,
" \"note\": \"penalty_table_vs_L1 is desktop (table L3-resident). An embedded target with only L1/L2 sees the DRAM-regime penalty (penalty_dram_vs_L1) once the 7.67 MB construction-B tables exceed its cache.\"\n");
267 printf(
"BENCH B: 7.67MB/L1 = %.2fx DRAM/L1 = %.2fx\n", at_table / baseline, at_dram / baseline);
271 FILE *js = fopen(
"results/cache_effect.json",
"w");
275 system(
"if not exist results mkdir results");
277 system(
"mkdir -p results");
279 js = fopen(
"results/cache_effect.json",
"w");
281 if (!js) { fprintf(stderr,
"cannot open results/cache_effect.json\n");
return 1; }
284 fprintf(js,
" \"host\": \"i9-13980HX, L1d 48KB/core, L2 2MB/core, L3 36MB\",\n");
285 fprintf(js,
" \"seed\": 42,\n");
286 fprintf(js,
" \"compiler\": \"gcc -O2\",\n");
291 printf(
"wrote results/cache_effect.json\n");
static void bench_working_set(FILE *js)
static double now_sec(void)
static int pow_mod(int base, int exp, int mod)
static uint32_t lcg_next(void)
static void bench_primitive(FILE *js)
static double chase_latency_ns(size_t bytes, long steps)
static uint64_t lcg_state