48#define WSIZE (8 * sizeof(int))
50#define bexchange(a, b, l) \
52 for(int _i = 0; _i < (l); _i++) { \
54 _c = *((char *)(a) + _i); \
55 *((char *)(a) + _i) = *((char *)(b) + _i); \
56 *((char *)(b) + _i) = _c; \
60#define exchange(a, b, t) \
71void errormsg1(
const char *f,
const char *msgname,
const char *msg);
99 errormsg1(
"SMAlloc",
"outofmem",
"out of memory space");
104 if (rows == 0 || cols == 0) {
109 result->
p = q = malloc(rows *
sizeof(
int *));
111 errormsg1(
"SMAlloc",
"outofmem",
"out of memory space");
114 result->
p_init = p = malloc(rows * cols *
sizeof(
int));
116 errormsg1(
"SMAlloc",
"outofmem",
"out of memory space");
119 for (i = 0; i < rows; i++) {
146#if defined(POLY_DEBUG) || defined(POLY_CH_DEBUG)
151 unsigned NbRows, NbColumns;
155 for (i = 0; i < NbRows; i++) {
157 for (j = 0; j < NbColumns; j++)
158 fprintf(stderr,
" %10X ", *p++);
159 fprintf(stderr,
"\n");
169 int *cp1, *cp2, *cp3;
175 for (i = 0; i < length; i++) {
186#define SMVector_Copy(p1, p2, length) \
187 memcpy((char *)(p2), (char *)(p1), (int)((length) * sizeof(int)))
192#define SMVector_Init(p1, length) \
193 memset((char *)(p1), 0, (int)((length) * sizeof(int)))
208 Value abs_a1, abs_a2, neg_a1;
263 int i, j, sat_nbcolumns;
264 unsigned jx1, jx2, bx1, bx2;
268 sat_nbcolumns = (Mat->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
275 for (i = 0, jx1 = 0, bx1 =
MSB; i < Ray->
NbRows; i++) {
276 for (j = 0, jx2 = 0, bx2 =
MSB; j < Mat->
NbRows; j++) {
277 if (
Sat->
p[j][jx1] & bx1)
278 result->
p[i][jx2] |= bx2;
301 int *equal_bound,
int *sup_bound,
unsigned RowSize1,
302 unsigned RowSize2,
unsigned bx,
unsigned jx) {
304 Value **uni_eq, **uni_sup, **uni_inf;
305 int **inc_eq, **inc_sup, **inc_inf;
316 *sup_bound = *equal_bound = NbBid;
317 uni_sup = uni_eq = Ray->
p + NbBid;
318 inc_sup = inc_eq =
Sat->
p + NbBid;
320 uni_inf = Ray->
p + NbRay;
321 inc_inf =
Sat->
p + NbRay;
323 while (inf_bound > *sup_bound) {
325 if (inc_eq != inc_sup) {
336 *((*inc_sup) + jx) |= bx;
343 if (inc_inf != inc_sup) {
366 Sat->
p = realloc(
Sat->
p, rows *
sizeof(
int *));
368 errormsg1(
"SatMatrix_Extend",
"outofmem",
"out of memory space");
373 errormsg1(
"SatMatrix_Extend",
"outofmem",
"out of memory space");
376 for (i = 0; i < rows; ++i)
393 unsigned NbMaxRays,
unsigned FirstConstraint,
395 unsigned NbRay, Dimension, NbConstraints, RowSize1, RowSize2, sat_nbcolumns;
396 int sup_bound, equal_bound, index_non_zero, bound;
397 int i, j, k, l, redundant, rayonly, nbcommonconstraints;
404 fprintf(stderr,
"[Chernikova: Input]\nRay = ");
406 fprintf(stderr,
"\nConstraints = ");
408 fprintf(stderr,
"\nSat = ");
412 NbConstraints = Mat->
NbRows;
417 RowSize1 = (Dimension + 1);
418 RowSize2 = sat_nbcolumns *
sizeof(int);
420 Temp = malloc(RowSize2);
422 errormsg1(
"Chernikova",
"outofmem",
"out of memory space");
435 jx = FirstConstraint /
WSIZE;
437 bx >>= FirstConstraint %
WSIZE;
438 for (k = FirstConstraint; k < NbConstraints; k++) {
445 index_non_zero = NbRay;
446 for (i = 0; i < NbRay; i++) {
455 for (j = 1; j < Dimension; j++) {
466 fprintf(stderr,
"[Chernikova: A]\nRay = ");
468 fprintf(stderr,
"\nConstraints = ");
470 fprintf(stderr,
"\nSat = ");
475 if (index_non_zero < NbBid) {
478 if (NbBid != index_non_zero)
482 fprintf(stderr,
"************\n");
483 for (i = 0; i < RowSize1; i++) {
486 fprintf(stderr,
"\n******\n");
487 for (i = 0; i < RowSize1; i++) {
490 fprintf(stderr,
"\n*******\n");
494 for (i = 0; i < NbBid; i++)
496 Combine(Ray->
p[i], Ray->
p[NbBid], Ray->
p[i], 0, Dimension);
503 for (j = 0; j < Dimension + 1; j++) {
511 fprintf(stderr,
"[Chernikova: B]\nRay = ");
514 fprintf(stderr,
"\nConstraints = ");
516 fprintf(stderr,
"\nSat = ");
521 for (i = NbBid + 1; i < NbRay; i++)
523 Combine(Ray->
p[i], Ray->
p[NbBid], Ray->
p[i], 0, Dimension);
527 for (j = 0; j < sat_nbcolumns; j++) {
528 Sat->
p[NbBid][j] = 0;
531 Sat->
p[NbBid][jx] |= bx;
533 if (--NbRay != NbBid) {
540 fprintf(stderr,
"[Chernikova: C]\nRay = ");
543 fprintf(stderr,
"\nConstraints = ");
545 fprintf(stderr,
"\nSat = ");
550 RaySort(Ray,
Sat, NbBid, NbRay, &equal_bound, &sup_bound, RowSize1,
564 fprintf(stderr,
"[Chernikova: D]\nRay = ");
567 fprintf(stderr,
"\nConstraints = ");
569 fprintf(stderr,
"\nSat = ");
575 for (i = equal_bound; i < sup_bound;
577 for (j = sup_bound; j < bound; j++) {
584 nbcommonconstraints = 0;
585 for (l = 0; l < jx; l++) {
586 aux = Temp[l] =
Sat->
p[i][l] |
Sat->
p[j][l];
587 for (
m =
MSB;
m != 0;
m >>= 1)
589 nbcommonconstraints++;
591 aux = Temp[jx] =
Sat->
p[i][jx] |
Sat->
p[j][jx];
592 for (
m =
MSB;
m != bx;
m >>= 1)
594 nbcommonconstraints++;
598 nbcommonconstraints++;
603 if (nbcommonconstraints + NbBid >=
607 for (
m = NbBid;
m < bound;
m++)
608 if ((
m != i) && (
m != j)) {
620 for (l = 0; l <= jx; l++, ip2++, ip1++)
630 fprintf(stderr,
"[Chernikova: E]\nRay = ");
633 fprintf(stderr,
"\nConstraints = ");
635 fprintf(stderr,
"\nSat = ");
644 if (NbRay == NbMaxRays) {
649 nc = (Mat->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
654 Combine(Ray->
p[j], Ray->
p[i], Ray->
p[NbRay], 0, Dimension);
657 Sat->
p[NbRay][jx] &= ~bx;
672 sup_bound, equal_bound, bound, NbRay, Dimension);
676 fprintf(stderr,
"[Chernikova: F]:\nRay = ");
687 fprintf(stderr,
"i = %d\nj = %d \n", i, j);
689 while ((j < bound) && (i > bound)) {
697 fprintf(stderr,
"i = %d\nj = %d \n", i, j);
706 sup_bound, equal_bound, bound, NbRay, Dimension);
710 fprintf(stderr,
"[Chernikova: G]\nRay = ");
729 fprintf(stderr,
"[Chernikova: Output]\nRay = ");
731 fprintf(stderr,
"\nConstraints = ");
733 fprintf(stderr,
"\nSat = ");
740static int Gauss4(
Value **p,
int NbEq,
int NbRows,
int Dimension) {
741 int i, j, k, pivot, Rank;
742 int *column_index = NULL;
746 column_index = malloc(Dimension *
sizeof(
int));
748 errormsg1(
"Gauss",
"outofmem",
"out of memory space");
762 for (j = 1; j <= Dimension; j++) {
763 for (i = Rank; i < NbEq; i++)
783 for (i = pivot + 1; i < NbEq;
788 Combine(p[i], p[Rank], p[i], j, Dimension);
794 column_index[Rank] = j;
800 for (k = Rank - 1; k >= 0; k--) {
804 for (i = 0; i < k; i++) {
808 Combine(p[i], p[k], p[i], j, Dimension);
812 for (i = NbEq; i < NbRows; i++) {
816 Combine(p[i], p[k], p[i], j, Dimension);
822 free(column_index), column_index = NULL;
838 fprintf(stderr,
"[Gauss : Input]\nRay =");
845 fprintf(stderr,
"[Gauss : Output]\nRay =");
867 unsigned Dimension, sat_nbcolumns, NbRay, NbConstraints, RowSize2,
868 *Trace = NULL, *bx = NULL, *jx = NULL, Dim_RaySpace, b;
869 unsigned NbBid, NbUni, NbEq, NbIneq;
870 unsigned NbBid2, NbUni2, NbEq2, NbIneq2;
872 int aux, *temp2 = NULL;
880 NbConstraints = Mat->
NbRows;
881 RowSize2 = sat_nbcolumns *
sizeof(int);
886 temp2 = (
int *)calloc(sat_nbcolumns,
sizeof(
int));
893 bx = malloc(NbConstraints *
sizeof(
unsigned));
896 jx = malloc(NbConstraints *
sizeof(
unsigned));
924 for (j = 0; j < NbConstraints; j++) {
939 for (i = 0; i < NbRay; i++) {
954 fprintf(stderr,
"[Remove_redundants : Init]\nConstraints =");
956 fprintf(stderr,
"\nRays =");
972 fprintf(stderr,
" j = ");
975 for (j = 0; j < NbConstraints; j++) {
978 fprintf(stderr,
" %i ", j);
984 temp2[jx[j]] |= bx[j];
992 fprintf(stderr,
"[Remove_redundants : IntoStep1]\nConstraints =");
994 fprintf(stderr,
" j = %i \n", j);
1002 for (i = 0; i < NbRay; i++)
1003 if (!(
Sat->
p[i][jx[j]] & bx[j])) {
1017 if (j == NbConstraints)
1020 exchange(jx[j], jx[NbConstraints], aux);
1021 exchange(bx[j], bx[NbConstraints], aux);
1028 for (i = 0; i < NbRay; i++)
1029 if (!(
Sat->
p[i][jx[j]] & bx[j])) {
1042 Mat->
NbRows = NbConstraints;
1045 for (i = 0; i < NbRay; i++) {
1063 fprintf(stderr,
"[Remove_redundants : Step1]\nConstraints =");
1065 fprintf(stderr,
"\nRay =");
1076 for (i = 0; i < NbEq; i++) {
1083 k < NbConstraints &&
value_cmp_si(Mat->
p[k][0], NbRay) != 0; k++)
1085 if (k >= NbConstraints)
1093 for (; k > i; k--) {
1112 for (i = 0; i < NbEq; i++) {
1120 for (i = 0; i < NbEq; i++) {
1123 for (j = i + 1; j < NbEq; j++) {
1125 if (!(temp2[jx[j]] & bx[j]))
1136 Filter[jx[i]] |= bx[i];
1141 fprintf(stderr,
"[Remove_redundants : Step2]\nConstraints =");
1143 fprintf(stderr,
"\nRay =");
1154 NbEq2 =
Gauss(Mat, NbEq, Dimension);
1159 if (NbEq2 >= Dimension)
1163 fprintf(stderr,
"[Remove_redundants : Step3]\nConstraints =");
1165 fprintf(stderr,
"\nRay =");
1176 for (i = 0, k = NbRay; i < NbBid && k > i; i++) {
1181 while (--k > i &&
value_cmp_si(Ray->
p[k][0], NbConstraints + 1) != 0)
1192 fprintf(stderr,
"[Remove_redundants : Step4]\nConstraints =");
1194 fprintf(stderr,
"\nRay =");
1205 NbBid2 =
Gauss(Ray, NbBid, Dimension);
1208 fprintf(stderr,
"[Remove_redundants : After Gauss]\nRay =");
1214 if (NbBid2 >= Dimension) {
1215 errormsg1(
"RemoveRedundants",
"rmrdt",
"dimension error");
1220 Dim_RaySpace = Dimension - 1 - NbEq2 - NbBid2;
1223 fprintf(stderr,
"[Remove_redundants : Step5]\nConstraints =");
1225 fprintf(stderr,
"\nRay =");
1237 for (j = 0; j < NbConstraints; j++) {
1261 else if (Status < Dim_RaySpace)
1263 else if (Status == NbRay)
1272 fprintf(stderr,
"[Remove_redundants : Step6]\nConstraints =");
1274 fprintf(stderr,
"\nRay =");
1284 for (j = 0; j < NbRay; j++) {
1287 if (Status < Dim_RaySpace)
1289 else if (Status == NbConstraints + 1)
1310 Pol->
NbBid = NbBid2;
1320 fprintf(stderr,
"[Remove_redundants : Step7]\nConstraints =");
1322 fprintf(stderr,
"\nRay =");
1339 Trace = malloc(sat_nbcolumns *
sizeof(
unsigned));
1367 for (j = NbEq; j < NbConstraints; j++) {
1371 for (k = 0; k < sat_nbcolumns; k++)
1376 for (i = NbBid; i < NbRay; i++)
1380 if (!(
Sat->
p[i][jx[j]] & bx[j]))
1381 for (k = 0; k < sat_nbcolumns; k++)
1382 Trace[k] |=
Sat->
p[i][k];
1389 for (i = NbEq; i < NbConstraints; i++) {
1393 !(Trace[jx[i]] & bx[i])) {
1404 Filter[jx[j]] |= bx[j];
1409 free(Trace), Trace = NULL;
1412 fprintf(stderr,
"[Remove_redundants : Step8]\nConstraints =");
1414 fprintf(stderr,
"\nRay =");
1427 Trace = malloc(NbRay *
sizeof(
unsigned));
1454 for (i = NbBid; i < NbRay; i++) {
1461 for (k = NbBid; k < NbRay; k++)
1468 for (k = NbBid; k < NbRay; k++)
1475 for (j = NbEq; j < NbConstraints; j++)
1479 if (!(
Sat->
p[i][jx[j]] & bx[j]))
1480 for (k = NbBid; k < NbRay; k++)
1481 Trace[k] |=
Sat->
p[k][jx[j]] & bx[j];
1493 for (j = NbBid; j < NbRay; j++) {
1496 if (
value_one_p(Ray->
p[j][0]) && (i != j) && !Trace[j]) {
1515 if (aux >= Dim_RaySpace) {
1526 fprintf(stderr,
"[Remove_redundants : Step9]\nConstraints =");
1528 fprintf(stderr,
"\nRay =");
1539 Pol->
NbRays = NbBid2 + NbUni2;
1547 errormsg1(
"Remove_Redundants",
"outofmem",
"out of memory space");
1577 unsigned NbRows, NbColumns;
1583 errormsg1(
"Polyhedron_Alloc",
"outofmem",
"out of memory space");
1594 NbRows = NbConstraints + NbRays;
1595 NbColumns = Dimension + 2;
1597 q = malloc(NbRows *
sizeof(
Value *));
1599 errormsg1(
"Polyhedron_Alloc",
"outofmem",
"out of memory space");
1605 errormsg1(
"Polyhedron_Alloc",
"outofmem",
"out of memory space");
1609 Pol->
Ray = q + NbConstraints;
1611 for (i = 0; i < NbRows; i++) {
1636 for (; Pol; Pol = Next) {
1647 unsigned Dimension, NbConstraints, NbRays;
1652 fprintf(Dst,
"<null polyhedron>\n");
1659 fprintf(Dst,
"POLYHEDRON Dimension:%d\n", Pol->
Dimension);
1660 fprintf(Dst,
" Constraints:%d Equations:%d Rays:%d Lines:%d\n",
1662 fprintf(Dst,
"Constraints %d %d\n", NbConstraints, Dimension);
1664 for (i = 0; i < NbConstraints; i++) {
1669 fprintf(Dst,
"Inequality: [");
1671 fprintf(Dst,
"Equality: [");
1673 for (j = 1; j < Dimension; j++) {
1676 (void)fprintf(Dst,
" ]\n");
1679 (void)fprintf(Dst,
"Rays %d %d\n", NbRays, Dimension);
1680 for (i = 0; i < NbRays; i++) {
1689 fprintf(Dst,
"Vertex: [");
1691 fprintf(Dst,
"Ray: [");
1694 fprintf(Dst,
"Line: [");
1696 for (j = 1; j < Dimension - 1; j++) {
1702 fprintf(Dst,
" ]/");
1706 fprintf(Dst,
" ]\n");
1709 fprintf(Dst,
"UNION ");
1735 errormsg1(
"Empty_Polyhedron",
"outofmem",
"out of memory space");
1739 for (i = 0; i <= Dimension; i++) {
1744 Pol->
NbEq = Dimension + 1;
1768 errormsg1(
"Universe_Polyhedron",
"outofmem",
"out of memory space");
1778 Vector_Set(Pol->
Ray[0], 0, (Dimension + 1) * (Dimension + 2));
1779 for (i = 0; i <= Dimension; i++) {
1788 Pol->
NbBid = Dimension;
1801 for (i = NbEq; i < Constraints->
NbRows; ++i) {
1803 for (k = i + 1; k < Constraints->
NbRows; ++k) {
1804 for (j = 1; j < Constraints->
NbColumns - 1; ++j) {
1823 if (k < Constraints->NbRows)
1825 Constraints->
p[Constraints->
NbRows],
1847 for (row = NbEq; row < Constraints->
NbRows; ++row) {
1859 for (nrow = row + 1; nrow < Constraints->
NbRows; ++nrow) {
1864 for (k = d; k < Constraints->
NbColumns - 1; ++k) {
1866 Constraints->
p[nrow][1 + k]))
1870 if (
value_eq(Constraints->
p[row][1 + k], Constraints->
p[nrow][1 + k]))
1885 value_addto(tmp, Constraints->
p[row][1 + k], Constraints->
p[nrow][1 + k]);
1917 unsigned Dimension, nbcolumns;
1921 if (Dimension < 1) {
1922 errormsg1(
"Constraints2Polyhedron",
"invalidpoly",
1923 "invalid polyhedron dimension");
1929 if (Constraints->
NbRows == 0) {
1943 for (i = 0; i < Constraints->
NbRows; ++i)
1947 Dimension + 1, &tmp)) {
1962 Rank =
Gauss(Constraints, NbEq, Dimension);
1964 for (i = NbEq; i < Constraints->
NbRows; ++i)
1966 Dimension + 1, &tmp);
1976 if (Constraints->
NbRows > NbEq)
1986 if (Dimension > NbMaxRays)
1987 NbMaxRays = Dimension;
2001 errormsg1(
"Constraints2Polyhedron",
"outofmem",
"out of memory space");
2005 for (i = 0; i < Dimension; i++) {
2016 nbcolumns = (Constraints->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
2035 Chernikova(Constraints, Ray,
Sat, Dimension - 1, NbMaxRays, 0, 0);
2038 fprintf(stderr,
"[constraints2polyhedron]\nConstraints = ");
2040 fprintf(stderr,
"\nRay = ");
2042 fprintf(stderr,
"\nSat = ");
2053 fprintf(stderr,
"\nPol = ");
2070 unsigned NbConstraints, Dimension;
2078 errormsg1(
"Polyhedron2Constraints",
"outofmem",
"out of memory space");
2099 unsigned Dimension, nbcolumns;
2104 SatTranspose = NULL;
2118 if (Dimension > NbMaxConstrs)
2119 NbMaxConstrs = Dimension;
2124 errormsg1(
"Rays2Polyhedron",
"outofmem",
"out of memory space");
2130 for (i = 0; i < Dimension; i++) {
2139 nbcolumns = (Ray->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
2140 SatTranspose =
SMAlloc(NbMaxConstrs, nbcolumns);
2142 SatTranspose->NbRows = Dimension;
2145 fprintf(stderr,
"[ray2polyhedron: Before]\nRay = ");
2147 fprintf(stderr,
"\nConstraints = ");
2149 fprintf(stderr,
"\nSatTranspose = ");
2150 SMPrint(SatTranspose);
2171 Chernikova(Ray, Mat, SatTranspose, Dimension, NbMaxConstrs, 0, 1);
2174 fprintf(stderr,
"[ray2polyhedron: After]\nRay = ");
2176 fprintf(stderr,
"\nConstraints = ");
2178 fprintf(stderr,
"\nSatTranspose = ");
2179 SMPrint(SatTranspose);
2187 fprintf(stderr,
"\nSat =");
2191 SMFree(&SatTranspose), SatTranspose = NULL;
2200 fprintf(stderr,
"\nPol = ");
2252 unsigned NbMaxRays) {
2256 Value *p1, *p2, *p3;
2257 unsigned Dimension, NbRay, bx, nbcolumns;
2269 nbcolumns = (Mat->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
2275 for (k = 0; k < NbConstraints; k++) {
2276 for (i = 0; i < NbRay; i++) {
2284 for (j = 0; j < Dimension; j++) {
2290 for (j = 0; j < NbRay; j++) {
2295 Sat->
p[j][jx] |= bx;
2312 unsigned NbMaxRays) {
2315 Matrix *Mat = NULL, *Ray = NULL;
2317 unsigned NbRay, NbCon, Dimension;
2319 if (NbConstraints == 0)
2330 errormsg1(
"AddConstraints",
"outofmem",
"out of memory space");
2357 if (NbRay > NbMaxRays)
2362 errormsg1(
"AddConstraints",
"outofmem",
"out of memory space");
2376 errormsg1(
"AddConstraints",
"outofmem",
"out of memory space");
2380 Ray->NbRows = NbRay;
2425 for (i = 0; i < Pol2->
NbRays; i++) {
2428 p1 = Pol2->
Ray[i] + 1;
2431 for (j = 0; j < Dimension; j++) {
2490 for (p = PolDomain, PolDomain = (
Polyhedron *)0; p; p = pnext) {
2504 p_domain_end->
next = p;
2521 p_domain_end->
next = Pol;
2544 Matrix *Mat = NULL, *Ray = NULL;
2546 unsigned NbRay, NbCon, NbEle1, Dimension;
2567 for (i = 1; i < Dimension; i++)
2570 if (i == Dimension) {
2578 NbEle1 = NbCon * Dimension;
2584 if (NbRay > NbMaxRays)
2589 errormsg1(
"SubConstraint",
"outofmem",
"out of memory space");
2600 for (i = 1; i < Dimension; i++)
2603 for (i = 1; i < Dimension; i++)
2607 Mat->
p[NbCon][Dimension - 1]);
2612 errormsg1(
"SubConstraint",
"outofmem",
"out of memory space");
2618 Ray->NbRows = NbRay;
2649 unsigned NbMaxRays) {
2656 errormsg1(
"DomainIntersection",
"diffdim",
2657 "operation on different dimensions");
2665 for (p1 = Pol1; p1; p1 = p1->
next) {
2666 for (p2 = Pol2; p2; p2 = p2->
next) {
2686 unsigned NbRays, Dimension;
2694 errormsg1(
"Polyhedron2Rays",
"outofmem",
"out of memory space");
2708 unsigned NbMaxConstrs) {
2711 Matrix *Mat = NULL, *Ray = NULL;
2713 unsigned NbCon, NbRay, NbEle1, Dimension;
2736 NbEle1 = NbRay * Dimension;
2740 errormsg1(
"AddRays",
"outofmem",
"out of memory space");
2750 Vector_Copy(AddedRays, Ray->p_Init + NbEle1, NbAddedRays * Dimension);
2757 if (NbMaxConstrs < NbCon)
2758 NbMaxConstrs = NbCon;
2763 errormsg1(
"AddRays",
"outofmem",
"out of memory space");
2775 SatTranspose =
BuildSat(Ray, Mat, NbRay, NbMaxConstrs);
2779 Chernikova(Ray, Mat, SatTranspose, Pol->
NbEq, NbMaxConstrs, NbRay, 1);
2784 SMFree(&SatTranspose), SatTranspose = NULL;
2812 if (!Ray || Ray->
NbRows == 0)
2815 errormsg1(
"DomainAddRays",
"diffdim",
"operation on different dimensions");
2821 for (p1 = Pol; p1; p1 = p1->
next) {
2826 for (p2 = PolA; p2; p2 = p2->
next) {
2838 PolEndA = PolA = p3;
2841 PolEndA = PolEndA->
next;
2861 errormsg1(
"Polyhedron_Copy",
"outofmem",
"out of memory space");
2919 int *tmpC,
int NbRays,
int NbConstraints) {
2932 for (i = 0; i < NbRays; i++)
2934 if (
Sat->
p[i][kj] & kb)
2944 for (j = 0; j < NbConstraints; j++) {
2945 if ((tmpC[j] >= 0) && (
Sat->
p[i][jx] & bx))
2968 int i, j, k, jx, found;
2970 unsigned Dimension, NbRays, NbConstraints, bx, nc;
2971 int NbConstraintsLeft;
2972 int *tmpC = NULL, *tmpR = NULL, previous_size = 0;
2980 if (tmpC) free(tmpC);
2983 if (Pol2 && Pol2 != P2)
2985 if (Pol && Pol != Pol2 && Pol != P2)
2999 NbConstraintsLeft = 0;
3001 if (Filter[jx] & bx) {
3005 NbConstraintsLeft++;
3023 if(tmpC) free(tmpC);
3036 if(NbConstraints + NbRays > previous_size) {
3038 tmpC = realloc(tmpC, (NbConstraints + NbRays) *
sizeof(
int));
3040 errormsg1(
"FindSimple",
"outofmem",
"out of memory space");
3047 previous_size = NbConstraints + NbRays;
3049 tmpR = tmpC + NbConstraints;
3050 for(i = 0; i < NbConstraints + NbRays; i++) {
3055 nc = (NbConstraints - 1) / (
sizeof(
int) * 8) + 1;
3062 for (k = 0; k < NbConstraints; k++) {
3063 if (Filter[jx] & bx)
3066 for (i = 0; i < NbRays; i++) {
3071 Sat->
p[i][jx] |= bx;
3081 for (i = 0; i < NbRays; i++)
3083 if ((tmpR[i] + 1 == NbConstraintsLeft)) {
3088 for (k = 0; k < NbConstraints; k++) {
3089 if ((tmpC[k] >= 0) && ((
Sat->
p[i][jx] & bx) == 0)) {
3095 NbConstraintsLeft--;
3110 cmax = (NbRays * NbConstraints + 1);
3112 for (k = 0; k < NbConstraints; k++)
3114 if (cmax > tmpC[k]) {
3120 errormsg1(
"DomSimplify",
"logerror",
"logic error");
3126 NbConstraintsLeft--;
3134 if(tmpC) free(tmpC);
3148 unsigned *Filter,
unsigned NbMaxRays) {
3151 Matrix *Mat = NULL, *Ray = NULL;
3153 unsigned NbRay, NbCon, NbCon1, NbCon2, NbEle1, Dimension, notempty;
3170 NbCon = NbCon1 + NbCon2;
3172 NbEle1 = NbCon1 * Dimension;
3178 if (NbRay > NbMaxRays)
3184 errormsg1(
"SimplifyConstraints",
"outofmem",
"out of memory space");
3198 errormsg1(
"SimplifyConstraints",
"outofmem",
"out of memory space");
3202 Ray->NbRows = NbRay;
3217 if (Filter &&
emptyQ(Pol)) {
3242 unsigned ix, bx, NbEqn, NbEqn1, NbEqn2, NbEle2, Dimension;
3245 NbEqn1 = Pol1->
NbEq;
3246 NbEqn2 = Pol2->
NbEq;
3247 NbEqn = NbEqn1 + NbEqn2;
3249 NbEle2 = NbEqn2 * Dimension;
3253 errormsg1(
"SimplifyEqualities",
"outofmem",
"out of memory space");
3264 Gauss(Mat, NbEqn2, Dimension - 1);
3268 for (i = NbEqn2; i < NbEqn; i++) {
3269 for (j = 1; j < Dimension; j++) {
3292 unsigned NbMaxRays) {
3295 unsigned k, jx, bx, nbentries, NbConstraints, Dimension, NbCon, empty;
3302 errormsg1(
"DomSimplify",
"diffdim",
"operation on different dimensions");
3314 for (p2 = Pol2; p2; p2 = p2->
next)
3320 for (p1 = Pol1; p1; p1 = p1->
next) {
3330 nbentries = (NbConstraints + NbCon - 1) / (
sizeof(
int) * 8) + 1;
3333 Filter = malloc(nbentries *
sizeof(
int));
3335 errormsg1(
"DomSimplify",
"outofmem",
"out of memory space\n");
3345 for (p2 = Pol2; p2; p2 = p2->
next) {
3366 errormsg1(
"DomSimplify",
"outofmem",
"out of memory space\n");
3371 for (k = 0, jx = 0, bx =
MSB; k < NbConstraints; k++) {
3375 if (Filter[jx] & bx) {
3405 unsigned NbMaxRays) {
3408 unsigned k, jx, bx, nbentries, NbConstraints, Dimension, NbCon, empty;
3409 unsigned *Filter = NULL;
3410 Matrix *Constraints = NULL;
3424 if (!Pol1 || !Pol2) {
3430 "operation on different dimensions");
3444 for (p2 = Pol2; p2; p2 = p2->
next)
3450 for (p1 = Pol1; p1; p1 = p1->
next) {
3458 nbentries = (NbConstraints + NbCon - 1) / (
sizeof(
int) * 8) + 1;
3461 Filter = malloc(nbentries *
sizeof(
int));
3463 errormsg1(
"DomainSimplify",
"outofmem",
"out of memory space");
3473 for (p2 = Pol2; p2; p2 = p2->
next) {
3489 errormsg1(
"DomainSimplify",
"outofmem",
"out of memory space");
3494 for (k = 0, jx = 0, bx =
MSB; k < NbConstraints; k++) {
3498 if (Filter[jx] & bx) {
3515 free(Filter), Filter = NULL;
3531 unsigned NbMaxRays) {
3533 Polyhedron *PolA, *PolEndA, *PolB, *PolEndB, *p1, *p2;
3541 errormsg1(
"DomainUnion",
"diffdim",
"operation on different dimensions");
3547 for (p1 = Pol1; p1; p1 = p1->
next) {
3551 for (p2 = Pol2; p2; p2 = p2->
next) {
3565 PolEndA = PolEndA->
next;
3572 for (p2 = Pol2; p2; p2 = p2->
next) {
3576 for (p1 = PolA; p1; p1 = p1->
next) {
3589 PolEndB = PolEndB->
next;
3596 PolEndA->
next = PolB;
3624 for (p = Pol->
next; p; p = p->
next) {
3642 unsigned NbMaxRays) {
3652 errormsg1(
"DomainDifference",
"diffdim",
3653 "operation on different dimensions");
3663 for (p2 = Pol2; p2; p2 = p2->
next) {
3664 for (p1 = Pol1; p1; p1 = p1->
next) {
3707 Polyhedron *p = NULL, **next, *result = NULL;
3720 if (align_dimension < Pol->Dimension) {
3721 errormsg1(
"align_context",
"diffdim",
"context dimension exceeds data");
3725 if (align_dimension == Pol->
Dimension) {
3735 for (; Pol; Pol = Pol->
next) {
3740 unsigned NbRays = have_rays ? Pol->
NbRays + k : 0;
3745 "context not of uniform dimension");
3752 for (i = 0; i < NbCons; ++i) {
3762 for (i = 0; i < k; ++i)
3764 for (i = 0; i < Pol->
NbRays; ++i) {
3813 errormsg1(
"Polyhedron_Scan",
"outofmem",
"out of memory space");
3823 for (i = 0; i < dim; i++) {
3825 for (j = i + 1; j < dim; j++) {
3828 Mat->
NbRows = dim - i - 1;
3992 unsigned Dimension1, Dimension2;
4007 for (i = 0; i < NbRays; i++) {
4009 for (j = 0; j < Dimension2; j++) {
4011 for (k = 0; k < Dimension1; k++) {
4034 unsigned Dimension1, Dimension2;
4049 for (i = 0; i < NbRays; i++) {
4051 for (j = 0; j < Dimension2; j++) {
4053 for (k = 0; k < Dimension1; k++) {
4077 unsigned NbMaxRays) {
4079 Matrix *Constraints = NULL;
4081 unsigned NbConstraints, Dimension1, Dimension2;
4097 if (Dimension1 != (Func->
NbRows)) {
4098 errormsg1(
"Polyhedron_Preimage",
"dimincomp",
"incompatible dimensions");
4111 Constraints =
Matrix_Alloc(NbConstraints, Dimension2 + 1);
4113 errormsg1(
"Polyhedron_Preimage",
"outofmem",
"out of memory space\n");
4148 if (!Pol || !Func) {
4153 for (p = Pol; p; p = p->
next) {
4168 unsigned NbMaxConstrs) {
4172 unsigned NbRays, Dimension1, Dimension2;
4188 Dimension2 = Func->
NbRows;
4190 errormsg1(
"Polyhedron_Image",
"dimincomp",
"incompatible dimensions");
4204 if (Dimension1 == Dimension2) {
4210 errormsg1(
"Polyhedron_Image",
"outofmem",
"out of memory space\n");
4221 errormsg1(
"Polyhedron_Image",
"outofmem",
"out of memory space\n");
4243 errormsg1(
"Polyhedron_Image",
"outofmem",
"out of memory space\n");
4277 if (!Pol || !Func) {
4282 for (p = Pol; p; p = p->
next) {
4305 int i, j, NbRay, Dim;
4306 Value *p1, *p2, p3, d, status;
4307 Value tmp1, tmp2, tmp3;
4339 errormsg1(
"DomainCost",
"outofmem",
"out of memory space\n");
4365 for (i = 0; i < NbRay; i++) {
4375 for (j = 1; j < Dim; j++) {
4449 unsigned NbMaxRays) {
4459 errormsg1(
"DomainAddConstraints",
"diffdim",
4460 "operation on different dimensions");
4466 for (p1 = Pol; p1; p1 = p1->
next) {
4471 for (p2 = PolA; p2; p2 = p2->
next) {
4483 PolEndA = PolA = p3;
4486 PolEndA = PolEndA->
next;
4504 Polyhedron *lP, *tmp, *Result, *lR, *prec, *reste;
4505 Polyhedron *p1, *p2, *p3, *Pol1, *dx, *d1, *d2, *pi, *newpi;
4508 if (flag != 0 && flag != 1) {
4509 errormsg1(
"Disjoint_Domain",
"invalidarg",
4510 "flag should be equal to 0 or 1");
4520 for (lP = P; lP; lP = lP->
next) {
4525 while (lR && reste) {
4528 for (p1 = reste; p1; p1 = p1->
next) {
4555 for (p1 = reste; p1; p1 = p1->
next) {
4580 if (newpi &&
emptyQ(newpi)) {
4594 for (p2 = reste; p2; p2 = p2->
next) {
4596 for (p1 = Pol1; p1; p1 = p1->
next) {
4624 if (newpi &&
emptyQ(newpi)) {
4658 for (tmp = d2; tmp->
next; tmp = tmp->
next)
4667 for (tmp = dx; tmp->
next; tmp = tmp->
next)
4676 errormsg1(
"Disjoint_Domain",
"internalerror",
"internal error");
4689 for (tmp = reste; tmp; tmp = tnext) {
4708 for (j = 0; j < Pol->
Dimension + 2; j++)
4717 for (Q = Pol; Q; Q = Q->
next)
4761 if(!P)
return(NULL);
4770 for (prev = &R; P; P = N) {
4775 if (
emptyQ(T) && prev != &R) {
void ExchangeRows(Matrix *M, int Row1, int Row2)
Matrix * Matrix_Copy(Matrix const *Src)
#define value_oppose(ref, val)
#define value_sub_int(ref, val, vint)
#define value_abs_eq(v1, v2)
#define VALUE_TO_INT(val)
#define value_notzero_p(val)
#define value_divexact(ref, val1, val2)
#define value_gcd(ref, val1, val2)
#define value_notone_p(val)
#define value_abs_ne(v1, v2)
#define value_absolute(ref, val)
#define value_add_int(ref, val, vint)
#define value_decrement(ref, val)
#define value_abs_lt(v1, v2)
#define value_zero_p(val)
#define value_assign(v1, v2)
#define value_increment(ref, val)
#define value_set_si(val, i)
#define value_addmul(ref, val1, val2)
#define value_division(ref, val1, val2)
#define value_multiply(ref, val1, val2)
#define value_print(Dst, fmt, val)
#define value_modulus(ref, val1, val2)
#define value_addto(ref, val1, val2)
#define value_cmp_si(val, n)
unsigned int any_exception_error
void free_exception_stack()
void Matrix_Extend(Matrix *Mat, unsigned NbRows)
Matrix * Matrix_Alloc(unsigned NbRows, unsigned NbColumns)
int Matrix_Inverse(Matrix *Mat, Matrix *MatInv)
void Matrix_Print(FILE *Dst, const char *Format, Matrix *Mat)
void Matrix_Free(Matrix *Mat)
void PolyPrint(Polyhedron *Pol)
static void SortConstraints(Matrix *Constraints, unsigned NbEq)
int PolyhedronIncludes(Polyhedron *Pol1, Polyhedron *Pol2)
static Polyhedron * Remove_Redundants(Matrix *Mat, Matrix *Ray, SatMatrix *Sat, unsigned *Filter)
Polyhedron * DomainAddRays(Polyhedron *Pol, Matrix *Ray, unsigned NbMaxConstrs)
Add rays pointed by 'Ray' to each and every polyhedron in the polyhedral domain 'Pol'.
void errormsg1(const char *f, const char *msgname, const char *msg)
Polyhedron * align_context(Polyhedron *Pol, int align_dimension, int NbMaxRays)
void Domain_PrintConstraints(FILE *Dst, const char *Format, Polyhedron *Pol)
Polyhedron * Polyhedron_Image(Polyhedron *Pol, Matrix *Func, unsigned NbMaxConstrs)
Polyhedron * Disjoint_Domain(Polyhedron *P, int flag, unsigned NbMaxRays)
static int Gauss4(Value **p, int NbEq, int NbRows, int Dimension)
#define SMVector_Copy(p1, p2, length)
void Polyhedron_PrintConstraints(FILE *Dst, const char *Format, Polyhedron *Pol)
Polyhedron * AddRays(Value *AddedRays, unsigned NbAddedRays, Polyhedron *Pol, unsigned NbMaxConstrs)
Add 'NbAddedRays' rays to polyhedron 'Pol'.
Polyhedron * Stras_DomainSimplify(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
static void Rays_Mult(Value **A, Matrix *B, Value **C, unsigned NbRays)
#define SMVector_Init(p1, length)
static void SMFree(SatMatrix **matrix)
static void SatMatrix_Extend(SatMatrix *Sat, unsigned rows, unsigned cols)
Polyhedron * DomainAddConstraints(Polyhedron *Pol, Matrix *Mat, unsigned NbMaxRays)
static void addToFilter(int k, unsigned *Filter, SatMatrix *Sat, int *tmpR, int *tmpC, int NbRays, int NbConstraints)
void Polyhedron_Free(Polyhedron *Pol)
void Polyhedron_Compute_Dual(Polyhedron *P)
Polyhedron * DomainConvex(Polyhedron *Pol, unsigned NbMaxConstrs)
static SatMatrix * SMAlloc(int rows, int cols)
Polyhedron * Empty_Polyhedron(unsigned Dimension)
Polyhedron * DomainConstraintSimplify(Polyhedron *P, unsigned MaxRays)
static int Chernikova(Matrix *Mat, Matrix *Ray, SatMatrix *Sat, unsigned NbBid, unsigned NbMaxRays, unsigned FirstConstraint, unsigned dual)
int Gauss(Matrix *Mat, int NbEq, int Dimension)
Polyhedron * Polyhedron_Scan(Polyhedron *D, Polyhedron *C, unsigned NbMaxRays)
Polyhedron * DomainDifference(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
Polyhedron * AddPolyToDomain(Polyhedron *Pol, Polyhedron *PolDomain)
Polyhedron * Polyhedron_Preimage(Polyhedron *Pol, Matrix *Func, unsigned NbMaxRays)
Polyhedron * DomainIntersection(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
Interval * DomainCost(Polyhedron *Pol, Value *Cost)
Polyhedron * Polyhedron_Copy(Polyhedron *Pol)
#define exchange(a, b, t)
Polyhedron * Domain_Copy(Polyhedron *Pol)
static void FindSimple(Polyhedron *P1, Polyhedron *P2, unsigned *Filter, unsigned NbMaxRays)
Polyhedron * DomainPreimage(Polyhedron *Pol, Matrix *Func, unsigned NbMaxRays)
Polyhedron * DomainSimplify(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
Polyhedron * Universe_Polyhedron(unsigned Dimension)
static int SimplifyConstraints(Polyhedron *Pol1, Polyhedron *Pol2, unsigned *Filter, unsigned NbMaxRays)
static SatMatrix * TransformSat(Matrix *Mat, Matrix *Ray, SatMatrix *Sat)
Polyhedron * Rays2Polyhedron(Matrix *Ray, unsigned NbMaxConstrs)
Given a matrix of rays 'Ray', create and return a polyhedron.
Polyhedron * DomainUnion(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
int lower_upper_bounds(int pos, Polyhedron *P, Value *context, Value *LBp, Value *UBp)
void polylib_close()
Free all cache allocated memory: call this function before exiting in a memory checker environment li...
#define bexchange(a, b, l)
static void SatVector_OR(int *p1, int *p2, int *p3, unsigned length)
Matrix * Polyhedron2Constraints(Polyhedron *Pol)
Polyhedron * DomainImage(Polyhedron *Pol, Matrix *Func, unsigned NbMaxConstrs)
Polyhedron * Polyhedron_Alloc(unsigned Dimension, unsigned NbConstraints, unsigned NbRays)
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Polyhedron * AddConstraints(Value *Con, unsigned NbConstraints, Polyhedron *Pol, unsigned NbMaxRays)
void Polyhedron_Print(FILE *Dst, const char *Format, const Polyhedron *Pol)
static void Rays_Mult_Transpose(Value **A, Matrix *B, Value **C, unsigned NbRays)
void Domain_Free(Polyhedron *Pol)
static void SimplifyEqualities(Polyhedron *Pol1, Polyhedron *Pol2, unsigned *Filter)
Polyhedron * SubConstraint(Value *Con, Polyhedron *Pol, unsigned NbMaxRays, int Pass)
static void Combine(Value *p1, Value *p2, Value *p3, int pos, unsigned length)
static int ImplicitEqualities(Matrix *Constraints, unsigned NbEq)
static void RaySort(Matrix *Ray, SatMatrix *Sat, int NbBid, int NbRay, int *equal_bound, int *sup_bound, unsigned RowSize1, unsigned RowSize2, unsigned bx, unsigned jx)
static Polyhedron * p_simplify_constraints(Polyhedron *P, Vector *row, Value *g, unsigned MaxRays)
static SatMatrix * BuildSat(Matrix *Mat, Matrix *Ray, unsigned NbConstraints, unsigned NbMaxRays)
Matrix * Polyhedron2Rays(Polyhedron *Pol)
Given a polyhedron 'Pol', return a matrix of rays.
#define POL_ENSURE_VERTICES(P)
#define POL_ENSURE_INEQUALITIES(P)
#define POL_ENSURE_POINTS(P)
#define POL_ENSURE_FACETS(P)
#define POL_ISSET(flags, f)
Value * value_alloc(int want, int *got)
void Vector_Free(Vector *vector)
void Vector_Scale(Value *p1, Value *p2, Value lambda, unsigned length)
void Vector_Set(Value *p, int n, unsigned length)
void Inner_Product(Value *p1, Value *p2, unsigned length, Value *ip)
void value_free(Value *p, int size)
void Vector_Combine(Value *p1, Value *p2, Value *p3, Value lambda, Value mu, unsigned length)
void Vector_Gcd(Value *p, unsigned length, Value *min)
void Vector_Exchange(Value *p1, Value *p2, unsigned length)
void Vector_AntiScale(Value *p1, Value *p2, Value lambda, unsigned length)
void Vector_Normalize(Value *p, unsigned length)
int Vector_Equal(Value *Vec1, Value *Vec2, unsigned length)
Vector * Vector_Alloc(unsigned length)
void Vector_Copy(Value *p1, Value *p2, unsigned length)
int First_Non_Zero(Value *p, unsigned length)
void Vector_Oppose(Value *p1, Value *p2, unsigned len)
int ConstraintSimplify(Value *old, Value *newp, int len, Value *v)