aboutsummaryrefslogtreecommitdiff
path: root/src/symcoord.c
blob: 202a4bf89a50a5eacaf4f78e07ef24d1216abc5c (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include "symcoord.h"

/* These constants have been computed generating the respective SymData */
#define CLASSES_CP_16        2768
#define CLASSES_EOFBEPOS_16  64430

static uint64_t    index_cp_sym16(Cube cube);
static uint64_t    index_eofbepos_sym16(Cube cube);
static uint64_t    index_drud_sym16(Cube cube);
static uint64_t    index_drudfin_noE_sym16(Cube cube);
static uint64_t    index_nxopt31(Cube cube);

static uint64_t    move_cp_sym16(Move m, uint64_t ind);
static uint64_t    move_eofbepos_sym16(Move m, uint64_t ind);
static uint64_t    move_drud_sym16(Move m, uint64_t ind);
static uint64_t    move_drudfin_noE_sym16(Move m, uint64_t ind);
static uint64_t    move_nxopt31(Move m, uint64_t ind);

static int         tfind_from_mask(uint64_t mask, Trans *ret);
static int         tfind_drud_sym16(uint64_t ind, Trans *ret);
static int         tfind_drudfin_noE_sym16(uint64_t ind, Trans *ret);
static int         tfind_nxopt31(uint64_t ind, Trans *ret);

static uint64_t    transform_cp(Trans t, uint64_t ind);
static uint64_t    transform_eofbepos(Trans t, uint64_t ind);
static uint64_t    transform_drud_sym16(Trans t, uint64_t ind);
static uint64_t    transform_drudfin_noE_sym16(Trans t, uint64_t ind);
static uint64_t    transform_nxopt31(Trans t, uint64_t ind);

static void        gensym(SymData *sd);
static void        init_symc_moves();
static void        init_symc_trans();
static bool        read_symdata_file(SymData *sd);
static bool        write_symdata_file(SymData *sd);

/* Some tables ***************************************************************/

static uint64_t    move_cp_16[NMOVES][CLASSES_CP_16];
static uint64_t    move_eofbepos_16[NMOVES][CLASSES_EOFBEPOS_16];

static int         trans_eofbepos[NTRANS][POW2TO11*BINOM12ON4];
static int         trans_epud[NTRANS][FACTORIAL8];
static int         trans_cpud_separate[NTRANS][BINOM8ON4];

static Trans       ttrep_move_cp_16[NMOVES][CLASSES_CP_16];
static Trans       ttrep_move_eofbepos_16[NMOVES][CLASSES_EOFBEPOS_16];


/* Transformation groups and symmetry data ***********************************/

static Trans
trans_group_udfix[16] = {
	uf, ur, ub, ul,
	df, dr, db, dl,
	uf_mirror, ur_mirror, ub_mirror, ul_mirror,
	df_mirror, dr_mirror, db_mirror, dl_mirror,
};

static SymData
sd_cp_16 = {
	.filename  = "sd_cp_16_new",
	.coord     = &coord_cp,
	.sym_coord = &coord_cp_sym16,
	.ntrans    = 16,
	.trans     = trans_group_udfix,
	.transform = transform_cp,
};

static SymData
sd_eofbepos_16 = {
	.filename  = "sd_eofbepos_16_new",
	.coord     = &coord_eofbepos,
	.sym_coord = &coord_eofbepos_sym16,
	.ntrans    = 16,
	.trans     = trans_group_udfix,
	.transform = transform_eofbepos,
};

SymData * all_sd[] = {
	&sd_cp_16,
	&sd_eofbepos_16,
	NULL
};


/* Coordinates and their implementation **************************************/

Coordinate
coord_eofbepos_sym16 = {
	.index     = index_eofbepos_sym16,
	.move      = move_eofbepos_sym16,
};

Coordinate
coord_cp_sym16 = {
	.index     = index_cp_sym16,
	.move      = move_cp_sym16,
};

Coordinate
coord_drud_sym16 = {
	.index     = index_drud_sym16,
	.move      = move_drud_sym16,
	.max       = POW3TO7 * CLASSES_EOFBEPOS_16,
	.transform = transform_drud_sym16,
	.tfind     = tfind_drud_sym16,
};

Coordinate
coord_drudfin_noE_sym16 = {
	.index     = index_drudfin_noE_sym16,
	.move      = move_drudfin_noE_sym16,
	.max       = FACTORIAL8 * CLASSES_CP_16,
	.transform = transform_drudfin_noE_sym16,
	.tfind     = tfind_drudfin_noE_sym16,
};

Coordinate
coord_nxopt31 = {
	.index     = index_nxopt31,
	.move      = move_nxopt31,
	.max       = POW3TO7 * BINOM8ON4 * CLASSES_EOFBEPOS_16,
	.transform = transform_nxopt31,
	.tfind     = tfind_nxopt31,
};

/* Functions *****************************************************************/

static uint64_t
index_cp_sym16(Cube cube)
{
	return sd_cp_16.class[coord_cp.index(cube)];
}

static uint64_t
index_drud_sym16(Cube cube)
{
	Trans t;

	t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)];

	return index_eofbepos_sym16(cube) * POW3TO7 + co_ttable[t][cube.coud];
}

static uint64_t
index_drudfin_noE_sym16(Cube cube)
{
	Trans t;
	Cube c;

	t = sd_cp_16.transtorep[coord_cp.index(cube)];
	c = apply_trans(t, cube);

	/* TODO: add transform to coord_epud to make this faster */
	return index_cp_sym16(c) * FACTORIAL8 + coord_epud.index(c);
}

static uint64_t
index_eofbepos_sym16(Cube cube)
{
	return sd_eofbepos_16.class[coord_eofbepos.index(cube)];
}

static uint64_t
index_nxopt31(Cube cube)
{
	Trans t;
	uint64_t a;
	int coud, cp;
	
	t = sd_eofbepos_16.transtorep[coord_eofbepos.index(cube)];
	coud = co_ttable[t][cube.coud];
	cp   = cp_ttable[t][cube.cp];
	a = (index_eofbepos_sym16(cube)*POW3TO7) + coud;

	return a * BINOM8ON4 + coord_cpud_separate.index((Cube){.cp = cp});
}

static uint64_t
move_cp_sym16(Move m, uint64_t ind)
{
	return move_cp_16[m][ind];
}

static uint64_t
move_eofbepos_sym16(Move m, uint64_t ind)
{
	return move_eofbepos_16[m][ind];
}

static uint64_t
move_drud_sym16(Move m, uint64_t ind)
{
	uint64_t coud, eofbepos;
	Trans ttr;

	eofbepos = move_eofbepos_16[m][ind / POW3TO7];
	ttr = ttrep_move_eofbepos_16[m][ind / POW3TO7];
	coud = coud_mtable[m][ind % POW3TO7];
	coud = co_ttable[ttr][coud]; /* Source is always coud */

	return eofbepos * POW3TO7 + coud;
}

static uint64_t
move_drudfin_noE_sym16(Move m, uint64_t ind)
{
	uint64_t cp, epud;
	Trans ttr;

	cp = move_cp_16[m][ind / FACTORIAL8];
	ttr = ttrep_move_cp_16[m][ind / FACTORIAL8];
	epud = coord_epud.move(m, ind % FACTORIAL8);
	epud = trans_epud[ttr][epud];

	return cp * FACTORIAL8 + epud;
}

static uint64_t
move_nxopt31(Move m, uint64_t ind)
{
	uint64_t eofbepos, cpsep, coud;
	Trans ttr;

	eofbepos = ind / (POW3TO7 * BINOM8ON4);
	coud = (ind / BINOM8ON4) % POW3TO7;
	cpsep = ind % BINOM8ON4;

	eofbepos = move_eofbepos_16[m][eofbepos];
	ttr = ttrep_move_eofbepos_16[m][eofbepos];
	coud = coud_mtable[m][coud];
	coud = co_ttable[ttr][coud]; /* Source is always coud */
	cpsep = coord_cpud_separate.move(m, cpsep);
	cpsep = trans_cpud_separate[ttr][cpsep];

	return (eofbepos * POW3TO7 + coud) * BINOM8ON4 + cpsep;
}

static uint64_t
transform_cp(Trans t, uint64_t ind)
{
	return cp_ttable[t][ind];
}

static uint64_t
transform_eofbepos(Trans t, uint64_t ind)
{
	return trans_eofbepos[t][ind];
}

static uint64_t
transform_drud_sym16(Trans t, uint64_t ind)
{
	uint64_t coud, eofbepos;

	eofbepos = ind / POW3TO7; /* Assum trans fixes eofbepos */
	coud = co_ttable[t][ind % POW3TO7]; /* Source is always coud */

	return eofbepos * POW3TO7 + coud;
}

static uint64_t
transform_drudfin_noE_sym16(Trans t, uint64_t ind)
{
	uint64_t cp, epud;

	cp = ind / FACTORIAL8; /* Assume trans fixes cp */
	epud = trans_epud[t][ind % FACTORIAL8];

	return cp * FACTORIAL8 + epud;
}

static uint64_t
transform_nxopt31(Trans t, uint64_t ind)
{
	uint64_t eofbepos, cpsep, coud;

	eofbepos = ind / (POW3TO7 * BINOM8ON4);
	coud = (ind / BINOM8ON4) % POW3TO7;
	cpsep = ind % BINOM8ON4;

	coud = co_ttable[t][coud]; /* Source is always coud */
	cpsep = trans_cpud_separate[t][cpsep];

	return (eofbepos * POW3TO7 + coud) * BINOM8ON4 + cpsep;
}

static int
tfind_from_mask(uint64_t mask, Trans *ret)
{
	Trans t;
	int i = 0;

	for (t = uf; t < NTRANS; t++)
		if (((uint64_t)1 << t) & mask)
			ret[i++] = t;

	return i;
}

static int
tfind_drud_sym16(uint64_t ind, Trans *ret)
{
	uint64_t mask = sd_eofbepos_16.selfsim[ind / POW3TO7];

	return tfind_from_mask(mask, ret);
}

static int
tfind_drudfin_noE_sym16(uint64_t ind, Trans *ret)
{
	uint64_t mask = sd_cp_16.selfsim[ind / FACTORIAL8];

	return tfind_from_mask(mask, ret);
}

static int
tfind_nxopt31(uint64_t ind, Trans *ret)
{
	uint64_t mask = sd_eofbepos_16.selfsim[ind / (POW3TO7 * BINOM8ON4)];

	return tfind_from_mask(mask, ret);
}

/* Other functions ***********************************************************/

void
free_sd(SymData *sd)
{
	if (sd->generated) {
		free(sd->class);
		free(sd->unsym);
		free(sd->transtorep);
	}

	sd->generated = false;
}

static void
gensym(SymData *sd)
{
	uint64_t i, in, nreps = 0;
	Trans t;
	int j;

	if (sd->generated)
		return;

	sd->class      = malloc(sd->coord->max * sizeof(uint64_t));
	sd->unsym      = malloc(sd->coord->max * sizeof(uint64_t));
	sd->transtorep = malloc(sd->coord->max * sizeof(Trans));
	sd->selfsim    = malloc(sd->coord->max * sizeof(uint64_t));

	if (read_symdata_file(sd)) {
		sd->generated = true;
		return;
	}

	fprintf(stderr, "Cannot load %s, generating it\n", sd->filename);

	for (i = 0; i < sd->coord->max; i++)
		sd->class[i] = sd->coord->max + 1;

	for (i = 0; i < sd->coord->max; i++) {
		if (sd->class[i] == sd->coord->max + 1) {
			sd->unsym[nreps] = i;
			sd->transtorep[i] = uf;
			sd->selfsim[nreps] = (uint64_t)0;
			for (j = 0; j < sd->ntrans; j++) {
				t = sd->trans[j];
				in = sd->transform(t, i);
				sd->class[in] = nreps;
				if (in == i)
					sd->selfsim[nreps] |=
					    ((uint64_t)1 << t);
				else
					sd->transtorep[in] = inverse_trans(t);
			}
			nreps++;
		}
	}

	sd->sym_coord->max = nreps;
	sd->unsym          = realloc(sd->unsym,   nreps * sizeof(uint64_t));
	sd->selfsim        = realloc(sd->selfsim, nreps * sizeof(uint64_t));
	sd->generated      = true;

	fprintf(stderr, "Found %" PRIu64 " classes\n", nreps);

	if (!write_symdata_file(sd))
		fprintf(stderr, "Error writing SymData file\n");

	return;
}

static bool
read_symdata_file(SymData *sd)
{
	init_env();

	FILE *f;
	char fname[strlen(tabledir)+100];
	uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max;
	bool r = true;

	strcpy(fname, tabledir);
	strcat(fname, "/");
	strcat(fname, sd->filename);

	if ((f = fopen(fname, "rb")) == NULL)
		return false;

	r = r && fread(&sd->sym_coord->max, sizeof(uint64_t), 1,   f) == 1;
	r = r && fread(sd->unsym,           sizeof(uint64_t), *sn, f) == *sn;
	r = r && fread(sd->selfsim,         sizeof(uint64_t), *sn, f) == *sn;
	r = r && fread(sd->class,           sizeof(uint64_t), n,   f) == n;
	r = r && fread(sd->transtorep,      sizeof(Trans),    n,   f) == n;

	fclose(f);
	return r;
}

/*
static int
selfsims(SymData *sd, uint64_t ind, Trans *ret)
{
	Cube cube, tcube;
	int i, n;
	uint64_t indnosym;

	cube = sd->sym_coord->cube(ind);
	indnosym = sd->coord->index(cube);
	n = 0;
	for (i = 0; i < sd->ntrans; i++) {
		tcube = apply_trans(sd->trans[i], cube);
		if (sd->coord->index(tcube) == indnosym)
			ret[n++] = sd->trans[i];
	}

	return n;
}
*/

static bool
write_symdata_file(SymData *sd)
{
	init_env();

	FILE *f;
	char fname[strlen(tabledir)+100];
	uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max;
	bool r = true;

	strcpy(fname, tabledir);
	strcat(fname, "/");
	strcat(fname, sd->filename);

	if ((f = fopen(fname, "wb")) == NULL)
		return false;

	r = r && fwrite(&sd->sym_coord->max, sizeof(uint64_t), 1,   f) == 1;
	r = r && fwrite(sd->unsym,           sizeof(uint64_t), *sn, f) == *sn;
	r = r && fwrite(sd->selfsim,         sizeof(uint64_t), *sn, f) == *sn;
	r = r && fwrite(sd->class,           sizeof(uint64_t), n,   f) == n;
	r = r && fwrite(sd->transtorep,      sizeof(Trans),    n,   f) == n;

	fclose(f);
	return r;
}

static void
init_symc_moves()
{
	uint64_t i, ii, coo;
	Move j;

	for (i = 0; i < CLASSES_CP_16; i++) {
		ii = sd_cp_16.unsym[i];
		for (j = 0; j < NMOVES; j++) {
			coo = sd_cp_16.coord->move(j, ii);
			move_cp_16[j][i] = sd_cp_16.class[coo];
			ttrep_move_cp_16[j][i] = sd_cp_16.transtorep[coo];
		}
	}

	for (i = 0; i < CLASSES_EOFBEPOS_16; i++) {
		ii = sd_eofbepos_16.unsym[i];
		for (j = 0; j < NMOVES; j++) {
			coo = sd_eofbepos_16.coord->move(j, ii);
			move_eofbepos_16[j][i] = sd_eofbepos_16.class[coo];
			ttrep_move_eofbepos_16[j][i] =
			    sd_eofbepos_16.transtorep[coo];
		}
	}
}

void
init_symc_trans()
{
	uint64_t i;
	int j, cp;
	int epe[4] = {FR, FL, BL, BR};
	int a[12] = { [8] = 8, [9] = 9, [10] = 10, [11] = 11 };
	Cube c;
	CubeArray *arr, *aux;
	Trans t;

	for (i = 0; i < POW2TO11*BINOM12ON4; i++) {
		for (j = 0; j < 16; j++) {
			t = trans_group_udfix[j];

			arr = new_cubearray((Cube){0}, pf_edges);
			int_to_sum_zero_array(i % POW2TO11, 2, 12, arr->eofb);
			epos_to_compatible_ep((i / POW2TO11)*24, arr->ep, epe);
			fix_eorleoud(arr);
			c = arrays_to_cube(arr, pf_edges);
			free_cubearray(arr, pf_edges);

			c = apply_trans(t, c);
			trans_eofbepos[t][i] = (c.epose/24)*POW2TO11 + c.eofb;
		}
	}

	aux = malloc(sizeof(CubeArray));
	aux->ep = a;
	for (i = 0; i < FACTORIAL8; i++) {
		index_to_perm(i, 8, a);
		c = arrays_to_cube(aux, pf_ep);
		for (j = 0; j < 16; j++) {
			t = trans_group_udfix[j];
			arr = new_cubearray(apply_trans(t, c), pf_ep);
			trans_epud[t][i] = perm_to_index(arr->ep, 8);
			free_cubearray(arr, pf_ep);
		}
	}
	free(aux);

	for (i = 0; i < BINOM8ON4; i++) {
		cp = cpud_separate_ant[i];
		for (j = 0; j < 16; j++) {
			t = trans_group_udfix[j];
			trans_cpud_separate[t][i] =
			    cpud_separate_ind[cp_ttable[t][cp]];
		}
	}
}

void
init_symcoord()
{
	int i;

	static bool initialized = false;
	if (initialized)
		return;
	initialized = true;

	init_coord();

	init_symc_trans();

	for (i = 0; all_sd[i] != NULL; i++)
		gensym(all_sd[i]);

	init_symc_moves();
}

Generated with cgit - Back to sebastiano.tronto.net