-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.c
More file actions
90 lines (78 loc) · 2.06 KB
/
cache.c
File metadata and controls
90 lines (78 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This is free and unencumbered software released into the public domain.
* Refer to LICENSE.txt in this directory. */
/* Randomized stress test of a sqrt(x) caching mechanism. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
unsigned char number;
unsigned char sqroot;
} cache_entry_t;
static cache_entry_t g_cache[100];
static int g_cache_size = sizeof(g_cache) / sizeof(g_cache[0]);
static void
cache_init(void)
{
int i;
for (i = 0; i < g_cache_size; ++i)
{
g_cache[i].number = 0;
g_cache[i].sqroot = 0;
}
}
static int
cache_calculate(int number)
{
int i;
for (i = 0; i < g_cache_size; ++i)
{
if (g_cache[i].number == number)
{
/* Cache hit. */
return g_cache[i].sqroot;
}
}
/* Cache miss. Find correct result and populate a few cache entries. */
int sqroot = 0;
int number_adj;
for (number_adj = number - 1; number_adj < number + 1; ++number_adj)
{
int sqroot_adj = (int)(sqrt(number_adj));
i = (int)(1.0 * g_cache_size * rand() / (RAND_MAX + 1.0));
g_cache[i].number = number_adj;
g_cache[i].sqroot = sqroot_adj;
if (number_adj == number)
{
/* This is our return value. */
sqroot = sqroot_adj;
}
}
return sqroot;
}
int
main(void)
{
cache_init();
/* Repeatedly check cache_calculate(). */
int i;
for (i = 0;; ++i)
{
if (i % 100 == 0)
{
printf("i=%i\n", i);
}
/* Check cache_calculate() with a random number. */
int number = (int)(256.0 * rand() / (RAND_MAX + 1.0));
int sqroot_cache = cache_calculate(number);
int sqroot_correct = (int)sqrt(number);
if (sqroot_cache != sqroot_correct)
{
/* cached_calculate() returned incorrect value. */
printf("i=%i: number=%i sqroot_cache=%i sqroot_correct=%i\n",
i, number, sqroot_cache, sqroot_correct);
abort();
}
}
return EXIT_SUCCESS;
}