-
Notifications
You must be signed in to change notification settings - Fork 2
/
find2c.c
176 lines (162 loc) · 4.34 KB
/
find2c.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "clock.h"
/* we rely on mask_t to be an integer type
* with at least 2 ** MAX_DIMENSION bits
*/
#define MAX_DIMENSION (6)
typedef unsigned long long mask_t;
mask_t fullmask[MAX_DIMENSION + 1] = {
0x0000000000000001,
0x0000000000000003,
0x000000000000000f,
0x00000000000000ff,
0x000000000000ffff,
0x00000000ffffffff,
0xffffffffffffffff
};
unsigned int step = 0;
clock_t t0;
mpz_t sum[MAX_DIMENSION + 1];
mpz_t scratch[MAX_DIMENSION + 1];
void init_gmp(void) {
int i;
for (i = 0; i <= MAX_DIMENSION; ++i) {
mpz_init(sum[i]);
mpz_init(scratch[i]);
}
}
void clear_gmp(void) {
int i;
for (i = 0; i <= MAX_DIMENSION; ++i) {
mpz_clear(sum[i]);
mpz_clear(scratch[i]);
}
}
/*
* count_part(d, mask): set sum[d] to the number of ways the specified
* shape can be partitioned into polyominoes of size at most 2.
* The shape is some or all of a I<d>-dimensional hypercube of side 2;
* I<mask> is a vector of C<2 ** d> bits specifying the unit I<d>-cubes
* that are included in the shape.
*
* Overwrites scratch[0..d] and sum[0..d] during calculation.
*/
void count_part(int d, mask_t mask) {
mask_t left, right, shared, u;
if (d == 0) {
mpz_set_ui(sum[d], 1);
return;
}
left = mask & fullmask[d - 1];
right = mask >> (1 << (d - 1));
shared = left & right;
u = 0;
mpz_set_ui(sum[d], 0);
while (1) {
count_part(d - 1, left ^ u);
mpz_set(scratch[d], sum[d - 1]);
count_part(d - 1, right ^ u);
mpz_mul(scratch[d], scratch[d], sum[d - 1]);
mpz_add(sum[d], sum[d], scratch[d]);
u = (u - shared) & shared;
if (u == 0)
break;
}
}
/*
* count_full(d): same as count_part(d, fullmask[d])
*/
void count_full(int d) {
mask_t lim, u;
if (d == 0) {
mpz_set_ui(sum[d], 1);
return;
}
lim = fullmask[d - 1];
mpz_set_ui(sum[d], 0);
for (u = 0; u <= lim; ++u) {
count_part(d - 1, u);
mpz_mul(scratch[d], sum[d - 1], sum[d - 1]);
mpz_add(sum[d], sum[d], scratch[d]);
if ((++step & 0xfffff) == 0) {
gmp_printf("300 at (%d, %llx) sum is %Zu (%.2f)\n",
d, u, sum[d], difftime(t0, curtime()));
}
}
}
#define LIM4 (1 << (1 << 4))
unsigned int cache4[LIM4]; /* the sum of these is 41025, int is ample room */
void init_cache4(void) {
int u;
for (u = 0; u < LIM4; ++u) {
count_part(4, (mask_t)u);
cache4[u] = mpz_get_ui(sum[4]);
}
}
/*
* count_5_part(mask): same as count_part(5, mask)
*
* Uses a prepared cache of results for d=4, to minimize recursion.
*/
void count_5_part(mask_t mask) {
mask_t left, right, shared, u;
left = mask & fullmask[4];
right = mask >> (1 << 4);
shared = left & right;
u = 0;
mpz_set_ui(sum[5], 0);
while (1) {
mpz_add_ui(sum[5], sum[5], cache4[left ^ u] * cache4[right ^ u]);
u = (u - shared) & shared;
if (u == 0)
break;
}
}
/*
* count_6_full(): same as count_full(6)
*
* Uses a prepared cache of results for d=4 (via count_5_part()) to reduce
* recursion at a reasonable memory cost.
*/
void count_6_full(void) {
mask_t lim, u;
init_cache4();
lim = fullmask[5];
mpz_set_ui(sum[6], 0);
for (u = 0; u <= lim; ++u) {
count_5_part(u);
mpz_mul(scratch[6], sum[5], sum[5]);
mpz_add(sum[6], sum[6], scratch[6]);
if ((++step & 0x3ffffff) == 0) {
gmp_printf("300 at (%d, %llx) sum is %Zu (%.2f)\n",
6, u, sum[6], difftime(t0, curtime()));
}
}
}
/*
* Calculates the number of ways a I<d>-dimensional hypercube of side 2
* can be partitioned into polyominoes of size at most 2.
*/
int main(int argc, char** argv) {
int d;
if (argc != 2) {
fprintf(stderr, "usage: %s <dimension>\n", argv[0]);
return 1;
}
d = atoi(argv[1]);
if (d < 0 || d > MAX_DIMENSION) {
fprintf(stderr, "dimension must be in the range 0 to %u\n",
MAX_DIMENSION);
return 1;
}
setup_clock();
t0 = curtime();
init_gmp();
(d == 6) ? count_6_full() : count_full(d);
gmp_printf("200 a(2, %d) = %Zu (%.2f)\n",
d, sum[d], difftime(t0, curtime()));
clear_gmp();
return 0;
}